blob: 287e8c5b0e5fe659625b6e8e2432d43dc7800d25 [file] [log] [blame]
Evan Chengd38c22b2006-05-11 23:55:42 +00001//===----- ScheduleDAGList.cpp - Reg pressure reduction list scheduler ----===//
2//
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"
Evan Chengd38c22b2006-05-11 23:55:42 +000019#include "llvm/CodeGen/ScheduleDAG.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",
44 " Bottom-up register reduction list scheduling",
45 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
48 " Top-down register reduction list scheduling",
49 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///
Chris Lattnere097e6f2006-06-28 22:17:39 +000056class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG {
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 Cheng7e4abde2008-07-02 09:23:51 +000062 /// Fast - True if we are performing fast scheduling.
63 ///
Evan Cheng2c977312008-07-01 18:05:03 +000064 bool Fast;
Evan Chengd38c22b2006-05-11 23:55:42 +000065
66 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000067 SchedulingPriorityQueue *AvailableQueue;
68
Evan Cheng5924bf72007-09-25 01:54:36 +000069 /// LiveRegs / LiveRegDefs - A set of physical registers and their definition
70 /// that are "live". These nodes must be scheduled before any other nodes that
71 /// modifies the registers can be scheduled.
72 SmallSet<unsigned, 4> LiveRegs;
73 std::vector<SUnit*> LiveRegDefs;
74 std::vector<unsigned> LiveRegCycles;
75
Evan Chengd38c22b2006-05-11 23:55:42 +000076public:
77 ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb,
Evan Cheng2c977312008-07-01 18:05:03 +000078 const TargetMachine &tm, bool isbottomup, bool f,
79 SchedulingPriorityQueue *availqueue)
80 : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup), Fast(f),
Evan Chengd38c22b2006-05-11 23:55:42 +000081 AvailableQueue(availqueue) {
82 }
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.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000091 bool IsReachable(SUnit *SU, SUnit *TargetSU);
92
93 /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
94 /// create a cycle.
95 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU);
96
97 /// AddPred - This adds the specified node X as a predecessor of
98 /// the current node Y if not already.
Roman Levenstein733a4d62008-03-26 11:23:38 +000099 /// This returns true if this is a new predecessor.
100 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000101 bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000102 unsigned PhyReg = 0, int Cost = 1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000103
Roman Levenstein733a4d62008-03-26 11:23:38 +0000104 /// RemovePred - This removes the specified node N from the predecessors of
105 /// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000106 bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isSpecial);
107
Evan Chengd38c22b2006-05-11 23:55:42 +0000108private:
Evan Cheng8e136a92007-09-26 21:36:17 +0000109 void ReleasePred(SUnit*, bool, unsigned);
110 void ReleaseSucc(SUnit*, bool isChain, unsigned);
111 void CapturePred(SUnit*, SUnit*, bool);
112 void ScheduleNodeBottomUp(SUnit*, unsigned);
113 void ScheduleNodeTopDown(SUnit*, unsigned);
114 void UnscheduleNodeBottomUp(SUnit*);
115 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
116 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000117 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +0000118 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000119 const TargetRegisterClass*,
120 SmallVector<SUnit*, 2>&);
121 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000122 void ListScheduleTopDown();
123 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000124 void CommuteNodesToReducePressure();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000125
126
127 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000128 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000129 SUnit *CreateNewSUnit(SDNode *N) {
130 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000131 // Update the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000132 if (NewNode->NodeNum >= Node2Index.size())
133 InitDAGTopologicalSorting();
134 return NewNode;
135 }
136
Roman Levenstein733a4d62008-03-26 11:23:38 +0000137 /// CreateClone - Creates a new SUnit from an existing one.
138 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000139 SUnit *CreateClone(SUnit *N) {
140 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000141 // Update the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000142 if (NewNode->NodeNum >= Node2Index.size())
143 InitDAGTopologicalSorting();
144 return NewNode;
145 }
146
147 /// Functions for preserving the topological ordering
148 /// even after dynamic insertions of new edges.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000149 /// This allows a very fast implementation of IsReachable.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150
Roman Levenstein733a4d62008-03-26 11:23:38 +0000151 /// InitDAGTopologicalSorting - create the initial topological
152 /// ordering from the DAG to be scheduled.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000153 void InitDAGTopologicalSorting();
154
155 /// DFS - make a DFS traversal and mark all nodes affected by the
Roman Levenstein733a4d62008-03-26 11:23:38 +0000156 /// edge insertion. These nodes will later get new topological indexes
157 /// by means of the Shift method.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000158 void DFS(SUnit *SU, int UpperBound, bool& HasLoop);
159
160 /// Shift - reassign topological indexes for the nodes in the DAG
Roman Levenstein733a4d62008-03-26 11:23:38 +0000161 /// to preserve the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000162 void Shift(BitVector& Visited, int LowerBound, int UpperBound);
163
Roman Levenstein733a4d62008-03-26 11:23:38 +0000164 /// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000165 void Allocate(int n, int index);
166
Roman Levenstein733a4d62008-03-26 11:23:38 +0000167 /// Index2Node - Maps topological index to the node number.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000168 std::vector<int> Index2Node;
Roman Levenstein733a4d62008-03-26 11:23:38 +0000169 /// Node2Index - Maps the node number to its topological index.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000170 std::vector<int> Node2Index;
Roman Levenstein733a4d62008-03-26 11:23:38 +0000171 /// Visited - a set of nodes visited during a DFS traversal.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000172 BitVector Visited;
Evan Chengd38c22b2006-05-11 23:55:42 +0000173};
174} // end anonymous namespace
175
176
177/// Schedule - Schedule the DAG using list scheduling.
178void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000179 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000180
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000181 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
182 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000183
Evan Chengd38c22b2006-05-11 23:55:42 +0000184 // Build scheduling units.
185 BuildSchedUnits();
186
Evan Chengd38c22b2006-05-11 23:55:42 +0000187 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000188 SUnits[su].dumpAll(&DAG));
Evan Cheng2c977312008-07-01 18:05:03 +0000189 if (!Fast) {
190 CalculateDepths();
191 CalculateHeights();
192 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000193 InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000194
Dan Gohman46520a22008-06-21 19:18:17 +0000195 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000196
Evan Chengd38c22b2006-05-11 23:55:42 +0000197 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
198 if (isBottomUp)
199 ListScheduleBottomUp();
200 else
201 ListScheduleTopDown();
202
203 AvailableQueue->releaseState();
Evan Cheng2c977312008-07-01 18:05:03 +0000204
205 if (!Fast)
206 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000207
Bill Wendling22e978a2006-12-07 20:04:42 +0000208 DOUT << "*** Final schedule ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000209 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000210 DOUT << "\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000211
212 // Emit in scheduled order
213 EmitSchedule();
214}
215
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000216/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000217/// it is not the last use of its first operand, add it to the CommuteSet if
218/// possible. It will be commuted when it is translated to a MI.
219void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000220 SmallPtrSet<SUnit*, 4> OperandSeen;
Dan Gohman4370f262008-04-15 01:22:18 +0000221 for (unsigned i = Sequence.size(); i != 0; ) {
222 --i;
Evan Chengafed73e2006-05-12 01:58:24 +0000223 SUnit *SU = Sequence[i];
Evan Cheng8e136a92007-09-26 21:36:17 +0000224 if (!SU || !SU->Node) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000225 if (SU->isCommutable) {
226 unsigned Opc = SU->Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000227 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000228 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000229 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000230 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000231 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000232 continue;
233
234 SDNode *OpN = SU->Node->getOperand(j).Val;
Dan Gohman46520a22008-06-21 19:18:17 +0000235 SUnit *OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000236 if (OpSU && OperandSeen.count(OpSU) == 1) {
237 // Ok, so SU is not the last use of OpSU, but SU is two-address so
238 // it will clobber OpSU. Try to commute SU if no other source operands
239 // are live below.
240 bool DoCommute = true;
241 for (unsigned k = 0; k < NumOps; ++k) {
242 if (k != j) {
243 OpN = SU->Node->getOperand(k).Val;
Dan Gohman46520a22008-06-21 19:18:17 +0000244 OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000245 if (OpSU && OperandSeen.count(OpSU) == 1) {
246 DoCommute = false;
247 break;
248 }
249 }
Evan Chengafed73e2006-05-12 01:58:24 +0000250 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000251 if (DoCommute)
252 CommuteSet.insert(SU->Node);
Evan Chengafed73e2006-05-12 01:58:24 +0000253 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000254
255 // Only look at the first use&def node for now.
256 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000257 }
258 }
259
Chris Lattnerd86418a2006-08-17 00:09:56 +0000260 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
261 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000262 if (!I->isCtrl)
Dan Gohmane6e13482008-06-21 15:52:51 +0000263 OperandSeen.insert(I->Dep->OrigNode);
Evan Chengafed73e2006-05-12 01:58:24 +0000264 }
265 }
266}
Evan Chengd38c22b2006-05-11 23:55:42 +0000267
268//===----------------------------------------------------------------------===//
269// Bottom-Up Scheduling
270//===----------------------------------------------------------------------===//
271
Evan Chengd38c22b2006-05-11 23:55:42 +0000272/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000273/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000274void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain,
275 unsigned CurCycle) {
276 // FIXME: the distance between two nodes is not always == the predecessor's
277 // latency. For example, the reader can very well read the register written
278 // by the predecessor later than the issue cycle. It also depends on the
279 // interrupt model (drain vs. freeze).
280 PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
281
Evan Cheng038dcc52007-09-28 19:24:24 +0000282 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000283
284#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000285 if (PredSU->NumSuccsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000286 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000287 PredSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000288 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000289 assert(0);
290 }
291#endif
292
Evan Cheng038dcc52007-09-28 19:24:24 +0000293 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000294 PredSU->isAvailable = true;
295 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000296 }
297}
298
299/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
300/// count of its predecessors. If a predecessor pending count is zero, add it to
301/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000302void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000303 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +0000304 DEBUG(SU->dump(&DAG));
305 SU->Cycle = CurCycle;
306
307 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000308
309 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000310 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000311 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000312 ReleasePred(I->Dep, I->isCtrl, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000313 if (I->Cost < 0) {
314 // This is a physical register dependency and it's impossible or
315 // expensive to copy the register. Make sure nothing that can
316 // clobber the register is scheduled between the predecessor and
317 // this node.
318 if (LiveRegs.insert(I->Reg)) {
319 LiveRegDefs[I->Reg] = I->Dep;
320 LiveRegCycles[I->Reg] = CurCycle;
321 }
322 }
323 }
324
325 // Release all the implicit physical register defs that are live.
326 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
327 I != E; ++I) {
328 if (I->Cost < 0) {
329 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
330 LiveRegs.erase(I->Reg);
331 assert(LiveRegDefs[I->Reg] == SU &&
332 "Physical register dependency violated?");
333 LiveRegDefs[I->Reg] = NULL;
334 LiveRegCycles[I->Reg] = 0;
335 }
336 }
337 }
338
Evan Chengd38c22b2006-05-11 23:55:42 +0000339 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000340}
341
Evan Cheng5924bf72007-09-25 01:54:36 +0000342/// CapturePred - This does the opposite of ReleasePred. Since SU is being
343/// unscheduled, incrcease the succ left count of its predecessors. Remove
344/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000345void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
346 unsigned CycleBound = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
348 I != E; ++I) {
349 if (I->Dep == SU)
350 continue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000351 CycleBound = std::max(CycleBound,
352 I->Dep->Cycle + PredSU->Latency);
Evan Cheng5924bf72007-09-25 01:54:36 +0000353 }
354
355 if (PredSU->isAvailable) {
356 PredSU->isAvailable = false;
357 if (!PredSU->isPending)
358 AvailableQueue->remove(PredSU);
359 }
360
Roman Levenstein6b371142008-04-29 09:07:59 +0000361 PredSU->CycleBound = CycleBound;
Evan Cheng038dcc52007-09-28 19:24:24 +0000362 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000363}
364
365/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
366/// its predecessor states to reflect the change.
367void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
368 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
369 DEBUG(SU->dump(&DAG));
370
371 AvailableQueue->UnscheduledNode(SU);
372
373 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
374 I != E; ++I) {
375 CapturePred(I->Dep, SU, I->isCtrl);
376 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
377 LiveRegs.erase(I->Reg);
378 assert(LiveRegDefs[I->Reg] == I->Dep &&
379 "Physical register dependency violated?");
380 LiveRegDefs[I->Reg] = NULL;
381 LiveRegCycles[I->Reg] = 0;
382 }
383 }
384
385 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
386 I != E; ++I) {
387 if (I->Cost < 0) {
388 if (LiveRegs.insert(I->Reg)) {
389 assert(!LiveRegDefs[I->Reg] &&
390 "Physical register dependency violated?");
391 LiveRegDefs[I->Reg] = SU;
392 }
393 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
394 LiveRegCycles[I->Reg] = I->Dep->Cycle;
395 }
396 }
397
398 SU->Cycle = 0;
399 SU->isScheduled = false;
400 SU->isAvailable = true;
401 AvailableQueue->push(SU);
402}
403
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000404/// IsReachable - Checks if SU is reachable from TargetSU.
405bool ScheduleDAGRRList::IsReachable(SUnit *SU, SUnit *TargetSU) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000406 // If insertion of the edge SU->TargetSU would create a cycle
407 // then there is a path from TargetSU to SU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000408 int UpperBound, LowerBound;
409 LowerBound = Node2Index[TargetSU->NodeNum];
410 UpperBound = Node2Index[SU->NodeNum];
411 bool HasLoop = false;
412 // Is Ord(TargetSU) < Ord(SU) ?
413 if (LowerBound < UpperBound) {
414 Visited.reset();
415 // There may be a path from TargetSU to SU. Check for it.
416 DFS(TargetSU, UpperBound, HasLoop);
Evan Chengcfd5f822007-09-27 00:25:29 +0000417 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000418 return HasLoop;
Evan Chengcfd5f822007-09-27 00:25:29 +0000419}
420
Roman Levenstein733a4d62008-03-26 11:23:38 +0000421/// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000422inline void ScheduleDAGRRList::Allocate(int n, int index) {
423 Node2Index[n] = index;
424 Index2Node[index] = n;
Evan Chengcfd5f822007-09-27 00:25:29 +0000425}
426
Roman Levenstein733a4d62008-03-26 11:23:38 +0000427/// InitDAGTopologicalSorting - create the initial topological
428/// ordering from the DAG to be scheduled.
Evan Cheng2c977312008-07-01 18:05:03 +0000429
430/// The idea of the algorithm is taken from
431/// "Online algorithms for managing the topological order of
432/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
433/// This is the MNR algorithm, which was first introduced by
434/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
435/// "Maintaining a topological order under edge insertions".
436///
437/// Short description of the algorithm:
438///
439/// Topological ordering, ord, of a DAG maps each node to a topological
440/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
441///
442/// This means that if there is a path from the node X to the node Z,
443/// then ord(X) < ord(Z).
444///
445/// This property can be used to check for reachability of nodes:
446/// if Z is reachable from X, then an insertion of the edge Z->X would
447/// create a cycle.
448///
449/// The algorithm first computes a topological ordering for the DAG by
450/// initializing the Index2Node and Node2Index arrays and then tries to keep
451/// the ordering up-to-date after edge insertions by reordering the DAG.
452///
453/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
454/// the nodes reachable from Y, and then shifts them using Shift to lie
455/// immediately after X in Index2Node.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000456void ScheduleDAGRRList::InitDAGTopologicalSorting() {
457 unsigned DAGSize = SUnits.size();
458 std::vector<unsigned> InDegree(DAGSize);
459 std::vector<SUnit*> WorkList;
460 WorkList.reserve(DAGSize);
461 std::vector<SUnit*> TopOrder;
462 TopOrder.reserve(DAGSize);
463
Roman Levenstein733a4d62008-03-26 11:23:38 +0000464 // Initialize the data structures.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000465 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
466 SUnit *SU = &SUnits[i];
467 int NodeNum = SU->NodeNum;
468 unsigned Degree = SU->Succs.size();
469 InDegree[NodeNum] = Degree;
470
471 // Is it a node without dependencies?
472 if (Degree == 0) {
473 assert(SU->Succs.empty() && "SUnit should have no successors");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000474 // Collect leaf nodes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000475 WorkList.push_back(SU);
476 }
477 }
478
479 while (!WorkList.empty()) {
480 SUnit *SU = WorkList.back();
481 WorkList.pop_back();
482 TopOrder.push_back(SU);
483 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
484 I != E; ++I) {
485 SUnit *SU = I->Dep;
486 if (!--InDegree[SU->NodeNum])
487 // If all dependencies of the node are processed already,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000488 // then the node can be computed now.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000489 WorkList.push_back(SU);
490 }
491 }
492
493 // Second pass, assign the actual topological order as node ids.
494 int Id = 0;
495
496 Index2Node.clear();
497 Node2Index.clear();
498 Index2Node.resize(DAGSize);
499 Node2Index.resize(DAGSize);
500 Visited.resize(DAGSize);
501
502 for (std::vector<SUnit*>::reverse_iterator TI = TopOrder.rbegin(),
503 TE = TopOrder.rend();TI != TE; ++TI) {
504 Allocate((*TI)->NodeNum, Id);
505 Id++;
506 }
507
508#ifndef NDEBUG
509 // Check correctness of the ordering
510 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
511 SUnit *SU = &SUnits[i];
512 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
513 I != E; ++I) {
514 assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] &&
515 "Wrong topological sorting");
516 }
517 }
518#endif
519}
520
Roman Levenstein733a4d62008-03-26 11:23:38 +0000521/// AddPred - adds an edge from SUnit X to SUnit Y.
522/// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000523bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
524 unsigned PhyReg, int Cost) {
525 int UpperBound, LowerBound;
526 LowerBound = Node2Index[Y->NodeNum];
527 UpperBound = Node2Index[X->NodeNum];
528 bool HasLoop = false;
529 // Is Ord(X) < Ord(Y) ?
530 if (LowerBound < UpperBound) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000531 // Update the topological order.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000532 Visited.reset();
533 DFS(Y, UpperBound, HasLoop);
534 assert(!HasLoop && "Inserted edge creates a loop!");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000535 // Recompute topological indexes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000536 Shift(Visited, LowerBound, UpperBound);
537 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000538 // Now really insert the edge.
539 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540}
541
Roman Levenstein733a4d62008-03-26 11:23:38 +0000542/// RemovePred - This removes the specified node N from the predecessors of
543/// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000544bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N,
545 bool isCtrl, bool isSpecial) {
546 // InitDAGTopologicalSorting();
547 return M->removePred(N, isCtrl, isSpecial);
548}
549
Roman Levenstein733a4d62008-03-26 11:23:38 +0000550/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
551/// all nodes affected by the edge insertion. These nodes will later get new
552/// topological indexes by means of the Shift method.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000553void ScheduleDAGRRList::DFS(SUnit *SU, int UpperBound, bool& HasLoop) {
554 std::vector<SUnit*> WorkList;
555 WorkList.reserve(SUnits.size());
556
557 WorkList.push_back(SU);
558 while (!WorkList.empty()) {
559 SU = WorkList.back();
560 WorkList.pop_back();
561 Visited.set(SU->NodeNum);
562 for (int I = SU->Succs.size()-1; I >= 0; --I) {
563 int s = SU->Succs[I].Dep->NodeNum;
564 if (Node2Index[s] == UpperBound) {
565 HasLoop = true;
566 return;
567 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000568 // Visit successors if not already and in affected region.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000569 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
570 WorkList.push_back(SU->Succs[I].Dep);
571 }
572 }
573 }
574}
575
Roman Levenstein733a4d62008-03-26 11:23:38 +0000576/// Shift - Renumber the nodes so that the topological ordering is
577/// preserved.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000578void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound,
579 int UpperBound) {
580 std::vector<int> L;
581 int shift = 0;
582 int i;
583
584 for (i = LowerBound; i <= UpperBound; ++i) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000585 // w is node at topological index i.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000586 int w = Index2Node[i];
587 if (Visited.test(w)) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000588 // Unmark.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000589 Visited.reset(w);
590 L.push_back(w);
591 shift = shift + 1;
592 } else {
593 Allocate(w, i - shift);
594 }
595 }
596
597 for (unsigned j = 0; j < L.size(); ++j) {
598 Allocate(L[j], i - shift);
599 i = i + 1;
600 }
601}
602
603
Dan Gohmanfd227e92008-03-25 17:10:29 +0000604/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Evan Chengcfd5f822007-09-27 00:25:29 +0000605/// create a cycle.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000606bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
607 if (IsReachable(TargetSU, SU))
Evan Chengcfd5f822007-09-27 00:25:29 +0000608 return true;
609 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
610 I != E; ++I)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000611 if (I->Cost < 0 && IsReachable(TargetSU, I->Dep))
Evan Chengcfd5f822007-09-27 00:25:29 +0000612 return true;
613 return false;
614}
615
Evan Cheng8e136a92007-09-26 21:36:17 +0000616/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000617/// BTCycle in order to schedule a specific node. Returns the last unscheduled
618/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000619void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
620 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000621 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000622 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000623 OldSU = Sequence.back();
624 Sequence.pop_back();
625 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000626 // Don't try to remove SU from AvailableQueue.
627 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000628 UnscheduleNodeBottomUp(OldSU);
629 --CurCycle;
630 }
631
632
633 if (SU->isSucc(OldSU)) {
634 assert(false && "Something is wrong!");
635 abort();
636 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000637
638 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000639}
640
Evan Cheng5924bf72007-09-25 01:54:36 +0000641/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
642/// successors to the newly created node.
643SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Evan Cheng79e97132007-10-05 01:39:18 +0000644 if (SU->FlaggedNodes.size())
645 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000646
Evan Cheng79e97132007-10-05 01:39:18 +0000647 SDNode *N = SU->Node;
648 if (!N)
649 return NULL;
650
651 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000652 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000653 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000654 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000655 if (VT == MVT::Flag)
656 return NULL;
657 else if (VT == MVT::Other)
658 TryUnfold = true;
659 }
Evan Cheng79e97132007-10-05 01:39:18 +0000660 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
661 const SDOperand &Op = N->getOperand(i);
Duncan Sands13237ac2008-06-06 12:08:01 +0000662 MVT VT = Op.Val->getValueType(Op.ResNo);
Evan Cheng79e97132007-10-05 01:39:18 +0000663 if (VT == MVT::Flag)
664 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000665 }
666
667 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000668 SmallVector<SDNode*, 2> NewNodes;
Owen Anderson0ec92e92008-01-07 01:35:56 +0000669 if (!TII->unfoldMemoryOperand(DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000670 return NULL;
671
672 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
673 assert(NewNodes.size() == 2 && "Expected a load folding node!");
674
675 N = NewNodes[1];
676 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000677 unsigned NumVals = N->getNumValues();
678 unsigned OldNumVals = SU->Node->getNumValues();
679 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattner3cfb56d2007-10-15 06:10:22 +0000680 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, i), SDOperand(N, i));
Evan Cheng79e97132007-10-05 01:39:18 +0000681 DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, OldNumVals-1),
Chris Lattner3cfb56d2007-10-15 06:10:22 +0000682 SDOperand(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000683
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000684 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000685 assert(N->getNodeId() == -1 && "Node already inserted!");
686 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000687
Chris Lattner03ad8852008-01-07 07:27:27 +0000688 const TargetInstrDesc &TID = TII->get(N->getTargetOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000689 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000690 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000691 NewSU->isTwoAddress = true;
692 break;
693 }
694 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000695 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000696 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000697 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000698 NewSU->Depth = SU->Depth;
699 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000700 ComputeLatency(NewSU);
701
Evan Cheng91e0fc92007-12-18 08:42:10 +0000702 // LoadNode may already exist. This can happen when there is another
703 // load from the same location and producing the same type of value
704 // but it has different alignment or volatileness.
705 bool isNewLoad = true;
706 SUnit *LoadSU;
Dan Gohman46520a22008-06-21 19:18:17 +0000707 if (LoadNode->getNodeId() != -1) {
708 LoadSU = &SUnits[LoadNode->getNodeId()];
Evan Cheng91e0fc92007-12-18 08:42:10 +0000709 isNewLoad = false;
710 } else {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000711 LoadSU = CreateNewSUnit(LoadNode);
Dan Gohman46520a22008-06-21 19:18:17 +0000712 LoadNode->setNodeId(LoadSU->NodeNum);
Evan Cheng91e0fc92007-12-18 08:42:10 +0000713
714 LoadSU->Depth = SU->Depth;
715 LoadSU->Height = SU->Height;
716 ComputeLatency(LoadSU);
717 }
718
Evan Cheng79e97132007-10-05 01:39:18 +0000719 SUnit *ChainPred = NULL;
720 SmallVector<SDep, 4> ChainSuccs;
721 SmallVector<SDep, 4> LoadPreds;
722 SmallVector<SDep, 4> NodePreds;
723 SmallVector<SDep, 4> NodeSuccs;
724 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
725 I != E; ++I) {
726 if (I->isCtrl)
727 ChainPred = I->Dep;
Evan Cheng567d2e52008-03-04 00:41:45 +0000728 else if (I->Dep->Node && I->Dep->Node->isOperandOf(LoadNode))
Evan Cheng79e97132007-10-05 01:39:18 +0000729 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
730 else
731 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
732 }
733 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
734 I != E; ++I) {
735 if (I->isCtrl)
736 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
737 I->isCtrl, I->isSpecial));
738 else
739 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
740 I->isCtrl, I->isSpecial));
741 }
742
Dan Gohman4370f262008-04-15 01:22:18 +0000743 if (ChainPred) {
744 RemovePred(SU, ChainPred, true, false);
745 if (isNewLoad)
746 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000747 }
Evan Cheng79e97132007-10-05 01:39:18 +0000748 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
749 SDep *Pred = &LoadPreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000750 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
751 if (isNewLoad) {
752 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000753 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000754 }
Evan Cheng79e97132007-10-05 01:39:18 +0000755 }
756 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
757 SDep *Pred = &NodePreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000758 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
759 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000760 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000761 }
762 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
763 SDep *Succ = &NodeSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000764 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
765 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000766 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000767 }
768 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
769 SDep *Succ = &ChainSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000770 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
771 if (isNewLoad) {
772 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000773 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000774 }
Evan Cheng79e97132007-10-05 01:39:18 +0000775 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000776 if (isNewLoad) {
777 AddPred(NewSU, LoadSU, false, false);
778 }
Evan Cheng79e97132007-10-05 01:39:18 +0000779
Evan Cheng91e0fc92007-12-18 08:42:10 +0000780 if (isNewLoad)
781 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000782 AvailableQueue->addNode(NewSU);
783
784 ++NumUnfolds;
785
786 if (NewSU->NumSuccsLeft == 0) {
787 NewSU->isAvailable = true;
788 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000789 }
790 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000791 }
792
793 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000794 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000795
796 // New SUnit has the exact same predecessors.
797 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
798 I != E; ++I)
799 if (!I->isSpecial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000800 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000801 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
802 }
803
804 // Only copy scheduled successors. Cut them from old node's successor
805 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000806 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000807 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
808 I != E; ++I) {
809 if (I->isSpecial)
810 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000811 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000812 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000813 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000814 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000815 }
816 }
817 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000818 SUnit *Succ = DelDeps[i].first;
819 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000820 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000821 }
822
823 AvailableQueue->updateNode(SU);
824 AvailableQueue->addNode(NewSU);
825
Evan Cheng1ec79b42007-09-27 07:09:03 +0000826 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000827 return NewSU;
828}
829
Evan Cheng1ec79b42007-09-27 07:09:03 +0000830/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
831/// and move all scheduled successors of the given SUnit to the last copy.
832void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
833 const TargetRegisterClass *DestRC,
834 const TargetRegisterClass *SrcRC,
835 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000836 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000837 CopyFromSU->CopySrcRC = SrcRC;
838 CopyFromSU->CopyDstRC = DestRC;
839 CopyFromSU->Depth = SU->Depth;
840 CopyFromSU->Height = SU->Height;
841
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000842 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000843 CopyToSU->CopySrcRC = DestRC;
844 CopyToSU->CopyDstRC = SrcRC;
845
846 // Only copy scheduled successors. Cut them from old node's successor
847 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000848 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000849 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
850 I != E; ++I) {
851 if (I->isSpecial)
852 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000853 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000854 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000855 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000856 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000857 }
858 }
859 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000860 SUnit *Succ = DelDeps[i].first;
861 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000862 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000863 }
864
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000865 AddPred(CopyFromSU, SU, false, false, Reg, -1);
866 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000867
868 AvailableQueue->updateNode(SU);
869 AvailableQueue->addNode(CopyFromSU);
870 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000871 Copies.push_back(CopyFromSU);
872 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000873
Evan Cheng1ec79b42007-09-27 07:09:03 +0000874 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000875}
876
877/// getPhysicalRegisterVT - Returns the ValueType of the physical register
878/// definition of the specified node.
879/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000880static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
881 const TargetInstrInfo *TII) {
Chris Lattner03ad8852008-01-07 07:27:27 +0000882 const TargetInstrDesc &TID = TII->get(N->getTargetOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000883 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000884 unsigned NumRes = TID.getNumDefs();
885 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000886 if (Reg == *ImpDef)
887 break;
888 ++NumRes;
889 }
890 return N->getValueType(NumRes);
891}
892
Evan Cheng5924bf72007-09-25 01:54:36 +0000893/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
894/// scheduling of the given node to satisfy live physical register dependencies.
895/// If the specific node is the last one that's available to schedule, do
896/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000897bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
898 SmallVector<unsigned, 4> &LRegs){
Evan Cheng5924bf72007-09-25 01:54:36 +0000899 if (LiveRegs.empty())
900 return false;
901
Evan Chenge6f92252007-09-27 18:46:06 +0000902 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000903 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000904 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
905 I != E; ++I) {
906 if (I->Cost < 0) {
907 unsigned Reg = I->Reg;
Evan Chenge6f92252007-09-27 18:46:06 +0000908 if (LiveRegs.count(Reg) && LiveRegDefs[Reg] != I->Dep) {
909 if (RegAdded.insert(Reg))
910 LRegs.push_back(Reg);
911 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000912 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000913 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000914 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != I->Dep) {
915 if (RegAdded.insert(*Alias))
916 LRegs.push_back(*Alias);
917 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000918 }
919 }
920
921 for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) {
922 SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1];
Evan Cheng8e136a92007-09-26 21:36:17 +0000923 if (!Node || !Node->isTargetOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000924 continue;
Chris Lattner03ad8852008-01-07 07:27:27 +0000925 const TargetInstrDesc &TID = TII->get(Node->getTargetOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000926 if (!TID.ImplicitDefs)
927 continue;
928 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Evan Chenge6f92252007-09-27 18:46:06 +0000929 if (LiveRegs.count(*Reg) && LiveRegDefs[*Reg] != SU) {
930 if (RegAdded.insert(*Reg))
931 LRegs.push_back(*Reg);
932 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000933 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000934 *Alias; ++Alias)
Evan Chenge6f92252007-09-27 18:46:06 +0000935 if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != SU) {
936 if (RegAdded.insert(*Alias))
937 LRegs.push_back(*Alias);
938 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000939 }
940 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000941 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000942}
943
Evan Cheng1ec79b42007-09-27 07:09:03 +0000944
Evan Chengd38c22b2006-05-11 23:55:42 +0000945/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
946/// schedulers.
947void ScheduleDAGRRList::ListScheduleBottomUp() {
948 unsigned CurCycle = 0;
949 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000950 if (!SUnits.empty()) {
Dan Gohman46520a22008-06-21 19:18:17 +0000951 SUnit *RootSU = &SUnits[DAG.getRoot().Val->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000952 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
953 RootSU->isAvailable = true;
954 AvailableQueue->push(RootSU);
955 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000956
957 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000958 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000959 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000960 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000961 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000962 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000963 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000964 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000965 SUnit *CurSU = AvailableQueue->pop();
966 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000967 if (CurSU->CycleBound <= CurCycle) {
968 SmallVector<unsigned, 4> LRegs;
969 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000970 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000971 Delayed = true;
972 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000973 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000974
975 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
976 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000977 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000978 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000979
980 // All candidates are delayed due to live physical reg dependencies.
981 // Try backtracking, code duplication, or inserting cross class copies
982 // to resolve it.
983 if (Delayed && !CurSU) {
984 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
985 SUnit *TrySU = NotReady[i];
986 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
987
988 // Try unscheduling up to the point where it's safe to schedule
989 // this node.
990 unsigned LiveCycle = CurCycle;
991 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
992 unsigned Reg = LRegs[j];
993 unsigned LCycle = LiveRegCycles[Reg];
994 LiveCycle = std::min(LiveCycle, LCycle);
995 }
996 SUnit *OldSU = Sequence[LiveCycle];
997 if (!WillCreateCycle(TrySU, OldSU)) {
998 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
999 // Force the current node to be scheduled before the node that
1000 // requires the physical reg dep.
1001 if (OldSU->isAvailable) {
1002 OldSU->isAvailable = false;
1003 AvailableQueue->remove(OldSU);
1004 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001005 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001006 // If one or more successors has been unscheduled, then the current
1007 // node is no longer avaialable. Schedule a successor that's now
1008 // available instead.
1009 if (!TrySU->isAvailable)
1010 CurSU = AvailableQueue->pop();
1011 else {
1012 CurSU = TrySU;
1013 TrySU->isPending = false;
1014 NotReady.erase(NotReady.begin()+i);
1015 }
1016 break;
1017 }
1018 }
1019
1020 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +00001021 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +00001022 // "expensive to copy" values to break the dependency. In case even
1023 // that doesn't work, insert cross class copies.
1024 SUnit *TrySU = NotReady[0];
1025 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1026 assert(LRegs.size() == 1 && "Can't handle this yet!");
1027 unsigned Reg = LRegs[0];
1028 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +00001029 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
1030 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +00001031 // Issue expensive cross register class copies.
Duncan Sands13237ac2008-06-06 12:08:01 +00001032 MVT VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001033 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +00001034 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001035 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001036 if (!DestRC) {
1037 assert(false && "Don't know how to copy this physical register!");
1038 abort();
1039 }
1040 SmallVector<SUnit*, 2> Copies;
1041 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1042 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
1043 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001044 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001045 NewDef = Copies.back();
1046 }
1047
1048 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
1049 << " to SU #" << TrySU->NodeNum << "\n";
1050 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001051 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001052 TrySU->isAvailable = false;
1053 CurSU = NewDef;
1054 }
1055
1056 if (!CurSU) {
1057 assert(false && "Unable to resolve live physical register dependencies!");
1058 abort();
1059 }
1060 }
1061
Evan Chengd38c22b2006-05-11 23:55:42 +00001062 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +00001063 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
1064 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +00001065 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +00001066 if (NotReady[i]->isAvailable)
1067 AvailableQueue->push(NotReady[i]);
1068 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001069 NotReady.clear();
1070
Evan Cheng5924bf72007-09-25 01:54:36 +00001071 if (!CurSU)
1072 Sequence.push_back(0);
1073 else {
1074 ScheduleNodeBottomUp(CurSU, CurCycle);
1075 Sequence.push_back(CurSU);
1076 }
1077 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001078 }
1079
Evan Chengd38c22b2006-05-11 23:55:42 +00001080 // Reverse the order if it is bottom up.
1081 std::reverse(Sequence.begin(), Sequence.end());
1082
1083
1084#ifndef NDEBUG
1085 // Verify that all SUnits were scheduled.
1086 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001087 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001088 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001089 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman4370f262008-04-15 01:22:18 +00001090 if (!SUnits[i].isScheduled) {
1091 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1092 ++DeadNodes;
1093 continue;
1094 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001095 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001096 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001097 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001098 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001099 AnyNotSched = true;
1100 }
Dan Gohman4370f262008-04-15 01:22:18 +00001101 if (SUnits[i].NumSuccsLeft != 0) {
1102 if (!AnyNotSched)
1103 cerr << "*** List scheduling failed! ***\n";
1104 SUnits[i].dump(&DAG);
1105 cerr << "has successors left!\n";
1106 AnyNotSched = true;
1107 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001108 }
Dan Gohman82b66732008-04-15 22:40:14 +00001109 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1110 if (!Sequence[i])
1111 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001112 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001113 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001114 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001115#endif
1116}
1117
1118//===----------------------------------------------------------------------===//
1119// Top-Down Scheduling
1120//===----------------------------------------------------------------------===//
1121
1122/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001123/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +00001124void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain,
1125 unsigned CurCycle) {
1126 // FIXME: the distance between two nodes is not always == the predecessor's
1127 // latency. For example, the reader can very well read the register written
1128 // by the predecessor later than the issue cycle. It also depends on the
1129 // interrupt model (drain vs. freeze).
1130 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
1131
Evan Cheng038dcc52007-09-28 19:24:24 +00001132 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +00001133
1134#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +00001135 if (SuccSU->NumPredsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001136 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001137 SuccSU->dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001138 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001139 assert(0);
1140 }
1141#endif
1142
Evan Cheng038dcc52007-09-28 19:24:24 +00001143 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001144 SuccSU->isAvailable = true;
1145 AvailableQueue->push(SuccSU);
1146 }
1147}
1148
1149
1150/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1151/// count of its successors. If a successor pending count is zero, add it to
1152/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +00001153void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001154 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Evan Chengd38c22b2006-05-11 23:55:42 +00001155 DEBUG(SU->dump(&DAG));
1156 SU->Cycle = CurCycle;
1157
1158 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001159
1160 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +00001161 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1162 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001163 ReleaseSucc(I->Dep, I->isCtrl, CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +00001164 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +00001165}
1166
Dan Gohman54a187e2007-08-20 19:28:38 +00001167/// ListScheduleTopDown - The main loop of list scheduling for top-down
1168/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001169void ScheduleDAGRRList::ListScheduleTopDown() {
1170 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001171
1172 // All leaves to Available queue.
1173 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1174 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001175 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001176 AvailableQueue->push(&SUnits[i]);
1177 SUnits[i].isAvailable = true;
1178 }
1179 }
1180
Evan Chengd38c22b2006-05-11 23:55:42 +00001181 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001182 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +00001183 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +00001184 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001185 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001186 SUnit *CurSU = AvailableQueue->pop();
1187 while (CurSU && CurSU->CycleBound > CurCycle) {
1188 NotReady.push_back(CurSU);
1189 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +00001190 }
1191
1192 // Add the nodes that aren't ready back onto the available list.
1193 AvailableQueue->push_all(NotReady);
1194 NotReady.clear();
1195
Evan Cheng5924bf72007-09-25 01:54:36 +00001196 if (!CurSU)
1197 Sequence.push_back(0);
1198 else {
1199 ScheduleNodeTopDown(CurSU, CurCycle);
1200 Sequence.push_back(CurSU);
1201 }
Dan Gohman4370f262008-04-15 01:22:18 +00001202 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001203 }
1204
1205
1206#ifndef NDEBUG
1207 // Verify that all SUnits were scheduled.
1208 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001209 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001210 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001211 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1212 if (!SUnits[i].isScheduled) {
Dan Gohman4370f262008-04-15 01:22:18 +00001213 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1214 ++DeadNodes;
1215 continue;
1216 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001217 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001218 cerr << "*** List scheduling failed! ***\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001219 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001220 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001221 AnyNotSched = true;
1222 }
Dan Gohman4370f262008-04-15 01:22:18 +00001223 if (SUnits[i].NumPredsLeft != 0) {
1224 if (!AnyNotSched)
1225 cerr << "*** List scheduling failed! ***\n";
1226 SUnits[i].dump(&DAG);
1227 cerr << "has predecessors left!\n";
1228 AnyNotSched = true;
1229 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001230 }
Dan Gohman82b66732008-04-15 22:40:14 +00001231 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1232 if (!Sequence[i])
1233 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001234 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001235 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001236 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001237#endif
1238}
1239
1240
1241
1242//===----------------------------------------------------------------------===//
1243// RegReductionPriorityQueue Implementation
1244//===----------------------------------------------------------------------===//
1245//
1246// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1247// to reduce register pressure.
1248//
1249namespace {
1250 template<class SF>
1251 class RegReductionPriorityQueue;
1252
1253 /// Sorting functions for the Available queue.
1254 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1255 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1256 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1257 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1258
1259 bool operator()(const SUnit* left, const SUnit* right) const;
1260 };
1261
Evan Cheng7e4abde2008-07-02 09:23:51 +00001262 struct bu_ls_rr_fast_sort : public std::binary_function<SUnit*, SUnit*, bool>{
1263 RegReductionPriorityQueue<bu_ls_rr_fast_sort> *SPQ;
1264 bu_ls_rr_fast_sort(RegReductionPriorityQueue<bu_ls_rr_fast_sort> *spq)
1265 : SPQ(spq) {}
1266 bu_ls_rr_fast_sort(const bu_ls_rr_fast_sort &RHS) : SPQ(RHS.SPQ) {}
1267
1268 bool operator()(const SUnit* left, const SUnit* right) const;
1269 };
1270
Evan Chengd38c22b2006-05-11 23:55:42 +00001271 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1272 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1273 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1274 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1275
1276 bool operator()(const SUnit* left, const SUnit* right) const;
1277 };
1278} // end anonymous namespace
1279
Evan Cheng961bbd32007-01-08 23:50:38 +00001280static inline bool isCopyFromLiveIn(const SUnit *SU) {
1281 SDNode *N = SU->Node;
Evan Cheng8e136a92007-09-26 21:36:17 +00001282 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +00001283 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
1284}
1285
Evan Cheng7e4abde2008-07-02 09:23:51 +00001286/// CalcNodeBUSethiUllmanNumber - Compute Sethi Ullman number for bottom up
1287/// scheduling. Smaller number is the higher priority.
1288static unsigned
1289CalcNodeBUSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1290 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1291 if (SethiUllmanNumber != 0)
1292 return SethiUllmanNumber;
1293
1294 unsigned Extra = 0;
1295 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1296 I != E; ++I) {
1297 if (I->isCtrl) continue; // ignore chain preds
1298 SUnit *PredSU = I->Dep;
1299 unsigned PredSethiUllman = CalcNodeBUSethiUllmanNumber(PredSU, SUNumbers);
1300 if (PredSethiUllman > SethiUllmanNumber) {
1301 SethiUllmanNumber = PredSethiUllman;
1302 Extra = 0;
1303 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1304 ++Extra;
1305 }
1306
1307 SethiUllmanNumber += Extra;
1308
1309 if (SethiUllmanNumber == 0)
1310 SethiUllmanNumber = 1;
1311
1312 return SethiUllmanNumber;
1313}
1314
1315/// CalcNodeTDSethiUllmanNumber - Compute Sethi Ullman number for top down
1316/// scheduling. Smaller number is the higher priority.
1317static unsigned
1318CalcNodeTDSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1319 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1320 if (SethiUllmanNumber != 0)
1321 return SethiUllmanNumber;
1322
1323 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
1324 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1325 SethiUllmanNumber = 0xffff;
1326 else if (SU->NumSuccsLeft == 0)
1327 // If SU does not have a use, i.e. it doesn't produce a value that would
1328 // be consumed (e.g. store), then it terminates a chain of computation.
1329 // Give it a small SethiUllman number so it will be scheduled right before
1330 // its predecessors that it doesn't lengthen their live ranges.
1331 SethiUllmanNumber = 0;
1332 else if (SU->NumPredsLeft == 0 &&
1333 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
1334 SethiUllmanNumber = 0xffff;
1335 else {
1336 int Extra = 0;
1337 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1338 I != E; ++I) {
1339 if (I->isCtrl) continue; // ignore chain preds
1340 SUnit *PredSU = I->Dep;
1341 unsigned PredSethiUllman = CalcNodeTDSethiUllmanNumber(PredSU, SUNumbers);
1342 if (PredSethiUllman > SethiUllmanNumber) {
1343 SethiUllmanNumber = PredSethiUllman;
1344 Extra = 0;
1345 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1346 ++Extra;
1347 }
1348
1349 SethiUllmanNumber += Extra;
1350 }
1351
1352 return SethiUllmanNumber;
1353}
1354
1355
Evan Chengd38c22b2006-05-11 23:55:42 +00001356namespace {
1357 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001358 class VISIBILITY_HIDDEN RegReductionPriorityQueue
1359 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +00001360 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +00001361 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001362
1363 public:
1364 RegReductionPriorityQueue() :
Roman Levenstein6b371142008-04-29 09:07:59 +00001365 Queue(SF(this)), currentQueueId(0) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001366
Dan Gohman46520a22008-06-21 19:18:17 +00001367 virtual void initNodes(std::vector<SUnit> &sunits) {}
Evan Cheng5924bf72007-09-25 01:54:36 +00001368
1369 virtual void addNode(const SUnit *SU) {}
1370
1371 virtual void updateNode(const SUnit *SU) {}
1372
Evan Chengd38c22b2006-05-11 23:55:42 +00001373 virtual void releaseState() {}
1374
Evan Cheng6730f032007-01-08 23:55:53 +00001375 virtual unsigned getNodePriority(const SUnit *SU) const {
Evan Chengd38c22b2006-05-11 23:55:42 +00001376 return 0;
1377 }
1378
Evan Cheng5924bf72007-09-25 01:54:36 +00001379 unsigned size() const { return Queue.size(); }
1380
Evan Chengd38c22b2006-05-11 23:55:42 +00001381 bool empty() const { return Queue.empty(); }
1382
1383 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001384 assert(!U->NodeQueueId && "Node in the queue already");
1385 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001386 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001387 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001388
Evan Chengd38c22b2006-05-11 23:55:42 +00001389 void push_all(const std::vector<SUnit *> &Nodes) {
1390 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001391 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001392 }
1393
1394 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001395 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001396 SUnit *V = Queue.top();
1397 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001398 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001399 return V;
1400 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001401
Evan Cheng5924bf72007-09-25 01:54:36 +00001402 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001403 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001404 assert(SU->NodeQueueId != 0 && "Not in queue!");
1405 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001406 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001407 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001408 };
1409
Chris Lattner996795b2006-06-28 23:17:24 +00001410 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001411 : public RegReductionPriorityQueue<bu_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001412 // SUnits - The SUnits for the current graph.
1413 const std::vector<SUnit> *SUnits;
1414
1415 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001416 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001417
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001418 const TargetInstrInfo *TII;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001419 const TargetRegisterInfo *TRI;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001420 ScheduleDAGRRList *scheduleDAG;
Evan Cheng2c977312008-07-01 18:05:03 +00001421
Evan Chengd38c22b2006-05-11 23:55:42 +00001422 public:
Evan Chengf9891412007-12-20 09:25:31 +00001423 explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001424 const TargetRegisterInfo *tri)
1425 : TII(tii), TRI(tri), scheduleDAG(NULL) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001426
Dan Gohman46520a22008-06-21 19:18:17 +00001427 void initNodes(std::vector<SUnit> &sunits) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001428 SUnits = &sunits;
1429 // Add pseudo dependency edges for two-address nodes.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001430 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001431 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001432 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001433 }
1434
Evan Cheng5924bf72007-09-25 01:54:36 +00001435 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001436 unsigned SUSize = SethiUllmanNumbers.size();
1437 if (SUnits->size() > SUSize)
1438 SethiUllmanNumbers.resize(SUSize*2, 0);
1439 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001440 }
1441
1442 void updateNode(const SUnit *SU) {
1443 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001444 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001445 }
1446
Evan Chengd38c22b2006-05-11 23:55:42 +00001447 void releaseState() {
1448 SUnits = 0;
1449 SethiUllmanNumbers.clear();
1450 }
1451
Evan Cheng6730f032007-01-08 23:55:53 +00001452 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001453 assert(SU->NodeNum < SethiUllmanNumbers.size());
Evan Cheng8e136a92007-09-26 21:36:17 +00001454 unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001455 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1456 // CopyFromReg should be close to its def because it restricts
1457 // allocation choices. But if it is a livein then perhaps we want it
1458 // closer to its uses so it can be coalesced.
1459 return 0xffff;
1460 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1461 // CopyToReg should be close to its uses to facilitate coalescing and
1462 // avoid spilling.
1463 return 0;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001464 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1465 Opc == TargetInstrInfo::INSERT_SUBREG)
1466 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1467 // facilitate coalescing.
1468 return 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001469 else if (SU->NumSuccs == 0)
1470 // If SU does not have a use, i.e. it doesn't produce a value that would
1471 // be consumed (e.g. store), then it terminates a chain of computation.
1472 // Give it a large SethiUllman number so it will be scheduled right
1473 // before its predecessors that it doesn't lengthen their live ranges.
1474 return 0xffff;
1475 else if (SU->NumPreds == 0)
1476 // If SU does not have a def, schedule it close to its uses because it
1477 // does not lengthen any live ranges.
1478 return 0;
1479 else
1480 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001481 }
1482
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001483 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1484 scheduleDAG = scheduleDag;
1485 }
1486
Evan Chengd38c22b2006-05-11 23:55:42 +00001487 private:
Evan Cheng73bdf042008-03-01 00:39:47 +00001488 bool canClobber(const SUnit *SU, const SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +00001489 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001490 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001491 };
1492
1493
1494 class VISIBILITY_HIDDEN BURegReductionFastPriorityQueue
1495 : public RegReductionPriorityQueue<bu_ls_rr_fast_sort> {
1496 // SUnits - The SUnits for the current graph.
1497 const std::vector<SUnit> *SUnits;
1498
1499 // SethiUllmanNumbers - The SethiUllman number for each node.
1500 std::vector<unsigned> SethiUllmanNumbers;
1501 public:
1502 explicit BURegReductionFastPriorityQueue() {}
1503
1504 void initNodes(std::vector<SUnit> &sunits) {
1505 SUnits = &sunits;
1506 // Calculate node priorities.
1507 CalculateSethiUllmanNumbers();
1508 }
1509
1510 void addNode(const SUnit *SU) {
1511 unsigned SUSize = SethiUllmanNumbers.size();
1512 if (SUnits->size() > SUSize)
1513 SethiUllmanNumbers.resize(SUSize*2, 0);
1514 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1515 }
1516
1517 void updateNode(const SUnit *SU) {
1518 SethiUllmanNumbers[SU->NodeNum] = 0;
1519 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1520 }
1521
1522 void releaseState() {
1523 SUnits = 0;
1524 SethiUllmanNumbers.clear();
1525 }
1526
1527 unsigned getNodePriority(const SUnit *SU) const {
1528 return SethiUllmanNumbers[SU->NodeNum];
1529 }
1530
1531 private:
1532 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001533 };
1534
1535
Dan Gohman54a187e2007-08-20 19:28:38 +00001536 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001537 : public RegReductionPriorityQueue<td_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001538 // SUnits - The SUnits for the current graph.
1539 const std::vector<SUnit> *SUnits;
1540
1541 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001542 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001543
1544 public:
1545 TDRegReductionPriorityQueue() {}
1546
Dan Gohman46520a22008-06-21 19:18:17 +00001547 void initNodes(std::vector<SUnit> &sunits) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001548 SUnits = &sunits;
1549 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001550 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001551 }
1552
Evan Cheng5924bf72007-09-25 01:54:36 +00001553 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001554 unsigned SUSize = SethiUllmanNumbers.size();
1555 if (SUnits->size() > SUSize)
1556 SethiUllmanNumbers.resize(SUSize*2, 0);
1557 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001558 }
1559
1560 void updateNode(const SUnit *SU) {
1561 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001562 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001563 }
1564
Evan Chengd38c22b2006-05-11 23:55:42 +00001565 void releaseState() {
1566 SUnits = 0;
1567 SethiUllmanNumbers.clear();
1568 }
1569
Evan Cheng6730f032007-01-08 23:55:53 +00001570 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001571 assert(SU->NodeNum < SethiUllmanNumbers.size());
1572 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001573 }
1574
1575 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001576 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001577 };
1578}
1579
Evan Chengb9e3db62007-03-14 22:43:40 +00001580/// closestSucc - Returns the scheduled cycle of the successor which is
1581/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001582static unsigned closestSucc(const SUnit *SU) {
1583 unsigned MaxCycle = 0;
1584 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001585 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001586 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001587 // If there are bunch of CopyToRegs stacked up, they should be considered
1588 // to be at the same position.
Evan Cheng8e136a92007-09-26 21:36:17 +00001589 if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001590 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001591 if (Cycle > MaxCycle)
1592 MaxCycle = Cycle;
1593 }
Evan Cheng28748552007-03-13 23:25:11 +00001594 return MaxCycle;
1595}
1596
Evan Cheng61bc51e2007-12-20 02:22:36 +00001597/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1598/// for scratch registers. Live-in operands and live-out results don't count
1599/// since they are "fixed".
1600static unsigned calcMaxScratches(const SUnit *SU) {
1601 unsigned Scratches = 0;
1602 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1603 I != E; ++I) {
1604 if (I->isCtrl) continue; // ignore chain preds
Evan Cheng0e400d42008-01-09 23:01:55 +00001605 if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001606 Scratches++;
1607 }
1608 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1609 I != E; ++I) {
1610 if (I->isCtrl) continue; // ignore chain succs
Evan Cheng0e400d42008-01-09 23:01:55 +00001611 if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001612 Scratches += 10;
1613 }
1614 return Scratches;
1615}
1616
Evan Chengd38c22b2006-05-11 23:55:42 +00001617// Bottom up
1618bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001619 unsigned LPriority = SPQ->getNodePriority(left);
1620 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001621 if (LPriority != RPriority)
1622 return LPriority > RPriority;
1623
1624 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1625 // e.g.
1626 // t1 = op t2, c1
1627 // t3 = op t4, c2
1628 //
1629 // and the following instructions are both ready.
1630 // t2 = op c3
1631 // t4 = op c4
1632 //
1633 // Then schedule t2 = op first.
1634 // i.e.
1635 // t4 = op c4
1636 // t2 = op c3
1637 // t1 = op t2, c1
1638 // t3 = op t4, c2
1639 //
1640 // This creates more short live intervals.
1641 unsigned LDist = closestSucc(left);
1642 unsigned RDist = closestSucc(right);
1643 if (LDist != RDist)
1644 return LDist < RDist;
1645
1646 // Intuitively, it's good to push down instructions whose results are
1647 // liveout so their long live ranges won't conflict with other values
1648 // which are needed inside the BB. Further prioritize liveout instructions
1649 // by the number of operands which are calculated within the BB.
1650 unsigned LScratch = calcMaxScratches(left);
1651 unsigned RScratch = calcMaxScratches(right);
1652 if (LScratch != RScratch)
1653 return LScratch > RScratch;
1654
1655 if (left->Height != right->Height)
1656 return left->Height > right->Height;
1657
1658 if (left->Depth != right->Depth)
1659 return left->Depth < right->Depth;
1660
1661 if (left->CycleBound != right->CycleBound)
1662 return left->CycleBound > right->CycleBound;
1663
Roman Levenstein6b371142008-04-29 09:07:59 +00001664 assert(left->NodeQueueId && right->NodeQueueId &&
1665 "NodeQueueId cannot be zero");
1666 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001667}
1668
Dan Gohman4b49be12008-06-21 01:08:22 +00001669bool
Evan Cheng7e4abde2008-07-02 09:23:51 +00001670bu_ls_rr_fast_sort::operator()(const SUnit *left, const SUnit *right) const {
1671 unsigned LPriority = SPQ->getNodePriority(left);
1672 unsigned RPriority = SPQ->getNodePriority(right);
1673 if (LPriority != RPriority)
1674 return LPriority > RPriority;
1675 assert(left->NodeQueueId && right->NodeQueueId &&
1676 "NodeQueueId cannot be zero");
1677 return (left->NodeQueueId > right->NodeQueueId);
1678}
1679
1680bool
Dan Gohman4b49be12008-06-21 01:08:22 +00001681BURegReductionPriorityQueue::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001682 if (SU->isTwoAddress) {
1683 unsigned Opc = SU->Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001684 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001685 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001686 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001687 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001688 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001689 SDNode *DU = SU->Node->getOperand(i).Val;
Dan Gohman46520a22008-06-21 19:18:17 +00001690 if (DU->getNodeId() != -1 &&
1691 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001692 return true;
1693 }
1694 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001695 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001696 return false;
1697}
1698
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001699
Evan Chenga5e595d2007-09-28 22:32:30 +00001700/// hasCopyToRegUse - Return true if SU has a value successor that is a
1701/// CopyToReg node.
1702static bool hasCopyToRegUse(SUnit *SU) {
1703 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1704 I != E; ++I) {
1705 if (I->isCtrl) continue;
1706 SUnit *SuccSU = I->Dep;
1707 if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg)
1708 return true;
1709 }
1710 return false;
1711}
1712
Evan Chengf9891412007-12-20 09:25:31 +00001713/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001714/// physical register defs.
Evan Chengf9891412007-12-20 09:25:31 +00001715static bool canClobberPhysRegDefs(SUnit *SuccSU, SUnit *SU,
1716 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001717 const TargetRegisterInfo *TRI) {
Evan Chengf9891412007-12-20 09:25:31 +00001718 SDNode *N = SuccSU->Node;
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001719 unsigned NumDefs = TII->get(N->getTargetOpcode()).getNumDefs();
1720 const unsigned *ImpDefs = TII->get(N->getTargetOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001721 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001722 const unsigned *SUImpDefs =
1723 TII->get(SU->Node->getTargetOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001724 if (!SUImpDefs)
1725 return false;
1726 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001727 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001728 if (VT == MVT::Flag || VT == MVT::Other)
1729 continue;
1730 unsigned Reg = ImpDefs[i - NumDefs];
1731 for (;*SUImpDefs; ++SUImpDefs) {
1732 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001733 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001734 return true;
1735 }
1736 }
1737 return false;
1738}
1739
Evan Chengd38c22b2006-05-11 23:55:42 +00001740/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1741/// it as a def&use operand. Add a pseudo control edge from it to the other
1742/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001743/// first (lower in the schedule). If both nodes are two-address, favor the
1744/// one that has a CopyToReg use (more likely to be a loop induction update).
1745/// If both are two-address, but one is commutable while the other is not
1746/// commutable, favor the one that's not commutable.
Dan Gohman4b49be12008-06-21 01:08:22 +00001747void BURegReductionPriorityQueue::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001748 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1749 SUnit *SU = (SUnit *)&((*SUnits)[i]);
1750 if (!SU->isTwoAddress)
1751 continue;
1752
1753 SDNode *Node = SU->Node;
Evan Chenga5e595d2007-09-28 22:32:30 +00001754 if (!Node || !Node->isTargetOpcode() || SU->FlaggedNodes.size() > 0)
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001755 continue;
1756
1757 unsigned Opc = Node->getTargetOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001758 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001759 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001760 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001761 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001762 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001763 SDNode *DU = SU->Node->getOperand(j).Val;
Dan Gohman46520a22008-06-21 19:18:17 +00001764 if (DU->getNodeId() == -1)
Evan Cheng1bf166312007-11-09 01:27:11 +00001765 continue;
Dan Gohman46520a22008-06-21 19:18:17 +00001766 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
Evan Chengf24d15f2006-11-06 21:33:46 +00001767 if (!DUSU) continue;
Dan Gohman46520a22008-06-21 19:18:17 +00001768 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1769 E = DUSU->Succs.end(); I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001770 if (I->isCtrl) continue;
1771 SUnit *SuccSU = I->Dep;
Evan Chengf9891412007-12-20 09:25:31 +00001772 if (SuccSU == SU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001773 continue;
Evan Cheng2dbffa42007-11-06 08:44:59 +00001774 // Be conservative. Ignore if nodes aren't at roughly the same
1775 // depth and height.
1776 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1777 continue;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001778 if (!SuccSU->Node || !SuccSU->Node->isTargetOpcode())
1779 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001780 // Don't constrain nodes with physical register defs if the
Dan Gohmancf8827a2008-01-29 12:43:50 +00001781 // predecessor can clobber them.
Evan Chengf9891412007-12-20 09:25:31 +00001782 if (SuccSU->hasPhysRegDefs) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001783 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Chengf9891412007-12-20 09:25:31 +00001784 continue;
1785 }
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001786 // Don't constraint extract_subreg / insert_subreg these may be
1787 // coalesced away. We don't them close to their uses.
1788 unsigned SuccOpc = SuccSU->Node->getTargetOpcode();
1789 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1790 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1791 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +00001792 if ((!canClobber(SuccSU, DUSU) ||
Evan Chenga5e595d2007-09-28 22:32:30 +00001793 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
Evan Cheng5924bf72007-09-25 01:54:36 +00001794 (!SU->isCommutable && SuccSU->isCommutable)) &&
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001795 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001796 DOUT << "Adding an edge from SU # " << SU->NodeNum
1797 << " to SU #" << SuccSU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001798 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001799 }
1800 }
1801 }
1802 }
1803 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001804}
1805
Evan Cheng6730f032007-01-08 23:55:53 +00001806/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1807/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001808void BURegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001809 SethiUllmanNumbers.assign(SUnits->size(), 0);
1810
1811 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001812 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
1813}
1814void BURegReductionFastPriorityQueue::CalculateSethiUllmanNumbers() {
1815 SethiUllmanNumbers.assign(SUnits->size(), 0);
1816
1817 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1818 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001819}
1820
Roman Levenstein30d09512008-03-27 09:44:37 +00001821/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001822/// predecessors of the successors of the SUnit SU. Stop when the provided
1823/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001824static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1825 unsigned Limit) {
1826 unsigned Sum = 0;
1827 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1828 I != E; ++I) {
1829 SUnit *SuccSU = I->Dep;
1830 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1831 EE = SuccSU->Preds.end(); II != EE; ++II) {
1832 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001833 if (!PredSU->isScheduled)
1834 if (++Sum > Limit)
1835 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001836 }
1837 }
1838 return Sum;
1839}
1840
Evan Chengd38c22b2006-05-11 23:55:42 +00001841
1842// Top down
1843bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001844 unsigned LPriority = SPQ->getNodePriority(left);
1845 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng8e136a92007-09-26 21:36:17 +00001846 bool LIsTarget = left->Node && left->Node->isTargetOpcode();
1847 bool RIsTarget = right->Node && right->Node->isTargetOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001848 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1849 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001850 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1851 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001852
1853 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1854 return false;
1855 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1856 return true;
1857
Evan Chengd38c22b2006-05-11 23:55:42 +00001858 if (LIsFloater)
1859 LBonus -= 2;
1860 if (RIsFloater)
1861 RBonus -= 2;
1862 if (left->NumSuccs == 1)
1863 LBonus += 2;
1864 if (right->NumSuccs == 1)
1865 RBonus += 2;
1866
Evan Cheng73bdf042008-03-01 00:39:47 +00001867 if (LPriority+LBonus != RPriority+RBonus)
1868 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001869
Evan Cheng73bdf042008-03-01 00:39:47 +00001870 if (left->Depth != right->Depth)
1871 return left->Depth < right->Depth;
1872
1873 if (left->NumSuccsLeft != right->NumSuccsLeft)
1874 return left->NumSuccsLeft > right->NumSuccsLeft;
1875
1876 if (left->CycleBound != right->CycleBound)
1877 return left->CycleBound > right->CycleBound;
1878
Roman Levenstein6b371142008-04-29 09:07:59 +00001879 assert(left->NodeQueueId && right->NodeQueueId &&
1880 "NodeQueueId cannot be zero");
1881 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001882}
1883
Evan Cheng6730f032007-01-08 23:55:53 +00001884/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1885/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001886void TDRegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001887 SethiUllmanNumbers.assign(SUnits->size(), 0);
1888
1889 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001890 CalcNodeTDSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001891}
1892
1893//===----------------------------------------------------------------------===//
1894// Public Constructor Functions
1895//===----------------------------------------------------------------------===//
1896
Jim Laskey03593f72006-08-01 18:29:48 +00001897llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1898 SelectionDAG *DAG,
Evan Cheng2c977312008-07-01 18:05:03 +00001899 MachineBasicBlock *BB,
1900 bool Fast) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001901 if (Fast)
1902 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true, true,
1903 new BURegReductionFastPriorityQueue());
1904
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001905 const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo();
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001906 const TargetRegisterInfo *TRI = DAG->getTarget().getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001907
Evan Cheng7e4abde2008-07-02 09:23:51 +00001908 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001909
Evan Cheng7e4abde2008-07-02 09:23:51 +00001910 ScheduleDAGRRList *SD =
1911 new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(),true,false, PQ);
1912 PQ->setScheduleDAG(SD);
1913 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001914}
1915
Jim Laskey03593f72006-08-01 18:29:48 +00001916llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1917 SelectionDAG *DAG,
Evan Cheng2c977312008-07-01 18:05:03 +00001918 MachineBasicBlock *BB,
1919 bool Fast) {
1920 return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false, Fast,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001921 new TDRegReductionPriorityQueue());
Evan Chengd38c22b2006-05-11 23:55:42 +00001922}