blob: 0575b41d1f554adbad4a8c93da899b70bf22f833 [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
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"
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 Chenge6f92252007-09-27 18:46:06 +000028#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000029#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000030#include "llvm/ADT/Statistic.h"
31#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000032#include <queue>
33#include "llvm/Support/CommandLine.h"
34using namespace llvm;
35
Evan Cheng1ec79b42007-09-27 07:09:03 +000036STATISTIC(NumBacktracks, "Number of times scheduler backtraced");
Evan Cheng79e97132007-10-05 01:39:18 +000037STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000038STATISTIC(NumDups, "Number of duplicated nodes");
39STATISTIC(NumCCCopies, "Number of cross class copies");
40
Jim Laskey95eda5b2006-08-01 14:21:23 +000041static RegisterScheduler
42 burrListDAGScheduler("list-burr",
43 " Bottom-up register reduction list scheduling",
44 createBURRListDAGScheduler);
45static RegisterScheduler
46 tdrListrDAGScheduler("list-tdrr",
47 " Top-down register reduction list scheduling",
48 createTDRRListDAGScheduler);
49
Evan Chengd38c22b2006-05-11 23:55:42 +000050namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000051//===----------------------------------------------------------------------===//
52/// ScheduleDAGRRList - The actual register reduction list scheduler
53/// implementation. This supports both top-down and bottom-up scheduling.
54///
Chris Lattnere097e6f2006-06-28 22:17:39 +000055class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG {
Evan Chengd38c22b2006-05-11 23:55:42 +000056private:
57 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
58 /// it is top-down.
59 bool isBottomUp;
60
61 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Cheng5924bf72007-09-25 01:54:36 +000062 ///a
Evan Chengd38c22b2006-05-11 23:55:42 +000063 SchedulingPriorityQueue *AvailableQueue;
64
Evan Cheng5924bf72007-09-25 01:54:36 +000065 /// LiveRegs / LiveRegDefs - A set of physical registers and their definition
66 /// that are "live". These nodes must be scheduled before any other nodes that
67 /// modifies the registers can be scheduled.
68 SmallSet<unsigned, 4> LiveRegs;
69 std::vector<SUnit*> LiveRegDefs;
70 std::vector<unsigned> LiveRegCycles;
71
Evan Chengd38c22b2006-05-11 23:55:42 +000072public:
73 ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb,
74 const TargetMachine &tm, bool isbottomup,
75 SchedulingPriorityQueue *availqueue)
76 : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup),
77 AvailableQueue(availqueue) {
78 }
79
80 ~ScheduleDAGRRList() {
81 delete AvailableQueue;
82 }
83
84 void Schedule();
85
86private:
Evan Cheng8e136a92007-09-26 21:36:17 +000087 void ReleasePred(SUnit*, bool, unsigned);
88 void ReleaseSucc(SUnit*, bool isChain, unsigned);
89 void CapturePred(SUnit*, SUnit*, bool);
90 void ScheduleNodeBottomUp(SUnit*, unsigned);
91 void ScheduleNodeTopDown(SUnit*, unsigned);
92 void UnscheduleNodeBottomUp(SUnit*);
93 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
94 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +000095 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +000096 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +000097 const TargetRegisterClass*,
98 SmallVector<SUnit*, 2>&);
99 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000100 void ListScheduleTopDown();
101 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000102 void CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000103};
104} // end anonymous namespace
105
106
107/// Schedule - Schedule the DAG using list scheduling.
108void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000109 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000110
111 LiveRegDefs.resize(MRI->getNumRegs(), NULL);
112 LiveRegCycles.resize(MRI->getNumRegs(), 0);
113
Evan Chengd38c22b2006-05-11 23:55:42 +0000114 // Build scheduling units.
115 BuildSchedUnits();
116
Evan Chengd38c22b2006-05-11 23:55:42 +0000117 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000118 SUnits[su].dumpAll(&DAG));
Evan Cheng47fbeda2006-10-14 08:34:06 +0000119 CalculateDepths();
120 CalculateHeights();
Evan Chengd38c22b2006-05-11 23:55:42 +0000121
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000122 AvailableQueue->initNodes(SUnitMap, SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000123
Evan Chengd38c22b2006-05-11 23:55:42 +0000124 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
125 if (isBottomUp)
126 ListScheduleBottomUp();
127 else
128 ListScheduleTopDown();
129
130 AvailableQueue->releaseState();
Dan Gohman54a187e2007-08-20 19:28:38 +0000131
Evan Cheng009f5f52006-05-25 08:37:31 +0000132 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000133
Bill Wendling22e978a2006-12-07 20:04:42 +0000134 DOUT << "*** Final schedule ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000135 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000136 DOUT << "\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000137
138 // Emit in scheduled order
139 EmitSchedule();
140}
141
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000142/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000143/// it is not the last use of its first operand, add it to the CommuteSet if
144/// possible. It will be commuted when it is translated to a MI.
145void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000146 SmallPtrSet<SUnit*, 4> OperandSeen;
Evan Chengafed73e2006-05-12 01:58:24 +0000147 for (unsigned i = Sequence.size()-1; i != 0; --i) { // Ignore first node.
148 SUnit *SU = Sequence[i];
Evan Cheng8e136a92007-09-26 21:36:17 +0000149 if (!SU || !SU->Node) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000150 if (SU->isCommutable) {
151 unsigned Opc = SU->Node->getTargetOpcode();
Evan Cheng100c8d62007-09-13 00:06:00 +0000152 unsigned NumRes = TII->getNumDefs(Opc);
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000153 unsigned NumOps = CountOperands(SU->Node);
154 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng67fc1412006-12-01 21:52:58 +0000155 if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000156 continue;
157
158 SDNode *OpN = SU->Node->getOperand(j).Val;
Evan Cheng5924bf72007-09-25 01:54:36 +0000159 SUnit *OpSU = SUnitMap[OpN][SU->InstanceNo];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000160 if (OpSU && OperandSeen.count(OpSU) == 1) {
161 // Ok, so SU is not the last use of OpSU, but SU is two-address so
162 // it will clobber OpSU. Try to commute SU if no other source operands
163 // are live below.
164 bool DoCommute = true;
165 for (unsigned k = 0; k < NumOps; ++k) {
166 if (k != j) {
167 OpN = SU->Node->getOperand(k).Val;
Evan Cheng5924bf72007-09-25 01:54:36 +0000168 OpSU = SUnitMap[OpN][SU->InstanceNo];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000169 if (OpSU && OperandSeen.count(OpSU) == 1) {
170 DoCommute = false;
171 break;
172 }
173 }
Evan Chengafed73e2006-05-12 01:58:24 +0000174 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000175 if (DoCommute)
176 CommuteSet.insert(SU->Node);
Evan Chengafed73e2006-05-12 01:58:24 +0000177 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000178
179 // Only look at the first use&def node for now.
180 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000181 }
182 }
183
Chris Lattnerd86418a2006-08-17 00:09:56 +0000184 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
185 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000186 if (!I->isCtrl)
187 OperandSeen.insert(I->Dep);
Evan Chengafed73e2006-05-12 01:58:24 +0000188 }
189 }
190}
Evan Chengd38c22b2006-05-11 23:55:42 +0000191
192//===----------------------------------------------------------------------===//
193// Bottom-Up Scheduling
194//===----------------------------------------------------------------------===//
195
Evan Chengd38c22b2006-05-11 23:55:42 +0000196/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000197/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000198void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain,
199 unsigned CurCycle) {
200 // FIXME: the distance between two nodes is not always == the predecessor's
201 // latency. For example, the reader can very well read the register written
202 // by the predecessor later than the issue cycle. It also depends on the
203 // interrupt model (drain vs. freeze).
204 PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
205
Evan Cheng038dcc52007-09-28 19:24:24 +0000206 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000207
208#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000209 if (PredSU->NumSuccsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000210 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000211 PredSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000212 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000213 assert(0);
214 }
215#endif
216
Evan Cheng038dcc52007-09-28 19:24:24 +0000217 if (PredSU->NumSuccsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000218 // EntryToken has to go last! Special case it here.
Evan Cheng8e136a92007-09-26 21:36:17 +0000219 if (!PredSU->Node || PredSU->Node->getOpcode() != ISD::EntryToken) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000220 PredSU->isAvailable = true;
221 AvailableQueue->push(PredSU);
222 }
223 }
224}
225
226/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
227/// count of its predecessors. If a predecessor pending count is zero, add it to
228/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000229void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000230 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000231 DEBUG(SU->dump(&DAG));
232 SU->Cycle = CurCycle;
233
234 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000235
236 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000237 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000238 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000239 ReleasePred(I->Dep, I->isCtrl, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000240 if (I->Cost < 0) {
241 // This is a physical register dependency and it's impossible or
242 // expensive to copy the register. Make sure nothing that can
243 // clobber the register is scheduled between the predecessor and
244 // this node.
245 if (LiveRegs.insert(I->Reg)) {
246 LiveRegDefs[I->Reg] = I->Dep;
247 LiveRegCycles[I->Reg] = CurCycle;
248 }
249 }
250 }
251
252 // Release all the implicit physical register defs that are live.
253 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
254 I != E; ++I) {
255 if (I->Cost < 0) {
256 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
257 LiveRegs.erase(I->Reg);
258 assert(LiveRegDefs[I->Reg] == SU &&
259 "Physical register dependency violated?");
260 LiveRegDefs[I->Reg] = NULL;
261 LiveRegCycles[I->Reg] = 0;
262 }
263 }
264 }
265
Evan Chengd38c22b2006-05-11 23:55:42 +0000266 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000267}
268
Evan Cheng5924bf72007-09-25 01:54:36 +0000269/// CapturePred - This does the opposite of ReleasePred. Since SU is being
270/// unscheduled, incrcease the succ left count of its predecessors. Remove
271/// them from AvailableQueue if necessary.
272void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
273 PredSU->CycleBound = 0;
274 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
275 I != E; ++I) {
276 if (I->Dep == SU)
277 continue;
278 PredSU->CycleBound = std::max(PredSU->CycleBound,
279 I->Dep->Cycle + PredSU->Latency);
280 }
281
282 if (PredSU->isAvailable) {
283 PredSU->isAvailable = false;
284 if (!PredSU->isPending)
285 AvailableQueue->remove(PredSU);
286 }
287
Evan Cheng038dcc52007-09-28 19:24:24 +0000288 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000289}
290
291/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
292/// its predecessor states to reflect the change.
293void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
294 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
295 DEBUG(SU->dump(&DAG));
296
297 AvailableQueue->UnscheduledNode(SU);
298
299 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
300 I != E; ++I) {
301 CapturePred(I->Dep, SU, I->isCtrl);
302 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
303 LiveRegs.erase(I->Reg);
304 assert(LiveRegDefs[I->Reg] == I->Dep &&
305 "Physical register dependency violated?");
306 LiveRegDefs[I->Reg] = NULL;
307 LiveRegCycles[I->Reg] = 0;
308 }
309 }
310
311 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
312 I != E; ++I) {
313 if (I->Cost < 0) {
314 if (LiveRegs.insert(I->Reg)) {
315 assert(!LiveRegDefs[I->Reg] &&
316 "Physical register dependency violated?");
317 LiveRegDefs[I->Reg] = SU;
318 }
319 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
320 LiveRegCycles[I->Reg] = I->Dep->Cycle;
321 }
322 }
323
324 SU->Cycle = 0;
325 SU->isScheduled = false;
326 SU->isAvailable = true;
327 AvailableQueue->push(SU);
328}
329
Evan Chengcfd5f822007-09-27 00:25:29 +0000330// FIXME: This is probably too slow!
331static void isReachable(SUnit *SU, SUnit *TargetSU,
332 SmallPtrSet<SUnit*, 32> &Visited, bool &Reached) {
333 if (Reached) return;
334 if (SU == TargetSU) {
335 Reached = true;
336 return;
337 }
338 if (!Visited.insert(SU)) return;
339
340 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); I != E;
341 ++I)
342 isReachable(I->Dep, TargetSU, Visited, Reached);
343}
344
345static bool isReachable(SUnit *SU, SUnit *TargetSU) {
346 SmallPtrSet<SUnit*, 32> Visited;
347 bool Reached = false;
348 isReachable(SU, TargetSU, Visited, Reached);
349 return Reached;
350}
351
352/// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
353/// create a cycle.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000354static bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
Evan Chengcfd5f822007-09-27 00:25:29 +0000355 if (isReachable(TargetSU, SU))
356 return true;
357 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
358 I != E; ++I)
359 if (I->Cost < 0 && isReachable(TargetSU, I->Dep))
360 return true;
361 return false;
362}
363
Evan Cheng8e136a92007-09-26 21:36:17 +0000364/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000365/// BTCycle in order to schedule a specific node. Returns the last unscheduled
366/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000367void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
368 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000369 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000370 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000371 OldSU = Sequence.back();
372 Sequence.pop_back();
373 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000374 // Don't try to remove SU from AvailableQueue.
375 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000376 UnscheduleNodeBottomUp(OldSU);
377 --CurCycle;
378 }
379
380
381 if (SU->isSucc(OldSU)) {
382 assert(false && "Something is wrong!");
383 abort();
384 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000385
386 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000387}
388
Evan Cheng5924bf72007-09-25 01:54:36 +0000389/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
390/// successors to the newly created node.
391SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Evan Cheng79e97132007-10-05 01:39:18 +0000392 if (SU->FlaggedNodes.size())
393 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000394
Evan Cheng79e97132007-10-05 01:39:18 +0000395 SDNode *N = SU->Node;
396 if (!N)
397 return NULL;
398
399 SUnit *NewSU;
400 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
401 if (N->getValueType(i) == MVT::Flag)
402 return NULL;
403 bool TryUnfold = false;
404 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
405 const SDOperand &Op = N->getOperand(i);
406 MVT::ValueType VT = Op.Val->getValueType(Op.ResNo);
407 if (VT == MVT::Flag)
408 return NULL;
409 else if (VT == MVT::Other)
410 TryUnfold = true;
411 }
412
413 if (TryUnfold) {
414 SmallVector<SDNode*, 4> NewNodes;
415 if (!MRI->unfoldMemoryOperand(DAG, N, NewNodes))
416 return NULL;
417
418 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
419 assert(NewNodes.size() == 2 && "Expected a load folding node!");
420
421 N = NewNodes[1];
422 SDNode *LoadNode = NewNodes[0];
423 std::vector<SDNode*> Deleted;
424 unsigned NumVals = N->getNumValues();
425 unsigned OldNumVals = SU->Node->getNumValues();
426 for (unsigned i = 0; i != NumVals; ++i)
427 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, i),
428 SDOperand(N, i), Deleted);
429 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, OldNumVals-1),
430 SDOperand(LoadNode, 1), Deleted);
431
432 SUnit *LoadSU = NewSUnit(LoadNode);
433 SUnit *NewSU = NewSUnit(N);
434 SUnitMap[LoadNode].push_back(LoadSU);
435 SUnitMap[N].push_back(NewSU);
436 const TargetInstrDescriptor *TID = &TII->get(LoadNode->getTargetOpcode());
437 for (unsigned i = 0; i != TID->numOperands; ++i) {
438 if (TID->getOperandConstraint(i, TOI::TIED_TO) != -1) {
439 LoadSU->isTwoAddress = true;
440 break;
441 }
442 }
443 if (TID->Flags & M_COMMUTABLE)
444 LoadSU->isCommutable = true;
445
446 TID = &TII->get(N->getTargetOpcode());
447 for (unsigned i = 0; i != TID->numOperands; ++i) {
448 if (TID->getOperandConstraint(i, TOI::TIED_TO) != -1) {
449 NewSU->isTwoAddress = true;
450 break;
451 }
452 }
453 if (TID->Flags & M_COMMUTABLE)
454 NewSU->isCommutable = true;
455
456 // FIXME: Calculate height / depth and propagate the changes?
457 LoadSU->Depth = NewSU->Depth = SU->Depth;
458 LoadSU->Height = NewSU->Height = SU->Height;
459 ComputeLatency(LoadSU);
460 ComputeLatency(NewSU);
461
462 SUnit *ChainPred = NULL;
463 SmallVector<SDep, 4> ChainSuccs;
464 SmallVector<SDep, 4> LoadPreds;
465 SmallVector<SDep, 4> NodePreds;
466 SmallVector<SDep, 4> NodeSuccs;
467 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
468 I != E; ++I) {
469 if (I->isCtrl)
470 ChainPred = I->Dep;
471 else if (I->Dep->Node && I->Dep->Node->isOperand(LoadNode))
472 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
473 else
474 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
475 }
476 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
477 I != E; ++I) {
478 if (I->isCtrl)
479 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
480 I->isCtrl, I->isSpecial));
481 else
482 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
483 I->isCtrl, I->isSpecial));
484 }
485
486 SU->removePred(ChainPred, true, false);
487 LoadSU->addPred(ChainPred, true, false);
488 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
489 SDep *Pred = &LoadPreds[i];
490 SU->removePred(Pred->Dep, Pred->isCtrl, Pred->isSpecial);
491 LoadSU->addPred(Pred->Dep, Pred->isCtrl, Pred->isSpecial,
492 Pred->Reg, Pred->Cost);
493 }
494 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
495 SDep *Pred = &NodePreds[i];
496 SU->removePred(Pred->Dep, Pred->isCtrl, Pred->isSpecial);
497 NewSU->addPred(Pred->Dep, Pred->isCtrl, Pred->isSpecial,
498 Pred->Reg, Pred->Cost);
499 }
500 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
501 SDep *Succ = &NodeSuccs[i];
502 Succ->Dep->removePred(SU, Succ->isCtrl, Succ->isSpecial);
503 Succ->Dep->addPred(NewSU, Succ->isCtrl, Succ->isSpecial,
504 Succ->Reg, Succ->Cost);
505 }
506 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
507 SDep *Succ = &ChainSuccs[i];
508 Succ->Dep->removePred(SU, Succ->isCtrl, Succ->isSpecial);
509 Succ->Dep->addPred(LoadSU, Succ->isCtrl, Succ->isSpecial,
510 Succ->Reg, Succ->Cost);
511 }
512 NewSU->addPred(LoadSU, false, false);
513
514 AvailableQueue->addNode(LoadSU);
515 AvailableQueue->addNode(NewSU);
516
517 ++NumUnfolds;
518
519 if (NewSU->NumSuccsLeft == 0) {
520 NewSU->isAvailable = true;
521 return NewSU;
522 } else
523 SU = NewSU;
524 }
525
526 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
527 NewSU = Clone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000528
529 // New SUnit has the exact same predecessors.
530 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
531 I != E; ++I)
532 if (!I->isSpecial) {
533 NewSU->addPred(I->Dep, I->isCtrl, false, I->Reg, I->Cost);
534 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
535 }
536
537 // Only copy scheduled successors. Cut them from old node's successor
538 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000539 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000540 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
541 I != E; ++I) {
542 if (I->isSpecial)
543 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000544 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000545 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Evan Cheng5924bf72007-09-25 01:54:36 +0000546 I->Dep->addPred(NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000547 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000548 }
549 }
550 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000551 SUnit *Succ = DelDeps[i].first;
552 bool isCtrl = DelDeps[i].second;
Evan Cheng5924bf72007-09-25 01:54:36 +0000553 Succ->removePred(SU, isCtrl, false);
554 }
555
556 AvailableQueue->updateNode(SU);
557 AvailableQueue->addNode(NewSU);
558
Evan Cheng1ec79b42007-09-27 07:09:03 +0000559 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000560 return NewSU;
561}
562
Evan Cheng1ec79b42007-09-27 07:09:03 +0000563/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
564/// and move all scheduled successors of the given SUnit to the last copy.
565void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
566 const TargetRegisterClass *DestRC,
567 const TargetRegisterClass *SrcRC,
568 SmallVector<SUnit*, 2> &Copies) {
Evan Cheng79e97132007-10-05 01:39:18 +0000569 abort();
Evan Cheng8e136a92007-09-26 21:36:17 +0000570 SUnit *CopyFromSU = NewSUnit(NULL);
571 CopyFromSU->CopySrcRC = SrcRC;
572 CopyFromSU->CopyDstRC = DestRC;
573 CopyFromSU->Depth = SU->Depth;
574 CopyFromSU->Height = SU->Height;
575
576 SUnit *CopyToSU = NewSUnit(NULL);
577 CopyToSU->CopySrcRC = DestRC;
578 CopyToSU->CopyDstRC = SrcRC;
579
580 // Only copy scheduled successors. Cut them from old node's successor
581 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000582 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000583 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
584 I != E; ++I) {
585 if (I->isSpecial)
586 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000587 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000588 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000589 I->Dep->addPred(CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000590 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000591 }
592 }
593 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000594 SUnit *Succ = DelDeps[i].first;
595 bool isCtrl = DelDeps[i].second;
Evan Cheng8e136a92007-09-26 21:36:17 +0000596 Succ->removePred(SU, isCtrl, false);
597 }
598
599 CopyFromSU->addPred(SU, false, false, Reg, -1);
600 CopyToSU->addPred(CopyFromSU, false, false, Reg, 1);
601
602 AvailableQueue->updateNode(SU);
603 AvailableQueue->addNode(CopyFromSU);
604 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000605 Copies.push_back(CopyFromSU);
606 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000607
Evan Cheng1ec79b42007-09-27 07:09:03 +0000608 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000609}
610
611/// getPhysicalRegisterVT - Returns the ValueType of the physical register
612/// definition of the specified node.
613/// FIXME: Move to SelectionDAG?
614static MVT::ValueType getPhysicalRegisterVT(SDNode *N, unsigned Reg,
615 const TargetInstrInfo *TII) {
616 const TargetInstrDescriptor &TID = TII->get(N->getTargetOpcode());
617 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
618 unsigned NumRes = TID.numDefs;
619 for (const unsigned *ImpDef = TID.ImplicitDefs; *ImpDef; ++ImpDef) {
620 if (Reg == *ImpDef)
621 break;
622 ++NumRes;
623 }
624 return N->getValueType(NumRes);
625}
626
Evan Cheng5924bf72007-09-25 01:54:36 +0000627/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
628/// scheduling of the given node to satisfy live physical register dependencies.
629/// If the specific node is the last one that's available to schedule, do
630/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000631bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
632 SmallVector<unsigned, 4> &LRegs){
Evan Cheng5924bf72007-09-25 01:54:36 +0000633 if (LiveRegs.empty())
634 return false;
635
Evan Chenge6f92252007-09-27 18:46:06 +0000636 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000637 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000638 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
639 I != E; ++I) {
640 if (I->Cost < 0) {
641 unsigned Reg = I->Reg;
Evan Chenge6f92252007-09-27 18:46:06 +0000642 if (LiveRegs.count(Reg) && LiveRegDefs[Reg] != I->Dep) {
643 if (RegAdded.insert(Reg))
644 LRegs.push_back(Reg);
645 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000646 for (const unsigned *Alias = MRI->getAliasSet(Reg);
647 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000648 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != I->Dep) {
649 if (RegAdded.insert(*Alias))
650 LRegs.push_back(*Alias);
651 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000652 }
653 }
654
655 for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) {
656 SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1];
Evan Cheng8e136a92007-09-26 21:36:17 +0000657 if (!Node || !Node->isTargetOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000658 continue;
659 const TargetInstrDescriptor &TID = TII->get(Node->getTargetOpcode());
660 if (!TID.ImplicitDefs)
661 continue;
662 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Evan Chenge6f92252007-09-27 18:46:06 +0000663 if (LiveRegs.count(*Reg) && LiveRegDefs[*Reg] != SU) {
664 if (RegAdded.insert(*Reg))
665 LRegs.push_back(*Reg);
666 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000667 for (const unsigned *Alias = MRI->getAliasSet(*Reg);
668 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000669 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != SU) {
670 if (RegAdded.insert(*Alias))
671 LRegs.push_back(*Alias);
672 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000673 }
674 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000675 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000676}
677
Evan Cheng1ec79b42007-09-27 07:09:03 +0000678
Evan Chengd38c22b2006-05-11 23:55:42 +0000679/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
680/// schedulers.
681void ScheduleDAGRRList::ListScheduleBottomUp() {
682 unsigned CurCycle = 0;
683 // Add root to Available queue.
Evan Cheng5924bf72007-09-25 01:54:36 +0000684 SUnit *RootSU = SUnitMap[DAG.getRoot().Val].front();
685 RootSU->isAvailable = true;
686 AvailableQueue->push(RootSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000687
688 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000689 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000690 SmallVector<SUnit*, 4> NotReady;
Evan Chengd38c22b2006-05-11 23:55:42 +0000691 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000692 bool Delayed = false;
693 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Evan Cheng5924bf72007-09-25 01:54:36 +0000694 SUnit *CurSU = AvailableQueue->pop();
695 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000696 if (CurSU->CycleBound <= CurCycle) {
697 SmallVector<unsigned, 4> LRegs;
698 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000699 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000700 Delayed = true;
701 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000702 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000703
704 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
705 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000706 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000707 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000708
709 // All candidates are delayed due to live physical reg dependencies.
710 // Try backtracking, code duplication, or inserting cross class copies
711 // to resolve it.
712 if (Delayed && !CurSU) {
713 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
714 SUnit *TrySU = NotReady[i];
715 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
716
717 // Try unscheduling up to the point where it's safe to schedule
718 // this node.
719 unsigned LiveCycle = CurCycle;
720 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
721 unsigned Reg = LRegs[j];
722 unsigned LCycle = LiveRegCycles[Reg];
723 LiveCycle = std::min(LiveCycle, LCycle);
724 }
725 SUnit *OldSU = Sequence[LiveCycle];
726 if (!WillCreateCycle(TrySU, OldSU)) {
727 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
728 // Force the current node to be scheduled before the node that
729 // requires the physical reg dep.
730 if (OldSU->isAvailable) {
731 OldSU->isAvailable = false;
732 AvailableQueue->remove(OldSU);
733 }
734 TrySU->addPred(OldSU, true, true);
735 // If one or more successors has been unscheduled, then the current
736 // node is no longer avaialable. Schedule a successor that's now
737 // available instead.
738 if (!TrySU->isAvailable)
739 CurSU = AvailableQueue->pop();
740 else {
741 CurSU = TrySU;
742 TrySU->isPending = false;
743 NotReady.erase(NotReady.begin()+i);
744 }
745 break;
746 }
747 }
748
749 if (!CurSU) {
750 // Can't backtrace. Try duplicating the nodes that produces these
751 // "expensive to copy" values to break the dependency. In case even
752 // that doesn't work, insert cross class copies.
753 SUnit *TrySU = NotReady[0];
754 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
755 assert(LRegs.size() == 1 && "Can't handle this yet!");
756 unsigned Reg = LRegs[0];
757 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +0000758 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
759 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000760 // Issue expensive cross register class copies.
761 MVT::ValueType VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII);
762 const TargetRegisterClass *RC =
763 MRI->getPhysicalRegisterRegClass(VT, Reg);
764 const TargetRegisterClass *DestRC = MRI->getCrossCopyRegClass(RC);
765 if (!DestRC) {
766 assert(false && "Don't know how to copy this physical register!");
767 abort();
768 }
769 SmallVector<SUnit*, 2> Copies;
770 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
771 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
772 << " to SU #" << Copies.front()->NodeNum << "\n";
773 TrySU->addPred(Copies.front(), true, true);
774 NewDef = Copies.back();
775 }
776
777 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
778 << " to SU #" << TrySU->NodeNum << "\n";
779 LiveRegDefs[Reg] = NewDef;
780 NewDef->addPred(TrySU, true, true);
781 TrySU->isAvailable = false;
782 CurSU = NewDef;
783 }
784
785 if (!CurSU) {
786 assert(false && "Unable to resolve live physical register dependencies!");
787 abort();
788 }
789 }
790
Evan Chengd38c22b2006-05-11 23:55:42 +0000791 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000792 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
793 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000794 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000795 if (NotReady[i]->isAvailable)
796 AvailableQueue->push(NotReady[i]);
797 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000798 NotReady.clear();
799
Evan Cheng5924bf72007-09-25 01:54:36 +0000800 if (!CurSU)
801 Sequence.push_back(0);
802 else {
803 ScheduleNodeBottomUp(CurSU, CurCycle);
804 Sequence.push_back(CurSU);
805 }
806 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000807 }
808
809 // Add entry node last
810 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000811 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front();
Evan Chengd38c22b2006-05-11 23:55:42 +0000812 Sequence.push_back(Entry);
813 }
814
815 // Reverse the order if it is bottom up.
816 std::reverse(Sequence.begin(), Sequence.end());
817
818
819#ifndef NDEBUG
820 // Verify that all SUnits were scheduled.
821 bool AnyNotSched = false;
822 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000823 if (SUnits[i].NumSuccsLeft != 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000824 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000825 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000826 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000827 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000828 AnyNotSched = true;
829 }
830 }
831 assert(!AnyNotSched);
832#endif
833}
834
835//===----------------------------------------------------------------------===//
836// Top-Down Scheduling
837//===----------------------------------------------------------------------===//
838
839/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000840/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000841void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain,
842 unsigned CurCycle) {
843 // FIXME: the distance between two nodes is not always == the predecessor's
844 // latency. For example, the reader can very well read the register written
845 // by the predecessor later than the issue cycle. It also depends on the
846 // interrupt model (drain vs. freeze).
847 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
848
Evan Cheng038dcc52007-09-28 19:24:24 +0000849 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000850
851#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000852 if (SuccSU->NumPredsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000853 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000854 SuccSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000855 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000856 assert(0);
857 }
858#endif
859
Evan Cheng038dcc52007-09-28 19:24:24 +0000860 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000861 SuccSU->isAvailable = true;
862 AvailableQueue->push(SuccSU);
863 }
864}
865
866
867/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
868/// count of its successors. If a successor pending count is zero, add it to
869/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000870void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000871 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000872 DEBUG(SU->dump(&DAG));
873 SU->Cycle = CurCycle;
874
875 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000876
877 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000878 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
879 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000880 ReleaseSucc(I->Dep, I->isCtrl, CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000881 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000882}
883
Dan Gohman54a187e2007-08-20 19:28:38 +0000884/// ListScheduleTopDown - The main loop of list scheduling for top-down
885/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000886void ScheduleDAGRRList::ListScheduleTopDown() {
887 unsigned CurCycle = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000888 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front();
Evan Chengd38c22b2006-05-11 23:55:42 +0000889
890 // All leaves to Available queue.
891 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
892 // It is available if it has no predecessors.
893 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
894 AvailableQueue->push(&SUnits[i]);
895 SUnits[i].isAvailable = true;
896 }
897 }
898
899 // Emit the entry node first.
900 ScheduleNodeTopDown(Entry, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000901 Sequence.push_back(Entry);
902 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000903
904 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000905 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +0000906 std::vector<SUnit*> NotReady;
Evan Chengd38c22b2006-05-11 23:55:42 +0000907 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000908 SUnit *CurSU = AvailableQueue->pop();
909 while (CurSU && CurSU->CycleBound > CurCycle) {
910 NotReady.push_back(CurSU);
911 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000912 }
913
914 // Add the nodes that aren't ready back onto the available list.
915 AvailableQueue->push_all(NotReady);
916 NotReady.clear();
917
Evan Cheng5924bf72007-09-25 01:54:36 +0000918 if (!CurSU)
919 Sequence.push_back(0);
920 else {
921 ScheduleNodeTopDown(CurSU, CurCycle);
922 Sequence.push_back(CurSU);
923 }
Evan Chengd12c97d2006-05-30 18:05:39 +0000924 CurCycle++;
Evan Chengd38c22b2006-05-11 23:55:42 +0000925 }
926
927
928#ifndef NDEBUG
929 // Verify that all SUnits were scheduled.
930 bool AnyNotSched = false;
931 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
932 if (!SUnits[i].isScheduled) {
933 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000934 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000935 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000936 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000937 AnyNotSched = true;
938 }
939 }
940 assert(!AnyNotSched);
941#endif
942}
943
944
945
946//===----------------------------------------------------------------------===//
947// RegReductionPriorityQueue Implementation
948//===----------------------------------------------------------------------===//
949//
950// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
951// to reduce register pressure.
952//
953namespace {
954 template<class SF>
955 class RegReductionPriorityQueue;
956
957 /// Sorting functions for the Available queue.
958 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
959 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
960 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
961 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
962
963 bool operator()(const SUnit* left, const SUnit* right) const;
964 };
965
966 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
967 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
968 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
969 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
970
971 bool operator()(const SUnit* left, const SUnit* right) const;
972 };
973} // end anonymous namespace
974
Evan Cheng961bbd32007-01-08 23:50:38 +0000975static inline bool isCopyFromLiveIn(const SUnit *SU) {
976 SDNode *N = SU->Node;
Evan Cheng8e136a92007-09-26 21:36:17 +0000977 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +0000978 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
979}
980
Evan Chengd38c22b2006-05-11 23:55:42 +0000981namespace {
982 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000983 class VISIBILITY_HIDDEN RegReductionPriorityQueue
984 : public SchedulingPriorityQueue {
Evan Chengd38c22b2006-05-11 23:55:42 +0000985 std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue;
986
987 public:
988 RegReductionPriorityQueue() :
989 Queue(SF(this)) {}
990
Evan Cheng5924bf72007-09-25 01:54:36 +0000991 virtual void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000992 std::vector<SUnit> &sunits) {}
Evan Cheng5924bf72007-09-25 01:54:36 +0000993
994 virtual void addNode(const SUnit *SU) {}
995
996 virtual void updateNode(const SUnit *SU) {}
997
Evan Chengd38c22b2006-05-11 23:55:42 +0000998 virtual void releaseState() {}
999
Evan Cheng6730f032007-01-08 23:55:53 +00001000 virtual unsigned getNodePriority(const SUnit *SU) const {
Evan Chengd38c22b2006-05-11 23:55:42 +00001001 return 0;
1002 }
1003
Evan Cheng5924bf72007-09-25 01:54:36 +00001004 unsigned size() const { return Queue.size(); }
1005
Evan Chengd38c22b2006-05-11 23:55:42 +00001006 bool empty() const { return Queue.empty(); }
1007
1008 void push(SUnit *U) {
1009 Queue.push(U);
1010 }
1011 void push_all(const std::vector<SUnit *> &Nodes) {
1012 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
1013 Queue.push(Nodes[i]);
1014 }
1015
1016 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001017 if (empty()) return NULL;
Evan Chengd38c22b2006-05-11 23:55:42 +00001018 SUnit *V = Queue.top();
1019 Queue.pop();
1020 return V;
1021 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001022
Evan Cheng5924bf72007-09-25 01:54:36 +00001023 /// remove - This is a really inefficient way to remove a node from a
1024 /// priority queue. We should roll our own heap to make this better or
1025 /// something.
1026 void remove(SUnit *SU) {
1027 std::vector<SUnit*> Temp;
1028
1029 assert(!Queue.empty() && "Not in queue!");
1030 while (Queue.top() != SU) {
1031 Temp.push_back(Queue.top());
1032 Queue.pop();
1033 assert(!Queue.empty() && "Not in queue!");
1034 }
1035
1036 // Remove the node from the PQ.
1037 Queue.pop();
1038
1039 // Add all the other nodes back.
1040 for (unsigned i = 0, e = Temp.size(); i != e; ++i)
1041 Queue.push(Temp[i]);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001042 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001043 };
1044
1045 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001046 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
1047 : public RegReductionPriorityQueue<SF> {
Evan Cheng5924bf72007-09-25 01:54:36 +00001048 // SUnitMap SDNode to SUnit mapping (n -> n).
1049 DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001050
Evan Chengd38c22b2006-05-11 23:55:42 +00001051 // SUnits - The SUnits for the current graph.
1052 const std::vector<SUnit> *SUnits;
1053
1054 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001055 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001056
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001057 const TargetInstrInfo *TII;
Evan Chengd38c22b2006-05-11 23:55:42 +00001058 public:
Dan Gohman54a187e2007-08-20 19:28:38 +00001059 explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii)
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001060 : TII(tii) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001061
Evan Cheng5924bf72007-09-25 01:54:36 +00001062 void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001063 std::vector<SUnit> &sunits) {
1064 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +00001065 SUnits = &sunits;
1066 // Add pseudo dependency edges for two-address nodes.
Evan Chengafed73e2006-05-12 01:58:24 +00001067 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001068 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001069 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001070 }
1071
Evan Cheng5924bf72007-09-25 01:54:36 +00001072 void addNode(const SUnit *SU) {
1073 SethiUllmanNumbers.resize(SUnits->size(), 0);
1074 CalcNodeSethiUllmanNumber(SU);
1075 }
1076
1077 void updateNode(const SUnit *SU) {
1078 SethiUllmanNumbers[SU->NodeNum] = 0;
1079 CalcNodeSethiUllmanNumber(SU);
1080 }
1081
Evan Chengd38c22b2006-05-11 23:55:42 +00001082 void releaseState() {
1083 SUnits = 0;
1084 SethiUllmanNumbers.clear();
1085 }
1086
Evan Cheng6730f032007-01-08 23:55:53 +00001087 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001088 assert(SU->NodeNum < SethiUllmanNumbers.size());
Evan Cheng8e136a92007-09-26 21:36:17 +00001089 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001090 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1091 // CopyFromReg should be close to its def because it restricts
1092 // allocation choices. But if it is a livein then perhaps we want it
1093 // closer to its uses so it can be coalesced.
1094 return 0xffff;
1095 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1096 // CopyToReg should be close to its uses to facilitate coalescing and
1097 // avoid spilling.
1098 return 0;
1099 else if (SU->NumSuccs == 0)
1100 // If SU does not have a use, i.e. it doesn't produce a value that would
1101 // be consumed (e.g. store), then it terminates a chain of computation.
1102 // Give it a large SethiUllman number so it will be scheduled right
1103 // before its predecessors that it doesn't lengthen their live ranges.
1104 return 0xffff;
1105 else if (SU->NumPreds == 0)
1106 // If SU does not have a def, schedule it close to its uses because it
1107 // does not lengthen any live ranges.
1108 return 0;
1109 else
1110 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001111 }
1112
1113 private:
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001114 bool canClobber(SUnit *SU, SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +00001115 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001116 void CalculateSethiUllmanNumbers();
1117 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001118 };
1119
1120
1121 template<class SF>
Dan Gohman54a187e2007-08-20 19:28:38 +00001122 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
1123 : public RegReductionPriorityQueue<SF> {
Evan Cheng5924bf72007-09-25 01:54:36 +00001124 // SUnitMap SDNode to SUnit mapping (n -> n).
1125 DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001126
Evan Chengd38c22b2006-05-11 23:55:42 +00001127 // SUnits - The SUnits for the current graph.
1128 const std::vector<SUnit> *SUnits;
1129
1130 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001131 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001132
1133 public:
1134 TDRegReductionPriorityQueue() {}
1135
Evan Cheng5924bf72007-09-25 01:54:36 +00001136 void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001137 std::vector<SUnit> &sunits) {
1138 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +00001139 SUnits = &sunits;
1140 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001141 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001142 }
1143
Evan Cheng5924bf72007-09-25 01:54:36 +00001144 void addNode(const SUnit *SU) {
1145 SethiUllmanNumbers.resize(SUnits->size(), 0);
1146 CalcNodeSethiUllmanNumber(SU);
1147 }
1148
1149 void updateNode(const SUnit *SU) {
1150 SethiUllmanNumbers[SU->NodeNum] = 0;
1151 CalcNodeSethiUllmanNumber(SU);
1152 }
1153
Evan Chengd38c22b2006-05-11 23:55:42 +00001154 void releaseState() {
1155 SUnits = 0;
1156 SethiUllmanNumbers.clear();
1157 }
1158
Evan Cheng6730f032007-01-08 23:55:53 +00001159 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001160 assert(SU->NodeNum < SethiUllmanNumbers.size());
1161 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001162 }
1163
1164 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001165 void CalculateSethiUllmanNumbers();
1166 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001167 };
1168}
1169
Evan Chengb9e3db62007-03-14 22:43:40 +00001170/// closestSucc - Returns the scheduled cycle of the successor which is
1171/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001172static unsigned closestSucc(const SUnit *SU) {
1173 unsigned MaxCycle = 0;
1174 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001175 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001176 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001177 // If there are bunch of CopyToRegs stacked up, they should be considered
1178 // to be at the same position.
Evan Cheng8e136a92007-09-26 21:36:17 +00001179 if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001180 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001181 if (Cycle > MaxCycle)
1182 MaxCycle = Cycle;
1183 }
Evan Cheng28748552007-03-13 23:25:11 +00001184 return MaxCycle;
1185}
1186
Evan Chengd38c22b2006-05-11 23:55:42 +00001187// Bottom up
1188bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
David Greene4c1e6f32007-06-29 03:42:23 +00001189 // There used to be a special tie breaker here that looked for
David Greene5b6f7552007-06-29 02:48:09 +00001190 // two-address instructions and preferred the instruction with a
1191 // def&use operand. The special case triggered diagnostics when
1192 // _GLIBCXX_DEBUG was enabled because it broke the strict weak
1193 // ordering that priority_queue requires. It didn't help much anyway
1194 // because AddPseudoTwoAddrDeps already covers many of the cases
1195 // where it would have applied. In addition, it's counter-intuitive
1196 // that a tie breaker would be the first thing attempted. There's a
1197 // "real" tie breaker below that is the operation of last resort.
1198 // The fact that the "special tie breaker" would trigger when there
1199 // wasn't otherwise a tie is what broke the strict weak ordering
1200 // constraint.
Evan Cheng99f2f792006-05-13 08:22:24 +00001201
Evan Cheng6730f032007-01-08 23:55:53 +00001202 unsigned LPriority = SPQ->getNodePriority(left);
1203 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng961bbd32007-01-08 23:50:38 +00001204 if (LPriority > RPriority)
Evan Chengd38c22b2006-05-11 23:55:42 +00001205 return true;
Evan Cheng28748552007-03-13 23:25:11 +00001206 else if (LPriority == RPriority) {
Dan Gohmane131e3a2007-04-26 19:40:56 +00001207 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
Evan Cheng28748552007-03-13 23:25:11 +00001208 // e.g.
1209 // t1 = op t2, c1
1210 // t3 = op t4, c2
1211 //
1212 // and the following instructions are both ready.
1213 // t2 = op c3
1214 // t4 = op c4
1215 //
1216 // Then schedule t2 = op first.
1217 // i.e.
1218 // t4 = op c4
1219 // t2 = op c3
1220 // t1 = op t2, c1
1221 // t3 = op t4, c2
1222 //
1223 // This creates more short live intervals.
1224 unsigned LDist = closestSucc(left);
1225 unsigned RDist = closestSucc(right);
1226 if (LDist < RDist)
Evan Chengd38c22b2006-05-11 23:55:42 +00001227 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +00001228 else if (LDist == RDist) {
Evan Chengf72693f2007-09-28 19:37:35 +00001229 if (left->Height > right->Height)
Evan Chengd38c22b2006-05-11 23:55:42 +00001230 return true;
Evan Chengf72693f2007-09-28 19:37:35 +00001231 else if (left->Height == right->Height)
1232 if (left->Depth < right->Depth)
Evan Cheng99f2f792006-05-13 08:22:24 +00001233 return true;
Evan Chengf72693f2007-09-28 19:37:35 +00001234 else if (left->Depth == right->Depth)
1235 if (left->CycleBound > right->CycleBound)
Evan Cheng28748552007-03-13 23:25:11 +00001236 return true;
Evan Chengb9e3db62007-03-14 22:43:40 +00001237 }
Evan Cheng28748552007-03-13 23:25:11 +00001238 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001239 return false;
1240}
1241
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001242template<class SF>
1243bool BURegReductionPriorityQueue<SF>::canClobber(SUnit *SU, SUnit *Op) {
1244 if (SU->isTwoAddress) {
1245 unsigned Opc = SU->Node->getTargetOpcode();
Evan Cheng100c8d62007-09-13 00:06:00 +00001246 unsigned NumRes = TII->getNumDefs(Opc);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001247 unsigned NumOps = ScheduleDAG::CountOperands(SU->Node);
1248 for (unsigned i = 0; i != NumOps; ++i) {
Evan Cheng67fc1412006-12-01 21:52:58 +00001249 if (TII->getOperandConstraint(Opc, i+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001250 SDNode *DU = SU->Node->getOperand(i).Val;
Evan Cheng5924bf72007-09-25 01:54:36 +00001251 if (Op == (*SUnitMap)[DU][SU->InstanceNo])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001252 return true;
1253 }
1254 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001255 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001256 return false;
1257}
1258
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001259
Evan Chenga5e595d2007-09-28 22:32:30 +00001260/// hasCopyToRegUse - Return true if SU has a value successor that is a
1261/// CopyToReg node.
1262static bool hasCopyToRegUse(SUnit *SU) {
1263 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1264 I != E; ++I) {
1265 if (I->isCtrl) continue;
1266 SUnit *SuccSU = I->Dep;
1267 if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg)
1268 return true;
1269 }
1270 return false;
1271}
1272
Evan Chengd38c22b2006-05-11 23:55:42 +00001273/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1274/// it as a def&use operand. Add a pseudo control edge from it to the other
1275/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001276/// first (lower in the schedule). If both nodes are two-address, favor the
1277/// one that has a CopyToReg use (more likely to be a loop induction update).
1278/// If both are two-address, but one is commutable while the other is not
1279/// commutable, favor the one that's not commutable.
Evan Chengd38c22b2006-05-11 23:55:42 +00001280template<class SF>
1281void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001282 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1283 SUnit *SU = (SUnit *)&((*SUnits)[i]);
1284 if (!SU->isTwoAddress)
1285 continue;
1286
1287 SDNode *Node = SU->Node;
Evan Chenga5e595d2007-09-28 22:32:30 +00001288 if (!Node || !Node->isTargetOpcode() || SU->FlaggedNodes.size() > 0)
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001289 continue;
1290
1291 unsigned Opc = Node->getTargetOpcode();
Evan Cheng100c8d62007-09-13 00:06:00 +00001292 unsigned NumRes = TII->getNumDefs(Opc);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001293 unsigned NumOps = ScheduleDAG::CountOperands(Node);
1294 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng67fc1412006-12-01 21:52:58 +00001295 if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001296 SDNode *DU = SU->Node->getOperand(j).Val;
Evan Cheng5924bf72007-09-25 01:54:36 +00001297 SUnit *DUSU = (*SUnitMap)[DU][SU->InstanceNo];
Evan Chengf24d15f2006-11-06 21:33:46 +00001298 if (!DUSU) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001299 for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end();
1300 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001301 if (I->isCtrl) continue;
1302 SUnit *SuccSU = I->Dep;
Evan Cheng5924bf72007-09-25 01:54:36 +00001303 // Don't constraint nodes with implicit defs. It can create cycles
1304 // plus it may increase register pressures.
Evan Chenga5e595d2007-09-28 22:32:30 +00001305 if (SuccSU == SU || SuccSU->hasPhysRegDefs)
Evan Cheng5924bf72007-09-25 01:54:36 +00001306 continue;
1307 // Be conservative. Ignore if nodes aren't at the same depth.
1308 if (SuccSU->Depth != SU->Depth)
1309 continue;
1310 if ((!canClobber(SuccSU, DUSU) ||
Evan Chenga5e595d2007-09-28 22:32:30 +00001311 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
Evan Cheng5924bf72007-09-25 01:54:36 +00001312 (!SU->isCommutable && SuccSU->isCommutable)) &&
1313 !isReachable(SuccSU, SU)) {
1314 DOUT << "Adding an edge from SU # " << SU->NodeNum
1315 << " to SU #" << SuccSU->NodeNum << "\n";
1316 SU->addPred(SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001317 }
1318 }
1319 }
1320 }
1321 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001322}
1323
Evan Cheng6730f032007-01-08 23:55:53 +00001324/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +00001325/// Smaller number is the higher priority.
1326template<class SF>
Chris Lattner296a83c2007-02-01 04:55:59 +00001327unsigned BURegReductionPriorityQueue<SF>::
1328CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +00001329 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001330 if (SethiUllmanNumber != 0)
1331 return SethiUllmanNumber;
1332
Evan Cheng961bbd32007-01-08 23:50:38 +00001333 unsigned Extra = 0;
1334 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1335 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001336 if (I->isCtrl) continue; // ignore chain preds
1337 SUnit *PredSU = I->Dep;
Evan Cheng6730f032007-01-08 23:55:53 +00001338 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Cheng961bbd32007-01-08 23:50:38 +00001339 if (PredSethiUllman > SethiUllmanNumber) {
1340 SethiUllmanNumber = PredSethiUllman;
1341 Extra = 0;
Evan Cheng0effc3a2007-09-19 01:38:40 +00001342 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
Evan Cheng5924bf72007-09-25 01:54:36 +00001343 ++Extra;
Evan Chengd38c22b2006-05-11 23:55:42 +00001344 }
Evan Cheng961bbd32007-01-08 23:50:38 +00001345
1346 SethiUllmanNumber += Extra;
1347
1348 if (SethiUllmanNumber == 0)
1349 SethiUllmanNumber = 1;
Evan Chengd38c22b2006-05-11 23:55:42 +00001350
1351 return SethiUllmanNumber;
1352}
1353
Evan Cheng6730f032007-01-08 23:55:53 +00001354/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1355/// scheduling units.
Evan Chengd38c22b2006-05-11 23:55:42 +00001356template<class SF>
Evan Cheng6730f032007-01-08 23:55:53 +00001357void BURegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001358 SethiUllmanNumbers.assign(SUnits->size(), 0);
1359
1360 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +00001361 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001362}
1363
1364static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) {
1365 unsigned Sum = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001366 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1367 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001368 SUnit *SuccSU = I->Dep;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001369 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1370 EE = SuccSU->Preds.end(); II != EE; ++II) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001371 SUnit *PredSU = II->Dep;
Evan Chengd38c22b2006-05-11 23:55:42 +00001372 if (!PredSU->isScheduled)
Evan Cheng5924bf72007-09-25 01:54:36 +00001373 ++Sum;
Evan Chengd38c22b2006-05-11 23:55:42 +00001374 }
1375 }
1376
1377 return Sum;
1378}
1379
1380
1381// Top down
1382bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001383 unsigned LPriority = SPQ->getNodePriority(left);
1384 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng8e136a92007-09-26 21:36:17 +00001385 bool LIsTarget = left->Node && left->Node->isTargetOpcode();
1386 bool RIsTarget = right->Node && right->Node->isTargetOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001387 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1388 bool RIsFloater = RIsTarget && right->NumPreds == 0;
1389 unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0;
1390 unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0;
1391
1392 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1393 return false;
1394 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1395 return true;
1396
1397 // Special tie breaker: if two nodes share a operand, the one that use it
1398 // as a def&use operand is preferred.
1399 if (LIsTarget && RIsTarget) {
1400 if (left->isTwoAddress && !right->isTwoAddress) {
1401 SDNode *DUNode = left->Node->getOperand(0).Val;
1402 if (DUNode->isOperand(right->Node))
1403 RBonus += 2;
1404 }
1405 if (!left->isTwoAddress && right->isTwoAddress) {
1406 SDNode *DUNode = right->Node->getOperand(0).Val;
1407 if (DUNode->isOperand(left->Node))
1408 LBonus += 2;
1409 }
1410 }
1411 if (LIsFloater)
1412 LBonus -= 2;
1413 if (RIsFloater)
1414 RBonus -= 2;
1415 if (left->NumSuccs == 1)
1416 LBonus += 2;
1417 if (right->NumSuccs == 1)
1418 RBonus += 2;
1419
1420 if (LPriority+LBonus < RPriority+RBonus)
1421 return true;
1422 else if (LPriority == RPriority)
1423 if (left->Depth < right->Depth)
1424 return true;
1425 else if (left->Depth == right->Depth)
1426 if (left->NumSuccsLeft > right->NumSuccsLeft)
1427 return true;
1428 else if (left->NumSuccsLeft == right->NumSuccsLeft)
1429 if (left->CycleBound > right->CycleBound)
1430 return true;
1431 return false;
1432}
1433
Evan Cheng6730f032007-01-08 23:55:53 +00001434/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +00001435/// Smaller number is the higher priority.
1436template<class SF>
Chris Lattner296a83c2007-02-01 04:55:59 +00001437unsigned TDRegReductionPriorityQueue<SF>::
1438CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +00001439 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001440 if (SethiUllmanNumber != 0)
1441 return SethiUllmanNumber;
1442
Evan Cheng8e136a92007-09-26 21:36:17 +00001443 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001444 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Evan Cheng961bbd32007-01-08 23:50:38 +00001445 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +00001446 else if (SU->NumSuccsLeft == 0)
1447 // If SU does not have a use, i.e. it doesn't produce a value that would
1448 // be consumed (e.g. store), then it terminates a chain of computation.
Chris Lattner296a83c2007-02-01 04:55:59 +00001449 // Give it a small SethiUllman number so it will be scheduled right before
1450 // its predecessors that it doesn't lengthen their live ranges.
Evan Cheng961bbd32007-01-08 23:50:38 +00001451 SethiUllmanNumber = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001452 else if (SU->NumPredsLeft == 0 &&
1453 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
Evan Cheng961bbd32007-01-08 23:50:38 +00001454 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +00001455 else {
1456 int Extra = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001457 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1458 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001459 if (I->isCtrl) continue; // ignore chain preds
1460 SUnit *PredSU = I->Dep;
Evan Cheng6730f032007-01-08 23:55:53 +00001461 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001462 if (PredSethiUllman > SethiUllmanNumber) {
1463 SethiUllmanNumber = PredSethiUllman;
1464 Extra = 0;
Evan Cheng0effc3a2007-09-19 01:38:40 +00001465 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
Evan Cheng5924bf72007-09-25 01:54:36 +00001466 ++Extra;
Evan Chengd38c22b2006-05-11 23:55:42 +00001467 }
1468
1469 SethiUllmanNumber += Extra;
1470 }
1471
1472 return SethiUllmanNumber;
1473}
1474
Evan Cheng6730f032007-01-08 23:55:53 +00001475/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1476/// scheduling units.
Evan Chengd38c22b2006-05-11 23:55:42 +00001477template<class SF>
Evan Cheng6730f032007-01-08 23:55:53 +00001478void TDRegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001479 SethiUllmanNumbers.assign(SUnits->size(), 0);
1480
1481 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +00001482 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001483}
1484
1485//===----------------------------------------------------------------------===//
1486// Public Constructor Functions
1487//===----------------------------------------------------------------------===//
1488
Jim Laskey03593f72006-08-01 18:29:48 +00001489llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1490 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +00001491 MachineBasicBlock *BB) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001492 const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo();
Jim Laskey95eda5b2006-08-01 14:21:23 +00001493 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001494 new BURegReductionPriorityQueue<bu_ls_rr_sort>(TII));
Evan Chengd38c22b2006-05-11 23:55:42 +00001495}
1496
Jim Laskey03593f72006-08-01 18:29:48 +00001497llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1498 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +00001499 MachineBasicBlock *BB) {
Jim Laskey95eda5b2006-08-01 14:21:23 +00001500 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false,
Chris Lattner296a83c2007-02-01 04:55:59 +00001501 new TDRegReductionPriorityQueue<td_ls_rr_sort>());
Evan Chengd38c22b2006-05-11 23:55:42 +00001502}
1503