blob: 03d3ef5feedb89512050a9b35f88b0baeb1ddcbe [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
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"
Dan Gohman60cb69e2008-11-19 23:18:57 +000019#include "llvm/CodeGen/ScheduleDAGSDNodes.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"
Dan Gohmana4db3352008-06-21 18:35:25 +000027#include "llvm/ADT/PriorityQueue.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 "llvm/Support/CommandLine.h"
33using namespace llvm;
34
Dan Gohmanfd227e92008-03-25 17:10:29 +000035STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000036STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000037STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000038STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039
Jim Laskey95eda5b2006-08-01 14:21:23 +000040static RegisterScheduler
41 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000042 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000043 createBURRListDAGScheduler);
44static RegisterScheduler
45 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000046 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000047 createTDRRListDAGScheduler);
48
Evan Chengd38c22b2006-05-11 23:55:42 +000049namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000050//===----------------------------------------------------------------------===//
51/// ScheduleDAGRRList - The actual register reduction list scheduler
52/// implementation. This supports both top-down and bottom-up scheduling.
53///
Dan Gohman60cb69e2008-11-19 23:18:57 +000054class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000055private:
56 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
57 /// it is top-down.
58 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000059
Evan Chengd38c22b2006-05-11 23:55:42 +000060 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000061 SchedulingPriorityQueue *AvailableQueue;
62
Dan Gohmanc07f6862008-09-23 18:50:48 +000063 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000064 /// that are "live". These nodes must be scheduled before any other nodes that
65 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000066 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000067 std::vector<SUnit*> LiveRegDefs;
68 std::vector<unsigned> LiveRegCycles;
69
Dan Gohmanad2134d2008-11-25 00:52:40 +000070 /// Topo - A topological ordering for SUnits which permits fast IsReachable
71 /// and similar queries.
72 ScheduleDAGTopologicalSort Topo;
73
Evan Chengd38c22b2006-05-11 23:55:42 +000074public:
Dan Gohman5a390b92008-11-13 21:21:28 +000075 ScheduleDAGRRList(SelectionDAG *dag, MachineBasicBlock *bb,
Dan Gohmanfd08af42008-11-20 03:11:19 +000076 const TargetMachine &tm, bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000077 SchedulingPriorityQueue *availqueue)
Dan Gohmanfd08af42008-11-20 03:11:19 +000078 : ScheduleDAGSDNodes(dag, bb, tm), isBottomUp(isbottomup),
Dan Gohmanad2134d2008-11-25 00:52:40 +000079 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000080 }
81
82 ~ScheduleDAGRRList() {
83 delete AvailableQueue;
84 }
85
86 void Schedule();
87
Roman Levenstein733a4d62008-03-26 11:23:38 +000088 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +000089 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
90 return Topo.IsReachable(SU, TargetSU);
91 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000092
93 /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
94 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +000095 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
96 return Topo.WillCreateCycle(SU, TargetSU);
97 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000098
Dan Gohman2d170892008-12-09 22:54:47 +000099 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000100 /// This returns true if this is a new predecessor.
101 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000102 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000103 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000104 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000105 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000106
Dan Gohman2d170892008-12-09 22:54:47 +0000107 /// RemovePred - removes a predecessor edge from SUnit SU.
108 /// This returns true if an edge was removed.
109 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000110 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000111 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000112 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000113 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000114
Evan Chengd38c22b2006-05-11 23:55:42 +0000115private:
Dan Gohman2d170892008-12-09 22:54:47 +0000116 void ReleasePred(SUnit *SU, SDep *PredEdge);
117 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
118 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000119 void ScheduleNodeBottomUp(SUnit*, unsigned);
120 void ScheduleNodeTopDown(SUnit*, unsigned);
121 void UnscheduleNodeBottomUp(SUnit*);
122 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
123 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000124 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
125 const TargetRegisterClass*,
126 const TargetRegisterClass*,
127 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000128 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000129 void ListScheduleTopDown();
130 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000131
132
133 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000134 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000135 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000136 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000138 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000139 if (NewNode->NodeNum >= NumSUnits)
140 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000141 return NewNode;
142 }
143
Roman Levenstein733a4d62008-03-26 11:23:38 +0000144 /// CreateClone - Creates a new SUnit from an existing one.
145 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000146 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000147 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000148 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000149 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000150 if (NewNode->NodeNum >= NumSUnits)
151 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000152 return NewNode;
153 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000154
155 /// ForceUnitLatencies - Return true, since register-pressure-reducing
156 /// scheduling doesn't need actual latency information.
157 bool ForceUnitLatencies() const { return true; }
Evan Chengd38c22b2006-05-11 23:55:42 +0000158};
159} // end anonymous namespace
160
161
162/// Schedule - Schedule the DAG using list scheduling.
163void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000164 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000165
Dan Gohmanc07f6862008-09-23 18:50:48 +0000166 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000167 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
168 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000169
Dan Gohman04543e72008-12-23 18:36:58 +0000170 // Build the scheduling graph.
171 BuildSchedGraph();
Evan Chengd38c22b2006-05-11 23:55:42 +0000172
Evan Chengd38c22b2006-05-11 23:55:42 +0000173 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000174 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000175 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000176
Dan Gohman46520a22008-06-21 19:18:17 +0000177 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000178
Evan Chengd38c22b2006-05-11 23:55:42 +0000179 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
180 if (isBottomUp)
181 ListScheduleBottomUp();
182 else
183 ListScheduleTopDown();
184
185 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000186}
Evan Chengd38c22b2006-05-11 23:55:42 +0000187
188//===----------------------------------------------------------------------===//
189// Bottom-Up Scheduling
190//===----------------------------------------------------------------------===//
191
Evan Chengd38c22b2006-05-11 23:55:42 +0000192/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000193/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000194void ScheduleDAGRRList::ReleasePred(SUnit *SU, SDep *PredEdge) {
195 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000196 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000197
198#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000199 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000200 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000201 PredSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000202 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000203 assert(0);
204 }
205#endif
206
Evan Cheng038dcc52007-09-28 19:24:24 +0000207 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000208 PredSU->isAvailable = true;
209 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000210 }
211}
212
213/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
214/// count of its predecessors. If a predecessor pending count is zero, add it to
215/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000216void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000217 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000218 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000219
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000220 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
221 SU->setHeightToAtLeast(CurCycle);
Dan Gohman6e587262008-11-18 21:22:20 +0000222 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000223
224 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000225 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000226 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000227 ReleasePred(SU, &*I);
228 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000229 // This is a physical register dependency and it's impossible or
230 // expensive to copy the register. Make sure nothing that can
231 // clobber the register is scheduled between the predecessor and
232 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000233 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000234 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000235 LiveRegDefs[I->getReg()] = I->getSUnit();
236 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000237 }
238 }
239 }
240
241 // Release all the implicit physical register defs that are live.
242 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
243 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000244 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000245 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000246 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000247 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000248 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000249 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000250 LiveRegDefs[I->getReg()] = NULL;
251 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000252 }
253 }
254 }
255
Evan Chengd38c22b2006-05-11 23:55:42 +0000256 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000257 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000258}
259
Evan Cheng5924bf72007-09-25 01:54:36 +0000260/// CapturePred - This does the opposite of ReleasePred. Since SU is being
261/// unscheduled, incrcease the succ left count of its predecessors. Remove
262/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000263void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
264 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000265 if (PredSU->isAvailable) {
266 PredSU->isAvailable = false;
267 if (!PredSU->isPending)
268 AvailableQueue->remove(PredSU);
269 }
270
Evan Cheng038dcc52007-09-28 19:24:24 +0000271 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000272}
273
274/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
275/// its predecessor states to reflect the change.
276void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000277 DOUT << "*** Unscheduling [" << SU->getHeight() << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000278 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000279
280 AvailableQueue->UnscheduledNode(SU);
281
282 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
283 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000284 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000285 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000286 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000287 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000288 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000289 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000290 LiveRegDefs[I->getReg()] = NULL;
291 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000292 }
293 }
294
295 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
296 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000297 if (I->isAssignedRegDep()) {
298 if (!LiveRegDefs[I->getReg()]) {
299 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000300 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000301 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000302 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
303 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000304 }
305 }
306
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000307 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000308 SU->isScheduled = false;
309 SU->isAvailable = true;
310 AvailableQueue->push(SU);
311}
312
Evan Cheng8e136a92007-09-26 21:36:17 +0000313/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000314/// BTCycle in order to schedule a specific node. Returns the last unscheduled
315/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000316void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
317 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000318 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000319 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000320 OldSU = Sequence.back();
321 Sequence.pop_back();
322 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000323 // Don't try to remove SU from AvailableQueue.
324 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000325 UnscheduleNodeBottomUp(OldSU);
326 --CurCycle;
327 }
328
329
330 if (SU->isSucc(OldSU)) {
331 assert(false && "Something is wrong!");
332 abort();
333 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000334
335 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000336}
337
Evan Cheng5924bf72007-09-25 01:54:36 +0000338/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
339/// successors to the newly created node.
340SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000341 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000342 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000343
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000344 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000345 if (!N)
346 return NULL;
347
348 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000349 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000350 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000351 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000352 if (VT == MVT::Flag)
353 return NULL;
354 else if (VT == MVT::Other)
355 TryUnfold = true;
356 }
Evan Cheng79e97132007-10-05 01:39:18 +0000357 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000358 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000359 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000360 if (VT == MVT::Flag)
361 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000362 }
363
364 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000365 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000366 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000367 return NULL;
368
369 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
370 assert(NewNodes.size() == 2 && "Expected a load folding node!");
371
372 N = NewNodes[1];
373 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000374 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000375 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000376 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000377 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
378 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000379 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000380
Dan Gohmane52e0892008-11-11 21:34:44 +0000381 // LoadNode may already exist. This can happen when there is another
382 // load from the same location and producing the same type of value
383 // but it has different alignment or volatileness.
384 bool isNewLoad = true;
385 SUnit *LoadSU;
386 if (LoadNode->getNodeId() != -1) {
387 LoadSU = &SUnits[LoadNode->getNodeId()];
388 isNewLoad = false;
389 } else {
390 LoadSU = CreateNewSUnit(LoadNode);
391 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000392 ComputeLatency(LoadSU);
393 }
394
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000395 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000396 assert(N->getNodeId() == -1 && "Node already inserted!");
397 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000398
Dan Gohman17059682008-07-17 19:10:17 +0000399 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000400 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000401 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000402 NewSU->isTwoAddress = true;
403 break;
404 }
405 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000406 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000407 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000408 ComputeLatency(NewSU);
409
Dan Gohman2d170892008-12-09 22:54:47 +0000410 SDep ChainPred;
Evan Cheng79e97132007-10-05 01:39:18 +0000411 SmallVector<SDep, 4> ChainSuccs;
412 SmallVector<SDep, 4> LoadPreds;
413 SmallVector<SDep, 4> NodePreds;
414 SmallVector<SDep, 4> NodeSuccs;
415 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
416 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000417 if (I->isCtrl())
418 ChainPred = *I;
419 else if (I->getSUnit()->getNode() &&
420 I->getSUnit()->getNode()->isOperandOf(LoadNode))
421 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000422 else
Dan Gohman2d170892008-12-09 22:54:47 +0000423 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000424 }
425 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
426 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000427 if (I->isCtrl())
428 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000429 else
Dan Gohman2d170892008-12-09 22:54:47 +0000430 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000431 }
432
Dan Gohman2d170892008-12-09 22:54:47 +0000433 if (ChainPred.getSUnit()) {
434 RemovePred(SU, ChainPred);
Dan Gohman4370f262008-04-15 01:22:18 +0000435 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000436 AddPred(LoadSU, ChainPred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000437 }
Evan Cheng79e97132007-10-05 01:39:18 +0000438 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000439 const SDep &Pred = LoadPreds[i];
440 RemovePred(SU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000441 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000442 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000443 }
Evan Cheng79e97132007-10-05 01:39:18 +0000444 }
445 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000446 const SDep &Pred = NodePreds[i];
447 RemovePred(SU, Pred);
448 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000449 }
450 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000451 SDep D = NodeSuccs[i];
452 SUnit *SuccDep = D.getSUnit();
453 D.setSUnit(SU);
454 RemovePred(SuccDep, D);
455 D.setSUnit(NewSU);
456 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000457 }
458 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000459 SDep D = ChainSuccs[i];
460 SUnit *SuccDep = D.getSUnit();
461 D.setSUnit(SU);
462 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000463 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000464 D.setSUnit(LoadSU);
465 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000466 }
Evan Cheng79e97132007-10-05 01:39:18 +0000467 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000468 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000469 AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000470 }
Evan Cheng79e97132007-10-05 01:39:18 +0000471
Evan Cheng91e0fc92007-12-18 08:42:10 +0000472 if (isNewLoad)
473 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000474 AvailableQueue->addNode(NewSU);
475
476 ++NumUnfolds;
477
478 if (NewSU->NumSuccsLeft == 0) {
479 NewSU->isAvailable = true;
480 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000481 }
482 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000483 }
484
485 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000486 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000487
488 // New SUnit has the exact same predecessors.
489 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
490 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000491 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000492 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000493
494 // Only copy scheduled successors. Cut them from old node's successor
495 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000496 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000497 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
498 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000499 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000500 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000501 SUnit *SuccSU = I->getSUnit();
502 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000503 SDep D = *I;
504 D.setSUnit(NewSU);
505 AddPred(SuccSU, D);
506 D.setSUnit(SU);
507 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000508 }
509 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000510 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000511 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000512
513 AvailableQueue->updateNode(SU);
514 AvailableQueue->addNode(NewSU);
515
Evan Cheng1ec79b42007-09-27 07:09:03 +0000516 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000517 return NewSU;
518}
519
Evan Chengb2c42c62009-01-12 03:19:55 +0000520/// InsertCopiesAndMoveSuccs - Insert register copies and move all
521/// scheduled successors of the given SUnit to the last copy.
522void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
523 const TargetRegisterClass *DestRC,
524 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000525 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000526 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000527 CopyFromSU->CopySrcRC = SrcRC;
528 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000529
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000530 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000531 CopyToSU->CopySrcRC = DestRC;
532 CopyToSU->CopyDstRC = SrcRC;
533
534 // Only copy scheduled successors. Cut them from old node's successor
535 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000536 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000537 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
538 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000539 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000540 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000541 SUnit *SuccSU = I->getSUnit();
542 if (SuccSU->isScheduled) {
543 SDep D = *I;
544 D.setSUnit(CopyToSU);
545 AddPred(SuccSU, D);
546 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000547 }
548 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000549 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000550 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000551
Dan Gohman2d170892008-12-09 22:54:47 +0000552 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
553 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000554
555 AvailableQueue->updateNode(SU);
556 AvailableQueue->addNode(CopyFromSU);
557 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000558 Copies.push_back(CopyFromSU);
559 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000560
Evan Chengb2c42c62009-01-12 03:19:55 +0000561 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000562}
563
564/// getPhysicalRegisterVT - Returns the ValueType of the physical register
565/// definition of the specified node.
566/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000567static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
568 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000569 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000570 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000571 unsigned NumRes = TID.getNumDefs();
572 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000573 if (Reg == *ImpDef)
574 break;
575 ++NumRes;
576 }
577 return N->getValueType(NumRes);
578}
579
Evan Cheng5924bf72007-09-25 01:54:36 +0000580/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
581/// scheduling of the given node to satisfy live physical register dependencies.
582/// If the specific node is the last one that's available to schedule, do
583/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000584bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
585 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000586 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000587 return false;
588
Evan Chenge6f92252007-09-27 18:46:06 +0000589 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000590 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000591 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
592 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000593 if (I->isAssignedRegDep()) {
594 unsigned Reg = I->getReg();
595 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
Evan Chenge6f92252007-09-27 18:46:06 +0000596 if (RegAdded.insert(Reg))
597 LRegs.push_back(Reg);
598 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000599 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000600 *Alias; ++Alias)
Dan Gohman2d170892008-12-09 22:54:47 +0000601 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
Evan Chenge6f92252007-09-27 18:46:06 +0000602 if (RegAdded.insert(*Alias))
603 LRegs.push_back(*Alias);
604 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000605 }
606 }
607
Dan Gohman072734e2008-11-13 23:24:17 +0000608 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
609 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000610 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000611 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000612 if (!TID.ImplicitDefs)
613 continue;
614 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000615 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000616 if (RegAdded.insert(*Reg))
617 LRegs.push_back(*Reg);
618 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000619 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000620 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000621 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000622 if (RegAdded.insert(*Alias))
623 LRegs.push_back(*Alias);
624 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000625 }
626 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000627 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000628}
629
Evan Cheng1ec79b42007-09-27 07:09:03 +0000630
Evan Chengd38c22b2006-05-11 23:55:42 +0000631/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
632/// schedulers.
633void ScheduleDAGRRList::ListScheduleBottomUp() {
634 unsigned CurCycle = 0;
635 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000636 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000637 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000638 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
639 RootSU->isAvailable = true;
640 AvailableQueue->push(RootSU);
641 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000642
643 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000644 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000645 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000646 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000647 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000648 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000649 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000650 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000651 SUnit *CurSU = AvailableQueue->pop();
652 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000653 SmallVector<unsigned, 4> LRegs;
654 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
655 break;
656 Delayed = true;
657 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000658
659 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
660 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000661 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000662 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000663
664 // All candidates are delayed due to live physical reg dependencies.
665 // Try backtracking, code duplication, or inserting cross class copies
666 // to resolve it.
667 if (Delayed && !CurSU) {
668 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
669 SUnit *TrySU = NotReady[i];
670 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
671
672 // Try unscheduling up to the point where it's safe to schedule
673 // this node.
674 unsigned LiveCycle = CurCycle;
675 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
676 unsigned Reg = LRegs[j];
677 unsigned LCycle = LiveRegCycles[Reg];
678 LiveCycle = std::min(LiveCycle, LCycle);
679 }
680 SUnit *OldSU = Sequence[LiveCycle];
681 if (!WillCreateCycle(TrySU, OldSU)) {
682 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
683 // Force the current node to be scheduled before the node that
684 // requires the physical reg dep.
685 if (OldSU->isAvailable) {
686 OldSU->isAvailable = false;
687 AvailableQueue->remove(OldSU);
688 }
Dan Gohman2d170892008-12-09 22:54:47 +0000689 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
690 /*Reg=*/0, /*isNormalMemory=*/false,
691 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000692 // If one or more successors has been unscheduled, then the current
693 // node is no longer avaialable. Schedule a successor that's now
694 // available instead.
695 if (!TrySU->isAvailable)
696 CurSU = AvailableQueue->pop();
697 else {
698 CurSU = TrySU;
699 TrySU->isPending = false;
700 NotReady.erase(NotReady.begin()+i);
701 }
702 break;
703 }
704 }
705
706 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000707 // Can't backtrack. If it's too expensive to copy the value, then try
708 // duplicate the nodes that produces these "too expensive to copy"
709 // values to break the dependency. In case even that doesn't work,
710 // insert cross class copies.
711 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000712 SUnit *TrySU = NotReady[0];
713 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
714 assert(LRegs.size() == 1 && "Can't handle this yet!");
715 unsigned Reg = LRegs[0];
716 SUnit *LRDef = LiveRegDefs[Reg];
Evan Chengb2c42c62009-01-12 03:19:55 +0000717 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
718 const TargetRegisterClass *RC =
719 TRI->getPhysicalRegisterRegClass(Reg, VT);
720 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
721
722 // If cross copy register class is null, then it must be possible copy
723 // the value directly. Do not try duplicate the def.
724 SUnit *NewDef = 0;
725 if (DestRC)
726 NewDef = CopyAndMoveSuccessors(LRDef);
727 else
728 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000729 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000730 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000731 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000732 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Cheng0c4fe262009-01-09 20:42:34 +0000733 DOUT << "Adding an edge from SU #" << TrySU->NodeNum
Evan Cheng1ec79b42007-09-27 07:09:03 +0000734 << " to SU #" << Copies.front()->NodeNum << "\n";
Dan Gohman2d170892008-12-09 22:54:47 +0000735 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000736 /*Reg=*/0, /*isNormalMemory=*/false,
737 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000738 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000739 NewDef = Copies.back();
740 }
741
Evan Cheng0c4fe262009-01-09 20:42:34 +0000742 DOUT << "Adding an edge from SU #" << NewDef->NodeNum
Evan Cheng1ec79b42007-09-27 07:09:03 +0000743 << " to SU #" << TrySU->NodeNum << "\n";
744 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000745 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000746 /*Reg=*/0, /*isNormalMemory=*/false,
747 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000748 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000749 TrySU->isAvailable = false;
750 CurSU = NewDef;
751 }
752
753 if (!CurSU) {
754 assert(false && "Unable to resolve live physical register dependencies!");
755 abort();
756 }
757 }
758
Evan Chengd38c22b2006-05-11 23:55:42 +0000759 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000760 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
761 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000762 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000763 if (NotReady[i]->isAvailable)
764 AvailableQueue->push(NotReady[i]);
765 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000766 NotReady.clear();
767
Dan Gohmanc602dd42008-11-21 00:10:42 +0000768 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000769 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000770 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000771 }
772
Evan Chengd38c22b2006-05-11 23:55:42 +0000773 // Reverse the order if it is bottom up.
774 std::reverse(Sequence.begin(), Sequence.end());
775
Evan Chengd38c22b2006-05-11 23:55:42 +0000776#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000777 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000778#endif
779}
780
781//===----------------------------------------------------------------------===//
782// Top-Down Scheduling
783//===----------------------------------------------------------------------===//
784
785/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000786/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000787void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
788 SUnit *SuccSU = SuccEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000789 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000790
791#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000792 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000793 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000794 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000795 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000796 assert(0);
797 }
798#endif
799
Evan Cheng038dcc52007-09-28 19:24:24 +0000800 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000801 SuccSU->isAvailable = true;
802 AvailableQueue->push(SuccSU);
803 }
804}
805
Evan Chengd38c22b2006-05-11 23:55:42 +0000806/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
807/// count of its successors. If a successor pending count is zero, add it to
808/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000809void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000810 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000811 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000812
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000813 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
814 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000815 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000816
817 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000818 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Dan Gohman14074842009-01-13 20:24:13 +0000819 I != E; ++I) {
820 assert(!I->isAssignedRegDep() &&
821 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
822
Dan Gohman2d170892008-12-09 22:54:47 +0000823 ReleaseSucc(SU, &*I);
Dan Gohman14074842009-01-13 20:24:13 +0000824 }
Dan Gohman92a36d72008-11-17 21:31:02 +0000825
Evan Chengd38c22b2006-05-11 23:55:42 +0000826 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000827 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000828}
829
Dan Gohman54a187e2007-08-20 19:28:38 +0000830/// ListScheduleTopDown - The main loop of list scheduling for top-down
831/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000832void ScheduleDAGRRList::ListScheduleTopDown() {
833 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000834
835 // All leaves to Available queue.
836 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
837 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000838 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000839 AvailableQueue->push(&SUnits[i]);
840 SUnits[i].isAvailable = true;
841 }
842 }
843
Evan Chengd38c22b2006-05-11 23:55:42 +0000844 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000845 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000846 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000847 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000848 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000849
Dan Gohmanc602dd42008-11-21 00:10:42 +0000850 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000851 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000852 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000853 }
854
Evan Chengd38c22b2006-05-11 23:55:42 +0000855#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000856 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000857#endif
858}
859
860
Evan Chengd38c22b2006-05-11 23:55:42 +0000861//===----------------------------------------------------------------------===//
862// RegReductionPriorityQueue Implementation
863//===----------------------------------------------------------------------===//
864//
865// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
866// to reduce register pressure.
867//
868namespace {
869 template<class SF>
870 class RegReductionPriorityQueue;
871
872 /// Sorting functions for the Available queue.
873 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
874 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
875 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
876 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
877
878 bool operator()(const SUnit* left, const SUnit* right) const;
879 };
880
881 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
882 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
883 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
884 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
885
886 bool operator()(const SUnit* left, const SUnit* right) const;
887 };
888} // end anonymous namespace
889
Evan Cheng961bbd32007-01-08 23:50:38 +0000890static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000891 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +0000892 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +0000893 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
894}
895
Dan Gohman186f65d2008-11-20 03:30:37 +0000896/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
897/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000898static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000899CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000900 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
901 if (SethiUllmanNumber != 0)
902 return SethiUllmanNumber;
903
904 unsigned Extra = 0;
905 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
906 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000907 if (I->isCtrl()) continue; // ignore chain preds
908 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000909 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000910 if (PredSethiUllman > SethiUllmanNumber) {
911 SethiUllmanNumber = PredSethiUllman;
912 Extra = 0;
Dan Gohman2d170892008-12-09 22:54:47 +0000913 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl())
Evan Cheng7e4abde2008-07-02 09:23:51 +0000914 ++Extra;
915 }
916
917 SethiUllmanNumber += Extra;
918
919 if (SethiUllmanNumber == 0)
920 SethiUllmanNumber = 1;
921
922 return SethiUllmanNumber;
923}
924
Evan Chengd38c22b2006-05-11 23:55:42 +0000925namespace {
926 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000927 class VISIBILITY_HIDDEN RegReductionPriorityQueue
928 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000929 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000930 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000931
Dan Gohman3f656df2008-11-20 02:45:51 +0000932 protected:
933 // SUnits - The SUnits for the current graph.
934 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000935
Dan Gohman3f656df2008-11-20 02:45:51 +0000936 const TargetInstrInfo *TII;
937 const TargetRegisterInfo *TRI;
938 ScheduleDAGRRList *scheduleDAG;
939
Dan Gohman186f65d2008-11-20 03:30:37 +0000940 // SethiUllmanNumbers - The SethiUllman number for each node.
941 std::vector<unsigned> SethiUllmanNumbers;
942
Dan Gohman3f656df2008-11-20 02:45:51 +0000943 public:
944 RegReductionPriorityQueue(const TargetInstrInfo *tii,
945 const TargetRegisterInfo *tri) :
946 Queue(SF(this)), currentQueueId(0),
947 TII(tii), TRI(tri), scheduleDAG(NULL) {}
948
949 void initNodes(std::vector<SUnit> &sunits) {
950 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000951 // Add pseudo dependency edges for two-address nodes.
952 AddPseudoTwoAddrDeps();
953 // Calculate node priorities.
954 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +0000955 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000956
Dan Gohman186f65d2008-11-20 03:30:37 +0000957 void addNode(const SUnit *SU) {
958 unsigned SUSize = SethiUllmanNumbers.size();
959 if (SUnits->size() > SUSize)
960 SethiUllmanNumbers.resize(SUSize*2, 0);
961 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
962 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000963
Dan Gohman186f65d2008-11-20 03:30:37 +0000964 void updateNode(const SUnit *SU) {
965 SethiUllmanNumbers[SU->NodeNum] = 0;
966 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
967 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000968
Dan Gohman186f65d2008-11-20 03:30:37 +0000969 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +0000970 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +0000971 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +0000972 }
Dan Gohman186f65d2008-11-20 03:30:37 +0000973
974 unsigned getNodePriority(const SUnit *SU) const {
975 assert(SU->NodeNum < SethiUllmanNumbers.size());
976 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
977 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
978 // CopyFromReg should be close to its def because it restricts
979 // allocation choices. But if it is a livein then perhaps we want it
980 // closer to its uses so it can be coalesced.
981 return 0xffff;
Dan Gohman261ee6b2009-01-07 22:30:55 +0000982 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +0000983 // CopyToReg should be close to its uses to facilitate coalescing and
984 // avoid spilling.
985 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +0000986 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
987 Opc == TargetInstrInfo::INSERT_SUBREG)
Dan Gohman186f65d2008-11-20 03:30:37 +0000988 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
989 // facilitate coalescing.
990 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +0000991 if (SU->NumSuccs == 0)
Dan Gohman186f65d2008-11-20 03:30:37 +0000992 // If SU does not have a use, i.e. it doesn't produce a value that would
993 // be consumed (e.g. store), then it terminates a chain of computation.
994 // Give it a large SethiUllman number so it will be scheduled right
995 // before its predecessors that it doesn't lengthen their live ranges.
996 return 0xffff;
Dan Gohman261ee6b2009-01-07 22:30:55 +0000997 if (SU->NumPreds == 0)
Dan Gohman186f65d2008-11-20 03:30:37 +0000998 // If SU does not have a def, schedule it close to its uses because it
999 // does not lengthen any live ranges.
1000 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001001 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001002 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001003
Evan Cheng5924bf72007-09-25 01:54:36 +00001004 unsigned size() const { return Queue.size(); }
1005
Evan Chengd38c22b2006-05-11 23:55:42 +00001006 bool empty() const { return Queue.empty(); }
1007
1008 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001009 assert(!U->NodeQueueId && "Node in the queue already");
1010 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001011 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001012 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001013
Evan Chengd38c22b2006-05-11 23:55:42 +00001014 void push_all(const std::vector<SUnit *> &Nodes) {
1015 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001016 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001017 }
1018
1019 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001020 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001021 SUnit *V = Queue.top();
1022 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001023 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001024 return V;
1025 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001026
Evan Cheng5924bf72007-09-25 01:54:36 +00001027 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001028 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001029 assert(SU->NodeQueueId != 0 && "Not in queue!");
1030 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001031 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001032 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001033
1034 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1035 scheduleDAG = scheduleDag;
1036 }
1037
1038 protected:
1039 bool canClobber(const SUnit *SU, const SUnit *Op);
1040 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001041 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001042 };
1043
Dan Gohman186f65d2008-11-20 03:30:37 +00001044 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1045 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001046
Dan Gohman186f65d2008-11-20 03:30:37 +00001047 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1048 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001049}
1050
Evan Chengb9e3db62007-03-14 22:43:40 +00001051/// closestSucc - Returns the scheduled cycle of the successor which is
1052/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001053static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001054 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001055 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001056 I != E; ++I) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001057 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001058 // If there are bunch of CopyToRegs stacked up, they should be considered
1059 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001060 if (I->getSUnit()->getNode() &&
1061 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001062 Height = closestSucc(I->getSUnit())+1;
1063 if (Height > MaxHeight)
1064 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001065 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001066 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001067}
1068
Evan Cheng61bc51e2007-12-20 02:22:36 +00001069/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1070/// for scratch registers. Live-in operands and live-out results don't count
1071/// since they are "fixed".
1072static unsigned calcMaxScratches(const SUnit *SU) {
1073 unsigned Scratches = 0;
1074 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1075 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001076 if (I->isCtrl()) continue; // ignore chain preds
1077 if (!I->getSUnit()->getNode() ||
1078 I->getSUnit()->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001079 Scratches++;
1080 }
1081 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1082 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001083 if (I->isCtrl()) continue; // ignore chain succs
1084 if (!I->getSUnit()->getNode() ||
1085 I->getSUnit()->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001086 Scratches += 10;
1087 }
1088 return Scratches;
1089}
1090
Evan Chengd38c22b2006-05-11 23:55:42 +00001091// Bottom up
1092bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001093 unsigned LPriority = SPQ->getNodePriority(left);
1094 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001095 if (LPriority != RPriority)
1096 return LPriority > RPriority;
1097
1098 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1099 // e.g.
1100 // t1 = op t2, c1
1101 // t3 = op t4, c2
1102 //
1103 // and the following instructions are both ready.
1104 // t2 = op c3
1105 // t4 = op c4
1106 //
1107 // Then schedule t2 = op first.
1108 // i.e.
1109 // t4 = op c4
1110 // t2 = op c3
1111 // t1 = op t2, c1
1112 // t3 = op t4, c2
1113 //
1114 // This creates more short live intervals.
1115 unsigned LDist = closestSucc(left);
1116 unsigned RDist = closestSucc(right);
1117 if (LDist != RDist)
1118 return LDist < RDist;
1119
1120 // Intuitively, it's good to push down instructions whose results are
1121 // liveout so their long live ranges won't conflict with other values
1122 // which are needed inside the BB. Further prioritize liveout instructions
1123 // by the number of operands which are calculated within the BB.
1124 unsigned LScratch = calcMaxScratches(left);
1125 unsigned RScratch = calcMaxScratches(right);
1126 if (LScratch != RScratch)
1127 return LScratch > RScratch;
1128
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001129 if (left->getHeight() != right->getHeight())
1130 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001131
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001132 if (left->getDepth() != right->getDepth())
1133 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001134
Roman Levenstein6b371142008-04-29 09:07:59 +00001135 assert(left->NodeQueueId && right->NodeQueueId &&
1136 "NodeQueueId cannot be zero");
1137 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001138}
1139
Dan Gohman3f656df2008-11-20 02:45:51 +00001140template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001141bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001142RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001143 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001144 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001145 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001146 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001147 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001148 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001149 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001150 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001151 if (DU->getNodeId() != -1 &&
1152 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001153 return true;
1154 }
1155 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001156 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001157 return false;
1158}
1159
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001160
Evan Chenga5e595d2007-09-28 22:32:30 +00001161/// hasCopyToRegUse - Return true if SU has a value successor that is a
1162/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001163static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001164 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1165 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001166 if (I->isCtrl()) continue;
1167 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001168 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001169 return true;
1170 }
1171 return false;
1172}
1173
Evan Chengf9891412007-12-20 09:25:31 +00001174/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001175/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001176static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001177 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001178 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001179 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001180 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1181 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001182 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001183 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001184 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001185 if (!SUImpDefs)
1186 return false;
1187 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001188 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001189 if (VT == MVT::Flag || VT == MVT::Other)
1190 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001191 if (!N->hasAnyUseOfValue(i))
1192 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001193 unsigned Reg = ImpDefs[i - NumDefs];
1194 for (;*SUImpDefs; ++SUImpDefs) {
1195 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001196 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001197 return true;
1198 }
1199 }
1200 return false;
1201}
1202
Evan Chengd38c22b2006-05-11 23:55:42 +00001203/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1204/// it as a def&use operand. Add a pseudo control edge from it to the other
1205/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001206/// first (lower in the schedule). If both nodes are two-address, favor the
1207/// one that has a CopyToReg use (more likely to be a loop induction update).
1208/// If both are two-address, but one is commutable while the other is not
1209/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001210template<class SF>
1211void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001212 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001213 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001214 if (!SU->isTwoAddress)
1215 continue;
1216
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001217 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001218 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001219 continue;
1220
Dan Gohman17059682008-07-17 19:10:17 +00001221 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001222 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001223 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001224 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001225 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001226 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1227 continue;
1228 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1229 if (DU->getNodeId() == -1)
1230 continue;
1231 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1232 if (!DUSU) continue;
1233 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1234 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001235 if (I->isCtrl()) continue;
1236 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001237 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001238 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001239 // Be conservative. Ignore if nodes aren't at roughly the same
1240 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001241 if (SuccSU->getHeight() < SU->getHeight() &&
1242 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001243 continue;
1244 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1245 continue;
1246 // Don't constrain nodes with physical register defs if the
1247 // predecessor can clobber them.
1248 if (SuccSU->hasPhysRegDefs) {
1249 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001250 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001251 }
1252 // Don't constraint extract_subreg / insert_subreg these may be
1253 // coalesced away. We don't them close to their uses.
1254 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1255 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1256 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1257 continue;
1258 if ((!canClobber(SuccSU, DUSU) ||
1259 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1260 (!SU->isCommutable && SuccSU->isCommutable)) &&
1261 !scheduleDAG->IsReachable(SuccSU, SU)) {
Dan Gohman30cad9c2008-12-04 02:14:57 +00001262 DOUT << "Adding a pseudo-two-addr edge from SU # " << SU->NodeNum
Dan Gohman82016c22008-11-19 02:00:32 +00001263 << " to SU #" << SuccSU->NodeNum << "\n";
Dan Gohman79c35162009-01-06 01:19:04 +00001264 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001265 /*Reg=*/0, /*isNormalMemory=*/false,
1266 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001267 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001268 }
1269 }
1270 }
1271 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001272}
1273
Evan Cheng6730f032007-01-08 23:55:53 +00001274/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1275/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001276template<class SF>
1277void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001278 SethiUllmanNumbers.assign(SUnits->size(), 0);
1279
1280 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001281 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001282}
Evan Chengd38c22b2006-05-11 23:55:42 +00001283
Roman Levenstein30d09512008-03-27 09:44:37 +00001284/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001285/// predecessors of the successors of the SUnit SU. Stop when the provided
1286/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001287static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1288 unsigned Limit) {
1289 unsigned Sum = 0;
1290 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1291 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001292 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001293 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1294 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001295 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001296 if (!PredSU->isScheduled)
1297 if (++Sum > Limit)
1298 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001299 }
1300 }
1301 return Sum;
1302}
1303
Evan Chengd38c22b2006-05-11 23:55:42 +00001304
1305// Top down
1306bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001307 unsigned LPriority = SPQ->getNodePriority(left);
1308 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001309 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1310 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001311 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1312 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001313 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1314 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001315
1316 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1317 return false;
1318 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1319 return true;
1320
Evan Chengd38c22b2006-05-11 23:55:42 +00001321 if (LIsFloater)
1322 LBonus -= 2;
1323 if (RIsFloater)
1324 RBonus -= 2;
1325 if (left->NumSuccs == 1)
1326 LBonus += 2;
1327 if (right->NumSuccs == 1)
1328 RBonus += 2;
1329
Evan Cheng73bdf042008-03-01 00:39:47 +00001330 if (LPriority+LBonus != RPriority+RBonus)
1331 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001332
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001333 if (left->getDepth() != right->getDepth())
1334 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001335
1336 if (left->NumSuccsLeft != right->NumSuccsLeft)
1337 return left->NumSuccsLeft > right->NumSuccsLeft;
1338
Roman Levenstein6b371142008-04-29 09:07:59 +00001339 assert(left->NodeQueueId && right->NodeQueueId &&
1340 "NodeQueueId cannot be zero");
1341 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001342}
1343
Evan Chengd38c22b2006-05-11 23:55:42 +00001344//===----------------------------------------------------------------------===//
1345// Public Constructor Functions
1346//===----------------------------------------------------------------------===//
1347
Jim Laskey03593f72006-08-01 18:29:48 +00001348llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1349 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001350 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001351 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001352 bool) {
Dan Gohman5499e892008-11-11 17:50:47 +00001353 const TargetInstrInfo *TII = TM->getInstrInfo();
1354 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001355
Evan Cheng7e4abde2008-07-02 09:23:51 +00001356 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001357
Evan Cheng7e4abde2008-07-02 09:23:51 +00001358 ScheduleDAGRRList *SD =
Dan Gohmanfd08af42008-11-20 03:11:19 +00001359 new ScheduleDAGRRList(DAG, BB, *TM, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001360 PQ->setScheduleDAG(SD);
1361 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001362}
1363
Jim Laskey03593f72006-08-01 18:29:48 +00001364llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1365 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001366 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001367 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001368 bool) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001369 const TargetInstrInfo *TII = TM->getInstrInfo();
1370 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
1371
1372 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1373
Dan Gohmanfd08af42008-11-20 03:11:19 +00001374 ScheduleDAGRRList *SD = new ScheduleDAGRRList(DAG, BB, *TM, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001375 PQ->setScheduleDAG(SD);
1376 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001377}