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