blob: 7cec092c0219ba16ff3791f38fe4914017cc4405 [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:
Dan Gohman5ebdb982008-11-18 00:38:59 +0000109 void ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain);
110 void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain);
Evan Cheng8e136a92007-09-26 21:36:17 +0000111 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 Gohman22d07b12008-11-18 02:06:40 +0000189 SUnits[su].dumpAll(this));
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.
Dan Gohman5ebdb982008-11-18 00:38:59 +0000268void ScheduleDAGRRList::ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000269 --PredSU->NumSuccsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +0000270
271#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +0000272 if (PredSU->NumSuccsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000273 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000274 PredSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +0000275 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +0000276 assert(0);
277 }
278#endif
279
Dan Gohman5ebdb982008-11-18 00:38:59 +0000280 // Compute how many cycles it will be before this actually becomes
281 // available. This is the max of the start time of all predecessors plus
282 // their latencies.
283 // If this is a token edge, we don't need to wait for the latency of the
284 // preceeding instruction (e.g. a long-latency load) unless there is also
285 // some other data dependence.
286 unsigned PredDoneCycle = SU->Cycle;
287 if (!isChain)
288 PredDoneCycle += PredSU->Latency;
289 else if (SU->Latency)
290 PredDoneCycle += 1;
291 PredSU->CycleBound = std::max(PredSU->CycleBound, PredDoneCycle);
292
Evan Cheng038dcc52007-09-28 19:24:24 +0000293 if (PredSU->NumSuccsLeft == 0) {
Dan Gohman4370f262008-04-15 01:22:18 +0000294 PredSU->isAvailable = true;
295 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000296 }
297}
298
299/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
300/// count of its predecessors. If a predecessor pending count is zero, add it to
301/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000302void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000303 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000304 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000305 SU->Cycle = CurCycle;
306
307 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000308
309 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000310 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000311 I != E; ++I) {
Dan Gohman5ebdb982008-11-18 00:38:59 +0000312 ReleasePred(SU, I->Dep, I->isCtrl);
Evan Cheng5924bf72007-09-25 01:54:36 +0000313 if (I->Cost < 0) {
314 // This is a physical register dependency and it's impossible or
315 // expensive to copy the register. Make sure nothing that can
316 // clobber the register is scheduled between the predecessor and
317 // this node.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000318 if (!LiveRegDefs[I->Reg]) {
319 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000320 LiveRegDefs[I->Reg] = I->Dep;
321 LiveRegCycles[I->Reg] = CurCycle;
322 }
323 }
324 }
325
326 // Release all the implicit physical register defs that are live.
327 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
328 I != E; ++I) {
329 if (I->Cost < 0) {
330 if (LiveRegCycles[I->Reg] == I->Dep->Cycle) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000331 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000332 assert(LiveRegDefs[I->Reg] == SU &&
333 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000334 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000335 LiveRegDefs[I->Reg] = NULL;
336 LiveRegCycles[I->Reg] = 0;
337 }
338 }
339 }
340
Evan Chengd38c22b2006-05-11 23:55:42 +0000341 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000342}
343
Evan Cheng5924bf72007-09-25 01:54:36 +0000344/// CapturePred - This does the opposite of ReleasePred. Since SU is being
345/// unscheduled, incrcease the succ left count of its predecessors. Remove
346/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000347void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
348 unsigned CycleBound = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000349 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
350 I != E; ++I) {
351 if (I->Dep == SU)
352 continue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000353 CycleBound = std::max(CycleBound,
354 I->Dep->Cycle + PredSU->Latency);
Evan Cheng5924bf72007-09-25 01:54:36 +0000355 }
356
357 if (PredSU->isAvailable) {
358 PredSU->isAvailable = false;
359 if (!PredSU->isPending)
360 AvailableQueue->remove(PredSU);
361 }
362
Roman Levenstein6b371142008-04-29 09:07:59 +0000363 PredSU->CycleBound = CycleBound;
Evan Cheng038dcc52007-09-28 19:24:24 +0000364 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000365}
366
367/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
368/// its predecessor states to reflect the change.
369void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
370 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000371 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000372
373 AvailableQueue->UnscheduledNode(SU);
374
375 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
376 I != E; ++I) {
377 CapturePred(I->Dep, SU, I->isCtrl);
378 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000379 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000380 assert(LiveRegDefs[I->Reg] == I->Dep &&
381 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000382 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000383 LiveRegDefs[I->Reg] = NULL;
384 LiveRegCycles[I->Reg] = 0;
385 }
386 }
387
388 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
389 I != E; ++I) {
390 if (I->Cost < 0) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000391 if (!LiveRegDefs[I->Reg]) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000392 LiveRegDefs[I->Reg] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000393 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000394 }
395 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
396 LiveRegCycles[I->Reg] = I->Dep->Cycle;
397 }
398 }
399
400 SU->Cycle = 0;
401 SU->isScheduled = false;
402 SU->isAvailable = true;
403 AvailableQueue->push(SU);
404}
405
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000406/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane955c482008-08-05 14:45:15 +0000407bool ScheduleDAGRRList::IsReachable(const SUnit *SU, const SUnit *TargetSU) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000408 // If insertion of the edge SU->TargetSU would create a cycle
409 // then there is a path from TargetSU to SU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000410 int UpperBound, LowerBound;
411 LowerBound = Node2Index[TargetSU->NodeNum];
412 UpperBound = Node2Index[SU->NodeNum];
413 bool HasLoop = false;
414 // Is Ord(TargetSU) < Ord(SU) ?
415 if (LowerBound < UpperBound) {
416 Visited.reset();
417 // There may be a path from TargetSU to SU. Check for it.
418 DFS(TargetSU, UpperBound, HasLoop);
Evan Chengcfd5f822007-09-27 00:25:29 +0000419 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000420 return HasLoop;
Evan Chengcfd5f822007-09-27 00:25:29 +0000421}
422
Roman Levenstein733a4d62008-03-26 11:23:38 +0000423/// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000424inline void ScheduleDAGRRList::Allocate(int n, int index) {
425 Node2Index[n] = index;
426 Index2Node[index] = n;
Evan Chengcfd5f822007-09-27 00:25:29 +0000427}
428
Roman Levenstein733a4d62008-03-26 11:23:38 +0000429/// InitDAGTopologicalSorting - create the initial topological
430/// ordering from the DAG to be scheduled.
Evan Cheng2c977312008-07-01 18:05:03 +0000431
432/// The idea of the algorithm is taken from
433/// "Online algorithms for managing the topological order of
434/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
435/// This is the MNR algorithm, which was first introduced by
436/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
437/// "Maintaining a topological order under edge insertions".
438///
439/// Short description of the algorithm:
440///
441/// Topological ordering, ord, of a DAG maps each node to a topological
442/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
443///
444/// This means that if there is a path from the node X to the node Z,
445/// then ord(X) < ord(Z).
446///
447/// This property can be used to check for reachability of nodes:
448/// if Z is reachable from X, then an insertion of the edge Z->X would
449/// create a cycle.
450///
451/// The algorithm first computes a topological ordering for the DAG by
452/// initializing the Index2Node and Node2Index arrays and then tries to keep
453/// the ordering up-to-date after edge insertions by reordering the DAG.
454///
455/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
456/// the nodes reachable from Y, and then shifts them using Shift to lie
457/// immediately after X in Index2Node.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000458void ScheduleDAGRRList::InitDAGTopologicalSorting() {
459 unsigned DAGSize = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000460 std::vector<SUnit*> WorkList;
461 WorkList.reserve(DAGSize);
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000462
463 Index2Node.resize(DAGSize);
464 Node2Index.resize(DAGSize);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000465
Roman Levenstein733a4d62008-03-26 11:23:38 +0000466 // Initialize the data structures.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000467 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
468 SUnit *SU = &SUnits[i];
469 int NodeNum = SU->NodeNum;
470 unsigned Degree = SU->Succs.size();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000471 // Temporarily use the Node2Index array as scratch space for degree counts.
472 Node2Index[NodeNum] = Degree;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000473
474 // Is it a node without dependencies?
475 if (Degree == 0) {
476 assert(SU->Succs.empty() && "SUnit should have no successors");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000477 // Collect leaf nodes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000478 WorkList.push_back(SU);
479 }
480 }
481
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000482 int Id = DAGSize;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000483 while (!WorkList.empty()) {
484 SUnit *SU = WorkList.back();
485 WorkList.pop_back();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000486 Allocate(SU->NodeNum, --Id);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000487 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
488 I != E; ++I) {
489 SUnit *SU = I->Dep;
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000490 if (!--Node2Index[SU->NodeNum])
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000491 // If all dependencies of the node are processed already,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000492 // then the node can be computed now.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000493 WorkList.push_back(SU);
494 }
495 }
496
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000497 Visited.resize(DAGSize);
498
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000499#ifndef NDEBUG
500 // Check correctness of the ordering
501 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
502 SUnit *SU = &SUnits[i];
503 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
504 I != E; ++I) {
505 assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] &&
506 "Wrong topological sorting");
507 }
508 }
509#endif
510}
511
Roman Levenstein733a4d62008-03-26 11:23:38 +0000512/// AddPred - adds an edge from SUnit X to SUnit Y.
513/// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000514bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
515 unsigned PhyReg, int Cost) {
516 int UpperBound, LowerBound;
517 LowerBound = Node2Index[Y->NodeNum];
518 UpperBound = Node2Index[X->NodeNum];
519 bool HasLoop = false;
520 // Is Ord(X) < Ord(Y) ?
521 if (LowerBound < UpperBound) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000522 // Update the topological order.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000523 Visited.reset();
524 DFS(Y, UpperBound, HasLoop);
525 assert(!HasLoop && "Inserted edge creates a loop!");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000526 // Recompute topological indexes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000527 Shift(Visited, LowerBound, UpperBound);
528 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000529 // Now really insert the edge.
530 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000531}
532
Roman Levenstein733a4d62008-03-26 11:23:38 +0000533/// RemovePred - This removes the specified node N from the predecessors of
534/// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000535bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N,
536 bool isCtrl, bool isSpecial) {
537 // InitDAGTopologicalSorting();
538 return M->removePred(N, isCtrl, isSpecial);
539}
540
Roman Levenstein733a4d62008-03-26 11:23:38 +0000541/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
542/// all nodes affected by the edge insertion. These nodes will later get new
543/// topological indexes by means of the Shift method.
Dan Gohmane955c482008-08-05 14:45:15 +0000544void ScheduleDAGRRList::DFS(const SUnit *SU, int UpperBound, bool& HasLoop) {
545 std::vector<const SUnit*> WorkList;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000546 WorkList.reserve(SUnits.size());
547
548 WorkList.push_back(SU);
549 while (!WorkList.empty()) {
550 SU = WorkList.back();
551 WorkList.pop_back();
552 Visited.set(SU->NodeNum);
553 for (int I = SU->Succs.size()-1; I >= 0; --I) {
554 int s = SU->Succs[I].Dep->NodeNum;
555 if (Node2Index[s] == UpperBound) {
556 HasLoop = true;
557 return;
558 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000559 // Visit successors if not already and in affected region.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000560 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
561 WorkList.push_back(SU->Succs[I].Dep);
562 }
563 }
564 }
565}
566
Roman Levenstein733a4d62008-03-26 11:23:38 +0000567/// Shift - Renumber the nodes so that the topological ordering is
568/// preserved.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000569void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound,
570 int UpperBound) {
571 std::vector<int> L;
572 int shift = 0;
573 int i;
574
575 for (i = LowerBound; i <= UpperBound; ++i) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000576 // w is node at topological index i.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000577 int w = Index2Node[i];
578 if (Visited.test(w)) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000579 // Unmark.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000580 Visited.reset(w);
581 L.push_back(w);
582 shift = shift + 1;
583 } else {
584 Allocate(w, i - shift);
585 }
586 }
587
588 for (unsigned j = 0; j < L.size(); ++j) {
589 Allocate(L[j], i - shift);
590 i = i + 1;
591 }
592}
593
594
Dan Gohmanfd227e92008-03-25 17:10:29 +0000595/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Evan Chengcfd5f822007-09-27 00:25:29 +0000596/// create a cycle.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000597bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
598 if (IsReachable(TargetSU, SU))
Evan Chengcfd5f822007-09-27 00:25:29 +0000599 return true;
600 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
601 I != E; ++I)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000602 if (I->Cost < 0 && IsReachable(TargetSU, I->Dep))
Evan Chengcfd5f822007-09-27 00:25:29 +0000603 return true;
604 return false;
605}
606
Evan Cheng8e136a92007-09-26 21:36:17 +0000607/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000608/// BTCycle in order to schedule a specific node. Returns the last unscheduled
609/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000610void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
611 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000612 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000613 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000614 OldSU = Sequence.back();
615 Sequence.pop_back();
616 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000617 // Don't try to remove SU from AvailableQueue.
618 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000619 UnscheduleNodeBottomUp(OldSU);
620 --CurCycle;
621 }
622
623
624 if (SU->isSucc(OldSU)) {
625 assert(false && "Something is wrong!");
626 abort();
627 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000628
629 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000630}
631
Evan Cheng5924bf72007-09-25 01:54:36 +0000632/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
633/// successors to the newly created node.
634SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000635 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000636 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000637
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000638 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000639 if (!N)
640 return NULL;
641
642 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000643 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000644 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000645 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000646 if (VT == MVT::Flag)
647 return NULL;
648 else if (VT == MVT::Other)
649 TryUnfold = true;
650 }
Evan Cheng79e97132007-10-05 01:39:18 +0000651 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000652 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000653 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000654 if (VT == MVT::Flag)
655 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000656 }
657
658 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000659 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000660 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000661 return NULL;
662
663 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
664 assert(NewNodes.size() == 2 && "Expected a load folding node!");
665
666 N = NewNodes[1];
667 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000668 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000669 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000670 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000671 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
672 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000673 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000674
Dan Gohmane52e0892008-11-11 21:34:44 +0000675 // LoadNode may already exist. This can happen when there is another
676 // load from the same location and producing the same type of value
677 // but it has different alignment or volatileness.
678 bool isNewLoad = true;
679 SUnit *LoadSU;
680 if (LoadNode->getNodeId() != -1) {
681 LoadSU = &SUnits[LoadNode->getNodeId()];
682 isNewLoad = false;
683 } else {
684 LoadSU = CreateNewSUnit(LoadNode);
685 LoadNode->setNodeId(LoadSU->NodeNum);
686
687 LoadSU->Depth = SU->Depth;
688 LoadSU->Height = SU->Height;
689 ComputeLatency(LoadSU);
690 }
691
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000692 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000693 assert(N->getNodeId() == -1 && "Node already inserted!");
694 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000695
Dan Gohman17059682008-07-17 19:10:17 +0000696 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000697 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000698 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000699 NewSU->isTwoAddress = true;
700 break;
701 }
702 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000703 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000704 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000705 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000706 NewSU->Depth = SU->Depth;
707 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000708 ComputeLatency(NewSU);
709
710 SUnit *ChainPred = NULL;
711 SmallVector<SDep, 4> ChainSuccs;
712 SmallVector<SDep, 4> LoadPreds;
713 SmallVector<SDep, 4> NodePreds;
714 SmallVector<SDep, 4> NodeSuccs;
715 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
716 I != E; ++I) {
717 if (I->isCtrl)
718 ChainPred = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000719 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Evan Cheng79e97132007-10-05 01:39:18 +0000720 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
721 else
722 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
723 }
724 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
725 I != E; ++I) {
726 if (I->isCtrl)
727 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
728 I->isCtrl, I->isSpecial));
729 else
730 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
731 I->isCtrl, I->isSpecial));
732 }
733
Dan Gohman4370f262008-04-15 01:22:18 +0000734 if (ChainPred) {
735 RemovePred(SU, ChainPred, true, false);
736 if (isNewLoad)
737 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000738 }
Evan Cheng79e97132007-10-05 01:39:18 +0000739 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
740 SDep *Pred = &LoadPreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000741 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
742 if (isNewLoad) {
743 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000744 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000745 }
Evan Cheng79e97132007-10-05 01:39:18 +0000746 }
747 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
748 SDep *Pred = &NodePreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000749 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
750 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000751 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000752 }
753 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
754 SDep *Succ = &NodeSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000755 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
756 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000757 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000758 }
759 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
760 SDep *Succ = &ChainSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000761 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
762 if (isNewLoad) {
763 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000764 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000765 }
Evan Cheng79e97132007-10-05 01:39:18 +0000766 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000767 if (isNewLoad) {
768 AddPred(NewSU, LoadSU, false, false);
769 }
Evan Cheng79e97132007-10-05 01:39:18 +0000770
Evan Cheng91e0fc92007-12-18 08:42:10 +0000771 if (isNewLoad)
772 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000773 AvailableQueue->addNode(NewSU);
774
775 ++NumUnfolds;
776
777 if (NewSU->NumSuccsLeft == 0) {
778 NewSU->isAvailable = true;
779 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000780 }
781 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000782 }
783
784 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000785 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000786
787 // New SUnit has the exact same predecessors.
788 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
789 I != E; ++I)
790 if (!I->isSpecial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000791 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000792 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
793 }
794
795 // Only copy scheduled successors. Cut them from old node's successor
796 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000797 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000798 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
799 I != E; ++I) {
800 if (I->isSpecial)
801 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000802 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000803 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000804 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000805 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000806 }
807 }
808 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000809 SUnit *Succ = DelDeps[i].first;
810 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000811 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000812 }
813
814 AvailableQueue->updateNode(SU);
815 AvailableQueue->addNode(NewSU);
816
Evan Cheng1ec79b42007-09-27 07:09:03 +0000817 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000818 return NewSU;
819}
820
Evan Cheng1ec79b42007-09-27 07:09:03 +0000821/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
822/// and move all scheduled successors of the given SUnit to the last copy.
823void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
824 const TargetRegisterClass *DestRC,
825 const TargetRegisterClass *SrcRC,
826 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000827 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000828 CopyFromSU->CopySrcRC = SrcRC;
829 CopyFromSU->CopyDstRC = DestRC;
830 CopyFromSU->Depth = SU->Depth;
831 CopyFromSU->Height = SU->Height;
832
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000833 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000834 CopyToSU->CopySrcRC = DestRC;
835 CopyToSU->CopyDstRC = SrcRC;
836
837 // Only copy scheduled successors. Cut them from old node's successor
838 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000839 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000840 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
841 I != E; ++I) {
842 if (I->isSpecial)
843 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000844 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000845 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000846 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000847 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000848 }
849 }
850 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000851 SUnit *Succ = DelDeps[i].first;
852 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000853 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000854 }
855
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000856 AddPred(CopyFromSU, SU, false, false, Reg, -1);
857 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000858
859 AvailableQueue->updateNode(SU);
860 AvailableQueue->addNode(CopyFromSU);
861 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000862 Copies.push_back(CopyFromSU);
863 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000864
Evan Cheng1ec79b42007-09-27 07:09:03 +0000865 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000866}
867
868/// getPhysicalRegisterVT - Returns the ValueType of the physical register
869/// definition of the specified node.
870/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000871static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
872 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000873 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000874 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000875 unsigned NumRes = TID.getNumDefs();
876 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000877 if (Reg == *ImpDef)
878 break;
879 ++NumRes;
880 }
881 return N->getValueType(NumRes);
882}
883
Evan Cheng5924bf72007-09-25 01:54:36 +0000884/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
885/// scheduling of the given node to satisfy live physical register dependencies.
886/// If the specific node is the last one that's available to schedule, do
887/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000888bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
889 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000890 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000891 return false;
892
Evan Chenge6f92252007-09-27 18:46:06 +0000893 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000894 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000895 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
896 I != E; ++I) {
897 if (I->Cost < 0) {
898 unsigned Reg = I->Reg;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000899 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000900 if (RegAdded.insert(Reg))
901 LRegs.push_back(Reg);
902 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000903 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000904 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000905 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000906 if (RegAdded.insert(*Alias))
907 LRegs.push_back(*Alias);
908 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000909 }
910 }
911
Dan Gohman072734e2008-11-13 23:24:17 +0000912 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
913 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000914 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000915 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000916 if (!TID.ImplicitDefs)
917 continue;
918 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000919 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000920 if (RegAdded.insert(*Reg))
921 LRegs.push_back(*Reg);
922 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000923 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000924 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000925 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000926 if (RegAdded.insert(*Alias))
927 LRegs.push_back(*Alias);
928 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000929 }
930 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000931 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000932}
933
Evan Cheng1ec79b42007-09-27 07:09:03 +0000934
Evan Chengd38c22b2006-05-11 23:55:42 +0000935/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
936/// schedulers.
937void ScheduleDAGRRList::ListScheduleBottomUp() {
938 unsigned CurCycle = 0;
939 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000940 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000941 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000942 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
943 RootSU->isAvailable = true;
944 AvailableQueue->push(RootSU);
945 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000946
947 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000948 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000949 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000950 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000951 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000952 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000953 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000954 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000955 SUnit *CurSU = AvailableQueue->pop();
956 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000957 if (CurSU->CycleBound <= CurCycle) {
958 SmallVector<unsigned, 4> LRegs;
959 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000960 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000961 Delayed = true;
962 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000963 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000964
965 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
966 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000967 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000968 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000969
970 // All candidates are delayed due to live physical reg dependencies.
971 // Try backtracking, code duplication, or inserting cross class copies
972 // to resolve it.
973 if (Delayed && !CurSU) {
974 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
975 SUnit *TrySU = NotReady[i];
976 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
977
978 // Try unscheduling up to the point where it's safe to schedule
979 // this node.
980 unsigned LiveCycle = CurCycle;
981 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
982 unsigned Reg = LRegs[j];
983 unsigned LCycle = LiveRegCycles[Reg];
984 LiveCycle = std::min(LiveCycle, LCycle);
985 }
986 SUnit *OldSU = Sequence[LiveCycle];
987 if (!WillCreateCycle(TrySU, OldSU)) {
988 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
989 // Force the current node to be scheduled before the node that
990 // requires the physical reg dep.
991 if (OldSU->isAvailable) {
992 OldSU->isAvailable = false;
993 AvailableQueue->remove(OldSU);
994 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000995 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000996 // If one or more successors has been unscheduled, then the current
997 // node is no longer avaialable. Schedule a successor that's now
998 // available instead.
999 if (!TrySU->isAvailable)
1000 CurSU = AvailableQueue->pop();
1001 else {
1002 CurSU = TrySU;
1003 TrySU->isPending = false;
1004 NotReady.erase(NotReady.begin()+i);
1005 }
1006 break;
1007 }
1008 }
1009
1010 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +00001011 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +00001012 // "expensive to copy" values to break the dependency. In case even
1013 // that doesn't work, insert cross class copies.
1014 SUnit *TrySU = NotReady[0];
1015 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1016 assert(LRegs.size() == 1 && "Can't handle this yet!");
1017 unsigned Reg = LRegs[0];
1018 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +00001019 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
1020 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +00001021 // Issue expensive cross register class copies.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001022 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001023 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +00001024 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001025 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001026 if (!DestRC) {
1027 assert(false && "Don't know how to copy this physical register!");
1028 abort();
1029 }
1030 SmallVector<SUnit*, 2> Copies;
1031 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1032 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
1033 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001034 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001035 NewDef = Copies.back();
1036 }
1037
1038 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
1039 << " to SU #" << TrySU->NodeNum << "\n";
1040 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001041 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001042 TrySU->isAvailable = false;
1043 CurSU = NewDef;
1044 }
1045
1046 if (!CurSU) {
1047 assert(false && "Unable to resolve live physical register dependencies!");
1048 abort();
1049 }
1050 }
1051
Evan Chengd38c22b2006-05-11 23:55:42 +00001052 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +00001053 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
1054 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +00001055 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +00001056 if (NotReady[i]->isAvailable)
1057 AvailableQueue->push(NotReady[i]);
1058 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001059 NotReady.clear();
1060
Evan Cheng5924bf72007-09-25 01:54:36 +00001061 if (!CurSU)
1062 Sequence.push_back(0);
1063 else {
1064 ScheduleNodeBottomUp(CurSU, CurCycle);
1065 Sequence.push_back(CurSU);
1066 }
1067 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001068 }
1069
Evan Chengd38c22b2006-05-11 23:55:42 +00001070 // Reverse the order if it is bottom up.
1071 std::reverse(Sequence.begin(), Sequence.end());
1072
1073
1074#ifndef NDEBUG
1075 // Verify that all SUnits were scheduled.
1076 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001077 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001078 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001079 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman4370f262008-04-15 01:22:18 +00001080 if (!SUnits[i].isScheduled) {
1081 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1082 ++DeadNodes;
1083 continue;
1084 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001085 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001086 cerr << "*** List scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001087 SUnits[i].dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +00001088 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001089 AnyNotSched = true;
1090 }
Dan Gohman4370f262008-04-15 01:22:18 +00001091 if (SUnits[i].NumSuccsLeft != 0) {
1092 if (!AnyNotSched)
1093 cerr << "*** List scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001094 SUnits[i].dump(this);
Dan Gohman4370f262008-04-15 01:22:18 +00001095 cerr << "has successors left!\n";
1096 AnyNotSched = true;
1097 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001098 }
Dan Gohman82b66732008-04-15 22:40:14 +00001099 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1100 if (!Sequence[i])
1101 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001102 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001103 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001104 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001105#endif
1106}
1107
1108//===----------------------------------------------------------------------===//
1109// Top-Down Scheduling
1110//===----------------------------------------------------------------------===//
1111
1112/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001113/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman5ebdb982008-11-18 00:38:59 +00001114void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +00001115 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +00001116
1117#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +00001118 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +00001119 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001120 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +00001121 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001122 assert(0);
1123 }
1124#endif
1125
Dan Gohman5ebdb982008-11-18 00:38:59 +00001126 // Compute how many cycles it will be before this actually becomes
1127 // available. This is the max of the start time of all predecessors plus
1128 // their latencies.
1129 // If this is a token edge, we don't need to wait for the latency of the
1130 // preceeding instruction (e.g. a long-latency load) unless there is also
1131 // some other data dependence.
1132 unsigned PredDoneCycle = SU->Cycle;
1133 if (!isChain)
1134 PredDoneCycle += SU->Latency;
1135 else if (SU->Latency)
1136 PredDoneCycle += 1;
1137 SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle);
1138
Evan Cheng038dcc52007-09-28 19:24:24 +00001139 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001140 SuccSU->isAvailable = true;
1141 AvailableQueue->push(SuccSU);
1142 }
1143}
1144
1145
1146/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1147/// count of its successors. If a successor pending count is zero, add it to
1148/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +00001149void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001150 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +00001151 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +00001152
Dan Gohman92a36d72008-11-17 21:31:02 +00001153 SU->Cycle = CurCycle;
1154 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001155
1156 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +00001157 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1158 I != E; ++I)
Dan Gohman5ebdb982008-11-18 00:38:59 +00001159 ReleaseSucc(SU, I->Dep, I->isCtrl);
Dan Gohman92a36d72008-11-17 21:31:02 +00001160
Evan Chengd38c22b2006-05-11 23:55:42 +00001161 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +00001162 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001163}
1164
Dan Gohman54a187e2007-08-20 19:28:38 +00001165/// ListScheduleTopDown - The main loop of list scheduling for top-down
1166/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001167void ScheduleDAGRRList::ListScheduleTopDown() {
1168 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001169
1170 // All leaves to Available queue.
1171 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1172 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001173 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001174 AvailableQueue->push(&SUnits[i]);
1175 SUnits[i].isAvailable = true;
1176 }
1177 }
1178
Evan Chengd38c22b2006-05-11 23:55:42 +00001179 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001180 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +00001181 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +00001182 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001183 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001184 SUnit *CurSU = AvailableQueue->pop();
1185 while (CurSU && CurSU->CycleBound > CurCycle) {
1186 NotReady.push_back(CurSU);
1187 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +00001188 }
1189
1190 // Add the nodes that aren't ready back onto the available list.
1191 AvailableQueue->push_all(NotReady);
1192 NotReady.clear();
1193
Evan Cheng5924bf72007-09-25 01:54:36 +00001194 if (!CurSU)
1195 Sequence.push_back(0);
1196 else {
1197 ScheduleNodeTopDown(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +00001198 }
Dan Gohman4370f262008-04-15 01:22:18 +00001199 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001200 }
1201
1202
1203#ifndef NDEBUG
1204 // Verify that all SUnits were scheduled.
1205 bool AnyNotSched = false;
Dan Gohman4370f262008-04-15 01:22:18 +00001206 unsigned DeadNodes = 0;
Dan Gohman82b66732008-04-15 22:40:14 +00001207 unsigned Noops = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001208 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1209 if (!SUnits[i].isScheduled) {
Dan Gohman4370f262008-04-15 01:22:18 +00001210 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
1211 ++DeadNodes;
1212 continue;
1213 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001214 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +00001215 cerr << "*** List scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001216 SUnits[i].dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +00001217 cerr << "has not been scheduled!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001218 AnyNotSched = true;
1219 }
Dan Gohman4370f262008-04-15 01:22:18 +00001220 if (SUnits[i].NumPredsLeft != 0) {
1221 if (!AnyNotSched)
1222 cerr << "*** List scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001223 SUnits[i].dump(this);
Dan Gohman4370f262008-04-15 01:22:18 +00001224 cerr << "has predecessors left!\n";
1225 AnyNotSched = true;
1226 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001227 }
Dan Gohman82b66732008-04-15 22:40:14 +00001228 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
1229 if (!Sequence[i])
1230 ++Noops;
Evan Chengd38c22b2006-05-11 23:55:42 +00001231 assert(!AnyNotSched);
Dan Gohman82b66732008-04-15 22:40:14 +00001232 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
Dan Gohman4370f262008-04-15 01:22:18 +00001233 "The number of nodes scheduled doesn't match the expected number!");
Evan Chengd38c22b2006-05-11 23:55:42 +00001234#endif
1235}
1236
1237
1238
1239//===----------------------------------------------------------------------===//
1240// RegReductionPriorityQueue Implementation
1241//===----------------------------------------------------------------------===//
1242//
1243// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1244// to reduce register pressure.
1245//
1246namespace {
1247 template<class SF>
1248 class RegReductionPriorityQueue;
1249
1250 /// Sorting functions for the Available queue.
1251 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1252 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1253 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1254 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1255
1256 bool operator()(const SUnit* left, const SUnit* right) const;
1257 };
1258
Evan Cheng7e4abde2008-07-02 09:23:51 +00001259 struct bu_ls_rr_fast_sort : public std::binary_function<SUnit*, SUnit*, bool>{
1260 RegReductionPriorityQueue<bu_ls_rr_fast_sort> *SPQ;
1261 bu_ls_rr_fast_sort(RegReductionPriorityQueue<bu_ls_rr_fast_sort> *spq)
1262 : SPQ(spq) {}
1263 bu_ls_rr_fast_sort(const bu_ls_rr_fast_sort &RHS) : SPQ(RHS.SPQ) {}
1264
1265 bool operator()(const SUnit* left, const SUnit* right) const;
1266 };
1267
Evan Chengd38c22b2006-05-11 23:55:42 +00001268 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1269 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1270 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1271 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1272
1273 bool operator()(const SUnit* left, const SUnit* right) const;
1274 };
1275} // end anonymous namespace
1276
Evan Cheng961bbd32007-01-08 23:50:38 +00001277static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001278 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +00001279 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +00001280 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
1281}
1282
Evan Cheng7e4abde2008-07-02 09:23:51 +00001283/// CalcNodeBUSethiUllmanNumber - Compute Sethi Ullman number for bottom up
1284/// scheduling. Smaller number is the higher priority.
1285static unsigned
1286CalcNodeBUSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1287 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1288 if (SethiUllmanNumber != 0)
1289 return SethiUllmanNumber;
1290
1291 unsigned Extra = 0;
1292 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1293 I != E; ++I) {
1294 if (I->isCtrl) continue; // ignore chain preds
1295 SUnit *PredSU = I->Dep;
1296 unsigned PredSethiUllman = CalcNodeBUSethiUllmanNumber(PredSU, SUNumbers);
1297 if (PredSethiUllman > SethiUllmanNumber) {
1298 SethiUllmanNumber = PredSethiUllman;
1299 Extra = 0;
1300 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1301 ++Extra;
1302 }
1303
1304 SethiUllmanNumber += Extra;
1305
1306 if (SethiUllmanNumber == 0)
1307 SethiUllmanNumber = 1;
1308
1309 return SethiUllmanNumber;
1310}
1311
1312/// CalcNodeTDSethiUllmanNumber - Compute Sethi Ullman number for top down
1313/// scheduling. Smaller number is the higher priority.
1314static unsigned
1315CalcNodeTDSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1316 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1317 if (SethiUllmanNumber != 0)
1318 return SethiUllmanNumber;
1319
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001320 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001321 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1322 SethiUllmanNumber = 0xffff;
1323 else if (SU->NumSuccsLeft == 0)
1324 // If SU does not have a use, i.e. it doesn't produce a value that would
1325 // be consumed (e.g. store), then it terminates a chain of computation.
1326 // Give it a small SethiUllman number so it will be scheduled right before
1327 // its predecessors that it doesn't lengthen their live ranges.
1328 SethiUllmanNumber = 0;
1329 else if (SU->NumPredsLeft == 0 &&
1330 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
1331 SethiUllmanNumber = 0xffff;
1332 else {
1333 int Extra = 0;
1334 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1335 I != E; ++I) {
1336 if (I->isCtrl) continue; // ignore chain preds
1337 SUnit *PredSU = I->Dep;
1338 unsigned PredSethiUllman = CalcNodeTDSethiUllmanNumber(PredSU, SUNumbers);
1339 if (PredSethiUllman > SethiUllmanNumber) {
1340 SethiUllmanNumber = PredSethiUllman;
1341 Extra = 0;
1342 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1343 ++Extra;
1344 }
1345
1346 SethiUllmanNumber += Extra;
1347 }
1348
1349 return SethiUllmanNumber;
1350}
1351
1352
Evan Chengd38c22b2006-05-11 23:55:42 +00001353namespace {
1354 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001355 class VISIBILITY_HIDDEN RegReductionPriorityQueue
1356 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +00001357 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +00001358 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001359
1360 public:
1361 RegReductionPriorityQueue() :
Roman Levenstein6b371142008-04-29 09:07:59 +00001362 Queue(SF(this)), currentQueueId(0) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001363
Dan Gohman50c76be2008-10-31 19:06:33 +00001364 virtual void initNodes(std::vector<SUnit> &sunits) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001365
Dan Gohman50c76be2008-10-31 19:06:33 +00001366 virtual void addNode(const SUnit *SU) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001367
Dan Gohman50c76be2008-10-31 19:06:33 +00001368 virtual void updateNode(const SUnit *SU) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001369
Dan Gohman50c76be2008-10-31 19:06:33 +00001370 virtual void releaseState() = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001371
Dan Gohman50c76be2008-10-31 19:06:33 +00001372 virtual unsigned getNodePriority(const SUnit *SU) const = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001373
Evan Cheng5924bf72007-09-25 01:54:36 +00001374 unsigned size() const { return Queue.size(); }
1375
Evan Chengd38c22b2006-05-11 23:55:42 +00001376 bool empty() const { return Queue.empty(); }
1377
1378 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001379 assert(!U->NodeQueueId && "Node in the queue already");
1380 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001381 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001382 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001383
Evan Chengd38c22b2006-05-11 23:55:42 +00001384 void push_all(const std::vector<SUnit *> &Nodes) {
1385 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001386 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001387 }
1388
1389 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001390 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001391 SUnit *V = Queue.top();
1392 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001393 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001394 return V;
1395 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001396
Evan Cheng5924bf72007-09-25 01:54:36 +00001397 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001398 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001399 assert(SU->NodeQueueId != 0 && "Not in queue!");
1400 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001401 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001402 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001403 };
1404
Chris Lattner996795b2006-06-28 23:17:24 +00001405 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001406 : public RegReductionPriorityQueue<bu_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001407 // SUnits - The SUnits for the current graph.
Dan Gohmane955c482008-08-05 14:45:15 +00001408 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +00001409
1410 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001411 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001412
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001413 const TargetInstrInfo *TII;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001414 const TargetRegisterInfo *TRI;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001415 ScheduleDAGRRList *scheduleDAG;
Evan Cheng2c977312008-07-01 18:05:03 +00001416
Evan Chengd38c22b2006-05-11 23:55:42 +00001417 public:
Evan Chengf9891412007-12-20 09:25:31 +00001418 explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001419 const TargetRegisterInfo *tri)
1420 : TII(tii), TRI(tri), scheduleDAG(NULL) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001421
Dan Gohman46520a22008-06-21 19:18:17 +00001422 void initNodes(std::vector<SUnit> &sunits) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001423 SUnits = &sunits;
1424 // Add pseudo dependency edges for two-address nodes.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001425 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001426 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001427 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001428 }
1429
Evan Cheng5924bf72007-09-25 01:54:36 +00001430 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001431 unsigned SUSize = SethiUllmanNumbers.size();
1432 if (SUnits->size() > SUSize)
1433 SethiUllmanNumbers.resize(SUSize*2, 0);
1434 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001435 }
1436
1437 void updateNode(const SUnit *SU) {
1438 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001439 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001440 }
1441
Evan Chengd38c22b2006-05-11 23:55:42 +00001442 void releaseState() {
1443 SUnits = 0;
1444 SethiUllmanNumbers.clear();
1445 }
1446
Evan Cheng6730f032007-01-08 23:55:53 +00001447 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001448 assert(SU->NodeNum < SethiUllmanNumbers.size());
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001449 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001450 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1451 // CopyFromReg should be close to its def because it restricts
1452 // allocation choices. But if it is a livein then perhaps we want it
1453 // closer to its uses so it can be coalesced.
1454 return 0xffff;
1455 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1456 // CopyToReg should be close to its uses to facilitate coalescing and
1457 // avoid spilling.
1458 return 0;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001459 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1460 Opc == TargetInstrInfo::INSERT_SUBREG)
1461 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1462 // facilitate coalescing.
1463 return 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001464 else if (SU->NumSuccs == 0)
1465 // If SU does not have a use, i.e. it doesn't produce a value that would
1466 // be consumed (e.g. store), then it terminates a chain of computation.
1467 // Give it a large SethiUllman number so it will be scheduled right
1468 // before its predecessors that it doesn't lengthen their live ranges.
1469 return 0xffff;
1470 else if (SU->NumPreds == 0)
1471 // If SU does not have a def, schedule it close to its uses because it
1472 // does not lengthen any live ranges.
1473 return 0;
1474 else
1475 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001476 }
1477
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001478 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1479 scheduleDAG = scheduleDag;
1480 }
1481
Evan Chengd38c22b2006-05-11 23:55:42 +00001482 private:
Evan Cheng73bdf042008-03-01 00:39:47 +00001483 bool canClobber(const SUnit *SU, const SUnit *Op);
Evan Chengd38c22b2006-05-11 23:55:42 +00001484 void AddPseudoTwoAddrDeps();
Evan Cheng6730f032007-01-08 23:55:53 +00001485 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001486 };
1487
1488
1489 class VISIBILITY_HIDDEN BURegReductionFastPriorityQueue
1490 : public RegReductionPriorityQueue<bu_ls_rr_fast_sort> {
1491 // SUnits - The SUnits for the current graph.
1492 const std::vector<SUnit> *SUnits;
1493
1494 // SethiUllmanNumbers - The SethiUllman number for each node.
1495 std::vector<unsigned> SethiUllmanNumbers;
1496 public:
1497 explicit BURegReductionFastPriorityQueue() {}
1498
1499 void initNodes(std::vector<SUnit> &sunits) {
1500 SUnits = &sunits;
1501 // Calculate node priorities.
1502 CalculateSethiUllmanNumbers();
1503 }
1504
1505 void addNode(const SUnit *SU) {
1506 unsigned SUSize = SethiUllmanNumbers.size();
1507 if (SUnits->size() > SUSize)
1508 SethiUllmanNumbers.resize(SUSize*2, 0);
1509 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1510 }
1511
1512 void updateNode(const SUnit *SU) {
1513 SethiUllmanNumbers[SU->NodeNum] = 0;
1514 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1515 }
1516
1517 void releaseState() {
1518 SUnits = 0;
1519 SethiUllmanNumbers.clear();
1520 }
1521
1522 unsigned getNodePriority(const SUnit *SU) const {
1523 return SethiUllmanNumbers[SU->NodeNum];
1524 }
1525
1526 private:
1527 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001528 };
1529
1530
Dan Gohman54a187e2007-08-20 19:28:38 +00001531 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001532 : public RegReductionPriorityQueue<td_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001533 // SUnits - The SUnits for the current graph.
1534 const std::vector<SUnit> *SUnits;
1535
1536 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001537 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001538
1539 public:
1540 TDRegReductionPriorityQueue() {}
1541
Dan Gohman46520a22008-06-21 19:18:17 +00001542 void initNodes(std::vector<SUnit> &sunits) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001543 SUnits = &sunits;
1544 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001545 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001546 }
1547
Evan Cheng5924bf72007-09-25 01:54:36 +00001548 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001549 unsigned SUSize = SethiUllmanNumbers.size();
1550 if (SUnits->size() > SUSize)
1551 SethiUllmanNumbers.resize(SUSize*2, 0);
1552 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001553 }
1554
1555 void updateNode(const SUnit *SU) {
1556 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001557 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001558 }
1559
Evan Chengd38c22b2006-05-11 23:55:42 +00001560 void releaseState() {
1561 SUnits = 0;
1562 SethiUllmanNumbers.clear();
1563 }
1564
Evan Cheng6730f032007-01-08 23:55:53 +00001565 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001566 assert(SU->NodeNum < SethiUllmanNumbers.size());
1567 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001568 }
1569
1570 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001571 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001572 };
1573}
1574
Evan Chengb9e3db62007-03-14 22:43:40 +00001575/// closestSucc - Returns the scheduled cycle of the successor which is
1576/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001577static unsigned closestSucc(const SUnit *SU) {
1578 unsigned MaxCycle = 0;
1579 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001580 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001581 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001582 // If there are bunch of CopyToRegs stacked up, they should be considered
1583 // to be at the same position.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001584 if (I->Dep->getNode() && I->Dep->getNode()->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001585 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001586 if (Cycle > MaxCycle)
1587 MaxCycle = Cycle;
1588 }
Evan Cheng28748552007-03-13 23:25:11 +00001589 return MaxCycle;
1590}
1591
Evan Cheng61bc51e2007-12-20 02:22:36 +00001592/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1593/// for scratch registers. Live-in operands and live-out results don't count
1594/// since they are "fixed".
1595static unsigned calcMaxScratches(const SUnit *SU) {
1596 unsigned Scratches = 0;
1597 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1598 I != E; ++I) {
1599 if (I->isCtrl) continue; // ignore chain preds
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001600 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001601 Scratches++;
1602 }
1603 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1604 I != E; ++I) {
1605 if (I->isCtrl) continue; // ignore chain succs
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001606 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001607 Scratches += 10;
1608 }
1609 return Scratches;
1610}
1611
Evan Chengd38c22b2006-05-11 23:55:42 +00001612// Bottom up
1613bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001614 unsigned LPriority = SPQ->getNodePriority(left);
1615 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001616 if (LPriority != RPriority)
1617 return LPriority > RPriority;
1618
1619 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1620 // e.g.
1621 // t1 = op t2, c1
1622 // t3 = op t4, c2
1623 //
1624 // and the following instructions are both ready.
1625 // t2 = op c3
1626 // t4 = op c4
1627 //
1628 // Then schedule t2 = op first.
1629 // i.e.
1630 // t4 = op c4
1631 // t2 = op c3
1632 // t1 = op t2, c1
1633 // t3 = op t4, c2
1634 //
1635 // This creates more short live intervals.
1636 unsigned LDist = closestSucc(left);
1637 unsigned RDist = closestSucc(right);
1638 if (LDist != RDist)
1639 return LDist < RDist;
1640
1641 // Intuitively, it's good to push down instructions whose results are
1642 // liveout so their long live ranges won't conflict with other values
1643 // which are needed inside the BB. Further prioritize liveout instructions
1644 // by the number of operands which are calculated within the BB.
1645 unsigned LScratch = calcMaxScratches(left);
1646 unsigned RScratch = calcMaxScratches(right);
1647 if (LScratch != RScratch)
1648 return LScratch > RScratch;
1649
1650 if (left->Height != right->Height)
1651 return left->Height > right->Height;
1652
1653 if (left->Depth != right->Depth)
1654 return left->Depth < right->Depth;
1655
1656 if (left->CycleBound != right->CycleBound)
1657 return left->CycleBound > right->CycleBound;
1658
Roman Levenstein6b371142008-04-29 09:07:59 +00001659 assert(left->NodeQueueId && right->NodeQueueId &&
1660 "NodeQueueId cannot be zero");
1661 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001662}
1663
Dan Gohman4b49be12008-06-21 01:08:22 +00001664bool
Evan Cheng7e4abde2008-07-02 09:23:51 +00001665bu_ls_rr_fast_sort::operator()(const SUnit *left, const SUnit *right) const {
1666 unsigned LPriority = SPQ->getNodePriority(left);
1667 unsigned RPriority = SPQ->getNodePriority(right);
1668 if (LPriority != RPriority)
1669 return LPriority > RPriority;
1670 assert(left->NodeQueueId && right->NodeQueueId &&
1671 "NodeQueueId cannot be zero");
1672 return (left->NodeQueueId > right->NodeQueueId);
1673}
1674
1675bool
Dan Gohman4b49be12008-06-21 01:08:22 +00001676BURegReductionPriorityQueue::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001677 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001678 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001679 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001680 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001681 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001682 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001683 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001684 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001685 if (DU->getNodeId() != -1 &&
1686 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001687 return true;
1688 }
1689 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001690 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001691 return false;
1692}
1693
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001694
Evan Chenga5e595d2007-09-28 22:32:30 +00001695/// hasCopyToRegUse - Return true if SU has a value successor that is a
1696/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001697static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001698 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1699 I != E; ++I) {
1700 if (I->isCtrl) continue;
Dan Gohmane955c482008-08-05 14:45:15 +00001701 const SUnit *SuccSU = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001702 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001703 return true;
1704 }
1705 return false;
1706}
1707
Evan Chengf9891412007-12-20 09:25:31 +00001708/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001709/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001710static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001711 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001712 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001713 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001714 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1715 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001716 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001717 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001718 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001719 if (!SUImpDefs)
1720 return false;
1721 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001722 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001723 if (VT == MVT::Flag || VT == MVT::Other)
1724 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001725 if (!N->hasAnyUseOfValue(i))
1726 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001727 unsigned Reg = ImpDefs[i - NumDefs];
1728 for (;*SUImpDefs; ++SUImpDefs) {
1729 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001730 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001731 return true;
1732 }
1733 }
1734 return false;
1735}
1736
Evan Chengd38c22b2006-05-11 23:55:42 +00001737/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1738/// it as a def&use operand. Add a pseudo control edge from it to the other
1739/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001740/// first (lower in the schedule). If both nodes are two-address, favor the
1741/// one that has a CopyToReg use (more likely to be a loop induction update).
1742/// If both are two-address, but one is commutable while the other is not
1743/// commutable, favor the one that's not commutable.
Dan Gohman4b49be12008-06-21 01:08:22 +00001744void BURegReductionPriorityQueue::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001745 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001746 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001747 if (!SU->isTwoAddress)
1748 continue;
1749
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001750 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001751 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001752 continue;
1753
Dan Gohman17059682008-07-17 19:10:17 +00001754 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001755 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001756 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001757 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001758 for (unsigned j = 0; j != NumOps; ++j) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001759 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001760 SDNode *DU = SU->getNode()->getOperand(j).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001761 if (DU->getNodeId() == -1)
Evan Cheng1bf166312007-11-09 01:27:11 +00001762 continue;
Dan Gohman46520a22008-06-21 19:18:17 +00001763 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
Evan Chengf24d15f2006-11-06 21:33:46 +00001764 if (!DUSU) continue;
Dan Gohman46520a22008-06-21 19:18:17 +00001765 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1766 E = DUSU->Succs.end(); I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001767 if (I->isCtrl) continue;
1768 SUnit *SuccSU = I->Dep;
Evan Chengf9891412007-12-20 09:25:31 +00001769 if (SuccSU == SU)
Evan Cheng5924bf72007-09-25 01:54:36 +00001770 continue;
Evan Cheng2dbffa42007-11-06 08:44:59 +00001771 // Be conservative. Ignore if nodes aren't at roughly the same
1772 // depth and height.
1773 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1774 continue;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001775 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001776 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001777 // Don't constrain nodes with physical register defs if the
Dan Gohmancf8827a2008-01-29 12:43:50 +00001778 // predecessor can clobber them.
Evan Chengf9891412007-12-20 09:25:31 +00001779 if (SuccSU->hasPhysRegDefs) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001780 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Chengf9891412007-12-20 09:25:31 +00001781 continue;
1782 }
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001783 // Don't constraint extract_subreg / insert_subreg these may be
1784 // coalesced away. We don't them close to their uses.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001785 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001786 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1787 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1788 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +00001789 if ((!canClobber(SuccSU, DUSU) ||
Evan Chenga5e595d2007-09-28 22:32:30 +00001790 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
Evan Cheng5924bf72007-09-25 01:54:36 +00001791 (!SU->isCommutable && SuccSU->isCommutable)) &&
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001792 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001793 DOUT << "Adding an edge from SU # " << SU->NodeNum
1794 << " to SU #" << SuccSU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001795 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001796 }
1797 }
1798 }
1799 }
1800 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001801}
1802
Evan Cheng6730f032007-01-08 23:55:53 +00001803/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1804/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001805void BURegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001806 SethiUllmanNumbers.assign(SUnits->size(), 0);
1807
1808 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001809 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
1810}
1811void BURegReductionFastPriorityQueue::CalculateSethiUllmanNumbers() {
1812 SethiUllmanNumbers.assign(SUnits->size(), 0);
1813
1814 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1815 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001816}
1817
Roman Levenstein30d09512008-03-27 09:44:37 +00001818/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001819/// predecessors of the successors of the SUnit SU. Stop when the provided
1820/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001821static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1822 unsigned Limit) {
1823 unsigned Sum = 0;
1824 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1825 I != E; ++I) {
Dan Gohmane955c482008-08-05 14:45:15 +00001826 const SUnit *SuccSU = I->Dep;
Roman Levensteinbc674502008-03-27 09:14:57 +00001827 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1828 EE = SuccSU->Preds.end(); II != EE; ++II) {
1829 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001830 if (!PredSU->isScheduled)
1831 if (++Sum > Limit)
1832 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001833 }
1834 }
1835 return Sum;
1836}
1837
Evan Chengd38c22b2006-05-11 23:55:42 +00001838
1839// Top down
1840bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001841 unsigned LPriority = SPQ->getNodePriority(left);
1842 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001843 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1844 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001845 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1846 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001847 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1848 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001849
1850 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1851 return false;
1852 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1853 return true;
1854
Evan Chengd38c22b2006-05-11 23:55:42 +00001855 if (LIsFloater)
1856 LBonus -= 2;
1857 if (RIsFloater)
1858 RBonus -= 2;
1859 if (left->NumSuccs == 1)
1860 LBonus += 2;
1861 if (right->NumSuccs == 1)
1862 RBonus += 2;
1863
Evan Cheng73bdf042008-03-01 00:39:47 +00001864 if (LPriority+LBonus != RPriority+RBonus)
1865 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001866
Evan Cheng73bdf042008-03-01 00:39:47 +00001867 if (left->Depth != right->Depth)
1868 return left->Depth < right->Depth;
1869
1870 if (left->NumSuccsLeft != right->NumSuccsLeft)
1871 return left->NumSuccsLeft > right->NumSuccsLeft;
1872
1873 if (left->CycleBound != right->CycleBound)
1874 return left->CycleBound > right->CycleBound;
1875
Roman Levenstein6b371142008-04-29 09:07:59 +00001876 assert(left->NodeQueueId && right->NodeQueueId &&
1877 "NodeQueueId cannot be zero");
1878 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001879}
1880
Evan Cheng6730f032007-01-08 23:55:53 +00001881/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1882/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001883void TDRegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001884 SethiUllmanNumbers.assign(SUnits->size(), 0);
1885
1886 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001887 CalcNodeTDSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001888}
1889
1890//===----------------------------------------------------------------------===//
1891// Public Constructor Functions
1892//===----------------------------------------------------------------------===//
1893
Jim Laskey03593f72006-08-01 18:29:48 +00001894llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1895 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001896 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001897 MachineBasicBlock *BB,
1898 bool Fast) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001899 if (Fast)
Dan Gohman5a390b92008-11-13 21:21:28 +00001900 return new ScheduleDAGRRList(DAG, BB, *TM, true, true,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001901 new BURegReductionFastPriorityQueue());
1902
Dan Gohman5499e892008-11-11 17:50:47 +00001903 const TargetInstrInfo *TII = TM->getInstrInfo();
1904 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001905
Evan Cheng7e4abde2008-07-02 09:23:51 +00001906 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001907
Evan Cheng7e4abde2008-07-02 09:23:51 +00001908 ScheduleDAGRRList *SD =
Dan Gohman5a390b92008-11-13 21:21:28 +00001909 new ScheduleDAGRRList(DAG, BB, *TM, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001910 PQ->setScheduleDAG(SD);
1911 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001912}
1913
Jim Laskey03593f72006-08-01 18:29:48 +00001914llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1915 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001916 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001917 MachineBasicBlock *BB,
1918 bool Fast) {
Dan Gohman5a390b92008-11-13 21:21:28 +00001919 return new ScheduleDAGRRList(DAG, BB, *TM, false, Fast,
Evan Cheng7e4abde2008-07-02 09:23:51 +00001920 new TDRegReductionPriorityQueue());
Evan Chengd38c22b2006-05-11 23:55:42 +00001921}