Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

382 lines
13 KiB

  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 a MachineSchedRegistry for registering alternative machine
  11. // schedulers. A Target may provide an alternative scheduler implementation by
  12. // implementing the following boilerplate:
  13. //
  14. // static ScheduleDAGInstrs *createCustomMachineSched(MachineSchedContext *C) {
  15. // return new CustomMachineScheduler(C);
  16. // }
  17. // static MachineSchedRegistry
  18. // SchedCustomRegistry("custom", "Run my target's custom scheduler",
  19. // createCustomMachineSched);
  20. //
  21. // Inside <Target>PassConfig:
  22. // enablePass(&MachineSchedulerID);
  23. // MachineSchedRegistry::setDefault(createCustomMachineSched);
  24. //
  25. //===----------------------------------------------------------------------===//
  26. #ifndef LLVM_CODEGEN_MACHINESCHEDULER_H
  27. #define LLVM_CODEGEN_MACHINESCHEDULER_H
  28. #include "llvm/CodeGen/MachinePassRegistry.h"
  29. #include "llvm/CodeGen/RegisterPressure.h"
  30. #include "llvm/CodeGen/ScheduleDAGInstrs.h"
  31. #include "llvm/Target/TargetInstrInfo.h"
  32. namespace llvm {
  33. extern cl::opt<bool> ForceTopDown;
  34. extern cl::opt<bool> ForceBottomUp;
  35. class AliasAnalysis;
  36. class LiveIntervals;
  37. class MachineDominatorTree;
  38. class MachineLoopInfo;
  39. class RegisterClassInfo;
  40. class ScheduleDAGInstrs;
  41. class SchedDFSResult;
  42. /// MachineSchedContext provides enough context from the MachineScheduler pass
  43. /// for the target to instantiate a scheduler.
  44. struct MachineSchedContext {
  45. MachineFunction *MF;
  46. const MachineLoopInfo *MLI;
  47. const MachineDominatorTree *MDT;
  48. const TargetPassConfig *PassConfig;
  49. AliasAnalysis *AA;
  50. LiveIntervals *LIS;
  51. RegisterClassInfo *RegClassInfo;
  52. MachineSchedContext();
  53. virtual ~MachineSchedContext();
  54. };
  55. /// MachineSchedRegistry provides a selection of available machine instruction
  56. /// schedulers.
  57. class MachineSchedRegistry : public MachinePassRegistryNode {
  58. public:
  59. typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineSchedContext *);
  60. // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
  61. typedef ScheduleDAGCtor FunctionPassCtor;
  62. static MachinePassRegistry Registry;
  63. MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
  64. : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
  65. Registry.Add(this);
  66. }
  67. ~MachineSchedRegistry() { Registry.Remove(this); }
  68. // Accessors.
  69. //
  70. MachineSchedRegistry *getNext() const {
  71. return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
  72. }
  73. static MachineSchedRegistry *getList() {
  74. return (MachineSchedRegistry *)Registry.getList();
  75. }
  76. static ScheduleDAGCtor getDefault() {
  77. return (ScheduleDAGCtor)Registry.getDefault();
  78. }
  79. static void setDefault(ScheduleDAGCtor C) {
  80. Registry.setDefault((MachinePassCtor)C);
  81. }
  82. static void setDefault(StringRef Name) {
  83. Registry.setDefault(Name);
  84. }
  85. static void setListener(MachinePassRegistryListener *L) {
  86. Registry.setListener(L);
  87. }
  88. };
  89. class ScheduleDAGMI;
  90. /// MachineSchedStrategy - Interface to the scheduling algorithm used by
  91. /// ScheduleDAGMI.
  92. class MachineSchedStrategy {
  93. public:
  94. virtual ~MachineSchedStrategy() {}
  95. /// Initialize the strategy after building the DAG for a new region.
  96. virtual void initialize(ScheduleDAGMI *DAG) = 0;
  97. /// Notify this strategy that all roots have been released (including those
  98. /// that depend on EntrySU or ExitSU).
  99. virtual void registerRoots() {}
  100. /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to
  101. /// schedule the node at the top of the unscheduled region. Otherwise it will
  102. /// be scheduled at the bottom.
  103. virtual SUnit *pickNode(bool &IsTopNode) = 0;
  104. /// \brief Scheduler callback to notify that a new subtree is scheduled.
  105. virtual void scheduleTree(unsigned SubtreeID) {}
  106. /// Notify MachineSchedStrategy that ScheduleDAGMI has scheduled an
  107. /// instruction and updated scheduled/remaining flags in the DAG nodes.
  108. virtual void schedNode(SUnit *SU, bool IsTopNode) = 0;
  109. /// When all predecessor dependencies have been resolved, free this node for
  110. /// top-down scheduling.
  111. virtual void releaseTopNode(SUnit *SU) = 0;
  112. /// When all successor dependencies have been resolved, free this node for
  113. /// bottom-up scheduling.
  114. virtual void releaseBottomNode(SUnit *SU) = 0;
  115. };
  116. /// ReadyQueue encapsulates vector of "ready" SUnits with basic convenience
  117. /// methods for pushing and removing nodes. ReadyQueue's are uniquely identified
  118. /// by an ID. SUnit::NodeQueueId is a mask of the ReadyQueues the SUnit is in.
  119. ///
  120. /// This is a convenience class that may be used by implementations of
  121. /// MachineSchedStrategy.
  122. class ReadyQueue {
  123. unsigned ID;
  124. std::string Name;
  125. std::vector<SUnit*> Queue;
  126. public:
  127. ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {}
  128. unsigned getID() const { return ID; }
  129. StringRef getName() const { return Name; }
  130. // SU is in this queue if it's NodeQueueID is a superset of this ID.
  131. bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); }
  132. bool empty() const { return Queue.empty(); }
  133. void clear() { Queue.clear(); }
  134. unsigned size() const { return Queue.size(); }
  135. typedef std::vector<SUnit*>::iterator iterator;
  136. iterator begin() { return Queue.begin(); }
  137. iterator end() { return Queue.end(); }
  138. ArrayRef<SUnit*> elements() { return Queue; }
  139. iterator find(SUnit *SU) {
  140. return std::find(Queue.begin(), Queue.end(), SU);
  141. }
  142. void push(SUnit *SU) {
  143. Queue.push_back(SU);
  144. SU->NodeQueueId |= ID;
  145. }
  146. iterator remove(iterator I) {
  147. (*I)->NodeQueueId &= ~ID;
  148. *I = Queue.back();
  149. unsigned idx = I - Queue.begin();
  150. Queue.pop_back();
  151. return Queue.begin() + idx;
  152. }
  153. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  154. void dump();
  155. #endif
  156. };
  157. /// Mutate the DAG as a postpass after normal DAG building.
  158. class ScheduleDAGMutation {
  159. public:
  160. virtual ~ScheduleDAGMutation() {}
  161. virtual void apply(ScheduleDAGMI *DAG) = 0;
  162. };
  163. /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that schedules
  164. /// machine instructions while updating LiveIntervals and tracking regpressure.
  165. class ScheduleDAGMI : public ScheduleDAGInstrs {
  166. protected:
  167. AliasAnalysis *AA;
  168. RegisterClassInfo *RegClassInfo;
  169. MachineSchedStrategy *SchedImpl;
  170. /// Information about DAG subtrees. If DFSResult is NULL, then SchedulerTrees
  171. /// will be empty.
  172. SchedDFSResult *DFSResult;
  173. BitVector ScheduledTrees;
  174. /// Topo - A topological ordering for SUnits which permits fast IsReachable
  175. /// and similar queries.
  176. ScheduleDAGTopologicalSort Topo;
  177. /// Ordered list of DAG postprocessing steps.
  178. std::vector<ScheduleDAGMutation*> Mutations;
  179. MachineBasicBlock::iterator LiveRegionEnd;
  180. /// Register pressure in this region computed by buildSchedGraph.
  181. IntervalPressure RegPressure;
  182. RegPressureTracker RPTracker;
  183. /// List of pressure sets that exceed the target's pressure limit before
  184. /// scheduling, listed in increasing set ID order. Each pressure set is paired
  185. /// with its max pressure in the currently scheduled regions.
  186. std::vector<PressureElement> RegionCriticalPSets;
  187. /// The top of the unscheduled zone.
  188. MachineBasicBlock::iterator CurrentTop;
  189. IntervalPressure TopPressure;
  190. RegPressureTracker TopRPTracker;
  191. /// The bottom of the unscheduled zone.
  192. MachineBasicBlock::iterator CurrentBottom;
  193. IntervalPressure BotPressure;
  194. RegPressureTracker BotRPTracker;
  195. /// Record the next node in a scheduled cluster.
  196. const SUnit *NextClusterPred;
  197. const SUnit *NextClusterSucc;
  198. #ifndef NDEBUG
  199. /// The number of instructions scheduled so far. Used to cut off the
  200. /// scheduler at the point determined by misched-cutoff.
  201. unsigned NumInstrsScheduled;
  202. #endif
  203. public:
  204. ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S):
  205. ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
  206. AA(C->AA), RegClassInfo(C->RegClassInfo), SchedImpl(S), DFSResult(0),
  207. Topo(SUnits, &ExitSU), RPTracker(RegPressure), CurrentTop(),
  208. TopRPTracker(TopPressure), CurrentBottom(), BotRPTracker(BotPressure),
  209. NextClusterPred(NULL), NextClusterSucc(NULL) {
  210. #ifndef NDEBUG
  211. NumInstrsScheduled = 0;
  212. #endif
  213. }
  214. virtual ~ScheduleDAGMI();
  215. /// Add a postprocessing step to the DAG builder.
  216. /// Mutations are applied in the order that they are added after normal DAG
  217. /// building and before MachineSchedStrategy initialization.
  218. ///
  219. /// ScheduleDAGMI takes ownership of the Mutation object.
  220. void addMutation(ScheduleDAGMutation *Mutation) {
  221. Mutations.push_back(Mutation);
  222. }
  223. /// \brief Add a DAG edge to the given SU with the given predecessor
  224. /// dependence data.
  225. ///
  226. /// \returns true if the edge may be added without creating a cycle OR if an
  227. /// equivalent edge already existed (false indicates failure).
  228. bool addEdge(SUnit *SuccSU, const SDep &PredDep);
  229. MachineBasicBlock::iterator top() const { return CurrentTop; }
  230. MachineBasicBlock::iterator bottom() const { return CurrentBottom; }
  231. /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
  232. /// region. This covers all instructions in a block, while schedule() may only
  233. /// cover a subset.
  234. void enterRegion(MachineBasicBlock *bb,
  235. MachineBasicBlock::iterator begin,
  236. MachineBasicBlock::iterator end,
  237. unsigned endcount);
  238. /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
  239. /// reorderable instructions.
  240. virtual void schedule();
  241. /// Change the position of an instruction within the basic block and update
  242. /// live ranges and region boundary iterators.
  243. void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos);
  244. /// Get current register pressure for the top scheduled instructions.
  245. const IntervalPressure &getTopPressure() const { return TopPressure; }
  246. const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; }
  247. /// Get current register pressure for the bottom scheduled instructions.
  248. const IntervalPressure &getBotPressure() const { return BotPressure; }
  249. const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; }
  250. /// Get register pressure for the entire scheduling region before scheduling.
  251. const IntervalPressure &getRegPressure() const { return RegPressure; }
  252. const std::vector<PressureElement> &getRegionCriticalPSets() const {
  253. return RegionCriticalPSets;
  254. }
  255. const SUnit *getNextClusterPred() const { return NextClusterPred; }
  256. const SUnit *getNextClusterSucc() const { return NextClusterSucc; }
  257. /// Compute a DFSResult after DAG building is complete, and before any
  258. /// queue comparisons.
  259. void computeDFSResult();
  260. /// Return a non-null DFS result if the scheduling strategy initialized it.
  261. const SchedDFSResult *getDFSResult() const { return DFSResult; }
  262. BitVector &getScheduledTrees() { return ScheduledTrees; }
  263. void viewGraph(const Twine &Name, const Twine &Title) LLVM_OVERRIDE;
  264. void viewGraph() LLVM_OVERRIDE;
  265. protected:
  266. // Top-Level entry points for the schedule() driver...
  267. /// Call ScheduleDAGInstrs::buildSchedGraph with register pressure tracking
  268. /// enabled. This sets up three trackers. RPTracker will cover the entire DAG
  269. /// region, TopTracker and BottomTracker will be initialized to the top and
  270. /// bottom of the DAG region without covereing any unscheduled instruction.
  271. void buildDAGWithRegPressure();
  272. /// Apply each ScheduleDAGMutation step in order. This allows different
  273. /// instances of ScheduleDAGMI to perform custom DAG postprocessing.
  274. void postprocessDAG();
  275. /// Release ExitSU predecessors and setup scheduler queues.
  276. void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots);
  277. /// Move an instruction and update register pressure.
  278. void scheduleMI(SUnit *SU, bool IsTopNode);
  279. /// Update scheduler DAG and queues after scheduling an instruction.
  280. void updateQueues(SUnit *SU, bool IsTopNode);
  281. /// Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
  282. void placeDebugValues();
  283. /// \brief dump the scheduled Sequence.
  284. void dumpSchedule() const;
  285. // Lesser helpers...
  286. void initRegPressure();
  287. void updateScheduledPressure(const std::vector<unsigned> &NewMaxPressure);
  288. bool checkSchedLimit();
  289. void findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots,
  290. SmallVectorImpl<SUnit*> &BotRoots);
  291. void releaseSucc(SUnit *SU, SDep *SuccEdge);
  292. void releaseSuccessors(SUnit *SU);
  293. void releasePred(SUnit *SU, SDep *PredEdge);
  294. void releasePredecessors(SUnit *SU);
  295. };
  296. } // namespace llvm
  297. #endif