blob: 5c44a0e24d65f9eea6bc4749403566d5c6f450f3 [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>();
Andrew Trickd04ec0c2012-03-09 00:52:20 +000085 AU.addRequired<TargetPassConfig>();
Andrew Trick96f678f2012-01-13 06:30:30 +000086 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 Trickd04ec0c2012-03-09 00:52:20 +000095/// A dummy default scheduler factory indicates whether the scheduler
96/// is overridden on the command line.
97static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) {
98 return 0;
99}
Andrew Trick96f678f2012-01-13 06:30:30 +0000100
101/// MachineSchedOpt allows command line selection of the scheduler.
102static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
103 RegisterPassParser<MachineSchedRegistry> >
104MachineSchedOpt("misched",
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000105 cl::init(&useDefaultMachineSched), cl::Hidden,
Andrew Trick96f678f2012-01-13 06:30:30 +0000106 cl::desc("Machine instruction scheduler to use"));
107
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000108static MachineSchedRegistry
109SchedDefaultRegistry("default", "Use the target's default scheduler choice.",
110 useDefaultMachineSched);
111
112/// Forward declare the common machine scheduler. This will be used as the
113/// default scheduler if the target does not set a default.
114static ScheduleDAGInstrs *createCommonMachineSched(MachineSchedContext *C);
115
Andrew Trick42b7a712012-01-17 06:55:03 +0000116bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000117 // Initialize the context of the pass.
118 MF = &mf;
119 MLI = &getAnalysis<MachineLoopInfo>();
120 MDT = &getAnalysis<MachineDominatorTree>();
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000121 PassConfig = &getAnalysis<TargetPassConfig>();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000122 AA = &getAnalysis<AliasAnalysis>();
123
Lang Hames907cc8f2012-01-27 22:36:19 +0000124 LIS = &getAnalysis<LiveIntervals>();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000125 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000126
127 // Select the scheduler, or set the default.
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000128 MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt;
129 if (Ctor == useDefaultMachineSched) {
130 // Get the default scheduler set by the target.
131 Ctor = MachineSchedRegistry::getDefault();
132 if (!Ctor) {
133 Ctor = createCommonMachineSched;
134 MachineSchedRegistry::setDefault(Ctor);
135 }
Andrew Trick96f678f2012-01-13 06:30:30 +0000136 }
137 // Instantiate the selected scheduler.
138 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
139
140 // Visit all machine basic blocks.
141 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
142 MBB != MBBEnd; ++MBB) {
143
Andrew Trick1fabd9f2012-03-09 08:02:51 +0000144 Scheduler->startBlock(MBB);
145
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000146 // Break the block into scheduling regions [I, RegionEnd), and schedule each
147 // region as soon as it is discovered.
148 unsigned RemainingCount = MBB->size();
Andrew Trick7799eb42012-03-09 03:46:39 +0000149 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
150 RegionEnd != MBB->begin();) {
Andrew Trick1fabd9f2012-03-09 08:02:51 +0000151 // Avoid decrementing RegionEnd for blocks with no terminator.
152 if (RegionEnd != MBB->end()
153 || TII->isSchedulingBoundary(llvm::prior(RegionEnd), MBB, *MF)) {
154 --RegionEnd;
155 // Count the boundary instruction.
156 --RemainingCount;
157 }
158
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000159 // The next region starts above the previous region. Look backward in the
160 // instruction stream until we find the nearest boundary.
161 MachineBasicBlock::iterator I = RegionEnd;
Andrew Trick7799eb42012-03-09 03:46:39 +0000162 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000163 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
164 break;
165 }
Andrew Trick47c14452012-03-07 05:21:52 +0000166 // Notify the scheduler of the region, even if we may skip scheduling
167 // it. Perhaps it still needs to be bundled.
168 Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount);
169
170 // Skip empty scheduling regions (0 or 1 schedulable instructions).
171 if (I == RegionEnd || I == llvm::prior(RegionEnd)) {
Andrew Trick47c14452012-03-07 05:21:52 +0000172 // Close the current region. Bundle the terminator if needed.
173 Scheduler->exitRegion();
Andrew Trick1fabd9f2012-03-09 08:02:51 +0000174 RegionEnd = I;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000175 continue;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000176 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000177 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
Andrew Trick291411c2012-02-08 02:17:21 +0000178 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
179 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
180 else dbgs() << "End";
181 dbgs() << " Remaining: " << RemainingCount << "\n");
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000182
Andrew Trickd24da972012-03-09 03:46:42 +0000183 // Schedule a region: possibly reorder instructions.
Andrew Trick953be892012-03-07 23:00:49 +0000184 Scheduler->schedule();
Andrew Trickd24da972012-03-09 03:46:42 +0000185
186 // Close the current region.
Andrew Trick47c14452012-03-07 05:21:52 +0000187 Scheduler->exitRegion();
188
189 // Scheduling has invalidated the current iterator 'I'. Ask the
190 // scheduler for the top of it's scheduled region.
191 RegionEnd = Scheduler->begin();
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000192 }
193 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick953be892012-03-07 23:00:49 +0000194 Scheduler->finishBlock();
Andrew Trick96f678f2012-01-13 06:30:30 +0000195 }
196 return true;
197}
198
Andrew Trick42b7a712012-01-17 06:55:03 +0000199void MachineScheduler::print(raw_ostream &O, const Module* m) const {
Andrew Trick96f678f2012-01-13 06:30:30 +0000200 // unimplemented
201}
202
Andrew Trick5edf2f02012-01-14 02:17:06 +0000203//===----------------------------------------------------------------------===//
Andrew Trickc174eaf2012-03-08 01:41:12 +0000204// ScheduleTopeDownLive - Base class for basic top-down scheduling with
205// LiveIntervals preservation.
206// ===----------------------------------------------------------------------===//
207
208namespace {
209/// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules
210/// machine instructions while updating LiveIntervals.
211class ScheduleTopDownLive : public ScheduleDAGInstrs {
212 AliasAnalysis *AA;
213public:
214 ScheduleTopDownLive(MachineSchedContext *C):
215 ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
216 AA(C->AA) {}
217
218 /// ScheduleDAGInstrs interface.
219 void schedule();
220
221 /// Interface implemented by the selected top-down liveinterval scheduler.
222 ///
223 /// Pick the next node to schedule, or return NULL.
224 virtual SUnit *pickNode() = 0;
225
226 /// When all preceeding dependencies have been resolved, free this node for
227 /// scheduling.
228 virtual void releaseNode(SUnit *SU) = 0;
229
230protected:
231 void releaseSucc(SUnit *SU, SDep *SuccEdge);
232 void releaseSuccessors(SUnit *SU);
233};
234} // namespace
235
236/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
237/// NumPredsLeft reaches zero, release the successor node.
238void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) {
239 SUnit *SuccSU = SuccEdge->getSUnit();
240
241#ifndef NDEBUG
242 if (SuccSU->NumPredsLeft == 0) {
243 dbgs() << "*** Scheduling failed! ***\n";
244 SuccSU->dump(this);
245 dbgs() << " has been released too many times!\n";
246 llvm_unreachable(0);
247 }
248#endif
249 --SuccSU->NumPredsLeft;
250 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
251 releaseNode(SuccSU);
252}
253
254/// releaseSuccessors - Call releaseSucc on each of SU's successors.
255void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) {
256 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
257 I != E; ++I) {
258 releaseSucc(SU, &*I);
259 }
260}
261
262/// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
263/// time to do some work.
264void ScheduleTopDownLive::schedule() {
265 buildSchedGraph(AA);
266
267 DEBUG(dbgs() << "********** MI Scheduling **********\n");
268 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
269 SUnits[su].dumpAll(this));
270
271 if (ViewMISchedDAGs) viewGraph();
272
273 // Release any successors of the special Entry node. It is currently unused,
274 // but we keep up appearances.
275 releaseSuccessors(&EntrySU);
276
277 // Release all DAG roots for scheduling.
278 for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
279 I != E; ++I) {
280 // A SUnit is ready to schedule if it has no predecessors.
281 if (I->Preds.empty())
282 releaseNode(&(*I));
283 }
284
Andrew Trick68675c62012-03-09 04:29:02 +0000285 MachineBasicBlock::iterator InsertPos = RegionBegin;
Andrew Trickc174eaf2012-03-08 01:41:12 +0000286 while (SUnit *SU = pickNode()) {
287 DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this));
288
289 // Move the instruction to its new location in the instruction stream.
290 MachineInstr *MI = SU->getInstr();
291 if (&*InsertPos == MI)
292 ++InsertPos;
293 else {
294 BB->splice(InsertPos, BB, MI);
295 LIS->handleMove(MI);
Andrew Trick68675c62012-03-09 04:29:02 +0000296 if (RegionBegin == InsertPos)
297 RegionBegin = MI;
Andrew Trickc174eaf2012-03-08 01:41:12 +0000298 }
299
300 // Release dependent instructions for scheduling.
301 releaseSuccessors(SU);
302 }
303}
304
305//===----------------------------------------------------------------------===//
306// Placeholder for the default machine instruction scheduler.
Andrew Trick42b7a712012-01-17 06:55:03 +0000307//===----------------------------------------------------------------------===//
308
309namespace {
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000310class CommonMachineScheduler : public ScheduleDAGInstrs {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000311 AliasAnalysis *AA;
Andrew Trick42b7a712012-01-17 06:55:03 +0000312public:
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000313 CommonMachineScheduler(MachineSchedContext *C):
Andrew Trickc174eaf2012-03-08 01:41:12 +0000314 ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
315 AA(C->AA) {}
Andrew Trick42b7a712012-01-17 06:55:03 +0000316
Andrew Trick953be892012-03-07 23:00:49 +0000317 /// schedule - This is called back from ScheduleDAGInstrs::Run() when it's
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000318 /// time to do some work.
Andrew Trick953be892012-03-07 23:00:49 +0000319 void schedule();
Andrew Trick42b7a712012-01-17 06:55:03 +0000320};
321} // namespace
322
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000323/// The common machine scheduler will be used as the default scheduler if the
324/// target does not set a default.
325static ScheduleDAGInstrs *createCommonMachineSched(MachineSchedContext *C) {
326 return new CommonMachineScheduler(C);
Andrew Trick42b7a712012-01-17 06:55:03 +0000327}
328static MachineSchedRegistry
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000329SchedCommonRegistry("common", "Use the target's default scheduler choice.",
330 createCommonMachineSched);
Andrew Trick42b7a712012-01-17 06:55:03 +0000331
Andrew Trick42b7a712012-01-17 06:55:03 +0000332/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
333/// time to do some work.
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000334void CommonMachineScheduler::schedule() {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000335 buildSchedGraph(AA);
Andrew Trick42b7a712012-01-17 06:55:03 +0000336
337 DEBUG(dbgs() << "********** MI Scheduling **********\n");
338 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
339 SUnits[su].dumpAll(this));
340
341 // TODO: Put interesting things here.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000342 //
343 // When this is fully implemented, it will become a subclass of
344 // ScheduleTopDownLive. So this driver will disappear.
Andrew Trick42b7a712012-01-17 06:55:03 +0000345}
346
347//===----------------------------------------------------------------------===//
Andrew Trick5edf2f02012-01-14 02:17:06 +0000348// Machine Instruction Shuffler for Correctness Testing
349//===----------------------------------------------------------------------===//
350
Andrew Trick96f678f2012-01-13 06:30:30 +0000351#ifndef NDEBUG
352namespace {
Andrew Trickb4566a92012-02-22 06:08:11 +0000353// Nodes with a higher number have higher priority. This way we attempt to
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000354// schedule the latest instructions earliest.
355//
356// TODO: Relies on the property of the BuildSchedGraph that results in SUnits
Andrew Trickb4566a92012-02-22 06:08:11 +0000357// being ordered in sequence top-down.
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000358struct ShuffleSUnitOrder {
359 bool operator()(SUnit *A, SUnit *B) const {
Andrew Trickb4566a92012-02-22 06:08:11 +0000360 return A->NodeNum < B->NodeNum;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000361 }
362};
363
Andrew Trick96f678f2012-01-13 06:30:30 +0000364/// Reorder instructions as much as possible.
Andrew Trick42b7a712012-01-17 06:55:03 +0000365class InstructionShuffler : public ScheduleTopDownLive {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000366 std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue;
Andrew Trick96f678f2012-01-13 06:30:30 +0000367public:
Andrew Trickc174eaf2012-03-08 01:41:12 +0000368 InstructionShuffler(MachineSchedContext *C):
369 ScheduleTopDownLive(C) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000370
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000371 /// ScheduleTopDownLive Interface
372
373 virtual SUnit *pickNode() {
374 if (Queue.empty()) return NULL;
375 SUnit *SU = Queue.top();
376 Queue.pop();
377 return SU;
378 }
379
380 virtual void releaseNode(SUnit *SU) {
381 Queue.push(SU);
Andrew Trick96f678f2012-01-13 06:30:30 +0000382 }
383};
384} // namespace
385
Andrew Trickc174eaf2012-03-08 01:41:12 +0000386static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) {
387 return new InstructionShuffler(C);
Andrew Trick96f678f2012-01-13 06:30:30 +0000388}
389static MachineSchedRegistry ShufflerRegistry("shuffle",
390 "Shuffle machine instructions",
391 createInstructionShuffler);
392#endif // !NDEBUG