Move GenericScheduler and PostGenericScheduler into a header.
[oota-llvm.git] / include / llvm / CodeGen / MachineScheduler.h
1 //==- MachineScheduler.h - MachineInstr Scheduling Pass ----------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides an interface for customizing the standard MachineScheduler
11 // pass. Note that the entire pass may be replaced as follows:
12 //
13 // <Target>TargetMachine::createPassConfig(PassManagerBase &PM) {
14 //   PM.substitutePass(&MachineSchedulerID, &CustomSchedulerPassID);
15 //   ...}
16 //
17 // The MachineScheduler pass is only responsible for choosing the regions to be
18 // scheduled. Targets can override the DAG builder and scheduler without
19 // replacing the pass as follows:
20 //
21 // ScheduleDAGInstrs *<Target>PassConfig::
22 // createMachineScheduler(MachineSchedContext *C) {
23 //   return new CustomMachineScheduler(C);
24 // }
25 //
26 // The default scheduler, ScheduleDAGMILive, builds the DAG and drives list
27 // scheduling while updating the instruction stream, register pressure, and live
28 // intervals. Most targets don't need to override the DAG builder and list
29 // schedulier, but subtargets that require custom scheduling heuristics may
30 // plugin an alternate MachineSchedStrategy. The strategy is responsible for
31 // selecting the highest priority node from the list:
32 //
33 // ScheduleDAGInstrs *<Target>PassConfig::
34 // createMachineScheduler(MachineSchedContext *C) {
35 //   return new ScheduleDAGMI(C, CustomStrategy(C));
36 // }
37 //
38 // The DAG builder can also be customized in a sense by adding DAG mutations
39 // that will run after DAG building and before list scheduling. DAG mutations
40 // can adjust dependencies based on target-specific knowledge or add weak edges
41 // to aid heuristics:
42 //
43 // ScheduleDAGInstrs *<Target>PassConfig::
44 // createMachineScheduler(MachineSchedContext *C) {
45 //   ScheduleDAGMI *DAG = new ScheduleDAGMI(C, CustomStrategy(C));
46 //   DAG->addMutation(new CustomDependencies(DAG->TII, DAG->TRI));
47 //   return DAG;
48 // }
49 //
50 // A target that supports alternative schedulers can use the
51 // MachineSchedRegistry to allow command line selection. This can be done by
52 // implementing the following boilerplate:
53 //
54 // static ScheduleDAGInstrs *createCustomMachineSched(MachineSchedContext *C) {
55 //  return new CustomMachineScheduler(C);
56 // }
57 // static MachineSchedRegistry
58 // SchedCustomRegistry("custom", "Run my target's custom scheduler",
59 //                     createCustomMachineSched);
60 //
61 //
62 // Finally, subtargets that don't need to implement custom heuristics but would
63 // like to configure the GenericScheduler's policy for a given scheduler region,
64 // including scheduling direction and register pressure tracking policy, can do
65 // this:
66 //
67 // void <SubTarget>Subtarget::
68 // overrideSchedPolicy(MachineSchedPolicy &Policy,
69 //                     MachineInstr *begin,
70 //                     MachineInstr *end,
71 //                     unsigned NumRegionInstrs) const {
72 //   Policy.<Flag> = true;
73 // }
74 //
75 //===----------------------------------------------------------------------===//
76
77 #ifndef LLVM_CODEGEN_MACHINESCHEDULER_H
78 #define LLVM_CODEGEN_MACHINESCHEDULER_H
79
80 #include "llvm/CodeGen/MachinePassRegistry.h"
81 #include "llvm/CodeGen/RegisterPressure.h"
82 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
83
84 #include <memory>
85
86 namespace llvm {
87
88 extern cl::opt<bool> ForceTopDown;
89 extern cl::opt<bool> ForceBottomUp;
90
91 class AliasAnalysis;
92 class LiveIntervals;
93 class MachineDominatorTree;
94 class MachineLoopInfo;
95 class RegisterClassInfo;
96 class ScheduleDAGInstrs;
97 class SchedDFSResult;
98 class ScheduleHazardRecognizer;
99
100 /// MachineSchedContext provides enough context from the MachineScheduler pass
101 /// for the target to instantiate a scheduler.
102 struct MachineSchedContext {
103   MachineFunction *MF;
104   const MachineLoopInfo *MLI;
105   const MachineDominatorTree *MDT;
106   const TargetPassConfig *PassConfig;
107   AliasAnalysis *AA;
108   LiveIntervals *LIS;
109
110   RegisterClassInfo *RegClassInfo;
111
112   MachineSchedContext();
113   virtual ~MachineSchedContext();
114 };
115
116 /// MachineSchedRegistry provides a selection of available machine instruction
117 /// schedulers.
118 class MachineSchedRegistry : public MachinePassRegistryNode {
119 public:
120   typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineSchedContext *);
121
122   // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
123   typedef ScheduleDAGCtor FunctionPassCtor;
124
125   static MachinePassRegistry Registry;
126
127   MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
128     : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
129     Registry.Add(this);
130   }
131   ~MachineSchedRegistry() { Registry.Remove(this); }
132
133   // Accessors.
134   //
135   MachineSchedRegistry *getNext() const {
136     return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
137   }
138   static MachineSchedRegistry *getList() {
139     return (MachineSchedRegistry *)Registry.getList();
140   }
141   static void setListener(MachinePassRegistryListener *L) {
142     Registry.setListener(L);
143   }
144 };
145
146 class ScheduleDAGMI;
147
148 /// Define a generic scheduling policy for targets that don't provide their own
149 /// MachineSchedStrategy. This can be overriden for each scheduling region
150 /// before building the DAG.
151 struct MachineSchedPolicy {
152   // Allow the scheduler to disable register pressure tracking.
153   bool ShouldTrackPressure;
154
155   // Allow the scheduler to force top-down or bottom-up scheduling. If neither
156   // is true, the scheduler runs in both directions and converges.
157   bool OnlyTopDown;
158   bool OnlyBottomUp;
159
160   MachineSchedPolicy(): ShouldTrackPressure(false), OnlyTopDown(false),
161     OnlyBottomUp(false) {}
162 };
163
164 /// MachineSchedStrategy - Interface to the scheduling algorithm used by
165 /// ScheduleDAGMI.
166 ///
167 /// Initialization sequence:
168 ///   initPolicy -> shouldTrackPressure -> initialize(DAG) -> registerRoots
169 class MachineSchedStrategy {
170   virtual void anchor();
171 public:
172   virtual ~MachineSchedStrategy() {}
173
174   /// Optionally override the per-region scheduling policy.
175   virtual void initPolicy(MachineBasicBlock::iterator Begin,
176                           MachineBasicBlock::iterator End,
177                           unsigned NumRegionInstrs) {}
178
179   /// Check if pressure tracking is needed before building the DAG and
180   /// initializing this strategy. Called after initPolicy.
181   virtual bool shouldTrackPressure() const { return true; }
182
183   /// Initialize the strategy after building the DAG for a new region.
184   virtual void initialize(ScheduleDAGMI *DAG) = 0;
185
186   /// Notify this strategy that all roots have been released (including those
187   /// that depend on EntrySU or ExitSU).
188   virtual void registerRoots() {}
189
190   /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to
191   /// schedule the node at the top of the unscheduled region. Otherwise it will
192   /// be scheduled at the bottom.
193   virtual SUnit *pickNode(bool &IsTopNode) = 0;
194
195   /// \brief Scheduler callback to notify that a new subtree is scheduled.
196   virtual void scheduleTree(unsigned SubtreeID) {}
197
198   /// Notify MachineSchedStrategy that ScheduleDAGMI has scheduled an
199   /// instruction and updated scheduled/remaining flags in the DAG nodes.
200   virtual void schedNode(SUnit *SU, bool IsTopNode) = 0;
201
202   /// When all predecessor dependencies have been resolved, free this node for
203   /// top-down scheduling.
204   virtual void releaseTopNode(SUnit *SU) = 0;
205   /// When all successor dependencies have been resolved, free this node for
206   /// bottom-up scheduling.
207   virtual void releaseBottomNode(SUnit *SU) = 0;
208 };
209
210 /// Mutate the DAG as a postpass after normal DAG building.
211 class ScheduleDAGMutation {
212   virtual void anchor();
213 public:
214   virtual ~ScheduleDAGMutation() {}
215
216   virtual void apply(ScheduleDAGMI *DAG) = 0;
217 };
218
219 /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply
220 /// schedules machine instructions according to the given MachineSchedStrategy
221 /// without much extra book-keeping. This is the common functionality between
222 /// PreRA and PostRA MachineScheduler.
223 class ScheduleDAGMI : public ScheduleDAGInstrs {
224 protected:
225   AliasAnalysis *AA;
226   std::unique_ptr<MachineSchedStrategy> SchedImpl;
227
228   /// Topo - A topological ordering for SUnits which permits fast IsReachable
229   /// and similar queries.
230   ScheduleDAGTopologicalSort Topo;
231
232   /// Ordered list of DAG postprocessing steps.
233   std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
234
235   /// The top of the unscheduled zone.
236   MachineBasicBlock::iterator CurrentTop;
237
238   /// The bottom of the unscheduled zone.
239   MachineBasicBlock::iterator CurrentBottom;
240
241   /// Record the next node in a scheduled cluster.
242   const SUnit *NextClusterPred;
243   const SUnit *NextClusterSucc;
244
245 #ifndef NDEBUG
246   /// The number of instructions scheduled so far. Used to cut off the
247   /// scheduler at the point determined by misched-cutoff.
248   unsigned NumInstrsScheduled;
249 #endif
250 public:
251   ScheduleDAGMI(MachineSchedContext *C, std::unique_ptr<MachineSchedStrategy> S,
252                 bool IsPostRA)
253       : ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, IsPostRA,
254                           /*RemoveKillFlags=*/IsPostRA, C->LIS),
255         AA(C->AA), SchedImpl(std::move(S)), Topo(SUnits, &ExitSU), CurrentTop(),
256         CurrentBottom(), NextClusterPred(nullptr), NextClusterSucc(nullptr) {
257 #ifndef NDEBUG
258     NumInstrsScheduled = 0;
259 #endif
260   }
261
262   // Provide a vtable anchor
263   ~ScheduleDAGMI() override;
264
265   /// Return true if this DAG supports VReg liveness and RegPressure.
266   virtual bool hasVRegLiveness() const { return false; }
267
268   /// Add a postprocessing step to the DAG builder.
269   /// Mutations are applied in the order that they are added after normal DAG
270   /// building and before MachineSchedStrategy initialization.
271   ///
272   /// ScheduleDAGMI takes ownership of the Mutation object.
273   void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
274     Mutations.push_back(std::move(Mutation));
275   }
276
277   /// \brief True if an edge can be added from PredSU to SuccSU without creating
278   /// a cycle.
279   bool canAddEdge(SUnit *SuccSU, SUnit *PredSU);
280
281   /// \brief Add a DAG edge to the given SU with the given predecessor
282   /// dependence data.
283   ///
284   /// \returns true if the edge may be added without creating a cycle OR if an
285   /// equivalent edge already existed (false indicates failure).
286   bool addEdge(SUnit *SuccSU, const SDep &PredDep);
287
288   MachineBasicBlock::iterator top() const { return CurrentTop; }
289   MachineBasicBlock::iterator bottom() const { return CurrentBottom; }
290
291   /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
292   /// region. This covers all instructions in a block, while schedule() may only
293   /// cover a subset.
294   void enterRegion(MachineBasicBlock *bb,
295                    MachineBasicBlock::iterator begin,
296                    MachineBasicBlock::iterator end,
297                    unsigned regioninstrs) override;
298
299   /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
300   /// reorderable instructions.
301   void schedule() override;
302
303   /// Change the position of an instruction within the basic block and update
304   /// live ranges and region boundary iterators.
305   void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos);
306
307   const SUnit *getNextClusterPred() const { return NextClusterPred; }
308
309   const SUnit *getNextClusterSucc() const { return NextClusterSucc; }
310
311   void viewGraph(const Twine &Name, const Twine &Title) override;
312   void viewGraph() override;
313
314 protected:
315   // Top-Level entry points for the schedule() driver...
316
317   /// Apply each ScheduleDAGMutation step in order. This allows different
318   /// instances of ScheduleDAGMI to perform custom DAG postprocessing.
319   void postprocessDAG();
320
321   /// Release ExitSU predecessors and setup scheduler queues.
322   void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots);
323
324   /// Update scheduler DAG and queues after scheduling an instruction.
325   void updateQueues(SUnit *SU, bool IsTopNode);
326
327   /// Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
328   void placeDebugValues();
329
330   /// \brief dump the scheduled Sequence.
331   void dumpSchedule() const;
332
333   // Lesser helpers...
334   bool checkSchedLimit();
335
336   void findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots,
337                              SmallVectorImpl<SUnit*> &BotRoots);
338
339   void releaseSucc(SUnit *SU, SDep *SuccEdge);
340   void releaseSuccessors(SUnit *SU);
341   void releasePred(SUnit *SU, SDep *PredEdge);
342   void releasePredecessors(SUnit *SU);
343 };
344
345 /// ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules
346 /// machine instructions while updating LiveIntervals and tracking regpressure.
347 class ScheduleDAGMILive : public ScheduleDAGMI {
348 protected:
349   RegisterClassInfo *RegClassInfo;
350
351   /// Information about DAG subtrees. If DFSResult is NULL, then SchedulerTrees
352   /// will be empty.
353   SchedDFSResult *DFSResult;
354   BitVector ScheduledTrees;
355
356   MachineBasicBlock::iterator LiveRegionEnd;
357
358   // Map each SU to its summary of pressure changes. This array is updated for
359   // liveness during bottom-up scheduling. Top-down scheduling may proceed but
360   // has no affect on the pressure diffs.
361   PressureDiffs SUPressureDiffs;
362
363   /// Register pressure in this region computed by initRegPressure.
364   bool ShouldTrackPressure;
365   IntervalPressure RegPressure;
366   RegPressureTracker RPTracker;
367
368   /// List of pressure sets that exceed the target's pressure limit before
369   /// scheduling, listed in increasing set ID order. Each pressure set is paired
370   /// with its max pressure in the currently scheduled regions.
371   std::vector<PressureChange> RegionCriticalPSets;
372
373   /// The top of the unscheduled zone.
374   IntervalPressure TopPressure;
375   RegPressureTracker TopRPTracker;
376
377   /// The bottom of the unscheduled zone.
378   IntervalPressure BotPressure;
379   RegPressureTracker BotRPTracker;
380
381 public:
382   ScheduleDAGMILive(MachineSchedContext *C,
383                     std::unique_ptr<MachineSchedStrategy> S)
384       : ScheduleDAGMI(C, std::move(S), /*IsPostRA=*/false),
385         RegClassInfo(C->RegClassInfo), DFSResult(nullptr),
386         ShouldTrackPressure(false), RPTracker(RegPressure),
387         TopRPTracker(TopPressure), BotRPTracker(BotPressure) {}
388
389   virtual ~ScheduleDAGMILive();
390
391   /// Return true if this DAG supports VReg liveness and RegPressure.
392   bool hasVRegLiveness() const override { return true; }
393
394   /// \brief Return true if register pressure tracking is enabled.
395   bool isTrackingPressure() const { return ShouldTrackPressure; }
396
397   /// Get current register pressure for the top scheduled instructions.
398   const IntervalPressure &getTopPressure() const { return TopPressure; }
399   const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; }
400
401   /// Get current register pressure for the bottom scheduled instructions.
402   const IntervalPressure &getBotPressure() const { return BotPressure; }
403   const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; }
404
405   /// Get register pressure for the entire scheduling region before scheduling.
406   const IntervalPressure &getRegPressure() const { return RegPressure; }
407
408   const std::vector<PressureChange> &getRegionCriticalPSets() const {
409     return RegionCriticalPSets;
410   }
411
412   PressureDiff &getPressureDiff(const SUnit *SU) {
413     return SUPressureDiffs[SU->NodeNum];
414   }
415
416   /// Compute a DFSResult after DAG building is complete, and before any
417   /// queue comparisons.
418   void computeDFSResult();
419
420   /// Return a non-null DFS result if the scheduling strategy initialized it.
421   const SchedDFSResult *getDFSResult() const { return DFSResult; }
422
423   BitVector &getScheduledTrees() { return ScheduledTrees; }
424
425   /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
426   /// region. This covers all instructions in a block, while schedule() may only
427   /// cover a subset.
428   void enterRegion(MachineBasicBlock *bb,
429                    MachineBasicBlock::iterator begin,
430                    MachineBasicBlock::iterator end,
431                    unsigned regioninstrs) override;
432
433   /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
434   /// reorderable instructions.
435   void schedule() override;
436
437   /// Compute the cyclic critical path through the DAG.
438   unsigned computeCyclicCriticalPath();
439
440 protected:
441   // Top-Level entry points for the schedule() driver...
442
443   /// Call ScheduleDAGInstrs::buildSchedGraph with register pressure tracking
444   /// enabled. This sets up three trackers. RPTracker will cover the entire DAG
445   /// region, TopTracker and BottomTracker will be initialized to the top and
446   /// bottom of the DAG region without covereing any unscheduled instruction.
447   void buildDAGWithRegPressure();
448
449   /// Move an instruction and update register pressure.
450   void scheduleMI(SUnit *SU, bool IsTopNode);
451
452   // Lesser helpers...
453
454   void initRegPressure();
455
456   void updatePressureDiffs(ArrayRef<unsigned> LiveUses);
457
458   void updateScheduledPressure(const SUnit *SU,
459                                const std::vector<unsigned> &NewMaxPressure);
460 };
461
462 //===----------------------------------------------------------------------===//
463 ///
464 /// Helpers for implementing custom MachineSchedStrategy classes. These take
465 /// care of the book-keeping associated with list scheduling heuristics.
466 ///
467 //===----------------------------------------------------------------------===//
468
469 /// ReadyQueue encapsulates vector of "ready" SUnits with basic convenience
470 /// methods for pushing and removing nodes. ReadyQueue's are uniquely identified
471 /// by an ID. SUnit::NodeQueueId is a mask of the ReadyQueues the SUnit is in.
472 ///
473 /// This is a convenience class that may be used by implementations of
474 /// MachineSchedStrategy.
475 class ReadyQueue {
476   unsigned ID;
477   std::string Name;
478   std::vector<SUnit*> Queue;
479
480 public:
481   ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {}
482
483   unsigned getID() const { return ID; }
484
485   StringRef getName() const { return Name; }
486
487   // SU is in this queue if it's NodeQueueID is a superset of this ID.
488   bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); }
489
490   bool empty() const { return Queue.empty(); }
491
492   void clear() { Queue.clear(); }
493
494   unsigned size() const { return Queue.size(); }
495
496   typedef std::vector<SUnit*>::iterator iterator;
497
498   iterator begin() { return Queue.begin(); }
499
500   iterator end() { return Queue.end(); }
501
502   ArrayRef<SUnit*> elements() { return Queue; }
503
504   iterator find(SUnit *SU) {
505     return std::find(Queue.begin(), Queue.end(), SU);
506   }
507
508   void push(SUnit *SU) {
509     Queue.push_back(SU);
510     SU->NodeQueueId |= ID;
511   }
512
513   iterator remove(iterator I) {
514     (*I)->NodeQueueId &= ~ID;
515     *I = Queue.back();
516     unsigned idx = I - Queue.begin();
517     Queue.pop_back();
518     return Queue.begin() + idx;
519   }
520
521 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
522   void dump();
523 #endif
524 };
525
526 /// Summarize the unscheduled region.
527 struct SchedRemainder {
528   // Critical path through the DAG in expected latency.
529   unsigned CriticalPath;
530   unsigned CyclicCritPath;
531
532   // Scaled count of micro-ops left to schedule.
533   unsigned RemIssueCount;
534
535   bool IsAcyclicLatencyLimited;
536
537   // Unscheduled resources
538   SmallVector<unsigned, 16> RemainingCounts;
539
540   void reset() {
541     CriticalPath = 0;
542     CyclicCritPath = 0;
543     RemIssueCount = 0;
544     IsAcyclicLatencyLimited = false;
545     RemainingCounts.clear();
546   }
547
548   SchedRemainder() { reset(); }
549
550   void init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel);
551 };
552
553 /// Each Scheduling boundary is associated with ready queues. It tracks the
554 /// current cycle in the direction of movement, and maintains the state
555 /// of "hazards" and other interlocks at the current cycle.
556 class SchedBoundary {
557 public:
558   /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
559   enum {
560     TopQID = 1,
561     BotQID = 2,
562     LogMaxQID = 2
563   };
564
565   ScheduleDAGMI *DAG;
566   const TargetSchedModel *SchedModel;
567   SchedRemainder *Rem;
568
569   ReadyQueue Available;
570   ReadyQueue Pending;
571
572   ScheduleHazardRecognizer *HazardRec;
573
574 private:
575   /// True if the pending Q should be checked/updated before scheduling another
576   /// instruction.
577   bool CheckPending;
578
579   // For heuristics, keep a list of the nodes that immediately depend on the
580   // most recently scheduled node.
581   SmallPtrSet<const SUnit*, 8> NextSUs;
582
583   /// Number of cycles it takes to issue the instructions scheduled in this
584   /// zone. It is defined as: scheduled-micro-ops / issue-width + stalls.
585   /// See getStalls().
586   unsigned CurrCycle;
587
588   /// Micro-ops issued in the current cycle
589   unsigned CurrMOps;
590
591   /// MinReadyCycle - Cycle of the soonest available instruction.
592   unsigned MinReadyCycle;
593
594   // The expected latency of the critical path in this scheduled zone.
595   unsigned ExpectedLatency;
596
597   // The latency of dependence chains leading into this zone.
598   // For each node scheduled bottom-up: DLat = max DLat, N.Depth.
599   // For each cycle scheduled: DLat -= 1.
600   unsigned DependentLatency;
601
602   /// Count the scheduled (issued) micro-ops that can be retired by
603   /// time=CurrCycle assuming the first scheduled instr is retired at time=0.
604   unsigned RetiredMOps;
605
606   // Count scheduled resources that have been executed. Resources are
607   // considered executed if they become ready in the time that it takes to
608   // saturate any resource including the one in question. Counts are scaled
609   // for direct comparison with other resources. Counts can be compared with
610   // MOps * getMicroOpFactor and Latency * getLatencyFactor.
611   SmallVector<unsigned, 16> ExecutedResCounts;
612
613   /// Cache the max count for a single resource.
614   unsigned MaxExecutedResCount;
615
616   // Cache the critical resources ID in this scheduled zone.
617   unsigned ZoneCritResIdx;
618
619   // Is the scheduled region resource limited vs. latency limited.
620   bool IsResourceLimited;
621
622   // Record the highest cycle at which each resource has been reserved by a
623   // scheduled instruction.
624   SmallVector<unsigned, 16> ReservedCycles;
625
626 #ifndef NDEBUG
627   // Remember the greatest operand latency as an upper bound on the number of
628   // times we should retry the pending queue because of a hazard.
629   unsigned MaxObservedLatency;
630 #endif
631
632 public:
633   /// Pending queues extend the ready queues with the same ID and the
634   /// PendingFlag set.
635   SchedBoundary(unsigned ID, const Twine &Name):
636     DAG(nullptr), SchedModel(nullptr), Rem(nullptr), Available(ID, Name+".A"),
637     Pending(ID << LogMaxQID, Name+".P"),
638     HazardRec(nullptr) {
639     reset();
640   }
641
642   ~SchedBoundary();
643
644   void reset();
645
646   void init(ScheduleDAGMI *dag, const TargetSchedModel *smodel,
647             SchedRemainder *rem);
648
649   bool isTop() const {
650     return Available.getID() == TopQID;
651   }
652
653   /// Number of cycles to issue the instructions scheduled in this zone.
654   unsigned getCurrCycle() const { return CurrCycle; }
655
656   /// Micro-ops issued in the current cycle
657   unsigned getCurrMOps() const { return CurrMOps; }
658
659   /// Return true if the given SU is used by the most recently scheduled
660   /// instruction.
661   bool isNextSU(const SUnit *SU) const { return NextSUs.count(SU); }
662
663   // The latency of dependence chains leading into this zone.
664   unsigned getDependentLatency() const { return DependentLatency; }
665
666   /// Get the number of latency cycles "covered" by the scheduled
667   /// instructions. This is the larger of the critical path within the zone
668   /// and the number of cycles required to issue the instructions.
669   unsigned getScheduledLatency() const {
670     return std::max(ExpectedLatency, CurrCycle);
671   }
672
673   unsigned getUnscheduledLatency(SUnit *SU) const {
674     return isTop() ? SU->getHeight() : SU->getDepth();
675   }
676
677   unsigned getResourceCount(unsigned ResIdx) const {
678     return ExecutedResCounts[ResIdx];
679   }
680
681   /// Get the scaled count of scheduled micro-ops and resources, including
682   /// executed resources.
683   unsigned getCriticalCount() const {
684     if (!ZoneCritResIdx)
685       return RetiredMOps * SchedModel->getMicroOpFactor();
686     return getResourceCount(ZoneCritResIdx);
687   }
688
689   /// Get a scaled count for the minimum execution time of the scheduled
690   /// micro-ops that are ready to execute by getExecutedCount. Notice the
691   /// feedback loop.
692   unsigned getExecutedCount() const {
693     return std::max(CurrCycle * SchedModel->getLatencyFactor(),
694                     MaxExecutedResCount);
695   }
696
697   unsigned getZoneCritResIdx() const { return ZoneCritResIdx; }
698
699   // Is the scheduled region resource limited vs. latency limited.
700   bool isResourceLimited() const { return IsResourceLimited; }
701
702   /// Get the difference between the given SUnit's ready time and the current
703   /// cycle.
704   unsigned getLatencyStallCycles(SUnit *SU);
705
706   unsigned getNextResourceCycle(unsigned PIdx, unsigned Cycles);
707
708   bool checkHazard(SUnit *SU);
709
710   unsigned findMaxLatency(ArrayRef<SUnit*> ReadySUs);
711
712   unsigned getOtherResourceCount(unsigned &OtherCritIdx);
713
714   void releaseNode(SUnit *SU, unsigned ReadyCycle);
715
716   void releaseTopNode(SUnit *SU);
717
718   void releaseBottomNode(SUnit *SU);
719
720   void bumpCycle(unsigned NextCycle);
721
722   void incExecutedResources(unsigned PIdx, unsigned Count);
723
724   unsigned countResource(unsigned PIdx, unsigned Cycles, unsigned ReadyCycle);
725
726   void bumpNode(SUnit *SU);
727
728   void releasePending();
729
730   void removeReady(SUnit *SU);
731
732   /// Call this before applying any other heuristics to the Available queue.
733   /// Updates the Available/Pending Q's if necessary and returns the single
734   /// available instruction, or NULL if there are multiple candidates.
735   SUnit *pickOnlyChoice();
736
737 #ifndef NDEBUG
738   void dumpScheduledState();
739 #endif
740 };
741
742 /// Base class for GenericScheduler. This class maintains information about
743 /// scheduling candidates based on TargetSchedModel making it easy to implement
744 /// heuristics for either preRA or postRA scheduling.
745 class GenericSchedulerBase : public MachineSchedStrategy {
746 public:
747   /// Represent the type of SchedCandidate found within a single queue.
748   /// pickNodeBidirectional depends on these listed by decreasing priority.
749   enum CandReason {
750     NoCand, PhysRegCopy, RegExcess, RegCritical, Stall, Cluster, Weak, RegMax,
751     ResourceReduce, ResourceDemand, BotHeightReduce, BotPathReduce,
752     TopDepthReduce, TopPathReduce, NextDefUse, NodeOrder};
753
754 #ifndef NDEBUG
755   static const char *getReasonStr(GenericSchedulerBase::CandReason Reason);
756 #endif
757
758   /// Policy for scheduling the next instruction in the candidate's zone.
759   struct CandPolicy {
760     bool ReduceLatency;
761     unsigned ReduceResIdx;
762     unsigned DemandResIdx;
763
764     CandPolicy(): ReduceLatency(false), ReduceResIdx(0), DemandResIdx(0) {}
765   };
766
767   /// Status of an instruction's critical resource consumption.
768   struct SchedResourceDelta {
769     // Count critical resources in the scheduled region required by SU.
770     unsigned CritResources;
771
772     // Count critical resources from another region consumed by SU.
773     unsigned DemandedResources;
774
775     SchedResourceDelta(): CritResources(0), DemandedResources(0) {}
776
777     bool operator==(const SchedResourceDelta &RHS) const {
778       return CritResources == RHS.CritResources
779         && DemandedResources == RHS.DemandedResources;
780     }
781     bool operator!=(const SchedResourceDelta &RHS) const {
782       return !operator==(RHS);
783     }
784   };
785
786   /// Store the state used by GenericScheduler heuristics, required for the
787   /// lifetime of one invocation of pickNode().
788   struct SchedCandidate {
789     CandPolicy Policy;
790
791     // The best SUnit candidate.
792     SUnit *SU;
793
794     // The reason for this candidate.
795     CandReason Reason;
796
797     // Set of reasons that apply to multiple candidates.
798     uint32_t RepeatReasonSet;
799
800     // Register pressure values for the best candidate.
801     RegPressureDelta RPDelta;
802
803     // Critical resource consumption of the best candidate.
804     SchedResourceDelta ResDelta;
805
806     SchedCandidate(const CandPolicy &policy)
807       : Policy(policy), SU(nullptr), Reason(NoCand), RepeatReasonSet(0) {}
808
809     bool isValid() const { return SU; }
810
811     // Copy the status of another candidate without changing policy.
812     void setBest(SchedCandidate &Best) {
813       assert(Best.Reason != NoCand && "uninitialized Sched candidate");
814       SU = Best.SU;
815       Reason = Best.Reason;
816       RPDelta = Best.RPDelta;
817       ResDelta = Best.ResDelta;
818     }
819
820     bool isRepeat(CandReason R) { return RepeatReasonSet & (1 << R); }
821     void setRepeat(CandReason R) { RepeatReasonSet |= (1 << R); }
822
823     void initResourceDelta(const ScheduleDAGMI *DAG,
824                            const TargetSchedModel *SchedModel);
825   };
826
827 protected:
828   const MachineSchedContext *Context;
829   const TargetSchedModel *SchedModel;
830   const TargetRegisterInfo *TRI;
831
832   SchedRemainder Rem;
833 protected:
834   GenericSchedulerBase(const MachineSchedContext *C):
835     Context(C), SchedModel(nullptr), TRI(nullptr) {}
836
837   void setPolicy(CandPolicy &Policy, bool IsPostRA, SchedBoundary &CurrZone,
838                  SchedBoundary *OtherZone);
839
840 #ifndef NDEBUG
841   void traceCandidate(const SchedCandidate &Cand);
842 #endif
843 };
844
845 /// GenericScheduler shrinks the unscheduled zone using heuristics to balance
846 /// the schedule.
847 class GenericScheduler : public GenericSchedulerBase {
848   ScheduleDAGMILive *DAG;
849
850   // State of the top and bottom scheduled instruction boundaries.
851   SchedBoundary Top;
852   SchedBoundary Bot;
853
854   MachineSchedPolicy RegionPolicy;
855 public:
856   GenericScheduler(const MachineSchedContext *C):
857     GenericSchedulerBase(C), DAG(nullptr), Top(SchedBoundary::TopQID, "TopQ"),
858     Bot(SchedBoundary::BotQID, "BotQ") {}
859
860   void initPolicy(MachineBasicBlock::iterator Begin,
861                   MachineBasicBlock::iterator End,
862                   unsigned NumRegionInstrs) override;
863
864   bool shouldTrackPressure() const override {
865     return RegionPolicy.ShouldTrackPressure;
866   }
867
868   void initialize(ScheduleDAGMI *dag) override;
869
870   SUnit *pickNode(bool &IsTopNode) override;
871
872   void schedNode(SUnit *SU, bool IsTopNode) override;
873
874   void releaseTopNode(SUnit *SU) override {
875     Top.releaseTopNode(SU);
876   }
877
878   void releaseBottomNode(SUnit *SU) override {
879     Bot.releaseBottomNode(SU);
880   }
881
882   void registerRoots() override;
883
884 protected:
885   void checkAcyclicLatency();
886
887   void tryCandidate(SchedCandidate &Cand,
888                     SchedCandidate &TryCand,
889                     SchedBoundary &Zone,
890                     const RegPressureTracker &RPTracker,
891                     RegPressureTracker &TempTracker);
892
893   SUnit *pickNodeBidirectional(bool &IsTopNode);
894
895   void pickNodeFromQueue(SchedBoundary &Zone,
896                          const RegPressureTracker &RPTracker,
897                          SchedCandidate &Candidate);
898
899   void reschedulePhysRegCopies(SUnit *SU, bool isTop);
900 };
901
902 /// PostGenericScheduler - Interface to the scheduling algorithm used by
903 /// ScheduleDAGMI.
904 ///
905 /// Callbacks from ScheduleDAGMI:
906 ///   initPolicy -> initialize(DAG) -> registerRoots -> pickNode ...
907 class PostGenericScheduler : public GenericSchedulerBase {
908   ScheduleDAGMI *DAG;
909   SchedBoundary Top;
910   SmallVector<SUnit*, 8> BotRoots;
911 public:
912   PostGenericScheduler(const MachineSchedContext *C):
913     GenericSchedulerBase(C), Top(SchedBoundary::TopQID, "TopQ") {}
914
915   virtual ~PostGenericScheduler() {}
916
917   void initPolicy(MachineBasicBlock::iterator Begin,
918                   MachineBasicBlock::iterator End,
919                   unsigned NumRegionInstrs) override {
920     /* no configurable policy */
921   };
922
923   /// PostRA scheduling does not track pressure.
924   bool shouldTrackPressure() const override { return false; }
925
926   void initialize(ScheduleDAGMI *Dag) override;
927
928   void registerRoots() override;
929
930   SUnit *pickNode(bool &IsTopNode) override;
931
932   void scheduleTree(unsigned SubtreeID) override {
933     llvm_unreachable("PostRA scheduler does not support subtree analysis.");
934   }
935
936   void schedNode(SUnit *SU, bool IsTopNode) override;
937
938   void releaseTopNode(SUnit *SU) override {
939     Top.releaseTopNode(SU);
940   }
941
942   // Only called for roots.
943   void releaseBottomNode(SUnit *SU) override {
944     BotRoots.push_back(SU);
945   }
946
947 protected:
948   void tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand);
949
950   void pickNodeFromQueue(SchedCandidate &Cand);
951 };
952
953 } // namespace llvm
954
955 #endif