blob: 202bfa3f591fa84765380c1438d05a2f43ff09ce [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;
144 virtual SUnit *pop() = 0;
145};
Chris Lattner9e95acc2006-03-09 06:37:29 +0000146}
Chris Lattnerfd22d422006-03-08 05:18:27 +0000147
148
Chris Lattnere50c0922006-03-05 22:45:01 +0000149
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000150namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +0000151//===----------------------------------------------------------------------===//
152/// ScheduleDAGList - The actual list scheduler implementation. This supports
153/// both top-down and bottom-up scheduling.
154///
Evan Cheng31272342006-01-23 08:26:10 +0000155class ScheduleDAGList : public ScheduleDAG {
156private:
Evan Chengab495562006-01-25 09:14:32 +0000157 // SDNode to SUnit mapping (many to one).
158 std::map<SDNode*, SUnit*> SUnitMap;
Chris Lattner00b52ea2006-03-05 23:59:20 +0000159 // The schedule. Null SUnit*'s represent noop instructions.
Evan Chengab495562006-01-25 09:14:32 +0000160 std::vector<SUnit*> Sequence;
161 // Current scheduling cycle.
162 unsigned CurrCycle;
Chris Lattner42e20262006-03-08 04:54:34 +0000163
164 // The scheduling units.
165 std::vector<SUnit> SUnits;
Evan Cheng31272342006-01-23 08:26:10 +0000166
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000167 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
168 /// it is top-down.
169 bool isBottomUp;
170
Chris Lattner9df64752006-03-09 06:35:14 +0000171 /// PriorityQueue - The priority queue to use.
172 SchedulingPriorityQueue *PriorityQueue;
173
Chris Lattnere50c0922006-03-05 22:45:01 +0000174 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +0000175 HazardRecognizer *HazardRec;
Chris Lattnere50c0922006-03-05 22:45:01 +0000176
Evan Cheng31272342006-01-23 08:26:10 +0000177public:
178 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000179 const TargetMachine &tm, bool isbottomup,
Chris Lattner9df64752006-03-09 06:35:14 +0000180 SchedulingPriorityQueue *priorityqueue,
Chris Lattner543832d2006-03-08 04:25:59 +0000181 HazardRecognizer *HR)
Evan Chengc4c339c2006-01-26 00:30:29 +0000182 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner9df64752006-03-09 06:35:14 +0000183 CurrCycle(0), isBottomUp(isbottomup),
184 PriorityQueue(priorityqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000185 }
Evan Chengab495562006-01-25 09:14:32 +0000186
187 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +0000188 delete HazardRec;
Chris Lattner9df64752006-03-09 06:35:14 +0000189 delete PriorityQueue;
Evan Chengab495562006-01-25 09:14:32 +0000190 }
Evan Cheng31272342006-01-23 08:26:10 +0000191
192 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000193
Chris Lattnerd4130372006-03-09 07:15:18 +0000194 void dumpSchedule() const;
Evan Chengab495562006-01-25 09:14:32 +0000195
196private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000197 SUnit *NewSUnit(SDNode *N);
Chris Lattner399bee22006-03-09 06:48:37 +0000198 void ReleasePred(SUnit *PredSU, bool isChain = false);
199 void ReleaseSucc(SUnit *SuccSU, bool isChain = false);
200 void ScheduleNodeBottomUp(SUnit *SU);
201 void ScheduleNodeTopDown(SUnit *SU);
202 void ListScheduleTopDown();
203 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000204 void BuildSchedUnits();
205 void EmitSchedule();
206};
Chris Lattneraf5e26c2006-03-08 04:37:58 +0000207} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +0000208
Chris Lattner47639db2006-03-06 00:22:00 +0000209HazardRecognizer::~HazardRecognizer() {}
210
Evan Chengc4c339c2006-01-26 00:30:29 +0000211
212/// NewSUnit - Creates a new SUnit and return a ptr to it.
213SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
Chris Lattnerfd22d422006-03-08 05:18:27 +0000214 SUnits.push_back(SUnit(N, SUnits.size()));
Chris Lattner42e20262006-03-08 04:54:34 +0000215 return &SUnits.back();
Evan Chengc4c339c2006-01-26 00:30:29 +0000216}
217
218/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
219/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner399bee22006-03-09 06:48:37 +0000220void ScheduleDAGList::ReleasePred(SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000221 // FIXME: the distance between two nodes is not always == the predecessor's
222 // latency. For example, the reader can very well read the register written
223 // by the predecessor later than the issue cycle. It also depends on the
224 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000225 PredSU->CycleBound = std::max(PredSU->CycleBound,CurrCycle + PredSU->Latency);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000226
Evan Chengc5c06582006-03-06 06:08:54 +0000227 if (!isChain)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000228 PredSU->NumSuccsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000229 else
Evan Cheng4e3904f2006-03-02 21:38:29 +0000230 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000231
Evan Chengab495562006-01-25 09:14:32 +0000232#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000233 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000234 std::cerr << "*** List scheduling failed! ***\n";
235 PredSU->dump(&DAG);
236 std::cerr << " has been released too many times!\n";
237 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000238 }
Evan Chengab495562006-01-25 09:14:32 +0000239#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000240
241 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
242 // EntryToken has to go last! Special case it here.
243 if (PredSU->Node->getOpcode() != ISD::EntryToken)
Chris Lattner399bee22006-03-09 06:48:37 +0000244 PriorityQueue->push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000245 }
Evan Chengab495562006-01-25 09:14:32 +0000246}
247
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000248/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
249/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner399bee22006-03-09 06:48:37 +0000250void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000251 // FIXME: the distance between two nodes is not always == the predecessor's
252 // latency. For example, the reader can very well read the register written
253 // by the predecessor later than the issue cycle. It also depends on the
254 // interrupt model (drain vs. freeze).
Chris Lattner12c6d892006-03-08 04:41:06 +0000255 SuccSU->CycleBound = std::max(SuccSU->CycleBound,CurrCycle + SuccSU->Latency);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000256
Evan Chengc5c06582006-03-06 06:08:54 +0000257 if (!isChain)
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000258 SuccSU->NumPredsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000259 else
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000260 SuccSU->NumChainPredsLeft--;
261
262#ifndef NDEBUG
263 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
264 std::cerr << "*** List scheduling failed! ***\n";
265 SuccSU->dump(&DAG);
266 std::cerr << " has been released too many times!\n";
267 abort();
268 }
269#endif
270
271 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
Chris Lattner399bee22006-03-09 06:48:37 +0000272 PriorityQueue->push(SuccSU);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000273}
274
275/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
276/// count of its predecessors. If a predecessor pending count is zero, add it to
277/// the Available queue.
Chris Lattner399bee22006-03-09 06:48:37 +0000278void ScheduleDAGList::ScheduleNodeBottomUp(SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000279 DEBUG(std::cerr << "*** Scheduling: ");
Chris Lattnerd4130372006-03-09 07:15:18 +0000280 DEBUG(SU->dump(&DAG));
Evan Cheng5e9a6952006-03-03 06:23:43 +0000281
Evan Chengab495562006-01-25 09:14:32 +0000282 Sequence.push_back(SU);
Evan Chengab495562006-01-25 09:14:32 +0000283
284 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000285 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
286 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner399bee22006-03-09 06:48:37 +0000287 ReleasePred(*I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000288 SU->NumPredsLeft--;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000289 }
290 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
291 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner399bee22006-03-09 06:48:37 +0000292 ReleasePred(*I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000293
294 CurrCycle++;
295}
296
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000297/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
298/// count of its successors. If a successor pending count is zero, add it to
299/// the Available queue.
Chris Lattner399bee22006-03-09 06:48:37 +0000300void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000301 DEBUG(std::cerr << "*** Scheduling: ");
Chris Lattnerd4130372006-03-09 07:15:18 +0000302 DEBUG(SU->dump(&DAG));
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000303
304 Sequence.push_back(SU);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000305
306 // Bottom up: release successors.
307 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
308 E1 = SU->Succs.end(); I1 != E1; ++I1) {
Chris Lattner399bee22006-03-09 06:48:37 +0000309 ReleaseSucc(*I1);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000310 SU->NumSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000311 }
312 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
313 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
Chris Lattner399bee22006-03-09 06:48:37 +0000314 ReleaseSucc(*I2, true);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000315
316 CurrCycle++;
317}
318
Evan Chengab495562006-01-25 09:14:32 +0000319/// isReady - True if node's lower cycle bound is less or equal to the current
320/// scheduling cycle. Always true if all nodes have uniform latency 1.
321static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
322 return SU->CycleBound <= CurrCycle;
323}
324
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000325/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
326/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000327void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000328 // Add root to Available queue.
Chris Lattner399bee22006-03-09 06:48:37 +0000329 PriorityQueue->push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000330
331 // While Available queue is not empty, grab the node with the highest
332 // priority. If it is not ready put it back. Schedule the node.
333 std::vector<SUnit*> NotReady;
Chris Lattner399bee22006-03-09 06:48:37 +0000334 while (!PriorityQueue->empty()) {
335 SUnit *CurrNode = PriorityQueue->pop();
Evan Chengab495562006-01-25 09:14:32 +0000336
Evan Chengab495562006-01-25 09:14:32 +0000337 while (!isReady(CurrNode, CurrCycle)) {
338 NotReady.push_back(CurrNode);
Chris Lattner399bee22006-03-09 06:48:37 +0000339 CurrNode = PriorityQueue->pop();
Evan Chengab495562006-01-25 09:14:32 +0000340 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000341
342 // Add the nodes that aren't ready back onto the available list.
343 while (!NotReady.empty()) {
Chris Lattner399bee22006-03-09 06:48:37 +0000344 PriorityQueue->push(NotReady.back());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000345 NotReady.pop_back();
346 }
Evan Chengab495562006-01-25 09:14:32 +0000347
Chris Lattner399bee22006-03-09 06:48:37 +0000348 ScheduleNodeBottomUp(CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000349 }
350
351 // Add entry node last
352 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
353 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Evan Chengab495562006-01-25 09:14:32 +0000354 Sequence.push_back(Entry);
355 }
356
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000357 // Reverse the order if it is bottom up.
358 std::reverse(Sequence.begin(), Sequence.end());
359
360
Evan Chengab495562006-01-25 09:14:32 +0000361#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000362 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000363 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000364 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
365 if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000366 if (!AnyNotSched)
367 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000368 SUnits[i].dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000369 std::cerr << "has not been scheduled!\n";
370 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000371 }
Evan Chengab495562006-01-25 09:14:32 +0000372 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000373 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000374#endif
Evan Chengab495562006-01-25 09:14:32 +0000375}
376
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000377/// ListScheduleTopDown - The main loop of list scheduling for top-down
378/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000379void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000380 // Emit the entry node first.
381 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner399bee22006-03-09 06:48:37 +0000382 ScheduleNodeTopDown(Entry);
Chris Lattner543832d2006-03-08 04:25:59 +0000383 HazardRec->EmitInstruction(Entry->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000384
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000385 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000386 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000387 // It is available if it has no predecessors.
Chris Lattner42e20262006-03-08 04:54:34 +0000388 if ((SUnits[i].Preds.size() + SUnits[i].ChainPreds.size()) == 0 &&
389 &SUnits[i] != Entry)
Chris Lattner399bee22006-03-09 06:48:37 +0000390 PriorityQueue->push(&SUnits[i]);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000391 }
392
393 // While Available queue is not empty, grab the node with the highest
394 // priority. If it is not ready put it back. Schedule the node.
395 std::vector<SUnit*> NotReady;
Chris Lattner399bee22006-03-09 06:48:37 +0000396 while (!PriorityQueue->empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000397 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000398
Chris Lattnere50c0922006-03-05 22:45:01 +0000399 bool HasNoopHazards = false;
400 do {
Chris Lattner399bee22006-03-09 06:48:37 +0000401 SUnit *CurNode = PriorityQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000402
403 // Get the node represented by this SUnit.
404 SDNode *N = CurNode->Node;
405 // If this is a pseudo op, like copyfromreg, look to see if there is a
406 // real target node flagged to it. If so, use the target node.
407 for (unsigned i = 0, e = CurNode->FlaggedNodes.size();
408 N->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
409 N = CurNode->FlaggedNodes[i];
410
Chris Lattner543832d2006-03-08 04:25:59 +0000411 HazardRecognizer::HazardType HT = HazardRec->getHazardType(N);
Chris Lattnere50c0922006-03-05 22:45:01 +0000412 if (HT == HazardRecognizer::NoHazard) {
Chris Lattner0c801bd2006-03-07 05:40:43 +0000413 FoundNode = CurNode;
Chris Lattnere50c0922006-03-05 22:45:01 +0000414 break;
415 }
416
417 // Remember if this is a noop hazard.
418 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
419
Chris Lattner0c801bd2006-03-07 05:40:43 +0000420 NotReady.push_back(CurNode);
Chris Lattner399bee22006-03-09 06:48:37 +0000421 } while (!PriorityQueue->empty());
Chris Lattnere50c0922006-03-05 22:45:01 +0000422
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000423 // Add the nodes that aren't ready back onto the available list.
424 while (!NotReady.empty()) {
Chris Lattner399bee22006-03-09 06:48:37 +0000425 PriorityQueue->push(NotReady.back());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000426 NotReady.pop_back();
427 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000428
429 // If we found a node to schedule, do it now.
430 if (FoundNode) {
Chris Lattner399bee22006-03-09 06:48:37 +0000431 ScheduleNodeTopDown(FoundNode);
Chris Lattner543832d2006-03-08 04:25:59 +0000432 HazardRec->EmitInstruction(FoundNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000433 } else if (!HasNoopHazards) {
434 // Otherwise, we have a pipeline stall, but no other problem, just advance
435 // the current cycle and try again.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000436 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000437 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000438 ++NumStalls;
Chris Lattnere50c0922006-03-05 22:45:01 +0000439 } else {
440 // Otherwise, we have no instructions to issue and we have instructions
441 // that will fault if we don't do this right. This is the case for
442 // processors without pipeline interlocks and other cases.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000443 DEBUG(std::cerr << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000444 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000445 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000446 ++NumNoops;
Chris Lattnere50c0922006-03-05 22:45:01 +0000447 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000448 }
449
450#ifndef NDEBUG
451 // Verify that all SUnits were scheduled.
452 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000453 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
454 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000455 if (!AnyNotSched)
456 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000457 SUnits[i].dump(&DAG);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000458 std::cerr << "has not been scheduled!\n";
459 AnyNotSched = true;
460 }
461 }
462 assert(!AnyNotSched);
463#endif
464}
465
466
Evan Chengab495562006-01-25 09:14:32 +0000467void ScheduleDAGList::BuildSchedUnits() {
Chris Lattner42e20262006-03-08 04:54:34 +0000468 // Reserve entries in the vector for each of the SUnits we are creating. This
469 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
470 // invalidated.
471 SUnits.reserve(NodeCount);
472
Chris Lattnerd4130372006-03-09 07:15:18 +0000473 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
474
Evan Chengc4c339c2006-01-26 00:30:29 +0000475 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000476 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000477 NodeInfo *NI = &Info[i];
478 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000479 if (isPassiveNode(N))
480 continue;
Evan Chengab495562006-01-25 09:14:32 +0000481
Evan Chengc4c339c2006-01-26 00:30:29 +0000482 SUnit *SU;
483 if (NI->isInGroup()) {
484 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
485 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000486
Evan Chengc4c339c2006-01-26 00:30:29 +0000487 SU = NewSUnit(N);
488 // Find the flagged nodes.
489 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
490 SDNode *Flag = FlagOp.Val;
491 unsigned ResNo = FlagOp.ResNo;
492 while (Flag->getValueType(ResNo) == MVT::Flag) {
493 NodeInfo *FNI = getNI(Flag);
494 assert(FNI->Group == NI->Group);
495 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
496 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000497
Evan Chengc4c339c2006-01-26 00:30:29 +0000498 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
499 Flag = FlagOp.Val;
500 ResNo = FlagOp.ResNo;
501 }
502 } else {
503 SU = NewSUnit(N);
504 }
505 SUnitMap[N] = SU;
Chris Lattnerd4130372006-03-09 07:15:18 +0000506
507 // Compute the latency for the node. We use the sum of the latencies for
508 // all nodes flagged together into this SUnit.
Chris Lattnerc6c9e652006-03-09 17:31:22 +0000509 if (InstrItins.isEmpty()) {
Chris Lattnerd4130372006-03-09 07:15:18 +0000510 // No latency information.
511 SU->Latency = 1;
512 } else {
513 SU->Latency = 0;
514 if (N->isTargetOpcode()) {
515 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
516 InstrStage *S = InstrItins.begin(SchedClass);
517 InstrStage *E = InstrItins.end(SchedClass);
518 for (; S != E; ++S)
519 SU->Latency += S->Cycles;
520 }
521 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
522 SDNode *FNode = SU->FlaggedNodes[i];
523 if (FNode->isTargetOpcode()) {
524 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
525 InstrStage *S = InstrItins.begin(SchedClass);
526 InstrStage *E = InstrItins.end(SchedClass);
527 for (; S != E; ++S)
528 SU->Latency += S->Cycles;
529 }
530 }
531 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000532 }
Evan Chengab495562006-01-25 09:14:32 +0000533
Evan Chengc4c339c2006-01-26 00:30:29 +0000534 // Pass 2: add the preds, succs, etc.
Chris Lattner42e20262006-03-08 04:54:34 +0000535 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
536 SUnit *SU = &SUnits[i];
Evan Chengc4c339c2006-01-26 00:30:29 +0000537 SDNode *N = SU->Node;
538 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000539
540 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
541 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000542
543 if (NI->isInGroup()) {
544 // Find all predecessors (of the group).
545 NodeGroupOpIterator NGOI(NI);
546 while (!NGOI.isEnd()) {
547 SDOperand Op = NGOI.next();
548 SDNode *OpN = Op.Val;
549 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
550 NodeInfo *OpNI = getNI(OpN);
551 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
552 assert(VT != MVT::Flag);
553 SUnit *OpSU = SUnitMap[OpN];
554 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000555 if (SU->ChainPreds.insert(OpSU).second)
556 SU->NumChainPredsLeft++;
557 if (OpSU->ChainSuccs.insert(SU).second)
558 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000559 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000560 if (SU->Preds.insert(OpSU).second)
561 SU->NumPredsLeft++;
562 if (OpSU->Succs.insert(SU).second)
563 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000564 }
Evan Chengab495562006-01-25 09:14:32 +0000565 }
566 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000567 } else {
568 // Find node predecessors.
569 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
570 SDOperand Op = N->getOperand(j);
571 SDNode *OpN = Op.Val;
572 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
573 if (!isPassiveNode(OpN)) {
574 assert(VT != MVT::Flag);
575 SUnit *OpSU = SUnitMap[OpN];
576 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000577 if (SU->ChainPreds.insert(OpSU).second)
578 SU->NumChainPredsLeft++;
579 if (OpSU->ChainSuccs.insert(SU).second)
580 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000581 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000582 if (SU->Preds.insert(OpSU).second)
583 SU->NumPredsLeft++;
584 if (OpSU->Succs.insert(SU).second)
585 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000586 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000587 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000588 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000589 }
590 }
Evan Chengab495562006-01-25 09:14:32 +0000591 }
Chris Lattnerd4130372006-03-09 07:15:18 +0000592
593 DEBUG(SU->dumpAll(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000594 }
Evan Chengab495562006-01-25 09:14:32 +0000595}
596
597/// EmitSchedule - Emit the machine code in scheduled order.
598void ScheduleDAGList::EmitSchedule() {
599 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000600 if (SUnit *SU = Sequence[i]) {
601 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
602 SDNode *N = SU->FlaggedNodes[j];
603 EmitNode(getNI(N));
604 }
605 EmitNode(getNI(SU->Node));
606 } else {
607 // Null SUnit* is a noop.
608 EmitNoop();
Evan Chengab495562006-01-25 09:14:32 +0000609 }
Evan Chengab495562006-01-25 09:14:32 +0000610 }
611}
612
613/// dump - dump the schedule.
Chris Lattnerd4130372006-03-09 07:15:18 +0000614void ScheduleDAGList::dumpSchedule() const {
Evan Chengab495562006-01-25 09:14:32 +0000615 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000616 if (SUnit *SU = Sequence[i])
Chris Lattnerd4130372006-03-09 07:15:18 +0000617 SU->dump(&DAG);
Chris Lattner2d945ba2006-03-05 23:51:47 +0000618 else
619 std::cerr << "**** NOOP ****\n";
Evan Chengab495562006-01-25 09:14:32 +0000620 }
621}
622
623/// Schedule - Schedule the DAG using list scheduling.
624/// FIXME: Right now it only supports the burr (bottom up register reducing)
625/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000626void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000627 DEBUG(std::cerr << "********** List Scheduling **********\n");
628
629 // Build scheduling units.
630 BuildSchedUnits();
Chris Lattnerfd22d422006-03-08 05:18:27 +0000631
Chris Lattner9df64752006-03-09 06:35:14 +0000632 PriorityQueue->initNodes(SUnits);
633
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000634 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
635 if (isBottomUp)
Chris Lattner399bee22006-03-09 06:48:37 +0000636 ListScheduleBottomUp();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000637 else
Chris Lattner399bee22006-03-09 06:48:37 +0000638 ListScheduleTopDown();
Chris Lattner9df64752006-03-09 06:35:14 +0000639
640 PriorityQueue->releaseState();
641
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000642 DEBUG(std::cerr << "*** Final schedule ***\n");
Chris Lattnerd4130372006-03-09 07:15:18 +0000643 DEBUG(dumpSchedule());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000644 DEBUG(std::cerr << "\n");
645
Evan Chengab495562006-01-25 09:14:32 +0000646 // Emit in scheduled order
647 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000648}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000649
Chris Lattner9df64752006-03-09 06:35:14 +0000650//===----------------------------------------------------------------------===//
651// RegReductionPriorityQueue Implementation
652//===----------------------------------------------------------------------===//
653//
654// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
655// to reduce register pressure.
656//
657namespace {
658 class RegReductionPriorityQueue;
659
660 /// Sorting functions for the Available queue.
661 struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
662 RegReductionPriorityQueue *SPQ;
663 ls_rr_sort(RegReductionPriorityQueue *spq) : SPQ(spq) {}
664 ls_rr_sort(const ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
665
666 bool operator()(const SUnit* left, const SUnit* right) const;
667 };
668} // end anonymous namespace
669
670namespace {
671 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
672 // SUnits - The SUnits for the current graph.
673 const std::vector<SUnit> *SUnits;
674
675 // SethiUllmanNumbers - The SethiUllman number for each node.
676 std::vector<int> SethiUllmanNumbers;
677
678 std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Queue;
679 public:
680 RegReductionPriorityQueue() : Queue(ls_rr_sort(this)) {
681 }
682
683 void initNodes(const std::vector<SUnit> &sunits) {
684 SUnits = &sunits;
685 // Calculate node priorities.
686 CalculatePriorities();
687 }
688 void releaseState() {
689 SUnits = 0;
690 SethiUllmanNumbers.clear();
691 }
692
693 unsigned getSethiUllmanNumber(unsigned NodeNum) const {
694 assert(NodeNum < SethiUllmanNumbers.size());
695 return SethiUllmanNumbers[NodeNum];
696 }
697
698 bool empty() const { return Queue.empty(); }
699
700 void push(SUnit *U) {
701 Queue.push(U);
702 }
703 SUnit *pop() {
704 SUnit *V = Queue.top();
705 Queue.pop();
706 return V;
707 }
708 private:
709 void CalculatePriorities();
710 int CalcNodePriority(const SUnit *SU);
711 };
712}
713
714bool ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
715 unsigned LeftNum = left->NodeNum;
716 unsigned RightNum = right->NodeNum;
717
718 int LBonus = (int)left ->isDefNUseOperand;
719 int RBonus = (int)right->isDefNUseOperand;
720
721 // Special tie breaker: if two nodes share a operand, the one that
722 // use it as a def&use operand is preferred.
723 if (left->isTwoAddress && !right->isTwoAddress) {
724 SDNode *DUNode = left->Node->getOperand(0).Val;
725 if (DUNode->isOperand(right->Node))
726 LBonus++;
727 }
728 if (!left->isTwoAddress && right->isTwoAddress) {
729 SDNode *DUNode = right->Node->getOperand(0).Val;
730 if (DUNode->isOperand(left->Node))
731 RBonus++;
732 }
733
734 // Priority1 is just the number of live range genned.
735 int LPriority1 = left ->NumPredsLeft - LBonus;
736 int RPriority1 = right->NumPredsLeft - RBonus;
737 int LPriority2 = SPQ->getSethiUllmanNumber(LeftNum) + LBonus;
738 int RPriority2 = SPQ->getSethiUllmanNumber(RightNum) + RBonus;
739
740 if (LPriority1 > RPriority1)
741 return true;
742 else if (LPriority1 == RPriority1)
743 if (LPriority2 < RPriority2)
744 return true;
745 else if (LPriority2 == RPriority2)
746 if (left->CycleBound > right->CycleBound)
747 return true;
748
749 return false;
750}
751
752
753/// CalcNodePriority - Priority is the Sethi Ullman number.
754/// Smaller number is the higher priority.
755int RegReductionPriorityQueue::CalcNodePriority(const SUnit *SU) {
756 int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
757 if (SethiUllmanNumber != INT_MIN)
758 return SethiUllmanNumber;
759
760 if (SU->Preds.size() == 0) {
761 SethiUllmanNumber = 1;
762 } else {
763 int Extra = 0;
Jeff Cohen6ce97682006-03-10 03:57:45 +0000764 for (std::set<SUnit*>::const_iterator I = SU->Preds.begin(),
Chris Lattner9df64752006-03-09 06:35:14 +0000765 E = SU->Preds.end(); I != E; ++I) {
766 SUnit *PredSU = *I;
767 int PredSethiUllman = CalcNodePriority(PredSU);
768 if (PredSethiUllman > SethiUllmanNumber) {
769 SethiUllmanNumber = PredSethiUllman;
770 Extra = 0;
771 } else if (PredSethiUllman == SethiUllmanNumber)
772 Extra++;
773 }
774
775 if (SU->Node->getOpcode() != ISD::TokenFactor)
776 SethiUllmanNumber += Extra;
777 else
778 SethiUllmanNumber = (Extra == 1) ? 0 : Extra-1;
779 }
780
781 return SethiUllmanNumber;
782}
783
784/// CalculatePriorities - Calculate priorities of all scheduling units.
785void RegReductionPriorityQueue::CalculatePriorities() {
786 SethiUllmanNumbers.assign(SUnits->size(), INT_MIN);
787
788 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
789 CalcNodePriority(&(*SUnits)[i]);
790}
791
Chris Lattner6398c132006-03-09 07:38:27 +0000792//===----------------------------------------------------------------------===//
793// LatencyPriorityQueue Implementation
794//===----------------------------------------------------------------------===//
795//
796// This is a SchedulingPriorityQueue that schedules using latency information to
797// reduce the length of the critical path through the basic block.
798//
799namespace {
800 class LatencyPriorityQueue;
801
802 /// Sorting functions for the Available queue.
803 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
804 LatencyPriorityQueue *PQ;
805 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
806 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
807
808 bool operator()(const SUnit* left, const SUnit* right) const;
809 };
810} // end anonymous namespace
811
812namespace {
813 class LatencyPriorityQueue : public SchedulingPriorityQueue {
814 // SUnits - The SUnits for the current graph.
815 const std::vector<SUnit> *SUnits;
816
817 // Latencies - The latency (max of latency from this node to the bb exit)
818 // for each node.
819 std::vector<int> Latencies;
820
821 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
822public:
823 LatencyPriorityQueue() : Queue(latency_sort(this)) {
824 }
825
826 void initNodes(const std::vector<SUnit> &sunits) {
827 SUnits = &sunits;
828 // Calculate node priorities.
829 CalculatePriorities();
830 }
831 void releaseState() {
832 SUnits = 0;
833 Latencies.clear();
834 }
835
836 unsigned getLatency(unsigned NodeNum) const {
837 assert(NodeNum < Latencies.size());
838 return Latencies[NodeNum];
839 }
840
841 bool empty() const { return Queue.empty(); }
842
843 void push(SUnit *U) {
844 Queue.push(U);
845 }
846 SUnit *pop() {
847 SUnit *V = Queue.top();
848 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000849 return V;
850 }
851private:
852 void CalculatePriorities();
853 int CalcLatency(const SUnit &SU);
854 };
855}
856
857bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
858 unsigned LHSNum = LHS->NodeNum;
859 unsigned RHSNum = RHS->NodeNum;
860
861 return PQ->getLatency(LHSNum) < PQ->getLatency(RHSNum);
862}
863
864
865/// CalcNodePriority - Calculate the maximal path from the node to the exit.
866///
867int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
868 int &Latency = Latencies[SU.NodeNum];
869 if (Latency != -1)
870 return Latency;
871
872 int MaxSuccLatency = 0;
Jeff Cohen6ce97682006-03-10 03:57:45 +0000873 for (std::set<SUnit*>::const_iterator I = SU.Succs.begin(),
Chris Lattner6398c132006-03-09 07:38:27 +0000874 E = SU.Succs.end(); I != E; ++I)
875 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I));
876
Jeff Cohen6ce97682006-03-10 03:57:45 +0000877 for (std::set<SUnit*>::const_iterator I = SU.ChainSuccs.begin(),
Chris Lattner6398c132006-03-09 07:38:27 +0000878 E = SU.ChainSuccs.end(); I != E; ++I)
879 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I));
880
881 return Latency = MaxSuccLatency + SU.Latency;
882}
883
884/// CalculatePriorities - Calculate priorities of all scheduling units.
885void LatencyPriorityQueue::CalculatePriorities() {
886 Latencies.assign(SUnits->size(), -1);
887
888 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
889 CalcLatency((*SUnits)[i]);
890}
891
Chris Lattner9df64752006-03-09 06:35:14 +0000892
893//===----------------------------------------------------------------------===//
894// Public Constructor Functions
895//===----------------------------------------------------------------------===//
896
Evan Chengab495562006-01-25 09:14:32 +0000897llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
898 MachineBasicBlock *BB) {
Chris Lattner543832d2006-03-08 04:25:59 +0000899 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true,
Chris Lattner9df64752006-03-09 06:35:14 +0000900 new RegReductionPriorityQueue(),
Chris Lattner543832d2006-03-08 04:25:59 +0000901 new HazardRecognizer());
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000902}
903
Chris Lattner47639db2006-03-06 00:22:00 +0000904/// createTDListDAGScheduler - This creates a top-down list scheduler with the
905/// specified hazard recognizer.
906ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
907 MachineBasicBlock *BB,
Chris Lattner543832d2006-03-08 04:25:59 +0000908 HazardRecognizer *HR) {
Chris Lattner9df64752006-03-09 06:35:14 +0000909 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false,
Chris Lattner6398c132006-03-09 07:38:27 +0000910 new LatencyPriorityQueue(),
Chris Lattner9df64752006-03-09 06:35:14 +0000911 HR);
Evan Cheng31272342006-01-23 08:26:10 +0000912}