MachineScheduler shouldn't use/preserve LiveDebugVariables.
[oota-llvm.git] / lib / CodeGen / MachineScheduler.cpp
1 //===- 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 "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/CodeGen/MachinePassRegistry.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #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
29 #include <queue>
30
31 using namespace llvm;
32
33 //===----------------------------------------------------------------------===//
34 // Machine Instruction Scheduling Pass and Registry
35 //===----------------------------------------------------------------------===//
36
37 namespace {
38 /// MachineScheduler runs after coalescing and before register allocation.
39 class MachineScheduler : public MachineFunctionPass {
40 public:
41   MachineFunction *MF;
42   const TargetInstrInfo *TII;
43   const MachineLoopInfo *MLI;
44   const MachineDominatorTree *MDT;
45   LiveIntervals *LIS;
46
47   MachineScheduler();
48
49   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50
51   virtual void releaseMemory() {}
52
53   virtual bool runOnMachineFunction(MachineFunction&);
54
55   virtual void print(raw_ostream &O, const Module* = 0) const;
56
57   static char ID; // Class identification, replacement for typeinfo
58 };
59 } // namespace
60
61 char MachineScheduler::ID = 0;
62
63 char &llvm::MachineSchedulerID = MachineScheduler::ID;
64
65 INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
66                       "Machine Instruction Scheduler", false, false)
67 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
68 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
69 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
70 INITIALIZE_PASS_END(MachineScheduler, "misched",
71                     "Machine Instruction Scheduler", false, false)
72
73 MachineScheduler::MachineScheduler()
74 : MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) {
75   initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
76 }
77
78 void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
79   AU.setPreservesCFG();
80   AU.addRequiredID(MachineDominatorsID);
81   AU.addRequired<MachineLoopInfo>();
82   AU.addRequired<AliasAnalysis>();
83   AU.addPreserved<AliasAnalysis>();
84   AU.addRequired<SlotIndexes>();
85   AU.addPreserved<SlotIndexes>();
86   AU.addRequired<LiveIntervals>();
87   AU.addPreserved<LiveIntervals>();
88   MachineFunctionPass::getAnalysisUsage(AU);
89 }
90
91 namespace {
92 /// MachineSchedRegistry provides a selection of available machine instruction
93 /// schedulers.
94 class MachineSchedRegistry : public MachinePassRegistryNode {
95 public:
96   typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineScheduler *);
97
98   // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
99   typedef ScheduleDAGCtor FunctionPassCtor;
100
101   static MachinePassRegistry Registry;
102
103   MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
104     : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
105     Registry.Add(this);
106   }
107   ~MachineSchedRegistry() { Registry.Remove(this); }
108
109   // Accessors.
110   //
111   MachineSchedRegistry *getNext() const {
112     return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
113   }
114   static MachineSchedRegistry *getList() {
115     return (MachineSchedRegistry *)Registry.getList();
116   }
117   static ScheduleDAGCtor getDefault() {
118     return (ScheduleDAGCtor)Registry.getDefault();
119   }
120   static void setDefault(ScheduleDAGCtor C) {
121     Registry.setDefault((MachinePassCtor)C);
122   }
123   static void setListener(MachinePassRegistryListener *L) {
124     Registry.setListener(L);
125   }
126 };
127 } // namespace
128
129 MachinePassRegistry MachineSchedRegistry::Registry;
130
131 static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P);
132
133 /// MachineSchedOpt allows command line selection of the scheduler.
134 static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
135                RegisterPassParser<MachineSchedRegistry> >
136 MachineSchedOpt("misched",
137                 cl::init(&createDefaultMachineSched), cl::Hidden,
138                 cl::desc("Machine instruction scheduler to use"));
139
140 //===----------------------------------------------------------------------===//
141 // Machine Instruction Scheduling Common Implementation
142 //===----------------------------------------------------------------------===//
143
144 namespace {
145 /// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules
146 /// machine instructions while updating LiveIntervals.
147 class ScheduleTopDownLive : public ScheduleDAGInstrs {
148 protected:
149   MachineScheduler *Pass;
150 public:
151   ScheduleTopDownLive(MachineScheduler *P):
152     ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
153
154   /// ScheduleDAGInstrs callback.
155   void Schedule();
156
157   /// Interface implemented by the selected top-down liveinterval scheduler.
158   ///
159   /// Pick the next node to schedule, or return NULL.
160   virtual SUnit *pickNode() = 0;
161
162   /// When all preceeding dependencies have been resolved, free this node for
163   /// scheduling.
164   virtual void releaseNode(SUnit *SU) = 0;
165
166 protected:
167   void releaseSucc(SUnit *SU, SDep *SuccEdge);
168   void releaseSuccessors(SUnit *SU);
169 };
170 } // namespace
171
172 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
173 /// NumPredsLeft reaches zero, release the successor node.
174 void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) {
175   SUnit *SuccSU = SuccEdge->getSUnit();
176
177 #ifndef NDEBUG
178   if (SuccSU->NumPredsLeft == 0) {
179     dbgs() << "*** Scheduling failed! ***\n";
180     SuccSU->dump(this);
181     dbgs() << " has been released too many times!\n";
182     llvm_unreachable(0);
183   }
184 #endif
185   --SuccSU->NumPredsLeft;
186   if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
187     releaseNode(SuccSU);
188 }
189
190 /// releaseSuccessors - Call releaseSucc on each of SU's successors.
191 void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) {
192   for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
193        I != E; ++I) {
194     releaseSucc(SU, &*I);
195   }
196 }
197
198 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
199 /// time to do some work.
200 void ScheduleTopDownLive::Schedule() {
201   BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
202
203   DEBUG(dbgs() << "********** MI Scheduling **********\n");
204   DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
205           SUnits[su].dumpAll(this));
206
207   // Release any successors of the special Entry node. It is currently unused,
208   // but we keep up appearances.
209   releaseSuccessors(&EntrySU);
210
211   // Release all DAG roots for scheduling.
212   for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end();
213        I != E; ++I) {
214     // A SUnit is ready to schedule if it has no predecessors.
215     if (I->Preds.empty())
216       releaseNode(&(*I));
217   }
218
219   InsertPos = Begin;
220   while (SUnit *SU = pickNode()) {
221     DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this));
222
223     // Move the instruction to its new location in the instruction stream.
224     MachineInstr *MI = SU->getInstr();
225     if (&*InsertPos == MI)
226       ++InsertPos;
227     else {
228       BB->splice(InsertPos, BB, MI);
229       Pass->LIS->handleMove(MI);
230       if (Begin == InsertPos)
231         Begin = MI;
232     }
233
234     // Release dependent instructions for scheduling.
235     releaseSuccessors(SU);
236   }
237 }
238
239 bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
240   // Initialize the context of the pass.
241   MF = &mf;
242   MLI = &getAnalysis<MachineLoopInfo>();
243   MDT = &getAnalysis<MachineDominatorTree>();
244   LIS = &getAnalysis<LiveIntervals>();
245   TII = MF->getTarget().getInstrInfo();
246
247   // Select the scheduler, or set the default.
248   MachineSchedRegistry::ScheduleDAGCtor Ctor =
249     MachineSchedRegistry::getDefault();
250   if (!Ctor) {
251     Ctor = MachineSchedOpt;
252     MachineSchedRegistry::setDefault(Ctor);
253   }
254   // Instantiate the selected scheduler.
255   OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
256
257   // Visit all machine basic blocks.
258   for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
259        MBB != MBBEnd; ++MBB) {
260
261     // Break the block into scheduling regions [I, RegionEnd), and schedule each
262     // region as soon as it is discovered.
263     unsigned RemainingCount = MBB->size();
264     for(MachineBasicBlock::iterator RegionEnd = MBB->end();
265         RegionEnd != MBB->begin();) {
266       // The next region starts above the previous region. Look backward in the
267       // instruction stream until we find the nearest boundary.
268       MachineBasicBlock::iterator I = RegionEnd;
269       for(;I != MBB->begin(); --I, --RemainingCount) {
270         if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
271           break;
272       }
273       if (I == RegionEnd) {
274         // Skip empty scheduling regions.
275         RegionEnd = llvm::prior(RegionEnd);
276         --RemainingCount;
277         continue;
278       }
279       // Skip regions with one instruction.
280       if (I == llvm::prior(RegionEnd)) {
281         RegionEnd = llvm::prior(RegionEnd);
282         continue;
283       }
284       DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
285             << ":BB#" << MBB->getNumber() << "\n  From: " << *I << "    To: ";
286             if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
287             else dbgs() << "End";
288             dbgs() << " Remaining: " << RemainingCount << "\n");
289
290       // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
291       // to our Schedule() method.
292       Scheduler->Run(MBB, I, RegionEnd, MBB->size());
293       RegionEnd = Scheduler->Begin;
294     }
295     assert(RemainingCount == 0 && "Instruction count mismatch!");
296   }
297   return true;
298 }
299
300 void MachineScheduler::print(raw_ostream &O, const Module* m) const {
301   // unimplemented
302 }
303
304 //===----------------------------------------------------------------------===//
305 // Placeholder for extending the machine instruction scheduler.
306 //===----------------------------------------------------------------------===//
307
308 namespace {
309 class DefaultMachineScheduler : public ScheduleDAGInstrs {
310   MachineScheduler *Pass;
311 public:
312   DefaultMachineScheduler(MachineScheduler *P):
313     ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
314
315   /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
316   /// time to do some work.
317   void Schedule();
318 };
319 } // namespace
320
321 static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P) {
322   return new DefaultMachineScheduler(P);
323 }
324 static MachineSchedRegistry
325 SchedDefaultRegistry("default", "Activate the scheduler pass, "
326                      "but don't reorder instructions",
327                      createDefaultMachineSched);
328
329
330 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
331 /// time to do some work.
332 void DefaultMachineScheduler::Schedule() {
333   BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
334
335   DEBUG(dbgs() << "********** MI Scheduling **********\n");
336   DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
337           SUnits[su].dumpAll(this));
338
339   // TODO: Put interesting things here.
340   //
341   // When this is fully implemented, it will become a subclass of
342   // ScheduleTopDownLive. So this driver will disappear.
343 }
344
345 //===----------------------------------------------------------------------===//
346 // Machine Instruction Shuffler for Correctness Testing
347 //===----------------------------------------------------------------------===//
348
349 #ifndef NDEBUG
350 namespace {
351 // Nodes with a higher number have lower priority. This way we attempt to
352 // schedule the latest instructions earliest.
353 //
354 // TODO: Relies on the property of the BuildSchedGraph that results in SUnits
355 // being ordered in sequence bottom-up. This will be formalized, probably be
356 // constructing SUnits in a prepass.
357 struct ShuffleSUnitOrder {
358   bool operator()(SUnit *A, SUnit *B) const {
359     return A->NodeNum > B->NodeNum;
360   }
361 };
362
363 /// Reorder instructions as much as possible.
364 class InstructionShuffler : public ScheduleTopDownLive {
365   std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue;
366 public:
367   InstructionShuffler(MachineScheduler *P):
368     ScheduleTopDownLive(P) {}
369
370   /// ScheduleTopDownLive Interface
371
372   virtual SUnit *pickNode() {
373     if (Queue.empty()) return NULL;
374     SUnit *SU = Queue.top();
375     Queue.pop();
376     return SU;
377   }
378
379   virtual void releaseNode(SUnit *SU) {
380     Queue.push(SU);
381   }
382 };
383 } // namespace
384
385 static ScheduleDAGInstrs *createInstructionShuffler(MachineScheduler *P) {
386   return new InstructionShuffler(P);
387 }
388 static MachineSchedRegistry ShufflerRegistry("shuffle",
389                                              "Shuffle machine instructions",
390                                              createInstructionShuffler);
391 #endif // !NDEBUG