blob: fd05980cdc028e6d6c2fc36a0ed07905a828e57b [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>
Chris Lattnerd4130372006-03-09 07:15:18 +000032#include "llvm/Support/CommandLine.h"
Evan Cheng31272342006-01-23 08:26:10 +000033using namespace llvm;
34
Evan Chengab495562006-01-25 09:14:32 +000035namespace {
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000036 Statistic<> NumNoops ("scheduler", "Number of noops inserted");
37 Statistic<> NumStalls("scheduler", "Number of pipeline stalls");
Evan Cheng31272342006-01-23 08:26:10 +000038
Chris Lattner12c6d892006-03-08 04:41:06 +000039 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
40 /// a group of nodes flagged together.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000041 struct SUnit {
42 SDNode *Node; // Representative node.
43 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
44 std::set<SUnit*> Preds; // All real predecessors.
45 std::set<SUnit*> ChainPreds; // All chain predecessors.
46 std::set<SUnit*> Succs; // All real successors.
47 std::set<SUnit*> ChainSuccs; // All chain successors.
Chris Lattner12c6d892006-03-08 04:41:06 +000048 short NumPredsLeft; // # of preds not scheduled.
49 short NumSuccsLeft; // # of succs not scheduled.
50 short NumChainPredsLeft; // # of chain preds not scheduled.
51 short NumChainSuccsLeft; // # of chain succs not scheduled.
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 Lattnerfd22d422006-03-08 05:18:27 +000056 unsigned NodeNum; // Entry # of node in the node vector.
Chris Lattneraf5e26c2006-03-08 04:37:58 +000057
Chris Lattnerfd22d422006-03-08 05:18:27 +000058 SUnit(SDNode *node, unsigned nodenum)
Chris Lattneraf5e26c2006-03-08 04:37:58 +000059 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
Evan Cheng4e3904f2006-03-02 21:38:29 +000060 NumChainPredsLeft(0), NumChainSuccsLeft(0),
Evan Cheng5e9a6952006-03-03 06:23:43 +000061 isTwoAddress(false), isDefNUseOperand(false),
Chris Lattnerfd22d422006-03-08 05:18:27 +000062 Latency(0), CycleBound(0), NodeNum(nodenum) {}
Chris Lattneraf5e26c2006-03-08 04:37:58 +000063
Chris Lattnerd4130372006-03-09 07:15:18 +000064 void dump(const SelectionDAG *G) const;
65 void dumpAll(const SelectionDAG *G) const;
Chris Lattneraf5e26c2006-03-08 04:37:58 +000066 };
67}
Evan Chengab495562006-01-25 09:14:32 +000068
Chris Lattnerd4130372006-03-09 07:15:18 +000069void SUnit::dump(const SelectionDAG *G) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000070 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000071 Node->dump(G);
72 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000073 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000074 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000075 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000076 FlaggedNodes[i]->dump(G);
77 std::cerr << "\n";
78 }
79 }
Chris Lattnerd4130372006-03-09 07:15:18 +000080}
Evan Chengab495562006-01-25 09:14:32 +000081
Chris Lattnerd4130372006-03-09 07:15:18 +000082void SUnit::dumpAll(const SelectionDAG *G) const {
83 dump(G);
Evan Chengc4c339c2006-01-26 00:30:29 +000084
Chris Lattnerd4130372006-03-09 07:15:18 +000085 std::cerr << " # preds left : " << NumPredsLeft << "\n";
86 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
87 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
88 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
89 std::cerr << " Latency : " << Latency << "\n";
90
91 if (Preds.size() != 0) {
92 std::cerr << " Predecessors:\n";
93 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
94 E = Preds.end(); I != E; ++I) {
95 std::cerr << " ";
96 (*I)->dump(G);
Evan Chengab495562006-01-25 09:14:32 +000097 }
98 }
Chris Lattnerd4130372006-03-09 07:15:18 +000099 if (ChainPreds.size() != 0) {
100 std::cerr << " Chained Preds:\n";
101 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
102 E = ChainPreds.end(); I != E; ++I) {
103 std::cerr << " ";
104 (*I)->dump(G);
105 }
106 }
107 if (Succs.size() != 0) {
108 std::cerr << " Successors:\n";
109 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
110 E = Succs.end(); I != E; ++I) {
111 std::cerr << " ";
112 (*I)->dump(G);
113 }
114 }
115 if (ChainSuccs.size() != 0) {
116 std::cerr << " Chained succs:\n";
117 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
118 E = ChainSuccs.end(); I != E; ++I) {
119 std::cerr << " ";
120 (*I)->dump(G);
121 }
122 }
123 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +0000124}
125
Chris Lattner9df64752006-03-09 06:35:14 +0000126//===----------------------------------------------------------------------===//
Chris Lattner9e95acc2006-03-09 06:37:29 +0000127/// SchedulingPriorityQueue - This interface is used to plug different
128/// priorities computation algorithms into the list scheduler. It implements the
129/// interface of a standard priority queue, where nodes are inserted in
130/// arbitrary order and returned in priority order. The computation of the
131/// priority and the representation of the queue are totally up to the
132/// implementation to decide.
133///
134namespace {
Chris Lattner9df64752006-03-09 06:35:14 +0000135class SchedulingPriorityQueue {
136public:
137 virtual ~SchedulingPriorityQueue() {}
Chris Lattnerfd22d422006-03-08 05:18:27 +0000138
Chris Lattner9df64752006-03-09 06:35:14 +0000139 virtual void initNodes(const std::vector<SUnit> &SUnits) = 0;
140 virtual void releaseState() = 0;
Chris Lattnerfd22d422006-03-08 05:18:27 +0000141
Chris Lattner9df64752006-03-09 06:35:14 +0000142 virtual bool empty() const = 0;
143 virtual void push(SUnit *U) = 0;
Chris Lattner25e25562006-03-10 04:32:49 +0000144
145 virtual void push_all(const std::vector<SUnit *> &Nodes) = 0;
Chris Lattner9df64752006-03-09 06:35:14 +0000146 virtual SUnit *pop() = 0;
Chris Lattner25e25562006-03-10 04:32:49 +0000147
148 /// ScheduledNode - As each node is scheduled, this method is invoked. This
149 /// allows the priority function to adjust the priority of node that have
150 /// already been emitted.
151 virtual void ScheduledNode(SUnit *Node) {}
Chris Lattner9df64752006-03-09 06:35:14 +0000152};
Chris Lattner9e95acc2006-03-09 06:37:29 +0000153}
Chris Lattnerfd22d422006-03-08 05:18:27 +0000154
155
Chris Lattnere50c0922006-03-05 22:45:01 +0000156
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000157namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +0000158//===----------------------------------------------------------------------===//
159/// ScheduleDAGList - The actual list scheduler implementation. This supports
160/// both top-down and bottom-up scheduling.
161///
Evan Cheng31272342006-01-23 08:26:10 +0000162class ScheduleDAGList : public ScheduleDAG {
163private:
Evan Chengab495562006-01-25 09:14:32 +0000164 // SDNode to SUnit mapping (many to one).
165 std::map<SDNode*, SUnit*> SUnitMap;
Chris Lattner00b52ea2006-03-05 23:59:20 +0000166 // The schedule. Null SUnit*'s represent noop instructions.
Evan Chengab495562006-01-25 09:14:32 +0000167 std::vector<SUnit*> Sequence;
168 // Current scheduling cycle.
169 unsigned CurrCycle;
Chris Lattner42e20262006-03-08 04:54:34 +0000170
171 // The scheduling units.
172 std::vector<SUnit> SUnits;
Evan Cheng31272342006-01-23 08:26:10 +0000173
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000174 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
175 /// it is top-down.
176 bool isBottomUp;
177
Chris Lattner9df64752006-03-09 06:35:14 +0000178 /// PriorityQueue - The priority queue to use.
179 SchedulingPriorityQueue *PriorityQueue;
180
Chris Lattnere50c0922006-03-05 22:45:01 +0000181 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +0000182 HazardRecognizer *HazardRec;
Chris Lattnere50c0922006-03-05 22:45:01 +0000183
Evan Cheng31272342006-01-23 08:26:10 +0000184public:
185 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000186 const TargetMachine &tm, bool isbottomup,
Chris Lattner9df64752006-03-09 06:35:14 +0000187 SchedulingPriorityQueue *priorityqueue,
Chris Lattner543832d2006-03-08 04:25:59 +0000188 HazardRecognizer *HR)
Evan Chengc4c339c2006-01-26 00:30:29 +0000189 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner9df64752006-03-09 06:35:14 +0000190 CurrCycle(0), isBottomUp(isbottomup),
191 PriorityQueue(priorityqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000192 }
Evan Chengab495562006-01-25 09:14:32 +0000193
194 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +0000195 delete HazardRec;
Chris Lattner9df64752006-03-09 06:35:14 +0000196 delete PriorityQueue;
Evan Chengab495562006-01-25 09:14:32 +0000197 }
Evan Cheng31272342006-01-23 08:26:10 +0000198
199 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000200
Chris Lattnerd4130372006-03-09 07:15:18 +0000201 void dumpSchedule() const;
Evan Chengab495562006-01-25 09:14:32 +0000202
203private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000204 SUnit *NewSUnit(SDNode *N);
Chris Lattner399bee22006-03-09 06:48:37 +0000205 void ReleasePred(SUnit *PredSU, bool isChain = false);
206 void ReleaseSucc(SUnit *SuccSU, bool isChain = false);
207 void ScheduleNodeBottomUp(SUnit *SU);
208 void ScheduleNodeTopDown(SUnit *SU);
209 void ListScheduleTopDown();
210 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000211 void BuildSchedUnits();
212 void EmitSchedule();
213};
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000214} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +0000215
Chris Lattner47639db2006-03-06 00:22:00 +0000216HazardRecognizer::~HazardRecognizer() {}
217
Evan Chengc4c339c2006-01-26 00:30:29 +0000218
219/// NewSUnit - Creates a new SUnit and return a ptr to it.
220SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
Chris Lattnerfd22d422006-03-08 05:18:27 +0000221 SUnits.push_back(SUnit(N, SUnits.size()));
Chris Lattner42e20262006-03-08 04:54:34 +0000222 return &SUnits.back();
Evan Chengc4c339c2006-01-26 00:30:29 +0000223}
224
225/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
226/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner399bee22006-03-09 06:48:37 +0000227void ScheduleDAGList::ReleasePred(SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000228 // FIXME: the distance between two nodes is not always == the predecessor's
229 // latency. For example, the reader can very well read the register written
230 // by the predecessor later than the issue cycle. It also depends on the
231 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000232 PredSU->CycleBound = std::max(PredSU->CycleBound,CurrCycle + PredSU->Latency);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000233
Evan Chengc5c06582006-03-06 06:08:54 +0000234 if (!isChain)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000235 PredSU->NumSuccsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000236 else
Evan Cheng4e3904f2006-03-02 21:38:29 +0000237 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000238
Evan Chengab495562006-01-25 09:14:32 +0000239#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000240 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000241 std::cerr << "*** List scheduling failed! ***\n";
242 PredSU->dump(&DAG);
243 std::cerr << " has been released too many times!\n";
244 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000245 }
Evan Chengab495562006-01-25 09:14:32 +0000246#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000247
248 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
249 // EntryToken has to go last! Special case it here.
250 if (PredSU->Node->getOpcode() != ISD::EntryToken)
Chris Lattner399bee22006-03-09 06:48:37 +0000251 PriorityQueue->push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000252 }
Evan Chengab495562006-01-25 09:14:32 +0000253}
254
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000255/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
256/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner399bee22006-03-09 06:48:37 +0000257void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000258 // FIXME: the distance between two nodes is not always == the predecessor's
259 // latency. For example, the reader can very well read the register written
260 // by the predecessor later than the issue cycle. It also depends on the
261 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000262 SuccSU->CycleBound = std::max(SuccSU->CycleBound,CurrCycle + SuccSU->Latency);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000263
Evan Chengc5c06582006-03-06 06:08:54 +0000264 if (!isChain)
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000265 SuccSU->NumPredsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000266 else
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000267 SuccSU->NumChainPredsLeft--;
268
269#ifndef NDEBUG
270 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
271 std::cerr << "*** List scheduling failed! ***\n";
272 SuccSU->dump(&DAG);
273 std::cerr << " has been released too many times!\n";
274 abort();
275 }
276#endif
277
278 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
Chris Lattner399bee22006-03-09 06:48:37 +0000279 PriorityQueue->push(SuccSU);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000280}
281
282/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
283/// count of its predecessors. If a predecessor pending count is zero, add it to
284/// the Available queue.
Chris Lattner399bee22006-03-09 06:48:37 +0000285void ScheduleDAGList::ScheduleNodeBottomUp(SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000286 DEBUG(std::cerr << "*** Scheduling: ");
Chris Lattnerd4130372006-03-09 07:15:18 +0000287 DEBUG(SU->dump(&DAG));
Evan Cheng5e9a6952006-03-03 06:23:43 +0000288
Evan Chengab495562006-01-25 09:14:32 +0000289 Sequence.push_back(SU);
Evan Chengab495562006-01-25 09:14:32 +0000290
291 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000292 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
293 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner399bee22006-03-09 06:48:37 +0000294 ReleasePred(*I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000295 SU->NumPredsLeft--;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000296 }
297 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
298 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner399bee22006-03-09 06:48:37 +0000299 ReleasePred(*I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000300
301 CurrCycle++;
302}
303
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000304/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
305/// count of its successors. If a successor pending count is zero, add it to
306/// the Available queue.
Chris Lattner399bee22006-03-09 06:48:37 +0000307void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000308 DEBUG(std::cerr << "*** Scheduling: ");
Chris Lattnerd4130372006-03-09 07:15:18 +0000309 DEBUG(SU->dump(&DAG));
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000310
311 Sequence.push_back(SU);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000312
313 // Bottom up: release successors.
314 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
315 E1 = SU->Succs.end(); I1 != E1; ++I1) {
Chris Lattner399bee22006-03-09 06:48:37 +0000316 ReleaseSucc(*I1);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000317 SU->NumSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000318 }
319 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
320 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
Chris Lattner399bee22006-03-09 06:48:37 +0000321 ReleaseSucc(*I2, true);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000322
323 CurrCycle++;
324}
325
Evan Chengab495562006-01-25 09:14:32 +0000326/// isReady - True if node's lower cycle bound is less or equal to the current
327/// scheduling cycle. Always true if all nodes have uniform latency 1.
328static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
329 return SU->CycleBound <= CurrCycle;
330}
331
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000332/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
333/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000334void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000335 // Add root to Available queue.
Chris Lattner399bee22006-03-09 06:48:37 +0000336 PriorityQueue->push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000337
338 // While Available queue is not empty, grab the node with the highest
339 // priority. If it is not ready put it back. Schedule the node.
340 std::vector<SUnit*> NotReady;
Chris Lattner399bee22006-03-09 06:48:37 +0000341 while (!PriorityQueue->empty()) {
342 SUnit *CurrNode = PriorityQueue->pop();
Evan Chengab495562006-01-25 09:14:32 +0000343
Evan Chengab495562006-01-25 09:14:32 +0000344 while (!isReady(CurrNode, CurrCycle)) {
345 NotReady.push_back(CurrNode);
Chris Lattner399bee22006-03-09 06:48:37 +0000346 CurrNode = PriorityQueue->pop();
Evan Chengab495562006-01-25 09:14:32 +0000347 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000348
349 // Add the nodes that aren't ready back onto the available list.
Chris Lattner25e25562006-03-10 04:32:49 +0000350 PriorityQueue->push_all(NotReady);
351 NotReady.clear();
Evan Chengab495562006-01-25 09:14:32 +0000352
Chris Lattner25e25562006-03-10 04:32:49 +0000353 PriorityQueue->ScheduledNode(CurrNode);
Chris Lattner399bee22006-03-09 06:48:37 +0000354 ScheduleNodeBottomUp(CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000355 }
356
357 // Add entry node last
358 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
359 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Evan Chengab495562006-01-25 09:14:32 +0000360 Sequence.push_back(Entry);
361 }
362
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000363 // Reverse the order if it is bottom up.
364 std::reverse(Sequence.begin(), Sequence.end());
365
366
Evan Chengab495562006-01-25 09:14:32 +0000367#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000368 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000369 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000370 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
371 if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000372 if (!AnyNotSched)
373 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000374 SUnits[i].dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000375 std::cerr << "has not been scheduled!\n";
376 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000377 }
Evan Chengab495562006-01-25 09:14:32 +0000378 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000379 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000380#endif
Evan Chengab495562006-01-25 09:14:32 +0000381}
382
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000383/// ListScheduleTopDown - The main loop of list scheduling for top-down
384/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000385void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000386 // Emit the entry node first.
387 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner399bee22006-03-09 06:48:37 +0000388 ScheduleNodeTopDown(Entry);
Chris Lattner543832d2006-03-08 04:25:59 +0000389 HazardRec->EmitInstruction(Entry->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000390
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000391 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000392 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000393 // It is available if it has no predecessors.
Chris Lattner42e20262006-03-08 04:54:34 +0000394 if ((SUnits[i].Preds.size() + SUnits[i].ChainPreds.size()) == 0 &&
395 &SUnits[i] != Entry)
Chris Lattner399bee22006-03-09 06:48:37 +0000396 PriorityQueue->push(&SUnits[i]);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000397 }
398
399 // While Available queue is not empty, grab the node with the highest
400 // priority. If it is not ready put it back. Schedule the node.
401 std::vector<SUnit*> NotReady;
Chris Lattner399bee22006-03-09 06:48:37 +0000402 while (!PriorityQueue->empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000403 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000404
Chris Lattnere50c0922006-03-05 22:45:01 +0000405 bool HasNoopHazards = false;
406 do {
Chris Lattner399bee22006-03-09 06:48:37 +0000407 SUnit *CurNode = PriorityQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000408
409 // Get the node represented by this SUnit.
410 SDNode *N = CurNode->Node;
411 // If this is a pseudo op, like copyfromreg, look to see if there is a
412 // real target node flagged to it. If so, use the target node.
413 for (unsigned i = 0, e = CurNode->FlaggedNodes.size();
414 N->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
415 N = CurNode->FlaggedNodes[i];
416
Chris Lattner543832d2006-03-08 04:25:59 +0000417 HazardRecognizer::HazardType HT = HazardRec->getHazardType(N);
Chris Lattnere50c0922006-03-05 22:45:01 +0000418 if (HT == HazardRecognizer::NoHazard) {
Chris Lattner0c801bd2006-03-07 05:40:43 +0000419 FoundNode = CurNode;
Chris Lattnere50c0922006-03-05 22:45:01 +0000420 break;
421 }
422
423 // Remember if this is a noop hazard.
424 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
425
Chris Lattner0c801bd2006-03-07 05:40:43 +0000426 NotReady.push_back(CurNode);
Chris Lattner399bee22006-03-09 06:48:37 +0000427 } while (!PriorityQueue->empty());
Chris Lattnere50c0922006-03-05 22:45:01 +0000428
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000429 // Add the nodes that aren't ready back onto the available list.
Chris Lattner25e25562006-03-10 04:32:49 +0000430 PriorityQueue->push_all(NotReady);
431 NotReady.clear();
Chris Lattnere50c0922006-03-05 22:45:01 +0000432
433 // If we found a node to schedule, do it now.
434 if (FoundNode) {
Chris Lattner25e25562006-03-10 04:32:49 +0000435 PriorityQueue->ScheduledNode(FoundNode);
Chris Lattner399bee22006-03-09 06:48:37 +0000436 ScheduleNodeTopDown(FoundNode);
Chris Lattner543832d2006-03-08 04:25:59 +0000437 HazardRec->EmitInstruction(FoundNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000438 } else if (!HasNoopHazards) {
439 // Otherwise, we have a pipeline stall, but no other problem, just advance
440 // the current cycle and try again.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000441 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000442 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000443 ++NumStalls;
Chris Lattnere50c0922006-03-05 22:45:01 +0000444 } else {
445 // Otherwise, we have no instructions to issue and we have instructions
446 // that will fault if we don't do this right. This is the case for
447 // processors without pipeline interlocks and other cases.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000448 DEBUG(std::cerr << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000449 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000450 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000451 ++NumNoops;
Chris Lattnere50c0922006-03-05 22:45:01 +0000452 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000453 }
454
455#ifndef NDEBUG
456 // Verify that all SUnits were scheduled.
457 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000458 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
459 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000460 if (!AnyNotSched)
461 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000462 SUnits[i].dump(&DAG);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000463 std::cerr << "has not been scheduled!\n";
464 AnyNotSched = true;
465 }
466 }
467 assert(!AnyNotSched);
468#endif
469}
470
471
Evan Chengab495562006-01-25 09:14:32 +0000472void ScheduleDAGList::BuildSchedUnits() {
Chris Lattner42e20262006-03-08 04:54:34 +0000473 // Reserve entries in the vector for each of the SUnits we are creating. This
474 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
475 // invalidated.
476 SUnits.reserve(NodeCount);
477
Chris Lattnerd4130372006-03-09 07:15:18 +0000478 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
479
Evan Chengc4c339c2006-01-26 00:30:29 +0000480 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000481 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000482 NodeInfo *NI = &Info[i];
483 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000484 if (isPassiveNode(N))
485 continue;
Evan Chengab495562006-01-25 09:14:32 +0000486
Evan Chengc4c339c2006-01-26 00:30:29 +0000487 SUnit *SU;
488 if (NI->isInGroup()) {
489 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
490 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000491
Evan Chengc4c339c2006-01-26 00:30:29 +0000492 SU = NewSUnit(N);
493 // Find the flagged nodes.
494 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
495 SDNode *Flag = FlagOp.Val;
496 unsigned ResNo = FlagOp.ResNo;
497 while (Flag->getValueType(ResNo) == MVT::Flag) {
498 NodeInfo *FNI = getNI(Flag);
499 assert(FNI->Group == NI->Group);
500 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
501 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000502
Evan Chengc4c339c2006-01-26 00:30:29 +0000503 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
504 Flag = FlagOp.Val;
505 ResNo = FlagOp.ResNo;
506 }
507 } else {
508 SU = NewSUnit(N);
509 }
510 SUnitMap[N] = SU;
Chris Lattnerd4130372006-03-09 07:15:18 +0000511
512 // Compute the latency for the node. We use the sum of the latencies for
513 // all nodes flagged together into this SUnit.
Chris Lattnerc6c9e652006-03-09 17:31:22 +0000514 if (InstrItins.isEmpty()) {
Chris Lattnerd4130372006-03-09 07:15:18 +0000515 // No latency information.
516 SU->Latency = 1;
517 } else {
518 SU->Latency = 0;
519 if (N->isTargetOpcode()) {
520 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
521 InstrStage *S = InstrItins.begin(SchedClass);
522 InstrStage *E = InstrItins.end(SchedClass);
523 for (; S != E; ++S)
524 SU->Latency += S->Cycles;
525 }
526 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
527 SDNode *FNode = SU->FlaggedNodes[i];
528 if (FNode->isTargetOpcode()) {
529 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
530 InstrStage *S = InstrItins.begin(SchedClass);
531 InstrStage *E = InstrItins.end(SchedClass);
532 for (; S != E; ++S)
533 SU->Latency += S->Cycles;
534 }
535 }
536 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000537 }
Evan Chengab495562006-01-25 09:14:32 +0000538
Evan Chengc4c339c2006-01-26 00:30:29 +0000539 // Pass 2: add the preds, succs, etc.
Chris Lattner42e20262006-03-08 04:54:34 +0000540 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
541 SUnit *SU = &SUnits[i];
Evan Chengc4c339c2006-01-26 00:30:29 +0000542 SDNode *N = SU->Node;
543 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000544
545 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
546 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000547
548 if (NI->isInGroup()) {
549 // Find all predecessors (of the group).
550 NodeGroupOpIterator NGOI(NI);
551 while (!NGOI.isEnd()) {
552 SDOperand Op = NGOI.next();
553 SDNode *OpN = Op.Val;
554 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
555 NodeInfo *OpNI = getNI(OpN);
556 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
557 assert(VT != MVT::Flag);
558 SUnit *OpSU = SUnitMap[OpN];
559 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000560 if (SU->ChainPreds.insert(OpSU).second)
561 SU->NumChainPredsLeft++;
562 if (OpSU->ChainSuccs.insert(SU).second)
563 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000564 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000565 if (SU->Preds.insert(OpSU).second)
566 SU->NumPredsLeft++;
567 if (OpSU->Succs.insert(SU).second)
568 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000569 }
Evan Chengab495562006-01-25 09:14:32 +0000570 }
571 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000572 } else {
573 // Find node predecessors.
574 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
575 SDOperand Op = N->getOperand(j);
576 SDNode *OpN = Op.Val;
577 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
578 if (!isPassiveNode(OpN)) {
579 assert(VT != MVT::Flag);
580 SUnit *OpSU = SUnitMap[OpN];
581 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000582 if (SU->ChainPreds.insert(OpSU).second)
583 SU->NumChainPredsLeft++;
584 if (OpSU->ChainSuccs.insert(SU).second)
585 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000586 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000587 if (SU->Preds.insert(OpSU).second)
588 SU->NumPredsLeft++;
589 if (OpSU->Succs.insert(SU).second)
590 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000591 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000592 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000593 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000594 }
595 }
Evan Chengab495562006-01-25 09:14:32 +0000596 }
Chris Lattnerd4130372006-03-09 07:15:18 +0000597
598 DEBUG(SU->dumpAll(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000599 }
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++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000605 if (SUnit *SU = Sequence[i]) {
606 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
607 SDNode *N = SU->FlaggedNodes[j];
608 EmitNode(getNI(N));
609 }
610 EmitNode(getNI(SU->Node));
611 } else {
612 // Null SUnit* is a noop.
613 EmitNoop();
Evan Chengab495562006-01-25 09:14:32 +0000614 }
Evan Chengab495562006-01-25 09:14:32 +0000615 }
616}
617
618/// dump - dump the schedule.
Chris Lattnerd4130372006-03-09 07:15:18 +0000619void ScheduleDAGList::dumpSchedule() const {
Evan Chengab495562006-01-25 09:14:32 +0000620 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000621 if (SUnit *SU = Sequence[i])
Chris Lattnerd4130372006-03-09 07:15:18 +0000622 SU->dump(&DAG);
Chris Lattner2d945ba2006-03-05 23:51:47 +0000623 else
624 std::cerr << "**** NOOP ****\n";
Evan Chengab495562006-01-25 09:14:32 +0000625 }
626}
627
628/// Schedule - Schedule the DAG using list scheduling.
629/// FIXME: Right now it only supports the burr (bottom up register reducing)
630/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000631void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000632 DEBUG(std::cerr << "********** List Scheduling **********\n");
633
634 // Build scheduling units.
635 BuildSchedUnits();
Chris Lattnerfd22d422006-03-08 05:18:27 +0000636
Chris Lattner9df64752006-03-09 06:35:14 +0000637 PriorityQueue->initNodes(SUnits);
638
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000639 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
640 if (isBottomUp)
Chris Lattner399bee22006-03-09 06:48:37 +0000641 ListScheduleBottomUp();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000642 else
Chris Lattner399bee22006-03-09 06:48:37 +0000643 ListScheduleTopDown();
Chris Lattner9df64752006-03-09 06:35:14 +0000644
645 PriorityQueue->releaseState();
646
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000647 DEBUG(std::cerr << "*** Final schedule ***\n");
Chris Lattnerd4130372006-03-09 07:15:18 +0000648 DEBUG(dumpSchedule());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000649 DEBUG(std::cerr << "\n");
650
Evan Chengab495562006-01-25 09:14:32 +0000651 // Emit in scheduled order
652 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000653}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000654
Chris Lattner9df64752006-03-09 06:35:14 +0000655//===----------------------------------------------------------------------===//
656// RegReductionPriorityQueue Implementation
657//===----------------------------------------------------------------------===//
658//
659// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
660// to reduce register pressure.
661//
662namespace {
663 class RegReductionPriorityQueue;
664
665 /// Sorting functions for the Available queue.
666 struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
667 RegReductionPriorityQueue *SPQ;
668 ls_rr_sort(RegReductionPriorityQueue *spq) : SPQ(spq) {}
669 ls_rr_sort(const ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
670
671 bool operator()(const SUnit* left, const SUnit* right) const;
672 };
673} // end anonymous namespace
674
675namespace {
676 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
677 // SUnits - The SUnits for the current graph.
678 const std::vector<SUnit> *SUnits;
679
680 // SethiUllmanNumbers - The SethiUllman number for each node.
681 std::vector<int> SethiUllmanNumbers;
682
683 std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Queue;
684 public:
685 RegReductionPriorityQueue() : Queue(ls_rr_sort(this)) {
686 }
687
688 void initNodes(const std::vector<SUnit> &sunits) {
689 SUnits = &sunits;
690 // Calculate node priorities.
691 CalculatePriorities();
692 }
693 void releaseState() {
694 SUnits = 0;
695 SethiUllmanNumbers.clear();
696 }
697
698 unsigned getSethiUllmanNumber(unsigned NodeNum) const {
699 assert(NodeNum < SethiUllmanNumbers.size());
700 return SethiUllmanNumbers[NodeNum];
701 }
702
703 bool empty() const { return Queue.empty(); }
704
705 void push(SUnit *U) {
706 Queue.push(U);
707 }
Chris Lattner25e25562006-03-10 04:32:49 +0000708 void push_all(const std::vector<SUnit *> &Nodes) {
709 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
710 Queue.push(Nodes[i]);
711 }
712
Chris Lattner9df64752006-03-09 06:35:14 +0000713 SUnit *pop() {
714 SUnit *V = Queue.top();
715 Queue.pop();
716 return V;
717 }
718 private:
719 void CalculatePriorities();
720 int CalcNodePriority(const SUnit *SU);
721 };
722}
723
724bool ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
725 unsigned LeftNum = left->NodeNum;
726 unsigned RightNum = right->NodeNum;
727
728 int LBonus = (int)left ->isDefNUseOperand;
729 int RBonus = (int)right->isDefNUseOperand;
730
731 // Special tie breaker: if two nodes share a operand, the one that
732 // use it as a def&use operand is preferred.
733 if (left->isTwoAddress && !right->isTwoAddress) {
734 SDNode *DUNode = left->Node->getOperand(0).Val;
735 if (DUNode->isOperand(right->Node))
736 LBonus++;
737 }
738 if (!left->isTwoAddress && right->isTwoAddress) {
739 SDNode *DUNode = right->Node->getOperand(0).Val;
740 if (DUNode->isOperand(left->Node))
741 RBonus++;
742 }
743
744 // Priority1 is just the number of live range genned.
745 int LPriority1 = left ->NumPredsLeft - LBonus;
746 int RPriority1 = right->NumPredsLeft - RBonus;
747 int LPriority2 = SPQ->getSethiUllmanNumber(LeftNum) + LBonus;
748 int RPriority2 = SPQ->getSethiUllmanNumber(RightNum) + RBonus;
749
750 if (LPriority1 > RPriority1)
751 return true;
752 else if (LPriority1 == RPriority1)
753 if (LPriority2 < RPriority2)
754 return true;
755 else if (LPriority2 == RPriority2)
756 if (left->CycleBound > right->CycleBound)
757 return true;
758
759 return false;
760}
761
762
763/// CalcNodePriority - Priority is the Sethi Ullman number.
764/// Smaller number is the higher priority.
765int RegReductionPriorityQueue::CalcNodePriority(const SUnit *SU) {
766 int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
767 if (SethiUllmanNumber != INT_MIN)
768 return SethiUllmanNumber;
769
770 if (SU->Preds.size() == 0) {
771 SethiUllmanNumber = 1;
772 } else {
773 int Extra = 0;
Jeff Cohen6ce97682006-03-10 03:57:45 +0000774 for (std::set<SUnit*>::const_iterator I = SU->Preds.begin(),
Chris Lattner9df64752006-03-09 06:35:14 +0000775 E = SU->Preds.end(); I != E; ++I) {
776 SUnit *PredSU = *I;
777 int PredSethiUllman = CalcNodePriority(PredSU);
778 if (PredSethiUllman > SethiUllmanNumber) {
779 SethiUllmanNumber = PredSethiUllman;
780 Extra = 0;
781 } else if (PredSethiUllman == SethiUllmanNumber)
782 Extra++;
783 }
784
785 if (SU->Node->getOpcode() != ISD::TokenFactor)
786 SethiUllmanNumber += Extra;
787 else
788 SethiUllmanNumber = (Extra == 1) ? 0 : Extra-1;
789 }
790
791 return SethiUllmanNumber;
792}
793
794/// CalculatePriorities - Calculate priorities of all scheduling units.
795void RegReductionPriorityQueue::CalculatePriorities() {
796 SethiUllmanNumbers.assign(SUnits->size(), INT_MIN);
797
798 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
799 CalcNodePriority(&(*SUnits)[i]);
800}
801
Chris Lattner6398c132006-03-09 07:38:27 +0000802//===----------------------------------------------------------------------===//
803// LatencyPriorityQueue Implementation
804//===----------------------------------------------------------------------===//
805//
806// This is a SchedulingPriorityQueue that schedules using latency information to
807// reduce the length of the critical path through the basic block.
808//
809namespace {
810 class LatencyPriorityQueue;
811
812 /// Sorting functions for the Available queue.
813 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
814 LatencyPriorityQueue *PQ;
815 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
816 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
817
818 bool operator()(const SUnit* left, const SUnit* right) const;
819 };
820} // end anonymous namespace
821
822namespace {
823 class LatencyPriorityQueue : public SchedulingPriorityQueue {
824 // SUnits - The SUnits for the current graph.
825 const std::vector<SUnit> *SUnits;
826
827 // Latencies - The latency (max of latency from this node to the bb exit)
828 // for each node.
829 std::vector<int> Latencies;
830
831 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
832public:
833 LatencyPriorityQueue() : Queue(latency_sort(this)) {
834 }
835
836 void initNodes(const std::vector<SUnit> &sunits) {
837 SUnits = &sunits;
838 // Calculate node priorities.
839 CalculatePriorities();
840 }
841 void releaseState() {
842 SUnits = 0;
843 Latencies.clear();
844 }
845
846 unsigned getLatency(unsigned NodeNum) const {
847 assert(NodeNum < Latencies.size());
848 return Latencies[NodeNum];
849 }
850
851 bool empty() const { return Queue.empty(); }
852
853 void push(SUnit *U) {
854 Queue.push(U);
855 }
Chris Lattner25e25562006-03-10 04:32:49 +0000856 void push_all(const std::vector<SUnit *> &Nodes) {
857 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
858 Queue.push(Nodes[i]);
859 }
860
Chris Lattner6398c132006-03-09 07:38:27 +0000861 SUnit *pop() {
862 SUnit *V = Queue.top();
863 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000864 return V;
865 }
866private:
867 void CalculatePriorities();
868 int CalcLatency(const SUnit &SU);
869 };
870}
871
872bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
873 unsigned LHSNum = LHS->NodeNum;
874 unsigned RHSNum = RHS->NodeNum;
875
876 return PQ->getLatency(LHSNum) < PQ->getLatency(RHSNum);
877}
878
879
880/// CalcNodePriority - Calculate the maximal path from the node to the exit.
881///
882int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
883 int &Latency = Latencies[SU.NodeNum];
884 if (Latency != -1)
885 return Latency;
886
887 int MaxSuccLatency = 0;
Jeff Cohen6ce97682006-03-10 03:57:45 +0000888 for (std::set<SUnit*>::const_iterator I = SU.Succs.begin(),
Chris Lattner6398c132006-03-09 07:38:27 +0000889 E = SU.Succs.end(); I != E; ++I)
890 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I));
891
Jeff Cohen6ce97682006-03-10 03:57:45 +0000892 for (std::set<SUnit*>::const_iterator I = SU.ChainSuccs.begin(),
Chris Lattner6398c132006-03-09 07:38:27 +0000893 E = SU.ChainSuccs.end(); I != E; ++I)
894 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I));
895
896 return Latency = MaxSuccLatency + SU.Latency;
897}
898
899/// CalculatePriorities - Calculate priorities of all scheduling units.
900void LatencyPriorityQueue::CalculatePriorities() {
901 Latencies.assign(SUnits->size(), -1);
902
903 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
904 CalcLatency((*SUnits)[i]);
905}
906
Chris Lattner9df64752006-03-09 06:35:14 +0000907
908//===----------------------------------------------------------------------===//
909// Public Constructor Functions
910//===----------------------------------------------------------------------===//
911
Evan Chengab495562006-01-25 09:14:32 +0000912llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
913 MachineBasicBlock *BB) {
Chris Lattner543832d2006-03-08 04:25:59 +0000914 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true,
Chris Lattner9df64752006-03-09 06:35:14 +0000915 new RegReductionPriorityQueue(),
Chris Lattner543832d2006-03-08 04:25:59 +0000916 new HazardRecognizer());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000917}
918
Chris Lattner47639db2006-03-06 00:22:00 +0000919/// createTDListDAGScheduler - This creates a top-down list scheduler with the
920/// specified hazard recognizer.
921ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
922 MachineBasicBlock *BB,
Chris Lattner543832d2006-03-08 04:25:59 +0000923 HazardRecognizer *HR) {
Chris Lattner9df64752006-03-09 06:35:14 +0000924 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false,
Chris Lattner6398c132006-03-09 07:38:27 +0000925 new LatencyPriorityQueue(),
Chris Lattner9df64752006-03-09 06:35:14 +0000926 HR);
Evan Cheng31272342006-01-23 08:26:10 +0000927}