blob: 5154fb1d3e91101122a1919ef7d5d92a9103f8df [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"
Dan Gohman79ce2762009-01-15 19:20:50 +000017#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohmanee2e4032008-09-18 16:26:26 +000018#include "llvm/Target/TargetRegisterInfo.h"
19#include "llvm/Target/TargetData.h"
Dan Gohmanee2e4032008-09-18 16:26:26 +000020#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 Gohman79ce2762009-01-15 19:20:50 +000074 ScheduleDAGFast(MachineFunction &mf)
75 : ScheduleDAGSDNodes(mf) {}
Dan Gohmanee2e4032008-09-18 16:26:26 +000076
77 void Schedule();
78
Dan Gohman54e4c362008-12-09 22:54:47 +000079 /// AddPred - adds a predecessor edge to SUnit SU.
Dan Gohmanee2e4032008-09-18 16:26:26 +000080 /// This returns true if this is a new predecessor.
Dan Gohmanffa39122008-12-16 01:00:55 +000081 void AddPred(SUnit *SU, const SDep &D) {
82 SU->addPred(D);
Dan Gohman54e4c362008-12-09 22:54:47 +000083 }
Dan Gohmanee2e4032008-09-18 16:26:26 +000084
Dan Gohman54e4c362008-12-09 22:54:47 +000085 /// RemovePred - removes a predecessor edge from SUnit SU.
86 /// This returns true if an edge was removed.
Dan Gohmanffa39122008-12-16 01:00:55 +000087 void RemovePred(SUnit *SU, const SDep &D) {
88 SU->removePred(D);
Dan Gohman54e4c362008-12-09 22:54:47 +000089 }
Dan Gohmanee2e4032008-09-18 16:26:26 +000090
91private:
Dan Gohman54e4c362008-12-09 22:54:47 +000092 void ReleasePred(SUnit *SU, SDep *PredEdge);
Dan Gohmanee2e4032008-09-18 16:26:26 +000093 void ScheduleNodeBottomUp(SUnit*, unsigned);
94 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengc29a56d2009-01-12 03:19:55 +000095 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
96 const TargetRegisterClass*,
97 const TargetRegisterClass*,
98 SmallVector<SUnit*, 2>&);
Dan Gohmanee2e4032008-09-18 16:26:26 +000099 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
100 void ListScheduleBottomUp();
Dan Gohman3f237442008-12-16 03:25:46 +0000101
102 /// ForceUnitLatencies - The fast scheduler doesn't care about real latencies.
103 bool ForceUnitLatencies() const { return true; }
Dan Gohmanee2e4032008-09-18 16:26:26 +0000104};
105} // end anonymous namespace
106
107
108/// Schedule - Schedule the DAG using list scheduling.
109void ScheduleDAGFast::Schedule() {
110 DOUT << "********** List Scheduling **********\n";
111
Dan Gohman086ec992008-09-23 18:50:48 +0000112 NumLiveRegs = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000113 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
114 LiveRegCycles.resize(TRI->getNumRegs(), 0);
115
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000116 // Build the scheduling graph.
117 BuildSchedGraph();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000118
119 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman3cc62432008-11-18 02:06:40 +0000120 SUnits[su].dumpAll(this));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000121
122 // Execute the actual scheduling loop.
123 ListScheduleBottomUp();
124}
125
126//===----------------------------------------------------------------------===//
127// Bottom-Up Scheduling
128//===----------------------------------------------------------------------===//
129
130/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
131/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +0000132void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
133 SUnit *PredSU = PredEdge->getSUnit();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000134 --PredSU->NumSuccsLeft;
135
136#ifndef NDEBUG
137 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman2d093f32008-11-18 00:38:59 +0000138 cerr << "*** Scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000139 PredSU->dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000140 cerr << " has been released too many times!\n";
141 assert(0);
142 }
143#endif
144
145 if (PredSU->NumSuccsLeft == 0) {
146 PredSU->isAvailable = true;
147 AvailableQueue.push(PredSU);
148 }
149}
150
151/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
152/// count of its predecessors. If a predecessor pending count is zero, add it to
153/// the Available queue.
154void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
155 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman3cc62432008-11-18 02:06:40 +0000156 DEBUG(SU->dump(this));
Dan Gohman1256f5f2008-11-18 21:22:20 +0000157
Dan Gohman3f237442008-12-16 03:25:46 +0000158 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
159 SU->setHeightToAtLeast(CurCycle);
Dan Gohman1256f5f2008-11-18 21:22:20 +0000160 Sequence.push_back(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000161
162 // Bottom up: release predecessors
163 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
164 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000165 ReleasePred(SU, &*I);
166 if (I->isAssignedRegDep()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000167 // This is a physical register dependency and it's impossible or
168 // expensive to copy the register. Make sure nothing that can
169 // clobber the register is scheduled between the predecessor and
170 // this node.
Dan Gohman54e4c362008-12-09 22:54:47 +0000171 if (!LiveRegDefs[I->getReg()]) {
Dan Gohman086ec992008-09-23 18:50:48 +0000172 ++NumLiveRegs;
Dan Gohman54e4c362008-12-09 22:54:47 +0000173 LiveRegDefs[I->getReg()] = I->getSUnit();
174 LiveRegCycles[I->getReg()] = CurCycle;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000175 }
176 }
177 }
178
179 // Release all the implicit physical register defs that are live.
180 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
181 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000182 if (I->isAssignedRegDep()) {
Dan Gohman3f237442008-12-16 03:25:46 +0000183 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohman086ec992008-09-23 18:50:48 +0000184 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman54e4c362008-12-09 22:54:47 +0000185 assert(LiveRegDefs[I->getReg()] == SU &&
Dan Gohmanee2e4032008-09-18 16:26:26 +0000186 "Physical register dependency violated?");
Dan Gohman086ec992008-09-23 18:50:48 +0000187 --NumLiveRegs;
Dan Gohman54e4c362008-12-09 22:54:47 +0000188 LiveRegDefs[I->getReg()] = NULL;
189 LiveRegCycles[I->getReg()] = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000190 }
191 }
192 }
193
194 SU->isScheduled = true;
195}
196
Dan Gohmanee2e4032008-09-18 16:26:26 +0000197/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
198/// successors to the newly created node.
199SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohmand23e0f82008-11-13 23:24:17 +0000200 if (SU->getNode()->getFlaggedNode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000201 return NULL;
202
Dan Gohman550f5af2008-11-13 21:36:12 +0000203 SDNode *N = SU->getNode();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000204 if (!N)
205 return NULL;
206
207 SUnit *NewSU;
208 bool TryUnfold = false;
209 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
210 MVT VT = N->getValueType(i);
211 if (VT == MVT::Flag)
212 return NULL;
213 else if (VT == MVT::Other)
214 TryUnfold = true;
215 }
216 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
217 const SDValue &Op = N->getOperand(i);
218 MVT VT = Op.getNode()->getValueType(Op.getResNo());
219 if (VT == MVT::Flag)
220 return NULL;
221 }
222
223 if (TryUnfold) {
224 SmallVector<SDNode*, 2> NewNodes;
Dan Gohmana23b3b82008-11-13 21:21:28 +0000225 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000226 return NULL;
227
228 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
229 assert(NewNodes.size() == 2 && "Expected a load folding node!");
230
231 N = NewNodes[1];
232 SDNode *LoadNode = NewNodes[0];
233 unsigned NumVals = N->getNumValues();
Dan Gohman550f5af2008-11-13 21:36:12 +0000234 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000235 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman550f5af2008-11-13 21:36:12 +0000236 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
237 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohmana23b3b82008-11-13 21:21:28 +0000238 SDValue(LoadNode, 1));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000239
Dan Gohmancdb260d2008-11-19 23:39:02 +0000240 SUnit *NewSU = NewSUnit(N);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000241 assert(N->getNodeId() == -1 && "Node already inserted!");
242 N->setNodeId(NewSU->NodeNum);
243
244 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
245 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
246 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
247 NewSU->isTwoAddress = true;
248 break;
249 }
250 }
251 if (TID.isCommutable())
252 NewSU->isCommutable = true;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000253
254 // LoadNode may already exist. This can happen when there is another
255 // load from the same location and producing the same type of value
256 // but it has different alignment or volatileness.
257 bool isNewLoad = true;
258 SUnit *LoadSU;
259 if (LoadNode->getNodeId() != -1) {
260 LoadSU = &SUnits[LoadNode->getNodeId()];
261 isNewLoad = false;
262 } else {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000263 LoadSU = NewSUnit(LoadNode);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000264 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000265 }
266
Dan Gohman54e4c362008-12-09 22:54:47 +0000267 SDep ChainPred;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000268 SmallVector<SDep, 4> ChainSuccs;
269 SmallVector<SDep, 4> LoadPreds;
270 SmallVector<SDep, 4> NodePreds;
271 SmallVector<SDep, 4> NodeSuccs;
272 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
273 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000274 if (I->isCtrl())
275 ChainPred = *I;
276 else if (I->getSUnit()->getNode() &&
277 I->getSUnit()->getNode()->isOperandOf(LoadNode))
278 LoadPreds.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000279 else
Dan Gohman54e4c362008-12-09 22:54:47 +0000280 NodePreds.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000281 }
282 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
283 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000284 if (I->isCtrl())
285 ChainSuccs.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000286 else
Dan Gohman54e4c362008-12-09 22:54:47 +0000287 NodeSuccs.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000288 }
289
Dan Gohman54e4c362008-12-09 22:54:47 +0000290 if (ChainPred.getSUnit()) {
291 RemovePred(SU, ChainPred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000292 if (isNewLoad)
Dan Gohman54e4c362008-12-09 22:54:47 +0000293 AddPred(LoadSU, ChainPred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000294 }
295 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000296 const SDep &Pred = LoadPreds[i];
297 RemovePred(SU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000298 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000299 AddPred(LoadSU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000300 }
301 }
302 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000303 const SDep &Pred = NodePreds[i];
304 RemovePred(SU, Pred);
305 AddPred(NewSU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000306 }
307 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000308 SDep D = NodeSuccs[i];
309 SUnit *SuccDep = D.getSUnit();
310 D.setSUnit(SU);
311 RemovePred(SuccDep, D);
312 D.setSUnit(NewSU);
313 AddPred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000314 }
315 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000316 SDep D = ChainSuccs[i];
317 SUnit *SuccDep = D.getSUnit();
318 D.setSUnit(SU);
319 RemovePred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000320 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000321 D.setSUnit(LoadSU);
322 AddPred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000323 }
324 }
325 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000326 AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000327 }
328
329 ++NumUnfolds;
330
331 if (NewSU->NumSuccsLeft == 0) {
332 NewSU->isAvailable = true;
333 return NewSU;
334 }
335 SU = NewSU;
336 }
337
338 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Dan Gohmancdb260d2008-11-19 23:39:02 +0000339 NewSU = Clone(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000340
341 // New SUnit has the exact same predecessors.
342 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
343 I != E; ++I)
Dan Gohman3f237442008-12-16 03:25:46 +0000344 if (!I->isArtificial())
Dan Gohman54e4c362008-12-09 22:54:47 +0000345 AddPred(NewSU, *I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000346
347 // Only copy scheduled successors. Cut them from old node's successor
348 // list and move them over.
Dan Gohman54e4c362008-12-09 22:54:47 +0000349 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000350 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
351 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000352 if (I->isArtificial())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000353 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000354 SUnit *SuccSU = I->getSUnit();
355 if (SuccSU->isScheduled) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000356 SDep D = *I;
357 D.setSUnit(NewSU);
358 AddPred(SuccSU, D);
359 D.setSUnit(SU);
360 DelDeps.push_back(std::make_pair(SuccSU, D));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000361 }
362 }
Evan Chengc29a56d2009-01-12 03:19:55 +0000363 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000364 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000365
366 ++NumDups;
367 return NewSU;
368}
369
Evan Chengc29a56d2009-01-12 03:19:55 +0000370/// InsertCopiesAndMoveSuccs - Insert register copies and move all
371/// scheduled successors of the given SUnit to the last copy.
372void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000373 const TargetRegisterClass *DestRC,
374 const TargetRegisterClass *SrcRC,
375 SmallVector<SUnit*, 2> &Copies) {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000376 SUnit *CopyFromSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000377 CopyFromSU->CopySrcRC = SrcRC;
378 CopyFromSU->CopyDstRC = DestRC;
379
Dan Gohmancdb260d2008-11-19 23:39:02 +0000380 SUnit *CopyToSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000381 CopyToSU->CopySrcRC = DestRC;
382 CopyToSU->CopyDstRC = SrcRC;
383
384 // Only copy scheduled successors. Cut them from old node's successor
385 // list and move them over.
Dan Gohman54e4c362008-12-09 22:54:47 +0000386 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000387 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
388 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000389 if (I->isArtificial())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000390 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000391 SUnit *SuccSU = I->getSUnit();
392 if (SuccSU->isScheduled) {
393 SDep D = *I;
394 D.setSUnit(CopyToSU);
395 AddPred(SuccSU, D);
396 DelDeps.push_back(std::make_pair(SuccSU, *I));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000397 }
398 }
399 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000400 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000401 }
402
Dan Gohman54e4c362008-12-09 22:54:47 +0000403 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
404 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000405
406 Copies.push_back(CopyFromSU);
407 Copies.push_back(CopyToSU);
408
Evan Chengc29a56d2009-01-12 03:19:55 +0000409 ++NumPRCopies;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000410}
411
412/// getPhysicalRegisterVT - Returns the ValueType of the physical register
413/// definition of the specified node.
414/// FIXME: Move to SelectionDAG?
415static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
416 const TargetInstrInfo *TII) {
417 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
418 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
419 unsigned NumRes = TID.getNumDefs();
420 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
421 if (Reg == *ImpDef)
422 break;
423 ++NumRes;
424 }
425 return N->getValueType(NumRes);
426}
427
428/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
429/// scheduling of the given node to satisfy live physical register dependencies.
430/// If the specific node is the last one that's available to schedule, do
431/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
432bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
433 SmallVector<unsigned, 4> &LRegs){
Dan Gohman086ec992008-09-23 18:50:48 +0000434 if (NumLiveRegs == 0)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000435 return false;
436
437 SmallSet<unsigned, 4> RegAdded;
438 // If this node would clobber any "live" register, then it's not ready.
439 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
440 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000441 if (I->isAssignedRegDep()) {
442 unsigned Reg = I->getReg();
443 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000444 if (RegAdded.insert(Reg))
445 LRegs.push_back(Reg);
446 }
447 for (const unsigned *Alias = TRI->getAliasSet(Reg);
448 *Alias; ++Alias)
Dan Gohman54e4c362008-12-09 22:54:47 +0000449 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000450 if (RegAdded.insert(*Alias))
451 LRegs.push_back(*Alias);
452 }
453 }
454 }
455
Dan Gohmand23e0f82008-11-13 23:24:17 +0000456 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
457 if (!Node->isMachineOpcode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000458 continue;
459 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
460 if (!TID.ImplicitDefs)
461 continue;
462 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohman086ec992008-09-23 18:50:48 +0000463 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000464 if (RegAdded.insert(*Reg))
465 LRegs.push_back(*Reg);
466 }
467 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
468 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000469 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000470 if (RegAdded.insert(*Alias))
471 LRegs.push_back(*Alias);
472 }
473 }
474 }
475 return !LRegs.empty();
476}
477
478
479/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
480/// schedulers.
481void ScheduleDAGFast::ListScheduleBottomUp() {
482 unsigned CurCycle = 0;
483 // Add root to Available queue.
484 if (!SUnits.empty()) {
Dan Gohmana23b3b82008-11-13 21:21:28 +0000485 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohmanee2e4032008-09-18 16:26:26 +0000486 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
487 RootSU->isAvailable = true;
488 AvailableQueue.push(RootSU);
489 }
490
491 // While Available queue is not empty, grab the node with the highest
492 // priority. If it is not ready put it back. Schedule the node.
493 SmallVector<SUnit*, 4> NotReady;
494 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
495 Sequence.reserve(SUnits.size());
496 while (!AvailableQueue.empty()) {
497 bool Delayed = false;
498 LRegsMap.clear();
499 SUnit *CurSU = AvailableQueue.pop();
500 while (CurSU) {
Dan Gohmane93483d2008-11-17 19:52:36 +0000501 SmallVector<unsigned, 4> LRegs;
502 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
503 break;
504 Delayed = true;
505 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000506
507 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
508 NotReady.push_back(CurSU);
509 CurSU = AvailableQueue.pop();
510 }
511
512 // All candidates are delayed due to live physical reg dependencies.
513 // Try code duplication or inserting cross class copies
514 // to resolve it.
515 if (Delayed && !CurSU) {
516 if (!CurSU) {
517 // Try duplicating the nodes that produces these
518 // "expensive to copy" values to break the dependency. In case even
519 // that doesn't work, insert cross class copies.
520 SUnit *TrySU = NotReady[0];
521 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
522 assert(LRegs.size() == 1 && "Can't handle this yet!");
523 unsigned Reg = LRegs[0];
524 SUnit *LRDef = LiveRegDefs[Reg];
Evan Chengc29a56d2009-01-12 03:19:55 +0000525 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
526 const TargetRegisterClass *RC =
527 TRI->getPhysicalRegisterRegClass(Reg, VT);
528 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
529
530 // If cross copy register class is null, then it must be possible copy
531 // the value directly. Do not try duplicate the def.
532 SUnit *NewDef = 0;
533 if (DestRC)
534 NewDef = CopyAndMoveSuccessors(LRDef);
535 else
536 DestRC = RC;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000537 if (!NewDef) {
Evan Chengc29a56d2009-01-12 03:19:55 +0000538 // Issue copies, these can be expensive cross register class copies.
Dan Gohmanee2e4032008-09-18 16:26:26 +0000539 SmallVector<SUnit*, 2> Copies;
Evan Chengc29a56d2009-01-12 03:19:55 +0000540 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000541 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
542 << " to SU #" << Copies.front()->NodeNum << "\n";
Dan Gohman54e4c362008-12-09 22:54:47 +0000543 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
544 /*Reg=*/0, /*isNormalMemory=*/false,
545 /*isMustAlias=*/false, /*isArtificial=*/true));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000546 NewDef = Copies.back();
547 }
548
549 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
550 << " to SU #" << TrySU->NodeNum << "\n";
551 LiveRegDefs[Reg] = NewDef;
Dan Gohman54e4c362008-12-09 22:54:47 +0000552 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
553 /*Reg=*/0, /*isNormalMemory=*/false,
554 /*isMustAlias=*/false, /*isArtificial=*/true));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000555 TrySU->isAvailable = false;
556 CurSU = NewDef;
557 }
558
559 if (!CurSU) {
560 assert(false && "Unable to resolve live physical register dependencies!");
561 abort();
562 }
563 }
564
565 // Add the nodes that aren't ready back onto the available list.
566 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
567 NotReady[i]->isPending = false;
568 // May no longer be available due to backtracking.
569 if (NotReady[i]->isAvailable)
570 AvailableQueue.push(NotReady[i]);
571 }
572 NotReady.clear();
573
Dan Gohman47d1a212008-11-21 00:10:42 +0000574 if (CurSU)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000575 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000576 ++CurCycle;
577 }
578
579 // Reverse the order if it is bottom up.
580 std::reverse(Sequence.begin(), Sequence.end());
581
582
583#ifndef NDEBUG
584 // Verify that all SUnits were scheduled.
585 bool AnyNotSched = false;
586 unsigned DeadNodes = 0;
587 unsigned Noops = 0;
588 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
589 if (!SUnits[i].isScheduled) {
590 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
591 ++DeadNodes;
592 continue;
593 }
594 if (!AnyNotSched)
595 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000596 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000597 cerr << "has not been scheduled!\n";
598 AnyNotSched = true;
599 }
600 if (SUnits[i].NumSuccsLeft != 0) {
601 if (!AnyNotSched)
602 cerr << "*** List scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000603 SUnits[i].dump(this);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000604 cerr << "has successors left!\n";
605 AnyNotSched = true;
606 }
607 }
608 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
609 if (!Sequence[i])
610 ++Noops;
611 assert(!AnyNotSched);
612 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
613 "The number of nodes scheduled doesn't match the expected number!");
614#endif
615}
616
617//===----------------------------------------------------------------------===//
618// Public Constructor Functions
619//===----------------------------------------------------------------------===//
620
Dan Gohman79ce2762009-01-15 19:20:50 +0000621llvm::ScheduleDAG* llvm::createFastDAGScheduler(SelectionDAGISel *IS, bool) {
622 return new ScheduleDAGFast(*IS->MF);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000623}