blob: 94006b5d8c8b1a3418f9d961937cd9e8e8af848a [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 Gohman483377c2009-02-06 17:22:58 +000019#include "ScheduleDAGSDNodes.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman619ef482009-01-15 19:20:50 +000021#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Dan Gohmana4db3352008-06-21 18:35:25 +000028#include "llvm/ADT/PriorityQueue.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000029#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000030#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000031#include "llvm/ADT/STLExtras.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000032#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000033using 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 Gohman619ef482009-01-15 19:20:50 +000075 ScheduleDAGRRList(MachineFunction &mf,
76 bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000077 SchedulingPriorityQueue *availqueue)
Dan Gohman619ef482009-01-15 19:20:50 +000078 : ScheduleDAGSDNodes(mf), 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
Dan Gohman60d68442009-01-29 19:49:27 +000093 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000094 /// 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 Gohman60d68442009-01-29 19:49:27 +0000116 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000117 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000118 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000119 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000120 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000121 void ScheduleNodeBottomUp(SUnit*, unsigned);
122 void ScheduleNodeTopDown(SUnit*, unsigned);
123 void UnscheduleNodeBottomUp(SUnit*);
124 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
125 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000126 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
127 const TargetRegisterClass*,
128 const TargetRegisterClass*,
129 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000130 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000131 void ListScheduleTopDown();
132 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000133
134
135 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000136 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000138 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000139 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000140 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000141 if (NewNode->NodeNum >= NumSUnits)
142 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000143 return NewNode;
144 }
145
Roman Levenstein733a4d62008-03-26 11:23:38 +0000146 /// CreateClone - Creates a new SUnit from an existing one.
147 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000148 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000149 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000151 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000152 if (NewNode->NodeNum >= NumSUnits)
153 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000154 return NewNode;
155 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000156
157 /// ForceUnitLatencies - Return true, since register-pressure-reducing
158 /// scheduling doesn't need actual latency information.
159 bool ForceUnitLatencies() const { return true; }
Evan Chengd38c22b2006-05-11 23:55:42 +0000160};
161} // end anonymous namespace
162
163
164/// Schedule - Schedule the DAG using list scheduling.
165void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000166 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000167
Dan Gohmanc07f6862008-09-23 18:50:48 +0000168 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000169 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
170 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000171
Dan Gohman04543e72008-12-23 18:36:58 +0000172 // Build the scheduling graph.
173 BuildSchedGraph();
Evan Chengd38c22b2006-05-11 23:55:42 +0000174
Evan Chengd38c22b2006-05-11 23:55:42 +0000175 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000176 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000177 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000178
Dan Gohman46520a22008-06-21 19:18:17 +0000179 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000180
Evan Chengd38c22b2006-05-11 23:55:42 +0000181 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
182 if (isBottomUp)
183 ListScheduleBottomUp();
184 else
185 ListScheduleTopDown();
186
187 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000188}
Evan Chengd38c22b2006-05-11 23:55:42 +0000189
190//===----------------------------------------------------------------------===//
191// Bottom-Up Scheduling
192//===----------------------------------------------------------------------===//
193
Evan Chengd38c22b2006-05-11 23:55:42 +0000194/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000195/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000196void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000197 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000198 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000199
200#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000201 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000202 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000203 PredSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000204 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000205 assert(0);
206 }
207#endif
208
Dan Gohmanb9543432009-02-10 23:27:53 +0000209 // If all the node's successors are scheduled, this node is ready
210 // to be scheduled. Ignore the special EntrySU node.
211 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000212 PredSU->isAvailable = true;
213 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000214 }
215}
216
Dan Gohmanb9543432009-02-10 23:27:53 +0000217void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000218 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000219 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000220 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000221 ReleasePred(SU, &*I);
222 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000223 // This is a physical register dependency and it's impossible or
224 // expensive to copy the register. Make sure nothing that can
225 // clobber the register is scheduled between the predecessor and
226 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000227 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000228 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000229 LiveRegDefs[I->getReg()] = I->getSUnit();
230 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000231 }
232 }
233 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000234}
235
236/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
237/// count of its predecessors. If a predecessor pending count is zero, add it to
238/// the Available queue.
239void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
240 DOUT << "*** Scheduling [" << CurCycle << "]: ";
241 DEBUG(SU->dump(this));
242
243 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
244 SU->setHeightToAtLeast(CurCycle);
245 Sequence.push_back(SU);
246
247 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000248
249 // Release all the implicit physical register defs that are live.
250 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
251 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000252 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000253 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000254 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000255 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000256 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000257 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000258 LiveRegDefs[I->getReg()] = NULL;
259 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000260 }
261 }
262 }
263
Evan Chengd38c22b2006-05-11 23:55:42 +0000264 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000265 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000266}
267
Evan Cheng5924bf72007-09-25 01:54:36 +0000268/// CapturePred - This does the opposite of ReleasePred. Since SU is being
269/// unscheduled, incrcease the succ left count of its predecessors. Remove
270/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000271void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
272 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000273 if (PredSU->isAvailable) {
274 PredSU->isAvailable = false;
275 if (!PredSU->isPending)
276 AvailableQueue->remove(PredSU);
277 }
278
Evan Cheng038dcc52007-09-28 19:24:24 +0000279 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000280}
281
282/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
283/// its predecessor states to reflect the change.
284void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000285 DOUT << "*** Unscheduling [" << SU->getHeight() << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000286 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000287
288 AvailableQueue->UnscheduledNode(SU);
289
290 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
291 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000292 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000293 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000294 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000295 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000296 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000297 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000298 LiveRegDefs[I->getReg()] = NULL;
299 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000300 }
301 }
302
303 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
304 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000305 if (I->isAssignedRegDep()) {
306 if (!LiveRegDefs[I->getReg()]) {
307 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000308 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000309 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000310 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
311 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000312 }
313 }
314
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000315 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000316 SU->isScheduled = false;
317 SU->isAvailable = true;
318 AvailableQueue->push(SU);
319}
320
Evan Cheng8e136a92007-09-26 21:36:17 +0000321/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000322/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000323void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
324 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000325 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000326 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000327 OldSU = Sequence.back();
328 Sequence.pop_back();
329 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000330 // Don't try to remove SU from AvailableQueue.
331 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000332 UnscheduleNodeBottomUp(OldSU);
333 --CurCycle;
334 }
335
Dan Gohman60d68442009-01-29 19:49:27 +0000336 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000337
338 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000339}
340
Evan Cheng5924bf72007-09-25 01:54:36 +0000341/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
342/// successors to the newly created node.
343SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000344 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000345 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000346
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000347 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000348 if (!N)
349 return NULL;
350
351 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000352 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000353 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000354 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000355 if (VT == MVT::Flag)
356 return NULL;
357 else if (VT == MVT::Other)
358 TryUnfold = true;
359 }
Evan Cheng79e97132007-10-05 01:39:18 +0000360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000361 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000362 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000363 if (VT == MVT::Flag)
364 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000365 }
366
367 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000368 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000369 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000370 return NULL;
371
372 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
373 assert(NewNodes.size() == 2 && "Expected a load folding node!");
374
375 N = NewNodes[1];
376 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000377 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000378 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000379 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000380 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
381 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000382 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000383
Dan Gohmane52e0892008-11-11 21:34:44 +0000384 // LoadNode may already exist. This can happen when there is another
385 // load from the same location and producing the same type of value
386 // but it has different alignment or volatileness.
387 bool isNewLoad = true;
388 SUnit *LoadSU;
389 if (LoadNode->getNodeId() != -1) {
390 LoadSU = &SUnits[LoadNode->getNodeId()];
391 isNewLoad = false;
392 } else {
393 LoadSU = CreateNewSUnit(LoadNode);
394 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000395 ComputeLatency(LoadSU);
396 }
397
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000398 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000399 assert(N->getNodeId() == -1 && "Node already inserted!");
400 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000401
Dan Gohman17059682008-07-17 19:10:17 +0000402 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000403 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000404 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000405 NewSU->isTwoAddress = true;
406 break;
407 }
408 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000409 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000410 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000411 ComputeLatency(NewSU);
412
Dan Gohman2d170892008-12-09 22:54:47 +0000413 SDep ChainPred;
Evan Cheng79e97132007-10-05 01:39:18 +0000414 SmallVector<SDep, 4> ChainSuccs;
415 SmallVector<SDep, 4> LoadPreds;
416 SmallVector<SDep, 4> NodePreds;
417 SmallVector<SDep, 4> NodeSuccs;
418 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
419 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000420 if (I->isCtrl())
421 ChainPred = *I;
422 else if (I->getSUnit()->getNode() &&
423 I->getSUnit()->getNode()->isOperandOf(LoadNode))
424 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000425 else
Dan Gohman2d170892008-12-09 22:54:47 +0000426 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000427 }
428 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
429 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000430 if (I->isCtrl())
431 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000432 else
Dan Gohman2d170892008-12-09 22:54:47 +0000433 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000434 }
435
Dan Gohman2d170892008-12-09 22:54:47 +0000436 if (ChainPred.getSUnit()) {
437 RemovePred(SU, ChainPred);
Dan Gohman4370f262008-04-15 01:22:18 +0000438 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000439 AddPred(LoadSU, ChainPred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000440 }
Evan Cheng79e97132007-10-05 01:39:18 +0000441 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000442 const SDep &Pred = LoadPreds[i];
443 RemovePred(SU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000444 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000445 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000446 }
Evan Cheng79e97132007-10-05 01:39:18 +0000447 }
448 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000449 const SDep &Pred = NodePreds[i];
450 RemovePred(SU, Pred);
451 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000452 }
453 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000454 SDep D = NodeSuccs[i];
455 SUnit *SuccDep = D.getSUnit();
456 D.setSUnit(SU);
457 RemovePred(SuccDep, D);
458 D.setSUnit(NewSU);
459 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000460 }
461 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000462 SDep D = ChainSuccs[i];
463 SUnit *SuccDep = D.getSUnit();
464 D.setSUnit(SU);
465 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000466 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000467 D.setSUnit(LoadSU);
468 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000469 }
Evan Cheng79e97132007-10-05 01:39:18 +0000470 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000471 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000472 AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000473 }
Evan Cheng79e97132007-10-05 01:39:18 +0000474
Evan Cheng91e0fc92007-12-18 08:42:10 +0000475 if (isNewLoad)
476 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000477 AvailableQueue->addNode(NewSU);
478
479 ++NumUnfolds;
480
481 if (NewSU->NumSuccsLeft == 0) {
482 NewSU->isAvailable = true;
483 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000484 }
485 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000486 }
487
488 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000489 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000490
491 // New SUnit has the exact same predecessors.
492 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
493 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000494 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000495 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000496
497 // Only copy scheduled successors. Cut them from old node's successor
498 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000499 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000500 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
501 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000502 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000503 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000504 SUnit *SuccSU = I->getSUnit();
505 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000506 SDep D = *I;
507 D.setSUnit(NewSU);
508 AddPred(SuccSU, D);
509 D.setSUnit(SU);
510 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000511 }
512 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000513 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000514 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000515
516 AvailableQueue->updateNode(SU);
517 AvailableQueue->addNode(NewSU);
518
Evan Cheng1ec79b42007-09-27 07:09:03 +0000519 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000520 return NewSU;
521}
522
Evan Chengb2c42c62009-01-12 03:19:55 +0000523/// InsertCopiesAndMoveSuccs - Insert register copies and move all
524/// scheduled successors of the given SUnit to the last copy.
525void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
526 const TargetRegisterClass *DestRC,
527 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000528 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000529 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000530 CopyFromSU->CopySrcRC = SrcRC;
531 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000532
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000533 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000534 CopyToSU->CopySrcRC = DestRC;
535 CopyToSU->CopyDstRC = SrcRC;
536
537 // Only copy scheduled successors. Cut them from old node's successor
538 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000539 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000540 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
541 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000542 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000543 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000544 SUnit *SuccSU = I->getSUnit();
545 if (SuccSU->isScheduled) {
546 SDep D = *I;
547 D.setSUnit(CopyToSU);
548 AddPred(SuccSU, D);
549 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000550 }
551 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000552 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000553 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000554
Dan Gohman2d170892008-12-09 22:54:47 +0000555 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
556 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000557
558 AvailableQueue->updateNode(SU);
559 AvailableQueue->addNode(CopyFromSU);
560 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000561 Copies.push_back(CopyFromSU);
562 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000563
Evan Chengb2c42c62009-01-12 03:19:55 +0000564 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000565}
566
567/// getPhysicalRegisterVT - Returns the ValueType of the physical register
568/// definition of the specified node.
569/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000570static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
571 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000572 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000573 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000574 unsigned NumRes = TID.getNumDefs();
575 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000576 if (Reg == *ImpDef)
577 break;
578 ++NumRes;
579 }
580 return N->getValueType(NumRes);
581}
582
Evan Cheng5924bf72007-09-25 01:54:36 +0000583/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
584/// scheduling of the given node to satisfy live physical register dependencies.
585/// If the specific node is the last one that's available to schedule, do
586/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000587bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
588 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000589 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000590 return false;
591
Evan Chenge6f92252007-09-27 18:46:06 +0000592 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000593 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000594 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
595 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000596 if (I->isAssignedRegDep()) {
597 unsigned Reg = I->getReg();
598 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
Evan Chenge6f92252007-09-27 18:46:06 +0000599 if (RegAdded.insert(Reg))
600 LRegs.push_back(Reg);
601 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000602 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000603 *Alias; ++Alias)
Dan Gohman2d170892008-12-09 22:54:47 +0000604 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
Evan Chenge6f92252007-09-27 18:46:06 +0000605 if (RegAdded.insert(*Alias))
606 LRegs.push_back(*Alias);
607 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000608 }
609 }
610
Dan Gohman072734e2008-11-13 23:24:17 +0000611 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
612 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000613 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000614 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000615 if (!TID.ImplicitDefs)
616 continue;
617 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000618 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000619 if (RegAdded.insert(*Reg))
620 LRegs.push_back(*Reg);
621 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000622 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000623 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000624 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000625 if (RegAdded.insert(*Alias))
626 LRegs.push_back(*Alias);
627 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000628 }
629 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000630 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000631}
632
Evan Cheng1ec79b42007-09-27 07:09:03 +0000633
Evan Chengd38c22b2006-05-11 23:55:42 +0000634/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
635/// schedulers.
636void ScheduleDAGRRList::ListScheduleBottomUp() {
637 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000638
639 // Release any predecessors of the special Exit node.
640 ReleasePredecessors(&ExitSU, CurCycle);
641
Evan Chengd38c22b2006-05-11 23:55:42 +0000642 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000643 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000644 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000645 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
646 RootSU->isAvailable = true;
647 AvailableQueue->push(RootSU);
648 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000649
650 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000651 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000652 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000653 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000654 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000655 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000656 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000657 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000658 SUnit *CurSU = AvailableQueue->pop();
659 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000660 SmallVector<unsigned, 4> LRegs;
661 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
662 break;
663 Delayed = true;
664 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000665
666 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
667 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000669 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000670
671 // All candidates are delayed due to live physical reg dependencies.
672 // Try backtracking, code duplication, or inserting cross class copies
673 // to resolve it.
674 if (Delayed && !CurSU) {
675 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
676 SUnit *TrySU = NotReady[i];
677 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
678
679 // Try unscheduling up to the point where it's safe to schedule
680 // this node.
681 unsigned LiveCycle = CurCycle;
682 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
683 unsigned Reg = LRegs[j];
684 unsigned LCycle = LiveRegCycles[Reg];
685 LiveCycle = std::min(LiveCycle, LCycle);
686 }
687 SUnit *OldSU = Sequence[LiveCycle];
688 if (!WillCreateCycle(TrySU, OldSU)) {
689 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
690 // Force the current node to be scheduled before the node that
691 // requires the physical reg dep.
692 if (OldSU->isAvailable) {
693 OldSU->isAvailable = false;
694 AvailableQueue->remove(OldSU);
695 }
Dan Gohman2d170892008-12-09 22:54:47 +0000696 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
697 /*Reg=*/0, /*isNormalMemory=*/false,
698 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000699 // If one or more successors has been unscheduled, then the current
700 // node is no longer avaialable. Schedule a successor that's now
701 // available instead.
702 if (!TrySU->isAvailable)
703 CurSU = AvailableQueue->pop();
704 else {
705 CurSU = TrySU;
706 TrySU->isPending = false;
707 NotReady.erase(NotReady.begin()+i);
708 }
709 break;
710 }
711 }
712
713 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000714 // Can't backtrack. If it's too expensive to copy the value, then try
715 // duplicate the nodes that produces these "too expensive to copy"
716 // values to break the dependency. In case even that doesn't work,
717 // insert cross class copies.
718 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000719 SUnit *TrySU = NotReady[0];
720 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
721 assert(LRegs.size() == 1 && "Can't handle this yet!");
722 unsigned Reg = LRegs[0];
723 SUnit *LRDef = LiveRegDefs[Reg];
Evan Chengb2c42c62009-01-12 03:19:55 +0000724 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
725 const TargetRegisterClass *RC =
726 TRI->getPhysicalRegisterRegClass(Reg, VT);
727 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
728
729 // If cross copy register class is null, then it must be possible copy
730 // the value directly. Do not try duplicate the def.
731 SUnit *NewDef = 0;
732 if (DestRC)
733 NewDef = CopyAndMoveSuccessors(LRDef);
734 else
735 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000736 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000737 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000738 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000739 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Cheng0c4fe262009-01-09 20:42:34 +0000740 DOUT << "Adding an edge from SU #" << TrySU->NodeNum
Evan Cheng1ec79b42007-09-27 07:09:03 +0000741 << " to SU #" << Copies.front()->NodeNum << "\n";
Dan Gohman2d170892008-12-09 22:54:47 +0000742 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000743 /*Reg=*/0, /*isNormalMemory=*/false,
744 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000745 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000746 NewDef = Copies.back();
747 }
748
Evan Cheng0c4fe262009-01-09 20:42:34 +0000749 DOUT << "Adding an edge from SU #" << NewDef->NodeNum
Evan Cheng1ec79b42007-09-27 07:09:03 +0000750 << " to SU #" << TrySU->NodeNum << "\n";
751 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000752 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000753 /*Reg=*/0, /*isNormalMemory=*/false,
754 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000755 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000756 TrySU->isAvailable = false;
757 CurSU = NewDef;
758 }
759
Dan Gohman60d68442009-01-29 19:49:27 +0000760 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000761 }
762
Evan Chengd38c22b2006-05-11 23:55:42 +0000763 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000764 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
765 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000766 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000767 if (NotReady[i]->isAvailable)
768 AvailableQueue->push(NotReady[i]);
769 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000770 NotReady.clear();
771
Dan Gohmanc602dd42008-11-21 00:10:42 +0000772 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000773 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000774 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000775 }
776
Evan Chengd38c22b2006-05-11 23:55:42 +0000777 // Reverse the order if it is bottom up.
778 std::reverse(Sequence.begin(), Sequence.end());
779
Evan Chengd38c22b2006-05-11 23:55:42 +0000780#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000781 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000782#endif
783}
784
785//===----------------------------------------------------------------------===//
786// Top-Down Scheduling
787//===----------------------------------------------------------------------===//
788
789/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000790/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000791void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000792 SUnit *SuccSU = SuccEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000793 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000794
795#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000796 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000797 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000798 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000799 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000800 assert(0);
801 }
802#endif
803
Dan Gohmanb9543432009-02-10 23:27:53 +0000804 // If all the node's predecessors are scheduled, this node is ready
805 // to be scheduled. Ignore the special ExitSU node.
806 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000807 SuccSU->isAvailable = true;
808 AvailableQueue->push(SuccSU);
809 }
810}
811
Dan Gohmanb9543432009-02-10 23:27:53 +0000812void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
813 // Top down: release successors
814 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
815 I != E; ++I) {
816 assert(!I->isAssignedRegDep() &&
817 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
818
819 ReleaseSucc(SU, &*I);
820 }
821}
822
Evan Chengd38c22b2006-05-11 23:55:42 +0000823/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
824/// count of its successors. If a successor pending count is zero, add it to
825/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000826void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000827 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000828 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000829
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000830 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
831 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000832 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000833
Dan Gohmanb9543432009-02-10 23:27:53 +0000834 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000835 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000836 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000837}
838
Dan Gohman54a187e2007-08-20 19:28:38 +0000839/// ListScheduleTopDown - The main loop of list scheduling for top-down
840/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000841void ScheduleDAGRRList::ListScheduleTopDown() {
842 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000843
Dan Gohmanb9543432009-02-10 23:27:53 +0000844 // Release any successors of the special Entry node.
845 ReleaseSuccessors(&EntrySU);
846
Evan Chengd38c22b2006-05-11 23:55:42 +0000847 // All leaves to Available queue.
848 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
849 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000850 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000851 AvailableQueue->push(&SUnits[i]);
852 SUnits[i].isAvailable = true;
853 }
854 }
855
Evan Chengd38c22b2006-05-11 23:55:42 +0000856 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000857 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000858 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000859 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000860 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000861
Dan Gohmanc602dd42008-11-21 00:10:42 +0000862 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000863 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000864 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000865 }
866
Evan Chengd38c22b2006-05-11 23:55:42 +0000867#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000868 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000869#endif
870}
871
872
Evan Chengd38c22b2006-05-11 23:55:42 +0000873//===----------------------------------------------------------------------===//
874// RegReductionPriorityQueue Implementation
875//===----------------------------------------------------------------------===//
876//
877// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
878// to reduce register pressure.
879//
880namespace {
881 template<class SF>
882 class RegReductionPriorityQueue;
883
884 /// Sorting functions for the Available queue.
885 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
886 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
887 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
888 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
889
890 bool operator()(const SUnit* left, const SUnit* right) const;
891 };
892
893 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
894 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
895 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
896 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
897
898 bool operator()(const SUnit* left, const SUnit* right) const;
899 };
900} // end anonymous namespace
901
Evan Cheng961bbd32007-01-08 23:50:38 +0000902static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000903 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +0000904 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +0000905 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
906}
907
Dan Gohman186f65d2008-11-20 03:30:37 +0000908/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
909/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000910static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000911CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000912 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
913 if (SethiUllmanNumber != 0)
914 return SethiUllmanNumber;
915
916 unsigned Extra = 0;
917 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
918 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000919 if (I->isCtrl()) continue; // ignore chain preds
920 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000921 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000922 if (PredSethiUllman > SethiUllmanNumber) {
923 SethiUllmanNumber = PredSethiUllman;
924 Extra = 0;
Dan Gohman2d170892008-12-09 22:54:47 +0000925 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl())
Evan Cheng7e4abde2008-07-02 09:23:51 +0000926 ++Extra;
927 }
928
929 SethiUllmanNumber += Extra;
930
931 if (SethiUllmanNumber == 0)
932 SethiUllmanNumber = 1;
933
934 return SethiUllmanNumber;
935}
936
Evan Chengd38c22b2006-05-11 23:55:42 +0000937namespace {
938 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000939 class VISIBILITY_HIDDEN RegReductionPriorityQueue
940 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000941 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000942 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000943
Dan Gohman3f656df2008-11-20 02:45:51 +0000944 protected:
945 // SUnits - The SUnits for the current graph.
946 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000947
Dan Gohman3f656df2008-11-20 02:45:51 +0000948 const TargetInstrInfo *TII;
949 const TargetRegisterInfo *TRI;
950 ScheduleDAGRRList *scheduleDAG;
951
Dan Gohman186f65d2008-11-20 03:30:37 +0000952 // SethiUllmanNumbers - The SethiUllman number for each node.
953 std::vector<unsigned> SethiUllmanNumbers;
954
Dan Gohman3f656df2008-11-20 02:45:51 +0000955 public:
956 RegReductionPriorityQueue(const TargetInstrInfo *tii,
957 const TargetRegisterInfo *tri) :
958 Queue(SF(this)), currentQueueId(0),
959 TII(tii), TRI(tri), scheduleDAG(NULL) {}
960
961 void initNodes(std::vector<SUnit> &sunits) {
962 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000963 // Add pseudo dependency edges for two-address nodes.
964 AddPseudoTwoAddrDeps();
965 // Calculate node priorities.
966 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +0000967 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000968
Dan Gohman186f65d2008-11-20 03:30:37 +0000969 void addNode(const SUnit *SU) {
970 unsigned SUSize = SethiUllmanNumbers.size();
971 if (SUnits->size() > SUSize)
972 SethiUllmanNumbers.resize(SUSize*2, 0);
973 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
974 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000975
Dan Gohman186f65d2008-11-20 03:30:37 +0000976 void updateNode(const SUnit *SU) {
977 SethiUllmanNumbers[SU->NodeNum] = 0;
978 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
979 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000980
Dan Gohman186f65d2008-11-20 03:30:37 +0000981 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +0000982 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +0000983 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +0000984 }
Dan Gohman186f65d2008-11-20 03:30:37 +0000985
986 unsigned getNodePriority(const SUnit *SU) const {
987 assert(SU->NodeNum < SethiUllmanNumbers.size());
988 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
989 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
990 // CopyFromReg should be close to its def because it restricts
991 // allocation choices. But if it is a livein then perhaps we want it
992 // closer to its uses so it can be coalesced.
993 return 0xffff;
Dan Gohman261ee6b2009-01-07 22:30:55 +0000994 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +0000995 // CopyToReg should be close to its uses to facilitate coalescing and
996 // avoid spilling.
997 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +0000998 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
999 Opc == TargetInstrInfo::INSERT_SUBREG)
Dan Gohman186f65d2008-11-20 03:30:37 +00001000 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1001 // facilitate coalescing.
1002 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001003 if (SU->NumSuccs == 0)
Dan Gohman186f65d2008-11-20 03:30:37 +00001004 // If SU does not have a use, i.e. it doesn't produce a value that would
1005 // be consumed (e.g. store), then it terminates a chain of computation.
1006 // Give it a large SethiUllman number so it will be scheduled right
1007 // before its predecessors that it doesn't lengthen their live ranges.
1008 return 0xffff;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001009 if (SU->NumPreds == 0)
Dan Gohman186f65d2008-11-20 03:30:37 +00001010 // If SU does not have a def, schedule it close to its uses because it
1011 // does not lengthen any live ranges.
1012 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001013 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001014 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001015
Evan Cheng5924bf72007-09-25 01:54:36 +00001016 unsigned size() const { return Queue.size(); }
1017
Evan Chengd38c22b2006-05-11 23:55:42 +00001018 bool empty() const { return Queue.empty(); }
1019
1020 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001021 assert(!U->NodeQueueId && "Node in the queue already");
1022 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001023 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001024 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001025
Evan Chengd38c22b2006-05-11 23:55:42 +00001026 void push_all(const std::vector<SUnit *> &Nodes) {
1027 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001028 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001029 }
1030
1031 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001032 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001033 SUnit *V = Queue.top();
1034 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001035 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001036 return V;
1037 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001038
Evan Cheng5924bf72007-09-25 01:54:36 +00001039 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001040 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001041 assert(SU->NodeQueueId != 0 && "Not in queue!");
1042 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001043 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001044 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001045
1046 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1047 scheduleDAG = scheduleDag;
1048 }
1049
1050 protected:
1051 bool canClobber(const SUnit *SU, const SUnit *Op);
1052 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001053 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001054 };
1055
Dan Gohman186f65d2008-11-20 03:30:37 +00001056 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1057 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001058
Dan Gohman186f65d2008-11-20 03:30:37 +00001059 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1060 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001061}
1062
Evan Chengb9e3db62007-03-14 22:43:40 +00001063/// closestSucc - Returns the scheduled cycle of the successor which is
1064/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001065static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001066 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001067 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001068 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001069 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001070 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001071 // If there are bunch of CopyToRegs stacked up, they should be considered
1072 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001073 if (I->getSUnit()->getNode() &&
1074 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001075 Height = closestSucc(I->getSUnit())+1;
1076 if (Height > MaxHeight)
1077 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001078 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001079 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001080}
1081
Evan Cheng61bc51e2007-12-20 02:22:36 +00001082/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1083/// for scratch registers. Live-in operands and live-out results don't count
1084/// since they are "fixed".
1085static unsigned calcMaxScratches(const SUnit *SU) {
1086 unsigned Scratches = 0;
1087 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1088 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001089 if (I->isCtrl()) continue; // ignore chain preds
1090 if (!I->getSUnit()->getNode() ||
1091 I->getSUnit()->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001092 Scratches++;
1093 }
1094 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1095 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001096 if (I->isCtrl()) continue; // ignore chain succs
1097 if (!I->getSUnit()->getNode() ||
1098 I->getSUnit()->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001099 Scratches += 10;
1100 }
1101 return Scratches;
1102}
1103
Evan Chengd38c22b2006-05-11 23:55:42 +00001104// Bottom up
1105bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001106 unsigned LPriority = SPQ->getNodePriority(left);
1107 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001108 if (LPriority != RPriority)
1109 return LPriority > RPriority;
1110
1111 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1112 // e.g.
1113 // t1 = op t2, c1
1114 // t3 = op t4, c2
1115 //
1116 // and the following instructions are both ready.
1117 // t2 = op c3
1118 // t4 = op c4
1119 //
1120 // Then schedule t2 = op first.
1121 // i.e.
1122 // t4 = op c4
1123 // t2 = op c3
1124 // t1 = op t2, c1
1125 // t3 = op t4, c2
1126 //
1127 // This creates more short live intervals.
1128 unsigned LDist = closestSucc(left);
1129 unsigned RDist = closestSucc(right);
1130 if (LDist != RDist)
1131 return LDist < RDist;
1132
1133 // Intuitively, it's good to push down instructions whose results are
1134 // liveout so their long live ranges won't conflict with other values
1135 // which are needed inside the BB. Further prioritize liveout instructions
1136 // by the number of operands which are calculated within the BB.
1137 unsigned LScratch = calcMaxScratches(left);
1138 unsigned RScratch = calcMaxScratches(right);
1139 if (LScratch != RScratch)
1140 return LScratch > RScratch;
1141
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001142 if (left->getHeight() != right->getHeight())
1143 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001144
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001145 if (left->getDepth() != right->getDepth())
1146 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001147
Roman Levenstein6b371142008-04-29 09:07:59 +00001148 assert(left->NodeQueueId && right->NodeQueueId &&
1149 "NodeQueueId cannot be zero");
1150 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001151}
1152
Dan Gohman3f656df2008-11-20 02:45:51 +00001153template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001154bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001155RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001156 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001157 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001158 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001159 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001160 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001161 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001162 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001163 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001164 if (DU->getNodeId() != -1 &&
1165 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001166 return true;
1167 }
1168 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001169 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001170 return false;
1171}
1172
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001173
Evan Chenga5e595d2007-09-28 22:32:30 +00001174/// hasCopyToRegUse - Return true if SU has a value successor that is a
1175/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001176static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001177 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1178 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001179 if (I->isCtrl()) continue;
1180 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001181 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001182 return true;
1183 }
1184 return false;
1185}
1186
Evan Chengf9891412007-12-20 09:25:31 +00001187/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001188/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001189static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001190 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001191 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001192 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001193 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1194 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001195 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001196 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001197 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001198 if (!SUImpDefs)
1199 return false;
1200 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001201 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001202 if (VT == MVT::Flag || VT == MVT::Other)
1203 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001204 if (!N->hasAnyUseOfValue(i))
1205 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001206 unsigned Reg = ImpDefs[i - NumDefs];
1207 for (;*SUImpDefs; ++SUImpDefs) {
1208 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001209 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001210 return true;
1211 }
1212 }
1213 return false;
1214}
1215
Evan Chengd38c22b2006-05-11 23:55:42 +00001216/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1217/// it as a def&use operand. Add a pseudo control edge from it to the other
1218/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001219/// first (lower in the schedule). If both nodes are two-address, favor the
1220/// one that has a CopyToReg use (more likely to be a loop induction update).
1221/// If both are two-address, but one is commutable while the other is not
1222/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001223template<class SF>
1224void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001225 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001226 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001227 if (!SU->isTwoAddress)
1228 continue;
1229
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001230 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001231 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001232 continue;
1233
Dan Gohman17059682008-07-17 19:10:17 +00001234 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001235 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001236 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001237 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001238 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001239 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1240 continue;
1241 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1242 if (DU->getNodeId() == -1)
1243 continue;
1244 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1245 if (!DUSU) continue;
1246 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1247 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001248 if (I->isCtrl()) continue;
1249 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001250 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001251 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001252 // Be conservative. Ignore if nodes aren't at roughly the same
1253 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001254 if (SuccSU->getHeight() < SU->getHeight() &&
1255 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001256 continue;
1257 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1258 continue;
1259 // Don't constrain nodes with physical register defs if the
1260 // predecessor can clobber them.
1261 if (SuccSU->hasPhysRegDefs) {
1262 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001263 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001264 }
1265 // Don't constraint extract_subreg / insert_subreg these may be
1266 // coalesced away. We don't them close to their uses.
1267 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1268 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1269 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1270 continue;
1271 if ((!canClobber(SuccSU, DUSU) ||
1272 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1273 (!SU->isCommutable && SuccSU->isCommutable)) &&
1274 !scheduleDAG->IsReachable(SuccSU, SU)) {
Dan Gohman30cad9c2008-12-04 02:14:57 +00001275 DOUT << "Adding a pseudo-two-addr edge from SU # " << SU->NodeNum
Dan Gohman82016c22008-11-19 02:00:32 +00001276 << " to SU #" << SuccSU->NodeNum << "\n";
Dan Gohman79c35162009-01-06 01:19:04 +00001277 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001278 /*Reg=*/0, /*isNormalMemory=*/false,
1279 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001280 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001281 }
1282 }
1283 }
1284 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001285}
1286
Evan Cheng6730f032007-01-08 23:55:53 +00001287/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1288/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001289template<class SF>
1290void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001291 SethiUllmanNumbers.assign(SUnits->size(), 0);
1292
1293 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001294 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001295}
Evan Chengd38c22b2006-05-11 23:55:42 +00001296
Roman Levenstein30d09512008-03-27 09:44:37 +00001297/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001298/// predecessors of the successors of the SUnit SU. Stop when the provided
1299/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001300static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1301 unsigned Limit) {
1302 unsigned Sum = 0;
1303 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1304 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001305 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001306 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1307 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001308 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001309 if (!PredSU->isScheduled)
1310 if (++Sum > Limit)
1311 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001312 }
1313 }
1314 return Sum;
1315}
1316
Evan Chengd38c22b2006-05-11 23:55:42 +00001317
1318// Top down
1319bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001320 unsigned LPriority = SPQ->getNodePriority(left);
1321 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001322 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1323 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001324 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1325 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001326 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1327 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001328
1329 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1330 return false;
1331 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1332 return true;
1333
Evan Chengd38c22b2006-05-11 23:55:42 +00001334 if (LIsFloater)
1335 LBonus -= 2;
1336 if (RIsFloater)
1337 RBonus -= 2;
1338 if (left->NumSuccs == 1)
1339 LBonus += 2;
1340 if (right->NumSuccs == 1)
1341 RBonus += 2;
1342
Evan Cheng73bdf042008-03-01 00:39:47 +00001343 if (LPriority+LBonus != RPriority+RBonus)
1344 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001345
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001346 if (left->getDepth() != right->getDepth())
1347 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001348
1349 if (left->NumSuccsLeft != right->NumSuccsLeft)
1350 return left->NumSuccsLeft > right->NumSuccsLeft;
1351
Roman Levenstein6b371142008-04-29 09:07:59 +00001352 assert(left->NodeQueueId && right->NodeQueueId &&
1353 "NodeQueueId cannot be zero");
1354 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001355}
1356
Evan Chengd38c22b2006-05-11 23:55:42 +00001357//===----------------------------------------------------------------------===//
1358// Public Constructor Functions
1359//===----------------------------------------------------------------------===//
1360
Jim Laskey03593f72006-08-01 18:29:48 +00001361llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001362 bool) {
Dan Gohman619ef482009-01-15 19:20:50 +00001363 const TargetMachine &TM = IS->TM;
1364 const TargetInstrInfo *TII = TM.getInstrInfo();
1365 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001366
Evan Cheng7e4abde2008-07-02 09:23:51 +00001367 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001368
Evan Cheng7e4abde2008-07-02 09:23:51 +00001369 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001370 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001371 PQ->setScheduleDAG(SD);
1372 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001373}
1374
Jim Laskey03593f72006-08-01 18:29:48 +00001375llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001376 bool) {
Dan Gohman619ef482009-01-15 19:20:50 +00001377 const TargetMachine &TM = IS->TM;
1378 const TargetInstrInfo *TII = TM.getInstrInfo();
1379 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001380
1381 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1382
Dan Gohman619ef482009-01-15 19:20:50 +00001383 ScheduleDAGRRList *SD =
1384 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001385 PQ->setScheduleDAG(SD);
1386 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001387}