blob: cd91b840d033695328414e935c69713c77c260e9 [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"
Torok Edwin56d06592009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Dan Gohmana4db3352008-06-21 18:35:25 +000029#include "llvm/ADT/PriorityQueue.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000030#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000031#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000032#include "llvm/ADT/STLExtras.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000033#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000035using namespace llvm;
36
Dan Gohmanfd227e92008-03-25 17:10:29 +000037STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000038STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000040STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041
Jim Laskey95eda5b2006-08-01 14:21:23 +000042static RegisterScheduler
43 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
50
Evan Chengd38c22b2006-05-11 23:55:42 +000051namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000052//===----------------------------------------------------------------------===//
53/// ScheduleDAGRRList - The actual register reduction list scheduler
54/// implementation. This supports both top-down and bottom-up scheduling.
55///
Dan Gohman60cb69e2008-11-19 23:18:57 +000056class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000057private:
58 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
59 /// it is top-down.
60 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000061
Evan Chengd38c22b2006-05-11 23:55:42 +000062 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000063 SchedulingPriorityQueue *AvailableQueue;
64
Dan Gohmanc07f6862008-09-23 18:50:48 +000065 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000066 /// that are "live". These nodes must be scheduled before any other nodes that
67 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000068 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000069 std::vector<SUnit*> LiveRegDefs;
70 std::vector<unsigned> LiveRegCycles;
71
Dan Gohmanad2134d2008-11-25 00:52:40 +000072 /// Topo - A topological ordering for SUnits which permits fast IsReachable
73 /// and similar queries.
74 ScheduleDAGTopologicalSort Topo;
75
Evan Chengd38c22b2006-05-11 23:55:42 +000076public:
Dan Gohman619ef482009-01-15 19:20:50 +000077 ScheduleDAGRRList(MachineFunction &mf,
78 bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000079 SchedulingPriorityQueue *availqueue)
Dan Gohman619ef482009-01-15 19:20:50 +000080 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup),
Dan Gohmanad2134d2008-11-25 00:52:40 +000081 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000082 }
83
84 ~ScheduleDAGRRList() {
85 delete AvailableQueue;
86 }
87
88 void Schedule();
89
Roman Levenstein733a4d62008-03-26 11:23:38 +000090 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +000091 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
92 return Topo.IsReachable(SU, TargetSU);
93 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000094
Dan Gohman60d68442009-01-29 19:49:27 +000095 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000096 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +000097 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
98 return Topo.WillCreateCycle(SU, TargetSU);
99 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000100
Dan Gohman2d170892008-12-09 22:54:47 +0000101 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000102 /// This returns true if this is a new predecessor.
103 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000104 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000105 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000106 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000107 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000108
Dan Gohman2d170892008-12-09 22:54:47 +0000109 /// RemovePred - removes a predecessor edge from SUnit SU.
110 /// This returns true if an edge was removed.
111 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000112 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000113 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000114 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000115 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000116
Evan Chengd38c22b2006-05-11 23:55:42 +0000117private:
Dan Gohman60d68442009-01-29 19:49:27 +0000118 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000119 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000120 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000121 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000122 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000123 void ScheduleNodeBottomUp(SUnit*, unsigned);
124 void ScheduleNodeTopDown(SUnit*, unsigned);
125 void UnscheduleNodeBottomUp(SUnit*);
126 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
127 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000128 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
129 const TargetRegisterClass*,
130 const TargetRegisterClass*,
131 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000132 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000133 void ListScheduleTopDown();
134 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000135
136
137 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000138 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000139 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000140 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000141 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000142 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000143 if (NewNode->NodeNum >= NumSUnits)
144 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000145 return NewNode;
146 }
147
Roman Levenstein733a4d62008-03-26 11:23:38 +0000148 /// CreateClone - Creates a new SUnit from an existing one.
149 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000151 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000152 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000153 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000154 if (NewNode->NodeNum >= NumSUnits)
155 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156 return NewNode;
157 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000158
159 /// ForceUnitLatencies - Return true, since register-pressure-reducing
160 /// scheduling doesn't need actual latency information.
161 bool ForceUnitLatencies() const { return true; }
Evan Chengd38c22b2006-05-11 23:55:42 +0000162};
163} // end anonymous namespace
164
165
166/// Schedule - Schedule the DAG using list scheduling.
167void ScheduleDAGRRList::Schedule() {
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000168 DEBUG(errs() << "********** List Scheduling **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000169
Dan Gohmanc07f6862008-09-23 18:50:48 +0000170 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000171 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
172 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000173
Dan Gohman04543e72008-12-23 18:36:58 +0000174 // Build the scheduling graph.
175 BuildSchedGraph();
Evan Chengd38c22b2006-05-11 23:55:42 +0000176
Evan Chengd38c22b2006-05-11 23:55:42 +0000177 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000178 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000179 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000180
Dan Gohman46520a22008-06-21 19:18:17 +0000181 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000182
Evan Chengd38c22b2006-05-11 23:55:42 +0000183 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
184 if (isBottomUp)
185 ListScheduleBottomUp();
186 else
187 ListScheduleTopDown();
188
189 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000190}
Evan Chengd38c22b2006-05-11 23:55:42 +0000191
192//===----------------------------------------------------------------------===//
193// Bottom-Up Scheduling
194//===----------------------------------------------------------------------===//
195
Evan Chengd38c22b2006-05-11 23:55:42 +0000196/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000197/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000198void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000199 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000200
Evan Chengd38c22b2006-05-11 23:55:42 +0000201#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000202 if (PredSU->NumSuccsLeft == 0) {
Chris Lattner317dbbc2009-08-23 07:05:07 +0000203 errs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000204 PredSU->dump(this);
Chris Lattner317dbbc2009-08-23 07:05:07 +0000205 errs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000206 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000207 }
208#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000209 --PredSU->NumSuccsLeft;
210
Dan Gohmanb9543432009-02-10 23:27:53 +0000211 // If all the node's successors are scheduled, this node is ready
212 // to be scheduled. Ignore the special EntrySU node.
213 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000214 PredSU->isAvailable = true;
215 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000216 }
217}
218
Dan Gohmanb9543432009-02-10 23:27:53 +0000219void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000220 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000221 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000222 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000223 ReleasePred(SU, &*I);
224 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000225 // This is a physical register dependency and it's impossible or
226 // expensive to copy the register. Make sure nothing that can
227 // clobber the register is scheduled between the predecessor and
228 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000229 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000230 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000231 LiveRegDefs[I->getReg()] = I->getSUnit();
232 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000233 }
234 }
235 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000236}
237
238/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
239/// count of its predecessors. If a predecessor pending count is zero, add it to
240/// the Available queue.
241void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000242 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000243 DEBUG(SU->dump(this));
244
245 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
246 SU->setHeightToAtLeast(CurCycle);
247 Sequence.push_back(SU);
248
249 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000250
251 // Release all the implicit physical register defs that are live.
252 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
253 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000254 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000255 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000256 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000257 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000258 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000259 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000260 LiveRegDefs[I->getReg()] = NULL;
261 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000262 }
263 }
264 }
265
Evan Chengd38c22b2006-05-11 23:55:42 +0000266 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000267 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000268}
269
Evan Cheng5924bf72007-09-25 01:54:36 +0000270/// CapturePred - This does the opposite of ReleasePred. Since SU is being
271/// unscheduled, incrcease the succ left count of its predecessors. Remove
272/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000273void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
274 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000275 if (PredSU->isAvailable) {
276 PredSU->isAvailable = false;
277 if (!PredSU->isPending)
278 AvailableQueue->remove(PredSU);
279 }
280
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000281 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000282 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000283}
284
285/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
286/// its predecessor states to reflect the change.
287void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000288 DEBUG(errs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000289 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000290
291 AvailableQueue->UnscheduledNode(SU);
292
293 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
294 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000295 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000296 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000297 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000298 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000299 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000300 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000301 LiveRegDefs[I->getReg()] = NULL;
302 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000303 }
304 }
305
306 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
307 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000308 if (I->isAssignedRegDep()) {
309 if (!LiveRegDefs[I->getReg()]) {
310 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000311 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000312 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000313 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
314 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000315 }
316 }
317
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000318 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000319 SU->isScheduled = false;
320 SU->isAvailable = true;
321 AvailableQueue->push(SU);
322}
323
Evan Cheng8e136a92007-09-26 21:36:17 +0000324/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000325/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000326void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
327 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000328 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000329 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000330 OldSU = Sequence.back();
331 Sequence.pop_back();
332 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000333 // Don't try to remove SU from AvailableQueue.
334 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000335 UnscheduleNodeBottomUp(OldSU);
336 --CurCycle;
337 }
338
Dan Gohman60d68442009-01-29 19:49:27 +0000339 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000340
341 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000342}
343
Evan Cheng5924bf72007-09-25 01:54:36 +0000344/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
345/// successors to the newly created node.
346SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000347 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000348 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000349
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000350 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000351 if (!N)
352 return NULL;
353
354 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000355 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000356 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000357 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000358 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000359 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000360 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000361 TryUnfold = true;
362 }
Evan Cheng79e97132007-10-05 01:39:18 +0000363 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000364 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000365 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000366 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000367 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000368 }
369
370 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000371 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000372 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000373 return NULL;
374
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000375 DEBUG(errs() << "Unfolding SU # " << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000376 assert(NewNodes.size() == 2 && "Expected a load folding node!");
377
378 N = NewNodes[1];
379 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000380 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000381 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000382 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000383 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
384 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000385 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000386
Dan Gohmane52e0892008-11-11 21:34:44 +0000387 // LoadNode may already exist. This can happen when there is another
388 // load from the same location and producing the same type of value
389 // but it has different alignment or volatileness.
390 bool isNewLoad = true;
391 SUnit *LoadSU;
392 if (LoadNode->getNodeId() != -1) {
393 LoadSU = &SUnits[LoadNode->getNodeId()];
394 isNewLoad = false;
395 } else {
396 LoadSU = CreateNewSUnit(LoadNode);
397 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000398 ComputeLatency(LoadSU);
399 }
400
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000401 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000402 assert(N->getNodeId() == -1 && "Node already inserted!");
403 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000404
Dan Gohman17059682008-07-17 19:10:17 +0000405 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000406 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000407 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000408 NewSU->isTwoAddress = true;
409 break;
410 }
411 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000412 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000413 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000414 ComputeLatency(NewSU);
415
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000416 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000417 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000418 SmallVector<SDep, 4> ChainSuccs;
419 SmallVector<SDep, 4> LoadPreds;
420 SmallVector<SDep, 4> NodePreds;
421 SmallVector<SDep, 4> NodeSuccs;
422 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
423 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000424 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000425 ChainPreds.push_back(*I);
Dan Gohman2d170892008-12-09 22:54:47 +0000426 else if (I->getSUnit()->getNode() &&
427 I->getSUnit()->getNode()->isOperandOf(LoadNode))
428 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000429 else
Dan Gohman2d170892008-12-09 22:54:47 +0000430 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000431 }
432 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
433 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000434 if (I->isCtrl())
435 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000436 else
Dan Gohman2d170892008-12-09 22:54:47 +0000437 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000438 }
439
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000440 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000441 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
442 const SDep &Pred = ChainPreds[i];
443 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000444 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000445 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000446 }
Evan Cheng79e97132007-10-05 01:39:18 +0000447 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000448 const SDep &Pred = LoadPreds[i];
449 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000450 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000451 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000452 }
453 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000454 const SDep &Pred = NodePreds[i];
455 RemovePred(SU, Pred);
456 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000457 }
458 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000459 SDep D = NodeSuccs[i];
460 SUnit *SuccDep = D.getSUnit();
461 D.setSUnit(SU);
462 RemovePred(SuccDep, D);
463 D.setSUnit(NewSU);
464 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000465 }
466 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000467 SDep D = ChainSuccs[i];
468 SUnit *SuccDep = D.getSUnit();
469 D.setSUnit(SU);
470 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000471 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000472 D.setSUnit(LoadSU);
473 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000474 }
Evan Cheng79e97132007-10-05 01:39:18 +0000475 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000476
477 // Add a data dependency to reflect that NewSU reads the value defined
478 // by LoadSU.
479 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000480
Evan Cheng91e0fc92007-12-18 08:42:10 +0000481 if (isNewLoad)
482 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000483 AvailableQueue->addNode(NewSU);
484
485 ++NumUnfolds;
486
487 if (NewSU->NumSuccsLeft == 0) {
488 NewSU->isAvailable = true;
489 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000490 }
491 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000492 }
493
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000494 DEBUG(errs() << "Duplicating SU # " << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000495 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000496
497 // New SUnit has the exact same predecessors.
498 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
499 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000500 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000501 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000502
503 // Only copy scheduled successors. Cut them from old node's successor
504 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000505 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000506 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
507 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000508 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000509 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000510 SUnit *SuccSU = I->getSUnit();
511 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000512 SDep D = *I;
513 D.setSUnit(NewSU);
514 AddPred(SuccSU, D);
515 D.setSUnit(SU);
516 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000517 }
518 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000519 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000520 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000521
522 AvailableQueue->updateNode(SU);
523 AvailableQueue->addNode(NewSU);
524
Evan Cheng1ec79b42007-09-27 07:09:03 +0000525 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000526 return NewSU;
527}
528
Evan Chengb2c42c62009-01-12 03:19:55 +0000529/// InsertCopiesAndMoveSuccs - Insert register copies and move all
530/// scheduled successors of the given SUnit to the last copy.
531void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
532 const TargetRegisterClass *DestRC,
533 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000534 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000535 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000536 CopyFromSU->CopySrcRC = SrcRC;
537 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000538
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000539 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000540 CopyToSU->CopySrcRC = DestRC;
541 CopyToSU->CopyDstRC = SrcRC;
542
543 // Only copy scheduled successors. Cut them from old node's successor
544 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000545 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000546 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
547 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000548 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000549 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000550 SUnit *SuccSU = I->getSUnit();
551 if (SuccSU->isScheduled) {
552 SDep D = *I;
553 D.setSUnit(CopyToSU);
554 AddPred(SuccSU, D);
555 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000556 }
557 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000558 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000559 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000560
Dan Gohman2d170892008-12-09 22:54:47 +0000561 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
562 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000563
564 AvailableQueue->updateNode(SU);
565 AvailableQueue->addNode(CopyFromSU);
566 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000567 Copies.push_back(CopyFromSU);
568 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000569
Evan Chengb2c42c62009-01-12 03:19:55 +0000570 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000571}
572
573/// getPhysicalRegisterVT - Returns the ValueType of the physical register
574/// definition of the specified node.
575/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000576static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000577 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000578 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000579 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000580 unsigned NumRes = TID.getNumDefs();
581 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000582 if (Reg == *ImpDef)
583 break;
584 ++NumRes;
585 }
586 return N->getValueType(NumRes);
587}
588
Evan Chengb8905c42009-03-04 01:41:49 +0000589/// CheckForLiveRegDef - Return true and update live register vector if the
590/// specified register def of the specified SUnit clobbers any "live" registers.
591static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
592 std::vector<SUnit*> &LiveRegDefs,
593 SmallSet<unsigned, 4> &RegAdded,
594 SmallVector<unsigned, 4> &LRegs,
595 const TargetRegisterInfo *TRI) {
596 bool Added = false;
597 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
598 if (RegAdded.insert(Reg)) {
599 LRegs.push_back(Reg);
600 Added = true;
601 }
602 }
603 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
604 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
605 if (RegAdded.insert(*Alias)) {
606 LRegs.push_back(*Alias);
607 Added = true;
608 }
609 }
610 return Added;
611}
612
Evan Cheng5924bf72007-09-25 01:54:36 +0000613/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
614/// scheduling of the given node to satisfy live physical register dependencies.
615/// If the specific node is the last one that's available to schedule, do
616/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000617bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
618 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000619 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000620 return false;
621
Evan Chenge6f92252007-09-27 18:46:06 +0000622 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000623 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000624 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
625 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000626 if (I->isAssignedRegDep())
627 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
628 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000629 }
630
Dan Gohman072734e2008-11-13 23:24:17 +0000631 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000632 if (Node->getOpcode() == ISD::INLINEASM) {
633 // Inline asm can clobber physical defs.
634 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000635 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000636 --NumOps; // Ignore the flag operand.
637
638 for (unsigned i = 2; i != NumOps;) {
639 unsigned Flags =
640 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng2e559232009-03-20 18:03:34 +0000641 unsigned NumVals = (Flags & 0xffff) >> 3;
Evan Chengb8905c42009-03-04 01:41:49 +0000642
643 ++i; // Skip the ID value.
644 if ((Flags & 7) == 2 || (Flags & 7) == 6) {
645 // Check for def of register or earlyclobber register.
646 for (; NumVals; --NumVals, ++i) {
647 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
648 if (TargetRegisterInfo::isPhysicalRegister(Reg))
649 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
650 }
651 } else
652 i += NumVals;
653 }
654 continue;
655 }
656
Dan Gohman072734e2008-11-13 23:24:17 +0000657 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000658 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000659 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000660 if (!TID.ImplicitDefs)
661 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000662 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
663 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000664 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000665 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000666}
667
Evan Cheng1ec79b42007-09-27 07:09:03 +0000668
Evan Chengd38c22b2006-05-11 23:55:42 +0000669/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
670/// schedulers.
671void ScheduleDAGRRList::ListScheduleBottomUp() {
672 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000673
674 // Release any predecessors of the special Exit node.
675 ReleasePredecessors(&ExitSU, CurCycle);
676
Evan Chengd38c22b2006-05-11 23:55:42 +0000677 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000678 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000679 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000680 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
681 RootSU->isAvailable = true;
682 AvailableQueue->push(RootSU);
683 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000684
685 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000686 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000687 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000688 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000689 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000690 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000691 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000692 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000693 SUnit *CurSU = AvailableQueue->pop();
694 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000695 SmallVector<unsigned, 4> LRegs;
696 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
697 break;
698 Delayed = true;
699 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000700
701 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
702 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000703 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000704 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000705
706 // All candidates are delayed due to live physical reg dependencies.
707 // Try backtracking, code duplication, or inserting cross class copies
708 // to resolve it.
709 if (Delayed && !CurSU) {
710 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
711 SUnit *TrySU = NotReady[i];
712 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
713
714 // Try unscheduling up to the point where it's safe to schedule
715 // this node.
716 unsigned LiveCycle = CurCycle;
717 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
718 unsigned Reg = LRegs[j];
719 unsigned LCycle = LiveRegCycles[Reg];
720 LiveCycle = std::min(LiveCycle, LCycle);
721 }
722 SUnit *OldSU = Sequence[LiveCycle];
723 if (!WillCreateCycle(TrySU, OldSU)) {
724 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
725 // Force the current node to be scheduled before the node that
726 // requires the physical reg dep.
727 if (OldSU->isAvailable) {
728 OldSU->isAvailable = false;
729 AvailableQueue->remove(OldSU);
730 }
Dan Gohman2d170892008-12-09 22:54:47 +0000731 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
732 /*Reg=*/0, /*isNormalMemory=*/false,
733 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000734 // If one or more successors has been unscheduled, then the current
735 // node is no longer avaialable. Schedule a successor that's now
736 // available instead.
737 if (!TrySU->isAvailable)
738 CurSU = AvailableQueue->pop();
739 else {
740 CurSU = TrySU;
741 TrySU->isPending = false;
742 NotReady.erase(NotReady.begin()+i);
743 }
744 break;
745 }
746 }
747
748 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000749 // Can't backtrack. If it's too expensive to copy the value, then try
750 // duplicate the nodes that produces these "too expensive to copy"
751 // values to break the dependency. In case even that doesn't work,
752 // insert cross class copies.
753 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000754 SUnit *TrySU = NotReady[0];
755 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
756 assert(LRegs.size() == 1 && "Can't handle this yet!");
757 unsigned Reg = LRegs[0];
758 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000759 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000760 const TargetRegisterClass *RC =
761 TRI->getPhysicalRegisterRegClass(Reg, VT);
762 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
763
764 // If cross copy register class is null, then it must be possible copy
765 // the value directly. Do not try duplicate the def.
766 SUnit *NewDef = 0;
767 if (DestRC)
768 NewDef = CopyAndMoveSuccessors(LRDef);
769 else
770 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000771 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000772 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000773 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000774 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000775 DEBUG(errs() << "Adding an edge from SU #" << TrySU->NodeNum
776 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000777 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000778 /*Reg=*/0, /*isNormalMemory=*/false,
779 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000780 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000781 NewDef = Copies.back();
782 }
783
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000784 DEBUG(errs() << "Adding an edge from SU #" << NewDef->NodeNum
785 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000786 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000787 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000788 /*Reg=*/0, /*isNormalMemory=*/false,
789 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000790 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000791 TrySU->isAvailable = false;
792 CurSU = NewDef;
793 }
794
Dan Gohman60d68442009-01-29 19:49:27 +0000795 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000796 }
797
Evan Chengd38c22b2006-05-11 23:55:42 +0000798 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000799 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
800 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000801 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000802 if (NotReady[i]->isAvailable)
803 AvailableQueue->push(NotReady[i]);
804 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000805 NotReady.clear();
806
Dan Gohmanc602dd42008-11-21 00:10:42 +0000807 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000808 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000809 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000810 }
811
Evan Chengd38c22b2006-05-11 23:55:42 +0000812 // Reverse the order if it is bottom up.
813 std::reverse(Sequence.begin(), Sequence.end());
814
Evan Chengd38c22b2006-05-11 23:55:42 +0000815#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000816 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000817#endif
818}
819
820//===----------------------------------------------------------------------===//
821// Top-Down Scheduling
822//===----------------------------------------------------------------------===//
823
824/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000825/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000826void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000827 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000828
Evan Chengd38c22b2006-05-11 23:55:42 +0000829#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000830 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner317dbbc2009-08-23 07:05:07 +0000831 errs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000832 SuccSU->dump(this);
Chris Lattner317dbbc2009-08-23 07:05:07 +0000833 errs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000834 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000835 }
836#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000837 --SuccSU->NumPredsLeft;
838
Dan Gohmanb9543432009-02-10 23:27:53 +0000839 // If all the node's predecessors are scheduled, this node is ready
840 // to be scheduled. Ignore the special ExitSU node.
841 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000842 SuccSU->isAvailable = true;
843 AvailableQueue->push(SuccSU);
844 }
845}
846
Dan Gohmanb9543432009-02-10 23:27:53 +0000847void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
848 // Top down: release successors
849 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
850 I != E; ++I) {
851 assert(!I->isAssignedRegDep() &&
852 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
853
854 ReleaseSucc(SU, &*I);
855 }
856}
857
Evan Chengd38c22b2006-05-11 23:55:42 +0000858/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
859/// count of its successors. If a successor pending count is zero, add it to
860/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000861void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000862 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000863 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000864
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000865 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
866 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000867 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000868
Dan Gohmanb9543432009-02-10 23:27:53 +0000869 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000870 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000871 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000872}
873
Dan Gohman54a187e2007-08-20 19:28:38 +0000874/// ListScheduleTopDown - The main loop of list scheduling for top-down
875/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000876void ScheduleDAGRRList::ListScheduleTopDown() {
877 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000878
Dan Gohmanb9543432009-02-10 23:27:53 +0000879 // Release any successors of the special Entry node.
880 ReleaseSuccessors(&EntrySU);
881
Evan Chengd38c22b2006-05-11 23:55:42 +0000882 // All leaves to Available queue.
883 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
884 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000885 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000886 AvailableQueue->push(&SUnits[i]);
887 SUnits[i].isAvailable = true;
888 }
889 }
890
Evan Chengd38c22b2006-05-11 23:55:42 +0000891 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000892 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000893 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000894 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000895 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000896
Dan Gohmanc602dd42008-11-21 00:10:42 +0000897 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000898 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000899 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000900 }
901
Evan Chengd38c22b2006-05-11 23:55:42 +0000902#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000903 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000904#endif
905}
906
907
Evan Chengd38c22b2006-05-11 23:55:42 +0000908//===----------------------------------------------------------------------===//
909// RegReductionPriorityQueue Implementation
910//===----------------------------------------------------------------------===//
911//
912// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
913// to reduce register pressure.
914//
915namespace {
916 template<class SF>
917 class RegReductionPriorityQueue;
918
919 /// Sorting functions for the Available queue.
920 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
921 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
922 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
923 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
924
925 bool operator()(const SUnit* left, const SUnit* right) const;
926 };
927
928 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
929 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
930 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
931 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
932
933 bool operator()(const SUnit* left, const SUnit* right) const;
934 };
935} // end anonymous namespace
936
Dan Gohman186f65d2008-11-20 03:30:37 +0000937/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
938/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000939static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000940CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000941 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
942 if (SethiUllmanNumber != 0)
943 return SethiUllmanNumber;
944
945 unsigned Extra = 0;
946 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
947 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000948 if (I->isCtrl()) continue; // ignore chain preds
949 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000950 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000951 if (PredSethiUllman > SethiUllmanNumber) {
952 SethiUllmanNumber = PredSethiUllman;
953 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +0000954 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +0000955 ++Extra;
956 }
957
958 SethiUllmanNumber += Extra;
959
960 if (SethiUllmanNumber == 0)
961 SethiUllmanNumber = 1;
962
963 return SethiUllmanNumber;
964}
965
Evan Chengd38c22b2006-05-11 23:55:42 +0000966namespace {
967 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000968 class VISIBILITY_HIDDEN RegReductionPriorityQueue
969 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000970 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000971 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000972
Dan Gohman3f656df2008-11-20 02:45:51 +0000973 protected:
974 // SUnits - The SUnits for the current graph.
975 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000976
Dan Gohman3f656df2008-11-20 02:45:51 +0000977 const TargetInstrInfo *TII;
978 const TargetRegisterInfo *TRI;
979 ScheduleDAGRRList *scheduleDAG;
980
Dan Gohman186f65d2008-11-20 03:30:37 +0000981 // SethiUllmanNumbers - The SethiUllman number for each node.
982 std::vector<unsigned> SethiUllmanNumbers;
983
Dan Gohman3f656df2008-11-20 02:45:51 +0000984 public:
985 RegReductionPriorityQueue(const TargetInstrInfo *tii,
986 const TargetRegisterInfo *tri) :
987 Queue(SF(this)), currentQueueId(0),
988 TII(tii), TRI(tri), scheduleDAG(NULL) {}
989
990 void initNodes(std::vector<SUnit> &sunits) {
991 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000992 // Add pseudo dependency edges for two-address nodes.
993 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +0000994 // Reroute edges to nodes with multiple uses.
995 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +0000996 // Calculate node priorities.
997 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +0000998 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000999
Dan Gohman186f65d2008-11-20 03:30:37 +00001000 void addNode(const SUnit *SU) {
1001 unsigned SUSize = SethiUllmanNumbers.size();
1002 if (SUnits->size() > SUSize)
1003 SethiUllmanNumbers.resize(SUSize*2, 0);
1004 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1005 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001006
Dan Gohman186f65d2008-11-20 03:30:37 +00001007 void updateNode(const SUnit *SU) {
1008 SethiUllmanNumbers[SU->NodeNum] = 0;
1009 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1010 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001011
Dan Gohman186f65d2008-11-20 03:30:37 +00001012 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001013 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001014 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001015 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001016
1017 unsigned getNodePriority(const SUnit *SU) const {
1018 assert(SU->NodeNum < SethiUllmanNumbers.size());
1019 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001020 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001021 // CopyToReg should be close to its uses to facilitate coalescing and
1022 // avoid spilling.
1023 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001024 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Dan Gohman3027bb62009-04-16 20:57:10 +00001025 Opc == TargetInstrInfo::SUBREG_TO_REG ||
Dan Gohman261ee6b2009-01-07 22:30:55 +00001026 Opc == TargetInstrInfo::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001027 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1028 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001029 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001030 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1031 // If SU does not have a register use, i.e. it doesn't produce a value
1032 // that would be consumed (e.g. store), then it terminates a chain of
1033 // computation. Give it a large SethiUllman number so it will be
1034 // scheduled right before its predecessors that it doesn't lengthen
1035 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001036 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001037 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1038 // If SU does not have a register def, schedule it close to its uses
1039 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001040 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001041 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001042 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001043
Evan Cheng5924bf72007-09-25 01:54:36 +00001044 unsigned size() const { return Queue.size(); }
1045
Evan Chengd38c22b2006-05-11 23:55:42 +00001046 bool empty() const { return Queue.empty(); }
1047
1048 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001049 assert(!U->NodeQueueId && "Node in the queue already");
1050 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001051 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001052 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001053
Evan Chengd38c22b2006-05-11 23:55:42 +00001054 void push_all(const std::vector<SUnit *> &Nodes) {
1055 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001056 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001057 }
1058
1059 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001060 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001061 SUnit *V = Queue.top();
1062 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001063 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001064 return V;
1065 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001066
Evan Cheng5924bf72007-09-25 01:54:36 +00001067 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001068 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001069 assert(SU->NodeQueueId != 0 && "Not in queue!");
1070 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001071 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001072 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001073
1074 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1075 scheduleDAG = scheduleDag;
1076 }
1077
1078 protected:
1079 bool canClobber(const SUnit *SU, const SUnit *Op);
1080 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001081 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001082 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001083 };
1084
Dan Gohman186f65d2008-11-20 03:30:37 +00001085 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1086 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001087
Dan Gohman186f65d2008-11-20 03:30:37 +00001088 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1089 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001090}
1091
Evan Chengb9e3db62007-03-14 22:43:40 +00001092/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001093/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001094static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001095 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001096 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001097 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001098 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001099 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001100 // If there are bunch of CopyToRegs stacked up, they should be considered
1101 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001102 if (I->getSUnit()->getNode() &&
1103 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001104 Height = closestSucc(I->getSUnit())+1;
1105 if (Height > MaxHeight)
1106 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001107 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001108 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001109}
1110
Evan Cheng61bc51e2007-12-20 02:22:36 +00001111/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001112/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001113static unsigned calcMaxScratches(const SUnit *SU) {
1114 unsigned Scratches = 0;
1115 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001116 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001117 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001118 Scratches++;
1119 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001120 return Scratches;
1121}
1122
Evan Chengd38c22b2006-05-11 23:55:42 +00001123// Bottom up
1124bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001125 unsigned LPriority = SPQ->getNodePriority(left);
1126 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001127 if (LPriority != RPriority)
1128 return LPriority > RPriority;
1129
1130 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1131 // e.g.
1132 // t1 = op t2, c1
1133 // t3 = op t4, c2
1134 //
1135 // and the following instructions are both ready.
1136 // t2 = op c3
1137 // t4 = op c4
1138 //
1139 // Then schedule t2 = op first.
1140 // i.e.
1141 // t4 = op c4
1142 // t2 = op c3
1143 // t1 = op t2, c1
1144 // t3 = op t4, c2
1145 //
1146 // This creates more short live intervals.
1147 unsigned LDist = closestSucc(left);
1148 unsigned RDist = closestSucc(right);
1149 if (LDist != RDist)
1150 return LDist < RDist;
1151
Evan Cheng3a14efa2009-02-12 08:59:45 +00001152 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001153 unsigned LScratch = calcMaxScratches(left);
1154 unsigned RScratch = calcMaxScratches(right);
1155 if (LScratch != RScratch)
1156 return LScratch > RScratch;
1157
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001158 if (left->getHeight() != right->getHeight())
1159 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001160
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001161 if (left->getDepth() != right->getDepth())
1162 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001163
Roman Levenstein6b371142008-04-29 09:07:59 +00001164 assert(left->NodeQueueId && right->NodeQueueId &&
1165 "NodeQueueId cannot be zero");
1166 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001167}
1168
Dan Gohman3f656df2008-11-20 02:45:51 +00001169template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001170bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001171RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001172 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001173 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001174 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001175 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001176 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001177 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001178 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001179 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001180 if (DU->getNodeId() != -1 &&
1181 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001182 return true;
1183 }
1184 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001185 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001186 return false;
1187}
1188
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001189
Evan Chenga5e595d2007-09-28 22:32:30 +00001190/// hasCopyToRegUse - Return true if SU has a value successor that is a
1191/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001192static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001193 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1194 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001195 if (I->isCtrl()) continue;
1196 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001197 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001198 return true;
1199 }
1200 return false;
1201}
1202
Evan Chengf9891412007-12-20 09:25:31 +00001203/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001204/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001205static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001206 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001207 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001208 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001209 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1210 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001211 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001212 for (const SDNode *SUNode = SU->getNode(); SUNode;
1213 SUNode = SUNode->getFlaggedNode()) {
1214 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001215 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001216 const unsigned *SUImpDefs =
1217 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1218 if (!SUImpDefs)
1219 return false;
1220 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001221 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001222 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001223 continue;
1224 if (!N->hasAnyUseOfValue(i))
1225 continue;
1226 unsigned Reg = ImpDefs[i - NumDefs];
1227 for (;*SUImpDefs; ++SUImpDefs) {
1228 unsigned SUReg = *SUImpDefs;
1229 if (TRI->regsOverlap(Reg, SUReg))
1230 return true;
1231 }
Evan Chengf9891412007-12-20 09:25:31 +00001232 }
1233 }
1234 return false;
1235}
1236
Dan Gohman9a658d72009-03-24 00:49:12 +00001237/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1238/// are not handled well by the general register pressure reduction
1239/// heuristics. When presented with code like this:
1240///
1241/// N
1242/// / |
1243/// / |
1244/// U store
1245/// |
1246/// ...
1247///
1248/// the heuristics tend to push the store up, but since the
1249/// operand of the store has another use (U), this would increase
1250/// the length of that other use (the U->N edge).
1251///
1252/// This function transforms code like the above to route U's
1253/// dependence through the store when possible, like this:
1254///
1255/// N
1256/// ||
1257/// ||
1258/// store
1259/// |
1260/// U
1261/// |
1262/// ...
1263///
1264/// This results in the store being scheduled immediately
1265/// after N, which shortens the U->N live range, reducing
1266/// register pressure.
1267///
1268template<class SF>
1269void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1270 // Visit all the nodes in topological order, working top-down.
1271 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1272 SUnit *SU = &(*SUnits)[i];
1273 // For now, only look at nodes with no data successors, such as stores.
1274 // These are especially important, due to the heuristics in
1275 // getNodePriority for nodes with no data successors.
1276 if (SU->NumSuccs != 0)
1277 continue;
1278 // For now, only look at nodes with exactly one data predecessor.
1279 if (SU->NumPreds != 1)
1280 continue;
1281 // Avoid prescheduling copies to virtual registers, which don't behave
1282 // like other nodes from the perspective of scheduling heuristics.
1283 if (SDNode *N = SU->getNode())
1284 if (N->getOpcode() == ISD::CopyToReg &&
1285 TargetRegisterInfo::isVirtualRegister
1286 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1287 continue;
1288
1289 // Locate the single data predecessor.
1290 SUnit *PredSU = 0;
1291 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1292 EE = SU->Preds.end(); II != EE; ++II)
1293 if (!II->isCtrl()) {
1294 PredSU = II->getSUnit();
1295 break;
1296 }
1297 assert(PredSU);
1298
1299 // Don't rewrite edges that carry physregs, because that requires additional
1300 // support infrastructure.
1301 if (PredSU->hasPhysRegDefs)
1302 continue;
1303 // Short-circuit the case where SU is PredSU's only data successor.
1304 if (PredSU->NumSuccs == 1)
1305 continue;
1306 // Avoid prescheduling to copies from virtual registers, which don't behave
1307 // like other nodes from the perspective of scheduling // heuristics.
1308 if (SDNode *N = SU->getNode())
1309 if (N->getOpcode() == ISD::CopyFromReg &&
1310 TargetRegisterInfo::isVirtualRegister
1311 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1312 continue;
1313
1314 // Perform checks on the successors of PredSU.
1315 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1316 EE = PredSU->Succs.end(); II != EE; ++II) {
1317 SUnit *PredSuccSU = II->getSUnit();
1318 if (PredSuccSU == SU) continue;
1319 // If PredSU has another successor with no data successors, for
1320 // now don't attempt to choose either over the other.
1321 if (PredSuccSU->NumSuccs == 0)
1322 goto outer_loop_continue;
1323 // Don't break physical register dependencies.
1324 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1325 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1326 goto outer_loop_continue;
1327 // Don't introduce graph cycles.
1328 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1329 goto outer_loop_continue;
1330 }
1331
1332 // Ok, the transformation is safe and the heuristics suggest it is
1333 // profitable. Update the graph.
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001334 DEBUG(errs() << "Prescheduling SU # " << SU->NodeNum
1335 << " next to PredSU # " << PredSU->NodeNum
1336 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001337 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1338 SDep Edge = PredSU->Succs[i];
1339 assert(!Edge.isAssignedRegDep());
1340 SUnit *SuccSU = Edge.getSUnit();
1341 if (SuccSU != SU) {
1342 Edge.setSUnit(PredSU);
1343 scheduleDAG->RemovePred(SuccSU, Edge);
1344 scheduleDAG->AddPred(SU, Edge);
1345 Edge.setSUnit(SU);
1346 scheduleDAG->AddPred(SuccSU, Edge);
1347 --i;
1348 }
1349 }
1350 outer_loop_continue:;
1351 }
1352}
1353
Evan Chengd38c22b2006-05-11 23:55:42 +00001354/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1355/// it as a def&use operand. Add a pseudo control edge from it to the other
1356/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001357/// first (lower in the schedule). If both nodes are two-address, favor the
1358/// one that has a CopyToReg use (more likely to be a loop induction update).
1359/// If both are two-address, but one is commutable while the other is not
1360/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001361template<class SF>
1362void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001363 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001364 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001365 if (!SU->isTwoAddress)
1366 continue;
1367
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001368 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001369 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001370 continue;
1371
Dan Gohman17059682008-07-17 19:10:17 +00001372 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001373 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001374 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001375 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001376 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001377 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1378 continue;
1379 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1380 if (DU->getNodeId() == -1)
1381 continue;
1382 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1383 if (!DUSU) continue;
1384 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1385 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001386 if (I->isCtrl()) continue;
1387 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001388 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001389 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001390 // Be conservative. Ignore if nodes aren't at roughly the same
1391 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001392 if (SuccSU->getHeight() < SU->getHeight() &&
1393 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001394 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001395 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1396 // constrains whatever is using the copy, instead of the copy
1397 // itself. In the case that the copy is coalesced, this
1398 // preserves the intent of the pseudo two-address heurietics.
1399 while (SuccSU->Succs.size() == 1 &&
1400 SuccSU->getNode()->isMachineOpcode() &&
1401 SuccSU->getNode()->getMachineOpcode() ==
1402 TargetInstrInfo::COPY_TO_REGCLASS)
1403 SuccSU = SuccSU->Succs.front().getSUnit();
1404 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001405 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1406 continue;
1407 // Don't constrain nodes with physical register defs if the
1408 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001409 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001410 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001411 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001412 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001413 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1414 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001415 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1416 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
Dan Gohman3027bb62009-04-16 20:57:10 +00001417 SuccOpc == TargetInstrInfo::INSERT_SUBREG ||
1418 SuccOpc == TargetInstrInfo::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001419 continue;
1420 if ((!canClobber(SuccSU, DUSU) ||
1421 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1422 (!SU->isCommutable && SuccSU->isCommutable)) &&
1423 !scheduleDAG->IsReachable(SuccSU, SU)) {
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001424 DEBUG(errs() << "Adding a pseudo-two-addr edge from SU # "
1425 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001426 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001427 /*Reg=*/0, /*isNormalMemory=*/false,
1428 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001429 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001430 }
1431 }
1432 }
1433 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001434}
1435
Evan Cheng6730f032007-01-08 23:55:53 +00001436/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1437/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001438template<class SF>
1439void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001440 SethiUllmanNumbers.assign(SUnits->size(), 0);
1441
1442 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001443 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001444}
Evan Chengd38c22b2006-05-11 23:55:42 +00001445
Roman Levenstein30d09512008-03-27 09:44:37 +00001446/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001447/// predecessors of the successors of the SUnit SU. Stop when the provided
1448/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001449static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1450 unsigned Limit) {
1451 unsigned Sum = 0;
1452 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1453 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001454 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001455 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1456 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001457 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001458 if (!PredSU->isScheduled)
1459 if (++Sum > Limit)
1460 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001461 }
1462 }
1463 return Sum;
1464}
1465
Evan Chengd38c22b2006-05-11 23:55:42 +00001466
1467// Top down
1468bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001469 unsigned LPriority = SPQ->getNodePriority(left);
1470 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001471 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1472 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001473 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1474 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001475 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1476 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001477
1478 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1479 return false;
1480 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1481 return true;
1482
Evan Chengd38c22b2006-05-11 23:55:42 +00001483 if (LIsFloater)
1484 LBonus -= 2;
1485 if (RIsFloater)
1486 RBonus -= 2;
1487 if (left->NumSuccs == 1)
1488 LBonus += 2;
1489 if (right->NumSuccs == 1)
1490 RBonus += 2;
1491
Evan Cheng73bdf042008-03-01 00:39:47 +00001492 if (LPriority+LBonus != RPriority+RBonus)
1493 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001494
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001495 if (left->getDepth() != right->getDepth())
1496 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001497
1498 if (left->NumSuccsLeft != right->NumSuccsLeft)
1499 return left->NumSuccsLeft > right->NumSuccsLeft;
1500
Roman Levenstein6b371142008-04-29 09:07:59 +00001501 assert(left->NodeQueueId && right->NodeQueueId &&
1502 "NodeQueueId cannot be zero");
1503 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001504}
1505
Evan Chengd38c22b2006-05-11 23:55:42 +00001506//===----------------------------------------------------------------------===//
1507// Public Constructor Functions
1508//===----------------------------------------------------------------------===//
1509
Dan Gohmandfaf6462009-02-11 04:27:20 +00001510llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001511llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001512 const TargetMachine &TM = IS->TM;
1513 const TargetInstrInfo *TII = TM.getInstrInfo();
1514 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001515
Evan Cheng7e4abde2008-07-02 09:23:51 +00001516 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001517
Evan Cheng7e4abde2008-07-02 09:23:51 +00001518 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001519 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001520 PQ->setScheduleDAG(SD);
1521 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001522}
1523
Dan Gohmandfaf6462009-02-11 04:27:20 +00001524llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001525llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001526 const TargetMachine &TM = IS->TM;
1527 const TargetInstrInfo *TII = TM.getInstrInfo();
1528 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001529
1530 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1531
Dan Gohman619ef482009-01-15 19:20:50 +00001532 ScheduleDAGRRList *SD =
1533 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001534 PQ->setScheduleDAG(SD);
1535 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001536}