blob: 2cda59758ce6e79e98a36bd79edc5703c63fbbf3 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
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
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Evan Chengd38c22b2006-05-11 23:55:42 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Evan Chenge6f92252007-09-27 18:46:06 +000027#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000029#include "llvm/ADT/Statistic.h"
30#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000031#include <queue>
32#include "llvm/Support/CommandLine.h"
33using namespace llvm;
34
Evan Cheng1ec79b42007-09-27 07:09:03 +000035STATISTIC(NumBacktracks, "Number of times scheduler backtraced");
Evan Cheng79e97132007-10-05 01:39:18 +000036STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000037STATISTIC(NumDups, "Number of duplicated nodes");
38STATISTIC(NumCCCopies, "Number of cross class copies");
39
Jim Laskey95eda5b2006-08-01 14:21:23 +000040static RegisterScheduler
41 burrListDAGScheduler("list-burr",
42 " Bottom-up register reduction list scheduling",
43 createBURRListDAGScheduler);
44static RegisterScheduler
45 tdrListrDAGScheduler("list-tdrr",
46 " Top-down register reduction list scheduling",
47 createTDRRListDAGScheduler);
48
Evan Chengd38c22b2006-05-11 23:55:42 +000049namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000050//===----------------------------------------------------------------------===//
51/// ScheduleDAGRRList - The actual register reduction list scheduler
52/// implementation. This supports both top-down and bottom-up scheduling.
53///
Chris Lattnere097e6f2006-06-28 22:17:39 +000054class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG {
Evan Chengd38c22b2006-05-11 23:55:42 +000055private:
56 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
57 /// it is top-down.
58 bool isBottomUp;
59
60 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000061 SchedulingPriorityQueue *AvailableQueue;
62
Evan Cheng5924bf72007-09-25 01:54:36 +000063 /// LiveRegs / LiveRegDefs - A set of physical registers and their definition
64 /// that are "live". These nodes must be scheduled before any other nodes that
65 /// modifies the registers can be scheduled.
66 SmallSet<unsigned, 4> LiveRegs;
67 std::vector<SUnit*> LiveRegDefs;
68 std::vector<unsigned> LiveRegCycles;
69
Evan Chengd38c22b2006-05-11 23:55:42 +000070public:
71 ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb,
72 const TargetMachine &tm, bool isbottomup,
73 SchedulingPriorityQueue *availqueue)
74 : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup),
75 AvailableQueue(availqueue) {
76 }
77
78 ~ScheduleDAGRRList() {
79 delete AvailableQueue;
80 }
81
82 void Schedule();
83
84private:
Evan Cheng8e136a92007-09-26 21:36:17 +000085 void ReleasePred(SUnit*, bool, unsigned);
86 void ReleaseSucc(SUnit*, bool isChain, unsigned);
87 void CapturePred(SUnit*, SUnit*, bool);
88 void ScheduleNodeBottomUp(SUnit*, unsigned);
89 void ScheduleNodeTopDown(SUnit*, unsigned);
90 void UnscheduleNodeBottomUp(SUnit*);
91 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
92 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +000093 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +000094 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +000095 const TargetRegisterClass*,
96 SmallVector<SUnit*, 2>&);
97 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +000098 void ListScheduleTopDown();
99 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000100 void CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000101};
102} // end anonymous namespace
103
104
105/// Schedule - Schedule the DAG using list scheduling.
106void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000107 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000108
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000109 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
110 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000111
Evan Chengd38c22b2006-05-11 23:55:42 +0000112 // Build scheduling units.
113 BuildSchedUnits();
114
Evan Chengd38c22b2006-05-11 23:55:42 +0000115 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000116 SUnits[su].dumpAll(&DAG));
Evan Cheng47fbeda2006-10-14 08:34:06 +0000117 CalculateDepths();
118 CalculateHeights();
Evan Chengd38c22b2006-05-11 23:55:42 +0000119
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000120 AvailableQueue->initNodes(SUnitMap, SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000121
Evan Chengd38c22b2006-05-11 23:55:42 +0000122 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
123 if (isBottomUp)
124 ListScheduleBottomUp();
125 else
126 ListScheduleTopDown();
127
128 AvailableQueue->releaseState();
Dan Gohman54a187e2007-08-20 19:28:38 +0000129
Evan Cheng009f5f52006-05-25 08:37:31 +0000130 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000131
Bill Wendling22e978a2006-12-07 20:04:42 +0000132 DOUT << "*** Final schedule ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000133 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000134 DOUT << "\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000135
136 // Emit in scheduled order
137 EmitSchedule();
138}
139
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000140/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000141/// it is not the last use of its first operand, add it to the CommuteSet if
142/// possible. It will be commuted when it is translated to a MI.
143void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000144 SmallPtrSet<SUnit*, 4> OperandSeen;
Evan Chengafed73e2006-05-12 01:58:24 +0000145 for (unsigned i = Sequence.size()-1; i != 0; --i) { // Ignore first node.
146 SUnit *SU = Sequence[i];
Evan Cheng8e136a92007-09-26 21:36:17 +0000147 if (!SU || !SU->Node) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000148 if (SU->isCommutable) {
149 unsigned Opc = SU->Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000150 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000151 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000152 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000153 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000154 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000155 continue;
156
157 SDNode *OpN = SU->Node->getOperand(j).Val;
Evan Cheng1bf166312007-11-09 01:27:11 +0000158 SUnit *OpSU = isPassiveNode(OpN) ? NULL : SUnitMap[OpN][SU->InstanceNo];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000159 if (OpSU && OperandSeen.count(OpSU) == 1) {
160 // Ok, so SU is not the last use of OpSU, but SU is two-address so
161 // it will clobber OpSU. Try to commute SU if no other source operands
162 // are live below.
163 bool DoCommute = true;
164 for (unsigned k = 0; k < NumOps; ++k) {
165 if (k != j) {
166 OpN = SU->Node->getOperand(k).Val;
Evan Cheng1bf166312007-11-09 01:27:11 +0000167 OpSU = isPassiveNode(OpN) ? NULL : SUnitMap[OpN][SU->InstanceNo];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000168 if (OpSU && OperandSeen.count(OpSU) == 1) {
169 DoCommute = false;
170 break;
171 }
172 }
Evan Chengafed73e2006-05-12 01:58:24 +0000173 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000174 if (DoCommute)
175 CommuteSet.insert(SU->Node);
Evan Chengafed73e2006-05-12 01:58:24 +0000176 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000177
178 // Only look at the first use&def node for now.
179 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000180 }
181 }
182
Chris Lattnerd86418a2006-08-17 00:09:56 +0000183 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
184 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000185 if (!I->isCtrl)
186 OperandSeen.insert(I->Dep);
Evan Chengafed73e2006-05-12 01:58:24 +0000187 }
188 }
189}
Evan Chengd38c22b2006-05-11 23:55:42 +0000190
191//===----------------------------------------------------------------------===//
192// Bottom-Up Scheduling
193//===----------------------------------------------------------------------===//
194
Evan Chengd38c22b2006-05-11 23:55:42 +0000195/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000196/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000197void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain,
198 unsigned CurCycle) {
199 // FIXME: the distance between two nodes is not always == the predecessor's
200 // latency. For example, the reader can very well read the register written
201 // by the predecessor later than the issue cycle. It also depends on the
202 // interrupt model (drain vs. freeze).
203 PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
204
Evan Cheng038dcc52007-09-28 19:24:24 +0000205 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000206
207#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000208 if (PredSU->NumSuccsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000209 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000210 PredSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000211 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000212 assert(0);
213 }
214#endif
215
Evan Cheng038dcc52007-09-28 19:24:24 +0000216 if (PredSU->NumSuccsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000217 // EntryToken has to go last! Special case it here.
Evan Cheng8e136a92007-09-26 21:36:17 +0000218 if (!PredSU->Node || PredSU->Node->getOpcode() != ISD::EntryToken) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000219 PredSU->isAvailable = true;
220 AvailableQueue->push(PredSU);
221 }
222 }
223}
224
225/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
226/// count of its predecessors. If a predecessor pending count is zero, add it to
227/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000228void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000229 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000230 DEBUG(SU->dump(&DAG));
231 SU->Cycle = CurCycle;
232
233 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000234
235 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000236 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000237 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000238 ReleasePred(I->Dep, I->isCtrl, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000239 if (I->Cost < 0) {
240 // This is a physical register dependency and it's impossible or
241 // expensive to copy the register. Make sure nothing that can
242 // clobber the register is scheduled between the predecessor and
243 // this node.
244 if (LiveRegs.insert(I->Reg)) {
245 LiveRegDefs[I->Reg] = I->Dep;
246 LiveRegCycles[I->Reg] = CurCycle;
247 }
248 }
249 }
250
251 // Release all the implicit physical register defs that are live.
252 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
253 I != E; ++I) {
254 if (I->Cost < 0) {
255 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
256 LiveRegs.erase(I->Reg);
257 assert(LiveRegDefs[I->Reg] == SU &&
258 "Physical register dependency violated?");
259 LiveRegDefs[I->Reg] = NULL;
260 LiveRegCycles[I->Reg] = 0;
261 }
262 }
263 }
264
Evan Chengd38c22b2006-05-11 23:55:42 +0000265 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000266}
267
Evan Cheng5924bf72007-09-25 01:54:36 +0000268/// CapturePred - This does the opposite of ReleasePred. Since SU is being
269/// unscheduled, incrcease the succ left count of its predecessors. Remove
270/// them from AvailableQueue if necessary.
271void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
272 PredSU->CycleBound = 0;
273 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
274 I != E; ++I) {
275 if (I->Dep == SU)
276 continue;
277 PredSU->CycleBound = std::max(PredSU->CycleBound,
278 I->Dep->Cycle + PredSU->Latency);
279 }
280
281 if (PredSU->isAvailable) {
282 PredSU->isAvailable = false;
283 if (!PredSU->isPending)
284 AvailableQueue->remove(PredSU);
285 }
286
Evan Cheng038dcc52007-09-28 19:24:24 +0000287 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000288}
289
290/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
291/// its predecessor states to reflect the change.
292void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
293 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
294 DEBUG(SU->dump(&DAG));
295
296 AvailableQueue->UnscheduledNode(SU);
297
298 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
299 I != E; ++I) {
300 CapturePred(I->Dep, SU, I->isCtrl);
301 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
302 LiveRegs.erase(I->Reg);
303 assert(LiveRegDefs[I->Reg] == I->Dep &&
304 "Physical register dependency violated?");
305 LiveRegDefs[I->Reg] = NULL;
306 LiveRegCycles[I->Reg] = 0;
307 }
308 }
309
310 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
311 I != E; ++I) {
312 if (I->Cost < 0) {
313 if (LiveRegs.insert(I->Reg)) {
314 assert(!LiveRegDefs[I->Reg] &&
315 "Physical register dependency violated?");
316 LiveRegDefs[I->Reg] = SU;
317 }
318 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
319 LiveRegCycles[I->Reg] = I->Dep->Cycle;
320 }
321 }
322
323 SU->Cycle = 0;
324 SU->isScheduled = false;
325 SU->isAvailable = true;
326 AvailableQueue->push(SU);
327}
328
Evan Chengcfd5f822007-09-27 00:25:29 +0000329// FIXME: This is probably too slow!
330static void isReachable(SUnit *SU, SUnit *TargetSU,
331 SmallPtrSet<SUnit*, 32> &Visited, bool &Reached) {
332 if (Reached) return;
333 if (SU == TargetSU) {
334 Reached = true;
335 return;
336 }
337 if (!Visited.insert(SU)) return;
338
339 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); I != E;
340 ++I)
341 isReachable(I->Dep, TargetSU, Visited, Reached);
342}
343
344static bool isReachable(SUnit *SU, SUnit *TargetSU) {
345 SmallPtrSet<SUnit*, 32> Visited;
346 bool Reached = false;
347 isReachable(SU, TargetSU, Visited, Reached);
348 return Reached;
349}
350
351/// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
352/// create a cycle.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000353static bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
Evan Chengcfd5f822007-09-27 00:25:29 +0000354 if (isReachable(TargetSU, SU))
355 return true;
356 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
357 I != E; ++I)
358 if (I->Cost < 0 && isReachable(TargetSU, I->Dep))
359 return true;
360 return false;
361}
362
Evan Cheng8e136a92007-09-26 21:36:17 +0000363/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000364/// BTCycle in order to schedule a specific node. Returns the last unscheduled
365/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000366void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
367 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000368 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000369 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000370 OldSU = Sequence.back();
371 Sequence.pop_back();
372 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000373 // Don't try to remove SU from AvailableQueue.
374 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000375 UnscheduleNodeBottomUp(OldSU);
376 --CurCycle;
377 }
378
379
380 if (SU->isSucc(OldSU)) {
381 assert(false && "Something is wrong!");
382 abort();
383 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000384
385 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000386}
387
Evan Cheng5924bf72007-09-25 01:54:36 +0000388/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
389/// successors to the newly created node.
390SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Evan Cheng79e97132007-10-05 01:39:18 +0000391 if (SU->FlaggedNodes.size())
392 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000393
Evan Cheng79e97132007-10-05 01:39:18 +0000394 SDNode *N = SU->Node;
395 if (!N)
396 return NULL;
397
398 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000399 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000400 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
401 MVT::ValueType VT = N->getValueType(i);
402 if (VT == MVT::Flag)
403 return NULL;
404 else if (VT == MVT::Other)
405 TryUnfold = true;
406 }
Evan Cheng79e97132007-10-05 01:39:18 +0000407 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
408 const SDOperand &Op = N->getOperand(i);
409 MVT::ValueType VT = Op.Val->getValueType(Op.ResNo);
410 if (VT == MVT::Flag)
411 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000412 }
413
414 if (TryUnfold) {
415 SmallVector<SDNode*, 4> NewNodes;
Owen Anderson0ec92e92008-01-07 01:35:56 +0000416 if (!TII->unfoldMemoryOperand(DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000417 return NULL;
418
419 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
420 assert(NewNodes.size() == 2 && "Expected a load folding node!");
421
422 N = NewNodes[1];
423 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000424 unsigned NumVals = N->getNumValues();
425 unsigned OldNumVals = SU->Node->getNumValues();
426 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattner3cfb56d2007-10-15 06:10:22 +0000427 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, i), SDOperand(N, i));
Evan Cheng79e97132007-10-05 01:39:18 +0000428 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, OldNumVals-1),
Chris Lattner3cfb56d2007-10-15 06:10:22 +0000429 SDOperand(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000430
Evan Cheng79e97132007-10-05 01:39:18 +0000431 SUnit *NewSU = NewSUnit(N);
Evan Cheng79e97132007-10-05 01:39:18 +0000432 SUnitMap[N].push_back(NewSU);
Chris Lattner03ad8852008-01-07 07:27:27 +0000433 const TargetInstrDesc &TID = TII->get(N->getTargetOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000434 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000435 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000436 NewSU->isTwoAddress = true;
437 break;
438 }
439 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000440 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000441 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000442 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000443 NewSU->Depth = SU->Depth;
444 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000445 ComputeLatency(NewSU);
446
Evan Cheng91e0fc92007-12-18 08:42:10 +0000447 // LoadNode may already exist. This can happen when there is another
448 // load from the same location and producing the same type of value
449 // but it has different alignment or volatileness.
450 bool isNewLoad = true;
451 SUnit *LoadSU;
452 DenseMap<SDNode*, std::vector<SUnit*> >::iterator SMI =
453 SUnitMap.find(LoadNode);
454 if (SMI != SUnitMap.end()) {
455 LoadSU = SMI->second.front();
456 isNewLoad = false;
457 } else {
458 LoadSU = NewSUnit(LoadNode);
459 SUnitMap[LoadNode].push_back(LoadSU);
460
461 LoadSU->Depth = SU->Depth;
462 LoadSU->Height = SU->Height;
463 ComputeLatency(LoadSU);
464 }
465
Evan Cheng79e97132007-10-05 01:39:18 +0000466 SUnit *ChainPred = NULL;
467 SmallVector<SDep, 4> ChainSuccs;
468 SmallVector<SDep, 4> LoadPreds;
469 SmallVector<SDep, 4> NodePreds;
470 SmallVector<SDep, 4> NodeSuccs;
471 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
472 I != E; ++I) {
473 if (I->isCtrl)
474 ChainPred = I->Dep;
475 else if (I->Dep->Node && I->Dep->Node->isOperand(LoadNode))
476 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
477 else
478 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
479 }
480 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
481 I != E; ++I) {
482 if (I->isCtrl)
483 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
484 I->isCtrl, I->isSpecial));
485 else
486 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
487 I->isCtrl, I->isSpecial));
488 }
489
490 SU->removePred(ChainPred, true, false);
Evan Cheng91e0fc92007-12-18 08:42:10 +0000491 if (isNewLoad)
492 LoadSU->addPred(ChainPred, true, false);
Evan Cheng79e97132007-10-05 01:39:18 +0000493 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
494 SDep *Pred = &LoadPreds[i];
495 SU->removePred(Pred->Dep, Pred->isCtrl, Pred->isSpecial);
Evan Cheng91e0fc92007-12-18 08:42:10 +0000496 if (isNewLoad)
497 LoadSU->addPred(Pred->Dep, Pred->isCtrl, Pred->isSpecial,
498 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000499 }
500 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
501 SDep *Pred = &NodePreds[i];
502 SU->removePred(Pred->Dep, Pred->isCtrl, Pred->isSpecial);
503 NewSU->addPred(Pred->Dep, Pred->isCtrl, Pred->isSpecial,
504 Pred->Reg, Pred->Cost);
505 }
506 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
507 SDep *Succ = &NodeSuccs[i];
508 Succ->Dep->removePred(SU, Succ->isCtrl, Succ->isSpecial);
509 Succ->Dep->addPred(NewSU, Succ->isCtrl, Succ->isSpecial,
510 Succ->Reg, Succ->Cost);
511 }
512 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
513 SDep *Succ = &ChainSuccs[i];
514 Succ->Dep->removePred(SU, Succ->isCtrl, Succ->isSpecial);
Evan Cheng91e0fc92007-12-18 08:42:10 +0000515 if (isNewLoad)
516 Succ->Dep->addPred(LoadSU, Succ->isCtrl, Succ->isSpecial,
517 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000518 }
Evan Cheng91e0fc92007-12-18 08:42:10 +0000519 if (isNewLoad)
520 NewSU->addPred(LoadSU, false, false);
Evan Cheng79e97132007-10-05 01:39:18 +0000521
Evan Cheng91e0fc92007-12-18 08:42:10 +0000522 if (isNewLoad)
523 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000524 AvailableQueue->addNode(NewSU);
525
526 ++NumUnfolds;
527
528 if (NewSU->NumSuccsLeft == 0) {
529 NewSU->isAvailable = true;
530 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000531 }
532 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000533 }
534
535 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
536 NewSU = Clone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000537
538 // New SUnit has the exact same predecessors.
539 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
540 I != E; ++I)
541 if (!I->isSpecial) {
542 NewSU->addPred(I->Dep, I->isCtrl, false, I->Reg, I->Cost);
543 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
544 }
545
546 // Only copy scheduled successors. Cut them from old node's successor
547 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000548 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000549 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
550 I != E; ++I) {
551 if (I->isSpecial)
552 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000553 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000554 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Evan Cheng5924bf72007-09-25 01:54:36 +0000555 I->Dep->addPred(NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000556 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000557 }
558 }
559 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000560 SUnit *Succ = DelDeps[i].first;
561 bool isCtrl = DelDeps[i].second;
Evan Cheng5924bf72007-09-25 01:54:36 +0000562 Succ->removePred(SU, isCtrl, false);
563 }
564
565 AvailableQueue->updateNode(SU);
566 AvailableQueue->addNode(NewSU);
567
Evan Cheng1ec79b42007-09-27 07:09:03 +0000568 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000569 return NewSU;
570}
571
Evan Cheng1ec79b42007-09-27 07:09:03 +0000572/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
573/// and move all scheduled successors of the given SUnit to the last copy.
574void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
575 const TargetRegisterClass *DestRC,
576 const TargetRegisterClass *SrcRC,
577 SmallVector<SUnit*, 2> &Copies) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000578 SUnit *CopyFromSU = NewSUnit(NULL);
579 CopyFromSU->CopySrcRC = SrcRC;
580 CopyFromSU->CopyDstRC = DestRC;
581 CopyFromSU->Depth = SU->Depth;
582 CopyFromSU->Height = SU->Height;
583
584 SUnit *CopyToSU = NewSUnit(NULL);
585 CopyToSU->CopySrcRC = DestRC;
586 CopyToSU->CopyDstRC = SrcRC;
587
588 // Only copy scheduled successors. Cut them from old node's successor
589 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000590 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000591 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
592 I != E; ++I) {
593 if (I->isSpecial)
594 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000595 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000596 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000597 I->Dep->addPred(CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000598 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000599 }
600 }
601 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000602 SUnit *Succ = DelDeps[i].first;
603 bool isCtrl = DelDeps[i].second;
Evan Cheng8e136a92007-09-26 21:36:17 +0000604 Succ->removePred(SU, isCtrl, false);
605 }
606
607 CopyFromSU->addPred(SU, false, false, Reg, -1);
608 CopyToSU->addPred(CopyFromSU, false, false, Reg, 1);
609
610 AvailableQueue->updateNode(SU);
611 AvailableQueue->addNode(CopyFromSU);
612 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000613 Copies.push_back(CopyFromSU);
614 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000615
Evan Cheng1ec79b42007-09-27 07:09:03 +0000616 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000617}
618
619/// getPhysicalRegisterVT - Returns the ValueType of the physical register
620/// definition of the specified node.
621/// FIXME: Move to SelectionDAG?
622static MVT::ValueType getPhysicalRegisterVT(SDNode *N, unsigned Reg,
623 const TargetInstrInfo *TII) {
Chris Lattner03ad8852008-01-07 07:27:27 +0000624 const TargetInstrDesc &TID = TII->get(N->getTargetOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000625 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000626 unsigned NumRes = TID.getNumDefs();
627 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000628 if (Reg == *ImpDef)
629 break;
630 ++NumRes;
631 }
632 return N->getValueType(NumRes);
633}
634
Evan Cheng5924bf72007-09-25 01:54:36 +0000635/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
636/// scheduling of the given node to satisfy live physical register dependencies.
637/// If the specific node is the last one that's available to schedule, do
638/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000639bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
640 SmallVector<unsigned, 4> &LRegs){
Evan Cheng5924bf72007-09-25 01:54:36 +0000641 if (LiveRegs.empty())
642 return false;
643
Evan Chenge6f92252007-09-27 18:46:06 +0000644 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000645 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000646 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
647 I != E; ++I) {
648 if (I->Cost < 0) {
649 unsigned Reg = I->Reg;
Evan Chenge6f92252007-09-27 18:46:06 +0000650 if (LiveRegs.count(Reg) && LiveRegDefs[Reg] != I->Dep) {
651 if (RegAdded.insert(Reg))
652 LRegs.push_back(Reg);
653 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000654 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000655 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000656 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != I->Dep) {
657 if (RegAdded.insert(*Alias))
658 LRegs.push_back(*Alias);
659 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000660 }
661 }
662
663 for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) {
664 SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1];
Evan Cheng8e136a92007-09-26 21:36:17 +0000665 if (!Node || !Node->isTargetOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000666 continue;
Chris Lattner03ad8852008-01-07 07:27:27 +0000667 const TargetInstrDesc &TID = TII->get(Node->getTargetOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 if (!TID.ImplicitDefs)
669 continue;
670 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Evan Chenge6f92252007-09-27 18:46:06 +0000671 if (LiveRegs.count(*Reg) && LiveRegDefs[*Reg] != SU) {
672 if (RegAdded.insert(*Reg))
673 LRegs.push_back(*Reg);
674 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000675 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000676 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000677 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != SU) {
678 if (RegAdded.insert(*Alias))
679 LRegs.push_back(*Alias);
680 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000681 }
682 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000683 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000684}
685
Evan Cheng1ec79b42007-09-27 07:09:03 +0000686
Evan Chengd38c22b2006-05-11 23:55:42 +0000687/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
688/// schedulers.
689void ScheduleDAGRRList::ListScheduleBottomUp() {
690 unsigned CurCycle = 0;
691 // Add root to Available queue.
Evan Cheng5924bf72007-09-25 01:54:36 +0000692 SUnit *RootSU = SUnitMap[DAG.getRoot().Val].front();
693 RootSU->isAvailable = true;
694 AvailableQueue->push(RootSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000695
696 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000697 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000698 SmallVector<SUnit*, 4> NotReady;
Evan Chengd38c22b2006-05-11 23:55:42 +0000699 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000700 bool Delayed = false;
701 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Evan Cheng5924bf72007-09-25 01:54:36 +0000702 SUnit *CurSU = AvailableQueue->pop();
703 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000704 if (CurSU->CycleBound <= CurCycle) {
705 SmallVector<unsigned, 4> LRegs;
706 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000707 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000708 Delayed = true;
709 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000710 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000711
712 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
713 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000714 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000715 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000716
717 // All candidates are delayed due to live physical reg dependencies.
718 // Try backtracking, code duplication, or inserting cross class copies
719 // to resolve it.
720 if (Delayed && !CurSU) {
721 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
722 SUnit *TrySU = NotReady[i];
723 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
724
725 // Try unscheduling up to the point where it's safe to schedule
726 // this node.
727 unsigned LiveCycle = CurCycle;
728 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
729 unsigned Reg = LRegs[j];
730 unsigned LCycle = LiveRegCycles[Reg];
731 LiveCycle = std::min(LiveCycle, LCycle);
732 }
733 SUnit *OldSU = Sequence[LiveCycle];
734 if (!WillCreateCycle(TrySU, OldSU)) {
735 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
736 // Force the current node to be scheduled before the node that
737 // requires the physical reg dep.
738 if (OldSU->isAvailable) {
739 OldSU->isAvailable = false;
740 AvailableQueue->remove(OldSU);
741 }
742 TrySU->addPred(OldSU, true, true);
743 // If one or more successors has been unscheduled, then the current
744 // node is no longer avaialable. Schedule a successor that's now
745 // available instead.
746 if (!TrySU->isAvailable)
747 CurSU = AvailableQueue->pop();
748 else {
749 CurSU = TrySU;
750 TrySU->isPending = false;
751 NotReady.erase(NotReady.begin()+i);
752 }
753 break;
754 }
755 }
756
757 if (!CurSU) {
758 // Can't backtrace. Try duplicating the nodes that produces these
759 // "expensive to copy" values to break the dependency. In case even
760 // that doesn't work, insert cross class copies.
761 SUnit *TrySU = NotReady[0];
762 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
763 assert(LRegs.size() == 1 && "Can't handle this yet!");
764 unsigned Reg = LRegs[0];
765 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +0000766 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
767 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000768 // Issue expensive cross register class copies.
769 MVT::ValueType VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII);
770 const TargetRegisterClass *RC =
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000771 TRI->getPhysicalRegisterRegClass(VT, Reg);
772 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000773 if (!DestRC) {
774 assert(false && "Don't know how to copy this physical register!");
775 abort();
776 }
777 SmallVector<SUnit*, 2> Copies;
778 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
779 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
780 << " to SU #" << Copies.front()->NodeNum << "\n";
781 TrySU->addPred(Copies.front(), true, true);
782 NewDef = Copies.back();
783 }
784
785 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
786 << " to SU #" << TrySU->NodeNum << "\n";
787 LiveRegDefs[Reg] = NewDef;
788 NewDef->addPred(TrySU, true, true);
789 TrySU->isAvailable = false;
790 CurSU = NewDef;
791 }
792
793 if (!CurSU) {
794 assert(false && "Unable to resolve live physical register dependencies!");
795 abort();
796 }
797 }
798
Evan Chengd38c22b2006-05-11 23:55:42 +0000799 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000800 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
801 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000802 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000803 if (NotReady[i]->isAvailable)
804 AvailableQueue->push(NotReady[i]);
805 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000806 NotReady.clear();
807
Evan Cheng5924bf72007-09-25 01:54:36 +0000808 if (!CurSU)
809 Sequence.push_back(0);
810 else {
811 ScheduleNodeBottomUp(CurSU, CurCycle);
812 Sequence.push_back(CurSU);
813 }
814 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000815 }
816
817 // Add entry node last
818 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000819 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front();
Evan Chengd38c22b2006-05-11 23:55:42 +0000820 Sequence.push_back(Entry);
821 }
822
823 // Reverse the order if it is bottom up.
824 std::reverse(Sequence.begin(), Sequence.end());
825
826
827#ifndef NDEBUG
828 // Verify that all SUnits were scheduled.
829 bool AnyNotSched = false;
830 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000831 if (SUnits[i].NumSuccsLeft != 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000832 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000833 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000834 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000835 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000836 AnyNotSched = true;
837 }
838 }
839 assert(!AnyNotSched);
840#endif
841}
842
843//===----------------------------------------------------------------------===//
844// Top-Down Scheduling
845//===----------------------------------------------------------------------===//
846
847/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000848/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000849void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain,
850 unsigned CurCycle) {
851 // FIXME: the distance between two nodes is not always == the predecessor's
852 // latency. For example, the reader can very well read the register written
853 // by the predecessor later than the issue cycle. It also depends on the
854 // interrupt model (drain vs. freeze).
855 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
856
Evan Cheng038dcc52007-09-28 19:24:24 +0000857 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000858
859#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000860 if (SuccSU->NumPredsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000861 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000862 SuccSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000863 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000864 assert(0);
865 }
866#endif
867
Evan Cheng038dcc52007-09-28 19:24:24 +0000868 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000869 SuccSU->isAvailable = true;
870 AvailableQueue->push(SuccSU);
871 }
872}
873
874
875/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
876/// count of its successors. If a successor pending count is zero, add it to
877/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000878void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000879 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000880 DEBUG(SU->dump(&DAG));
881 SU->Cycle = CurCycle;
882
883 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000884
885 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000886 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
887 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000888 ReleaseSucc(I->Dep, I->isCtrl, CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000889 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000890}
891
Dan Gohman54a187e2007-08-20 19:28:38 +0000892/// ListScheduleTopDown - The main loop of list scheduling for top-down
893/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000894void ScheduleDAGRRList::ListScheduleTopDown() {
895 unsigned CurCycle = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000896 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front();
Evan Chengd38c22b2006-05-11 23:55:42 +0000897
898 // All leaves to Available queue.
899 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
900 // It is available if it has no predecessors.
Dan Gohman70de4cb2008-01-29 13:02:09 +0000901 if (SUnits[i].Preds.empty() && &SUnits[i] != Entry) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000902 AvailableQueue->push(&SUnits[i]);
903 SUnits[i].isAvailable = true;
904 }
905 }
906
907 // Emit the entry node first.
908 ScheduleNodeTopDown(Entry, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000909 Sequence.push_back(Entry);
910 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000911
912 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000913 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +0000914 std::vector<SUnit*> NotReady;
Evan Chengd38c22b2006-05-11 23:55:42 +0000915 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000916 SUnit *CurSU = AvailableQueue->pop();
917 while (CurSU && CurSU->CycleBound > CurCycle) {
918 NotReady.push_back(CurSU);
919 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000920 }
921
922 // Add the nodes that aren't ready back onto the available list.
923 AvailableQueue->push_all(NotReady);
924 NotReady.clear();
925
Evan Cheng5924bf72007-09-25 01:54:36 +0000926 if (!CurSU)
927 Sequence.push_back(0);
928 else {
929 ScheduleNodeTopDown(CurSU, CurCycle);
930 Sequence.push_back(CurSU);
931 }
Evan Chengd12c97d2006-05-30 18:05:39 +0000932 CurCycle++;
Evan Chengd38c22b2006-05-11 23:55:42 +0000933 }
934
935
936#ifndef NDEBUG
937 // Verify that all SUnits were scheduled.
938 bool AnyNotSched = false;
939 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
940 if (!SUnits[i].isScheduled) {
941 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000942 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000943 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000944 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000945 AnyNotSched = true;
946 }
947 }
948 assert(!AnyNotSched);
949#endif
950}
951
952
953
954//===----------------------------------------------------------------------===//
955// RegReductionPriorityQueue Implementation
956//===----------------------------------------------------------------------===//
957//
958// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
959// to reduce register pressure.
960//
961namespace {
962 template<class SF>
963 class RegReductionPriorityQueue;
964
965 /// Sorting functions for the Available queue.
966 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
967 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
968 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
969 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
970
971 bool operator()(const SUnit* left, const SUnit* right) const;
972 };
973
974 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
975 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
976 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
977 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
978
979 bool operator()(const SUnit* left, const SUnit* right) const;
980 };
981} // end anonymous namespace
982
Evan Cheng961bbd32007-01-08 23:50:38 +0000983static inline bool isCopyFromLiveIn(const SUnit *SU) {
984 SDNode *N = SU->Node;
Evan Cheng8e136a92007-09-26 21:36:17 +0000985 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +0000986 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
987}
988
Evan Chengd38c22b2006-05-11 23:55:42 +0000989namespace {
990 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000991 class VISIBILITY_HIDDEN RegReductionPriorityQueue
992 : public SchedulingPriorityQueue {
Evan Chengd38c22b2006-05-11 23:55:42 +0000993 std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue;
994
995 public:
996 RegReductionPriorityQueue() :
997 Queue(SF(this)) {}
998
Evan Cheng5924bf72007-09-25 01:54:36 +0000999 virtual void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001000 std::vector<SUnit> &sunits) {}
Evan Cheng5924bf72007-09-25 01:54:36 +00001001
1002 virtual void addNode(const SUnit *SU) {}
1003
1004 virtual void updateNode(const SUnit *SU) {}
1005
Evan Chengd38c22b2006-05-11 23:55:42 +00001006 virtual void releaseState() {}
1007
Evan Cheng6730f032007-01-08 23:55:53 +00001008 virtual unsigned getNodePriority(const SUnit *SU) const {
Evan Chengd38c22b2006-05-11 23:55:42 +00001009 return 0;
1010 }
1011
Evan Cheng5924bf72007-09-25 01:54:36 +00001012 unsigned size() const { return Queue.size(); }
1013
Evan Chengd38c22b2006-05-11 23:55:42 +00001014 bool empty() const { return Queue.empty(); }
1015
1016 void push(SUnit *U) {
1017 Queue.push(U);
1018 }
1019 void push_all(const std::vector<SUnit *> &Nodes) {
1020 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
1021 Queue.push(Nodes[i]);
1022 }
1023
1024 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001025 if (empty()) return NULL;
Evan Chengd38c22b2006-05-11 23:55:42 +00001026 SUnit *V = Queue.top();
1027 Queue.pop();
1028 return V;
1029 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001030
Evan Cheng5924bf72007-09-25 01:54:36 +00001031 /// remove - This is a really inefficient way to remove a node from a
1032 /// priority queue. We should roll our own heap to make this better or
1033 /// something.
1034 void remove(SUnit *SU) {
1035 std::vector<SUnit*> Temp;
1036
1037 assert(!Queue.empty() && "Not in queue!");
1038 while (Queue.top() != SU) {
1039 Temp.push_back(Queue.top());
1040 Queue.pop();
1041 assert(!Queue.empty() && "Not in queue!");
1042 }
1043
1044 // Remove the node from the PQ.
1045 Queue.pop();
1046
1047 // Add all the other nodes back.
1048 for (unsigned i = 0, e = Temp.size(); i != e; ++i)
1049 Queue.push(Temp[i]);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001050 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001051 };
1052
1053 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001054 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
1055 : public RegReductionPriorityQueue<SF> {
Evan Cheng5924bf72007-09-25 01:54:36 +00001056 // SUnitMap SDNode to SUnit mapping (n -> n).
1057 DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001058
Evan Chengd38c22b2006-05-11 23:55:42 +00001059 // SUnits - The SUnits for the current graph.
1060 const std::vector<SUnit> *SUnits;
1061
1062 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001063 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001064
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001065 const TargetInstrInfo *TII;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001066 const TargetRegisterInfo *TRI;
Evan Chengd38c22b2006-05-11 23:55:42 +00001067 public:
Evan Chengf9891412007-12-20 09:25:31 +00001068 explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001069 const TargetRegisterInfo *tri)
1070 : TII(tii), TRI(tri) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001071
Evan Cheng5924bf72007-09-25 01:54:36 +00001072 void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001073 std::vector<SUnit> &sunits) {
1074 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +00001075 SUnits = &sunits;
1076 // Add pseudo dependency edges for two-address nodes.
Evan Chengafed73e2006-05-12 01:58:24 +00001077 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001078 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001079 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001080 }
1081
Evan Cheng5924bf72007-09-25 01:54:36 +00001082 void addNode(const SUnit *SU) {
1083 SethiUllmanNumbers.resize(SUnits->size(), 0);
1084 CalcNodeSethiUllmanNumber(SU);
1085 }
1086
1087 void updateNode(const SUnit *SU) {
1088 SethiUllmanNumbers[SU->NodeNum] = 0;
1089 CalcNodeSethiUllmanNumber(SU);
1090 }
1091
Evan Chengd38c22b2006-05-11 23:55:42 +00001092 void releaseState() {
1093 SUnits = 0;
1094 SethiUllmanNumbers.clear();
1095 }
1096
Evan Cheng6730f032007-01-08 23:55:53 +00001097 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001098 assert(SU->NodeNum < SethiUllmanNumbers.size());
Evan Cheng8e136a92007-09-26 21:36:17 +00001099 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001100 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1101 // CopyFromReg should be close to its def because it restricts
1102 // allocation choices. But if it is a livein then perhaps we want it
1103 // closer to its uses so it can be coalesced.
1104 return 0xffff;
1105 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1106 // CopyToReg should be close to its uses to facilitate coalescing and
1107 // avoid spilling.
1108 return 0;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001109 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1110 Opc == TargetInstrInfo::INSERT_SUBREG)
1111 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1112 // facilitate coalescing.
1113 return 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001114 else if (SU->NumSuccs == 0)
1115 // If SU does not have a use, i.e. it doesn't produce a value that would
1116 // be consumed (e.g. store), then it terminates a chain of computation.
1117 // Give it a large SethiUllman number so it will be scheduled right
1118 // before its predecessors that it doesn't lengthen their live ranges.
1119 return 0xffff;
1120 else if (SU->NumPreds == 0)
1121 // If SU does not have a def, schedule it close to its uses because it
1122 // does not lengthen any live ranges.
1123 return 0;
1124 else
1125 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001126 }
1127
1128 private:
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001129 bool canClobber(SUnit *SU, SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +00001130 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001131 void CalculateSethiUllmanNumbers();
1132 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001133 };
1134
1135
1136 template<class SF>
Dan Gohman54a187e2007-08-20 19:28:38 +00001137 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
1138 : public RegReductionPriorityQueue<SF> {
Evan Cheng5924bf72007-09-25 01:54:36 +00001139 // SUnitMap SDNode to SUnit mapping (n -> n).
1140 DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001141
Evan Chengd38c22b2006-05-11 23:55:42 +00001142 // SUnits - The SUnits for the current graph.
1143 const std::vector<SUnit> *SUnits;
1144
1145 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001146 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001147
1148 public:
1149 TDRegReductionPriorityQueue() {}
1150
Evan Cheng5924bf72007-09-25 01:54:36 +00001151 void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001152 std::vector<SUnit> &sunits) {
1153 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +00001154 SUnits = &sunits;
1155 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001156 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001157 }
1158
Evan Cheng5924bf72007-09-25 01:54:36 +00001159 void addNode(const SUnit *SU) {
1160 SethiUllmanNumbers.resize(SUnits->size(), 0);
1161 CalcNodeSethiUllmanNumber(SU);
1162 }
1163
1164 void updateNode(const SUnit *SU) {
1165 SethiUllmanNumbers[SU->NodeNum] = 0;
1166 CalcNodeSethiUllmanNumber(SU);
1167 }
1168
Evan Chengd38c22b2006-05-11 23:55:42 +00001169 void releaseState() {
1170 SUnits = 0;
1171 SethiUllmanNumbers.clear();
1172 }
1173
Evan Cheng6730f032007-01-08 23:55:53 +00001174 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001175 assert(SU->NodeNum < SethiUllmanNumbers.size());
1176 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001177 }
1178
1179 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001180 void CalculateSethiUllmanNumbers();
1181 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001182 };
1183}
1184
Evan Chengb9e3db62007-03-14 22:43:40 +00001185/// closestSucc - Returns the scheduled cycle of the successor which is
1186/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001187static unsigned closestSucc(const SUnit *SU) {
1188 unsigned MaxCycle = 0;
1189 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001190 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001191 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001192 // If there are bunch of CopyToRegs stacked up, they should be considered
1193 // to be at the same position.
Evan Cheng8e136a92007-09-26 21:36:17 +00001194 if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001195 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001196 if (Cycle > MaxCycle)
1197 MaxCycle = Cycle;
1198 }
Evan Cheng28748552007-03-13 23:25:11 +00001199 return MaxCycle;
1200}
1201
Evan Cheng61bc51e2007-12-20 02:22:36 +00001202/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1203/// for scratch registers. Live-in operands and live-out results don't count
1204/// since they are "fixed".
1205static unsigned calcMaxScratches(const SUnit *SU) {
1206 unsigned Scratches = 0;
1207 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1208 I != E; ++I) {
1209 if (I->isCtrl) continue; // ignore chain preds
Evan Cheng0e400d42008-01-09 23:01:55 +00001210 if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001211 Scratches++;
1212 }
1213 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1214 I != E; ++I) {
1215 if (I->isCtrl) continue; // ignore chain succs
Evan Cheng0e400d42008-01-09 23:01:55 +00001216 if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001217 Scratches += 10;
1218 }
1219 return Scratches;
1220}
1221
Evan Chengd38c22b2006-05-11 23:55:42 +00001222// Bottom up
1223bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
David Greene4c1e6f32007-06-29 03:42:23 +00001224 // There used to be a special tie breaker here that looked for
David Greene5b6f7552007-06-29 02:48:09 +00001225 // two-address instructions and preferred the instruction with a
1226 // def&use operand. The special case triggered diagnostics when
1227 // _GLIBCXX_DEBUG was enabled because it broke the strict weak
1228 // ordering that priority_queue requires. It didn't help much anyway
1229 // because AddPseudoTwoAddrDeps already covers many of the cases
1230 // where it would have applied. In addition, it's counter-intuitive
1231 // that a tie breaker would be the first thing attempted. There's a
1232 // "real" tie breaker below that is the operation of last resort.
1233 // The fact that the "special tie breaker" would trigger when there
1234 // wasn't otherwise a tie is what broke the strict weak ordering
1235 // constraint.
Evan Cheng99f2f792006-05-13 08:22:24 +00001236
Evan Cheng6730f032007-01-08 23:55:53 +00001237 unsigned LPriority = SPQ->getNodePriority(left);
1238 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng961bbd32007-01-08 23:50:38 +00001239 if (LPriority > RPriority)
Evan Chengd38c22b2006-05-11 23:55:42 +00001240 return true;
Evan Cheng28748552007-03-13 23:25:11 +00001241 else if (LPriority == RPriority) {
Dan Gohmane131e3a2007-04-26 19:40:56 +00001242 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
Evan Cheng28748552007-03-13 23:25:11 +00001243 // e.g.
1244 // t1 = op t2, c1
1245 // t3 = op t4, c2
1246 //
1247 // and the following instructions are both ready.
1248 // t2 = op c3
1249 // t4 = op c4
1250 //
1251 // Then schedule t2 = op first.
1252 // i.e.
1253 // t4 = op c4
1254 // t2 = op c3
1255 // t1 = op t2, c1
1256 // t3 = op t4, c2
1257 //
1258 // This creates more short live intervals.
1259 unsigned LDist = closestSucc(left);
1260 unsigned RDist = closestSucc(right);
1261 if (LDist < RDist)
Evan Chengd38c22b2006-05-11 23:55:42 +00001262 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +00001263 else if (LDist == RDist) {
Evan Cheng61bc51e2007-12-20 02:22:36 +00001264 // Intuitively, it's good to push down instructions whose results are
1265 // liveout so their long live ranges won't conflict with other values
1266 // which are needed inside the BB. Further prioritize liveout instructions
1267 // by the number of operands which are calculated within the BB.
1268 unsigned LScratch = calcMaxScratches(left);
1269 unsigned RScratch = calcMaxScratches(right);
1270 if (LScratch > RScratch)
Evan Chengd38c22b2006-05-11 23:55:42 +00001271 return true;
Evan Cheng61bc51e2007-12-20 02:22:36 +00001272 else if (LScratch == RScratch)
1273 if (left->Height > right->Height)
Evan Cheng99f2f792006-05-13 08:22:24 +00001274 return true;
Evan Cheng61bc51e2007-12-20 02:22:36 +00001275 else if (left->Height == right->Height)
1276 if (left->Depth < right->Depth)
Evan Cheng28748552007-03-13 23:25:11 +00001277 return true;
Evan Cheng61bc51e2007-12-20 02:22:36 +00001278 else if (left->Depth == right->Depth)
1279 if (left->CycleBound > right->CycleBound)
1280 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +00001281 }
Evan Cheng28748552007-03-13 23:25:11 +00001282 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001283 return false;
1284}
1285
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001286template<class SF>
1287bool BURegReductionPriorityQueue<SF>::canClobber(SUnit *SU, SUnit *Op) {
1288 if (SU->isTwoAddress) {
1289 unsigned Opc = SU->Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001290 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001291 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001292 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001293 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001294 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001295 SDNode *DU = SU->Node->getOperand(i).Val;
Evan Cheng1bf166312007-11-09 01:27:11 +00001296 if ((*SUnitMap).find(DU) != (*SUnitMap).end() &&
1297 Op == (*SUnitMap)[DU][SU->InstanceNo])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001298 return true;
1299 }
1300 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001301 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001302 return false;
1303}
1304
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001305
Evan Chenga5e595d2007-09-28 22:32:30 +00001306/// hasCopyToRegUse - Return true if SU has a value successor that is a
1307/// CopyToReg node.
1308static bool hasCopyToRegUse(SUnit *SU) {
1309 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1310 I != E; ++I) {
1311 if (I->isCtrl) continue;
1312 SUnit *SuccSU = I->Dep;
1313 if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg)
1314 return true;
1315 }
1316 return false;
1317}
1318
Evan Chengf9891412007-12-20 09:25:31 +00001319/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
1320/// physical register def.
1321static bool canClobberPhysRegDefs(SUnit *SuccSU, SUnit *SU,
1322 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001323 const TargetRegisterInfo *TRI) {
Evan Chengf9891412007-12-20 09:25:31 +00001324 SDNode *N = SuccSU->Node;
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001325 unsigned NumDefs = TII->get(N->getTargetOpcode()).getNumDefs();
1326 const unsigned *ImpDefs = TII->get(N->getTargetOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001327 if (!ImpDefs)
1328 return false;
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001329 const unsigned *SUImpDefs =
1330 TII->get(SU->Node->getTargetOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001331 if (!SUImpDefs)
1332 return false;
1333 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1334 MVT::ValueType VT = N->getValueType(i);
1335 if (VT == MVT::Flag || VT == MVT::Other)
1336 continue;
1337 unsigned Reg = ImpDefs[i - NumDefs];
1338 for (;*SUImpDefs; ++SUImpDefs) {
1339 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001340 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001341 return true;
1342 }
1343 }
1344 return false;
1345}
1346
Evan Chengd38c22b2006-05-11 23:55:42 +00001347/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1348/// it as a def&use operand. Add a pseudo control edge from it to the other
1349/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001350/// first (lower in the schedule). If both nodes are two-address, favor the
1351/// one that has a CopyToReg use (more likely to be a loop induction update).
1352/// If both are two-address, but one is commutable while the other is not
1353/// commutable, favor the one that's not commutable.
Evan Chengd38c22b2006-05-11 23:55:42 +00001354template<class SF>
1355void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001356 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1357 SUnit *SU = (SUnit *)&((*SUnits)[i]);
1358 if (!SU->isTwoAddress)
1359 continue;
1360
1361 SDNode *Node = SU->Node;
Evan Chenga5e595d2007-09-28 22:32:30 +00001362 if (!Node || !Node->isTargetOpcode() || SU->FlaggedNodes.size() > 0)
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001363 continue;
1364
1365 unsigned Opc = Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001366 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001367 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001368 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001369 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001370 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001371 SDNode *DU = SU->Node->getOperand(j).Val;
Evan Cheng1bf166312007-11-09 01:27:11 +00001372 if ((*SUnitMap).find(DU) == (*SUnitMap).end())
1373 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +00001374 SUnit *DUSU = (*SUnitMap)[DU][SU->InstanceNo];
Evan Chengf24d15f2006-11-06 21:33:46 +00001375 if (!DUSU) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001376 for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end();
1377 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001378 if (I->isCtrl) continue;
1379 SUnit *SuccSU = I->Dep;
Evan Chengf9891412007-12-20 09:25:31 +00001380 if (SuccSU == SU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001381 continue;
Evan Cheng2dbffa42007-11-06 08:44:59 +00001382 // Be conservative. Ignore if nodes aren't at roughly the same
1383 // depth and height.
1384 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1385 continue;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001386 if (!SuccSU->Node || !SuccSU->Node->isTargetOpcode())
1387 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001388 // Don't constrain nodes with physical register defs if the
Dan Gohmancf8827a2008-01-29 12:43:50 +00001389 // predecessor can clobber them.
Evan Chengf9891412007-12-20 09:25:31 +00001390 if (SuccSU->hasPhysRegDefs) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001391 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Chengf9891412007-12-20 09:25:31 +00001392 continue;
1393 }
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001394 // Don't constraint extract_subreg / insert_subreg these may be
1395 // coalesced away. We don't them close to their uses.
1396 unsigned SuccOpc = SuccSU->Node->getTargetOpcode();
1397 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1398 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1399 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +00001400 if ((!canClobber(SuccSU, DUSU) ||
Evan Chenga5e595d2007-09-28 22:32:30 +00001401 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
Evan Cheng5924bf72007-09-25 01:54:36 +00001402 (!SU->isCommutable && SuccSU->isCommutable)) &&
1403 !isReachable(SuccSU, SU)) {
1404 DOUT << "Adding an edge from SU # " << SU->NodeNum
1405 << " to SU #" << SuccSU->NodeNum << "\n";
1406 SU->addPred(SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001407 }
1408 }
1409 }
1410 }
1411 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001412}
1413
Evan Cheng6730f032007-01-08 23:55:53 +00001414/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +00001415/// Smaller number is the higher priority.
1416template<class SF>
Chris Lattner296a83c2007-02-01 04:55:59 +00001417unsigned BURegReductionPriorityQueue<SF>::
1418CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +00001419 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001420 if (SethiUllmanNumber != 0)
1421 return SethiUllmanNumber;
1422
Evan Cheng961bbd32007-01-08 23:50:38 +00001423 unsigned Extra = 0;
1424 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1425 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001426 if (I->isCtrl) continue; // ignore chain preds
1427 SUnit *PredSU = I->Dep;
Evan Cheng6730f032007-01-08 23:55:53 +00001428 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Cheng961bbd32007-01-08 23:50:38 +00001429 if (PredSethiUllman > SethiUllmanNumber) {
1430 SethiUllmanNumber = PredSethiUllman;
1431 Extra = 0;
Evan Cheng0effc3a2007-09-19 01:38:40 +00001432 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
Evan Cheng5924bf72007-09-25 01:54:36 +00001433 ++Extra;
Evan Chengd38c22b2006-05-11 23:55:42 +00001434 }
Evan Cheng961bbd32007-01-08 23:50:38 +00001435
1436 SethiUllmanNumber += Extra;
1437
1438 if (SethiUllmanNumber == 0)
1439 SethiUllmanNumber = 1;
Evan Chengd38c22b2006-05-11 23:55:42 +00001440
1441 return SethiUllmanNumber;
1442}
1443
Evan Cheng6730f032007-01-08 23:55:53 +00001444/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1445/// scheduling units.
Evan Chengd38c22b2006-05-11 23:55:42 +00001446template<class SF>
Evan Cheng6730f032007-01-08 23:55:53 +00001447void BURegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001448 SethiUllmanNumbers.assign(SUnits->size(), 0);
1449
1450 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +00001451 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001452}
1453
1454static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) {
1455 unsigned Sum = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001456 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1457 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001458 SUnit *SuccSU = I->Dep;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001459 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1460 EE = SuccSU->Preds.end(); II != EE; ++II) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001461 SUnit *PredSU = II->Dep;
Evan Chengd38c22b2006-05-11 23:55:42 +00001462 if (!PredSU->isScheduled)
Evan Cheng5924bf72007-09-25 01:54:36 +00001463 ++Sum;
Evan Chengd38c22b2006-05-11 23:55:42 +00001464 }
1465 }
1466
1467 return Sum;
1468}
1469
1470
1471// Top down
1472bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001473 unsigned LPriority = SPQ->getNodePriority(left);
1474 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng8e136a92007-09-26 21:36:17 +00001475 bool LIsTarget = left->Node && left->Node->isTargetOpcode();
1476 bool RIsTarget = right->Node && right->Node->isTargetOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001477 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1478 bool RIsFloater = RIsTarget && right->NumPreds == 0;
1479 unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0;
1480 unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0;
1481
1482 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1483 return false;
1484 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1485 return true;
1486
1487 // Special tie breaker: if two nodes share a operand, the one that use it
1488 // as a def&use operand is preferred.
1489 if (LIsTarget && RIsTarget) {
1490 if (left->isTwoAddress && !right->isTwoAddress) {
1491 SDNode *DUNode = left->Node->getOperand(0).Val;
1492 if (DUNode->isOperand(right->Node))
1493 RBonus += 2;
1494 }
1495 if (!left->isTwoAddress && right->isTwoAddress) {
1496 SDNode *DUNode = right->Node->getOperand(0).Val;
1497 if (DUNode->isOperand(left->Node))
1498 LBonus += 2;
1499 }
1500 }
1501 if (LIsFloater)
1502 LBonus -= 2;
1503 if (RIsFloater)
1504 RBonus -= 2;
1505 if (left->NumSuccs == 1)
1506 LBonus += 2;
1507 if (right->NumSuccs == 1)
1508 RBonus += 2;
1509
1510 if (LPriority+LBonus < RPriority+RBonus)
1511 return true;
1512 else if (LPriority == RPriority)
1513 if (left->Depth < right->Depth)
1514 return true;
1515 else if (left->Depth == right->Depth)
1516 if (left->NumSuccsLeft > right->NumSuccsLeft)
1517 return true;
1518 else if (left->NumSuccsLeft == right->NumSuccsLeft)
1519 if (left->CycleBound > right->CycleBound)
1520 return true;
1521 return false;
1522}
1523
Evan Cheng6730f032007-01-08 23:55:53 +00001524/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +00001525/// Smaller number is the higher priority.
1526template<class SF>
Chris Lattner296a83c2007-02-01 04:55:59 +00001527unsigned TDRegReductionPriorityQueue<SF>::
1528CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +00001529 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001530 if (SethiUllmanNumber != 0)
1531 return SethiUllmanNumber;
1532
Evan Cheng8e136a92007-09-26 21:36:17 +00001533 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001534 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Evan Cheng961bbd32007-01-08 23:50:38 +00001535 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +00001536 else if (SU->NumSuccsLeft == 0)
1537 // If SU does not have a use, i.e. it doesn't produce a value that would
1538 // be consumed (e.g. store), then it terminates a chain of computation.
Chris Lattner296a83c2007-02-01 04:55:59 +00001539 // Give it a small SethiUllman number so it will be scheduled right before
1540 // its predecessors that it doesn't lengthen their live ranges.
Evan Cheng961bbd32007-01-08 23:50:38 +00001541 SethiUllmanNumber = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001542 else if (SU->NumPredsLeft == 0 &&
1543 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
Evan Cheng961bbd32007-01-08 23:50:38 +00001544 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +00001545 else {
1546 int Extra = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001547 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1548 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001549 if (I->isCtrl) continue; // ignore chain preds
1550 SUnit *PredSU = I->Dep;
Evan Cheng6730f032007-01-08 23:55:53 +00001551 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001552 if (PredSethiUllman > SethiUllmanNumber) {
1553 SethiUllmanNumber = PredSethiUllman;
1554 Extra = 0;
Evan Cheng0effc3a2007-09-19 01:38:40 +00001555 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
Evan Cheng5924bf72007-09-25 01:54:36 +00001556 ++Extra;
Evan Chengd38c22b2006-05-11 23:55:42 +00001557 }
1558
1559 SethiUllmanNumber += Extra;
1560 }
1561
1562 return SethiUllmanNumber;
1563}
1564
Evan Cheng6730f032007-01-08 23:55:53 +00001565/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1566/// scheduling units.
Evan Chengd38c22b2006-05-11 23:55:42 +00001567template<class SF>
Evan Cheng6730f032007-01-08 23:55:53 +00001568void TDRegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001569 SethiUllmanNumbers.assign(SUnits->size(), 0);
1570
1571 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +00001572 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001573}
1574
1575//===----------------------------------------------------------------------===//
1576// Public Constructor Functions
1577//===----------------------------------------------------------------------===//
1578
Jim Laskey03593f72006-08-01 18:29:48 +00001579llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1580 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +00001581 MachineBasicBlock *BB) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001582 const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo();
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001583 const TargetRegisterInfo *TRI = DAG->getTarget().getRegisterInfo();
Jim Laskey95eda5b2006-08-01 14:21:23 +00001584 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001585 new BURegReductionPriorityQueue<bu_ls_rr_sort>(TII, TRI));
Evan Chengd38c22b2006-05-11 23:55:42 +00001586}
1587
Jim Laskey03593f72006-08-01 18:29:48 +00001588llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1589 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +00001590 MachineBasicBlock *BB) {
Jim Laskey95eda5b2006-08-01 14:21:23 +00001591 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false,
Chris Lattner296a83c2007-02-01 04:55:59 +00001592 new TDRegReductionPriorityQueue<td_ls_rr_sort>());
Evan Chengd38c22b2006-05-11 23:55:42 +00001593}
1594