blob: 0af03580f6641e3cf7f5df8c942fb26fc7949def [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"
15#include "llvm/CodeGen/ScheduleDAG.h"
16#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");
31STATISTIC(NumCCCopies, "Number of cross class copies");
32
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///
61class VISIBILITY_HIDDEN ScheduleDAGFast : public ScheduleDAG {
62private:
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)
76 : ScheduleDAG(dag, bb, tm) {}
77
78 void Schedule();
79
80 /// AddPred - This adds the specified node X as a predecessor of
81 /// the current node Y if not already.
82 /// This returns true if this is a new predecessor.
83 bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
84 unsigned PhyReg = 0, int Cost = 1);
85
86 /// RemovePred - This removes the specified node N from the predecessors of
87 /// the current node M.
88 bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isSpecial);
89
90private:
Dan Gohman2d093f32008-11-18 00:38:59 +000091 void ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain);
Dan Gohmanee2e4032008-09-18 16:26:26 +000092 void ScheduleNodeBottomUp(SUnit*, unsigned);
93 SUnit *CopyAndMoveSuccessors(SUnit*);
94 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
95 const TargetRegisterClass*,
96 const TargetRegisterClass*,
97 SmallVector<SUnit*, 2>&);
98 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
99 void ListScheduleBottomUp();
100
101 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
102 SUnit *CreateNewSUnit(SDNode *N) {
103 SUnit *NewNode = NewSUnit(N);
104 return NewNode;
105 }
106
107 /// CreateClone - Creates a new SUnit from an existing one.
108 SUnit *CreateClone(SUnit *N) {
109 SUnit *NewNode = Clone(N);
110 return NewNode;
111 }
112};
113} // end anonymous namespace
114
115
116/// Schedule - Schedule the DAG using list scheduling.
117void ScheduleDAGFast::Schedule() {
118 DOUT << "********** List Scheduling **********\n";
119
Dan Gohman086ec992008-09-23 18:50:48 +0000120 NumLiveRegs = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000121 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
122 LiveRegCycles.resize(TRI->getNumRegs(), 0);
123
124 // Build scheduling units.
125 BuildSchedUnits();
126
127 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman3cc62432008-11-18 02:06:40 +0000128 SUnits[su].dumpAll(this));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000129
130 // Execute the actual scheduling loop.
131 ListScheduleBottomUp();
132}
133
134//===----------------------------------------------------------------------===//
135// Bottom-Up Scheduling
136//===----------------------------------------------------------------------===//
137
138/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
139/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d093f32008-11-18 00:38:59 +0000140void ScheduleDAGFast::ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000141 --PredSU->NumSuccsLeft;
142
143#ifndef NDEBUG
144 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman2d093f32008-11-18 00:38:59 +0000145 cerr << "*** Scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000146 PredSU->dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000147 cerr << " has been released too many times!\n";
148 assert(0);
149 }
150#endif
151
152 if (PredSU->NumSuccsLeft == 0) {
153 PredSU->isAvailable = true;
154 AvailableQueue.push(PredSU);
155 }
156}
157
158/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
159/// count of its predecessors. If a predecessor pending count is zero, add it to
160/// the Available queue.
161void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
162 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman3cc62432008-11-18 02:06:40 +0000163 DEBUG(SU->dump(this));
Dan Gohman1256f5f2008-11-18 21:22:20 +0000164
Dan Gohmanee2e4032008-09-18 16:26:26 +0000165 SU->Cycle = CurCycle;
Dan Gohman1256f5f2008-11-18 21:22:20 +0000166 Sequence.push_back(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000167
168 // Bottom up: release predecessors
169 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
170 I != E; ++I) {
Dan Gohman2d093f32008-11-18 00:38:59 +0000171 ReleasePred(SU, I->Dep, I->isCtrl);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000172 if (I->Cost < 0) {
173 // This is a physical register dependency and it's impossible or
174 // expensive to copy the register. Make sure nothing that can
175 // clobber the register is scheduled between the predecessor and
176 // this node.
Dan Gohman086ec992008-09-23 18:50:48 +0000177 if (!LiveRegDefs[I->Reg]) {
178 ++NumLiveRegs;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000179 LiveRegDefs[I->Reg] = I->Dep;
180 LiveRegCycles[I->Reg] = CurCycle;
181 }
182 }
183 }
184
185 // Release all the implicit physical register defs that are live.
186 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
187 I != E; ++I) {
188 if (I->Cost < 0) {
189 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
Dan Gohman086ec992008-09-23 18:50:48 +0000190 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohmanee2e4032008-09-18 16:26:26 +0000191 assert(LiveRegDefs[I->Reg] == SU &&
192 "Physical register dependency violated?");
Dan Gohman086ec992008-09-23 18:50:48 +0000193 --NumLiveRegs;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000194 LiveRegDefs[I->Reg] = NULL;
195 LiveRegCycles[I->Reg] = 0;
196 }
197 }
198 }
199
200 SU->isScheduled = true;
201}
202
203/// AddPred - adds an edge from SUnit X to SUnit Y.
204bool ScheduleDAGFast::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
205 unsigned PhyReg, int Cost) {
206 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
207}
208
209/// RemovePred - This removes the specified node N from the predecessors of
210/// the current node M.
211bool ScheduleDAGFast::RemovePred(SUnit *M, SUnit *N,
212 bool isCtrl, bool isSpecial) {
213 return M->removePred(N, isCtrl, isSpecial);
214}
215
216/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
217/// successors to the newly created node.
218SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohmand23e0f82008-11-13 23:24:17 +0000219 if (SU->getNode()->getFlaggedNode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000220 return NULL;
221
Dan Gohman550f5af2008-11-13 21:36:12 +0000222 SDNode *N = SU->getNode();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000223 if (!N)
224 return NULL;
225
226 SUnit *NewSU;
227 bool TryUnfold = false;
228 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
229 MVT VT = N->getValueType(i);
230 if (VT == MVT::Flag)
231 return NULL;
232 else if (VT == MVT::Other)
233 TryUnfold = true;
234 }
235 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
236 const SDValue &Op = N->getOperand(i);
237 MVT VT = Op.getNode()->getValueType(Op.getResNo());
238 if (VT == MVT::Flag)
239 return NULL;
240 }
241
242 if (TryUnfold) {
243 SmallVector<SDNode*, 2> NewNodes;
Dan Gohmana23b3b82008-11-13 21:21:28 +0000244 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000245 return NULL;
246
247 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
248 assert(NewNodes.size() == 2 && "Expected a load folding node!");
249
250 N = NewNodes[1];
251 SDNode *LoadNode = NewNodes[0];
252 unsigned NumVals = N->getNumValues();
Dan Gohman550f5af2008-11-13 21:36:12 +0000253 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000254 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman550f5af2008-11-13 21:36:12 +0000255 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
256 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohmana23b3b82008-11-13 21:21:28 +0000257 SDValue(LoadNode, 1));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000258
259 SUnit *NewSU = CreateNewSUnit(N);
260 assert(N->getNodeId() == -1 && "Node already inserted!");
261 N->setNodeId(NewSU->NodeNum);
262
263 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
264 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
265 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
266 NewSU->isTwoAddress = true;
267 break;
268 }
269 }
270 if (TID.isCommutable())
271 NewSU->isCommutable = true;
272 // FIXME: Calculate height / depth and propagate the changes?
273 NewSU->Depth = SU->Depth;
274 NewSU->Height = SU->Height;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000275
276 // LoadNode may already exist. This can happen when there is another
277 // load from the same location and producing the same type of value
278 // but it has different alignment or volatileness.
279 bool isNewLoad = true;
280 SUnit *LoadSU;
281 if (LoadNode->getNodeId() != -1) {
282 LoadSU = &SUnits[LoadNode->getNodeId()];
283 isNewLoad = false;
284 } else {
285 LoadSU = CreateNewSUnit(LoadNode);
286 LoadNode->setNodeId(LoadSU->NodeNum);
287
288 LoadSU->Depth = SU->Depth;
289 LoadSU->Height = SU->Height;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000290 }
291
292 SUnit *ChainPred = NULL;
293 SmallVector<SDep, 4> ChainSuccs;
294 SmallVector<SDep, 4> LoadPreds;
295 SmallVector<SDep, 4> NodePreds;
296 SmallVector<SDep, 4> NodeSuccs;
297 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
298 I != E; ++I) {
299 if (I->isCtrl)
300 ChainPred = I->Dep;
Dan Gohman550f5af2008-11-13 21:36:12 +0000301 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000302 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
303 else
304 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
305 }
306 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
307 I != E; ++I) {
308 if (I->isCtrl)
309 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
310 I->isCtrl, I->isSpecial));
311 else
312 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
313 I->isCtrl, I->isSpecial));
314 }
315
316 if (ChainPred) {
317 RemovePred(SU, ChainPred, true, false);
318 if (isNewLoad)
319 AddPred(LoadSU, ChainPred, true, false);
320 }
321 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
322 SDep *Pred = &LoadPreds[i];
323 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
324 if (isNewLoad) {
325 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
326 Pred->Reg, Pred->Cost);
327 }
328 }
329 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
330 SDep *Pred = &NodePreds[i];
331 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
332 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
333 Pred->Reg, Pred->Cost);
334 }
335 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
336 SDep *Succ = &NodeSuccs[i];
337 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
338 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
339 Succ->Reg, Succ->Cost);
340 }
341 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
342 SDep *Succ = &ChainSuccs[i];
343 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
344 if (isNewLoad) {
345 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
346 Succ->Reg, Succ->Cost);
347 }
348 }
349 if (isNewLoad) {
350 AddPred(NewSU, LoadSU, false, false);
351 }
352
353 ++NumUnfolds;
354
355 if (NewSU->NumSuccsLeft == 0) {
356 NewSU->isAvailable = true;
357 return NewSU;
358 }
359 SU = NewSU;
360 }
361
362 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
363 NewSU = CreateClone(SU);
364
365 // New SUnit has the exact same predecessors.
366 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
367 I != E; ++I)
368 if (!I->isSpecial) {
369 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
370 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
371 }
372
373 // Only copy scheduled successors. Cut them from old node's successor
374 // list and move them over.
375 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
376 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
377 I != E; ++I) {
378 if (I->isSpecial)
379 continue;
380 if (I->Dep->isScheduled) {
381 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
382 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
383 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
384 }
385 }
386 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
387 SUnit *Succ = DelDeps[i].first;
388 bool isCtrl = DelDeps[i].second;
389 RemovePred(Succ, SU, isCtrl, false);
390 }
391
392 ++NumDups;
393 return NewSU;
394}
395
396/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
397/// and move all scheduled successors of the given SUnit to the last copy.
398void ScheduleDAGFast::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
399 const TargetRegisterClass *DestRC,
400 const TargetRegisterClass *SrcRC,
401 SmallVector<SUnit*, 2> &Copies) {
402 SUnit *CopyFromSU = CreateNewSUnit(NULL);
403 CopyFromSU->CopySrcRC = SrcRC;
404 CopyFromSU->CopyDstRC = DestRC;
405
406 SUnit *CopyToSU = CreateNewSUnit(NULL);
407 CopyToSU->CopySrcRC = DestRC;
408 CopyToSU->CopyDstRC = SrcRC;
409
410 // Only copy scheduled successors. Cut them from old node's successor
411 // list and move them over.
412 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
413 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
414 I != E; ++I) {
415 if (I->isSpecial)
416 continue;
417 if (I->Dep->isScheduled) {
418 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
419 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
420 }
421 }
422 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
423 SUnit *Succ = DelDeps[i].first;
424 bool isCtrl = DelDeps[i].second;
425 RemovePred(Succ, SU, isCtrl, false);
426 }
427
428 AddPred(CopyFromSU, SU, false, false, Reg, -1);
429 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
430
431 Copies.push_back(CopyFromSU);
432 Copies.push_back(CopyToSU);
433
434 ++NumCCCopies;
435}
436
437/// getPhysicalRegisterVT - Returns the ValueType of the physical register
438/// definition of the specified node.
439/// FIXME: Move to SelectionDAG?
440static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
441 const TargetInstrInfo *TII) {
442 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
443 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
444 unsigned NumRes = TID.getNumDefs();
445 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
446 if (Reg == *ImpDef)
447 break;
448 ++NumRes;
449 }
450 return N->getValueType(NumRes);
451}
452
453/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
454/// scheduling of the given node to satisfy live physical register dependencies.
455/// If the specific node is the last one that's available to schedule, do
456/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
457bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
458 SmallVector<unsigned, 4> &LRegs){
Dan Gohman086ec992008-09-23 18:50:48 +0000459 if (NumLiveRegs == 0)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000460 return false;
461
462 SmallSet<unsigned, 4> RegAdded;
463 // If this node would clobber any "live" register, then it's not ready.
464 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
465 I != E; ++I) {
466 if (I->Cost < 0) {
467 unsigned Reg = I->Reg;
Dan Gohman086ec992008-09-23 18:50:48 +0000468 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000469 if (RegAdded.insert(Reg))
470 LRegs.push_back(Reg);
471 }
472 for (const unsigned *Alias = TRI->getAliasSet(Reg);
473 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000474 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000475 if (RegAdded.insert(*Alias))
476 LRegs.push_back(*Alias);
477 }
478 }
479 }
480
Dan Gohmand23e0f82008-11-13 23:24:17 +0000481 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
482 if (!Node->isMachineOpcode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000483 continue;
484 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
485 if (!TID.ImplicitDefs)
486 continue;
487 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohman086ec992008-09-23 18:50:48 +0000488 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000489 if (RegAdded.insert(*Reg))
490 LRegs.push_back(*Reg);
491 }
492 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
493 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000494 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000495 if (RegAdded.insert(*Alias))
496 LRegs.push_back(*Alias);
497 }
498 }
499 }
500 return !LRegs.empty();
501}
502
503
504/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
505/// schedulers.
506void ScheduleDAGFast::ListScheduleBottomUp() {
507 unsigned CurCycle = 0;
508 // Add root to Available queue.
509 if (!SUnits.empty()) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000510 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohmanee2e4032008-09-18 16:26:26 +0000511 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
512 RootSU->isAvailable = true;
513 AvailableQueue.push(RootSU);
514 }
515
516 // While Available queue is not empty, grab the node with the highest
517 // priority. If it is not ready put it back. Schedule the node.
518 SmallVector<SUnit*, 4> NotReady;
519 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
520 Sequence.reserve(SUnits.size());
521 while (!AvailableQueue.empty()) {
522 bool Delayed = false;
523 LRegsMap.clear();
524 SUnit *CurSU = AvailableQueue.pop();
525 while (CurSU) {
Dan Gohmane93483d2008-11-17 19:52:36 +0000526 SmallVector<unsigned, 4> LRegs;
527 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
528 break;
529 Delayed = true;
530 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000531
532 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
533 NotReady.push_back(CurSU);
534 CurSU = AvailableQueue.pop();
535 }
536
537 // All candidates are delayed due to live physical reg dependencies.
538 // Try code duplication or inserting cross class copies
539 // to resolve it.
540 if (Delayed && !CurSU) {
541 if (!CurSU) {
542 // Try duplicating the nodes that produces these
543 // "expensive to copy" values to break the dependency. In case even
544 // that doesn't work, insert cross class copies.
545 SUnit *TrySU = NotReady[0];
546 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
547 assert(LRegs.size() == 1 && "Can't handle this yet!");
548 unsigned Reg = LRegs[0];
549 SUnit *LRDef = LiveRegDefs[Reg];
550 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
551 if (!NewDef) {
552 // Issue expensive cross register class copies.
Dan Gohman550f5af2008-11-13 21:36:12 +0000553 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000554 const TargetRegisterClass *RC =
555 TRI->getPhysicalRegisterRegClass(Reg, VT);
556 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
557 if (!DestRC) {
558 assert(false && "Don't know how to copy this physical register!");
559 abort();
560 }
561 SmallVector<SUnit*, 2> Copies;
562 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
563 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
564 << " to SU #" << Copies.front()->NodeNum << "\n";
565 AddPred(TrySU, Copies.front(), true, true);
566 NewDef = Copies.back();
567 }
568
569 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
570 << " to SU #" << TrySU->NodeNum << "\n";
571 LiveRegDefs[Reg] = NewDef;
572 AddPred(NewDef, TrySU, true, true);
573 TrySU->isAvailable = false;
574 CurSU = NewDef;
575 }
576
577 if (!CurSU) {
578 assert(false && "Unable to resolve live physical register dependencies!");
579 abort();
580 }
581 }
582
583 // Add the nodes that aren't ready back onto the available list.
584 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
585 NotReady[i]->isPending = false;
586 // May no longer be available due to backtracking.
587 if (NotReady[i]->isAvailable)
588 AvailableQueue.push(NotReady[i]);
589 }
590 NotReady.clear();
591
592 if (!CurSU)
593 Sequence.push_back(0);
Dan Gohman1256f5f2008-11-18 21:22:20 +0000594 else
Dan Gohmanee2e4032008-09-18 16:26:26 +0000595 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000596 ++CurCycle;
597 }
598
599 // Reverse the order if it is bottom up.
600 std::reverse(Sequence.begin(), Sequence.end());
601
602
603#ifndef NDEBUG
604 // Verify that all SUnits were scheduled.
605 bool AnyNotSched = false;
606 unsigned DeadNodes = 0;
607 unsigned Noops = 0;
608 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
609 if (!SUnits[i].isScheduled) {
610 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
611 ++DeadNodes;
612 continue;
613 }
614 if (!AnyNotSched)
615 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000616 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000617 cerr << "has not been scheduled!\n";
618 AnyNotSched = true;
619 }
620 if (SUnits[i].NumSuccsLeft != 0) {
621 if (!AnyNotSched)
622 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000623 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000624 cerr << "has successors left!\n";
625 AnyNotSched = true;
626 }
627 }
628 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
629 if (!Sequence[i])
630 ++Noops;
631 assert(!AnyNotSched);
632 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
633 "The number of nodes scheduled doesn't match the expected number!");
634#endif
635}
636
637//===----------------------------------------------------------------------===//
638// Public Constructor Functions
639//===----------------------------------------------------------------------===//
640
641llvm::ScheduleDAG* llvm::createFastDAGScheduler(SelectionDAGISel *IS,
642 SelectionDAG *DAG,
Dan Gohman9b75b372008-11-11 17:50:47 +0000643 const TargetMachine *TM,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000644 MachineBasicBlock *BB, bool) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000645 return new ScheduleDAGFast(DAG, BB, *TM);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000646}