1 //===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // MachineScheduler schedules machine instructions after phi elimination. It
11 // preserves LiveIntervals so it can be invoked before register allocation.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "misched"
17 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
18 #include "llvm/CodeGen/MachinePassRegistry.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/ADT/OwningPtr.h"
34 static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden,
35 cl::desc("Pop up a window to show MISched dags after they are processed"));
37 static bool ViewMISchedDAGs = false;
40 //===----------------------------------------------------------------------===//
41 // Machine Instruction Scheduling Pass and Registry
42 //===----------------------------------------------------------------------===//
45 /// MachineScheduler runs after coalescing and before register allocation.
46 class MachineScheduler : public MachineFunctionPass {
49 const TargetInstrInfo *TII;
50 const MachineLoopInfo *MLI;
51 const MachineDominatorTree *MDT;
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
58 virtual void releaseMemory() {}
60 virtual bool runOnMachineFunction(MachineFunction&);
62 virtual void print(raw_ostream &O, const Module* = 0) const;
64 static char ID; // Class identification, replacement for typeinfo
68 char MachineScheduler::ID = 0;
70 char &llvm::MachineSchedulerID = MachineScheduler::ID;
72 INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
73 "Machine Instruction Scheduler", false, false)
74 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
75 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
76 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
77 INITIALIZE_PASS_END(MachineScheduler, "misched",
78 "Machine Instruction Scheduler", false, false)
80 MachineScheduler::MachineScheduler()
81 : MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) {
82 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
85 void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.addRequiredID(MachineDominatorsID);
88 AU.addRequired<MachineLoopInfo>();
89 AU.addRequired<AliasAnalysis>();
90 AU.addPreserved<AliasAnalysis>();
91 AU.addRequired<SlotIndexes>();
92 AU.addPreserved<SlotIndexes>();
93 AU.addRequired<LiveIntervals>();
94 AU.addPreserved<LiveIntervals>();
95 MachineFunctionPass::getAnalysisUsage(AU);
99 /// MachineSchedRegistry provides a selection of available machine instruction
101 class MachineSchedRegistry : public MachinePassRegistryNode {
103 typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineScheduler *);
105 // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
106 typedef ScheduleDAGCtor FunctionPassCtor;
108 static MachinePassRegistry Registry;
110 MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
111 : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
114 ~MachineSchedRegistry() { Registry.Remove(this); }
118 MachineSchedRegistry *getNext() const {
119 return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
121 static MachineSchedRegistry *getList() {
122 return (MachineSchedRegistry *)Registry.getList();
124 static ScheduleDAGCtor getDefault() {
125 return (ScheduleDAGCtor)Registry.getDefault();
127 static void setDefault(ScheduleDAGCtor C) {
128 Registry.setDefault((MachinePassCtor)C);
130 static void setListener(MachinePassRegistryListener *L) {
131 Registry.setListener(L);
136 MachinePassRegistry MachineSchedRegistry::Registry;
138 static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P);
140 /// MachineSchedOpt allows command line selection of the scheduler.
141 static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
142 RegisterPassParser<MachineSchedRegistry> >
143 MachineSchedOpt("misched",
144 cl::init(&createDefaultMachineSched), cl::Hidden,
145 cl::desc("Machine instruction scheduler to use"));
147 //===----------------------------------------------------------------------===//
148 // Machine Instruction Scheduling Common Implementation
149 //===----------------------------------------------------------------------===//
152 /// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules
153 /// machine instructions while updating LiveIntervals.
154 class ScheduleTopDownLive : public ScheduleDAGInstrs {
156 MachineScheduler *Pass;
158 ScheduleTopDownLive(MachineScheduler *P):
159 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false, P->LIS),
162 /// ScheduleDAGInstrs callback.
165 /// Interface implemented by the selected top-down liveinterval scheduler.
167 /// Pick the next node to schedule, or return NULL.
168 virtual SUnit *pickNode() = 0;
170 /// When all preceeding dependencies have been resolved, free this node for
172 virtual void releaseNode(SUnit *SU) = 0;
175 void releaseSucc(SUnit *SU, SDep *SuccEdge);
176 void releaseSuccessors(SUnit *SU);
180 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
181 /// NumPredsLeft reaches zero, release the successor node.
182 void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) {
183 SUnit *SuccSU = SuccEdge->getSUnit();
186 if (SuccSU->NumPredsLeft == 0) {
187 dbgs() << "*** Scheduling failed! ***\n";
189 dbgs() << " has been released too many times!\n";
193 --SuccSU->NumPredsLeft;
194 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
198 /// releaseSuccessors - Call releaseSucc on each of SU's successors.
199 void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) {
200 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
202 releaseSucc(SU, &*I);
206 /// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
207 /// time to do some work.
208 void ScheduleTopDownLive::schedule() {
209 buildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
211 DEBUG(dbgs() << "********** MI Scheduling **********\n");
212 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
213 SUnits[su].dumpAll(this));
215 if (ViewMISchedDAGs) viewGraph();
217 // Release any successors of the special Entry node. It is currently unused,
218 // but we keep up appearances.
219 releaseSuccessors(&EntrySU);
221 // Release all DAG roots for scheduling.
222 for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
224 // A SUnit is ready to schedule if it has no predecessors.
225 if (I->Preds.empty())
229 MachineBasicBlock::iterator InsertPos = Begin;
230 while (SUnit *SU = pickNode()) {
231 DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this));
233 // Move the instruction to its new location in the instruction stream.
234 MachineInstr *MI = SU->getInstr();
235 if (&*InsertPos == MI)
238 BB->splice(InsertPos, BB, MI);
239 Pass->LIS->handleMove(MI);
240 if (Begin == InsertPos)
244 // Release dependent instructions for scheduling.
245 releaseSuccessors(SU);
249 bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
250 // Initialize the context of the pass.
252 MLI = &getAnalysis<MachineLoopInfo>();
253 MDT = &getAnalysis<MachineDominatorTree>();
254 LIS = &getAnalysis<LiveIntervals>();
255 TII = MF->getTarget().getInstrInfo();
257 // Select the scheduler, or set the default.
258 MachineSchedRegistry::ScheduleDAGCtor Ctor =
259 MachineSchedRegistry::getDefault();
261 Ctor = MachineSchedOpt;
262 MachineSchedRegistry::setDefault(Ctor);
264 // Instantiate the selected scheduler.
265 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
267 // Visit all machine basic blocks.
268 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
269 MBB != MBBEnd; ++MBB) {
271 // Break the block into scheduling regions [I, RegionEnd), and schedule each
272 // region as soon as it is discovered.
273 unsigned RemainingCount = MBB->size();
274 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
275 RegionEnd != MBB->begin();) {
276 Scheduler->startBlock(MBB);
277 // The next region starts above the previous region. Look backward in the
278 // instruction stream until we find the nearest boundary.
279 MachineBasicBlock::iterator I = RegionEnd;
280 for(;I != MBB->begin(); --I, --RemainingCount) {
281 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
284 // Notify the scheduler of the region, even if we may skip scheduling
285 // it. Perhaps it still needs to be bundled.
286 Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount);
288 // Skip empty scheduling regions (0 or 1 schedulable instructions).
289 if (I == RegionEnd || I == llvm::prior(RegionEnd)) {
290 RegionEnd = llvm::prior(RegionEnd);
293 // Close the current region. Bundle the terminator if needed.
294 Scheduler->exitRegion();
297 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
298 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
299 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
300 else dbgs() << "End";
301 dbgs() << " Remaining: " << RemainingCount << "\n");
303 // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
304 // to our schedule() method.
305 Scheduler->schedule();
306 Scheduler->exitRegion();
308 // Scheduling has invalidated the current iterator 'I'. Ask the
309 // scheduler for the top of it's scheduled region.
310 RegionEnd = Scheduler->begin();
312 assert(RemainingCount == 0 && "Instruction count mismatch!");
313 Scheduler->finishBlock();
318 void MachineScheduler::print(raw_ostream &O, const Module* m) const {
322 //===----------------------------------------------------------------------===//
323 // Placeholder for extending the machine instruction scheduler.
324 //===----------------------------------------------------------------------===//
327 class DefaultMachineScheduler : public ScheduleDAGInstrs {
328 MachineScheduler *Pass;
330 DefaultMachineScheduler(MachineScheduler *P):
331 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false, P->LIS),
334 /// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
335 /// time to do some work.
340 static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P) {
341 return new DefaultMachineScheduler(P);
343 static MachineSchedRegistry
344 SchedDefaultRegistry("default", "Activate the scheduler pass, "
345 "but don't reorder instructions",
346 createDefaultMachineSched);
349 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
350 /// time to do some work.
351 void DefaultMachineScheduler::schedule() {
352 buildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
354 DEBUG(dbgs() << "********** MI Scheduling **********\n");
355 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
356 SUnits[su].dumpAll(this));
358 // TODO: Put interesting things here.
360 // When this is fully implemented, it will become a subclass of
361 // ScheduleTopDownLive. So this driver will disappear.
364 //===----------------------------------------------------------------------===//
365 // Machine Instruction Shuffler for Correctness Testing
366 //===----------------------------------------------------------------------===//
370 // Nodes with a higher number have higher priority. This way we attempt to
371 // schedule the latest instructions earliest.
373 // TODO: Relies on the property of the BuildSchedGraph that results in SUnits
374 // being ordered in sequence top-down.
375 struct ShuffleSUnitOrder {
376 bool operator()(SUnit *A, SUnit *B) const {
377 return A->NodeNum < B->NodeNum;
381 /// Reorder instructions as much as possible.
382 class InstructionShuffler : public ScheduleTopDownLive {
383 std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue;
385 InstructionShuffler(MachineScheduler *P):
386 ScheduleTopDownLive(P) {}
388 /// ScheduleTopDownLive Interface
390 virtual SUnit *pickNode() {
391 if (Queue.empty()) return NULL;
392 SUnit *SU = Queue.top();
397 virtual void releaseNode(SUnit *SU) {
403 static ScheduleDAGInstrs *createInstructionShuffler(MachineScheduler *P) {
404 return new InstructionShuffler(P);
406 static MachineSchedRegistry ShufflerRegistry("shuffle",
407 "Shuffle machine instructions",
408 createInstructionShuffler);