blob: c68373ad9f2c589d0a59c867e95f075008ce5854 [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"
Andrew Trick17d35e52012-03-14 04:00:41 +000028#include "llvm/ADT/PriorityQueue.h"
Andrew Trick96f678f2012-01-13 06:30:30 +000029
Andrew Trickc6cf11b2012-01-17 06:55:07 +000030#include <queue>
31
Andrew Trick96f678f2012-01-13 06:30:30 +000032using namespace llvm;
33
Andrew Trick17d35e52012-03-14 04:00:41 +000034static cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden,
35 cl::desc("Force top-down list scheduling"));
36static cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden,
37 cl::desc("Force bottom-up list scheduling"));
38
Andrew Trick0df7f882012-03-07 00:18:25 +000039#ifndef NDEBUG
40static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden,
41 cl::desc("Pop up a window to show MISched dags after they are processed"));
42#else
43static bool ViewMISchedDAGs = false;
44#endif // NDEBUG
45
Andrew Trick5edf2f02012-01-14 02:17:06 +000046//===----------------------------------------------------------------------===//
47// Machine Instruction Scheduling Pass and Registry
48//===----------------------------------------------------------------------===//
49
Andrew Trick96f678f2012-01-13 06:30:30 +000050namespace {
Andrew Trick42b7a712012-01-17 06:55:03 +000051/// MachineScheduler runs after coalescing and before register allocation.
Andrew Trickc174eaf2012-03-08 01:41:12 +000052class MachineScheduler : public MachineSchedContext,
53 public MachineFunctionPass {
Andrew Trick96f678f2012-01-13 06:30:30 +000054public:
Andrew Trick42b7a712012-01-17 06:55:03 +000055 MachineScheduler();
Andrew Trick96f678f2012-01-13 06:30:30 +000056
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
58
59 virtual void releaseMemory() {}
60
61 virtual bool runOnMachineFunction(MachineFunction&);
62
63 virtual void print(raw_ostream &O, const Module* = 0) const;
64
65 static char ID; // Class identification, replacement for typeinfo
66};
67} // namespace
68
Andrew Trick42b7a712012-01-17 06:55:03 +000069char MachineScheduler::ID = 0;
Andrew Trick96f678f2012-01-13 06:30:30 +000070
Andrew Trick42b7a712012-01-17 06:55:03 +000071char &llvm::MachineSchedulerID = MachineScheduler::ID;
Andrew Trick96f678f2012-01-13 06:30:30 +000072
Andrew Trick42b7a712012-01-17 06:55:03 +000073INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000074 "Machine Instruction Scheduler", false, false)
75INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
76INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
77INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Andrew Trick42b7a712012-01-17 06:55:03 +000078INITIALIZE_PASS_END(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000079 "Machine Instruction Scheduler", false, false)
80
Andrew Trick42b7a712012-01-17 06:55:03 +000081MachineScheduler::MachineScheduler()
Andrew Trickc174eaf2012-03-08 01:41:12 +000082: MachineFunctionPass(ID) {
Andrew Trick42b7a712012-01-17 06:55:03 +000083 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick96f678f2012-01-13 06:30:30 +000084}
85
Andrew Trick42b7a712012-01-17 06:55:03 +000086void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Trick96f678f2012-01-13 06:30:30 +000087 AU.setPreservesCFG();
88 AU.addRequiredID(MachineDominatorsID);
89 AU.addRequired<MachineLoopInfo>();
90 AU.addRequired<AliasAnalysis>();
Andrew Trickd04ec0c2012-03-09 00:52:20 +000091 AU.addRequired<TargetPassConfig>();
Andrew Trick96f678f2012-01-13 06:30:30 +000092 AU.addRequired<SlotIndexes>();
93 AU.addPreserved<SlotIndexes>();
94 AU.addRequired<LiveIntervals>();
95 AU.addPreserved<LiveIntervals>();
Andrew Trick96f678f2012-01-13 06:30:30 +000096 MachineFunctionPass::getAnalysisUsage(AU);
97}
98
Andrew Trick96f678f2012-01-13 06:30:30 +000099MachinePassRegistry MachineSchedRegistry::Registry;
100
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000101/// A dummy default scheduler factory indicates whether the scheduler
102/// is overridden on the command line.
103static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) {
104 return 0;
105}
Andrew Trick96f678f2012-01-13 06:30:30 +0000106
107/// MachineSchedOpt allows command line selection of the scheduler.
108static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
109 RegisterPassParser<MachineSchedRegistry> >
110MachineSchedOpt("misched",
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000111 cl::init(&useDefaultMachineSched), cl::Hidden,
Andrew Trick96f678f2012-01-13 06:30:30 +0000112 cl::desc("Machine instruction scheduler to use"));
113
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000114static MachineSchedRegistry
Andrew Trick17d35e52012-03-14 04:00:41 +0000115DefaultSchedRegistry("default", "Use the target's default scheduler choice.",
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000116 useDefaultMachineSched);
117
Andrew Trick17d35e52012-03-14 04:00:41 +0000118/// Forward declare the standard machine scheduler. This will be used as the
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000119/// default scheduler if the target does not set a default.
Andrew Trick17d35e52012-03-14 04:00:41 +0000120static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C);
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000121
Andrew Trickcb058d52012-03-14 04:00:38 +0000122/// Top-level MachineScheduler pass driver.
123///
124/// Visit blocks in function order. Divide each block into scheduling regions
Andrew Trick17d35e52012-03-14 04:00:41 +0000125/// and visit them bottom-up. Visiting regions bottom-up is not required, but is
126/// consistent with the DAG builder, which traverses the interior of the
127/// scheduling regions bottom-up.
Andrew Trickcb058d52012-03-14 04:00:38 +0000128///
129/// This design avoids exposing scheduling boundaries to the DAG builder,
Andrew Trick17d35e52012-03-14 04:00:41 +0000130/// simplifying the DAG builder's support for "special" target instructions.
131/// At the same time the design allows target schedulers to operate across
Andrew Trickcb058d52012-03-14 04:00:38 +0000132/// scheduling boundaries, for example to bundle the boudary instructions
133/// without reordering them. This creates complexity, because the target
134/// scheduler must update the RegionBegin and RegionEnd positions cached by
135/// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler
136/// design would be to split blocks at scheduling boundaries, but LLVM has a
137/// general bias against block splitting purely for implementation simplicity.
Andrew Trick42b7a712012-01-17 06:55:03 +0000138bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000139 // Initialize the context of the pass.
140 MF = &mf;
141 MLI = &getAnalysis<MachineLoopInfo>();
142 MDT = &getAnalysis<MachineDominatorTree>();
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000143 PassConfig = &getAnalysis<TargetPassConfig>();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000144 AA = &getAnalysis<AliasAnalysis>();
145
Lang Hames907cc8f2012-01-27 22:36:19 +0000146 LIS = &getAnalysis<LiveIntervals>();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000147 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000148
149 // Select the scheduler, or set the default.
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000150 MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt;
151 if (Ctor == useDefaultMachineSched) {
152 // Get the default scheduler set by the target.
153 Ctor = MachineSchedRegistry::getDefault();
154 if (!Ctor) {
Andrew Trick17d35e52012-03-14 04:00:41 +0000155 Ctor = createConvergingSched;
Andrew Trickd04ec0c2012-03-09 00:52:20 +0000156 MachineSchedRegistry::setDefault(Ctor);
157 }
Andrew Trick96f678f2012-01-13 06:30:30 +0000158 }
159 // Instantiate the selected scheduler.
160 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
161
162 // Visit all machine basic blocks.
163 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
164 MBB != MBBEnd; ++MBB) {
165
Andrew Trick1fabd9f2012-03-09 08:02:51 +0000166 Scheduler->startBlock(MBB);
167
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000168 // Break the block into scheduling regions [I, RegionEnd), and schedule each
Andrew Trickfe4d6df2012-03-09 22:34:56 +0000169 // region as soon as it is discovered. RegionEnd points the the scheduling
170 // boundary at the bottom of the region. The DAG does not include RegionEnd,
171 // but the region does (i.e. the next RegionEnd is above the previous
172 // RegionBegin). If the current block has no terminator then RegionEnd ==
173 // MBB->end() for the bottom region.
174 //
175 // The Scheduler may insert instructions during either schedule() or
176 // exitRegion(), even for empty regions. So the local iterators 'I' and
177 // 'RegionEnd' are invalid across these calls.
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000178 unsigned RemainingCount = MBB->size();
Andrew Trick7799eb42012-03-09 03:46:39 +0000179 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
Andrew Trickfe4d6df2012-03-09 22:34:56 +0000180 RegionEnd != MBB->begin(); RegionEnd = Scheduler->begin()) {
Andrew Trick1fabd9f2012-03-09 08:02:51 +0000181 // Avoid decrementing RegionEnd for blocks with no terminator.
182 if (RegionEnd != MBB->end()
183 || TII->isSchedulingBoundary(llvm::prior(RegionEnd), MBB, *MF)) {
184 --RegionEnd;
185 // Count the boundary instruction.
186 --RemainingCount;
187 }
188
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000189 // The next region starts above the previous region. Look backward in the
190 // instruction stream until we find the nearest boundary.
191 MachineBasicBlock::iterator I = RegionEnd;
Andrew Trick7799eb42012-03-09 03:46:39 +0000192 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000193 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
194 break;
195 }
Andrew Trick47c14452012-03-07 05:21:52 +0000196 // Notify the scheduler of the region, even if we may skip scheduling
197 // it. Perhaps it still needs to be bundled.
198 Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount);
199
200 // Skip empty scheduling regions (0 or 1 schedulable instructions).
201 if (I == RegionEnd || I == llvm::prior(RegionEnd)) {
Andrew Trick47c14452012-03-07 05:21:52 +0000202 // Close the current region. Bundle the terminator if needed.
Andrew Trickfe4d6df2012-03-09 22:34:56 +0000203 // This invalidates 'RegionEnd' and 'I'.
Andrew Trick47c14452012-03-07 05:21:52 +0000204 Scheduler->exitRegion();
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000205 continue;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000206 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000207 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
Andrew Trick291411c2012-02-08 02:17:21 +0000208 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
209 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
210 else dbgs() << "End";
211 dbgs() << " Remaining: " << RemainingCount << "\n");
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000212
Andrew Trickd24da972012-03-09 03:46:42 +0000213 // Schedule a region: possibly reorder instructions.
Andrew Trickfe4d6df2012-03-09 22:34:56 +0000214 // This invalidates 'RegionEnd' and 'I'.
Andrew Trick953be892012-03-07 23:00:49 +0000215 Scheduler->schedule();
Andrew Trickd24da972012-03-09 03:46:42 +0000216
217 // Close the current region.
Andrew Trick47c14452012-03-07 05:21:52 +0000218 Scheduler->exitRegion();
219
220 // Scheduling has invalidated the current iterator 'I'. Ask the
221 // scheduler for the top of it's scheduled region.
222 RegionEnd = Scheduler->begin();
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000223 }
224 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick953be892012-03-07 23:00:49 +0000225 Scheduler->finishBlock();
Andrew Trick96f678f2012-01-13 06:30:30 +0000226 }
227 return true;
228}
229
Andrew Trick42b7a712012-01-17 06:55:03 +0000230void MachineScheduler::print(raw_ostream &O, const Module* m) const {
Andrew Trick96f678f2012-01-13 06:30:30 +0000231 // unimplemented
232}
233
Andrew Trick5edf2f02012-01-14 02:17:06 +0000234//===----------------------------------------------------------------------===//
Andrew Trick17d35e52012-03-14 04:00:41 +0000235// MachineSchedStrategy - Interface to a machine scheduling algorithm.
236//===----------------------------------------------------------------------===//
Andrew Trickc174eaf2012-03-08 01:41:12 +0000237
238namespace {
Andrew Trick17d35e52012-03-14 04:00:41 +0000239class ScheduleDAGMI;
Andrew Trickc174eaf2012-03-08 01:41:12 +0000240
Andrew Trick17d35e52012-03-14 04:00:41 +0000241/// MachineSchedStrategy - Interface used by ScheduleDAGMI to drive the selected
242/// scheduling algorithm.
243///
244/// If this works well and targets wish to reuse ScheduleDAGMI, we may expose it
245/// in ScheduleDAGInstrs.h
246class MachineSchedStrategy {
247public:
248 virtual ~MachineSchedStrategy() {}
249
250 /// Initialize the strategy after building the DAG for a new region.
251 virtual void initialize(ScheduleDAGMI *DAG) = 0;
252
253 /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to
254 /// schedule the node at the top of the unscheduled region. Otherwise it will
255 /// be scheduled at the bottom.
256 virtual SUnit *pickNode(bool &IsTopNode) = 0;
257
258 /// When all predecessor dependencies have been resolved, free this node for
259 /// top-down scheduling.
260 virtual void releaseTopNode(SUnit *SU) = 0;
261 /// When all successor dependencies have been resolved, free this node for
262 /// bottom-up scheduling.
263 virtual void releaseBottomNode(SUnit *SU) = 0;
264};
265} // namespace
266
267//===----------------------------------------------------------------------===//
268// ScheduleDAGMI - Base class for MachineInstr scheduling with LiveIntervals
269// preservation.
270//===----------------------------------------------------------------------===//
271
272namespace {
273/// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that schedules
274/// machine instructions while updating LiveIntervals.
275class ScheduleDAGMI : public ScheduleDAGInstrs {
276 AliasAnalysis *AA;
277 MachineSchedStrategy *SchedImpl;
278
279 /// The top of the unscheduled zone.
280 MachineBasicBlock::iterator CurrentTop;
281
282 /// The bottom of the unscheduled zone.
283 MachineBasicBlock::iterator CurrentBottom;
284public:
285 ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S):
286 ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
287 AA(C->AA), SchedImpl(S), CurrentTop(), CurrentBottom() {}
288
289 ~ScheduleDAGMI() {
290 delete SchedImpl;
291 }
292
293 MachineBasicBlock::iterator top() const { return CurrentTop; }
294 MachineBasicBlock::iterator bottom() const { return CurrentBottom; }
295
296 /// Implement ScheduleDAGInstrs interface.
Andrew Trickc174eaf2012-03-08 01:41:12 +0000297 void schedule();
298
Andrew Trickc174eaf2012-03-08 01:41:12 +0000299protected:
Andrew Trick17d35e52012-03-14 04:00:41 +0000300 void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos);
301
Andrew Trickc174eaf2012-03-08 01:41:12 +0000302 void releaseSucc(SUnit *SU, SDep *SuccEdge);
303 void releaseSuccessors(SUnit *SU);
Andrew Trick17d35e52012-03-14 04:00:41 +0000304 void releasePred(SUnit *SU, SDep *PredEdge);
305 void releasePredecessors(SUnit *SU);
Andrew Trickc174eaf2012-03-08 01:41:12 +0000306};
307} // namespace
308
309/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
310/// NumPredsLeft reaches zero, release the successor node.
Andrew Trick17d35e52012-03-14 04:00:41 +0000311void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000312 SUnit *SuccSU = SuccEdge->getSUnit();
313
314#ifndef NDEBUG
315 if (SuccSU->NumPredsLeft == 0) {
316 dbgs() << "*** Scheduling failed! ***\n";
317 SuccSU->dump(this);
318 dbgs() << " has been released too many times!\n";
319 llvm_unreachable(0);
320 }
321#endif
322 --SuccSU->NumPredsLeft;
323 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Andrew Trick17d35e52012-03-14 04:00:41 +0000324 SchedImpl->releaseTopNode(SuccSU);
Andrew Trickc174eaf2012-03-08 01:41:12 +0000325}
326
327/// releaseSuccessors - Call releaseSucc on each of SU's successors.
Andrew Trick17d35e52012-03-14 04:00:41 +0000328void ScheduleDAGMI::releaseSuccessors(SUnit *SU) {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000329 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
330 I != E; ++I) {
331 releaseSucc(SU, &*I);
332 }
333}
334
Andrew Trick17d35e52012-03-14 04:00:41 +0000335/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When
336/// NumSuccsLeft reaches zero, release the predecessor node.
337void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) {
338 SUnit *PredSU = PredEdge->getSUnit();
339
340#ifndef NDEBUG
341 if (PredSU->NumSuccsLeft == 0) {
342 dbgs() << "*** Scheduling failed! ***\n";
343 PredSU->dump(this);
344 dbgs() << " has been released too many times!\n";
345 llvm_unreachable(0);
346 }
347#endif
348 --PredSU->NumSuccsLeft;
349 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU)
350 SchedImpl->releaseBottomNode(PredSU);
351}
352
353/// releasePredecessors - Call releasePred on each of SU's predecessors.
354void ScheduleDAGMI::releasePredecessors(SUnit *SU) {
355 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
356 I != E; ++I) {
357 releasePred(SU, &*I);
358 }
359}
360
361void ScheduleDAGMI::moveInstruction(MachineInstr *MI,
362 MachineBasicBlock::iterator InsertPos) {
363 BB->splice(InsertPos, BB, MI);
364 LIS->handleMove(MI);
365 if (RegionBegin == InsertPos)
366 RegionBegin = MI;
367}
368
369/// schedule - Called back from MachineScheduler::runOnMachineFunction
370/// after setting up the current scheduling region.
371void ScheduleDAGMI::schedule() {
Andrew Trickc174eaf2012-03-08 01:41:12 +0000372 buildSchedGraph(AA);
373
374 DEBUG(dbgs() << "********** MI Scheduling **********\n");
375 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
376 SUnits[su].dumpAll(this));
377
378 if (ViewMISchedDAGs) viewGraph();
379
Andrew Trick17d35e52012-03-14 04:00:41 +0000380 SchedImpl->initialize(this);
381
382 // Release edges from the special Entry node or to the special Exit node.
Andrew Trickc174eaf2012-03-08 01:41:12 +0000383 releaseSuccessors(&EntrySU);
Andrew Trick17d35e52012-03-14 04:00:41 +0000384 releasePredecessors(&ExitSU);
Andrew Trickc174eaf2012-03-08 01:41:12 +0000385
386 // Release all DAG roots for scheduling.
387 for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
388 I != E; ++I) {
Andrew Trick17d35e52012-03-14 04:00:41 +0000389 // A SUnit is ready to top schedule if it has no predecessors.
Andrew Trickc174eaf2012-03-08 01:41:12 +0000390 if (I->Preds.empty())
Andrew Trick17d35e52012-03-14 04:00:41 +0000391 SchedImpl->releaseTopNode(&(*I));
392 // A SUnit is ready to bottom schedule if it has no successors.
393 if (I->Succs.empty())
394 SchedImpl->releaseBottomNode(&(*I));
Andrew Trickc174eaf2012-03-08 01:41:12 +0000395 }
396
Andrew Trick17d35e52012-03-14 04:00:41 +0000397 CurrentTop = RegionBegin;
398 CurrentBottom = RegionEnd;
399 bool IsTopNode = false;
400 while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) {
401 DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom")
402 << " Scheduling Instruction:\n"; SU->dump(this));
Andrew Trickc174eaf2012-03-08 01:41:12 +0000403
404 // Move the instruction to its new location in the instruction stream.
405 MachineInstr *MI = SU->getInstr();
Andrew Trickc174eaf2012-03-08 01:41:12 +0000406
Andrew Trick17d35e52012-03-14 04:00:41 +0000407 if (IsTopNode) {
408 assert(SU->isTopReady() && "node still has unscheduled dependencies");
409 if (&*CurrentTop == MI)
410 ++CurrentTop;
411 else
412 moveInstruction(MI, CurrentTop);
413 // Release dependent instructions for scheduling.
414 releaseSuccessors(SU);
415 }
416 else {
417 assert(SU->isBottomReady() && "node still has unscheduled dependencies");
418 if (&*llvm::prior(CurrentBottom) == MI)
419 --CurrentBottom;
420 else {
421 moveInstruction(MI, CurrentBottom);
422 CurrentBottom = MI;
423 }
424 // Release dependent instructions for scheduling.
425 releasePredecessors(SU);
426 }
427 SU->isScheduled = true;
Andrew Trickc174eaf2012-03-08 01:41:12 +0000428 }
Andrew Trick17d35e52012-03-14 04:00:41 +0000429 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
Andrew Trickc174eaf2012-03-08 01:41:12 +0000430}
431
432//===----------------------------------------------------------------------===//
Andrew Trick17d35e52012-03-14 04:00:41 +0000433// ConvergingScheduler - Implementation of the standard MachineSchedStrategy.
Andrew Trick42b7a712012-01-17 06:55:03 +0000434//===----------------------------------------------------------------------===//
435
436namespace {
Andrew Trick17d35e52012-03-14 04:00:41 +0000437/// ConvergingScheduler shrinks the unscheduled zone using heuristics to balance
438/// the schedule.
439class ConvergingScheduler : public MachineSchedStrategy {
440 ScheduleDAGMI *DAG;
Andrew Trick42b7a712012-01-17 06:55:03 +0000441
Andrew Trick17d35e52012-03-14 04:00:41 +0000442 unsigned NumTopReady;
443 unsigned NumBottomReady;
444
445public:
446 virtual void initialize(ScheduleDAGMI *dag) {
447 DAG = dag;
448
449 assert(!ForceTopDown || !ForceBottomUp &&
450 "-misched-topdown incompatible with -misched-bottomup");
451 }
452
453 virtual SUnit *pickNode(bool &IsTopNode) {
454 if (DAG->top() == DAG->bottom())
455 return NULL;
456
457 // As an initial placeholder heuristic, schedule in the direction that has
458 // the fewest choices.
459 SUnit *SU;
460 if (ForceTopDown || (!ForceBottomUp && NumTopReady <= NumBottomReady)) {
461 SU = DAG->getSUnit(DAG->top());
462 IsTopNode = true;
463 }
464 else {
465 SU = DAG->getSUnit(llvm::prior(DAG->bottom()));
466 IsTopNode = false;
467 }
468 if (SU->isTopReady()) {
469 assert(NumTopReady > 0 && "bad ready count");
470 --NumTopReady;
471 }
472 if (SU->isBottomReady()) {
473 assert(NumBottomReady > 0 && "bad ready count");
474 --NumBottomReady;
475 }
476 return SU;
477 }
478
479 virtual void releaseTopNode(SUnit *SU) {
480 ++NumTopReady;
481 }
482 virtual void releaseBottomNode(SUnit *SU) {
483 ++NumBottomReady;
484 }
Andrew Trick42b7a712012-01-17 06:55:03 +0000485};
486} // namespace
487
Andrew Trick17d35e52012-03-14 04:00:41 +0000488/// Create the standard converging machine scheduler. This will be used as the
489/// default scheduler if the target does not set a default.
490static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C) {
491 assert(!ForceTopDown || !ForceBottomUp &&
492 "-misched-topdown incompatible with -misched-bottomup");
493 return new ScheduleDAGMI(C, new ConvergingScheduler());
Andrew Trick42b7a712012-01-17 06:55:03 +0000494}
495static MachineSchedRegistry
Andrew Trick17d35e52012-03-14 04:00:41 +0000496ConvergingSchedRegistry("converge", "Standard converging scheduler.",
497 createConvergingSched);
Andrew Trick42b7a712012-01-17 06:55:03 +0000498
499//===----------------------------------------------------------------------===//
Andrew Trick5edf2f02012-01-14 02:17:06 +0000500// Machine Instruction Shuffler for Correctness Testing
501//===----------------------------------------------------------------------===//
502
Andrew Trick96f678f2012-01-13 06:30:30 +0000503#ifndef NDEBUG
504namespace {
Andrew Trick17d35e52012-03-14 04:00:41 +0000505/// Apply a less-than relation on the node order, which corresponds to the
506/// instruction order prior to scheduling. IsReverse implements greater-than.
507template<bool IsReverse>
508struct SUnitOrder {
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000509 bool operator()(SUnit *A, SUnit *B) const {
Andrew Trick17d35e52012-03-14 04:00:41 +0000510 if (IsReverse)
511 return A->NodeNum > B->NodeNum;
512 else
513 return A->NodeNum < B->NodeNum;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000514 }
515};
516
Andrew Trick96f678f2012-01-13 06:30:30 +0000517/// Reorder instructions as much as possible.
Andrew Trick17d35e52012-03-14 04:00:41 +0000518class InstructionShuffler : public MachineSchedStrategy {
519 bool IsAlternating;
520 bool IsTopDown;
521
522 // Using a less-than relation (SUnitOrder<false>) for the TopQ priority
523 // gives nodes with a higher number higher priority causing the latest
524 // instructions to be scheduled first.
525 PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false> >
526 TopQ;
527 // When scheduling bottom-up, use greater-than as the queue priority.
528 PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true> >
529 BottomQ;
Andrew Trick96f678f2012-01-13 06:30:30 +0000530public:
Andrew Trick17d35e52012-03-14 04:00:41 +0000531 InstructionShuffler(bool alternate, bool topdown)
532 : IsAlternating(alternate), IsTopDown(topdown) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000533
Andrew Trick17d35e52012-03-14 04:00:41 +0000534 virtual void initialize(ScheduleDAGMI *) {
535 TopQ.clear();
536 BottomQ.clear();
537 }
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000538
Andrew Trick17d35e52012-03-14 04:00:41 +0000539 /// Implement MachineSchedStrategy interface.
540 /// -----------------------------------------
541
542 virtual SUnit *pickNode(bool &IsTopNode) {
543 SUnit *SU;
544 if (IsTopDown) {
545 do {
546 if (TopQ.empty()) return NULL;
547 SU = TopQ.top();
548 TopQ.pop();
549 } while (SU->isScheduled);
550 IsTopNode = true;
551 }
552 else {
553 do {
554 if (BottomQ.empty()) return NULL;
555 SU = BottomQ.top();
556 BottomQ.pop();
557 } while (SU->isScheduled);
558 IsTopNode = false;
559 }
560 if (IsAlternating)
561 IsTopDown = !IsTopDown;
Andrew Trickc6cf11b2012-01-17 06:55:07 +0000562 return SU;
563 }
564
Andrew Trick17d35e52012-03-14 04:00:41 +0000565 virtual void releaseTopNode(SUnit *SU) {
566 TopQ.push(SU);
567 }
568 virtual void releaseBottomNode(SUnit *SU) {
569 BottomQ.push(SU);
Andrew Trick96f678f2012-01-13 06:30:30 +0000570 }
571};
572} // namespace
573
Andrew Trickc174eaf2012-03-08 01:41:12 +0000574static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) {
Andrew Trick17d35e52012-03-14 04:00:41 +0000575 bool Alternate = !ForceTopDown && !ForceBottomUp;
576 bool TopDown = !ForceBottomUp;
577 assert(TopDown || !ForceTopDown &&
578 "-misched-topdown incompatible with -misched-bottomup");
579 return new ScheduleDAGMI(C, new InstructionShuffler(Alternate, TopDown));
Andrew Trick96f678f2012-01-13 06:30:30 +0000580}
Andrew Trick17d35e52012-03-14 04:00:41 +0000581static MachineSchedRegistry ShufflerRegistry(
582 "shuffle", "Shuffle machine instructions alternating directions",
583 createInstructionShuffler);
Andrew Trick96f678f2012-01-13 06:30:30 +0000584#endif // !NDEBUG