blob: 9e6bd75475771fb6b7962e1380febb35ae8b89a5 [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 Lattner12c6d892006-03-08 04:41:06 +000051 bool isTwoAddress : 1; // Is a two-address instruction.
52 bool isDefNUseOperand : 1; // Is a def&use operand.
53 unsigned short Latency; // Node latency.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000054 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
Chris Lattnerfd22d422006-03-08 05:18:27 +000055 unsigned NodeNum; // Entry # of node in the node vector.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000056
Chris Lattnerfd22d422006-03-08 05:18:27 +000057 SUnit(SDNode *node, unsigned nodenum)
Chris Lattneraf5e26c2006-03-08 04:37:58 +000058 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
Evan Cheng4e3904f2006-03-02 21:38:29 +000059 NumChainPredsLeft(0), NumChainSuccsLeft(0),
Evan Cheng5e9a6952006-03-03 06:23:43 +000060 isTwoAddress(false), isDefNUseOperand(false),
Chris Lattnerfd22d422006-03-08 05:18:27 +000061 Latency(0), CycleBound(0), NodeNum(nodenum) {}
Chris Lattneraf5e26c2006-03-08 04:37:58 +000062
63 void dump(const SelectionDAG *G, bool All=true) const;
64 };
65}
Evan Chengab495562006-01-25 09:14:32 +000066
67void SUnit::dump(const SelectionDAG *G, bool All) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000068 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000069 Node->dump(G);
70 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000071 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000072 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000073 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000074 FlaggedNodes[i]->dump(G);
75 std::cerr << "\n";
76 }
77 }
78
79 if (All) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000080 std::cerr << " # preds left : " << NumPredsLeft << "\n";
81 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
82 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
83 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
84 std::cerr << " Latency : " << Latency << "\n";
Evan Chengc4c339c2006-01-26 00:30:29 +000085
Evan Chengab495562006-01-25 09:14:32 +000086 if (Preds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000087 std::cerr << " Predecessors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000088 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000089 E = Preds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000090 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000091 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000092 }
93 }
94 if (ChainPreds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000095 std::cerr << " Chained Preds:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000096 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000097 E = ChainPreds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000098 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000099 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000100 }
101 }
102 if (Succs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000103 std::cerr << " Successors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000104 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000105 E = Succs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000106 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000107 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000108 }
109 }
110 if (ChainSuccs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000111 std::cerr << " Chained succs:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000112 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000113 E = ChainSuccs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000114 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000115 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000116 }
117 }
118 }
119}
120
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000121namespace {
Chris Lattnerfd22d422006-03-08 05:18:27 +0000122 class SchedulingPriorityQueue;
123
124 /// Sorting functions for the Available queue.
125 struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
126 SchedulingPriorityQueue *SPQ;
127 ls_rr_sort(SchedulingPriorityQueue *spq) : SPQ(spq) {}
128 ls_rr_sort(const ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
129
130 bool operator()(const SUnit* left, const SUnit* right) const;
131 };
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000132} // end anonymous namespace
Evan Cheng31272342006-01-23 08:26:10 +0000133
Chris Lattnerfd22d422006-03-08 05:18:27 +0000134namespace {
135 class SchedulingPriorityQueue {
136 // SUnits - The SUnits for the current graph.
137 std::vector<SUnit> &SUnits;
138
139 // SethiUllmanNumbers - The SethiUllman number for each node.
140 std::vector<int> SethiUllmanNumbers;
141
142 std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Queue;
143 public:
144 SchedulingPriorityQueue(std::vector<SUnit> &sunits)
145 : SUnits(sunits), Queue(ls_rr_sort(this)) {
146 // Calculate node priorities.
147 CalculatePriorities();
148 }
149
150 unsigned getSethiUllmanNumber(unsigned NodeNum) const {
151 assert(NodeNum < SethiUllmanNumbers.size());
152 return SethiUllmanNumbers[NodeNum];
153 }
154
155 bool empty() const { return Queue.empty(); }
156
157 void push(SUnit *U) {
158 Queue.push(U);
159 }
160 SUnit *pop() {
161 SUnit *V = Queue.top();
162 Queue.pop();
163 return V;
164 }
165 private:
166 void CalculatePriorities();
167 int CalcNodePriority(SUnit *SU);
168 };
169}
170
171bool ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
172 unsigned LeftNum = left->NodeNum;
173 unsigned RightNum = right->NodeNum;
174
175 int LBonus = (int)left ->isDefNUseOperand;
176 int RBonus = (int)right->isDefNUseOperand;
177
178 // Special tie breaker: if two nodes share a operand, the one that
179 // use it as a def&use operand is preferred.
180 if (left->isTwoAddress && !right->isTwoAddress) {
181 SDNode *DUNode = left->Node->getOperand(0).Val;
182 if (DUNode->isOperand(right->Node))
183 LBonus++;
184 }
185 if (!left->isTwoAddress && right->isTwoAddress) {
186 SDNode *DUNode = right->Node->getOperand(0).Val;
187 if (DUNode->isOperand(left->Node))
188 RBonus++;
189 }
190
191 // Priority1 is just the number of live range genned.
192 int LPriority1 = left ->NumPredsLeft - LBonus;
193 int RPriority1 = right->NumPredsLeft - RBonus;
194 int LPriority2 = SPQ->getSethiUllmanNumber(LeftNum) + LBonus;
195 int RPriority2 = SPQ->getSethiUllmanNumber(RightNum) + RBonus;
196
197 if (LPriority1 > RPriority1)
198 return true;
199 else if (LPriority1 == RPriority1)
200 if (LPriority2 < RPriority2)
201 return true;
202 else if (LPriority2 == RPriority2)
203 if (left->CycleBound > right->CycleBound)
204 return true;
205
206 return false;
207}
208
209
210/// CalcNodePriority - Priority is the Sethi Ullman number.
211/// Smaller number is the higher priority.
212int SchedulingPriorityQueue::CalcNodePriority(SUnit *SU) {
213 int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
214 if (SethiUllmanNumber != INT_MIN)
215 return SethiUllmanNumber;
216
217 if (SU->Preds.size() == 0) {
218 SethiUllmanNumber = 1;
219 } else {
220 int Extra = 0;
221 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
222 E = SU->Preds.end(); I != E; ++I) {
223 SUnit *PredSU = *I;
224 int PredSethiUllman = CalcNodePriority(PredSU);
225 if (PredSethiUllman > SethiUllmanNumber) {
226 SethiUllmanNumber = PredSethiUllman;
227 Extra = 0;
228 } else if (PredSethiUllman == SethiUllmanNumber)
229 Extra++;
230 }
231
232 if (SU->Node->getOpcode() != ISD::TokenFactor)
233 SethiUllmanNumber += Extra;
234 else
235 SethiUllmanNumber = (Extra == 1) ? 0 : Extra-1;
236 }
237
238 return SethiUllmanNumber;
239}
240
241/// CalculatePriorities - Calculate priorities of all scheduling units.
242void SchedulingPriorityQueue::CalculatePriorities() {
243 SethiUllmanNumbers.assign(SUnits.size(), INT_MIN);
244
245 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
246 // FIXME: assumes uniform latency for now.
247 SUnits[i].Latency = 1;
248 (void)CalcNodePriority(&SUnits[i]);
249 }
250}
251
252
253
Chris Lattnere50c0922006-03-05 22:45:01 +0000254
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000255namespace {
Evan Cheng31272342006-01-23 08:26:10 +0000256/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000257class ScheduleDAGList : public ScheduleDAG {
258private:
Evan Chengab495562006-01-25 09:14:32 +0000259 // SDNode to SUnit mapping (many to one).
260 std::map<SDNode*, SUnit*> SUnitMap;
Chris Lattner00b52ea2006-03-05 23:59:20 +0000261 // The schedule. Null SUnit*'s represent noop instructions.
Evan Chengab495562006-01-25 09:14:32 +0000262 std::vector<SUnit*> Sequence;
263 // Current scheduling cycle.
264 unsigned CurrCycle;
Chris Lattner42e20262006-03-08 04:54:34 +0000265
266 // The scheduling units.
267 std::vector<SUnit> SUnits;
Evan Cheng31272342006-01-23 08:26:10 +0000268
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000269 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
270 /// it is top-down.
271 bool isBottomUp;
272
Chris Lattnere50c0922006-03-05 22:45:01 +0000273 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +0000274 HazardRecognizer *HazardRec;
Chris Lattnere50c0922006-03-05 22:45:01 +0000275
Evan Cheng31272342006-01-23 08:26:10 +0000276public:
277 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000278 const TargetMachine &tm, bool isbottomup,
Chris Lattner543832d2006-03-08 04:25:59 +0000279 HazardRecognizer *HR)
Evan Chengc4c339c2006-01-26 00:30:29 +0000280 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner42e20262006-03-08 04:54:34 +0000281 CurrCycle(0), isBottomUp(isbottomup), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000282 }
Evan Chengab495562006-01-25 09:14:32 +0000283
284 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +0000285 delete HazardRec;
Evan Chengab495562006-01-25 09:14:32 +0000286 }
Evan Cheng31272342006-01-23 08:26:10 +0000287
288 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000289
Evan Chengab495562006-01-25 09:14:32 +0000290 void dump() const;
291
292private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000293 SUnit *NewSUnit(SDNode *N);
Chris Lattnerfd22d422006-03-08 05:18:27 +0000294 void ReleasePred(SchedulingPriorityQueue &Avail,
295 SUnit *PredSU, bool isChain = false);
296 void ReleaseSucc(SchedulingPriorityQueue &Avail,
297 SUnit *SuccSU, bool isChain = false);
298 void ScheduleNodeBottomUp(SchedulingPriorityQueue &Avail, SUnit *SU);
299 void ScheduleNodeTopDown(SchedulingPriorityQueue &Avail, SUnit *SU);
300 void ListScheduleTopDown(SchedulingPriorityQueue &Available);
301 void ListScheduleBottomUp(SchedulingPriorityQueue &Available);
Evan Chengab495562006-01-25 09:14:32 +0000302 void BuildSchedUnits();
303 void EmitSchedule();
304};
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000305} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +0000306
Chris Lattner47639db2006-03-06 00:22:00 +0000307HazardRecognizer::~HazardRecognizer() {}
308
Evan Chengc4c339c2006-01-26 00:30:29 +0000309
310/// NewSUnit - Creates a new SUnit and return a ptr to it.
311SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
Chris Lattnerfd22d422006-03-08 05:18:27 +0000312 SUnits.push_back(SUnit(N, SUnits.size()));
Chris Lattner42e20262006-03-08 04:54:34 +0000313 return &SUnits.back();
Evan Chengc4c339c2006-01-26 00:30:29 +0000314}
315
316/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
317/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattnerfd22d422006-03-08 05:18:27 +0000318void ScheduleDAGList::ReleasePred(SchedulingPriorityQueue &Available,
Chris Lattner7a36d972006-03-05 20:21:55 +0000319 SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000320 // FIXME: the distance between two nodes is not always == the predecessor's
321 // latency. For example, the reader can very well read the register written
322 // by the predecessor later than the issue cycle. It also depends on the
323 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000324 PredSU->CycleBound = std::max(PredSU->CycleBound,CurrCycle + PredSU->Latency);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000325
Evan Chengc5c06582006-03-06 06:08:54 +0000326 if (!isChain)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000327 PredSU->NumSuccsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000328 else
Evan Cheng4e3904f2006-03-02 21:38:29 +0000329 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000330
Evan Chengab495562006-01-25 09:14:32 +0000331#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000332 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000333 std::cerr << "*** List scheduling failed! ***\n";
334 PredSU->dump(&DAG);
335 std::cerr << " has been released too many times!\n";
336 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000337 }
Evan Chengab495562006-01-25 09:14:32 +0000338#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000339
340 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
341 // EntryToken has to go last! Special case it here.
342 if (PredSU->Node->getOpcode() != ISD::EntryToken)
343 Available.push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000344 }
Evan Chengab495562006-01-25 09:14:32 +0000345}
346
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000347/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
348/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattnerfd22d422006-03-08 05:18:27 +0000349void ScheduleDAGList::ReleaseSucc(SchedulingPriorityQueue &Available,
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000350 SUnit *SuccSU, bool isChain) {
351 // FIXME: the distance between two nodes is not always == the predecessor's
352 // latency. For example, the reader can very well read the register written
353 // by the predecessor later than the issue cycle. It also depends on the
354 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000355 SuccSU->CycleBound = std::max(SuccSU->CycleBound,CurrCycle + SuccSU->Latency);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000356
Evan Chengc5c06582006-03-06 06:08:54 +0000357 if (!isChain)
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000358 SuccSU->NumPredsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000359 else
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000360 SuccSU->NumChainPredsLeft--;
361
362#ifndef NDEBUG
363 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
364 std::cerr << "*** List scheduling failed! ***\n";
365 SuccSU->dump(&DAG);
366 std::cerr << " has been released too many times!\n";
367 abort();
368 }
369#endif
370
371 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
372 Available.push(SuccSU);
373}
374
375/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
376/// count of its predecessors. If a predecessor pending count is zero, add it to
377/// the Available queue.
Chris Lattnerfd22d422006-03-08 05:18:27 +0000378void ScheduleDAGList::ScheduleNodeBottomUp(SchedulingPriorityQueue &Available,
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000379 SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000380 DEBUG(std::cerr << "*** Scheduling: ");
381 DEBUG(SU->dump(&DAG, false));
382
Evan Chengab495562006-01-25 09:14:32 +0000383 Sequence.push_back(SU);
Evan Chengab495562006-01-25 09:14:32 +0000384
385 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000386 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
387 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000388 ReleasePred(Available, *I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000389 SU->NumPredsLeft--;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000390 }
391 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
392 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner7a36d972006-03-05 20:21:55 +0000393 ReleasePred(Available, *I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000394
395 CurrCycle++;
396}
397
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000398/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
399/// count of its successors. If a successor pending count is zero, add it to
400/// the Available queue.
Chris Lattnerfd22d422006-03-08 05:18:27 +0000401void ScheduleDAGList::ScheduleNodeTopDown(SchedulingPriorityQueue &Available,
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000402 SUnit *SU) {
403 DEBUG(std::cerr << "*** Scheduling: ");
404 DEBUG(SU->dump(&DAG, false));
405
406 Sequence.push_back(SU);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000407
408 // Bottom up: release successors.
409 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
410 E1 = SU->Succs.end(); I1 != E1; ++I1) {
411 ReleaseSucc(Available, *I1);
412 SU->NumSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000413 }
414 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
415 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
416 ReleaseSucc(Available, *I2, true);
417
418 CurrCycle++;
419}
420
Evan Chengab495562006-01-25 09:14:32 +0000421/// isReady - True if node's lower cycle bound is less or equal to the current
422/// scheduling cycle. Always true if all nodes have uniform latency 1.
423static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
424 return SU->CycleBound <= CurrCycle;
425}
426
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000427/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
428/// schedulers.
Chris Lattnerfd22d422006-03-08 05:18:27 +0000429void ScheduleDAGList::ListScheduleBottomUp(SchedulingPriorityQueue &Available) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000430 // Add root to Available queue.
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000431 Available.push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000432
433 // While Available queue is not empty, grab the node with the highest
434 // priority. If it is not ready put it back. Schedule the node.
435 std::vector<SUnit*> NotReady;
436 while (!Available.empty()) {
Chris Lattnerfd22d422006-03-08 05:18:27 +0000437 SUnit *CurrNode = Available.pop();
Evan Chengab495562006-01-25 09:14:32 +0000438
Evan Chengab495562006-01-25 09:14:32 +0000439 while (!isReady(CurrNode, CurrCycle)) {
440 NotReady.push_back(CurrNode);
Chris Lattnerfd22d422006-03-08 05:18:27 +0000441 CurrNode = Available.pop();
Evan Chengab495562006-01-25 09:14:32 +0000442 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000443
444 // Add the nodes that aren't ready back onto the available list.
445 while (!NotReady.empty()) {
446 Available.push(NotReady.back());
447 NotReady.pop_back();
448 }
Evan Chengab495562006-01-25 09:14:32 +0000449
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000450 ScheduleNodeBottomUp(Available, CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000451 }
452
453 // Add entry node last
454 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
455 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Evan Chengab495562006-01-25 09:14:32 +0000456 Sequence.push_back(Entry);
457 }
458
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000459 // Reverse the order if it is bottom up.
460 std::reverse(Sequence.begin(), Sequence.end());
461
462
Evan Chengab495562006-01-25 09:14:32 +0000463#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000464 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000465 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000466 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
467 if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000468 if (!AnyNotSched)
469 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000470 SUnits[i].dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000471 std::cerr << "has not been scheduled!\n";
472 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000473 }
Evan Chengab495562006-01-25 09:14:32 +0000474 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000475 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000476#endif
Evan Chengab495562006-01-25 09:14:32 +0000477}
478
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000479/// ListScheduleTopDown - The main loop of list scheduling for top-down
480/// schedulers.
Chris Lattnerfd22d422006-03-08 05:18:27 +0000481void ScheduleDAGList::ListScheduleTopDown(SchedulingPriorityQueue &Available) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000482 // Emit the entry node first.
483 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
484 ScheduleNodeTopDown(Available, Entry);
Chris Lattner543832d2006-03-08 04:25:59 +0000485 HazardRec->EmitInstruction(Entry->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000486
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000487 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000488 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000489 // It is available if it has no predecessors.
Chris Lattner42e20262006-03-08 04:54:34 +0000490 if ((SUnits[i].Preds.size() + SUnits[i].ChainPreds.size()) == 0 &&
491 &SUnits[i] != Entry)
492 Available.push(&SUnits[i]);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000493 }
494
495 // While Available queue is not empty, grab the node with the highest
496 // priority. If it is not ready put it back. Schedule the node.
497 std::vector<SUnit*> NotReady;
498 while (!Available.empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000499 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000500
Chris Lattnere50c0922006-03-05 22:45:01 +0000501 bool HasNoopHazards = false;
502 do {
Chris Lattnerfd22d422006-03-08 05:18:27 +0000503 SUnit *CurNode = Available.pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000504
505 // Get the node represented by this SUnit.
506 SDNode *N = CurNode->Node;
507 // If this is a pseudo op, like copyfromreg, look to see if there is a
508 // real target node flagged to it. If so, use the target node.
509 for (unsigned i = 0, e = CurNode->FlaggedNodes.size();
510 N->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
511 N = CurNode->FlaggedNodes[i];
512
Chris Lattner543832d2006-03-08 04:25:59 +0000513 HazardRecognizer::HazardType HT = HazardRec->getHazardType(N);
Chris Lattnere50c0922006-03-05 22:45:01 +0000514 if (HT == HazardRecognizer::NoHazard) {
Chris Lattner0c801bd2006-03-07 05:40:43 +0000515 FoundNode = CurNode;
Chris Lattnere50c0922006-03-05 22:45:01 +0000516 break;
517 }
518
519 // Remember if this is a noop hazard.
520 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
521
Chris Lattner0c801bd2006-03-07 05:40:43 +0000522 NotReady.push_back(CurNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000523 } while (!Available.empty());
524
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000525 // Add the nodes that aren't ready back onto the available list.
526 while (!NotReady.empty()) {
527 Available.push(NotReady.back());
528 NotReady.pop_back();
529 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000530
531 // If we found a node to schedule, do it now.
532 if (FoundNode) {
533 ScheduleNodeTopDown(Available, FoundNode);
Chris Lattner543832d2006-03-08 04:25:59 +0000534 HazardRec->EmitInstruction(FoundNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000535 } else if (!HasNoopHazards) {
536 // Otherwise, we have a pipeline stall, but no other problem, just advance
537 // the current cycle and try again.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000538 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000539 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000540 ++NumStalls;
Chris Lattnere50c0922006-03-05 22:45:01 +0000541 } else {
542 // Otherwise, we have no instructions to issue and we have instructions
543 // that will fault if we don't do this right. This is the case for
544 // processors without pipeline interlocks and other cases.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000545 DEBUG(std::cerr << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000546 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000547 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000548 ++NumNoops;
Chris Lattnere50c0922006-03-05 22:45:01 +0000549 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000550 }
551
552#ifndef NDEBUG
553 // Verify that all SUnits were scheduled.
554 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000555 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
556 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000557 if (!AnyNotSched)
558 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000559 SUnits[i].dump(&DAG);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000560 std::cerr << "has not been scheduled!\n";
561 AnyNotSched = true;
562 }
563 }
564 assert(!AnyNotSched);
565#endif
566}
567
568
Evan Chengab495562006-01-25 09:14:32 +0000569void ScheduleDAGList::BuildSchedUnits() {
Chris Lattner42e20262006-03-08 04:54:34 +0000570 // Reserve entries in the vector for each of the SUnits we are creating. This
571 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
572 // invalidated.
573 SUnits.reserve(NodeCount);
574
Evan Chengc4c339c2006-01-26 00:30:29 +0000575 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000576 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000577 NodeInfo *NI = &Info[i];
578 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000579 if (isPassiveNode(N))
580 continue;
Evan Chengab495562006-01-25 09:14:32 +0000581
Evan Chengc4c339c2006-01-26 00:30:29 +0000582 SUnit *SU;
583 if (NI->isInGroup()) {
584 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
585 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000586
Evan Chengc4c339c2006-01-26 00:30:29 +0000587 SU = NewSUnit(N);
588 // Find the flagged nodes.
589 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
590 SDNode *Flag = FlagOp.Val;
591 unsigned ResNo = FlagOp.ResNo;
592 while (Flag->getValueType(ResNo) == MVT::Flag) {
593 NodeInfo *FNI = getNI(Flag);
594 assert(FNI->Group == NI->Group);
595 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
596 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000597
Evan Chengc4c339c2006-01-26 00:30:29 +0000598 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
599 Flag = FlagOp.Val;
600 ResNo = FlagOp.ResNo;
601 }
602 } else {
603 SU = NewSUnit(N);
604 }
605 SUnitMap[N] = SU;
606 }
Evan Chengab495562006-01-25 09:14:32 +0000607
Evan Chengc4c339c2006-01-26 00:30:29 +0000608 // Pass 2: add the preds, succs, etc.
Chris Lattner42e20262006-03-08 04:54:34 +0000609 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
610 SUnit *SU = &SUnits[i];
Evan Chengc4c339c2006-01-26 00:30:29 +0000611 SDNode *N = SU->Node;
612 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000613
614 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
615 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000616
617 if (NI->isInGroup()) {
618 // Find all predecessors (of the group).
619 NodeGroupOpIterator NGOI(NI);
620 while (!NGOI.isEnd()) {
621 SDOperand Op = NGOI.next();
622 SDNode *OpN = Op.Val;
623 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
624 NodeInfo *OpNI = getNI(OpN);
625 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
626 assert(VT != MVT::Flag);
627 SUnit *OpSU = SUnitMap[OpN];
628 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000629 if (SU->ChainPreds.insert(OpSU).second)
630 SU->NumChainPredsLeft++;
631 if (OpSU->ChainSuccs.insert(SU).second)
632 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000633 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000634 if (SU->Preds.insert(OpSU).second)
635 SU->NumPredsLeft++;
636 if (OpSU->Succs.insert(SU).second)
637 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000638 }
Evan Chengab495562006-01-25 09:14:32 +0000639 }
640 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000641 } else {
642 // Find node predecessors.
643 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
644 SDOperand Op = N->getOperand(j);
645 SDNode *OpN = Op.Val;
646 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
647 if (!isPassiveNode(OpN)) {
648 assert(VT != MVT::Flag);
649 SUnit *OpSU = SUnitMap[OpN];
650 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000651 if (SU->ChainPreds.insert(OpSU).second)
652 SU->NumChainPredsLeft++;
653 if (OpSU->ChainSuccs.insert(SU).second)
654 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000655 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000656 if (SU->Preds.insert(OpSU).second)
657 SU->NumPredsLeft++;
658 if (OpSU->Succs.insert(SU).second)
659 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000660 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000661 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000662 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000663 }
664 }
Evan Chengab495562006-01-25 09:14:32 +0000665 }
666 }
Evan Chengab495562006-01-25 09:14:32 +0000667}
668
669/// EmitSchedule - Emit the machine code in scheduled order.
670void ScheduleDAGList::EmitSchedule() {
671 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000672 if (SUnit *SU = Sequence[i]) {
673 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
674 SDNode *N = SU->FlaggedNodes[j];
675 EmitNode(getNI(N));
676 }
677 EmitNode(getNI(SU->Node));
678 } else {
679 // Null SUnit* is a noop.
680 EmitNoop();
Evan Chengab495562006-01-25 09:14:32 +0000681 }
Evan Chengab495562006-01-25 09:14:32 +0000682 }
683}
684
685/// dump - dump the schedule.
686void ScheduleDAGList::dump() const {
687 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000688 if (SUnit *SU = Sequence[i])
689 SU->dump(&DAG, false);
690 else
691 std::cerr << "**** NOOP ****\n";
Evan Chengab495562006-01-25 09:14:32 +0000692 }
693}
694
695/// Schedule - Schedule the DAG using list scheduling.
696/// FIXME: Right now it only supports the burr (bottom up register reducing)
697/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000698void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000699 DEBUG(std::cerr << "********** List Scheduling **********\n");
700
701 // Build scheduling units.
702 BuildSchedUnits();
Chris Lattnerfd22d422006-03-08 05:18:27 +0000703
704 SchedulingPriorityQueue PQ(SUnits);
705
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000706 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
707 if (isBottomUp)
Chris Lattnerfd22d422006-03-08 05:18:27 +0000708 ListScheduleBottomUp(PQ);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000709 else
Chris Lattnerfd22d422006-03-08 05:18:27 +0000710 ListScheduleTopDown(PQ);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000711
712 DEBUG(std::cerr << "*** Final schedule ***\n");
713 DEBUG(dump());
714 DEBUG(std::cerr << "\n");
715
Evan Chengab495562006-01-25 09:14:32 +0000716 // Emit in scheduled order
717 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000718}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000719
Evan Chengab495562006-01-25 09:14:32 +0000720llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
721 MachineBasicBlock *BB) {
Chris Lattner543832d2006-03-08 04:25:59 +0000722 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true,
723 new HazardRecognizer());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000724}
725
Chris Lattner47639db2006-03-06 00:22:00 +0000726/// createTDListDAGScheduler - This creates a top-down list scheduler with the
727/// specified hazard recognizer.
728ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
729 MachineBasicBlock *BB,
Chris Lattner543832d2006-03-08 04:25:59 +0000730 HazardRecognizer *HR) {
Chris Lattner47639db2006-03-06 00:22:00 +0000731 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, HR);
Evan Cheng31272342006-01-23 08:26:10 +0000732}