blob: c3b6137ae36ece4914e8dd7579b04b6c2aa162e7 [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman60cb69e2008-11-19 23:18:57 +000019#include "llvm/CodeGen/ScheduleDAGSDNodes.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Dan Gohmana4db3352008-06-21 18:35:25 +000027#include "llvm/ADT/BitVector.h"
28#include "llvm/ADT/PriorityQueue.h"
Evan Chenge6f92252007-09-27 18:46:06 +000029#include "llvm/ADT/SmallPtrSet.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"
Evan Chengd38c22b2006-05-11 23:55:42 +000033#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include "llvm/Support/CommandLine.h"
35using 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");
40STATISTIC(NumCCCopies, "Number of cross class copies");
41
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 Gohman5a390b92008-11-13 21:21:28 +000077 ScheduleDAGRRList(SelectionDAG *dag, MachineBasicBlock *bb,
Dan Gohmanfd08af42008-11-20 03:11:19 +000078 const TargetMachine &tm, bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000079 SchedulingPriorityQueue *availqueue)
Dan Gohmanfd08af42008-11-20 03:11:19 +000080 : ScheduleDAGSDNodes(dag, bb, tm), 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
95 /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
96 /// 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 Gohman2d170892008-12-09 22:54:47 +0000118 void ReleasePred(SUnit *SU, SDep *PredEdge);
119 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
120 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000121 void ScheduleNodeBottomUp(SUnit*, unsigned);
122 void ScheduleNodeTopDown(SUnit*, unsigned);
123 void UnscheduleNodeBottomUp(SUnit*);
124 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
125 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000126 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +0000127 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000128 const TargetRegisterClass*,
129 SmallVector<SUnit*, 2>&);
130 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000131 void ListScheduleTopDown();
132 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000133 void CommuteNodesToReducePressure();
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() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000167 DOUT << "********** 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
Evan Chengd38c22b2006-05-11 23:55:42 +0000173 // Build scheduling units.
174 BuildSchedUnits();
175
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 Cheng2c977312008-07-01 18:05:03 +0000189
Dan Gohmanfd08af42008-11-20 03:11:19 +0000190 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000191}
192
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000193/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000194/// it is not the last use of its first operand, add it to the CommuteSet if
195/// possible. It will be commuted when it is translated to a MI.
196void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000197 SmallPtrSet<SUnit*, 4> OperandSeen;
Dan Gohman4370f262008-04-15 01:22:18 +0000198 for (unsigned i = Sequence.size(); i != 0; ) {
199 --i;
Evan Chengafed73e2006-05-12 01:58:24 +0000200 SUnit *SU = Sequence[i];
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000201 if (!SU || !SU->getNode()) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000202 if (SU->isCommutable) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000203 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000204 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000205 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000206 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000207 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000208 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000209 continue;
210
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000211 SDNode *OpN = SU->getNode()->getOperand(j).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000212 SUnit *OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000213 if (OpSU && OperandSeen.count(OpSU) == 1) {
214 // Ok, so SU is not the last use of OpSU, but SU is two-address so
215 // it will clobber OpSU. Try to commute SU if no other source operands
216 // are live below.
217 bool DoCommute = true;
218 for (unsigned k = 0; k < NumOps; ++k) {
219 if (k != j) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000220 OpN = SU->getNode()->getOperand(k).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000221 OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000222 if (OpSU && OperandSeen.count(OpSU) == 1) {
223 DoCommute = false;
224 break;
225 }
226 }
Evan Chengafed73e2006-05-12 01:58:24 +0000227 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000228 if (DoCommute)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000229 CommuteSet.insert(SU->getNode());
Evan Chengafed73e2006-05-12 01:58:24 +0000230 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000231
232 // Only look at the first use&def node for now.
233 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000234 }
235 }
236
Chris Lattnerd86418a2006-08-17 00:09:56 +0000237 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
238 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000239 if (!I->isCtrl())
240 OperandSeen.insert(I->getSUnit()->OrigNode);
Evan Chengafed73e2006-05-12 01:58:24 +0000241 }
242 }
243}
Evan Chengd38c22b2006-05-11 23:55:42 +0000244
245//===----------------------------------------------------------------------===//
246// Bottom-Up Scheduling
247//===----------------------------------------------------------------------===//
248
Evan Chengd38c22b2006-05-11 23:55:42 +0000249/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000250/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000251void ScheduleDAGRRList::ReleasePred(SUnit *SU, SDep *PredEdge) {
252 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000253 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000254
255#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000256 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000257 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000258 PredSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000259 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000260 assert(0);
261 }
262#endif
263
Evan Cheng038dcc52007-09-28 19:24:24 +0000264 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000265 PredSU->isAvailable = true;
266 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000267 }
268}
269
270/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
271/// count of its predecessors. If a predecessor pending count is zero, add it to
272/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000273void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000274 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000275 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000276
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000277 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
278 SU->setHeightToAtLeast(CurCycle);
Dan Gohman6e587262008-11-18 21:22:20 +0000279 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000280
281 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000282 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000283 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000284 ReleasePred(SU, &*I);
285 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000286 // This is a physical register dependency and it's impossible or
287 // expensive to copy the register. Make sure nothing that can
288 // clobber the register is scheduled between the predecessor and
289 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000290 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000291 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000292 LiveRegDefs[I->getReg()] = I->getSUnit();
293 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000294 }
295 }
296 }
297
298 // Release all the implicit physical register defs that are live.
299 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
300 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000301 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000302 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000303 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000304 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000305 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000306 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000307 LiveRegDefs[I->getReg()] = NULL;
308 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000309 }
310 }
311 }
312
Evan Chengd38c22b2006-05-11 23:55:42 +0000313 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000314 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000315}
316
Evan Cheng5924bf72007-09-25 01:54:36 +0000317/// CapturePred - This does the opposite of ReleasePred. Since SU is being
318/// unscheduled, incrcease the succ left count of its predecessors. Remove
319/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000320void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
321 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000322 if (PredSU->isAvailable) {
323 PredSU->isAvailable = false;
324 if (!PredSU->isPending)
325 AvailableQueue->remove(PredSU);
326 }
327
Evan Cheng038dcc52007-09-28 19:24:24 +0000328 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000329}
330
331/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
332/// its predecessor states to reflect the change.
333void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000334 DOUT << "*** Unscheduling [" << SU->getHeight() << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000335 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000336
337 AvailableQueue->UnscheduledNode(SU);
338
339 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
340 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000341 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000342 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000343 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000344 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000345 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000346 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000347 LiveRegDefs[I->getReg()] = NULL;
348 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000349 }
350 }
351
352 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
353 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000354 if (I->isAssignedRegDep()) {
355 if (!LiveRegDefs[I->getReg()]) {
356 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000357 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000358 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000359 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
360 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000361 }
362 }
363
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000364 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000365 SU->isScheduled = false;
366 SU->isAvailable = true;
367 AvailableQueue->push(SU);
368}
369
Evan Cheng8e136a92007-09-26 21:36:17 +0000370/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000371/// BTCycle in order to schedule a specific node. Returns the last unscheduled
372/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000373void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
374 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000375 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000376 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000377 OldSU = Sequence.back();
378 Sequence.pop_back();
379 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000380 // Don't try to remove SU from AvailableQueue.
381 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000382 UnscheduleNodeBottomUp(OldSU);
383 --CurCycle;
384 }
385
386
387 if (SU->isSucc(OldSU)) {
388 assert(false && "Something is wrong!");
389 abort();
390 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000391
392 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000393}
394
Evan Cheng5924bf72007-09-25 01:54:36 +0000395/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
396/// successors to the newly created node.
397SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000398 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000399 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000400
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000401 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000402 if (!N)
403 return NULL;
404
405 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000406 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000407 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000408 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000409 if (VT == MVT::Flag)
410 return NULL;
411 else if (VT == MVT::Other)
412 TryUnfold = true;
413 }
Evan Cheng79e97132007-10-05 01:39:18 +0000414 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000415 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000416 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000417 if (VT == MVT::Flag)
418 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000419 }
420
421 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000422 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000423 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000424 return NULL;
425
426 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
427 assert(NewNodes.size() == 2 && "Expected a load folding node!");
428
429 N = NewNodes[1];
430 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000431 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000432 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000433 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000434 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
435 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000436 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000437
Dan Gohmane52e0892008-11-11 21:34:44 +0000438 // LoadNode may already exist. This can happen when there is another
439 // load from the same location and producing the same type of value
440 // but it has different alignment or volatileness.
441 bool isNewLoad = true;
442 SUnit *LoadSU;
443 if (LoadNode->getNodeId() != -1) {
444 LoadSU = &SUnits[LoadNode->getNodeId()];
445 isNewLoad = false;
446 } else {
447 LoadSU = CreateNewSUnit(LoadNode);
448 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000449 ComputeLatency(LoadSU);
450 }
451
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000452 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000453 assert(N->getNodeId() == -1 && "Node already inserted!");
454 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000455
Dan Gohman17059682008-07-17 19:10:17 +0000456 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000457 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000458 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000459 NewSU->isTwoAddress = true;
460 break;
461 }
462 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000463 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000464 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000465 ComputeLatency(NewSU);
466
Dan Gohman2d170892008-12-09 22:54:47 +0000467 SDep ChainPred;
Evan Cheng79e97132007-10-05 01:39:18 +0000468 SmallVector<SDep, 4> ChainSuccs;
469 SmallVector<SDep, 4> LoadPreds;
470 SmallVector<SDep, 4> NodePreds;
471 SmallVector<SDep, 4> NodeSuccs;
472 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
473 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000474 if (I->isCtrl())
475 ChainPred = *I;
476 else if (I->getSUnit()->getNode() &&
477 I->getSUnit()->getNode()->isOperandOf(LoadNode))
478 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000479 else
Dan Gohman2d170892008-12-09 22:54:47 +0000480 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000481 }
482 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
483 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000484 if (I->isCtrl())
485 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000486 else
Dan Gohman2d170892008-12-09 22:54:47 +0000487 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000488 }
489
Dan Gohman2d170892008-12-09 22:54:47 +0000490 if (ChainPred.getSUnit()) {
491 RemovePred(SU, ChainPred);
Dan Gohman4370f262008-04-15 01:22:18 +0000492 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000493 AddPred(LoadSU, ChainPred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000494 }
Evan Cheng79e97132007-10-05 01:39:18 +0000495 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000496 const SDep &Pred = LoadPreds[i];
497 RemovePred(SU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000498 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000499 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000500 }
Evan Cheng79e97132007-10-05 01:39:18 +0000501 }
502 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000503 const SDep &Pred = NodePreds[i];
504 RemovePred(SU, Pred);
505 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000506 }
507 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000508 SDep D = NodeSuccs[i];
509 SUnit *SuccDep = D.getSUnit();
510 D.setSUnit(SU);
511 RemovePred(SuccDep, D);
512 D.setSUnit(NewSU);
513 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000514 }
515 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000516 SDep D = ChainSuccs[i];
517 SUnit *SuccDep = D.getSUnit();
518 D.setSUnit(SU);
519 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000520 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000521 D.setSUnit(LoadSU);
522 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000523 }
Evan Cheng79e97132007-10-05 01:39:18 +0000524 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000525 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000526 AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency));
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000527 }
Evan Cheng79e97132007-10-05 01:39:18 +0000528
Evan Cheng91e0fc92007-12-18 08:42:10 +0000529 if (isNewLoad)
530 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000531 AvailableQueue->addNode(NewSU);
532
533 ++NumUnfolds;
534
535 if (NewSU->NumSuccsLeft == 0) {
536 NewSU->isAvailable = true;
537 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000538 }
539 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000540 }
541
542 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000543 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000544
545 // New SUnit has the exact same predecessors.
546 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
547 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000548 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000549 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000550
551 // Only copy scheduled successors. Cut them from old node's successor
552 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000553 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000554 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
555 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000556 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000557 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000558 SUnit *SuccSU = I->getSUnit();
559 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000560 SDep D = *I;
561 D.setSUnit(NewSU);
562 AddPred(SuccSU, D);
563 D.setSUnit(SU);
564 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000565 }
566 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000567 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000568 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000569
570 AvailableQueue->updateNode(SU);
571 AvailableQueue->addNode(NewSU);
572
Evan Cheng1ec79b42007-09-27 07:09:03 +0000573 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000574 return NewSU;
575}
576
Evan Cheng1ec79b42007-09-27 07:09:03 +0000577/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
578/// and move all scheduled successors of the given SUnit to the last copy.
579void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
580 const TargetRegisterClass *DestRC,
581 const TargetRegisterClass *SrcRC,
582 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000583 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000584 CopyFromSU->CopySrcRC = SrcRC;
585 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000586
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000587 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000588 CopyToSU->CopySrcRC = DestRC;
589 CopyToSU->CopyDstRC = SrcRC;
590
591 // Only copy scheduled successors. Cut them from old node's successor
592 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000593 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000594 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
595 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000596 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000597 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000598 SUnit *SuccSU = I->getSUnit();
599 if (SuccSU->isScheduled) {
600 SDep D = *I;
601 D.setSUnit(CopyToSU);
602 AddPred(SuccSU, D);
603 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000604 }
605 }
606 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000607 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000608 }
609
Dan Gohman2d170892008-12-09 22:54:47 +0000610 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
611 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000612
613 AvailableQueue->updateNode(SU);
614 AvailableQueue->addNode(CopyFromSU);
615 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000616 Copies.push_back(CopyFromSU);
617 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000618
Evan Cheng1ec79b42007-09-27 07:09:03 +0000619 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000620}
621
622/// getPhysicalRegisterVT - Returns the ValueType of the physical register
623/// definition of the specified node.
624/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000625static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
626 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000627 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000628 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000629 unsigned NumRes = TID.getNumDefs();
630 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000631 if (Reg == *ImpDef)
632 break;
633 ++NumRes;
634 }
635 return N->getValueType(NumRes);
636}
637
Evan Cheng5924bf72007-09-25 01:54:36 +0000638/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
639/// scheduling of the given node to satisfy live physical register dependencies.
640/// If the specific node is the last one that's available to schedule, do
641/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000642bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
643 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000644 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000645 return false;
646
Evan Chenge6f92252007-09-27 18:46:06 +0000647 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000648 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000649 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
650 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000651 if (I->isAssignedRegDep()) {
652 unsigned Reg = I->getReg();
653 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) {
Evan Chenge6f92252007-09-27 18:46:06 +0000654 if (RegAdded.insert(Reg))
655 LRegs.push_back(Reg);
656 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000657 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000658 *Alias; ++Alias)
Dan Gohman2d170892008-12-09 22:54:47 +0000659 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) {
Evan Chenge6f92252007-09-27 18:46:06 +0000660 if (RegAdded.insert(*Alias))
661 LRegs.push_back(*Alias);
662 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000663 }
664 }
665
Dan Gohman072734e2008-11-13 23:24:17 +0000666 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
667 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000669 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000670 if (!TID.ImplicitDefs)
671 continue;
672 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000673 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000674 if (RegAdded.insert(*Reg))
675 LRegs.push_back(*Reg);
676 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000677 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000678 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000679 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000680 if (RegAdded.insert(*Alias))
681 LRegs.push_back(*Alias);
682 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000683 }
684 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000685 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000686}
687
Evan Cheng1ec79b42007-09-27 07:09:03 +0000688
Evan Chengd38c22b2006-05-11 23:55:42 +0000689/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
690/// schedulers.
691void ScheduleDAGRRList::ListScheduleBottomUp() {
692 unsigned CurCycle = 0;
693 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000694 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000695 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000696 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
697 RootSU->isAvailable = true;
698 AvailableQueue->push(RootSU);
699 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000700
701 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000702 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000703 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000704 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000705 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000706 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000707 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000708 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000709 SUnit *CurSU = AvailableQueue->pop();
710 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000711 SmallVector<unsigned, 4> LRegs;
712 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
713 break;
714 Delayed = true;
715 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000716
717 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
718 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000719 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000720 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000721
722 // All candidates are delayed due to live physical reg dependencies.
723 // Try backtracking, code duplication, or inserting cross class copies
724 // to resolve it.
725 if (Delayed && !CurSU) {
726 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
727 SUnit *TrySU = NotReady[i];
728 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
729
730 // Try unscheduling up to the point where it's safe to schedule
731 // this node.
732 unsigned LiveCycle = CurCycle;
733 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
734 unsigned Reg = LRegs[j];
735 unsigned LCycle = LiveRegCycles[Reg];
736 LiveCycle = std::min(LiveCycle, LCycle);
737 }
738 SUnit *OldSU = Sequence[LiveCycle];
739 if (!WillCreateCycle(TrySU, OldSU)) {
740 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
741 // Force the current node to be scheduled before the node that
742 // requires the physical reg dep.
743 if (OldSU->isAvailable) {
744 OldSU->isAvailable = false;
745 AvailableQueue->remove(OldSU);
746 }
Dan Gohman2d170892008-12-09 22:54:47 +0000747 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
748 /*Reg=*/0, /*isNormalMemory=*/false,
749 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000750 // If one or more successors has been unscheduled, then the current
751 // node is no longer avaialable. Schedule a successor that's now
752 // available instead.
753 if (!TrySU->isAvailable)
754 CurSU = AvailableQueue->pop();
755 else {
756 CurSU = TrySU;
757 TrySU->isPending = false;
758 NotReady.erase(NotReady.begin()+i);
759 }
760 break;
761 }
762 }
763
764 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +0000765 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +0000766 // "expensive to copy" values to break the dependency. In case even
767 // that doesn't work, insert cross class copies.
768 SUnit *TrySU = NotReady[0];
769 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
770 assert(LRegs.size() == 1 && "Can't handle this yet!");
771 unsigned Reg = LRegs[0];
772 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +0000773 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
774 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000775 // Issue expensive cross register class copies.
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000776 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000777 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +0000778 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000779 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000780 if (!DestRC) {
781 assert(false && "Don't know how to copy this physical register!");
782 abort();
783 }
784 SmallVector<SUnit*, 2> Copies;
785 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
786 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
787 << " to SU #" << Copies.front()->NodeNum << "\n";
Dan Gohman2d170892008-12-09 22:54:47 +0000788 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
789 /*Reg=*/0, /*isMustAlias=*/false,
790 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000791 NewDef = Copies.back();
792 }
793
794 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
795 << " to SU #" << TrySU->NodeNum << "\n";
796 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000797 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
798 /*Reg=*/0, /*isMustAlias=*/false,
799 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000800 TrySU->isAvailable = false;
801 CurSU = NewDef;
802 }
803
804 if (!CurSU) {
805 assert(false && "Unable to resolve live physical register dependencies!");
806 abort();
807 }
808 }
809
Evan Chengd38c22b2006-05-11 23:55:42 +0000810 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000811 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
812 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000813 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000814 if (NotReady[i]->isAvailable)
815 AvailableQueue->push(NotReady[i]);
816 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000817 NotReady.clear();
818
Dan Gohmanc602dd42008-11-21 00:10:42 +0000819 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000820 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000821 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000822 }
823
Evan Chengd38c22b2006-05-11 23:55:42 +0000824 // Reverse the order if it is bottom up.
825 std::reverse(Sequence.begin(), Sequence.end());
826
Evan Chengd38c22b2006-05-11 23:55:42 +0000827#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000828 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000829#endif
830}
831
832//===----------------------------------------------------------------------===//
833// Top-Down Scheduling
834//===----------------------------------------------------------------------===//
835
836/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000837/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000838void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
839 SUnit *SuccSU = SuccEdge->getSUnit();
Evan Cheng038dcc52007-09-28 19:24:24 +0000840 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000841
842#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000843 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000844 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000845 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000846 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000847 assert(0);
848 }
849#endif
850
Evan Cheng038dcc52007-09-28 19:24:24 +0000851 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000852 SuccSU->isAvailable = true;
853 AvailableQueue->push(SuccSU);
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) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000861 DOUT << "*** 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
868 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000869 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
870 I != E; ++I)
Dan Gohman2d170892008-12-09 22:54:47 +0000871 ReleaseSucc(SU, &*I);
Dan Gohman92a36d72008-11-17 21:31:02 +0000872
Evan Chengd38c22b2006-05-11 23:55:42 +0000873 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000874 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000875}
876
Dan Gohman54a187e2007-08-20 19:28:38 +0000877/// ListScheduleTopDown - The main loop of list scheduling for top-down
878/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000879void ScheduleDAGRRList::ListScheduleTopDown() {
880 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000881
882 // 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
Evan Cheng961bbd32007-01-08 23:50:38 +0000937static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000938 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +0000939 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +0000940 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
941}
942
Dan Gohman186f65d2008-11-20 03:30:37 +0000943/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
944/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000945static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000946CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000947 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
948 if (SethiUllmanNumber != 0)
949 return SethiUllmanNumber;
950
951 unsigned Extra = 0;
952 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
953 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000954 if (I->isCtrl()) continue; // ignore chain preds
955 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000956 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000957 if (PredSethiUllman > SethiUllmanNumber) {
958 SethiUllmanNumber = PredSethiUllman;
959 Extra = 0;
Dan Gohman2d170892008-12-09 22:54:47 +0000960 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl())
Evan Cheng7e4abde2008-07-02 09:23:51 +0000961 ++Extra;
962 }
963
964 SethiUllmanNumber += Extra;
965
966 if (SethiUllmanNumber == 0)
967 SethiUllmanNumber = 1;
968
969 return SethiUllmanNumber;
970}
971
Evan Chengd38c22b2006-05-11 23:55:42 +0000972namespace {
973 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000974 class VISIBILITY_HIDDEN RegReductionPriorityQueue
975 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000976 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000977 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000978
Dan Gohman3f656df2008-11-20 02:45:51 +0000979 protected:
980 // SUnits - The SUnits for the current graph.
981 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000982
Dan Gohman3f656df2008-11-20 02:45:51 +0000983 const TargetInstrInfo *TII;
984 const TargetRegisterInfo *TRI;
985 ScheduleDAGRRList *scheduleDAG;
986
Dan Gohman186f65d2008-11-20 03:30:37 +0000987 // SethiUllmanNumbers - The SethiUllman number for each node.
988 std::vector<unsigned> SethiUllmanNumbers;
989
Dan Gohman3f656df2008-11-20 02:45:51 +0000990 public:
991 RegReductionPriorityQueue(const TargetInstrInfo *tii,
992 const TargetRegisterInfo *tri) :
993 Queue(SF(this)), currentQueueId(0),
994 TII(tii), TRI(tri), scheduleDAG(NULL) {}
995
996 void initNodes(std::vector<SUnit> &sunits) {
997 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000998 // Add pseudo dependency edges for two-address nodes.
999 AddPseudoTwoAddrDeps();
1000 // Calculate node priorities.
1001 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001002 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001003
Dan Gohman186f65d2008-11-20 03:30:37 +00001004 void addNode(const SUnit *SU) {
1005 unsigned SUSize = SethiUllmanNumbers.size();
1006 if (SUnits->size() > SUSize)
1007 SethiUllmanNumbers.resize(SUSize*2, 0);
1008 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1009 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001010
Dan Gohman186f65d2008-11-20 03:30:37 +00001011 void updateNode(const SUnit *SU) {
1012 SethiUllmanNumbers[SU->NodeNum] = 0;
1013 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1014 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001015
Dan Gohman186f65d2008-11-20 03:30:37 +00001016 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001017 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001018 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001019 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001020
1021 unsigned getNodePriority(const SUnit *SU) const {
1022 assert(SU->NodeNum < SethiUllmanNumbers.size());
1023 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1024 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1025 // CopyFromReg should be close to its def because it restricts
1026 // allocation choices. But if it is a livein then perhaps we want it
1027 // closer to its uses so it can be coalesced.
1028 return 0xffff;
1029 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1030 // CopyToReg should be close to its uses to facilitate coalescing and
1031 // avoid spilling.
1032 return 0;
1033 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1034 Opc == TargetInstrInfo::INSERT_SUBREG)
1035 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1036 // facilitate coalescing.
1037 return 0;
1038 else if (SU->NumSuccs == 0)
1039 // If SU does not have a use, i.e. it doesn't produce a value that would
1040 // be consumed (e.g. store), then it terminates a chain of computation.
1041 // Give it a large SethiUllman number so it will be scheduled right
1042 // before its predecessors that it doesn't lengthen their live ranges.
1043 return 0xffff;
1044 else if (SU->NumPreds == 0)
1045 // If SU does not have a def, schedule it close to its uses because it
1046 // does not lengthen any live ranges.
1047 return 0;
1048 else
1049 return SethiUllmanNumbers[SU->NodeNum];
1050 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001051
Evan Cheng5924bf72007-09-25 01:54:36 +00001052 unsigned size() const { return Queue.size(); }
1053
Evan Chengd38c22b2006-05-11 23:55:42 +00001054 bool empty() const { return Queue.empty(); }
1055
1056 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001057 assert(!U->NodeQueueId && "Node in the queue already");
1058 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001059 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001060 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001061
Evan Chengd38c22b2006-05-11 23:55:42 +00001062 void push_all(const std::vector<SUnit *> &Nodes) {
1063 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001064 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001065 }
1066
1067 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001068 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001069 SUnit *V = Queue.top();
1070 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001071 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001072 return V;
1073 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001074
Evan Cheng5924bf72007-09-25 01:54:36 +00001075 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001076 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001077 assert(SU->NodeQueueId != 0 && "Not in queue!");
1078 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001079 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001080 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001081
1082 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1083 scheduleDAG = scheduleDag;
1084 }
1085
1086 protected:
1087 bool canClobber(const SUnit *SU, const SUnit *Op);
1088 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001089 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001090 };
1091
Dan Gohman186f65d2008-11-20 03:30:37 +00001092 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1093 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001094
Dan Gohman186f65d2008-11-20 03:30:37 +00001095 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1096 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001097}
1098
Evan Chengb9e3db62007-03-14 22:43:40 +00001099/// closestSucc - Returns the scheduled cycle of the successor which is
1100/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001101static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001102 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001103 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001104 I != E; ++I) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001105 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001106 // If there are bunch of CopyToRegs stacked up, they should be considered
1107 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001108 if (I->getSUnit()->getNode() &&
1109 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001110 Height = closestSucc(I->getSUnit())+1;
1111 if (Height > MaxHeight)
1112 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001113 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001114 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001115}
1116
Evan Cheng61bc51e2007-12-20 02:22:36 +00001117/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1118/// for scratch registers. Live-in operands and live-out results don't count
1119/// since they are "fixed".
1120static unsigned calcMaxScratches(const SUnit *SU) {
1121 unsigned Scratches = 0;
1122 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1123 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001124 if (I->isCtrl()) continue; // ignore chain preds
1125 if (!I->getSUnit()->getNode() ||
1126 I->getSUnit()->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001127 Scratches++;
1128 }
1129 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1130 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001131 if (I->isCtrl()) continue; // ignore chain succs
1132 if (!I->getSUnit()->getNode() ||
1133 I->getSUnit()->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001134 Scratches += 10;
1135 }
1136 return Scratches;
1137}
1138
Evan Chengd38c22b2006-05-11 23:55:42 +00001139// Bottom up
1140bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001141 unsigned LPriority = SPQ->getNodePriority(left);
1142 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001143 if (LPriority != RPriority)
1144 return LPriority > RPriority;
1145
1146 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1147 // e.g.
1148 // t1 = op t2, c1
1149 // t3 = op t4, c2
1150 //
1151 // and the following instructions are both ready.
1152 // t2 = op c3
1153 // t4 = op c4
1154 //
1155 // Then schedule t2 = op first.
1156 // i.e.
1157 // t4 = op c4
1158 // t2 = op c3
1159 // t1 = op t2, c1
1160 // t3 = op t4, c2
1161 //
1162 // This creates more short live intervals.
1163 unsigned LDist = closestSucc(left);
1164 unsigned RDist = closestSucc(right);
1165 if (LDist != RDist)
1166 return LDist < RDist;
1167
1168 // Intuitively, it's good to push down instructions whose results are
1169 // liveout so their long live ranges won't conflict with other values
1170 // which are needed inside the BB. Further prioritize liveout instructions
1171 // by the number of operands which are calculated within the BB.
1172 unsigned LScratch = calcMaxScratches(left);
1173 unsigned RScratch = calcMaxScratches(right);
1174 if (LScratch != RScratch)
1175 return LScratch > RScratch;
1176
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001177 if (left->getHeight() != right->getHeight())
1178 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001179
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001180 if (left->getDepth() != right->getDepth())
1181 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001182
Roman Levenstein6b371142008-04-29 09:07:59 +00001183 assert(left->NodeQueueId && right->NodeQueueId &&
1184 "NodeQueueId cannot be zero");
1185 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001186}
1187
Dan Gohman3f656df2008-11-20 02:45:51 +00001188template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001189bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001190RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001191 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001192 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001193 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001194 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001195 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001196 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001197 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001198 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001199 if (DU->getNodeId() != -1 &&
1200 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001201 return true;
1202 }
1203 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001204 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001205 return false;
1206}
1207
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001208
Evan Chenga5e595d2007-09-28 22:32:30 +00001209/// hasCopyToRegUse - Return true if SU has a value successor that is a
1210/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001211static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001212 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1213 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001214 if (I->isCtrl()) continue;
1215 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001216 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001217 return true;
1218 }
1219 return false;
1220}
1221
Evan Chengf9891412007-12-20 09:25:31 +00001222/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001223/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001224static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001225 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001226 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001227 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001228 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1229 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001230 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001231 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001232 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001233 if (!SUImpDefs)
1234 return false;
1235 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001236 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001237 if (VT == MVT::Flag || VT == MVT::Other)
1238 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001239 if (!N->hasAnyUseOfValue(i))
1240 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001241 unsigned Reg = ImpDefs[i - NumDefs];
1242 for (;*SUImpDefs; ++SUImpDefs) {
1243 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001244 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001245 return true;
1246 }
1247 }
1248 return false;
1249}
1250
Evan Chengd38c22b2006-05-11 23:55:42 +00001251/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1252/// it as a def&use operand. Add a pseudo control edge from it to the other
1253/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001254/// first (lower in the schedule). If both nodes are two-address, favor the
1255/// one that has a CopyToReg use (more likely to be a loop induction update).
1256/// If both are two-address, but one is commutable while the other is not
1257/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001258template<class SF>
1259void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001260 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001261 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001262 if (!SU->isTwoAddress)
1263 continue;
1264
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001265 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001266 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001267 continue;
1268
Dan Gohman17059682008-07-17 19:10:17 +00001269 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001270 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001271 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001272 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001273 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001274 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1275 continue;
1276 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1277 if (DU->getNodeId() == -1)
1278 continue;
1279 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1280 if (!DUSU) continue;
1281 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1282 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001283 if (I->isCtrl()) continue;
1284 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001285 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001286 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001287 // Be conservative. Ignore if nodes aren't at roughly the same
1288 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001289 if (SuccSU->getHeight() < SU->getHeight() &&
1290 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001291 continue;
1292 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1293 continue;
1294 // Don't constrain nodes with physical register defs if the
1295 // predecessor can clobber them.
1296 if (SuccSU->hasPhysRegDefs) {
1297 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001298 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001299 }
1300 // Don't constraint extract_subreg / insert_subreg these may be
1301 // coalesced away. We don't them close to their uses.
1302 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1303 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1304 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1305 continue;
1306 if ((!canClobber(SuccSU, DUSU) ||
1307 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1308 (!SU->isCommutable && SuccSU->isCommutable)) &&
1309 !scheduleDAG->IsReachable(SuccSU, SU)) {
Dan Gohman30cad9c2008-12-04 02:14:57 +00001310 DOUT << "Adding a pseudo-two-addr edge from SU # " << SU->NodeNum
Dan Gohman82016c22008-11-19 02:00:32 +00001311 << " to SU #" << SuccSU->NodeNum << "\n";
Dan Gohman2d170892008-12-09 22:54:47 +00001312 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/1,
1313 /*Reg=*/0, /*isMustAlias=*/false,
1314 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001315 }
1316 }
1317 }
1318 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001319}
1320
Evan Cheng6730f032007-01-08 23:55:53 +00001321/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1322/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001323template<class SF>
1324void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001325 SethiUllmanNumbers.assign(SUnits->size(), 0);
1326
1327 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001328 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001329}
Evan Chengd38c22b2006-05-11 23:55:42 +00001330
Roman Levenstein30d09512008-03-27 09:44:37 +00001331/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001332/// predecessors of the successors of the SUnit SU. Stop when the provided
1333/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001334static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1335 unsigned Limit) {
1336 unsigned Sum = 0;
1337 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1338 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001339 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001340 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1341 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001342 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001343 if (!PredSU->isScheduled)
1344 if (++Sum > Limit)
1345 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001346 }
1347 }
1348 return Sum;
1349}
1350
Evan Chengd38c22b2006-05-11 23:55:42 +00001351
1352// Top down
1353bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001354 unsigned LPriority = SPQ->getNodePriority(left);
1355 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001356 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1357 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001358 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1359 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001360 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1361 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001362
1363 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1364 return false;
1365 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1366 return true;
1367
Evan Chengd38c22b2006-05-11 23:55:42 +00001368 if (LIsFloater)
1369 LBonus -= 2;
1370 if (RIsFloater)
1371 RBonus -= 2;
1372 if (left->NumSuccs == 1)
1373 LBonus += 2;
1374 if (right->NumSuccs == 1)
1375 RBonus += 2;
1376
Evan Cheng73bdf042008-03-01 00:39:47 +00001377 if (LPriority+LBonus != RPriority+RBonus)
1378 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001379
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001380 if (left->getDepth() != right->getDepth())
1381 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001382
1383 if (left->NumSuccsLeft != right->NumSuccsLeft)
1384 return left->NumSuccsLeft > right->NumSuccsLeft;
1385
Roman Levenstein6b371142008-04-29 09:07:59 +00001386 assert(left->NodeQueueId && right->NodeQueueId &&
1387 "NodeQueueId cannot be zero");
1388 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001389}
1390
Evan Chengd38c22b2006-05-11 23:55:42 +00001391//===----------------------------------------------------------------------===//
1392// Public Constructor Functions
1393//===----------------------------------------------------------------------===//
1394
Jim Laskey03593f72006-08-01 18:29:48 +00001395llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1396 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001397 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001398 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001399 bool) {
Dan Gohman5499e892008-11-11 17:50:47 +00001400 const TargetInstrInfo *TII = TM->getInstrInfo();
1401 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001402
Evan Cheng7e4abde2008-07-02 09:23:51 +00001403 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001404
Evan Cheng7e4abde2008-07-02 09:23:51 +00001405 ScheduleDAGRRList *SD =
Dan Gohmanfd08af42008-11-20 03:11:19 +00001406 new ScheduleDAGRRList(DAG, BB, *TM, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001407 PQ->setScheduleDAG(SD);
1408 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001409}
1410
Jim Laskey03593f72006-08-01 18:29:48 +00001411llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1412 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001413 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001414 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001415 bool) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001416 const TargetInstrInfo *TII = TM->getInstrInfo();
1417 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
1418
1419 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1420
Dan Gohmanfd08af42008-11-20 03:11:19 +00001421 ScheduleDAGRRList *SD = new ScheduleDAGRRList(DAG, BB, *TM, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001422 PQ->setScheduleDAG(SD);
1423 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001424}