blob: 0fc6c3370bb384927706d76730ecf8c89134d76e [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"
22#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000023#include "llvm/CodeGen/ScheduleDAGInstrs.h"
24#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"
30#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner459525d2008-01-14 19:00:06 +000032#include "llvm/Support/Compiler.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000033#include "llvm/Support/Debug.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000034#include "llvm/ADT/Statistic.h"
Dan Gohman21d90032008-11-25 00:52:40 +000035#include "llvm/ADT/DenseSet.h"
Dan Gohmancef874a2008-12-03 23:07:27 +000036#include "llvm/ADT/SmallVector.h"
Dan Gohman21d90032008-11-25 00:52:40 +000037#include <map>
38#include <climits>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000039using namespace llvm;
40
Dan Gohman343f0c02008-11-19 23:18:57 +000041STATISTIC(NumStalls, "Number of pipeline stalls");
42
Dan Gohman21d90032008-11-25 00:52:40 +000043static cl::opt<bool>
44EnableAntiDepBreaking("break-anti-dependencies",
45 cl::desc("Break scheduling anti-dependencies"),
46 cl::init(false));
47
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000048namespace {
Dan Gohman343f0c02008-11-19 23:18:57 +000049 class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000050 public:
51 static char ID;
Dan Gohman343f0c02008-11-19 23:18:57 +000052 PostRAScheduler() : MachineFunctionPass(&ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000053
Dan Gohman3f237442008-12-16 03:25:46 +000054 void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.addRequired<MachineDominatorTree>();
56 AU.addPreserved<MachineDominatorTree>();
57 AU.addRequired<MachineLoopInfo>();
58 AU.addPreserved<MachineLoopInfo>();
59 MachineFunctionPass::getAnalysisUsage(AU);
60 }
61
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000062 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +000063 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000064 }
65
66 bool runOnMachineFunction(MachineFunction &Fn);
67 };
Dan Gohman343f0c02008-11-19 23:18:57 +000068 char PostRAScheduler::ID = 0;
69
70 class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +000071 /// AvailableQueue - The priority queue to use for the available SUnits.
72 ///
73 LatencyPriorityQueue AvailableQueue;
74
75 /// PendingQueue - This contains all of the instructions whose operands have
76 /// been issued, but their results are not ready yet (due to the latency of
77 /// the operation). Once the operands becomes available, the instruction is
78 /// added to the AvailableQueue.
79 std::vector<SUnit*> PendingQueue;
80
Dan Gohman21d90032008-11-25 00:52:40 +000081 /// Topo - A topological ordering for SUnits.
82 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +000083
Dan Gohman21d90032008-11-25 00:52:40 +000084 public:
Dan Gohman3f237442008-12-16 03:25:46 +000085 SchedulePostRATDList(MachineBasicBlock *mbb, const TargetMachine &tm,
86 const MachineLoopInfo &MLI,
87 const MachineDominatorTree &MDT)
88 : ScheduleDAGInstrs(mbb, tm, MLI, MDT), Topo(SUnits) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000089
90 void Schedule();
91
92 private:
Dan Gohman54e4c362008-12-09 22:54:47 +000093 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
Dan Gohman343f0c02008-11-19 23:18:57 +000094 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
95 void ListScheduleTopDown();
Dan Gohman21d90032008-11-25 00:52:40 +000096 bool BreakAntiDependencies();
Dan Gohman343f0c02008-11-19 23:18:57 +000097 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000098}
99
Dan Gohman343f0c02008-11-19 23:18:57 +0000100bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
101 DOUT << "PostRAScheduler\n";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000102
Dan Gohman3f237442008-12-16 03:25:46 +0000103 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
104 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
105
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000106 // Loop over all of the basic blocks
107 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000108 MBB != MBBe; ++MBB) {
109
Dan Gohman3f237442008-12-16 03:25:46 +0000110 SchedulePostRATDList Scheduler(MBB, Fn.getTarget(), MLI, MDT);
Dan Gohman343f0c02008-11-19 23:18:57 +0000111
112 Scheduler.Run();
113
114 Scheduler.EmitSchedule();
115 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000116
117 return true;
118}
119
Dan Gohman343f0c02008-11-19 23:18:57 +0000120/// Schedule - Schedule the DAG using list scheduling.
121void SchedulePostRATDList::Schedule() {
122 DOUT << "********** List Scheduling **********\n";
123
124 // Build scheduling units.
125 BuildSchedUnits();
126
Dan Gohman21d90032008-11-25 00:52:40 +0000127 if (EnableAntiDepBreaking) {
128 if (BreakAntiDependencies()) {
129 // We made changes. Update the dependency graph.
130 // Theoretically we could update the graph in place:
131 // When a live range is changed to use a different register, remove
132 // the def's anti-dependence *and* output-dependence edges due to
133 // that register, and add new anti-dependence and output-dependence
134 // edges based on the next live range of the register.
135 SUnits.clear();
136 BuildSchedUnits();
137 }
138 }
139
Dan Gohman343f0c02008-11-19 23:18:57 +0000140 AvailableQueue.initNodes(SUnits);
Dan Gohman21d90032008-11-25 00:52:40 +0000141
Dan Gohman343f0c02008-11-19 23:18:57 +0000142 ListScheduleTopDown();
143
144 AvailableQueue.releaseState();
145}
146
Dan Gohman21d90032008-11-25 00:52:40 +0000147/// getInstrOperandRegClass - Return register class of the operand of an
148/// instruction of the specified TargetInstrDesc.
149static const TargetRegisterClass*
150getInstrOperandRegClass(const TargetRegisterInfo *TRI,
151 const TargetInstrInfo *TII, const TargetInstrDesc &II,
152 unsigned Op) {
153 if (Op >= II.getNumOperands())
154 return NULL;
155 if (II.OpInfo[Op].isLookupPtrRegClass())
156 return TII->getPointerRegClass();
157 return TRI->getRegClass(II.OpInfo[Op].RegClass);
158}
159
Dan Gohman3f237442008-12-16 03:25:46 +0000160/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
161/// critical path.
162static SDep *CriticalPathStep(SUnit *SU) {
163 SDep *Next = 0;
164 unsigned NextDepth = 0;
165 // Find the predecessor edge with the greatest depth.
166 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
167 P != PE; ++P) {
168 SUnit *PredSU = P->getSUnit();
169 unsigned PredLatency = P->getLatency();
170 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
171 // In the case of a latency tie, prefer an anti-dependency edge over
172 // other types of edges.
173 if (NextDepth < PredTotalLatency ||
174 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
175 NextDepth = PredTotalLatency;
176 Next = &*P;
177 }
178 }
179 return Next;
180}
181
Dan Gohman21d90032008-11-25 00:52:40 +0000182/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
183/// of the ScheduleDAG and break them by renaming registers.
184///
185bool SchedulePostRATDList::BreakAntiDependencies() {
186 // The code below assumes that there is at least one instruction,
187 // so just duck out immediately if the block is empty.
188 if (BB->empty()) return false;
189
Dan Gohman3f237442008-12-16 03:25:46 +0000190 // Find the node at the bottom of the critical path.
Dan Gohman21d90032008-11-25 00:52:40 +0000191 SUnit *Max = 0;
Dan Gohman3f237442008-12-16 03:25:46 +0000192 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
193 SUnit *SU = &SUnits[i];
194 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
Dan Gohman21d90032008-11-25 00:52:40 +0000195 Max = SU;
196 }
197
198 DOUT << "Critical path has total latency "
Dan Gohman3f237442008-12-16 03:25:46 +0000199 << (Max ? Max->getDepth() + Max->Latency : 0) << "\n";
Dan Gohman21d90032008-11-25 00:52:40 +0000200
201 // Walk the critical path from the bottom up. Collect all anti-dependence
202 // edges on the critical path. Skip anti-dependencies between SUnits that
203 // are connected with other edges, since such units won't be able to be
204 // scheduled past each other anyway.
205 //
206 // The heuristic is that edges on the critical path are more important to
207 // break than other edges. And since there are a limited number of
208 // registers, we don't want to waste them breaking edges that aren't
209 // important.
210 //
211 // TODO: Instructions with multiple defs could have multiple
212 // anti-dependencies. The current code here only knows how to break one
213 // edge per instruction. Note that we'd have to be able to break all of
214 // the anti-dependencies in an instruction in order to be effective.
215 BitVector AllocatableSet = TRI->getAllocatableSet(*MF);
216 DenseMap<MachineInstr *, unsigned> CriticalAntiDeps;
Dan Gohman3f237442008-12-16 03:25:46 +0000217 SUnit *SU = Max;
218 for (SDep *Edge = CriticalPathStep(SU); Edge;
219 Edge = CriticalPathStep(SU = Edge->getSUnit())) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000220 SUnit *NextSU = Edge->getSUnit();
Dan Gohman0dba0e52008-12-03 19:32:26 +0000221 // Only consider anti-dependence edges.
Dan Gohman54e4c362008-12-09 22:54:47 +0000222 if (Edge->getKind() != SDep::Anti)
Dan Gohman0dba0e52008-12-03 19:32:26 +0000223 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000224 unsigned AntiDepReg = Edge->getReg();
Dan Gohman0dba0e52008-12-03 19:32:26 +0000225 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Dan Gohman21d90032008-11-25 00:52:40 +0000226 // Don't break anti-dependencies on non-allocatable registers.
227 if (!AllocatableSet.test(AntiDepReg))
228 continue;
229 // If the SUnit has other dependencies on the SUnit that it
230 // anti-depends on, don't bother breaking the anti-dependency.
231 // Also, if there are dependencies on other SUnits with the
232 // same register as the anti-dependency, don't attempt to
233 // break it.
234 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
235 P != PE; ++P)
Dan Gohman54e4c362008-12-09 22:54:47 +0000236 if (P->getSUnit() == NextSU ?
237 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
238 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
Dan Gohman21d90032008-11-25 00:52:40 +0000239 AntiDepReg = 0;
240 break;
241 }
242 if (AntiDepReg != 0)
243 CriticalAntiDeps[SU->getInstr()] = AntiDepReg;
244 }
245
246 // For live regs that are only used in one register class in a live range,
Dan Gohmane96cc772008-12-03 19:38:38 +0000247 // the register class. If the register is not live, the corresponding value
248 // is null. If the register is live but used in multiple register classes,
249 // the corresponding value is -1 casted to a pointer.
Dan Gohman21d90032008-11-25 00:52:40 +0000250 const TargetRegisterClass *
251 Classes[TargetRegisterInfo::FirstVirtualRegister] = {};
252
253 // Map registers to all their references within a live range.
254 std::multimap<unsigned, MachineOperand *> RegRefs;
255
256 // The index of the most recent kill (proceding bottom-up), or -1 if
257 // the register is not live.
258 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
259 std::fill(KillIndices, array_endof(KillIndices), -1);
260 // The index of the most recent def (proceding bottom up), or -1 if
261 // the register is live.
262 unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
263 std::fill(DefIndices, array_endof(DefIndices), BB->size());
264
265 // Determine the live-out physregs for this block.
266 if (!BB->empty() && BB->back().getDesc().isReturn())
267 // In a return block, examine the function live-out regs.
268 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
269 E = MRI.liveout_end(); I != E; ++I) {
270 unsigned Reg = *I;
271 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
272 KillIndices[Reg] = BB->size();
273 DefIndices[Reg] = -1;
274 // Repeat, for all aliases.
275 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
276 unsigned AliasReg = *Alias;
277 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
278 KillIndices[AliasReg] = BB->size();
279 DefIndices[AliasReg] = -1;
280 }
281 }
282 else
283 // In a non-return block, examine the live-in regs of all successors.
284 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
285 SE = BB->succ_end(); SI != SE; ++SI)
286 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
287 E = (*SI)->livein_end(); I != E; ++I) {
288 unsigned Reg = *I;
289 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
290 KillIndices[Reg] = BB->size();
291 DefIndices[Reg] = -1;
292 // Repeat, for all aliases.
293 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
294 unsigned AliasReg = *Alias;
295 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
296 KillIndices[AliasReg] = BB->size();
297 DefIndices[AliasReg] = -1;
298 }
299 }
300
301 // Consider callee-saved registers as live-out, since we're running after
302 // prologue/epilogue insertion so there's no way to add additional
303 // saved registers.
304 //
305 // TODO: If the callee saves and restores these, then we can potentially
306 // use them between the save and the restore. To do that, we could scan
307 // the exit blocks to see which of these registers are defined.
Dan Gohmanebb0a312008-12-03 19:30:13 +0000308 // Alternatively, calle-saved registers that aren't saved and restored
309 // could be marked live-in in every block.
Dan Gohman21d90032008-11-25 00:52:40 +0000310 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
311 unsigned Reg = *I;
312 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
313 KillIndices[Reg] = BB->size();
314 DefIndices[Reg] = -1;
315 // Repeat, for all aliases.
316 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
317 unsigned AliasReg = *Alias;
318 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
319 KillIndices[AliasReg] = BB->size();
320 DefIndices[AliasReg] = -1;
321 }
322 }
323
324 // Consider this pattern:
325 // A = ...
326 // ... = A
327 // A = ...
328 // ... = A
329 // A = ...
330 // ... = A
331 // A = ...
332 // ... = A
333 // There are three anti-dependencies here, and without special care,
334 // we'd break all of them using the same register:
335 // A = ...
336 // ... = A
337 // B = ...
338 // ... = B
339 // B = ...
340 // ... = B
341 // B = ...
342 // ... = B
343 // because at each anti-dependence, B is the first register that
344 // isn't A which is free. This re-introduces anti-dependencies
345 // at all but one of the original anti-dependencies that we were
346 // trying to break. To avoid this, keep track of the most recent
347 // register that each register was replaced with, avoid avoid
348 // using it to repair an anti-dependence on the same register.
349 // This lets us produce this:
350 // A = ...
351 // ... = A
352 // B = ...
353 // ... = B
354 // C = ...
355 // ... = C
356 // B = ...
357 // ... = B
358 // This still has an anti-dependence on B, but at least it isn't on the
359 // original critical path.
360 //
361 // TODO: If we tracked more than one register here, we could potentially
362 // fix that remaining critical edge too. This is a little more involved,
363 // because unlike the most recent register, less recent registers should
364 // still be considered, though only if no other registers are available.
365 unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
366
Dan Gohman21d90032008-11-25 00:52:40 +0000367 // Attempt to break anti-dependence edges on the critical path. Walk the
368 // instructions from the bottom up, tracking information about liveness
369 // as we go to help determine which registers are available.
370 bool Changed = false;
371 unsigned Count = BB->size() - 1;
372 for (MachineBasicBlock::reverse_iterator I = BB->rbegin(), E = BB->rend();
373 I != E; ++I, --Count) {
374 MachineInstr *MI = &*I;
375
Dan Gohman490b1832008-12-05 05:30:02 +0000376 // After regalloc, IMPLICIT_DEF instructions aren't safe to treat as
377 // dependence-breaking. In the case of an INSERT_SUBREG, the IMPLICIT_DEF
378 // is left behind appearing to clobber the super-register, while the
379 // subregister needs to remain live. So we just ignore them.
380 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
381 continue;
382
Dan Gohman21d90032008-11-25 00:52:40 +0000383 // Check if this instruction has an anti-dependence that we're
384 // interested in.
385 DenseMap<MachineInstr *, unsigned>::iterator C = CriticalAntiDeps.find(MI);
386 unsigned AntiDepReg = C != CriticalAntiDeps.end() ?
387 C->second : 0;
388
389 // Scan the register operands for this instruction and update
390 // Classes and RegRefs.
391 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
392 MachineOperand &MO = MI->getOperand(i);
393 if (!MO.isReg()) continue;
394 unsigned Reg = MO.getReg();
395 if (Reg == 0) continue;
396 const TargetRegisterClass *NewRC =
397 getInstrOperandRegClass(TRI, TII, MI->getDesc(), i);
398
399 // If this instruction has a use of AntiDepReg, breaking it
400 // is invalid.
401 if (MO.isUse() && AntiDepReg == Reg)
402 AntiDepReg = 0;
403
404 // For now, only allow the register to be changed if its register
405 // class is consistent across all uses.
406 if (!Classes[Reg] && NewRC)
407 Classes[Reg] = NewRC;
408 else if (!NewRC || Classes[Reg] != NewRC)
409 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
410
411 // Now check for aliases.
412 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
413 // If an alias of the reg is used during the live range, give up.
414 // Note that this allows us to skip checking if AntiDepReg
415 // overlaps with any of the aliases, among other things.
416 unsigned AliasReg = *Alias;
417 if (Classes[AliasReg]) {
418 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
419 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
420 }
421 }
422
423 // If we're still willing to consider this register, note the reference.
424 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
425 RegRefs.insert(std::make_pair(Reg, &MO));
426 }
427
428 // Determine AntiDepReg's register class, if it is live and is
429 // consistently used within a single class.
430 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
Nick Lewyckya89d1022008-11-27 17:29:52 +0000431 assert((AntiDepReg == 0 || RC != NULL) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000432 "Register should be live if it's causing an anti-dependence!");
433 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
434 AntiDepReg = 0;
435
436 // Look for a suitable register to use to break the anti-depenence.
437 //
438 // TODO: Instead of picking the first free register, consider which might
439 // be the best.
440 if (AntiDepReg != 0) {
441 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(*MF),
442 RE = RC->allocation_order_end(*MF); R != RE; ++R) {
443 unsigned NewReg = *R;
444 // Don't replace a register with itself.
445 if (NewReg == AntiDepReg) continue;
446 // Don't replace a register with one that was recently used to repair
447 // an anti-dependence with this AntiDepReg, because that would
448 // re-introduce that anti-dependence.
449 if (NewReg == LastNewReg[AntiDepReg]) continue;
450 // If NewReg is dead and NewReg's most recent def is not before
451 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Dan Gohman878ef1d2008-11-25 18:53:54 +0000452 assert(((KillIndices[AntiDepReg] == -1u) != (DefIndices[AntiDepReg] == -1u)) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000453 "Kill and Def maps aren't consistent for AntiDepReg!");
Dan Gohman878ef1d2008-11-25 18:53:54 +0000454 assert(((KillIndices[NewReg] == -1u) != (DefIndices[NewReg] == -1u)) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000455 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohman878ef1d2008-11-25 18:53:54 +0000456 if (KillIndices[NewReg] == -1u &&
Dan Gohman21d90032008-11-25 00:52:40 +0000457 KillIndices[AntiDepReg] <= DefIndices[NewReg]) {
Dan Gohman80e201b2008-12-04 02:15:26 +0000458 DOUT << "Breaking anti-dependence edge on "
459 << TRI->getName(AntiDepReg)
Dan Gohmancef874a2008-12-03 23:07:27 +0000460 << " with " << RegRefs.count(AntiDepReg) << " references"
Dan Gohman80e201b2008-12-04 02:15:26 +0000461 << " using " << TRI->getName(NewReg) << "!\n";
Dan Gohman21d90032008-11-25 00:52:40 +0000462
463 // Update the references to the old register to refer to the new
464 // register.
465 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
466 std::multimap<unsigned, MachineOperand *>::iterator>
467 Range = RegRefs.equal_range(AntiDepReg);
468 for (std::multimap<unsigned, MachineOperand *>::iterator
469 Q = Range.first, QE = Range.second; Q != QE; ++Q)
470 Q->second->setReg(NewReg);
471
472 // We just went back in time and modified history; the
473 // liveness information for the anti-depenence reg is now
474 // inconsistent. Set the state as if it were dead.
475 Classes[NewReg] = Classes[AntiDepReg];
476 DefIndices[NewReg] = DefIndices[AntiDepReg];
477 KillIndices[NewReg] = KillIndices[AntiDepReg];
478
479 Classes[AntiDepReg] = 0;
480 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
481 KillIndices[AntiDepReg] = -1;
482
483 RegRefs.erase(AntiDepReg);
484 Changed = true;
485 LastNewReg[AntiDepReg] = NewReg;
486 break;
487 }
488 }
489 }
490
491 // Update liveness.
Dan Gohmancef874a2008-12-03 23:07:27 +0000492 // Proceding upwards, registers that are defed but not used in this
493 // instruction are now dead.
Dan Gohman21d90032008-11-25 00:52:40 +0000494 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
495 MachineOperand &MO = MI->getOperand(i);
496 if (!MO.isReg()) continue;
497 unsigned Reg = MO.getReg();
498 if (Reg == 0) continue;
Dan Gohmancef874a2008-12-03 23:07:27 +0000499 if (!MO.isDef()) continue;
500 // Ignore two-addr defs.
Dan Gohman2ce7f202008-12-05 05:45:42 +0000501 if (MI->isRegReDefinedByTwoAddr(i)) continue;
Dan Gohmancef874a2008-12-03 23:07:27 +0000502
Dan Gohman21d90032008-11-25 00:52:40 +0000503 DefIndices[Reg] = Count;
504 KillIndices[Reg] = -1;
505 Classes[Reg] = 0;
506 RegRefs.erase(Reg);
507 // Repeat, for all subregs.
508 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
509 *Subreg; ++Subreg) {
510 unsigned SubregReg = *Subreg;
511 DefIndices[SubregReg] = Count;
512 KillIndices[SubregReg] = -1;
513 Classes[SubregReg] = 0;
514 RegRefs.erase(SubregReg);
515 }
Dan Gohman3f237442008-12-16 03:25:46 +0000516 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
517 *Super; ++Super) {
518 unsigned SuperReg = *Super;
519 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
520 }
Dan Gohman21d90032008-11-25 00:52:40 +0000521 }
Dan Gohmancef874a2008-12-03 23:07:27 +0000522 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
523 MachineOperand &MO = MI->getOperand(i);
524 if (!MO.isReg()) continue;
525 unsigned Reg = MO.getReg();
526 if (Reg == 0) continue;
527 if (!MO.isUse()) continue;
528
529 const TargetRegisterClass *NewRC =
530 getInstrOperandRegClass(TRI, TII, MI->getDesc(), i);
531
532 // For now, only allow the register to be changed if its register
533 // class is consistent across all uses.
534 if (!Classes[Reg] && NewRC)
535 Classes[Reg] = NewRC;
536 else if (!NewRC || Classes[Reg] != NewRC)
537 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
538
539 RegRefs.insert(std::make_pair(Reg, &MO));
540
541 // It wasn't previously live but now it is, this is a kill.
542 if (KillIndices[Reg] == -1u) {
543 KillIndices[Reg] = Count;
544 DefIndices[Reg] = -1u;
545 }
546 // Repeat, for all aliases.
547 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
548 unsigned AliasReg = *Alias;
549 if (KillIndices[AliasReg] == -1u) {
550 KillIndices[AliasReg] = Count;
551 DefIndices[AliasReg] = -1u;
552 }
553 }
554 }
Dan Gohman21d90032008-11-25 00:52:40 +0000555 }
556 assert(Count == -1u && "Count mismatch!");
557
558 return Changed;
559}
560
Dan Gohman343f0c02008-11-19 23:18:57 +0000561//===----------------------------------------------------------------------===//
562// Top-Down Scheduling
563//===----------------------------------------------------------------------===//
564
565/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
566/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +0000567void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
568 SUnit *SuccSU = SuccEdge->getSUnit();
Dan Gohman343f0c02008-11-19 23:18:57 +0000569 --SuccSU->NumPredsLeft;
570
571#ifndef NDEBUG
572 if (SuccSU->NumPredsLeft < 0) {
573 cerr << "*** Scheduling failed! ***\n";
574 SuccSU->dump(this);
575 cerr << " has been released too many times!\n";
576 assert(0);
577 }
578#endif
579
580 // Compute how many cycles it will be before this actually becomes
581 // available. This is the max of the start time of all predecessors plus
582 // their latencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000583 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Dan Gohman343f0c02008-11-19 23:18:57 +0000584
585 if (SuccSU->NumPredsLeft == 0) {
586 PendingQueue.push_back(SuccSU);
587 }
588}
589
590/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
591/// count of its successors. If a successor pending count is zero, add it to
592/// the Available queue.
593void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
594 DOUT << "*** Scheduling [" << CurCycle << "]: ";
595 DEBUG(SU->dump(this));
596
597 Sequence.push_back(SU);
Dan Gohman3f237442008-12-16 03:25:46 +0000598 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
599 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000600
601 // Top down: release successors.
602 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
603 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000604 ReleaseSucc(SU, &*I);
Dan Gohman343f0c02008-11-19 23:18:57 +0000605
606 SU->isScheduled = true;
607 AvailableQueue.ScheduledNode(SU);
608}
609
610/// ListScheduleTopDown - The main loop of list scheduling for top-down
611/// schedulers.
612void SchedulePostRATDList::ListScheduleTopDown() {
613 unsigned CurCycle = 0;
614
615 // All leaves to Available queue.
616 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
617 // It is available if it has no predecessors.
618 if (SUnits[i].Preds.empty()) {
619 AvailableQueue.push(&SUnits[i]);
620 SUnits[i].isAvailable = true;
621 }
622 }
623
624 // While Available queue is not empty, grab the node with the highest
625 // priority. If it is not ready put it back. Schedule the node.
626 Sequence.reserve(SUnits.size());
627 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
628 // Check to see if any of the pending instructions are ready to issue. If
629 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000630 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000631 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman3f237442008-12-16 03:25:46 +0000632 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000633 AvailableQueue.push(PendingQueue[i]);
634 PendingQueue[i]->isAvailable = true;
635 PendingQueue[i] = PendingQueue.back();
636 PendingQueue.pop_back();
637 --i; --e;
Dan Gohman3f237442008-12-16 03:25:46 +0000638 } else if (PendingQueue[i]->getDepth() < MinDepth)
639 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000640 }
641
Dan Gohman21d90032008-11-25 00:52:40 +0000642 // If there are no instructions available, don't try to issue anything.
Dan Gohman343f0c02008-11-19 23:18:57 +0000643 if (AvailableQueue.empty()) {
Dan Gohman3f237442008-12-16 03:25:46 +0000644 CurCycle = MinDepth != ~0u ? MinDepth : CurCycle + 1;
Dan Gohman343f0c02008-11-19 23:18:57 +0000645 continue;
646 }
647
648 SUnit *FoundSUnit = AvailableQueue.pop();
649
650 // If we found a node to schedule, do it now.
651 if (FoundSUnit) {
652 ScheduleNodeTopDown(FoundSUnit, CurCycle);
653
654 // If this is a pseudo-op node, we don't want to increment the current
655 // cycle.
656 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
657 ++CurCycle;
658 } else {
659 // Otherwise, we have a pipeline stall, but no other problem, just advance
660 // the current cycle and try again.
661 DOUT << "*** Advancing cycle, no work to do\n";
662 ++NumStalls;
663 ++CurCycle;
664 }
665 }
666
667#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000668 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000669#endif
670}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000671
672//===----------------------------------------------------------------------===//
673// Public Constructor Functions
674//===----------------------------------------------------------------------===//
675
676FunctionPass *llvm::createPostRAScheduler() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000677 return new PostRAScheduler();
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000678}