LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MachineScheduler.h
Go to the documentation of this file.
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, ScheduleDAGMI, builds the DAG and drives list
27 // scheduling while updating the instruction stream, register pressure, and live
28 // intervals. Most targets don't need to override the DAG builder and list
29 // schedulier, but subtargets that require custom scheduling heuristics may
30 // plugin an alternate MachineSchedStrategy. The strategy is responsible for
31 // selecting the highest priority node from the list:
32 //
33 // ScheduleDAGInstrs *<Target>PassConfig::
34 // createMachineScheduler(MachineSchedContext *C) {
35 // return new ScheduleDAGMI(C, CustomStrategy(C));
36 // }
37 //
38 // The DAG builder can also be customized in a sense by adding DAG mutations
39 // that will run after DAG building and before list scheduling. DAG mutations
40 // can adjust dependencies based on target-specific knowledge or add weak edges
41 // to aid heuristics:
42 //
43 // ScheduleDAGInstrs *<Target>PassConfig::
44 // createMachineScheduler(MachineSchedContext *C) {
45 // ScheduleDAGMI *DAG = new ScheduleDAGMI(C, CustomStrategy(C));
46 // DAG->addMutation(new CustomDependencies(DAG->TII, DAG->TRI));
47 // return DAG;
48 // }
49 //
50 // A target that supports alternative schedulers can use the
51 // MachineSchedRegistry to allow command line selection. This can be done by
52 // implementing the following boilerplate:
53 //
54 // static ScheduleDAGInstrs *createCustomMachineSched(MachineSchedContext *C) {
55 // return new CustomMachineScheduler(C);
56 // }
57 // static MachineSchedRegistry
58 // SchedCustomRegistry("custom", "Run my target's custom scheduler",
59 // createCustomMachineSched);
60 //
61 //
62 // Finally, subtargets that don't need to implement custom heuristics but would
63 // like to configure the GenericScheduler's policy for a given scheduler region,
64 // including scheduling direction and register pressure tracking policy, can do
65 // this:
66 //
67 // void <SubTarget>Subtarget::
68 // overrideSchedPolicy(MachineSchedPolicy &Policy,
69 // MachineInstr *begin,
70 // MachineInstr *end,
71 // unsigned NumRegionInstrs) const {
72 // Policy.<Flag> = true;
73 // }
74 //
75 //===----------------------------------------------------------------------===//
76 
77 #ifndef LLVM_CODEGEN_MACHINESCHEDULER_H
78 #define LLVM_CODEGEN_MACHINESCHEDULER_H
79 
83 
84 namespace llvm {
85 
86 extern cl::opt<bool> ForceTopDown;
87 extern cl::opt<bool> ForceBottomUp;
88 
89 class AliasAnalysis;
90 class LiveIntervals;
91 class MachineDominatorTree;
92 class MachineLoopInfo;
93 class RegisterClassInfo;
94 class ScheduleDAGInstrs;
95 class SchedDFSResult;
96 
97 /// MachineSchedContext provides enough context from the MachineScheduler pass
98 /// for the target to instantiate a scheduler.
106 
108 
110  virtual ~MachineSchedContext();
111 };
112 
113 /// MachineSchedRegistry provides a selection of available machine instruction
114 /// schedulers.
116 public:
117  typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineSchedContext *);
118 
119  // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
121 
123 
124  MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
126  Registry.Add(this);
127  }
128  ~MachineSchedRegistry() { Registry.Remove(this); }
129 
130  // Accessors.
131  //
134  }
136  return (MachineSchedRegistry *)Registry.getList();
137  }
139  Registry.setListener(L);
140  }
141 };
142 
143 class ScheduleDAGMI;
144 
145 /// Define a generic scheduling policy for targets that don't provide their own
146 /// MachineSchedStrategy. This can be overriden for each scheduling region
147 /// before building the DAG.
149  // Allow the scheduler to disable register pressure tracking.
151 
152  // Allow the scheduler to force top-down or bottom-up scheduling. If neither
153  // is true, the scheduler runs in both directions and converges.
156 
159 };
160 
161 /// MachineSchedStrategy - Interface to the scheduling algorithm used by
162 /// ScheduleDAGMI.
163 ///
164 /// Initialization sequence:
165 /// initPolicy -> shouldTrackPressure -> initialize(DAG) -> registerRoots
167  virtual void anchor();
168 public:
170 
171  /// Optionally override the per-region scheduling policy.
174  unsigned NumRegionInstrs) {}
175 
176  /// Check if pressure tracking is needed before building the DAG and
177  /// initializing this strategy. Called after initPolicy.
178  virtual bool shouldTrackPressure() const { return true; }
179 
180  /// Initialize the strategy after building the DAG for a new region.
181  virtual void initialize(ScheduleDAGMI *DAG) = 0;
182 
183  /// Notify this strategy that all roots have been released (including those
184  /// that depend on EntrySU or ExitSU).
185  virtual void registerRoots() {}
186 
187  /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to
188  /// schedule the node at the top of the unscheduled region. Otherwise it will
189  /// be scheduled at the bottom.
190  virtual SUnit *pickNode(bool &IsTopNode) = 0;
191 
192  /// \brief Scheduler callback to notify that a new subtree is scheduled.
193  virtual void scheduleTree(unsigned SubtreeID) {}
194 
195  /// Notify MachineSchedStrategy that ScheduleDAGMI has scheduled an
196  /// instruction and updated scheduled/remaining flags in the DAG nodes.
197  virtual void schedNode(SUnit *SU, bool IsTopNode) = 0;
198 
199  /// When all predecessor dependencies have been resolved, free this node for
200  /// top-down scheduling.
201  virtual void releaseTopNode(SUnit *SU) = 0;
202  /// When all successor dependencies have been resolved, free this node for
203  /// bottom-up scheduling.
204  virtual void releaseBottomNode(SUnit *SU) = 0;
205 };
206 
207 /// ReadyQueue encapsulates vector of "ready" SUnits with basic convenience
208 /// methods for pushing and removing nodes. ReadyQueue's are uniquely identified
209 /// by an ID. SUnit::NodeQueueId is a mask of the ReadyQueues the SUnit is in.
210 ///
211 /// This is a convenience class that may be used by implementations of
212 /// MachineSchedStrategy.
213 class ReadyQueue {
214  unsigned ID;
215  std::string Name;
216  std::vector<SUnit*> Queue;
217 
218 public:
219  ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {}
220 
221  unsigned getID() const { return ID; }
222 
223  StringRef getName() const { return Name; }
224 
225  // SU is in this queue if it's NodeQueueID is a superset of this ID.
226  bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); }
227 
228  bool empty() const { return Queue.empty(); }
229 
230  void clear() { Queue.clear(); }
231 
232  unsigned size() const { return Queue.size(); }
233 
234  typedef std::vector<SUnit*>::iterator iterator;
235 
236  iterator begin() { return Queue.begin(); }
237 
238  iterator end() { return Queue.end(); }
239 
240  ArrayRef<SUnit*> elements() { return Queue; }
241 
243  return std::find(Queue.begin(), Queue.end(), SU);
244  }
245 
246  void push(SUnit *SU) {
247  Queue.push_back(SU);
248  SU->NodeQueueId |= ID;
249  }
250 
251  iterator remove(iterator I) {
252  (*I)->NodeQueueId &= ~ID;
253  *I = Queue.back();
254  unsigned idx = I - Queue.begin();
255  Queue.pop_back();
256  return Queue.begin() + idx;
257  }
258 
259 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
260  void dump();
261 #endif
262 };
263 
264 /// Mutate the DAG as a postpass after normal DAG building.
266  virtual void anchor();
267 public:
268  virtual ~ScheduleDAGMutation() {}
269 
270  virtual void apply(ScheduleDAGMI *DAG) = 0;
271 };
272 
273 /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that schedules
274 /// machine instructions while updating LiveIntervals and tracking regpressure.
276 protected:
280 
281  /// Information about DAG subtrees. If DFSResult is NULL, then SchedulerTrees
282  /// will be empty.
285 
286  /// Topo - A topological ordering for SUnits which permits fast IsReachable
287  /// and similar queries.
289 
290  /// Ordered list of DAG postprocessing steps.
291  std::vector<ScheduleDAGMutation*> Mutations;
292 
294 
295  // Map each SU to its summary of pressure changes. This array is updated for
296  // liveness during bottom-up scheduling. Top-down scheduling may proceed but
297  // has no affect on the pressure diffs.
299 
300  /// Register pressure in this region computed by initRegPressure.
304 
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 
310  /// The top of the unscheduled zone.
314 
315  /// The bottom of the unscheduled zone.
319 
320  /// Record the next node in a scheduled cluster.
323 
324 #ifndef NDEBUG
325  /// The number of instructions scheduled so far. Used to cut off the
326  /// scheduler at the point determined by misched-cutoff.
328 #endif
329 
330 public:
332  ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
337  NextClusterPred(NULL), NextClusterSucc(NULL) {
338 #ifndef NDEBUG
339  NumInstrsScheduled = 0;
340 #endif
341  }
342 
343  virtual ~ScheduleDAGMI();
344 
345  /// \brief Return true if register pressure tracking is enabled.
346  bool isTrackingPressure() const { return ShouldTrackPressure; }
347 
348  /// Add a postprocessing step to the DAG builder.
349  /// Mutations are applied in the order that they are added after normal DAG
350  /// building and before MachineSchedStrategy initialization.
351  ///
352  /// ScheduleDAGMI takes ownership of the Mutation object.
354  Mutations.push_back(Mutation);
355  }
356 
357  /// \brief True if an edge can be added from PredSU to SuccSU without creating
358  /// a cycle.
359  bool canAddEdge(SUnit *SuccSU, SUnit *PredSU);
360 
361  /// \brief Add a DAG edge to the given SU with the given predecessor
362  /// dependence data.
363  ///
364  /// \returns true if the edge may be added without creating a cycle OR if an
365  /// equivalent edge already existed (false indicates failure).
366  bool addEdge(SUnit *SuccSU, const SDep &PredDep);
367 
370 
371  /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
372  /// region. This covers all instructions in a block, while schedule() may only
373  /// cover a subset.
377  unsigned regioninstrs) LLVM_OVERRIDE;
378 
379  /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
380  /// reorderable instructions.
381  virtual void schedule();
382 
383  /// Change the position of an instruction within the basic block and update
384  /// live ranges and region boundary iterators.
386 
387  /// Get current register pressure for the top scheduled instructions.
388  const IntervalPressure &getTopPressure() const { return TopPressure; }
390 
391  /// Get current register pressure for the bottom scheduled instructions.
392  const IntervalPressure &getBotPressure() const { return BotPressure; }
394 
395  /// Get register pressure for the entire scheduling region before scheduling.
396  const IntervalPressure &getRegPressure() const { return RegPressure; }
397 
398  const std::vector<PressureChange> &getRegionCriticalPSets() const {
399  return RegionCriticalPSets;
400  }
401 
403  return SUPressureDiffs[SU->NodeNum];
404  }
405 
406  const SUnit *getNextClusterPred() const { return NextClusterPred; }
407 
408  const SUnit *getNextClusterSucc() const { return NextClusterSucc; }
409 
410  /// Compute a DFSResult after DAG building is complete, and before any
411  /// queue comparisons.
412  void computeDFSResult();
413 
414  /// Return a non-null DFS result if the scheduling strategy initialized it.
415  const SchedDFSResult *getDFSResult() const { return DFSResult; }
416 
418 
419  /// Compute the cyclic critical path through the DAG.
420  unsigned computeCyclicCriticalPath();
421 
422  void viewGraph(const Twine &Name, const Twine &Title) LLVM_OVERRIDE;
423  void viewGraph() LLVM_OVERRIDE;
424 
425 protected:
426  // Top-Level entry points for the schedule() driver...
427 
428  /// Call ScheduleDAGInstrs::buildSchedGraph with register pressure tracking
429  /// enabled. This sets up three trackers. RPTracker will cover the entire DAG
430  /// region, TopTracker and BottomTracker will be initialized to the top and
431  /// bottom of the DAG region without covereing any unscheduled instruction.
433 
434  /// Apply each ScheduleDAGMutation step in order. This allows different
435  /// instances of ScheduleDAGMI to perform custom DAG postprocessing.
436  void postprocessDAG();
437 
438  /// Release ExitSU predecessors and setup scheduler queues.
439  void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots);
440 
441  /// Move an instruction and update register pressure.
442  void scheduleMI(SUnit *SU, bool IsTopNode);
443 
444  /// Update scheduler DAG and queues after scheduling an instruction.
445  void updateQueues(SUnit *SU, bool IsTopNode);
446 
447  /// Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
448  void placeDebugValues();
449 
450  /// \brief dump the scheduled Sequence.
451  void dumpSchedule() const;
452 
453  // Lesser helpers...
454 
455  void initRegPressure();
456 
457  void updatePressureDiffs(ArrayRef<unsigned> LiveUses);
458 
459  void updateScheduledPressure(const SUnit *SU,
460  const std::vector<unsigned> &NewMaxPressure);
461 
462  bool checkSchedLimit();
463 
465  SmallVectorImpl<SUnit*> &BotRoots);
466 
467  void releaseSucc(SUnit *SU, SDep *SuccEdge);
468  void releaseSuccessors(SUnit *SU);
469  void releasePred(SUnit *SU, SDep *PredEdge);
470  void releasePredecessors(SUnit *SU);
471 };
472 
473 } // namespace llvm
474 
475 #endif
void releaseSucc(SUnit *SU, SDep *SuccEdge)
IntervalPressure TopPressure
virtual void initialize(ScheduleDAGMI *DAG)=0
Initialize the strategy after building the DAG for a new region.
const IntervalPressure & getTopPressure() const
Get current register pressure for the top scheduled instructions.
void addMutation(ScheduleDAGMutation *Mutation)
virtual void releaseTopNode(SUnit *SU)=0
bool isInQueue(SUnit *SU) const
ScheduleDAGTopologicalSort Topo
MachineBasicBlock::iterator CurrentTop
The top of the unscheduled zone.
const MachineLoopInfo * MLI
void buildDAGWithRegPressure()
Build the DAG and setup three register pressure trackers.
void *(* MachinePassCtor)()
MachineBasicBlock::iterator begin() const
begin - Return an iterator to the top of the current scheduling region.
MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
Mutate the DAG as a postpass after normal DAG building.
IntervalPressure BotPressure
unsigned getID() const
static void setListener(MachinePassRegistryListener *L)
virtual bool shouldTrackPressure() const
MachineBasicBlock::iterator top() const
MachineSchedRegistry * getNext() const
void updateQueues(SUnit *SU, bool IsTopNode)
Update scheduler DAG and queues after scheduling an instruction.
virtual void schedNode(SUnit *SU, bool IsTopNode)=0
const RegPressureTracker & getTopRPTracker() const
MachineFunction & MF
Definition: ScheduleDAG.h:543
ArrayRef< SUnit * > elements()
void viewGraph() LLVM_OVERRIDE
Out-of-line implementation with no arguments is handy for gdb.
BitVector & getScheduledTrees()
const TargetPassConfig * PassConfig
IntervalPressure RegPressure
void enterRegion(MachineBasicBlock *bb, MachineBasicBlock::iterator begin, MachineBasicBlock::iterator end, unsigned regioninstrs) LLVM_OVERRIDE
#define false
Definition: ConvertUTF.c:64
ScheduleDAGCtor FunctionPassCtor
static MachineSchedRegistry * getList()
Compute the values of each DAG node for various metrics during DFS.
Definition: ScheduleDFS.h:68
iterator find(SUnit *SU)
const MachineLoopInfo & MLI
virtual void initPolicy(MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned NumRegionInstrs)
Optionally override the per-region scheduling policy.
bool isTrackingPressure() const
Return true if register pressure tracking is enabled.
std::vector< SUnit * >::iterator iterator
MachineSchedStrategy * SchedImpl
RegisterClassInfo * RegClassInfo
bundle_iterator< MachineInstr, instr_iterator > iterator
std::vector< ScheduleDAGMutation * > Mutations
Ordered list of DAG postprocessing steps.
Array of PressureDiffs.
void updatePressureDiffs(ArrayRef< unsigned > LiveUses)
void dumpSchedule() const
dump the scheduled Sequence.
MachinePassRegistryNode * getNext() const
PressureDiff & getPressureDiff(const SUnit *SU)
bool addEdge(SUnit *SuccSU, const SDep &PredDep)
Add a DAG edge to the given SU with the given predecessor dependence data.
static MachinePassRegistry Registry
bool empty() const
void releasePred(SUnit *SU, SDep *PredEdge)
virtual void releaseBottomNode(SUnit *SU)=0
void findRootsAndBiasEdges(SmallVectorImpl< SUnit * > &TopRoots, SmallVectorImpl< SUnit * > &BotRoots)
void updateScheduledPressure(const SUnit *SU, const std::vector< unsigned > &NewMaxPressure)
ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S)
void releaseSuccessors(SUnit *SU)
releaseSuccessors - Call releaseSucc on each of SU's successors.
const SUnit * NextClusterSucc
void scheduleMI(SUnit *SU, bool IsTopNode)
Move an instruction and update register pressure.
MachineBasicBlock::iterator LiveRegionEnd
const MachineDominatorTree & MDT
const SUnit * NextClusterPred
Record the next node in a scheduled cluster.
ScheduleDAGInstrs *(* ScheduleDAGCtor)(MachineSchedContext *)
unsigned computeCyclicCriticalPath()
Compute the cyclic critical path through the DAG.
const std::vector< PressureChange > & getRegionCriticalPSets() const
PressureDiffs SUPressureDiffs
ReadyQueue(unsigned id, const Twine &name)
MachineBasicBlock::iterator bottom() const
void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos)
MachineBasicBlock::iterator end() const
end - Return an iterator to the bottom of the current scheduling region.
const IntervalPressure & getRegPressure() const
Get register pressure for the entire scheduling region before scheduling.
const SUnit * getNextClusterSucc() const
virtual void scheduleTree(unsigned SubtreeID)
Scheduler callback to notify that a new subtree is scheduled.
RegPressureTracker BotRPTracker
void releasePredecessors(SUnit *SU)
releasePredecessors - Call releasePred on each of SU's predecessors.
unsigned NodeQueueId
Definition: ScheduleDAG.h:272
virtual SUnit * pickNode(bool &IsTopNode)=0
RegPressureTracker RPTracker
const MachineDominatorTree * MDT
cl::opt< bool > ForceBottomUp
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
void placeDebugValues()
Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
RegisterClassInfo * RegClassInfo
SchedDFSResult * DFSResult
std::vector< PressureChange > RegionCriticalPSets
unsigned NodeNum
Definition: ScheduleDAG.h:271
MachineBasicBlock::iterator CurrentBottom
The bottom of the unscheduled zone.
const IntervalPressure & getBotPressure() const
Get current register pressure for the bottom scheduled instructions.
void initQueues(ArrayRef< SUnit * > TopRoots, ArrayRef< SUnit * > BotRoots)
Release ExitSU predecessors and setup scheduler queues.
void postprocessDAG()
Apply each ScheduleDAGMutation step in order.
bool ShouldTrackPressure
Register pressure in this region computed by initRegPressure.
RegPressureTracker TopRPTracker
const SUnit * getNextClusterPred() const
StringRef getName() const
unsigned size() const
void push(SUnit *SU)
bool canAddEdge(SUnit *SuccSU, SUnit *PredSU)
True if an edge can be added from PredSU to SuccSU without creating a cycle.
const RegPressureTracker & getBotRPTracker() const
std::vector< SUnit > SUnits
Definition: ScheduleDAG.h:545
AliasAnalysis * AA
const SchedDFSResult * getDFSResult() const
Return a non-null DFS result if the scheduling strategy initialized it.
virtual void apply(ScheduleDAGMI *DAG)=0
LiveIntervals * LIS
Live Intervals provides reaching defs in preRA scheduling.
SUnit - Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:249
cl::opt< bool > ForceTopDown
#define LLVM_OVERRIDE
Definition: Compiler.h:155