blob: 447beffcc339a4ec72a1ce148ec81b074532ebfc [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
163/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000164class ScheduleDAGList : public ScheduleDAG {
165private:
Evan Chengab495562006-01-25 09:14:32 +0000166 // SDNode to SUnit mapping (many to one).
167 std::map<SDNode*, SUnit*> SUnitMap;
Evan Chengab495562006-01-25 09:14:32 +0000168 // The schedule.
169 std::vector<SUnit*> Sequence;
170 // Current scheduling cycle.
171 unsigned CurrCycle;
Evan Chengc4c339c2006-01-26 00:30:29 +0000172 // First and last SUnit created.
173 SUnit *HeadSUnit, *TailSUnit;
Evan Cheng31272342006-01-23 08:26:10 +0000174
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000175 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
176 /// it is top-down.
177 bool isBottomUp;
178
Chris Lattner7a36d972006-03-05 20:21:55 +0000179 typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort>
180 AvailableQueueTy;
181
Evan Cheng31272342006-01-23 08:26:10 +0000182public:
183 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000184 const TargetMachine &tm, bool isbottomup)
Evan Chengc4c339c2006-01-26 00:30:29 +0000185 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000186 CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL), isBottomUp(isbottomup) {}
Evan Chengab495562006-01-25 09:14:32 +0000187
188 ~ScheduleDAGList() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000189 SUnit *SU = HeadSUnit;
190 while (SU) {
191 SUnit *NextSU = SU->Next;
192 delete SU;
193 SU = NextSU;
Evan Chengab495562006-01-25 09:14:32 +0000194 }
195 }
Evan Cheng31272342006-01-23 08:26:10 +0000196
197 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000198
Evan Chengab495562006-01-25 09:14:32 +0000199 void dump() const;
200
201private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000202 SUnit *NewSUnit(SDNode *N);
Chris Lattner7a36d972006-03-05 20:21:55 +0000203 void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000204 void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false);
205 void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU);
206 void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU);
Evan Chengab495562006-01-25 09:14:32 +0000207 int CalcNodePriority(SUnit *SU);
208 void CalculatePriorities();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000209 void ListScheduleTopDown();
210 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000211 void BuildSchedUnits();
212 void EmitSchedule();
213};
214} // end namespace
215
Evan Chengc4c339c2006-01-26 00:30:29 +0000216
217/// NewSUnit - Creates a new SUnit and return a ptr to it.
218SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
219 SUnit *CurrSUnit = new SUnit(N);
220
221 if (HeadSUnit == NULL)
222 HeadSUnit = CurrSUnit;
223 if (TailSUnit != NULL)
224 TailSUnit->Next = CurrSUnit;
225 TailSUnit = CurrSUnit;
226
227 return CurrSUnit;
228}
229
230/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
231/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner7a36d972006-03-05 20:21:55 +0000232void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available,
233 SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000234 // FIXME: the distance between two nodes is not always == the predecessor's
235 // latency. For example, the reader can very well read the register written
236 // by the predecessor later than the issue cycle. It also depends on the
237 // interrupt model (drain vs. freeze).
238 PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency);
239
240 if (!isChain) {
241 PredSU->NumSuccsLeft--;
242 PredSU->Priority1++;
243 } else
244 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000245
Evan Chengab495562006-01-25 09:14:32 +0000246#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000247 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000248 std::cerr << "*** List scheduling failed! ***\n";
249 PredSU->dump(&DAG);
250 std::cerr << " has been released too many times!\n";
251 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000252 }
Evan Chengab495562006-01-25 09:14:32 +0000253#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000254
255 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
256 // EntryToken has to go last! Special case it here.
257 if (PredSU->Node->getOpcode() != ISD::EntryToken)
258 Available.push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000259 }
Evan Chengab495562006-01-25 09:14:32 +0000260}
261
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000262/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
263/// the Available queue is the count reaches zero. Also update its cycle bound.
264void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available,
265 SUnit *SuccSU, bool isChain) {
266 // FIXME: the distance between two nodes is not always == the predecessor's
267 // latency. For example, the reader can very well read the register written
268 // by the predecessor later than the issue cycle. It also depends on the
269 // interrupt model (drain vs. freeze).
270 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurrCycle + SuccSU->Latency);
271
272 if (!isChain) {
273 SuccSU->NumPredsLeft--;
274 SuccSU->Priority1++; // FIXME: ??
275 } else
276 SuccSU->NumChainPredsLeft--;
277
278#ifndef NDEBUG
279 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
280 std::cerr << "*** List scheduling failed! ***\n";
281 SuccSU->dump(&DAG);
282 std::cerr << " has been released too many times!\n";
283 abort();
284 }
285#endif
286
287 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
288 Available.push(SuccSU);
289}
290
291/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
292/// count of its predecessors. If a predecessor pending count is zero, add it to
293/// the Available queue.
294void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available,
295 SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000296 DEBUG(std::cerr << "*** Scheduling: ");
297 DEBUG(SU->dump(&DAG, false));
298
Evan Chengab495562006-01-25 09:14:32 +0000299 Sequence.push_back(SU);
300 SU->Slot = CurrCycle;
301
302 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000303 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
304 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000305 ReleasePred(Available, *I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000306 SU->NumPredsLeft--;
307 SU->Priority1--;
308 }
309 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
310 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner7a36d972006-03-05 20:21:55 +0000311 ReleasePred(Available, *I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000312
313 CurrCycle++;
314}
315
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000316/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
317/// count of its successors. If a successor pending count is zero, add it to
318/// the Available queue.
319void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available,
320 SUnit *SU) {
321 DEBUG(std::cerr << "*** Scheduling: ");
322 DEBUG(SU->dump(&DAG, false));
323
324 Sequence.push_back(SU);
325 SU->Slot = CurrCycle;
326
327 // Bottom up: release successors.
328 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
329 E1 = SU->Succs.end(); I1 != E1; ++I1) {
330 ReleaseSucc(Available, *I1);
331 SU->NumSuccsLeft--;
332 SU->Priority1--; // FIXME: what is this??
333 }
334 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
335 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
336 ReleaseSucc(Available, *I2, true);
337
338 CurrCycle++;
339}
340
Evan Chengab495562006-01-25 09:14:32 +0000341/// isReady - True if node's lower cycle bound is less or equal to the current
342/// scheduling cycle. Always true if all nodes have uniform latency 1.
343static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
344 return SU->CycleBound <= CurrCycle;
345}
346
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000347/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
348/// schedulers.
349void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000350 // Available queue.
351 AvailableQueueTy Available;
352
353 // Add root to Available queue.
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000354 Available.push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000355
356 // While Available queue is not empty, grab the node with the highest
357 // priority. If it is not ready put it back. Schedule the node.
358 std::vector<SUnit*> NotReady;
359 while (!Available.empty()) {
360 SUnit *CurrNode = Available.top();
361 Available.pop();
362
Evan Chengab495562006-01-25 09:14:32 +0000363 while (!isReady(CurrNode, CurrCycle)) {
364 NotReady.push_back(CurrNode);
365 CurrNode = Available.top();
366 Available.pop();
367 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000368
369 // Add the nodes that aren't ready back onto the available list.
370 while (!NotReady.empty()) {
371 Available.push(NotReady.back());
372 NotReady.pop_back();
373 }
Evan Chengab495562006-01-25 09:14:32 +0000374
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000375 ScheduleNodeBottomUp(Available, CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000376 }
377
378 // Add entry node last
379 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
380 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
381 Entry->Slot = CurrCycle;
382 Sequence.push_back(Entry);
383 }
384
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000385 // Reverse the order if it is bottom up.
386 std::reverse(Sequence.begin(), Sequence.end());
387
388
Evan Chengab495562006-01-25 09:14:32 +0000389#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000390 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000391 bool AnyNotSched = false;
392 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000393 if (SU->NumSuccsLeft != 0 || SU->NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000394 if (!AnyNotSched)
395 std::cerr << "*** List scheduling failed! ***\n";
Evan Chengab495562006-01-25 09:14:32 +0000396 SU->dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000397 std::cerr << "has not been scheduled!\n";
398 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000399 }
Evan Chengab495562006-01-25 09:14:32 +0000400 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000401 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000402#endif
Evan Chengab495562006-01-25 09:14:32 +0000403}
404
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000405/// ListScheduleTopDown - The main loop of list scheduling for top-down
406/// schedulers.
407void ScheduleDAGList::ListScheduleTopDown() {
408 // Available queue.
409 AvailableQueueTy Available;
410
411 // Emit the entry node first.
412 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
413 ScheduleNodeTopDown(Available, Entry);
414
415 // All leaves to Available queue.
416 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
417 // It is available if it has no predecessors.
418 if ((SU->Preds.size() + SU->ChainPreds.size()) == 0 && SU != Entry)
419 Available.push(SU);
420 }
421
422 // While Available queue is not empty, grab the node with the highest
423 // priority. If it is not ready put it back. Schedule the node.
424 std::vector<SUnit*> NotReady;
425 while (!Available.empty()) {
426 SUnit *CurrNode = Available.top();
427 Available.pop();
428
429 // FIXME: when priorities make sense, reenable this.
430 while (0 && !isReady(CurrNode, CurrCycle)) {
431 NotReady.push_back(CurrNode);
432 CurrNode = Available.top();
433 Available.pop();
434 }
435
436 // Add the nodes that aren't ready back onto the available list.
437 while (!NotReady.empty()) {
438 Available.push(NotReady.back());
439 NotReady.pop_back();
440 }
441
442 ScheduleNodeTopDown(Available, CurrNode);
443 }
444
445#ifndef NDEBUG
446 // Verify that all SUnits were scheduled.
447 bool AnyNotSched = false;
448 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
449 if (SU->NumPredsLeft != 0 || SU->NumChainPredsLeft != 0) {
450 if (!AnyNotSched)
451 std::cerr << "*** List scheduling failed! ***\n";
452 SU->dump(&DAG);
453 std::cerr << "has not been scheduled!\n";
454 AnyNotSched = true;
455 }
456 }
457 assert(!AnyNotSched);
458#endif
459}
460
461
Evan Cheng4e3904f2006-03-02 21:38:29 +0000462/// CalcNodePriority - Priority1 is just the number of live range genned -
463/// number of live range killed. Priority2 is the Sethi Ullman number. It
464/// returns Priority2 since it is calculated recursively.
465/// Smaller number is the higher priority for Priority2. Reverse is true for
466/// Priority1.
Evan Chengab495562006-01-25 09:14:32 +0000467int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
468 if (SU->Priority2 != INT_MIN)
469 return SU->Priority2;
470
Evan Cheng4e3904f2006-03-02 21:38:29 +0000471 SU->Priority1 = SU->NumPredsLeft - SU->NumSuccsLeft;
Evan Chengab495562006-01-25 09:14:32 +0000472
473 if (SU->Preds.size() == 0) {
474 SU->Priority2 = 1;
475 } else {
476 int Extra = 0;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000477 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
478 E = SU->Preds.end(); I != E; ++I) {
479 SUnit *PredSU = *I;
Evan Chengab495562006-01-25 09:14:32 +0000480 int PredPriority = CalcNodePriority(PredSU);
481 if (PredPriority > SU->Priority2) {
482 SU->Priority2 = PredPriority;
483 Extra = 0;
484 } else if (PredPriority == SU->Priority2)
485 Extra++;
486 }
487
488 if (SU->Node->getOpcode() != ISD::TokenFactor)
489 SU->Priority2 += Extra;
490 else
491 SU->Priority2 = (Extra == 1) ? 0 : Extra-1;
492 }
493
494 return SU->Priority2;
495}
496
497/// CalculatePriorities - Calculate priorities of all scheduling units.
498void ScheduleDAGList::CalculatePriorities() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000499 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000500 // FIXME: assumes uniform latency for now.
501 SU->Latency = 1;
502 (void)CalcNodePriority(SU);
Evan Chengc4c339c2006-01-26 00:30:29 +0000503 DEBUG(SU->dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000504 DEBUG(std::cerr << "\n");
505 }
506}
507
Evan Chengab495562006-01-25 09:14:32 +0000508void ScheduleDAGList::BuildSchedUnits() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000509 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000510 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000511 NodeInfo *NI = &Info[i];
512 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000513 if (isPassiveNode(N))
514 continue;
Evan Chengab495562006-01-25 09:14:32 +0000515
Evan Chengc4c339c2006-01-26 00:30:29 +0000516 SUnit *SU;
517 if (NI->isInGroup()) {
518 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
519 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000520
Evan Chengc4c339c2006-01-26 00:30:29 +0000521 SU = NewSUnit(N);
522 // Find the flagged nodes.
523 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
524 SDNode *Flag = FlagOp.Val;
525 unsigned ResNo = FlagOp.ResNo;
526 while (Flag->getValueType(ResNo) == MVT::Flag) {
527 NodeInfo *FNI = getNI(Flag);
528 assert(FNI->Group == NI->Group);
529 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
530 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000531
Evan Chengc4c339c2006-01-26 00:30:29 +0000532 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
533 Flag = FlagOp.Val;
534 ResNo = FlagOp.ResNo;
535 }
536 } else {
537 SU = NewSUnit(N);
538 }
539 SUnitMap[N] = SU;
540 }
Evan Chengab495562006-01-25 09:14:32 +0000541
Evan Chengc4c339c2006-01-26 00:30:29 +0000542 // Pass 2: add the preds, succs, etc.
543 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
544 SDNode *N = SU->Node;
545 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000546
547 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
548 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000549
550 if (NI->isInGroup()) {
551 // Find all predecessors (of the group).
552 NodeGroupOpIterator NGOI(NI);
553 while (!NGOI.isEnd()) {
554 SDOperand Op = NGOI.next();
555 SDNode *OpN = Op.Val;
556 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
557 NodeInfo *OpNI = getNI(OpN);
558 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
559 assert(VT != MVT::Flag);
560 SUnit *OpSU = SUnitMap[OpN];
561 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000562 if (SU->ChainPreds.insert(OpSU).second)
563 SU->NumChainPredsLeft++;
564 if (OpSU->ChainSuccs.insert(SU).second)
565 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000566 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000567 if (SU->Preds.insert(OpSU).second)
568 SU->NumPredsLeft++;
569 if (OpSU->Succs.insert(SU).second)
570 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000571 }
Evan Chengab495562006-01-25 09:14:32 +0000572 }
573 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000574 } else {
575 // Find node predecessors.
576 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
577 SDOperand Op = N->getOperand(j);
578 SDNode *OpN = Op.Val;
579 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
580 if (!isPassiveNode(OpN)) {
581 assert(VT != MVT::Flag);
582 SUnit *OpSU = SUnitMap[OpN];
583 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000584 if (SU->ChainPreds.insert(OpSU).second)
585 SU->NumChainPredsLeft++;
586 if (OpSU->ChainSuccs.insert(SU).second)
587 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000588 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000589 if (SU->Preds.insert(OpSU).second)
590 SU->NumPredsLeft++;
591 if (OpSU->Succs.insert(SU).second)
592 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000593 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000594 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000595 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000596 }
597 }
Evan Chengab495562006-01-25 09:14:32 +0000598 }
599 }
Evan Chengab495562006-01-25 09:14:32 +0000600}
601
602/// EmitSchedule - Emit the machine code in scheduled order.
603void ScheduleDAGList::EmitSchedule() {
604 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
605 SDNode *N;
606 SUnit *SU = Sequence[i];
607 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
608 N = SU->FlaggedNodes[j];
609 EmitNode(getNI(N));
610 }
611 EmitNode(getNI(SU->Node));
612 }
613}
614
615/// dump - dump the schedule.
616void ScheduleDAGList::dump() const {
617 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
618 SUnit *SU = Sequence[i];
619 SU->dump(&DAG, false);
620 }
621}
622
623/// Schedule - Schedule the DAG using list scheduling.
624/// FIXME: Right now it only supports the burr (bottom up register reducing)
625/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000626void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000627 DEBUG(std::cerr << "********** List Scheduling **********\n");
628
629 // Build scheduling units.
630 BuildSchedUnits();
631
632 // Calculate node prirorities.
633 CalculatePriorities();
634
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000635 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
636 if (isBottomUp)
637 ListScheduleBottomUp();
638 else
639 ListScheduleTopDown();
640
641 DEBUG(std::cerr << "*** Final schedule ***\n");
642 DEBUG(dump());
643 DEBUG(std::cerr << "\n");
644
Evan Chengab495562006-01-25 09:14:32 +0000645 // Emit in scheduled order
646 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000647}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000648
Evan Chengab495562006-01-25 09:14:32 +0000649llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
650 MachineBasicBlock *BB) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000651 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true);
652}
653
654/// createTDG5ListDAGScheduler - This creates a top-down list scheduler for
655/// the PowerPC G5. FIXME: pull the priority function out into the PPC
656/// backend!
657ScheduleDAG* llvm::createTDG5ListDAGScheduler(SelectionDAG &DAG,
658 MachineBasicBlock *BB) {
659 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false);
Evan Cheng31272342006-01-23 08:26:10 +0000660}