blob: 203ddfd18bc7a7e223fcfbaaf0423d6b0625d375 [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"
Andrew Trickc174eaf2012-03-08 01:41:12 +000018#include "llvm/CodeGen/MachineScheduler.h"
Andrew Trick96f678f2012-01-13 06:30:30 +000019#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.
Andrew Trickc174eaf2012-03-08 01:41:12 +000046class MachineScheduler : public MachineSchedContext,
47 public MachineFunctionPass {
Andrew Trick96f678f2012-01-13 06:30:30 +000048public:
Andrew Trick42b7a712012-01-17 06:55:03 +000049 MachineScheduler();
Andrew Trick96f678f2012-01-13 06:30:30 +000050
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
52
53 virtual void releaseMemory() {}
54
55 virtual bool runOnMachineFunction(MachineFunction&);
56
57 virtual void print(raw_ostream &O, const Module* = 0) const;
58
59 static char ID; // Class identification, replacement for typeinfo
60};
61} // namespace
62
Andrew Trick42b7a712012-01-17 06:55:03 +000063char MachineScheduler::ID = 0;
Andrew Trick96f678f2012-01-13 06:30:30 +000064
Andrew Trick42b7a712012-01-17 06:55:03 +000065char &llvm::MachineSchedulerID = MachineScheduler::ID;
Andrew Trick96f678f2012-01-13 06:30:30 +000066
Andrew Trick42b7a712012-01-17 06:55:03 +000067INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000068 "Machine Instruction Scheduler", false, false)
69INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
70INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
71INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
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 Trickc174eaf2012-03-08 01:41:12 +000076: MachineFunctionPass(ID) {
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>();
Andrew Trick96f678f2012-01-13 06:30:30 +000090 MachineFunctionPass::getAnalysisUsage(AU);
91}
92
Andrew Trick96f678f2012-01-13 06:30:30 +000093MachinePassRegistry MachineSchedRegistry::Registry;
94
Andrew Trickc174eaf2012-03-08 01:41:12 +000095static ScheduleDAGInstrs *createDefaultMachineSched(MachineSchedContext *C);
Andrew Trick96f678f2012-01-13 06:30:30 +000096
97/// MachineSchedOpt allows command line selection of the scheduler.
98static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
99 RegisterPassParser<MachineSchedRegistry> >
100MachineSchedOpt("misched",
101 cl::init(&createDefaultMachineSched), cl::Hidden,
102 cl::desc("Machine instruction scheduler to use"));
103
Andrew Trick42b7a712012-01-17 06:55:03 +0000104bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000105 // Initialize the context of the pass.
106 MF = &mf;
107 MLI = &getAnalysis<MachineLoopInfo>();
108 MDT = &getAnalysis<MachineDominatorTree>();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000109 AA = &getAnalysis<AliasAnalysis>();
110
Lang Hames907cc8f2012-01-27 22:36:19 +0000111 LIS = &getAnalysis<LiveIntervals>();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000112 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000113
114 // Select the scheduler, or set the default.
115 MachineSchedRegistry::ScheduleDAGCtor Ctor =
116 MachineSchedRegistry::getDefault();
117 if (!Ctor) {
118 Ctor = MachineSchedOpt;
119 MachineSchedRegistry::setDefault(Ctor);
120 }
121 // Instantiate the selected scheduler.
122 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
123
124 // Visit all machine basic blocks.
125 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
126 MBB != MBBEnd; ++MBB) {
127
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000128 // Break the block into scheduling regions [I, RegionEnd), and schedule each
129 // region as soon as it is discovered.
130 unsigned RemainingCount = MBB->size();
131 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
132 RegionEnd != MBB->begin();) {
Andrew Trick953be892012-03-07 23:00:49 +0000133 Scheduler->startBlock(MBB);
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000134 // The next region starts above the previous region. Look backward in the
135 // instruction stream until we find the nearest boundary.
136 MachineBasicBlock::iterator I = RegionEnd;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000137 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000138 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
139 break;
140 }
Andrew Trick47c14452012-03-07 05:21:52 +0000141 // Notify the scheduler of the region, even if we may skip scheduling
142 // it. Perhaps it still needs to be bundled.
143 Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount);
144
145 // Skip empty scheduling regions (0 or 1 schedulable instructions).
146 if (I == RegionEnd || I == llvm::prior(RegionEnd)) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000147 RegionEnd = llvm::prior(RegionEnd);
Andrew Trick47c14452012-03-07 05:21:52 +0000148 if (I != RegionEnd)
149 --RemainingCount;
150 // Close the current region. Bundle the terminator if needed.
151 Scheduler->exitRegion();
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000152 continue;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000153 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000154 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
Andrew Trick291411c2012-02-08 02:17:21 +0000155 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
156 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
157 else dbgs() << "End";
158 dbgs() << " Remaining: " << RemainingCount << "\n");
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000159
160 // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
Andrew Trick953be892012-03-07 23:00:49 +0000161 // to our schedule() method.
162 Scheduler->schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000163 Scheduler->exitRegion();
164
165 // Scheduling has invalidated the current iterator 'I'. Ask the
166 // scheduler for the top of it's scheduled region.
167 RegionEnd = Scheduler->begin();
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000168 }
169 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick953be892012-03-07 23:00:49 +0000170 Scheduler->finishBlock();
Andrew Trick96f678f2012-01-13 06:30:30 +0000171 }
172 return true;
173}
174
Andrew Trick42b7a712012-01-17 06:55:03 +0000175void MachineScheduler::print(raw_ostream &O, const Module* m) const {
Andrew Trick96f678f2012-01-13 06:30:30 +0000176 // unimplemented
177}
178
Andrew Trick5edf2f02012-01-14 02:17:06 +0000179//===----------------------------------------------------------------------===//
Andrew Trickc174eaf2012-03-08 01:41:12 +0000180// ScheduleTopeDownLive - Base class for basic top-down scheduling with
181// LiveIntervals preservation.
182// ===----------------------------------------------------------------------===//
183
184namespace {
185/// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules
186/// machine instructions while updating LiveIntervals.
187class ScheduleTopDownLive : public ScheduleDAGInstrs {
188 AliasAnalysis *AA;
189public:
190 ScheduleTopDownLive(MachineSchedContext *C):
191 ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
192 AA(C->AA) {}
193
194 /// ScheduleDAGInstrs interface.
195 void schedule();
196
197 /// Interface implemented by the selected top-down liveinterval scheduler.
198 ///
199 /// Pick the next node to schedule, or return NULL.
200 virtual SUnit *pickNode() = 0;
201
202 /// When all preceeding dependencies have been resolved, free this node for
203 /// scheduling.
204 virtual void releaseNode(SUnit *SU) = 0;
205
206protected:
207 void releaseSucc(SUnit *SU, SDep *SuccEdge);
208 void releaseSuccessors(SUnit *SU);
209};
210} // namespace
211
212/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
213/// NumPredsLeft reaches zero, release the successor node.
214void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) {
215 SUnit *SuccSU = SuccEdge->getSUnit();
216
217#ifndef NDEBUG
218 if (SuccSU->NumPredsLeft == 0) {
219 dbgs() << "*** Scheduling failed! ***\n";
220 SuccSU->dump(this);
221 dbgs() << " has been released too many times!\n";
222 llvm_unreachable(0);
223 }
224#endif
225 --SuccSU->NumPredsLeft;
226 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
227 releaseNode(SuccSU);
228}
229
230/// releaseSuccessors - Call releaseSucc on each of SU's successors.
231void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) {
232 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
233 I != E; ++I) {
234 releaseSucc(SU, &*I);
235 }
236}
237
238/// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
239/// time to do some work.
240void ScheduleTopDownLive::schedule() {
241 buildSchedGraph(AA);
242
243 DEBUG(dbgs() << "********** MI Scheduling **********\n");
244 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
245 SUnits[su].dumpAll(this));
246
247 if (ViewMISchedDAGs) viewGraph();
248
249 // Release any successors of the special Entry node. It is currently unused,
250 // but we keep up appearances.
251 releaseSuccessors(&EntrySU);
252
253 // Release all DAG roots for scheduling.
254 for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
255 I != E; ++I) {
256 // A SUnit is ready to schedule if it has no predecessors.
257 if (I->Preds.empty())
258 releaseNode(&(*I));
259 }
260
261 MachineBasicBlock::iterator InsertPos = Begin;
262 while (SUnit *SU = pickNode()) {
263 DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this));
264
265 // Move the instruction to its new location in the instruction stream.
266 MachineInstr *MI = SU->getInstr();
267 if (&*InsertPos == MI)
268 ++InsertPos;
269 else {
270 BB->splice(InsertPos, BB, MI);
271 LIS->handleMove(MI);
272 if (Begin == InsertPos)
273 Begin = MI;
274 }
275
276 // Release dependent instructions for scheduling.
277 releaseSuccessors(SU);
278 }
279}
280
281//===----------------------------------------------------------------------===//
282// Placeholder for the default machine instruction scheduler.
Andrew Trick42b7a712012-01-17 06:55:03 +0000283//===----------------------------------------------------------------------===//
284
285namespace {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000286class DefaultMachineScheduler : public ScheduleDAGInstrs {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000287 AliasAnalysis *AA;
Andrew Trick42b7a712012-01-17 06:55:03 +0000288public:
Andrew Trickc174eaf2012-03-08 01:41:12 +0000289 DefaultMachineScheduler(MachineSchedContext *C):
290 ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
291 AA(C->AA) {}
Andrew Trick42b7a712012-01-17 06:55:03 +0000292
Andrew Trick953be892012-03-07 23:00:49 +0000293 /// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000294 /// time to do some work.
Andrew Trick953be892012-03-07 23:00:49 +0000295 void schedule();
Andrew Trick42b7a712012-01-17 06:55:03 +0000296};
297} // namespace
298
Andrew Trickc174eaf2012-03-08 01:41:12 +0000299static ScheduleDAGInstrs *createDefaultMachineSched(MachineSchedContext *C) {
300 return new DefaultMachineScheduler(C);
Andrew Trick42b7a712012-01-17 06:55:03 +0000301}
302static MachineSchedRegistry
303SchedDefaultRegistry("default", "Activate the scheduler pass, "
304 "but don't reorder instructions",
305 createDefaultMachineSched);
306
Andrew Trick42b7a712012-01-17 06:55:03 +0000307/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
308/// time to do some work.
Andrew Trick953be892012-03-07 23:00:49 +0000309void DefaultMachineScheduler::schedule() {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000310 buildSchedGraph(AA);
Andrew Trick42b7a712012-01-17 06:55:03 +0000311
312 DEBUG(dbgs() << "********** MI Scheduling **********\n");
313 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
314 SUnits[su].dumpAll(this));
315
316 // TODO: Put interesting things here.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000317 //
318 // When this is fully implemented, it will become a subclass of
319 // ScheduleTopDownLive. So this driver will disappear.
Andrew Trick42b7a712012-01-17 06:55:03 +0000320}
321
322//===----------------------------------------------------------------------===//
Andrew Trick5edf2f02012-01-14 02:17:06 +0000323// Machine Instruction Shuffler for Correctness Testing
324//===----------------------------------------------------------------------===//
325
Andrew Trick96f678f2012-01-13 06:30:30 +0000326#ifndef NDEBUG
327namespace {
Andrew Trickb4566a92012-02-22 06:08:11 +0000328// Nodes with a higher number have higher priority. This way we attempt to
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000329// schedule the latest instructions earliest.
330//
331// TODO: Relies on the property of the BuildSchedGraph that results in SUnits
Andrew Trickb4566a92012-02-22 06:08:11 +0000332// being ordered in sequence top-down.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000333struct ShuffleSUnitOrder {
334 bool operator()(SUnit *A, SUnit *B) const {
Andrew Trickb4566a92012-02-22 06:08:11 +0000335 return A->NodeNum < B->NodeNum;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000336 }
337};
338
Andrew Trick96f678f2012-01-13 06:30:30 +0000339/// Reorder instructions as much as possible.
Andrew Trick42b7a712012-01-17 06:55:03 +0000340class InstructionShuffler : public ScheduleTopDownLive {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000341 std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue;
Andrew Trick96f678f2012-01-13 06:30:30 +0000342public:
Andrew Trickc174eaf2012-03-08 01:41:12 +0000343 InstructionShuffler(MachineSchedContext *C):
344 ScheduleTopDownLive(C) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000345
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000346 /// ScheduleTopDownLive Interface
347
348 virtual SUnit *pickNode() {
349 if (Queue.empty()) return NULL;
350 SUnit *SU = Queue.top();
351 Queue.pop();
352 return SU;
353 }
354
355 virtual void releaseNode(SUnit *SU) {
356 Queue.push(SU);
Andrew Trick96f678f2012-01-13 06:30:30 +0000357 }
358};
359} // namespace
360
Andrew Trickc174eaf2012-03-08 01:41:12 +0000361static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) {
362 return new InstructionShuffler(C);
Andrew Trick96f678f2012-01-13 06:30:30 +0000363}
364static MachineSchedRegistry ShufflerRegistry("shuffle",
365 "Shuffle machine instructions",
366 createInstructionShuffler);
367#endif // !NDEBUG