blob: 5cbffc7c26cbc4d74552d99be74e7ff3629cf925 [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
101 /// AddPred - This adds the specified node X as a predecessor of
102 /// the current node Y if not already.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000103 /// This returns true if this is a new predecessor.
104 /// Updates the topological ordering if required.
Dan Gohman67b35bd2008-11-21 02:18:56 +0000105 bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isArtificial,
Dan Gohmanad2134d2008-11-25 00:52:40 +0000106 unsigned PhyReg = 0, int Cost = 1) {
107 Topo.AddPred(Y, X);
108 return Y->addPred(X, isCtrl, isArtificial, PhyReg, Cost);
109 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000110
Roman Levenstein733a4d62008-03-26 11:23:38 +0000111 /// RemovePred - This removes the specified node N from the predecessors of
112 /// the current node M. Updates the topological ordering if required.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000113 bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isArtificial) {
114 Topo.RemovePred(M, N);
115 return M->removePred(N, isCtrl, isArtificial, false);
116 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000117
Evan Chengd38c22b2006-05-11 23:55:42 +0000118private:
Dan Gohman5ebdb982008-11-18 00:38:59 +0000119 void ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain);
120 void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain);
Evan Cheng8e136a92007-09-26 21:36:17 +0000121 void CapturePred(SUnit*, SUnit*, bool);
122 void ScheduleNodeBottomUp(SUnit*, unsigned);
123 void ScheduleNodeTopDown(SUnit*, unsigned);
124 void UnscheduleNodeBottomUp(SUnit*);
125 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
126 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000127 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +0000128 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000129 const TargetRegisterClass*,
130 SmallVector<SUnit*, 2>&);
131 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000132 void ListScheduleTopDown();
133 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000134 void CommuteNodesToReducePressure();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000135
136
137 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000138 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000139 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000140 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000141 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000142 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000143 if (NewNode->NodeNum >= NumSUnits)
144 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000145 return NewNode;
146 }
147
Roman Levenstein733a4d62008-03-26 11:23:38 +0000148 /// CreateClone - Creates a new SUnit from an existing one.
149 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000151 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000152 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000153 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000154 if (NewNode->NodeNum >= NumSUnits)
155 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156 return NewNode;
157 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000158};
159} // end anonymous namespace
160
161
162/// Schedule - Schedule the DAG using list scheduling.
163void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000164 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000165
Dan Gohmanc07f6862008-09-23 18:50:48 +0000166 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000167 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
168 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000169
Evan Chengd38c22b2006-05-11 23:55:42 +0000170 // Build scheduling units.
171 BuildSchedUnits();
172
Evan Chengd38c22b2006-05-11 23:55:42 +0000173 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000174 SUnits[su].dumpAll(this));
Dan Gohmanfd08af42008-11-20 03:11:19 +0000175 CalculateDepths();
176 CalculateHeights();
Dan Gohmanad2134d2008-11-25 00:52:40 +0000177 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000178
Dan Gohman46520a22008-06-21 19:18:17 +0000179 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000180
Evan Chengd38c22b2006-05-11 23:55:42 +0000181 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
182 if (isBottomUp)
183 ListScheduleBottomUp();
184 else
185 ListScheduleTopDown();
186
187 AvailableQueue->releaseState();
Evan Cheng2c977312008-07-01 18:05:03 +0000188
Dan Gohmanfd08af42008-11-20 03:11:19 +0000189 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000190}
191
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000192/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000193/// it is not the last use of its first operand, add it to the CommuteSet if
194/// possible. It will be commuted when it is translated to a MI.
195void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000196 SmallPtrSet<SUnit*, 4> OperandSeen;
Dan Gohman4370f262008-04-15 01:22:18 +0000197 for (unsigned i = Sequence.size(); i != 0; ) {
198 --i;
Evan Chengafed73e2006-05-12 01:58:24 +0000199 SUnit *SU = Sequence[i];
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000200 if (!SU || !SU->getNode()) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000201 if (SU->isCommutable) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000202 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000203 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000204 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000205 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000206 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000207 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000208 continue;
209
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000210 SDNode *OpN = SU->getNode()->getOperand(j).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000211 SUnit *OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000212 if (OpSU && OperandSeen.count(OpSU) == 1) {
213 // Ok, so SU is not the last use of OpSU, but SU is two-address so
214 // it will clobber OpSU. Try to commute SU if no other source operands
215 // are live below.
216 bool DoCommute = true;
217 for (unsigned k = 0; k < NumOps; ++k) {
218 if (k != j) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000219 OpN = SU->getNode()->getOperand(k).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000220 OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000221 if (OpSU && OperandSeen.count(OpSU) == 1) {
222 DoCommute = false;
223 break;
224 }
225 }
Evan Chengafed73e2006-05-12 01:58:24 +0000226 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000227 if (DoCommute)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000228 CommuteSet.insert(SU->getNode());
Evan Chengafed73e2006-05-12 01:58:24 +0000229 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000230
231 // Only look at the first use&def node for now.
232 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000233 }
234 }
235
Chris Lattnerd86418a2006-08-17 00:09:56 +0000236 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
237 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000238 if (!I->isCtrl)
Dan Gohmane6e13482008-06-21 15:52:51 +0000239 OperandSeen.insert(I->Dep->OrigNode);
Evan Chengafed73e2006-05-12 01:58:24 +0000240 }
241 }
242}
Evan Chengd38c22b2006-05-11 23:55:42 +0000243
244//===----------------------------------------------------------------------===//
245// Bottom-Up Scheduling
246//===----------------------------------------------------------------------===//
247
Evan Chengd38c22b2006-05-11 23:55:42 +0000248/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000249/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman5ebdb982008-11-18 00:38:59 +0000250void ScheduleDAGRRList::ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000251 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000252
253#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000254 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000255 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000256 PredSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000257 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000258 assert(0);
259 }
260#endif
261
Evan Cheng038dcc52007-09-28 19:24:24 +0000262 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000263 PredSU->isAvailable = true;
264 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000265 }
266}
267
268/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
269/// count of its predecessors. If a predecessor pending count is zero, add it to
270/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000271void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000272 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000273 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000274
Dan Gohman6e587262008-11-18 21:22:20 +0000275 SU->Cycle = CurCycle;
276 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000277
278 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000279 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000280 I != E; ++I) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000281 ReleasePred(SU, I->Dep, I->isCtrl);
Evan Cheng5924bf72007-09-25 01:54:36 +0000282 if (I->Cost < 0) {
283 // This is a physical register dependency and it's impossible or
284 // expensive to copy the register. Make sure nothing that can
285 // clobber the register is scheduled between the predecessor and
286 // this node.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000287 if (!LiveRegDefs[I->Reg]) {
288 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000289 LiveRegDefs[I->Reg] = I->Dep;
290 LiveRegCycles[I->Reg] = CurCycle;
291 }
292 }
293 }
294
295 // Release all the implicit physical register defs that are live.
296 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
297 I != E; ++I) {
298 if (I->Cost < 0) {
299 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000300 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000301 assert(LiveRegDefs[I->Reg] == SU &&
302 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000303 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000304 LiveRegDefs[I->Reg] = NULL;
305 LiveRegCycles[I->Reg] = 0;
306 }
307 }
308 }
309
Evan Chengd38c22b2006-05-11 23:55:42 +0000310 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000311 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000312}
313
Evan Cheng5924bf72007-09-25 01:54:36 +0000314/// CapturePred - This does the opposite of ReleasePred. Since SU is being
315/// unscheduled, incrcease the succ left count of its predecessors. Remove
316/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000317void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000318 if (PredSU->isAvailable) {
319 PredSU->isAvailable = false;
320 if (!PredSU->isPending)
321 AvailableQueue->remove(PredSU);
322 }
323
Evan Cheng038dcc52007-09-28 19:24:24 +0000324 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000325}
326
327/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
328/// its predecessor states to reflect the change.
329void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
330 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000331 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000332
333 AvailableQueue->UnscheduledNode(SU);
334
335 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
336 I != E; ++I) {
337 CapturePred(I->Dep, SU, I->isCtrl);
338 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000339 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000340 assert(LiveRegDefs[I->Reg] == I->Dep &&
341 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000342 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000343 LiveRegDefs[I->Reg] = NULL;
344 LiveRegCycles[I->Reg] = 0;
345 }
346 }
347
348 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
349 I != E; ++I) {
350 if (I->Cost < 0) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000351 if (!LiveRegDefs[I->Reg]) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000352 LiveRegDefs[I->Reg] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000353 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000354 }
355 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
356 LiveRegCycles[I->Reg] = I->Dep->Cycle;
357 }
358 }
359
360 SU->Cycle = 0;
361 SU->isScheduled = false;
362 SU->isAvailable = true;
363 AvailableQueue->push(SU);
364}
365
Evan Cheng8e136a92007-09-26 21:36:17 +0000366/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000367/// BTCycle in order to schedule a specific node. Returns the last unscheduled
368/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000369void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
370 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000371 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000372 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000373 OldSU = Sequence.back();
374 Sequence.pop_back();
375 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000376 // Don't try to remove SU from AvailableQueue.
377 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000378 UnscheduleNodeBottomUp(OldSU);
379 --CurCycle;
380 }
381
382
383 if (SU->isSucc(OldSU)) {
384 assert(false && "Something is wrong!");
385 abort();
386 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000387
388 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000389}
390
Evan Cheng5924bf72007-09-25 01:54:36 +0000391/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
392/// successors to the newly created node.
393SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000394 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000395 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000396
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000397 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000398 if (!N)
399 return NULL;
400
401 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000402 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000403 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000404 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000405 if (VT == MVT::Flag)
406 return NULL;
407 else if (VT == MVT::Other)
408 TryUnfold = true;
409 }
Evan Cheng79e97132007-10-05 01:39:18 +0000410 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000411 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000412 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000413 if (VT == MVT::Flag)
414 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000415 }
416
417 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000418 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000419 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000420 return NULL;
421
422 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
423 assert(NewNodes.size() == 2 && "Expected a load folding node!");
424
425 N = NewNodes[1];
426 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000427 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000428 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000429 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000430 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
431 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000432 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000433
Dan Gohmane52e0892008-11-11 21:34:44 +0000434 // LoadNode may already exist. This can happen when there is another
435 // load from the same location and producing the same type of value
436 // but it has different alignment or volatileness.
437 bool isNewLoad = true;
438 SUnit *LoadSU;
439 if (LoadNode->getNodeId() != -1) {
440 LoadSU = &SUnits[LoadNode->getNodeId()];
441 isNewLoad = false;
442 } else {
443 LoadSU = CreateNewSUnit(LoadNode);
444 LoadNode->setNodeId(LoadSU->NodeNum);
445
446 LoadSU->Depth = SU->Depth;
447 LoadSU->Height = SU->Height;
448 ComputeLatency(LoadSU);
449 }
450
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000451 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000452 assert(N->getNodeId() == -1 && "Node already inserted!");
453 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000454
Dan Gohman17059682008-07-17 19:10:17 +0000455 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000456 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000457 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000458 NewSU->isTwoAddress = true;
459 break;
460 }
461 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000462 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000463 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000464 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000465 NewSU->Depth = SU->Depth;
466 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000467 ComputeLatency(NewSU);
468
469 SUnit *ChainPred = NULL;
470 SmallVector<SDep, 4> ChainSuccs;
471 SmallVector<SDep, 4> LoadPreds;
472 SmallVector<SDep, 4> NodePreds;
473 SmallVector<SDep, 4> NodeSuccs;
474 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
475 I != E; ++I) {
476 if (I->isCtrl)
477 ChainPred = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000478 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Dan Gohmanf00cef42008-11-21 02:27:52 +0000479 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false, false));
Evan Cheng79e97132007-10-05 01:39:18 +0000480 else
Dan Gohmanf00cef42008-11-21 02:27:52 +0000481 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false, false));
Evan Cheng79e97132007-10-05 01:39:18 +0000482 }
483 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
484 I != E; ++I) {
485 if (I->isCtrl)
486 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
Dan Gohmanf00cef42008-11-21 02:27:52 +0000487 I->isCtrl, I->isArtificial, I->isAntiDep));
Evan Cheng79e97132007-10-05 01:39:18 +0000488 else
489 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
Dan Gohmanf00cef42008-11-21 02:27:52 +0000490 I->isCtrl, I->isArtificial, I->isAntiDep));
Evan Cheng79e97132007-10-05 01:39:18 +0000491 }
492
Dan Gohman4370f262008-04-15 01:22:18 +0000493 if (ChainPred) {
494 RemovePred(SU, ChainPred, true, false);
495 if (isNewLoad)
496 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000497 }
Evan Cheng79e97132007-10-05 01:39:18 +0000498 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
499 SDep *Pred = &LoadPreds[i];
Dan Gohman67b35bd2008-11-21 02:18:56 +0000500 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isArtificial);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000501 if (isNewLoad) {
Dan Gohman67b35bd2008-11-21 02:18:56 +0000502 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isArtificial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000503 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000504 }
Evan Cheng79e97132007-10-05 01:39:18 +0000505 }
506 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
507 SDep *Pred = &NodePreds[i];
Dan Gohman67b35bd2008-11-21 02:18:56 +0000508 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isArtificial);
509 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isArtificial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000510 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000511 }
512 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
513 SDep *Succ = &NodeSuccs[i];
Dan Gohman67b35bd2008-11-21 02:18:56 +0000514 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isArtificial);
515 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isArtificial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000516 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000517 }
518 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
519 SDep *Succ = &ChainSuccs[i];
Dan Gohman67b35bd2008-11-21 02:18:56 +0000520 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isArtificial);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000521 if (isNewLoad) {
Dan Gohman67b35bd2008-11-21 02:18:56 +0000522 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isArtificial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000523 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000524 }
Evan Cheng79e97132007-10-05 01:39:18 +0000525 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000526 if (isNewLoad) {
527 AddPred(NewSU, LoadSU, false, false);
528 }
Evan Cheng79e97132007-10-05 01:39:18 +0000529
Evan Cheng91e0fc92007-12-18 08:42:10 +0000530 if (isNewLoad)
531 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000532 AvailableQueue->addNode(NewSU);
533
534 ++NumUnfolds;
535
536 if (NewSU->NumSuccsLeft == 0) {
537 NewSU->isAvailable = true;
538 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000539 }
540 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000541 }
542
543 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000544 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000545
546 // New SUnit has the exact same predecessors.
547 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
548 I != E; ++I)
Dan Gohman67b35bd2008-11-21 02:18:56 +0000549 if (!I->isArtificial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000550 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000551 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
552 }
553
554 // Only copy scheduled successors. Cut them from old node's successor
555 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000556 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000557 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
558 I != E; ++I) {
Dan Gohman67b35bd2008-11-21 02:18:56 +0000559 if (I->isArtificial)
Evan Cheng5924bf72007-09-25 01:54:36 +0000560 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000561 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000562 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000563 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000564 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000565 }
566 }
567 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000568 SUnit *Succ = DelDeps[i].first;
569 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000570 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000571 }
572
573 AvailableQueue->updateNode(SU);
574 AvailableQueue->addNode(NewSU);
575
Evan Cheng1ec79b42007-09-27 07:09:03 +0000576 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000577 return NewSU;
578}
579
Evan Cheng1ec79b42007-09-27 07:09:03 +0000580/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
581/// and move all scheduled successors of the given SUnit to the last copy.
582void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
583 const TargetRegisterClass *DestRC,
584 const TargetRegisterClass *SrcRC,
585 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000586 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000587 CopyFromSU->CopySrcRC = SrcRC;
588 CopyFromSU->CopyDstRC = DestRC;
589 CopyFromSU->Depth = SU->Depth;
590 CopyFromSU->Height = SU->Height;
591
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000592 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000593 CopyToSU->CopySrcRC = DestRC;
594 CopyToSU->CopyDstRC = SrcRC;
595
596 // Only copy scheduled successors. Cut them from old node's successor
597 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000598 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000599 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
600 I != E; ++I) {
Dan Gohman67b35bd2008-11-21 02:18:56 +0000601 if (I->isArtificial)
Evan Cheng8e136a92007-09-26 21:36:17 +0000602 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000603 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000604 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000605 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000606 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000607 }
608 }
609 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000610 SUnit *Succ = DelDeps[i].first;
611 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000612 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000613 }
614
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000615 AddPred(CopyFromSU, SU, false, false, Reg, -1);
616 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000617
618 AvailableQueue->updateNode(SU);
619 AvailableQueue->addNode(CopyFromSU);
620 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000621 Copies.push_back(CopyFromSU);
622 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000623
Evan Cheng1ec79b42007-09-27 07:09:03 +0000624 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000625}
626
627/// getPhysicalRegisterVT - Returns the ValueType of the physical register
628/// definition of the specified node.
629/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000630static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
631 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000632 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000633 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000634 unsigned NumRes = TID.getNumDefs();
635 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000636 if (Reg == *ImpDef)
637 break;
638 ++NumRes;
639 }
640 return N->getValueType(NumRes);
641}
642
Evan Cheng5924bf72007-09-25 01:54:36 +0000643/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
644/// scheduling of the given node to satisfy live physical register dependencies.
645/// If the specific node is the last one that's available to schedule, do
646/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000647bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
648 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000649 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000650 return false;
651
Evan Chenge6f92252007-09-27 18:46:06 +0000652 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000653 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000654 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
655 I != E; ++I) {
656 if (I->Cost < 0) {
657 unsigned Reg = I->Reg;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000658 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000659 if (RegAdded.insert(Reg))
660 LRegs.push_back(Reg);
661 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000662 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000663 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000664 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000665 if (RegAdded.insert(*Alias))
666 LRegs.push_back(*Alias);
667 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 }
669 }
670
Dan Gohman072734e2008-11-13 23:24:17 +0000671 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
672 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000673 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000674 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000675 if (!TID.ImplicitDefs)
676 continue;
677 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000678 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000679 if (RegAdded.insert(*Reg))
680 LRegs.push_back(*Reg);
681 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000682 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000683 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000684 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000685 if (RegAdded.insert(*Alias))
686 LRegs.push_back(*Alias);
687 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000688 }
689 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000690 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000691}
692
Evan Cheng1ec79b42007-09-27 07:09:03 +0000693
Evan Chengd38c22b2006-05-11 23:55:42 +0000694/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
695/// schedulers.
696void ScheduleDAGRRList::ListScheduleBottomUp() {
697 unsigned CurCycle = 0;
698 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000699 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000700 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000701 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
702 RootSU->isAvailable = true;
703 AvailableQueue->push(RootSU);
704 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000705
706 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000707 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000708 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000709 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000710 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000711 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000712 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000713 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000714 SUnit *CurSU = AvailableQueue->pop();
715 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000716 SmallVector<unsigned, 4> LRegs;
717 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
718 break;
719 Delayed = true;
720 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000721
722 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
723 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000724 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000725 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000726
727 // All candidates are delayed due to live physical reg dependencies.
728 // Try backtracking, code duplication, or inserting cross class copies
729 // to resolve it.
730 if (Delayed && !CurSU) {
731 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
732 SUnit *TrySU = NotReady[i];
733 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
734
735 // Try unscheduling up to the point where it's safe to schedule
736 // this node.
737 unsigned LiveCycle = CurCycle;
738 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
739 unsigned Reg = LRegs[j];
740 unsigned LCycle = LiveRegCycles[Reg];
741 LiveCycle = std::min(LiveCycle, LCycle);
742 }
743 SUnit *OldSU = Sequence[LiveCycle];
744 if (!WillCreateCycle(TrySU, OldSU)) {
745 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
746 // Force the current node to be scheduled before the node that
747 // requires the physical reg dep.
748 if (OldSU->isAvailable) {
749 OldSU->isAvailable = false;
750 AvailableQueue->remove(OldSU);
751 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000752 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000753 // If one or more successors has been unscheduled, then the current
754 // node is no longer avaialable. Schedule a successor that's now
755 // available instead.
756 if (!TrySU->isAvailable)
757 CurSU = AvailableQueue->pop();
758 else {
759 CurSU = TrySU;
760 TrySU->isPending = false;
761 NotReady.erase(NotReady.begin()+i);
762 }
763 break;
764 }
765 }
766
767 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +0000768 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +0000769 // "expensive to copy" values to break the dependency. In case even
770 // that doesn't work, insert cross class copies.
771 SUnit *TrySU = NotReady[0];
772 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
773 assert(LRegs.size() == 1 && "Can't handle this yet!");
774 unsigned Reg = LRegs[0];
775 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +0000776 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
777 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000778 // Issue expensive cross register class copies.
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000779 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000780 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +0000781 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000782 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000783 if (!DestRC) {
784 assert(false && "Don't know how to copy this physical register!");
785 abort();
786 }
787 SmallVector<SUnit*, 2> Copies;
788 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
789 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
790 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000791 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000792 NewDef = Copies.back();
793 }
794
795 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
796 << " to SU #" << TrySU->NodeNum << "\n";
797 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000798 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000799 TrySU->isAvailable = false;
800 CurSU = NewDef;
801 }
802
803 if (!CurSU) {
804 assert(false && "Unable to resolve live physical register dependencies!");
805 abort();
806 }
807 }
808
Evan Chengd38c22b2006-05-11 23:55:42 +0000809 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000810 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
811 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000812 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000813 if (NotReady[i]->isAvailable)
814 AvailableQueue->push(NotReady[i]);
815 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000816 NotReady.clear();
817
Dan Gohmanc602dd42008-11-21 00:10:42 +0000818 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000819 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000820 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000821 }
822
Evan Chengd38c22b2006-05-11 23:55:42 +0000823 // Reverse the order if it is bottom up.
824 std::reverse(Sequence.begin(), Sequence.end());
825
Evan Chengd38c22b2006-05-11 23:55:42 +0000826#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000827 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000828#endif
829}
830
831//===----------------------------------------------------------------------===//
832// Top-Down Scheduling
833//===----------------------------------------------------------------------===//
834
835/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000836/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman5ebdb982008-11-18 00:38:59 +0000837void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000838 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000839
840#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000841 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000842 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000843 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000844 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000845 assert(0);
846 }
847#endif
848
Evan Cheng038dcc52007-09-28 19:24:24 +0000849 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000850 SuccSU->isAvailable = true;
851 AvailableQueue->push(SuccSU);
852 }
853}
854
Evan Chengd38c22b2006-05-11 23:55:42 +0000855/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
856/// count of its successors. If a successor pending count is zero, add it to
857/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000858void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000859 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000860 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000861
Dan Gohman92a36d72008-11-17 21:31:02 +0000862 SU->Cycle = CurCycle;
863 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000864
865 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000866 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
867 I != E; ++I)
Dan Gohman5ebdb982008-11-18 00:38:59 +0000868 ReleaseSucc(SU, I->Dep, I->isCtrl);
Dan Gohman92a36d72008-11-17 21:31:02 +0000869
Evan Chengd38c22b2006-05-11 23:55:42 +0000870 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000871 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000872}
873
Dan Gohman54a187e2007-08-20 19:28:38 +0000874/// ListScheduleTopDown - The main loop of list scheduling for top-down
875/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000876void ScheduleDAGRRList::ListScheduleTopDown() {
877 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000878
879 // All leaves to Available queue.
880 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
881 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000882 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000883 AvailableQueue->push(&SUnits[i]);
884 SUnits[i].isAvailable = true;
885 }
886 }
887
Evan Chengd38c22b2006-05-11 23:55:42 +0000888 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000889 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000890 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000891 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000892 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000893
Dan Gohmanc602dd42008-11-21 00:10:42 +0000894 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000895 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000896 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000897 }
898
Evan Chengd38c22b2006-05-11 23:55:42 +0000899#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000900 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000901#endif
902}
903
904
Evan Chengd38c22b2006-05-11 23:55:42 +0000905//===----------------------------------------------------------------------===//
906// RegReductionPriorityQueue Implementation
907//===----------------------------------------------------------------------===//
908//
909// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
910// to reduce register pressure.
911//
912namespace {
913 template<class SF>
914 class RegReductionPriorityQueue;
915
916 /// Sorting functions for the Available queue.
917 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
918 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
919 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
920 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
921
922 bool operator()(const SUnit* left, const SUnit* right) const;
923 };
924
925 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
926 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
927 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
928 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
929
930 bool operator()(const SUnit* left, const SUnit* right) const;
931 };
932} // end anonymous namespace
933
Evan Cheng961bbd32007-01-08 23:50:38 +0000934static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000935 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +0000936 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +0000937 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
938}
939
Dan Gohman186f65d2008-11-20 03:30:37 +0000940/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
941/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000942static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000943CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000944 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
945 if (SethiUllmanNumber != 0)
946 return SethiUllmanNumber;
947
948 unsigned Extra = 0;
949 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
950 I != E; ++I) {
951 if (I->isCtrl) continue; // ignore chain preds
952 SUnit *PredSU = I->Dep;
Dan Gohman186f65d2008-11-20 03:30:37 +0000953 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000954 if (PredSethiUllman > SethiUllmanNumber) {
955 SethiUllmanNumber = PredSethiUllman;
956 Extra = 0;
957 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
958 ++Extra;
959 }
960
961 SethiUllmanNumber += Extra;
962
963 if (SethiUllmanNumber == 0)
964 SethiUllmanNumber = 1;
965
966 return SethiUllmanNumber;
967}
968
Evan Chengd38c22b2006-05-11 23:55:42 +0000969namespace {
970 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +0000971 class VISIBILITY_HIDDEN RegReductionPriorityQueue
972 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000973 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000974 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000975
Dan Gohman3f656df2008-11-20 02:45:51 +0000976 protected:
977 // SUnits - The SUnits for the current graph.
978 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000979
Dan Gohman3f656df2008-11-20 02:45:51 +0000980 const TargetInstrInfo *TII;
981 const TargetRegisterInfo *TRI;
982 ScheduleDAGRRList *scheduleDAG;
983
Dan Gohman186f65d2008-11-20 03:30:37 +0000984 // SethiUllmanNumbers - The SethiUllman number for each node.
985 std::vector<unsigned> SethiUllmanNumbers;
986
Dan Gohman3f656df2008-11-20 02:45:51 +0000987 public:
988 RegReductionPriorityQueue(const TargetInstrInfo *tii,
989 const TargetRegisterInfo *tri) :
990 Queue(SF(this)), currentQueueId(0),
991 TII(tii), TRI(tri), scheduleDAG(NULL) {}
992
993 void initNodes(std::vector<SUnit> &sunits) {
994 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +0000995 // Add pseudo dependency edges for two-address nodes.
996 AddPseudoTwoAddrDeps();
997 // Calculate node priorities.
998 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +0000999 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001000
Dan Gohman186f65d2008-11-20 03:30:37 +00001001 void addNode(const SUnit *SU) {
1002 unsigned SUSize = SethiUllmanNumbers.size();
1003 if (SUnits->size() > SUSize)
1004 SethiUllmanNumbers.resize(SUSize*2, 0);
1005 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1006 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001007
Dan Gohman186f65d2008-11-20 03:30:37 +00001008 void updateNode(const SUnit *SU) {
1009 SethiUllmanNumbers[SU->NodeNum] = 0;
1010 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1011 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001012
Dan Gohman186f65d2008-11-20 03:30:37 +00001013 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001014 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001015 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001016 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001017
1018 unsigned getNodePriority(const SUnit *SU) const {
1019 assert(SU->NodeNum < SethiUllmanNumbers.size());
1020 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1021 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1022 // CopyFromReg should be close to its def because it restricts
1023 // allocation choices. But if it is a livein then perhaps we want it
1024 // closer to its uses so it can be coalesced.
1025 return 0xffff;
1026 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1027 // CopyToReg should be close to its uses to facilitate coalescing and
1028 // avoid spilling.
1029 return 0;
1030 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1031 Opc == TargetInstrInfo::INSERT_SUBREG)
1032 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1033 // facilitate coalescing.
1034 return 0;
1035 else if (SU->NumSuccs == 0)
1036 // If SU does not have a use, i.e. it doesn't produce a value that would
1037 // be consumed (e.g. store), then it terminates a chain of computation.
1038 // Give it a large SethiUllman number so it will be scheduled right
1039 // before its predecessors that it doesn't lengthen their live ranges.
1040 return 0xffff;
1041 else if (SU->NumPreds == 0)
1042 // If SU does not have a def, schedule it close to its uses because it
1043 // does not lengthen any live ranges.
1044 return 0;
1045 else
1046 return SethiUllmanNumbers[SU->NodeNum];
1047 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001048
Evan Cheng5924bf72007-09-25 01:54:36 +00001049 unsigned size() const { return Queue.size(); }
1050
Evan Chengd38c22b2006-05-11 23:55:42 +00001051 bool empty() const { return Queue.empty(); }
1052
1053 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001054 assert(!U->NodeQueueId && "Node in the queue already");
1055 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001056 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001057 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001058
Evan Chengd38c22b2006-05-11 23:55:42 +00001059 void push_all(const std::vector<SUnit *> &Nodes) {
1060 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001061 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001062 }
1063
1064 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001065 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001066 SUnit *V = Queue.top();
1067 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001068 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001069 return V;
1070 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001071
Evan Cheng5924bf72007-09-25 01:54:36 +00001072 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001073 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001074 assert(SU->NodeQueueId != 0 && "Not in queue!");
1075 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001076 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001077 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001078
1079 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1080 scheduleDAG = scheduleDag;
1081 }
1082
1083 protected:
1084 bool canClobber(const SUnit *SU, const SUnit *Op);
1085 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001086 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001087 };
1088
Dan Gohman186f65d2008-11-20 03:30:37 +00001089 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1090 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001091
Dan Gohman186f65d2008-11-20 03:30:37 +00001092 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1093 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001094}
1095
Evan Chengb9e3db62007-03-14 22:43:40 +00001096/// closestSucc - Returns the scheduled cycle of the successor which is
1097/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001098static unsigned closestSucc(const SUnit *SU) {
1099 unsigned MaxCycle = 0;
1100 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001101 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001102 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001103 // If there are bunch of CopyToRegs stacked up, they should be considered
1104 // to be at the same position.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001105 if (I->Dep->getNode() && I->Dep->getNode()->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001106 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001107 if (Cycle > MaxCycle)
1108 MaxCycle = Cycle;
1109 }
Evan Cheng28748552007-03-13 23:25:11 +00001110 return MaxCycle;
1111}
1112
Evan Cheng61bc51e2007-12-20 02:22:36 +00001113/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1114/// for scratch registers. Live-in operands and live-out results don't count
1115/// since they are "fixed".
1116static unsigned calcMaxScratches(const SUnit *SU) {
1117 unsigned Scratches = 0;
1118 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1119 I != E; ++I) {
1120 if (I->isCtrl) continue; // ignore chain preds
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001121 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001122 Scratches++;
1123 }
1124 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1125 I != E; ++I) {
1126 if (I->isCtrl) continue; // ignore chain succs
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001127 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001128 Scratches += 10;
1129 }
1130 return Scratches;
1131}
1132
Evan Chengd38c22b2006-05-11 23:55:42 +00001133// Bottom up
1134bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
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
1162 // Intuitively, it's good to push down instructions whose results are
1163 // liveout so their long live ranges won't conflict with other values
1164 // which are needed inside the BB. Further prioritize liveout instructions
1165 // by the number of operands which are calculated within the BB.
1166 unsigned LScratch = calcMaxScratches(left);
1167 unsigned RScratch = calcMaxScratches(right);
1168 if (LScratch != RScratch)
1169 return LScratch > RScratch;
1170
1171 if (left->Height != right->Height)
1172 return left->Height > right->Height;
1173
1174 if (left->Depth != right->Depth)
1175 return left->Depth < right->Depth;
1176
Roman Levenstein6b371142008-04-29 09:07:59 +00001177 assert(left->NodeQueueId && right->NodeQueueId &&
1178 "NodeQueueId cannot be zero");
1179 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001180}
1181
Dan Gohman3f656df2008-11-20 02:45:51 +00001182template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001183bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001184RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001185 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001186 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001187 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001188 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001189 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001190 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001191 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001192 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001193 if (DU->getNodeId() != -1 &&
1194 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001195 return true;
1196 }
1197 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001198 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001199 return false;
1200}
1201
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001202
Evan Chenga5e595d2007-09-28 22:32:30 +00001203/// hasCopyToRegUse - Return true if SU has a value successor that is a
1204/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001205static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001206 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1207 I != E; ++I) {
1208 if (I->isCtrl) continue;
Dan Gohmane955c482008-08-05 14:45:15 +00001209 const SUnit *SuccSU = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001210 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001211 return true;
1212 }
1213 return false;
1214}
1215
Evan Chengf9891412007-12-20 09:25:31 +00001216/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001217/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001218static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001219 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001220 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001221 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001222 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1223 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001224 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001225 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001226 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001227 if (!SUImpDefs)
1228 return false;
1229 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001230 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001231 if (VT == MVT::Flag || VT == MVT::Other)
1232 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001233 if (!N->hasAnyUseOfValue(i))
1234 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001235 unsigned Reg = ImpDefs[i - NumDefs];
1236 for (;*SUImpDefs; ++SUImpDefs) {
1237 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001238 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001239 return true;
1240 }
1241 }
1242 return false;
1243}
1244
Evan Chengd38c22b2006-05-11 23:55:42 +00001245/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1246/// it as a def&use operand. Add a pseudo control edge from it to the other
1247/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001248/// first (lower in the schedule). If both nodes are two-address, favor the
1249/// one that has a CopyToReg use (more likely to be a loop induction update).
1250/// If both are two-address, but one is commutable while the other is not
1251/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001252template<class SF>
1253void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001254 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001255 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001256 if (!SU->isTwoAddress)
1257 continue;
1258
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001259 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001260 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001261 continue;
1262
Dan Gohman17059682008-07-17 19:10:17 +00001263 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001264 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001265 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001266 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001267 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001268 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1269 continue;
1270 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1271 if (DU->getNodeId() == -1)
1272 continue;
1273 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1274 if (!DUSU) continue;
1275 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1276 E = DUSU->Succs.end(); I != E; ++I) {
1277 if (I->isCtrl) continue;
1278 SUnit *SuccSU = I->Dep;
1279 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001280 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001281 // Be conservative. Ignore if nodes aren't at roughly the same
1282 // depth and height.
1283 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1284 continue;
1285 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1286 continue;
1287 // Don't constrain nodes with physical register defs if the
1288 // predecessor can clobber them.
1289 if (SuccSU->hasPhysRegDefs) {
1290 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001291 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001292 }
1293 // Don't constraint extract_subreg / insert_subreg these may be
1294 // coalesced away. We don't them close to their uses.
1295 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1296 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1297 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1298 continue;
1299 if ((!canClobber(SuccSU, DUSU) ||
1300 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1301 (!SU->isCommutable && SuccSU->isCommutable)) &&
1302 !scheduleDAG->IsReachable(SuccSU, SU)) {
1303 DOUT << "Adding an edge from SU # " << SU->NodeNum
1304 << " to SU #" << SuccSU->NodeNum << "\n";
1305 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001306 }
1307 }
1308 }
1309 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001310}
1311
Evan Cheng6730f032007-01-08 23:55:53 +00001312/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1313/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001314template<class SF>
1315void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001316 SethiUllmanNumbers.assign(SUnits->size(), 0);
1317
1318 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001319 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001320}
Evan Chengd38c22b2006-05-11 23:55:42 +00001321
Roman Levenstein30d09512008-03-27 09:44:37 +00001322/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001323/// predecessors of the successors of the SUnit SU. Stop when the provided
1324/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001325static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1326 unsigned Limit) {
1327 unsigned Sum = 0;
1328 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1329 I != E; ++I) {
Dan Gohmane955c482008-08-05 14:45:15 +00001330 const SUnit *SuccSU = I->Dep;
Roman Levensteinbc674502008-03-27 09:14:57 +00001331 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1332 EE = SuccSU->Preds.end(); II != EE; ++II) {
1333 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001334 if (!PredSU->isScheduled)
1335 if (++Sum > Limit)
1336 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001337 }
1338 }
1339 return Sum;
1340}
1341
Evan Chengd38c22b2006-05-11 23:55:42 +00001342
1343// Top down
1344bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001345 unsigned LPriority = SPQ->getNodePriority(left);
1346 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001347 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1348 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001349 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1350 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001351 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1352 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001353
1354 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1355 return false;
1356 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1357 return true;
1358
Evan Chengd38c22b2006-05-11 23:55:42 +00001359 if (LIsFloater)
1360 LBonus -= 2;
1361 if (RIsFloater)
1362 RBonus -= 2;
1363 if (left->NumSuccs == 1)
1364 LBonus += 2;
1365 if (right->NumSuccs == 1)
1366 RBonus += 2;
1367
Evan Cheng73bdf042008-03-01 00:39:47 +00001368 if (LPriority+LBonus != RPriority+RBonus)
1369 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001370
Evan Cheng73bdf042008-03-01 00:39:47 +00001371 if (left->Depth != right->Depth)
1372 return left->Depth < right->Depth;
1373
1374 if (left->NumSuccsLeft != right->NumSuccsLeft)
1375 return left->NumSuccsLeft > right->NumSuccsLeft;
1376
Roman Levenstein6b371142008-04-29 09:07:59 +00001377 assert(left->NodeQueueId && right->NodeQueueId &&
1378 "NodeQueueId cannot be zero");
1379 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001380}
1381
Evan Chengd38c22b2006-05-11 23:55:42 +00001382//===----------------------------------------------------------------------===//
1383// Public Constructor Functions
1384//===----------------------------------------------------------------------===//
1385
Jim Laskey03593f72006-08-01 18:29:48 +00001386llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1387 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001388 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001389 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001390 bool) {
Dan Gohman5499e892008-11-11 17:50:47 +00001391 const TargetInstrInfo *TII = TM->getInstrInfo();
1392 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001393
Evan Cheng7e4abde2008-07-02 09:23:51 +00001394 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001395
Evan Cheng7e4abde2008-07-02 09:23:51 +00001396 ScheduleDAGRRList *SD =
Dan Gohmanfd08af42008-11-20 03:11:19 +00001397 new ScheduleDAGRRList(DAG, BB, *TM, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001398 PQ->setScheduleDAG(SD);
1399 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001400}
1401
Jim Laskey03593f72006-08-01 18:29:48 +00001402llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1403 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001404 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001405 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001406 bool) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001407 const TargetInstrInfo *TII = TM->getInstrInfo();
1408 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
1409
1410 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1411
Dan Gohmanfd08af42008-11-20 03:11:19 +00001412 ScheduleDAGRRList *SD = new ScheduleDAGRRList(DAG, BB, *TM, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001413 PQ->setScheduleDAG(SD);
1414 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001415}