blob: d1930b1a2c75285374bb9a60ab02abd0782f9d88 [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 Gohman84fbac52009-02-06 17:22:58 +000015#include "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"
Torok Edwin7d696d82009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohmanee2e4032008-09-18 16:26:26 +000029using namespace llvm;
30
31STATISTIC(NumUnfolds, "Number of nodes unfolded");
32STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengc29a56d2009-01-12 03:19:55 +000033STATISTIC(NumPRCopies, "Number of physical copies");
Dan Gohmanee2e4032008-09-18 16:26:26 +000034
35static RegisterScheduler
Dan Gohmanb8cab922008-10-14 20:25:08 +000036 fastDAGScheduler("fast", "Fast suboptimal list scheduling",
Dan Gohmanee2e4032008-09-18 16:26:26 +000037 createFastDAGScheduler);
38
39namespace {
40 /// FastPriorityQueue - A degenerate priority queue that considers
41 /// all nodes to have the same priority.
42 ///
43 struct VISIBILITY_HIDDEN FastPriorityQueue {
Dan Gohman086ec992008-09-23 18:50:48 +000044 SmallVector<SUnit *, 16> Queue;
Dan Gohmanee2e4032008-09-18 16:26:26 +000045
46 bool empty() const { return Queue.empty(); }
47
48 void push(SUnit *U) {
49 Queue.push_back(U);
50 }
51
52 SUnit *pop() {
53 if (empty()) return NULL;
54 SUnit *V = Queue.back();
55 Queue.pop_back();
56 return V;
57 }
58 };
59
60//===----------------------------------------------------------------------===//
61/// ScheduleDAGFast - The actual "fast" list scheduler implementation.
62///
Dan Gohman343f0c02008-11-19 23:18:57 +000063class VISIBILITY_HIDDEN ScheduleDAGFast : public ScheduleDAGSDNodes {
Dan Gohmanee2e4032008-09-18 16:26:26 +000064private:
65 /// AvailableQueue - The priority queue to use for the available SUnits.
66 FastPriorityQueue AvailableQueue;
67
Dan Gohman086ec992008-09-23 18:50:48 +000068 /// LiveRegDefs - A set of physical registers and their definition
Dan Gohmanee2e4032008-09-18 16:26:26 +000069 /// that are "live". These nodes must be scheduled before any other nodes that
70 /// modifies the registers can be scheduled.
Dan Gohman086ec992008-09-23 18:50:48 +000071 unsigned NumLiveRegs;
Dan Gohmanee2e4032008-09-18 16:26:26 +000072 std::vector<SUnit*> LiveRegDefs;
73 std::vector<unsigned> LiveRegCycles;
74
75public:
Dan Gohman79ce2762009-01-15 19:20:50 +000076 ScheduleDAGFast(MachineFunction &mf)
77 : ScheduleDAGSDNodes(mf) {}
Dan Gohmanee2e4032008-09-18 16:26:26 +000078
79 void Schedule();
80
Dan Gohman54e4c362008-12-09 22:54:47 +000081 /// AddPred - adds a predecessor edge to SUnit SU.
Dan Gohmanee2e4032008-09-18 16:26:26 +000082 /// This returns true if this is a new predecessor.
Dan Gohmanffa39122008-12-16 01:00:55 +000083 void AddPred(SUnit *SU, const SDep &D) {
84 SU->addPred(D);
Dan Gohman54e4c362008-12-09 22:54:47 +000085 }
Dan Gohmanee2e4032008-09-18 16:26:26 +000086
Dan Gohman54e4c362008-12-09 22:54:47 +000087 /// RemovePred - removes a predecessor edge from SUnit SU.
88 /// This returns true if an edge was removed.
Dan Gohmanffa39122008-12-16 01:00:55 +000089 void RemovePred(SUnit *SU, const SDep &D) {
90 SU->removePred(D);
Dan Gohman54e4c362008-12-09 22:54:47 +000091 }
Dan Gohmanee2e4032008-09-18 16:26:26 +000092
93private:
Dan Gohman54e4c362008-12-09 22:54:47 +000094 void ReleasePred(SUnit *SU, SDep *PredEdge);
Dan Gohman9e64bbb2009-02-10 23:27:53 +000095 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +000096 void ScheduleNodeBottomUp(SUnit*, unsigned);
97 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengc29a56d2009-01-12 03:19:55 +000098 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
99 const TargetRegisterClass*,
100 const TargetRegisterClass*,
101 SmallVector<SUnit*, 2>&);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000102 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
103 void ListScheduleBottomUp();
Dan Gohman3f237442008-12-16 03:25:46 +0000104
105 /// ForceUnitLatencies - The fast scheduler doesn't care about real latencies.
106 bool ForceUnitLatencies() const { return true; }
Dan Gohmanee2e4032008-09-18 16:26:26 +0000107};
108} // end anonymous namespace
109
110
111/// Schedule - Schedule the DAG using list scheduling.
112void ScheduleDAGFast::Schedule() {
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000113 DEBUG(errs() << "********** List Scheduling **********\n");
Dan Gohmanee2e4032008-09-18 16:26:26 +0000114
Dan Gohman086ec992008-09-23 18:50:48 +0000115 NumLiveRegs = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000116 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
117 LiveRegCycles.resize(TRI->getNumRegs(), 0);
118
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000119 // Build the scheduling graph.
120 BuildSchedGraph();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000121
122 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman3cc62432008-11-18 02:06:40 +0000123 SUnits[su].dumpAll(this));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000124
125 // Execute the actual scheduling loop.
126 ListScheduleBottomUp();
127}
128
129//===----------------------------------------------------------------------===//
130// Bottom-Up Scheduling
131//===----------------------------------------------------------------------===//
132
133/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
134/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +0000135void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
136 SUnit *PredSU = PredEdge->getSUnit();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000137 --PredSU->NumSuccsLeft;
138
139#ifndef NDEBUG
140 if (PredSU->NumSuccsLeft < 0) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000141 errs() << "*** Scheduling failed! ***\n";
Dan Gohman3cc62432008-11-18 02:06:40 +0000142 PredSU->dump(this);
Chris Lattner4437ae22009-08-23 07:05:07 +0000143 errs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000144 llvm_unreachable(0);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000145 }
146#endif
147
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000148 // If all the node's successors are scheduled, this node is ready
149 // to be scheduled. Ignore the special EntrySU node.
150 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000151 PredSU->isAvailable = true;
152 AvailableQueue.push(PredSU);
153 }
154}
155
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000156void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000157 // Bottom up: release predecessors
158 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
159 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000160 ReleasePred(SU, &*I);
161 if (I->isAssignedRegDep()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000162 // This is a physical register dependency and it's impossible or
163 // expensive to copy the register. Make sure nothing that can
164 // clobber the register is scheduled between the predecessor and
165 // this node.
Dan Gohman54e4c362008-12-09 22:54:47 +0000166 if (!LiveRegDefs[I->getReg()]) {
Dan Gohman086ec992008-09-23 18:50:48 +0000167 ++NumLiveRegs;
Dan Gohman54e4c362008-12-09 22:54:47 +0000168 LiveRegDefs[I->getReg()] = I->getSUnit();
169 LiveRegCycles[I->getReg()] = CurCycle;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000170 }
171 }
172 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000173}
174
175/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
176/// count of its predecessors. If a predecessor pending count is zero, add it to
177/// the Available queue.
178void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000179 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000180 DEBUG(SU->dump(this));
181
182 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
183 SU->setHeightToAtLeast(CurCycle);
184 Sequence.push_back(SU);
185
186 ReleasePredecessors(SU, CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000187
188 // Release all the implicit physical register defs that are live.
189 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
190 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000191 if (I->isAssignedRegDep()) {
Dan Gohman3f237442008-12-16 03:25:46 +0000192 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohman086ec992008-09-23 18:50:48 +0000193 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman54e4c362008-12-09 22:54:47 +0000194 assert(LiveRegDefs[I->getReg()] == SU &&
Dan Gohmanee2e4032008-09-18 16:26:26 +0000195 "Physical register dependency violated?");
Dan Gohman086ec992008-09-23 18:50:48 +0000196 --NumLiveRegs;
Dan Gohman54e4c362008-12-09 22:54:47 +0000197 LiveRegDefs[I->getReg()] = NULL;
198 LiveRegCycles[I->getReg()] = 0;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000199 }
200 }
201 }
202
203 SU->isScheduled = true;
204}
205
Dan Gohmanee2e4032008-09-18 16:26:26 +0000206/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
207/// successors to the newly created node.
208SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohmand23e0f82008-11-13 23:24:17 +0000209 if (SU->getNode()->getFlaggedNode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000210 return NULL;
211
Dan Gohman550f5af2008-11-13 21:36:12 +0000212 SDNode *N = SU->getNode();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000213 if (!N)
214 return NULL;
215
216 SUnit *NewSU;
217 bool TryUnfold = false;
218 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000219 EVT VT = N->getValueType(i);
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 if (VT == MVT::Flag)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000221 return NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 else if (VT == MVT::Other)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000223 TryUnfold = true;
224 }
225 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
226 const SDValue &Op = N->getOperand(i);
Owen Andersone50ed302009-08-10 22:56:29 +0000227 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 if (VT == MVT::Flag)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000229 return NULL;
230 }
231
232 if (TryUnfold) {
233 SmallVector<SDNode*, 2> NewNodes;
Dan Gohmana23b3b82008-11-13 21:21:28 +0000234 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Dan Gohmanee2e4032008-09-18 16:26:26 +0000235 return NULL;
236
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000237 DEBUG(errs() << "Unfolding SU # " << SU->NodeNum << "\n");
Dan Gohmanee2e4032008-09-18 16:26:26 +0000238 assert(NewNodes.size() == 2 && "Expected a load folding node!");
239
240 N = NewNodes[1];
241 SDNode *LoadNode = NewNodes[0];
242 unsigned NumVals = N->getNumValues();
Dan Gohman550f5af2008-11-13 21:36:12 +0000243 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohmanee2e4032008-09-18 16:26:26 +0000244 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman550f5af2008-11-13 21:36:12 +0000245 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
246 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohmana23b3b82008-11-13 21:21:28 +0000247 SDValue(LoadNode, 1));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000248
Dan Gohmancdb260d2008-11-19 23:39:02 +0000249 SUnit *NewSU = NewSUnit(N);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000250 assert(N->getNodeId() == -1 && "Node already inserted!");
251 N->setNodeId(NewSU->NodeNum);
252
253 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
254 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
255 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
256 NewSU->isTwoAddress = true;
257 break;
258 }
259 }
260 if (TID.isCommutable())
261 NewSU->isCommutable = true;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000262
263 // LoadNode may already exist. This can happen when there is another
264 // load from the same location and producing the same type of value
265 // but it has different alignment or volatileness.
266 bool isNewLoad = true;
267 SUnit *LoadSU;
268 if (LoadNode->getNodeId() != -1) {
269 LoadSU = &SUnits[LoadNode->getNodeId()];
270 isNewLoad = false;
271 } else {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000272 LoadSU = NewSUnit(LoadNode);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000273 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000274 }
275
Dan Gohman54e4c362008-12-09 22:54:47 +0000276 SDep ChainPred;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000277 SmallVector<SDep, 4> ChainSuccs;
278 SmallVector<SDep, 4> LoadPreds;
279 SmallVector<SDep, 4> NodePreds;
280 SmallVector<SDep, 4> NodeSuccs;
281 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
282 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000283 if (I->isCtrl())
284 ChainPred = *I;
285 else if (I->getSUnit()->getNode() &&
286 I->getSUnit()->getNode()->isOperandOf(LoadNode))
287 LoadPreds.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000288 else
Dan Gohman54e4c362008-12-09 22:54:47 +0000289 NodePreds.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000290 }
291 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
292 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000293 if (I->isCtrl())
294 ChainSuccs.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000295 else
Dan Gohman54e4c362008-12-09 22:54:47 +0000296 NodeSuccs.push_back(*I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000297 }
298
Dan Gohman54e4c362008-12-09 22:54:47 +0000299 if (ChainPred.getSUnit()) {
300 RemovePred(SU, ChainPred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000301 if (isNewLoad)
Dan Gohman54e4c362008-12-09 22:54:47 +0000302 AddPred(LoadSU, ChainPred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000303 }
304 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000305 const SDep &Pred = LoadPreds[i];
306 RemovePred(SU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000307 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000308 AddPred(LoadSU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000309 }
310 }
311 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000312 const SDep &Pred = NodePreds[i];
313 RemovePred(SU, Pred);
314 AddPred(NewSU, Pred);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000315 }
316 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000317 SDep D = NodeSuccs[i];
318 SUnit *SuccDep = D.getSUnit();
319 D.setSUnit(SU);
320 RemovePred(SuccDep, D);
321 D.setSUnit(NewSU);
322 AddPred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000323 }
324 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000325 SDep D = ChainSuccs[i];
326 SUnit *SuccDep = D.getSUnit();
327 D.setSUnit(SU);
328 RemovePred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000329 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000330 D.setSUnit(LoadSU);
331 AddPred(SuccDep, D);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000332 }
333 }
334 if (isNewLoad) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000335 AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000336 }
337
338 ++NumUnfolds;
339
340 if (NewSU->NumSuccsLeft == 0) {
341 NewSU->isAvailable = true;
342 return NewSU;
343 }
344 SU = NewSU;
345 }
346
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000347 DEBUG(errs() << "Duplicating SU # " << SU->NodeNum << "\n");
Dan Gohmancdb260d2008-11-19 23:39:02 +0000348 NewSU = Clone(SU);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000349
350 // New SUnit has the exact same predecessors.
351 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
352 I != E; ++I)
Dan Gohman3f237442008-12-16 03:25:46 +0000353 if (!I->isArtificial())
Dan Gohman54e4c362008-12-09 22:54:47 +0000354 AddPred(NewSU, *I);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000355
356 // Only copy scheduled successors. Cut them from old node's successor
357 // list and move them over.
Dan Gohman54e4c362008-12-09 22:54:47 +0000358 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000359 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
360 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000361 if (I->isArtificial())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000362 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000363 SUnit *SuccSU = I->getSUnit();
364 if (SuccSU->isScheduled) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000365 SDep D = *I;
366 D.setSUnit(NewSU);
367 AddPred(SuccSU, D);
368 D.setSUnit(SU);
369 DelDeps.push_back(std::make_pair(SuccSU, D));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000370 }
371 }
Evan Chengc29a56d2009-01-12 03:19:55 +0000372 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000373 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000374
375 ++NumDups;
376 return NewSU;
377}
378
Evan Chengc29a56d2009-01-12 03:19:55 +0000379/// InsertCopiesAndMoveSuccs - Insert register copies and move all
380/// scheduled successors of the given SUnit to the last copy.
381void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000382 const TargetRegisterClass *DestRC,
383 const TargetRegisterClass *SrcRC,
384 SmallVector<SUnit*, 2> &Copies) {
Dan Gohmancdb260d2008-11-19 23:39:02 +0000385 SUnit *CopyFromSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000386 CopyFromSU->CopySrcRC = SrcRC;
387 CopyFromSU->CopyDstRC = DestRC;
388
Dan Gohmancdb260d2008-11-19 23:39:02 +0000389 SUnit *CopyToSU = NewSUnit(static_cast<SDNode *>(NULL));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000390 CopyToSU->CopySrcRC = DestRC;
391 CopyToSU->CopyDstRC = SrcRC;
392
393 // Only copy scheduled successors. Cut them from old node's successor
394 // list and move them over.
Dan Gohman54e4c362008-12-09 22:54:47 +0000395 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000396 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
397 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000398 if (I->isArtificial())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000399 continue;
Dan Gohman54e4c362008-12-09 22:54:47 +0000400 SUnit *SuccSU = I->getSUnit();
401 if (SuccSU->isScheduled) {
402 SDep D = *I;
403 D.setSUnit(CopyToSU);
404 AddPred(SuccSU, D);
405 DelDeps.push_back(std::make_pair(SuccSU, *I));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000406 }
407 }
408 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000409 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000410 }
411
Dan Gohman54e4c362008-12-09 22:54:47 +0000412 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
413 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000414
415 Copies.push_back(CopyFromSU);
416 Copies.push_back(CopyToSU);
417
Evan Chengc29a56d2009-01-12 03:19:55 +0000418 ++NumPRCopies;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000419}
420
421/// getPhysicalRegisterVT - Returns the ValueType of the physical register
422/// definition of the specified node.
423/// FIXME: Move to SelectionDAG?
Owen Andersone50ed302009-08-10 22:56:29 +0000424static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Dan Gohmanee2e4032008-09-18 16:26:26 +0000425 const TargetInstrInfo *TII) {
426 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
427 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
428 unsigned NumRes = TID.getNumDefs();
429 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
430 if (Reg == *ImpDef)
431 break;
432 ++NumRes;
433 }
434 return N->getValueType(NumRes);
435}
436
437/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
438/// scheduling of the given node to satisfy live physical register dependencies.
439/// If the specific node is the last one that's available to schedule, do
440/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
441bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
442 SmallVector<unsigned, 4> &LRegs){
Dan Gohman086ec992008-09-23 18:50:48 +0000443 if (NumLiveRegs == 0)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000444 return false;
445
446 SmallSet<unsigned, 4> RegAdded;
447 // If this node would clobber any "live" register, then it's not ready.
448 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
449 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000450 if (I->isAssignedRegDep()) {
451 unsigned Reg = I->getReg();
452 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000453 if (RegAdded.insert(Reg))
454 LRegs.push_back(Reg);
455 }
456 for (const unsigned *Alias = TRI->getAliasSet(Reg);
457 *Alias; ++Alias)
Dan Gohman54e4c362008-12-09 22:54:47 +0000458 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000459 if (RegAdded.insert(*Alias))
460 LRegs.push_back(*Alias);
461 }
462 }
463 }
464
Dan Gohmand23e0f82008-11-13 23:24:17 +0000465 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
466 if (!Node->isMachineOpcode())
Dan Gohmanee2e4032008-09-18 16:26:26 +0000467 continue;
468 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
469 if (!TID.ImplicitDefs)
470 continue;
471 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohman086ec992008-09-23 18:50:48 +0000472 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000473 if (RegAdded.insert(*Reg))
474 LRegs.push_back(*Reg);
475 }
476 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
477 *Alias; ++Alias)
Dan Gohman086ec992008-09-23 18:50:48 +0000478 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Dan Gohmanee2e4032008-09-18 16:26:26 +0000479 if (RegAdded.insert(*Alias))
480 LRegs.push_back(*Alias);
481 }
482 }
483 }
484 return !LRegs.empty();
485}
486
487
488/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
489/// schedulers.
490void ScheduleDAGFast::ListScheduleBottomUp() {
491 unsigned CurCycle = 0;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000492
493 // Release any predecessors of the special Exit node.
494 ReleasePredecessors(&ExitSU, CurCycle);
495
Dan Gohmanee2e4032008-09-18 16:26:26 +0000496 // 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];
Owen Andersone50ed302009-08-10 22:56:29 +0000538 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengc29a56d2009-01-12 03:19:55 +0000539 const TargetRegisterClass *RC =
540 TRI->getPhysicalRegisterRegClass(Reg, VT);
541 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
542
543 // If cross copy register class is null, then it must be possible copy
544 // the value directly. Do not try duplicate the def.
545 SUnit *NewDef = 0;
546 if (DestRC)
547 NewDef = CopyAndMoveSuccessors(LRDef);
548 else
549 DestRC = RC;
Dan Gohmanee2e4032008-09-18 16:26:26 +0000550 if (!NewDef) {
Evan Chengc29a56d2009-01-12 03:19:55 +0000551 // Issue copies, these can be expensive cross register class copies.
Dan Gohmanee2e4032008-09-18 16:26:26 +0000552 SmallVector<SUnit*, 2> Copies;
Evan Chengc29a56d2009-01-12 03:19:55 +0000553 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000554 DEBUG(errs() << "Adding an edge from SU # " << TrySU->NodeNum
555 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman54e4c362008-12-09 22:54:47 +0000556 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
557 /*Reg=*/0, /*isNormalMemory=*/false,
558 /*isMustAlias=*/false, /*isArtificial=*/true));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000559 NewDef = Copies.back();
560 }
561
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000562 DEBUG(errs() << "Adding an edge from SU # " << NewDef->NodeNum
563 << " to SU #" << TrySU->NodeNum << "\n");
Dan Gohmanee2e4032008-09-18 16:26:26 +0000564 LiveRegDefs[Reg] = NewDef;
Dan Gohman54e4c362008-12-09 22:54:47 +0000565 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
566 /*Reg=*/0, /*isNormalMemory=*/false,
567 /*isMustAlias=*/false, /*isArtificial=*/true));
Dan Gohmanee2e4032008-09-18 16:26:26 +0000568 TrySU->isAvailable = false;
569 CurSU = NewDef;
570 }
571
572 if (!CurSU) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000573 llvm_unreachable("Unable to resolve live physical register dependencies!");
Dan Gohmanee2e4032008-09-18 16:26:26 +0000574 }
575 }
576
577 // Add the nodes that aren't ready back onto the available list.
578 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
579 NotReady[i]->isPending = false;
580 // May no longer be available due to backtracking.
581 if (NotReady[i]->isAvailable)
582 AvailableQueue.push(NotReady[i]);
583 }
584 NotReady.clear();
585
Dan Gohman47d1a212008-11-21 00:10:42 +0000586 if (CurSU)
Dan Gohmanee2e4032008-09-18 16:26:26 +0000587 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000588 ++CurCycle;
589 }
590
Dan Gohman937d2d82009-09-28 16:09:41 +0000591 // Reverse the order since it is bottom up.
Dan Gohmanee2e4032008-09-18 16:26:26 +0000592 std::reverse(Sequence.begin(), Sequence.end());
Dan Gohman937d2d82009-09-28 16:09:41 +0000593
Dan Gohmanee2e4032008-09-18 16:26:26 +0000594#ifndef NDEBUG
Dan Gohman937d2d82009-09-28 16:09:41 +0000595 VerifySchedule(/*isBottomUp=*/true);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000596#endif
597}
598
599//===----------------------------------------------------------------------===//
600// Public Constructor Functions
601//===----------------------------------------------------------------------===//
602
Dan Gohman47ac0f02009-02-11 04:27:20 +0000603llvm::ScheduleDAGSDNodes *
Bill Wendling98a366d2009-04-29 23:29:43 +0000604llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman79ce2762009-01-15 19:20:50 +0000605 return new ScheduleDAGFast(*IS->MF);
Dan Gohmanee2e4032008-09-18 16:26:26 +0000606}