Handle regmasks in FixupKills.
[oota-llvm.git] / lib / CodeGen / PostRASchedulerList.cpp
1 //===----- SchedulePostRAList.cpp - list 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 // This implements a top-down list scheduler, using standard algorithms.
11 // The basic approach uses a priority queue of available nodes to schedule.
12 // One at a time, nodes are taken from the priority queue (thus in priority
13 // order), checked for legality to schedule, and emitted if legal.
14 //
15 // Nodes may not be legal to schedule either due to structural hazards (e.g.
16 // pipeline or resource constraints) or because an input to the instruction has
17 // not completed execution.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #define DEBUG_TYPE "post-RA-sched"
22 #include "AntiDepBreaker.h"
23 #include "AggressiveAntiDepBreaker.h"
24 #include "CriticalAntiDepBreaker.h"
25 #include "RegisterClassInfo.h"
26 #include "ScheduleDAGInstrs.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/CodeGen/LatencyPriorityQueue.h"
29 #include "llvm/CodeGen/SchedulerRegistry.h"
30 #include "llvm/CodeGen/MachineDominators.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineLoopInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
36 #include "llvm/Analysis/AliasAnalysis.h"
37 #include "llvm/Target/TargetLowering.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetRegisterInfo.h"
41 #include "llvm/Target/TargetSubtargetInfo.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/ADT/BitVector.h"
47 #include "llvm/ADT/Statistic.h"
48 #include <set>
49 using namespace llvm;
50
51 STATISTIC(NumNoops, "Number of noops inserted");
52 STATISTIC(NumStalls, "Number of pipeline stalls");
53 STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
54
55 // Post-RA scheduling is enabled with
56 // TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
57 // override the target.
58 static cl::opt<bool>
59 EnablePostRAScheduler("post-RA-scheduler",
60                        cl::desc("Enable scheduling after register allocation"),
61                        cl::init(false), cl::Hidden);
62 static cl::opt<std::string>
63 EnableAntiDepBreaking("break-anti-dependencies",
64                       cl::desc("Break post-RA scheduling anti-dependencies: "
65                                "\"critical\", \"all\", or \"none\""),
66                       cl::init("none"), cl::Hidden);
67
68 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
69 static cl::opt<int>
70 DebugDiv("postra-sched-debugdiv",
71                       cl::desc("Debug control MBBs that are scheduled"),
72                       cl::init(0), cl::Hidden);
73 static cl::opt<int>
74 DebugMod("postra-sched-debugmod",
75                       cl::desc("Debug control MBBs that are scheduled"),
76                       cl::init(0), cl::Hidden);
77
78 AntiDepBreaker::~AntiDepBreaker() { }
79
80 namespace {
81   class PostRAScheduler : public MachineFunctionPass {
82     AliasAnalysis *AA;
83     const TargetInstrInfo *TII;
84     RegisterClassInfo RegClassInfo;
85
86   public:
87     static char ID;
88     PostRAScheduler() : MachineFunctionPass(ID) {}
89
90     void getAnalysisUsage(AnalysisUsage &AU) const {
91       AU.setPreservesCFG();
92       AU.addRequired<AliasAnalysis>();
93       AU.addRequired<TargetPassConfig>();
94       AU.addRequired<MachineDominatorTree>();
95       AU.addPreserved<MachineDominatorTree>();
96       AU.addRequired<MachineLoopInfo>();
97       AU.addPreserved<MachineLoopInfo>();
98       MachineFunctionPass::getAnalysisUsage(AU);
99     }
100
101     bool runOnMachineFunction(MachineFunction &Fn);
102   };
103   char PostRAScheduler::ID = 0;
104
105   class SchedulePostRATDList : public ScheduleDAGInstrs {
106     /// AvailableQueue - The priority queue to use for the available SUnits.
107     ///
108     LatencyPriorityQueue AvailableQueue;
109
110     /// PendingQueue - This contains all of the instructions whose operands have
111     /// been issued, but their results are not ready yet (due to the latency of
112     /// the operation).  Once the operands becomes available, the instruction is
113     /// added to the AvailableQueue.
114     std::vector<SUnit*> PendingQueue;
115
116     /// Topo - A topological ordering for SUnits.
117     ScheduleDAGTopologicalSort Topo;
118
119     /// HazardRec - The hazard recognizer to use.
120     ScheduleHazardRecognizer *HazardRec;
121
122     /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
123     AntiDepBreaker *AntiDepBreak;
124
125     /// AA - AliasAnalysis for making memory reference queries.
126     AliasAnalysis *AA;
127
128     /// KillIndices - The index of the most recent kill (proceding bottom-up),
129     /// or ~0u if the register is not live.
130     std::vector<unsigned> KillIndices;
131
132   public:
133     SchedulePostRATDList(
134       MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
135       AliasAnalysis *AA, const RegisterClassInfo&,
136       TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
137       SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
138
139     ~SchedulePostRATDList();
140
141     /// StartBlock - Initialize register live-range state for scheduling in
142     /// this block.
143     ///
144     void StartBlock(MachineBasicBlock *BB);
145
146     /// Schedule - Schedule the instruction range using list scheduling.
147     ///
148     void Schedule();
149
150     /// Observe - Update liveness information to account for the current
151     /// instruction, which will not be scheduled.
152     ///
153     void Observe(MachineInstr *MI, unsigned Count);
154
155     /// FinishBlock - Clean up register live-range state.
156     ///
157     void FinishBlock();
158
159     /// FixupKills - Fix register kill flags that have been made
160     /// invalid due to scheduling
161     ///
162     void FixupKills(MachineBasicBlock *MBB);
163
164   private:
165     void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
166     void ReleaseSuccessors(SUnit *SU);
167     void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
168     void ListScheduleTopDown();
169     void StartBlockForKills(MachineBasicBlock *BB);
170
171     // ToggleKillFlag - Toggle a register operand kill flag. Other
172     // adjustments may be made to the instruction if necessary. Return
173     // true if the operand has been deleted, false if not.
174     bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
175   };
176 }
177
178 char &llvm::PostRASchedulerID = PostRAScheduler::ID;
179
180 INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
181                 "Post RA top-down list latency scheduler", false, false)
182
183 SchedulePostRATDList::SchedulePostRATDList(
184   MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
185   AliasAnalysis *AA, const RegisterClassInfo &RCI,
186   TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
187   SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
188   : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
189     KillIndices(TRI->getNumRegs())
190 {
191   const TargetMachine &TM = MF.getTarget();
192   const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
193   HazardRec =
194     TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
195   AntiDepBreak =
196     ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
197      (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
198      ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
199       (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
200 }
201
202 SchedulePostRATDList::~SchedulePostRATDList() {
203   delete HazardRec;
204   delete AntiDepBreak;
205 }
206
207 bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
208   TII = Fn.getTarget().getInstrInfo();
209   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
210   MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
211   AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
212   TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
213
214   RegClassInfo.runOnMachineFunction(Fn);
215
216   // Check for explicit enable/disable of post-ra scheduling.
217   TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
218     TargetSubtargetInfo::ANTIDEP_NONE;
219   SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
220   if (EnablePostRAScheduler.getPosition() > 0) {
221     if (!EnablePostRAScheduler)
222       return false;
223   } else {
224     // Check that post-RA scheduling is enabled for this target.
225     // This may upgrade the AntiDepMode.
226     const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
227     if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
228                                   CriticalPathRCs))
229       return false;
230   }
231
232   // Check for antidep breaking override...
233   if (EnableAntiDepBreaking.getPosition() > 0) {
234     AntiDepMode = (EnableAntiDepBreaking == "all")
235       ? TargetSubtargetInfo::ANTIDEP_ALL
236       : ((EnableAntiDepBreaking == "critical")
237          ? TargetSubtargetInfo::ANTIDEP_CRITICAL
238          : TargetSubtargetInfo::ANTIDEP_NONE);
239   }
240
241   DEBUG(dbgs() << "PostRAScheduler\n");
242
243   SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
244                                  CriticalPathRCs);
245
246   // Loop over all of the basic blocks
247   for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
248        MBB != MBBe; ++MBB) {
249 #ifndef NDEBUG
250     // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
251     if (DebugDiv > 0) {
252       static int bbcnt = 0;
253       if (bbcnt++ % DebugDiv != DebugMod)
254         continue;
255       dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
256              << ":BB#" << MBB->getNumber() << " ***\n";
257     }
258 #endif
259
260     // Initialize register live-range state for scheduling in this block.
261     Scheduler.StartBlock(MBB);
262
263     // Schedule each sequence of instructions not interrupted by a label
264     // or anything else that effectively needs to shut down scheduling.
265     MachineBasicBlock::iterator Current = MBB->end();
266     unsigned Count = MBB->size(), CurrentCount = Count;
267     for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
268       MachineInstr *MI = llvm::prior(I);
269       if (TII->isSchedulingBoundary(MI, MBB, Fn)) {
270         Scheduler.Run(MBB, I, Current, CurrentCount);
271         Scheduler.EmitSchedule();
272         Current = MI;
273         CurrentCount = Count - 1;
274         Scheduler.Observe(MI, CurrentCount);
275       }
276       I = MI;
277       --Count;
278       if (MI->isBundle())
279         Count -= MI->getBundleSize();
280     }
281     assert(Count == 0 && "Instruction count mismatch!");
282     assert((MBB->begin() == Current || CurrentCount != 0) &&
283            "Instruction count mismatch!");
284     Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
285     Scheduler.EmitSchedule();
286
287     // Clean up register live-range state.
288     Scheduler.FinishBlock();
289
290     // Update register kills
291     Scheduler.FixupKills(MBB);
292   }
293
294   return true;
295 }
296
297 /// StartBlock - Initialize register live-range state for scheduling in
298 /// this block.
299 ///
300 void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
301   // Call the superclass.
302   ScheduleDAGInstrs::StartBlock(BB);
303
304   // Reset the hazard recognizer and anti-dep breaker.
305   HazardRec->Reset();
306   if (AntiDepBreak != NULL)
307     AntiDepBreak->StartBlock(BB);
308 }
309
310 /// Schedule - Schedule the instruction range using list scheduling.
311 ///
312 void SchedulePostRATDList::Schedule() {
313   // Build the scheduling graph.
314   BuildSchedGraph(AA);
315
316   if (AntiDepBreak != NULL) {
317     unsigned Broken =
318       AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
319                                           InsertPosIndex, DbgValues);
320
321     if (Broken != 0) {
322       // We made changes. Update the dependency graph.
323       // Theoretically we could update the graph in place:
324       // When a live range is changed to use a different register, remove
325       // the def's anti-dependence *and* output-dependence edges due to
326       // that register, and add new anti-dependence and output-dependence
327       // edges based on the next live range of the register.
328       SUnits.clear();
329       Sequence.clear();
330       EntrySU = SUnit();
331       ExitSU = SUnit();
332       BuildSchedGraph(AA);
333
334       NumFixedAnti += Broken;
335     }
336   }
337
338   DEBUG(dbgs() << "********** List Scheduling **********\n");
339   DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
340           SUnits[su].dumpAll(this));
341
342   AvailableQueue.initNodes(SUnits);
343   ListScheduleTopDown();
344   AvailableQueue.releaseState();
345 }
346
347 /// Observe - Update liveness information to account for the current
348 /// instruction, which will not be scheduled.
349 ///
350 void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
351   if (AntiDepBreak != NULL)
352     AntiDepBreak->Observe(MI, Count, InsertPosIndex);
353 }
354
355 /// FinishBlock - Clean up register live-range state.
356 ///
357 void SchedulePostRATDList::FinishBlock() {
358   if (AntiDepBreak != NULL)
359     AntiDepBreak->FinishBlock();
360
361   // Call the superclass.
362   ScheduleDAGInstrs::FinishBlock();
363 }
364
365 /// StartBlockForKills - Initialize register live-range state for updating kills
366 ///
367 void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
368   // Initialize the indices to indicate that no registers are live.
369   for (unsigned i = 0; i < TRI->getNumRegs(); ++i)
370     KillIndices[i] = ~0u;
371
372   // Determine the live-out physregs for this block.
373   if (!BB->empty() && BB->back().isReturn()) {
374     // In a return block, examine the function live-out regs.
375     for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
376            E = MRI.liveout_end(); I != E; ++I) {
377       unsigned Reg = *I;
378       KillIndices[Reg] = BB->size();
379       // Repeat, for all subregs.
380       for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
381            *Subreg; ++Subreg) {
382         KillIndices[*Subreg] = BB->size();
383       }
384     }
385   }
386   else {
387     // In a non-return block, examine the live-in regs of all successors.
388     for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
389            SE = BB->succ_end(); SI != SE; ++SI) {
390       for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
391              E = (*SI)->livein_end(); I != E; ++I) {
392         unsigned Reg = *I;
393         KillIndices[Reg] = BB->size();
394         // Repeat, for all subregs.
395         for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
396              *Subreg; ++Subreg) {
397           KillIndices[*Subreg] = BB->size();
398         }
399       }
400     }
401   }
402 }
403
404 bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
405                                           MachineOperand &MO) {
406   // Setting kill flag...
407   if (!MO.isKill()) {
408     MO.setIsKill(true);
409     return false;
410   }
411
412   // If MO itself is live, clear the kill flag...
413   if (KillIndices[MO.getReg()] != ~0u) {
414     MO.setIsKill(false);
415     return false;
416   }
417
418   // If any subreg of MO is live, then create an imp-def for that
419   // subreg and keep MO marked as killed.
420   MO.setIsKill(false);
421   bool AllDead = true;
422   const unsigned SuperReg = MO.getReg();
423   for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
424        *Subreg; ++Subreg) {
425     if (KillIndices[*Subreg] != ~0u) {
426       MI->addOperand(MachineOperand::CreateReg(*Subreg,
427                                                true  /*IsDef*/,
428                                                true  /*IsImp*/,
429                                                false /*IsKill*/,
430                                                false /*IsDead*/));
431       AllDead = false;
432     }
433   }
434
435   if(AllDead)
436     MO.setIsKill(true);
437   return false;
438 }
439
440 /// FixupKills - Fix the register kill flags, they may have been made
441 /// incorrect by instruction reordering.
442 ///
443 void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
444   DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
445
446   std::set<unsigned> killedRegs;
447   BitVector ReservedRegs = TRI->getReservedRegs(MF);
448
449   StartBlockForKills(MBB);
450
451   // Examine block from end to start...
452   unsigned Count = MBB->size();
453   for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
454        I != E; --Count) {
455     MachineInstr *MI = --I;
456     if (MI->isDebugValue())
457       continue;
458
459     // Update liveness.  Registers that are defed but not used in this
460     // instruction are now dead. Mark register and all subregs as they
461     // are completely defined.
462     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
463       MachineOperand &MO = MI->getOperand(i);
464       if (MO.isRegMask())
465         for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
466           if (MO.clobbersPhysReg(i))
467             KillIndices[i] = ~0u;
468       if (!MO.isReg()) continue;
469       unsigned Reg = MO.getReg();
470       if (Reg == 0) continue;
471       if (!MO.isDef()) continue;
472       // Ignore two-addr defs.
473       if (MI->isRegTiedToUseOperand(i)) continue;
474
475       KillIndices[Reg] = ~0u;
476
477       // Repeat for all subregs.
478       for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
479            *Subreg; ++Subreg) {
480         KillIndices[*Subreg] = ~0u;
481       }
482     }
483
484     // Examine all used registers and set/clear kill flag. When a
485     // register is used multiple times we only set the kill flag on
486     // the first use.
487     killedRegs.clear();
488     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
489       MachineOperand &MO = MI->getOperand(i);
490       if (!MO.isReg() || !MO.isUse()) continue;
491       unsigned Reg = MO.getReg();
492       if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
493
494       bool kill = false;
495       if (killedRegs.find(Reg) == killedRegs.end()) {
496         kill = true;
497         // A register is not killed if any subregs are live...
498         for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
499              *Subreg; ++Subreg) {
500           if (KillIndices[*Subreg] != ~0u) {
501             kill = false;
502             break;
503           }
504         }
505
506         // If subreg is not live, then register is killed if it became
507         // live in this instruction
508         if (kill)
509           kill = (KillIndices[Reg] == ~0u);
510       }
511
512       if (MO.isKill() != kill) {
513         DEBUG(dbgs() << "Fixing " << MO << " in ");
514         // Warning: ToggleKillFlag may invalidate MO.
515         ToggleKillFlag(MI, MO);
516         DEBUG(MI->dump());
517       }
518
519       killedRegs.insert(Reg);
520     }
521
522     // Mark any used register (that is not using undef) and subregs as
523     // now live...
524     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
525       MachineOperand &MO = MI->getOperand(i);
526       if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
527       unsigned Reg = MO.getReg();
528       if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
529
530       KillIndices[Reg] = Count;
531
532       for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
533            *Subreg; ++Subreg) {
534         KillIndices[*Subreg] = Count;
535       }
536     }
537   }
538 }
539
540 //===----------------------------------------------------------------------===//
541 //  Top-Down Scheduling
542 //===----------------------------------------------------------------------===//
543
544 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
545 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
546 void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
547   SUnit *SuccSU = SuccEdge->getSUnit();
548
549 #ifndef NDEBUG
550   if (SuccSU->NumPredsLeft == 0) {
551     dbgs() << "*** Scheduling failed! ***\n";
552     SuccSU->dump(this);
553     dbgs() << " has been released too many times!\n";
554     llvm_unreachable(0);
555   }
556 #endif
557   --SuccSU->NumPredsLeft;
558
559   // Standard scheduler algorithms will recompute the depth of the successor
560   // here as such:
561   //   SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
562   //
563   // However, we lazily compute node depth instead. Note that
564   // ScheduleNodeTopDown has already updated the depth of this node which causes
565   // all descendents to be marked dirty. Setting the successor depth explicitly
566   // here would cause depth to be recomputed for all its ancestors. If the
567   // successor is not yet ready (because of a transitively redundant edge) then
568   // this causes depth computation to be quadratic in the size of the DAG.
569
570   // If all the node's predecessors are scheduled, this node is ready
571   // to be scheduled. Ignore the special ExitSU node.
572   if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
573     PendingQueue.push_back(SuccSU);
574 }
575
576 /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
577 void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
578   for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
579        I != E; ++I) {
580     ReleaseSucc(SU, &*I);
581   }
582 }
583
584 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
585 /// count of its successors. If a successor pending count is zero, add it to
586 /// the Available queue.
587 void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
588   DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
589   DEBUG(SU->dump(this));
590
591   Sequence.push_back(SU);
592   assert(CurCycle >= SU->getDepth() &&
593          "Node scheduled above its depth!");
594   SU->setDepthToAtLeast(CurCycle);
595
596   ReleaseSuccessors(SU);
597   SU->isScheduled = true;
598   AvailableQueue.ScheduledNode(SU);
599 }
600
601 /// ListScheduleTopDown - The main loop of list scheduling for top-down
602 /// schedulers.
603 void SchedulePostRATDList::ListScheduleTopDown() {
604   unsigned CurCycle = 0;
605
606   // We're scheduling top-down but we're visiting the regions in
607   // bottom-up order, so we don't know the hazards at the start of a
608   // region. So assume no hazards (this should usually be ok as most
609   // blocks are a single region).
610   HazardRec->Reset();
611
612   // Release any successors of the special Entry node.
613   ReleaseSuccessors(&EntrySU);
614
615   // Add all leaves to Available queue.
616   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
617     // It is available if it has no predecessors.
618     bool available = SUnits[i].Preds.empty();
619     if (available) {
620       AvailableQueue.push(&SUnits[i]);
621       SUnits[i].isAvailable = true;
622     }
623   }
624
625   // In any cycle where we can't schedule any instructions, we must
626   // stall or emit a noop, depending on the target.
627   bool CycleHasInsts = false;
628
629   // While Available queue is not empty, grab the node with the highest
630   // priority. If it is not ready put it back.  Schedule the node.
631   std::vector<SUnit*> NotReady;
632   Sequence.reserve(SUnits.size());
633   while (!AvailableQueue.empty() || !PendingQueue.empty()) {
634     // Check to see if any of the pending instructions are ready to issue.  If
635     // so, add them to the available queue.
636     unsigned MinDepth = ~0u;
637     for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
638       if (PendingQueue[i]->getDepth() <= CurCycle) {
639         AvailableQueue.push(PendingQueue[i]);
640         PendingQueue[i]->isAvailable = true;
641         PendingQueue[i] = PendingQueue.back();
642         PendingQueue.pop_back();
643         --i; --e;
644       } else if (PendingQueue[i]->getDepth() < MinDepth)
645         MinDepth = PendingQueue[i]->getDepth();
646     }
647
648     DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
649
650     SUnit *FoundSUnit = 0;
651     bool HasNoopHazards = false;
652     while (!AvailableQueue.empty()) {
653       SUnit *CurSUnit = AvailableQueue.pop();
654
655       ScheduleHazardRecognizer::HazardType HT =
656         HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
657       if (HT == ScheduleHazardRecognizer::NoHazard) {
658         FoundSUnit = CurSUnit;
659         break;
660       }
661
662       // Remember if this is a noop hazard.
663       HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
664
665       NotReady.push_back(CurSUnit);
666     }
667
668     // Add the nodes that aren't ready back onto the available list.
669     if (!NotReady.empty()) {
670       AvailableQueue.push_all(NotReady);
671       NotReady.clear();
672     }
673
674     // If we found a node to schedule...
675     if (FoundSUnit) {
676       // ... schedule the node...
677       ScheduleNodeTopDown(FoundSUnit, CurCycle);
678       HazardRec->EmitInstruction(FoundSUnit);
679       CycleHasInsts = true;
680       if (HazardRec->atIssueLimit()) {
681         DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
682         HazardRec->AdvanceCycle();
683         ++CurCycle;
684         CycleHasInsts = false;
685       }
686     } else {
687       if (CycleHasInsts) {
688         DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
689         HazardRec->AdvanceCycle();
690       } else if (!HasNoopHazards) {
691         // Otherwise, we have a pipeline stall, but no other problem,
692         // just advance the current cycle and try again.
693         DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
694         HazardRec->AdvanceCycle();
695         ++NumStalls;
696       } else {
697         // Otherwise, we have no instructions to issue and we have instructions
698         // that will fault if we don't do this right.  This is the case for
699         // processors without pipeline interlocks and other cases.
700         DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
701         HazardRec->EmitNoop();
702         Sequence.push_back(0);   // NULL here means noop
703         ++NumNoops;
704       }
705
706       ++CurCycle;
707       CycleHasInsts = false;
708     }
709   }
710
711 #ifndef NDEBUG
712   VerifySchedule(/*isBottomUp=*/false);
713 #endif
714 }