blob: 1ad7919962b3048a04747ad08d5d4e15046b4913 [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"
Torok Edwin56d06592009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.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"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000032#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000033#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000034using namespace llvm;
35
Dan Gohmanfd227e92008-03-25 17:10:29 +000036STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000037STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000038STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000039STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000040
Jim Laskey95eda5b2006-08-01 14:21:23 +000041static RegisterScheduler
42 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000043 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000044 createBURRListDAGScheduler);
45static RegisterScheduler
46 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000047 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000048 createTDRRListDAGScheduler);
49
Evan Chengd38c22b2006-05-11 23:55:42 +000050namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000051//===----------------------------------------------------------------------===//
52/// ScheduleDAGRRList - The actual register reduction list scheduler
53/// implementation. This supports both top-down and bottom-up scheduling.
54///
Nick Lewycky02d5f772009-10-25 06:33:48 +000055class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000056private:
57 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
58 /// it is top-down.
59 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000060
Evan Chengd38c22b2006-05-11 23:55:42 +000061 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000062 SchedulingPriorityQueue *AvailableQueue;
63
Dan Gohmanc07f6862008-09-23 18:50:48 +000064 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000065 /// that are "live". These nodes must be scheduled before any other nodes that
66 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000067 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000068 std::vector<SUnit*> LiveRegDefs;
69 std::vector<unsigned> LiveRegCycles;
70
Dan Gohmanad2134d2008-11-25 00:52:40 +000071 /// Topo - A topological ordering for SUnits which permits fast IsReachable
72 /// and similar queries.
73 ScheduleDAGTopologicalSort Topo;
74
Evan Chengd38c22b2006-05-11 23:55:42 +000075public:
Dan Gohman619ef482009-01-15 19:20:50 +000076 ScheduleDAGRRList(MachineFunction &mf,
77 bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000078 SchedulingPriorityQueue *availqueue)
Dan Gohman619ef482009-01-15 19:20:50 +000079 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup),
Dan Gohmanad2134d2008-11-25 00:52:40 +000080 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000081 }
82
83 ~ScheduleDAGRRList() {
84 delete AvailableQueue;
85 }
86
87 void Schedule();
88
Roman Levenstein733a4d62008-03-26 11:23:38 +000089 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +000090 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
91 return Topo.IsReachable(SU, TargetSU);
92 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000093
Dan Gohman60d68442009-01-29 19:49:27 +000094 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000095 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +000096 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
97 return Topo.WillCreateCycle(SU, TargetSU);
98 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000099
Dan Gohman2d170892008-12-09 22:54:47 +0000100 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000101 /// This returns true if this is a new predecessor.
102 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000103 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000104 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000105 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000106 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000107
Dan Gohman2d170892008-12-09 22:54:47 +0000108 /// RemovePred - removes a predecessor edge from SUnit SU.
109 /// This returns true if an edge was removed.
110 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000111 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000112 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000113 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Evan Chengd38c22b2006-05-11 23:55:42 +0000116private:
Dan Gohman60d68442009-01-29 19:49:27 +0000117 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000118 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000119 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000120 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000121 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000122 void ScheduleNodeBottomUp(SUnit*, unsigned);
123 void ScheduleNodeTopDown(SUnit*, unsigned);
124 void UnscheduleNodeBottomUp(SUnit*);
125 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
126 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000127 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
128 const TargetRegisterClass*,
129 const TargetRegisterClass*,
130 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000131 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000132 void ListScheduleTopDown();
133 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000134
135
136 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000137 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000138 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000139 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000140 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000141 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000142 if (NewNode->NodeNum >= NumSUnits)
143 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000144 return NewNode;
145 }
146
Roman Levenstein733a4d62008-03-26 11:23:38 +0000147 /// CreateClone - Creates a new SUnit from an existing one.
148 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000149 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000150 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000151 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000152 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000153 if (NewNode->NodeNum >= NumSUnits)
154 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000155 return NewNode;
156 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000157
158 /// ForceUnitLatencies - Return true, since register-pressure-reducing
159 /// scheduling doesn't need actual latency information.
160 bool ForceUnitLatencies() const { return true; }
Evan Chengd38c22b2006-05-11 23:55:42 +0000161};
162} // end anonymous namespace
163
164
165/// Schedule - Schedule the DAG using list scheduling.
166void ScheduleDAGRRList::Schedule() {
David Greenef34d7ac2010-01-05 01:24:54 +0000167 DEBUG(dbgs() << "********** List Scheduling **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000168
Dan Gohmanc07f6862008-09-23 18:50:48 +0000169 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000170 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
171 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000172
Dan Gohman04543e72008-12-23 18:36:58 +0000173 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000174 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000175
Evan Chengd38c22b2006-05-11 23:55:42 +0000176 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000177 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000178 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000179
Dan Gohman46520a22008-06-21 19:18:17 +0000180 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000181
Evan Chengd38c22b2006-05-11 23:55:42 +0000182 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
183 if (isBottomUp)
184 ListScheduleBottomUp();
185 else
186 ListScheduleTopDown();
187
188 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000189}
Evan Chengd38c22b2006-05-11 23:55:42 +0000190
191//===----------------------------------------------------------------------===//
192// Bottom-Up Scheduling
193//===----------------------------------------------------------------------===//
194
Evan Chengd38c22b2006-05-11 23:55:42 +0000195/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000196/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000197void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000198 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000199
Evan Chengd38c22b2006-05-11 23:55:42 +0000200#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000201 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000202 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000203 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000204 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000205 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000206 }
207#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000208 --PredSU->NumSuccsLeft;
209
Dan Gohmanb9543432009-02-10 23:27:53 +0000210 // If all the node's successors are scheduled, this node is ready
211 // to be scheduled. Ignore the special EntrySU node.
212 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000213 PredSU->isAvailable = true;
214 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000215 }
216}
217
Dan Gohmanb9543432009-02-10 23:27:53 +0000218void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000219 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000220 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000221 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000222 ReleasePred(SU, &*I);
223 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000224 // This is a physical register dependency and it's impossible or
225 // expensive to copy the register. Make sure nothing that can
226 // clobber the register is scheduled between the predecessor and
227 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000228 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000229 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000230 LiveRegDefs[I->getReg()] = I->getSUnit();
231 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000232 }
233 }
234 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000235}
236
237/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
238/// count of its predecessors. If a predecessor pending count is zero, add it to
239/// the Available queue.
240void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000241 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000242 DEBUG(SU->dump(this));
243
244 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
245 SU->setHeightToAtLeast(CurCycle);
246 Sequence.push_back(SU);
247
248 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000249
250 // Release all the implicit physical register defs that are live.
251 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
252 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000253 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000254 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000255 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000256 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000257 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000258 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000259 LiveRegDefs[I->getReg()] = NULL;
260 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000261 }
262 }
263 }
264
Evan Chengd38c22b2006-05-11 23:55:42 +0000265 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000266 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000267}
268
Evan Cheng5924bf72007-09-25 01:54:36 +0000269/// CapturePred - This does the opposite of ReleasePred. Since SU is being
270/// unscheduled, incrcease the succ left count of its predecessors. Remove
271/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000272void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
273 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000274 if (PredSU->isAvailable) {
275 PredSU->isAvailable = false;
276 if (!PredSU->isPending)
277 AvailableQueue->remove(PredSU);
278 }
279
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000280 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000281 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000282}
283
284/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
285/// its predecessor states to reflect the change.
286void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000287 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000288 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000289
290 AvailableQueue->UnscheduledNode(SU);
291
292 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
293 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000294 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000295 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000296 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000297 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000298 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000299 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000300 LiveRegDefs[I->getReg()] = NULL;
301 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000302 }
303 }
304
305 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
306 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000307 if (I->isAssignedRegDep()) {
308 if (!LiveRegDefs[I->getReg()]) {
309 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000310 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000311 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000312 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
313 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000314 }
315 }
316
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000317 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000318 SU->isScheduled = false;
319 SU->isAvailable = true;
320 AvailableQueue->push(SU);
321}
322
Evan Cheng8e136a92007-09-26 21:36:17 +0000323/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000324/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000325void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
326 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000327 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000328 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000329 OldSU = Sequence.back();
330 Sequence.pop_back();
331 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000332 // Don't try to remove SU from AvailableQueue.
333 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000334 UnscheduleNodeBottomUp(OldSU);
335 --CurCycle;
336 }
337
Dan Gohman60d68442009-01-29 19:49:27 +0000338 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000339
340 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000341}
342
Evan Cheng5924bf72007-09-25 01:54:36 +0000343/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
344/// successors to the newly created node.
345SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000346 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000347 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000348
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000349 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000350 if (!N)
351 return NULL;
352
353 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000354 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000355 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000356 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000357 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000358 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000359 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000360 TryUnfold = true;
361 }
Evan Cheng79e97132007-10-05 01:39:18 +0000362 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000363 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000364 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000365 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000366 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000367 }
368
369 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000370 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000371 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000372 return NULL;
373
David Greenef34d7ac2010-01-05 01:24:54 +0000374 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000375 assert(NewNodes.size() == 2 && "Expected a load folding node!");
376
377 N = NewNodes[1];
378 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000379 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000380 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000381 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000382 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
383 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000384 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000385
Dan Gohmane52e0892008-11-11 21:34:44 +0000386 // LoadNode may already exist. This can happen when there is another
387 // load from the same location and producing the same type of value
388 // but it has different alignment or volatileness.
389 bool isNewLoad = true;
390 SUnit *LoadSU;
391 if (LoadNode->getNodeId() != -1) {
392 LoadSU = &SUnits[LoadNode->getNodeId()];
393 isNewLoad = false;
394 } else {
395 LoadSU = CreateNewSUnit(LoadNode);
396 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000397 ComputeLatency(LoadSU);
398 }
399
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000400 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000401 assert(N->getNodeId() == -1 && "Node already inserted!");
402 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000403
Dan Gohman17059682008-07-17 19:10:17 +0000404 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000405 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000406 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000407 NewSU->isTwoAddress = true;
408 break;
409 }
410 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000411 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000412 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000413 ComputeLatency(NewSU);
414
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000415 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000416 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000417 SmallVector<SDep, 4> ChainSuccs;
418 SmallVector<SDep, 4> LoadPreds;
419 SmallVector<SDep, 4> NodePreds;
420 SmallVector<SDep, 4> NodeSuccs;
421 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
422 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000423 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000424 ChainPreds.push_back(*I);
Dan Gohman2d170892008-12-09 22:54:47 +0000425 else if (I->getSUnit()->getNode() &&
426 I->getSUnit()->getNode()->isOperandOf(LoadNode))
427 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000428 else
Dan Gohman2d170892008-12-09 22:54:47 +0000429 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000430 }
431 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
432 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000433 if (I->isCtrl())
434 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000435 else
Dan Gohman2d170892008-12-09 22:54:47 +0000436 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000437 }
438
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000439 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000440 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
441 const SDep &Pred = ChainPreds[i];
442 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000443 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000444 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000445 }
Evan Cheng79e97132007-10-05 01:39:18 +0000446 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000447 const SDep &Pred = LoadPreds[i];
448 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000449 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000450 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000451 }
452 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000453 const SDep &Pred = NodePreds[i];
454 RemovePred(SU, Pred);
455 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000456 }
457 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000458 SDep D = NodeSuccs[i];
459 SUnit *SuccDep = D.getSUnit();
460 D.setSUnit(SU);
461 RemovePred(SuccDep, D);
462 D.setSUnit(NewSU);
463 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000464 }
465 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000466 SDep D = ChainSuccs[i];
467 SUnit *SuccDep = D.getSUnit();
468 D.setSUnit(SU);
469 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000470 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000471 D.setSUnit(LoadSU);
472 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000473 }
Evan Cheng79e97132007-10-05 01:39:18 +0000474 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000475
476 // Add a data dependency to reflect that NewSU reads the value defined
477 // by LoadSU.
478 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000479
Evan Cheng91e0fc92007-12-18 08:42:10 +0000480 if (isNewLoad)
481 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000482 AvailableQueue->addNode(NewSU);
483
484 ++NumUnfolds;
485
486 if (NewSU->NumSuccsLeft == 0) {
487 NewSU->isAvailable = true;
488 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000489 }
490 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000491 }
492
David Greenef34d7ac2010-01-05 01:24:54 +0000493 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000494 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000495
496 // New SUnit has the exact same predecessors.
497 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
498 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000499 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000500 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000501
502 // Only copy scheduled successors. Cut them from old node's successor
503 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000504 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000505 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
506 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000507 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000508 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000509 SUnit *SuccSU = I->getSUnit();
510 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000511 SDep D = *I;
512 D.setSUnit(NewSU);
513 AddPred(SuccSU, D);
514 D.setSUnit(SU);
515 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000516 }
517 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000518 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000519 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000520
521 AvailableQueue->updateNode(SU);
522 AvailableQueue->addNode(NewSU);
523
Evan Cheng1ec79b42007-09-27 07:09:03 +0000524 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000525 return NewSU;
526}
527
Evan Chengb2c42c62009-01-12 03:19:55 +0000528/// InsertCopiesAndMoveSuccs - Insert register copies and move all
529/// scheduled successors of the given SUnit to the last copy.
530void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
531 const TargetRegisterClass *DestRC,
532 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000533 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000534 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000535 CopyFromSU->CopySrcRC = SrcRC;
536 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000537
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000538 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000539 CopyToSU->CopySrcRC = DestRC;
540 CopyToSU->CopyDstRC = SrcRC;
541
542 // Only copy scheduled successors. Cut them from old node's successor
543 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000544 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000545 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
546 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000547 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000548 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000549 SUnit *SuccSU = I->getSUnit();
550 if (SuccSU->isScheduled) {
551 SDep D = *I;
552 D.setSUnit(CopyToSU);
553 AddPred(SuccSU, D);
554 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000555 }
556 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000557 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000558 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000559
Dan Gohman2d170892008-12-09 22:54:47 +0000560 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
561 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000562
563 AvailableQueue->updateNode(SU);
564 AvailableQueue->addNode(CopyFromSU);
565 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000566 Copies.push_back(CopyFromSU);
567 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000568
Evan Chengb2c42c62009-01-12 03:19:55 +0000569 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000570}
571
572/// getPhysicalRegisterVT - Returns the ValueType of the physical register
573/// definition of the specified node.
574/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000575static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000576 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000577 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000578 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000579 unsigned NumRes = TID.getNumDefs();
580 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000581 if (Reg == *ImpDef)
582 break;
583 ++NumRes;
584 }
585 return N->getValueType(NumRes);
586}
587
Evan Chengb8905c42009-03-04 01:41:49 +0000588/// CheckForLiveRegDef - Return true and update live register vector if the
589/// specified register def of the specified SUnit clobbers any "live" registers.
590static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
591 std::vector<SUnit*> &LiveRegDefs,
592 SmallSet<unsigned, 4> &RegAdded,
593 SmallVector<unsigned, 4> &LRegs,
594 const TargetRegisterInfo *TRI) {
595 bool Added = false;
596 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
597 if (RegAdded.insert(Reg)) {
598 LRegs.push_back(Reg);
599 Added = true;
600 }
601 }
602 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
603 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
604 if (RegAdded.insert(*Alias)) {
605 LRegs.push_back(*Alias);
606 Added = true;
607 }
608 }
609 return Added;
610}
611
Evan Cheng5924bf72007-09-25 01:54:36 +0000612/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
613/// scheduling of the given node to satisfy live physical register dependencies.
614/// If the specific node is the last one that's available to schedule, do
615/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000616bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
617 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000618 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000619 return false;
620
Evan Chenge6f92252007-09-27 18:46:06 +0000621 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000622 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000623 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
624 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000625 if (I->isAssignedRegDep())
626 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
627 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000628 }
629
Dan Gohman072734e2008-11-13 23:24:17 +0000630 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000631 if (Node->getOpcode() == ISD::INLINEASM) {
632 // Inline asm can clobber physical defs.
633 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000634 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000635 --NumOps; // Ignore the flag operand.
636
637 for (unsigned i = 2; i != NumOps;) {
638 unsigned Flags =
639 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng2e559232009-03-20 18:03:34 +0000640 unsigned NumVals = (Flags & 0xffff) >> 3;
Evan Chengb8905c42009-03-04 01:41:49 +0000641
642 ++i; // Skip the ID value.
643 if ((Flags & 7) == 2 || (Flags & 7) == 6) {
644 // Check for def of register or earlyclobber register.
645 for (; NumVals; --NumVals, ++i) {
646 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
647 if (TargetRegisterInfo::isPhysicalRegister(Reg))
648 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
649 }
650 } else
651 i += NumVals;
652 }
653 continue;
654 }
655
Dan Gohman072734e2008-11-13 23:24:17 +0000656 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000657 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000658 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000659 if (!TID.ImplicitDefs)
660 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000661 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
662 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000663 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000664 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000665}
666
Evan Cheng1ec79b42007-09-27 07:09:03 +0000667
Evan Chengd38c22b2006-05-11 23:55:42 +0000668/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
669/// schedulers.
670void ScheduleDAGRRList::ListScheduleBottomUp() {
671 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000672
673 // Release any predecessors of the special Exit node.
674 ReleasePredecessors(&ExitSU, CurCycle);
675
Evan Chengd38c22b2006-05-11 23:55:42 +0000676 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000677 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000678 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000679 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
680 RootSU->isAvailable = true;
681 AvailableQueue->push(RootSU);
682 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000683
684 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000685 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000686 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000687 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000688 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000689 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000690 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000691 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000692 SUnit *CurSU = AvailableQueue->pop();
693 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000694 SmallVector<unsigned, 4> LRegs;
695 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
696 break;
697 Delayed = true;
698 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000699
700 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
701 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000702 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000703 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000704
705 // All candidates are delayed due to live physical reg dependencies.
706 // Try backtracking, code duplication, or inserting cross class copies
707 // to resolve it.
708 if (Delayed && !CurSU) {
709 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
710 SUnit *TrySU = NotReady[i];
711 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
712
713 // Try unscheduling up to the point where it's safe to schedule
714 // this node.
715 unsigned LiveCycle = CurCycle;
716 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
717 unsigned Reg = LRegs[j];
718 unsigned LCycle = LiveRegCycles[Reg];
719 LiveCycle = std::min(LiveCycle, LCycle);
720 }
721 SUnit *OldSU = Sequence[LiveCycle];
722 if (!WillCreateCycle(TrySU, OldSU)) {
723 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
724 // Force the current node to be scheduled before the node that
725 // requires the physical reg dep.
726 if (OldSU->isAvailable) {
727 OldSU->isAvailable = false;
728 AvailableQueue->remove(OldSU);
729 }
Dan Gohman2d170892008-12-09 22:54:47 +0000730 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
731 /*Reg=*/0, /*isNormalMemory=*/false,
732 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000733 // If one or more successors has been unscheduled, then the current
734 // node is no longer avaialable. Schedule a successor that's now
735 // available instead.
736 if (!TrySU->isAvailable)
737 CurSU = AvailableQueue->pop();
738 else {
739 CurSU = TrySU;
740 TrySU->isPending = false;
741 NotReady.erase(NotReady.begin()+i);
742 }
743 break;
744 }
745 }
746
747 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000748 // Can't backtrack. If it's too expensive to copy the value, then try
749 // duplicate the nodes that produces these "too expensive to copy"
750 // values to break the dependency. In case even that doesn't work,
751 // insert cross class copies.
752 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000753 SUnit *TrySU = NotReady[0];
754 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
755 assert(LRegs.size() == 1 && "Can't handle this yet!");
756 unsigned Reg = LRegs[0];
757 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000758 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000759 const TargetRegisterClass *RC =
760 TRI->getPhysicalRegisterRegClass(Reg, VT);
761 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
762
763 // If cross copy register class is null, then it must be possible copy
764 // the value directly. Do not try duplicate the def.
765 SUnit *NewDef = 0;
766 if (DestRC)
767 NewDef = CopyAndMoveSuccessors(LRDef);
768 else
769 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000770 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000771 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000772 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000773 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
David Greenef34d7ac2010-01-05 01:24:54 +0000774 DEBUG(dbgs() << "Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000775 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000776 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000777 /*Reg=*/0, /*isNormalMemory=*/false,
778 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000779 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000780 NewDef = Copies.back();
781 }
782
David Greenef34d7ac2010-01-05 01:24:54 +0000783 DEBUG(dbgs() << "Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000784 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000785 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000786 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000787 /*Reg=*/0, /*isNormalMemory=*/false,
788 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000789 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000790 TrySU->isAvailable = false;
791 CurSU = NewDef;
792 }
793
Dan Gohman60d68442009-01-29 19:49:27 +0000794 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000795 }
796
Evan Chengd38c22b2006-05-11 23:55:42 +0000797 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000798 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
799 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000800 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000801 if (NotReady[i]->isAvailable)
802 AvailableQueue->push(NotReady[i]);
803 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000804 NotReady.clear();
805
Dan Gohmanc602dd42008-11-21 00:10:42 +0000806 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000807 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000808 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000809 }
810
Evan Chengd38c22b2006-05-11 23:55:42 +0000811 // Reverse the order if it is bottom up.
812 std::reverse(Sequence.begin(), Sequence.end());
813
Evan Chengd38c22b2006-05-11 23:55:42 +0000814#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000815 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000816#endif
817}
818
819//===----------------------------------------------------------------------===//
820// Top-Down Scheduling
821//===----------------------------------------------------------------------===//
822
823/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000824/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000825void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000826 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000827
Evan Chengd38c22b2006-05-11 23:55:42 +0000828#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000829 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000830 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000831 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000832 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000833 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000834 }
835#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000836 --SuccSU->NumPredsLeft;
837
Dan Gohmanb9543432009-02-10 23:27:53 +0000838 // If all the node's predecessors are scheduled, this node is ready
839 // to be scheduled. Ignore the special ExitSU node.
840 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000841 SuccSU->isAvailable = true;
842 AvailableQueue->push(SuccSU);
843 }
844}
845
Dan Gohmanb9543432009-02-10 23:27:53 +0000846void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
847 // Top down: release successors
848 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
849 I != E; ++I) {
850 assert(!I->isAssignedRegDep() &&
851 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
852
853 ReleaseSucc(SU, &*I);
854 }
855}
856
Evan Chengd38c22b2006-05-11 23:55:42 +0000857/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
858/// count of its successors. If a successor pending count is zero, add it to
859/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000860void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000861 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000862 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000863
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000864 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
865 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000866 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000867
Dan Gohmanb9543432009-02-10 23:27:53 +0000868 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000869 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000870 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000871}
872
Dan Gohman54a187e2007-08-20 19:28:38 +0000873/// ListScheduleTopDown - The main loop of list scheduling for top-down
874/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000875void ScheduleDAGRRList::ListScheduleTopDown() {
876 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000877
Dan Gohmanb9543432009-02-10 23:27:53 +0000878 // Release any successors of the special Entry node.
879 ReleaseSuccessors(&EntrySU);
880
Evan Chengd38c22b2006-05-11 23:55:42 +0000881 // All leaves to Available queue.
882 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
883 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000884 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000885 AvailableQueue->push(&SUnits[i]);
886 SUnits[i].isAvailable = true;
887 }
888 }
889
Evan Chengd38c22b2006-05-11 23:55:42 +0000890 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000891 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000892 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000893 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000894 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000895
Dan Gohmanc602dd42008-11-21 00:10:42 +0000896 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000897 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000898 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000899 }
900
Evan Chengd38c22b2006-05-11 23:55:42 +0000901#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000902 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000903#endif
904}
905
906
Evan Chengd38c22b2006-05-11 23:55:42 +0000907//===----------------------------------------------------------------------===//
908// RegReductionPriorityQueue Implementation
909//===----------------------------------------------------------------------===//
910//
911// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
912// to reduce register pressure.
913//
914namespace {
915 template<class SF>
916 class RegReductionPriorityQueue;
917
918 /// Sorting functions for the Available queue.
919 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
920 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
921 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
922 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
923
924 bool operator()(const SUnit* left, const SUnit* right) const;
925 };
926
927 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
928 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
929 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
930 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
931
932 bool operator()(const SUnit* left, const SUnit* right) const;
933 };
934} // end anonymous namespace
935
Dan Gohman186f65d2008-11-20 03:30:37 +0000936/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
937/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000938static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000939CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000940 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
941 if (SethiUllmanNumber != 0)
942 return SethiUllmanNumber;
943
944 unsigned Extra = 0;
945 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
946 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000947 if (I->isCtrl()) continue; // ignore chain preds
948 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000949 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000950 if (PredSethiUllman > SethiUllmanNumber) {
951 SethiUllmanNumber = PredSethiUllman;
952 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +0000953 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +0000954 ++Extra;
955 }
956
957 SethiUllmanNumber += Extra;
958
959 if (SethiUllmanNumber == 0)
960 SethiUllmanNumber = 1;
961
962 return SethiUllmanNumber;
963}
964
Evan Chengd38c22b2006-05-11 23:55:42 +0000965namespace {
966 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +0000967 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000968 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000969 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000970
Dan Gohman3f656df2008-11-20 02:45:51 +0000971 protected:
972 // SUnits - The SUnits for the current graph.
973 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000974
Dan Gohman3f656df2008-11-20 02:45:51 +0000975 const TargetInstrInfo *TII;
976 const TargetRegisterInfo *TRI;
977 ScheduleDAGRRList *scheduleDAG;
978
Dan Gohman186f65d2008-11-20 03:30:37 +0000979 // SethiUllmanNumbers - The SethiUllman number for each node.
980 std::vector<unsigned> SethiUllmanNumbers;
981
Dan Gohman3f656df2008-11-20 02:45:51 +0000982 public:
983 RegReductionPriorityQueue(const TargetInstrInfo *tii,
984 const TargetRegisterInfo *tri) :
985 Queue(SF(this)), currentQueueId(0),
986 TII(tii), TRI(tri), scheduleDAG(NULL) {}
987
988 void initNodes(std::vector<SUnit> &sunits) {
989 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000990 // Add pseudo dependency edges for two-address nodes.
991 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +0000992 // Reroute edges to nodes with multiple uses.
993 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +0000994 // Calculate node priorities.
995 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +0000996 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000997
Dan Gohman186f65d2008-11-20 03:30:37 +0000998 void addNode(const SUnit *SU) {
999 unsigned SUSize = SethiUllmanNumbers.size();
1000 if (SUnits->size() > SUSize)
1001 SethiUllmanNumbers.resize(SUSize*2, 0);
1002 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1003 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001004
Dan Gohman186f65d2008-11-20 03:30:37 +00001005 void updateNode(const SUnit *SU) {
1006 SethiUllmanNumbers[SU->NodeNum] = 0;
1007 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1008 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001009
Dan Gohman186f65d2008-11-20 03:30:37 +00001010 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001011 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001012 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001013 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001014
1015 unsigned getNodePriority(const SUnit *SU) const {
1016 assert(SU->NodeNum < SethiUllmanNumbers.size());
1017 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001018 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001019 // CopyToReg should be close to its uses to facilitate coalescing and
1020 // avoid spilling.
1021 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001022 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Dan Gohman3027bb62009-04-16 20:57:10 +00001023 Opc == TargetInstrInfo::SUBREG_TO_REG ||
Dan Gohman261ee6b2009-01-07 22:30:55 +00001024 Opc == TargetInstrInfo::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001025 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1026 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001027 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001028 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1029 // If SU does not have a register use, i.e. it doesn't produce a value
1030 // that would be consumed (e.g. store), then it terminates a chain of
1031 // computation. Give it a large SethiUllman number so it will be
1032 // scheduled right before its predecessors that it doesn't lengthen
1033 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001034 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001035 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1036 // If SU does not have a register def, schedule it close to its uses
1037 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001038 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001039 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001040 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001041
1042 unsigned getNodeOrdering(const SUnit *SU) const {
1043 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1044 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001045
Evan Cheng5924bf72007-09-25 01:54:36 +00001046 unsigned size() const { return Queue.size(); }
1047
Evan Chengd38c22b2006-05-11 23:55:42 +00001048 bool empty() const { return Queue.empty(); }
1049
1050 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001051 assert(!U->NodeQueueId && "Node in the queue already");
1052 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001053 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001054 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001055
Evan Chengd38c22b2006-05-11 23:55:42 +00001056 void push_all(const std::vector<SUnit *> &Nodes) {
1057 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001058 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001059 }
1060
1061 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001062 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001063 SUnit *V = Queue.top();
1064 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001065 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001066 return V;
1067 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001068
Evan Cheng5924bf72007-09-25 01:54:36 +00001069 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001070 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001071 assert(SU->NodeQueueId != 0 && "Not in queue!");
1072 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001073 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001074 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001075
1076 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1077 scheduleDAG = scheduleDag;
1078 }
1079
1080 protected:
1081 bool canClobber(const SUnit *SU, const SUnit *Op);
1082 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001083 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001084 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001085 };
1086
Dan Gohman186f65d2008-11-20 03:30:37 +00001087 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1088 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001089
Dan Gohman186f65d2008-11-20 03:30:37 +00001090 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1091 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001092}
1093
Evan Chengb9e3db62007-03-14 22:43:40 +00001094/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001095/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001096static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001097 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001098 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001099 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001100 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001101 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001102 // If there are bunch of CopyToRegs stacked up, they should be considered
1103 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001104 if (I->getSUnit()->getNode() &&
1105 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001106 Height = closestSucc(I->getSUnit())+1;
1107 if (Height > MaxHeight)
1108 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001109 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001110 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001111}
1112
Evan Cheng61bc51e2007-12-20 02:22:36 +00001113/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001114/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001115static unsigned calcMaxScratches(const SUnit *SU) {
1116 unsigned Scratches = 0;
1117 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001118 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001119 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001120 Scratches++;
1121 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001122 return Scratches;
1123}
1124
Evan Chengd38c22b2006-05-11 23:55:42 +00001125// Bottom up
1126bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling0a7056f2010-01-05 23:48:12 +00001127 unsigned LOrder = SPQ->getNodeOrdering(left);
1128 unsigned ROrder = SPQ->getNodeOrdering(right);
1129
1130 // Prefer an ordering where the lower the non-zero order number, the higher
1131 // the preference.
1132 if ((LOrder || ROrder) && LOrder != ROrder)
1133 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1134
Evan Cheng6730f032007-01-08 23:55:53 +00001135 unsigned LPriority = SPQ->getNodePriority(left);
1136 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001137 if (LPriority != RPriority)
1138 return LPriority > RPriority;
1139
1140 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1141 // e.g.
1142 // t1 = op t2, c1
1143 // t3 = op t4, c2
1144 //
1145 // and the following instructions are both ready.
1146 // t2 = op c3
1147 // t4 = op c4
1148 //
1149 // Then schedule t2 = op first.
1150 // i.e.
1151 // t4 = op c4
1152 // t2 = op c3
1153 // t1 = op t2, c1
1154 // t3 = op t4, c2
1155 //
1156 // This creates more short live intervals.
1157 unsigned LDist = closestSucc(left);
1158 unsigned RDist = closestSucc(right);
1159 if (LDist != RDist)
1160 return LDist < RDist;
1161
Evan Cheng3a14efa2009-02-12 08:59:45 +00001162 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001163 unsigned LScratch = calcMaxScratches(left);
1164 unsigned RScratch = calcMaxScratches(right);
1165 if (LScratch != RScratch)
1166 return LScratch > RScratch;
1167
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001168 if (left->getHeight() != right->getHeight())
1169 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001170
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001171 if (left->getDepth() != right->getDepth())
1172 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001173
Roman Levenstein6b371142008-04-29 09:07:59 +00001174 assert(left->NodeQueueId && right->NodeQueueId &&
1175 "NodeQueueId cannot be zero");
1176 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001177}
1178
Dan Gohman3f656df2008-11-20 02:45:51 +00001179template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001180bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001181RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001182 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001183 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001184 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001185 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001186 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001187 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001188 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001189 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001190 if (DU->getNodeId() != -1 &&
1191 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001192 return true;
1193 }
1194 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001195 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001196 return false;
1197}
1198
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001199
Evan Chenga5e595d2007-09-28 22:32:30 +00001200/// hasCopyToRegUse - Return true if SU has a value successor that is a
1201/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001202static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001203 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1204 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001205 if (I->isCtrl()) continue;
1206 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001207 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001208 return true;
1209 }
1210 return false;
1211}
1212
Evan Chengf9891412007-12-20 09:25:31 +00001213/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001214/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001215static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001216 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001217 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001218 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001219 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1220 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001221 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001222 for (const SDNode *SUNode = SU->getNode(); SUNode;
1223 SUNode = SUNode->getFlaggedNode()) {
1224 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001225 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001226 const unsigned *SUImpDefs =
1227 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1228 if (!SUImpDefs)
1229 return false;
1230 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001231 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001232 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001233 continue;
1234 if (!N->hasAnyUseOfValue(i))
1235 continue;
1236 unsigned Reg = ImpDefs[i - NumDefs];
1237 for (;*SUImpDefs; ++SUImpDefs) {
1238 unsigned SUReg = *SUImpDefs;
1239 if (TRI->regsOverlap(Reg, SUReg))
1240 return true;
1241 }
Evan Chengf9891412007-12-20 09:25:31 +00001242 }
1243 }
1244 return false;
1245}
1246
Dan Gohman9a658d72009-03-24 00:49:12 +00001247/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1248/// are not handled well by the general register pressure reduction
1249/// heuristics. When presented with code like this:
1250///
1251/// N
1252/// / |
1253/// / |
1254/// U store
1255/// |
1256/// ...
1257///
1258/// the heuristics tend to push the store up, but since the
1259/// operand of the store has another use (U), this would increase
1260/// the length of that other use (the U->N edge).
1261///
1262/// This function transforms code like the above to route U's
1263/// dependence through the store when possible, like this:
1264///
1265/// N
1266/// ||
1267/// ||
1268/// store
1269/// |
1270/// U
1271/// |
1272/// ...
1273///
1274/// This results in the store being scheduled immediately
1275/// after N, which shortens the U->N live range, reducing
1276/// register pressure.
1277///
1278template<class SF>
1279void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1280 // Visit all the nodes in topological order, working top-down.
1281 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1282 SUnit *SU = &(*SUnits)[i];
1283 // For now, only look at nodes with no data successors, such as stores.
1284 // These are especially important, due to the heuristics in
1285 // getNodePriority for nodes with no data successors.
1286 if (SU->NumSuccs != 0)
1287 continue;
1288 // For now, only look at nodes with exactly one data predecessor.
1289 if (SU->NumPreds != 1)
1290 continue;
1291 // Avoid prescheduling copies to virtual registers, which don't behave
1292 // like other nodes from the perspective of scheduling heuristics.
1293 if (SDNode *N = SU->getNode())
1294 if (N->getOpcode() == ISD::CopyToReg &&
1295 TargetRegisterInfo::isVirtualRegister
1296 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1297 continue;
1298
1299 // Locate the single data predecessor.
1300 SUnit *PredSU = 0;
1301 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1302 EE = SU->Preds.end(); II != EE; ++II)
1303 if (!II->isCtrl()) {
1304 PredSU = II->getSUnit();
1305 break;
1306 }
1307 assert(PredSU);
1308
1309 // Don't rewrite edges that carry physregs, because that requires additional
1310 // support infrastructure.
1311 if (PredSU->hasPhysRegDefs)
1312 continue;
1313 // Short-circuit the case where SU is PredSU's only data successor.
1314 if (PredSU->NumSuccs == 1)
1315 continue;
1316 // Avoid prescheduling to copies from virtual registers, which don't behave
1317 // like other nodes from the perspective of scheduling // heuristics.
1318 if (SDNode *N = SU->getNode())
1319 if (N->getOpcode() == ISD::CopyFromReg &&
1320 TargetRegisterInfo::isVirtualRegister
1321 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1322 continue;
1323
1324 // Perform checks on the successors of PredSU.
1325 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1326 EE = PredSU->Succs.end(); II != EE; ++II) {
1327 SUnit *PredSuccSU = II->getSUnit();
1328 if (PredSuccSU == SU) continue;
1329 // If PredSU has another successor with no data successors, for
1330 // now don't attempt to choose either over the other.
1331 if (PredSuccSU->NumSuccs == 0)
1332 goto outer_loop_continue;
1333 // Don't break physical register dependencies.
1334 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1335 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1336 goto outer_loop_continue;
1337 // Don't introduce graph cycles.
1338 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1339 goto outer_loop_continue;
1340 }
1341
1342 // Ok, the transformation is safe and the heuristics suggest it is
1343 // profitable. Update the graph.
David Greenef34d7ac2010-01-05 01:24:54 +00001344 DEBUG(dbgs() << "Prescheduling SU # " << SU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001345 << " next to PredSU # " << PredSU->NodeNum
1346 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001347 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1348 SDep Edge = PredSU->Succs[i];
1349 assert(!Edge.isAssignedRegDep());
1350 SUnit *SuccSU = Edge.getSUnit();
1351 if (SuccSU != SU) {
1352 Edge.setSUnit(PredSU);
1353 scheduleDAG->RemovePred(SuccSU, Edge);
1354 scheduleDAG->AddPred(SU, Edge);
1355 Edge.setSUnit(SU);
1356 scheduleDAG->AddPred(SuccSU, Edge);
1357 --i;
1358 }
1359 }
1360 outer_loop_continue:;
1361 }
1362}
1363
Evan Chengd38c22b2006-05-11 23:55:42 +00001364/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1365/// it as a def&use operand. Add a pseudo control edge from it to the other
1366/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001367/// first (lower in the schedule). If both nodes are two-address, favor the
1368/// one that has a CopyToReg use (more likely to be a loop induction update).
1369/// If both are two-address, but one is commutable while the other is not
1370/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001371template<class SF>
1372void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001373 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001374 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001375 if (!SU->isTwoAddress)
1376 continue;
1377
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001378 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001379 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001380 continue;
1381
Dan Gohman17059682008-07-17 19:10:17 +00001382 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001383 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001384 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001385 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001386 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001387 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1388 continue;
1389 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1390 if (DU->getNodeId() == -1)
1391 continue;
1392 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1393 if (!DUSU) continue;
1394 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1395 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001396 if (I->isCtrl()) continue;
1397 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001398 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001399 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001400 // Be conservative. Ignore if nodes aren't at roughly the same
1401 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001402 if (SuccSU->getHeight() < SU->getHeight() &&
1403 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001404 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001405 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1406 // constrains whatever is using the copy, instead of the copy
1407 // itself. In the case that the copy is coalesced, this
1408 // preserves the intent of the pseudo two-address heurietics.
1409 while (SuccSU->Succs.size() == 1 &&
1410 SuccSU->getNode()->isMachineOpcode() &&
1411 SuccSU->getNode()->getMachineOpcode() ==
1412 TargetInstrInfo::COPY_TO_REGCLASS)
1413 SuccSU = SuccSU->Succs.front().getSUnit();
1414 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001415 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1416 continue;
1417 // Don't constrain nodes with physical register defs if the
1418 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001419 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001420 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001421 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001422 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001423 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1424 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001425 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1426 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
Dan Gohman3027bb62009-04-16 20:57:10 +00001427 SuccOpc == TargetInstrInfo::INSERT_SUBREG ||
1428 SuccOpc == TargetInstrInfo::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001429 continue;
1430 if ((!canClobber(SuccSU, DUSU) ||
1431 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1432 (!SU->isCommutable && SuccSU->isCommutable)) &&
1433 !scheduleDAG->IsReachable(SuccSU, SU)) {
David Greenef34d7ac2010-01-05 01:24:54 +00001434 DEBUG(dbgs() << "Adding a pseudo-two-addr edge from SU # "
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001435 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001436 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001437 /*Reg=*/0, /*isNormalMemory=*/false,
1438 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001439 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001440 }
1441 }
1442 }
1443 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001444}
1445
Evan Cheng6730f032007-01-08 23:55:53 +00001446/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1447/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001448template<class SF>
1449void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001450 SethiUllmanNumbers.assign(SUnits->size(), 0);
1451
1452 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001453 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001454}
Evan Chengd38c22b2006-05-11 23:55:42 +00001455
Roman Levenstein30d09512008-03-27 09:44:37 +00001456/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001457/// predecessors of the successors of the SUnit SU. Stop when the provided
1458/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001459static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1460 unsigned Limit) {
1461 unsigned Sum = 0;
1462 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1463 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001464 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001465 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1466 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001467 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001468 if (!PredSU->isScheduled)
1469 if (++Sum > Limit)
1470 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001471 }
1472 }
1473 return Sum;
1474}
1475
Evan Chengd38c22b2006-05-11 23:55:42 +00001476
1477// Top down
1478bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001479 unsigned LPriority = SPQ->getNodePriority(left);
1480 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001481 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1482 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001483 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1484 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001485 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1486 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001487
1488 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1489 return false;
1490 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1491 return true;
1492
Evan Chengd38c22b2006-05-11 23:55:42 +00001493 if (LIsFloater)
1494 LBonus -= 2;
1495 if (RIsFloater)
1496 RBonus -= 2;
1497 if (left->NumSuccs == 1)
1498 LBonus += 2;
1499 if (right->NumSuccs == 1)
1500 RBonus += 2;
1501
Evan Cheng73bdf042008-03-01 00:39:47 +00001502 if (LPriority+LBonus != RPriority+RBonus)
1503 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001504
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001505 if (left->getDepth() != right->getDepth())
1506 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001507
1508 if (left->NumSuccsLeft != right->NumSuccsLeft)
1509 return left->NumSuccsLeft > right->NumSuccsLeft;
1510
Roman Levenstein6b371142008-04-29 09:07:59 +00001511 assert(left->NodeQueueId && right->NodeQueueId &&
1512 "NodeQueueId cannot be zero");
1513 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001514}
1515
Evan Chengd38c22b2006-05-11 23:55:42 +00001516//===----------------------------------------------------------------------===//
1517// Public Constructor Functions
1518//===----------------------------------------------------------------------===//
1519
Dan Gohmandfaf6462009-02-11 04:27:20 +00001520llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001521llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001522 const TargetMachine &TM = IS->TM;
1523 const TargetInstrInfo *TII = TM.getInstrInfo();
1524 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001525
Evan Cheng7e4abde2008-07-02 09:23:51 +00001526 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001527
Evan Cheng7e4abde2008-07-02 09:23:51 +00001528 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001529 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001530 PQ->setScheduleDAG(SD);
1531 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001532}
1533
Dan Gohmandfaf6462009-02-11 04:27:20 +00001534llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001535llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001536 const TargetMachine &TM = IS->TM;
1537 const TargetInstrInfo *TII = TM.getInstrInfo();
1538 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001539
1540 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1541
Dan Gohman619ef482009-01-15 19:20:50 +00001542 ScheduleDAGRRList *SD =
1543 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001544 PQ->setScheduleDAG(SD);
1545 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001546}