Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:32

0001 //===- MachineScheduler.h - MachineInstr Scheduling Pass --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file provides an interface for customizing the standard MachineScheduler
0010 // pass. Note that the entire pass may be replaced as follows:
0011 //
0012 // <Target>TargetMachine::createPassConfig(PassManagerBase &PM) {
0013 //   PM.substitutePass(&MachineSchedulerID, &CustomSchedulerPassID);
0014 //   ...}
0015 //
0016 // The MachineScheduler pass is only responsible for choosing the regions to be
0017 // scheduled. Targets can override the DAG builder and scheduler without
0018 // replacing the pass as follows:
0019 //
0020 // ScheduleDAGInstrs *<Target>PassConfig::
0021 // createMachineScheduler(MachineSchedContext *C) {
0022 //   return new CustomMachineScheduler(C);
0023 // }
0024 //
0025 // The default scheduler, ScheduleDAGMILive, builds the DAG and drives list
0026 // scheduling while updating the instruction stream, register pressure, and live
0027 // intervals. Most targets don't need to override the DAG builder and list
0028 // scheduler, but subtargets that require custom scheduling heuristics may
0029 // plugin an alternate MachineSchedStrategy. The strategy is responsible for
0030 // selecting the highest priority node from the list:
0031 //
0032 // ScheduleDAGInstrs *<Target>PassConfig::
0033 // createMachineScheduler(MachineSchedContext *C) {
0034 //   return new ScheduleDAGMILive(C, CustomStrategy(C));
0035 // }
0036 //
0037 // The DAG builder can also be customized in a sense by adding DAG mutations
0038 // that will run after DAG building and before list scheduling. DAG mutations
0039 // can adjust dependencies based on target-specific knowledge or add weak edges
0040 // to aid heuristics:
0041 //
0042 // ScheduleDAGInstrs *<Target>PassConfig::
0043 // createMachineScheduler(MachineSchedContext *C) {
0044 //   ScheduleDAGMI *DAG = createGenericSchedLive(C);
0045 //   DAG->addMutation(new CustomDAGMutation(...));
0046 //   return DAG;
0047 // }
0048 //
0049 // A target that supports alternative schedulers can use the
0050 // MachineSchedRegistry to allow command line selection. This can be done by
0051 // implementing the following boilerplate:
0052 //
0053 // static ScheduleDAGInstrs *createCustomMachineSched(MachineSchedContext *C) {
0054 //  return new CustomMachineScheduler(C);
0055 // }
0056 // static MachineSchedRegistry
0057 // SchedCustomRegistry("custom", "Run my target's custom scheduler",
0058 //                     createCustomMachineSched);
0059 //
0060 //
0061 // Finally, subtargets that don't need to implement custom heuristics but would
0062 // like to configure the GenericScheduler's policy for a given scheduler region,
0063 // including scheduling direction and register pressure tracking policy, can do
0064 // this:
0065 //
0066 // void <SubTarget>Subtarget::
0067 // overrideSchedPolicy(MachineSchedPolicy &Policy,
0068 //                     unsigned NumRegionInstrs) const {
0069 //   Policy.<Flag> = true;
0070 // }
0071 //
0072 //===----------------------------------------------------------------------===//
0073 
0074 #ifndef LLVM_CODEGEN_MACHINESCHEDULER_H
0075 #define LLVM_CODEGEN_MACHINESCHEDULER_H
0076 
0077 #include "llvm/ADT/APInt.h"
0078 #include "llvm/ADT/ArrayRef.h"
0079 #include "llvm/ADT/BitVector.h"
0080 #include "llvm/ADT/STLExtras.h"
0081 #include "llvm/ADT/SmallVector.h"
0082 #include "llvm/ADT/StringRef.h"
0083 #include "llvm/ADT/Twine.h"
0084 #include "llvm/CodeGen/MachineBasicBlock.h"
0085 #include "llvm/CodeGen/MachinePassRegistry.h"
0086 #include "llvm/CodeGen/RegisterPressure.h"
0087 #include "llvm/CodeGen/ScheduleDAG.h"
0088 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
0089 #include "llvm/CodeGen/ScheduleDAGMutation.h"
0090 #include "llvm/CodeGen/TargetSchedule.h"
0091 #include "llvm/Support/CommandLine.h"
0092 #include "llvm/Support/ErrorHandling.h"
0093 #include <algorithm>
0094 #include <cassert>
0095 #include <llvm/Support/raw_ostream.h>
0096 #include <memory>
0097 #include <string>
0098 #include <vector>
0099 
0100 namespace llvm {
0101 
0102 namespace MISched {
0103 enum Direction {
0104   Unspecified,
0105   TopDown,
0106   BottomUp,
0107   Bidirectional,
0108 };
0109 } // namespace MISched
0110 
0111 extern cl::opt<MISched::Direction> PreRADirection;
0112 extern cl::opt<bool> VerifyScheduling;
0113 #ifndef NDEBUG
0114 extern cl::opt<bool> ViewMISchedDAGs;
0115 extern cl::opt<bool> PrintDAGs;
0116 #else
0117 extern const bool ViewMISchedDAGs;
0118 extern const bool PrintDAGs;
0119 #endif
0120 
0121 class AAResults;
0122 class LiveIntervals;
0123 class MachineDominatorTree;
0124 class MachineFunction;
0125 class MachineInstr;
0126 class MachineLoopInfo;
0127 class RegisterClassInfo;
0128 class SchedDFSResult;
0129 class ScheduleHazardRecognizer;
0130 class TargetInstrInfo;
0131 class TargetPassConfig;
0132 class TargetRegisterInfo;
0133 
0134 /// MachineSchedContext provides enough context from the MachineScheduler pass
0135 /// for the target to instantiate a scheduler.
0136 struct MachineSchedContext {
0137   MachineFunction *MF = nullptr;
0138   const MachineLoopInfo *MLI = nullptr;
0139   const MachineDominatorTree *MDT = nullptr;
0140   const TargetPassConfig *PassConfig = nullptr;
0141   AAResults *AA = nullptr;
0142   LiveIntervals *LIS = nullptr;
0143 
0144   RegisterClassInfo *RegClassInfo;
0145 
0146   MachineSchedContext();
0147   MachineSchedContext &operator=(const MachineSchedContext &other) = delete;
0148   MachineSchedContext(const MachineSchedContext &other) = delete;
0149   virtual ~MachineSchedContext();
0150 };
0151 
0152 /// MachineSchedRegistry provides a selection of available machine instruction
0153 /// schedulers.
0154 class MachineSchedRegistry
0155     : public MachinePassRegistryNode<
0156           ScheduleDAGInstrs *(*)(MachineSchedContext *)> {
0157 public:
0158   using ScheduleDAGCtor = ScheduleDAGInstrs *(*)(MachineSchedContext *);
0159 
0160   // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
0161   using FunctionPassCtor = ScheduleDAGCtor;
0162 
0163   static MachinePassRegistry<ScheduleDAGCtor> Registry;
0164 
0165   MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
0166       : MachinePassRegistryNode(N, D, C) {
0167     Registry.Add(this);
0168   }
0169 
0170   ~MachineSchedRegistry() { Registry.Remove(this); }
0171 
0172   // Accessors.
0173   //
0174   MachineSchedRegistry *getNext() const {
0175     return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
0176   }
0177 
0178   static MachineSchedRegistry *getList() {
0179     return (MachineSchedRegistry *)Registry.getList();
0180   }
0181 
0182   static void setListener(MachinePassRegistryListener<FunctionPassCtor> *L) {
0183     Registry.setListener(L);
0184   }
0185 };
0186 
0187 class ScheduleDAGMI;
0188 
0189 /// Define a generic scheduling policy for targets that don't provide their own
0190 /// MachineSchedStrategy. This can be overriden for each scheduling region
0191 /// before building the DAG.
0192 struct MachineSchedPolicy {
0193   // Allow the scheduler to disable register pressure tracking.
0194   bool ShouldTrackPressure = false;
0195   /// Track LaneMasks to allow reordering of independent subregister writes
0196   /// of the same vreg. \sa MachineSchedStrategy::shouldTrackLaneMasks()
0197   bool ShouldTrackLaneMasks = false;
0198 
0199   // Allow the scheduler to force top-down or bottom-up scheduling. If neither
0200   // is true, the scheduler runs in both directions and converges.
0201   bool OnlyTopDown = false;
0202   bool OnlyBottomUp = false;
0203 
0204   // Disable heuristic that tries to fetch nodes from long dependency chains
0205   // first.
0206   bool DisableLatencyHeuristic = false;
0207 
0208   // Compute DFSResult for use in scheduling heuristics.
0209   bool ComputeDFSResult = false;
0210 
0211   MachineSchedPolicy() = default;
0212 };
0213 
0214 /// MachineSchedStrategy - Interface to the scheduling algorithm used by
0215 /// ScheduleDAGMI.
0216 ///
0217 /// Initialization sequence:
0218 ///   initPolicy -> shouldTrackPressure -> initialize(DAG) -> registerRoots
0219 class MachineSchedStrategy {
0220   virtual void anchor();
0221 
0222 public:
0223   virtual ~MachineSchedStrategy() = default;
0224 
0225   /// Optionally override the per-region scheduling policy.
0226   virtual void initPolicy(MachineBasicBlock::iterator Begin,
0227                           MachineBasicBlock::iterator End,
0228                           unsigned NumRegionInstrs) {}
0229 
0230   virtual MachineSchedPolicy getPolicy() const { return {}; }
0231   virtual void dumpPolicy() const {}
0232 
0233   /// Check if pressure tracking is needed before building the DAG and
0234   /// initializing this strategy. Called after initPolicy.
0235   virtual bool shouldTrackPressure() const { return true; }
0236 
0237   /// Returns true if lanemasks should be tracked. LaneMask tracking is
0238   /// necessary to reorder independent subregister defs for the same vreg.
0239   /// This has to be enabled in combination with shouldTrackPressure().
0240   virtual bool shouldTrackLaneMasks() const { return false; }
0241 
0242   // If this method returns true, handling of the scheduling regions
0243   // themselves (in case of a scheduling boundary in MBB) will be done
0244   // beginning with the topmost region of MBB.
0245   virtual bool doMBBSchedRegionsTopDown() const { return false; }
0246 
0247   /// Initialize the strategy after building the DAG for a new region.
0248   virtual void initialize(ScheduleDAGMI *DAG) = 0;
0249 
0250   /// Tell the strategy that MBB is about to be processed.
0251   virtual void enterMBB(MachineBasicBlock *MBB) {};
0252 
0253   /// Tell the strategy that current MBB is done.
0254   virtual void leaveMBB() {};
0255 
0256   /// Notify this strategy that all roots have been released (including those
0257   /// that depend on EntrySU or ExitSU).
0258   virtual void registerRoots() {}
0259 
0260   /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to
0261   /// schedule the node at the top of the unscheduled region. Otherwise it will
0262   /// be scheduled at the bottom.
0263   virtual SUnit *pickNode(bool &IsTopNode) = 0;
0264 
0265   /// Scheduler callback to notify that a new subtree is scheduled.
0266   virtual void scheduleTree(unsigned SubtreeID) {}
0267 
0268   /// Notify MachineSchedStrategy that ScheduleDAGMI has scheduled an
0269   /// instruction and updated scheduled/remaining flags in the DAG nodes.
0270   virtual void schedNode(SUnit *SU, bool IsTopNode) = 0;
0271 
0272   /// When all predecessor dependencies have been resolved, free this node for
0273   /// top-down scheduling.
0274   virtual void releaseTopNode(SUnit *SU) = 0;
0275 
0276   /// When all successor dependencies have been resolved, free this node for
0277   /// bottom-up scheduling.
0278   virtual void releaseBottomNode(SUnit *SU) = 0;
0279 };
0280 
0281 /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply
0282 /// schedules machine instructions according to the given MachineSchedStrategy
0283 /// without much extra book-keeping. This is the common functionality between
0284 /// PreRA and PostRA MachineScheduler.
0285 class ScheduleDAGMI : public ScheduleDAGInstrs {
0286 protected:
0287   AAResults *AA;
0288   LiveIntervals *LIS;
0289   std::unique_ptr<MachineSchedStrategy> SchedImpl;
0290 
0291   /// Ordered list of DAG postprocessing steps.
0292   std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
0293 
0294   /// The top of the unscheduled zone.
0295   MachineBasicBlock::iterator CurrentTop;
0296 
0297   /// The bottom of the unscheduled zone.
0298   MachineBasicBlock::iterator CurrentBottom;
0299 
0300   /// Record the next node in a scheduled cluster.
0301   const SUnit *NextClusterPred = nullptr;
0302   const SUnit *NextClusterSucc = nullptr;
0303 
0304 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
0305   /// The number of instructions scheduled so far. Used to cut off the
0306   /// scheduler at the point determined by misched-cutoff.
0307   unsigned NumInstrsScheduled = 0;
0308 #endif
0309 
0310 public:
0311   ScheduleDAGMI(MachineSchedContext *C, std::unique_ptr<MachineSchedStrategy> S,
0312                 bool RemoveKillFlags)
0313       : ScheduleDAGInstrs(*C->MF, C->MLI, RemoveKillFlags), AA(C->AA),
0314         LIS(C->LIS), SchedImpl(std::move(S)) {}
0315 
0316   // Provide a vtable anchor
0317   ~ScheduleDAGMI() override;
0318 
0319   /// If this method returns true, handling of the scheduling regions
0320   /// themselves (in case of a scheduling boundary in MBB) will be done
0321   /// beginning with the topmost region of MBB.
0322   bool doMBBSchedRegionsTopDown() const override {
0323     return SchedImpl->doMBBSchedRegionsTopDown();
0324   }
0325 
0326   // Returns LiveIntervals instance for use in DAG mutators and such.
0327   LiveIntervals *getLIS() const { return LIS; }
0328 
0329   /// Return true if this DAG supports VReg liveness and RegPressure.
0330   virtual bool hasVRegLiveness() const { return false; }
0331 
0332   /// Add a postprocessing step to the DAG builder.
0333   /// Mutations are applied in the order that they are added after normal DAG
0334   /// building and before MachineSchedStrategy initialization.
0335   ///
0336   /// ScheduleDAGMI takes ownership of the Mutation object.
0337   void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
0338     if (Mutation)
0339       Mutations.push_back(std::move(Mutation));
0340   }
0341 
0342   MachineBasicBlock::iterator top() const { return CurrentTop; }
0343   MachineBasicBlock::iterator bottom() const { return CurrentBottom; }
0344 
0345   /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
0346   /// region. This covers all instructions in a block, while schedule() may only
0347   /// cover a subset.
0348   void enterRegion(MachineBasicBlock *bb,
0349                    MachineBasicBlock::iterator begin,
0350                    MachineBasicBlock::iterator end,
0351                    unsigned regioninstrs) override;
0352 
0353   /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
0354   /// reorderable instructions.
0355   void schedule() override;
0356 
0357   void startBlock(MachineBasicBlock *bb) override;
0358   void finishBlock() override;
0359 
0360   /// Change the position of an instruction within the basic block and update
0361   /// live ranges and region boundary iterators.
0362   void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos);
0363 
0364   const SUnit *getNextClusterPred() const { return NextClusterPred; }
0365 
0366   const SUnit *getNextClusterSucc() const { return NextClusterSucc; }
0367 
0368   void viewGraph(const Twine &Name, const Twine &Title) override;
0369   void viewGraph() override;
0370 
0371 protected:
0372   // Top-Level entry points for the schedule() driver...
0373 
0374   /// Apply each ScheduleDAGMutation step in order. This allows different
0375   /// instances of ScheduleDAGMI to perform custom DAG postprocessing.
0376   void postProcessDAG();
0377 
0378   /// Release ExitSU predecessors and setup scheduler queues.
0379   void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots);
0380 
0381   /// Update scheduler DAG and queues after scheduling an instruction.
0382   void updateQueues(SUnit *SU, bool IsTopNode);
0383 
0384   /// Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
0385   void placeDebugValues();
0386 
0387   /// dump the scheduled Sequence.
0388   void dumpSchedule() const;
0389   /// Print execution trace of the schedule top-down or bottom-up.
0390   void dumpScheduleTraceTopDown() const;
0391   void dumpScheduleTraceBottomUp() const;
0392 
0393   // Lesser helpers...
0394   bool checkSchedLimit();
0395 
0396   void findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots,
0397                              SmallVectorImpl<SUnit*> &BotRoots);
0398 
0399   void releaseSucc(SUnit *SU, SDep *SuccEdge);
0400   void releaseSuccessors(SUnit *SU);
0401   void releasePred(SUnit *SU, SDep *PredEdge);
0402   void releasePredecessors(SUnit *SU);
0403 };
0404 
0405 /// ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules
0406 /// machine instructions while updating LiveIntervals and tracking regpressure.
0407 class ScheduleDAGMILive : public ScheduleDAGMI {
0408 protected:
0409   RegisterClassInfo *RegClassInfo;
0410 
0411   /// Information about DAG subtrees. If DFSResult is NULL, then SchedulerTrees
0412   /// will be empty.
0413   SchedDFSResult *DFSResult = nullptr;
0414   BitVector ScheduledTrees;
0415 
0416   MachineBasicBlock::iterator LiveRegionEnd;
0417 
0418   /// Maps vregs to the SUnits of their uses in the current scheduling region.
0419   VReg2SUnitMultiMap VRegUses;
0420 
0421   // Map each SU to its summary of pressure changes. This array is updated for
0422   // liveness during bottom-up scheduling. Top-down scheduling may proceed but
0423   // has no affect on the pressure diffs.
0424   PressureDiffs SUPressureDiffs;
0425 
0426   /// Register pressure in this region computed by initRegPressure.
0427   bool ShouldTrackPressure = false;
0428   bool ShouldTrackLaneMasks = false;
0429   IntervalPressure RegPressure;
0430   RegPressureTracker RPTracker;
0431 
0432   /// List of pressure sets that exceed the target's pressure limit before
0433   /// scheduling, listed in increasing set ID order. Each pressure set is paired
0434   /// with its max pressure in the currently scheduled regions.
0435   std::vector<PressureChange> RegionCriticalPSets;
0436 
0437   /// The top of the unscheduled zone.
0438   IntervalPressure TopPressure;
0439   RegPressureTracker TopRPTracker;
0440 
0441   /// The bottom of the unscheduled zone.
0442   IntervalPressure BotPressure;
0443   RegPressureTracker BotRPTracker;
0444 
0445 public:
0446   ScheduleDAGMILive(MachineSchedContext *C,
0447                     std::unique_ptr<MachineSchedStrategy> S)
0448       : ScheduleDAGMI(C, std::move(S), /*RemoveKillFlags=*/false),
0449         RegClassInfo(C->RegClassInfo), RPTracker(RegPressure),
0450         TopRPTracker(TopPressure), BotRPTracker(BotPressure) {}
0451 
0452   ~ScheduleDAGMILive() override;
0453 
0454   /// Return true if this DAG supports VReg liveness and RegPressure.
0455   bool hasVRegLiveness() const override { return true; }
0456 
0457   /// Return true if register pressure tracking is enabled.
0458   bool isTrackingPressure() const { return ShouldTrackPressure; }
0459 
0460   /// Get current register pressure for the top scheduled instructions.
0461   const IntervalPressure &getTopPressure() const { return TopPressure; }
0462   const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; }
0463 
0464   /// Get current register pressure for the bottom scheduled instructions.
0465   const IntervalPressure &getBotPressure() const { return BotPressure; }
0466   const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; }
0467 
0468   /// Get register pressure for the entire scheduling region before scheduling.
0469   const IntervalPressure &getRegPressure() const { return RegPressure; }
0470 
0471   const std::vector<PressureChange> &getRegionCriticalPSets() const {
0472     return RegionCriticalPSets;
0473   }
0474 
0475   PressureDiff &getPressureDiff(const SUnit *SU) {
0476     return SUPressureDiffs[SU->NodeNum];
0477   }
0478   const PressureDiff &getPressureDiff(const SUnit *SU) const {
0479     return SUPressureDiffs[SU->NodeNum];
0480   }
0481 
0482   /// Compute a DFSResult after DAG building is complete, and before any
0483   /// queue comparisons.
0484   void computeDFSResult();
0485 
0486   /// Return a non-null DFS result if the scheduling strategy initialized it.
0487   const SchedDFSResult *getDFSResult() const { return DFSResult; }
0488 
0489   BitVector &getScheduledTrees() { return ScheduledTrees; }
0490 
0491   /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
0492   /// region. This covers all instructions in a block, while schedule() may only
0493   /// cover a subset.
0494   void enterRegion(MachineBasicBlock *bb,
0495                    MachineBasicBlock::iterator begin,
0496                    MachineBasicBlock::iterator end,
0497                    unsigned regioninstrs) override;
0498 
0499   /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
0500   /// reorderable instructions.
0501   void schedule() override;
0502 
0503   /// Compute the cyclic critical path through the DAG.
0504   unsigned computeCyclicCriticalPath();
0505 
0506   void dump() const override;
0507 
0508 protected:
0509   // Top-Level entry points for the schedule() driver...
0510 
0511   /// Call ScheduleDAGInstrs::buildSchedGraph with register pressure tracking
0512   /// enabled. This sets up three trackers. RPTracker will cover the entire DAG
0513   /// region, TopTracker and BottomTracker will be initialized to the top and
0514   /// bottom of the DAG region without covereing any unscheduled instruction.
0515   void buildDAGWithRegPressure();
0516 
0517   /// Release ExitSU predecessors and setup scheduler queues. Re-position
0518   /// the Top RP tracker in case the region beginning has changed.
0519   void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots);
0520 
0521   /// Move an instruction and update register pressure.
0522   void scheduleMI(SUnit *SU, bool IsTopNode);
0523 
0524   // Lesser helpers...
0525 
0526   void initRegPressure();
0527 
0528   void updatePressureDiffs(ArrayRef<VRegMaskOrUnit> LiveUses);
0529 
0530   void updateScheduledPressure(const SUnit *SU,
0531                                const std::vector<unsigned> &NewMaxPressure);
0532 
0533   void collectVRegUses(SUnit &SU);
0534 };
0535 
0536 //===----------------------------------------------------------------------===//
0537 ///
0538 /// Helpers for implementing custom MachineSchedStrategy classes. These take
0539 /// care of the book-keeping associated with list scheduling heuristics.
0540 ///
0541 //===----------------------------------------------------------------------===//
0542 
0543 /// ReadyQueue encapsulates vector of "ready" SUnits with basic convenience
0544 /// methods for pushing and removing nodes. ReadyQueue's are uniquely identified
0545 /// by an ID. SUnit::NodeQueueId is a mask of the ReadyQueues the SUnit is in.
0546 ///
0547 /// This is a convenience class that may be used by implementations of
0548 /// MachineSchedStrategy.
0549 class ReadyQueue {
0550   unsigned ID;
0551   std::string Name;
0552   std::vector<SUnit*> Queue;
0553 
0554 public:
0555   ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {}
0556 
0557   unsigned getID() const { return ID; }
0558 
0559   StringRef getName() const { return Name; }
0560 
0561   // SU is in this queue if it's NodeQueueID is a superset of this ID.
0562   bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); }
0563 
0564   bool empty() const { return Queue.empty(); }
0565 
0566   void clear() { Queue.clear(); }
0567 
0568   unsigned size() const { return Queue.size(); }
0569 
0570   using iterator = std::vector<SUnit*>::iterator;
0571 
0572   iterator begin() { return Queue.begin(); }
0573 
0574   iterator end() { return Queue.end(); }
0575 
0576   ArrayRef<SUnit*> elements() { return Queue; }
0577 
0578   iterator find(SUnit *SU) { return llvm::find(Queue, SU); }
0579 
0580   void push(SUnit *SU) {
0581     Queue.push_back(SU);
0582     SU->NodeQueueId |= ID;
0583   }
0584 
0585   iterator remove(iterator I) {
0586     (*I)->NodeQueueId &= ~ID;
0587     *I = Queue.back();
0588     unsigned idx = I - Queue.begin();
0589     Queue.pop_back();
0590     return Queue.begin() + idx;
0591   }
0592 
0593   void dump() const;
0594 };
0595 
0596 /// Summarize the unscheduled region.
0597 struct SchedRemainder {
0598   // Critical path through the DAG in expected latency.
0599   unsigned CriticalPath;
0600   unsigned CyclicCritPath;
0601 
0602   // Scaled count of micro-ops left to schedule.
0603   unsigned RemIssueCount;
0604 
0605   bool IsAcyclicLatencyLimited;
0606 
0607   // Unscheduled resources
0608   SmallVector<unsigned, 16> RemainingCounts;
0609 
0610   SchedRemainder() { reset(); }
0611 
0612   void reset() {
0613     CriticalPath = 0;
0614     CyclicCritPath = 0;
0615     RemIssueCount = 0;
0616     IsAcyclicLatencyLimited = false;
0617     RemainingCounts.clear();
0618   }
0619 
0620   void init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel);
0621 };
0622 
0623 /// ResourceSegments are a collection of intervals closed on the
0624 /// left and opened on the right:
0625 ///
0626 ///     list{ [a1, b1), [a2, b2), ..., [a_N, b_N) }
0627 ///
0628 /// The collection has the following properties:
0629 ///
0630 /// 1. The list is ordered: a_i < b_i and b_i < a_(i+1)
0631 ///
0632 /// 2. The intervals in the collection do not intersect each other.
0633 ///
0634 /// A \ref ResourceSegments instance represents the cycle
0635 /// reservation history of the instance of and individual resource.
0636 class ResourceSegments {
0637 public:
0638   /// Represents an interval of discrete integer values closed on
0639   /// the left and open on the right: [a, b).
0640   typedef std::pair<int64_t, int64_t> IntervalTy;
0641 
0642   /// Adds an interval [a, b) to the collection of the instance.
0643   ///
0644   /// When adding [a, b[ to the collection, the operation merges the
0645   /// adjacent intervals. For example
0646   ///
0647   ///       0  1  2  3  4  5  6  7  8  9  10
0648   ///       [-----)  [--)     [--)
0649   ///     +       [--)
0650   ///     = [-----------)     [--)
0651   ///
0652   /// To be able to debug duplicate resource usage, the function has
0653   /// assertion that checks that no interval should be added if it
0654   /// overlaps any of the intervals in the collection. We can
0655   /// require this because by definition a \ref ResourceSegments is
0656   /// attached only to an individual resource instance.
0657   void add(IntervalTy A, const unsigned CutOff = 10);
0658 
0659 public:
0660   /// Checks whether intervals intersect.
0661   static bool intersects(IntervalTy A, IntervalTy B);
0662 
0663   /// These function return the interval used by a resource in bottom and top
0664   /// scheduling.
0665   ///
0666   /// Consider an instruction that uses resources X0, X1 and X2 as follows:
0667   ///
0668   /// X0 X1 X1 X2    +--------+-------------+--------------+
0669   ///                |Resource|AcquireAtCycle|ReleaseAtCycle|
0670   ///                +--------+-------------+--------------+
0671   ///                |   X0   |     0       |       1      |
0672   ///                +--------+-------------+--------------+
0673   ///                |   X1   |     1       |       3      |
0674   ///                +--------+-------------+--------------+
0675   ///                |   X2   |     3       |       4      |
0676   ///                +--------+-------------+--------------+
0677   ///
0678   /// If we can schedule the instruction at cycle C, we need to
0679   /// compute the interval of the resource as follows:
0680   ///
0681   /// # TOP DOWN SCHEDULING
0682   ///
0683   /// Cycles scheduling flows to the _right_, in the same direction
0684   /// of time.
0685   ///
0686   ///       C      1      2      3      4      5  ...
0687   /// ------|------|------|------|------|------|----->
0688   ///       X0     X1     X1     X2   ---> direction of time
0689   /// X0    [C, C+1)
0690   /// X1           [C+1,      C+3)
0691   /// X2                         [C+3, C+4)
0692   ///
0693   /// Therefore, the formula to compute the interval for a resource
0694   /// of an instruction that can be scheduled at cycle C in top-down
0695   /// scheduling is:
0696   ///
0697   ///       [C+AcquireAtCycle, C+ReleaseAtCycle)
0698   ///
0699   ///
0700   /// # BOTTOM UP SCHEDULING
0701   ///
0702   /// Cycles scheduling flows to the _left_, in opposite direction
0703   /// of time.
0704   ///
0705   /// In bottom up scheduling, the scheduling happens in opposite
0706   /// direction to the execution of the cycles of the
0707   /// instruction. When the instruction is scheduled at cycle `C`,
0708   /// the resources are allocated in the past relative to `C`:
0709   ///
0710   ///       2      1      C     -1     -2     -3     -4     -5  ...
0711   /// <-----|------|------|------|------|------|------|------|---
0712   ///                     X0     X1     X1     X2   ---> direction of time
0713   /// X0           (C+1, C]
0714   /// X1                  (C,        C-2]
0715   /// X2                              (C-2, C-3]
0716   ///
0717   /// Therefore, the formula to compute the interval for a resource
0718   /// of an instruction that can be scheduled at cycle C in bottom-up
0719   /// scheduling is:
0720   ///
0721   ///       [C-ReleaseAtCycle+1, C-AcquireAtCycle+1)
0722   ///
0723   ///
0724   /// NOTE: In both cases, the number of cycles booked by a
0725   /// resources is the value (ReleaseAtCycle - AcquireAtCycle).
0726   static IntervalTy getResourceIntervalBottom(unsigned C, unsigned AcquireAtCycle,
0727                                               unsigned ReleaseAtCycle) {
0728     return std::make_pair<long, long>((long)C - (long)ReleaseAtCycle + 1L,
0729                                       (long)C - (long)AcquireAtCycle + 1L);
0730   }
0731   static IntervalTy getResourceIntervalTop(unsigned C, unsigned AcquireAtCycle,
0732                                            unsigned ReleaseAtCycle) {
0733     return std::make_pair<long, long>((long)C + (long)AcquireAtCycle,
0734                                       (long)C + (long)ReleaseAtCycle);
0735   }
0736 
0737 private:
0738   /// Finds the first cycle in which a resource can be allocated.
0739   ///
0740   /// The function uses the \param IntervalBuider [*] to build a
0741   /// resource interval [a, b[ out of the input parameters \param
0742   /// CurrCycle, \param AcquireAtCycle and \param ReleaseAtCycle.
0743   ///
0744   /// The function then loops through the intervals in the ResourceSegments
0745   /// and shifts the interval [a, b[ and the ReturnCycle to the
0746   /// right until there is no intersection between the intervals of
0747   /// the \ref ResourceSegments instance and the new shifted [a, b[. When
0748   /// this condition is met, the ReturnCycle  (which
0749   /// correspond to the cycle in which the resource can be
0750   /// allocated) is returned.
0751   ///
0752   ///               c = CurrCycle in input
0753   ///               c   1   2   3   4   5   6   7   8   9   10 ... ---> (time
0754   ///               flow)
0755   ///  ResourceSegments...  [---)   [-------)           [-----------)
0756   ///               c   [1     3[  -> AcquireAtCycle=1, ReleaseAtCycle=3
0757   ///                 ++c   [1     3)
0758   ///                     ++c   [1     3)
0759   ///                         ++c   [1     3)
0760   ///                             ++c   [1     3)
0761   ///                                 ++c   [1     3)    ---> returns c
0762   ///                                 incremented by 5 (c+5)
0763   ///
0764   ///
0765   /// Notice that for bottom-up scheduling the diagram is slightly
0766   /// different because the current cycle c is always on the right
0767   /// of the interval [a, b) (see \ref
0768   /// `getResourceIntervalBottom`). This is because the cycle
0769   /// increments for bottom-up scheduling moved in the direction
0770   /// opposite to the direction of time:
0771   ///
0772   ///     --------> direction of time.
0773   ///     XXYZZZ    (resource usage)
0774   ///     --------> direction of top-down execution cycles.
0775   ///     <-------- direction of bottom-up execution cycles.
0776   ///
0777   /// Even though bottom-up scheduling moves against the flow of
0778   /// time, the algorithm used to find the first free slot in between
0779   /// intervals is the same as for top-down scheduling.
0780   ///
0781   /// [*] See \ref `getResourceIntervalTop` and
0782   /// \ref `getResourceIntervalBottom` to see how such resource intervals
0783   /// are built.
0784   unsigned getFirstAvailableAt(
0785       unsigned CurrCycle, unsigned AcquireAtCycle, unsigned ReleaseAtCycle,
0786       std::function<IntervalTy(unsigned, unsigned, unsigned)> IntervalBuilder)
0787       const;
0788 
0789 public:
0790   /// getFirstAvailableAtFromBottom and getFirstAvailableAtFromTop
0791   /// should be merged in a single function in which a function that
0792   /// creates the `NewInterval` is passed as a parameter.
0793   unsigned getFirstAvailableAtFromBottom(unsigned CurrCycle,
0794                                          unsigned AcquireAtCycle,
0795                                          unsigned ReleaseAtCycle) const {
0796     return getFirstAvailableAt(CurrCycle, AcquireAtCycle, ReleaseAtCycle,
0797                                getResourceIntervalBottom);
0798   }
0799   unsigned getFirstAvailableAtFromTop(unsigned CurrCycle,
0800                                       unsigned AcquireAtCycle,
0801                                       unsigned ReleaseAtCycle) const {
0802     return getFirstAvailableAt(CurrCycle, AcquireAtCycle, ReleaseAtCycle,
0803                                getResourceIntervalTop);
0804   }
0805 
0806 private:
0807   std::list<IntervalTy> _Intervals;
0808   /// Merge all adjacent intervals in the collection. For all pairs
0809   /// of adjacient intervals, it performs [a, b) + [b, c) -> [a, c).
0810   ///
0811   /// Before performing the merge operation, the intervals are
0812   /// sorted with \ref sort_predicate.
0813   void sortAndMerge();
0814 
0815 public:
0816   // constructor for empty set
0817   explicit ResourceSegments(){};
0818   bool empty() const { return _Intervals.empty(); }
0819   explicit ResourceSegments(const std::list<IntervalTy> &Intervals)
0820       : _Intervals(Intervals) {
0821     sortAndMerge();
0822   }
0823 
0824   friend bool operator==(const ResourceSegments &c1,
0825                          const ResourceSegments &c2) {
0826     return c1._Intervals == c2._Intervals;
0827   }
0828   friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
0829                                        const ResourceSegments &Segments) {
0830     os << "{ ";
0831     for (auto p : Segments._Intervals)
0832       os << "[" << p.first << ", " << p.second << "), ";
0833     os << "}\n";
0834     return os;
0835   }
0836 };
0837 
0838 /// Each Scheduling boundary is associated with ready queues. It tracks the
0839 /// current cycle in the direction of movement, and maintains the state
0840 /// of "hazards" and other interlocks at the current cycle.
0841 class SchedBoundary {
0842 public:
0843   /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
0844   enum {
0845     TopQID = 1,
0846     BotQID = 2,
0847     LogMaxQID = 2
0848   };
0849 
0850   ScheduleDAGMI *DAG = nullptr;
0851   const TargetSchedModel *SchedModel = nullptr;
0852   SchedRemainder *Rem = nullptr;
0853 
0854   ReadyQueue Available;
0855   ReadyQueue Pending;
0856 
0857   ScheduleHazardRecognizer *HazardRec = nullptr;
0858 
0859 private:
0860   /// True if the pending Q should be checked/updated before scheduling another
0861   /// instruction.
0862   bool CheckPending;
0863 
0864   /// Number of cycles it takes to issue the instructions scheduled in this
0865   /// zone. It is defined as: scheduled-micro-ops / issue-width + stalls.
0866   /// See getStalls().
0867   unsigned CurrCycle;
0868 
0869   /// Micro-ops issued in the current cycle
0870   unsigned CurrMOps;
0871 
0872   /// MinReadyCycle - Cycle of the soonest available instruction.
0873   unsigned MinReadyCycle;
0874 
0875   // The expected latency of the critical path in this scheduled zone.
0876   unsigned ExpectedLatency;
0877 
0878   // The latency of dependence chains leading into this zone.
0879   // For each node scheduled bottom-up: DLat = max DLat, N.Depth.
0880   // For each cycle scheduled: DLat -= 1.
0881   unsigned DependentLatency;
0882 
0883   /// Count the scheduled (issued) micro-ops that can be retired by
0884   /// time=CurrCycle assuming the first scheduled instr is retired at time=0.
0885   unsigned RetiredMOps;
0886 
0887   // Count scheduled resources that have been executed. Resources are
0888   // considered executed if they become ready in the time that it takes to
0889   // saturate any resource including the one in question. Counts are scaled
0890   // for direct comparison with other resources. Counts can be compared with
0891   // MOps * getMicroOpFactor and Latency * getLatencyFactor.
0892   SmallVector<unsigned, 16> ExecutedResCounts;
0893 
0894   /// Cache the max count for a single resource.
0895   unsigned MaxExecutedResCount;
0896 
0897   // Cache the critical resources ID in this scheduled zone.
0898   unsigned ZoneCritResIdx;
0899 
0900   // Is the scheduled region resource limited vs. latency limited.
0901   bool IsResourceLimited;
0902 
0903 public:
0904 private:
0905   /// Record how resources have been allocated across the cycles of
0906   /// the execution.
0907   std::map<unsigned, ResourceSegments> ReservedResourceSegments;
0908   std::vector<unsigned> ReservedCycles;
0909   /// For each PIdx, stores first index into ReservedResourceSegments that
0910   /// corresponds to it.
0911   ///
0912   /// For example, consider the following 3 resources (ResourceCount =
0913   /// 3):
0914   ///
0915   ///   +------------+--------+
0916   ///   |ResourceName|NumUnits|
0917   ///   +------------+--------+
0918   ///   |     X      |    2   |
0919   ///   +------------+--------+
0920   ///   |     Y      |    3   |
0921   ///   +------------+--------+
0922   ///   |     Z      |    1   |
0923   ///   +------------+--------+
0924   ///
0925   /// In this case, the total number of resource instances is 6. The
0926   /// vector \ref ReservedResourceSegments will have a slot for each instance.
0927   /// The vector \ref ReservedCyclesIndex will track at what index the first
0928   /// instance of the resource is found in the vector of \ref
0929   /// ReservedResourceSegments:
0930   ///
0931   ///                              Indexes of instances in
0932   ///                              ReservedResourceSegments
0933   ///
0934   ///                              0   1   2   3   4  5
0935   /// ReservedCyclesIndex[0] = 0; [X0, X1,
0936   /// ReservedCyclesIndex[1] = 2;          Y0, Y1, Y2
0937   /// ReservedCyclesIndex[2] = 5;                     Z
0938   SmallVector<unsigned, 16> ReservedCyclesIndex;
0939 
0940   // For each PIdx, stores the resource group IDs of its subunits
0941   SmallVector<APInt, 16> ResourceGroupSubUnitMasks;
0942 
0943 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
0944   // Remember the greatest possible stall as an upper bound on the number of
0945   // times we should retry the pending queue because of a hazard.
0946   unsigned MaxObservedStall;
0947 #endif
0948 
0949 public:
0950   /// Pending queues extend the ready queues with the same ID and the
0951   /// PendingFlag set.
0952   SchedBoundary(unsigned ID, const Twine &Name):
0953     Available(ID, Name+".A"), Pending(ID << LogMaxQID, Name+".P") {
0954     reset();
0955   }
0956   SchedBoundary &operator=(const SchedBoundary &other) = delete;
0957   SchedBoundary(const SchedBoundary &other) = delete;
0958   ~SchedBoundary();
0959 
0960   void reset();
0961 
0962   void init(ScheduleDAGMI *dag, const TargetSchedModel *smodel,
0963             SchedRemainder *rem);
0964 
0965   bool isTop() const {
0966     return Available.getID() == TopQID;
0967   }
0968 
0969   /// Number of cycles to issue the instructions scheduled in this zone.
0970   unsigned getCurrCycle() const { return CurrCycle; }
0971 
0972   /// Micro-ops issued in the current cycle
0973   unsigned getCurrMOps() const { return CurrMOps; }
0974 
0975   // The latency of dependence chains leading into this zone.
0976   unsigned getDependentLatency() const { return DependentLatency; }
0977 
0978   /// Get the number of latency cycles "covered" by the scheduled
0979   /// instructions. This is the larger of the critical path within the zone
0980   /// and the number of cycles required to issue the instructions.
0981   unsigned getScheduledLatency() const {
0982     return std::max(ExpectedLatency, CurrCycle);
0983   }
0984 
0985   unsigned getUnscheduledLatency(SUnit *SU) const {
0986     return isTop() ? SU->getHeight() : SU->getDepth();
0987   }
0988 
0989   unsigned getResourceCount(unsigned ResIdx) const {
0990     return ExecutedResCounts[ResIdx];
0991   }
0992 
0993   /// Get the scaled count of scheduled micro-ops and resources, including
0994   /// executed resources.
0995   unsigned getCriticalCount() const {
0996     if (!ZoneCritResIdx)
0997       return RetiredMOps * SchedModel->getMicroOpFactor();
0998     return getResourceCount(ZoneCritResIdx);
0999   }
1000 
1001   /// Get a scaled count for the minimum execution time of the scheduled
1002   /// micro-ops that are ready to execute by getExecutedCount. Notice the
1003   /// feedback loop.
1004   unsigned getExecutedCount() const {
1005     return std::max(CurrCycle * SchedModel->getLatencyFactor(),
1006                     MaxExecutedResCount);
1007   }
1008 
1009   unsigned getZoneCritResIdx() const { return ZoneCritResIdx; }
1010 
1011   // Is the scheduled region resource limited vs. latency limited.
1012   bool isResourceLimited() const { return IsResourceLimited; }
1013 
1014   /// Get the difference between the given SUnit's ready time and the current
1015   /// cycle.
1016   unsigned getLatencyStallCycles(SUnit *SU);
1017 
1018   unsigned getNextResourceCycleByInstance(unsigned InstanceIndex,
1019                                           unsigned ReleaseAtCycle,
1020                                           unsigned AcquireAtCycle);
1021 
1022   std::pair<unsigned, unsigned> getNextResourceCycle(const MCSchedClassDesc *SC,
1023                                                      unsigned PIdx,
1024                                                      unsigned ReleaseAtCycle,
1025                                                      unsigned AcquireAtCycle);
1026 
1027   bool isUnbufferedGroup(unsigned PIdx) const {
1028     return SchedModel->getProcResource(PIdx)->SubUnitsIdxBegin &&
1029            !SchedModel->getProcResource(PIdx)->BufferSize;
1030   }
1031 
1032   bool checkHazard(SUnit *SU);
1033 
1034   unsigned findMaxLatency(ArrayRef<SUnit*> ReadySUs);
1035 
1036   unsigned getOtherResourceCount(unsigned &OtherCritIdx);
1037 
1038   /// Release SU to make it ready. If it's not in hazard, remove it from
1039   /// pending queue (if already in) and push into available queue.
1040   /// Otherwise, push the SU into pending queue.
1041   ///
1042   /// @param SU The unit to be released.
1043   /// @param ReadyCycle Until which cycle the unit is ready.
1044   /// @param InPQueue Whether SU is already in pending queue.
1045   /// @param Idx Position offset in pending queue (if in it).
1046   void releaseNode(SUnit *SU, unsigned ReadyCycle, bool InPQueue,
1047                    unsigned Idx = 0);
1048 
1049   void bumpCycle(unsigned NextCycle);
1050 
1051   void incExecutedResources(unsigned PIdx, unsigned Count);
1052 
1053   unsigned countResource(const MCSchedClassDesc *SC, unsigned PIdx,
1054                          unsigned Cycles, unsigned ReadyCycle,
1055                          unsigned StartAtCycle);
1056 
1057   void bumpNode(SUnit *SU);
1058 
1059   void releasePending();
1060 
1061   void removeReady(SUnit *SU);
1062 
1063   /// Call this before applying any other heuristics to the Available queue.
1064   /// Updates the Available/Pending Q's if necessary and returns the single
1065   /// available instruction, or NULL if there are multiple candidates.
1066   SUnit *pickOnlyChoice();
1067 
1068   /// Dump the state of the information that tracks resource usage.
1069   void dumpReservedCycles() const;
1070   void dumpScheduledState() const;
1071 };
1072 
1073 /// Base class for GenericScheduler. This class maintains information about
1074 /// scheduling candidates based on TargetSchedModel making it easy to implement
1075 /// heuristics for either preRA or postRA scheduling.
1076 class GenericSchedulerBase : public MachineSchedStrategy {
1077 public:
1078   /// Represent the type of SchedCandidate found within a single queue.
1079   /// pickNodeBidirectional depends on these listed by decreasing priority.
1080   enum CandReason : uint8_t {
1081     NoCand, Only1, PhysReg, RegExcess, RegCritical, Stall, Cluster, Weak,
1082     RegMax, ResourceReduce, ResourceDemand, BotHeightReduce, BotPathReduce,
1083     TopDepthReduce, TopPathReduce, NextDefUse, NodeOrder};
1084 
1085 #ifndef NDEBUG
1086   static const char *getReasonStr(GenericSchedulerBase::CandReason Reason);
1087 #endif
1088 
1089   /// Policy for scheduling the next instruction in the candidate's zone.
1090   struct CandPolicy {
1091     bool ReduceLatency = false;
1092     unsigned ReduceResIdx = 0;
1093     unsigned DemandResIdx = 0;
1094 
1095     CandPolicy() = default;
1096 
1097     bool operator==(const CandPolicy &RHS) const {
1098       return ReduceLatency == RHS.ReduceLatency &&
1099              ReduceResIdx == RHS.ReduceResIdx &&
1100              DemandResIdx == RHS.DemandResIdx;
1101     }
1102     bool operator!=(const CandPolicy &RHS) const {
1103       return !(*this == RHS);
1104     }
1105   };
1106 
1107   /// Status of an instruction's critical resource consumption.
1108   struct SchedResourceDelta {
1109     // Count critical resources in the scheduled region required by SU.
1110     unsigned CritResources = 0;
1111 
1112     // Count critical resources from another region consumed by SU.
1113     unsigned DemandedResources = 0;
1114 
1115     SchedResourceDelta() = default;
1116 
1117     bool operator==(const SchedResourceDelta &RHS) const {
1118       return CritResources == RHS.CritResources
1119         && DemandedResources == RHS.DemandedResources;
1120     }
1121     bool operator!=(const SchedResourceDelta &RHS) const {
1122       return !operator==(RHS);
1123     }
1124   };
1125 
1126   /// Store the state used by GenericScheduler heuristics, required for the
1127   /// lifetime of one invocation of pickNode().
1128   struct SchedCandidate {
1129     CandPolicy Policy;
1130 
1131     // The best SUnit candidate.
1132     SUnit *SU;
1133 
1134     // The reason for this candidate.
1135     CandReason Reason;
1136 
1137     // Whether this candidate should be scheduled at top/bottom.
1138     bool AtTop;
1139 
1140     // Register pressure values for the best candidate.
1141     RegPressureDelta RPDelta;
1142 
1143     // Critical resource consumption of the best candidate.
1144     SchedResourceDelta ResDelta;
1145 
1146     SchedCandidate() { reset(CandPolicy()); }
1147     SchedCandidate(const CandPolicy &Policy) { reset(Policy); }
1148 
1149     void reset(const CandPolicy &NewPolicy) {
1150       Policy = NewPolicy;
1151       SU = nullptr;
1152       Reason = NoCand;
1153       AtTop = false;
1154       RPDelta = RegPressureDelta();
1155       ResDelta = SchedResourceDelta();
1156     }
1157 
1158     bool isValid() const { return SU; }
1159 
1160     // Copy the status of another candidate without changing policy.
1161     void setBest(SchedCandidate &Best) {
1162       assert(Best.Reason != NoCand && "uninitialized Sched candidate");
1163       SU = Best.SU;
1164       Reason = Best.Reason;
1165       AtTop = Best.AtTop;
1166       RPDelta = Best.RPDelta;
1167       ResDelta = Best.ResDelta;
1168     }
1169 
1170     void initResourceDelta(const ScheduleDAGMI *DAG,
1171                            const TargetSchedModel *SchedModel);
1172   };
1173 
1174 protected:
1175   const MachineSchedContext *Context;
1176   const TargetSchedModel *SchedModel = nullptr;
1177   const TargetRegisterInfo *TRI = nullptr;
1178 
1179   MachineSchedPolicy RegionPolicy;
1180 
1181   SchedRemainder Rem;
1182 
1183   GenericSchedulerBase(const MachineSchedContext *C) : Context(C) {}
1184 
1185   void setPolicy(CandPolicy &Policy, bool IsPostRA, SchedBoundary &CurrZone,
1186                  SchedBoundary *OtherZone);
1187 
1188   MachineSchedPolicy getPolicy() const override { return RegionPolicy; }
1189 
1190 #ifndef NDEBUG
1191   void traceCandidate(const SchedCandidate &Cand);
1192 #endif
1193 
1194 private:
1195   bool shouldReduceLatency(const CandPolicy &Policy, SchedBoundary &CurrZone,
1196                            bool ComputeRemLatency, unsigned &RemLatency) const;
1197 };
1198 
1199 // Utility functions used by heuristics in tryCandidate().
1200 bool tryLess(int TryVal, int CandVal,
1201              GenericSchedulerBase::SchedCandidate &TryCand,
1202              GenericSchedulerBase::SchedCandidate &Cand,
1203              GenericSchedulerBase::CandReason Reason);
1204 bool tryGreater(int TryVal, int CandVal,
1205                 GenericSchedulerBase::SchedCandidate &TryCand,
1206                 GenericSchedulerBase::SchedCandidate &Cand,
1207                 GenericSchedulerBase::CandReason Reason);
1208 bool tryLatency(GenericSchedulerBase::SchedCandidate &TryCand,
1209                 GenericSchedulerBase::SchedCandidate &Cand,
1210                 SchedBoundary &Zone);
1211 bool tryPressure(const PressureChange &TryP,
1212                  const PressureChange &CandP,
1213                  GenericSchedulerBase::SchedCandidate &TryCand,
1214                  GenericSchedulerBase::SchedCandidate &Cand,
1215                  GenericSchedulerBase::CandReason Reason,
1216                  const TargetRegisterInfo *TRI,
1217                  const MachineFunction &MF);
1218 unsigned getWeakLeft(const SUnit *SU, bool isTop);
1219 int biasPhysReg(const SUnit *SU, bool isTop);
1220 
1221 /// GenericScheduler shrinks the unscheduled zone using heuristics to balance
1222 /// the schedule.
1223 class GenericScheduler : public GenericSchedulerBase {
1224 public:
1225   GenericScheduler(const MachineSchedContext *C):
1226     GenericSchedulerBase(C), Top(SchedBoundary::TopQID, "TopQ"),
1227     Bot(SchedBoundary::BotQID, "BotQ") {}
1228 
1229   void initPolicy(MachineBasicBlock::iterator Begin,
1230                   MachineBasicBlock::iterator End,
1231                   unsigned NumRegionInstrs) override;
1232 
1233   void dumpPolicy() const override;
1234 
1235   bool shouldTrackPressure() const override {
1236     return RegionPolicy.ShouldTrackPressure;
1237   }
1238 
1239   bool shouldTrackLaneMasks() const override {
1240     return RegionPolicy.ShouldTrackLaneMasks;
1241   }
1242 
1243   void initialize(ScheduleDAGMI *dag) override;
1244 
1245   SUnit *pickNode(bool &IsTopNode) override;
1246 
1247   void schedNode(SUnit *SU, bool IsTopNode) override;
1248 
1249   void releaseTopNode(SUnit *SU) override {
1250     if (SU->isScheduled)
1251       return;
1252 
1253     Top.releaseNode(SU, SU->TopReadyCycle, false);
1254     TopCand.SU = nullptr;
1255   }
1256 
1257   void releaseBottomNode(SUnit *SU) override {
1258     if (SU->isScheduled)
1259       return;
1260 
1261     Bot.releaseNode(SU, SU->BotReadyCycle, false);
1262     BotCand.SU = nullptr;
1263   }
1264 
1265   void registerRoots() override;
1266 
1267 protected:
1268   ScheduleDAGMILive *DAG = nullptr;
1269 
1270   // State of the top and bottom scheduled instruction boundaries.
1271   SchedBoundary Top;
1272   SchedBoundary Bot;
1273 
1274   /// Candidate last picked from Top boundary.
1275   SchedCandidate TopCand;
1276   /// Candidate last picked from Bot boundary.
1277   SchedCandidate BotCand;
1278 
1279   void checkAcyclicLatency();
1280 
1281   void initCandidate(SchedCandidate &Cand, SUnit *SU, bool AtTop,
1282                      const RegPressureTracker &RPTracker,
1283                      RegPressureTracker &TempTracker);
1284 
1285   virtual bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand,
1286                             SchedBoundary *Zone) const;
1287 
1288   SUnit *pickNodeBidirectional(bool &IsTopNode);
1289 
1290   void pickNodeFromQueue(SchedBoundary &Zone,
1291                          const CandPolicy &ZonePolicy,
1292                          const RegPressureTracker &RPTracker,
1293                          SchedCandidate &Candidate);
1294 
1295   void reschedulePhysReg(SUnit *SU, bool isTop);
1296 };
1297 
1298 /// PostGenericScheduler - Interface to the scheduling algorithm used by
1299 /// ScheduleDAGMI.
1300 ///
1301 /// Callbacks from ScheduleDAGMI:
1302 ///   initPolicy -> initialize(DAG) -> registerRoots -> pickNode ...
1303 class PostGenericScheduler : public GenericSchedulerBase {
1304 protected:
1305   ScheduleDAGMI *DAG = nullptr;
1306   SchedBoundary Top;
1307   SchedBoundary Bot;
1308 
1309   /// Candidate last picked from Top boundary.
1310   SchedCandidate TopCand;
1311   /// Candidate last picked from Bot boundary.
1312   SchedCandidate BotCand;
1313 
1314 public:
1315   PostGenericScheduler(const MachineSchedContext *C)
1316       : GenericSchedulerBase(C), Top(SchedBoundary::TopQID, "TopQ"),
1317         Bot(SchedBoundary::BotQID, "BotQ") {}
1318 
1319   ~PostGenericScheduler() override = default;
1320 
1321   void initPolicy(MachineBasicBlock::iterator Begin,
1322                   MachineBasicBlock::iterator End,
1323                   unsigned NumRegionInstrs) override;
1324 
1325   /// PostRA scheduling does not track pressure.
1326   bool shouldTrackPressure() const override { return false; }
1327 
1328   void initialize(ScheduleDAGMI *Dag) override;
1329 
1330   void registerRoots() override;
1331 
1332   SUnit *pickNode(bool &IsTopNode) override;
1333 
1334   SUnit *pickNodeBidirectional(bool &IsTopNode);
1335 
1336   void scheduleTree(unsigned SubtreeID) override {
1337     llvm_unreachable("PostRA scheduler does not support subtree analysis.");
1338   }
1339 
1340   void schedNode(SUnit *SU, bool IsTopNode) override;
1341 
1342   void releaseTopNode(SUnit *SU) override {
1343     if (SU->isScheduled)
1344       return;
1345     Top.releaseNode(SU, SU->TopReadyCycle, false);
1346     TopCand.SU = nullptr;
1347   }
1348 
1349   void releaseBottomNode(SUnit *SU) override {
1350     if (SU->isScheduled)
1351       return;
1352     Bot.releaseNode(SU, SU->BotReadyCycle, false);
1353     BotCand.SU = nullptr;
1354   }
1355 
1356 protected:
1357   virtual bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand);
1358 
1359   void pickNodeFromQueue(SchedBoundary &Zone, SchedCandidate &Cand);
1360 };
1361 
1362 /// Create the standard converging machine scheduler. This will be used as the
1363 /// default scheduler if the target does not set a default.
1364 /// Adds default DAG mutations.
1365 ScheduleDAGMILive *createGenericSchedLive(MachineSchedContext *C);
1366 
1367 /// Create a generic scheduler with no vreg liveness or DAG mutation passes.
1368 ScheduleDAGMI *createGenericSchedPostRA(MachineSchedContext *C);
1369 
1370 /// If ReorderWhileClustering is set to true, no attempt will be made to
1371 /// reduce reordering due to store clustering.
1372 std::unique_ptr<ScheduleDAGMutation>
1373 createLoadClusterDAGMutation(const TargetInstrInfo *TII,
1374                              const TargetRegisterInfo *TRI,
1375                              bool ReorderWhileClustering = false);
1376 
1377 /// If ReorderWhileClustering is set to true, no attempt will be made to
1378 /// reduce reordering due to store clustering.
1379 std::unique_ptr<ScheduleDAGMutation>
1380 createStoreClusterDAGMutation(const TargetInstrInfo *TII,
1381                               const TargetRegisterInfo *TRI,
1382                               bool ReorderWhileClustering = false);
1383 
1384 std::unique_ptr<ScheduleDAGMutation>
1385 createCopyConstrainDAGMutation(const TargetInstrInfo *TII,
1386                                const TargetRegisterInfo *TRI);
1387 
1388 } // end namespace llvm
1389 
1390 #endif // LLVM_CODEGEN_MACHINESCHEDULER_H