MachineScheduler.h 38 KB

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