blob: 15fffdfeea1d16c6d45bd0ae1db34f631b6213d7 [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
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",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
50
Evan Chengd38c22b2006-05-11 23:55:42 +000051namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000052//===----------------------------------------------------------------------===//
53/// ScheduleDAGRRList - The actual register reduction list scheduler
54/// implementation. This supports both top-down and bottom-up scheduling.
55///
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
Dan Gohmanc07f6862008-09-23 18:50:48 +000069 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000070 /// that are "live". These nodes must be scheduled before any other nodes that
71 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000072 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000073 std::vector<SUnit*> LiveRegDefs;
74 std::vector<unsigned> LiveRegCycles;
75
Evan Chengd38c22b2006-05-11 23:55:42 +000076public:
Dan Gohman5a390b92008-11-13 21:21:28 +000077 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.
Dan Gohmane955c482008-08-05 14:45:15 +000091 bool IsReachable(const SUnit *SU, const SUnit *TargetSU);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000092
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.
Dan Gohmane955c482008-08-05 14:45:15 +0000158 void DFS(const SUnit *SU, int UpperBound, bool& HasLoop);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000159
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 Gohmanc07f6862008-09-23 18:50:48 +0000181 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000182 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
183 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000184
Evan Chengd38c22b2006-05-11 23:55:42 +0000185 // Build scheduling units.
186 BuildSchedUnits();
187
Evan Chengd38c22b2006-05-11 23:55:42 +0000188 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman5a390b92008-11-13 21:21:28 +0000189 SUnits[su].dumpAll(DAG));
Evan Cheng2c977312008-07-01 18:05:03 +0000190 if (!Fast) {
191 CalculateDepths();
192 CalculateHeights();
193 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000194 InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000195
Dan Gohman46520a22008-06-21 19:18:17 +0000196 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000197
Evan Chengd38c22b2006-05-11 23:55:42 +0000198 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
199 if (isBottomUp)
200 ListScheduleBottomUp();
201 else
202 ListScheduleTopDown();
203
204 AvailableQueue->releaseState();
Evan Cheng2c977312008-07-01 18:05:03 +0000205
206 if (!Fast)
207 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000208}
209
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000210/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000211/// it is not the last use of its first operand, add it to the CommuteSet if
212/// possible. It will be commuted when it is translated to a MI.
213void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000214 SmallPtrSet<SUnit*, 4> OperandSeen;
Dan Gohman4370f262008-04-15 01:22:18 +0000215 for (unsigned i = Sequence.size(); i != 0; ) {
216 --i;
Evan Chengafed73e2006-05-12 01:58:24 +0000217 SUnit *SU = Sequence[i];
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000218 if (!SU || !SU->getNode()) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000219 if (SU->isCommutable) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000220 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000221 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000222 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000223 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000224 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000225 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000226 continue;
227
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000228 SDNode *OpN = SU->getNode()->getOperand(j).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000229 SUnit *OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000230 if (OpSU && OperandSeen.count(OpSU) == 1) {
231 // Ok, so SU is not the last use of OpSU, but SU is two-address so
232 // it will clobber OpSU. Try to commute SU if no other source operands
233 // are live below.
234 bool DoCommute = true;
235 for (unsigned k = 0; k < NumOps; ++k) {
236 if (k != j) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000237 OpN = SU->getNode()->getOperand(k).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000238 OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000239 if (OpSU && OperandSeen.count(OpSU) == 1) {
240 DoCommute = false;
241 break;
242 }
243 }
Evan Chengafed73e2006-05-12 01:58:24 +0000244 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000245 if (DoCommute)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000246 CommuteSet.insert(SU->getNode());
Evan Chengafed73e2006-05-12 01:58:24 +0000247 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000248
249 // Only look at the first use&def node for now.
250 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000251 }
252 }
253
Chris Lattnerd86418a2006-08-17 00:09:56 +0000254 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
255 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000256 if (!I->isCtrl)
Dan Gohmane6e13482008-06-21 15:52:51 +0000257 OperandSeen.insert(I->Dep->OrigNode);
Evan Chengafed73e2006-05-12 01:58:24 +0000258 }
259 }
260}
Evan Chengd38c22b2006-05-11 23:55:42 +0000261
262//===----------------------------------------------------------------------===//
263// Bottom-Up Scheduling
264//===----------------------------------------------------------------------===//
265
Evan Chengd38c22b2006-05-11 23:55:42 +0000266/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000267/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +0000268void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain,
269 unsigned CurCycle) {
270 // FIXME: the distance between two nodes is not always == the predecessor's
271 // latency. For example, the reader can very well read the register written
272 // by the predecessor later than the issue cycle. It also depends on the
273 // interrupt model (drain vs. freeze).
274 PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
275
Evan Cheng038dcc52007-09-28 19:24:24 +0000276 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000277
278#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000279 if (PredSU->NumSuccsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000280 cerr << "*** List scheduling failed! ***\n";
Dan Gohman5a390b92008-11-13 21:21:28 +0000281 PredSU->dump(DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000282 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000283 assert(0);
284 }
285#endif
286
Evan Cheng038dcc52007-09-28 19:24:24 +0000287 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000288 PredSU->isAvailable = true;
289 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000290 }
291}
292
293/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
294/// count of its predecessors. If a predecessor pending count is zero, add it to
295/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000296void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000297 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman5a390b92008-11-13 21:21:28 +0000298 DEBUG(SU->dump(DAG));
Evan Chengd38c22b2006-05-11 23:55:42 +0000299 SU->Cycle = CurCycle;
300
301 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000302
303 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000304 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000305 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000306 ReleasePred(I->Dep, I->isCtrl, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000307 if (I->Cost < 0) {
308 // This is a physical register dependency and it's impossible or
309 // expensive to copy the register. Make sure nothing that can
310 // clobber the register is scheduled between the predecessor and
311 // this node.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000312 if (!LiveRegDefs[I->Reg]) {
313 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000314 LiveRegDefs[I->Reg] = I->Dep;
315 LiveRegCycles[I->Reg] = CurCycle;
316 }
317 }
318 }
319
320 // Release all the implicit physical register defs that are live.
321 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
322 I != E; ++I) {
323 if (I->Cost < 0) {
324 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000325 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000326 assert(LiveRegDefs[I->Reg] == SU &&
327 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000328 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000329 LiveRegDefs[I->Reg] = NULL;
330 LiveRegCycles[I->Reg] = 0;
331 }
332 }
333 }
334
Evan Chengd38c22b2006-05-11 23:55:42 +0000335 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000336}
337
Evan Cheng5924bf72007-09-25 01:54:36 +0000338/// CapturePred - This does the opposite of ReleasePred. Since SU is being
339/// unscheduled, incrcease the succ left count of its predecessors. Remove
340/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000341void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
342 unsigned CycleBound = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000343 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
344 I != E; ++I) {
345 if (I->Dep == SU)
346 continue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000347 CycleBound = std::max(CycleBound,
348 I->Dep->Cycle + PredSU->Latency);
Evan Cheng5924bf72007-09-25 01:54:36 +0000349 }
350
351 if (PredSU->isAvailable) {
352 PredSU->isAvailable = false;
353 if (!PredSU->isPending)
354 AvailableQueue->remove(PredSU);
355 }
356
Roman Levenstein6b371142008-04-29 09:07:59 +0000357 PredSU->CycleBound = CycleBound;
Evan Cheng038dcc52007-09-28 19:24:24 +0000358 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000359}
360
361/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
362/// its predecessor states to reflect the change.
363void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
364 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
Dan Gohman5a390b92008-11-13 21:21:28 +0000365 DEBUG(SU->dump(DAG));
Evan Cheng5924bf72007-09-25 01:54:36 +0000366
367 AvailableQueue->UnscheduledNode(SU);
368
369 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
370 I != E; ++I) {
371 CapturePred(I->Dep, SU, I->isCtrl);
372 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000373 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000374 assert(LiveRegDefs[I->Reg] == I->Dep &&
375 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000376 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000377 LiveRegDefs[I->Reg] = NULL;
378 LiveRegCycles[I->Reg] = 0;
379 }
380 }
381
382 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
383 I != E; ++I) {
384 if (I->Cost < 0) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000385 if (!LiveRegDefs[I->Reg]) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000386 LiveRegDefs[I->Reg] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000387 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000388 }
389 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
390 LiveRegCycles[I->Reg] = I->Dep->Cycle;
391 }
392 }
393
394 SU->Cycle = 0;
395 SU->isScheduled = false;
396 SU->isAvailable = true;
397 AvailableQueue->push(SU);
398}
399
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000400/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane955c482008-08-05 14:45:15 +0000401bool ScheduleDAGRRList::IsReachable(const SUnit *SU, const SUnit *TargetSU) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000402 // If insertion of the edge SU->TargetSU would create a cycle
403 // then there is a path from TargetSU to SU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000404 int UpperBound, LowerBound;
405 LowerBound = Node2Index[TargetSU->NodeNum];
406 UpperBound = Node2Index[SU->NodeNum];
407 bool HasLoop = false;
408 // Is Ord(TargetSU) < Ord(SU) ?
409 if (LowerBound < UpperBound) {
410 Visited.reset();
411 // There may be a path from TargetSU to SU. Check for it.
412 DFS(TargetSU, UpperBound, HasLoop);
Evan Chengcfd5f822007-09-27 00:25:29 +0000413 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000414 return HasLoop;
Evan Chengcfd5f822007-09-27 00:25:29 +0000415}
416
Roman Levenstein733a4d62008-03-26 11:23:38 +0000417/// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000418inline void ScheduleDAGRRList::Allocate(int n, int index) {
419 Node2Index[n] = index;
420 Index2Node[index] = n;
Evan Chengcfd5f822007-09-27 00:25:29 +0000421}
422
Roman Levenstein733a4d62008-03-26 11:23:38 +0000423/// InitDAGTopologicalSorting - create the initial topological
424/// ordering from the DAG to be scheduled.
Evan Cheng2c977312008-07-01 18:05:03 +0000425
426/// The idea of the algorithm is taken from
427/// "Online algorithms for managing the topological order of
428/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
429/// This is the MNR algorithm, which was first introduced by
430/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
431/// "Maintaining a topological order under edge insertions".
432///
433/// Short description of the algorithm:
434///
435/// Topological ordering, ord, of a DAG maps each node to a topological
436/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
437///
438/// This means that if there is a path from the node X to the node Z,
439/// then ord(X) < ord(Z).
440///
441/// This property can be used to check for reachability of nodes:
442/// if Z is reachable from X, then an insertion of the edge Z->X would
443/// create a cycle.
444///
445/// The algorithm first computes a topological ordering for the DAG by
446/// initializing the Index2Node and Node2Index arrays and then tries to keep
447/// the ordering up-to-date after edge insertions by reordering the DAG.
448///
449/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
450/// the nodes reachable from Y, and then shifts them using Shift to lie
451/// immediately after X in Index2Node.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000452void ScheduleDAGRRList::InitDAGTopologicalSorting() {
453 unsigned DAGSize = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000454 std::vector<SUnit*> WorkList;
455 WorkList.reserve(DAGSize);
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000456
457 Index2Node.resize(DAGSize);
458 Node2Index.resize(DAGSize);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000459
Roman Levenstein733a4d62008-03-26 11:23:38 +0000460 // Initialize the data structures.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000461 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
462 SUnit *SU = &SUnits[i];
463 int NodeNum = SU->NodeNum;
464 unsigned Degree = SU->Succs.size();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000465 // Temporarily use the Node2Index array as scratch space for degree counts.
466 Node2Index[NodeNum] = Degree;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000467
468 // Is it a node without dependencies?
469 if (Degree == 0) {
470 assert(SU->Succs.empty() && "SUnit should have no successors");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000471 // Collect leaf nodes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000472 WorkList.push_back(SU);
473 }
474 }
475
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000476 int Id = DAGSize;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000477 while (!WorkList.empty()) {
478 SUnit *SU = WorkList.back();
479 WorkList.pop_back();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000480 Allocate(SU->NodeNum, --Id);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000481 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
482 I != E; ++I) {
483 SUnit *SU = I->Dep;
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000484 if (!--Node2Index[SU->NodeNum])
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000485 // If all dependencies of the node are processed already,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000486 // then the node can be computed now.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000487 WorkList.push_back(SU);
488 }
489 }
490
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000491 Visited.resize(DAGSize);
492
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000493#ifndef NDEBUG
494 // Check correctness of the ordering
495 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
496 SUnit *SU = &SUnits[i];
497 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
498 I != E; ++I) {
499 assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] &&
500 "Wrong topological sorting");
501 }
502 }
503#endif
504}
505
Roman Levenstein733a4d62008-03-26 11:23:38 +0000506/// AddPred - adds an edge from SUnit X to SUnit Y.
507/// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000508bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
509 unsigned PhyReg, int Cost) {
510 int UpperBound, LowerBound;
511 LowerBound = Node2Index[Y->NodeNum];
512 UpperBound = Node2Index[X->NodeNum];
513 bool HasLoop = false;
514 // Is Ord(X) < Ord(Y) ?
515 if (LowerBound < UpperBound) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000516 // Update the topological order.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000517 Visited.reset();
518 DFS(Y, UpperBound, HasLoop);
519 assert(!HasLoop && "Inserted edge creates a loop!");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000520 // Recompute topological indexes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000521 Shift(Visited, LowerBound, UpperBound);
522 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000523 // Now really insert the edge.
524 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000525}
526
Roman Levenstein733a4d62008-03-26 11:23:38 +0000527/// RemovePred - This removes the specified node N from the predecessors of
528/// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000529bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N,
530 bool isCtrl, bool isSpecial) {
531 // InitDAGTopologicalSorting();
532 return M->removePred(N, isCtrl, isSpecial);
533}
534
Roman Levenstein733a4d62008-03-26 11:23:38 +0000535/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
536/// all nodes affected by the edge insertion. These nodes will later get new
537/// topological indexes by means of the Shift method.
Dan Gohmane955c482008-08-05 14:45:15 +0000538void ScheduleDAGRRList::DFS(const SUnit *SU, int UpperBound, bool& HasLoop) {
539 std::vector<const SUnit*> WorkList;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540 WorkList.reserve(SUnits.size());
541
542 WorkList.push_back(SU);
543 while (!WorkList.empty()) {
544 SU = WorkList.back();
545 WorkList.pop_back();
546 Visited.set(SU->NodeNum);
547 for (int I = SU->Succs.size()-1; I >= 0; --I) {
548 int s = SU->Succs[I].Dep->NodeNum;
549 if (Node2Index[s] == UpperBound) {
550 HasLoop = true;
551 return;
552 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000553 // Visit successors if not already and in affected region.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000554 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
555 WorkList.push_back(SU->Succs[I].Dep);
556 }
557 }
558 }
559}
560
Roman Levenstein733a4d62008-03-26 11:23:38 +0000561/// Shift - Renumber the nodes so that the topological ordering is
562/// preserved.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000563void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound,
564 int UpperBound) {
565 std::vector<int> L;
566 int shift = 0;
567 int i;
568
569 for (i = LowerBound; i <= UpperBound; ++i) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000570 // w is node at topological index i.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000571 int w = Index2Node[i];
572 if (Visited.test(w)) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000573 // Unmark.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000574 Visited.reset(w);
575 L.push_back(w);
576 shift = shift + 1;
577 } else {
578 Allocate(w, i - shift);
579 }
580 }
581
582 for (unsigned j = 0; j < L.size(); ++j) {
583 Allocate(L[j], i - shift);
584 i = i + 1;
585 }
586}
587
588
Dan Gohmanfd227e92008-03-25 17:10:29 +0000589/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Evan Chengcfd5f822007-09-27 00:25:29 +0000590/// create a cycle.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000591bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
592 if (IsReachable(TargetSU, SU))
Evan Chengcfd5f822007-09-27 00:25:29 +0000593 return true;
594 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
595 I != E; ++I)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000596 if (I->Cost < 0 && IsReachable(TargetSU, I->Dep))
Evan Chengcfd5f822007-09-27 00:25:29 +0000597 return true;
598 return false;
599}
600
Evan Cheng8e136a92007-09-26 21:36:17 +0000601/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000602/// BTCycle in order to schedule a specific node. Returns the last unscheduled
603/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000604void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
605 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000606 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000607 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000608 OldSU = Sequence.back();
609 Sequence.pop_back();
610 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000611 // Don't try to remove SU from AvailableQueue.
612 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000613 UnscheduleNodeBottomUp(OldSU);
614 --CurCycle;
615 }
616
617
618 if (SU->isSucc(OldSU)) {
619 assert(false && "Something is wrong!");
620 abort();
621 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000622
623 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000624}
625
Evan Cheng5924bf72007-09-25 01:54:36 +0000626/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
627/// successors to the newly created node.
628SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Evan Cheng79e97132007-10-05 01:39:18 +0000629 if (SU->FlaggedNodes.size())
630 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000631
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000632 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000633 if (!N)
634 return NULL;
635
636 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000637 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000638 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000639 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000640 if (VT == MVT::Flag)
641 return NULL;
642 else if (VT == MVT::Other)
643 TryUnfold = true;
644 }
Evan Cheng79e97132007-10-05 01:39:18 +0000645 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000646 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000647 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000648 if (VT == MVT::Flag)
649 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000650 }
651
652 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000653 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000654 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000655 return NULL;
656
657 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
658 assert(NewNodes.size() == 2 && "Expected a load folding node!");
659
660 N = NewNodes[1];
661 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000662 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000663 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000664 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000665 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
666 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000667 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000668
Dan Gohmane52e0892008-11-11 21:34:44 +0000669 // LoadNode may already exist. This can happen when there is another
670 // load from the same location and producing the same type of value
671 // but it has different alignment or volatileness.
672 bool isNewLoad = true;
673 SUnit *LoadSU;
674 if (LoadNode->getNodeId() != -1) {
675 LoadSU = &SUnits[LoadNode->getNodeId()];
676 isNewLoad = false;
677 } else {
678 LoadSU = CreateNewSUnit(LoadNode);
679 LoadNode->setNodeId(LoadSU->NodeNum);
680
681 LoadSU->Depth = SU->Depth;
682 LoadSU->Height = SU->Height;
683 ComputeLatency(LoadSU);
684 }
685
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000686 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000687 assert(N->getNodeId() == -1 && "Node already inserted!");
688 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000689
Dan Gohman17059682008-07-17 19:10:17 +0000690 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000691 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000692 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000693 NewSU->isTwoAddress = true;
694 break;
695 }
696 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000697 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000698 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000699 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000700 NewSU->Depth = SU->Depth;
701 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000702 ComputeLatency(NewSU);
703
704 SUnit *ChainPred = NULL;
705 SmallVector<SDep, 4> ChainSuccs;
706 SmallVector<SDep, 4> LoadPreds;
707 SmallVector<SDep, 4> NodePreds;
708 SmallVector<SDep, 4> NodeSuccs;
709 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
710 I != E; ++I) {
711 if (I->isCtrl)
712 ChainPred = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000713 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Evan Cheng79e97132007-10-05 01:39:18 +0000714 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
715 else
716 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
717 }
718 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
719 I != E; ++I) {
720 if (I->isCtrl)
721 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
722 I->isCtrl, I->isSpecial));
723 else
724 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
725 I->isCtrl, I->isSpecial));
726 }
727
Dan Gohman4370f262008-04-15 01:22:18 +0000728 if (ChainPred) {
729 RemovePred(SU, ChainPred, true, false);
730 if (isNewLoad)
731 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000732 }
Evan Cheng79e97132007-10-05 01:39:18 +0000733 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
734 SDep *Pred = &LoadPreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000735 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
736 if (isNewLoad) {
737 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000738 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000739 }
Evan Cheng79e97132007-10-05 01:39:18 +0000740 }
741 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
742 SDep *Pred = &NodePreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000743 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
744 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000745 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000746 }
747 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
748 SDep *Succ = &NodeSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000749 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
750 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000751 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000752 }
753 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
754 SDep *Succ = &ChainSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000755 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
756 if (isNewLoad) {
757 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000758 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000759 }
Evan Cheng79e97132007-10-05 01:39:18 +0000760 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000761 if (isNewLoad) {
762 AddPred(NewSU, LoadSU, false, false);
763 }
Evan Cheng79e97132007-10-05 01:39:18 +0000764
Evan Cheng91e0fc92007-12-18 08:42:10 +0000765 if (isNewLoad)
766 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000767 AvailableQueue->addNode(NewSU);
768
769 ++NumUnfolds;
770
771 if (NewSU->NumSuccsLeft == 0) {
772 NewSU->isAvailable = true;
773 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000774 }
775 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000776 }
777
778 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000779 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000780
781 // New SUnit has the exact same predecessors.
782 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
783 I != E; ++I)
784 if (!I->isSpecial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000785 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000786 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
787 }
788
789 // Only copy scheduled successors. Cut them from old node's successor
790 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000791 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000792 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
793 I != E; ++I) {
794 if (I->isSpecial)
795 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000796 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000797 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000798 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000799 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000800 }
801 }
802 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000803 SUnit *Succ = DelDeps[i].first;
804 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000805 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000806 }
807
808 AvailableQueue->updateNode(SU);
809 AvailableQueue->addNode(NewSU);
810
Evan Cheng1ec79b42007-09-27 07:09:03 +0000811 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000812 return NewSU;
813}
814
Evan Cheng1ec79b42007-09-27 07:09:03 +0000815/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
816/// and move all scheduled successors of the given SUnit to the last copy.
817void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
818 const TargetRegisterClass *DestRC,
819 const TargetRegisterClass *SrcRC,
820 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000821 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000822 CopyFromSU->CopySrcRC = SrcRC;
823 CopyFromSU->CopyDstRC = DestRC;
824 CopyFromSU->Depth = SU->Depth;
825 CopyFromSU->Height = SU->Height;
826
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000827 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000828 CopyToSU->CopySrcRC = DestRC;
829 CopyToSU->CopyDstRC = SrcRC;
830
831 // Only copy scheduled successors. Cut them from old node's successor
832 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000833 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000834 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
835 I != E; ++I) {
836 if (I->isSpecial)
837 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000838 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000839 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000840 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000841 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000842 }
843 }
844 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000845 SUnit *Succ = DelDeps[i].first;
846 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000847 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000848 }
849
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000850 AddPred(CopyFromSU, SU, false, false, Reg, -1);
851 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000852
853 AvailableQueue->updateNode(SU);
854 AvailableQueue->addNode(CopyFromSU);
855 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000856 Copies.push_back(CopyFromSU);
857 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000858
Evan Cheng1ec79b42007-09-27 07:09:03 +0000859 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000860}
861
862/// getPhysicalRegisterVT - Returns the ValueType of the physical register
863/// definition of the specified node.
864/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000865static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
866 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000867 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000868 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000869 unsigned NumRes = TID.getNumDefs();
870 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000871 if (Reg == *ImpDef)
872 break;
873 ++NumRes;
874 }
875 return N->getValueType(NumRes);
876}
877
Evan Cheng5924bf72007-09-25 01:54:36 +0000878/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
879/// scheduling of the given node to satisfy live physical register dependencies.
880/// If the specific node is the last one that's available to schedule, do
881/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000882bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
883 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000884 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000885 return false;
886
Evan Chenge6f92252007-09-27 18:46:06 +0000887 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000888 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000889 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
890 I != E; ++I) {
891 if (I->Cost < 0) {
892 unsigned Reg = I->Reg;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000893 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000894 if (RegAdded.insert(Reg))
895 LRegs.push_back(Reg);
896 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000897 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000898 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000899 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000900 if (RegAdded.insert(*Alias))
901 LRegs.push_back(*Alias);
902 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000903 }
904 }
905
906 for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000907 SDNode *Node = (i == 0) ? SU->getNode() : SU->FlaggedNodes[i-1];
Dan Gohman17059682008-07-17 19:10:17 +0000908 if (!Node || !Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000909 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000910 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000911 if (!TID.ImplicitDefs)
912 continue;
913 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000914 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000915 if (RegAdded.insert(*Reg))
916 LRegs.push_back(*Reg);
917 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000918 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000919 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000920 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000921 if (RegAdded.insert(*Alias))
922 LRegs.push_back(*Alias);
923 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000924 }
925 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000926 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000927}
928
Evan Cheng1ec79b42007-09-27 07:09:03 +0000929
Evan Chengd38c22b2006-05-11 23:55:42 +0000930/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
931/// schedulers.
932void ScheduleDAGRRList::ListScheduleBottomUp() {
933 unsigned CurCycle = 0;
934 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000935 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000936 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000937 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
938 RootSU->isAvailable = true;
939 AvailableQueue->push(RootSU);
940 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000941
942 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000943 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000944 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000945 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000946 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000947 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000948 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000949 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000950 SUnit *CurSU = AvailableQueue->pop();
951 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000952 if (CurSU->CycleBound <= CurCycle) {
953 SmallVector<unsigned, 4> LRegs;
954 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000955 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000956 Delayed = true;
957 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000958 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000959
960 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
961 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000962 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000963 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000964
965 // All candidates are delayed due to live physical reg dependencies.
966 // Try backtracking, code duplication, or inserting cross class copies
967 // to resolve it.
968 if (Delayed && !CurSU) {
969 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
970 SUnit *TrySU = NotReady[i];
971 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
972
973 // Try unscheduling up to the point where it's safe to schedule
974 // this node.
975 unsigned LiveCycle = CurCycle;
976 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
977 unsigned Reg = LRegs[j];
978 unsigned LCycle = LiveRegCycles[Reg];
979 LiveCycle = std::min(LiveCycle, LCycle);
980 }
981 SUnit *OldSU = Sequence[LiveCycle];
982 if (!WillCreateCycle(TrySU, OldSU)) {
983 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
984 // Force the current node to be scheduled before the node that
985 // requires the physical reg dep.
986 if (OldSU->isAvailable) {
987 OldSU->isAvailable = false;
988 AvailableQueue->remove(OldSU);
989 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000990 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000991 // If one or more successors has been unscheduled, then the current
992 // node is no longer avaialable. Schedule a successor that's now
993 // available instead.
994 if (!TrySU->isAvailable)
995 CurSU = AvailableQueue->pop();
996 else {
997 CurSU = TrySU;
998 TrySU->isPending = false;
999 NotReady.erase(NotReady.begin()+i);
1000 }
1001 break;
1002 }
1003 }
1004
1005 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +00001006 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +00001007 // "expensive to copy" values to break the dependency. In case even
1008 // that doesn't work, insert cross class copies.
1009 SUnit *TrySU = NotReady[0];
1010 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1011 assert(LRegs.size() == 1 && "Can't handle this yet!");
1012 unsigned Reg = LRegs[0];
1013 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +00001014 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
1015 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +00001016 // Issue expensive cross register class copies.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001017 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001018 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +00001019 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001020 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001021 if (!DestRC) {
1022 assert(false && "Don't know how to copy this physical register!");
1023 abort();
1024 }
1025 SmallVector<SUnit*, 2> Copies;
1026 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1027 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
1028 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001029 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001030 NewDef = Copies.back();
1031 }
1032
1033 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
1034 << " to SU #" << TrySU->NodeNum << "\n";
1035 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001036 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001037 TrySU->isAvailable = false;
1038 CurSU = NewDef;
1039 }
1040
1041 if (!CurSU) {
1042 assert(false && "Unable to resolve live physical register dependencies!");
1043 abort();
1044 }
1045 }
1046
Evan Chengd38c22b2006-05-11 23:55:42 +00001047 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +00001048 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
1049 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +00001050 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +00001051 if (NotReady[i]->isAvailable)
1052 AvailableQueue->push(NotReady[i]);
1053 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001054 NotReady.clear();
1055
Evan Cheng5924bf72007-09-25 01:54:36 +00001056 if (!CurSU)
1057 Sequence.push_back(0);
1058 else {
1059 ScheduleNodeBottomUp(CurSU, CurCycle);
1060 Sequence.push_back(CurSU);
1061 }
1062 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001063 }
1064
Evan Chengd38c22b2006-05-11 23:55:42 +00001065 // Reverse the order if it is bottom up.
1066 std::reverse(Sequence.begin(), Sequence.end());
1067
1068
1069#ifndef NDEBUG
1070 // Verify that all SUnits were scheduled.
1071 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001072 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001073 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001074 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman4370f262008-04-15 01:22:18 +00001075 if (!SUnits[i].isScheduled) {
1076 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1077 ++DeadNodes;
1078 continue;
1079 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001080 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001081 cerr << "*** List scheduling failed! ***\n";
Dan Gohman5a390b92008-11-13 21:21:28 +00001082 SUnits[i].dump(DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001083 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001084 AnyNotSched = true;
1085 }
Dan Gohman4370f262008-04-15 01:22:18 +00001086 if (SUnits[i].NumSuccsLeft != 0) {
1087 if (!AnyNotSched)
1088 cerr << "*** List scheduling failed! ***\n";
Dan Gohman5a390b92008-11-13 21:21:28 +00001089 SUnits[i].dump(DAG);
Dan Gohman4370f262008-04-15 01:22:18 +00001090 cerr << "has successors left!\n";
1091 AnyNotSched = true;
1092 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001093 }
Dan Gohman82b66732008-04-15 22:40:14 +00001094 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1095 if (!Sequence[i])
1096 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001097 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001098 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001099 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001100#endif
1101}
1102
1103//===----------------------------------------------------------------------===//
1104// Top-Down Scheduling
1105//===----------------------------------------------------------------------===//
1106
1107/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001108/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Evan Chengd38c22b2006-05-11 23:55:42 +00001109void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain,
1110 unsigned CurCycle) {
1111 // FIXME: the distance between two nodes is not always == the predecessor's
1112 // latency. For example, the reader can very well read the register written
1113 // by the predecessor later than the issue cycle. It also depends on the
1114 // interrupt model (drain vs. freeze).
1115 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
1116
Evan Cheng038dcc52007-09-28 19:24:24 +00001117 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +00001118
1119#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +00001120 if (SuccSU->NumPredsLeft < 0) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001121 cerr << "*** List scheduling failed! ***\n";
Dan Gohman5a390b92008-11-13 21:21:28 +00001122 SuccSU->dump(DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001123 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001124 assert(0);
1125 }
1126#endif
1127
Evan Cheng038dcc52007-09-28 19:24:24 +00001128 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001129 SuccSU->isAvailable = true;
1130 AvailableQueue->push(SuccSU);
1131 }
1132}
1133
1134
1135/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1136/// count of its successors. If a successor pending count is zero, add it to
1137/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +00001138void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001139 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman5a390b92008-11-13 21:21:28 +00001140 DEBUG(SU->dump(DAG));
Evan Chengd38c22b2006-05-11 23:55:42 +00001141 SU->Cycle = CurCycle;
1142
1143 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001144
1145 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +00001146 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1147 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001148 ReleaseSucc(I->Dep, I->isCtrl, CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +00001149 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +00001150}
1151
Dan Gohman54a187e2007-08-20 19:28:38 +00001152/// ListScheduleTopDown - The main loop of list scheduling for top-down
1153/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001154void ScheduleDAGRRList::ListScheduleTopDown() {
1155 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001156
1157 // All leaves to Available queue.
1158 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1159 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001160 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001161 AvailableQueue->push(&SUnits[i]);
1162 SUnits[i].isAvailable = true;
1163 }
1164 }
1165
Evan Chengd38c22b2006-05-11 23:55:42 +00001166 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001167 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +00001168 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +00001169 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001170 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001171 SUnit *CurSU = AvailableQueue->pop();
1172 while (CurSU && CurSU->CycleBound > CurCycle) {
1173 NotReady.push_back(CurSU);
1174 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +00001175 }
1176
1177 // Add the nodes that aren't ready back onto the available list.
1178 AvailableQueue->push_all(NotReady);
1179 NotReady.clear();
1180
Evan Cheng5924bf72007-09-25 01:54:36 +00001181 if (!CurSU)
1182 Sequence.push_back(0);
1183 else {
1184 ScheduleNodeTopDown(CurSU, CurCycle);
1185 Sequence.push_back(CurSU);
1186 }
Dan Gohman4370f262008-04-15 01:22:18 +00001187 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001188 }
1189
1190
1191#ifndef NDEBUG
1192 // Verify that all SUnits were scheduled.
1193 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001194 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001195 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001196 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1197 if (!SUnits[i].isScheduled) {
Dan Gohman4370f262008-04-15 01:22:18 +00001198 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1199 ++DeadNodes;
1200 continue;
1201 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001202 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001203 cerr << "*** List scheduling failed! ***\n";
Dan Gohman5a390b92008-11-13 21:21:28 +00001204 SUnits[i].dump(DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +00001205 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001206 AnyNotSched = true;
1207 }
Dan Gohman4370f262008-04-15 01:22:18 +00001208 if (SUnits[i].NumPredsLeft != 0) {
1209 if (!AnyNotSched)
1210 cerr << "*** List scheduling failed! ***\n";
Dan Gohman5a390b92008-11-13 21:21:28 +00001211 SUnits[i].dump(DAG);
Dan Gohman4370f262008-04-15 01:22:18 +00001212 cerr << "has predecessors left!\n";
1213 AnyNotSched = true;
1214 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001215 }
Dan Gohman82b66732008-04-15 22:40:14 +00001216 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1217 if (!Sequence[i])
1218 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001219 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001220 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001221 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001222#endif
1223}
1224
1225
1226
1227//===----------------------------------------------------------------------===//
1228// RegReductionPriorityQueue Implementation
1229//===----------------------------------------------------------------------===//
1230//
1231// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1232// to reduce register pressure.
1233//
1234namespace {
1235 template<class SF>
1236 class RegReductionPriorityQueue;
1237
1238 /// Sorting functions for the Available queue.
1239 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1240 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1241 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1242 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1243
1244 bool operator()(const SUnit* left, const SUnit* right) const;
1245 };
1246
Evan Cheng7e4abde2008-07-02 09:23:51 +00001247 struct bu_ls_rr_fast_sort : public std::binary_function<SUnit*, SUnit*, bool>{
1248 RegReductionPriorityQueue<bu_ls_rr_fast_sort> *SPQ;
1249 bu_ls_rr_fast_sort(RegReductionPriorityQueue<bu_ls_rr_fast_sort> *spq)
1250 : SPQ(spq) {}
1251 bu_ls_rr_fast_sort(const bu_ls_rr_fast_sort &RHS) : SPQ(RHS.SPQ) {}
1252
1253 bool operator()(const SUnit* left, const SUnit* right) const;
1254 };
1255
Evan Chengd38c22b2006-05-11 23:55:42 +00001256 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1257 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1258 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1259 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1260
1261 bool operator()(const SUnit* left, const SUnit* right) const;
1262 };
1263} // end anonymous namespace
1264
Evan Cheng961bbd32007-01-08 23:50:38 +00001265static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001266 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +00001267 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +00001268 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
1269}
1270
Evan Cheng7e4abde2008-07-02 09:23:51 +00001271/// CalcNodeBUSethiUllmanNumber - Compute Sethi Ullman number for bottom up
1272/// scheduling. Smaller number is the higher priority.
1273static unsigned
1274CalcNodeBUSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1275 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1276 if (SethiUllmanNumber != 0)
1277 return SethiUllmanNumber;
1278
1279 unsigned Extra = 0;
1280 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1281 I != E; ++I) {
1282 if (I->isCtrl) continue; // ignore chain preds
1283 SUnit *PredSU = I->Dep;
1284 unsigned PredSethiUllman = CalcNodeBUSethiUllmanNumber(PredSU, SUNumbers);
1285 if (PredSethiUllman > SethiUllmanNumber) {
1286 SethiUllmanNumber = PredSethiUllman;
1287 Extra = 0;
1288 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1289 ++Extra;
1290 }
1291
1292 SethiUllmanNumber += Extra;
1293
1294 if (SethiUllmanNumber == 0)
1295 SethiUllmanNumber = 1;
1296
1297 return SethiUllmanNumber;
1298}
1299
1300/// CalcNodeTDSethiUllmanNumber - Compute Sethi Ullman number for top down
1301/// scheduling. Smaller number is the higher priority.
1302static unsigned
1303CalcNodeTDSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1304 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1305 if (SethiUllmanNumber != 0)
1306 return SethiUllmanNumber;
1307
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001308 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001309 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1310 SethiUllmanNumber = 0xffff;
1311 else if (SU->NumSuccsLeft == 0)
1312 // If SU does not have a use, i.e. it doesn't produce a value that would
1313 // be consumed (e.g. store), then it terminates a chain of computation.
1314 // Give it a small SethiUllman number so it will be scheduled right before
1315 // its predecessors that it doesn't lengthen their live ranges.
1316 SethiUllmanNumber = 0;
1317 else if (SU->NumPredsLeft == 0 &&
1318 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
1319 SethiUllmanNumber = 0xffff;
1320 else {
1321 int Extra = 0;
1322 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1323 I != E; ++I) {
1324 if (I->isCtrl) continue; // ignore chain preds
1325 SUnit *PredSU = I->Dep;
1326 unsigned PredSethiUllman = CalcNodeTDSethiUllmanNumber(PredSU, SUNumbers);
1327 if (PredSethiUllman > SethiUllmanNumber) {
1328 SethiUllmanNumber = PredSethiUllman;
1329 Extra = 0;
1330 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1331 ++Extra;
1332 }
1333
1334 SethiUllmanNumber += Extra;
1335 }
1336
1337 return SethiUllmanNumber;
1338}
1339
1340
Evan Chengd38c22b2006-05-11 23:55:42 +00001341namespace {
1342 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001343 class VISIBILITY_HIDDEN RegReductionPriorityQueue
1344 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +00001345 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +00001346 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001347
1348 public:
1349 RegReductionPriorityQueue() :
Roman Levenstein6b371142008-04-29 09:07:59 +00001350 Queue(SF(this)), currentQueueId(0) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001351
Dan Gohman50c76be2008-10-31 19:06:33 +00001352 virtual void initNodes(std::vector<SUnit> &sunits) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001353
Dan Gohman50c76be2008-10-31 19:06:33 +00001354 virtual void addNode(const SUnit *SU) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001355
Dan Gohman50c76be2008-10-31 19:06:33 +00001356 virtual void updateNode(const SUnit *SU) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001357
Dan Gohman50c76be2008-10-31 19:06:33 +00001358 virtual void releaseState() = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001359
Dan Gohman50c76be2008-10-31 19:06:33 +00001360 virtual unsigned getNodePriority(const SUnit *SU) const = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001361
Evan Cheng5924bf72007-09-25 01:54:36 +00001362 unsigned size() const { return Queue.size(); }
1363
Evan Chengd38c22b2006-05-11 23:55:42 +00001364 bool empty() const { return Queue.empty(); }
1365
1366 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001367 assert(!U->NodeQueueId && "Node in the queue already");
1368 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001369 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001370 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001371
Evan Chengd38c22b2006-05-11 23:55:42 +00001372 void push_all(const std::vector<SUnit *> &Nodes) {
1373 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001374 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001375 }
1376
1377 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001378 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001379 SUnit *V = Queue.top();
1380 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001381 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001382 return V;
1383 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001384
Evan Cheng5924bf72007-09-25 01:54:36 +00001385 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001386 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001387 assert(SU->NodeQueueId != 0 && "Not in queue!");
1388 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001389 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001390 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001391 };
1392
Chris Lattner996795b2006-06-28 23:17:24 +00001393 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001394 : public RegReductionPriorityQueue<bu_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001395 // SUnits - The SUnits for the current graph.
Dan Gohmane955c482008-08-05 14:45:15 +00001396 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +00001397
1398 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001399 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001400
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001401 const TargetInstrInfo *TII;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001402 const TargetRegisterInfo *TRI;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001403 ScheduleDAGRRList *scheduleDAG;
Evan Cheng2c977312008-07-01 18:05:03 +00001404
Evan Chengd38c22b2006-05-11 23:55:42 +00001405 public:
Evan Chengf9891412007-12-20 09:25:31 +00001406 explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001407 const TargetRegisterInfo *tri)
1408 : TII(tii), TRI(tri), scheduleDAG(NULL) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001409
Dan Gohman46520a22008-06-21 19:18:17 +00001410 void initNodes(std::vector<SUnit> &sunits) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001411 SUnits = &sunits;
1412 // Add pseudo dependency edges for two-address nodes.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001413 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001414 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001415 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001416 }
1417
Evan Cheng5924bf72007-09-25 01:54:36 +00001418 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001419 unsigned SUSize = SethiUllmanNumbers.size();
1420 if (SUnits->size() > SUSize)
1421 SethiUllmanNumbers.resize(SUSize*2, 0);
1422 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001423 }
1424
1425 void updateNode(const SUnit *SU) {
1426 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001427 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001428 }
1429
Evan Chengd38c22b2006-05-11 23:55:42 +00001430 void releaseState() {
1431 SUnits = 0;
1432 SethiUllmanNumbers.clear();
1433 }
1434
Evan Cheng6730f032007-01-08 23:55:53 +00001435 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001436 assert(SU->NodeNum < SethiUllmanNumbers.size());
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001437 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001438 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1439 // CopyFromReg should be close to its def because it restricts
1440 // allocation choices. But if it is a livein then perhaps we want it
1441 // closer to its uses so it can be coalesced.
1442 return 0xffff;
1443 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1444 // CopyToReg should be close to its uses to facilitate coalescing and
1445 // avoid spilling.
1446 return 0;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001447 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1448 Opc == TargetInstrInfo::INSERT_SUBREG)
1449 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1450 // facilitate coalescing.
1451 return 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001452 else if (SU->NumSuccs == 0)
1453 // If SU does not have a use, i.e. it doesn't produce a value that would
1454 // be consumed (e.g. store), then it terminates a chain of computation.
1455 // Give it a large SethiUllman number so it will be scheduled right
1456 // before its predecessors that it doesn't lengthen their live ranges.
1457 return 0xffff;
1458 else if (SU->NumPreds == 0)
1459 // If SU does not have a def, schedule it close to its uses because it
1460 // does not lengthen any live ranges.
1461 return 0;
1462 else
1463 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001464 }
1465
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001466 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1467 scheduleDAG = scheduleDag;
1468 }
1469
Evan Chengd38c22b2006-05-11 23:55:42 +00001470 private:
Evan Cheng73bdf042008-03-01 00:39:47 +00001471 bool canClobber(const SUnit *SU, const SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +00001472 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001473 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001474 };
1475
1476
1477 class VISIBILITY_HIDDEN BURegReductionFastPriorityQueue
1478 : public RegReductionPriorityQueue<bu_ls_rr_fast_sort> {
1479 // SUnits - The SUnits for the current graph.
1480 const std::vector<SUnit> *SUnits;
1481
1482 // SethiUllmanNumbers - The SethiUllman number for each node.
1483 std::vector<unsigned> SethiUllmanNumbers;
1484 public:
1485 explicit BURegReductionFastPriorityQueue() {}
1486
1487 void initNodes(std::vector<SUnit> &sunits) {
1488 SUnits = &sunits;
1489 // Calculate node priorities.
1490 CalculateSethiUllmanNumbers();
1491 }
1492
1493 void addNode(const SUnit *SU) {
1494 unsigned SUSize = SethiUllmanNumbers.size();
1495 if (SUnits->size() > SUSize)
1496 SethiUllmanNumbers.resize(SUSize*2, 0);
1497 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1498 }
1499
1500 void updateNode(const SUnit *SU) {
1501 SethiUllmanNumbers[SU->NodeNum] = 0;
1502 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1503 }
1504
1505 void releaseState() {
1506 SUnits = 0;
1507 SethiUllmanNumbers.clear();
1508 }
1509
1510 unsigned getNodePriority(const SUnit *SU) const {
1511 return SethiUllmanNumbers[SU->NodeNum];
1512 }
1513
1514 private:
1515 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001516 };
1517
1518
Dan Gohman54a187e2007-08-20 19:28:38 +00001519 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001520 : public RegReductionPriorityQueue<td_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001521 // SUnits - The SUnits for the current graph.
1522 const std::vector<SUnit> *SUnits;
1523
1524 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001525 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001526
1527 public:
1528 TDRegReductionPriorityQueue() {}
1529
Dan Gohman46520a22008-06-21 19:18:17 +00001530 void initNodes(std::vector<SUnit> &sunits) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001531 SUnits = &sunits;
1532 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001533 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001534 }
1535
Evan Cheng5924bf72007-09-25 01:54:36 +00001536 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001537 unsigned SUSize = SethiUllmanNumbers.size();
1538 if (SUnits->size() > SUSize)
1539 SethiUllmanNumbers.resize(SUSize*2, 0);
1540 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001541 }
1542
1543 void updateNode(const SUnit *SU) {
1544 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001545 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001546 }
1547
Evan Chengd38c22b2006-05-11 23:55:42 +00001548 void releaseState() {
1549 SUnits = 0;
1550 SethiUllmanNumbers.clear();
1551 }
1552
Evan Cheng6730f032007-01-08 23:55:53 +00001553 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001554 assert(SU->NodeNum < SethiUllmanNumbers.size());
1555 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001556 }
1557
1558 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001559 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001560 };
1561}
1562
Evan Chengb9e3db62007-03-14 22:43:40 +00001563/// closestSucc - Returns the scheduled cycle of the successor which is
1564/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001565static unsigned closestSucc(const SUnit *SU) {
1566 unsigned MaxCycle = 0;
1567 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001568 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001569 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001570 // If there are bunch of CopyToRegs stacked up, they should be considered
1571 // to be at the same position.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001572 if (I->Dep->getNode() && I->Dep->getNode()->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001573 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001574 if (Cycle > MaxCycle)
1575 MaxCycle = Cycle;
1576 }
Evan Cheng28748552007-03-13 23:25:11 +00001577 return MaxCycle;
1578}
1579
Evan Cheng61bc51e2007-12-20 02:22:36 +00001580/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1581/// for scratch registers. Live-in operands and live-out results don't count
1582/// since they are "fixed".
1583static unsigned calcMaxScratches(const SUnit *SU) {
1584 unsigned Scratches = 0;
1585 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1586 I != E; ++I) {
1587 if (I->isCtrl) continue; // ignore chain preds
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001588 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001589 Scratches++;
1590 }
1591 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1592 I != E; ++I) {
1593 if (I->isCtrl) continue; // ignore chain succs
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001594 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001595 Scratches += 10;
1596 }
1597 return Scratches;
1598}
1599
Evan Chengd38c22b2006-05-11 23:55:42 +00001600// Bottom up
1601bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001602 unsigned LPriority = SPQ->getNodePriority(left);
1603 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001604 if (LPriority != RPriority)
1605 return LPriority > RPriority;
1606
1607 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1608 // e.g.
1609 // t1 = op t2, c1
1610 // t3 = op t4, c2
1611 //
1612 // and the following instructions are both ready.
1613 // t2 = op c3
1614 // t4 = op c4
1615 //
1616 // Then schedule t2 = op first.
1617 // i.e.
1618 // t4 = op c4
1619 // t2 = op c3
1620 // t1 = op t2, c1
1621 // t3 = op t4, c2
1622 //
1623 // This creates more short live intervals.
1624 unsigned LDist = closestSucc(left);
1625 unsigned RDist = closestSucc(right);
1626 if (LDist != RDist)
1627 return LDist < RDist;
1628
1629 // Intuitively, it's good to push down instructions whose results are
1630 // liveout so their long live ranges won't conflict with other values
1631 // which are needed inside the BB. Further prioritize liveout instructions
1632 // by the number of operands which are calculated within the BB.
1633 unsigned LScratch = calcMaxScratches(left);
1634 unsigned RScratch = calcMaxScratches(right);
1635 if (LScratch != RScratch)
1636 return LScratch > RScratch;
1637
1638 if (left->Height != right->Height)
1639 return left->Height > right->Height;
1640
1641 if (left->Depth != right->Depth)
1642 return left->Depth < right->Depth;
1643
1644 if (left->CycleBound != right->CycleBound)
1645 return left->CycleBound > right->CycleBound;
1646
Roman Levenstein6b371142008-04-29 09:07:59 +00001647 assert(left->NodeQueueId && right->NodeQueueId &&
1648 "NodeQueueId cannot be zero");
1649 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001650}
1651
Dan Gohman4b49be12008-06-21 01:08:22 +00001652bool
Evan Cheng7e4abde2008-07-02 09:23:51 +00001653bu_ls_rr_fast_sort::operator()(const SUnit *left, const SUnit *right) const {
1654 unsigned LPriority = SPQ->getNodePriority(left);
1655 unsigned RPriority = SPQ->getNodePriority(right);
1656 if (LPriority != RPriority)
1657 return LPriority > RPriority;
1658 assert(left->NodeQueueId && right->NodeQueueId &&
1659 "NodeQueueId cannot be zero");
1660 return (left->NodeQueueId > right->NodeQueueId);
1661}
1662
1663bool
Dan Gohman4b49be12008-06-21 01:08:22 +00001664BURegReductionPriorityQueue::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001665 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001666 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001667 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001668 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001669 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001670 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001671 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001672 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001673 if (DU->getNodeId() != -1 &&
1674 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001675 return true;
1676 }
1677 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001678 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001679 return false;
1680}
1681
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001682
Evan Chenga5e595d2007-09-28 22:32:30 +00001683/// hasCopyToRegUse - Return true if SU has a value successor that is a
1684/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001685static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001686 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1687 I != E; ++I) {
1688 if (I->isCtrl) continue;
Dan Gohmane955c482008-08-05 14:45:15 +00001689 const SUnit *SuccSU = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001690 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001691 return true;
1692 }
1693 return false;
1694}
1695
Evan Chengf9891412007-12-20 09:25:31 +00001696/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001697/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001698static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001699 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001700 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001701 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001702 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1703 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001704 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001705 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001706 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001707 if (!SUImpDefs)
1708 return false;
1709 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001710 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001711 if (VT == MVT::Flag || VT == MVT::Other)
1712 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001713 if (!N->hasAnyUseOfValue(i))
1714 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001715 unsigned Reg = ImpDefs[i - NumDefs];
1716 for (;*SUImpDefs; ++SUImpDefs) {
1717 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001718 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001719 return true;
1720 }
1721 }
1722 return false;
1723}
1724
Evan Chengd38c22b2006-05-11 23:55:42 +00001725/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1726/// it as a def&use operand. Add a pseudo control edge from it to the other
1727/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001728/// first (lower in the schedule). If both nodes are two-address, favor the
1729/// one that has a CopyToReg use (more likely to be a loop induction update).
1730/// If both are two-address, but one is commutable while the other is not
1731/// commutable, favor the one that's not commutable.
Dan Gohman4b49be12008-06-21 01:08:22 +00001732void BURegReductionPriorityQueue::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001733 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001734 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001735 if (!SU->isTwoAddress)
1736 continue;
1737
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001738 SDNode *Node = SU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001739 if (!Node || !Node->isMachineOpcode() || SU->FlaggedNodes.size() > 0)
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001740 continue;
1741
Dan Gohman17059682008-07-17 19:10:17 +00001742 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001743 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001744 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001745 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001746 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001747 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001748 SDNode *DU = SU->getNode()->getOperand(j).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001749 if (DU->getNodeId() == -1)
Evan Cheng1bf166312007-11-09 01:27:11 +00001750 continue;
Dan Gohman46520a22008-06-21 19:18:17 +00001751 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
Evan Chengf24d15f2006-11-06 21:33:46 +00001752 if (!DUSU) continue;
Dan Gohman46520a22008-06-21 19:18:17 +00001753 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1754 E = DUSU->Succs.end(); I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001755 if (I->isCtrl) continue;
1756 SUnit *SuccSU = I->Dep;
Evan Chengf9891412007-12-20 09:25:31 +00001757 if (SuccSU == SU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001758 continue;
Evan Cheng2dbffa42007-11-06 08:44:59 +00001759 // Be conservative. Ignore if nodes aren't at roughly the same
1760 // depth and height.
1761 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1762 continue;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001763 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001764 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001765 // Don't constrain nodes with physical register defs if the
Dan Gohmancf8827a2008-01-29 12:43:50 +00001766 // predecessor can clobber them.
Evan Chengf9891412007-12-20 09:25:31 +00001767 if (SuccSU->hasPhysRegDefs) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001768 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Chengf9891412007-12-20 09:25:31 +00001769 continue;
1770 }
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001771 // Don't constraint extract_subreg / insert_subreg these may be
1772 // coalesced away. We don't them close to their uses.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001773 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001774 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1775 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1776 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +00001777 if ((!canClobber(SuccSU, DUSU) ||
Evan Chenga5e595d2007-09-28 22:32:30 +00001778 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
Evan Cheng5924bf72007-09-25 01:54:36 +00001779 (!SU->isCommutable && SuccSU->isCommutable)) &&
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001780 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001781 DOUT << "Adding an edge from SU # " << SU->NodeNum
1782 << " to SU #" << SuccSU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001783 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001784 }
1785 }
1786 }
1787 }
1788 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001789}
1790
Evan Cheng6730f032007-01-08 23:55:53 +00001791/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1792/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001793void BURegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001794 SethiUllmanNumbers.assign(SUnits->size(), 0);
1795
1796 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001797 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
1798}
1799void BURegReductionFastPriorityQueue::CalculateSethiUllmanNumbers() {
1800 SethiUllmanNumbers.assign(SUnits->size(), 0);
1801
1802 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1803 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001804}
1805
Roman Levenstein30d09512008-03-27 09:44:37 +00001806/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001807/// predecessors of the successors of the SUnit SU. Stop when the provided
1808/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001809static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1810 unsigned Limit) {
1811 unsigned Sum = 0;
1812 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1813 I != E; ++I) {
Dan Gohmane955c482008-08-05 14:45:15 +00001814 const SUnit *SuccSU = I->Dep;
Roman Levensteinbc674502008-03-27 09:14:57 +00001815 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1816 EE = SuccSU->Preds.end(); II != EE; ++II) {
1817 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001818 if (!PredSU->isScheduled)
1819 if (++Sum > Limit)
1820 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001821 }
1822 }
1823 return Sum;
1824}
1825
Evan Chengd38c22b2006-05-11 23:55:42 +00001826
1827// Top down
1828bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001829 unsigned LPriority = SPQ->getNodePriority(left);
1830 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001831 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1832 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001833 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1834 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001835 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1836 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001837
1838 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1839 return false;
1840 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1841 return true;
1842
Evan Chengd38c22b2006-05-11 23:55:42 +00001843 if (LIsFloater)
1844 LBonus -= 2;
1845 if (RIsFloater)
1846 RBonus -= 2;
1847 if (left->NumSuccs == 1)
1848 LBonus += 2;
1849 if (right->NumSuccs == 1)
1850 RBonus += 2;
1851
Evan Cheng73bdf042008-03-01 00:39:47 +00001852 if (LPriority+LBonus != RPriority+RBonus)
1853 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001854
Evan Cheng73bdf042008-03-01 00:39:47 +00001855 if (left->Depth != right->Depth)
1856 return left->Depth < right->Depth;
1857
1858 if (left->NumSuccsLeft != right->NumSuccsLeft)
1859 return left->NumSuccsLeft > right->NumSuccsLeft;
1860
1861 if (left->CycleBound != right->CycleBound)
1862 return left->CycleBound > right->CycleBound;
1863
Roman Levenstein6b371142008-04-29 09:07:59 +00001864 assert(left->NodeQueueId && right->NodeQueueId &&
1865 "NodeQueueId cannot be zero");
1866 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001867}
1868
Evan Cheng6730f032007-01-08 23:55:53 +00001869/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1870/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001871void TDRegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001872 SethiUllmanNumbers.assign(SUnits->size(), 0);
1873
1874 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001875 CalcNodeTDSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001876}
1877
1878//===----------------------------------------------------------------------===//
1879// Public Constructor Functions
1880//===----------------------------------------------------------------------===//
1881
Jim Laskey03593f72006-08-01 18:29:48 +00001882llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1883 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001884 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001885 MachineBasicBlock *BB,
1886 bool Fast) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001887 if (Fast)
Dan Gohman5a390b92008-11-13 21:21:28 +00001888 return new ScheduleDAGRRList(DAG, BB, *TM, true, true,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001889 new BURegReductionFastPriorityQueue());
1890
Dan Gohman5499e892008-11-11 17:50:47 +00001891 const TargetInstrInfo *TII = TM->getInstrInfo();
1892 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001893
Evan Cheng7e4abde2008-07-02 09:23:51 +00001894 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001895
Evan Cheng7e4abde2008-07-02 09:23:51 +00001896 ScheduleDAGRRList *SD =
Dan Gohman5a390b92008-11-13 21:21:28 +00001897 new ScheduleDAGRRList(DAG, BB, *TM, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001898 PQ->setScheduleDAG(SD);
1899 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001900}
1901
Jim Laskey03593f72006-08-01 18:29:48 +00001902llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1903 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001904 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001905 MachineBasicBlock *BB,
1906 bool Fast) {
Dan Gohman5a390b92008-11-13 21:21:28 +00001907 return new ScheduleDAGRRList(DAG, BB, *TM, false, Fast,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001908 new TDRegReductionPriorityQueue());
Evan Chengd38c22b2006-05-11 23:55:42 +00001909}