blob: 2f234b29ee0164af30190c728edb1f90dc970337 [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman60cb69e2008-11-19 23:18:57 +000019#include "llvm/CodeGen/ScheduleDAGSDNodes.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Dan Gohmana4db3352008-06-21 18:35:25 +000027#include "llvm/ADT/BitVector.h"
28#include "llvm/ADT/PriorityQueue.h"
Evan Chenge6f92252007-09-27 18:46:06 +000029#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000030#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000031#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000032#include "llvm/ADT/STLExtras.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000033#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include "llvm/Support/CommandLine.h"
35using namespace llvm;
36
Dan Gohmanfd227e92008-03-25 17:10:29 +000037STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000038STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039STATISTIC(NumDups, "Number of duplicated nodes");
40STATISTIC(NumCCCopies, "Number of cross class copies");
41
Jim Laskey95eda5b2006-08-01 14:21:23 +000042static RegisterScheduler
43 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
50
Evan Chengd38c22b2006-05-11 23:55:42 +000051namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000052//===----------------------------------------------------------------------===//
53/// ScheduleDAGRRList - The actual register reduction list scheduler
54/// implementation. This supports both top-down and bottom-up scheduling.
55///
Dan Gohman60cb69e2008-11-19 23:18:57 +000056class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000057private:
58 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
59 /// it is top-down.
60 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000061
Evan Chengd38c22b2006-05-11 23:55:42 +000062 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000063 SchedulingPriorityQueue *AvailableQueue;
64
Dan Gohmanc07f6862008-09-23 18:50:48 +000065 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000066 /// that are "live". These nodes must be scheduled before any other nodes that
67 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000068 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000069 std::vector<SUnit*> LiveRegDefs;
70 std::vector<unsigned> LiveRegCycles;
71
Evan Chengd38c22b2006-05-11 23:55:42 +000072public:
Dan Gohman5a390b92008-11-13 21:21:28 +000073 ScheduleDAGRRList(SelectionDAG *dag, MachineBasicBlock *bb,
Dan Gohmanfd08af42008-11-20 03:11:19 +000074 const TargetMachine &tm, bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000075 SchedulingPriorityQueue *availqueue)
Dan Gohmanfd08af42008-11-20 03:11:19 +000076 : ScheduleDAGSDNodes(dag, bb, tm), isBottomUp(isbottomup),
Evan Chengd38c22b2006-05-11 23:55:42 +000077 AvailableQueue(availqueue) {
78 }
79
80 ~ScheduleDAGRRList() {
81 delete AvailableQueue;
82 }
83
84 void Schedule();
85
Roman Levenstein733a4d62008-03-26 11:23:38 +000086 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane955c482008-08-05 14:45:15 +000087 bool IsReachable(const SUnit *SU, const SUnit *TargetSU);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000088
89 /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will
90 /// create a cycle.
91 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU);
92
93 /// AddPred - This adds the specified node X as a predecessor of
94 /// the current node Y if not already.
Roman Levenstein733a4d62008-03-26 11:23:38 +000095 /// This returns true if this is a new predecessor.
96 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000097 bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +000098 unsigned PhyReg = 0, int Cost = 1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000099
Roman Levenstein733a4d62008-03-26 11:23:38 +0000100 /// RemovePred - This removes the specified node N from the predecessors of
101 /// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000102 bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isSpecial);
103
Evan Chengd38c22b2006-05-11 23:55:42 +0000104private:
Dan Gohman5ebdb982008-11-18 00:38:59 +0000105 void ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain);
106 void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain);
Evan Cheng8e136a92007-09-26 21:36:17 +0000107 void CapturePred(SUnit*, SUnit*, bool);
108 void ScheduleNodeBottomUp(SUnit*, unsigned);
109 void ScheduleNodeTopDown(SUnit*, unsigned);
110 void UnscheduleNodeBottomUp(SUnit*);
111 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
112 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000113 void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned,
Evan Cheng8e136a92007-09-26 21:36:17 +0000114 const TargetRegisterClass*,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000115 const TargetRegisterClass*,
116 SmallVector<SUnit*, 2>&);
117 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000118 void ListScheduleTopDown();
119 void ListScheduleBottomUp();
Evan Chengafed73e2006-05-12 01:58:24 +0000120 void CommuteNodesToReducePressure();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000121
122
123 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000124 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000125 SUnit *CreateNewSUnit(SDNode *N) {
126 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000127 // Update the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000128 if (NewNode->NodeNum >= Node2Index.size())
129 InitDAGTopologicalSorting();
130 return NewNode;
131 }
132
Roman Levenstein733a4d62008-03-26 11:23:38 +0000133 /// CreateClone - Creates a new SUnit from an existing one.
134 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000135 SUnit *CreateClone(SUnit *N) {
136 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000137 // Update the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000138 if (NewNode->NodeNum >= Node2Index.size())
139 InitDAGTopologicalSorting();
140 return NewNode;
141 }
142
143 /// Functions for preserving the topological ordering
144 /// even after dynamic insertions of new edges.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000145 /// This allows a very fast implementation of IsReachable.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000146
Roman Levenstein733a4d62008-03-26 11:23:38 +0000147 /// InitDAGTopologicalSorting - create the initial topological
148 /// ordering from the DAG to be scheduled.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000149 void InitDAGTopologicalSorting();
150
151 /// DFS - make a DFS traversal and mark all nodes affected by the
Roman Levenstein733a4d62008-03-26 11:23:38 +0000152 /// edge insertion. These nodes will later get new topological indexes
153 /// by means of the Shift method.
Dan Gohmane955c482008-08-05 14:45:15 +0000154 void DFS(const SUnit *SU, int UpperBound, bool& HasLoop);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000155
156 /// Shift - reassign topological indexes for the nodes in the DAG
Roman Levenstein733a4d62008-03-26 11:23:38 +0000157 /// to preserve the topological ordering.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000158 void Shift(BitVector& Visited, int LowerBound, int UpperBound);
159
Roman Levenstein733a4d62008-03-26 11:23:38 +0000160 /// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000161 void Allocate(int n, int index);
162
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 /// Index2Node - Maps topological index to the node number.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000164 std::vector<int> Index2Node;
Roman Levenstein733a4d62008-03-26 11:23:38 +0000165 /// Node2Index - Maps the node number to its topological index.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000166 std::vector<int> Node2Index;
Roman Levenstein733a4d62008-03-26 11:23:38 +0000167 /// Visited - a set of nodes visited during a DFS traversal.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000168 BitVector Visited;
Evan Chengd38c22b2006-05-11 23:55:42 +0000169};
170} // end anonymous namespace
171
172
173/// Schedule - Schedule the DAG using list scheduling.
174void ScheduleDAGRRList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +0000175 DOUT << "********** List Scheduling **********\n";
Evan Cheng5924bf72007-09-25 01:54:36 +0000176
Dan Gohmanc07f6862008-09-23 18:50:48 +0000177 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000178 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
179 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000180
Evan Chengd38c22b2006-05-11 23:55:42 +0000181 // Build scheduling units.
182 BuildSchedUnits();
183
Evan Chengd38c22b2006-05-11 23:55:42 +0000184 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000185 SUnits[su].dumpAll(this));
Dan Gohmanfd08af42008-11-20 03:11:19 +0000186 CalculateDepths();
187 CalculateHeights();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000188 InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000189
Dan Gohman46520a22008-06-21 19:18:17 +0000190 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000191
Evan Chengd38c22b2006-05-11 23:55:42 +0000192 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
193 if (isBottomUp)
194 ListScheduleBottomUp();
195 else
196 ListScheduleTopDown();
197
198 AvailableQueue->releaseState();
Evan Cheng2c977312008-07-01 18:05:03 +0000199
Dan Gohmanfd08af42008-11-20 03:11:19 +0000200 CommuteNodesToReducePressure();
Evan Chengd38c22b2006-05-11 23:55:42 +0000201}
202
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000203/// CommuteNodesToReducePressure - If a node is two-address and commutable, and
Evan Chengafed73e2006-05-12 01:58:24 +0000204/// it is not the last use of its first operand, add it to the CommuteSet if
205/// possible. It will be commuted when it is translated to a MI.
206void ScheduleDAGRRList::CommuteNodesToReducePressure() {
Evan Chenge3c44192007-06-22 01:35:51 +0000207 SmallPtrSet<SUnit*, 4> OperandSeen;
Dan Gohman4370f262008-04-15 01:22:18 +0000208 for (unsigned i = Sequence.size(); i != 0; ) {
209 --i;
Evan Chengafed73e2006-05-12 01:58:24 +0000210 SUnit *SU = Sequence[i];
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000211 if (!SU || !SU->getNode()) continue;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000212 if (SU->isCommutable) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000213 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000214 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000215 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +0000216 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000217 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000218 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000219 continue;
220
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000221 SDNode *OpN = SU->getNode()->getOperand(j).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000222 SUnit *OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000223 if (OpSU && OperandSeen.count(OpSU) == 1) {
224 // Ok, so SU is not the last use of OpSU, but SU is two-address so
225 // it will clobber OpSU. Try to commute SU if no other source operands
226 // are live below.
227 bool DoCommute = true;
228 for (unsigned k = 0; k < NumOps; ++k) {
229 if (k != j) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000230 OpN = SU->getNode()->getOperand(k).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +0000231 OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()];
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000232 if (OpSU && OperandSeen.count(OpSU) == 1) {
233 DoCommute = false;
234 break;
235 }
236 }
Evan Chengafed73e2006-05-12 01:58:24 +0000237 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000238 if (DoCommute)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000239 CommuteSet.insert(SU->getNode());
Evan Chengafed73e2006-05-12 01:58:24 +0000240 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000241
242 // Only look at the first use&def node for now.
243 break;
Evan Chengafed73e2006-05-12 01:58:24 +0000244 }
245 }
246
Chris Lattnerd86418a2006-08-17 00:09:56 +0000247 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
248 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000249 if (!I->isCtrl)
Dan Gohmane6e13482008-06-21 15:52:51 +0000250 OperandSeen.insert(I->Dep->OrigNode);
Evan Chengafed73e2006-05-12 01:58:24 +0000251 }
252 }
253}
Evan Chengd38c22b2006-05-11 23:55:42 +0000254
255//===----------------------------------------------------------------------===//
256// Bottom-Up Scheduling
257//===----------------------------------------------------------------------===//
258
Evan Chengd38c22b2006-05-11 23:55:42 +0000259/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000260/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman5ebdb982008-11-18 00:38:59 +0000261void ScheduleDAGRRList::ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000262 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000263
264#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000265 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000266 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000267 PredSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000268 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000269 assert(0);
270 }
271#endif
272
Evan Cheng038dcc52007-09-28 19:24:24 +0000273 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000274 PredSU->isAvailable = true;
275 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000276 }
277}
278
279/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
280/// count of its predecessors. If a predecessor pending count is zero, add it to
281/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000282void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000283 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000284 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000285
Dan Gohman6e587262008-11-18 21:22:20 +0000286 SU->Cycle = CurCycle;
287 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000288
289 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000290 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000291 I != E; ++I) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000292 ReleasePred(SU, I->Dep, I->isCtrl);
Evan Cheng5924bf72007-09-25 01:54:36 +0000293 if (I->Cost < 0) {
294 // This is a physical register dependency and it's impossible or
295 // expensive to copy the register. Make sure nothing that can
296 // clobber the register is scheduled between the predecessor and
297 // this node.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000298 if (!LiveRegDefs[I->Reg]) {
299 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000300 LiveRegDefs[I->Reg] = I->Dep;
301 LiveRegCycles[I->Reg] = CurCycle;
302 }
303 }
304 }
305
306 // Release all the implicit physical register defs that are live.
307 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
308 I != E; ++I) {
309 if (I->Cost < 0) {
310 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000311 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000312 assert(LiveRegDefs[I->Reg] == SU &&
313 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000314 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000315 LiveRegDefs[I->Reg] = NULL;
316 LiveRegCycles[I->Reg] = 0;
317 }
318 }
319 }
320
Evan Chengd38c22b2006-05-11 23:55:42 +0000321 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000322 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000323}
324
Evan Cheng5924bf72007-09-25 01:54:36 +0000325/// CapturePred - This does the opposite of ReleasePred. Since SU is being
326/// unscheduled, incrcease the succ left count of its predecessors. Remove
327/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000328void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000329 if (PredSU->isAvailable) {
330 PredSU->isAvailable = false;
331 if (!PredSU->isPending)
332 AvailableQueue->remove(PredSU);
333 }
334
Evan Cheng038dcc52007-09-28 19:24:24 +0000335 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000336}
337
338/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
339/// its predecessor states to reflect the change.
340void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
341 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000342 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000343
344 AvailableQueue->UnscheduledNode(SU);
345
346 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
347 I != E; ++I) {
348 CapturePred(I->Dep, SU, I->isCtrl);
349 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000350 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000351 assert(LiveRegDefs[I->Reg] == I->Dep &&
352 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000353 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000354 LiveRegDefs[I->Reg] = NULL;
355 LiveRegCycles[I->Reg] = 0;
356 }
357 }
358
359 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
360 I != E; ++I) {
361 if (I->Cost < 0) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000362 if (!LiveRegDefs[I->Reg]) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000363 LiveRegDefs[I->Reg] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000364 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000365 }
366 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
367 LiveRegCycles[I->Reg] = I->Dep->Cycle;
368 }
369 }
370
371 SU->Cycle = 0;
372 SU->isScheduled = false;
373 SU->isAvailable = true;
374 AvailableQueue->push(SU);
375}
376
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000377/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane955c482008-08-05 14:45:15 +0000378bool ScheduleDAGRRList::IsReachable(const SUnit *SU, const SUnit *TargetSU) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000379 // If insertion of the edge SU->TargetSU would create a cycle
380 // then there is a path from TargetSU to SU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000381 int UpperBound, LowerBound;
382 LowerBound = Node2Index[TargetSU->NodeNum];
383 UpperBound = Node2Index[SU->NodeNum];
384 bool HasLoop = false;
385 // Is Ord(TargetSU) < Ord(SU) ?
386 if (LowerBound < UpperBound) {
387 Visited.reset();
388 // There may be a path from TargetSU to SU. Check for it.
389 DFS(TargetSU, UpperBound, HasLoop);
Evan Chengcfd5f822007-09-27 00:25:29 +0000390 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000391 return HasLoop;
Evan Chengcfd5f822007-09-27 00:25:29 +0000392}
393
Roman Levenstein733a4d62008-03-26 11:23:38 +0000394/// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000395inline void ScheduleDAGRRList::Allocate(int n, int index) {
396 Node2Index[n] = index;
397 Index2Node[index] = n;
Evan Chengcfd5f822007-09-27 00:25:29 +0000398}
399
Roman Levenstein733a4d62008-03-26 11:23:38 +0000400/// InitDAGTopologicalSorting - create the initial topological
401/// ordering from the DAG to be scheduled.
Evan Cheng2c977312008-07-01 18:05:03 +0000402
403/// The idea of the algorithm is taken from
404/// "Online algorithms for managing the topological order of
405/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
406/// This is the MNR algorithm, which was first introduced by
407/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
408/// "Maintaining a topological order under edge insertions".
409///
410/// Short description of the algorithm:
411///
412/// Topological ordering, ord, of a DAG maps each node to a topological
413/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
414///
415/// This means that if there is a path from the node X to the node Z,
416/// then ord(X) < ord(Z).
417///
418/// This property can be used to check for reachability of nodes:
419/// if Z is reachable from X, then an insertion of the edge Z->X would
420/// create a cycle.
421///
422/// The algorithm first computes a topological ordering for the DAG by
423/// initializing the Index2Node and Node2Index arrays and then tries to keep
424/// the ordering up-to-date after edge insertions by reordering the DAG.
425///
426/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
427/// the nodes reachable from Y, and then shifts them using Shift to lie
428/// immediately after X in Index2Node.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000429void ScheduleDAGRRList::InitDAGTopologicalSorting() {
430 unsigned DAGSize = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000431 std::vector<SUnit*> WorkList;
432 WorkList.reserve(DAGSize);
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000433
434 Index2Node.resize(DAGSize);
435 Node2Index.resize(DAGSize);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000436
Roman Levenstein733a4d62008-03-26 11:23:38 +0000437 // Initialize the data structures.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000438 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
439 SUnit *SU = &SUnits[i];
440 int NodeNum = SU->NodeNum;
441 unsigned Degree = SU->Succs.size();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000442 // Temporarily use the Node2Index array as scratch space for degree counts.
443 Node2Index[NodeNum] = Degree;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000444
445 // Is it a node without dependencies?
446 if (Degree == 0) {
447 assert(SU->Succs.empty() && "SUnit should have no successors");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000448 // Collect leaf nodes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000449 WorkList.push_back(SU);
450 }
451 }
452
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000453 int Id = DAGSize;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000454 while (!WorkList.empty()) {
455 SUnit *SU = WorkList.back();
456 WorkList.pop_back();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000457 Allocate(SU->NodeNum, --Id);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000458 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
459 I != E; ++I) {
460 SUnit *SU = I->Dep;
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000461 if (!--Node2Index[SU->NodeNum])
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000462 // If all dependencies of the node are processed already,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000463 // then the node can be computed now.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000464 WorkList.push_back(SU);
465 }
466 }
467
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000468 Visited.resize(DAGSize);
469
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000470#ifndef NDEBUG
471 // Check correctness of the ordering
472 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
473 SUnit *SU = &SUnits[i];
474 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
475 I != E; ++I) {
476 assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] &&
477 "Wrong topological sorting");
478 }
479 }
480#endif
481}
482
Roman Levenstein733a4d62008-03-26 11:23:38 +0000483/// AddPred - adds an edge from SUnit X to SUnit Y.
484/// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000485bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
486 unsigned PhyReg, int Cost) {
487 int UpperBound, LowerBound;
488 LowerBound = Node2Index[Y->NodeNum];
489 UpperBound = Node2Index[X->NodeNum];
490 bool HasLoop = false;
491 // Is Ord(X) < Ord(Y) ?
492 if (LowerBound < UpperBound) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000493 // Update the topological order.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000494 Visited.reset();
495 DFS(Y, UpperBound, HasLoop);
496 assert(!HasLoop && "Inserted edge creates a loop!");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000497 // Recompute topological indexes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000498 Shift(Visited, LowerBound, UpperBound);
499 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000500 // Now really insert the edge.
501 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000502}
503
Roman Levenstein733a4d62008-03-26 11:23:38 +0000504/// RemovePred - This removes the specified node N from the predecessors of
505/// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000506bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N,
507 bool isCtrl, bool isSpecial) {
508 // InitDAGTopologicalSorting();
509 return M->removePred(N, isCtrl, isSpecial);
510}
511
Roman Levenstein733a4d62008-03-26 11:23:38 +0000512/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
513/// all nodes affected by the edge insertion. These nodes will later get new
514/// topological indexes by means of the Shift method.
Dan Gohmane955c482008-08-05 14:45:15 +0000515void ScheduleDAGRRList::DFS(const SUnit *SU, int UpperBound, bool& HasLoop) {
516 std::vector<const SUnit*> WorkList;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000517 WorkList.reserve(SUnits.size());
518
519 WorkList.push_back(SU);
520 while (!WorkList.empty()) {
521 SU = WorkList.back();
522 WorkList.pop_back();
523 Visited.set(SU->NodeNum);
524 for (int I = SU->Succs.size()-1; I >= 0; --I) {
525 int s = SU->Succs[I].Dep->NodeNum;
526 if (Node2Index[s] == UpperBound) {
527 HasLoop = true;
528 return;
529 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000530 // Visit successors if not already and in affected region.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000531 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
532 WorkList.push_back(SU->Succs[I].Dep);
533 }
534 }
535 }
536}
537
Roman Levenstein733a4d62008-03-26 11:23:38 +0000538/// Shift - Renumber the nodes so that the topological ordering is
539/// preserved.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound,
541 int UpperBound) {
542 std::vector<int> L;
543 int shift = 0;
544 int i;
545
546 for (i = LowerBound; i <= UpperBound; ++i) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000547 // w is node at topological index i.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000548 int w = Index2Node[i];
549 if (Visited.test(w)) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000550 // Unmark.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000551 Visited.reset(w);
552 L.push_back(w);
553 shift = shift + 1;
554 } else {
555 Allocate(w, i - shift);
556 }
557 }
558
559 for (unsigned j = 0; j < L.size(); ++j) {
560 Allocate(L[j], i - shift);
561 i = i + 1;
562 }
563}
564
565
Dan Gohmanfd227e92008-03-25 17:10:29 +0000566/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Evan Chengcfd5f822007-09-27 00:25:29 +0000567/// create a cycle.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000568bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
569 if (IsReachable(TargetSU, SU))
Evan Chengcfd5f822007-09-27 00:25:29 +0000570 return true;
571 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
572 I != E; ++I)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000573 if (I->Cost < 0 && IsReachable(TargetSU, I->Dep))
Evan Chengcfd5f822007-09-27 00:25:29 +0000574 return true;
575 return false;
576}
577
Evan Cheng8e136a92007-09-26 21:36:17 +0000578/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000579/// BTCycle in order to schedule a specific node. Returns the last unscheduled
580/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000581void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
582 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000583 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000584 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000585 OldSU = Sequence.back();
586 Sequence.pop_back();
587 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000588 // Don't try to remove SU from AvailableQueue.
589 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000590 UnscheduleNodeBottomUp(OldSU);
591 --CurCycle;
592 }
593
594
595 if (SU->isSucc(OldSU)) {
596 assert(false && "Something is wrong!");
597 abort();
598 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000599
600 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000601}
602
Evan Cheng5924bf72007-09-25 01:54:36 +0000603/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
604/// successors to the newly created node.
605SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000606 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000607 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000608
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000609 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000610 if (!N)
611 return NULL;
612
613 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000614 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000615 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000616 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000617 if (VT == MVT::Flag)
618 return NULL;
619 else if (VT == MVT::Other)
620 TryUnfold = true;
621 }
Evan Cheng79e97132007-10-05 01:39:18 +0000622 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000623 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000624 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000625 if (VT == MVT::Flag)
626 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000627 }
628
629 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000630 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000631 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000632 return NULL;
633
634 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
635 assert(NewNodes.size() == 2 && "Expected a load folding node!");
636
637 N = NewNodes[1];
638 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000639 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000640 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000641 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000642 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
643 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000644 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000645
Dan Gohmane52e0892008-11-11 21:34:44 +0000646 // LoadNode may already exist. This can happen when there is another
647 // load from the same location and producing the same type of value
648 // but it has different alignment or volatileness.
649 bool isNewLoad = true;
650 SUnit *LoadSU;
651 if (LoadNode->getNodeId() != -1) {
652 LoadSU = &SUnits[LoadNode->getNodeId()];
653 isNewLoad = false;
654 } else {
655 LoadSU = CreateNewSUnit(LoadNode);
656 LoadNode->setNodeId(LoadSU->NodeNum);
657
658 LoadSU->Depth = SU->Depth;
659 LoadSU->Height = SU->Height;
660 ComputeLatency(LoadSU);
661 }
662
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000663 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000664 assert(N->getNodeId() == -1 && "Node already inserted!");
665 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000666
Dan Gohman17059682008-07-17 19:10:17 +0000667 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000668 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000669 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000670 NewSU->isTwoAddress = true;
671 break;
672 }
673 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000674 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000675 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000676 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000677 NewSU->Depth = SU->Depth;
678 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000679 ComputeLatency(NewSU);
680
681 SUnit *ChainPred = NULL;
682 SmallVector<SDep, 4> ChainSuccs;
683 SmallVector<SDep, 4> LoadPreds;
684 SmallVector<SDep, 4> NodePreds;
685 SmallVector<SDep, 4> NodeSuccs;
686 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
687 I != E; ++I) {
688 if (I->isCtrl)
689 ChainPred = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000690 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Evan Cheng79e97132007-10-05 01:39:18 +0000691 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
692 else
693 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
694 }
695 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
696 I != E; ++I) {
697 if (I->isCtrl)
698 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
699 I->isCtrl, I->isSpecial));
700 else
701 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
702 I->isCtrl, I->isSpecial));
703 }
704
Dan Gohman4370f262008-04-15 01:22:18 +0000705 if (ChainPred) {
706 RemovePred(SU, ChainPred, true, false);
707 if (isNewLoad)
708 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000709 }
Evan Cheng79e97132007-10-05 01:39:18 +0000710 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
711 SDep *Pred = &LoadPreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000712 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
713 if (isNewLoad) {
714 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000715 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000716 }
Evan Cheng79e97132007-10-05 01:39:18 +0000717 }
718 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
719 SDep *Pred = &NodePreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000720 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
721 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000722 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000723 }
724 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
725 SDep *Succ = &NodeSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000726 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
727 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000728 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000729 }
730 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
731 SDep *Succ = &ChainSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000732 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
733 if (isNewLoad) {
734 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000735 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000736 }
Evan Cheng79e97132007-10-05 01:39:18 +0000737 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000738 if (isNewLoad) {
739 AddPred(NewSU, LoadSU, false, false);
740 }
Evan Cheng79e97132007-10-05 01:39:18 +0000741
Evan Cheng91e0fc92007-12-18 08:42:10 +0000742 if (isNewLoad)
743 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000744 AvailableQueue->addNode(NewSU);
745
746 ++NumUnfolds;
747
748 if (NewSU->NumSuccsLeft == 0) {
749 NewSU->isAvailable = true;
750 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000751 }
752 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000753 }
754
755 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000756 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000757
758 // New SUnit has the exact same predecessors.
759 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
760 I != E; ++I)
761 if (!I->isSpecial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000762 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000763 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
764 }
765
766 // Only copy scheduled successors. Cut them from old node's successor
767 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000768 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000769 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
770 I != E; ++I) {
771 if (I->isSpecial)
772 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000773 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000774 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000775 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000776 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000777 }
778 }
779 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000780 SUnit *Succ = DelDeps[i].first;
781 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000782 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000783 }
784
785 AvailableQueue->updateNode(SU);
786 AvailableQueue->addNode(NewSU);
787
Evan Cheng1ec79b42007-09-27 07:09:03 +0000788 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000789 return NewSU;
790}
791
Evan Cheng1ec79b42007-09-27 07:09:03 +0000792/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
793/// and move all scheduled successors of the given SUnit to the last copy.
794void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
795 const TargetRegisterClass *DestRC,
796 const TargetRegisterClass *SrcRC,
797 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000798 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000799 CopyFromSU->CopySrcRC = SrcRC;
800 CopyFromSU->CopyDstRC = DestRC;
801 CopyFromSU->Depth = SU->Depth;
802 CopyFromSU->Height = SU->Height;
803
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000804 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000805 CopyToSU->CopySrcRC = DestRC;
806 CopyToSU->CopyDstRC = SrcRC;
807
808 // Only copy scheduled successors. Cut them from old node's successor
809 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000810 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000811 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
812 I != E; ++I) {
813 if (I->isSpecial)
814 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000815 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000816 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000817 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000818 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000819 }
820 }
821 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000822 SUnit *Succ = DelDeps[i].first;
823 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000824 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000825 }
826
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000827 AddPred(CopyFromSU, SU, false, false, Reg, -1);
828 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000829
830 AvailableQueue->updateNode(SU);
831 AvailableQueue->addNode(CopyFromSU);
832 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000833 Copies.push_back(CopyFromSU);
834 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000835
Evan Cheng1ec79b42007-09-27 07:09:03 +0000836 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000837}
838
839/// getPhysicalRegisterVT - Returns the ValueType of the physical register
840/// definition of the specified node.
841/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000842static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
843 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000844 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000845 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000846 unsigned NumRes = TID.getNumDefs();
847 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000848 if (Reg == *ImpDef)
849 break;
850 ++NumRes;
851 }
852 return N->getValueType(NumRes);
853}
854
Evan Cheng5924bf72007-09-25 01:54:36 +0000855/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
856/// scheduling of the given node to satisfy live physical register dependencies.
857/// If the specific node is the last one that's available to schedule, do
858/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000859bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
860 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000861 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000862 return false;
863
Evan Chenge6f92252007-09-27 18:46:06 +0000864 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000865 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000866 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
867 I != E; ++I) {
868 if (I->Cost < 0) {
869 unsigned Reg = I->Reg;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000870 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000871 if (RegAdded.insert(Reg))
872 LRegs.push_back(Reg);
873 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000874 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000875 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000876 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000877 if (RegAdded.insert(*Alias))
878 LRegs.push_back(*Alias);
879 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000880 }
881 }
882
Dan Gohman072734e2008-11-13 23:24:17 +0000883 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
884 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000885 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000886 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000887 if (!TID.ImplicitDefs)
888 continue;
889 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000890 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000891 if (RegAdded.insert(*Reg))
892 LRegs.push_back(*Reg);
893 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000894 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000895 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000896 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000897 if (RegAdded.insert(*Alias))
898 LRegs.push_back(*Alias);
899 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000900 }
901 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000902 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000903}
904
Evan Cheng1ec79b42007-09-27 07:09:03 +0000905
Evan Chengd38c22b2006-05-11 23:55:42 +0000906/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
907/// schedulers.
908void ScheduleDAGRRList::ListScheduleBottomUp() {
909 unsigned CurCycle = 0;
910 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000911 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000912 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000913 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
914 RootSU->isAvailable = true;
915 AvailableQueue->push(RootSU);
916 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000917
918 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000919 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000920 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000921 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000922 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000923 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000924 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000925 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000926 SUnit *CurSU = AvailableQueue->pop();
927 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000928 SmallVector<unsigned, 4> LRegs;
929 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
930 break;
931 Delayed = true;
932 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000933
934 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
935 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000936 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000937 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000938
939 // All candidates are delayed due to live physical reg dependencies.
940 // Try backtracking, code duplication, or inserting cross class copies
941 // to resolve it.
942 if (Delayed && !CurSU) {
943 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
944 SUnit *TrySU = NotReady[i];
945 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
946
947 // Try unscheduling up to the point where it's safe to schedule
948 // this node.
949 unsigned LiveCycle = CurCycle;
950 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
951 unsigned Reg = LRegs[j];
952 unsigned LCycle = LiveRegCycles[Reg];
953 LiveCycle = std::min(LiveCycle, LCycle);
954 }
955 SUnit *OldSU = Sequence[LiveCycle];
956 if (!WillCreateCycle(TrySU, OldSU)) {
957 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
958 // Force the current node to be scheduled before the node that
959 // requires the physical reg dep.
960 if (OldSU->isAvailable) {
961 OldSU->isAvailable = false;
962 AvailableQueue->remove(OldSU);
963 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000964 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000965 // If one or more successors has been unscheduled, then the current
966 // node is no longer avaialable. Schedule a successor that's now
967 // available instead.
968 if (!TrySU->isAvailable)
969 CurSU = AvailableQueue->pop();
970 else {
971 CurSU = TrySU;
972 TrySU->isPending = false;
973 NotReady.erase(NotReady.begin()+i);
974 }
975 break;
976 }
977 }
978
979 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +0000980 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +0000981 // "expensive to copy" values to break the dependency. In case even
982 // that doesn't work, insert cross class copies.
983 SUnit *TrySU = NotReady[0];
984 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
985 assert(LRegs.size() == 1 && "Can't handle this yet!");
986 unsigned Reg = LRegs[0];
987 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +0000988 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
989 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000990 // Issue expensive cross register class copies.
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000991 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000992 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +0000993 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000994 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000995 if (!DestRC) {
996 assert(false && "Don't know how to copy this physical register!");
997 abort();
998 }
999 SmallVector<SUnit*, 2> Copies;
1000 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1001 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
1002 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001003 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001004 NewDef = Copies.back();
1005 }
1006
1007 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
1008 << " to SU #" << TrySU->NodeNum << "\n";
1009 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001010 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001011 TrySU->isAvailable = false;
1012 CurSU = NewDef;
1013 }
1014
1015 if (!CurSU) {
1016 assert(false && "Unable to resolve live physical register dependencies!");
1017 abort();
1018 }
1019 }
1020
Evan Chengd38c22b2006-05-11 23:55:42 +00001021 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +00001022 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
1023 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +00001024 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +00001025 if (NotReady[i]->isAvailable)
1026 AvailableQueue->push(NotReady[i]);
1027 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001028 NotReady.clear();
1029
Dan Gohmanc602dd42008-11-21 00:10:42 +00001030 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001031 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +00001032 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001033 }
1034
Evan Chengd38c22b2006-05-11 23:55:42 +00001035 // Reverse the order if it is bottom up.
1036 std::reverse(Sequence.begin(), Sequence.end());
1037
Evan Chengd38c22b2006-05-11 23:55:42 +00001038#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +00001039 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +00001040#endif
1041}
1042
1043//===----------------------------------------------------------------------===//
1044// Top-Down Scheduling
1045//===----------------------------------------------------------------------===//
1046
1047/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001048/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman5ebdb982008-11-18 00:38:59 +00001049void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +00001050 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +00001051
1052#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +00001053 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +00001054 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001055 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +00001056 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001057 assert(0);
1058 }
1059#endif
1060
Evan Cheng038dcc52007-09-28 19:24:24 +00001061 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001062 SuccSU->isAvailable = true;
1063 AvailableQueue->push(SuccSU);
1064 }
1065}
1066
Evan Chengd38c22b2006-05-11 23:55:42 +00001067/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1068/// count of its successors. If a successor pending count is zero, add it to
1069/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +00001070void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001071 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +00001072 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +00001073
Dan Gohman92a36d72008-11-17 21:31:02 +00001074 SU->Cycle = CurCycle;
1075 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001076
1077 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +00001078 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1079 I != E; ++I)
Dan Gohman5ebdb982008-11-18 00:38:59 +00001080 ReleaseSucc(SU, I->Dep, I->isCtrl);
Dan Gohman92a36d72008-11-17 21:31:02 +00001081
Evan Chengd38c22b2006-05-11 23:55:42 +00001082 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +00001083 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001084}
1085
Dan Gohman54a187e2007-08-20 19:28:38 +00001086/// ListScheduleTopDown - The main loop of list scheduling for top-down
1087/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001088void ScheduleDAGRRList::ListScheduleTopDown() {
1089 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001090
1091 // All leaves to Available queue.
1092 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1093 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001094 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001095 AvailableQueue->push(&SUnits[i]);
1096 SUnits[i].isAvailable = true;
1097 }
1098 }
1099
Evan Chengd38c22b2006-05-11 23:55:42 +00001100 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001101 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +00001102 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001103 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001104 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +00001105
Dan Gohmanc602dd42008-11-21 00:10:42 +00001106 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001107 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +00001108 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001109 }
1110
Evan Chengd38c22b2006-05-11 23:55:42 +00001111#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +00001112 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +00001113#endif
1114}
1115
1116
Evan Chengd38c22b2006-05-11 23:55:42 +00001117//===----------------------------------------------------------------------===//
1118// RegReductionPriorityQueue Implementation
1119//===----------------------------------------------------------------------===//
1120//
1121// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1122// to reduce register pressure.
1123//
1124namespace {
1125 template<class SF>
1126 class RegReductionPriorityQueue;
1127
1128 /// Sorting functions for the Available queue.
1129 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1130 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1131 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1132 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1133
1134 bool operator()(const SUnit* left, const SUnit* right) const;
1135 };
1136
1137 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1138 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1139 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1140 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1141
1142 bool operator()(const SUnit* left, const SUnit* right) const;
1143 };
1144} // end anonymous namespace
1145
Evan Cheng961bbd32007-01-08 23:50:38 +00001146static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001147 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +00001148 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +00001149 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
1150}
1151
Dan Gohman186f65d2008-11-20 03:30:37 +00001152/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1153/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001154static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001155CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001156 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1157 if (SethiUllmanNumber != 0)
1158 return SethiUllmanNumber;
1159
1160 unsigned Extra = 0;
1161 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1162 I != E; ++I) {
1163 if (I->isCtrl) continue; // ignore chain preds
1164 SUnit *PredSU = I->Dep;
Dan Gohman186f65d2008-11-20 03:30:37 +00001165 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001166 if (PredSethiUllman > SethiUllmanNumber) {
1167 SethiUllmanNumber = PredSethiUllman;
1168 Extra = 0;
1169 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1170 ++Extra;
1171 }
1172
1173 SethiUllmanNumber += Extra;
1174
1175 if (SethiUllmanNumber == 0)
1176 SethiUllmanNumber = 1;
1177
1178 return SethiUllmanNumber;
1179}
1180
Evan Chengd38c22b2006-05-11 23:55:42 +00001181namespace {
1182 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001183 class VISIBILITY_HIDDEN RegReductionPriorityQueue
1184 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +00001185 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +00001186 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001187
Dan Gohman3f656df2008-11-20 02:45:51 +00001188 protected:
1189 // SUnits - The SUnits for the current graph.
1190 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +00001191
Dan Gohman3f656df2008-11-20 02:45:51 +00001192 const TargetInstrInfo *TII;
1193 const TargetRegisterInfo *TRI;
1194 ScheduleDAGRRList *scheduleDAG;
1195
Dan Gohman186f65d2008-11-20 03:30:37 +00001196 // SethiUllmanNumbers - The SethiUllman number for each node.
1197 std::vector<unsigned> SethiUllmanNumbers;
1198
Dan Gohman3f656df2008-11-20 02:45:51 +00001199 public:
1200 RegReductionPriorityQueue(const TargetInstrInfo *tii,
1201 const TargetRegisterInfo *tri) :
1202 Queue(SF(this)), currentQueueId(0),
1203 TII(tii), TRI(tri), scheduleDAG(NULL) {}
1204
1205 void initNodes(std::vector<SUnit> &sunits) {
1206 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001207 // Add pseudo dependency edges for two-address nodes.
1208 AddPseudoTwoAddrDeps();
1209 // Calculate node priorities.
1210 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001211 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001212
Dan Gohman186f65d2008-11-20 03:30:37 +00001213 void addNode(const SUnit *SU) {
1214 unsigned SUSize = SethiUllmanNumbers.size();
1215 if (SUnits->size() > SUSize)
1216 SethiUllmanNumbers.resize(SUSize*2, 0);
1217 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1218 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001219
Dan Gohman186f65d2008-11-20 03:30:37 +00001220 void updateNode(const SUnit *SU) {
1221 SethiUllmanNumbers[SU->NodeNum] = 0;
1222 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1223 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001224
Dan Gohman186f65d2008-11-20 03:30:37 +00001225 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001226 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001227 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001228 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001229
1230 unsigned getNodePriority(const SUnit *SU) const {
1231 assert(SU->NodeNum < SethiUllmanNumbers.size());
1232 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1233 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1234 // CopyFromReg should be close to its def because it restricts
1235 // allocation choices. But if it is a livein then perhaps we want it
1236 // closer to its uses so it can be coalesced.
1237 return 0xffff;
1238 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1239 // CopyToReg should be close to its uses to facilitate coalescing and
1240 // avoid spilling.
1241 return 0;
1242 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1243 Opc == TargetInstrInfo::INSERT_SUBREG)
1244 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1245 // facilitate coalescing.
1246 return 0;
1247 else if (SU->NumSuccs == 0)
1248 // If SU does not have a use, i.e. it doesn't produce a value that would
1249 // be consumed (e.g. store), then it terminates a chain of computation.
1250 // Give it a large SethiUllman number so it will be scheduled right
1251 // before its predecessors that it doesn't lengthen their live ranges.
1252 return 0xffff;
1253 else if (SU->NumPreds == 0)
1254 // If SU does not have a def, schedule it close to its uses because it
1255 // does not lengthen any live ranges.
1256 return 0;
1257 else
1258 return SethiUllmanNumbers[SU->NodeNum];
1259 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001260
Evan Cheng5924bf72007-09-25 01:54:36 +00001261 unsigned size() const { return Queue.size(); }
1262
Evan Chengd38c22b2006-05-11 23:55:42 +00001263 bool empty() const { return Queue.empty(); }
1264
1265 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001266 assert(!U->NodeQueueId && "Node in the queue already");
1267 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001268 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001269 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001270
Evan Chengd38c22b2006-05-11 23:55:42 +00001271 void push_all(const std::vector<SUnit *> &Nodes) {
1272 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001273 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001274 }
1275
1276 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001277 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001278 SUnit *V = Queue.top();
1279 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001280 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001281 return V;
1282 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001283
Evan Cheng5924bf72007-09-25 01:54:36 +00001284 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001285 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001286 assert(SU->NodeQueueId != 0 && "Not in queue!");
1287 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001288 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001289 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001290
1291 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1292 scheduleDAG = scheduleDag;
1293 }
1294
1295 protected:
1296 bool canClobber(const SUnit *SU, const SUnit *Op);
1297 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001298 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001299 };
1300
Dan Gohman186f65d2008-11-20 03:30:37 +00001301 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1302 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001303
Dan Gohman186f65d2008-11-20 03:30:37 +00001304 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1305 TDRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001306}
1307
Evan Chengb9e3db62007-03-14 22:43:40 +00001308/// closestSucc - Returns the scheduled cycle of the successor which is
1309/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001310static unsigned closestSucc(const SUnit *SU) {
1311 unsigned MaxCycle = 0;
1312 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001313 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001314 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001315 // If there are bunch of CopyToRegs stacked up, they should be considered
1316 // to be at the same position.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001317 if (I->Dep->getNode() && I->Dep->getNode()->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001318 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001319 if (Cycle > MaxCycle)
1320 MaxCycle = Cycle;
1321 }
Evan Cheng28748552007-03-13 23:25:11 +00001322 return MaxCycle;
1323}
1324
Evan Cheng61bc51e2007-12-20 02:22:36 +00001325/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1326/// for scratch registers. Live-in operands and live-out results don't count
1327/// since they are "fixed".
1328static unsigned calcMaxScratches(const SUnit *SU) {
1329 unsigned Scratches = 0;
1330 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1331 I != E; ++I) {
1332 if (I->isCtrl) continue; // ignore chain preds
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001333 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001334 Scratches++;
1335 }
1336 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1337 I != E; ++I) {
1338 if (I->isCtrl) continue; // ignore chain succs
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001339 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001340 Scratches += 10;
1341 }
1342 return Scratches;
1343}
1344
Evan Chengd38c22b2006-05-11 23:55:42 +00001345// Bottom up
1346bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001347 unsigned LPriority = SPQ->getNodePriority(left);
1348 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001349 if (LPriority != RPriority)
1350 return LPriority > RPriority;
1351
1352 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1353 // e.g.
1354 // t1 = op t2, c1
1355 // t3 = op t4, c2
1356 //
1357 // and the following instructions are both ready.
1358 // t2 = op c3
1359 // t4 = op c4
1360 //
1361 // Then schedule t2 = op first.
1362 // i.e.
1363 // t4 = op c4
1364 // t2 = op c3
1365 // t1 = op t2, c1
1366 // t3 = op t4, c2
1367 //
1368 // This creates more short live intervals.
1369 unsigned LDist = closestSucc(left);
1370 unsigned RDist = closestSucc(right);
1371 if (LDist != RDist)
1372 return LDist < RDist;
1373
1374 // Intuitively, it's good to push down instructions whose results are
1375 // liveout so their long live ranges won't conflict with other values
1376 // which are needed inside the BB. Further prioritize liveout instructions
1377 // by the number of operands which are calculated within the BB.
1378 unsigned LScratch = calcMaxScratches(left);
1379 unsigned RScratch = calcMaxScratches(right);
1380 if (LScratch != RScratch)
1381 return LScratch > RScratch;
1382
1383 if (left->Height != right->Height)
1384 return left->Height > right->Height;
1385
1386 if (left->Depth != right->Depth)
1387 return left->Depth < right->Depth;
1388
Roman Levenstein6b371142008-04-29 09:07:59 +00001389 assert(left->NodeQueueId && right->NodeQueueId &&
1390 "NodeQueueId cannot be zero");
1391 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001392}
1393
Dan Gohman3f656df2008-11-20 02:45:51 +00001394template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001395bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001396RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001397 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001398 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001399 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001400 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001401 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001402 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001403 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001404 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001405 if (DU->getNodeId() != -1 &&
1406 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001407 return true;
1408 }
1409 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001410 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001411 return false;
1412}
1413
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001414
Evan Chenga5e595d2007-09-28 22:32:30 +00001415/// hasCopyToRegUse - Return true if SU has a value successor that is a
1416/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001417static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001418 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1419 I != E; ++I) {
1420 if (I->isCtrl) continue;
Dan Gohmane955c482008-08-05 14:45:15 +00001421 const SUnit *SuccSU = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001422 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001423 return true;
1424 }
1425 return false;
1426}
1427
Evan Chengf9891412007-12-20 09:25:31 +00001428/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001429/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001430static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001431 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001432 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001433 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001434 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1435 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001436 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001437 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001438 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001439 if (!SUImpDefs)
1440 return false;
1441 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001442 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001443 if (VT == MVT::Flag || VT == MVT::Other)
1444 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001445 if (!N->hasAnyUseOfValue(i))
1446 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001447 unsigned Reg = ImpDefs[i - NumDefs];
1448 for (;*SUImpDefs; ++SUImpDefs) {
1449 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001450 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001451 return true;
1452 }
1453 }
1454 return false;
1455}
1456
Evan Chengd38c22b2006-05-11 23:55:42 +00001457/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1458/// it as a def&use operand. Add a pseudo control edge from it to the other
1459/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001460/// first (lower in the schedule). If both nodes are two-address, favor the
1461/// one that has a CopyToReg use (more likely to be a loop induction update).
1462/// If both are two-address, but one is commutable while the other is not
1463/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001464template<class SF>
1465void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001466 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001467 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001468 if (!SU->isTwoAddress)
1469 continue;
1470
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001471 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001472 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001473 continue;
1474
Dan Gohman17059682008-07-17 19:10:17 +00001475 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001476 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001477 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001478 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001479 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001480 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1481 continue;
1482 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1483 if (DU->getNodeId() == -1)
1484 continue;
1485 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1486 if (!DUSU) continue;
1487 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1488 E = DUSU->Succs.end(); I != E; ++I) {
1489 if (I->isCtrl) continue;
1490 SUnit *SuccSU = I->Dep;
1491 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001492 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001493 // Be conservative. Ignore if nodes aren't at roughly the same
1494 // depth and height.
1495 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1496 continue;
1497 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1498 continue;
1499 // Don't constrain nodes with physical register defs if the
1500 // predecessor can clobber them.
1501 if (SuccSU->hasPhysRegDefs) {
1502 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001503 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001504 }
1505 // Don't constraint extract_subreg / insert_subreg these may be
1506 // coalesced away. We don't them close to their uses.
1507 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1508 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1509 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1510 continue;
1511 if ((!canClobber(SuccSU, DUSU) ||
1512 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1513 (!SU->isCommutable && SuccSU->isCommutable)) &&
1514 !scheduleDAG->IsReachable(SuccSU, SU)) {
1515 DOUT << "Adding an edge from SU # " << SU->NodeNum
1516 << " to SU #" << SuccSU->NodeNum << "\n";
1517 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001518 }
1519 }
1520 }
1521 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001522}
1523
Evan Cheng6730f032007-01-08 23:55:53 +00001524/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1525/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001526template<class SF>
1527void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001528 SethiUllmanNumbers.assign(SUnits->size(), 0);
1529
1530 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001531 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001532}
Evan Chengd38c22b2006-05-11 23:55:42 +00001533
Roman Levenstein30d09512008-03-27 09:44:37 +00001534/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001535/// predecessors of the successors of the SUnit SU. Stop when the provided
1536/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001537static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1538 unsigned Limit) {
1539 unsigned Sum = 0;
1540 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1541 I != E; ++I) {
Dan Gohmane955c482008-08-05 14:45:15 +00001542 const SUnit *SuccSU = I->Dep;
Roman Levensteinbc674502008-03-27 09:14:57 +00001543 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1544 EE = SuccSU->Preds.end(); II != EE; ++II) {
1545 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001546 if (!PredSU->isScheduled)
1547 if (++Sum > Limit)
1548 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001549 }
1550 }
1551 return Sum;
1552}
1553
Evan Chengd38c22b2006-05-11 23:55:42 +00001554
1555// Top down
1556bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001557 unsigned LPriority = SPQ->getNodePriority(left);
1558 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001559 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1560 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001561 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1562 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001563 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1564 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001565
1566 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1567 return false;
1568 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1569 return true;
1570
Evan Chengd38c22b2006-05-11 23:55:42 +00001571 if (LIsFloater)
1572 LBonus -= 2;
1573 if (RIsFloater)
1574 RBonus -= 2;
1575 if (left->NumSuccs == 1)
1576 LBonus += 2;
1577 if (right->NumSuccs == 1)
1578 RBonus += 2;
1579
Evan Cheng73bdf042008-03-01 00:39:47 +00001580 if (LPriority+LBonus != RPriority+RBonus)
1581 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001582
Evan Cheng73bdf042008-03-01 00:39:47 +00001583 if (left->Depth != right->Depth)
1584 return left->Depth < right->Depth;
1585
1586 if (left->NumSuccsLeft != right->NumSuccsLeft)
1587 return left->NumSuccsLeft > right->NumSuccsLeft;
1588
Roman Levenstein6b371142008-04-29 09:07:59 +00001589 assert(left->NodeQueueId && right->NodeQueueId &&
1590 "NodeQueueId cannot be zero");
1591 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001592}
1593
Evan Chengd38c22b2006-05-11 23:55:42 +00001594//===----------------------------------------------------------------------===//
1595// Public Constructor Functions
1596//===----------------------------------------------------------------------===//
1597
Jim Laskey03593f72006-08-01 18:29:48 +00001598llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1599 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001600 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001601 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001602 bool) {
Dan Gohman5499e892008-11-11 17:50:47 +00001603 const TargetInstrInfo *TII = TM->getInstrInfo();
1604 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001605
Evan Cheng7e4abde2008-07-02 09:23:51 +00001606 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001607
Evan Cheng7e4abde2008-07-02 09:23:51 +00001608 ScheduleDAGRRList *SD =
Dan Gohmanfd08af42008-11-20 03:11:19 +00001609 new ScheduleDAGRRList(DAG, BB, *TM, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001610 PQ->setScheduleDAG(SD);
1611 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001612}
1613
Jim Laskey03593f72006-08-01 18:29:48 +00001614llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1615 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001616 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001617 MachineBasicBlock *BB,
Dan Gohmanfd08af42008-11-20 03:11:19 +00001618 bool) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001619 const TargetInstrInfo *TII = TM->getInstrInfo();
1620 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
1621
1622 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1623
Dan Gohmanfd08af42008-11-20 03:11:19 +00001624 ScheduleDAGRRList *SD = new ScheduleDAGRRList(DAG, BB, *TM, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001625 PQ->setScheduleDAG(SD);
1626 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001627}