blob: 023ebf3b5599e3be215c1146ce6f4c464383b4a4 [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");
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///
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
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();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000100};
101} // end anonymous namespace
102
103
104/// Schedule - Schedule the DAG using list scheduling.
105void ScheduleDAGFast::Schedule() {
106 DOUT << "********** List Scheduling **********\n";
107
Dan Gohman086ec992008-09-23 18:50:48 +0000108 NumLiveRegs = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000109 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
110 LiveRegCycles.resize(TRI->getNumRegs(), 0);
111
112 // Build scheduling units.
113 BuildSchedUnits();
114
115 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman3cc62432008-11-18 02:06:40 +0000116 SUnits[su].dumpAll(this));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000117
118 // Execute the actual scheduling loop.
119 ListScheduleBottomUp();
120}
121
122//===----------------------------------------------------------------------===//
123// Bottom-Up Scheduling
124//===----------------------------------------------------------------------===//
125
126/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
127/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d093f32008-11-18 00:38:59 +0000128void ScheduleDAGFast::ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000129 --PredSU->NumSuccsLeft;
130
131#ifndef NDEBUG
132 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman2d093f32008-11-18 00:38:59 +0000133 cerr << "*** Scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000134 PredSU->dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000135 cerr << " has been released too many times!\n";
136 assert(0);
137 }
138#endif
139
140 if (PredSU->NumSuccsLeft == 0) {
141 PredSU->isAvailable = true;
142 AvailableQueue.push(PredSU);
143 }
144}
145
146/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
147/// count of its predecessors. If a predecessor pending count is zero, add it to
148/// the Available queue.
149void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
150 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman3cc62432008-11-18 02:06:40 +0000151 DEBUG(SU->dump(this));
Dan Gohman1256f5f2008-11-18 21:22:20 +0000152
Dan Gohmanee2e4032008-09-18 16:26:26 +0000153 SU->Cycle = CurCycle;
Dan Gohman1256f5f2008-11-18 21:22:20 +0000154 Sequence.push_back(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000155
156 // Bottom up: release predecessors
157 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
158 I != E; ++I) {
Dan Gohman2d093f32008-11-18 00:38:59 +0000159 ReleasePred(SU, I->Dep, I->isCtrl);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000160 if (I->Cost < 0) {
161 // This is a physical register dependency and it's impossible or
162 // expensive to copy the register. Make sure nothing that can
163 // clobber the register is scheduled between the predecessor and
164 // this node.
Dan Gohman086ec992008-09-23 18:50:48 +0000165 if (!LiveRegDefs[I->Reg]) {
166 ++NumLiveRegs;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000167 LiveRegDefs[I->Reg] = I->Dep;
168 LiveRegCycles[I->Reg] = CurCycle;
169 }
170 }
171 }
172
173 // Release all the implicit physical register defs that are live.
174 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
175 I != E; ++I) {
176 if (I->Cost < 0) {
177 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
Dan Gohman086ec992008-09-23 18:50:48 +0000178 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohmanee2e4032008-09-18 16:26:26 +0000179 assert(LiveRegDefs[I->Reg] == SU &&
180 "Physical register dependency violated?");
Dan Gohman086ec992008-09-23 18:50:48 +0000181 --NumLiveRegs;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000182 LiveRegDefs[I->Reg] = NULL;
183 LiveRegCycles[I->Reg] = 0;
184 }
185 }
186 }
187
188 SU->isScheduled = true;
189}
190
191/// AddPred - adds an edge from SUnit X to SUnit Y.
192bool ScheduleDAGFast::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
193 unsigned PhyReg, int Cost) {
194 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
195}
196
197/// RemovePred - This removes the specified node N from the predecessors of
198/// the current node M.
199bool ScheduleDAGFast::RemovePred(SUnit *M, SUnit *N,
200 bool isCtrl, bool isSpecial) {
201 return M->removePred(N, isCtrl, isSpecial);
202}
203
204/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
205/// successors to the newly created node.
206SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohmand23e0f82008-11-13 23:24:17 +0000207 if (SU->getNode()->getFlaggedNode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000208 return NULL;
209
Dan Gohman550f5af2008-11-13 21:36:12 +0000210 SDNode *N = SU->getNode();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000211 if (!N)
212 return NULL;
213
214 SUnit *NewSU;
215 bool TryUnfold = false;
216 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
217 MVT VT = N->getValueType(i);
218 if (VT == MVT::Flag)
219 return NULL;
220 else if (VT == MVT::Other)
221 TryUnfold = true;
222 }
223 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
224 const SDValue &Op = N->getOperand(i);
225 MVT VT = Op.getNode()->getValueType(Op.getResNo());
226 if (VT == MVT::Flag)
227 return NULL;
228 }
229
230 if (TryUnfold) {
231 SmallVector<SDNode*, 2> NewNodes;
Dan Gohmana23b3b82008-11-13 21:21:28 +0000232 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000233 return NULL;
234
235 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
236 assert(NewNodes.size() == 2 && "Expected a load folding node!");
237
238 N = NewNodes[1];
239 SDNode *LoadNode = NewNodes[0];
240 unsigned NumVals = N->getNumValues();
Dan Gohman550f5af2008-11-13 21:36:12 +0000241 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000242 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman550f5af2008-11-13 21:36:12 +0000243 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
244 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohmana23b3b82008-11-13 21:21:28 +0000245 SDValue(LoadNode, 1));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000246
Dan Gohmancdb260d2008-11-19 23:39:02 +0000247 SUnit *NewSU = NewSUnit(N);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000248 assert(N->getNodeId() == -1 && "Node already inserted!");
249 N->setNodeId(NewSU->NodeNum);
250
251 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
252 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
253 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
254 NewSU->isTwoAddress = true;
255 break;
256 }
257 }
258 if (TID.isCommutable())
259 NewSU->isCommutable = true;
260 // FIXME: Calculate height / depth and propagate the changes?
261 NewSU->Depth = SU->Depth;
262 NewSU->Height = SU->Height;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000263
264 // LoadNode may already exist. This can happen when there is another
265 // load from the same location and producing the same type of value
266 // but it has different alignment or volatileness.
267 bool isNewLoad = true;
268 SUnit *LoadSU;
269 if (LoadNode->getNodeId() != -1) {
270 LoadSU = &SUnits[LoadNode->getNodeId()];
271 isNewLoad = false;
272 } else {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000273 LoadSU = NewSUnit(LoadNode);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000274 LoadNode->setNodeId(LoadSU->NodeNum);
275
276 LoadSU->Depth = SU->Depth;
277 LoadSU->Height = SU->Height;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000278 }
279
280 SUnit *ChainPred = NULL;
281 SmallVector<SDep, 4> ChainSuccs;
282 SmallVector<SDep, 4> LoadPreds;
283 SmallVector<SDep, 4> NodePreds;
284 SmallVector<SDep, 4> NodeSuccs;
285 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
286 I != E; ++I) {
287 if (I->isCtrl)
288 ChainPred = I->Dep;
Dan Gohman550f5af2008-11-13 21:36:12 +0000289 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000290 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
291 else
292 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
293 }
294 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
295 I != E; ++I) {
296 if (I->isCtrl)
297 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
298 I->isCtrl, I->isSpecial));
299 else
300 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
301 I->isCtrl, I->isSpecial));
302 }
303
304 if (ChainPred) {
305 RemovePred(SU, ChainPred, true, false);
306 if (isNewLoad)
307 AddPred(LoadSU, ChainPred, true, false);
308 }
309 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
310 SDep *Pred = &LoadPreds[i];
311 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
312 if (isNewLoad) {
313 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
314 Pred->Reg, Pred->Cost);
315 }
316 }
317 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
318 SDep *Pred = &NodePreds[i];
319 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
320 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
321 Pred->Reg, Pred->Cost);
322 }
323 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
324 SDep *Succ = &NodeSuccs[i];
325 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
326 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
327 Succ->Reg, Succ->Cost);
328 }
329 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
330 SDep *Succ = &ChainSuccs[i];
331 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
332 if (isNewLoad) {
333 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
334 Succ->Reg, Succ->Cost);
335 }
336 }
337 if (isNewLoad) {
338 AddPred(NewSU, LoadSU, false, false);
339 }
340
341 ++NumUnfolds;
342
343 if (NewSU->NumSuccsLeft == 0) {
344 NewSU->isAvailable = true;
345 return NewSU;
346 }
347 SU = NewSU;
348 }
349
350 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Dan Gohmancdb260d2008-11-19 23:39:02 +0000351 NewSU = Clone(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000352
353 // New SUnit has the exact same predecessors.
354 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
355 I != E; ++I)
356 if (!I->isSpecial) {
357 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
358 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
359 }
360
361 // Only copy scheduled successors. Cut them from old node's successor
362 // list and move them over.
363 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
364 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
365 I != E; ++I) {
366 if (I->isSpecial)
367 continue;
368 if (I->Dep->isScheduled) {
369 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
370 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
371 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
372 }
373 }
374 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
375 SUnit *Succ = DelDeps[i].first;
376 bool isCtrl = DelDeps[i].second;
377 RemovePred(Succ, SU, isCtrl, false);
378 }
379
380 ++NumDups;
381 return NewSU;
382}
383
384/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
385/// and move all scheduled successors of the given SUnit to the last copy.
386void ScheduleDAGFast::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
387 const TargetRegisterClass *DestRC,
388 const TargetRegisterClass *SrcRC,
389 SmallVector<SUnit*, 2> &Copies) {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000390 SUnit *CopyFromSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000391 CopyFromSU->CopySrcRC = SrcRC;
392 CopyFromSU->CopyDstRC = DestRC;
393
Dan Gohmancdb260d2008-11-19 23:39:02 +0000394 SUnit *CopyToSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000395 CopyToSU->CopySrcRC = DestRC;
396 CopyToSU->CopyDstRC = SrcRC;
397
398 // Only copy scheduled successors. Cut them from old node's successor
399 // list and move them over.
400 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
401 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
402 I != E; ++I) {
403 if (I->isSpecial)
404 continue;
405 if (I->Dep->isScheduled) {
406 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
407 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
408 }
409 }
410 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
411 SUnit *Succ = DelDeps[i].first;
412 bool isCtrl = DelDeps[i].second;
413 RemovePred(Succ, SU, isCtrl, false);
414 }
415
416 AddPred(CopyFromSU, SU, false, false, Reg, -1);
417 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
418
419 Copies.push_back(CopyFromSU);
420 Copies.push_back(CopyToSU);
421
422 ++NumCCCopies;
423}
424
425/// getPhysicalRegisterVT - Returns the ValueType of the physical register
426/// definition of the specified node.
427/// FIXME: Move to SelectionDAG?
428static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
429 const TargetInstrInfo *TII) {
430 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
431 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
432 unsigned NumRes = TID.getNumDefs();
433 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
434 if (Reg == *ImpDef)
435 break;
436 ++NumRes;
437 }
438 return N->getValueType(NumRes);
439}
440
441/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
442/// scheduling of the given node to satisfy live physical register dependencies.
443/// If the specific node is the last one that's available to schedule, do
444/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
445bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
446 SmallVector<unsigned, 4> &LRegs){
Dan Gohman086ec992008-09-23 18:50:48 +0000447 if (NumLiveRegs == 0)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000448 return false;
449
450 SmallSet<unsigned, 4> RegAdded;
451 // If this node would clobber any "live" register, then it's not ready.
452 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
453 I != E; ++I) {
454 if (I->Cost < 0) {
455 unsigned Reg = I->Reg;
Dan Gohman086ec992008-09-23 18:50:48 +0000456 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000457 if (RegAdded.insert(Reg))
458 LRegs.push_back(Reg);
459 }
460 for (const unsigned *Alias = TRI->getAliasSet(Reg);
461 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000462 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000463 if (RegAdded.insert(*Alias))
464 LRegs.push_back(*Alias);
465 }
466 }
467 }
468
Dan Gohmand23e0f82008-11-13 23:24:17 +0000469 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
470 if (!Node->isMachineOpcode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000471 continue;
472 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
473 if (!TID.ImplicitDefs)
474 continue;
475 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohman086ec992008-09-23 18:50:48 +0000476 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000477 if (RegAdded.insert(*Reg))
478 LRegs.push_back(*Reg);
479 }
480 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
481 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000482 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000483 if (RegAdded.insert(*Alias))
484 LRegs.push_back(*Alias);
485 }
486 }
487 }
488 return !LRegs.empty();
489}
490
491
492/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
493/// schedulers.
494void ScheduleDAGFast::ListScheduleBottomUp() {
495 unsigned CurCycle = 0;
496 // Add root to Available queue.
497 if (!SUnits.empty()) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000498 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohmanee2e4032008-09-18 16:26:26 +0000499 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
500 RootSU->isAvailable = true;
501 AvailableQueue.push(RootSU);
502 }
503
504 // While Available queue is not empty, grab the node with the highest
505 // priority. If it is not ready put it back. Schedule the node.
506 SmallVector<SUnit*, 4> NotReady;
507 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
508 Sequence.reserve(SUnits.size());
509 while (!AvailableQueue.empty()) {
510 bool Delayed = false;
511 LRegsMap.clear();
512 SUnit *CurSU = AvailableQueue.pop();
513 while (CurSU) {
Dan Gohmane93483d2008-11-17 19:52:36 +0000514 SmallVector<unsigned, 4> LRegs;
515 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
516 break;
517 Delayed = true;
518 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000519
520 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
521 NotReady.push_back(CurSU);
522 CurSU = AvailableQueue.pop();
523 }
524
525 // All candidates are delayed due to live physical reg dependencies.
526 // Try code duplication or inserting cross class copies
527 // to resolve it.
528 if (Delayed && !CurSU) {
529 if (!CurSU) {
530 // Try duplicating the nodes that produces these
531 // "expensive to copy" values to break the dependency. In case even
532 // that doesn't work, insert cross class copies.
533 SUnit *TrySU = NotReady[0];
534 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
535 assert(LRegs.size() == 1 && "Can't handle this yet!");
536 unsigned Reg = LRegs[0];
537 SUnit *LRDef = LiveRegDefs[Reg];
538 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
539 if (!NewDef) {
540 // Issue expensive cross register class copies.
Dan Gohman550f5af2008-11-13 21:36:12 +0000541 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000542 const TargetRegisterClass *RC =
543 TRI->getPhysicalRegisterRegClass(Reg, VT);
544 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
545 if (!DestRC) {
546 assert(false && "Don't know how to copy this physical register!");
547 abort();
548 }
549 SmallVector<SUnit*, 2> Copies;
550 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
551 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
552 << " to SU #" << Copies.front()->NodeNum << "\n";
553 AddPred(TrySU, Copies.front(), true, true);
554 NewDef = Copies.back();
555 }
556
557 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
558 << " to SU #" << TrySU->NodeNum << "\n";
559 LiveRegDefs[Reg] = NewDef;
560 AddPred(NewDef, TrySU, true, true);
561 TrySU->isAvailable = false;
562 CurSU = NewDef;
563 }
564
565 if (!CurSU) {
566 assert(false && "Unable to resolve live physical register dependencies!");
567 abort();
568 }
569 }
570
571 // Add the nodes that aren't ready back onto the available list.
572 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
573 NotReady[i]->isPending = false;
574 // May no longer be available due to backtracking.
575 if (NotReady[i]->isAvailable)
576 AvailableQueue.push(NotReady[i]);
577 }
578 NotReady.clear();
579
580 if (!CurSU)
581 Sequence.push_back(0);
Dan Gohman1256f5f2008-11-18 21:22:20 +0000582 else
Dan Gohmanee2e4032008-09-18 16:26:26 +0000583 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000584 ++CurCycle;
585 }
586
587 // Reverse the order if it is bottom up.
588 std::reverse(Sequence.begin(), Sequence.end());
589
590
591#ifndef NDEBUG
592 // Verify that all SUnits were scheduled.
593 bool AnyNotSched = false;
594 unsigned DeadNodes = 0;
595 unsigned Noops = 0;
596 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
597 if (!SUnits[i].isScheduled) {
598 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
599 ++DeadNodes;
600 continue;
601 }
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 not been scheduled!\n";
606 AnyNotSched = true;
607 }
608 if (SUnits[i].NumSuccsLeft != 0) {
609 if (!AnyNotSched)
610 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000611 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000612 cerr << "has successors left!\n";
613 AnyNotSched = true;
614 }
615 }
616 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
617 if (!Sequence[i])
618 ++Noops;
619 assert(!AnyNotSched);
620 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
621 "The number of nodes scheduled doesn't match the expected number!");
622#endif
623}
624
625//===----------------------------------------------------------------------===//
626// Public Constructor Functions
627//===----------------------------------------------------------------------===//
628
629llvm::ScheduleDAG* llvm::createFastDAGScheduler(SelectionDAGISel *IS,
630 SelectionDAG *DAG,
Dan Gohman9b75b372008-11-11 17:50:47 +0000631 const TargetMachine *TM,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000632 MachineBasicBlock *BB, bool) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000633 return new ScheduleDAGFast(DAG, BB, *TM);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000634}