blob: 30dc4768aa03cf4d72e0f75730acbfda0fd94858 [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 Gohmaned0e8d42009-03-23 20:20:43 +0000413 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000414 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000415 SmallVector<SDep, 4> ChainSuccs;
416 SmallVector<SDep, 4> LoadPreds;
417 SmallVector<SDep, 4> NodePreds;
418 SmallVector<SDep, 4> NodeSuccs;
419 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
420 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000421 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000422 ChainPreds.push_back(*I);
Dan Gohman2d170892008-12-09 22:54:47 +0000423 else if (I->getSUnit()->getNode() &&
424 I->getSUnit()->getNode()->isOperandOf(LoadNode))
425 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000426 else
Dan Gohman2d170892008-12-09 22:54:47 +0000427 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000428 }
429 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
430 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000431 if (I->isCtrl())
432 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000433 else
Dan Gohman2d170892008-12-09 22:54:47 +0000434 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000435 }
436
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000437 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000438 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
439 const SDep &Pred = ChainPreds[i];
440 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000441 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000442 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000443 }
Evan Cheng79e97132007-10-05 01:39:18 +0000444 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000445 const SDep &Pred = LoadPreds[i];
446 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000447 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000448 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000449 }
450 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000451 const SDep &Pred = NodePreds[i];
452 RemovePred(SU, Pred);
453 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000454 }
455 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000456 SDep D = NodeSuccs[i];
457 SUnit *SuccDep = D.getSUnit();
458 D.setSUnit(SU);
459 RemovePred(SuccDep, D);
460 D.setSUnit(NewSU);
461 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000462 }
463 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000464 SDep D = ChainSuccs[i];
465 SUnit *SuccDep = D.getSUnit();
466 D.setSUnit(SU);
467 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000468 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000469 D.setSUnit(LoadSU);
470 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000471 }
Evan Cheng79e97132007-10-05 01:39:18 +0000472 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000473
474 // Add a data dependency to reflect that NewSU reads the value defined
475 // by LoadSU.
476 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000477
Evan Cheng91e0fc92007-12-18 08:42:10 +0000478 if (isNewLoad)
479 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000480 AvailableQueue->addNode(NewSU);
481
482 ++NumUnfolds;
483
484 if (NewSU->NumSuccsLeft == 0) {
485 NewSU->isAvailable = true;
486 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000487 }
488 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000489 }
490
491 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000492 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000493
494 // New SUnit has the exact same predecessors.
495 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
496 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000497 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000498 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000499
500 // Only copy scheduled successors. Cut them from old node's successor
501 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000502 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000503 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
504 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000505 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000506 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000507 SUnit *SuccSU = I->getSUnit();
508 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000509 SDep D = *I;
510 D.setSUnit(NewSU);
511 AddPred(SuccSU, D);
512 D.setSUnit(SU);
513 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000514 }
515 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000516 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000517 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000518
519 AvailableQueue->updateNode(SU);
520 AvailableQueue->addNode(NewSU);
521
Evan Cheng1ec79b42007-09-27 07:09:03 +0000522 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000523 return NewSU;
524}
525
Evan Chengb2c42c62009-01-12 03:19:55 +0000526/// InsertCopiesAndMoveSuccs - Insert register copies and move all
527/// scheduled successors of the given SUnit to the last copy.
528void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
529 const TargetRegisterClass *DestRC,
530 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000531 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000532 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000533 CopyFromSU->CopySrcRC = SrcRC;
534 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000535
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000536 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000537 CopyToSU->CopySrcRC = DestRC;
538 CopyToSU->CopyDstRC = SrcRC;
539
540 // Only copy scheduled successors. Cut them from old node's successor
541 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000542 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000543 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
544 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000545 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000546 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000547 SUnit *SuccSU = I->getSUnit();
548 if (SuccSU->isScheduled) {
549 SDep D = *I;
550 D.setSUnit(CopyToSU);
551 AddPred(SuccSU, D);
552 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000553 }
554 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000555 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000556 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000557
Dan Gohman2d170892008-12-09 22:54:47 +0000558 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
559 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000560
561 AvailableQueue->updateNode(SU);
562 AvailableQueue->addNode(CopyFromSU);
563 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000564 Copies.push_back(CopyFromSU);
565 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000566
Evan Chengb2c42c62009-01-12 03:19:55 +0000567 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000568}
569
570/// getPhysicalRegisterVT - Returns the ValueType of the physical register
571/// definition of the specified node.
572/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000573static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
574 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000575 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000576 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000577 unsigned NumRes = TID.getNumDefs();
578 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000579 if (Reg == *ImpDef)
580 break;
581 ++NumRes;
582 }
583 return N->getValueType(NumRes);
584}
585
Evan Chengb8905c42009-03-04 01:41:49 +0000586/// CheckForLiveRegDef - Return true and update live register vector if the
587/// specified register def of the specified SUnit clobbers any "live" registers.
588static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
589 std::vector<SUnit*> &LiveRegDefs,
590 SmallSet<unsigned, 4> &RegAdded,
591 SmallVector<unsigned, 4> &LRegs,
592 const TargetRegisterInfo *TRI) {
593 bool Added = false;
594 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
595 if (RegAdded.insert(Reg)) {
596 LRegs.push_back(Reg);
597 Added = true;
598 }
599 }
600 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
601 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
602 if (RegAdded.insert(*Alias)) {
603 LRegs.push_back(*Alias);
604 Added = true;
605 }
606 }
607 return Added;
608}
609
Evan Cheng5924bf72007-09-25 01:54:36 +0000610/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
611/// scheduling of the given node to satisfy live physical register dependencies.
612/// If the specific node is the last one that's available to schedule, do
613/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000614bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
615 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000616 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000617 return false;
618
Evan Chenge6f92252007-09-27 18:46:06 +0000619 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000620 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000621 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
622 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000623 if (I->isAssignedRegDep())
624 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
625 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000626 }
627
Dan Gohman072734e2008-11-13 23:24:17 +0000628 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000629 if (Node->getOpcode() == ISD::INLINEASM) {
630 // Inline asm can clobber physical defs.
631 unsigned NumOps = Node->getNumOperands();
632 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
633 --NumOps; // Ignore the flag operand.
634
635 for (unsigned i = 2; i != NumOps;) {
636 unsigned Flags =
637 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng2e559232009-03-20 18:03:34 +0000638 unsigned NumVals = (Flags & 0xffff) >> 3;
Evan Chengb8905c42009-03-04 01:41:49 +0000639
640 ++i; // Skip the ID value.
641 if ((Flags & 7) == 2 || (Flags & 7) == 6) {
642 // Check for def of register or earlyclobber register.
643 for (; NumVals; --NumVals, ++i) {
644 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
645 if (TargetRegisterInfo::isPhysicalRegister(Reg))
646 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
647 }
648 } else
649 i += NumVals;
650 }
651 continue;
652 }
653
Dan Gohman072734e2008-11-13 23:24:17 +0000654 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000655 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000656 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000657 if (!TID.ImplicitDefs)
658 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000659 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
660 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000661 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000662 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000663}
664
Evan Cheng1ec79b42007-09-27 07:09:03 +0000665
Evan Chengd38c22b2006-05-11 23:55:42 +0000666/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
667/// schedulers.
668void ScheduleDAGRRList::ListScheduleBottomUp() {
669 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000670
671 // Release any predecessors of the special Exit node.
672 ReleasePredecessors(&ExitSU, CurCycle);
673
Evan Chengd38c22b2006-05-11 23:55:42 +0000674 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000675 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000676 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000677 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
678 RootSU->isAvailable = true;
679 AvailableQueue->push(RootSU);
680 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000681
682 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000683 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000684 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000685 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000686 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000687 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000688 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000689 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000690 SUnit *CurSU = AvailableQueue->pop();
691 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000692 SmallVector<unsigned, 4> LRegs;
693 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
694 break;
695 Delayed = true;
696 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000697
698 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
699 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000700 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000701 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000702
703 // All candidates are delayed due to live physical reg dependencies.
704 // Try backtracking, code duplication, or inserting cross class copies
705 // to resolve it.
706 if (Delayed && !CurSU) {
707 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
708 SUnit *TrySU = NotReady[i];
709 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
710
711 // Try unscheduling up to the point where it's safe to schedule
712 // this node.
713 unsigned LiveCycle = CurCycle;
714 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
715 unsigned Reg = LRegs[j];
716 unsigned LCycle = LiveRegCycles[Reg];
717 LiveCycle = std::min(LiveCycle, LCycle);
718 }
719 SUnit *OldSU = Sequence[LiveCycle];
720 if (!WillCreateCycle(TrySU, OldSU)) {
721 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
722 // Force the current node to be scheduled before the node that
723 // requires the physical reg dep.
724 if (OldSU->isAvailable) {
725 OldSU->isAvailable = false;
726 AvailableQueue->remove(OldSU);
727 }
Dan Gohman2d170892008-12-09 22:54:47 +0000728 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
729 /*Reg=*/0, /*isNormalMemory=*/false,
730 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000731 // If one or more successors has been unscheduled, then the current
732 // node is no longer avaialable. Schedule a successor that's now
733 // available instead.
734 if (!TrySU->isAvailable)
735 CurSU = AvailableQueue->pop();
736 else {
737 CurSU = TrySU;
738 TrySU->isPending = false;
739 NotReady.erase(NotReady.begin()+i);
740 }
741 break;
742 }
743 }
744
745 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000746 // Can't backtrack. If it's too expensive to copy the value, then try
747 // duplicate the nodes that produces these "too expensive to copy"
748 // values to break the dependency. In case even that doesn't work,
749 // insert cross class copies.
750 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000751 SUnit *TrySU = NotReady[0];
752 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
753 assert(LRegs.size() == 1 && "Can't handle this yet!");
754 unsigned Reg = LRegs[0];
755 SUnit *LRDef = LiveRegDefs[Reg];
Evan Chengb2c42c62009-01-12 03:19:55 +0000756 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
757 const TargetRegisterClass *RC =
758 TRI->getPhysicalRegisterRegClass(Reg, VT);
759 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
760
761 // If cross copy register class is null, then it must be possible copy
762 // the value directly. Do not try duplicate the def.
763 SUnit *NewDef = 0;
764 if (DestRC)
765 NewDef = CopyAndMoveSuccessors(LRDef);
766 else
767 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000768 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000769 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000770 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000771 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Cheng0c4fe262009-01-09 20:42:34 +0000772 DOUT << "Adding an edge from SU #" << TrySU->NodeNum
Evan Cheng1ec79b42007-09-27 07:09:03 +0000773 << " to SU #" << Copies.front()->NodeNum << "\n";
Dan Gohman2d170892008-12-09 22:54:47 +0000774 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000775 /*Reg=*/0, /*isNormalMemory=*/false,
776 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000777 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000778 NewDef = Copies.back();
779 }
780
Evan Cheng0c4fe262009-01-09 20:42:34 +0000781 DOUT << "Adding an edge from SU #" << NewDef->NodeNum
Evan Cheng1ec79b42007-09-27 07:09:03 +0000782 << " to SU #" << TrySU->NodeNum << "\n";
783 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000784 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000785 /*Reg=*/0, /*isNormalMemory=*/false,
786 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000787 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000788 TrySU->isAvailable = false;
789 CurSU = NewDef;
790 }
791
Dan Gohman60d68442009-01-29 19:49:27 +0000792 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000793 }
794
Evan Chengd38c22b2006-05-11 23:55:42 +0000795 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000796 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
797 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000798 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000799 if (NotReady[i]->isAvailable)
800 AvailableQueue->push(NotReady[i]);
801 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000802 NotReady.clear();
803
Dan Gohmanc602dd42008-11-21 00:10:42 +0000804 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000805 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000806 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000807 }
808
Evan Chengd38c22b2006-05-11 23:55:42 +0000809 // Reverse the order if it is bottom up.
810 std::reverse(Sequence.begin(), Sequence.end());
811
Evan Chengd38c22b2006-05-11 23:55:42 +0000812#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000813 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000814#endif
815}
816
817//===----------------------------------------------------------------------===//
818// Top-Down Scheduling
819//===----------------------------------------------------------------------===//
820
821/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000822/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000823void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000824 SUnit *SuccSU = SuccEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000825 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000826
827#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000828 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000829 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000830 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000831 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000832 assert(0);
833 }
834#endif
835
Dan Gohmanb9543432009-02-10 23:27:53 +0000836 // If all the node's predecessors are scheduled, this node is ready
837 // to be scheduled. Ignore the special ExitSU node.
838 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000839 SuccSU->isAvailable = true;
840 AvailableQueue->push(SuccSU);
841 }
842}
843
Dan Gohmanb9543432009-02-10 23:27:53 +0000844void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
845 // Top down: release successors
846 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
847 I != E; ++I) {
848 assert(!I->isAssignedRegDep() &&
849 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
850
851 ReleaseSucc(SU, &*I);
852 }
853}
854
Evan Chengd38c22b2006-05-11 23:55:42 +0000855/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
856/// count of its successors. If a successor pending count is zero, add it to
857/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000858void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000859 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000860 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000861
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000862 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
863 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000864 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000865
Dan Gohmanb9543432009-02-10 23:27:53 +0000866 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000867 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000868 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000869}
870
Dan Gohman54a187e2007-08-20 19:28:38 +0000871/// ListScheduleTopDown - The main loop of list scheduling for top-down
872/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000873void ScheduleDAGRRList::ListScheduleTopDown() {
874 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000875
Dan Gohmanb9543432009-02-10 23:27:53 +0000876 // Release any successors of the special Entry node.
877 ReleaseSuccessors(&EntrySU);
878
Evan Chengd38c22b2006-05-11 23:55:42 +0000879 // All leaves to Available queue.
880 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
881 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000882 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000883 AvailableQueue->push(&SUnits[i]);
884 SUnits[i].isAvailable = true;
885 }
886 }
887
Evan Chengd38c22b2006-05-11 23:55:42 +0000888 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000889 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000890 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000891 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000892 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000893
Dan Gohmanc602dd42008-11-21 00:10:42 +0000894 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000895 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000896 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000897 }
898
Evan Chengd38c22b2006-05-11 23:55:42 +0000899#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000900 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000901#endif
902}
903
904
Evan Chengd38c22b2006-05-11 23:55:42 +0000905//===----------------------------------------------------------------------===//
906// RegReductionPriorityQueue Implementation
907//===----------------------------------------------------------------------===//
908//
909// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
910// to reduce register pressure.
911//
912namespace {
913 template<class SF>
914 class RegReductionPriorityQueue;
915
916 /// Sorting functions for the Available queue.
917 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
918 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
919 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
920 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
921
922 bool operator()(const SUnit* left, const SUnit* right) const;
923 };
924
925 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
926 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
927 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
928 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
929
930 bool operator()(const SUnit* left, const SUnit* right) const;
931 };
932} // end anonymous namespace
933
Dan Gohman186f65d2008-11-20 03:30:37 +0000934/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
935/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000936static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000937CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000938 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
939 if (SethiUllmanNumber != 0)
940 return SethiUllmanNumber;
941
942 unsigned Extra = 0;
943 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
944 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000945 if (I->isCtrl()) continue; // ignore chain preds
946 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000947 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000948 if (PredSethiUllman > SethiUllmanNumber) {
949 SethiUllmanNumber = PredSethiUllman;
950 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +0000951 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +0000952 ++Extra;
953 }
954
955 SethiUllmanNumber += Extra;
956
957 if (SethiUllmanNumber == 0)
958 SethiUllmanNumber = 1;
959
960 return SethiUllmanNumber;
961}
962
Evan Chengd38c22b2006-05-11 23:55:42 +0000963namespace {
964 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000965 class VISIBILITY_HIDDEN RegReductionPriorityQueue
966 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000967 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000968 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000969
Dan Gohman3f656df2008-11-20 02:45:51 +0000970 protected:
971 // SUnits - The SUnits for the current graph.
972 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000973
Dan Gohman3f656df2008-11-20 02:45:51 +0000974 const TargetInstrInfo *TII;
975 const TargetRegisterInfo *TRI;
976 ScheduleDAGRRList *scheduleDAG;
977
Dan Gohman186f65d2008-11-20 03:30:37 +0000978 // SethiUllmanNumbers - The SethiUllman number for each node.
979 std::vector<unsigned> SethiUllmanNumbers;
980
Dan Gohman3f656df2008-11-20 02:45:51 +0000981 public:
982 RegReductionPriorityQueue(const TargetInstrInfo *tii,
983 const TargetRegisterInfo *tri) :
984 Queue(SF(this)), currentQueueId(0),
985 TII(tii), TRI(tri), scheduleDAG(NULL) {}
986
987 void initNodes(std::vector<SUnit> &sunits) {
988 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000989 // Add pseudo dependency edges for two-address nodes.
990 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +0000991 // Reroute edges to nodes with multiple uses.
992 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +0000993 // Calculate node priorities.
994 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +0000995 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000996
Dan Gohman186f65d2008-11-20 03:30:37 +0000997 void addNode(const SUnit *SU) {
998 unsigned SUSize = SethiUllmanNumbers.size();
999 if (SUnits->size() > SUSize)
1000 SethiUllmanNumbers.resize(SUSize*2, 0);
1001 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1002 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001003
Dan Gohman186f65d2008-11-20 03:30:37 +00001004 void updateNode(const SUnit *SU) {
1005 SethiUllmanNumbers[SU->NodeNum] = 0;
1006 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1007 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001008
Dan Gohman186f65d2008-11-20 03:30:37 +00001009 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001010 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001011 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001012 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001013
1014 unsigned getNodePriority(const SUnit *SU) const {
1015 assert(SU->NodeNum < SethiUllmanNumbers.size());
1016 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001017 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001018 // CopyToReg should be close to its uses to facilitate coalescing and
1019 // avoid spilling.
1020 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001021 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1022 Opc == TargetInstrInfo::INSERT_SUBREG)
Dan Gohman186f65d2008-11-20 03:30:37 +00001023 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1024 // facilitate coalescing.
1025 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001026 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1027 // If SU does not have a register use, i.e. it doesn't produce a value
1028 // that would be consumed (e.g. store), then it terminates a chain of
1029 // computation. Give it a large SethiUllman number so it will be
1030 // scheduled right before its predecessors that it doesn't lengthen
1031 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001032 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001033 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1034 // If SU does not have a register def, schedule it close to its uses
1035 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001036 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001037 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001038 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001039
Evan Cheng5924bf72007-09-25 01:54:36 +00001040 unsigned size() const { return Queue.size(); }
1041
Evan Chengd38c22b2006-05-11 23:55:42 +00001042 bool empty() const { return Queue.empty(); }
1043
1044 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001045 assert(!U->NodeQueueId && "Node in the queue already");
1046 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001047 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001048 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001049
Evan Chengd38c22b2006-05-11 23:55:42 +00001050 void push_all(const std::vector<SUnit *> &Nodes) {
1051 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001052 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001053 }
1054
1055 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001056 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001057 SUnit *V = Queue.top();
1058 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001059 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001060 return V;
1061 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001062
Evan Cheng5924bf72007-09-25 01:54:36 +00001063 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001064 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001065 assert(SU->NodeQueueId != 0 && "Not in queue!");
1066 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001067 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001068 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001069
1070 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1071 scheduleDAG = scheduleDag;
1072 }
1073
1074 protected:
1075 bool canClobber(const SUnit *SU, const SUnit *Op);
1076 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001077 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001078 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001079 };
1080
Dan Gohman186f65d2008-11-20 03:30:37 +00001081 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1082 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001083
Dan Gohman186f65d2008-11-20 03:30:37 +00001084 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1085 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001086}
1087
Evan Chengb9e3db62007-03-14 22:43:40 +00001088/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001089/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001090static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001091 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001092 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001093 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001094 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001095 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001096 // If there are bunch of CopyToRegs stacked up, they should be considered
1097 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001098 if (I->getSUnit()->getNode() &&
1099 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001100 Height = closestSucc(I->getSUnit())+1;
1101 if (Height > MaxHeight)
1102 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001103 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001104 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001105}
1106
Evan Cheng61bc51e2007-12-20 02:22:36 +00001107/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001108/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001109static unsigned calcMaxScratches(const SUnit *SU) {
1110 unsigned Scratches = 0;
1111 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001112 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001113 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001114 Scratches++;
1115 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001116 return Scratches;
1117}
1118
Evan Chengd38c22b2006-05-11 23:55:42 +00001119// Bottom up
1120bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001121 unsigned LPriority = SPQ->getNodePriority(left);
1122 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001123 if (LPriority != RPriority)
1124 return LPriority > RPriority;
1125
1126 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1127 // e.g.
1128 // t1 = op t2, c1
1129 // t3 = op t4, c2
1130 //
1131 // and the following instructions are both ready.
1132 // t2 = op c3
1133 // t4 = op c4
1134 //
1135 // Then schedule t2 = op first.
1136 // i.e.
1137 // t4 = op c4
1138 // t2 = op c3
1139 // t1 = op t2, c1
1140 // t3 = op t4, c2
1141 //
1142 // This creates more short live intervals.
1143 unsigned LDist = closestSucc(left);
1144 unsigned RDist = closestSucc(right);
1145 if (LDist != RDist)
1146 return LDist < RDist;
1147
Evan Cheng3a14efa2009-02-12 08:59:45 +00001148 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001149 unsigned LScratch = calcMaxScratches(left);
1150 unsigned RScratch = calcMaxScratches(right);
1151 if (LScratch != RScratch)
1152 return LScratch > RScratch;
1153
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001154 if (left->getHeight() != right->getHeight())
1155 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001156
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001157 if (left->getDepth() != right->getDepth())
1158 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001159
Roman Levenstein6b371142008-04-29 09:07:59 +00001160 assert(left->NodeQueueId && right->NodeQueueId &&
1161 "NodeQueueId cannot be zero");
1162 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001163}
1164
Dan Gohman3f656df2008-11-20 02:45:51 +00001165template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001166bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001167RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001168 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001169 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001170 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001171 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001172 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001173 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001174 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001175 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001176 if (DU->getNodeId() != -1 &&
1177 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001178 return true;
1179 }
1180 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001181 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001182 return false;
1183}
1184
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001185
Evan Chenga5e595d2007-09-28 22:32:30 +00001186/// hasCopyToRegUse - Return true if SU has a value successor that is a
1187/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001188static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001189 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1190 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001191 if (I->isCtrl()) continue;
1192 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001193 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001194 return true;
1195 }
1196 return false;
1197}
1198
Evan Chengf9891412007-12-20 09:25:31 +00001199/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001200/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001201static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001202 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001203 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001204 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001205 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1206 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001207 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001208 for (const SDNode *SUNode = SU->getNode(); SUNode;
1209 SUNode = SUNode->getFlaggedNode()) {
1210 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001211 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001212 const unsigned *SUImpDefs =
1213 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1214 if (!SUImpDefs)
1215 return false;
1216 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1217 MVT VT = N->getValueType(i);
1218 if (VT == MVT::Flag || VT == MVT::Other)
1219 continue;
1220 if (!N->hasAnyUseOfValue(i))
1221 continue;
1222 unsigned Reg = ImpDefs[i - NumDefs];
1223 for (;*SUImpDefs; ++SUImpDefs) {
1224 unsigned SUReg = *SUImpDefs;
1225 if (TRI->regsOverlap(Reg, SUReg))
1226 return true;
1227 }
Evan Chengf9891412007-12-20 09:25:31 +00001228 }
1229 }
1230 return false;
1231}
1232
Dan Gohman9a658d72009-03-24 00:49:12 +00001233/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1234/// are not handled well by the general register pressure reduction
1235/// heuristics. When presented with code like this:
1236///
1237/// N
1238/// / |
1239/// / |
1240/// U store
1241/// |
1242/// ...
1243///
1244/// the heuristics tend to push the store up, but since the
1245/// operand of the store has another use (U), this would increase
1246/// the length of that other use (the U->N edge).
1247///
1248/// This function transforms code like the above to route U's
1249/// dependence through the store when possible, like this:
1250///
1251/// N
1252/// ||
1253/// ||
1254/// store
1255/// |
1256/// U
1257/// |
1258/// ...
1259///
1260/// This results in the store being scheduled immediately
1261/// after N, which shortens the U->N live range, reducing
1262/// register pressure.
1263///
1264template<class SF>
1265void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1266 // Visit all the nodes in topological order, working top-down.
1267 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1268 SUnit *SU = &(*SUnits)[i];
1269 // For now, only look at nodes with no data successors, such as stores.
1270 // These are especially important, due to the heuristics in
1271 // getNodePriority for nodes with no data successors.
1272 if (SU->NumSuccs != 0)
1273 continue;
1274 // For now, only look at nodes with exactly one data predecessor.
1275 if (SU->NumPreds != 1)
1276 continue;
1277 // Avoid prescheduling copies to virtual registers, which don't behave
1278 // like other nodes from the perspective of scheduling heuristics.
1279 if (SDNode *N = SU->getNode())
1280 if (N->getOpcode() == ISD::CopyToReg &&
1281 TargetRegisterInfo::isVirtualRegister
1282 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1283 continue;
1284
1285 // Locate the single data predecessor.
1286 SUnit *PredSU = 0;
1287 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1288 EE = SU->Preds.end(); II != EE; ++II)
1289 if (!II->isCtrl()) {
1290 PredSU = II->getSUnit();
1291 break;
1292 }
1293 assert(PredSU);
1294
1295 // Don't rewrite edges that carry physregs, because that requires additional
1296 // support infrastructure.
1297 if (PredSU->hasPhysRegDefs)
1298 continue;
1299 // Short-circuit the case where SU is PredSU's only data successor.
1300 if (PredSU->NumSuccs == 1)
1301 continue;
1302 // Avoid prescheduling to copies from virtual registers, which don't behave
1303 // like other nodes from the perspective of scheduling // heuristics.
1304 if (SDNode *N = SU->getNode())
1305 if (N->getOpcode() == ISD::CopyFromReg &&
1306 TargetRegisterInfo::isVirtualRegister
1307 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1308 continue;
1309
1310 // Perform checks on the successors of PredSU.
1311 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1312 EE = PredSU->Succs.end(); II != EE; ++II) {
1313 SUnit *PredSuccSU = II->getSUnit();
1314 if (PredSuccSU == SU) continue;
1315 // If PredSU has another successor with no data successors, for
1316 // now don't attempt to choose either over the other.
1317 if (PredSuccSU->NumSuccs == 0)
1318 goto outer_loop_continue;
1319 // Don't break physical register dependencies.
1320 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1321 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1322 goto outer_loop_continue;
1323 // Don't introduce graph cycles.
1324 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1325 goto outer_loop_continue;
1326 }
1327
1328 // Ok, the transformation is safe and the heuristics suggest it is
1329 // profitable. Update the graph.
1330 DOUT << "Prescheduling SU # " << SU->NodeNum
1331 << " next to PredSU # " << PredSU->NodeNum
1332 << " to guide scheduling in the presence of multiple uses\n";
1333 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1334 SDep Edge = PredSU->Succs[i];
1335 assert(!Edge.isAssignedRegDep());
1336 SUnit *SuccSU = Edge.getSUnit();
1337 if (SuccSU != SU) {
1338 Edge.setSUnit(PredSU);
1339 scheduleDAG->RemovePred(SuccSU, Edge);
1340 scheduleDAG->AddPred(SU, Edge);
1341 Edge.setSUnit(SU);
1342 scheduleDAG->AddPred(SuccSU, Edge);
1343 --i;
1344 }
1345 }
1346 outer_loop_continue:;
1347 }
1348}
1349
Evan Chengd38c22b2006-05-11 23:55:42 +00001350/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1351/// it as a def&use operand. Add a pseudo control edge from it to the other
1352/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001353/// first (lower in the schedule). If both nodes are two-address, favor the
1354/// one that has a CopyToReg use (more likely to be a loop induction update).
1355/// If both are two-address, but one is commutable while the other is not
1356/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001357template<class SF>
1358void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001359 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001360 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001361 if (!SU->isTwoAddress)
1362 continue;
1363
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001364 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001365 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001366 continue;
1367
Dan Gohman17059682008-07-17 19:10:17 +00001368 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001369 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001370 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001371 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001372 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001373 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1374 continue;
1375 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1376 if (DU->getNodeId() == -1)
1377 continue;
1378 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1379 if (!DUSU) continue;
1380 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1381 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001382 if (I->isCtrl()) continue;
1383 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001384 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001385 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001386 // Be conservative. Ignore if nodes aren't at roughly the same
1387 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001388 if (SuccSU->getHeight() < SU->getHeight() &&
1389 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001390 continue;
1391 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1392 continue;
1393 // Don't constrain nodes with physical register defs if the
1394 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001395 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001396 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001397 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001398 }
Dan Gohman45889d22009-02-11 21:32:08 +00001399 // Don't constrain extract_subreg / insert_subreg; these may be
1400 // coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001401 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1402 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1403 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1404 continue;
1405 if ((!canClobber(SuccSU, DUSU) ||
1406 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1407 (!SU->isCommutable && SuccSU->isCommutable)) &&
1408 !scheduleDAG->IsReachable(SuccSU, SU)) {
Dan Gohman30cad9c2008-12-04 02:14:57 +00001409 DOUT << "Adding a pseudo-two-addr edge from SU # " << SU->NodeNum
Dan Gohman82016c22008-11-19 02:00:32 +00001410 << " to SU #" << SuccSU->NodeNum << "\n";
Dan Gohman79c35162009-01-06 01:19:04 +00001411 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001412 /*Reg=*/0, /*isNormalMemory=*/false,
1413 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001414 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001415 }
1416 }
1417 }
1418 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001419}
1420
Evan Cheng6730f032007-01-08 23:55:53 +00001421/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1422/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001423template<class SF>
1424void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001425 SethiUllmanNumbers.assign(SUnits->size(), 0);
1426
1427 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001428 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001429}
Evan Chengd38c22b2006-05-11 23:55:42 +00001430
Roman Levenstein30d09512008-03-27 09:44:37 +00001431/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001432/// predecessors of the successors of the SUnit SU. Stop when the provided
1433/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001434static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1435 unsigned Limit) {
1436 unsigned Sum = 0;
1437 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1438 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001439 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001440 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1441 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001442 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001443 if (!PredSU->isScheduled)
1444 if (++Sum > Limit)
1445 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001446 }
1447 }
1448 return Sum;
1449}
1450
Evan Chengd38c22b2006-05-11 23:55:42 +00001451
1452// Top down
1453bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001454 unsigned LPriority = SPQ->getNodePriority(left);
1455 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001456 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1457 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001458 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1459 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001460 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1461 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001462
1463 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1464 return false;
1465 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1466 return true;
1467
Evan Chengd38c22b2006-05-11 23:55:42 +00001468 if (LIsFloater)
1469 LBonus -= 2;
1470 if (RIsFloater)
1471 RBonus -= 2;
1472 if (left->NumSuccs == 1)
1473 LBonus += 2;
1474 if (right->NumSuccs == 1)
1475 RBonus += 2;
1476
Evan Cheng73bdf042008-03-01 00:39:47 +00001477 if (LPriority+LBonus != RPriority+RBonus)
1478 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001479
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001480 if (left->getDepth() != right->getDepth())
1481 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001482
1483 if (left->NumSuccsLeft != right->NumSuccsLeft)
1484 return left->NumSuccsLeft > right->NumSuccsLeft;
1485
Roman Levenstein6b371142008-04-29 09:07:59 +00001486 assert(left->NodeQueueId && right->NodeQueueId &&
1487 "NodeQueueId cannot be zero");
1488 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001489}
1490
Evan Chengd38c22b2006-05-11 23:55:42 +00001491//===----------------------------------------------------------------------===//
1492// Public Constructor Functions
1493//===----------------------------------------------------------------------===//
1494
Dan Gohmandfaf6462009-02-11 04:27:20 +00001495llvm::ScheduleDAGSDNodes *
1496llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, bool) {
Dan Gohman619ef482009-01-15 19:20:50 +00001497 const TargetMachine &TM = IS->TM;
1498 const TargetInstrInfo *TII = TM.getInstrInfo();
1499 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001500
Evan Cheng7e4abde2008-07-02 09:23:51 +00001501 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001502
Evan Cheng7e4abde2008-07-02 09:23:51 +00001503 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001504 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001505 PQ->setScheduleDAG(SD);
1506 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001507}
1508
Dan Gohmandfaf6462009-02-11 04:27:20 +00001509llvm::ScheduleDAGSDNodes *
1510llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, bool) {
Dan Gohman619ef482009-01-15 19:20:50 +00001511 const TargetMachine &TM = IS->TM;
1512 const TargetInstrInfo *TII = TM.getInstrInfo();
1513 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001514
1515 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1516
Dan Gohman619ef482009-01-15 19:20:50 +00001517 ScheduleDAGRRList *SD =
1518 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001519 PQ->setScheduleDAG(SD);
1520 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001521}