blob: de7746855b3f48e51f207cbd0ef4e10792325bc7 [file] [log] [blame]
Dale Johannesen72f15962007-07-13 17:31:29 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00007//
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"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000022#include "ScheduleDAGInstrs.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000023#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000024#include "llvm/CodeGen/LatencyPriorityQueue.h"
25#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3f237442008-12-16 03:25:46 +000026#include "llvm/CodeGen/MachineDominators.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000028#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2836c282009-01-16 01:33:36 +000030#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmanbed353d2009-02-10 23:29:38 +000031#include "llvm/Target/TargetLowering.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000032#include "llvm/Target/TargetMachine.h"
Dan Gohman21d90032008-11-25 00:52:40 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner459525d2008-01-14 19:00:06 +000035#include "llvm/Support/Compiler.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000036#include "llvm/Support/Debug.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000037#include "llvm/ADT/Statistic.h"
Dan Gohman21d90032008-11-25 00:52:40 +000038#include <map>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000039using namespace llvm;
40
Dan Gohman2836c282009-01-16 01:33:36 +000041STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000042STATISTIC(NumStalls, "Number of pipeline stalls");
43
Dan Gohman21d90032008-11-25 00:52:40 +000044static cl::opt<bool>
45EnableAntiDepBreaking("break-anti-dependencies",
Dan Gohman00dc84a2008-12-16 19:27:52 +000046 cl::desc("Break post-RA scheduling anti-dependencies"),
47 cl::init(true), cl::Hidden);
Dan Gohman21d90032008-11-25 00:52:40 +000048
Dan Gohman2836c282009-01-16 01:33:36 +000049static cl::opt<bool>
50EnablePostRAHazardAvoidance("avoid-hazards",
51 cl::desc("Enable simple hazard-avoidance"),
52 cl::init(true), cl::Hidden);
53
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000054namespace {
Dan Gohman343f0c02008-11-19 23:18:57 +000055 class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000056 public:
57 static char ID;
Dan Gohman343f0c02008-11-19 23:18:57 +000058 PostRAScheduler() : MachineFunctionPass(&ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000059
Dan Gohman3f237442008-12-16 03:25:46 +000060 void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.addRequired<MachineDominatorTree>();
62 AU.addPreserved<MachineDominatorTree>();
63 AU.addRequired<MachineLoopInfo>();
64 AU.addPreserved<MachineLoopInfo>();
65 MachineFunctionPass::getAnalysisUsage(AU);
66 }
67
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000068 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +000069 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000070 }
71
72 bool runOnMachineFunction(MachineFunction &Fn);
73 };
Dan Gohman343f0c02008-11-19 23:18:57 +000074 char PostRAScheduler::ID = 0;
75
76 class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +000077 /// AvailableQueue - The priority queue to use for the available SUnits.
78 ///
79 LatencyPriorityQueue AvailableQueue;
80
81 /// PendingQueue - This contains all of the instructions whose operands have
82 /// been issued, but their results are not ready yet (due to the latency of
83 /// the operation). Once the operands becomes available, the instruction is
84 /// added to the AvailableQueue.
85 std::vector<SUnit*> PendingQueue;
86
Dan Gohman21d90032008-11-25 00:52:40 +000087 /// Topo - A topological ordering for SUnits.
88 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +000089
Dan Gohman79ce2762009-01-15 19:20:50 +000090 /// AllocatableSet - The set of allocatable registers.
91 /// We'll be ignoring anti-dependencies on non-allocatable registers,
92 /// because they may not be safe to break.
93 const BitVector AllocatableSet;
94
Dan Gohman2836c282009-01-16 01:33:36 +000095 /// HazardRec - The hazard recognizer to use.
96 ScheduleHazardRecognizer *HazardRec;
97
Dan Gohman9e64bbb2009-02-10 23:27:53 +000098 /// Classes - For live regs that are only used in one register class in a
99 /// live range, the register class. If the register is not live, the
100 /// corresponding value is null. If the register is live but used in
101 /// multiple register classes, the corresponding value is -1 casted to a
102 /// pointer.
103 const TargetRegisterClass *
104 Classes[TargetRegisterInfo::FirstVirtualRegister];
105
106 /// RegRegs - Map registers to all their references within a live range.
107 std::multimap<unsigned, MachineOperand *> RegRefs;
108
109 /// The index of the most recent kill (proceding bottom-up), or ~0u if
110 /// the register is not live.
111 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
112
113 /// The index of the most recent complete def (proceding bottom up), or ~0u
114 /// if the register is live.
115 unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
116
Dan Gohman21d90032008-11-25 00:52:40 +0000117 public:
Dan Gohman79ce2762009-01-15 19:20:50 +0000118 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman3f237442008-12-16 03:25:46 +0000119 const MachineLoopInfo &MLI,
Dan Gohman2836c282009-01-16 01:33:36 +0000120 const MachineDominatorTree &MDT,
121 ScheduleHazardRecognizer *HR)
Dan Gohman79ce2762009-01-15 19:20:50 +0000122 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
Dan Gohman2836c282009-01-16 01:33:36 +0000123 AllocatableSet(TRI->getAllocatableSet(MF)),
124 HazardRec(HR) {}
125
126 ~SchedulePostRATDList() {
127 delete HazardRec;
128 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000129
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000130 /// StartBlock - Initialize register live-range state for scheduling in
131 /// this block.
132 ///
133 void StartBlock(MachineBasicBlock *BB);
134
135 /// Schedule - Schedule the instruction range using list scheduling.
136 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000137 void Schedule();
138
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000139 /// Observe - Update liveness information to account for the current
140 /// instruction, which will not be scheduled.
141 ///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000142 void Observe(MachineInstr *MI, unsigned Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000143
144 /// FinishBlock - Clean up register live-range state.
145 ///
146 void FinishBlock();
147
Dan Gohman343f0c02008-11-19 23:18:57 +0000148 private:
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000149 void PrescanInstruction(MachineInstr *MI);
150 void ScanInstruction(MachineInstr *MI, unsigned Count);
Dan Gohman54e4c362008-12-09 22:54:47 +0000151 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000152 void ReleaseSuccessors(SUnit *SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000153 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
154 void ListScheduleTopDown();
Dan Gohman21d90032008-11-25 00:52:40 +0000155 bool BreakAntiDependencies();
Dan Gohman343f0c02008-11-19 23:18:57 +0000156 };
Dan Gohman2836c282009-01-16 01:33:36 +0000157
158 /// SimpleHazardRecognizer - A *very* simple hazard recognizer. It uses
159 /// a coarse classification and attempts to avoid that instructions of
160 /// a given class aren't grouped too densely together.
161 class SimpleHazardRecognizer : public ScheduleHazardRecognizer {
162 /// Class - A simple classification for SUnits.
163 enum Class {
164 Other, Load, Store
165 };
166
167 /// Window - The Class values of the most recently issued
168 /// instructions.
169 Class Window[8];
170
171 /// getClass - Classify the given SUnit.
172 Class getClass(const SUnit *SU) {
173 const MachineInstr *MI = SU->getInstr();
174 const TargetInstrDesc &TID = MI->getDesc();
175 if (TID.mayLoad())
176 return Load;
177 if (TID.mayStore())
178 return Store;
179 return Other;
180 }
181
182 /// Step - Rotate the existing entries in Window and insert the
183 /// given class value in position as the most recent.
184 void Step(Class C) {
185 std::copy(Window+1, array_endof(Window), Window);
186 Window[array_lengthof(Window)-1] = C;
187 }
188
189 public:
190 SimpleHazardRecognizer() : Window() {}
191
192 virtual HazardType getHazardType(SUnit *SU) {
193 Class C = getClass(SU);
194 if (C == Other)
195 return NoHazard;
196 unsigned Score = 0;
Dan Gohman79ce4ce2009-01-16 17:55:08 +0000197 for (unsigned i = 0; i != array_lengthof(Window); ++i)
Dan Gohman2836c282009-01-16 01:33:36 +0000198 if (Window[i] == C)
199 Score += i + 1;
200 if (Score > array_lengthof(Window) * 2)
201 return Hazard;
202 return NoHazard;
203 }
204
205 virtual void EmitInstruction(SUnit *SU) {
206 Step(getClass(SU));
207 }
208
209 virtual void AdvanceCycle() {
210 Step(Other);
211 }
212 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000213}
214
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000215/// isSchedulingBoundary - Test if the given instruction should be
216/// considered a scheduling boundary. This primarily includes labels
217/// and terminators.
218///
219static bool isSchedulingBoundary(const MachineInstr *MI,
220 const MachineFunction &MF) {
221 // Terminators and labels can't be scheduled around.
222 if (MI->getDesc().isTerminator() || MI->isLabel())
223 return true;
224
Dan Gohmanbed353d2009-02-10 23:29:38 +0000225 // Don't attempt to schedule around any instruction that modifies
226 // a stack-oriented pointer, as it's unlikely to be profitable. This
227 // saves compile time, because it doesn't require every single
228 // stack slot reference to depend on the instruction that does the
229 // modification.
230 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
231 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore()))
232 return true;
233
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000234 return false;
235}
236
Dan Gohman343f0c02008-11-19 23:18:57 +0000237bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
238 DOUT << "PostRAScheduler\n";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000239
Dan Gohman3f237442008-12-16 03:25:46 +0000240 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
241 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
Dan Gohman2836c282009-01-16 01:33:36 +0000242 ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ?
243 new SimpleHazardRecognizer :
244 new ScheduleHazardRecognizer();
Dan Gohman3f237442008-12-16 03:25:46 +0000245
Dan Gohman2836c282009-01-16 01:33:36 +0000246 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR);
Dan Gohman79ce2762009-01-15 19:20:50 +0000247
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000248 // Loop over all of the basic blocks
249 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000250 MBB != MBBe; ++MBB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000251 // Initialize register live-range state for scheduling in this block.
252 Scheduler.StartBlock(MBB);
253
Dan Gohmanf7119392009-01-16 22:10:20 +0000254 // Schedule each sequence of instructions not interrupted by a label
255 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000256 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000257 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000258 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
259 MachineInstr *MI = prior(I);
260 if (isSchedulingBoundary(MI, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000261 Scheduler.Run(MBB, I, Current, CurrentCount);
262 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000263 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000264 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000265 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000266 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000267 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000268 --Count;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000269 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000270 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000271 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000272 "Instruction count mismatch!");
273 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohman343f0c02008-11-19 23:18:57 +0000274 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000275
276 // Clean up register live-range state.
277 Scheduler.FinishBlock();
Dan Gohman343f0c02008-11-19 23:18:57 +0000278 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000279
280 return true;
281}
282
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000283/// StartBlock - Initialize register live-range state for scheduling in
284/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000285///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000286void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
287 // Call the superclass.
288 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000289
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000290 // Clear out the register class data.
291 std::fill(Classes, array_endof(Classes),
292 static_cast<const TargetRegisterClass *>(0));
Dan Gohman21d90032008-11-25 00:52:40 +0000293
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000294 // Initialize the indices to indicate that no registers are live.
Dan Gohman6c3643c2008-12-19 22:23:43 +0000295 std::fill(KillIndices, array_endof(KillIndices), ~0u);
Dan Gohman21d90032008-11-25 00:52:40 +0000296 std::fill(DefIndices, array_endof(DefIndices), BB->size());
297
298 // Determine the live-out physregs for this block.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000299 if (!BB->empty() && BB->back().getDesc().isReturn())
Dan Gohman21d90032008-11-25 00:52:40 +0000300 // In a return block, examine the function live-out regs.
301 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
302 E = MRI.liveout_end(); I != E; ++I) {
303 unsigned Reg = *I;
304 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
305 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000306 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000307 // Repeat, for all aliases.
308 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
309 unsigned AliasReg = *Alias;
310 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
311 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000312 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000313 }
314 }
315 else
316 // In a non-return block, examine the live-in regs of all successors.
317 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
Dan Gohman47ac0f02009-02-11 04:27:20 +0000318 SE = BB->succ_end(); SI != SE; ++SI)
Dan Gohman21d90032008-11-25 00:52:40 +0000319 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
320 E = (*SI)->livein_end(); I != E; ++I) {
321 unsigned Reg = *I;
322 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
323 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000324 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000325 // Repeat, for all aliases.
326 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
327 unsigned AliasReg = *Alias;
328 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
329 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000330 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000331 }
332 }
333
334 // Consider callee-saved registers as live-out, since we're running after
335 // prologue/epilogue insertion so there's no way to add additional
336 // saved registers.
337 //
338 // TODO: If the callee saves and restores these, then we can potentially
339 // use them between the save and the restore. To do that, we could scan
340 // the exit blocks to see which of these registers are defined.
Dan Gohman00dc84a2008-12-16 19:27:52 +0000341 // Alternatively, callee-saved registers that aren't saved and restored
Dan Gohmanebb0a312008-12-03 19:30:13 +0000342 // could be marked live-in in every block.
Dan Gohman21d90032008-11-25 00:52:40 +0000343 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
344 unsigned Reg = *I;
345 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
346 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000347 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000348 // Repeat, for all aliases.
349 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
350 unsigned AliasReg = *Alias;
351 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
352 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000353 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000354 }
355 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000356}
357
358/// Schedule - Schedule the instruction range using list scheduling.
359///
360void SchedulePostRATDList::Schedule() {
361 DOUT << "********** List Scheduling **********\n";
362
363 // Build the scheduling graph.
364 BuildSchedGraph();
365
366 if (EnableAntiDepBreaking) {
367 if (BreakAntiDependencies()) {
368 // We made changes. Update the dependency graph.
369 // Theoretically we could update the graph in place:
370 // When a live range is changed to use a different register, remove
371 // the def's anti-dependence *and* output-dependence edges due to
372 // that register, and add new anti-dependence and output-dependence
373 // edges based on the next live range of the register.
374 SUnits.clear();
375 EntrySU = SUnit();
376 ExitSU = SUnit();
377 BuildSchedGraph();
378 }
379 }
380
381 AvailableQueue.initNodes(SUnits);
382
383 ListScheduleTopDown();
384
385 AvailableQueue.releaseState();
386}
387
388/// Observe - Update liveness information to account for the current
389/// instruction, which will not be scheduled.
390///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000391void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000392 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
393
394 // Any register which was defined within the previous scheduling region
395 // may have been rescheduled and its lifetime may overlap with registers
396 // in ways not reflected in our current liveness state. For each such
397 // register, adjust the liveness state to be conservatively correct.
398 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg)
399 if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
400 assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
401 // Mark this register to be non-renamable.
402 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
403 // Move the def index to the end of the previous region, to reflect
404 // that the def could theoretically have been scheduled at the end.
405 DefIndices[Reg] = InsertPosIndex;
406 }
407
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000408 PrescanInstruction(MI);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000409 ScanInstruction(MI, Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000410}
411
412/// FinishBlock - Clean up register live-range state.
413///
414void SchedulePostRATDList::FinishBlock() {
415 RegRefs.clear();
416
417 // Call the superclass.
418 ScheduleDAGInstrs::FinishBlock();
419}
420
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000421/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
422/// critical path.
423static SDep *CriticalPathStep(SUnit *SU) {
424 SDep *Next = 0;
425 unsigned NextDepth = 0;
426 // Find the predecessor edge with the greatest depth.
427 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
428 P != PE; ++P) {
429 SUnit *PredSU = P->getSUnit();
430 unsigned PredLatency = P->getLatency();
431 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
432 // In the case of a latency tie, prefer an anti-dependency edge over
433 // other types of edges.
434 if (NextDepth < PredTotalLatency ||
435 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
436 NextDepth = PredTotalLatency;
437 Next = &*P;
438 }
439 }
440 return Next;
441}
442
443void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) {
444 // Scan the register operands for this instruction and update
445 // Classes and RegRefs.
446 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
447 MachineOperand &MO = MI->getOperand(i);
448 if (!MO.isReg()) continue;
449 unsigned Reg = MO.getReg();
450 if (Reg == 0) continue;
451 const TargetRegisterClass *NewRC =
452 getInstrOperandRegClass(TRI, MI->getDesc(), i);
453
454 // For now, only allow the register to be changed if its register
455 // class is consistent across all uses.
456 if (!Classes[Reg] && NewRC)
457 Classes[Reg] = NewRC;
458 else if (!NewRC || Classes[Reg] != NewRC)
459 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
460
461 // Now check for aliases.
462 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
463 // If an alias of the reg is used during the live range, give up.
464 // Note that this allows us to skip checking if AntiDepReg
465 // overlaps with any of the aliases, among other things.
466 unsigned AliasReg = *Alias;
467 if (Classes[AliasReg]) {
468 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
469 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
470 }
471 }
472
473 // If we're still willing to consider this register, note the reference.
474 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
475 RegRefs.insert(std::make_pair(Reg, &MO));
476 }
477}
478
479void SchedulePostRATDList::ScanInstruction(MachineInstr *MI,
480 unsigned Count) {
481 // Update liveness.
482 // Proceding upwards, registers that are defed but not used in this
483 // instruction are now dead.
484 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
485 MachineOperand &MO = MI->getOperand(i);
486 if (!MO.isReg()) continue;
487 unsigned Reg = MO.getReg();
488 if (Reg == 0) continue;
489 if (!MO.isDef()) continue;
490 // Ignore two-addr defs.
Bob Wilsond9df5012009-04-09 17:16:43 +0000491 if (MI->isRegTiedToUseOperand(i)) continue;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000492
493 DefIndices[Reg] = Count;
494 KillIndices[Reg] = ~0u;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000495 assert(((KillIndices[Reg] == ~0u) !=
496 (DefIndices[Reg] == ~0u)) &&
497 "Kill and Def maps aren't consistent for Reg!");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000498 Classes[Reg] = 0;
499 RegRefs.erase(Reg);
500 // Repeat, for all subregs.
501 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
502 *Subreg; ++Subreg) {
503 unsigned SubregReg = *Subreg;
504 DefIndices[SubregReg] = Count;
505 KillIndices[SubregReg] = ~0u;
506 Classes[SubregReg] = 0;
507 RegRefs.erase(SubregReg);
508 }
509 // Conservatively mark super-registers as unusable.
510 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
511 *Super; ++Super) {
512 unsigned SuperReg = *Super;
513 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
514 }
515 }
516 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
517 MachineOperand &MO = MI->getOperand(i);
518 if (!MO.isReg()) continue;
519 unsigned Reg = MO.getReg();
520 if (Reg == 0) continue;
521 if (!MO.isUse()) continue;
522
523 const TargetRegisterClass *NewRC =
524 getInstrOperandRegClass(TRI, MI->getDesc(), i);
525
526 // For now, only allow the register to be changed if its register
527 // class is consistent across all uses.
528 if (!Classes[Reg] && NewRC)
529 Classes[Reg] = NewRC;
530 else if (!NewRC || Classes[Reg] != NewRC)
531 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
532
533 RegRefs.insert(std::make_pair(Reg, &MO));
534
535 // It wasn't previously live but now it is, this is a kill.
536 if (KillIndices[Reg] == ~0u) {
537 KillIndices[Reg] = Count;
538 DefIndices[Reg] = ~0u;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000539 assert(((KillIndices[Reg] == ~0u) !=
540 (DefIndices[Reg] == ~0u)) &&
541 "Kill and Def maps aren't consistent for Reg!");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000542 }
543 // Repeat, for all aliases.
544 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
545 unsigned AliasReg = *Alias;
546 if (KillIndices[AliasReg] == ~0u) {
547 KillIndices[AliasReg] = Count;
548 DefIndices[AliasReg] = ~0u;
549 }
550 }
551 }
552}
553
554/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
555/// of the ScheduleDAG and break them by renaming registers.
556///
557bool SchedulePostRATDList::BreakAntiDependencies() {
558 // The code below assumes that there is at least one instruction,
559 // so just duck out immediately if the block is empty.
560 if (SUnits.empty()) return false;
561
562 // Find the node at the bottom of the critical path.
563 SUnit *Max = 0;
564 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
565 SUnit *SU = &SUnits[i];
566 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
567 Max = SU;
568 }
569
570 DOUT << "Critical path has total latency "
571 << (Max->getDepth() + Max->Latency) << "\n";
572
573 // Track progress along the critical path through the SUnit graph as we walk
574 // the instructions.
575 SUnit *CriticalPathSU = Max;
576 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
Dan Gohman21d90032008-11-25 00:52:40 +0000577
578 // Consider this pattern:
579 // A = ...
580 // ... = A
581 // A = ...
582 // ... = A
583 // A = ...
584 // ... = A
585 // A = ...
586 // ... = A
587 // There are three anti-dependencies here, and without special care,
588 // we'd break all of them using the same register:
589 // A = ...
590 // ... = A
591 // B = ...
592 // ... = B
593 // B = ...
594 // ... = B
595 // B = ...
596 // ... = B
597 // because at each anti-dependence, B is the first register that
598 // isn't A which is free. This re-introduces anti-dependencies
599 // at all but one of the original anti-dependencies that we were
600 // trying to break. To avoid this, keep track of the most recent
601 // register that each register was replaced with, avoid avoid
602 // using it to repair an anti-dependence on the same register.
603 // This lets us produce this:
604 // A = ...
605 // ... = A
606 // B = ...
607 // ... = B
608 // C = ...
609 // ... = C
610 // B = ...
611 // ... = B
612 // This still has an anti-dependence on B, but at least it isn't on the
613 // original critical path.
614 //
615 // TODO: If we tracked more than one register here, we could potentially
616 // fix that remaining critical edge too. This is a little more involved,
617 // because unlike the most recent register, less recent registers should
618 // still be considered, though only if no other registers are available.
619 unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
620
Dan Gohman21d90032008-11-25 00:52:40 +0000621 // Attempt to break anti-dependence edges on the critical path. Walk the
622 // instructions from the bottom up, tracking information about liveness
623 // as we go to help determine which registers are available.
624 bool Changed = false;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000625 unsigned Count = InsertPosIndex - 1;
626 for (MachineBasicBlock::iterator I = InsertPos, E = Begin;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000627 I != E; --Count) {
628 MachineInstr *MI = --I;
Dan Gohman21d90032008-11-25 00:52:40 +0000629
Dan Gohman490b1832008-12-05 05:30:02 +0000630 // After regalloc, IMPLICIT_DEF instructions aren't safe to treat as
631 // dependence-breaking. In the case of an INSERT_SUBREG, the IMPLICIT_DEF
632 // is left behind appearing to clobber the super-register, while the
633 // subregister needs to remain live. So we just ignore them.
634 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
635 continue;
636
Dan Gohman00dc84a2008-12-16 19:27:52 +0000637 // Check if this instruction has a dependence on the critical path that
638 // is an anti-dependence that we may be able to break. If it is, set
639 // AntiDepReg to the non-zero register associated with the anti-dependence.
640 //
641 // We limit our attention to the critical path as a heuristic to avoid
642 // breaking anti-dependence edges that aren't going to significantly
643 // impact the overall schedule. There are a limited number of registers
644 // and we want to save them for the important edges.
645 //
646 // TODO: Instructions with multiple defs could have multiple
647 // anti-dependencies. The current code here only knows how to break one
648 // edge per instruction. Note that we'd have to be able to break all of
649 // the anti-dependencies in an instruction in order to be effective.
650 unsigned AntiDepReg = 0;
651 if (MI == CriticalPathMI) {
652 if (SDep *Edge = CriticalPathStep(CriticalPathSU)) {
653 SUnit *NextSU = Edge->getSUnit();
654
655 // Only consider anti-dependence edges.
656 if (Edge->getKind() == SDep::Anti) {
657 AntiDepReg = Edge->getReg();
658 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
659 // Don't break anti-dependencies on non-allocatable registers.
Dan Gohman49bb50e2009-01-16 21:57:43 +0000660 if (!AllocatableSet.test(AntiDepReg))
661 AntiDepReg = 0;
662 else {
Dan Gohman00dc84a2008-12-16 19:27:52 +0000663 // If the SUnit has other dependencies on the SUnit that it
664 // anti-depends on, don't bother breaking the anti-dependency
665 // since those edges would prevent such units from being
666 // scheduled past each other regardless.
667 //
668 // Also, if there are dependencies on other SUnits with the
669 // same register as the anti-dependency, don't attempt to
670 // break it.
671 for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(),
672 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
673 if (P->getSUnit() == NextSU ?
674 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
675 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
676 AntiDepReg = 0;
677 break;
678 }
679 }
680 }
681 CriticalPathSU = NextSU;
682 CriticalPathMI = CriticalPathSU->getInstr();
683 } else {
684 // We've reached the end of the critical path.
685 CriticalPathSU = 0;
686 CriticalPathMI = 0;
687 }
688 }
Dan Gohman21d90032008-11-25 00:52:40 +0000689
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000690 PrescanInstruction(MI);
691
692 // If this instruction has a use of AntiDepReg, breaking it
693 // is invalid.
Dan Gohman21d90032008-11-25 00:52:40 +0000694 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
695 MachineOperand &MO = MI->getOperand(i);
696 if (!MO.isReg()) continue;
697 unsigned Reg = MO.getReg();
698 if (Reg == 0) continue;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000699 if (MO.isUse() && AntiDepReg == Reg) {
Dan Gohman21d90032008-11-25 00:52:40 +0000700 AntiDepReg = 0;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000701 break;
Dan Gohman21d90032008-11-25 00:52:40 +0000702 }
Dan Gohman21d90032008-11-25 00:52:40 +0000703 }
704
705 // Determine AntiDepReg's register class, if it is live and is
706 // consistently used within a single class.
707 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
Nick Lewyckya89d1022008-11-27 17:29:52 +0000708 assert((AntiDepReg == 0 || RC != NULL) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000709 "Register should be live if it's causing an anti-dependence!");
710 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
711 AntiDepReg = 0;
712
713 // Look for a suitable register to use to break the anti-depenence.
714 //
715 // TODO: Instead of picking the first free register, consider which might
716 // be the best.
717 if (AntiDepReg != 0) {
Dan Gohman79ce2762009-01-15 19:20:50 +0000718 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
719 RE = RC->allocation_order_end(MF); R != RE; ++R) {
Dan Gohman21d90032008-11-25 00:52:40 +0000720 unsigned NewReg = *R;
721 // Don't replace a register with itself.
722 if (NewReg == AntiDepReg) continue;
723 // Don't replace a register with one that was recently used to repair
724 // an anti-dependence with this AntiDepReg, because that would
725 // re-introduce that anti-dependence.
726 if (NewReg == LastNewReg[AntiDepReg]) continue;
727 // If NewReg is dead and NewReg's most recent def is not before
728 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Dan Gohman6c3643c2008-12-19 22:23:43 +0000729 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000730 "Kill and Def maps aren't consistent for AntiDepReg!");
Dan Gohman6c3643c2008-12-19 22:23:43 +0000731 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000732 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohman6c3643c2008-12-19 22:23:43 +0000733 if (KillIndices[NewReg] == ~0u &&
Dan Gohmanfde221f2008-12-16 06:20:58 +0000734 Classes[NewReg] != reinterpret_cast<TargetRegisterClass *>(-1) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000735 KillIndices[AntiDepReg] <= DefIndices[NewReg]) {
Dan Gohman80e201b2008-12-04 02:15:26 +0000736 DOUT << "Breaking anti-dependence edge on "
737 << TRI->getName(AntiDepReg)
Dan Gohmancef874a2008-12-03 23:07:27 +0000738 << " with " << RegRefs.count(AntiDepReg) << " references"
Dan Gohman80e201b2008-12-04 02:15:26 +0000739 << " using " << TRI->getName(NewReg) << "!\n";
Dan Gohman21d90032008-11-25 00:52:40 +0000740
741 // Update the references to the old register to refer to the new
742 // register.
743 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
744 std::multimap<unsigned, MachineOperand *>::iterator>
745 Range = RegRefs.equal_range(AntiDepReg);
746 for (std::multimap<unsigned, MachineOperand *>::iterator
747 Q = Range.first, QE = Range.second; Q != QE; ++Q)
748 Q->second->setReg(NewReg);
749
750 // We just went back in time and modified history; the
751 // liveness information for the anti-depenence reg is now
752 // inconsistent. Set the state as if it were dead.
753 Classes[NewReg] = Classes[AntiDepReg];
754 DefIndices[NewReg] = DefIndices[AntiDepReg];
755 KillIndices[NewReg] = KillIndices[AntiDepReg];
Dan Gohman47ac0f02009-02-11 04:27:20 +0000756 assert(((KillIndices[NewReg] == ~0u) !=
757 (DefIndices[NewReg] == ~0u)) &&
758 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000759
760 Classes[AntiDepReg] = 0;
761 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
Dan Gohman6c3643c2008-12-19 22:23:43 +0000762 KillIndices[AntiDepReg] = ~0u;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000763 assert(((KillIndices[AntiDepReg] == ~0u) !=
764 (DefIndices[AntiDepReg] == ~0u)) &&
765 "Kill and Def maps aren't consistent for AntiDepReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000766
767 RegRefs.erase(AntiDepReg);
768 Changed = true;
769 LastNewReg[AntiDepReg] = NewReg;
770 break;
771 }
772 }
773 }
774
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000775 ScanInstruction(MI, Count);
Dan Gohman21d90032008-11-25 00:52:40 +0000776 }
Dan Gohman21d90032008-11-25 00:52:40 +0000777
778 return Changed;
779}
780
Dan Gohman343f0c02008-11-19 23:18:57 +0000781//===----------------------------------------------------------------------===//
782// Top-Down Scheduling
783//===----------------------------------------------------------------------===//
784
785/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
786/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +0000787void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
788 SUnit *SuccSU = SuccEdge->getSUnit();
Dan Gohman343f0c02008-11-19 23:18:57 +0000789 --SuccSU->NumPredsLeft;
790
791#ifndef NDEBUG
792 if (SuccSU->NumPredsLeft < 0) {
793 cerr << "*** Scheduling failed! ***\n";
794 SuccSU->dump(this);
795 cerr << " has been released too many times!\n";
796 assert(0);
797 }
798#endif
799
800 // Compute how many cycles it will be before this actually becomes
801 // available. This is the max of the start time of all predecessors plus
802 // their latencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000803 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Dan Gohman343f0c02008-11-19 23:18:57 +0000804
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000805 // If all the node's predecessors are scheduled, this node is ready
806 // to be scheduled. Ignore the special ExitSU node.
807 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000808 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000809}
810
811/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
812void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
813 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
814 I != E; ++I)
815 ReleaseSucc(SU, &*I);
Dan Gohman343f0c02008-11-19 23:18:57 +0000816}
817
818/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
819/// count of its successors. If a successor pending count is zero, add it to
820/// the Available queue.
821void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
822 DOUT << "*** Scheduling [" << CurCycle << "]: ";
823 DEBUG(SU->dump(this));
824
825 Sequence.push_back(SU);
Dan Gohman3f237442008-12-16 03:25:46 +0000826 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
827 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000828
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000829 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000830 SU->isScheduled = true;
831 AvailableQueue.ScheduledNode(SU);
832}
833
834/// ListScheduleTopDown - The main loop of list scheduling for top-down
835/// schedulers.
836void SchedulePostRATDList::ListScheduleTopDown() {
837 unsigned CurCycle = 0;
838
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000839 // Release any successors of the special Entry node.
840 ReleaseSuccessors(&EntrySU);
841
Dan Gohman343f0c02008-11-19 23:18:57 +0000842 // All leaves to Available queue.
843 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
844 // It is available if it has no predecessors.
845 if (SUnits[i].Preds.empty()) {
846 AvailableQueue.push(&SUnits[i]);
847 SUnits[i].isAvailable = true;
848 }
849 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000850
Dan Gohman343f0c02008-11-19 23:18:57 +0000851 // While Available queue is not empty, grab the node with the highest
852 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000853 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000854 Sequence.reserve(SUnits.size());
855 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
856 // Check to see if any of the pending instructions are ready to issue. If
857 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000858 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000859 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman3f237442008-12-16 03:25:46 +0000860 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000861 AvailableQueue.push(PendingQueue[i]);
862 PendingQueue[i]->isAvailable = true;
863 PendingQueue[i] = PendingQueue.back();
864 PendingQueue.pop_back();
865 --i; --e;
Dan Gohman3f237442008-12-16 03:25:46 +0000866 } else if (PendingQueue[i]->getDepth() < MinDepth)
867 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000868 }
869
Dan Gohman2836c282009-01-16 01:33:36 +0000870 // If there are no instructions available, don't try to issue anything, and
871 // don't advance the hazard recognizer.
Dan Gohman343f0c02008-11-19 23:18:57 +0000872 if (AvailableQueue.empty()) {
Dan Gohman3f237442008-12-16 03:25:46 +0000873 CurCycle = MinDepth != ~0u ? MinDepth : CurCycle + 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000874 continue;
875 }
876
Dan Gohman2836c282009-01-16 01:33:36 +0000877 SUnit *FoundSUnit = 0;
878
879 bool HasNoopHazards = false;
880 while (!AvailableQueue.empty()) {
881 SUnit *CurSUnit = AvailableQueue.pop();
882
883 ScheduleHazardRecognizer::HazardType HT =
884 HazardRec->getHazardType(CurSUnit);
885 if (HT == ScheduleHazardRecognizer::NoHazard) {
886 FoundSUnit = CurSUnit;
887 break;
888 }
889
890 // Remember if this is a noop hazard.
891 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
892
893 NotReady.push_back(CurSUnit);
894 }
895
896 // Add the nodes that aren't ready back onto the available list.
897 if (!NotReady.empty()) {
898 AvailableQueue.push_all(NotReady);
899 NotReady.clear();
900 }
901
Dan Gohman343f0c02008-11-19 23:18:57 +0000902 // If we found a node to schedule, do it now.
903 if (FoundSUnit) {
904 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000905 HazardRec->EmitInstruction(FoundSUnit);
Dan Gohman343f0c02008-11-19 23:18:57 +0000906
907 // If this is a pseudo-op node, we don't want to increment the current
908 // cycle.
909 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
Dan Gohman2836c282009-01-16 01:33:36 +0000910 ++CurCycle;
911 } else if (!HasNoopHazards) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000912 // Otherwise, we have a pipeline stall, but no other problem, just advance
913 // the current cycle and try again.
914 DOUT << "*** Advancing cycle, no work to do\n";
Dan Gohman2836c282009-01-16 01:33:36 +0000915 HazardRec->AdvanceCycle();
Dan Gohman343f0c02008-11-19 23:18:57 +0000916 ++NumStalls;
917 ++CurCycle;
Dan Gohman2836c282009-01-16 01:33:36 +0000918 } else {
919 // Otherwise, we have no instructions to issue and we have instructions
920 // that will fault if we don't do this right. This is the case for
921 // processors without pipeline interlocks and other cases.
922 DOUT << "*** Emitting noop\n";
923 HazardRec->EmitNoop();
924 Sequence.push_back(0); // NULL here means noop
925 ++NumNoops;
926 ++CurCycle;
Dan Gohman343f0c02008-11-19 23:18:57 +0000927 }
928 }
929
930#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000931 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000932#endif
933}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000934
935//===----------------------------------------------------------------------===//
936// Public Constructor Functions
937//===----------------------------------------------------------------------===//
938
939FunctionPass *llvm::createPostRAScheduler() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000940 return new PostRAScheduler();
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000941}