blob: f07bcd5fb1062857386106d33f3a39f9972e51ee [file] [log] [blame]
Evan Chengd38c22b2006-05-11 23:55:42 +00001//===----- ScheduleDAGList.cpp - Reg pressure reduction list scheduler ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Evan Chengd38c22b2006-05-11 23:55:42 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Evan Chenge6f92252007-09-27 18:46:06 +000027#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000029#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000030#include "llvm/ADT/STLExtras.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000031#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000032#include <queue>
33#include "llvm/Support/CommandLine.h"
34using namespace llvm;
35
Dan Gohmanfd227e92008-03-25 17:10:29 +000036STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000037STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000038STATISTIC(NumDups, "Number of duplicated nodes");
39STATISTIC(NumCCCopies, "Number of cross class copies");
40
Jim Laskey95eda5b2006-08-01 14:21:23 +000041static RegisterScheduler
42 burrListDAGScheduler("list-burr",
43 " Bottom-up register reduction list scheduling",
44 createBURRListDAGScheduler);
45static RegisterScheduler
46 tdrListrDAGScheduler("list-tdrr",
47 " Top-down register reduction list scheduling",
48 createTDRRListDAGScheduler);
49
Evan Chengd38c22b2006-05-11 23:55:42 +000050namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000051//===----------------------------------------------------------------------===//
52/// ScheduleDAGRRList - The actual register reduction list scheduler
53/// implementation. This supports both top-down and bottom-up scheduling.
54///
Chris Lattnere097e6f2006-06-28 22:17:39 +000055class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG {
Evan Chengd38c22b2006-05-11 23:55:42 +000056private:
57 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
58 /// it is top-down.
59 bool isBottomUp;
60
61 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000062 SchedulingPriorityQueue *AvailableQueue;
63
Evan Cheng5924bf72007-09-25 01:54:36 +000064 /// LiveRegs / LiveRegDefs - A set of physical registers and their definition
65 /// that are "live". These nodes must be scheduled before any other nodes that
66 /// modifies the registers can be scheduled.
67 SmallSet<unsigned, 4> LiveRegs;
68 std::vector<SUnit*> LiveRegDefs;
69 std::vector<unsigned> LiveRegCycles;
70
Evan Chengd38c22b2006-05-11 23:55:42 +000071public:
72 ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb,
73 const TargetMachine &tm, bool isbottomup,
74 SchedulingPriorityQueue *availqueue)
75 : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup),
76 AvailableQueue(availqueue) {
77 }
78
79 ~ScheduleDAGRRList() {
80 delete AvailableQueue;
81 }
82
83 void Schedule();
84
Roman Levenstein733a4d62008-03-26 11:23:38 +000085 /// IsReachable - Checks if SU is reachable from TargetSU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000086 bool IsReachable(SUnit *SU, SUnit *TargetSU);
87
88 /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
89 /// create a cycle.
90 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU);
91
92 /// AddPred - This adds the specified node X as a predecessor of
93 /// the current node Y if not already.
Roman Levenstein733a4d62008-03-26 11:23:38 +000094 /// This returns true if this is a new predecessor.
95 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000096 bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +000097 unsigned PhyReg = 0, int Cost = 1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000098
Roman Levenstein733a4d62008-03-26 11:23:38 +000099 /// RemovePred - This removes the specified node N from the predecessors of
100 /// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000101 bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isSpecial);
102
Evan Chengd38c22b2006-05-11 23:55:42 +0000103private:
Evan Cheng8e136a92007-09-26 21:36:17 +0000104 void ReleasePred(SUnit*, bool, unsigned);
105 void ReleaseSucc(SUnit*, bool isChain, unsigned);
106 void CapturePred(SUnit*, SUnit*, bool);
107 void ScheduleNodeBottomUp(SUnit*, unsigned);
108 void ScheduleNodeTopDown(SUnit*, unsigned);
109 void UnscheduleNodeBottomUp(SUnit*);
110 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
111 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000112 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +0000113 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000114 const TargetRegisterClass*,
115 SmallVector<SUnit*, 2>&);
116 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000117 void ListScheduleTopDown();
118 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000119 void CommuteNodesToReducePressure();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000120
121
122 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000123 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000124 SUnit *CreateNewSUnit(SDNode *N) {
125 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000126 // Update the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000127 if (NewNode->NodeNum >= Node2Index.size())
128 InitDAGTopologicalSorting();
129 return NewNode;
130 }
131
Roman Levenstein733a4d62008-03-26 11:23:38 +0000132 /// CreateClone - Creates a new SUnit from an existing one.
133 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000134 SUnit *CreateClone(SUnit *N) {
135 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000136 // Update the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137 if (NewNode->NodeNum >= Node2Index.size())
138 InitDAGTopologicalSorting();
139 return NewNode;
140 }
141
142 /// Functions for preserving the topological ordering
143 /// even after dynamic insertions of new edges.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000144 /// This allows a very fast implementation of IsReachable.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000145
146
147 /**
148 The idea of the algorithm is taken from
149 "Online algorithms for managing the topological order of
Roman Levenstein733a4d62008-03-26 11:23:38 +0000150 a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
151 This is the MNR algorithm, which was first introduced by
152 A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000153 "Maintaining a topological order under edge insertions".
154
155 Short description of the algorithm:
156
157 Topological ordering, ord, of a DAG maps each node to a topological
Roman Levenstein733a4d62008-03-26 11:23:38 +0000158 index so that for all edges X->Y it is the case that ord(X) < ord(Y).
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000159
160 This means that if there is a path from the node X to the node Z,
161 then ord(X) < ord(Z).
162
163 This property can be used to check for reachability of nodes:
164 if Z is reachable from X, then an insertion of the edge Z->X would
165 create a cycle.
166
Roman Levenstein733a4d62008-03-26 11:23:38 +0000167 The algorithm first computes a topological ordering for the DAG by initializing
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000168 the Index2Node and Node2Index arrays and then tries to keep the ordering
169 up-to-date after edge insertions by reordering the DAG.
170
171 On insertion of the edge X->Y, the algorithm first marks by calling DFS the
172 nodes reachable from Y, and then shifts them using Shift to lie immediately
173 after X in Index2Node.
174 */
175
Roman Levenstein733a4d62008-03-26 11:23:38 +0000176 /// InitDAGTopologicalSorting - create the initial topological
177 /// ordering from the DAG to be scheduled.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000178 void InitDAGTopologicalSorting();
179
180 /// DFS - make a DFS traversal and mark all nodes affected by the
Roman Levenstein733a4d62008-03-26 11:23:38 +0000181 /// edge insertion. These nodes will later get new topological indexes
182 /// by means of the Shift method.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000183 void DFS(SUnit *SU, int UpperBound, bool& HasLoop);
184
185 /// Shift - reassign topological indexes for the nodes in the DAG
Roman Levenstein733a4d62008-03-26 11:23:38 +0000186 /// to preserve the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000187 void Shift(BitVector& Visited, int LowerBound, int UpperBound);
188
Roman Levenstein733a4d62008-03-26 11:23:38 +0000189 /// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000190 void Allocate(int n, int index);
191
Roman Levenstein733a4d62008-03-26 11:23:38 +0000192 /// Index2Node - Maps topological index to the node number.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000193 std::vector<int> Index2Node;
Roman Levenstein733a4d62008-03-26 11:23:38 +0000194 /// Node2Index - Maps the node number to its topological index.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000195 std::vector<int> Node2Index;
Roman Levenstein733a4d62008-03-26 11:23:38 +0000196 /// Visited - a set of nodes visited during a DFS traversal.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000197 BitVector Visited;
Evan Chengd38c22b2006-05-11 23:55:42 +0000198};
199} // end anonymous namespace
200
201
202/// Schedule - Schedule the DAG using list scheduling.
203void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000204 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000205
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000206 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
207 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000208
Evan Chengd38c22b2006-05-11 23:55:42 +0000209 // Build scheduling units.
210 BuildSchedUnits();
211
Evan Chengd38c22b2006-05-11 23:55:42 +0000212 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000213 SUnits[su].dumpAll(&DAG));
Evan Cheng47fbeda2006-10-14 08:34:06 +0000214 CalculateDepths();
215 CalculateHeights();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000216 InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000217
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000218 AvailableQueue->initNodes(SUnitMap, SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000219
Evan Chengd38c22b2006-05-11 23:55:42 +0000220 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
221 if (isBottomUp)
222 ListScheduleBottomUp();
223 else
224 ListScheduleTopDown();
225
226 AvailableQueue->releaseState();
Dan Gohman54a187e2007-08-20 19:28:38 +0000227
Evan Cheng009f5f52006-05-25 08:37:31 +0000228 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000229
Bill Wendling22e978a2006-12-07 20:04:42 +0000230 DOUT << "*** Final schedule ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000231 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000232 DOUT << "\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000233
234 // Emit in scheduled order
235 EmitSchedule();
236}
237
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000238/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000239/// it is not the last use of its first operand, add it to the CommuteSet if
240/// possible. It will be commuted when it is translated to a MI.
241void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000242 SmallPtrSet<SUnit*, 4> OperandSeen;
Dan Gohman4370f262008-04-15 01:22:18 +0000243 for (unsigned i = Sequence.size(); i != 0; ) {
244 --i;
Evan Chengafed73e2006-05-12 01:58:24 +0000245 SUnit *SU = Sequence[i];
Evan Cheng8e136a92007-09-26 21:36:17 +0000246 if (!SU || !SU->Node) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000247 if (SU->isCommutable) {
248 unsigned Opc = SU->Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000249 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000250 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000251 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000252 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000253 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000254 continue;
255
256 SDNode *OpN = SU->Node->getOperand(j).Val;
Dan Gohmane6e13482008-06-21 15:52:51 +0000257 SUnit *OpSU = isPassiveNode(OpN) ? NULL : SUnitMap[OpN];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000258 if (OpSU && OperandSeen.count(OpSU) == 1) {
259 // Ok, so SU is not the last use of OpSU, but SU is two-address so
260 // it will clobber OpSU. Try to commute SU if no other source operands
261 // are live below.
262 bool DoCommute = true;
263 for (unsigned k = 0; k < NumOps; ++k) {
264 if (k != j) {
265 OpN = SU->Node->getOperand(k).Val;
Dan Gohmane6e13482008-06-21 15:52:51 +0000266 OpSU = isPassiveNode(OpN) ? NULL : SUnitMap[OpN];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000267 if (OpSU && OperandSeen.count(OpSU) == 1) {
268 DoCommute = false;
269 break;
270 }
271 }
Evan Chengafed73e2006-05-12 01:58:24 +0000272 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000273 if (DoCommute)
274 CommuteSet.insert(SU->Node);
Evan Chengafed73e2006-05-12 01:58:24 +0000275 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000276
277 // Only look at the first use&def node for now.
278 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000279 }
280 }
281
Chris Lattnerd86418a2006-08-17 00:09:56 +0000282 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
283 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000284 if (!I->isCtrl)
Dan Gohmane6e13482008-06-21 15:52:51 +0000285 OperandSeen.insert(I->Dep->OrigNode);
Evan Chengafed73e2006-05-12 01:58:24 +0000286 }
287 }
288}
Evan Chengd38c22b2006-05-11 23:55:42 +0000289
290//===----------------------------------------------------------------------===//
291// Bottom-Up Scheduling
292//===----------------------------------------------------------------------===//
293
Evan Chengd38c22b2006-05-11 23:55:42 +0000294/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000295/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000296void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain,
297 unsigned CurCycle) {
298 // FIXME: the distance between two nodes is not always == the predecessor's
299 // latency. For example, the reader can very well read the register written
300 // by the predecessor later than the issue cycle. It also depends on the
301 // interrupt model (drain vs. freeze).
302 PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
303
Evan Cheng038dcc52007-09-28 19:24:24 +0000304 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000305
306#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000307 if (PredSU->NumSuccsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000308 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000309 PredSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000310 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000311 assert(0);
312 }
313#endif
314
Evan Cheng038dcc52007-09-28 19:24:24 +0000315 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000316 PredSU->isAvailable = true;
317 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000318 }
319}
320
321/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
322/// count of its predecessors. If a predecessor pending count is zero, add it to
323/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000324void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000325 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000326 DEBUG(SU->dump(&DAG));
327 SU->Cycle = CurCycle;
328
329 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000330
331 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000332 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000333 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000334 ReleasePred(I->Dep, I->isCtrl, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000335 if (I->Cost < 0) {
336 // This is a physical register dependency and it's impossible or
337 // expensive to copy the register. Make sure nothing that can
338 // clobber the register is scheduled between the predecessor and
339 // this node.
340 if (LiveRegs.insert(I->Reg)) {
341 LiveRegDefs[I->Reg] = I->Dep;
342 LiveRegCycles[I->Reg] = CurCycle;
343 }
344 }
345 }
346
347 // Release all the implicit physical register defs that are live.
348 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
349 I != E; ++I) {
350 if (I->Cost < 0) {
351 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
352 LiveRegs.erase(I->Reg);
353 assert(LiveRegDefs[I->Reg] == SU &&
354 "Physical register dependency violated?");
355 LiveRegDefs[I->Reg] = NULL;
356 LiveRegCycles[I->Reg] = 0;
357 }
358 }
359 }
360
Evan Chengd38c22b2006-05-11 23:55:42 +0000361 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000362}
363
Evan Cheng5924bf72007-09-25 01:54:36 +0000364/// CapturePred - This does the opposite of ReleasePred. Since SU is being
365/// unscheduled, incrcease the succ left count of its predecessors. Remove
366/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000367void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
368 unsigned CycleBound = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000369 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
370 I != E; ++I) {
371 if (I->Dep == SU)
372 continue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000373 CycleBound = std::max(CycleBound,
374 I->Dep->Cycle + PredSU->Latency);
Evan Cheng5924bf72007-09-25 01:54:36 +0000375 }
376
377 if (PredSU->isAvailable) {
378 PredSU->isAvailable = false;
379 if (!PredSU->isPending)
380 AvailableQueue->remove(PredSU);
381 }
382
Roman Levenstein6b371142008-04-29 09:07:59 +0000383 PredSU->CycleBound = CycleBound;
Evan Cheng038dcc52007-09-28 19:24:24 +0000384 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000385}
386
387/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
388/// its predecessor states to reflect the change.
389void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
390 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
391 DEBUG(SU->dump(&DAG));
392
393 AvailableQueue->UnscheduledNode(SU);
394
395 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
396 I != E; ++I) {
397 CapturePred(I->Dep, SU, I->isCtrl);
398 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
399 LiveRegs.erase(I->Reg);
400 assert(LiveRegDefs[I->Reg] == I->Dep &&
401 "Physical register dependency violated?");
402 LiveRegDefs[I->Reg] = NULL;
403 LiveRegCycles[I->Reg] = 0;
404 }
405 }
406
407 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
408 I != E; ++I) {
409 if (I->Cost < 0) {
410 if (LiveRegs.insert(I->Reg)) {
411 assert(!LiveRegDefs[I->Reg] &&
412 "Physical register dependency violated?");
413 LiveRegDefs[I->Reg] = SU;
414 }
415 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
416 LiveRegCycles[I->Reg] = I->Dep->Cycle;
417 }
418 }
419
420 SU->Cycle = 0;
421 SU->isScheduled = false;
422 SU->isAvailable = true;
423 AvailableQueue->push(SU);
424}
425
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000426/// IsReachable - Checks if SU is reachable from TargetSU.
427bool ScheduleDAGRRList::IsReachable(SUnit *SU, SUnit *TargetSU) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000428 // If insertion of the edge SU->TargetSU would create a cycle
429 // then there is a path from TargetSU to SU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000430 int UpperBound, LowerBound;
431 LowerBound = Node2Index[TargetSU->NodeNum];
432 UpperBound = Node2Index[SU->NodeNum];
433 bool HasLoop = false;
434 // Is Ord(TargetSU) < Ord(SU) ?
435 if (LowerBound < UpperBound) {
436 Visited.reset();
437 // There may be a path from TargetSU to SU. Check for it.
438 DFS(TargetSU, UpperBound, HasLoop);
Evan Chengcfd5f822007-09-27 00:25:29 +0000439 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000440 return HasLoop;
Evan Chengcfd5f822007-09-27 00:25:29 +0000441}
442
Roman Levenstein733a4d62008-03-26 11:23:38 +0000443/// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000444inline void ScheduleDAGRRList::Allocate(int n, int index) {
445 Node2Index[n] = index;
446 Index2Node[index] = n;
Evan Chengcfd5f822007-09-27 00:25:29 +0000447}
448
Roman Levenstein733a4d62008-03-26 11:23:38 +0000449/// InitDAGTopologicalSorting - create the initial topological
450/// ordering from the DAG to be scheduled.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000451void ScheduleDAGRRList::InitDAGTopologicalSorting() {
452 unsigned DAGSize = SUnits.size();
453 std::vector<unsigned> InDegree(DAGSize);
454 std::vector<SUnit*> WorkList;
455 WorkList.reserve(DAGSize);
456 std::vector<SUnit*> TopOrder;
457 TopOrder.reserve(DAGSize);
458
Roman Levenstein733a4d62008-03-26 11:23:38 +0000459 // Initialize the data structures.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000460 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
461 SUnit *SU = &SUnits[i];
462 int NodeNum = SU->NodeNum;
463 unsigned Degree = SU->Succs.size();
464 InDegree[NodeNum] = Degree;
465
466 // Is it a node without dependencies?
467 if (Degree == 0) {
468 assert(SU->Succs.empty() && "SUnit should have no successors");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000469 // Collect leaf nodes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000470 WorkList.push_back(SU);
471 }
472 }
473
474 while (!WorkList.empty()) {
475 SUnit *SU = WorkList.back();
476 WorkList.pop_back();
477 TopOrder.push_back(SU);
478 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
479 I != E; ++I) {
480 SUnit *SU = I->Dep;
481 if (!--InDegree[SU->NodeNum])
482 // If all dependencies of the node are processed already,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000483 // then the node can be computed now.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000484 WorkList.push_back(SU);
485 }
486 }
487
488 // Second pass, assign the actual topological order as node ids.
489 int Id = 0;
490
491 Index2Node.clear();
492 Node2Index.clear();
493 Index2Node.resize(DAGSize);
494 Node2Index.resize(DAGSize);
495 Visited.resize(DAGSize);
496
497 for (std::vector<SUnit*>::reverse_iterator TI = TopOrder.rbegin(),
498 TE = TopOrder.rend();TI != TE; ++TI) {
499 Allocate((*TI)->NodeNum, Id);
500 Id++;
501 }
502
503#ifndef NDEBUG
504 // Check correctness of the ordering
505 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
506 SUnit *SU = &SUnits[i];
507 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
508 I != E; ++I) {
509 assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] &&
510 "Wrong topological sorting");
511 }
512 }
513#endif
514}
515
Roman Levenstein733a4d62008-03-26 11:23:38 +0000516/// AddPred - adds an edge from SUnit X to SUnit Y.
517/// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000518bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
519 unsigned PhyReg, int Cost) {
520 int UpperBound, LowerBound;
521 LowerBound = Node2Index[Y->NodeNum];
522 UpperBound = Node2Index[X->NodeNum];
523 bool HasLoop = false;
524 // Is Ord(X) < Ord(Y) ?
525 if (LowerBound < UpperBound) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000526 // Update the topological order.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000527 Visited.reset();
528 DFS(Y, UpperBound, HasLoop);
529 assert(!HasLoop && "Inserted edge creates a loop!");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000530 // Recompute topological indexes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000531 Shift(Visited, LowerBound, UpperBound);
532 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000533 // Now really insert the edge.
534 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000535}
536
Roman Levenstein733a4d62008-03-26 11:23:38 +0000537/// RemovePred - This removes the specified node N from the predecessors of
538/// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000539bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N,
540 bool isCtrl, bool isSpecial) {
541 // InitDAGTopologicalSorting();
542 return M->removePred(N, isCtrl, isSpecial);
543}
544
Roman Levenstein733a4d62008-03-26 11:23:38 +0000545/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
546/// all nodes affected by the edge insertion. These nodes will later get new
547/// topological indexes by means of the Shift method.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000548void ScheduleDAGRRList::DFS(SUnit *SU, int UpperBound, bool& HasLoop) {
549 std::vector<SUnit*> WorkList;
550 WorkList.reserve(SUnits.size());
551
552 WorkList.push_back(SU);
553 while (!WorkList.empty()) {
554 SU = WorkList.back();
555 WorkList.pop_back();
556 Visited.set(SU->NodeNum);
557 for (int I = SU->Succs.size()-1; I >= 0; --I) {
558 int s = SU->Succs[I].Dep->NodeNum;
559 if (Node2Index[s] == UpperBound) {
560 HasLoop = true;
561 return;
562 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000563 // Visit successors if not already and in affected region.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000564 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
565 WorkList.push_back(SU->Succs[I].Dep);
566 }
567 }
568 }
569}
570
Roman Levenstein733a4d62008-03-26 11:23:38 +0000571/// Shift - Renumber the nodes so that the topological ordering is
572/// preserved.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000573void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound,
574 int UpperBound) {
575 std::vector<int> L;
576 int shift = 0;
577 int i;
578
579 for (i = LowerBound; i <= UpperBound; ++i) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000580 // w is node at topological index i.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000581 int w = Index2Node[i];
582 if (Visited.test(w)) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000583 // Unmark.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000584 Visited.reset(w);
585 L.push_back(w);
586 shift = shift + 1;
587 } else {
588 Allocate(w, i - shift);
589 }
590 }
591
592 for (unsigned j = 0; j < L.size(); ++j) {
593 Allocate(L[j], i - shift);
594 i = i + 1;
595 }
596}
597
598
Dan Gohmanfd227e92008-03-25 17:10:29 +0000599/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Evan Chengcfd5f822007-09-27 00:25:29 +0000600/// create a cycle.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000601bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
602 if (IsReachable(TargetSU, SU))
Evan Chengcfd5f822007-09-27 00:25:29 +0000603 return true;
604 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
605 I != E; ++I)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000606 if (I->Cost < 0 && IsReachable(TargetSU, I->Dep))
Evan Chengcfd5f822007-09-27 00:25:29 +0000607 return true;
608 return false;
609}
610
Evan Cheng8e136a92007-09-26 21:36:17 +0000611/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000612/// BTCycle in order to schedule a specific node. Returns the last unscheduled
613/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000614void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
615 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000616 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000617 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000618 OldSU = Sequence.back();
619 Sequence.pop_back();
620 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000621 // Don't try to remove SU from AvailableQueue.
622 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000623 UnscheduleNodeBottomUp(OldSU);
624 --CurCycle;
625 }
626
627
628 if (SU->isSucc(OldSU)) {
629 assert(false && "Something is wrong!");
630 abort();
631 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000632
633 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000634}
635
Evan Cheng5924bf72007-09-25 01:54:36 +0000636/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
637/// successors to the newly created node.
638SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Evan Cheng79e97132007-10-05 01:39:18 +0000639 if (SU->FlaggedNodes.size())
640 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000641
Evan Cheng79e97132007-10-05 01:39:18 +0000642 SDNode *N = SU->Node;
643 if (!N)
644 return NULL;
645
646 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000647 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000648 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000649 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000650 if (VT == MVT::Flag)
651 return NULL;
652 else if (VT == MVT::Other)
653 TryUnfold = true;
654 }
Evan Cheng79e97132007-10-05 01:39:18 +0000655 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
656 const SDOperand &Op = N->getOperand(i);
Duncan Sands13237ac2008-06-06 12:08:01 +0000657 MVT VT = Op.Val->getValueType(Op.ResNo);
Evan Cheng79e97132007-10-05 01:39:18 +0000658 if (VT == MVT::Flag)
659 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000660 }
661
662 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000663 SmallVector<SDNode*, 2> NewNodes;
Owen Anderson0ec92e92008-01-07 01:35:56 +0000664 if (!TII->unfoldMemoryOperand(DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000665 return NULL;
666
667 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
668 assert(NewNodes.size() == 2 && "Expected a load folding node!");
669
670 N = NewNodes[1];
671 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000672 unsigned NumVals = N->getNumValues();
673 unsigned OldNumVals = SU->Node->getNumValues();
674 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattner3cfb56d2007-10-15 06:10:22 +0000675 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, i), SDOperand(N, i));
Evan Cheng79e97132007-10-05 01:39:18 +0000676 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, OldNumVals-1),
Chris Lattner3cfb56d2007-10-15 06:10:22 +0000677 SDOperand(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000678
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000679 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohmane6e13482008-06-21 15:52:51 +0000680 bool isNew = SUnitMap.insert(std::make_pair(N, NewSU));
681 isNew = isNew;
682 assert(isNew && "Node already inserted!");
683
Chris Lattner03ad8852008-01-07 07:27:27 +0000684 const TargetInstrDesc &TID = TII->get(N->getTargetOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000685 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000686 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000687 NewSU->isTwoAddress = true;
688 break;
689 }
690 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000691 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000692 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000693 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000694 NewSU->Depth = SU->Depth;
695 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000696 ComputeLatency(NewSU);
697
Evan Cheng91e0fc92007-12-18 08:42:10 +0000698 // LoadNode may already exist. This can happen when there is another
699 // load from the same location and producing the same type of value
700 // but it has different alignment or volatileness.
701 bool isNewLoad = true;
702 SUnit *LoadSU;
Dan Gohmane6e13482008-06-21 15:52:51 +0000703 DenseMap<SDNode*, SUnit*>::iterator SMI = SUnitMap.find(LoadNode);
Evan Cheng91e0fc92007-12-18 08:42:10 +0000704 if (SMI != SUnitMap.end()) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000705 LoadSU = SMI->second;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000706 isNewLoad = false;
707 } else {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000708 LoadSU = CreateNewSUnit(LoadNode);
Dan Gohmane6e13482008-06-21 15:52:51 +0000709 bool isNew = SUnitMap.insert(std::make_pair(LoadNode, LoadSU));
710 isNew = isNew;
711 assert(isNew && "Node already inserted!");
Evan Cheng91e0fc92007-12-18 08:42:10 +0000712
713 LoadSU->Depth = SU->Depth;
714 LoadSU->Height = SU->Height;
715 ComputeLatency(LoadSU);
716 }
717
Evan Cheng79e97132007-10-05 01:39:18 +0000718 SUnit *ChainPred = NULL;
719 SmallVector<SDep, 4> ChainSuccs;
720 SmallVector<SDep, 4> LoadPreds;
721 SmallVector<SDep, 4> NodePreds;
722 SmallVector<SDep, 4> NodeSuccs;
723 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
724 I != E; ++I) {
725 if (I->isCtrl)
726 ChainPred = I->Dep;
Evan Cheng567d2e52008-03-04 00:41:45 +0000727 else if (I->Dep->Node && I->Dep->Node->isOperandOf(LoadNode))
Evan Cheng79e97132007-10-05 01:39:18 +0000728 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
729 else
730 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
731 }
732 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
733 I != E; ++I) {
734 if (I->isCtrl)
735 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
736 I->isCtrl, I->isSpecial));
737 else
738 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
739 I->isCtrl, I->isSpecial));
740 }
741
Dan Gohman4370f262008-04-15 01:22:18 +0000742 if (ChainPred) {
743 RemovePred(SU, ChainPred, true, false);
744 if (isNewLoad)
745 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000746 }
Evan Cheng79e97132007-10-05 01:39:18 +0000747 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
748 SDep *Pred = &LoadPreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000749 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
750 if (isNewLoad) {
751 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000752 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000753 }
Evan Cheng79e97132007-10-05 01:39:18 +0000754 }
755 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
756 SDep *Pred = &NodePreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000757 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
758 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000759 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000760 }
761 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
762 SDep *Succ = &NodeSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000763 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
764 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000765 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000766 }
767 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
768 SDep *Succ = &ChainSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000769 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
770 if (isNewLoad) {
771 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000772 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000773 }
Evan Cheng79e97132007-10-05 01:39:18 +0000774 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000775 if (isNewLoad) {
776 AddPred(NewSU, LoadSU, false, false);
777 }
Evan Cheng79e97132007-10-05 01:39:18 +0000778
Evan Cheng91e0fc92007-12-18 08:42:10 +0000779 if (isNewLoad)
780 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000781 AvailableQueue->addNode(NewSU);
782
783 ++NumUnfolds;
784
785 if (NewSU->NumSuccsLeft == 0) {
786 NewSU->isAvailable = true;
787 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000788 }
789 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000790 }
791
792 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000793 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000794
795 // New SUnit has the exact same predecessors.
796 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
797 I != E; ++I)
798 if (!I->isSpecial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000799 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000800 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
801 }
802
803 // Only copy scheduled successors. Cut them from old node's successor
804 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000805 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000806 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
807 I != E; ++I) {
808 if (I->isSpecial)
809 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000810 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000811 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000812 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000813 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000814 }
815 }
816 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000817 SUnit *Succ = DelDeps[i].first;
818 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000819 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000820 }
821
822 AvailableQueue->updateNode(SU);
823 AvailableQueue->addNode(NewSU);
824
Evan Cheng1ec79b42007-09-27 07:09:03 +0000825 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000826 return NewSU;
827}
828
Evan Cheng1ec79b42007-09-27 07:09:03 +0000829/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
830/// and move all scheduled successors of the given SUnit to the last copy.
831void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
832 const TargetRegisterClass *DestRC,
833 const TargetRegisterClass *SrcRC,
834 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000835 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000836 CopyFromSU->CopySrcRC = SrcRC;
837 CopyFromSU->CopyDstRC = DestRC;
838 CopyFromSU->Depth = SU->Depth;
839 CopyFromSU->Height = SU->Height;
840
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000841 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000842 CopyToSU->CopySrcRC = DestRC;
843 CopyToSU->CopyDstRC = SrcRC;
844
845 // Only copy scheduled successors. Cut them from old node's successor
846 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000847 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000848 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
849 I != E; ++I) {
850 if (I->isSpecial)
851 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000852 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000853 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000854 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000855 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000856 }
857 }
858 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000859 SUnit *Succ = DelDeps[i].first;
860 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000861 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000862 }
863
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000864 AddPred(CopyFromSU, SU, false, false, Reg, -1);
865 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000866
867 AvailableQueue->updateNode(SU);
868 AvailableQueue->addNode(CopyFromSU);
869 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000870 Copies.push_back(CopyFromSU);
871 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000872
Evan Cheng1ec79b42007-09-27 07:09:03 +0000873 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000874}
875
876/// getPhysicalRegisterVT - Returns the ValueType of the physical register
877/// definition of the specified node.
878/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000879static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
880 const TargetInstrInfo *TII) {
Chris Lattner03ad8852008-01-07 07:27:27 +0000881 const TargetInstrDesc &TID = TII->get(N->getTargetOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000882 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000883 unsigned NumRes = TID.getNumDefs();
884 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000885 if (Reg == *ImpDef)
886 break;
887 ++NumRes;
888 }
889 return N->getValueType(NumRes);
890}
891
Evan Cheng5924bf72007-09-25 01:54:36 +0000892/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
893/// scheduling of the given node to satisfy live physical register dependencies.
894/// If the specific node is the last one that's available to schedule, do
895/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000896bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
897 SmallVector<unsigned, 4> &LRegs){
Evan Cheng5924bf72007-09-25 01:54:36 +0000898 if (LiveRegs.empty())
899 return false;
900
Evan Chenge6f92252007-09-27 18:46:06 +0000901 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000902 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000903 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
904 I != E; ++I) {
905 if (I->Cost < 0) {
906 unsigned Reg = I->Reg;
Evan Chenge6f92252007-09-27 18:46:06 +0000907 if (LiveRegs.count(Reg) && LiveRegDefs[Reg] != I->Dep) {
908 if (RegAdded.insert(Reg))
909 LRegs.push_back(Reg);
910 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000911 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000912 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000913 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != I->Dep) {
914 if (RegAdded.insert(*Alias))
915 LRegs.push_back(*Alias);
916 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000917 }
918 }
919
920 for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) {
921 SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1];
Evan Cheng8e136a92007-09-26 21:36:17 +0000922 if (!Node || !Node->isTargetOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000923 continue;
Chris Lattner03ad8852008-01-07 07:27:27 +0000924 const TargetInstrDesc &TID = TII->get(Node->getTargetOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000925 if (!TID.ImplicitDefs)
926 continue;
927 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Evan Chenge6f92252007-09-27 18:46:06 +0000928 if (LiveRegs.count(*Reg) && LiveRegDefs[*Reg] != SU) {
929 if (RegAdded.insert(*Reg))
930 LRegs.push_back(*Reg);
931 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000932 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000933 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000934 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != SU) {
935 if (RegAdded.insert(*Alias))
936 LRegs.push_back(*Alias);
937 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000938 }
939 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000940 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000941}
942
Evan Cheng1ec79b42007-09-27 07:09:03 +0000943
Evan Chengd38c22b2006-05-11 23:55:42 +0000944/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
945/// schedulers.
946void ScheduleDAGRRList::ListScheduleBottomUp() {
947 unsigned CurCycle = 0;
948 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000949 if (!SUnits.empty()) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000950 SUnit *RootSU = SUnitMap[DAG.getRoot().Val];
Dan Gohman4370f262008-04-15 01:22:18 +0000951 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
952 RootSU->isAvailable = true;
953 AvailableQueue->push(RootSU);
954 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000955
956 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000957 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000958 SmallVector<SUnit*, 4> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +0000959 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000960 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000961 bool Delayed = false;
962 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Evan Cheng5924bf72007-09-25 01:54:36 +0000963 SUnit *CurSU = AvailableQueue->pop();
964 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000965 if (CurSU->CycleBound <= CurCycle) {
966 SmallVector<unsigned, 4> LRegs;
967 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000968 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000969 Delayed = true;
970 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000971 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000972
973 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
974 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000975 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000976 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000977
978 // All candidates are delayed due to live physical reg dependencies.
979 // Try backtracking, code duplication, or inserting cross class copies
980 // to resolve it.
981 if (Delayed && !CurSU) {
982 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
983 SUnit *TrySU = NotReady[i];
984 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
985
986 // Try unscheduling up to the point where it's safe to schedule
987 // this node.
988 unsigned LiveCycle = CurCycle;
989 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
990 unsigned Reg = LRegs[j];
991 unsigned LCycle = LiveRegCycles[Reg];
992 LiveCycle = std::min(LiveCycle, LCycle);
993 }
994 SUnit *OldSU = Sequence[LiveCycle];
995 if (!WillCreateCycle(TrySU, OldSU)) {
996 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
997 // Force the current node to be scheduled before the node that
998 // requires the physical reg dep.
999 if (OldSU->isAvailable) {
1000 OldSU->isAvailable = false;
1001 AvailableQueue->remove(OldSU);
1002 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001003 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001004 // If one or more successors has been unscheduled, then the current
1005 // node is no longer avaialable. Schedule a successor that's now
1006 // available instead.
1007 if (!TrySU->isAvailable)
1008 CurSU = AvailableQueue->pop();
1009 else {
1010 CurSU = TrySU;
1011 TrySU->isPending = false;
1012 NotReady.erase(NotReady.begin()+i);
1013 }
1014 break;
1015 }
1016 }
1017
1018 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +00001019 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +00001020 // "expensive to copy" values to break the dependency. In case even
1021 // that doesn't work, insert cross class copies.
1022 SUnit *TrySU = NotReady[0];
1023 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1024 assert(LRegs.size() == 1 && "Can't handle this yet!");
1025 unsigned Reg = LRegs[0];
1026 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +00001027 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
1028 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +00001029 // Issue expensive cross register class copies.
Duncan Sands13237ac2008-06-06 12:08:01 +00001030 MVT VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001031 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +00001032 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001033 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001034 if (!DestRC) {
1035 assert(false && "Don't know how to copy this physical register!");
1036 abort();
1037 }
1038 SmallVector<SUnit*, 2> Copies;
1039 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1040 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
1041 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001042 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001043 NewDef = Copies.back();
1044 }
1045
1046 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
1047 << " to SU #" << TrySU->NodeNum << "\n";
1048 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001049 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001050 TrySU->isAvailable = false;
1051 CurSU = NewDef;
1052 }
1053
1054 if (!CurSU) {
1055 assert(false && "Unable to resolve live physical register dependencies!");
1056 abort();
1057 }
1058 }
1059
Evan Chengd38c22b2006-05-11 23:55:42 +00001060 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +00001061 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
1062 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +00001063 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +00001064 if (NotReady[i]->isAvailable)
1065 AvailableQueue->push(NotReady[i]);
1066 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001067 NotReady.clear();
1068
Evan Cheng5924bf72007-09-25 01:54:36 +00001069 if (!CurSU)
1070 Sequence.push_back(0);
1071 else {
1072 ScheduleNodeBottomUp(CurSU, CurCycle);
1073 Sequence.push_back(CurSU);
1074 }
1075 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001076 }
1077
Evan Chengd38c22b2006-05-11 23:55:42 +00001078 // Reverse the order if it is bottom up.
1079 std::reverse(Sequence.begin(), Sequence.end());
1080
1081
1082#ifndef NDEBUG
1083 // Verify that all SUnits were scheduled.
1084 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001085 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001086 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001087 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman4370f262008-04-15 01:22:18 +00001088 if (!SUnits[i].isScheduled) {
1089 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1090 ++DeadNodes;
1091 continue;
1092 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001093 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001094 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001095 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001096 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001097 AnyNotSched = true;
1098 }
Dan Gohman4370f262008-04-15 01:22:18 +00001099 if (SUnits[i].NumSuccsLeft != 0) {
1100 if (!AnyNotSched)
1101 cerr << "*** List scheduling failed! ***\n";
1102 SUnits[i].dump(&DAG);
1103 cerr << "has successors left!\n";
1104 AnyNotSched = true;
1105 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001106 }
Dan Gohman82b66732008-04-15 22:40:14 +00001107 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1108 if (!Sequence[i])
1109 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001110 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001111 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001112 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001113#endif
1114}
1115
1116//===----------------------------------------------------------------------===//
1117// Top-Down Scheduling
1118//===----------------------------------------------------------------------===//
1119
1120/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001121/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +00001122void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain,
1123 unsigned CurCycle) {
1124 // FIXME: the distance between two nodes is not always == the predecessor's
1125 // latency. For example, the reader can very well read the register written
1126 // by the predecessor later than the issue cycle. It also depends on the
1127 // interrupt model (drain vs. freeze).
1128 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
1129
Evan Cheng038dcc52007-09-28 19:24:24 +00001130 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +00001131
1132#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +00001133 if (SuccSU->NumPredsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001134 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001135 SuccSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001136 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001137 assert(0);
1138 }
1139#endif
1140
Evan Cheng038dcc52007-09-28 19:24:24 +00001141 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001142 SuccSU->isAvailable = true;
1143 AvailableQueue->push(SuccSU);
1144 }
1145}
1146
1147
1148/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1149/// count of its successors. If a successor pending count is zero, add it to
1150/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +00001151void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001152 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +00001153 DEBUG(SU->dump(&DAG));
1154 SU->Cycle = CurCycle;
1155
1156 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001157
1158 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +00001159 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1160 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001161 ReleaseSucc(I->Dep, I->isCtrl, CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +00001162 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +00001163}
1164
Dan Gohman54a187e2007-08-20 19:28:38 +00001165/// ListScheduleTopDown - The main loop of list scheduling for top-down
1166/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001167void ScheduleDAGRRList::ListScheduleTopDown() {
1168 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001169
1170 // All leaves to Available queue.
1171 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1172 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001173 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001174 AvailableQueue->push(&SUnits[i]);
1175 SUnits[i].isAvailable = true;
1176 }
1177 }
1178
Evan Chengd38c22b2006-05-11 23:55:42 +00001179 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001180 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +00001181 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +00001182 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001183 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001184 SUnit *CurSU = AvailableQueue->pop();
1185 while (CurSU && CurSU->CycleBound > CurCycle) {
1186 NotReady.push_back(CurSU);
1187 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +00001188 }
1189
1190 // Add the nodes that aren't ready back onto the available list.
1191 AvailableQueue->push_all(NotReady);
1192 NotReady.clear();
1193
Evan Cheng5924bf72007-09-25 01:54:36 +00001194 if (!CurSU)
1195 Sequence.push_back(0);
1196 else {
1197 ScheduleNodeTopDown(CurSU, CurCycle);
1198 Sequence.push_back(CurSU);
1199 }
Dan Gohman4370f262008-04-15 01:22:18 +00001200 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001201 }
1202
1203
1204#ifndef NDEBUG
1205 // Verify that all SUnits were scheduled.
1206 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001207 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001208 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001209 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1210 if (!SUnits[i].isScheduled) {
Dan Gohman4370f262008-04-15 01:22:18 +00001211 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1212 ++DeadNodes;
1213 continue;
1214 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001215 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001216 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001217 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001218 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001219 AnyNotSched = true;
1220 }
Dan Gohman4370f262008-04-15 01:22:18 +00001221 if (SUnits[i].NumPredsLeft != 0) {
1222 if (!AnyNotSched)
1223 cerr << "*** List scheduling failed! ***\n";
1224 SUnits[i].dump(&DAG);
1225 cerr << "has predecessors left!\n";
1226 AnyNotSched = true;
1227 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001228 }
Dan Gohman82b66732008-04-15 22:40:14 +00001229 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1230 if (!Sequence[i])
1231 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001232 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001233 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001234 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001235#endif
1236}
1237
1238
1239
1240//===----------------------------------------------------------------------===//
1241// RegReductionPriorityQueue Implementation
1242//===----------------------------------------------------------------------===//
1243//
1244// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1245// to reduce register pressure.
1246//
1247namespace {
1248 template<class SF>
1249 class RegReductionPriorityQueue;
1250
1251 /// Sorting functions for the Available queue.
1252 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1253 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1254 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1255 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1256
1257 bool operator()(const SUnit* left, const SUnit* right) const;
1258 };
1259
1260 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1261 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1262 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1263 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1264
1265 bool operator()(const SUnit* left, const SUnit* right) const;
1266 };
1267} // end anonymous namespace
1268
Evan Cheng961bbd32007-01-08 23:50:38 +00001269static inline bool isCopyFromLiveIn(const SUnit *SU) {
1270 SDNode *N = SU->Node;
Evan Cheng8e136a92007-09-26 21:36:17 +00001271 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +00001272 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
1273}
1274
Evan Chengd38c22b2006-05-11 23:55:42 +00001275namespace {
1276 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001277 class VISIBILITY_HIDDEN RegReductionPriorityQueue
1278 : public SchedulingPriorityQueue {
Roman Levenstein6b371142008-04-29 09:07:59 +00001279 std::set<SUnit*, SF> Queue;
1280 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001281
1282 public:
1283 RegReductionPriorityQueue() :
Roman Levenstein6b371142008-04-29 09:07:59 +00001284 Queue(SF(this)), currentQueueId(0) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001285
Dan Gohmane6e13482008-06-21 15:52:51 +00001286 virtual void initNodes(DenseMap<SDNode*, SUnit*> &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001287 std::vector<SUnit> &sunits) {}
Evan Cheng5924bf72007-09-25 01:54:36 +00001288
1289 virtual void addNode(const SUnit *SU) {}
1290
1291 virtual void updateNode(const SUnit *SU) {}
1292
Evan Chengd38c22b2006-05-11 23:55:42 +00001293 virtual void releaseState() {}
1294
Evan Cheng6730f032007-01-08 23:55:53 +00001295 virtual unsigned getNodePriority(const SUnit *SU) const {
Evan Chengd38c22b2006-05-11 23:55:42 +00001296 return 0;
1297 }
1298
Evan Cheng5924bf72007-09-25 01:54:36 +00001299 unsigned size() const { return Queue.size(); }
1300
Evan Chengd38c22b2006-05-11 23:55:42 +00001301 bool empty() const { return Queue.empty(); }
1302
1303 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001304 assert(!U->NodeQueueId && "Node in the queue already");
1305 U->NodeQueueId = ++currentQueueId;
1306 Queue.insert(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001307 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001308
Evan Chengd38c22b2006-05-11 23:55:42 +00001309 void push_all(const std::vector<SUnit *> &Nodes) {
1310 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001311 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001312 }
1313
1314 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001315 if (empty()) return NULL;
Roman Levenstein6b371142008-04-29 09:07:59 +00001316 typename std::set<SUnit*, SF>::iterator i = prior(Queue.end());
1317 SUnit *V = *i;
1318 Queue.erase(i);
1319 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001320 return V;
1321 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001322
Evan Cheng5924bf72007-09-25 01:54:36 +00001323 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001324 assert(!Queue.empty() && "Queue is empty!");
1325 size_t RemovedNum = Queue.erase(SU);
Duncan Sands70424d12008-05-16 09:19:16 +00001326 RemovedNum = RemovedNum; // Silence compiler warning.
Roman Levenstein6b371142008-04-29 09:07:59 +00001327 assert(RemovedNum > 0 && "Not in queue!");
1328 assert(RemovedNum == 1 && "Multiple times in the queue!");
1329 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001330 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001331 };
1332
Chris Lattner996795b2006-06-28 23:17:24 +00001333 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001334 : public RegReductionPriorityQueue<bu_ls_rr_sort> {
Evan Cheng5924bf72007-09-25 01:54:36 +00001335 // SUnitMap SDNode to SUnit mapping (n -> n).
Dan Gohmane6e13482008-06-21 15:52:51 +00001336 DenseMap<SDNode*, SUnit*> *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001337
Evan Chengd38c22b2006-05-11 23:55:42 +00001338 // SUnits - The SUnits for the current graph.
1339 const std::vector<SUnit> *SUnits;
1340
1341 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001342 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001343
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001344 const TargetInstrInfo *TII;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001345 const TargetRegisterInfo *TRI;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001346 ScheduleDAGRRList *scheduleDAG;
Evan Chengd38c22b2006-05-11 23:55:42 +00001347 public:
Evan Chengf9891412007-12-20 09:25:31 +00001348 explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001349 const TargetRegisterInfo *tri)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001350 : TII(tii), TRI(tri), scheduleDAG(NULL) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001351
Dan Gohmane6e13482008-06-21 15:52:51 +00001352 void initNodes(DenseMap<SDNode*, SUnit*> &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001353 std::vector<SUnit> &sunits) {
1354 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +00001355 SUnits = &sunits;
1356 // Add pseudo dependency edges for two-address nodes.
Evan Chengafed73e2006-05-12 01:58:24 +00001357 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001358 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001359 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001360 }
1361
Evan Cheng5924bf72007-09-25 01:54:36 +00001362 void addNode(const SUnit *SU) {
1363 SethiUllmanNumbers.resize(SUnits->size(), 0);
1364 CalcNodeSethiUllmanNumber(SU);
1365 }
1366
1367 void updateNode(const SUnit *SU) {
1368 SethiUllmanNumbers[SU->NodeNum] = 0;
1369 CalcNodeSethiUllmanNumber(SU);
1370 }
1371
Evan Chengd38c22b2006-05-11 23:55:42 +00001372 void releaseState() {
1373 SUnits = 0;
1374 SethiUllmanNumbers.clear();
1375 }
1376
Evan Cheng6730f032007-01-08 23:55:53 +00001377 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001378 assert(SU->NodeNum < SethiUllmanNumbers.size());
Evan Cheng8e136a92007-09-26 21:36:17 +00001379 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001380 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1381 // CopyFromReg should be close to its def because it restricts
1382 // allocation choices. But if it is a livein then perhaps we want it
1383 // closer to its uses so it can be coalesced.
1384 return 0xffff;
1385 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1386 // CopyToReg should be close to its uses to facilitate coalescing and
1387 // avoid spilling.
1388 return 0;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001389 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1390 Opc == TargetInstrInfo::INSERT_SUBREG)
1391 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1392 // facilitate coalescing.
1393 return 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001394 else if (SU->NumSuccs == 0)
1395 // If SU does not have a use, i.e. it doesn't produce a value that would
1396 // be consumed (e.g. store), then it terminates a chain of computation.
1397 // Give it a large SethiUllman number so it will be scheduled right
1398 // before its predecessors that it doesn't lengthen their live ranges.
1399 return 0xffff;
1400 else if (SU->NumPreds == 0)
1401 // If SU does not have a def, schedule it close to its uses because it
1402 // does not lengthen any live ranges.
1403 return 0;
1404 else
1405 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001406 }
1407
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001408 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1409 scheduleDAG = scheduleDag;
1410 }
1411
Evan Chengd38c22b2006-05-11 23:55:42 +00001412 private:
Evan Cheng73bdf042008-03-01 00:39:47 +00001413 bool canClobber(const SUnit *SU, const SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +00001414 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001415 void CalculateSethiUllmanNumbers();
1416 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001417 };
1418
1419
Dan Gohman54a187e2007-08-20 19:28:38 +00001420 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001421 : public RegReductionPriorityQueue<td_ls_rr_sort> {
Evan Cheng5924bf72007-09-25 01:54:36 +00001422 // SUnitMap SDNode to SUnit mapping (n -> n).
Dan Gohmane6e13482008-06-21 15:52:51 +00001423 DenseMap<SDNode*, SUnit*> *SUnitMap;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001424
Evan Chengd38c22b2006-05-11 23:55:42 +00001425 // SUnits - The SUnits for the current graph.
1426 const std::vector<SUnit> *SUnits;
1427
1428 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001429 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001430
1431 public:
1432 TDRegReductionPriorityQueue() {}
1433
Dan Gohmane6e13482008-06-21 15:52:51 +00001434 void initNodes(DenseMap<SDNode*, SUnit*> &sumap,
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001435 std::vector<SUnit> &sunits) {
1436 SUnitMap = &sumap;
Evan Chengd38c22b2006-05-11 23:55:42 +00001437 SUnits = &sunits;
1438 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001439 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001440 }
1441
Evan Cheng5924bf72007-09-25 01:54:36 +00001442 void addNode(const SUnit *SU) {
1443 SethiUllmanNumbers.resize(SUnits->size(), 0);
1444 CalcNodeSethiUllmanNumber(SU);
1445 }
1446
1447 void updateNode(const SUnit *SU) {
1448 SethiUllmanNumbers[SU->NodeNum] = 0;
1449 CalcNodeSethiUllmanNumber(SU);
1450 }
1451
Evan Chengd38c22b2006-05-11 23:55:42 +00001452 void releaseState() {
1453 SUnits = 0;
1454 SethiUllmanNumbers.clear();
1455 }
1456
Evan Cheng6730f032007-01-08 23:55:53 +00001457 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001458 assert(SU->NodeNum < SethiUllmanNumbers.size());
1459 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001460 }
1461
1462 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001463 void CalculateSethiUllmanNumbers();
1464 unsigned CalcNodeSethiUllmanNumber(const SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001465 };
1466}
1467
Evan Chengb9e3db62007-03-14 22:43:40 +00001468/// closestSucc - Returns the scheduled cycle of the successor which is
1469/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001470static unsigned closestSucc(const SUnit *SU) {
1471 unsigned MaxCycle = 0;
1472 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001473 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001474 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001475 // If there are bunch of CopyToRegs stacked up, they should be considered
1476 // to be at the same position.
Evan Cheng8e136a92007-09-26 21:36:17 +00001477 if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001478 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001479 if (Cycle > MaxCycle)
1480 MaxCycle = Cycle;
1481 }
Evan Cheng28748552007-03-13 23:25:11 +00001482 return MaxCycle;
1483}
1484
Evan Cheng61bc51e2007-12-20 02:22:36 +00001485/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1486/// for scratch registers. Live-in operands and live-out results don't count
1487/// since they are "fixed".
1488static unsigned calcMaxScratches(const SUnit *SU) {
1489 unsigned Scratches = 0;
1490 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1491 I != E; ++I) {
1492 if (I->isCtrl) continue; // ignore chain preds
Evan Cheng0e400d42008-01-09 23:01:55 +00001493 if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001494 Scratches++;
1495 }
1496 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1497 I != E; ++I) {
1498 if (I->isCtrl) continue; // ignore chain succs
Evan Cheng0e400d42008-01-09 23:01:55 +00001499 if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001500 Scratches += 10;
1501 }
1502 return Scratches;
1503}
1504
Evan Chengd38c22b2006-05-11 23:55:42 +00001505// Bottom up
1506bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng99f2f792006-05-13 08:22:24 +00001507
Evan Cheng6730f032007-01-08 23:55:53 +00001508 unsigned LPriority = SPQ->getNodePriority(left);
1509 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001510 if (LPriority != RPriority)
1511 return LPriority > RPriority;
1512
1513 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1514 // e.g.
1515 // t1 = op t2, c1
1516 // t3 = op t4, c2
1517 //
1518 // and the following instructions are both ready.
1519 // t2 = op c3
1520 // t4 = op c4
1521 //
1522 // Then schedule t2 = op first.
1523 // i.e.
1524 // t4 = op c4
1525 // t2 = op c3
1526 // t1 = op t2, c1
1527 // t3 = op t4, c2
1528 //
1529 // This creates more short live intervals.
1530 unsigned LDist = closestSucc(left);
1531 unsigned RDist = closestSucc(right);
1532 if (LDist != RDist)
1533 return LDist < RDist;
1534
1535 // Intuitively, it's good to push down instructions whose results are
1536 // liveout so their long live ranges won't conflict with other values
1537 // which are needed inside the BB. Further prioritize liveout instructions
1538 // by the number of operands which are calculated within the BB.
1539 unsigned LScratch = calcMaxScratches(left);
1540 unsigned RScratch = calcMaxScratches(right);
1541 if (LScratch != RScratch)
1542 return LScratch > RScratch;
1543
1544 if (left->Height != right->Height)
1545 return left->Height > right->Height;
1546
1547 if (left->Depth != right->Depth)
1548 return left->Depth < right->Depth;
1549
1550 if (left->CycleBound != right->CycleBound)
1551 return left->CycleBound > right->CycleBound;
1552
Roman Levenstein6b371142008-04-29 09:07:59 +00001553 assert(left->NodeQueueId && right->NodeQueueId &&
1554 "NodeQueueId cannot be zero");
1555 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001556}
1557
Dan Gohman4b49be12008-06-21 01:08:22 +00001558bool
1559BURegReductionPriorityQueue::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001560 if (SU->isTwoAddress) {
1561 unsigned Opc = SU->Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001562 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001563 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001564 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001565 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001566 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001567 SDNode *DU = SU->Node->getOperand(i).Val;
Evan Cheng1bf166312007-11-09 01:27:11 +00001568 if ((*SUnitMap).find(DU) != (*SUnitMap).end() &&
Dan Gohmane6e13482008-06-21 15:52:51 +00001569 Op->OrigNode == (*SUnitMap)[DU])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001570 return true;
1571 }
1572 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001573 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001574 return false;
1575}
1576
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001577
Evan Chenga5e595d2007-09-28 22:32:30 +00001578/// hasCopyToRegUse - Return true if SU has a value successor that is a
1579/// CopyToReg node.
1580static bool hasCopyToRegUse(SUnit *SU) {
1581 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1582 I != E; ++I) {
1583 if (I->isCtrl) continue;
1584 SUnit *SuccSU = I->Dep;
1585 if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg)
1586 return true;
1587 }
1588 return false;
1589}
1590
Evan Chengf9891412007-12-20 09:25:31 +00001591/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
1592/// physical register def.
1593static bool canClobberPhysRegDefs(SUnit *SuccSU, SUnit *SU,
1594 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001595 const TargetRegisterInfo *TRI) {
Evan Chengf9891412007-12-20 09:25:31 +00001596 SDNode *N = SuccSU->Node;
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001597 unsigned NumDefs = TII->get(N->getTargetOpcode()).getNumDefs();
1598 const unsigned *ImpDefs = TII->get(N->getTargetOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001599 if (!ImpDefs)
1600 return false;
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001601 const unsigned *SUImpDefs =
1602 TII->get(SU->Node->getTargetOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001603 if (!SUImpDefs)
1604 return false;
1605 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001606 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001607 if (VT == MVT::Flag || VT == MVT::Other)
1608 continue;
1609 unsigned Reg = ImpDefs[i - NumDefs];
1610 for (;*SUImpDefs; ++SUImpDefs) {
1611 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001612 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001613 return true;
1614 }
1615 }
1616 return false;
1617}
1618
Evan Chengd38c22b2006-05-11 23:55:42 +00001619/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1620/// it as a def&use operand. Add a pseudo control edge from it to the other
1621/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001622/// first (lower in the schedule). If both nodes are two-address, favor the
1623/// one that has a CopyToReg use (more likely to be a loop induction update).
1624/// If both are two-address, but one is commutable while the other is not
1625/// commutable, favor the one that's not commutable.
Dan Gohman4b49be12008-06-21 01:08:22 +00001626void BURegReductionPriorityQueue::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001627 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1628 SUnit *SU = (SUnit *)&((*SUnits)[i]);
1629 if (!SU->isTwoAddress)
1630 continue;
1631
1632 SDNode *Node = SU->Node;
Evan Chenga5e595d2007-09-28 22:32:30 +00001633 if (!Node || !Node->isTargetOpcode() || SU->FlaggedNodes.size() > 0)
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001634 continue;
1635
1636 unsigned Opc = Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001637 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001638 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001639 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001640 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001641 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001642 SDNode *DU = SU->Node->getOperand(j).Val;
Evan Cheng1bf166312007-11-09 01:27:11 +00001643 if ((*SUnitMap).find(DU) == (*SUnitMap).end())
1644 continue;
Dan Gohmane6e13482008-06-21 15:52:51 +00001645 SUnit *DUSU = (*SUnitMap)[DU];
Evan Chengf24d15f2006-11-06 21:33:46 +00001646 if (!DUSU) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001647 for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end();
1648 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001649 if (I->isCtrl) continue;
1650 SUnit *SuccSU = I->Dep;
Evan Chengf9891412007-12-20 09:25:31 +00001651 if (SuccSU == SU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001652 continue;
Evan Cheng2dbffa42007-11-06 08:44:59 +00001653 // Be conservative. Ignore if nodes aren't at roughly the same
1654 // depth and height.
1655 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1656 continue;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001657 if (!SuccSU->Node || !SuccSU->Node->isTargetOpcode())
1658 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001659 // Don't constrain nodes with physical register defs if the
Dan Gohmancf8827a2008-01-29 12:43:50 +00001660 // predecessor can clobber them.
Evan Chengf9891412007-12-20 09:25:31 +00001661 if (SuccSU->hasPhysRegDefs) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001662 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Chengf9891412007-12-20 09:25:31 +00001663 continue;
1664 }
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001665 // Don't constraint extract_subreg / insert_subreg these may be
1666 // coalesced away. We don't them close to their uses.
1667 unsigned SuccOpc = SuccSU->Node->getTargetOpcode();
1668 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1669 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1670 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +00001671 if ((!canClobber(SuccSU, DUSU) ||
Evan Chenga5e595d2007-09-28 22:32:30 +00001672 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
Evan Cheng5924bf72007-09-25 01:54:36 +00001673 (!SU->isCommutable && SuccSU->isCommutable)) &&
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001674 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001675 DOUT << "Adding an edge from SU # " << SU->NodeNum
1676 << " to SU #" << SuccSU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001677 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001678 }
1679 }
1680 }
1681 }
1682 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001683}
1684
Evan Cheng6730f032007-01-08 23:55:53 +00001685/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +00001686/// Smaller number is the higher priority.
Dan Gohman4b49be12008-06-21 01:08:22 +00001687unsigned BURegReductionPriorityQueue::
Chris Lattner296a83c2007-02-01 04:55:59 +00001688CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +00001689 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001690 if (SethiUllmanNumber != 0)
1691 return SethiUllmanNumber;
1692
Evan Cheng961bbd32007-01-08 23:50:38 +00001693 unsigned Extra = 0;
1694 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1695 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001696 if (I->isCtrl) continue; // ignore chain preds
1697 SUnit *PredSU = I->Dep;
Evan Cheng6730f032007-01-08 23:55:53 +00001698 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Cheng961bbd32007-01-08 23:50:38 +00001699 if (PredSethiUllman > SethiUllmanNumber) {
1700 SethiUllmanNumber = PredSethiUllman;
1701 Extra = 0;
Evan Cheng0effc3a2007-09-19 01:38:40 +00001702 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
Evan Cheng5924bf72007-09-25 01:54:36 +00001703 ++Extra;
Evan Chengd38c22b2006-05-11 23:55:42 +00001704 }
Evan Cheng961bbd32007-01-08 23:50:38 +00001705
1706 SethiUllmanNumber += Extra;
1707
1708 if (SethiUllmanNumber == 0)
1709 SethiUllmanNumber = 1;
Evan Chengd38c22b2006-05-11 23:55:42 +00001710
1711 return SethiUllmanNumber;
1712}
1713
Evan Cheng6730f032007-01-08 23:55:53 +00001714/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1715/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001716void BURegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001717 SethiUllmanNumbers.assign(SUnits->size(), 0);
1718
1719 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +00001720 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001721}
1722
Roman Levenstein30d09512008-03-27 09:44:37 +00001723/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001724/// predecessors of the successors of the SUnit SU. Stop when the provided
1725/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001726static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1727 unsigned Limit) {
1728 unsigned Sum = 0;
1729 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1730 I != E; ++I) {
1731 SUnit *SuccSU = I->Dep;
1732 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1733 EE = SuccSU->Preds.end(); II != EE; ++II) {
1734 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001735 if (!PredSU->isScheduled)
1736 if (++Sum > Limit)
1737 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001738 }
1739 }
1740 return Sum;
1741}
1742
Evan Chengd38c22b2006-05-11 23:55:42 +00001743
1744// Top down
1745bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001746 unsigned LPriority = SPQ->getNodePriority(left);
1747 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng8e136a92007-09-26 21:36:17 +00001748 bool LIsTarget = left->Node && left->Node->isTargetOpcode();
1749 bool RIsTarget = right->Node && right->Node->isTargetOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001750 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1751 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001752 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1753 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001754
1755 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1756 return false;
1757 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1758 return true;
1759
Evan Chengd38c22b2006-05-11 23:55:42 +00001760 if (LIsFloater)
1761 LBonus -= 2;
1762 if (RIsFloater)
1763 RBonus -= 2;
1764 if (left->NumSuccs == 1)
1765 LBonus += 2;
1766 if (right->NumSuccs == 1)
1767 RBonus += 2;
1768
Evan Cheng73bdf042008-03-01 00:39:47 +00001769 if (LPriority+LBonus != RPriority+RBonus)
1770 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001771
Evan Cheng73bdf042008-03-01 00:39:47 +00001772 if (left->Depth != right->Depth)
1773 return left->Depth < right->Depth;
1774
1775 if (left->NumSuccsLeft != right->NumSuccsLeft)
1776 return left->NumSuccsLeft > right->NumSuccsLeft;
1777
1778 if (left->CycleBound != right->CycleBound)
1779 return left->CycleBound > right->CycleBound;
1780
Roman Levenstein6b371142008-04-29 09:07:59 +00001781 assert(left->NodeQueueId && right->NodeQueueId &&
1782 "NodeQueueId cannot be zero");
1783 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001784}
1785
Evan Cheng6730f032007-01-08 23:55:53 +00001786/// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number.
Evan Chengd38c22b2006-05-11 23:55:42 +00001787/// Smaller number is the higher priority.
Dan Gohman4b49be12008-06-21 01:08:22 +00001788unsigned TDRegReductionPriorityQueue::
Chris Lattner296a83c2007-02-01 04:55:59 +00001789CalcNodeSethiUllmanNumber(const SUnit *SU) {
Evan Cheng961bbd32007-01-08 23:50:38 +00001790 unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001791 if (SethiUllmanNumber != 0)
1792 return SethiUllmanNumber;
1793
Evan Cheng8e136a92007-09-26 21:36:17 +00001794 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001795 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Evan Cheng961bbd32007-01-08 23:50:38 +00001796 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +00001797 else if (SU->NumSuccsLeft == 0)
1798 // If SU does not have a use, i.e. it doesn't produce a value that would
1799 // be consumed (e.g. store), then it terminates a chain of computation.
Chris Lattner296a83c2007-02-01 04:55:59 +00001800 // Give it a small SethiUllman number so it will be scheduled right before
1801 // its predecessors that it doesn't lengthen their live ranges.
Evan Cheng961bbd32007-01-08 23:50:38 +00001802 SethiUllmanNumber = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001803 else if (SU->NumPredsLeft == 0 &&
1804 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
Evan Cheng961bbd32007-01-08 23:50:38 +00001805 SethiUllmanNumber = 0xffff;
Evan Chengd38c22b2006-05-11 23:55:42 +00001806 else {
1807 int Extra = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +00001808 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1809 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001810 if (I->isCtrl) continue; // ignore chain preds
1811 SUnit *PredSU = I->Dep;
Evan Cheng6730f032007-01-08 23:55:53 +00001812 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001813 if (PredSethiUllman > SethiUllmanNumber) {
1814 SethiUllmanNumber = PredSethiUllman;
1815 Extra = 0;
Evan Cheng0effc3a2007-09-19 01:38:40 +00001816 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
Evan Cheng5924bf72007-09-25 01:54:36 +00001817 ++Extra;
Evan Chengd38c22b2006-05-11 23:55:42 +00001818 }
1819
1820 SethiUllmanNumber += Extra;
1821 }
1822
1823 return SethiUllmanNumber;
1824}
1825
Evan Cheng6730f032007-01-08 23:55:53 +00001826/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1827/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001828void TDRegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001829 SethiUllmanNumbers.assign(SUnits->size(), 0);
1830
1831 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng6730f032007-01-08 23:55:53 +00001832 CalcNodeSethiUllmanNumber(&(*SUnits)[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001833}
1834
1835//===----------------------------------------------------------------------===//
1836// Public Constructor Functions
1837//===----------------------------------------------------------------------===//
1838
Jim Laskey03593f72006-08-01 18:29:48 +00001839llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1840 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +00001841 MachineBasicBlock *BB) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001842 const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo();
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001843 const TargetRegisterInfo *TRI = DAG->getTarget().getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001844
Dan Gohman4b49be12008-06-21 01:08:22 +00001845 BURegReductionPriorityQueue *priorityQueue =
1846 new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001847
1848 ScheduleDAGRRList * scheduleDAG =
1849 new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true, priorityQueue);
1850 priorityQueue->setScheduleDAG(scheduleDAG);
1851 return scheduleDAG;
Evan Chengd38c22b2006-05-11 23:55:42 +00001852}
1853
Jim Laskey03593f72006-08-01 18:29:48 +00001854llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1855 SelectionDAG *DAG,
Evan Chengd38c22b2006-05-11 23:55:42 +00001856 MachineBasicBlock *BB) {
Jim Laskey95eda5b2006-08-01 14:21:23 +00001857 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false,
Dan Gohman4b49be12008-06-21 01:08:22 +00001858 new TDRegReductionPriorityQueue());
Evan Chengd38c22b2006-05-11 23:55:42 +00001859}
1860