blob: 79206365f3d653dabee661958bd58b596df97807 [file] [log] [blame]
Evan Chengab495562006-01-25 09:14:32 +00001//===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===//
Evan Cheng31272342006-01-23 08:26:10 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Evan Cheng and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "sched"
17#include "llvm/CodeGen/ScheduleDAG.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
Evan Chengab495562006-01-25 09:14:32 +000021#include "llvm/Support/Debug.h"
22#include <climits>
23#include <iostream>
Evan Cheng31272342006-01-23 08:26:10 +000024#include <queue>
Evan Cheng4e3904f2006-03-02 21:38:29 +000025#include <set>
26#include <vector>
Evan Cheng31272342006-01-23 08:26:10 +000027using namespace llvm;
28
Evan Chengab495562006-01-25 09:14:32 +000029namespace {
Evan Cheng31272342006-01-23 08:26:10 +000030
Evan Chengab495562006-01-25 09:14:32 +000031/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or a
32/// group of nodes flagged together.
33struct SUnit {
34 SDNode *Node; // Representative node.
35 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
Evan Cheng4e3904f2006-03-02 21:38:29 +000036 std::set<SUnit*> Preds; // All real predecessors.
37 std::set<SUnit*> ChainPreds; // All chain predecessors.
38 std::set<SUnit*> Succs; // All real successors.
39 std::set<SUnit*> ChainSuccs; // All chain successors.
Evan Chengab495562006-01-25 09:14:32 +000040 int NumPredsLeft; // # of preds not scheduled.
41 int NumSuccsLeft; // # of succs not scheduled.
Evan Cheng4e3904f2006-03-02 21:38:29 +000042 int NumChainPredsLeft; // # of chain preds not scheduled.
43 int NumChainSuccsLeft; // # of chain succs not scheduled.
Evan Chengab495562006-01-25 09:14:32 +000044 int Priority1; // Scheduling priority 1.
45 int Priority2; // Scheduling priority 2.
Evan Cheng5e9a6952006-03-03 06:23:43 +000046 bool isTwoAddress; // Is a two-address instruction.
Evan Cheng4e3904f2006-03-02 21:38:29 +000047 bool isDefNUseOperand; // Is a def&use operand.
Evan Chengab495562006-01-25 09:14:32 +000048 unsigned Latency; // Node latency.
49 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
50 unsigned Slot; // Cycle node is scheduled at.
Evan Chengc4c339c2006-01-26 00:30:29 +000051 SUnit *Next;
Evan Chengab495562006-01-25 09:14:32 +000052
53 SUnit(SDNode *node)
54 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
Evan Cheng4e3904f2006-03-02 21:38:29 +000055 NumChainPredsLeft(0), NumChainSuccsLeft(0),
Evan Cheng5e9a6952006-03-03 06:23:43 +000056 Priority1(INT_MIN), Priority2(INT_MIN),
57 isTwoAddress(false), isDefNUseOperand(false),
Evan Cheng4e3904f2006-03-02 21:38:29 +000058 Latency(0), CycleBound(0), Slot(0), Next(NULL) {}
Evan Chengab495562006-01-25 09:14:32 +000059
60 void dump(const SelectionDAG *G, bool All=true) const;
61};
62
63void SUnit::dump(const SelectionDAG *G, bool All) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000064 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000065 Node->dump(G);
66 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000067 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000068 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000069 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000070 FlaggedNodes[i]->dump(G);
71 std::cerr << "\n";
72 }
73 }
74
75 if (All) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000076 std::cerr << " # preds left : " << NumPredsLeft << "\n";
77 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
78 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
79 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
80 std::cerr << " Latency : " << Latency << "\n";
81 std::cerr << " Priority : " << Priority1 << " , "
82 << Priority2 << "\n";
Evan Chengc4c339c2006-01-26 00:30:29 +000083
Evan Chengab495562006-01-25 09:14:32 +000084 if (Preds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000085 std::cerr << " Predecessors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000086 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000087 E = Preds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000088 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000089 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000090 }
91 }
92 if (ChainPreds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000093 std::cerr << " Chained Preds:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000094 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000095 E = ChainPreds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000096 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000097 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000098 }
99 }
100 if (Succs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000101 std::cerr << " Successors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000102 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000103 E = Succs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000104 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000105 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000106 }
107 }
108 if (ChainSuccs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000109 std::cerr << " Chained succs:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000110 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000111 E = ChainSuccs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000112 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000113 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000114 }
115 }
116 }
117}
118
119/// Sorting functions for the Available queue.
120struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
121 bool operator()(const SUnit* left, const SUnit* right) const {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000122 bool LFloater = (left ->Preds.size() == 0);
123 bool RFloater = (right->Preds.size() == 0);
124 int LBonus = (int)left ->isDefNUseOperand;
125 int RBonus = (int)right->isDefNUseOperand;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000126
127 // Special tie breaker: if two nodes share a operand, the one that
128 // use it as a def&use operand is preferred.
129 if (left->isTwoAddress && !right->isTwoAddress) {
130 SDNode *DUNode = left->Node->getOperand(0).Val;
131 if (DUNode->isOperand(right->Node))
132 LBonus++;
133 }
134 if (!left->isTwoAddress && right->isTwoAddress) {
135 SDNode *DUNode = right->Node->getOperand(0).Val;
136 if (DUNode->isOperand(left->Node))
137 RBonus++;
138 }
139
Evan Cheng4e3904f2006-03-02 21:38:29 +0000140 int LPriority1 = left ->Priority1 - LBonus;
141 int RPriority1 = right->Priority1 - RBonus;
142 int LPriority2 = left ->Priority2 + LBonus;
143 int RPriority2 = right->Priority2 + RBonus;
144
145 // Favor floaters (i.e. node with no non-passive predecessors):
146 // e.g. MOV32ri.
147 if (!LFloater && RFloater)
Evan Chengab495562006-01-25 09:14:32 +0000148 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000149 else if (LFloater == RFloater)
150 if (LPriority1 > RPriority1)
Evan Chengab495562006-01-25 09:14:32 +0000151 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000152 else if (LPriority1 == RPriority1)
153 if (LPriority2 < RPriority2)
Evan Chengab495562006-01-25 09:14:32 +0000154 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000155 else if (LPriority1 == RPriority1)
Evan Chengab495562006-01-25 09:14:32 +0000156 if (left->CycleBound > right->CycleBound)
157 return true;
Evan Chengab495562006-01-25 09:14:32 +0000158
159 return false;
Evan Cheng31272342006-01-23 08:26:10 +0000160 }
161};
162
Chris Lattnere50c0922006-03-05 22:45:01 +0000163
164/// HazardRecognizer - This determines whether or not an instruction can be
165/// issued this cycle, and whether or not a noop needs to be inserted to handle
166/// the hazard.
167namespace {
168 class HazardRecognizer {
169 public:
170 virtual ~HazardRecognizer() {}
171
172 enum HazardType {
173 NoHazard, // This instruction can be emitted at this cycle.
174 Hazard, // This instruction can't be emitted at this cycle.
175 NoopHazard, // This instruction can't be emitted, and needs noops.
176 };
177
178 /// getHazardType - Return the hazard type of emitting this node. There are
179 /// three possible results. Either:
180 /// * NoHazard: it is legal to issue this instruction on this cycle.
181 /// * Hazard: issuing this instruction would stall the machine. If some
182 /// other instruction is available, issue it first.
183 /// * NoopHazard: issuing this instruction would break the program. If
184 /// some other instruction can be issued, do so, otherwise issue a noop.
185 virtual HazardType getHazardType(SDNode *Node) {
186 return NoHazard;
187 }
188
189 /// EmitInstruction - This callback is invoked when an instruction is
190 /// emitted, to advance the hazard state.
191 virtual void EmitInstruction(SDNode *Node) {
192 }
193
194 /// AdvanceCycle - This callback is invoked when no instructions can be
195 /// issued on this cycle without a hazard. This should increment the
196 /// internal state of the hazard recognizer so that previously "Hazard"
197 /// instructions will now not be hazards.
198 virtual void AdvanceCycle() {
199 }
200
201 /// EmitNoop - This callback is invoked when a noop was added to the
202 /// instruction stream.
203 virtual void EmitNoop() {
204 }
205 };
206}
207
208
Evan Cheng31272342006-01-23 08:26:10 +0000209/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000210class ScheduleDAGList : public ScheduleDAG {
211private:
Evan Chengab495562006-01-25 09:14:32 +0000212 // SDNode to SUnit mapping (many to one).
213 std::map<SDNode*, SUnit*> SUnitMap;
Evan Chengab495562006-01-25 09:14:32 +0000214 // The schedule.
215 std::vector<SUnit*> Sequence;
216 // Current scheduling cycle.
217 unsigned CurrCycle;
Evan Chengc4c339c2006-01-26 00:30:29 +0000218 // First and last SUnit created.
219 SUnit *HeadSUnit, *TailSUnit;
Evan Cheng31272342006-01-23 08:26:10 +0000220
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000221 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
222 /// it is top-down.
223 bool isBottomUp;
224
Chris Lattnere50c0922006-03-05 22:45:01 +0000225 /// HazardRec - The hazard recognizer to use.
226 HazardRecognizer *HazardRec;
227
Chris Lattner7a36d972006-03-05 20:21:55 +0000228 typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort>
229 AvailableQueueTy;
230
Evan Cheng31272342006-01-23 08:26:10 +0000231public:
232 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000233 const TargetMachine &tm, bool isbottomup,
234 HazardRecognizer *HR = 0)
Evan Chengc4c339c2006-01-26 00:30:29 +0000235 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattnere50c0922006-03-05 22:45:01 +0000236 CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL), isBottomUp(isbottomup) {
237 if (HR == 0) HR = new HazardRecognizer();
238 HazardRec = HR;
239 }
Evan Chengab495562006-01-25 09:14:32 +0000240
241 ~ScheduleDAGList() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000242 SUnit *SU = HeadSUnit;
243 while (SU) {
244 SUnit *NextSU = SU->Next;
245 delete SU;
246 SU = NextSU;
Evan Chengab495562006-01-25 09:14:32 +0000247 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000248
249 delete HazardRec;
Evan Chengab495562006-01-25 09:14:32 +0000250 }
Evan Cheng31272342006-01-23 08:26:10 +0000251
252 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000253
Evan Chengab495562006-01-25 09:14:32 +0000254 void dump() const;
255
256private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000257 SUnit *NewSUnit(SDNode *N);
Chris Lattner7a36d972006-03-05 20:21:55 +0000258 void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000259 void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false);
260 void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU);
261 void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU);
Evan Chengab495562006-01-25 09:14:32 +0000262 int CalcNodePriority(SUnit *SU);
263 void CalculatePriorities();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000264 void ListScheduleTopDown();
265 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000266 void BuildSchedUnits();
267 void EmitSchedule();
268};
269} // end namespace
270
Evan Chengc4c339c2006-01-26 00:30:29 +0000271
272/// NewSUnit - Creates a new SUnit and return a ptr to it.
273SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
274 SUnit *CurrSUnit = new SUnit(N);
275
276 if (HeadSUnit == NULL)
277 HeadSUnit = CurrSUnit;
278 if (TailSUnit != NULL)
279 TailSUnit->Next = CurrSUnit;
280 TailSUnit = CurrSUnit;
281
282 return CurrSUnit;
283}
284
285/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
286/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner7a36d972006-03-05 20:21:55 +0000287void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available,
288 SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000289 // FIXME: the distance between two nodes is not always == the predecessor's
290 // latency. For example, the reader can very well read the register written
291 // by the predecessor later than the issue cycle. It also depends on the
292 // interrupt model (drain vs. freeze).
293 PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency);
294
295 if (!isChain) {
296 PredSU->NumSuccsLeft--;
297 PredSU->Priority1++;
298 } else
299 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000300
Evan Chengab495562006-01-25 09:14:32 +0000301#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000302 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000303 std::cerr << "*** List scheduling failed! ***\n";
304 PredSU->dump(&DAG);
305 std::cerr << " has been released too many times!\n";
306 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000307 }
Evan Chengab495562006-01-25 09:14:32 +0000308#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000309
310 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
311 // EntryToken has to go last! Special case it here.
312 if (PredSU->Node->getOpcode() != ISD::EntryToken)
313 Available.push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000314 }
Evan Chengab495562006-01-25 09:14:32 +0000315}
316
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000317/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
318/// the Available queue is the count reaches zero. Also update its cycle bound.
319void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available,
320 SUnit *SuccSU, bool isChain) {
321 // FIXME: the distance between two nodes is not always == the predecessor's
322 // latency. For example, the reader can very well read the register written
323 // by the predecessor later than the issue cycle. It also depends on the
324 // interrupt model (drain vs. freeze).
325 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurrCycle + SuccSU->Latency);
326
327 if (!isChain) {
328 SuccSU->NumPredsLeft--;
329 SuccSU->Priority1++; // FIXME: ??
330 } else
331 SuccSU->NumChainPredsLeft--;
332
333#ifndef NDEBUG
334 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
335 std::cerr << "*** List scheduling failed! ***\n";
336 SuccSU->dump(&DAG);
337 std::cerr << " has been released too many times!\n";
338 abort();
339 }
340#endif
341
342 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
343 Available.push(SuccSU);
344}
345
346/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
347/// count of its predecessors. If a predecessor pending count is zero, add it to
348/// the Available queue.
349void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available,
350 SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000351 DEBUG(std::cerr << "*** Scheduling: ");
352 DEBUG(SU->dump(&DAG, false));
353
Evan Chengab495562006-01-25 09:14:32 +0000354 Sequence.push_back(SU);
355 SU->Slot = CurrCycle;
356
357 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000358 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
359 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000360 ReleasePred(Available, *I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000361 SU->NumPredsLeft--;
362 SU->Priority1--;
363 }
364 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
365 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner7a36d972006-03-05 20:21:55 +0000366 ReleasePred(Available, *I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000367
368 CurrCycle++;
369}
370
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000371/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
372/// count of its successors. If a successor pending count is zero, add it to
373/// the Available queue.
374void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available,
375 SUnit *SU) {
376 DEBUG(std::cerr << "*** Scheduling: ");
377 DEBUG(SU->dump(&DAG, false));
378
379 Sequence.push_back(SU);
380 SU->Slot = CurrCycle;
381
382 // Bottom up: release successors.
383 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
384 E1 = SU->Succs.end(); I1 != E1; ++I1) {
385 ReleaseSucc(Available, *I1);
386 SU->NumSuccsLeft--;
387 SU->Priority1--; // FIXME: what is this??
388 }
389 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
390 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
391 ReleaseSucc(Available, *I2, true);
392
393 CurrCycle++;
394}
395
Evan Chengab495562006-01-25 09:14:32 +0000396/// isReady - True if node's lower cycle bound is less or equal to the current
397/// scheduling cycle. Always true if all nodes have uniform latency 1.
398static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
399 return SU->CycleBound <= CurrCycle;
400}
401
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000402/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
403/// schedulers.
404void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000405 // Available queue.
406 AvailableQueueTy Available;
407
408 // Add root to Available queue.
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000409 Available.push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000410
411 // While Available queue is not empty, grab the node with the highest
412 // priority. If it is not ready put it back. Schedule the node.
413 std::vector<SUnit*> NotReady;
414 while (!Available.empty()) {
415 SUnit *CurrNode = Available.top();
416 Available.pop();
417
Evan Chengab495562006-01-25 09:14:32 +0000418 while (!isReady(CurrNode, CurrCycle)) {
419 NotReady.push_back(CurrNode);
420 CurrNode = Available.top();
421 Available.pop();
422 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000423
424 // Add the nodes that aren't ready back onto the available list.
425 while (!NotReady.empty()) {
426 Available.push(NotReady.back());
427 NotReady.pop_back();
428 }
Evan Chengab495562006-01-25 09:14:32 +0000429
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000430 ScheduleNodeBottomUp(Available, CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000431 }
432
433 // Add entry node last
434 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
435 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
436 Entry->Slot = CurrCycle;
437 Sequence.push_back(Entry);
438 }
439
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000440 // Reverse the order if it is bottom up.
441 std::reverse(Sequence.begin(), Sequence.end());
442
443
Evan Chengab495562006-01-25 09:14:32 +0000444#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000445 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000446 bool AnyNotSched = false;
447 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000448 if (SU->NumSuccsLeft != 0 || SU->NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000449 if (!AnyNotSched)
450 std::cerr << "*** List scheduling failed! ***\n";
Evan Chengab495562006-01-25 09:14:32 +0000451 SU->dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000452 std::cerr << "has not been scheduled!\n";
453 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000454 }
Evan Chengab495562006-01-25 09:14:32 +0000455 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000456 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000457#endif
Evan Chengab495562006-01-25 09:14:32 +0000458}
459
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000460/// ListScheduleTopDown - The main loop of list scheduling for top-down
461/// schedulers.
462void ScheduleDAGList::ListScheduleTopDown() {
463 // Available queue.
464 AvailableQueueTy Available;
465
466 // Emit the entry node first.
467 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
468 ScheduleNodeTopDown(Available, Entry);
Chris Lattnere50c0922006-03-05 22:45:01 +0000469 HazardRec->EmitInstruction(Entry->Node);
470
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000471 // All leaves to Available queue.
472 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
473 // It is available if it has no predecessors.
474 if ((SU->Preds.size() + SU->ChainPreds.size()) == 0 && SU != Entry)
475 Available.push(SU);
476 }
477
478 // While Available queue is not empty, grab the node with the highest
479 // priority. If it is not ready put it back. Schedule the node.
480 std::vector<SUnit*> NotReady;
481 while (!Available.empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000482 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000483
Chris Lattnere50c0922006-03-05 22:45:01 +0000484 bool HasNoopHazards = false;
485 do {
486 SUnit *CurrNode = Available.top();
487 Available.pop();
488 HazardRecognizer::HazardType HT =
489 HazardRec->getHazardType(CurrNode->Node);
490 if (HT == HazardRecognizer::NoHazard) {
491 FoundNode = CurrNode;
492 break;
493 }
494
495 // Remember if this is a noop hazard.
496 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
497
498 NotReady.push_back(CurrNode);
499 } while (!Available.empty());
500
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000501 // Add the nodes that aren't ready back onto the available list.
502 while (!NotReady.empty()) {
503 Available.push(NotReady.back());
504 NotReady.pop_back();
505 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000506
507 // If we found a node to schedule, do it now.
508 if (FoundNode) {
509 ScheduleNodeTopDown(Available, FoundNode);
510 HazardRec->EmitInstruction(FoundNode->Node);
511 } else if (!HasNoopHazards) {
512 // Otherwise, we have a pipeline stall, but no other problem, just advance
513 // the current cycle and try again.
514 HazardRec->AdvanceCycle();
515 } else {
516 // Otherwise, we have no instructions to issue and we have instructions
517 // that will fault if we don't do this right. This is the case for
518 // processors without pipeline interlocks and other cases.
519 HazardRec->EmitNoop();
520 // FIXME: Add a noop to the schedule!!
521 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000522 }
523
524#ifndef NDEBUG
525 // Verify that all SUnits were scheduled.
526 bool AnyNotSched = false;
527 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
528 if (SU->NumPredsLeft != 0 || SU->NumChainPredsLeft != 0) {
529 if (!AnyNotSched)
530 std::cerr << "*** List scheduling failed! ***\n";
531 SU->dump(&DAG);
532 std::cerr << "has not been scheduled!\n";
533 AnyNotSched = true;
534 }
535 }
536 assert(!AnyNotSched);
537#endif
538}
539
540
Evan Cheng4e3904f2006-03-02 21:38:29 +0000541/// CalcNodePriority - Priority1 is just the number of live range genned -
542/// number of live range killed. Priority2 is the Sethi Ullman number. It
543/// returns Priority2 since it is calculated recursively.
544/// Smaller number is the higher priority for Priority2. Reverse is true for
545/// Priority1.
Evan Chengab495562006-01-25 09:14:32 +0000546int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
547 if (SU->Priority2 != INT_MIN)
548 return SU->Priority2;
549
Evan Cheng4e3904f2006-03-02 21:38:29 +0000550 SU->Priority1 = SU->NumPredsLeft - SU->NumSuccsLeft;
Evan Chengab495562006-01-25 09:14:32 +0000551
552 if (SU->Preds.size() == 0) {
553 SU->Priority2 = 1;
554 } else {
555 int Extra = 0;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000556 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
557 E = SU->Preds.end(); I != E; ++I) {
558 SUnit *PredSU = *I;
Evan Chengab495562006-01-25 09:14:32 +0000559 int PredPriority = CalcNodePriority(PredSU);
560 if (PredPriority > SU->Priority2) {
561 SU->Priority2 = PredPriority;
562 Extra = 0;
563 } else if (PredPriority == SU->Priority2)
564 Extra++;
565 }
566
567 if (SU->Node->getOpcode() != ISD::TokenFactor)
568 SU->Priority2 += Extra;
569 else
570 SU->Priority2 = (Extra == 1) ? 0 : Extra-1;
571 }
572
573 return SU->Priority2;
574}
575
576/// CalculatePriorities - Calculate priorities of all scheduling units.
577void ScheduleDAGList::CalculatePriorities() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000578 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000579 // FIXME: assumes uniform latency for now.
580 SU->Latency = 1;
581 (void)CalcNodePriority(SU);
Evan Chengc4c339c2006-01-26 00:30:29 +0000582 DEBUG(SU->dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000583 DEBUG(std::cerr << "\n");
584 }
585}
586
Evan Chengab495562006-01-25 09:14:32 +0000587void ScheduleDAGList::BuildSchedUnits() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000588 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000589 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000590 NodeInfo *NI = &Info[i];
591 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000592 if (isPassiveNode(N))
593 continue;
Evan Chengab495562006-01-25 09:14:32 +0000594
Evan Chengc4c339c2006-01-26 00:30:29 +0000595 SUnit *SU;
596 if (NI->isInGroup()) {
597 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
598 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000599
Evan Chengc4c339c2006-01-26 00:30:29 +0000600 SU = NewSUnit(N);
601 // Find the flagged nodes.
602 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
603 SDNode *Flag = FlagOp.Val;
604 unsigned ResNo = FlagOp.ResNo;
605 while (Flag->getValueType(ResNo) == MVT::Flag) {
606 NodeInfo *FNI = getNI(Flag);
607 assert(FNI->Group == NI->Group);
608 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
609 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000610
Evan Chengc4c339c2006-01-26 00:30:29 +0000611 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
612 Flag = FlagOp.Val;
613 ResNo = FlagOp.ResNo;
614 }
615 } else {
616 SU = NewSUnit(N);
617 }
618 SUnitMap[N] = SU;
619 }
Evan Chengab495562006-01-25 09:14:32 +0000620
Evan Chengc4c339c2006-01-26 00:30:29 +0000621 // Pass 2: add the preds, succs, etc.
622 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
623 SDNode *N = SU->Node;
624 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000625
626 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
627 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000628
629 if (NI->isInGroup()) {
630 // Find all predecessors (of the group).
631 NodeGroupOpIterator NGOI(NI);
632 while (!NGOI.isEnd()) {
633 SDOperand Op = NGOI.next();
634 SDNode *OpN = Op.Val;
635 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
636 NodeInfo *OpNI = getNI(OpN);
637 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
638 assert(VT != MVT::Flag);
639 SUnit *OpSU = SUnitMap[OpN];
640 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000641 if (SU->ChainPreds.insert(OpSU).second)
642 SU->NumChainPredsLeft++;
643 if (OpSU->ChainSuccs.insert(SU).second)
644 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000645 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000646 if (SU->Preds.insert(OpSU).second)
647 SU->NumPredsLeft++;
648 if (OpSU->Succs.insert(SU).second)
649 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000650 }
Evan Chengab495562006-01-25 09:14:32 +0000651 }
652 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000653 } else {
654 // Find node predecessors.
655 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
656 SDOperand Op = N->getOperand(j);
657 SDNode *OpN = Op.Val;
658 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
659 if (!isPassiveNode(OpN)) {
660 assert(VT != MVT::Flag);
661 SUnit *OpSU = SUnitMap[OpN];
662 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000663 if (SU->ChainPreds.insert(OpSU).second)
664 SU->NumChainPredsLeft++;
665 if (OpSU->ChainSuccs.insert(SU).second)
666 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000667 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000668 if (SU->Preds.insert(OpSU).second)
669 SU->NumPredsLeft++;
670 if (OpSU->Succs.insert(SU).second)
671 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000672 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000673 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000674 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000675 }
676 }
Evan Chengab495562006-01-25 09:14:32 +0000677 }
678 }
Evan Chengab495562006-01-25 09:14:32 +0000679}
680
681/// EmitSchedule - Emit the machine code in scheduled order.
682void ScheduleDAGList::EmitSchedule() {
683 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
684 SDNode *N;
685 SUnit *SU = Sequence[i];
686 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
687 N = SU->FlaggedNodes[j];
688 EmitNode(getNI(N));
689 }
690 EmitNode(getNI(SU->Node));
691 }
692}
693
694/// dump - dump the schedule.
695void ScheduleDAGList::dump() const {
696 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
697 SUnit *SU = Sequence[i];
698 SU->dump(&DAG, false);
699 }
700}
701
702/// Schedule - Schedule the DAG using list scheduling.
703/// FIXME: Right now it only supports the burr (bottom up register reducing)
704/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000705void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000706 DEBUG(std::cerr << "********** List Scheduling **********\n");
707
708 // Build scheduling units.
709 BuildSchedUnits();
710
711 // Calculate node prirorities.
712 CalculatePriorities();
713
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000714 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
715 if (isBottomUp)
716 ListScheduleBottomUp();
717 else
718 ListScheduleTopDown();
719
720 DEBUG(std::cerr << "*** Final schedule ***\n");
721 DEBUG(dump());
722 DEBUG(std::cerr << "\n");
723
Evan Chengab495562006-01-25 09:14:32 +0000724 // Emit in scheduled order
725 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000726}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000727
Evan Chengab495562006-01-25 09:14:32 +0000728llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
729 MachineBasicBlock *BB) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000730 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true);
731}
732
Chris Lattnere50c0922006-03-05 22:45:01 +0000733/// G5HazardRecognizer - A hazard recognizer for the PowerPC G5 processor.
734/// FIXME: Implement
735/// FIXME: Move to the PowerPC backend.
736class G5HazardRecognizer : public HazardRecognizer {
737public:
738 G5HazardRecognizer() {}
739};
740
741
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000742/// createTDG5ListDAGScheduler - This creates a top-down list scheduler for
743/// the PowerPC G5. FIXME: pull the priority function out into the PPC
744/// backend!
745ScheduleDAG* llvm::createTDG5ListDAGScheduler(SelectionDAG &DAG,
746 MachineBasicBlock *BB) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000747 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false,
748 new G5HazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000749}