blob: da52df9feaeefa2419cf36df83b4024449e45f51 [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
17#include "ScheduleDAGInstrs.h"
18#include "LiveDebugVariables.h"
19#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "llvm/CodeGen/MachinePassRegistry.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Andrew Tricke9ef4ed2012-01-14 02:17:09 +000023#include "llvm/Target/TargetInstrInfo.h"
Andrew Trick96f678f2012-01-13 06:30:30 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/ADT/OwningPtr.h"
29
Andrew Trickc6cf11b2012-01-17 06:55:07 +000030#include <queue>
31
Andrew Trick96f678f2012-01-13 06:30:30 +000032using namespace llvm;
33
Andrew Trick5edf2f02012-01-14 02:17:06 +000034//===----------------------------------------------------------------------===//
35// Machine Instruction Scheduling Pass and Registry
36//===----------------------------------------------------------------------===//
37
Andrew Trick96f678f2012-01-13 06:30:30 +000038namespace {
Andrew Trick42b7a712012-01-17 06:55:03 +000039/// MachineScheduler runs after coalescing and before register allocation.
40class MachineScheduler : public MachineFunctionPass {
Andrew Trick96f678f2012-01-13 06:30:30 +000041public:
42 MachineFunction *MF;
Andrew Trick5edf2f02012-01-14 02:17:06 +000043 const TargetInstrInfo *TII;
Andrew Trick96f678f2012-01-13 06:30:30 +000044 const MachineLoopInfo *MLI;
45 const MachineDominatorTree *MDT;
Lang Hames907cc8f2012-01-27 22:36:19 +000046 LiveIntervals *LIS;
Andrew Trick96f678f2012-01-13 06:30:30 +000047
Andrew Trick42b7a712012-01-17 06:55:03 +000048 MachineScheduler();
Andrew Trick96f678f2012-01-13 06:30:30 +000049
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
51
52 virtual void releaseMemory() {}
53
54 virtual bool runOnMachineFunction(MachineFunction&);
55
56 virtual void print(raw_ostream &O, const Module* = 0) const;
57
58 static char ID; // Class identification, replacement for typeinfo
59};
60} // namespace
61
Andrew Trick42b7a712012-01-17 06:55:03 +000062char MachineScheduler::ID = 0;
Andrew Trick96f678f2012-01-13 06:30:30 +000063
Andrew Trick42b7a712012-01-17 06:55:03 +000064char &llvm::MachineSchedulerID = MachineScheduler::ID;
Andrew Trick96f678f2012-01-13 06:30:30 +000065
Andrew Trick42b7a712012-01-17 06:55:03 +000066INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000067 "Machine Instruction Scheduler", false, false)
68INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
69INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
70INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
71INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
Andrew Trick42b7a712012-01-17 06:55:03 +000072INITIALIZE_PASS_END(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000073 "Machine Instruction Scheduler", false, false)
74
Andrew Trick42b7a712012-01-17 06:55:03 +000075MachineScheduler::MachineScheduler()
Andrew Trick96f678f2012-01-13 06:30:30 +000076: MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) {
Andrew Trick42b7a712012-01-17 06:55:03 +000077 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick96f678f2012-01-13 06:30:30 +000078}
79
Andrew Trick42b7a712012-01-17 06:55:03 +000080void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Trick96f678f2012-01-13 06:30:30 +000081 AU.setPreservesCFG();
82 AU.addRequiredID(MachineDominatorsID);
83 AU.addRequired<MachineLoopInfo>();
84 AU.addRequired<AliasAnalysis>();
85 AU.addPreserved<AliasAnalysis>();
86 AU.addRequired<SlotIndexes>();
87 AU.addPreserved<SlotIndexes>();
88 AU.addRequired<LiveIntervals>();
89 AU.addPreserved<LiveIntervals>();
90 AU.addRequired<LiveDebugVariables>();
91 AU.addPreserved<LiveDebugVariables>();
Andrew Trick96f678f2012-01-13 06:30:30 +000092 MachineFunctionPass::getAnalysisUsage(AU);
93}
94
95namespace {
Andrew Trick96f678f2012-01-13 06:30:30 +000096/// MachineSchedRegistry provides a selection of available machine instruction
97/// schedulers.
98class MachineSchedRegistry : public MachinePassRegistryNode {
99public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000100 typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineScheduler *);
Andrew Trick96f678f2012-01-13 06:30:30 +0000101
102 // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
103 typedef ScheduleDAGCtor FunctionPassCtor;
104
105 static MachinePassRegistry Registry;
106
107 MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
108 : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
109 Registry.Add(this);
110 }
111 ~MachineSchedRegistry() { Registry.Remove(this); }
112
113 // Accessors.
114 //
115 MachineSchedRegistry *getNext() const {
116 return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
117 }
118 static MachineSchedRegistry *getList() {
119 return (MachineSchedRegistry *)Registry.getList();
120 }
121 static ScheduleDAGCtor getDefault() {
122 return (ScheduleDAGCtor)Registry.getDefault();
123 }
124 static void setDefault(ScheduleDAGCtor C) {
125 Registry.setDefault((MachinePassCtor)C);
126 }
127 static void setListener(MachinePassRegistryListener *L) {
128 Registry.setListener(L);
129 }
130};
131} // namespace
132
133MachinePassRegistry MachineSchedRegistry::Registry;
134
Andrew Trick42b7a712012-01-17 06:55:03 +0000135static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P);
Andrew Trick96f678f2012-01-13 06:30:30 +0000136
137/// MachineSchedOpt allows command line selection of the scheduler.
138static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
139 RegisterPassParser<MachineSchedRegistry> >
140MachineSchedOpt("misched",
141 cl::init(&createDefaultMachineSched), cl::Hidden,
142 cl::desc("Machine instruction scheduler to use"));
143
Andrew Trick5edf2f02012-01-14 02:17:06 +0000144//===----------------------------------------------------------------------===//
Andrew Trick42b7a712012-01-17 06:55:03 +0000145// Machine Instruction Scheduling Common Implementation
Andrew Trick5edf2f02012-01-14 02:17:06 +0000146//===----------------------------------------------------------------------===//
147
148namespace {
Andrew Trick78b29612012-02-09 00:40:52 +0000149/// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules
Andrew Trick5edf2f02012-01-14 02:17:06 +0000150/// machine instructions while updating LiveIntervals.
Andrew Trick42b7a712012-01-17 06:55:03 +0000151class ScheduleTopDownLive : public ScheduleDAGInstrs {
152protected:
153 MachineScheduler *Pass;
Andrew Trick5edf2f02012-01-14 02:17:06 +0000154public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000155 ScheduleTopDownLive(MachineScheduler *P):
Andrew Trick5e920d72012-01-14 02:17:12 +0000156 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000157
158 /// ScheduleDAGInstrs callback.
159 void Schedule();
160
161 /// Interface implemented by the selected top-down liveinterval scheduler.
162 ///
163 /// Pick the next node to schedule, or return NULL.
164 virtual SUnit *pickNode() = 0;
165
166 /// When all preceeding dependencies have been resolved, free this node for
167 /// scheduling.
168 virtual void releaseNode(SUnit *SU) = 0;
169
170protected:
171 void releaseSucc(SUnit *SU, SDep *SuccEdge);
172 void releaseSuccessors(SUnit *SU);
Andrew Trick5edf2f02012-01-14 02:17:06 +0000173};
174} // namespace
175
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000176/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
177/// NumPredsLeft reaches zero, release the successor node.
178void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) {
179 SUnit *SuccSU = SuccEdge->getSUnit();
180
181#ifndef NDEBUG
182 if (SuccSU->NumPredsLeft == 0) {
183 dbgs() << "*** Scheduling failed! ***\n";
184 SuccSU->dump(this);
185 dbgs() << " has been released too many times!\n";
186 llvm_unreachable(0);
187 }
188#endif
189 --SuccSU->NumPredsLeft;
190 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
191 releaseNode(SuccSU);
192}
193
194/// releaseSuccessors - Call releaseSucc on each of SU's successors.
195void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) {
196 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
197 I != E; ++I) {
198 releaseSucc(SU, &*I);
199 }
200}
201
202/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
203/// time to do some work.
204void ScheduleTopDownLive::Schedule() {
205 BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
206
207 DEBUG(dbgs() << "********** MI Scheduling **********\n");
208 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
209 SUnits[su].dumpAll(this));
210
211 // Release any successors of the special Entry node. It is currently unused,
212 // but we keep up appearances.
213 releaseSuccessors(&EntrySU);
214
215 // Release all DAG roots for scheduling.
216 for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
217 I != E; ++I) {
218 // A SUnit is ready to schedule if it has no predecessors.
219 if (I->Preds.empty())
220 releaseNode(&(*I));
221 }
222
223 InsertPos = Begin;
224 while (SUnit *SU = pickNode()) {
225 DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this));
226
227 // Move the instruction to its new location in the instruction stream.
228 MachineInstr *MI = SU->getInstr();
229 if (&*InsertPos == MI)
230 ++InsertPos;
231 else {
Lang Hamesda7984f2012-02-15 01:23:52 +0000232 BB->splice(InsertPos, BB, MI);
233 Pass->LIS->handleMove(MI);
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000234 if (Begin == InsertPos)
235 Begin = MI;
236 }
237
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000238 // Release dependent instructions for scheduling.
239 releaseSuccessors(SU);
240 }
241}
242
Andrew Trick42b7a712012-01-17 06:55:03 +0000243bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000244 // Initialize the context of the pass.
245 MF = &mf;
246 MLI = &getAnalysis<MachineLoopInfo>();
247 MDT = &getAnalysis<MachineDominatorTree>();
Lang Hames907cc8f2012-01-27 22:36:19 +0000248 LIS = &getAnalysis<LiveIntervals>();
Andrew Trick5edf2f02012-01-14 02:17:06 +0000249 TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000250
251 // Select the scheduler, or set the default.
252 MachineSchedRegistry::ScheduleDAGCtor Ctor =
253 MachineSchedRegistry::getDefault();
254 if (!Ctor) {
255 Ctor = MachineSchedOpt;
256 MachineSchedRegistry::setDefault(Ctor);
257 }
258 // Instantiate the selected scheduler.
259 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
260
261 // Visit all machine basic blocks.
262 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
263 MBB != MBBEnd; ++MBB) {
264
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000265 // Break the block into scheduling regions [I, RegionEnd), and schedule each
266 // region as soon as it is discovered.
267 unsigned RemainingCount = MBB->size();
268 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
269 RegionEnd != MBB->begin();) {
270 // The next region starts above the previous region. Look backward in the
271 // instruction stream until we find the nearest boundary.
272 MachineBasicBlock::iterator I = RegionEnd;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000273 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000274 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
275 break;
276 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000277 if (I == RegionEnd) {
278 // Skip empty scheduling regions.
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000279 RegionEnd = llvm::prior(RegionEnd);
Andrew Trick3c58ba82012-01-14 02:17:18 +0000280 --RemainingCount;
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000281 continue;
282 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000283 // Skip regions with one instruction.
284 if (I == llvm::prior(RegionEnd)) {
285 RegionEnd = llvm::prior(RegionEnd);
286 continue;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000287 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000288 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
Andrew Trick291411c2012-02-08 02:17:21 +0000289 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
290 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
291 else dbgs() << "End";
292 dbgs() << " Remaining: " << RemainingCount << "\n");
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000293
294 // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
295 // to our Schedule() method.
296 Scheduler->Run(MBB, I, RegionEnd, MBB->size());
297 RegionEnd = Scheduler->Begin;
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000298 }
299 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick96f678f2012-01-13 06:30:30 +0000300 }
301 return true;
302}
303
Andrew Trick42b7a712012-01-17 06:55:03 +0000304void MachineScheduler::print(raw_ostream &O, const Module* m) const {
Andrew Trick96f678f2012-01-13 06:30:30 +0000305 // unimplemented
306}
307
Andrew Trick5edf2f02012-01-14 02:17:06 +0000308//===----------------------------------------------------------------------===//
Andrew Trick42b7a712012-01-17 06:55:03 +0000309// Placeholder for extending the machine instruction scheduler.
310//===----------------------------------------------------------------------===//
311
312namespace {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000313class DefaultMachineScheduler : public ScheduleDAGInstrs {
314 MachineScheduler *Pass;
Andrew Trick42b7a712012-01-17 06:55:03 +0000315public:
316 DefaultMachineScheduler(MachineScheduler *P):
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000317 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
Andrew Trick42b7a712012-01-17 06:55:03 +0000318
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000319 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
320 /// time to do some work.
Andrew Trick42b7a712012-01-17 06:55:03 +0000321 void Schedule();
322};
323} // namespace
324
325static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P) {
326 return new DefaultMachineScheduler(P);
327}
328static MachineSchedRegistry
329SchedDefaultRegistry("default", "Activate the scheduler pass, "
330 "but don't reorder instructions",
331 createDefaultMachineSched);
332
333
334/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
335/// time to do some work.
336void DefaultMachineScheduler::Schedule() {
337 BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
338
339 DEBUG(dbgs() << "********** MI Scheduling **********\n");
340 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
341 SUnits[su].dumpAll(this));
342
343 // TODO: Put interesting things here.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000344 //
345 // When this is fully implemented, it will become a subclass of
346 // ScheduleTopDownLive. So this driver will disappear.
Andrew Trick42b7a712012-01-17 06:55:03 +0000347}
348
349//===----------------------------------------------------------------------===//
Andrew Trick5edf2f02012-01-14 02:17:06 +0000350// Machine Instruction Shuffler for Correctness Testing
351//===----------------------------------------------------------------------===//
352
Andrew Trick96f678f2012-01-13 06:30:30 +0000353#ifndef NDEBUG
354namespace {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000355// Nodes with a higher number have lower priority. This way we attempt to
356// schedule the latest instructions earliest.
357//
358// TODO: Relies on the property of the BuildSchedGraph that results in SUnits
359// being ordered in sequence bottom-up. This will be formalized, probably be
360// constructing SUnits in a prepass.
361struct ShuffleSUnitOrder {
362 bool operator()(SUnit *A, SUnit *B) const {
363 return A->NodeNum > B->NodeNum;
364 }
365};
366
Andrew Trick96f678f2012-01-13 06:30:30 +0000367/// Reorder instructions as much as possible.
Andrew Trick42b7a712012-01-17 06:55:03 +0000368class InstructionShuffler : public ScheduleTopDownLive {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000369 std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue;
Andrew Trick96f678f2012-01-13 06:30:30 +0000370public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000371 InstructionShuffler(MachineScheduler *P):
372 ScheduleTopDownLive(P) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000373
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000374 /// ScheduleTopDownLive Interface
375
376 virtual SUnit *pickNode() {
377 if (Queue.empty()) return NULL;
378 SUnit *SU = Queue.top();
379 Queue.pop();
380 return SU;
381 }
382
383 virtual void releaseNode(SUnit *SU) {
384 Queue.push(SU);
Andrew Trick96f678f2012-01-13 06:30:30 +0000385 }
386};
387} // namespace
388
Andrew Trick42b7a712012-01-17 06:55:03 +0000389static ScheduleDAGInstrs *createInstructionShuffler(MachineScheduler *P) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000390 return new InstructionShuffler(P);
391}
392static MachineSchedRegistry ShufflerRegistry("shuffle",
393 "Shuffle machine instructions",
394 createInstructionShuffler);
395#endif // !NDEBUG