blob: aff67ce09e8f94133948b5a3e3a377c3de7be13c [file] [log] [blame]
Andrew Trick96f678f2012-01-13 06:30:30 +00001//===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===//
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// MachineScheduler schedules machine instructions after phi elimination. It
11// preserves LiveIntervals so it can be invoked before register allocation.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "misched"
16
Andrew Trick96f678f2012-01-13 06:30:30 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
18#include "llvm/CodeGen/MachinePassRegistry.h"
19#include "llvm/CodeGen/Passes.h"
Andrew Tricked395c82012-03-07 23:01:06 +000020#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Andrew Trick96f678f2012-01-13 06:30:30 +000021#include "llvm/Analysis/AliasAnalysis.h"
Andrew Tricke9ef4ed2012-01-14 02:17:09 +000022#include "llvm/Target/TargetInstrInfo.h"
Andrew Trick96f678f2012-01-13 06:30:30 +000023#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"
28
Andrew Trickc6cf11b2012-01-17 06:55:07 +000029#include <queue>
30
Andrew Trick96f678f2012-01-13 06:30:30 +000031using namespace llvm;
32
Andrew Trick0df7f882012-03-07 00:18:25 +000033#ifndef NDEBUG
34static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden,
35 cl::desc("Pop up a window to show MISched dags after they are processed"));
36#else
37static bool ViewMISchedDAGs = false;
38#endif // NDEBUG
39
Andrew Trick5edf2f02012-01-14 02:17:06 +000040//===----------------------------------------------------------------------===//
41// Machine Instruction Scheduling Pass and Registry
42//===----------------------------------------------------------------------===//
43
Andrew Trick96f678f2012-01-13 06:30:30 +000044namespace {
Andrew Trick42b7a712012-01-17 06:55:03 +000045/// MachineScheduler runs after coalescing and before register allocation.
46class MachineScheduler : public MachineFunctionPass {
Andrew Trick96f678f2012-01-13 06:30:30 +000047public:
48 MachineFunction *MF;
Andrew Trick5edf2f02012-01-14 02:17:06 +000049 const TargetInstrInfo *TII;
Andrew Trick96f678f2012-01-13 06:30:30 +000050 const MachineLoopInfo *MLI;
51 const MachineDominatorTree *MDT;
Lang Hames907cc8f2012-01-27 22:36:19 +000052 LiveIntervals *LIS;
Andrew Trick96f678f2012-01-13 06:30:30 +000053
Andrew Trick42b7a712012-01-17 06:55:03 +000054 MachineScheduler();
Andrew Trick96f678f2012-01-13 06:30:30 +000055
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
57
58 virtual void releaseMemory() {}
59
60 virtual bool runOnMachineFunction(MachineFunction&);
61
62 virtual void print(raw_ostream &O, const Module* = 0) const;
63
64 static char ID; // Class identification, replacement for typeinfo
65};
66} // namespace
67
Andrew Trick42b7a712012-01-17 06:55:03 +000068char MachineScheduler::ID = 0;
Andrew Trick96f678f2012-01-13 06:30:30 +000069
Andrew Trick42b7a712012-01-17 06:55:03 +000070char &llvm::MachineSchedulerID = MachineScheduler::ID;
Andrew Trick96f678f2012-01-13 06:30:30 +000071
Andrew Trick42b7a712012-01-17 06:55:03 +000072INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000073 "Machine Instruction Scheduler", false, false)
74INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
75INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
76INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Andrew Trick42b7a712012-01-17 06:55:03 +000077INITIALIZE_PASS_END(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000078 "Machine Instruction Scheduler", false, false)
79
Andrew Trick42b7a712012-01-17 06:55:03 +000080MachineScheduler::MachineScheduler()
Andrew Trick96f678f2012-01-13 06:30:30 +000081: MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) {
Andrew Trick42b7a712012-01-17 06:55:03 +000082 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick96f678f2012-01-13 06:30:30 +000083}
84
Andrew Trick42b7a712012-01-17 06:55:03 +000085void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Trick96f678f2012-01-13 06:30:30 +000086 AU.setPreservesCFG();
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>();
Andrew Trick96f678f2012-01-13 06:30:30 +000095 MachineFunctionPass::getAnalysisUsage(AU);
96}
97
98namespace {
Andrew Trick96f678f2012-01-13 06:30:30 +000099/// MachineSchedRegistry provides a selection of available machine instruction
100/// schedulers.
101class MachineSchedRegistry : public MachinePassRegistryNode {
102public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000103 typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineScheduler *);
Andrew Trick96f678f2012-01-13 06:30:30 +0000104
105 // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
106 typedef ScheduleDAGCtor FunctionPassCtor;
107
108 static MachinePassRegistry Registry;
109
110 MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
111 : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
112 Registry.Add(this);
113 }
114 ~MachineSchedRegistry() { Registry.Remove(this); }
115
116 // Accessors.
117 //
118 MachineSchedRegistry *getNext() const {
119 return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
120 }
121 static MachineSchedRegistry *getList() {
122 return (MachineSchedRegistry *)Registry.getList();
123 }
124 static ScheduleDAGCtor getDefault() {
125 return (ScheduleDAGCtor)Registry.getDefault();
126 }
127 static void setDefault(ScheduleDAGCtor C) {
128 Registry.setDefault((MachinePassCtor)C);
129 }
130 static void setListener(MachinePassRegistryListener *L) {
131 Registry.setListener(L);
132 }
133};
134} // namespace
135
136MachinePassRegistry MachineSchedRegistry::Registry;
137
Andrew Trick42b7a712012-01-17 06:55:03 +0000138static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P);
Andrew Trick96f678f2012-01-13 06:30:30 +0000139
140/// MachineSchedOpt allows command line selection of the scheduler.
141static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
142 RegisterPassParser<MachineSchedRegistry> >
143MachineSchedOpt("misched",
144 cl::init(&createDefaultMachineSched), cl::Hidden,
145 cl::desc("Machine instruction scheduler to use"));
146
Andrew Trick5edf2f02012-01-14 02:17:06 +0000147//===----------------------------------------------------------------------===//
Andrew Trick42b7a712012-01-17 06:55:03 +0000148// Machine Instruction Scheduling Common Implementation
Andrew Trick5edf2f02012-01-14 02:17:06 +0000149//===----------------------------------------------------------------------===//
150
151namespace {
Andrew Trick78b29612012-02-09 00:40:52 +0000152/// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules
Andrew Trick5edf2f02012-01-14 02:17:06 +0000153/// machine instructions while updating LiveIntervals.
Andrew Trick42b7a712012-01-17 06:55:03 +0000154class ScheduleTopDownLive : public ScheduleDAGInstrs {
155protected:
156 MachineScheduler *Pass;
Andrew Trick5edf2f02012-01-14 02:17:06 +0000157public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000158 ScheduleTopDownLive(MachineScheduler *P):
Andrew Trickb4566a92012-02-22 06:08:11 +0000159 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false, P->LIS),
160 Pass(P) {}
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000161
162 /// ScheduleDAGInstrs callback.
Andrew Trick953be892012-03-07 23:00:49 +0000163 void schedule();
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000164
165 /// Interface implemented by the selected top-down liveinterval scheduler.
166 ///
167 /// Pick the next node to schedule, or return NULL.
168 virtual SUnit *pickNode() = 0;
169
170 /// When all preceeding dependencies have been resolved, free this node for
171 /// scheduling.
172 virtual void releaseNode(SUnit *SU) = 0;
173
174protected:
175 void releaseSucc(SUnit *SU, SDep *SuccEdge);
176 void releaseSuccessors(SUnit *SU);
Andrew Trick5edf2f02012-01-14 02:17:06 +0000177};
178} // namespace
179
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000180/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
181/// NumPredsLeft reaches zero, release the successor node.
182void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) {
183 SUnit *SuccSU = SuccEdge->getSUnit();
184
185#ifndef NDEBUG
186 if (SuccSU->NumPredsLeft == 0) {
187 dbgs() << "*** Scheduling failed! ***\n";
188 SuccSU->dump(this);
189 dbgs() << " has been released too many times!\n";
190 llvm_unreachable(0);
191 }
192#endif
193 --SuccSU->NumPredsLeft;
194 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
195 releaseNode(SuccSU);
196}
197
198/// releaseSuccessors - Call releaseSucc on each of SU's successors.
199void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) {
200 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
201 I != E; ++I) {
202 releaseSucc(SU, &*I);
203 }
204}
205
Andrew Trick953be892012-03-07 23:00:49 +0000206/// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000207/// time to do some work.
Andrew Trick953be892012-03-07 23:00:49 +0000208void ScheduleTopDownLive::schedule() {
209 buildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000210
211 DEBUG(dbgs() << "********** MI Scheduling **********\n");
212 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
213 SUnits[su].dumpAll(this));
214
Andrew Trick0df7f882012-03-07 00:18:25 +0000215 if (ViewMISchedDAGs) viewGraph();
216
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000217 // Release any successors of the special Entry node. It is currently unused,
218 // but we keep up appearances.
219 releaseSuccessors(&EntrySU);
220
221 // Release all DAG roots for scheduling.
222 for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
223 I != E; ++I) {
224 // A SUnit is ready to schedule if it has no predecessors.
225 if (I->Preds.empty())
226 releaseNode(&(*I));
227 }
228
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000229 MachineBasicBlock::iterator InsertPos = Begin;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000230 while (SUnit *SU = pickNode()) {
231 DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this));
232
233 // Move the instruction to its new location in the instruction stream.
234 MachineInstr *MI = SU->getInstr();
235 if (&*InsertPos == MI)
236 ++InsertPos;
237 else {
Lang Hamesda7984f2012-02-15 01:23:52 +0000238 BB->splice(InsertPos, BB, MI);
239 Pass->LIS->handleMove(MI);
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000240 if (Begin == InsertPos)
241 Begin = MI;
242 }
243
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000244 // Release dependent instructions for scheduling.
245 releaseSuccessors(SU);
246 }
247}
248
Andrew Trick42b7a712012-01-17 06:55:03 +0000249bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000250 // Initialize the context of the pass.
251 MF = &mf;
252 MLI = &getAnalysis<MachineLoopInfo>();
253 MDT = &getAnalysis<MachineDominatorTree>();
Lang Hames907cc8f2012-01-27 22:36:19 +0000254 LIS = &getAnalysis<LiveIntervals>();
Andrew Trick5edf2f02012-01-14 02:17:06 +0000255 TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000256
257 // Select the scheduler, or set the default.
258 MachineSchedRegistry::ScheduleDAGCtor Ctor =
259 MachineSchedRegistry::getDefault();
260 if (!Ctor) {
261 Ctor = MachineSchedOpt;
262 MachineSchedRegistry::setDefault(Ctor);
263 }
264 // Instantiate the selected scheduler.
265 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
266
267 // Visit all machine basic blocks.
268 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
269 MBB != MBBEnd; ++MBB) {
270
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000271 // 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();) {
Andrew Trick953be892012-03-07 23:00:49 +0000276 Scheduler->startBlock(MBB);
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000277 // 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;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000280 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000281 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
282 break;
283 }
Andrew Trick47c14452012-03-07 05:21:52 +0000284 // 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);
287
288 // Skip empty scheduling regions (0 or 1 schedulable instructions).
289 if (I == RegionEnd || I == llvm::prior(RegionEnd)) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000290 RegionEnd = llvm::prior(RegionEnd);
Andrew Trick47c14452012-03-07 05:21:52 +0000291 if (I != RegionEnd)
292 --RemainingCount;
293 // Close the current region. Bundle the terminator if needed.
294 Scheduler->exitRegion();
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000295 continue;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000296 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000297 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
Andrew Trick291411c2012-02-08 02:17:21 +0000298 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
299 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
300 else dbgs() << "End";
301 dbgs() << " Remaining: " << RemainingCount << "\n");
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000302
303 // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
Andrew Trick953be892012-03-07 23:00:49 +0000304 // to our schedule() method.
305 Scheduler->schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000306 Scheduler->exitRegion();
307
308 // Scheduling has invalidated the current iterator 'I'. Ask the
309 // scheduler for the top of it's scheduled region.
310 RegionEnd = Scheduler->begin();
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000311 }
312 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick953be892012-03-07 23:00:49 +0000313 Scheduler->finishBlock();
Andrew Trick96f678f2012-01-13 06:30:30 +0000314 }
315 return true;
316}
317
Andrew Trick42b7a712012-01-17 06:55:03 +0000318void MachineScheduler::print(raw_ostream &O, const Module* m) const {
Andrew Trick96f678f2012-01-13 06:30:30 +0000319 // unimplemented
320}
321
Andrew Trick5edf2f02012-01-14 02:17:06 +0000322//===----------------------------------------------------------------------===//
Andrew Trick42b7a712012-01-17 06:55:03 +0000323// Placeholder for extending the machine instruction scheduler.
324//===----------------------------------------------------------------------===//
325
326namespace {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000327class DefaultMachineScheduler : public ScheduleDAGInstrs {
328 MachineScheduler *Pass;
Andrew Trick42b7a712012-01-17 06:55:03 +0000329public:
330 DefaultMachineScheduler(MachineScheduler *P):
Andrew Trickb4566a92012-02-22 06:08:11 +0000331 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false, P->LIS),
332 Pass(P) {}
Andrew Trick42b7a712012-01-17 06:55:03 +0000333
Andrew Trick953be892012-03-07 23:00:49 +0000334 /// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000335 /// time to do some work.
Andrew Trick953be892012-03-07 23:00:49 +0000336 void schedule();
Andrew Trick42b7a712012-01-17 06:55:03 +0000337};
338} // namespace
339
340static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P) {
341 return new DefaultMachineScheduler(P);
342}
343static MachineSchedRegistry
344SchedDefaultRegistry("default", "Activate the scheduler pass, "
345 "but don't reorder instructions",
346 createDefaultMachineSched);
347
348
349/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
350/// time to do some work.
Andrew Trick953be892012-03-07 23:00:49 +0000351void DefaultMachineScheduler::schedule() {
352 buildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
Andrew Trick42b7a712012-01-17 06:55:03 +0000353
354 DEBUG(dbgs() << "********** MI Scheduling **********\n");
355 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
356 SUnits[su].dumpAll(this));
357
358 // TODO: Put interesting things here.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000359 //
360 // When this is fully implemented, it will become a subclass of
361 // ScheduleTopDownLive. So this driver will disappear.
Andrew Trick42b7a712012-01-17 06:55:03 +0000362}
363
364//===----------------------------------------------------------------------===//
Andrew Trick5edf2f02012-01-14 02:17:06 +0000365// Machine Instruction Shuffler for Correctness Testing
366//===----------------------------------------------------------------------===//
367
Andrew Trick96f678f2012-01-13 06:30:30 +0000368#ifndef NDEBUG
369namespace {
Andrew Trickb4566a92012-02-22 06:08:11 +0000370// Nodes with a higher number have higher priority. This way we attempt to
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000371// schedule the latest instructions earliest.
372//
373// TODO: Relies on the property of the BuildSchedGraph that results in SUnits
Andrew Trickb4566a92012-02-22 06:08:11 +0000374// being ordered in sequence top-down.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000375struct ShuffleSUnitOrder {
376 bool operator()(SUnit *A, SUnit *B) const {
Andrew Trickb4566a92012-02-22 06:08:11 +0000377 return A->NodeNum < B->NodeNum;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000378 }
379};
380
Andrew Trick96f678f2012-01-13 06:30:30 +0000381/// Reorder instructions as much as possible.
Andrew Trick42b7a712012-01-17 06:55:03 +0000382class InstructionShuffler : public ScheduleTopDownLive {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000383 std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue;
Andrew Trick96f678f2012-01-13 06:30:30 +0000384public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000385 InstructionShuffler(MachineScheduler *P):
386 ScheduleTopDownLive(P) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000387
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000388 /// ScheduleTopDownLive Interface
389
390 virtual SUnit *pickNode() {
391 if (Queue.empty()) return NULL;
392 SUnit *SU = Queue.top();
393 Queue.pop();
394 return SU;
395 }
396
397 virtual void releaseNode(SUnit *SU) {
398 Queue.push(SU);
Andrew Trick96f678f2012-01-13 06:30:30 +0000399 }
400};
401} // namespace
402
Andrew Trick42b7a712012-01-17 06:55:03 +0000403static ScheduleDAGInstrs *createInstructionShuffler(MachineScheduler *P) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000404 return new InstructionShuffler(P);
405}
406static MachineSchedRegistry ShufflerRegistry("shuffle",
407 "Shuffle machine instructions",
408 createInstructionShuffler);
409#endif // !NDEBUG