blob: 1e364763925760819f7b7575caa53756aa4d3046 [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//
Chris Lattner01aa7522006-03-06 17:58:04 +000010// This implements bottom-up and top-down list schedulers, using standard
11// algorithms. The basic approach uses a priority queue of available nodes to
12// schedule. One at a time, nodes are taken from the priority queue (thus in
13// priority 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.
Evan Cheng31272342006-01-23 08:26:10 +000018//
19//===----------------------------------------------------------------------===//
20
21#define DEBUG_TYPE "sched"
22#include "llvm/CodeGen/ScheduleDAG.h"
Evan Cheng31272342006-01-23 08:26:10 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
Evan Chengab495562006-01-25 09:14:32 +000025#include "llvm/Support/Debug.h"
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000026#include "llvm/ADT/Statistic.h"
Evan Chengab495562006-01-25 09:14:32 +000027#include <climits>
28#include <iostream>
Evan Cheng31272342006-01-23 08:26:10 +000029#include <queue>
Evan Cheng4e3904f2006-03-02 21:38:29 +000030#include <set>
31#include <vector>
Evan Cheng31272342006-01-23 08:26:10 +000032using namespace llvm;
33
Evan Chengab495562006-01-25 09:14:32 +000034namespace {
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000035 Statistic<> NumNoops ("scheduler", "Number of noops inserted");
36 Statistic<> NumStalls("scheduler", "Number of pipeline stalls");
Evan Cheng31272342006-01-23 08:26:10 +000037
Chris Lattner12c6d892006-03-08 04:41:06 +000038 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
39 /// a group of nodes flagged together.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000040 struct SUnit {
41 SDNode *Node; // Representative node.
42 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
43 std::set<SUnit*> Preds; // All real predecessors.
44 std::set<SUnit*> ChainPreds; // All chain predecessors.
45 std::set<SUnit*> Succs; // All real successors.
46 std::set<SUnit*> ChainSuccs; // All chain successors.
Chris Lattner12c6d892006-03-08 04:41:06 +000047 short NumPredsLeft; // # of preds not scheduled.
48 short NumSuccsLeft; // # of succs not scheduled.
49 short NumChainPredsLeft; // # of chain preds not scheduled.
50 short NumChainSuccsLeft; // # of chain succs not scheduled.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000051 int SethiUllman; // Sethi Ullman number.
Chris Lattner12c6d892006-03-08 04:41:06 +000052 bool isTwoAddress : 1; // Is a two-address instruction.
53 bool isDefNUseOperand : 1; // Is a def&use operand.
54 unsigned short Latency; // Node latency.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000055 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000056
57 SUnit(SDNode *node)
58 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
Evan Cheng4e3904f2006-03-02 21:38:29 +000059 NumChainPredsLeft(0), NumChainSuccsLeft(0),
Evan Chengc5c06582006-03-06 06:08:54 +000060 SethiUllman(INT_MIN),
Evan Cheng5e9a6952006-03-03 06:23:43 +000061 isTwoAddress(false), isDefNUseOperand(false),
Chris Lattner42e20262006-03-08 04:54:34 +000062 Latency(0), CycleBound(0) {}
Chris Lattneraf5e26c2006-03-08 04:37:58 +000063
64 void dump(const SelectionDAG *G, bool All=true) const;
65 };
66}
Evan Chengab495562006-01-25 09:14:32 +000067
68void SUnit::dump(const SelectionDAG *G, bool All) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000069 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000070 Node->dump(G);
71 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000072 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000073 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000074 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000075 FlaggedNodes[i]->dump(G);
76 std::cerr << "\n";
77 }
78 }
79
80 if (All) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000081 std::cerr << " # preds left : " << NumPredsLeft << "\n";
82 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
83 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
84 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
85 std::cerr << " Latency : " << Latency << "\n";
Evan Chengc5c06582006-03-06 06:08:54 +000086 std::cerr << " SethiUllman : " << SethiUllman << "\n";
Evan Chengc4c339c2006-01-26 00:30:29 +000087
Evan Chengab495562006-01-25 09:14:32 +000088 if (Preds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000089 std::cerr << " Predecessors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000090 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000091 E = Preds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000092 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000093 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000094 }
95 }
96 if (ChainPreds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000097 std::cerr << " Chained Preds:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000098 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000099 E = ChainPreds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000100 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000101 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000102 }
103 }
104 if (Succs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000105 std::cerr << " Successors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000106 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000107 E = Succs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000108 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000109 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000110 }
111 }
112 if (ChainSuccs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000113 std::cerr << " Chained succs:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000114 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000115 E = ChainSuccs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000116 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000117 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000118 }
119 }
120 }
121}
122
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000123namespace {
Evan Chengab495562006-01-25 09:14:32 +0000124/// Sorting functions for the Available queue.
125struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
126 bool operator()(const SUnit* left, const SUnit* right) const {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000127 int LBonus = (int)left ->isDefNUseOperand;
128 int RBonus = (int)right->isDefNUseOperand;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000129
130 // Special tie breaker: if two nodes share a operand, the one that
131 // use it as a def&use operand is preferred.
132 if (left->isTwoAddress && !right->isTwoAddress) {
133 SDNode *DUNode = left->Node->getOperand(0).Val;
134 if (DUNode->isOperand(right->Node))
135 LBonus++;
136 }
137 if (!left->isTwoAddress && right->isTwoAddress) {
138 SDNode *DUNode = right->Node->getOperand(0).Val;
139 if (DUNode->isOperand(left->Node))
140 RBonus++;
141 }
142
Evan Chengc5c06582006-03-06 06:08:54 +0000143 // Priority1 is just the number of live range genned.
144 int LPriority1 = left ->NumPredsLeft - LBonus;
145 int RPriority1 = right->NumPredsLeft - RBonus;
146 int LPriority2 = left ->SethiUllman + LBonus;
147 int RPriority2 = right->SethiUllman + RBonus;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000148
Evan Chenga00c6192006-03-06 07:31:44 +0000149 if (LPriority1 > RPriority1)
Evan Chengab495562006-01-25 09:14:32 +0000150 return true;
Evan Chenga00c6192006-03-06 07:31:44 +0000151 else if (LPriority1 == RPriority1)
152 if (LPriority2 < RPriority2)
Evan Chengab495562006-01-25 09:14:32 +0000153 return true;
Evan Chenga00c6192006-03-06 07:31:44 +0000154 else if (LPriority2 == RPriority2)
155 if (left->CycleBound > right->CycleBound)
Evan Chengab495562006-01-25 09:14:32 +0000156 return true;
Evan Chengab495562006-01-25 09:14:32 +0000157
158 return false;
Evan Cheng31272342006-01-23 08:26:10 +0000159 }
160};
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000161} // end anonymous namespace
Evan Cheng31272342006-01-23 08:26:10 +0000162
Chris Lattnere50c0922006-03-05 22:45:01 +0000163
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000164namespace {
Evan Cheng31272342006-01-23 08:26:10 +0000165/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000166class ScheduleDAGList : public ScheduleDAG {
167private:
Evan Chengab495562006-01-25 09:14:32 +0000168 // SDNode to SUnit mapping (many to one).
169 std::map<SDNode*, SUnit*> SUnitMap;
Chris Lattner00b52ea2006-03-05 23:59:20 +0000170 // The schedule. Null SUnit*'s represent noop instructions.
Evan Chengab495562006-01-25 09:14:32 +0000171 std::vector<SUnit*> Sequence;
172 // Current scheduling cycle.
173 unsigned CurrCycle;
Chris Lattner42e20262006-03-08 04:54:34 +0000174
175 // The scheduling units.
176 std::vector<SUnit> SUnits;
Evan Cheng31272342006-01-23 08:26:10 +0000177
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000178 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
179 /// it is top-down.
180 bool isBottomUp;
181
Chris Lattnere50c0922006-03-05 22:45:01 +0000182 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +0000183 HazardRecognizer *HazardRec;
Chris Lattnere50c0922006-03-05 22:45:01 +0000184
Chris Lattner7a36d972006-03-05 20:21:55 +0000185 typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort>
186 AvailableQueueTy;
187
Evan Cheng31272342006-01-23 08:26:10 +0000188public:
189 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000190 const TargetMachine &tm, bool isbottomup,
Chris Lattner543832d2006-03-08 04:25:59 +0000191 HazardRecognizer *HR)
Evan Chengc4c339c2006-01-26 00:30:29 +0000192 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner42e20262006-03-08 04:54:34 +0000193 CurrCycle(0), isBottomUp(isbottomup), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000194 }
Evan Chengab495562006-01-25 09:14:32 +0000195
196 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +0000197 delete HazardRec;
Evan Chengab495562006-01-25 09:14:32 +0000198 }
Evan Cheng31272342006-01-23 08:26:10 +0000199
200 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000201
Evan Chengab495562006-01-25 09:14:32 +0000202 void dump() const;
203
204private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000205 SUnit *NewSUnit(SDNode *N);
Chris Lattner7a36d972006-03-05 20:21:55 +0000206 void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000207 void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false);
208 void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU);
209 void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU);
Evan Chengab495562006-01-25 09:14:32 +0000210 int CalcNodePriority(SUnit *SU);
211 void CalculatePriorities();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000212 void ListScheduleTopDown();
213 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000214 void BuildSchedUnits();
215 void EmitSchedule();
216};
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000217} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +0000218
Chris Lattner47639db2006-03-06 00:22:00 +0000219HazardRecognizer::~HazardRecognizer() {}
220
Evan Chengc4c339c2006-01-26 00:30:29 +0000221
222/// NewSUnit - Creates a new SUnit and return a ptr to it.
223SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
Chris Lattner42e20262006-03-08 04:54:34 +0000224 SUnits.push_back(N);
225 return &SUnits.back();
Evan Chengc4c339c2006-01-26 00:30:29 +0000226}
227
228/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
229/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner7a36d972006-03-05 20:21:55 +0000230void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available,
231 SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000232 // FIXME: the distance between two nodes is not always == the predecessor's
233 // latency. For example, the reader can very well read the register written
234 // by the predecessor later than the issue cycle. It also depends on the
235 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000236 PredSU->CycleBound = std::max(PredSU->CycleBound,CurrCycle + PredSU->Latency);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000237
Evan Chengc5c06582006-03-06 06:08:54 +0000238 if (!isChain)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000239 PredSU->NumSuccsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000240 else
Evan Cheng4e3904f2006-03-02 21:38:29 +0000241 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000242
Evan Chengab495562006-01-25 09:14:32 +0000243#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000244 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000245 std::cerr << "*** List scheduling failed! ***\n";
246 PredSU->dump(&DAG);
247 std::cerr << " has been released too many times!\n";
248 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000249 }
Evan Chengab495562006-01-25 09:14:32 +0000250#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000251
252 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
253 // EntryToken has to go last! Special case it here.
254 if (PredSU->Node->getOpcode() != ISD::EntryToken)
255 Available.push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000256 }
Evan Chengab495562006-01-25 09:14:32 +0000257}
258
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000259/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
260/// the Available queue is the count reaches zero. Also update its cycle bound.
261void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available,
262 SUnit *SuccSU, bool isChain) {
263 // FIXME: the distance between two nodes is not always == the predecessor's
264 // latency. For example, the reader can very well read the register written
265 // by the predecessor later than the issue cycle. It also depends on the
266 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000267 SuccSU->CycleBound = std::max(SuccSU->CycleBound,CurrCycle + SuccSU->Latency);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000268
Evan Chengc5c06582006-03-06 06:08:54 +0000269 if (!isChain)
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000270 SuccSU->NumPredsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000271 else
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000272 SuccSU->NumChainPredsLeft--;
273
274#ifndef NDEBUG
275 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
276 std::cerr << "*** List scheduling failed! ***\n";
277 SuccSU->dump(&DAG);
278 std::cerr << " has been released too many times!\n";
279 abort();
280 }
281#endif
282
283 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
284 Available.push(SuccSU);
285}
286
287/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
288/// count of its predecessors. If a predecessor pending count is zero, add it to
289/// the Available queue.
290void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available,
291 SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000292 DEBUG(std::cerr << "*** Scheduling: ");
293 DEBUG(SU->dump(&DAG, false));
294
Evan Chengab495562006-01-25 09:14:32 +0000295 Sequence.push_back(SU);
Evan Chengab495562006-01-25 09:14:32 +0000296
297 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000298 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
299 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000300 ReleasePred(Available, *I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000301 SU->NumPredsLeft--;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000302 }
303 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
304 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner7a36d972006-03-05 20:21:55 +0000305 ReleasePred(Available, *I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000306
307 CurrCycle++;
308}
309
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000310/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
311/// count of its successors. If a successor pending count is zero, add it to
312/// the Available queue.
313void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available,
314 SUnit *SU) {
315 DEBUG(std::cerr << "*** Scheduling: ");
316 DEBUG(SU->dump(&DAG, false));
317
318 Sequence.push_back(SU);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000319
320 // Bottom up: release successors.
321 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
322 E1 = SU->Succs.end(); I1 != E1; ++I1) {
323 ReleaseSucc(Available, *I1);
324 SU->NumSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000325 }
326 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
327 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
328 ReleaseSucc(Available, *I2, true);
329
330 CurrCycle++;
331}
332
Evan Chengab495562006-01-25 09:14:32 +0000333/// isReady - True if node's lower cycle bound is less or equal to the current
334/// scheduling cycle. Always true if all nodes have uniform latency 1.
335static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
336 return SU->CycleBound <= CurrCycle;
337}
338
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000339/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
340/// schedulers.
341void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000342 // Available queue.
343 AvailableQueueTy Available;
344
345 // Add root to Available queue.
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000346 Available.push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000347
348 // While Available queue is not empty, grab the node with the highest
349 // priority. If it is not ready put it back. Schedule the node.
350 std::vector<SUnit*> NotReady;
351 while (!Available.empty()) {
352 SUnit *CurrNode = Available.top();
353 Available.pop();
354
Evan Chengab495562006-01-25 09:14:32 +0000355 while (!isReady(CurrNode, CurrCycle)) {
356 NotReady.push_back(CurrNode);
357 CurrNode = Available.top();
358 Available.pop();
359 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000360
361 // Add the nodes that aren't ready back onto the available list.
362 while (!NotReady.empty()) {
363 Available.push(NotReady.back());
364 NotReady.pop_back();
365 }
Evan Chengab495562006-01-25 09:14:32 +0000366
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000367 ScheduleNodeBottomUp(Available, CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000368 }
369
370 // Add entry node last
371 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
372 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Evan Chengab495562006-01-25 09:14:32 +0000373 Sequence.push_back(Entry);
374 }
375
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000376 // Reverse the order if it is bottom up.
377 std::reverse(Sequence.begin(), Sequence.end());
378
379
Evan Chengab495562006-01-25 09:14:32 +0000380#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000381 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000382 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000383 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
384 if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000385 if (!AnyNotSched)
386 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000387 SUnits[i].dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000388 std::cerr << "has not been scheduled!\n";
389 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000390 }
Evan Chengab495562006-01-25 09:14:32 +0000391 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000392 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000393#endif
Evan Chengab495562006-01-25 09:14:32 +0000394}
395
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000396/// ListScheduleTopDown - The main loop of list scheduling for top-down
397/// schedulers.
398void ScheduleDAGList::ListScheduleTopDown() {
399 // Available queue.
400 AvailableQueueTy Available;
Chris Lattner47639db2006-03-06 00:22:00 +0000401
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000402 // Emit the entry node first.
403 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
404 ScheduleNodeTopDown(Available, Entry);
Chris Lattner543832d2006-03-08 04:25:59 +0000405 HazardRec->EmitInstruction(Entry->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000406
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000407 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000408 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000409 // It is available if it has no predecessors.
Chris Lattner42e20262006-03-08 04:54:34 +0000410 if ((SUnits[i].Preds.size() + SUnits[i].ChainPreds.size()) == 0 &&
411 &SUnits[i] != Entry)
412 Available.push(&SUnits[i]);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000413 }
414
415 // While Available queue is not empty, grab the node with the highest
416 // priority. If it is not ready put it back. Schedule the node.
417 std::vector<SUnit*> NotReady;
418 while (!Available.empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000419 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000420
Chris Lattnere50c0922006-03-05 22:45:01 +0000421 bool HasNoopHazards = false;
422 do {
Chris Lattner0c801bd2006-03-07 05:40:43 +0000423 SUnit *CurNode = Available.top();
Chris Lattnere50c0922006-03-05 22:45:01 +0000424 Available.pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000425
426 // Get the node represented by this SUnit.
427 SDNode *N = CurNode->Node;
428 // If this is a pseudo op, like copyfromreg, look to see if there is a
429 // real target node flagged to it. If so, use the target node.
430 for (unsigned i = 0, e = CurNode->FlaggedNodes.size();
431 N->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
432 N = CurNode->FlaggedNodes[i];
433
Chris Lattner543832d2006-03-08 04:25:59 +0000434 HazardRecognizer::HazardType HT = HazardRec->getHazardType(N);
Chris Lattnere50c0922006-03-05 22:45:01 +0000435 if (HT == HazardRecognizer::NoHazard) {
Chris Lattner0c801bd2006-03-07 05:40:43 +0000436 FoundNode = CurNode;
Chris Lattnere50c0922006-03-05 22:45:01 +0000437 break;
438 }
439
440 // Remember if this is a noop hazard.
441 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
442
Chris Lattner0c801bd2006-03-07 05:40:43 +0000443 NotReady.push_back(CurNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000444 } while (!Available.empty());
445
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000446 // Add the nodes that aren't ready back onto the available list.
447 while (!NotReady.empty()) {
448 Available.push(NotReady.back());
449 NotReady.pop_back();
450 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000451
452 // If we found a node to schedule, do it now.
453 if (FoundNode) {
454 ScheduleNodeTopDown(Available, FoundNode);
Chris Lattner543832d2006-03-08 04:25:59 +0000455 HazardRec->EmitInstruction(FoundNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000456 } else if (!HasNoopHazards) {
457 // Otherwise, we have a pipeline stall, but no other problem, just advance
458 // the current cycle and try again.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000459 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000460 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000461 ++NumStalls;
Chris Lattnere50c0922006-03-05 22:45:01 +0000462 } else {
463 // Otherwise, we have no instructions to issue and we have instructions
464 // that will fault if we don't do this right. This is the case for
465 // processors without pipeline interlocks and other cases.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000466 DEBUG(std::cerr << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000467 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000468 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000469 ++NumNoops;
Chris Lattnere50c0922006-03-05 22:45:01 +0000470 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000471 }
472
473#ifndef NDEBUG
474 // Verify that all SUnits were scheduled.
475 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000476 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
477 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000478 if (!AnyNotSched)
479 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000480 SUnits[i].dump(&DAG);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000481 std::cerr << "has not been scheduled!\n";
482 AnyNotSched = true;
483 }
484 }
485 assert(!AnyNotSched);
486#endif
487}
488
489
Evan Chengc5c06582006-03-06 06:08:54 +0000490/// CalcNodePriority - Priority is the Sethi Ullman number.
491/// Smaller number is the higher priority.
Evan Chengab495562006-01-25 09:14:32 +0000492int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
Evan Chengc5c06582006-03-06 06:08:54 +0000493 if (SU->SethiUllman != INT_MIN)
494 return SU->SethiUllman;
Evan Chengab495562006-01-25 09:14:32 +0000495
496 if (SU->Preds.size() == 0) {
Evan Chengc5c06582006-03-06 06:08:54 +0000497 SU->SethiUllman = 1;
Evan Chengab495562006-01-25 09:14:32 +0000498 } else {
499 int Extra = 0;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000500 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
501 E = SU->Preds.end(); I != E; ++I) {
502 SUnit *PredSU = *I;
Evan Chengc5c06582006-03-06 06:08:54 +0000503 int PredSethiUllman = CalcNodePriority(PredSU);
504 if (PredSethiUllman > SU->SethiUllman) {
505 SU->SethiUllman = PredSethiUllman;
Evan Chengab495562006-01-25 09:14:32 +0000506 Extra = 0;
Evan Chengc5c06582006-03-06 06:08:54 +0000507 } else if (PredSethiUllman == SU->SethiUllman)
Evan Chengab495562006-01-25 09:14:32 +0000508 Extra++;
509 }
510
511 if (SU->Node->getOpcode() != ISD::TokenFactor)
Evan Chengc5c06582006-03-06 06:08:54 +0000512 SU->SethiUllman += Extra;
Evan Chengab495562006-01-25 09:14:32 +0000513 else
Evan Chengc5c06582006-03-06 06:08:54 +0000514 SU->SethiUllman = (Extra == 1) ? 0 : Extra-1;
Evan Chengab495562006-01-25 09:14:32 +0000515 }
516
Evan Chengc5c06582006-03-06 06:08:54 +0000517 return SU->SethiUllman;
Evan Chengab495562006-01-25 09:14:32 +0000518}
519
520/// CalculatePriorities - Calculate priorities of all scheduling units.
521void ScheduleDAGList::CalculatePriorities() {
Chris Lattner42e20262006-03-08 04:54:34 +0000522 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Evan Chengab495562006-01-25 09:14:32 +0000523 // FIXME: assumes uniform latency for now.
Chris Lattner42e20262006-03-08 04:54:34 +0000524 SUnits[i].Latency = 1;
525 (void)CalcNodePriority(&SUnits[i]);
526 DEBUG(SUnits[i].dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000527 DEBUG(std::cerr << "\n");
528 }
529}
530
Evan Chengab495562006-01-25 09:14:32 +0000531void ScheduleDAGList::BuildSchedUnits() {
Chris Lattner42e20262006-03-08 04:54:34 +0000532 // Reserve entries in the vector for each of the SUnits we are creating. This
533 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
534 // invalidated.
535 SUnits.reserve(NodeCount);
536
Evan Chengc4c339c2006-01-26 00:30:29 +0000537 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000538 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000539 NodeInfo *NI = &Info[i];
540 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000541 if (isPassiveNode(N))
542 continue;
Evan Chengab495562006-01-25 09:14:32 +0000543
Evan Chengc4c339c2006-01-26 00:30:29 +0000544 SUnit *SU;
545 if (NI->isInGroup()) {
546 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
547 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000548
Evan Chengc4c339c2006-01-26 00:30:29 +0000549 SU = NewSUnit(N);
550 // Find the flagged nodes.
551 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
552 SDNode *Flag = FlagOp.Val;
553 unsigned ResNo = FlagOp.ResNo;
554 while (Flag->getValueType(ResNo) == MVT::Flag) {
555 NodeInfo *FNI = getNI(Flag);
556 assert(FNI->Group == NI->Group);
557 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
558 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000559
Evan Chengc4c339c2006-01-26 00:30:29 +0000560 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
561 Flag = FlagOp.Val;
562 ResNo = FlagOp.ResNo;
563 }
564 } else {
565 SU = NewSUnit(N);
566 }
567 SUnitMap[N] = SU;
568 }
Evan Chengab495562006-01-25 09:14:32 +0000569
Evan Chengc4c339c2006-01-26 00:30:29 +0000570 // Pass 2: add the preds, succs, etc.
Chris Lattner42e20262006-03-08 04:54:34 +0000571 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
572 SUnit *SU = &SUnits[i];
Evan Chengc4c339c2006-01-26 00:30:29 +0000573 SDNode *N = SU->Node;
574 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000575
576 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
577 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000578
579 if (NI->isInGroup()) {
580 // Find all predecessors (of the group).
581 NodeGroupOpIterator NGOI(NI);
582 while (!NGOI.isEnd()) {
583 SDOperand Op = NGOI.next();
584 SDNode *OpN = Op.Val;
585 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
586 NodeInfo *OpNI = getNI(OpN);
587 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
588 assert(VT != MVT::Flag);
589 SUnit *OpSU = SUnitMap[OpN];
590 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000591 if (SU->ChainPreds.insert(OpSU).second)
592 SU->NumChainPredsLeft++;
593 if (OpSU->ChainSuccs.insert(SU).second)
594 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000595 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000596 if (SU->Preds.insert(OpSU).second)
597 SU->NumPredsLeft++;
598 if (OpSU->Succs.insert(SU).second)
599 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000600 }
Evan Chengab495562006-01-25 09:14:32 +0000601 }
602 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000603 } else {
604 // Find node predecessors.
605 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
606 SDOperand Op = N->getOperand(j);
607 SDNode *OpN = Op.Val;
608 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
609 if (!isPassiveNode(OpN)) {
610 assert(VT != MVT::Flag);
611 SUnit *OpSU = SUnitMap[OpN];
612 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000613 if (SU->ChainPreds.insert(OpSU).second)
614 SU->NumChainPredsLeft++;
615 if (OpSU->ChainSuccs.insert(SU).second)
616 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000617 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000618 if (SU->Preds.insert(OpSU).second)
619 SU->NumPredsLeft++;
620 if (OpSU->Succs.insert(SU).second)
621 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000622 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000623 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000624 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000625 }
626 }
Evan Chengab495562006-01-25 09:14:32 +0000627 }
628 }
Evan Chengab495562006-01-25 09:14:32 +0000629}
630
631/// EmitSchedule - Emit the machine code in scheduled order.
632void ScheduleDAGList::EmitSchedule() {
633 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000634 if (SUnit *SU = Sequence[i]) {
635 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
636 SDNode *N = SU->FlaggedNodes[j];
637 EmitNode(getNI(N));
638 }
639 EmitNode(getNI(SU->Node));
640 } else {
641 // Null SUnit* is a noop.
642 EmitNoop();
Evan Chengab495562006-01-25 09:14:32 +0000643 }
Evan Chengab495562006-01-25 09:14:32 +0000644 }
645}
646
647/// dump - dump the schedule.
648void ScheduleDAGList::dump() const {
649 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000650 if (SUnit *SU = Sequence[i])
651 SU->dump(&DAG, false);
652 else
653 std::cerr << "**** NOOP ****\n";
Evan Chengab495562006-01-25 09:14:32 +0000654 }
655}
656
657/// Schedule - Schedule the DAG using list scheduling.
658/// FIXME: Right now it only supports the burr (bottom up register reducing)
659/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000660void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000661 DEBUG(std::cerr << "********** List Scheduling **********\n");
662
663 // Build scheduling units.
664 BuildSchedUnits();
665
Chris Lattner47639db2006-03-06 00:22:00 +0000666 // Calculate node priorities.
Evan Chengab495562006-01-25 09:14:32 +0000667 CalculatePriorities();
668
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000669 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
670 if (isBottomUp)
671 ListScheduleBottomUp();
672 else
673 ListScheduleTopDown();
674
675 DEBUG(std::cerr << "*** Final schedule ***\n");
676 DEBUG(dump());
677 DEBUG(std::cerr << "\n");
678
Evan Chengab495562006-01-25 09:14:32 +0000679 // Emit in scheduled order
680 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000681}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000682
Evan Chengab495562006-01-25 09:14:32 +0000683llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
684 MachineBasicBlock *BB) {
Chris Lattner543832d2006-03-08 04:25:59 +0000685 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true,
686 new HazardRecognizer());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000687}
688
Chris Lattner47639db2006-03-06 00:22:00 +0000689/// createTDListDAGScheduler - This creates a top-down list scheduler with the
690/// specified hazard recognizer.
691ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
692 MachineBasicBlock *BB,
Chris Lattner543832d2006-03-08 04:25:59 +0000693 HazardRecognizer *HR) {
Chris Lattner47639db2006-03-06 00:22:00 +0000694 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, HR);
Evan Cheng31272342006-01-23 08:26:10 +0000695}