blob: b86492992c4ea6615f24c55562cac492e0569ed4 [file] [log] [blame]
Dan Gohmanee2e4032008-09-18 16:26:26 +00001//===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements a fast scheduler.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman343f0c02008-11-19 23:18:57 +000015#include "llvm/CodeGen/ScheduleDAGSDNodes.h"
Dan Gohmanee2e4032008-09-18 16:26:26 +000016#include "llvm/CodeGen/SchedulerRegistry.h"
17#include "llvm/Target/TargetRegisterInfo.h"
18#include "llvm/Target/TargetData.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/CommandLine.h"
27using namespace llvm;
28
29STATISTIC(NumUnfolds, "Number of nodes unfolded");
30STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengc29a56d2009-01-12 03:19:55 +000031STATISTIC(NumPRCopies, "Number of physical copies");
Dan Gohmanee2e4032008-09-18 16:26:26 +000032
33static RegisterScheduler
Dan Gohmanb8cab922008-10-14 20:25:08 +000034 fastDAGScheduler("fast", "Fast suboptimal list scheduling",
Dan Gohmanee2e4032008-09-18 16:26:26 +000035 createFastDAGScheduler);
36
37namespace {
38 /// FastPriorityQueue - A degenerate priority queue that considers
39 /// all nodes to have the same priority.
40 ///
41 struct VISIBILITY_HIDDEN FastPriorityQueue {
Dan Gohman086ec992008-09-23 18:50:48 +000042 SmallVector<SUnit *, 16> Queue;
Dan Gohmanee2e4032008-09-18 16:26:26 +000043
44 bool empty() const { return Queue.empty(); }
45
46 void push(SUnit *U) {
47 Queue.push_back(U);
48 }
49
50 SUnit *pop() {
51 if (empty()) return NULL;
52 SUnit *V = Queue.back();
53 Queue.pop_back();
54 return V;
55 }
56 };
57
58//===----------------------------------------------------------------------===//
59/// ScheduleDAGFast - The actual "fast" list scheduler implementation.
60///
Dan Gohman343f0c02008-11-19 23:18:57 +000061class VISIBILITY_HIDDEN ScheduleDAGFast : public ScheduleDAGSDNodes {
Dan Gohmanee2e4032008-09-18 16:26:26 +000062private:
63 /// AvailableQueue - The priority queue to use for the available SUnits.
64 FastPriorityQueue AvailableQueue;
65
Dan Gohman086ec992008-09-23 18:50:48 +000066 /// LiveRegDefs - A set of physical registers and their definition
Dan Gohmanee2e4032008-09-18 16:26:26 +000067 /// that are "live". These nodes must be scheduled before any other nodes that
68 /// modifies the registers can be scheduled.
Dan Gohman086ec992008-09-23 18:50:48 +000069 unsigned NumLiveRegs;
Dan Gohmanee2e4032008-09-18 16:26:26 +000070 std::vector<SUnit*> LiveRegDefs;
71 std::vector<unsigned> LiveRegCycles;
72
73public:
Dan Gohmana23b3b82008-11-13 21:21:28 +000074 ScheduleDAGFast(SelectionDAG *dag, MachineBasicBlock *bb,
Dan Gohmanee2e4032008-09-18 16:26:26 +000075 const TargetMachine &tm)
Dan Gohman343f0c02008-11-19 23:18:57 +000076 : ScheduleDAGSDNodes(dag, bb, tm) {}
Dan Gohmanee2e4032008-09-18 16:26:26 +000077
78 void Schedule();
79
Dan Gohman54e4c362008-12-09 22:54:47 +000080 /// AddPred - adds a predecessor edge to SUnit SU.
Dan Gohmanee2e4032008-09-18 16:26:26 +000081 /// This returns true if this is a new predecessor.
Dan Gohmanffa39122008-12-16 01:00:55 +000082 void AddPred(SUnit *SU, const SDep &D) {
83 SU->addPred(D);
Dan Gohman54e4c362008-12-09 22:54:47 +000084 }
Dan Gohmanee2e4032008-09-18 16:26:26 +000085
Dan Gohman54e4c362008-12-09 22:54:47 +000086 /// RemovePred - removes a predecessor edge from SUnit SU.
87 /// This returns true if an edge was removed.
Dan Gohmanffa39122008-12-16 01:00:55 +000088 void RemovePred(SUnit *SU, const SDep &D) {
89 SU->removePred(D);
Dan Gohman54e4c362008-12-09 22:54:47 +000090 }
Dan Gohmanee2e4032008-09-18 16:26:26 +000091
92private:
Dan Gohman54e4c362008-12-09 22:54:47 +000093 void ReleasePred(SUnit *SU, SDep *PredEdge);
Dan Gohmanee2e4032008-09-18 16:26:26 +000094 void ScheduleNodeBottomUp(SUnit*, unsigned);
95 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengc29a56d2009-01-12 03:19:55 +000096 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
97 const TargetRegisterClass*,
98 const TargetRegisterClass*,
99 SmallVector<SUnit*, 2>&);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000100 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
101 void ListScheduleBottomUp();
Dan Gohman3f237442008-12-16 03:25:46 +0000102
103 /// ForceUnitLatencies - The fast scheduler doesn't care about real latencies.
104 bool ForceUnitLatencies() const { return true; }
Dan Gohmanee2e4032008-09-18 16:26:26 +0000105};
106} // end anonymous namespace
107
108
109/// Schedule - Schedule the DAG using list scheduling.
110void ScheduleDAGFast::Schedule() {
111 DOUT << "********** List Scheduling **********\n";
112
Dan Gohman086ec992008-09-23 18:50:48 +0000113 NumLiveRegs = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000114 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
115 LiveRegCycles.resize(TRI->getNumRegs(), 0);
116
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000117 // Build the scheduling graph.
118 BuildSchedGraph();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000119
120 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman3cc62432008-11-18 02:06:40 +0000121 SUnits[su].dumpAll(this));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000122
123 // Execute the actual scheduling loop.
124 ListScheduleBottomUp();
125}
126
127//===----------------------------------------------------------------------===//
128// Bottom-Up Scheduling
129//===----------------------------------------------------------------------===//
130
131/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
132/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +0000133void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
134 SUnit *PredSU = PredEdge->getSUnit();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000135 --PredSU->NumSuccsLeft;
136
137#ifndef NDEBUG
138 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman2d093f32008-11-18 00:38:59 +0000139 cerr << "*** Scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000140 PredSU->dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000141 cerr << " has been released too many times!\n";
142 assert(0);
143 }
144#endif
145
146 if (PredSU->NumSuccsLeft == 0) {
147 PredSU->isAvailable = true;
148 AvailableQueue.push(PredSU);
149 }
150}
151
152/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
153/// count of its predecessors. If a predecessor pending count is zero, add it to
154/// the Available queue.
155void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
156 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman3cc62432008-11-18 02:06:40 +0000157 DEBUG(SU->dump(this));
Dan Gohman1256f5f2008-11-18 21:22:20 +0000158
Dan Gohman3f237442008-12-16 03:25:46 +0000159 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
160 SU->setHeightToAtLeast(CurCycle);
Dan Gohman1256f5f2008-11-18 21:22:20 +0000161 Sequence.push_back(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000162
163 // Bottom up: release predecessors
164 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
165 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000166 ReleasePred(SU, &*I);
167 if (I->isAssignedRegDep()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000168 // This is a physical register dependency and it's impossible or
169 // expensive to copy the register. Make sure nothing that can
170 // clobber the register is scheduled between the predecessor and
171 // this node.
Dan Gohman54e4c362008-12-09 22:54:47 +0000172 if (!LiveRegDefs[I->getReg()]) {
Dan Gohman086ec992008-09-23 18:50:48 +0000173 ++NumLiveRegs;
Dan Gohman54e4c362008-12-09 22:54:47 +0000174 LiveRegDefs[I->getReg()] = I->getSUnit();
175 LiveRegCycles[I->getReg()] = CurCycle;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000176 }
177 }
178 }
179
180 // Release all the implicit physical register defs that are live.
181 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
182 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000183 if (I->isAssignedRegDep()) {
Dan Gohman3f237442008-12-16 03:25:46 +0000184 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohman086ec992008-09-23 18:50:48 +0000185 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman54e4c362008-12-09 22:54:47 +0000186 assert(LiveRegDefs[I->getReg()] == SU &&
Dan Gohmanee2e4032008-09-18 16:26:26 +0000187 "Physical register dependency violated?");
Dan Gohman086ec992008-09-23 18:50:48 +0000188 --NumLiveRegs;
Dan Gohman54e4c362008-12-09 22:54:47 +0000189 LiveRegDefs[I->getReg()] = NULL;
190 LiveRegCycles[I->getReg()] = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000191 }
192 }
193 }
194
195 SU->isScheduled = true;
196}
197
Dan Gohmanee2e4032008-09-18 16:26:26 +0000198/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
199/// successors to the newly created node.
200SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohmand23e0f82008-11-13 23:24:17 +0000201 if (SU->getNode()->getFlaggedNode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000202 return NULL;
203
Dan Gohman550f5af2008-11-13 21:36:12 +0000204 SDNode *N = SU->getNode();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000205 if (!N)
206 return NULL;
207
208 SUnit *NewSU;
209 bool TryUnfold = false;
210 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
211 MVT VT = N->getValueType(i);
212 if (VT == MVT::Flag)
213 return NULL;
214 else if (VT == MVT::Other)
215 TryUnfold = true;
216 }
217 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
218 const SDValue &Op = N->getOperand(i);
219 MVT VT = Op.getNode()->getValueType(Op.getResNo());
220 if (VT == MVT::Flag)
221 return NULL;
222 }
223
224 if (TryUnfold) {
225 SmallVector<SDNode*, 2> NewNodes;
Dan Gohmana23b3b82008-11-13 21:21:28 +0000226 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000227 return NULL;
228
229 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
230 assert(NewNodes.size() == 2 && "Expected a load folding node!");
231
232 N = NewNodes[1];
233 SDNode *LoadNode = NewNodes[0];
234 unsigned NumVals = N->getNumValues();
Dan Gohman550f5af2008-11-13 21:36:12 +0000235 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000236 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman550f5af2008-11-13 21:36:12 +0000237 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
238 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohmana23b3b82008-11-13 21:21:28 +0000239 SDValue(LoadNode, 1));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000240
Dan Gohmancdb260d2008-11-19 23:39:02 +0000241 SUnit *NewSU = NewSUnit(N);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000242 assert(N->getNodeId() == -1 && "Node already inserted!");
243 N->setNodeId(NewSU->NodeNum);
244
245 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
246 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
247 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
248 NewSU->isTwoAddress = true;
249 break;
250 }
251 }
252 if (TID.isCommutable())
253 NewSU->isCommutable = true;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000254
255 // LoadNode may already exist. This can happen when there is another
256 // load from the same location and producing the same type of value
257 // but it has different alignment or volatileness.
258 bool isNewLoad = true;
259 SUnit *LoadSU;
260 if (LoadNode->getNodeId() != -1) {
261 LoadSU = &SUnits[LoadNode->getNodeId()];
262 isNewLoad = false;
263 } else {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000264 LoadSU = NewSUnit(LoadNode);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000265 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000266 }
267
Dan Gohman54e4c362008-12-09 22:54:47 +0000268 SDep ChainPred;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000269 SmallVector<SDep, 4> ChainSuccs;
270 SmallVector<SDep, 4> LoadPreds;
271 SmallVector<SDep, 4> NodePreds;
272 SmallVector<SDep, 4> NodeSuccs;
273 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
274 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000275 if (I->isCtrl())
276 ChainPred = *I;
277 else if (I->getSUnit()->getNode() &&
278 I->getSUnit()->getNode()->isOperandOf(LoadNode))
279 LoadPreds.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000280 else
Dan Gohman54e4c362008-12-09 22:54:47 +0000281 NodePreds.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000282 }
283 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
284 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000285 if (I->isCtrl())
286 ChainSuccs.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000287 else
Dan Gohman54e4c362008-12-09 22:54:47 +0000288 NodeSuccs.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000289 }
290
Dan Gohman54e4c362008-12-09 22:54:47 +0000291 if (ChainPred.getSUnit()) {
292 RemovePred(SU, ChainPred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000293 if (isNewLoad)
Dan Gohman54e4c362008-12-09 22:54:47 +0000294 AddPred(LoadSU, ChainPred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000295 }
296 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000297 const SDep &Pred = LoadPreds[i];
298 RemovePred(SU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000299 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000300 AddPred(LoadSU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000301 }
302 }
303 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000304 const SDep &Pred = NodePreds[i];
305 RemovePred(SU, Pred);
306 AddPred(NewSU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000307 }
308 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000309 SDep D = NodeSuccs[i];
310 SUnit *SuccDep = D.getSUnit();
311 D.setSUnit(SU);
312 RemovePred(SuccDep, D);
313 D.setSUnit(NewSU);
314 AddPred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000315 }
316 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000317 SDep D = ChainSuccs[i];
318 SUnit *SuccDep = D.getSUnit();
319 D.setSUnit(SU);
320 RemovePred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000321 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000322 D.setSUnit(LoadSU);
323 AddPred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000324 }
325 }
326 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000327 AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000328 }
329
330 ++NumUnfolds;
331
332 if (NewSU->NumSuccsLeft == 0) {
333 NewSU->isAvailable = true;
334 return NewSU;
335 }
336 SU = NewSU;
337 }
338
339 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Dan Gohmancdb260d2008-11-19 23:39:02 +0000340 NewSU = Clone(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000341
342 // New SUnit has the exact same predecessors.
343 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
344 I != E; ++I)
Dan Gohman3f237442008-12-16 03:25:46 +0000345 if (!I->isArtificial())
Dan Gohman54e4c362008-12-09 22:54:47 +0000346 AddPred(NewSU, *I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000347
348 // Only copy scheduled successors. Cut them from old node's successor
349 // list and move them over.
Dan Gohman54e4c362008-12-09 22:54:47 +0000350 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000351 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
352 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000353 if (I->isArtificial())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000354 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000355 SUnit *SuccSU = I->getSUnit();
356 if (SuccSU->isScheduled) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000357 SDep D = *I;
358 D.setSUnit(NewSU);
359 AddPred(SuccSU, D);
360 D.setSUnit(SU);
361 DelDeps.push_back(std::make_pair(SuccSU, D));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000362 }
363 }
Evan Chengc29a56d2009-01-12 03:19:55 +0000364 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000365 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000366
367 ++NumDups;
368 return NewSU;
369}
370
Evan Chengc29a56d2009-01-12 03:19:55 +0000371/// InsertCopiesAndMoveSuccs - Insert register copies and move all
372/// scheduled successors of the given SUnit to the last copy.
373void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000374 const TargetRegisterClass *DestRC,
375 const TargetRegisterClass *SrcRC,
376 SmallVector<SUnit*, 2> &Copies) {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000377 SUnit *CopyFromSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000378 CopyFromSU->CopySrcRC = SrcRC;
379 CopyFromSU->CopyDstRC = DestRC;
380
Dan Gohmancdb260d2008-11-19 23:39:02 +0000381 SUnit *CopyToSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000382 CopyToSU->CopySrcRC = DestRC;
383 CopyToSU->CopyDstRC = SrcRC;
384
385 // Only copy scheduled successors. Cut them from old node's successor
386 // list and move them over.
Dan Gohman54e4c362008-12-09 22:54:47 +0000387 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000388 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
389 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000390 if (I->isArtificial())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000391 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000392 SUnit *SuccSU = I->getSUnit();
393 if (SuccSU->isScheduled) {
394 SDep D = *I;
395 D.setSUnit(CopyToSU);
396 AddPred(SuccSU, D);
397 DelDeps.push_back(std::make_pair(SuccSU, *I));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000398 }
399 }
400 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000401 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000402 }
403
Dan Gohman54e4c362008-12-09 22:54:47 +0000404 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
405 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000406
407 Copies.push_back(CopyFromSU);
408 Copies.push_back(CopyToSU);
409
Evan Chengc29a56d2009-01-12 03:19:55 +0000410 ++NumPRCopies;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000411}
412
413/// getPhysicalRegisterVT - Returns the ValueType of the physical register
414/// definition of the specified node.
415/// FIXME: Move to SelectionDAG?
416static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
417 const TargetInstrInfo *TII) {
418 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
419 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
420 unsigned NumRes = TID.getNumDefs();
421 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
422 if (Reg == *ImpDef)
423 break;
424 ++NumRes;
425 }
426 return N->getValueType(NumRes);
427}
428
429/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
430/// scheduling of the given node to satisfy live physical register dependencies.
431/// If the specific node is the last one that's available to schedule, do
432/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
433bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
434 SmallVector<unsigned, 4> &LRegs){
Dan Gohman086ec992008-09-23 18:50:48 +0000435 if (NumLiveRegs == 0)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000436 return false;
437
438 SmallSet<unsigned, 4> RegAdded;
439 // If this node would clobber any "live" register, then it's not ready.
440 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
441 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000442 if (I->isAssignedRegDep()) {
443 unsigned Reg = I->getReg();
444 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000445 if (RegAdded.insert(Reg))
446 LRegs.push_back(Reg);
447 }
448 for (const unsigned *Alias = TRI->getAliasSet(Reg);
449 *Alias; ++Alias)
Dan Gohman54e4c362008-12-09 22:54:47 +0000450 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000451 if (RegAdded.insert(*Alias))
452 LRegs.push_back(*Alias);
453 }
454 }
455 }
456
Dan Gohmand23e0f82008-11-13 23:24:17 +0000457 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
458 if (!Node->isMachineOpcode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000459 continue;
460 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
461 if (!TID.ImplicitDefs)
462 continue;
463 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohman086ec992008-09-23 18:50:48 +0000464 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000465 if (RegAdded.insert(*Reg))
466 LRegs.push_back(*Reg);
467 }
468 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
469 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000470 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000471 if (RegAdded.insert(*Alias))
472 LRegs.push_back(*Alias);
473 }
474 }
475 }
476 return !LRegs.empty();
477}
478
479
480/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
481/// schedulers.
482void ScheduleDAGFast::ListScheduleBottomUp() {
483 unsigned CurCycle = 0;
484 // Add root to Available queue.
485 if (!SUnits.empty()) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000486 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohmanee2e4032008-09-18 16:26:26 +0000487 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
488 RootSU->isAvailable = true;
489 AvailableQueue.push(RootSU);
490 }
491
492 // While Available queue is not empty, grab the node with the highest
493 // priority. If it is not ready put it back. Schedule the node.
494 SmallVector<SUnit*, 4> NotReady;
495 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
496 Sequence.reserve(SUnits.size());
497 while (!AvailableQueue.empty()) {
498 bool Delayed = false;
499 LRegsMap.clear();
500 SUnit *CurSU = AvailableQueue.pop();
501 while (CurSU) {
Dan Gohmane93483d2008-11-17 19:52:36 +0000502 SmallVector<unsigned, 4> LRegs;
503 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
504 break;
505 Delayed = true;
506 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000507
508 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
509 NotReady.push_back(CurSU);
510 CurSU = AvailableQueue.pop();
511 }
512
513 // All candidates are delayed due to live physical reg dependencies.
514 // Try code duplication or inserting cross class copies
515 // to resolve it.
516 if (Delayed && !CurSU) {
517 if (!CurSU) {
518 // Try duplicating the nodes that produces these
519 // "expensive to copy" values to break the dependency. In case even
520 // that doesn't work, insert cross class copies.
521 SUnit *TrySU = NotReady[0];
522 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
523 assert(LRegs.size() == 1 && "Can't handle this yet!");
524 unsigned Reg = LRegs[0];
525 SUnit *LRDef = LiveRegDefs[Reg];
Evan Chengc29a56d2009-01-12 03:19:55 +0000526 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
527 const TargetRegisterClass *RC =
528 TRI->getPhysicalRegisterRegClass(Reg, VT);
529 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
530
531 // If cross copy register class is null, then it must be possible copy
532 // the value directly. Do not try duplicate the def.
533 SUnit *NewDef = 0;
534 if (DestRC)
535 NewDef = CopyAndMoveSuccessors(LRDef);
536 else
537 DestRC = RC;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000538 if (!NewDef) {
Evan Chengc29a56d2009-01-12 03:19:55 +0000539 // Issue copies, these can be expensive cross register class copies.
Dan Gohmanee2e4032008-09-18 16:26:26 +0000540 SmallVector<SUnit*, 2> Copies;
Evan Chengc29a56d2009-01-12 03:19:55 +0000541 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000542 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
543 << " to SU #" << Copies.front()->NodeNum << "\n";
Dan Gohman54e4c362008-12-09 22:54:47 +0000544 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
545 /*Reg=*/0, /*isNormalMemory=*/false,
546 /*isMustAlias=*/false, /*isArtificial=*/true));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000547 NewDef = Copies.back();
548 }
549
550 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
551 << " to SU #" << TrySU->NodeNum << "\n";
552 LiveRegDefs[Reg] = NewDef;
Dan Gohman54e4c362008-12-09 22:54:47 +0000553 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
554 /*Reg=*/0, /*isNormalMemory=*/false,
555 /*isMustAlias=*/false, /*isArtificial=*/true));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000556 TrySU->isAvailable = false;
557 CurSU = NewDef;
558 }
559
560 if (!CurSU) {
561 assert(false && "Unable to resolve live physical register dependencies!");
562 abort();
563 }
564 }
565
566 // Add the nodes that aren't ready back onto the available list.
567 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
568 NotReady[i]->isPending = false;
569 // May no longer be available due to backtracking.
570 if (NotReady[i]->isAvailable)
571 AvailableQueue.push(NotReady[i]);
572 }
573 NotReady.clear();
574
Dan Gohman47d1a212008-11-21 00:10:42 +0000575 if (CurSU)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000576 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000577 ++CurCycle;
578 }
579
580 // Reverse the order if it is bottom up.
581 std::reverse(Sequence.begin(), Sequence.end());
582
583
584#ifndef NDEBUG
585 // Verify that all SUnits were scheduled.
586 bool AnyNotSched = false;
587 unsigned DeadNodes = 0;
588 unsigned Noops = 0;
589 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
590 if (!SUnits[i].isScheduled) {
591 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
592 ++DeadNodes;
593 continue;
594 }
595 if (!AnyNotSched)
596 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000597 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000598 cerr << "has not been scheduled!\n";
599 AnyNotSched = true;
600 }
601 if (SUnits[i].NumSuccsLeft != 0) {
602 if (!AnyNotSched)
603 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000604 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000605 cerr << "has successors left!\n";
606 AnyNotSched = true;
607 }
608 }
609 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
610 if (!Sequence[i])
611 ++Noops;
612 assert(!AnyNotSched);
613 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
614 "The number of nodes scheduled doesn't match the expected number!");
615#endif
616}
617
618//===----------------------------------------------------------------------===//
619// Public Constructor Functions
620//===----------------------------------------------------------------------===//
621
622llvm::ScheduleDAG* llvm::createFastDAGScheduler(SelectionDAGISel *IS,
623 SelectionDAG *DAG,
Dan Gohman9b75b372008-11-11 17:50:47 +0000624 const TargetMachine *TM,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000625 MachineBasicBlock *BB, bool) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000626 return new ScheduleDAGFast(DAG, BB, *TM);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000627}