blob: 51831ff75a8e4322c6202a2f433f600a03a6c821 [file] [log] [blame]
Evan Chengd38c22b2006-05-11 23:55:42 +00001//===----- ScheduleDAGList.cpp - Reg pressure reduction list scheduler ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Evan Cheng and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "sched"
19#include "llvm/CodeGen/ScheduleDAG.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000021#include "llvm/CodeGen/SSARegMap.h"
22#include "llvm/Target/MRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000028#include "llvm/ADT/Statistic.h"
29#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000030#include <queue>
31#include "llvm/Support/CommandLine.h"
32using namespace llvm;
33
Jim Laskey95eda5b2006-08-01 14:21:23 +000034static RegisterScheduler
35 burrListDAGScheduler("list-burr",
36 " Bottom-up register reduction list scheduling",
37 createBURRListDAGScheduler);
38static RegisterScheduler
39 tdrListrDAGScheduler("list-tdrr",
40 " Top-down register reduction list scheduling",
41 createTDRRListDAGScheduler);
42
Evan Chengd38c22b2006-05-11 23:55:42 +000043namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000044//===----------------------------------------------------------------------===//
45/// ScheduleDAGRRList - The actual register reduction list scheduler
46/// implementation. This supports both top-down and bottom-up scheduling.
47///
48
Chris Lattnere097e6f2006-06-28 22:17:39 +000049class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG {
Evan Chengd38c22b2006-05-11 23:55:42 +000050private:
51 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
52 /// it is top-down.
53 bool isBottomUp;
54
55 /// AvailableQueue - The priority queue to use for the available SUnits.
56 ///
57 SchedulingPriorityQueue *AvailableQueue;
58
59public:
60 ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb,
61 const TargetMachine &tm, bool isbottomup,
62 SchedulingPriorityQueue *availqueue)
63 : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup),
64 AvailableQueue(availqueue) {
65 }
66
67 ~ScheduleDAGRRList() {
68 delete AvailableQueue;
69 }
70
71 void Schedule();
72
73private:
74 void ReleasePred(SUnit *PredSU, bool isChain, unsigned CurCycle);
75 void ReleaseSucc(SUnit *SuccSU, bool isChain, unsigned CurCycle);
Evan Chengd12c97d2006-05-30 18:05:39 +000076 void ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle);
77 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +000078 void ListScheduleTopDown();
79 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +000080 void CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +000081};
82} // end anonymous namespace
83
84
85/// Schedule - Schedule the DAG using list scheduling.
86void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +000087 DOUT << "********** List Scheduling **********\n";
Evan Chengd38c22b2006-05-11 23:55:42 +000088
89 // Build scheduling units.
90 BuildSchedUnits();
91
Evan Chengd38c22b2006-05-11 23:55:42 +000092 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Chris Lattnerd86418a2006-08-17 00:09:56 +000093 SUnits[su].dumpAll(&DAG));
Evan Cheng47fbeda2006-10-14 08:34:06 +000094 CalculateDepths();
95 CalculateHeights();
Evan Chengd38c22b2006-05-11 23:55:42 +000096
Evan Chengfd2c5dd2006-11-04 09:44:31 +000097 AvailableQueue->initNodes(SUnitMap, SUnits);
Evan Chengd38c22b2006-05-11 23:55:42 +000098
99 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
100 if (isBottomUp)
101 ListScheduleBottomUp();
102 else
103 ListScheduleTopDown();
104
105 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000106
Evan Cheng009f5f52006-05-25 08:37:31 +0000107 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000108
Bill Wendling22e978a2006-12-07 20:04:42 +0000109 DOUT << "*** Final schedule ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000110 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000111 DOUT << "\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000112
113 // Emit in scheduled order
114 EmitSchedule();
115}
116
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000117/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000118/// it is not the last use of its first operand, add it to the CommuteSet if
119/// possible. It will be commuted when it is translated to a MI.
120void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000121 SmallPtrSet<SUnit*, 4> OperandSeen;
Evan Chengafed73e2006-05-12 01:58:24 +0000122 for (unsigned i = Sequence.size()-1; i != 0; --i) { // Ignore first node.
123 SUnit *SU = Sequence[i];
124 if (!SU) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000125 if (SU->isCommutable) {
126 unsigned Opc = SU->Node->getTargetOpcode();
127 unsigned NumRes = CountResults(SU->Node);
128 unsigned NumOps = CountOperands(SU->Node);
129 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng67fc1412006-12-01 21:52:58 +0000130 if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000131 continue;
132
133 SDNode *OpN = SU->Node->getOperand(j).Val;
134 SUnit *OpSU = SUnitMap[OpN];
135 if (OpSU && OperandSeen.count(OpSU) == 1) {
136 // Ok, so SU is not the last use of OpSU, but SU is two-address so
137 // it will clobber OpSU. Try to commute SU if no other source operands
138 // are live below.
139 bool DoCommute = true;
140 for (unsigned k = 0; k < NumOps; ++k) {
141 if (k != j) {
142 OpN = SU->Node->getOperand(k).Val;
143 OpSU = SUnitMap[OpN];
144 if (OpSU && OperandSeen.count(OpSU) == 1) {
145 DoCommute = false;
146 break;
147 }
148 }
Evan Chengafed73e2006-05-12 01:58:24 +0000149 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000150 if (DoCommute)
151 CommuteSet.insert(SU->Node);
Evan Chengafed73e2006-05-12 01:58:24 +0000152 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000153
154 // Only look at the first use&def node for now.
155 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000156 }
157 }
158
Chris Lattnerd86418a2006-08-17 00:09:56 +0000159 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
160 I != E; ++I) {
Evan Chengafed73e2006-05-12 01:58:24 +0000161 if (!I->second)
162 OperandSeen.insert(I->first);
163 }
164 }
165}
Evan Chengd38c22b2006-05-11 23:55:42 +0000166
167//===----------------------------------------------------------------------===//
168// Bottom-Up Scheduling
169//===----------------------------------------------------------------------===//
170
Evan Chengd38c22b2006-05-11 23:55:42 +0000171/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
172/// the Available queue is the count reaches zero. Also update its cycle bound.
173void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain,
174 unsigned CurCycle) {
175 // FIXME: the distance between two nodes is not always == the predecessor's
176 // latency. For example, the reader can very well read the register written
177 // by the predecessor later than the issue cycle. It also depends on the
178 // interrupt model (drain vs. freeze).
179 PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
180
181 if (!isChain)
182 PredSU->NumSuccsLeft--;
183 else
184 PredSU->NumChainSuccsLeft--;
185
186#ifndef NDEBUG
187 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000188 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000189 PredSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000190 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000191 assert(0);
192 }
193#endif
194
195 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
196 // EntryToken has to go last! Special case it here.
197 if (PredSU->Node->getOpcode() != ISD::EntryToken) {
198 PredSU->isAvailable = true;
199 AvailableQueue->push(PredSU);
200 }
201 }
202}
203
204/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
205/// count of its predecessors. If a predecessor pending count is zero, add it to
206/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000207void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000208 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000209 DEBUG(SU->dump(&DAG));
210 SU->Cycle = CurCycle;
211
212 AvailableQueue->ScheduledNode(SU);
213 Sequence.push_back(SU);
214
215 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000216 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
217 I != E; ++I)
Evan Chengd38c22b2006-05-11 23:55:42 +0000218 ReleasePred(I->first, I->second, CurCycle);
219 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000220}
221
222/// isReady - True if node's lower cycle bound is less or equal to the current
223/// scheduling cycle. Always true if all nodes have uniform latency 1.
224static inline bool isReady(SUnit *SU, unsigned CurCycle) {
225 return SU->CycleBound <= CurCycle;
226}
227
228/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
229/// schedulers.
230void ScheduleDAGRRList::ListScheduleBottomUp() {
231 unsigned CurCycle = 0;
232 // Add root to Available queue.
233 AvailableQueue->push(SUnitMap[DAG.getRoot().Val]);
234
235 // While Available queue is not empty, grab the node with the highest
236 // priority. If it is not ready put it back. Schedule the node.
237 std::vector<SUnit*> NotReady;
Evan Chengd38c22b2006-05-11 23:55:42 +0000238 while (!AvailableQueue->empty()) {
239 SUnit *CurNode = AvailableQueue->pop();
Evan Chengd12c97d2006-05-30 18:05:39 +0000240 while (CurNode && !isReady(CurNode, CurCycle)) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000241 NotReady.push_back(CurNode);
242 CurNode = AvailableQueue->pop();
243 }
244
245 // Add the nodes that aren't ready back onto the available list.
246 AvailableQueue->push_all(NotReady);
247 NotReady.clear();
248
Evan Chengd12c97d2006-05-30 18:05:39 +0000249 if (CurNode != NULL)
250 ScheduleNodeBottomUp(CurNode, CurCycle);
251 CurCycle++;
Evan Chengd38c22b2006-05-11 23:55:42 +0000252 }
253
254 // Add entry node last
255 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
256 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
257 Sequence.push_back(Entry);
258 }
259
260 // Reverse the order if it is bottom up.
261 std::reverse(Sequence.begin(), Sequence.end());
262
263
264#ifndef NDEBUG
265 // Verify that all SUnits were scheduled.
266 bool AnyNotSched = false;
267 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
268 if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
269 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000270 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000271 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000272 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000273 AnyNotSched = true;
274 }
275 }
276 assert(!AnyNotSched);
277#endif
278}
279
280//===----------------------------------------------------------------------===//
281// Top-Down Scheduling
282//===----------------------------------------------------------------------===//
283
284/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
285/// the PendingQueue if the count reaches zero.
286void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain,
287 unsigned CurCycle) {
288 // FIXME: the distance between two nodes is not always == the predecessor's
289 // latency. For example, the reader can very well read the register written
290 // by the predecessor later than the issue cycle. It also depends on the
291 // interrupt model (drain vs. freeze).
292 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
293
294 if (!isChain)
295 SuccSU->NumPredsLeft--;
296 else
297 SuccSU->NumChainPredsLeft--;
298
299#ifndef NDEBUG
300 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000301 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000302 SuccSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000303 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000304 assert(0);
305 }
306#endif
307
308 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) {
309 SuccSU->isAvailable = true;
310 AvailableQueue->push(SuccSU);
311 }
312}
313
314
315/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
316/// count of its successors. If a successor pending count is zero, add it to
317/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000318void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000319 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000320 DEBUG(SU->dump(&DAG));
321 SU->Cycle = CurCycle;
322
323 AvailableQueue->ScheduledNode(SU);
324 Sequence.push_back(SU);
325
326 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000327 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
328 I != E; ++I)
Evan Chengd38c22b2006-05-11 23:55:42 +0000329 ReleaseSucc(I->first, I->second, CurCycle);
330 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000331}
332
333void ScheduleDAGRRList::ListScheduleTopDown() {
334 unsigned CurCycle = 0;
335 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
336
337 // All leaves to Available queue.
338 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
339 // It is available if it has no predecessors.
340 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
341 AvailableQueue->push(&SUnits[i]);
342 SUnits[i].isAvailable = true;
343 }
344 }
345
346 // Emit the entry node first.
347 ScheduleNodeTopDown(Entry, CurCycle);
Evan Chengd12c97d2006-05-30 18:05:39 +0000348 CurCycle++;
Evan Chengd38c22b2006-05-11 23:55:42 +0000349
350 // While Available queue is not empty, grab the node with the highest
351 // priority. If it is not ready put it back. Schedule the node.
352 std::vector<SUnit*> NotReady;
Evan Chengd38c22b2006-05-11 23:55:42 +0000353 while (!AvailableQueue->empty()) {
354 SUnit *CurNode = AvailableQueue->pop();
Evan Chengd12c97d2006-05-30 18:05:39 +0000355 while (CurNode && !isReady(CurNode, CurCycle)) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000356 NotReady.push_back(CurNode);
357 CurNode = AvailableQueue->pop();
358 }
359
360 // Add the nodes that aren't ready back onto the available list.
361 AvailableQueue->push_all(NotReady);
362 NotReady.clear();
363
Evan Chengd12c97d2006-05-30 18:05:39 +0000364 if (CurNode != NULL)
365 ScheduleNodeTopDown(CurNode, CurCycle);
366 CurCycle++;
Evan Chengd38c22b2006-05-11 23:55:42 +0000367 }
368
369
370#ifndef NDEBUG
371 // Verify that all SUnits were scheduled.
372 bool AnyNotSched = false;
373 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
374 if (!SUnits[i].isScheduled) {
375 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000376 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000377 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000378 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000379 AnyNotSched = true;
380 }
381 }
382 assert(!AnyNotSched);
383#endif
384}
385
386
387
388//===----------------------------------------------------------------------===//
389// RegReductionPriorityQueue Implementation
390//===----------------------------------------------------------------------===//
391//
392// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
393// to reduce register pressure.
394//
395namespace {
396 template<class SF>
397 class RegReductionPriorityQueue;
398
399 /// Sorting functions for the Available queue.
400 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
401 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
402 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
403 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
404
405 bool operator()(const SUnit* left, const SUnit* right) const;
406 };
407
408 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
409 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
410 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
411 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
412
413 bool operator()(const SUnit* left, const SUnit* right) const;
414 };
415} // end anonymous namespace
416
Evan Cheng961bbd32007-01-08 23:50:38 +0000417static inline bool isCopyFromLiveIn(const SUnit *SU) {
418 SDNode *N = SU->Node;
419 return N->getOpcode() == ISD::CopyFromReg &&
420 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
421}
422
Evan Chengd38c22b2006-05-11 23:55:42 +0000423namespace {
424 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000425 class VISIBILITY_HIDDEN RegReductionPriorityQueue
426 : public SchedulingPriorityQueue {
Evan Chengd38c22b2006-05-11 23:55:42 +0000427 std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue;
428
429 public:
430 RegReductionPriorityQueue() :
431 Queue(SF(this)) {}
432
Chris Lattner0a30b1f2007-02-03 01:34:13 +0000433 virtual void initNodes(DenseMap<SDNode*, SUnit*> &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000434 std::vector<SUnit> &sunits) {}
Evan Chengd38c22b2006-05-11 23:55:42 +0000435 virtual void releaseState() {}
436
Evan Cheng6730f032007-01-08 23:55:53 +0000437 virtual unsigned getNodePriority(const SUnit *SU) const {
Evan Chengd38c22b2006-05-11 23:55:42 +0000438 return 0;
439 }
440
441 bool empty() const { return Queue.empty(); }
442
443 void push(SUnit *U) {
444 Queue.push(U);
445 }
446 void push_all(const std::vector<SUnit *> &Nodes) {
447 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
448 Queue.push(Nodes[i]);
449 }
450
451 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +0000452 if (empty()) return NULL;
Evan Chengd38c22b2006-05-11 23:55:42 +0000453 SUnit *V = Queue.top();
454 Queue.pop();
455 return V;
456 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000457
458 virtual bool isDUOperand(const SUnit *SU1, const SUnit *SU2) {
459 return false;
460 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000461 };
462
463 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000464 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
465 : public RegReductionPriorityQueue<SF> {
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000466 // SUnitMap SDNode to SUnit mapping (n -> 1).
Chris Lattner0a30b1f2007-02-03 01:34:13 +0000467 DenseMap<SDNode*, SUnit*> *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000468
Evan Chengd38c22b2006-05-11 23:55:42 +0000469 // SUnits - The SUnits for the current graph.
470 const std::vector<SUnit> *SUnits;
471
472 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +0000473 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +0000474
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000475 const TargetInstrInfo *TII;
Evan Chengd38c22b2006-05-11 23:55:42 +0000476 public:
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000477 BURegReductionPriorityQueue(const TargetInstrInfo *tii)
478 : TII(tii) {}
Evan Chengd38c22b2006-05-11 23:55:42 +0000479
Chris Lattner0a30b1f2007-02-03 01:34:13 +0000480 void initNodes(DenseMap<SDNode*, SUnit*> &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000481 std::vector<SUnit> &sunits) {
482 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +0000483 SUnits = &sunits;
484 // Add pseudo dependency edges for two-address nodes.
Evan Chengafed73e2006-05-12 01:58:24 +0000485 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +0000486 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +0000487 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +0000488 }
489
490 void releaseState() {
491 SUnits = 0;
492 SethiUllmanNumbers.clear();
493 }
494
Evan Cheng6730f032007-01-08 23:55:53 +0000495 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +0000496 assert(SU->NodeNum < SethiUllmanNumbers.size());
497 unsigned Opc = SU->Node->getOpcode();
498 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
499 // CopyFromReg should be close to its def because it restricts
500 // allocation choices. But if it is a livein then perhaps we want it
501 // closer to its uses so it can be coalesced.
502 return 0xffff;
503 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
504 // CopyToReg should be close to its uses to facilitate coalescing and
505 // avoid spilling.
506 return 0;
507 else if (SU->NumSuccs == 0)
508 // If SU does not have a use, i.e. it doesn't produce a value that would
509 // be consumed (e.g. store), then it terminates a chain of computation.
510 // Give it a large SethiUllman number so it will be scheduled right
511 // before its predecessors that it doesn't lengthen their live ranges.
512 return 0xffff;
513 else if (SU->NumPreds == 0)
514 // If SU does not have a def, schedule it close to its uses because it
515 // does not lengthen any live ranges.
516 return 0;
517 else
518 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +0000519 }
520
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000521 bool isDUOperand(const SUnit *SU1, const SUnit *SU2) {
522 unsigned Opc = SU1->Node->getTargetOpcode();
523 unsigned NumRes = ScheduleDAG::CountResults(SU1->Node);
524 unsigned NumOps = ScheduleDAG::CountOperands(SU1->Node);
525 for (unsigned i = 0; i != NumOps; ++i) {
Evan Cheng67fc1412006-12-01 21:52:58 +0000526 if (TII->getOperandConstraint(Opc, i+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000527 continue;
528 if (SU1->Node->getOperand(i).isOperand(SU2->Node))
529 return true;
530 }
531 return false;
532 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000533 private:
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000534 bool canClobber(SUnit *SU, SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +0000535 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +0000536 void CalculateSethiUllmanNumbers();
537 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000538 };
539
540
541 template<class SF>
542 class TDRegReductionPriorityQueue : public RegReductionPriorityQueue<SF> {
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000543 // SUnitMap SDNode to SUnit mapping (n -> 1).
Chris Lattner0a30b1f2007-02-03 01:34:13 +0000544 DenseMap<SDNode*, SUnit*> *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000545
Evan Chengd38c22b2006-05-11 23:55:42 +0000546 // SUnits - The SUnits for the current graph.
547 const std::vector<SUnit> *SUnits;
548
549 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +0000550 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +0000551
552 public:
553 TDRegReductionPriorityQueue() {}
554
Chris Lattner0a30b1f2007-02-03 01:34:13 +0000555 void initNodes(DenseMap<SDNode*, SUnit*> &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000556 std::vector<SUnit> &sunits) {
557 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +0000558 SUnits = &sunits;
559 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +0000560 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +0000561 }
562
563 void releaseState() {
564 SUnits = 0;
565 SethiUllmanNumbers.clear();
566 }
567
Evan Cheng6730f032007-01-08 23:55:53 +0000568 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +0000569 assert(SU->NodeNum < SethiUllmanNumbers.size());
570 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +0000571 }
572
573 private:
Evan Cheng6730f032007-01-08 23:55:53 +0000574 void CalculateSethiUllmanNumbers();
575 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000576 };
577}
578
Evan Chengb9e3db62007-03-14 22:43:40 +0000579/// closestSucc - Returns the scheduled cycle of the successor which is
580/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +0000581static unsigned closestSucc(const SUnit *SU) {
582 unsigned MaxCycle = 0;
583 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +0000584 I != E; ++I) {
585 unsigned Cycle = I->first->Cycle;
586 // If there are bunch of CopyToRegs stacked up, they should be considered
587 // to be at the same position.
588 if (I->first->Node->getOpcode() == ISD::CopyToReg)
589 Cycle = closestSucc(I->first)+1;
590 if (Cycle > MaxCycle)
591 MaxCycle = Cycle;
592 }
Evan Cheng28748552007-03-13 23:25:11 +0000593 return MaxCycle;
594}
595
Evan Chengb9e3db62007-03-14 22:43:40 +0000596/// calcMaxScratches - Returns an cost estimate of the worse case requirement
597/// for scratch registers. Live-in operands and live-out results don't count
598/// since they are "fixed".
599static unsigned calcMaxScratches(const SUnit *SU) {
600 unsigned Scratches = 0;
601 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
602 I != E; ++I) {
603 if (I->second) continue; // ignore chain preds
604 if (I->first->Node->getOpcode() != ISD::CopyFromReg)
605 Scratches++;
606 }
607 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
608 I != E; ++I) {
609 if (I->second) continue; // ignore chain succs
610 if (I->first->Node->getOpcode() != ISD::CopyToReg)
611 Scratches += 10;
612 }
613 return Scratches;
614}
615
Evan Chengd38c22b2006-05-11 23:55:42 +0000616// Bottom up
617bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
David Greene4c1e6f32007-06-29 03:42:23 +0000618 // There used to be a special tie breaker here that looked for
David Greene5b6f7552007-06-29 02:48:09 +0000619 // two-address instructions and preferred the instruction with a
620 // def&use operand. The special case triggered diagnostics when
621 // _GLIBCXX_DEBUG was enabled because it broke the strict weak
622 // ordering that priority_queue requires. It didn't help much anyway
623 // because AddPseudoTwoAddrDeps already covers many of the cases
624 // where it would have applied. In addition, it's counter-intuitive
625 // that a tie breaker would be the first thing attempted. There's a
626 // "real" tie breaker below that is the operation of last resort.
627 // The fact that the "special tie breaker" would trigger when there
628 // wasn't otherwise a tie is what broke the strict weak ordering
629 // constraint.
Evan Cheng99f2f792006-05-13 08:22:24 +0000630
Evan Cheng6730f032007-01-08 23:55:53 +0000631 unsigned LPriority = SPQ->getNodePriority(left);
632 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng961bbd32007-01-08 23:50:38 +0000633 if (LPriority > RPriority)
Evan Chengd38c22b2006-05-11 23:55:42 +0000634 return true;
Evan Cheng28748552007-03-13 23:25:11 +0000635 else if (LPriority == RPriority) {
Dan Gohmane131e3a2007-04-26 19:40:56 +0000636 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
Evan Cheng28748552007-03-13 23:25:11 +0000637 // e.g.
638 // t1 = op t2, c1
639 // t3 = op t4, c2
640 //
641 // and the following instructions are both ready.
642 // t2 = op c3
643 // t4 = op c4
644 //
645 // Then schedule t2 = op first.
646 // i.e.
647 // t4 = op c4
648 // t2 = op c3
649 // t1 = op t2, c1
650 // t3 = op t4, c2
651 //
652 // This creates more short live intervals.
653 unsigned LDist = closestSucc(left);
654 unsigned RDist = closestSucc(right);
655 if (LDist < RDist)
Evan Chengd38c22b2006-05-11 23:55:42 +0000656 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +0000657 else if (LDist == RDist) {
658 // Intuitively, it's good to push down instructions whose results are
659 // liveout so their long live ranges won't conflict with other values
660 // which are needed inside the BB. Further prioritize liveout instructions
661 // by the number of operands which are calculated within the BB.
662 unsigned LScratch = calcMaxScratches(left);
663 unsigned RScratch = calcMaxScratches(right);
664 if (LScratch > RScratch)
Evan Chengd38c22b2006-05-11 23:55:42 +0000665 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +0000666 else if (LScratch == RScratch)
667 if (left->Height > right->Height)
Evan Cheng99f2f792006-05-13 08:22:24 +0000668 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +0000669 else if (left->Height == right->Height)
670 if (left->Depth < right->Depth)
Evan Cheng28748552007-03-13 23:25:11 +0000671 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +0000672 else if (left->Depth == right->Depth)
673 if (left->CycleBound > right->CycleBound)
674 return true;
675 }
Evan Cheng28748552007-03-13 23:25:11 +0000676 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000677 return false;
678}
679
Evan Chengd38c22b2006-05-11 23:55:42 +0000680// FIXME: This is probably too slow!
681static void isReachable(SUnit *SU, SUnit *TargetSU,
Evan Chenge3c44192007-06-22 01:35:51 +0000682 SmallPtrSet<SUnit*, 32> &Visited, bool &Reached) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000683 if (Reached) return;
684 if (SU == TargetSU) {
685 Reached = true;
686 return;
687 }
Evan Chenge3c44192007-06-22 01:35:51 +0000688 if (!Visited.insert(SU)) return;
Evan Chengd38c22b2006-05-11 23:55:42 +0000689
Chris Lattnerd86418a2006-08-17 00:09:56 +0000690 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); I != E;
691 ++I)
Evan Chengd38c22b2006-05-11 23:55:42 +0000692 isReachable(I->first, TargetSU, Visited, Reached);
693}
694
695static bool isReachable(SUnit *SU, SUnit *TargetSU) {
Evan Chenge3c44192007-06-22 01:35:51 +0000696 SmallPtrSet<SUnit*, 32> Visited;
Evan Chengd38c22b2006-05-11 23:55:42 +0000697 bool Reached = false;
698 isReachable(SU, TargetSU, Visited, Reached);
699 return Reached;
700}
701
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000702template<class SF>
703bool BURegReductionPriorityQueue<SF>::canClobber(SUnit *SU, SUnit *Op) {
704 if (SU->isTwoAddress) {
705 unsigned Opc = SU->Node->getTargetOpcode();
706 unsigned NumRes = ScheduleDAG::CountResults(SU->Node);
707 unsigned NumOps = ScheduleDAG::CountOperands(SU->Node);
708 for (unsigned i = 0; i != NumOps; ++i) {
Evan Cheng67fc1412006-12-01 21:52:58 +0000709 if (TII->getOperandConstraint(Opc, i+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000710 SDNode *DU = SU->Node->getOperand(i).Val;
711 if (Op == (*SUnitMap)[DU])
712 return true;
713 }
714 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000715 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000716 return false;
717}
718
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000719
Evan Chengd38c22b2006-05-11 23:55:42 +0000720/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
721/// it as a def&use operand. Add a pseudo control edge from it to the other
722/// node (if it won't create a cycle) so the two-address one will be scheduled
723/// first (lower in the schedule).
724template<class SF>
725void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000726 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
727 SUnit *SU = (SUnit *)&((*SUnits)[i]);
728 if (!SU->isTwoAddress)
729 continue;
730
731 SDNode *Node = SU->Node;
732 if (!Node->isTargetOpcode())
733 continue;
734
735 unsigned Opc = Node->getTargetOpcode();
736 unsigned NumRes = ScheduleDAG::CountResults(Node);
737 unsigned NumOps = ScheduleDAG::CountOperands(Node);
738 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng67fc1412006-12-01 21:52:58 +0000739 if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000740 SDNode *DU = SU->Node->getOperand(j).Val;
741 SUnit *DUSU = (*SUnitMap)[DU];
Evan Chengf24d15f2006-11-06 21:33:46 +0000742 if (!DUSU) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000743 for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end();
744 I != E; ++I) {
745 if (I->second) continue;
746 SUnit *SuccSU = I->first;
747 if (SuccSU != SU &&
748 (!canClobber(SuccSU, DUSU) ||
749 (!SU->isCommutable && SuccSU->isCommutable))){
750 if (SuccSU->Depth == SU->Depth && !isReachable(SuccSU, SU)) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000751 DOUT << "Adding an edge from SU # " << SU->NodeNum
752 << " to SU #" << SuccSU->NodeNum << "\n";
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000753 if (SU->addPred(SuccSU, true))
754 SU->NumChainPredsLeft++;
755 if (SuccSU->addSucc(SU, true))
756 SuccSU->NumChainSuccsLeft++;
757 }
758 }
759 }
760 }
761 }
762 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000763}
764
Evan Cheng6730f032007-01-08 23:55:53 +0000765/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +0000766/// Smaller number is the higher priority.
767template<class SF>
Chris Lattner296a83c2007-02-01 04:55:59 +0000768unsigned BURegReductionPriorityQueue<SF>::
769CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +0000770 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +0000771 if (SethiUllmanNumber != 0)
772 return SethiUllmanNumber;
773
Evan Cheng961bbd32007-01-08 23:50:38 +0000774 unsigned Extra = 0;
775 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
776 I != E; ++I) {
777 if (I->second) continue; // ignore chain preds
778 SUnit *PredSU = I->first;
Evan Cheng6730f032007-01-08 23:55:53 +0000779 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Cheng961bbd32007-01-08 23:50:38 +0000780 if (PredSethiUllman > SethiUllmanNumber) {
781 SethiUllmanNumber = PredSethiUllman;
782 Extra = 0;
783 } else if (PredSethiUllman == SethiUllmanNumber && !I->second)
784 Extra++;
Evan Chengd38c22b2006-05-11 23:55:42 +0000785 }
Evan Cheng961bbd32007-01-08 23:50:38 +0000786
787 SethiUllmanNumber += Extra;
788
789 if (SethiUllmanNumber == 0)
790 SethiUllmanNumber = 1;
Evan Chengd38c22b2006-05-11 23:55:42 +0000791
792 return SethiUllmanNumber;
793}
794
Evan Cheng6730f032007-01-08 23:55:53 +0000795/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
796/// scheduling units.
Evan Chengd38c22b2006-05-11 23:55:42 +0000797template<class SF>
Evan Cheng6730f032007-01-08 23:55:53 +0000798void BURegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +0000799 SethiUllmanNumbers.assign(SUnits->size(), 0);
800
801 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +0000802 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +0000803}
804
805static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) {
806 unsigned Sum = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000807 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
808 I != E; ++I) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000809 SUnit *SuccSU = I->first;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000810 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
811 EE = SuccSU->Preds.end(); II != EE; ++II) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000812 SUnit *PredSU = II->first;
813 if (!PredSU->isScheduled)
814 Sum++;
815 }
816 }
817
818 return Sum;
819}
820
821
822// Top down
823bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +0000824 unsigned LPriority = SPQ->getNodePriority(left);
825 unsigned RPriority = SPQ->getNodePriority(right);
Evan Chengd38c22b2006-05-11 23:55:42 +0000826 bool LIsTarget = left->Node->isTargetOpcode();
827 bool RIsTarget = right->Node->isTargetOpcode();
828 bool LIsFloater = LIsTarget && left->NumPreds == 0;
829 bool RIsFloater = RIsTarget && right->NumPreds == 0;
830 unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0;
831 unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0;
832
833 if (left->NumSuccs == 0 && right->NumSuccs != 0)
834 return false;
835 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
836 return true;
837
838 // Special tie breaker: if two nodes share a operand, the one that use it
839 // as a def&use operand is preferred.
840 if (LIsTarget && RIsTarget) {
841 if (left->isTwoAddress && !right->isTwoAddress) {
842 SDNode *DUNode = left->Node->getOperand(0).Val;
843 if (DUNode->isOperand(right->Node))
844 RBonus += 2;
845 }
846 if (!left->isTwoAddress && right->isTwoAddress) {
847 SDNode *DUNode = right->Node->getOperand(0).Val;
848 if (DUNode->isOperand(left->Node))
849 LBonus += 2;
850 }
851 }
852 if (LIsFloater)
853 LBonus -= 2;
854 if (RIsFloater)
855 RBonus -= 2;
856 if (left->NumSuccs == 1)
857 LBonus += 2;
858 if (right->NumSuccs == 1)
859 RBonus += 2;
860
861 if (LPriority+LBonus < RPriority+RBonus)
862 return true;
863 else if (LPriority == RPriority)
864 if (left->Depth < right->Depth)
865 return true;
866 else if (left->Depth == right->Depth)
867 if (left->NumSuccsLeft > right->NumSuccsLeft)
868 return true;
869 else if (left->NumSuccsLeft == right->NumSuccsLeft)
870 if (left->CycleBound > right->CycleBound)
871 return true;
872 return false;
873}
874
Evan Cheng6730f032007-01-08 23:55:53 +0000875/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +0000876/// Smaller number is the higher priority.
877template<class SF>
Chris Lattner296a83c2007-02-01 04:55:59 +0000878unsigned TDRegReductionPriorityQueue<SF>::
879CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +0000880 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +0000881 if (SethiUllmanNumber != 0)
882 return SethiUllmanNumber;
883
884 unsigned Opc = SU->Node->getOpcode();
885 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Evan Cheng961bbd32007-01-08 23:50:38 +0000886 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +0000887 else if (SU->NumSuccsLeft == 0)
888 // If SU does not have a use, i.e. it doesn't produce a value that would
889 // be consumed (e.g. store), then it terminates a chain of computation.
Chris Lattner296a83c2007-02-01 04:55:59 +0000890 // Give it a small SethiUllman number so it will be scheduled right before
891 // its predecessors that it doesn't lengthen their live ranges.
Evan Cheng961bbd32007-01-08 23:50:38 +0000892 SethiUllmanNumber = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000893 else if (SU->NumPredsLeft == 0 &&
894 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
Evan Cheng961bbd32007-01-08 23:50:38 +0000895 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +0000896 else {
897 int Extra = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000898 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
899 I != E; ++I) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000900 if (I->second) continue; // ignore chain preds
901 SUnit *PredSU = I->first;
Evan Cheng6730f032007-01-08 23:55:53 +0000902 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000903 if (PredSethiUllman > SethiUllmanNumber) {
904 SethiUllmanNumber = PredSethiUllman;
905 Extra = 0;
906 } else if (PredSethiUllman == SethiUllmanNumber && !I->second)
907 Extra++;
908 }
909
910 SethiUllmanNumber += Extra;
911 }
912
913 return SethiUllmanNumber;
914}
915
Evan Cheng6730f032007-01-08 23:55:53 +0000916/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
917/// scheduling units.
Evan Chengd38c22b2006-05-11 23:55:42 +0000918template<class SF>
Evan Cheng6730f032007-01-08 23:55:53 +0000919void TDRegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +0000920 SethiUllmanNumbers.assign(SUnits->size(), 0);
921
922 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +0000923 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +0000924}
925
926//===----------------------------------------------------------------------===//
927// Public Constructor Functions
928//===----------------------------------------------------------------------===//
929
Jim Laskey03593f72006-08-01 18:29:48 +0000930llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
931 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +0000932 MachineBasicBlock *BB) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000933 const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo();
Jim Laskey95eda5b2006-08-01 14:21:23 +0000934 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true,
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000935 new BURegReductionPriorityQueue<bu_ls_rr_sort>(TII));
Evan Chengd38c22b2006-05-11 23:55:42 +0000936}
937
Jim Laskey03593f72006-08-01 18:29:48 +0000938llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
939 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +0000940 MachineBasicBlock *BB) {
Jim Laskey95eda5b2006-08-01 14:21:23 +0000941 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false,
Chris Lattner296a83c2007-02-01 04:55:59 +0000942 new TDRegReductionPriorityQueue<td_ls_rr_sort>());
Evan Chengd38c22b2006-05-11 23:55:42 +0000943}
944