d8afbbbbe7d5556f675cb2e40e15ba08a6469b34
[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 "llvm/CodeGen/Passes.h"
23 #include "AggressiveAntiDepBreaker.h"
24 #include "AntiDepBreaker.h"
25 #include "CriticalAntiDepBreaker.h"
26 #include "llvm/ADT/BitVector.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Analysis/AliasAnalysis.h"
29 #include "llvm/CodeGen/LatencyPriorityQueue.h"
30 #include "llvm/CodeGen/MachineDominators.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineLoopInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/RegisterClassInfo.h"
37 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
38 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
39 #include "llvm/CodeGen/SchedulerRegistry.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetRegisterInfo.h"
48 #include "llvm/Target/TargetSubtargetInfo.h"
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     const TargetInstrInfo *TII;
83     RegisterClassInfo RegClassInfo;
84
85   public:
86     static char ID;
87     PostRAScheduler() : MachineFunctionPass(ID) {}
88
89     void getAnalysisUsage(AnalysisUsage &AU) const {
90       AU.setPreservesCFG();
91       AU.addRequired<AliasAnalysis>();
92       AU.addRequired<TargetPassConfig>();
93       AU.addRequired<MachineDominatorTree>();
94       AU.addPreserved<MachineDominatorTree>();
95       AU.addRequired<MachineLoopInfo>();
96       AU.addPreserved<MachineLoopInfo>();
97       MachineFunctionPass::getAnalysisUsage(AU);
98     }
99
100     bool runOnMachineFunction(MachineFunction &Fn);
101   };
102   char PostRAScheduler::ID = 0;
103
104   class SchedulePostRATDList : public ScheduleDAGInstrs {
105     /// AvailableQueue - The priority queue to use for the available SUnits.
106     ///
107     LatencyPriorityQueue AvailableQueue;
108
109     /// PendingQueue - This contains all of the instructions whose operands have
110     /// been issued, but their results are not ready yet (due to the latency of
111     /// the operation).  Once the operands becomes available, the instruction is
112     /// added to the AvailableQueue.
113     std::vector<SUnit*> PendingQueue;
114
115     /// HazardRec - The hazard recognizer to use.
116     ScheduleHazardRecognizer *HazardRec;
117
118     /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
119     AntiDepBreaker *AntiDepBreak;
120
121     /// AA - AliasAnalysis for making memory reference queries.
122     AliasAnalysis *AA;
123
124     /// LiveRegs - true if the register is live.
125     BitVector LiveRegs;
126
127     /// The schedule. Null SUnit*'s represent noop instructions.
128     std::vector<SUnit*> Sequence;
129
130     /// The index in BB of RegionEnd.
131     ///
132     /// This is the instruction number from the top of the current block, not
133     /// the SlotIndex. It is only used by the AntiDepBreaker.
134     unsigned EndIndex;
135
136   public:
137     SchedulePostRATDList(
138       MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
139       AliasAnalysis *AA, const RegisterClassInfo&,
140       TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
141       SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
142
143     ~SchedulePostRATDList();
144
145     /// startBlock - Initialize register live-range state for scheduling in
146     /// this block.
147     ///
148     void startBlock(MachineBasicBlock *BB);
149
150     // Set the index of RegionEnd within the current BB.
151     void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
152
153     /// Initialize the scheduler state for the next scheduling region.
154     virtual void enterRegion(MachineBasicBlock *bb,
155                              MachineBasicBlock::iterator begin,
156                              MachineBasicBlock::iterator end,
157                              unsigned regioninstrs);
158
159     /// Notify that the scheduler has finished scheduling the current region.
160     virtual void exitRegion();
161
162     /// Schedule - Schedule the instruction range using list scheduling.
163     ///
164     void schedule();
165
166     void EmitSchedule();
167
168     /// Observe - Update liveness information to account for the current
169     /// instruction, which will not be scheduled.
170     ///
171     void Observe(MachineInstr *MI, unsigned Count);
172
173     /// finishBlock - Clean up register live-range state.
174     ///
175     void finishBlock();
176
177     /// FixupKills - Fix register kill flags that have been made
178     /// invalid due to scheduling
179     ///
180     void FixupKills(MachineBasicBlock *MBB);
181
182   private:
183     void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
184     void ReleaseSuccessors(SUnit *SU);
185     void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
186     void ListScheduleTopDown();
187     void StartBlockForKills(MachineBasicBlock *BB);
188
189     // ToggleKillFlag - Toggle a register operand kill flag. Other
190     // adjustments may be made to the instruction if necessary. Return
191     // true if the operand has been deleted, false if not.
192     bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
193
194     void dumpSchedule() const;
195     void emitNoop(unsigned CurCycle);
196   };
197 }
198
199 char &llvm::PostRASchedulerID = PostRAScheduler::ID;
200
201 INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
202                 "Post RA top-down list latency scheduler", false, false)
203
204 SchedulePostRATDList::SchedulePostRATDList(
205   MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
206   AliasAnalysis *AA, const RegisterClassInfo &RCI,
207   TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
208   SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
209   : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA),
210     LiveRegs(TRI->getNumRegs()), EndIndex(0)
211 {
212   const TargetMachine &TM = MF.getTarget();
213   const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
214   HazardRec =
215     TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
216
217   assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
218           MRI.tracksLiveness()) &&
219          "Live-ins must be accurate for anti-dependency breaking");
220   AntiDepBreak =
221     ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
222      (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
223      ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
224       (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
225 }
226
227 SchedulePostRATDList::~SchedulePostRATDList() {
228   delete HazardRec;
229   delete AntiDepBreak;
230 }
231
232 /// Initialize state associated with the next scheduling region.
233 void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
234                  MachineBasicBlock::iterator begin,
235                  MachineBasicBlock::iterator end,
236                  unsigned regioninstrs) {
237   ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
238   Sequence.clear();
239 }
240
241 /// Print the schedule before exiting the region.
242 void SchedulePostRATDList::exitRegion() {
243   DEBUG({
244       dbgs() << "*** Final schedule ***\n";
245       dumpSchedule();
246       dbgs() << '\n';
247     });
248   ScheduleDAGInstrs::exitRegion();
249 }
250
251 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
252 /// dumpSchedule - dump the scheduled Sequence.
253 void SchedulePostRATDList::dumpSchedule() const {
254   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
255     if (SUnit *SU = Sequence[i])
256       SU->dump(this);
257     else
258       dbgs() << "**** NOOP ****\n";
259   }
260 }
261 #endif
262
263 bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
264   TII = Fn.getTarget().getInstrInfo();
265   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
266   MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
267   AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
268   TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
269
270   RegClassInfo.runOnMachineFunction(Fn);
271
272   // Check for explicit enable/disable of post-ra scheduling.
273   TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
274     TargetSubtargetInfo::ANTIDEP_NONE;
275   SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
276   if (EnablePostRAScheduler.getPosition() > 0) {
277     if (!EnablePostRAScheduler)
278       return false;
279   } else {
280     // Check that post-RA scheduling is enabled for this target.
281     // This may upgrade the AntiDepMode.
282     const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
283     if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
284                                   CriticalPathRCs))
285       return false;
286   }
287
288   // Check for antidep breaking override...
289   if (EnableAntiDepBreaking.getPosition() > 0) {
290     AntiDepMode = (EnableAntiDepBreaking == "all")
291       ? TargetSubtargetInfo::ANTIDEP_ALL
292       : ((EnableAntiDepBreaking == "critical")
293          ? TargetSubtargetInfo::ANTIDEP_CRITICAL
294          : TargetSubtargetInfo::ANTIDEP_NONE);
295   }
296
297   DEBUG(dbgs() << "PostRAScheduler\n");
298
299   SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
300                                  CriticalPathRCs);
301
302   // Loop over all of the basic blocks
303   for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
304        MBB != MBBe; ++MBB) {
305 #ifndef NDEBUG
306     // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
307     if (DebugDiv > 0) {
308       static int bbcnt = 0;
309       if (bbcnt++ % DebugDiv != DebugMod)
310         continue;
311       dbgs() << "*** DEBUG scheduling " << Fn.getName()
312              << ":BB#" << MBB->getNumber() << " ***\n";
313     }
314 #endif
315
316     // Initialize register live-range state for scheduling in this block.
317     Scheduler.startBlock(MBB);
318
319     // Schedule each sequence of instructions not interrupted by a label
320     // or anything else that effectively needs to shut down scheduling.
321     MachineBasicBlock::iterator Current = MBB->end();
322     unsigned Count = MBB->size(), CurrentCount = Count;
323     for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
324       MachineInstr *MI = llvm::prior(I);
325       --Count;
326       // Calls are not scheduling boundaries before register allocation, but
327       // post-ra we don't gain anything by scheduling across calls since we
328       // don't need to worry about register pressure.
329       if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
330         Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count);
331         Scheduler.setEndIndex(CurrentCount);
332         Scheduler.schedule();
333         Scheduler.exitRegion();
334         Scheduler.EmitSchedule();
335         Current = MI;
336         CurrentCount = Count;
337         Scheduler.Observe(MI, CurrentCount);
338       }
339       I = MI;
340       if (MI->isBundle())
341         Count -= MI->getBundleSize();
342     }
343     assert(Count == 0 && "Instruction count mismatch!");
344     assert((MBB->begin() == Current || CurrentCount != 0) &&
345            "Instruction count mismatch!");
346     Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
347     Scheduler.setEndIndex(CurrentCount);
348     Scheduler.schedule();
349     Scheduler.exitRegion();
350     Scheduler.EmitSchedule();
351
352     // Clean up register live-range state.
353     Scheduler.finishBlock();
354
355     // Update register kills
356     Scheduler.FixupKills(MBB);
357   }
358
359   return true;
360 }
361
362 /// StartBlock - Initialize register live-range state for scheduling in
363 /// this block.
364 ///
365 void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
366   // Call the superclass.
367   ScheduleDAGInstrs::startBlock(BB);
368
369   // Reset the hazard recognizer and anti-dep breaker.
370   HazardRec->Reset();
371   if (AntiDepBreak != NULL)
372     AntiDepBreak->StartBlock(BB);
373 }
374
375 /// Schedule - Schedule the instruction range using list scheduling.
376 ///
377 void SchedulePostRATDList::schedule() {
378   // Build the scheduling graph.
379   buildSchedGraph(AA);
380
381   if (AntiDepBreak != NULL) {
382     unsigned Broken =
383       AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
384                                           EndIndex, DbgValues);
385
386     if (Broken != 0) {
387       // We made changes. Update the dependency graph.
388       // Theoretically we could update the graph in place:
389       // When a live range is changed to use a different register, remove
390       // the def's anti-dependence *and* output-dependence edges due to
391       // that register, and add new anti-dependence and output-dependence
392       // edges based on the next live range of the register.
393       ScheduleDAG::clearDAG();
394       buildSchedGraph(AA);
395
396       NumFixedAnti += Broken;
397     }
398   }
399
400   DEBUG(dbgs() << "********** List Scheduling **********\n");
401   DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
402           SUnits[su].dumpAll(this));
403
404   AvailableQueue.initNodes(SUnits);
405   ListScheduleTopDown();
406   AvailableQueue.releaseState();
407 }
408
409 /// Observe - Update liveness information to account for the current
410 /// instruction, which will not be scheduled.
411 ///
412 void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
413   if (AntiDepBreak != NULL)
414     AntiDepBreak->Observe(MI, Count, EndIndex);
415 }
416
417 /// FinishBlock - Clean up register live-range state.
418 ///
419 void SchedulePostRATDList::finishBlock() {
420   if (AntiDepBreak != NULL)
421     AntiDepBreak->FinishBlock();
422
423   // Call the superclass.
424   ScheduleDAGInstrs::finishBlock();
425 }
426
427 /// StartBlockForKills - Initialize register live-range state for updating kills
428 ///
429 void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
430   // Start with no live registers.
431   LiveRegs.reset();
432
433   // Examine the live-in regs of all successors.
434   for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
435        SE = BB->succ_end(); SI != SE; ++SI) {
436     for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
437          E = (*SI)->livein_end(); I != E; ++I) {
438       unsigned Reg = *I;
439       // Repeat, for reg and all subregs.
440       for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
441            SubRegs.isValid(); ++SubRegs)
442         LiveRegs.set(*SubRegs);
443     }
444   }
445 }
446
447 bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
448                                           MachineOperand &MO) {
449   // Setting kill flag...
450   if (!MO.isKill()) {
451     MO.setIsKill(true);
452     return false;
453   }
454
455   // If MO itself is live, clear the kill flag...
456   if (LiveRegs.test(MO.getReg())) {
457     MO.setIsKill(false);
458     return false;
459   }
460
461   // If any subreg of MO is live, then create an imp-def for that
462   // subreg and keep MO marked as killed.
463   MO.setIsKill(false);
464   bool AllDead = true;
465   const unsigned SuperReg = MO.getReg();
466   MachineInstrBuilder MIB(MF, MI);
467   for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
468     if (LiveRegs.test(*SubRegs)) {
469       MIB.addReg(*SubRegs, RegState::ImplicitDefine);
470       AllDead = false;
471     }
472   }
473
474   if(AllDead)
475     MO.setIsKill(true);
476   return false;
477 }
478
479 /// FixupKills - Fix the register kill flags, they may have been made
480 /// incorrect by instruction reordering.
481 ///
482 void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
483   DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
484
485   BitVector killedRegs(TRI->getNumRegs());
486
487   StartBlockForKills(MBB);
488
489   // Examine block from end to start...
490   unsigned Count = MBB->size();
491   for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
492        I != E; --Count) {
493     MachineInstr *MI = --I;
494     if (MI->isDebugValue())
495       continue;
496
497     // Update liveness.  Registers that are defed but not used in this
498     // instruction are now dead. Mark register and all subregs as they
499     // are completely defined.
500     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
501       MachineOperand &MO = MI->getOperand(i);
502       if (MO.isRegMask())
503         LiveRegs.clearBitsNotInMask(MO.getRegMask());
504       if (!MO.isReg()) continue;
505       unsigned Reg = MO.getReg();
506       if (Reg == 0) continue;
507       if (!MO.isDef()) continue;
508       // Ignore two-addr defs.
509       if (MI->isRegTiedToUseOperand(i)) continue;
510
511       // Repeat for reg and all subregs.
512       for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
513            SubRegs.isValid(); ++SubRegs)
514         LiveRegs.reset(*SubRegs);
515     }
516
517     // Examine all used registers and set/clear kill flag. When a
518     // register is used multiple times we only set the kill flag on
519     // the first use. Don't set kill flags on undef operands.
520     killedRegs.reset();
521     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
522       MachineOperand &MO = MI->getOperand(i);
523       if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
524       unsigned Reg = MO.getReg();
525       if ((Reg == 0) || MRI.isReserved(Reg)) continue;
526
527       bool kill = false;
528       if (!killedRegs.test(Reg)) {
529         kill = true;
530         // A register is not killed if any subregs are live...
531         for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
532           if (LiveRegs.test(*SubRegs)) {
533             kill = false;
534             break;
535           }
536         }
537
538         // If subreg is not live, then register is killed if it became
539         // live in this instruction
540         if (kill)
541           kill = !LiveRegs.test(Reg);
542       }
543
544       if (MO.isKill() != kill) {
545         DEBUG(dbgs() << "Fixing " << MO << " in ");
546         // Warning: ToggleKillFlag may invalidate MO.
547         ToggleKillFlag(MI, MO);
548         DEBUG(MI->dump());
549       }
550
551       killedRegs.set(Reg);
552     }
553
554     // Mark any used register (that is not using undef) and subregs as
555     // now live...
556     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
557       MachineOperand &MO = MI->getOperand(i);
558       if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
559       unsigned Reg = MO.getReg();
560       if ((Reg == 0) || MRI.isReserved(Reg)) continue;
561
562       for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
563            SubRegs.isValid(); ++SubRegs)
564         LiveRegs.set(*SubRegs);
565     }
566   }
567 }
568
569 //===----------------------------------------------------------------------===//
570 //  Top-Down Scheduling
571 //===----------------------------------------------------------------------===//
572
573 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
574 /// the PendingQueue if the count reaches zero.
575 void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
576   SUnit *SuccSU = SuccEdge->getSUnit();
577
578   if (SuccEdge->isWeak()) {
579     --SuccSU->WeakPredsLeft;
580     return;
581   }
582 #ifndef NDEBUG
583   if (SuccSU->NumPredsLeft == 0) {
584     dbgs() << "*** Scheduling failed! ***\n";
585     SuccSU->dump(this);
586     dbgs() << " has been released too many times!\n";
587     llvm_unreachable(0);
588   }
589 #endif
590   --SuccSU->NumPredsLeft;
591
592   // Standard scheduler algorithms will recompute the depth of the successor
593   // here as such:
594   //   SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
595   //
596   // However, we lazily compute node depth instead. Note that
597   // ScheduleNodeTopDown has already updated the depth of this node which causes
598   // all descendents to be marked dirty. Setting the successor depth explicitly
599   // here would cause depth to be recomputed for all its ancestors. If the
600   // successor is not yet ready (because of a transitively redundant edge) then
601   // this causes depth computation to be quadratic in the size of the DAG.
602
603   // If all the node's predecessors are scheduled, this node is ready
604   // to be scheduled. Ignore the special ExitSU node.
605   if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
606     PendingQueue.push_back(SuccSU);
607 }
608
609 /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
610 void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
611   for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
612        I != E; ++I) {
613     ReleaseSucc(SU, &*I);
614   }
615 }
616
617 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
618 /// count of its successors. If a successor pending count is zero, add it to
619 /// the Available queue.
620 void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
621   DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
622   DEBUG(SU->dump(this));
623
624   Sequence.push_back(SU);
625   assert(CurCycle >= SU->getDepth() &&
626          "Node scheduled above its depth!");
627   SU->setDepthToAtLeast(CurCycle);
628
629   ReleaseSuccessors(SU);
630   SU->isScheduled = true;
631   AvailableQueue.scheduledNode(SU);
632 }
633
634 /// emitNoop - Add a noop to the current instruction sequence.
635 void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
636   DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
637   HazardRec->EmitNoop();
638   Sequence.push_back(0);   // NULL here means noop
639   ++NumNoops;
640 }
641
642 /// ListScheduleTopDown - The main loop of list scheduling for top-down
643 /// schedulers.
644 void SchedulePostRATDList::ListScheduleTopDown() {
645   unsigned CurCycle = 0;
646
647   // We're scheduling top-down but we're visiting the regions in
648   // bottom-up order, so we don't know the hazards at the start of a
649   // region. So assume no hazards (this should usually be ok as most
650   // blocks are a single region).
651   HazardRec->Reset();
652
653   // Release any successors of the special Entry node.
654   ReleaseSuccessors(&EntrySU);
655
656   // Add all leaves to Available queue.
657   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
658     // It is available if it has no predecessors.
659     if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
660       AvailableQueue.push(&SUnits[i]);
661       SUnits[i].isAvailable = true;
662     }
663   }
664
665   // In any cycle where we can't schedule any instructions, we must
666   // stall or emit a noop, depending on the target.
667   bool CycleHasInsts = false;
668
669   // While Available queue is not empty, grab the node with the highest
670   // priority. If it is not ready put it back.  Schedule the node.
671   std::vector<SUnit*> NotReady;
672   Sequence.reserve(SUnits.size());
673   while (!AvailableQueue.empty() || !PendingQueue.empty()) {
674     // Check to see if any of the pending instructions are ready to issue.  If
675     // so, add them to the available queue.
676     unsigned MinDepth = ~0u;
677     for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
678       if (PendingQueue[i]->getDepth() <= CurCycle) {
679         AvailableQueue.push(PendingQueue[i]);
680         PendingQueue[i]->isAvailable = true;
681         PendingQueue[i] = PendingQueue.back();
682         PendingQueue.pop_back();
683         --i; --e;
684       } else if (PendingQueue[i]->getDepth() < MinDepth)
685         MinDepth = PendingQueue[i]->getDepth();
686     }
687
688     DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
689
690     SUnit *FoundSUnit = 0, *NotPreferredSUnit = 0;
691     bool HasNoopHazards = false;
692     while (!AvailableQueue.empty()) {
693       SUnit *CurSUnit = AvailableQueue.pop();
694
695       ScheduleHazardRecognizer::HazardType HT =
696         HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
697       if (HT == ScheduleHazardRecognizer::NoHazard) {
698         if (HazardRec->ShouldPreferAnother(CurSUnit)) {
699           if (!NotPreferredSUnit) {
700             // If this is the first non-preferred node for this cycle, then
701             // record it and continue searching for a preferred node. If this
702             // is not the first non-preferred node, then treat it as though
703             // there had been a hazard.
704             NotPreferredSUnit = CurSUnit;
705             continue;
706           }
707         } else {
708           FoundSUnit = CurSUnit;
709           break;
710         }
711       }
712
713       // Remember if this is a noop hazard.
714       HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
715
716       NotReady.push_back(CurSUnit);
717     }
718
719     // If we have a non-preferred node, push it back onto the available list.
720     // If we did not find a preferred node, then schedule this first
721     // non-preferred node.
722     if (NotPreferredSUnit) {
723       if (!FoundSUnit) {
724         DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
725         FoundSUnit = NotPreferredSUnit;
726       } else {
727         AvailableQueue.push(NotPreferredSUnit);
728       }
729
730       NotPreferredSUnit = 0;
731     }
732
733     // Add the nodes that aren't ready back onto the available list.
734     if (!NotReady.empty()) {
735       AvailableQueue.push_all(NotReady);
736       NotReady.clear();
737     }
738
739     // If we found a node to schedule...
740     if (FoundSUnit) {
741       // If we need to emit noops prior to this instruction, then do so.
742       unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
743       for (unsigned i = 0; i != NumPreNoops; ++i)
744         emitNoop(CurCycle);
745
746       // ... schedule the node...
747       ScheduleNodeTopDown(FoundSUnit, CurCycle);
748       HazardRec->EmitInstruction(FoundSUnit);
749       CycleHasInsts = true;
750       if (HazardRec->atIssueLimit()) {
751         DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
752         HazardRec->AdvanceCycle();
753         ++CurCycle;
754         CycleHasInsts = false;
755       }
756     } else {
757       if (CycleHasInsts) {
758         DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
759         HazardRec->AdvanceCycle();
760       } else if (!HasNoopHazards) {
761         // Otherwise, we have a pipeline stall, but no other problem,
762         // just advance the current cycle and try again.
763         DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
764         HazardRec->AdvanceCycle();
765         ++NumStalls;
766       } else {
767         // Otherwise, we have no instructions to issue and we have instructions
768         // that will fault if we don't do this right.  This is the case for
769         // processors without pipeline interlocks and other cases.
770         emitNoop(CurCycle);
771       }
772
773       ++CurCycle;
774       CycleHasInsts = false;
775     }
776   }
777
778 #ifndef NDEBUG
779   unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
780   unsigned Noops = 0;
781   for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
782     if (!Sequence[i])
783       ++Noops;
784   assert(Sequence.size() - Noops == ScheduledNodes &&
785          "The number of nodes scheduled doesn't match the expected number!");
786 #endif // NDEBUG
787 }
788
789 // EmitSchedule - Emit the machine code in scheduled order.
790 void SchedulePostRATDList::EmitSchedule() {
791   RegionBegin = RegionEnd;
792
793   // If first instruction was a DBG_VALUE then put it back.
794   if (FirstDbgValue)
795     BB->splice(RegionEnd, BB, FirstDbgValue);
796
797   // Then re-insert them according to the given schedule.
798   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
799     if (SUnit *SU = Sequence[i])
800       BB->splice(RegionEnd, BB, SU->getInstr());
801     else
802       // Null SUnit* is a noop.
803       TII->insertNoop(*BB, RegionEnd);
804
805     // Update the Begin iterator, as the first instruction in the block
806     // may have been scheduled later.
807     if (i == 0)
808       RegionBegin = prior(RegionEnd);
809   }
810
811   // Reinsert any remaining debug_values.
812   for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
813          DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
814     std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
815     MachineInstr *DbgValue = P.first;
816     MachineBasicBlock::iterator OrigPrivMI = P.second;
817     BB->splice(++OrigPrivMI, BB, DbgValue);
818   }
819   DbgValues.clear();
820   FirstDbgValue = NULL;
821 }