MachineScheduler.h 31 KB

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