1 //===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // MachineScheduler schedules machine instructions after phi elimination. It
11 // preserves LiveIntervals so it can be invoked before register allocation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/ADT/PriorityQueue.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/CodeGen/LiveInterval.h"
24 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineDominators.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineLoopInfo.h"
31 #include "llvm/CodeGen/MachineOperand.h"
32 #include "llvm/CodeGen/MachinePassRegistry.h"
33 #include "llvm/CodeGen/RegisterPressure.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/MachineScheduler.h"
36 #include "llvm/CodeGen/MachineValueType.h"
37 #include "llvm/CodeGen/Passes.h"
38 #include "llvm/CodeGen/RegisterClassInfo.h"
39 #include "llvm/CodeGen/ScheduleDAG.h"
40 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
41 #include "llvm/CodeGen/ScheduleDAGMutation.h"
42 #include "llvm/CodeGen/ScheduleDFS.h"
43 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
44 #include "llvm/CodeGen/SlotIndexes.h"
45 #include "llvm/CodeGen/TargetPassConfig.h"
46 #include "llvm/CodeGen/TargetSchedule.h"
47 #include "llvm/MC/LaneBitmask.h"
48 #include "llvm/Pass.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Compiler.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/GraphWriter.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/TargetInstrInfo.h"
56 #include "llvm/Target/TargetLowering.h"
57 #include "llvm/Target/TargetRegisterInfo.h"
58 #include "llvm/Target/TargetSubtargetInfo.h"
72 #define DEBUG_TYPE "misched"
76 cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden,
77 cl::desc("Force top-down list scheduling"));
78 cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden,
79 cl::desc("Force bottom-up list scheduling"));
81 DumpCriticalPathLength("misched-dcpl", cl::Hidden,
82 cl::desc("Print critical path length to stdout"));
84 } // end namespace llvm
87 static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden,
88 cl::desc("Pop up a window to show MISched dags after they are processed"));
90 /// In some situations a few uninteresting nodes depend on nearly all other
91 /// nodes in the graph, provide a cutoff to hide them.
92 static cl::opt<unsigned> ViewMISchedCutoff("view-misched-cutoff", cl::Hidden,
93 cl::desc("Hide nodes with more predecessor/successor than cutoff"));
95 static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden,
96 cl::desc("Stop scheduling after N instructions"), cl::init(~0U));
98 static cl::opt<std::string> SchedOnlyFunc("misched-only-func", cl::Hidden,
99 cl::desc("Only schedule this function"));
100 static cl::opt<unsigned> SchedOnlyBlock("misched-only-block", cl::Hidden,
101 cl::desc("Only schedule this MBB#"));
103 static bool ViewMISchedDAGs = false;
106 /// Avoid quadratic complexity in unusually large basic blocks by limiting the
107 /// size of the ready lists.
108 static cl::opt<unsigned> ReadyListLimit("misched-limit", cl::Hidden,
109 cl::desc("Limit ready list to N instructions"), cl::init(256));
111 static cl::opt<bool> EnableRegPressure("misched-regpressure", cl::Hidden,
112 cl::desc("Enable register pressure scheduling."), cl::init(true));
114 static cl::opt<bool> EnableCyclicPath("misched-cyclicpath", cl::Hidden,
115 cl::desc("Enable cyclic critical path analysis."), cl::init(true));
117 static cl::opt<bool> EnableMemOpCluster("misched-cluster", cl::Hidden,
118 cl::desc("Enable memop clustering."),
121 static cl::opt<bool> VerifyScheduling("verify-misched", cl::Hidden,
122 cl::desc("Verify machine instrs before and after machine scheduling"));
124 // DAG subtrees must have at least this many nodes.
125 static const unsigned MinSubtreeSize = 8;
127 // Pin the vtables to this file.
128 void MachineSchedStrategy::anchor() {}
130 void ScheduleDAGMutation::anchor() {}
132 //===----------------------------------------------------------------------===//
133 // Machine Instruction Scheduling Pass and Registry
134 //===----------------------------------------------------------------------===//
136 MachineSchedContext::MachineSchedContext() {
137 RegClassInfo = new RegisterClassInfo();
140 MachineSchedContext::~MachineSchedContext() {
146 /// Base class for a machine scheduler class that can run at any point.
147 class MachineSchedulerBase : public MachineSchedContext,
148 public MachineFunctionPass {
150 MachineSchedulerBase(char &ID): MachineFunctionPass(ID) {}
152 void print(raw_ostream &O, const Module* = nullptr) const override;
155 void scheduleRegions(ScheduleDAGInstrs &Scheduler, bool FixKillFlags);
158 /// MachineScheduler runs after coalescing and before register allocation.
159 class MachineScheduler : public MachineSchedulerBase {
163 void getAnalysisUsage(AnalysisUsage &AU) const override;
165 bool runOnMachineFunction(MachineFunction&) override;
167 static char ID; // Class identification, replacement for typeinfo
170 ScheduleDAGInstrs *createMachineScheduler();
173 /// PostMachineScheduler runs after shortly before code emission.
174 class PostMachineScheduler : public MachineSchedulerBase {
176 PostMachineScheduler();
178 void getAnalysisUsage(AnalysisUsage &AU) const override;
180 bool runOnMachineFunction(MachineFunction&) override;
182 static char ID; // Class identification, replacement for typeinfo
185 ScheduleDAGInstrs *createPostMachineScheduler();
188 } // end anonymous namespace
190 char MachineScheduler::ID = 0;
192 char &llvm::MachineSchedulerID = MachineScheduler::ID;
194 INITIALIZE_PASS_BEGIN(MachineScheduler, "machine-scheduler",
195 "Machine Instruction Scheduler", false, false)
196 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
197 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
198 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
199 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
200 INITIALIZE_PASS_END(MachineScheduler, "machine-scheduler",
201 "Machine Instruction Scheduler", false, false)
203 MachineScheduler::MachineScheduler()
204 : MachineSchedulerBase(ID) {
205 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
208 void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.setPreservesCFG();
210 AU.addRequiredID(MachineDominatorsID);
211 AU.addRequired<MachineLoopInfo>();
212 AU.addRequired<AAResultsWrapperPass>();
213 AU.addRequired<TargetPassConfig>();
214 AU.addRequired<SlotIndexes>();
215 AU.addPreserved<SlotIndexes>();
216 AU.addRequired<LiveIntervals>();
217 AU.addPreserved<LiveIntervals>();
218 MachineFunctionPass::getAnalysisUsage(AU);
221 char PostMachineScheduler::ID = 0;
223 char &llvm::PostMachineSchedulerID = PostMachineScheduler::ID;
225 INITIALIZE_PASS(PostMachineScheduler, "postmisched",
226 "PostRA Machine Instruction Scheduler", false, false)
228 PostMachineScheduler::PostMachineScheduler()
229 : MachineSchedulerBase(ID) {
230 initializePostMachineSchedulerPass(*PassRegistry::getPassRegistry());
233 void PostMachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
234 AU.setPreservesCFG();
235 AU.addRequiredID(MachineDominatorsID);
236 AU.addRequired<MachineLoopInfo>();
237 AU.addRequired<TargetPassConfig>();
238 MachineFunctionPass::getAnalysisUsage(AU);
241 MachinePassRegistry MachineSchedRegistry::Registry;
243 /// A dummy default scheduler factory indicates whether the scheduler
244 /// is overridden on the command line.
245 static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) {
249 /// MachineSchedOpt allows command line selection of the scheduler.
250 static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
251 RegisterPassParser<MachineSchedRegistry>>
252 MachineSchedOpt("misched",
253 cl::init(&useDefaultMachineSched), cl::Hidden,
254 cl::desc("Machine instruction scheduler to use"));
256 static MachineSchedRegistry
257 DefaultSchedRegistry("default", "Use the target's default scheduler choice.",
258 useDefaultMachineSched);
260 static cl::opt<bool> EnableMachineSched(
262 cl::desc("Enable the machine instruction scheduling pass."), cl::init(true),
265 static cl::opt<bool> EnablePostRAMachineSched(
266 "enable-post-misched",
267 cl::desc("Enable the post-ra machine instruction scheduling pass."),
268 cl::init(true), cl::Hidden);
270 /// Decrement this iterator until reaching the top or a non-debug instr.
271 static MachineBasicBlock::const_iterator
272 priorNonDebug(MachineBasicBlock::const_iterator I,
273 MachineBasicBlock::const_iterator Beg) {
274 assert(I != Beg && "reached the top of the region, cannot decrement");
276 if (!I->isDebugValue())
282 /// Non-const version.
283 static MachineBasicBlock::iterator
284 priorNonDebug(MachineBasicBlock::iterator I,
285 MachineBasicBlock::const_iterator Beg) {
286 return priorNonDebug(MachineBasicBlock::const_iterator(I), Beg)
287 .getNonConstIterator();
290 /// If this iterator is a debug value, increment until reaching the End or a
291 /// non-debug instruction.
292 static MachineBasicBlock::const_iterator
293 nextIfDebug(MachineBasicBlock::const_iterator I,
294 MachineBasicBlock::const_iterator End) {
295 for(; I != End; ++I) {
296 if (!I->isDebugValue())
302 /// Non-const version.
303 static MachineBasicBlock::iterator
304 nextIfDebug(MachineBasicBlock::iterator I,
305 MachineBasicBlock::const_iterator End) {
306 return nextIfDebug(MachineBasicBlock::const_iterator(I), End)
307 .getNonConstIterator();
310 /// Instantiate a ScheduleDAGInstrs that will be owned by the caller.
311 ScheduleDAGInstrs *MachineScheduler::createMachineScheduler() {
312 // Select the scheduler, or set the default.
313 MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt;
314 if (Ctor != useDefaultMachineSched)
317 // Get the default scheduler set by the target for this function.
318 ScheduleDAGInstrs *Scheduler = PassConfig->createMachineScheduler(this);
322 // Default to GenericScheduler.
323 return createGenericSchedLive(this);
326 /// Instantiate a ScheduleDAGInstrs for PostRA scheduling that will be owned by
327 /// the caller. We don't have a command line option to override the postRA
328 /// scheduler. The Target must configure it.
329 ScheduleDAGInstrs *PostMachineScheduler::createPostMachineScheduler() {
330 // Get the postRA scheduler set by the target for this function.
331 ScheduleDAGInstrs *Scheduler = PassConfig->createPostMachineScheduler(this);
335 // Default to GenericScheduler.
336 return createGenericSchedPostRA(this);
339 /// Top-level MachineScheduler pass driver.
341 /// Visit blocks in function order. Divide each block into scheduling regions
342 /// and visit them bottom-up. Visiting regions bottom-up is not required, but is
343 /// consistent with the DAG builder, which traverses the interior of the
344 /// scheduling regions bottom-up.
346 /// This design avoids exposing scheduling boundaries to the DAG builder,
347 /// simplifying the DAG builder's support for "special" target instructions.
348 /// At the same time the design allows target schedulers to operate across
349 /// scheduling boundaries, for example to bundle the boudary instructions
350 /// without reordering them. This creates complexity, because the target
351 /// scheduler must update the RegionBegin and RegionEnd positions cached by
352 /// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler
353 /// design would be to split blocks at scheduling boundaries, but LLVM has a
354 /// general bias against block splitting purely for implementation simplicity.
355 bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
356 if (skipFunction(*mf.getFunction()))
359 if (EnableMachineSched.getNumOccurrences()) {
360 if (!EnableMachineSched)
362 } else if (!mf.getSubtarget().enableMachineScheduler())
365 DEBUG(dbgs() << "Before MISched:\n"; mf.print(dbgs()));
367 // Initialize the context of the pass.
369 MLI = &getAnalysis<MachineLoopInfo>();
370 MDT = &getAnalysis<MachineDominatorTree>();
371 PassConfig = &getAnalysis<TargetPassConfig>();
372 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
374 LIS = &getAnalysis<LiveIntervals>();
376 if (VerifyScheduling) {
378 MF->verify(this, "Before machine scheduling.");
380 RegClassInfo->runOnMachineFunction(*MF);
382 // Instantiate the selected scheduler for this target, function, and
383 // optimization level.
384 std::unique_ptr<ScheduleDAGInstrs> Scheduler(createMachineScheduler());
385 scheduleRegions(*Scheduler, false);
388 if (VerifyScheduling)
389 MF->verify(this, "After machine scheduling.");
393 bool PostMachineScheduler::runOnMachineFunction(MachineFunction &mf) {
394 if (skipFunction(*mf.getFunction()))
397 if (EnablePostRAMachineSched.getNumOccurrences()) {
398 if (!EnablePostRAMachineSched)
400 } else if (!mf.getSubtarget().enablePostRAScheduler()) {
401 DEBUG(dbgs() << "Subtarget disables post-MI-sched.\n");
404 DEBUG(dbgs() << "Before post-MI-sched:\n"; mf.print(dbgs()));
406 // Initialize the context of the pass.
408 PassConfig = &getAnalysis<TargetPassConfig>();
410 if (VerifyScheduling)
411 MF->verify(this, "Before post machine scheduling.");
413 // Instantiate the selected scheduler for this target, function, and
414 // optimization level.
415 std::unique_ptr<ScheduleDAGInstrs> Scheduler(createPostMachineScheduler());
416 scheduleRegions(*Scheduler, true);
418 if (VerifyScheduling)
419 MF->verify(this, "After post machine scheduling.");
423 /// Return true of the given instruction should not be included in a scheduling
426 /// MachineScheduler does not currently support scheduling across calls. To
427 /// handle calls, the DAG builder needs to be modified to create register
428 /// anti/output dependencies on the registers clobbered by the call's regmask
429 /// operand. In PreRA scheduling, the stack pointer adjustment already prevents
430 /// scheduling across calls. In PostRA scheduling, we need the isCall to enforce
431 /// the boundary, but there would be no benefit to postRA scheduling across
432 /// calls this late anyway.
433 static bool isSchedBoundary(MachineBasicBlock::iterator MI,
434 MachineBasicBlock *MBB,
436 const TargetInstrInfo *TII) {
437 return MI->isCall() || TII->isSchedulingBoundary(*MI, MBB, *MF);
440 /// Main driver for both MachineScheduler and PostMachineScheduler.
441 void MachineSchedulerBase::scheduleRegions(ScheduleDAGInstrs &Scheduler,
443 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
445 // Visit all machine basic blocks.
447 // TODO: Visit blocks in global postorder or postorder within the bottom-up
448 // loop tree. Then we can optionally compute global RegPressure.
449 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
450 MBB != MBBEnd; ++MBB) {
452 Scheduler.startBlock(&*MBB);
455 if (SchedOnlyFunc.getNumOccurrences() && SchedOnlyFunc != MF->getName())
457 if (SchedOnlyBlock.getNumOccurrences()
458 && (int)SchedOnlyBlock != MBB->getNumber())
462 // Break the block into scheduling regions [I, RegionEnd), and schedule each
463 // region as soon as it is discovered. RegionEnd points the scheduling
464 // boundary at the bottom of the region. The DAG does not include RegionEnd,
465 // but the region does (i.e. the next RegionEnd is above the previous
466 // RegionBegin). If the current block has no terminator then RegionEnd ==
467 // MBB->end() for the bottom region.
469 // The Scheduler may insert instructions during either schedule() or
470 // exitRegion(), even for empty regions. So the local iterators 'I' and
471 // 'RegionEnd' are invalid across these calls.
473 // MBB::size() uses instr_iterator to count. Here we need a bundle to count
474 // as a single instruction.
475 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
476 RegionEnd != MBB->begin(); RegionEnd = Scheduler.begin()) {
478 // Avoid decrementing RegionEnd for blocks with no terminator.
479 if (RegionEnd != MBB->end() ||
480 isSchedBoundary(&*std::prev(RegionEnd), &*MBB, MF, TII)) {
484 // The next region starts above the previous region. Look backward in the
485 // instruction stream until we find the nearest boundary.
486 unsigned NumRegionInstrs = 0;
487 MachineBasicBlock::iterator I = RegionEnd;
488 for (; I != MBB->begin(); --I) {
489 MachineInstr &MI = *std::prev(I);
490 if (isSchedBoundary(&MI, &*MBB, MF, TII))
492 if (!MI.isDebugValue())
495 // Notify the scheduler of the region, even if we may skip scheduling
496 // it. Perhaps it still needs to be bundled.
497 Scheduler.enterRegion(&*MBB, I, RegionEnd, NumRegionInstrs);
499 // Skip empty scheduling regions (0 or 1 schedulable instructions).
500 if (I == RegionEnd || I == std::prev(RegionEnd)) {
501 // Close the current region. Bundle the terminator if needed.
502 // This invalidates 'RegionEnd' and 'I'.
503 Scheduler.exitRegion();
506 DEBUG(dbgs() << "********** MI Scheduling **********\n");
507 DEBUG(dbgs() << MF->getName()
508 << ":BB#" << MBB->getNumber() << " " << MBB->getName()
509 << "\n From: " << *I << " To: ";
510 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
511 else dbgs() << "End";
512 dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
513 if (DumpCriticalPathLength) {
514 errs() << MF->getName();
515 errs() << ":BB# " << MBB->getNumber();
516 errs() << " " << MBB->getName() << " \n";
519 // Schedule a region: possibly reorder instructions.
520 // This invalidates 'RegionEnd' and 'I'.
521 Scheduler.schedule();
523 // Close the current region.
524 Scheduler.exitRegion();
526 // Scheduling has invalidated the current iterator 'I'. Ask the
527 // scheduler for the top of it's scheduled region.
528 RegionEnd = Scheduler.begin();
530 Scheduler.finishBlock();
531 // FIXME: Ideally, no further passes should rely on kill flags. However,
532 // thumb2 size reduction is currently an exception, so the PostMIScheduler
535 Scheduler.fixupKills(&*MBB);
537 Scheduler.finalizeSchedule();
540 void MachineSchedulerBase::print(raw_ostream &O, const Module* m) const {
544 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
545 LLVM_DUMP_METHOD void ReadyQueue::dump() {
546 dbgs() << "Queue " << Name << ": ";
547 for (unsigned i = 0, e = Queue.size(); i < e; ++i)
548 dbgs() << Queue[i]->NodeNum << " ";
553 //===----------------------------------------------------------------------===//
554 // ScheduleDAGMI - Basic machine instruction scheduling. This is
555 // independent of PreRA/PostRA scheduling and involves no extra book-keeping for
556 // virtual registers.
557 // ===----------------------------------------------------------------------===/
559 // Provide a vtable anchor.
560 ScheduleDAGMI::~ScheduleDAGMI() = default;
562 bool ScheduleDAGMI::canAddEdge(SUnit *SuccSU, SUnit *PredSU) {
563 return SuccSU == &ExitSU || !Topo.IsReachable(PredSU, SuccSU);
566 bool ScheduleDAGMI::addEdge(SUnit *SuccSU, const SDep &PredDep) {
567 if (SuccSU != &ExitSU) {
568 // Do not use WillCreateCycle, it assumes SD scheduling.
569 // If Pred is reachable from Succ, then the edge creates a cycle.
570 if (Topo.IsReachable(PredDep.getSUnit(), SuccSU))
572 Topo.AddPred(SuccSU, PredDep.getSUnit());
574 SuccSU->addPred(PredDep, /*Required=*/!PredDep.isArtificial());
575 // Return true regardless of whether a new edge needed to be inserted.
579 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
580 /// NumPredsLeft reaches zero, release the successor node.
582 /// FIXME: Adjust SuccSU height based on MinLatency.
583 void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) {
584 SUnit *SuccSU = SuccEdge->getSUnit();
586 if (SuccEdge->isWeak()) {
587 --SuccSU->WeakPredsLeft;
588 if (SuccEdge->isCluster())
589 NextClusterSucc = SuccSU;
593 if (SuccSU->NumPredsLeft == 0) {
594 dbgs() << "*** Scheduling failed! ***\n";
596 dbgs() << " has been released too many times!\n";
597 llvm_unreachable(nullptr);
600 // SU->TopReadyCycle was set to CurrCycle when it was scheduled. However,
601 // CurrCycle may have advanced since then.
602 if (SuccSU->TopReadyCycle < SU->TopReadyCycle + SuccEdge->getLatency())
603 SuccSU->TopReadyCycle = SU->TopReadyCycle + SuccEdge->getLatency();
605 --SuccSU->NumPredsLeft;
606 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
607 SchedImpl->releaseTopNode(SuccSU);
610 /// releaseSuccessors - Call releaseSucc on each of SU's successors.
611 void ScheduleDAGMI::releaseSuccessors(SUnit *SU) {
612 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
614 releaseSucc(SU, &*I);
618 /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When
619 /// NumSuccsLeft reaches zero, release the predecessor node.
621 /// FIXME: Adjust PredSU height based on MinLatency.
622 void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) {
623 SUnit *PredSU = PredEdge->getSUnit();
625 if (PredEdge->isWeak()) {
626 --PredSU->WeakSuccsLeft;
627 if (PredEdge->isCluster())
628 NextClusterPred = PredSU;
632 if (PredSU->NumSuccsLeft == 0) {
633 dbgs() << "*** Scheduling failed! ***\n";
635 dbgs() << " has been released too many times!\n";
636 llvm_unreachable(nullptr);
639 // SU->BotReadyCycle was set to CurrCycle when it was scheduled. However,
640 // CurrCycle may have advanced since then.
641 if (PredSU->BotReadyCycle < SU->BotReadyCycle + PredEdge->getLatency())
642 PredSU->BotReadyCycle = SU->BotReadyCycle + PredEdge->getLatency();
644 --PredSU->NumSuccsLeft;
645 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU)
646 SchedImpl->releaseBottomNode(PredSU);
649 /// releasePredecessors - Call releasePred on each of SU's predecessors.
650 void ScheduleDAGMI::releasePredecessors(SUnit *SU) {
651 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
653 releasePred(SU, &*I);
657 /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after
658 /// crossing a scheduling boundary. [begin, end) includes all instructions in
659 /// the region, including the boundary itself and single-instruction regions
660 /// that don't get scheduled.
661 void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb,
662 MachineBasicBlock::iterator begin,
663 MachineBasicBlock::iterator end,
664 unsigned regioninstrs)
666 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
668 SchedImpl->initPolicy(begin, end, regioninstrs);
671 /// This is normally called from the main scheduler loop but may also be invoked
672 /// by the scheduling strategy to perform additional code motion.
673 void ScheduleDAGMI::moveInstruction(
674 MachineInstr *MI, MachineBasicBlock::iterator InsertPos) {
675 // Advance RegionBegin if the first instruction moves down.
676 if (&*RegionBegin == MI)
679 // Update the instruction stream.
680 BB->splice(InsertPos, BB, MI);
682 // Update LiveIntervals
684 LIS->handleMove(*MI, /*UpdateFlags=*/true);
686 // Recede RegionBegin if an instruction moves above the first.
687 if (RegionBegin == InsertPos)
691 bool ScheduleDAGMI::checkSchedLimit() {
693 if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) {
694 CurrentTop = CurrentBottom;
697 ++NumInstrsScheduled;
702 /// Per-region scheduling driver, called back from
703 /// MachineScheduler::runOnMachineFunction. This is a simplified driver that
704 /// does not consider liveness or register pressure. It is useful for PostRA
705 /// scheduling and potentially other custom schedulers.
706 void ScheduleDAGMI::schedule() {
707 DEBUG(dbgs() << "ScheduleDAGMI::schedule starting\n");
708 DEBUG(SchedImpl->dumpPolicy());
713 Topo.InitDAGTopologicalSorting();
717 SmallVector<SUnit*, 8> TopRoots, BotRoots;
718 findRootsAndBiasEdges(TopRoots, BotRoots);
720 // Initialize the strategy before modifying the DAG.
721 // This may initialize a DFSResult to be used for queue priority.
722 SchedImpl->initialize(this);
725 if (EntrySU.getInstr() != nullptr)
726 EntrySU.dumpAll(this);
727 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
728 SUnits[su].dumpAll(this);
729 if (ExitSU.getInstr() != nullptr)
730 ExitSU.dumpAll(this);
732 if (ViewMISchedDAGs) viewGraph();
734 // Initialize ready queues now that the DAG and priority data are finalized.
735 initQueues(TopRoots, BotRoots);
737 bool IsTopNode = false;
739 DEBUG(dbgs() << "** ScheduleDAGMI::schedule picking next node\n");
740 SUnit *SU = SchedImpl->pickNode(IsTopNode);
743 assert(!SU->isScheduled && "Node already scheduled");
744 if (!checkSchedLimit())
747 MachineInstr *MI = SU->getInstr();
749 assert(SU->isTopReady() && "node still has unscheduled dependencies");
750 if (&*CurrentTop == MI)
751 CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom);
753 moveInstruction(MI, CurrentTop);
755 assert(SU->isBottomReady() && "node still has unscheduled dependencies");
756 MachineBasicBlock::iterator priorII =
757 priorNonDebug(CurrentBottom, CurrentTop);
759 CurrentBottom = priorII;
761 if (&*CurrentTop == MI)
762 CurrentTop = nextIfDebug(++CurrentTop, priorII);
763 moveInstruction(MI, CurrentBottom);
767 // Notify the scheduling strategy before updating the DAG.
768 // This sets the scheduled node's ReadyCycle to CurrCycle. When updateQueues
769 // runs, it can then use the accurate ReadyCycle time to determine whether
770 // newly released nodes can move to the readyQ.
771 SchedImpl->schedNode(SU, IsTopNode);
773 updateQueues(SU, IsTopNode);
775 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
780 unsigned BBNum = begin()->getParent()->getNumber();
781 dbgs() << "*** Final schedule for BB#" << BBNum << " ***\n";
787 /// Apply each ScheduleDAGMutation step in order.
788 void ScheduleDAGMI::postprocessDAG() {
789 for (unsigned i = 0, e = Mutations.size(); i < e; ++i) {
790 Mutations[i]->apply(this);
795 findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots,
796 SmallVectorImpl<SUnit*> &BotRoots) {
797 for (std::vector<SUnit>::iterator
798 I = SUnits.begin(), E = SUnits.end(); I != E; ++I) {
800 assert(!SU->isBoundaryNode() && "Boundary node should not be in SUnits");
802 // Order predecessors so DFSResult follows the critical path.
803 SU->biasCriticalPath();
805 // A SUnit is ready to top schedule if it has no predecessors.
806 if (!I->NumPredsLeft)
807 TopRoots.push_back(SU);
808 // A SUnit is ready to bottom schedule if it has no successors.
809 if (!I->NumSuccsLeft)
810 BotRoots.push_back(SU);
812 ExitSU.biasCriticalPath();
815 /// Identify DAG roots and setup scheduler queues.
816 void ScheduleDAGMI::initQueues(ArrayRef<SUnit*> TopRoots,
817 ArrayRef<SUnit*> BotRoots) {
818 NextClusterSucc = nullptr;
819 NextClusterPred = nullptr;
821 // Release all DAG roots for scheduling, not including EntrySU/ExitSU.
823 // Nodes with unreleased weak edges can still be roots.
824 // Release top roots in forward order.
825 for (SmallVectorImpl<SUnit*>::const_iterator
826 I = TopRoots.begin(), E = TopRoots.end(); I != E; ++I) {
827 SchedImpl->releaseTopNode(*I);
829 // Release bottom roots in reverse order so the higher priority nodes appear
830 // first. This is more natural and slightly more efficient.
831 for (SmallVectorImpl<SUnit*>::const_reverse_iterator
832 I = BotRoots.rbegin(), E = BotRoots.rend(); I != E; ++I) {
833 SchedImpl->releaseBottomNode(*I);
836 releaseSuccessors(&EntrySU);
837 releasePredecessors(&ExitSU);
839 SchedImpl->registerRoots();
841 // Advance past initial DebugValues.
842 CurrentTop = nextIfDebug(RegionBegin, RegionEnd);
843 CurrentBottom = RegionEnd;
846 /// Update scheduler queues after scheduling an instruction.
847 void ScheduleDAGMI::updateQueues(SUnit *SU, bool IsTopNode) {
848 // Release dependent instructions for scheduling.
850 releaseSuccessors(SU);
852 releasePredecessors(SU);
854 SU->isScheduled = true;
857 /// Reinsert any remaining debug_values, just like the PostRA scheduler.
858 void ScheduleDAGMI::placeDebugValues() {
859 // If first instruction was a DBG_VALUE then put it back.
861 BB->splice(RegionBegin, BB, FirstDbgValue);
862 RegionBegin = FirstDbgValue;
865 for (std::vector<std::pair<MachineInstr *, MachineInstr *>>::iterator
866 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
867 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
868 MachineInstr *DbgValue = P.first;
869 MachineBasicBlock::iterator OrigPrevMI = P.second;
870 if (&*RegionBegin == DbgValue)
872 BB->splice(++OrigPrevMI, BB, DbgValue);
873 if (OrigPrevMI == std::prev(RegionEnd))
874 RegionEnd = DbgValue;
877 FirstDbgValue = nullptr;
880 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
881 LLVM_DUMP_METHOD void ScheduleDAGMI::dumpSchedule() const {
882 for (MachineBasicBlock::iterator MI = begin(), ME = end(); MI != ME; ++MI) {
883 if (SUnit *SU = getSUnit(&(*MI)))
886 dbgs() << "Missing SUnit\n";
891 //===----------------------------------------------------------------------===//
892 // ScheduleDAGMILive - Base class for MachineInstr scheduling with LiveIntervals
894 //===----------------------------------------------------------------------===//
896 ScheduleDAGMILive::~ScheduleDAGMILive() {
900 void ScheduleDAGMILive::collectVRegUses(SUnit &SU) {
901 const MachineInstr &MI = *SU.getInstr();
902 for (const MachineOperand &MO : MI.operands()) {
907 if (TrackLaneMasks && !MO.isUse())
910 unsigned Reg = MO.getReg();
911 if (!TargetRegisterInfo::isVirtualRegister(Reg))
915 if (TrackLaneMasks) {
916 bool FoundDef = false;
917 for (const MachineOperand &MO2 : MI.operands()) {
918 if (MO2.isReg() && MO2.isDef() && MO2.getReg() == Reg && !MO2.isDead()) {
927 // Record this local VReg use.
928 VReg2SUnitMultiMap::iterator UI = VRegUses.find(Reg);
929 for (; UI != VRegUses.end(); ++UI) {
933 if (UI == VRegUses.end())
934 VRegUses.insert(VReg2SUnit(Reg, LaneBitmask::getNone(), &SU));
938 /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after
939 /// crossing a scheduling boundary. [begin, end) includes all instructions in
940 /// the region, including the boundary itself and single-instruction regions
941 /// that don't get scheduled.
942 void ScheduleDAGMILive::enterRegion(MachineBasicBlock *bb,
943 MachineBasicBlock::iterator begin,
944 MachineBasicBlock::iterator end,
945 unsigned regioninstrs)
947 // ScheduleDAGMI initializes SchedImpl's per-region policy.
948 ScheduleDAGMI::enterRegion(bb, begin, end, regioninstrs);
950 // For convenience remember the end of the liveness region.
951 LiveRegionEnd = (RegionEnd == bb->end()) ? RegionEnd : std::next(RegionEnd);
953 SUPressureDiffs.clear();
955 ShouldTrackPressure = SchedImpl->shouldTrackPressure();
956 ShouldTrackLaneMasks = SchedImpl->shouldTrackLaneMasks();
958 assert((!ShouldTrackLaneMasks || ShouldTrackPressure) &&
959 "ShouldTrackLaneMasks requires ShouldTrackPressure");
962 // Setup the register pressure trackers for the top scheduled top and bottom
963 // scheduled regions.
964 void ScheduleDAGMILive::initRegPressure() {
966 VRegUses.setUniverse(MRI.getNumVirtRegs());
967 for (SUnit &SU : SUnits)
970 TopRPTracker.init(&MF, RegClassInfo, LIS, BB, RegionBegin,
971 ShouldTrackLaneMasks, false);
972 BotRPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd,
973 ShouldTrackLaneMasks, false);
975 // Close the RPTracker to finalize live ins.
976 RPTracker.closeRegion();
978 DEBUG(RPTracker.dump());
980 // Initialize the live ins and live outs.
981 TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs);
982 BotRPTracker.addLiveRegs(RPTracker.getPressure().LiveOutRegs);
984 // Close one end of the tracker so we can call
985 // getMaxUpward/DownwardPressureDelta before advancing across any
986 // instructions. This converts currently live regs into live ins/outs.
987 TopRPTracker.closeTop();
988 BotRPTracker.closeBottom();
990 BotRPTracker.initLiveThru(RPTracker);
991 if (!BotRPTracker.getLiveThru().empty()) {
992 TopRPTracker.initLiveThru(BotRPTracker.getLiveThru());
993 DEBUG(dbgs() << "Live Thru: ";
994 dumpRegSetPressure(BotRPTracker.getLiveThru(), TRI));
997 // For each live out vreg reduce the pressure change associated with other
998 // uses of the same vreg below the live-out reaching def.
999 updatePressureDiffs(RPTracker.getPressure().LiveOutRegs);
1001 // Account for liveness generated by the region boundary.
1002 if (LiveRegionEnd != RegionEnd) {
1003 SmallVector<RegisterMaskPair, 8> LiveUses;
1004 BotRPTracker.recede(&LiveUses);
1005 updatePressureDiffs(LiveUses);
1009 dbgs() << "Top Pressure:\n";
1010 dumpRegSetPressure(TopRPTracker.getRegSetPressureAtPos(), TRI);
1011 dbgs() << "Bottom Pressure:\n";
1012 dumpRegSetPressure(BotRPTracker.getRegSetPressureAtPos(), TRI);
1015 assert(BotRPTracker.getPos() == RegionEnd && "Can't find the region bottom");
1017 // Cache the list of excess pressure sets in this region. This will also track
1018 // the max pressure in the scheduled code for these sets.
1019 RegionCriticalPSets.clear();
1020 const std::vector<unsigned> &RegionPressure =
1021 RPTracker.getPressure().MaxSetPressure;
1022 for (unsigned i = 0, e = RegionPressure.size(); i < e; ++i) {
1023 unsigned Limit = RegClassInfo->getRegPressureSetLimit(i);
1024 if (RegionPressure[i] > Limit) {
1025 DEBUG(dbgs() << TRI->getRegPressureSetName(i)
1026 << " Limit " << Limit
1027 << " Actual " << RegionPressure[i] << "\n");
1028 RegionCriticalPSets.push_back(PressureChange(i));
1031 DEBUG(dbgs() << "Excess PSets: ";
1032 for (unsigned i = 0, e = RegionCriticalPSets.size(); i != e; ++i)
1033 dbgs() << TRI->getRegPressureSetName(
1034 RegionCriticalPSets[i].getPSet()) << " ";
1038 void ScheduleDAGMILive::
1039 updateScheduledPressure(const SUnit *SU,
1040 const std::vector<unsigned> &NewMaxPressure) {
1041 const PressureDiff &PDiff = getPressureDiff(SU);
1042 unsigned CritIdx = 0, CritEnd = RegionCriticalPSets.size();
1043 for (PressureDiff::const_iterator I = PDiff.begin(), E = PDiff.end();
1047 unsigned ID = I->getPSet();
1048 while (CritIdx != CritEnd && RegionCriticalPSets[CritIdx].getPSet() < ID)
1050 if (CritIdx != CritEnd && RegionCriticalPSets[CritIdx].getPSet() == ID) {
1051 if ((int)NewMaxPressure[ID] > RegionCriticalPSets[CritIdx].getUnitInc()
1052 && NewMaxPressure[ID] <= (unsigned)std::numeric_limits<int16_t>::max())
1053 RegionCriticalPSets[CritIdx].setUnitInc(NewMaxPressure[ID]);
1055 unsigned Limit = RegClassInfo->getRegPressureSetLimit(ID);
1056 if (NewMaxPressure[ID] >= Limit - 2) {
1057 DEBUG(dbgs() << " " << TRI->getRegPressureSetName(ID) << ": "
1058 << NewMaxPressure[ID]
1059 << ((NewMaxPressure[ID] > Limit) ? " > " : " <= ") << Limit
1060 << "(+ " << BotRPTracker.getLiveThru()[ID] << " livethru)\n");
1065 /// Update the PressureDiff array for liveness after scheduling this
1067 void ScheduleDAGMILive::updatePressureDiffs(
1068 ArrayRef<RegisterMaskPair> LiveUses) {
1069 for (const RegisterMaskPair &P : LiveUses) {
1070 unsigned Reg = P.RegUnit;
1071 /// FIXME: Currently assuming single-use physregs.
1072 if (!TRI->isVirtualRegister(Reg))
1075 if (ShouldTrackLaneMasks) {
1076 // If the register has just become live then other uses won't change
1077 // this fact anymore => decrement pressure.
1078 // If the register has just become dead then other uses make it come
1079 // back to life => increment pressure.
1080 bool Decrement = P.LaneMask.any();
1082 for (const VReg2SUnit &V2SU
1083 : make_range(VRegUses.find(Reg), VRegUses.end())) {
1084 SUnit &SU = *V2SU.SU;
1085 if (SU.isScheduled || &SU == &ExitSU)
1088 PressureDiff &PDiff = getPressureDiff(&SU);
1089 PDiff.addPressureChange(Reg, Decrement, &MRI);
1091 dbgs() << " UpdateRegP: SU(" << SU.NodeNum << ") "
1092 << PrintReg(Reg, TRI) << ':' << PrintLaneMask(P.LaneMask)
1093 << ' ' << *SU.getInstr();
1099 assert(P.LaneMask.any());
1100 DEBUG(dbgs() << " LiveReg: " << PrintVRegOrUnit(Reg, TRI) << "\n");
1101 // This may be called before CurrentBottom has been initialized. However,
1102 // BotRPTracker must have a valid position. We want the value live into the
1103 // instruction or live out of the block, so ask for the previous
1104 // instruction's live-out.
1105 const LiveInterval &LI = LIS->getInterval(Reg);
1107 MachineBasicBlock::const_iterator I =
1108 nextIfDebug(BotRPTracker.getPos(), BB->end());
1110 VNI = LI.getVNInfoBefore(LIS->getMBBEndIdx(BB));
1112 LiveQueryResult LRQ = LI.Query(LIS->getInstructionIndex(*I));
1113 VNI = LRQ.valueIn();
1115 // RegisterPressureTracker guarantees that readsReg is true for LiveUses.
1116 assert(VNI && "No live value at use.");
1117 for (const VReg2SUnit &V2SU
1118 : make_range(VRegUses.find(Reg), VRegUses.end())) {
1119 SUnit *SU = V2SU.SU;
1120 // If this use comes before the reaching def, it cannot be a last use,
1121 // so decrease its pressure change.
1122 if (!SU->isScheduled && SU != &ExitSU) {
1123 LiveQueryResult LRQ =
1124 LI.Query(LIS->getInstructionIndex(*SU->getInstr()));
1125 if (LRQ.valueIn() == VNI) {
1126 PressureDiff &PDiff = getPressureDiff(SU);
1127 PDiff.addPressureChange(Reg, true, &MRI);
1129 dbgs() << " UpdateRegP: SU(" << SU->NodeNum << ") "
1141 /// schedule - Called back from MachineScheduler::runOnMachineFunction
1142 /// after setting up the current scheduling region. [RegionBegin, RegionEnd)
1143 /// only includes instructions that have DAG nodes, not scheduling boundaries.
1145 /// This is a skeletal driver, with all the functionality pushed into helpers,
1146 /// so that it can be easily extended by experimental schedulers. Generally,
1147 /// implementing MachineSchedStrategy should be sufficient to implement a new
1148 /// scheduling algorithm. However, if a scheduler further subclasses
1149 /// ScheduleDAGMILive then it will want to override this virtual method in order
1150 /// to update any specialized state.
1151 void ScheduleDAGMILive::schedule() {
1152 DEBUG(dbgs() << "ScheduleDAGMILive::schedule starting\n");
1153 DEBUG(SchedImpl->dumpPolicy());
1154 buildDAGWithRegPressure();
1156 Topo.InitDAGTopologicalSorting();
1160 SmallVector<SUnit*, 8> TopRoots, BotRoots;
1161 findRootsAndBiasEdges(TopRoots, BotRoots);
1163 // Initialize the strategy before modifying the DAG.
1164 // This may initialize a DFSResult to be used for queue priority.
1165 SchedImpl->initialize(this);
1168 if (EntrySU.getInstr() != nullptr)
1169 EntrySU.dumpAll(this);
1170 for (const SUnit &SU : SUnits) {
1172 if (ShouldTrackPressure) {
1173 dbgs() << " Pressure Diff : ";
1174 getPressureDiff(&SU).dump(*TRI);
1176 dbgs() << " Single Issue : ";
1177 if (SchedModel.mustBeginGroup(SU.getInstr()) &&
1178 SchedModel.mustEndGroup(SU.getInstr()))
1184 if (ExitSU.getInstr() != nullptr)
1185 ExitSU.dumpAll(this);
1187 if (ViewMISchedDAGs) viewGraph();
1189 // Initialize ready queues now that the DAG and priority data are finalized.
1190 initQueues(TopRoots, BotRoots);
1192 bool IsTopNode = false;
1194 DEBUG(dbgs() << "** ScheduleDAGMILive::schedule picking next node\n");
1195 SUnit *SU = SchedImpl->pickNode(IsTopNode);
1198 assert(!SU->isScheduled && "Node already scheduled");
1199 if (!checkSchedLimit())
1202 scheduleMI(SU, IsTopNode);
1205 unsigned SubtreeID = DFSResult->getSubtreeID(SU);
1206 if (!ScheduledTrees.test(SubtreeID)) {
1207 ScheduledTrees.set(SubtreeID);
1208 DFSResult->scheduleTree(SubtreeID);
1209 SchedImpl->scheduleTree(SubtreeID);
1213 // Notify the scheduling strategy after updating the DAG.
1214 SchedImpl->schedNode(SU, IsTopNode);
1216 updateQueues(SU, IsTopNode);
1218 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
1223 unsigned BBNum = begin()->getParent()->getNumber();
1224 dbgs() << "*** Final schedule for BB#" << BBNum << " ***\n";
1230 /// Build the DAG and setup three register pressure trackers.
1231 void ScheduleDAGMILive::buildDAGWithRegPressure() {
1232 if (!ShouldTrackPressure) {
1234 RegionCriticalPSets.clear();
1235 buildSchedGraph(AA);
1239 // Initialize the register pressure tracker used by buildSchedGraph.
1240 RPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd,
1241 ShouldTrackLaneMasks, /*TrackUntiedDefs=*/true);
1243 // Account for liveness generate by the region boundary.
1244 if (LiveRegionEnd != RegionEnd)
1247 // Build the DAG, and compute current register pressure.
1248 buildSchedGraph(AA, &RPTracker, &SUPressureDiffs, LIS, ShouldTrackLaneMasks);
1250 // Initialize top/bottom trackers after computing region pressure.
1254 void ScheduleDAGMILive::computeDFSResult() {
1256 DFSResult = new SchedDFSResult(/*BottomU*/true, MinSubtreeSize);
1258 ScheduledTrees.clear();
1259 DFSResult->resize(SUnits.size());
1260 DFSResult->compute(SUnits);
1261 ScheduledTrees.resize(DFSResult->getNumSubtrees());
1264 /// Compute the max cyclic critical path through the DAG. The scheduling DAG
1265 /// only provides the critical path for single block loops. To handle loops that
1266 /// span blocks, we could use the vreg path latencies provided by
1267 /// MachineTraceMetrics instead. However, MachineTraceMetrics is not currently
1268 /// available for use in the scheduler.
1270 /// The cyclic path estimation identifies a def-use pair that crosses the back
1271 /// edge and considers the depth and height of the nodes. For example, consider
1272 /// the following instruction sequence where each instruction has unit latency
1273 /// and defines an epomymous virtual register:
1275 /// a->b(a,c)->c(b)->d(c)->exit
1277 /// The cyclic critical path is a two cycles: b->c->b
1278 /// The acyclic critical path is four cycles: a->b->c->d->exit
1279 /// LiveOutHeight = height(c) = len(c->d->exit) = 2
1280 /// LiveOutDepth = depth(c) + 1 = len(a->b->c) + 1 = 3
1281 /// LiveInHeight = height(b) + 1 = len(b->c->d->exit) + 1 = 4
1282 /// LiveInDepth = depth(b) = len(a->b) = 1
1284 /// LiveOutDepth - LiveInDepth = 3 - 1 = 2
1285 /// LiveInHeight - LiveOutHeight = 4 - 2 = 2
1286 /// CyclicCriticalPath = min(2, 2) = 2
1288 /// This could be relevant to PostRA scheduling, but is currently implemented
1289 /// assuming LiveIntervals.
1290 unsigned ScheduleDAGMILive::computeCyclicCriticalPath() {
1291 // This only applies to single block loop.
1292 if (!BB->isSuccessor(BB))
1295 unsigned MaxCyclicLatency = 0;
1296 // Visit each live out vreg def to find def/use pairs that cross iterations.
1297 for (const RegisterMaskPair &P : RPTracker.getPressure().LiveOutRegs) {
1298 unsigned Reg = P.RegUnit;
1299 if (!TRI->isVirtualRegister(Reg))
1301 const LiveInterval &LI = LIS->getInterval(Reg);
1302 const VNInfo *DefVNI = LI.getVNInfoBefore(LIS->getMBBEndIdx(BB));
1306 MachineInstr *DefMI = LIS->getInstructionFromIndex(DefVNI->def);
1307 const SUnit *DefSU = getSUnit(DefMI);
1311 unsigned LiveOutHeight = DefSU->getHeight();
1312 unsigned LiveOutDepth = DefSU->getDepth() + DefSU->Latency;
1313 // Visit all local users of the vreg def.
1314 for (const VReg2SUnit &V2SU
1315 : make_range(VRegUses.find(Reg), VRegUses.end())) {
1316 SUnit *SU = V2SU.SU;
1320 // Only consider uses of the phi.
1321 LiveQueryResult LRQ = LI.Query(LIS->getInstructionIndex(*SU->getInstr()));
1322 if (!LRQ.valueIn()->isPHIDef())
1325 // Assume that a path spanning two iterations is a cycle, which could
1326 // overestimate in strange cases. This allows cyclic latency to be
1327 // estimated as the minimum slack of the vreg's depth or height.
1328 unsigned CyclicLatency = 0;
1329 if (LiveOutDepth > SU->getDepth())
1330 CyclicLatency = LiveOutDepth - SU->getDepth();
1332 unsigned LiveInHeight = SU->getHeight() + DefSU->Latency;
1333 if (LiveInHeight > LiveOutHeight) {
1334 if (LiveInHeight - LiveOutHeight < CyclicLatency)
1335 CyclicLatency = LiveInHeight - LiveOutHeight;
1339 DEBUG(dbgs() << "Cyclic Path: SU(" << DefSU->NodeNum << ") -> SU("
1340 << SU->NodeNum << ") = " << CyclicLatency << "c\n");
1341 if (CyclicLatency > MaxCyclicLatency)
1342 MaxCyclicLatency = CyclicLatency;
1345 DEBUG(dbgs() << "Cyclic Critical Path: " << MaxCyclicLatency << "c\n");
1346 return MaxCyclicLatency;
1349 /// Release ExitSU predecessors and setup scheduler queues. Re-position
1350 /// the Top RP tracker in case the region beginning has changed.
1351 void ScheduleDAGMILive::initQueues(ArrayRef<SUnit*> TopRoots,
1352 ArrayRef<SUnit*> BotRoots) {
1353 ScheduleDAGMI::initQueues(TopRoots, BotRoots);
1354 if (ShouldTrackPressure) {
1355 assert(TopRPTracker.getPos() == RegionBegin && "bad initial Top tracker");
1356 TopRPTracker.setPos(CurrentTop);
1360 /// Move an instruction and update register pressure.
1361 void ScheduleDAGMILive::scheduleMI(SUnit *SU, bool IsTopNode) {
1362 // Move the instruction to its new location in the instruction stream.
1363 MachineInstr *MI = SU->getInstr();
1366 assert(SU->isTopReady() && "node still has unscheduled dependencies");
1367 if (&*CurrentTop == MI)
1368 CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom);
1370 moveInstruction(MI, CurrentTop);
1371 TopRPTracker.setPos(MI);
1374 if (ShouldTrackPressure) {
1375 // Update top scheduled pressure.
1376 RegisterOperands RegOpers;
1377 RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
1378 if (ShouldTrackLaneMasks) {
1379 // Adjust liveness and add missing dead+read-undef flags.
1380 SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
1381 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
1383 // Adjust for missing dead-def flags.
1384 RegOpers.detectDeadDefs(*MI, *LIS);
1387 TopRPTracker.advance(RegOpers);
1388 assert(TopRPTracker.getPos() == CurrentTop && "out of sync");
1390 dbgs() << "Top Pressure:\n";
1391 dumpRegSetPressure(TopRPTracker.getRegSetPressureAtPos(), TRI);
1394 updateScheduledPressure(SU, TopRPTracker.getPressure().MaxSetPressure);
1397 assert(SU->isBottomReady() && "node still has unscheduled dependencies");
1398 MachineBasicBlock::iterator priorII =
1399 priorNonDebug(CurrentBottom, CurrentTop);
1400 if (&*priorII == MI)
1401 CurrentBottom = priorII;
1403 if (&*CurrentTop == MI) {
1404 CurrentTop = nextIfDebug(++CurrentTop, priorII);
1405 TopRPTracker.setPos(CurrentTop);
1407 moveInstruction(MI, CurrentBottom);
1410 if (ShouldTrackPressure) {
1411 RegisterOperands RegOpers;
1412 RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
1413 if (ShouldTrackLaneMasks) {
1414 // Adjust liveness and add missing dead+read-undef flags.
1415 SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
1416 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
1418 // Adjust for missing dead-def flags.
1419 RegOpers.detectDeadDefs(*MI, *LIS);
1422 BotRPTracker.recedeSkipDebugValues();
1423 SmallVector<RegisterMaskPair, 8> LiveUses;
1424 BotRPTracker.recede(RegOpers, &LiveUses);
1425 assert(BotRPTracker.getPos() == CurrentBottom && "out of sync");
1427 dbgs() << "Bottom Pressure:\n";
1428 dumpRegSetPressure(BotRPTracker.getRegSetPressureAtPos(), TRI);
1431 updateScheduledPressure(SU, BotRPTracker.getPressure().MaxSetPressure);
1432 updatePressureDiffs(LiveUses);
1437 //===----------------------------------------------------------------------===//
1438 // BaseMemOpClusterMutation - DAG post-processing to cluster loads or stores.
1439 //===----------------------------------------------------------------------===//
1443 /// \brief Post-process the DAG to create cluster edges between neighboring
1444 /// loads or between neighboring stores.
1445 class BaseMemOpClusterMutation : public ScheduleDAGMutation {
1451 MemOpInfo(SUnit *su, unsigned reg, int64_t ofs)
1452 : SU(su), BaseReg(reg), Offset(ofs) {}
1454 bool operator<(const MemOpInfo&RHS) const {
1455 return std::tie(BaseReg, Offset, SU->NodeNum) <
1456 std::tie(RHS.BaseReg, RHS.Offset, RHS.SU->NodeNum);
1460 const TargetInstrInfo *TII;
1461 const TargetRegisterInfo *TRI;
1465 BaseMemOpClusterMutation(const TargetInstrInfo *tii,
1466 const TargetRegisterInfo *tri, bool IsLoad)
1467 : TII(tii), TRI(tri), IsLoad(IsLoad) {}
1469 void apply(ScheduleDAGInstrs *DAGInstrs) override;
1472 void clusterNeighboringMemOps(ArrayRef<SUnit *> MemOps, ScheduleDAGMI *DAG);
1475 class StoreClusterMutation : public BaseMemOpClusterMutation {
1477 StoreClusterMutation(const TargetInstrInfo *tii,
1478 const TargetRegisterInfo *tri)
1479 : BaseMemOpClusterMutation(tii, tri, false) {}
1482 class LoadClusterMutation : public BaseMemOpClusterMutation {
1484 LoadClusterMutation(const TargetInstrInfo *tii, const TargetRegisterInfo *tri)
1485 : BaseMemOpClusterMutation(tii, tri, true) {}
1488 } // end anonymous namespace
1492 std::unique_ptr<ScheduleDAGMutation>
1493 createLoadClusterDAGMutation(const TargetInstrInfo *TII,
1494 const TargetRegisterInfo *TRI) {
1495 return EnableMemOpCluster ? llvm::make_unique<LoadClusterMutation>(TII, TRI)
1499 std::unique_ptr<ScheduleDAGMutation>
1500 createStoreClusterDAGMutation(const TargetInstrInfo *TII,
1501 const TargetRegisterInfo *TRI) {
1502 return EnableMemOpCluster ? llvm::make_unique<StoreClusterMutation>(TII, TRI)
1506 } // end namespace llvm
1508 void BaseMemOpClusterMutation::clusterNeighboringMemOps(
1509 ArrayRef<SUnit *> MemOps, ScheduleDAGMI *DAG) {
1510 SmallVector<MemOpInfo, 32> MemOpRecords;
1511 for (unsigned Idx = 0, End = MemOps.size(); Idx != End; ++Idx) {
1512 SUnit *SU = MemOps[Idx];
1515 if (TII->getMemOpBaseRegImmOfs(*SU->getInstr(), BaseReg, Offset, TRI))
1516 MemOpRecords.push_back(MemOpInfo(SU, BaseReg, Offset));
1518 if (MemOpRecords.size() < 2)
1521 std::sort(MemOpRecords.begin(), MemOpRecords.end());
1522 unsigned ClusterLength = 1;
1523 for (unsigned Idx = 0, End = MemOpRecords.size(); Idx < (End - 1); ++Idx) {
1524 if (MemOpRecords[Idx].BaseReg != MemOpRecords[Idx+1].BaseReg) {
1529 SUnit *SUa = MemOpRecords[Idx].SU;
1530 SUnit *SUb = MemOpRecords[Idx+1].SU;
1531 if (TII->shouldClusterMemOps(*SUa->getInstr(), *SUb->getInstr(),
1533 DAG->addEdge(SUb, SDep(SUa, SDep::Cluster))) {
1534 DEBUG(dbgs() << "Cluster ld/st SU(" << SUa->NodeNum << ") - SU("
1535 << SUb->NodeNum << ")\n");
1536 // Copy successor edges from SUa to SUb. Interleaving computation
1537 // dependent on SUa can prevent load combining due to register reuse.
1538 // Predecessor edges do not need to be copied from SUb to SUa since nearby
1539 // loads should have effectively the same inputs.
1540 for (SUnit::const_succ_iterator
1541 SI = SUa->Succs.begin(), SE = SUa->Succs.end(); SI != SE; ++SI) {
1542 if (SI->getSUnit() == SUb)
1544 DEBUG(dbgs() << " Copy Succ SU(" << SI->getSUnit()->NodeNum << ")\n");
1545 DAG->addEdge(SI->getSUnit(), SDep(SUb, SDep::Artificial));
1553 /// \brief Callback from DAG postProcessing to create cluster edges for loads.
1554 void BaseMemOpClusterMutation::apply(ScheduleDAGInstrs *DAGInstrs) {
1556 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
1558 // Map DAG NodeNum to store chain ID.
1559 DenseMap<unsigned, unsigned> StoreChainIDs;
1560 // Map each store chain to a set of dependent MemOps.
1561 SmallVector<SmallVector<SUnit*,4>, 32> StoreChainDependents;
1562 for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) {
1563 SUnit *SU = &DAG->SUnits[Idx];
1564 if ((IsLoad && !SU->getInstr()->mayLoad()) ||
1565 (!IsLoad && !SU->getInstr()->mayStore()))
1568 unsigned ChainPredID = DAG->SUnits.size();
1569 for (SUnit::const_pred_iterator
1570 PI = SU->Preds.begin(), PE = SU->Preds.end(); PI != PE; ++PI) {
1572 ChainPredID = PI->getSUnit()->NodeNum;
1576 // Check if this chain-like pred has been seen
1577 // before. ChainPredID==MaxNodeID at the top of the schedule.
1578 unsigned NumChains = StoreChainDependents.size();
1579 std::pair<DenseMap<unsigned, unsigned>::iterator, bool> Result =
1580 StoreChainIDs.insert(std::make_pair(ChainPredID, NumChains));
1582 StoreChainDependents.resize(NumChains + 1);
1583 StoreChainDependents[Result.first->second].push_back(SU);
1586 // Iterate over the store chains.
1587 for (unsigned Idx = 0, End = StoreChainDependents.size(); Idx != End; ++Idx)
1588 clusterNeighboringMemOps(StoreChainDependents[Idx], DAG);
1591 //===----------------------------------------------------------------------===//
1592 // CopyConstrain - DAG post-processing to encourage copy elimination.
1593 //===----------------------------------------------------------------------===//
1597 /// \brief Post-process the DAG to create weak edges from all uses of a copy to
1598 /// the one use that defines the copy's source vreg, most likely an induction
1599 /// variable increment.
1600 class CopyConstrain : public ScheduleDAGMutation {
1602 SlotIndex RegionBeginIdx;
1603 // RegionEndIdx is the slot index of the last non-debug instruction in the
1604 // scheduling region. So we may have RegionBeginIdx == RegionEndIdx.
1605 SlotIndex RegionEndIdx;
1608 CopyConstrain(const TargetInstrInfo *, const TargetRegisterInfo *) {}
1610 void apply(ScheduleDAGInstrs *DAGInstrs) override;
1613 void constrainLocalCopy(SUnit *CopySU, ScheduleDAGMILive *DAG);
1616 } // end anonymous namespace
1620 std::unique_ptr<ScheduleDAGMutation>
1621 createCopyConstrainDAGMutation(const TargetInstrInfo *TII,
1622 const TargetRegisterInfo *TRI) {
1623 return llvm::make_unique<CopyConstrain>(TII, TRI);
1626 } // end namespace llvm
1628 /// constrainLocalCopy handles two possibilities:
1633 /// I3: dst = src (copy)
1634 /// (create pred->succ edges I0->I1, I2->I1)
1637 /// I0: dst = src (copy)
1641 /// (create pred->succ edges I1->I2, I3->I2)
1643 /// Although the MachineScheduler is currently constrained to single blocks,
1644 /// this algorithm should handle extended blocks. An EBB is a set of
1645 /// contiguously numbered blocks such that the previous block in the EBB is
1646 /// always the single predecessor.
1647 void CopyConstrain::constrainLocalCopy(SUnit *CopySU, ScheduleDAGMILive *DAG) {
1648 LiveIntervals *LIS = DAG->getLIS();
1649 MachineInstr *Copy = CopySU->getInstr();
1651 // Check for pure vreg copies.
1652 const MachineOperand &SrcOp = Copy->getOperand(1);
1653 unsigned SrcReg = SrcOp.getReg();
1654 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) || !SrcOp.readsReg())
1657 const MachineOperand &DstOp = Copy->getOperand(0);
1658 unsigned DstReg = DstOp.getReg();
1659 if (!TargetRegisterInfo::isVirtualRegister(DstReg) || DstOp.isDead())
1662 // Check if either the dest or source is local. If it's live across a back
1663 // edge, it's not local. Note that if both vregs are live across the back
1664 // edge, we cannot successfully contrain the copy without cyclic scheduling.
1665 // If both the copy's source and dest are local live intervals, then we
1666 // should treat the dest as the global for the purpose of adding
1667 // constraints. This adds edges from source's other uses to the copy.
1668 unsigned LocalReg = SrcReg;
1669 unsigned GlobalReg = DstReg;
1670 LiveInterval *LocalLI = &LIS->getInterval(LocalReg);
1671 if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx)) {
1674 LocalLI = &LIS->getInterval(LocalReg);
1675 if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx))
1678 LiveInterval *GlobalLI = &LIS->getInterval(GlobalReg);
1680 // Find the global segment after the start of the local LI.
1681 LiveInterval::iterator GlobalSegment = GlobalLI->find(LocalLI->beginIndex());
1682 // If GlobalLI does not overlap LocalLI->start, then a copy directly feeds a
1683 // local live range. We could create edges from other global uses to the local
1684 // start, but the coalescer should have already eliminated these cases, so
1685 // don't bother dealing with it.
1686 if (GlobalSegment == GlobalLI->end())
1689 // If GlobalSegment is killed at the LocalLI->start, the call to find()
1690 // returned the next global segment. But if GlobalSegment overlaps with
1691 // LocalLI->start, then advance to the next segement. If a hole in GlobalLI
1692 // exists in LocalLI's vicinity, GlobalSegment will be the end of the hole.
1693 if (GlobalSegment->contains(LocalLI->beginIndex()))
1696 if (GlobalSegment == GlobalLI->end())
1699 // Check if GlobalLI contains a hole in the vicinity of LocalLI.
1700 if (GlobalSegment != GlobalLI->begin()) {
1701 // Two address defs have no hole.
1702 if (SlotIndex::isSameInstr(std::prev(GlobalSegment)->end,
1703 GlobalSegment->start)) {
1706 // If the prior global segment may be defined by the same two-address
1707 // instruction that also defines LocalLI, then can't make a hole here.
1708 if (SlotIndex::isSameInstr(std::prev(GlobalSegment)->start,
1709 LocalLI->beginIndex())) {
1712 // If GlobalLI has a prior segment, it must be live into the EBB. Otherwise
1713 // it would be a disconnected component in the live range.
1714 assert(std::prev(GlobalSegment)->start < LocalLI->beginIndex() &&
1715 "Disconnected LRG within the scheduling region.");
1717 MachineInstr *GlobalDef = LIS->getInstructionFromIndex(GlobalSegment->start);
1721 SUnit *GlobalSU = DAG->getSUnit(GlobalDef);
1725 // GlobalDef is the bottom of the GlobalLI hole. Open the hole by
1726 // constraining the uses of the last local def to precede GlobalDef.
1727 SmallVector<SUnit*,8> LocalUses;
1728 const VNInfo *LastLocalVN = LocalLI->getVNInfoBefore(LocalLI->endIndex());
1729 MachineInstr *LastLocalDef = LIS->getInstructionFromIndex(LastLocalVN->def);
1730 SUnit *LastLocalSU = DAG->getSUnit(LastLocalDef);
1731 for (SUnit::const_succ_iterator
1732 I = LastLocalSU->Succs.begin(), E = LastLocalSU->Succs.end();
1734 if (I->getKind() != SDep::Data || I->getReg() != LocalReg)
1736 if (I->getSUnit() == GlobalSU)
1738 if (!DAG->canAddEdge(GlobalSU, I->getSUnit()))
1740 LocalUses.push_back(I->getSUnit());
1742 // Open the top of the GlobalLI hole by constraining any earlier global uses
1743 // to precede the start of LocalLI.
1744 SmallVector<SUnit*,8> GlobalUses;
1745 MachineInstr *FirstLocalDef =
1746 LIS->getInstructionFromIndex(LocalLI->beginIndex());
1747 SUnit *FirstLocalSU = DAG->getSUnit(FirstLocalDef);
1748 for (SUnit::const_pred_iterator
1749 I = GlobalSU->Preds.begin(), E = GlobalSU->Preds.end(); I != E; ++I) {
1750 if (I->getKind() != SDep::Anti || I->getReg() != GlobalReg)
1752 if (I->getSUnit() == FirstLocalSU)
1754 if (!DAG->canAddEdge(FirstLocalSU, I->getSUnit()))
1756 GlobalUses.push_back(I->getSUnit());
1758 DEBUG(dbgs() << "Constraining copy SU(" << CopySU->NodeNum << ")\n");
1759 // Add the weak edges.
1760 for (SmallVectorImpl<SUnit*>::const_iterator
1761 I = LocalUses.begin(), E = LocalUses.end(); I != E; ++I) {
1762 DEBUG(dbgs() << " Local use SU(" << (*I)->NodeNum << ") -> SU("
1763 << GlobalSU->NodeNum << ")\n");
1764 DAG->addEdge(GlobalSU, SDep(*I, SDep::Weak));
1766 for (SmallVectorImpl<SUnit*>::const_iterator
1767 I = GlobalUses.begin(), E = GlobalUses.end(); I != E; ++I) {
1768 DEBUG(dbgs() << " Global use SU(" << (*I)->NodeNum << ") -> SU("
1769 << FirstLocalSU->NodeNum << ")\n");
1770 DAG->addEdge(FirstLocalSU, SDep(*I, SDep::Weak));
1774 /// \brief Callback from DAG postProcessing to create weak edges to encourage
1775 /// copy elimination.
1776 void CopyConstrain::apply(ScheduleDAGInstrs *DAGInstrs) {
1777 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
1778 assert(DAG->hasVRegLiveness() && "Expect VRegs with LiveIntervals");
1780 MachineBasicBlock::iterator FirstPos = nextIfDebug(DAG->begin(), DAG->end());
1781 if (FirstPos == DAG->end())
1783 RegionBeginIdx = DAG->getLIS()->getInstructionIndex(*FirstPos);
1784 RegionEndIdx = DAG->getLIS()->getInstructionIndex(
1785 *priorNonDebug(DAG->end(), DAG->begin()));
1787 for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) {
1788 SUnit *SU = &DAG->SUnits[Idx];
1789 if (!SU->getInstr()->isCopy())
1792 constrainLocalCopy(SU, static_cast<ScheduleDAGMILive*>(DAG));
1796 //===----------------------------------------------------------------------===//
1797 // MachineSchedStrategy helpers used by GenericScheduler, GenericPostScheduler
1798 // and possibly other custom schedulers.
1799 //===----------------------------------------------------------------------===//
1801 static const unsigned InvalidCycle = ~0U;
1803 SchedBoundary::~SchedBoundary() { delete HazardRec; }
1805 void SchedBoundary::reset() {
1806 // A new HazardRec is created for each DAG and owned by SchedBoundary.
1807 // Destroying and reconstructing it is very expensive though. So keep
1808 // invalid, placeholder HazardRecs.
1809 if (HazardRec && HazardRec->isEnabled()) {
1811 HazardRec = nullptr;
1815 CheckPending = false;
1818 MinReadyCycle = std::numeric_limits<unsigned>::max();
1819 ExpectedLatency = 0;
1820 DependentLatency = 0;
1822 MaxExecutedResCount = 0;
1824 IsResourceLimited = false;
1825 ReservedCycles.clear();
1827 // Track the maximum number of stall cycles that could arise either from the
1828 // latency of a DAG edge or the number of cycles that a processor resource is
1829 // reserved (SchedBoundary::ReservedCycles).
1830 MaxObservedStall = 0;
1832 // Reserve a zero-count for invalid CritResIdx.
1833 ExecutedResCounts.resize(1);
1834 assert(!ExecutedResCounts[0] && "nonzero count for bad resource");
1837 void SchedRemainder::
1838 init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel) {
1840 if (!SchedModel->hasInstrSchedModel())
1842 RemainingCounts.resize(SchedModel->getNumProcResourceKinds());
1843 for (std::vector<SUnit>::iterator
1844 I = DAG->SUnits.begin(), E = DAG->SUnits.end(); I != E; ++I) {
1845 const MCSchedClassDesc *SC = DAG->getSchedClass(&*I);
1846 RemIssueCount += SchedModel->getNumMicroOps(I->getInstr(), SC)
1847 * SchedModel->getMicroOpFactor();
1848 for (TargetSchedModel::ProcResIter
1849 PI = SchedModel->getWriteProcResBegin(SC),
1850 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
1851 unsigned PIdx = PI->ProcResourceIdx;
1852 unsigned Factor = SchedModel->getResourceFactor(PIdx);
1853 RemainingCounts[PIdx] += (Factor * PI->Cycles);
1858 void SchedBoundary::
1859 init(ScheduleDAGMI *dag, const TargetSchedModel *smodel, SchedRemainder *rem) {
1862 SchedModel = smodel;
1864 if (SchedModel->hasInstrSchedModel()) {
1865 ExecutedResCounts.resize(SchedModel->getNumProcResourceKinds());
1866 ReservedCycles.resize(SchedModel->getNumProcResourceKinds(), InvalidCycle);
1870 /// Compute the stall cycles based on this SUnit's ready time. Heuristics treat
1871 /// these "soft stalls" differently than the hard stall cycles based on CPU
1872 /// resources and computed by checkHazard(). A fully in-order model
1873 /// (MicroOpBufferSize==0) will not make use of this since instructions are not
1874 /// available for scheduling until they are ready. However, a weaker in-order
1875 /// model may use this for heuristics. For example, if a processor has in-order
1876 /// behavior when reading certain resources, this may come into play.
1877 unsigned SchedBoundary::getLatencyStallCycles(SUnit *SU) {
1878 if (!SU->isUnbuffered)
1881 unsigned ReadyCycle = (isTop() ? SU->TopReadyCycle : SU->BotReadyCycle);
1882 if (ReadyCycle > CurrCycle)
1883 return ReadyCycle - CurrCycle;
1887 /// Compute the next cycle at which the given processor resource can be
1889 unsigned SchedBoundary::
1890 getNextResourceCycle(unsigned PIdx, unsigned Cycles) {
1891 unsigned NextUnreserved = ReservedCycles[PIdx];
1892 // If this resource has never been used, always return cycle zero.
1893 if (NextUnreserved == InvalidCycle)
1895 // For bottom-up scheduling add the cycles needed for the current operation.
1897 NextUnreserved += Cycles;
1898 return NextUnreserved;
1901 /// Does this SU have a hazard within the current instruction group.
1903 /// The scheduler supports two modes of hazard recognition. The first is the
1904 /// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that
1905 /// supports highly complicated in-order reservation tables
1906 /// (ScoreboardHazardRecognizer) and arbitraty target-specific logic.
1908 /// The second is a streamlined mechanism that checks for hazards based on
1909 /// simple counters that the scheduler itself maintains. It explicitly checks
1910 /// for instruction dispatch limitations, including the number of micro-ops that
1911 /// can dispatch per cycle.
1913 /// TODO: Also check whether the SU must start a new group.
1914 bool SchedBoundary::checkHazard(SUnit *SU) {
1915 if (HazardRec->isEnabled()
1916 && HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard) {
1920 unsigned uops = SchedModel->getNumMicroOps(SU->getInstr());
1921 if ((CurrMOps > 0) && (CurrMOps + uops > SchedModel->getIssueWidth())) {
1922 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") uops="
1923 << SchedModel->getNumMicroOps(SU->getInstr()) << '\n');
1928 ((isTop() && SchedModel->mustBeginGroup(SU->getInstr())) ||
1929 (!isTop() && SchedModel->mustEndGroup(SU->getInstr())))) {
1930 DEBUG(dbgs() << " hazard: SU(" << SU->NodeNum << ") must "
1931 << (isTop()? "begin" : "end") << " group\n");
1935 if (SchedModel->hasInstrSchedModel() && SU->hasReservedResource) {
1936 const MCSchedClassDesc *SC = DAG->getSchedClass(SU);
1937 for (TargetSchedModel::ProcResIter
1938 PI = SchedModel->getWriteProcResBegin(SC),
1939 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
1940 unsigned NRCycle = getNextResourceCycle(PI->ProcResourceIdx, PI->Cycles);
1941 if (NRCycle > CurrCycle) {
1943 MaxObservedStall = std::max(PI->Cycles, MaxObservedStall);
1945 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") "
1946 << SchedModel->getResourceName(PI->ProcResourceIdx)
1947 << "=" << NRCycle << "c\n");
1955 // Find the unscheduled node in ReadySUs with the highest latency.
1956 unsigned SchedBoundary::
1957 findMaxLatency(ArrayRef<SUnit*> ReadySUs) {
1958 SUnit *LateSU = nullptr;
1959 unsigned RemLatency = 0;
1960 for (ArrayRef<SUnit*>::iterator I = ReadySUs.begin(), E = ReadySUs.end();
1962 unsigned L = getUnscheduledLatency(*I);
1963 if (L > RemLatency) {
1969 DEBUG(dbgs() << Available.getName() << " RemLatency SU("
1970 << LateSU->NodeNum << ") " << RemLatency << "c\n");
1975 // Count resources in this zone and the remaining unscheduled
1976 // instruction. Return the max count, scaled. Set OtherCritIdx to the critical
1977 // resource index, or zero if the zone is issue limited.
1978 unsigned SchedBoundary::
1979 getOtherResourceCount(unsigned &OtherCritIdx) {
1981 if (!SchedModel->hasInstrSchedModel())
1984 unsigned OtherCritCount = Rem->RemIssueCount
1985 + (RetiredMOps * SchedModel->getMicroOpFactor());
1986 DEBUG(dbgs() << " " << Available.getName() << " + Remain MOps: "
1987 << OtherCritCount / SchedModel->getMicroOpFactor() << '\n');
1988 for (unsigned PIdx = 1, PEnd = SchedModel->getNumProcResourceKinds();
1989 PIdx != PEnd; ++PIdx) {
1990 unsigned OtherCount = getResourceCount(PIdx) + Rem->RemainingCounts[PIdx];
1991 if (OtherCount > OtherCritCount) {
1992 OtherCritCount = OtherCount;
1993 OtherCritIdx = PIdx;
1997 DEBUG(dbgs() << " " << Available.getName() << " + Remain CritRes: "
1998 << OtherCritCount / SchedModel->getResourceFactor(OtherCritIdx)
1999 << " " << SchedModel->getResourceName(OtherCritIdx) << "\n");
2001 return OtherCritCount;
2004 void SchedBoundary::releaseNode(SUnit *SU, unsigned ReadyCycle) {
2005 assert(SU->getInstr() && "Scheduled SUnit must have instr");
2008 // ReadyCycle was been bumped up to the CurrCycle when this node was
2009 // scheduled, but CurrCycle may have been eagerly advanced immediately after
2010 // scheduling, so may now be greater than ReadyCycle.
2011 if (ReadyCycle > CurrCycle)
2012 MaxObservedStall = std::max(ReadyCycle - CurrCycle, MaxObservedStall);
2015 if (ReadyCycle < MinReadyCycle)
2016 MinReadyCycle = ReadyCycle;
2018 // Check for interlocks first. For the purpose of other heuristics, an
2019 // instruction that cannot issue appears as if it's not in the ReadyQueue.
2020 bool IsBuffered = SchedModel->getMicroOpBufferSize() != 0;
2021 if ((!IsBuffered && ReadyCycle > CurrCycle) || checkHazard(SU) ||
2022 Available.size() >= ReadyListLimit)
2028 /// Move the boundary of scheduled code by one cycle.
2029 void SchedBoundary::bumpCycle(unsigned NextCycle) {
2030 if (SchedModel->getMicroOpBufferSize() == 0) {
2031 assert(MinReadyCycle < std::numeric_limits<unsigned>::max() &&
2032 "MinReadyCycle uninitialized");
2033 if (MinReadyCycle > NextCycle)
2034 NextCycle = MinReadyCycle;
2036 // Update the current micro-ops, which will issue in the next cycle.
2037 unsigned DecMOps = SchedModel->getIssueWidth() * (NextCycle - CurrCycle);
2038 CurrMOps = (CurrMOps <= DecMOps) ? 0 : CurrMOps - DecMOps;
2040 // Decrement DependentLatency based on the next cycle.
2041 if ((NextCycle - CurrCycle) > DependentLatency)
2042 DependentLatency = 0;
2044 DependentLatency -= (NextCycle - CurrCycle);
2046 if (!HazardRec->isEnabled()) {
2047 // Bypass HazardRec virtual calls.
2048 CurrCycle = NextCycle;
2050 // Bypass getHazardType calls in case of long latency.
2051 for (; CurrCycle != NextCycle; ++CurrCycle) {
2053 HazardRec->AdvanceCycle();
2055 HazardRec->RecedeCycle();
2058 CheckPending = true;
2059 unsigned LFactor = SchedModel->getLatencyFactor();
2061 (int)(getCriticalCount() - (getScheduledLatency() * LFactor))
2064 DEBUG(dbgs() << "Cycle: " << CurrCycle << ' ' << Available.getName() << '\n');
2067 void SchedBoundary::incExecutedResources(unsigned PIdx, unsigned Count) {
2068 ExecutedResCounts[PIdx] += Count;
2069 if (ExecutedResCounts[PIdx] > MaxExecutedResCount)
2070 MaxExecutedResCount = ExecutedResCounts[PIdx];
2073 /// Add the given processor resource to this scheduled zone.
2075 /// \param Cycles indicates the number of consecutive (non-pipelined) cycles
2076 /// during which this resource is consumed.
2078 /// \return the next cycle at which the instruction may execute without
2079 /// oversubscribing resources.
2080 unsigned SchedBoundary::
2081 countResource(unsigned PIdx, unsigned Cycles, unsigned NextCycle) {
2082 unsigned Factor = SchedModel->getResourceFactor(PIdx);
2083 unsigned Count = Factor * Cycles;
2084 DEBUG(dbgs() << " " << SchedModel->getResourceName(PIdx)
2085 << " +" << Cycles << "x" << Factor << "u\n");
2087 // Update Executed resources counts.
2088 incExecutedResources(PIdx, Count);
2089 assert(Rem->RemainingCounts[PIdx] >= Count && "resource double counted");
2090 Rem->RemainingCounts[PIdx] -= Count;
2092 // Check if this resource exceeds the current critical resource. If so, it
2093 // becomes the critical resource.
2094 if (ZoneCritResIdx != PIdx && (getResourceCount(PIdx) > getCriticalCount())) {
2095 ZoneCritResIdx = PIdx;
2096 DEBUG(dbgs() << " *** Critical resource "
2097 << SchedModel->getResourceName(PIdx) << ": "
2098 << getResourceCount(PIdx) / SchedModel->getLatencyFactor() << "c\n");
2100 // For reserved resources, record the highest cycle using the resource.
2101 unsigned NextAvailable = getNextResourceCycle(PIdx, Cycles);
2102 if (NextAvailable > CurrCycle) {
2103 DEBUG(dbgs() << " Resource conflict: "
2104 << SchedModel->getProcResource(PIdx)->Name << " reserved until @"
2105 << NextAvailable << "\n");
2107 return NextAvailable;
2110 /// Move the boundary of scheduled code by one SUnit.
2111 void SchedBoundary::bumpNode(SUnit *SU) {
2112 // Update the reservation table.
2113 if (HazardRec->isEnabled()) {
2114 if (!isTop() && SU->isCall) {
2115 // Calls are scheduled with their preceding instructions. For bottom-up
2116 // scheduling, clear the pipeline state before emitting.
2119 HazardRec->EmitInstruction(SU);
2121 // checkHazard should prevent scheduling multiple instructions per cycle that
2122 // exceed the issue width.
2123 const MCSchedClassDesc *SC = DAG->getSchedClass(SU);
2124 unsigned IncMOps = SchedModel->getNumMicroOps(SU->getInstr());
2126 (CurrMOps == 0 || (CurrMOps + IncMOps) <= SchedModel->getIssueWidth()) &&
2127 "Cannot schedule this instruction's MicroOps in the current cycle.");
2129 unsigned ReadyCycle = (isTop() ? SU->TopReadyCycle : SU->BotReadyCycle);
2130 DEBUG(dbgs() << " Ready @" << ReadyCycle << "c\n");
2132 unsigned NextCycle = CurrCycle;
2133 switch (SchedModel->getMicroOpBufferSize()) {
2135 assert(ReadyCycle <= CurrCycle && "Broken PendingQueue");
2138 if (ReadyCycle > NextCycle) {
2139 NextCycle = ReadyCycle;
2140 DEBUG(dbgs() << " *** Stall until: " << ReadyCycle << "\n");
2144 // We don't currently model the OOO reorder buffer, so consider all
2145 // scheduled MOps to be "retired". We do loosely model in-order resource
2146 // latency. If this instruction uses an in-order resource, account for any
2147 // likely stall cycles.
2148 if (SU->isUnbuffered && ReadyCycle > NextCycle)
2149 NextCycle = ReadyCycle;
2152 RetiredMOps += IncMOps;
2154 // Update resource counts and critical resource.
2155 if (SchedModel->hasInstrSchedModel()) {
2156 unsigned DecRemIssue = IncMOps * SchedModel->getMicroOpFactor();
2157 assert(Rem->RemIssueCount >= DecRemIssue && "MOps double counted");
2158 Rem->RemIssueCount -= DecRemIssue;
2159 if (ZoneCritResIdx) {
2160 // Scale scheduled micro-ops for comparing with the critical resource.
2161 unsigned ScaledMOps =
2162 RetiredMOps * SchedModel->getMicroOpFactor();
2164 // If scaled micro-ops are now more than the previous critical resource by
2165 // a full cycle, then micro-ops issue becomes critical.
2166 if ((int)(ScaledMOps - getResourceCount(ZoneCritResIdx))
2167 >= (int)SchedModel->getLatencyFactor()) {
2169 DEBUG(dbgs() << " *** Critical resource NumMicroOps: "
2170 << ScaledMOps / SchedModel->getLatencyFactor() << "c\n");
2173 for (TargetSchedModel::ProcResIter
2174 PI = SchedModel->getWriteProcResBegin(SC),
2175 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
2177 countResource(PI->ProcResourceIdx, PI->Cycles, NextCycle);
2178 if (RCycle > NextCycle)
2181 if (SU->hasReservedResource) {
2182 // For reserved resources, record the highest cycle using the resource.
2183 // For top-down scheduling, this is the cycle in which we schedule this
2184 // instruction plus the number of cycles the operations reserves the
2185 // resource. For bottom-up is it simply the instruction's cycle.
2186 for (TargetSchedModel::ProcResIter
2187 PI = SchedModel->getWriteProcResBegin(SC),
2188 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
2189 unsigned PIdx = PI->ProcResourceIdx;
2190 if (SchedModel->getProcResource(PIdx)->BufferSize == 0) {
2192 ReservedCycles[PIdx] =
2193 std::max(getNextResourceCycle(PIdx, 0), NextCycle + PI->Cycles);
2196 ReservedCycles[PIdx] = NextCycle;
2201 // Update ExpectedLatency and DependentLatency.
2202 unsigned &TopLatency = isTop() ? ExpectedLatency : DependentLatency;
2203 unsigned &BotLatency = isTop() ? DependentLatency : ExpectedLatency;
2204 if (SU->getDepth() > TopLatency) {
2205 TopLatency = SU->getDepth();
2206 DEBUG(dbgs() << " " << Available.getName()
2207 << " TopLatency SU(" << SU->NodeNum << ") " << TopLatency << "c\n");
2209 if (SU->getHeight() > BotLatency) {
2210 BotLatency = SU->getHeight();
2211 DEBUG(dbgs() << " " << Available.getName()
2212 << " BotLatency SU(" << SU->NodeNum << ") " << BotLatency << "c\n");
2214 // If we stall for any reason, bump the cycle.
2215 if (NextCycle > CurrCycle) {
2216 bumpCycle(NextCycle);
2218 // After updating ZoneCritResIdx and ExpectedLatency, check if we're
2219 // resource limited. If a stall occurred, bumpCycle does this.
2220 unsigned LFactor = SchedModel->getLatencyFactor();
2222 (int)(getCriticalCount() - (getScheduledLatency() * LFactor))
2225 // Update CurrMOps after calling bumpCycle to handle stalls, since bumpCycle
2226 // resets CurrMOps. Loop to handle instructions with more MOps than issue in
2227 // one cycle. Since we commonly reach the max MOps here, opportunistically
2228 // bump the cycle to avoid uselessly checking everything in the readyQ.
2229 CurrMOps += IncMOps;
2231 // Bump the cycle count for issue group constraints.
2232 // This must be done after NextCycle has been adjust for all other stalls.
2233 // Calling bumpCycle(X) will reduce CurrMOps by one issue group and set
2235 if ((isTop() && SchedModel->mustEndGroup(SU->getInstr())) ||
2236 (!isTop() && SchedModel->mustBeginGroup(SU->getInstr()))) {
2237 DEBUG(dbgs() << " Bump cycle to "
2238 << (isTop() ? "end" : "begin") << " group\n");
2239 bumpCycle(++NextCycle);
2242 while (CurrMOps >= SchedModel->getIssueWidth()) {
2243 DEBUG(dbgs() << " *** Max MOps " << CurrMOps
2244 << " at cycle " << CurrCycle << '\n');
2245 bumpCycle(++NextCycle);
2247 DEBUG(dumpScheduledState());
2250 /// Release pending ready nodes in to the available queue. This makes them
2251 /// visible to heuristics.
2252 void SchedBoundary::releasePending() {
2253 // If the available queue is empty, it is safe to reset MinReadyCycle.
2254 if (Available.empty())
2255 MinReadyCycle = std::numeric_limits<unsigned>::max();
2257 // Check to see if any of the pending instructions are ready to issue. If
2258 // so, add them to the available queue.
2259 bool IsBuffered = SchedModel->getMicroOpBufferSize() != 0;
2260 for (unsigned i = 0, e = Pending.size(); i != e; ++i) {
2261 SUnit *SU = *(Pending.begin()+i);
2262 unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle;
2264 if (ReadyCycle < MinReadyCycle)
2265 MinReadyCycle = ReadyCycle;
2267 if (!IsBuffered && ReadyCycle > CurrCycle)
2270 if (checkHazard(SU))
2273 if (Available.size() >= ReadyListLimit)
2277 Pending.remove(Pending.begin()+i);
2280 CheckPending = false;
2283 /// Remove SU from the ready set for this boundary.
2284 void SchedBoundary::removeReady(SUnit *SU) {
2285 if (Available.isInQueue(SU))
2286 Available.remove(Available.find(SU));
2288 assert(Pending.isInQueue(SU) && "bad ready count");
2289 Pending.remove(Pending.find(SU));
2293 /// If this queue only has one ready candidate, return it. As a side effect,
2294 /// defer any nodes that now hit a hazard, and advance the cycle until at least
2295 /// one node is ready. If multiple instructions are ready, return NULL.
2296 SUnit *SchedBoundary::pickOnlyChoice() {
2301 // Defer any ready instrs that now have a hazard.
2302 for (ReadyQueue::iterator I = Available.begin(); I != Available.end();) {
2303 if (checkHazard(*I)) {
2305 I = Available.remove(I);
2311 for (unsigned i = 0; Available.empty(); ++i) {
2312 // FIXME: Re-enable assert once PR20057 is resolved.
2313 // assert(i <= (HazardRec->getMaxLookAhead() + MaxObservedStall) &&
2314 // "permanent hazard");
2316 bumpCycle(CurrCycle + 1);
2320 DEBUG(Pending.dump());
2321 DEBUG(Available.dump());
2323 if (Available.size() == 1)
2324 return *Available.begin();
2328 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2329 // This is useful information to dump after bumpNode.
2330 // Note that the Queue contents are more useful before pickNodeFromQueue.
2331 LLVM_DUMP_METHOD void SchedBoundary::dumpScheduledState() {
2334 if (ZoneCritResIdx) {
2335 ResFactor = SchedModel->getResourceFactor(ZoneCritResIdx);
2336 ResCount = getResourceCount(ZoneCritResIdx);
2338 ResFactor = SchedModel->getMicroOpFactor();
2339 ResCount = RetiredMOps * SchedModel->getMicroOpFactor();
2341 unsigned LFactor = SchedModel->getLatencyFactor();
2342 dbgs() << Available.getName() << " @" << CurrCycle << "c\n"
2343 << " Retired: " << RetiredMOps;
2344 dbgs() << "\n Executed: " << getExecutedCount() / LFactor << "c";
2345 dbgs() << "\n Critical: " << ResCount / LFactor << "c, "
2346 << ResCount / ResFactor << " "
2347 << SchedModel->getResourceName(ZoneCritResIdx)
2348 << "\n ExpectedLatency: " << ExpectedLatency << "c\n"
2349 << (IsResourceLimited ? " - Resource" : " - Latency")
2354 //===----------------------------------------------------------------------===//
2355 // GenericScheduler - Generic implementation of MachineSchedStrategy.
2356 //===----------------------------------------------------------------------===//
2358 void GenericSchedulerBase::SchedCandidate::
2359 initResourceDelta(const ScheduleDAGMI *DAG,
2360 const TargetSchedModel *SchedModel) {
2361 if (!Policy.ReduceResIdx && !Policy.DemandResIdx)
2364 const MCSchedClassDesc *SC = DAG->getSchedClass(SU);
2365 for (TargetSchedModel::ProcResIter
2366 PI = SchedModel->getWriteProcResBegin(SC),
2367 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
2368 if (PI->ProcResourceIdx == Policy.ReduceResIdx)
2369 ResDelta.CritResources += PI->Cycles;
2370 if (PI->ProcResourceIdx == Policy.DemandResIdx)
2371 ResDelta.DemandedResources += PI->Cycles;
2375 /// Set the CandPolicy given a scheduling zone given the current resources and
2376 /// latencies inside and outside the zone.
2377 void GenericSchedulerBase::setPolicy(CandPolicy &Policy, bool IsPostRA,
2378 SchedBoundary &CurrZone,
2379 SchedBoundary *OtherZone) {
2380 // Apply preemptive heuristics based on the total latency and resources
2381 // inside and outside this zone. Potential stalls should be considered before
2382 // following this policy.
2384 // Compute remaining latency. We need this both to determine whether the
2385 // overall schedule has become latency-limited and whether the instructions
2386 // outside this zone are resource or latency limited.
2388 // The "dependent" latency is updated incrementally during scheduling as the
2389 // max height/depth of scheduled nodes minus the cycles since it was
2391 // DLat = max (N.depth - (CurrCycle - N.ReadyCycle) for N in Zone
2393 // The "independent" latency is the max ready queue depth:
2394 // ILat = max N.depth for N in Available|Pending
2396 // RemainingLatency is the greater of independent and dependent latency.
2397 unsigned RemLatency = CurrZone.getDependentLatency();
2398 RemLatency = std::max(RemLatency,
2399 CurrZone.findMaxLatency(CurrZone.Available.elements()));
2400 RemLatency = std::max(RemLatency,
2401 CurrZone.findMaxLatency(CurrZone.Pending.elements()));
2403 // Compute the critical resource outside the zone.
2404 unsigned OtherCritIdx = 0;
2405 unsigned OtherCount =
2406 OtherZone ? OtherZone->getOtherResourceCount(OtherCritIdx) : 0;
2408 bool OtherResLimited = false;
2409 if (SchedModel->hasInstrSchedModel()) {
2410 unsigned LFactor = SchedModel->getLatencyFactor();
2411 OtherResLimited = (int)(OtherCount - (RemLatency * LFactor)) > (int)LFactor;
2413 // Schedule aggressively for latency in PostRA mode. We don't check for
2414 // acyclic latency during PostRA, and highly out-of-order processors will
2415 // skip PostRA scheduling.
2416 if (!OtherResLimited) {
2417 if (IsPostRA || (RemLatency + CurrZone.getCurrCycle() > Rem.CriticalPath)) {
2418 Policy.ReduceLatency |= true;
2419 DEBUG(dbgs() << " " << CurrZone.Available.getName()
2420 << " RemainingLatency " << RemLatency << " + "
2421 << CurrZone.getCurrCycle() << "c > CritPath "
2422 << Rem.CriticalPath << "\n");
2425 // If the same resource is limiting inside and outside the zone, do nothing.
2426 if (CurrZone.getZoneCritResIdx() == OtherCritIdx)
2430 if (CurrZone.isResourceLimited()) {
2431 dbgs() << " " << CurrZone.Available.getName() << " ResourceLimited: "
2432 << SchedModel->getResourceName(CurrZone.getZoneCritResIdx())
2435 if (OtherResLimited)
2436 dbgs() << " RemainingLimit: "
2437 << SchedModel->getResourceName(OtherCritIdx) << "\n";
2438 if (!CurrZone.isResourceLimited() && !OtherResLimited)
2439 dbgs() << " Latency limited both directions.\n");
2441 if (CurrZone.isResourceLimited() && !Policy.ReduceResIdx)
2442 Policy.ReduceResIdx = CurrZone.getZoneCritResIdx();
2444 if (OtherResLimited)
2445 Policy.DemandResIdx = OtherCritIdx;
2449 const char *GenericSchedulerBase::getReasonStr(
2450 GenericSchedulerBase::CandReason Reason) {
2452 case NoCand: return "NOCAND ";
2453 case Only1: return "ONLY1 ";
2454 case PhysRegCopy: return "PREG-COPY ";
2455 case RegExcess: return "REG-EXCESS";
2456 case RegCritical: return "REG-CRIT ";
2457 case Stall: return "STALL ";
2458 case Cluster: return "CLUSTER ";
2459 case Weak: return "WEAK ";
2460 case RegMax: return "REG-MAX ";
2461 case ResourceReduce: return "RES-REDUCE";
2462 case ResourceDemand: return "RES-DEMAND";
2463 case TopDepthReduce: return "TOP-DEPTH ";
2464 case TopPathReduce: return "TOP-PATH ";
2465 case BotHeightReduce:return "BOT-HEIGHT";
2466 case BotPathReduce: return "BOT-PATH ";
2467 case NextDefUse: return "DEF-USE ";
2468 case NodeOrder: return "ORDER ";
2470 llvm_unreachable("Unknown reason!");
2473 void GenericSchedulerBase::traceCandidate(const SchedCandidate &Cand) {
2475 unsigned ResIdx = 0;
2476 unsigned Latency = 0;
2477 switch (Cand.Reason) {
2481 P = Cand.RPDelta.Excess;
2484 P = Cand.RPDelta.CriticalMax;
2487 P = Cand.RPDelta.CurrentMax;
2489 case ResourceReduce:
2490 ResIdx = Cand.Policy.ReduceResIdx;
2492 case ResourceDemand:
2493 ResIdx = Cand.Policy.DemandResIdx;
2495 case TopDepthReduce:
2496 Latency = Cand.SU->getDepth();
2499 Latency = Cand.SU->getHeight();
2501 case BotHeightReduce:
2502 Latency = Cand.SU->getHeight();
2505 Latency = Cand.SU->getDepth();
2508 dbgs() << " Cand SU(" << Cand.SU->NodeNum << ") " << getReasonStr(Cand.Reason);
2510 dbgs() << " " << TRI->getRegPressureSetName(P.getPSet())
2511 << ":" << P.getUnitInc() << " ";
2515 dbgs() << " " << SchedModel->getProcResource(ResIdx)->Name << " ";
2519 dbgs() << " " << Latency << " cycles ";
2526 /// Return true if this heuristic determines order.
2527 static bool tryLess(int TryVal, int CandVal,
2528 GenericSchedulerBase::SchedCandidate &TryCand,
2529 GenericSchedulerBase::SchedCandidate &Cand,
2530 GenericSchedulerBase::CandReason Reason) {
2531 if (TryVal < CandVal) {
2532 TryCand.Reason = Reason;
2535 if (TryVal > CandVal) {
2536 if (Cand.Reason > Reason)
2537 Cand.Reason = Reason;
2543 static bool tryGreater(int TryVal, int CandVal,
2544 GenericSchedulerBase::SchedCandidate &TryCand,
2545 GenericSchedulerBase::SchedCandidate &Cand,
2546 GenericSchedulerBase::CandReason Reason) {
2547 if (TryVal > CandVal) {
2548 TryCand.Reason = Reason;
2551 if (TryVal < CandVal) {
2552 if (Cand.Reason > Reason)
2553 Cand.Reason = Reason;
2559 static bool tryLatency(GenericSchedulerBase::SchedCandidate &TryCand,
2560 GenericSchedulerBase::SchedCandidate &Cand,
2561 SchedBoundary &Zone) {
2563 if (Cand.SU->getDepth() > Zone.getScheduledLatency()) {
2564 if (tryLess(TryCand.SU->getDepth(), Cand.SU->getDepth(),
2565 TryCand, Cand, GenericSchedulerBase::TopDepthReduce))
2568 if (tryGreater(TryCand.SU->getHeight(), Cand.SU->getHeight(),
2569 TryCand, Cand, GenericSchedulerBase::TopPathReduce))
2572 if (Cand.SU->getHeight() > Zone.getScheduledLatency()) {
2573 if (tryLess(TryCand.SU->getHeight(), Cand.SU->getHeight(),
2574 TryCand, Cand, GenericSchedulerBase::BotHeightReduce))
2577 if (tryGreater(TryCand.SU->getDepth(), Cand.SU->getDepth(),
2578 TryCand, Cand, GenericSchedulerBase::BotPathReduce))
2584 static void tracePick(GenericSchedulerBase::CandReason Reason, bool IsTop) {
2585 DEBUG(dbgs() << "Pick " << (IsTop ? "Top " : "Bot ")
2586 << GenericSchedulerBase::getReasonStr(Reason) << '\n');
2589 static void tracePick(const GenericSchedulerBase::SchedCandidate &Cand) {
2590 tracePick(Cand.Reason, Cand.AtTop);
2593 void GenericScheduler::initialize(ScheduleDAGMI *dag) {
2594 assert(dag->hasVRegLiveness() &&
2595 "(PreRA)GenericScheduler needs vreg liveness");
2596 DAG = static_cast<ScheduleDAGMILive*>(dag);
2597 SchedModel = DAG->getSchedModel();
2600 Rem.init(DAG, SchedModel);
2601 Top.init(DAG, SchedModel, &Rem);
2602 Bot.init(DAG, SchedModel, &Rem);
2604 // Initialize resource counts.
2606 // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or
2607 // are disabled, then these HazardRecs will be disabled.
2608 const InstrItineraryData *Itin = SchedModel->getInstrItineraries();
2609 if (!Top.HazardRec) {
2611 DAG->MF.getSubtarget().getInstrInfo()->CreateTargetMIHazardRecognizer(
2614 if (!Bot.HazardRec) {
2616 DAG->MF.getSubtarget().getInstrInfo()->CreateTargetMIHazardRecognizer(
2619 TopCand.SU = nullptr;
2620 BotCand.SU = nullptr;
2623 /// Initialize the per-region scheduling policy.
2624 void GenericScheduler::initPolicy(MachineBasicBlock::iterator Begin,
2625 MachineBasicBlock::iterator End,
2626 unsigned NumRegionInstrs) {
2627 const MachineFunction &MF = *Begin->getParent()->getParent();
2628 const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
2630 // Avoid setting up the register pressure tracker for small regions to save
2631 // compile time. As a rough heuristic, only track pressure when the number of
2632 // schedulable instructions exceeds half the integer register file.
2633 RegionPolicy.ShouldTrackPressure = true;
2634 for (unsigned VT = MVT::i32; VT > (unsigned)MVT::i1; --VT) {
2635 MVT::SimpleValueType LegalIntVT = (MVT::SimpleValueType)VT;
2636 if (TLI->isTypeLegal(LegalIntVT)) {
2637 unsigned NIntRegs = Context->RegClassInfo->getNumAllocatableRegs(
2638 TLI->getRegClassFor(LegalIntVT));
2639 RegionPolicy.ShouldTrackPressure = NumRegionInstrs > (NIntRegs / 2);
2643 // For generic targets, we default to bottom-up, because it's simpler and more
2644 // compile-time optimizations have been implemented in that direction.
2645 RegionPolicy.OnlyBottomUp = true;
2647 // Allow the subtarget to override default policy.
2648 MF.getSubtarget().overrideSchedPolicy(RegionPolicy, NumRegionInstrs);
2650 // After subtarget overrides, apply command line options.
2651 if (!EnableRegPressure)
2652 RegionPolicy.ShouldTrackPressure = false;
2654 // Check -misched-topdown/bottomup can force or unforce scheduling direction.
2655 // e.g. -misched-bottomup=false allows scheduling in both directions.
2656 assert((!ForceTopDown || !ForceBottomUp) &&
2657 "-misched-topdown incompatible with -misched-bottomup");
2658 if (ForceBottomUp.getNumOccurrences() > 0) {
2659 RegionPolicy.OnlyBottomUp = ForceBottomUp;
2660 if (RegionPolicy.OnlyBottomUp)
2661 RegionPolicy.OnlyTopDown = false;
2663 if (ForceTopDown.getNumOccurrences() > 0) {
2664 RegionPolicy.OnlyTopDown = ForceTopDown;
2665 if (RegionPolicy.OnlyTopDown)
2666 RegionPolicy.OnlyBottomUp = false;
2670 void GenericScheduler::dumpPolicy() {
2671 // Cannot completely remove virtual function even in release mode.
2672 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2673 dbgs() << "GenericScheduler RegionPolicy: "
2674 << " ShouldTrackPressure=" << RegionPolicy.ShouldTrackPressure
2675 << " OnlyTopDown=" << RegionPolicy.OnlyTopDown
2676 << " OnlyBottomUp=" << RegionPolicy.OnlyBottomUp
2681 /// Set IsAcyclicLatencyLimited if the acyclic path is longer than the cyclic
2682 /// critical path by more cycles than it takes to drain the instruction buffer.
2683 /// We estimate an upper bounds on in-flight instructions as:
2685 /// CyclesPerIteration = max( CyclicPath, Loop-Resource-Height )
2686 /// InFlightIterations = AcyclicPath / CyclesPerIteration
2687 /// InFlightResources = InFlightIterations * LoopResources
2689 /// TODO: Check execution resources in addition to IssueCount.
2690 void GenericScheduler::checkAcyclicLatency() {
2691 if (Rem.CyclicCritPath == 0 || Rem.CyclicCritPath >= Rem.CriticalPath)
2694 // Scaled number of cycles per loop iteration.
2695 unsigned IterCount =
2696 std::max(Rem.CyclicCritPath * SchedModel->getLatencyFactor(),
2698 // Scaled acyclic critical path.
2699 unsigned AcyclicCount = Rem.CriticalPath * SchedModel->getLatencyFactor();
2700 // InFlightCount = (AcyclicPath / IterCycles) * InstrPerLoop
2701 unsigned InFlightCount =
2702 (AcyclicCount * Rem.RemIssueCount + IterCount-1) / IterCount;
2703 unsigned BufferLimit =
2704 SchedModel->getMicroOpBufferSize() * SchedModel->getMicroOpFactor();
2706 Rem.IsAcyclicLatencyLimited = InFlightCount > BufferLimit;
2708 DEBUG(dbgs() << "IssueCycles="
2709 << Rem.RemIssueCount / SchedModel->getLatencyFactor() << "c "
2710 << "IterCycles=" << IterCount / SchedModel->getLatencyFactor()
2711 << "c NumIters=" << (AcyclicCount + IterCount-1) / IterCount
2712 << " InFlight=" << InFlightCount / SchedModel->getMicroOpFactor()
2713 << "m BufferLim=" << SchedModel->getMicroOpBufferSize() << "m\n";
2714 if (Rem.IsAcyclicLatencyLimited)
2715 dbgs() << " ACYCLIC LATENCY LIMIT\n");
2718 void GenericScheduler::registerRoots() {
2719 Rem.CriticalPath = DAG->ExitSU.getDepth();
2721 // Some roots may not feed into ExitSU. Check all of them in case.
2722 for (std::vector<SUnit*>::const_iterator
2723 I = Bot.Available.begin(), E = Bot.Available.end(); I != E; ++I) {
2724 if ((*I)->getDepth() > Rem.CriticalPath)
2725 Rem.CriticalPath = (*I)->getDepth();
2727 DEBUG(dbgs() << "Critical Path(GS-RR ): " << Rem.CriticalPath << '\n');
2728 if (DumpCriticalPathLength) {
2729 errs() << "Critical Path(GS-RR ): " << Rem.CriticalPath << " \n";
2732 if (EnableCyclicPath && SchedModel->getMicroOpBufferSize() > 0) {
2733 Rem.CyclicCritPath = DAG->computeCyclicCriticalPath();
2734 checkAcyclicLatency();
2738 static bool tryPressure(const PressureChange &TryP,
2739 const PressureChange &CandP,
2740 GenericSchedulerBase::SchedCandidate &TryCand,
2741 GenericSchedulerBase::SchedCandidate &Cand,
2742 GenericSchedulerBase::CandReason Reason,
2743 const TargetRegisterInfo *TRI,
2744 const MachineFunction &MF) {
2745 // If one candidate decreases and the other increases, go with it.
2746 // Invalid candidates have UnitInc==0.
2747 if (tryGreater(TryP.getUnitInc() < 0, CandP.getUnitInc() < 0, TryCand, Cand,
2751 // Do not compare the magnitude of pressure changes between top and bottom
2753 if (Cand.AtTop != TryCand.AtTop)
2756 // If both candidates affect the same set in the same boundary, go with the
2757 // smallest increase.
2758 unsigned TryPSet = TryP.getPSetOrMax();
2759 unsigned CandPSet = CandP.getPSetOrMax();
2760 if (TryPSet == CandPSet) {
2761 return tryLess(TryP.getUnitInc(), CandP.getUnitInc(), TryCand, Cand,
2765 int TryRank = TryP.isValid() ? TRI->getRegPressureSetScore(MF, TryPSet) :
2766 std::numeric_limits<int>::max();
2768 int CandRank = CandP.isValid() ? TRI->getRegPressureSetScore(MF, CandPSet) :
2769 std::numeric_limits<int>::max();
2771 // If the candidates are decreasing pressure, reverse priority.
2772 if (TryP.getUnitInc() < 0)
2773 std::swap(TryRank, CandRank);
2774 return tryGreater(TryRank, CandRank, TryCand, Cand, Reason);
2777 static unsigned getWeakLeft(const SUnit *SU, bool isTop) {
2778 return (isTop) ? SU->WeakPredsLeft : SU->WeakSuccsLeft;
2781 /// Minimize physical register live ranges. Regalloc wants them adjacent to
2782 /// their physreg def/use.
2784 /// FIXME: This is an unnecessary check on the critical path. Most are root/leaf
2785 /// copies which can be prescheduled. The rest (e.g. x86 MUL) could be bundled
2786 /// with the operation that produces or consumes the physreg. We'll do this when
2787 /// regalloc has support for parallel copies.
2788 static int biasPhysRegCopy(const SUnit *SU, bool isTop) {
2789 const MachineInstr *MI = SU->getInstr();
2793 unsigned ScheduledOper = isTop ? 1 : 0;
2794 unsigned UnscheduledOper = isTop ? 0 : 1;
2795 // If we have already scheduled the physreg produce/consumer, immediately
2796 // schedule the copy.
2797 if (TargetRegisterInfo::isPhysicalRegister(
2798 MI->getOperand(ScheduledOper).getReg()))
2800 // If the physreg is at the boundary, defer it. Otherwise schedule it
2801 // immediately to free the dependent. We can hoist the copy later.
2802 bool AtBoundary = isTop ? !SU->NumSuccsLeft : !SU->NumPredsLeft;
2803 if (TargetRegisterInfo::isPhysicalRegister(
2804 MI->getOperand(UnscheduledOper).getReg()))
2805 return AtBoundary ? -1 : 1;
2809 void GenericScheduler::initCandidate(SchedCandidate &Cand, SUnit *SU,
2811 const RegPressureTracker &RPTracker,
2812 RegPressureTracker &TempTracker) {
2815 if (DAG->isTrackingPressure()) {
2817 TempTracker.getMaxDownwardPressureDelta(
2818 Cand.SU->getInstr(),
2820 DAG->getRegionCriticalPSets(),
2821 DAG->getRegPressure().MaxSetPressure);
2823 if (VerifyScheduling) {
2824 TempTracker.getMaxUpwardPressureDelta(
2825 Cand.SU->getInstr(),
2826 &DAG->getPressureDiff(Cand.SU),
2828 DAG->getRegionCriticalPSets(),
2829 DAG->getRegPressure().MaxSetPressure);
2831 RPTracker.getUpwardPressureDelta(
2832 Cand.SU->getInstr(),
2833 DAG->getPressureDiff(Cand.SU),
2835 DAG->getRegionCriticalPSets(),
2836 DAG->getRegPressure().MaxSetPressure);
2840 DEBUG(if (Cand.RPDelta.Excess.isValid())
2841 dbgs() << " Try SU(" << Cand.SU->NodeNum << ") "
2842 << TRI->getRegPressureSetName(Cand.RPDelta.Excess.getPSet())
2843 << ":" << Cand.RPDelta.Excess.getUnitInc() << "\n");
2846 /// Apply a set of heursitics to a new candidate. Heuristics are currently
2847 /// hierarchical. This may be more efficient than a graduated cost model because
2848 /// we don't need to evaluate all aspects of the model for each node in the
2849 /// queue. But it's really done to make the heuristics easier to debug and
2850 /// statistically analyze.
2852 /// \param Cand provides the policy and current best candidate.
2853 /// \param TryCand refers to the next SUnit candidate, otherwise uninitialized.
2854 /// \param Zone describes the scheduled zone that we are extending, or nullptr
2855 // if Cand is from a different zone than TryCand.
2856 void GenericScheduler::tryCandidate(SchedCandidate &Cand,
2857 SchedCandidate &TryCand,
2858 SchedBoundary *Zone) {
2859 // Initialize the candidate if needed.
2860 if (!Cand.isValid()) {
2861 TryCand.Reason = NodeOrder;
2865 if (tryGreater(biasPhysRegCopy(TryCand.SU, TryCand.AtTop),
2866 biasPhysRegCopy(Cand.SU, Cand.AtTop),
2867 TryCand, Cand, PhysRegCopy))
2870 // Avoid exceeding the target's limit.
2871 if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.Excess,
2872 Cand.RPDelta.Excess,
2873 TryCand, Cand, RegExcess, TRI,
2877 // Avoid increasing the max critical pressure in the scheduled region.
2878 if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CriticalMax,
2879 Cand.RPDelta.CriticalMax,
2880 TryCand, Cand, RegCritical, TRI,
2884 // We only compare a subset of features when comparing nodes between
2885 // Top and Bottom boundary. Some properties are simply incomparable, in many
2886 // other instances we should only override the other boundary if something
2887 // is a clear good pick on one boundary. Skip heuristics that are more
2888 // "tie-breaking" in nature.
2889 bool SameBoundary = Zone != nullptr;
2891 // For loops that are acyclic path limited, aggressively schedule for
2892 // latency. Within an single cycle, whenever CurrMOps > 0, allow normal
2893 // heuristics to take precedence.
2894 if (Rem.IsAcyclicLatencyLimited && !Zone->getCurrMOps() &&
2895 tryLatency(TryCand, Cand, *Zone))
2898 // Prioritize instructions that read unbuffered resources by stall cycles.
2899 if (tryLess(Zone->getLatencyStallCycles(TryCand.SU),
2900 Zone->getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
2904 // Keep clustered nodes together to encourage downstream peephole
2905 // optimizations which may reduce resource requirements.
2907 // This is a best effort to set things up for a post-RA pass. Optimizations
2908 // like generating loads of multiple registers should ideally be done within
2909 // the scheduler pass by combining the loads during DAG postprocessing.
2910 const SUnit *CandNextClusterSU =
2911 Cand.AtTop ? DAG->getNextClusterSucc() : DAG->getNextClusterPred();
2912 const SUnit *TryCandNextClusterSU =
2913 TryCand.AtTop ? DAG->getNextClusterSucc() : DAG->getNextClusterPred();
2914 if (tryGreater(TryCand.SU == TryCandNextClusterSU,
2915 Cand.SU == CandNextClusterSU,
2916 TryCand, Cand, Cluster))
2920 // Weak edges are for clustering and other constraints.
2921 if (tryLess(getWeakLeft(TryCand.SU, TryCand.AtTop),
2922 getWeakLeft(Cand.SU, Cand.AtTop),
2923 TryCand, Cand, Weak))
2927 // Avoid increasing the max pressure of the entire region.
2928 if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CurrentMax,
2929 Cand.RPDelta.CurrentMax,
2930 TryCand, Cand, RegMax, TRI,
2935 // Avoid critical resource consumption and balance the schedule.
2936 TryCand.initResourceDelta(DAG, SchedModel);
2937 if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
2938 TryCand, Cand, ResourceReduce))
2940 if (tryGreater(TryCand.ResDelta.DemandedResources,
2941 Cand.ResDelta.DemandedResources,
2942 TryCand, Cand, ResourceDemand))
2945 // Avoid serializing long latency dependence chains.
2946 // For acyclic path limited loops, latency was already checked above.
2947 if (!RegionPolicy.DisableLatencyHeuristic && TryCand.Policy.ReduceLatency &&
2948 !Rem.IsAcyclicLatencyLimited && tryLatency(TryCand, Cand, *Zone))
2951 // Fall through to original instruction order.
2952 if ((Zone->isTop() && TryCand.SU->NodeNum < Cand.SU->NodeNum)
2953 || (!Zone->isTop() && TryCand.SU->NodeNum > Cand.SU->NodeNum)) {
2954 TryCand.Reason = NodeOrder;
2959 /// Pick the best candidate from the queue.
2961 /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during
2962 /// DAG building. To adjust for the current scheduling location we need to
2963 /// maintain the number of vreg uses remaining to be top-scheduled.
2964 void GenericScheduler::pickNodeFromQueue(SchedBoundary &Zone,
2965 const CandPolicy &ZonePolicy,
2966 const RegPressureTracker &RPTracker,
2967 SchedCandidate &Cand) {
2968 // getMaxPressureDelta temporarily modifies the tracker.
2969 RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
2971 ReadyQueue &Q = Zone.Available;
2972 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
2974 SchedCandidate TryCand(ZonePolicy);
2975 initCandidate(TryCand, *I, Zone.isTop(), RPTracker, TempTracker);
2976 // Pass SchedBoundary only when comparing nodes from the same boundary.
2977 SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
2978 tryCandidate(Cand, TryCand, ZoneArg);
2979 if (TryCand.Reason != NoCand) {
2980 // Initialize resource delta if needed in case future heuristics query it.
2981 if (TryCand.ResDelta == SchedResourceDelta())
2982 TryCand.initResourceDelta(DAG, SchedModel);
2983 Cand.setBest(TryCand);
2984 DEBUG(traceCandidate(Cand));
2989 /// Pick the best candidate node from either the top or bottom queue.
2990 SUnit *GenericScheduler::pickNodeBidirectional(bool &IsTopNode) {
2991 // Schedule as far as possible in the direction of no choice. This is most
2992 // efficient, but also provides the best heuristics for CriticalPSets.
2993 if (SUnit *SU = Bot.pickOnlyChoice()) {
2995 tracePick(Only1, false);
2998 if (SUnit *SU = Top.pickOnlyChoice()) {
3000 tracePick(Only1, true);
3003 // Set the bottom-up policy based on the state of the current bottom zone and
3004 // the instructions outside the zone, including the top zone.
3005 CandPolicy BotPolicy;
3006 setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
3007 // Set the top-down policy based on the state of the current top zone and
3008 // the instructions outside the zone, including the bottom zone.
3009 CandPolicy TopPolicy;
3010 setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
3012 // See if BotCand is still valid (because we previously scheduled from Top).
3013 DEBUG(dbgs() << "Picking from Bot:\n");
3014 if (!BotCand.isValid() || BotCand.SU->isScheduled ||
3015 BotCand.Policy != BotPolicy) {
3016 BotCand.reset(CandPolicy());
3017 pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
3018 assert(BotCand.Reason != NoCand && "failed to find the first candidate");
3020 DEBUG(traceCandidate(BotCand));
3022 if (VerifyScheduling) {
3023 SchedCandidate TCand;
3024 TCand.reset(CandPolicy());
3025 pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), TCand);
3026 assert(TCand.SU == BotCand.SU &&
3027 "Last pick result should correspond to re-picking right now");
3032 // Check if the top Q has a better candidate.
3033 DEBUG(dbgs() << "Picking from Top:\n");
3034 if (!TopCand.isValid() || TopCand.SU->isScheduled ||
3035 TopCand.Policy != TopPolicy) {
3036 TopCand.reset(CandPolicy());
3037 pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
3038 assert(TopCand.Reason != NoCand && "failed to find the first candidate");
3040 DEBUG(traceCandidate(TopCand));
3042 if (VerifyScheduling) {
3043 SchedCandidate TCand;
3044 TCand.reset(CandPolicy());
3045 pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TCand);
3046 assert(TCand.SU == TopCand.SU &&
3047 "Last pick result should correspond to re-picking right now");
3052 // Pick best from BotCand and TopCand.
3053 assert(BotCand.isValid());
3054 assert(TopCand.isValid());
3055 SchedCandidate Cand = BotCand;
3056 TopCand.Reason = NoCand;
3057 tryCandidate(Cand, TopCand, nullptr);
3058 if (TopCand.Reason != NoCand) {
3059 Cand.setBest(TopCand);
3060 DEBUG(traceCandidate(Cand));
3063 IsTopNode = Cand.AtTop;
3068 /// Pick the best node to balance the schedule. Implements MachineSchedStrategy.
3069 SUnit *GenericScheduler::pickNode(bool &IsTopNode) {
3070 if (DAG->top() == DAG->bottom()) {
3071 assert(Top.Available.empty() && Top.Pending.empty() &&
3072 Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
3077 if (RegionPolicy.OnlyTopDown) {
3078 SU = Top.pickOnlyChoice();
3080 CandPolicy NoPolicy;
3081 TopCand.reset(NoPolicy);
3082 pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
3083 assert(TopCand.Reason != NoCand && "failed to find a candidate");
3088 } else if (RegionPolicy.OnlyBottomUp) {
3089 SU = Bot.pickOnlyChoice();
3091 CandPolicy NoPolicy;
3092 BotCand.reset(NoPolicy);
3093 pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
3094 assert(BotCand.Reason != NoCand && "failed to find a candidate");
3100 SU = pickNodeBidirectional(IsTopNode);
3102 } while (SU->isScheduled);
3104 if (SU->isTopReady())
3105 Top.removeReady(SU);
3106 if (SU->isBottomReady())
3107 Bot.removeReady(SU);
3109 DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr());
3113 void GenericScheduler::reschedulePhysRegCopies(SUnit *SU, bool isTop) {
3114 MachineBasicBlock::iterator InsertPos = SU->getInstr();
3117 SmallVectorImpl<SDep> &Deps = isTop ? SU->Preds : SU->Succs;
3119 // Find already scheduled copies with a single physreg dependence and move
3120 // them just above the scheduled instruction.
3121 for (SmallVectorImpl<SDep>::iterator I = Deps.begin(), E = Deps.end();
3123 if (I->getKind() != SDep::Data || !TRI->isPhysicalRegister(I->getReg()))
3125 SUnit *DepSU = I->getSUnit();
3126 if (isTop ? DepSU->Succs.size() > 1 : DepSU->Preds.size() > 1)
3128 MachineInstr *Copy = DepSU->getInstr();
3129 if (!Copy->isCopy())
3131 DEBUG(dbgs() << " Rescheduling physreg copy ";
3132 I->getSUnit()->dump(DAG));
3133 DAG->moveInstruction(Copy, InsertPos);
3137 /// Update the scheduler's state after scheduling a node. This is the same node
3138 /// that was just returned by pickNode(). However, ScheduleDAGMILive needs to
3139 /// update it's state based on the current cycle before MachineSchedStrategy
3142 /// FIXME: Eventually, we may bundle physreg copies rather than rescheduling
3143 /// them here. See comments in biasPhysRegCopy.
3144 void GenericScheduler::schedNode(SUnit *SU, bool IsTopNode) {
3146 SU->TopReadyCycle = std::max(SU->TopReadyCycle, Top.getCurrCycle());
3148 if (SU->hasPhysRegUses)
3149 reschedulePhysRegCopies(SU, true);
3151 SU->BotReadyCycle = std::max(SU->BotReadyCycle, Bot.getCurrCycle());
3153 if (SU->hasPhysRegDefs)
3154 reschedulePhysRegCopies(SU, false);
3158 /// Create the standard converging machine scheduler. This will be used as the
3159 /// default scheduler if the target does not set a default.
3160 ScheduleDAGMILive *llvm::createGenericSchedLive(MachineSchedContext *C) {
3161 ScheduleDAGMILive *DAG =
3162 new ScheduleDAGMILive(C, llvm::make_unique<GenericScheduler>(C));
3163 // Register DAG post-processors.
3165 // FIXME: extend the mutation API to allow earlier mutations to instantiate
3166 // data and pass it to later mutations. Have a single mutation that gathers
3167 // the interesting nodes in one pass.
3168 DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
3172 static ScheduleDAGInstrs *createConveringSched(MachineSchedContext *C) {
3173 return createGenericSchedLive(C);
3176 static MachineSchedRegistry
3177 GenericSchedRegistry("converge", "Standard converging scheduler.",
3178 createConveringSched);
3180 //===----------------------------------------------------------------------===//
3181 // PostGenericScheduler - Generic PostRA implementation of MachineSchedStrategy.
3182 //===----------------------------------------------------------------------===//
3184 void PostGenericScheduler::initialize(ScheduleDAGMI *Dag) {
3186 SchedModel = DAG->getSchedModel();
3189 Rem.init(DAG, SchedModel);
3190 Top.init(DAG, SchedModel, &Rem);
3193 // Initialize the HazardRecognizers. If itineraries don't exist, are empty,
3194 // or are disabled, then these HazardRecs will be disabled.
3195 const InstrItineraryData *Itin = SchedModel->getInstrItineraries();
3196 if (!Top.HazardRec) {
3198 DAG->MF.getSubtarget().getInstrInfo()->CreateTargetMIHazardRecognizer(
3203 void PostGenericScheduler::registerRoots() {
3204 Rem.CriticalPath = DAG->ExitSU.getDepth();
3206 // Some roots may not feed into ExitSU. Check all of them in case.
3207 for (SmallVectorImpl<SUnit*>::const_iterator
3208 I = BotRoots.begin(), E = BotRoots.end(); I != E; ++I) {
3209 if ((*I)->getDepth() > Rem.CriticalPath)
3210 Rem.CriticalPath = (*I)->getDepth();
3212 DEBUG(dbgs() << "Critical Path: (PGS-RR) " << Rem.CriticalPath << '\n');
3213 if (DumpCriticalPathLength) {
3214 errs() << "Critical Path(PGS-RR ): " << Rem.CriticalPath << " \n";
3218 /// Apply a set of heursitics to a new candidate for PostRA scheduling.
3220 /// \param Cand provides the policy and current best candidate.
3221 /// \param TryCand refers to the next SUnit candidate, otherwise uninitialized.
3222 void PostGenericScheduler::tryCandidate(SchedCandidate &Cand,
3223 SchedCandidate &TryCand) {
3225 // Initialize the candidate if needed.
3226 if (!Cand.isValid()) {
3227 TryCand.Reason = NodeOrder;
3231 // Prioritize instructions that read unbuffered resources by stall cycles.
3232 if (tryLess(Top.getLatencyStallCycles(TryCand.SU),
3233 Top.getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
3236 // Avoid critical resource consumption and balance the schedule.
3237 if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
3238 TryCand, Cand, ResourceReduce))
3240 if (tryGreater(TryCand.ResDelta.DemandedResources,
3241 Cand.ResDelta.DemandedResources,
3242 TryCand, Cand, ResourceDemand))
3245 // Avoid serializing long latency dependence chains.
3246 if (Cand.Policy.ReduceLatency && tryLatency(TryCand, Cand, Top)) {
3250 // Fall through to original instruction order.
3251 if (TryCand.SU->NodeNum < Cand.SU->NodeNum)
3252 TryCand.Reason = NodeOrder;
3255 void PostGenericScheduler::pickNodeFromQueue(SchedCandidate &Cand) {
3256 ReadyQueue &Q = Top.Available;
3257 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
3258 SchedCandidate TryCand(Cand.Policy);
3260 TryCand.AtTop = true;
3261 TryCand.initResourceDelta(DAG, SchedModel);
3262 tryCandidate(Cand, TryCand);
3263 if (TryCand.Reason != NoCand) {
3264 Cand.setBest(TryCand);
3265 DEBUG(traceCandidate(Cand));
3270 /// Pick the next node to schedule.
3271 SUnit *PostGenericScheduler::pickNode(bool &IsTopNode) {
3272 if (DAG->top() == DAG->bottom()) {
3273 assert(Top.Available.empty() && Top.Pending.empty() && "ReadyQ garbage");
3278 SU = Top.pickOnlyChoice();
3280 tracePick(Only1, true);
3282 CandPolicy NoPolicy;
3283 SchedCandidate TopCand(NoPolicy);
3284 // Set the top-down policy based on the state of the current top zone and
3285 // the instructions outside the zone, including the bottom zone.
3286 setPolicy(TopCand.Policy, /*IsPostRA=*/true, Top, nullptr);
3287 pickNodeFromQueue(TopCand);
3288 assert(TopCand.Reason != NoCand && "failed to find a candidate");
3292 } while (SU->isScheduled);
3295 Top.removeReady(SU);
3297 DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr());
3301 /// Called after ScheduleDAGMI has scheduled an instruction and updated
3302 /// scheduled/remaining flags in the DAG nodes.
3303 void PostGenericScheduler::schedNode(SUnit *SU, bool IsTopNode) {
3304 SU->TopReadyCycle = std::max(SU->TopReadyCycle, Top.getCurrCycle());
3308 ScheduleDAGMI *llvm::createGenericSchedPostRA(MachineSchedContext *C) {
3309 return new ScheduleDAGMI(C, llvm::make_unique<PostGenericScheduler>(C),
3310 /*RemoveKillFlags=*/true);
3313 //===----------------------------------------------------------------------===//
3314 // ILP Scheduler. Currently for experimental analysis of heuristics.
3315 //===----------------------------------------------------------------------===//
3319 /// \brief Order nodes by the ILP metric.
3321 const SchedDFSResult *DFSResult = nullptr;
3322 const BitVector *ScheduledTrees = nullptr;
3325 ILPOrder(bool MaxILP) : MaximizeILP(MaxILP) {}
3327 /// \brief Apply a less-than relation on node priority.
3329 /// (Return true if A comes after B in the Q.)
3330 bool operator()(const SUnit *A, const SUnit *B) const {
3331 unsigned SchedTreeA = DFSResult->getSubtreeID(A);
3332 unsigned SchedTreeB = DFSResult->getSubtreeID(B);
3333 if (SchedTreeA != SchedTreeB) {
3334 // Unscheduled trees have lower priority.
3335 if (ScheduledTrees->test(SchedTreeA) != ScheduledTrees->test(SchedTreeB))
3336 return ScheduledTrees->test(SchedTreeB);
3338 // Trees with shallower connections have have lower priority.
3339 if (DFSResult->getSubtreeLevel(SchedTreeA)
3340 != DFSResult->getSubtreeLevel(SchedTreeB)) {
3341 return DFSResult->getSubtreeLevel(SchedTreeA)
3342 < DFSResult->getSubtreeLevel(SchedTreeB);
3346 return DFSResult->getILP(A) < DFSResult->getILP(B);
3348 return DFSResult->getILP(A) > DFSResult->getILP(B);
3352 /// \brief Schedule based on the ILP metric.
3353 class ILPScheduler : public MachineSchedStrategy {
3354 ScheduleDAGMILive *DAG = nullptr;
3357 std::vector<SUnit*> ReadyQ;
3360 ILPScheduler(bool MaximizeILP) : Cmp(MaximizeILP) {}
3362 void initialize(ScheduleDAGMI *dag) override {
3363 assert(dag->hasVRegLiveness() && "ILPScheduler needs vreg liveness");
3364 DAG = static_cast<ScheduleDAGMILive*>(dag);
3365 DAG->computeDFSResult();
3366 Cmp.DFSResult = DAG->getDFSResult();
3367 Cmp.ScheduledTrees = &DAG->getScheduledTrees();
3371 void registerRoots() override {
3372 // Restore the heap in ReadyQ with the updated DFS results.
3373 std::make_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
3376 /// Implement MachineSchedStrategy interface.
3377 /// -----------------------------------------
3379 /// Callback to select the highest priority node from the ready Q.
3380 SUnit *pickNode(bool &IsTopNode) override {
3381 if (ReadyQ.empty()) return nullptr;
3382 std::pop_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
3383 SUnit *SU = ReadyQ.back();
3386 DEBUG(dbgs() << "Pick node " << "SU(" << SU->NodeNum << ") "
3387 << " ILP: " << DAG->getDFSResult()->getILP(SU)
3388 << " Tree: " << DAG->getDFSResult()->getSubtreeID(SU) << " @"
3389 << DAG->getDFSResult()->getSubtreeLevel(
3390 DAG->getDFSResult()->getSubtreeID(SU)) << '\n'
3391 << "Scheduling " << *SU->getInstr());
3395 /// \brief Scheduler callback to notify that a new subtree is scheduled.
3396 void scheduleTree(unsigned SubtreeID) override {
3397 std::make_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
3400 /// Callback after a node is scheduled. Mark a newly scheduled tree, notify
3401 /// DFSResults, and resort the priority Q.
3402 void schedNode(SUnit *SU, bool IsTopNode) override {
3403 assert(!IsTopNode && "SchedDFSResult needs bottom-up");
3406 void releaseTopNode(SUnit *) override { /*only called for top roots*/ }
3408 void releaseBottomNode(SUnit *SU) override {
3409 ReadyQ.push_back(SU);
3410 std::push_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
3414 } // end anonymous namespace
3416 static ScheduleDAGInstrs *createILPMaxScheduler(MachineSchedContext *C) {
3417 return new ScheduleDAGMILive(C, llvm::make_unique<ILPScheduler>(true));
3419 static ScheduleDAGInstrs *createILPMinScheduler(MachineSchedContext *C) {
3420 return new ScheduleDAGMILive(C, llvm::make_unique<ILPScheduler>(false));
3423 static MachineSchedRegistry ILPMaxRegistry(
3424 "ilpmax", "Schedule bottom-up for max ILP", createILPMaxScheduler);
3425 static MachineSchedRegistry ILPMinRegistry(
3426 "ilpmin", "Schedule bottom-up for min ILP", createILPMinScheduler);
3428 //===----------------------------------------------------------------------===//
3429 // Machine Instruction Shuffler for Correctness Testing
3430 //===----------------------------------------------------------------------===//
3435 /// Apply a less-than relation on the node order, which corresponds to the
3436 /// instruction order prior to scheduling. IsReverse implements greater-than.
3437 template<bool IsReverse>
3439 bool operator()(SUnit *A, SUnit *B) const {
3441 return A->NodeNum > B->NodeNum;
3443 return A->NodeNum < B->NodeNum;
3447 /// Reorder instructions as much as possible.
3448 class InstructionShuffler : public MachineSchedStrategy {
3452 // Using a less-than relation (SUnitOrder<false>) for the TopQ priority
3453 // gives nodes with a higher number higher priority causing the latest
3454 // instructions to be scheduled first.
3455 PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false>>
3457 // When scheduling bottom-up, use greater-than as the queue priority.
3458 PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true>>
3462 InstructionShuffler(bool alternate, bool topdown)
3463 : IsAlternating(alternate), IsTopDown(topdown) {}
3465 void initialize(ScheduleDAGMI*) override {
3470 /// Implement MachineSchedStrategy interface.
3471 /// -----------------------------------------
3473 SUnit *pickNode(bool &IsTopNode) override {
3477 if (TopQ.empty()) return nullptr;
3480 } while (SU->isScheduled);
3484 if (BottomQ.empty()) return nullptr;
3487 } while (SU->isScheduled);
3491 IsTopDown = !IsTopDown;
3495 void schedNode(SUnit *SU, bool IsTopNode) override {}
3497 void releaseTopNode(SUnit *SU) override {
3500 void releaseBottomNode(SUnit *SU) override {
3505 } // end anonymous namespace
3507 static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) {
3508 bool Alternate = !ForceTopDown && !ForceBottomUp;
3509 bool TopDown = !ForceBottomUp;
3510 assert((TopDown || !ForceTopDown) &&
3511 "-misched-topdown incompatible with -misched-bottomup");
3512 return new ScheduleDAGMILive(
3513 C, llvm::make_unique<InstructionShuffler>(Alternate, TopDown));
3516 static MachineSchedRegistry ShufflerRegistry(
3517 "shuffle", "Shuffle machine instructions alternating directions",
3518 createInstructionShuffler);
3521 //===----------------------------------------------------------------------===//
3522 // GraphWriter support for ScheduleDAGMILive.
3523 //===----------------------------------------------------------------------===//
3528 template<> struct GraphTraits<
3529 ScheduleDAGMI*> : public GraphTraits<ScheduleDAG*> {};
3532 struct DOTGraphTraits<ScheduleDAGMI*> : public DefaultDOTGraphTraits {
3533 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
3535 static std::string getGraphName(const ScheduleDAG *G) {
3536 return G->MF.getName();
3539 static bool renderGraphFromBottomUp() {
3543 static bool isNodeHidden(const SUnit *Node) {
3544 if (ViewMISchedCutoff == 0)
3546 return (Node->Preds.size() > ViewMISchedCutoff
3547 || Node->Succs.size() > ViewMISchedCutoff);
3550 /// If you want to override the dot attributes printed for a particular
3551 /// edge, override this method.
3552 static std::string getEdgeAttributes(const SUnit *Node,
3554 const ScheduleDAG *Graph) {
3555 if (EI.isArtificialDep())
3556 return "color=cyan,style=dashed";
3558 return "color=blue,style=dashed";
3562 static std::string getNodeLabel(const SUnit *SU, const ScheduleDAG *G) {
3564 raw_string_ostream SS(Str);
3565 const ScheduleDAGMI *DAG = static_cast<const ScheduleDAGMI*>(G);
3566 const SchedDFSResult *DFS = DAG->hasVRegLiveness() ?
3567 static_cast<const ScheduleDAGMILive*>(G)->getDFSResult() : nullptr;
3568 SS << "SU:" << SU->NodeNum;
3570 SS << " I:" << DFS->getNumInstrs(SU);
3573 static std::string getNodeDescription(const SUnit *SU, const ScheduleDAG *G) {
3574 return G->getGraphNodeLabel(SU);
3577 static std::string getNodeAttributes(const SUnit *N, const ScheduleDAG *G) {
3578 std::string Str("shape=Mrecord");
3579 const ScheduleDAGMI *DAG = static_cast<const ScheduleDAGMI*>(G);
3580 const SchedDFSResult *DFS = DAG->hasVRegLiveness() ?
3581 static_cast<const ScheduleDAGMILive*>(G)->getDFSResult() : nullptr;
3583 Str += ",style=filled,fillcolor=\"#";
3584 Str += DOT::getColorString(DFS->getSubtreeID(N));
3591 } // end namespace llvm
3594 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
3595 /// rendered using 'dot'.
3597 void ScheduleDAGMI::viewGraph(const Twine &Name, const Twine &Title) {
3599 ViewGraph(this, Name, false, Title);
3601 errs() << "ScheduleDAGMI::viewGraph is only available in debug builds on "
3602 << "systems with Graphviz or gv!\n";
3606 /// Out-of-line implementation with no arguments is handy for gdb.
3607 void ScheduleDAGMI::viewGraph() {
3608 viewGraph(getDAGName(), "Scheduling-Units Graph for " + getDAGName());