blob: 98048dfd5d4a90c8e8114c387da446d1d21b4d33 [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman60cb69e2008-11-19 23:18:57 +000019#include "llvm/CodeGen/ScheduleDAGSDNodes.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Dan Gohmana4db3352008-06-21 18:35:25 +000027#include "llvm/ADT/BitVector.h"
28#include "llvm/ADT/PriorityQueue.h"
Evan Chenge6f92252007-09-27 18:46:06 +000029#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000030#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000031#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000032#include "llvm/ADT/STLExtras.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000033#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include "llvm/Support/CommandLine.h"
35using namespace llvm;
36
Dan Gohmanfd227e92008-03-25 17:10:29 +000037STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000038STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039STATISTIC(NumDups, "Number of duplicated nodes");
40STATISTIC(NumCCCopies, "Number of cross class copies");
41
Jim Laskey95eda5b2006-08-01 14:21:23 +000042static RegisterScheduler
43 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
50
Evan Chengd38c22b2006-05-11 23:55:42 +000051namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000052//===----------------------------------------------------------------------===//
53/// ScheduleDAGRRList - The actual register reduction list scheduler
54/// implementation. This supports both top-down and bottom-up scheduling.
55///
Dan Gohman60cb69e2008-11-19 23:18:57 +000056class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000057private:
58 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
59 /// it is top-down.
60 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000061
Evan 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)
Dan Gohman60cb69e2008-11-19 23:18:57 +000080 : ScheduleDAGSDNodes(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
Dan Gohman6e587262008-11-18 21:22:20 +0000306 SU->Cycle = CurCycle;
307 Sequence.push_back(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;
Dan Gohman6e587262008-11-18 21:22:20 +0000342 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000343}
344
Evan Cheng5924bf72007-09-25 01:54:36 +0000345/// CapturePred - This does the opposite of ReleasePred. Since SU is being
346/// unscheduled, incrcease the succ left count of its predecessors. Remove
347/// them from AvailableQueue if necessary.
Roman Levenstein6b371142008-04-29 09:07:59 +0000348void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) {
349 unsigned CycleBound = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000350 for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end();
351 I != E; ++I) {
352 if (I->Dep == SU)
353 continue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000354 CycleBound = std::max(CycleBound,
355 I->Dep->Cycle + PredSU->Latency);
Evan Cheng5924bf72007-09-25 01:54:36 +0000356 }
357
358 if (PredSU->isAvailable) {
359 PredSU->isAvailable = false;
360 if (!PredSU->isPending)
361 AvailableQueue->remove(PredSU);
362 }
363
Roman Levenstein6b371142008-04-29 09:07:59 +0000364 PredSU->CycleBound = CycleBound;
Evan Cheng038dcc52007-09-28 19:24:24 +0000365 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000366}
367
368/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
369/// its predecessor states to reflect the change.
370void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
371 DOUT << "*** Unscheduling [" << SU->Cycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000372 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000373
374 AvailableQueue->UnscheduledNode(SU);
375
376 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
377 I != E; ++I) {
378 CapturePred(I->Dep, SU, I->isCtrl);
379 if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000380 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Evan Cheng5924bf72007-09-25 01:54:36 +0000381 assert(LiveRegDefs[I->Reg] == I->Dep &&
382 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000383 --NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000384 LiveRegDefs[I->Reg] = NULL;
385 LiveRegCycles[I->Reg] = 0;
386 }
387 }
388
389 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
390 I != E; ++I) {
391 if (I->Cost < 0) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000392 if (!LiveRegDefs[I->Reg]) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000393 LiveRegDefs[I->Reg] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000394 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000395 }
396 if (I->Dep->Cycle < LiveRegCycles[I->Reg])
397 LiveRegCycles[I->Reg] = I->Dep->Cycle;
398 }
399 }
400
401 SU->Cycle = 0;
402 SU->isScheduled = false;
403 SU->isAvailable = true;
404 AvailableQueue->push(SU);
405}
406
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000407/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane955c482008-08-05 14:45:15 +0000408bool ScheduleDAGRRList::IsReachable(const SUnit *SU, const SUnit *TargetSU) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000409 // If insertion of the edge SU->TargetSU would create a cycle
410 // then there is a path from TargetSU to SU.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000411 int UpperBound, LowerBound;
412 LowerBound = Node2Index[TargetSU->NodeNum];
413 UpperBound = Node2Index[SU->NodeNum];
414 bool HasLoop = false;
415 // Is Ord(TargetSU) < Ord(SU) ?
416 if (LowerBound < UpperBound) {
417 Visited.reset();
418 // There may be a path from TargetSU to SU. Check for it.
419 DFS(TargetSU, UpperBound, HasLoop);
Evan Chengcfd5f822007-09-27 00:25:29 +0000420 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000421 return HasLoop;
Evan Chengcfd5f822007-09-27 00:25:29 +0000422}
423
Roman Levenstein733a4d62008-03-26 11:23:38 +0000424/// Allocate - assign the topological index to the node n.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000425inline void ScheduleDAGRRList::Allocate(int n, int index) {
426 Node2Index[n] = index;
427 Index2Node[index] = n;
Evan Chengcfd5f822007-09-27 00:25:29 +0000428}
429
Roman Levenstein733a4d62008-03-26 11:23:38 +0000430/// InitDAGTopologicalSorting - create the initial topological
431/// ordering from the DAG to be scheduled.
Evan Cheng2c977312008-07-01 18:05:03 +0000432
433/// The idea of the algorithm is taken from
434/// "Online algorithms for managing the topological order of
435/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
436/// This is the MNR algorithm, which was first introduced by
437/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
438/// "Maintaining a topological order under edge insertions".
439///
440/// Short description of the algorithm:
441///
442/// Topological ordering, ord, of a DAG maps each node to a topological
443/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
444///
445/// This means that if there is a path from the node X to the node Z,
446/// then ord(X) < ord(Z).
447///
448/// This property can be used to check for reachability of nodes:
449/// if Z is reachable from X, then an insertion of the edge Z->X would
450/// create a cycle.
451///
452/// The algorithm first computes a topological ordering for the DAG by
453/// initializing the Index2Node and Node2Index arrays and then tries to keep
454/// the ordering up-to-date after edge insertions by reordering the DAG.
455///
456/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
457/// the nodes reachable from Y, and then shifts them using Shift to lie
458/// immediately after X in Index2Node.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000459void ScheduleDAGRRList::InitDAGTopologicalSorting() {
460 unsigned DAGSize = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000461 std::vector<SUnit*> WorkList;
462 WorkList.reserve(DAGSize);
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000463
464 Index2Node.resize(DAGSize);
465 Node2Index.resize(DAGSize);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000466
Roman Levenstein733a4d62008-03-26 11:23:38 +0000467 // Initialize the data structures.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000468 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
469 SUnit *SU = &SUnits[i];
470 int NodeNum = SU->NodeNum;
471 unsigned Degree = SU->Succs.size();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000472 // Temporarily use the Node2Index array as scratch space for degree counts.
473 Node2Index[NodeNum] = Degree;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000474
475 // Is it a node without dependencies?
476 if (Degree == 0) {
477 assert(SU->Succs.empty() && "SUnit should have no successors");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000478 // Collect leaf nodes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000479 WorkList.push_back(SU);
480 }
481 }
482
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000483 int Id = DAGSize;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000484 while (!WorkList.empty()) {
485 SUnit *SU = WorkList.back();
486 WorkList.pop_back();
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000487 Allocate(SU->NodeNum, --Id);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000488 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
489 I != E; ++I) {
490 SUnit *SU = I->Dep;
Dan Gohman3a3a52d2008-08-27 16:29:48 +0000491 if (!--Node2Index[SU->NodeNum])
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000492 // If all dependencies of the node are processed already,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000493 // then the node can be computed now.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000494 WorkList.push_back(SU);
495 }
496 }
497
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000498 Visited.resize(DAGSize);
499
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000500#ifndef NDEBUG
501 // Check correctness of the ordering
502 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
503 SUnit *SU = &SUnits[i];
504 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
505 I != E; ++I) {
506 assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] &&
507 "Wrong topological sorting");
508 }
509 }
510#endif
511}
512
Roman Levenstein733a4d62008-03-26 11:23:38 +0000513/// AddPred - adds an edge from SUnit X to SUnit Y.
514/// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000515bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial,
516 unsigned PhyReg, int Cost) {
517 int UpperBound, LowerBound;
518 LowerBound = Node2Index[Y->NodeNum];
519 UpperBound = Node2Index[X->NodeNum];
520 bool HasLoop = false;
521 // Is Ord(X) < Ord(Y) ?
522 if (LowerBound < UpperBound) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000523 // Update the topological order.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000524 Visited.reset();
525 DFS(Y, UpperBound, HasLoop);
526 assert(!HasLoop && "Inserted edge creates a loop!");
Roman Levenstein733a4d62008-03-26 11:23:38 +0000527 // Recompute topological indexes.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000528 Shift(Visited, LowerBound, UpperBound);
529 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000530 // Now really insert the edge.
531 return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000532}
533
Roman Levenstein733a4d62008-03-26 11:23:38 +0000534/// RemovePred - This removes the specified node N from the predecessors of
535/// the current node M. Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000536bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N,
537 bool isCtrl, bool isSpecial) {
538 // InitDAGTopologicalSorting();
539 return M->removePred(N, isCtrl, isSpecial);
540}
541
Roman Levenstein733a4d62008-03-26 11:23:38 +0000542/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
543/// all nodes affected by the edge insertion. These nodes will later get new
544/// topological indexes by means of the Shift method.
Dan Gohmane955c482008-08-05 14:45:15 +0000545void ScheduleDAGRRList::DFS(const SUnit *SU, int UpperBound, bool& HasLoop) {
546 std::vector<const SUnit*> WorkList;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000547 WorkList.reserve(SUnits.size());
548
549 WorkList.push_back(SU);
550 while (!WorkList.empty()) {
551 SU = WorkList.back();
552 WorkList.pop_back();
553 Visited.set(SU->NodeNum);
554 for (int I = SU->Succs.size()-1; I >= 0; --I) {
555 int s = SU->Succs[I].Dep->NodeNum;
556 if (Node2Index[s] == UpperBound) {
557 HasLoop = true;
558 return;
559 }
Roman Levenstein733a4d62008-03-26 11:23:38 +0000560 // Visit successors if not already and in affected region.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000561 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
562 WorkList.push_back(SU->Succs[I].Dep);
563 }
564 }
565 }
566}
567
Roman Levenstein733a4d62008-03-26 11:23:38 +0000568/// Shift - Renumber the nodes so that the topological ordering is
569/// preserved.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000570void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound,
571 int UpperBound) {
572 std::vector<int> L;
573 int shift = 0;
574 int i;
575
576 for (i = LowerBound; i <= UpperBound; ++i) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000577 // w is node at topological index i.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000578 int w = Index2Node[i];
579 if (Visited.test(w)) {
Roman Levenstein733a4d62008-03-26 11:23:38 +0000580 // Unmark.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000581 Visited.reset(w);
582 L.push_back(w);
583 shift = shift + 1;
584 } else {
585 Allocate(w, i - shift);
586 }
587 }
588
589 for (unsigned j = 0; j < L.size(); ++j) {
590 Allocate(L[j], i - shift);
591 i = i + 1;
592 }
593}
594
595
Dan Gohmanfd227e92008-03-25 17:10:29 +0000596/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Evan Chengcfd5f822007-09-27 00:25:29 +0000597/// create a cycle.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000598bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
599 if (IsReachable(TargetSU, SU))
Evan Chengcfd5f822007-09-27 00:25:29 +0000600 return true;
601 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
602 I != E; ++I)
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000603 if (I->Cost < 0 && IsReachable(TargetSU, I->Dep))
Evan Chengcfd5f822007-09-27 00:25:29 +0000604 return true;
605 return false;
606}
607
Evan Cheng8e136a92007-09-26 21:36:17 +0000608/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Evan Cheng5924bf72007-09-25 01:54:36 +0000609/// BTCycle in order to schedule a specific node. Returns the last unscheduled
610/// SUnit. Also returns if a successor is unscheduled in the process.
Evan Cheng8e136a92007-09-26 21:36:17 +0000611void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
612 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000613 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000614 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000615 OldSU = Sequence.back();
616 Sequence.pop_back();
617 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000618 // Don't try to remove SU from AvailableQueue.
619 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000620 UnscheduleNodeBottomUp(OldSU);
621 --CurCycle;
622 }
623
624
625 if (SU->isSucc(OldSU)) {
626 assert(false && "Something is wrong!");
627 abort();
628 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000629
630 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000631}
632
Evan Cheng5924bf72007-09-25 01:54:36 +0000633/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
634/// successors to the newly created node.
635SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000636 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000637 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000638
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000639 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000640 if (!N)
641 return NULL;
642
643 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000644 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000645 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000646 MVT VT = N->getValueType(i);
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000647 if (VT == MVT::Flag)
648 return NULL;
649 else if (VT == MVT::Other)
650 TryUnfold = true;
651 }
Evan Cheng79e97132007-10-05 01:39:18 +0000652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000653 const SDValue &Op = N->getOperand(i);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000654 MVT VT = Op.getNode()->getValueType(Op.getResNo());
Evan Cheng79e97132007-10-05 01:39:18 +0000655 if (VT == MVT::Flag)
656 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000657 }
658
659 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000660 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000661 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000662 return NULL;
663
664 DOUT << "Unfolding SU # " << SU->NodeNum << "\n";
665 assert(NewNodes.size() == 2 && "Expected a load folding node!");
666
667 N = NewNodes[1];
668 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000669 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000670 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000671 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000672 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
673 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000674 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000675
Dan Gohmane52e0892008-11-11 21:34:44 +0000676 // LoadNode may already exist. This can happen when there is another
677 // load from the same location and producing the same type of value
678 // but it has different alignment or volatileness.
679 bool isNewLoad = true;
680 SUnit *LoadSU;
681 if (LoadNode->getNodeId() != -1) {
682 LoadSU = &SUnits[LoadNode->getNodeId()];
683 isNewLoad = false;
684 } else {
685 LoadSU = CreateNewSUnit(LoadNode);
686 LoadNode->setNodeId(LoadSU->NodeNum);
687
688 LoadSU->Depth = SU->Depth;
689 LoadSU->Height = SU->Height;
690 ComputeLatency(LoadSU);
691 }
692
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000693 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000694 assert(N->getNodeId() == -1 && "Node already inserted!");
695 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000696
Dan Gohman17059682008-07-17 19:10:17 +0000697 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000698 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000699 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000700 NewSU->isTwoAddress = true;
701 break;
702 }
703 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000704 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000705 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000706 // FIXME: Calculate height / depth and propagate the changes?
Evan Cheng91e0fc92007-12-18 08:42:10 +0000707 NewSU->Depth = SU->Depth;
708 NewSU->Height = SU->Height;
Evan Cheng79e97132007-10-05 01:39:18 +0000709 ComputeLatency(NewSU);
710
711 SUnit *ChainPred = NULL;
712 SmallVector<SDep, 4> ChainSuccs;
713 SmallVector<SDep, 4> LoadPreds;
714 SmallVector<SDep, 4> NodePreds;
715 SmallVector<SDep, 4> NodeSuccs;
716 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
717 I != E; ++I) {
718 if (I->isCtrl)
719 ChainPred = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000720 else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode))
Evan Cheng79e97132007-10-05 01:39:18 +0000721 LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
722 else
723 NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false));
724 }
725 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
726 I != E; ++I) {
727 if (I->isCtrl)
728 ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
729 I->isCtrl, I->isSpecial));
730 else
731 NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost,
732 I->isCtrl, I->isSpecial));
733 }
734
Dan Gohman4370f262008-04-15 01:22:18 +0000735 if (ChainPred) {
736 RemovePred(SU, ChainPred, true, false);
737 if (isNewLoad)
738 AddPred(LoadSU, ChainPred, true, false);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000739 }
Evan Cheng79e97132007-10-05 01:39:18 +0000740 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
741 SDep *Pred = &LoadPreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000742 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
743 if (isNewLoad) {
744 AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000745 Pred->Reg, Pred->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000746 }
Evan Cheng79e97132007-10-05 01:39:18 +0000747 }
748 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
749 SDep *Pred = &NodePreds[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000750 RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial);
751 AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000752 Pred->Reg, Pred->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000753 }
754 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
755 SDep *Succ = &NodeSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000756 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
757 AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000758 Succ->Reg, Succ->Cost);
Evan Cheng79e97132007-10-05 01:39:18 +0000759 }
760 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
761 SDep *Succ = &ChainSuccs[i];
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000762 RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial);
763 if (isNewLoad) {
764 AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial,
Roman Levenstein733a4d62008-03-26 11:23:38 +0000765 Succ->Reg, Succ->Cost);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000766 }
Evan Cheng79e97132007-10-05 01:39:18 +0000767 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000768 if (isNewLoad) {
769 AddPred(NewSU, LoadSU, false, false);
770 }
Evan Cheng79e97132007-10-05 01:39:18 +0000771
Evan Cheng91e0fc92007-12-18 08:42:10 +0000772 if (isNewLoad)
773 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000774 AvailableQueue->addNode(NewSU);
775
776 ++NumUnfolds;
777
778 if (NewSU->NumSuccsLeft == 0) {
779 NewSU->isAvailable = true;
780 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000781 }
782 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000783 }
784
785 DOUT << "Duplicating SU # " << SU->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000786 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000787
788 // New SUnit has the exact same predecessors.
789 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
790 I != E; ++I)
791 if (!I->isSpecial) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000792 AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost);
Evan Cheng5924bf72007-09-25 01:54:36 +0000793 NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1);
794 }
795
796 // Only copy scheduled successors. Cut them from old node's successor
797 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000798 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000799 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
800 I != E; ++I) {
801 if (I->isSpecial)
802 continue;
Evan Cheng5924bf72007-09-25 01:54:36 +0000803 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000804 NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000805 AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000806 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng5924bf72007-09-25 01:54:36 +0000807 }
808 }
809 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000810 SUnit *Succ = DelDeps[i].first;
811 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000812 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng5924bf72007-09-25 01:54:36 +0000813 }
814
815 AvailableQueue->updateNode(SU);
816 AvailableQueue->addNode(NewSU);
817
Evan Cheng1ec79b42007-09-27 07:09:03 +0000818 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000819 return NewSU;
820}
821
Evan Cheng1ec79b42007-09-27 07:09:03 +0000822/// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies
823/// and move all scheduled successors of the given SUnit to the last copy.
824void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
825 const TargetRegisterClass *DestRC,
826 const TargetRegisterClass *SrcRC,
827 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000828 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000829 CopyFromSU->CopySrcRC = SrcRC;
830 CopyFromSU->CopyDstRC = DestRC;
831 CopyFromSU->Depth = SU->Depth;
832 CopyFromSU->Height = SU->Height;
833
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000834 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000835 CopyToSU->CopySrcRC = DestRC;
836 CopyToSU->CopyDstRC = SrcRC;
837
838 // Only copy scheduled successors. Cut them from old node's successor
839 // list and move them over.
Evan Chengbde499b2007-09-27 07:29:27 +0000840 SmallVector<std::pair<SUnit*, bool>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000841 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
842 I != E; ++I) {
843 if (I->isSpecial)
844 continue;
Evan Cheng8e136a92007-09-26 21:36:17 +0000845 if (I->Dep->isScheduled) {
Evan Chengbde499b2007-09-27 07:29:27 +0000846 CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000847 AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost);
Evan Chengbde499b2007-09-27 07:29:27 +0000848 DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl));
Evan Cheng8e136a92007-09-26 21:36:17 +0000849 }
850 }
851 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Evan Chengbde499b2007-09-27 07:29:27 +0000852 SUnit *Succ = DelDeps[i].first;
853 bool isCtrl = DelDeps[i].second;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000854 RemovePred(Succ, SU, isCtrl, false);
Evan Cheng8e136a92007-09-26 21:36:17 +0000855 }
856
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000857 AddPred(CopyFromSU, SU, false, false, Reg, -1);
858 AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1);
Evan Cheng8e136a92007-09-26 21:36:17 +0000859
860 AvailableQueue->updateNode(SU);
861 AvailableQueue->addNode(CopyFromSU);
862 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000863 Copies.push_back(CopyFromSU);
864 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000865
Evan Cheng1ec79b42007-09-27 07:09:03 +0000866 ++NumCCCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000867}
868
869/// getPhysicalRegisterVT - Returns the ValueType of the physical register
870/// definition of the specified node.
871/// FIXME: Move to SelectionDAG?
Duncan Sands13237ac2008-06-06 12:08:01 +0000872static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
873 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000874 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000875 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000876 unsigned NumRes = TID.getNumDefs();
877 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000878 if (Reg == *ImpDef)
879 break;
880 ++NumRes;
881 }
882 return N->getValueType(NumRes);
883}
884
Evan Cheng5924bf72007-09-25 01:54:36 +0000885/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
886/// scheduling of the given node to satisfy live physical register dependencies.
887/// If the specific node is the last one that's available to schedule, do
888/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000889bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
890 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000891 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000892 return false;
893
Evan Chenge6f92252007-09-27 18:46:06 +0000894 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000895 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000896 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
897 I != E; ++I) {
898 if (I->Cost < 0) {
899 unsigned Reg = I->Reg;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000900 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000901 if (RegAdded.insert(Reg))
902 LRegs.push_back(Reg);
903 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000904 for (const unsigned *Alias = TRI->getAliasSet(Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000905 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000906 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) {
Evan Chenge6f92252007-09-27 18:46:06 +0000907 if (RegAdded.insert(*Alias))
908 LRegs.push_back(*Alias);
909 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000910 }
911 }
912
Dan Gohman072734e2008-11-13 23:24:17 +0000913 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
914 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000915 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000916 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000917 if (!TID.ImplicitDefs)
918 continue;
919 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000920 if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000921 if (RegAdded.insert(*Reg))
922 LRegs.push_back(*Reg);
923 }
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000924 for (const unsigned *Alias = TRI->getAliasSet(*Reg);
Evan Cheng5924bf72007-09-25 01:54:36 +0000925 *Alias; ++Alias)
Dan Gohmanc07f6862008-09-23 18:50:48 +0000926 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Evan Chenge6f92252007-09-27 18:46:06 +0000927 if (RegAdded.insert(*Alias))
928 LRegs.push_back(*Alias);
929 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000930 }
931 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000932 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000933}
934
Evan Cheng1ec79b42007-09-27 07:09:03 +0000935
Evan Chengd38c22b2006-05-11 23:55:42 +0000936/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
937/// schedulers.
938void ScheduleDAGRRList::ListScheduleBottomUp() {
939 unsigned CurCycle = 0;
940 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000941 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000942 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000943 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
944 RootSU->isAvailable = true;
945 AvailableQueue->push(RootSU);
946 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000947
948 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000949 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000950 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000951 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000952 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000953 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000954 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000955 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000956 SUnit *CurSU = AvailableQueue->pop();
957 while (CurSU) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000958 if (CurSU->CycleBound <= CurCycle) {
959 SmallVector<unsigned, 4> LRegs;
960 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
Evan Cheng5924bf72007-09-25 01:54:36 +0000961 break;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000962 Delayed = true;
963 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng5924bf72007-09-25 01:54:36 +0000964 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000965
966 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
967 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000968 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000969 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000970
971 // All candidates are delayed due to live physical reg dependencies.
972 // Try backtracking, code duplication, or inserting cross class copies
973 // to resolve it.
974 if (Delayed && !CurSU) {
975 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
976 SUnit *TrySU = NotReady[i];
977 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
978
979 // Try unscheduling up to the point where it's safe to schedule
980 // this node.
981 unsigned LiveCycle = CurCycle;
982 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
983 unsigned Reg = LRegs[j];
984 unsigned LCycle = LiveRegCycles[Reg];
985 LiveCycle = std::min(LiveCycle, LCycle);
986 }
987 SUnit *OldSU = Sequence[LiveCycle];
988 if (!WillCreateCycle(TrySU, OldSU)) {
989 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
990 // Force the current node to be scheduled before the node that
991 // requires the physical reg dep.
992 if (OldSU->isAvailable) {
993 OldSU->isAvailable = false;
994 AvailableQueue->remove(OldSU);
995 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000996 AddPred(TrySU, OldSU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000997 // If one or more successors has been unscheduled, then the current
998 // node is no longer avaialable. Schedule a successor that's now
999 // available instead.
1000 if (!TrySU->isAvailable)
1001 CurSU = AvailableQueue->pop();
1002 else {
1003 CurSU = TrySU;
1004 TrySU->isPending = false;
1005 NotReady.erase(NotReady.begin()+i);
1006 }
1007 break;
1008 }
1009 }
1010
1011 if (!CurSU) {
Dan Gohmanfd227e92008-03-25 17:10:29 +00001012 // Can't backtrack. Try duplicating the nodes that produces these
Evan Cheng1ec79b42007-09-27 07:09:03 +00001013 // "expensive to copy" values to break the dependency. In case even
1014 // that doesn't work, insert cross class copies.
1015 SUnit *TrySU = NotReady[0];
1016 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1017 assert(LRegs.size() == 1 && "Can't handle this yet!");
1018 unsigned Reg = LRegs[0];
1019 SUnit *LRDef = LiveRegDefs[Reg];
Evan Cheng79e97132007-10-05 01:39:18 +00001020 SUnit *NewDef = CopyAndMoveSuccessors(LRDef);
1021 if (!NewDef) {
Evan Cheng1ec79b42007-09-27 07:09:03 +00001022 // Issue expensive cross register class copies.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001023 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001024 const TargetRegisterClass *RC =
Evan Chenge88a6252008-03-11 07:19:34 +00001025 TRI->getPhysicalRegisterRegClass(Reg, VT);
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001026 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001027 if (!DestRC) {
1028 assert(false && "Don't know how to copy this physical register!");
1029 abort();
1030 }
1031 SmallVector<SUnit*, 2> Copies;
1032 InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1033 DOUT << "Adding an edge from SU # " << TrySU->NodeNum
1034 << " to SU #" << Copies.front()->NodeNum << "\n";
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001035 AddPred(TrySU, Copies.front(), true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001036 NewDef = Copies.back();
1037 }
1038
1039 DOUT << "Adding an edge from SU # " << NewDef->NodeNum
1040 << " to SU #" << TrySU->NodeNum << "\n";
1041 LiveRegDefs[Reg] = NewDef;
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001042 AddPred(NewDef, TrySU, true, true);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001043 TrySU->isAvailable = false;
1044 CurSU = NewDef;
1045 }
1046
1047 if (!CurSU) {
1048 assert(false && "Unable to resolve live physical register dependencies!");
1049 abort();
1050 }
1051 }
1052
Evan Chengd38c22b2006-05-11 23:55:42 +00001053 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +00001054 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
1055 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +00001056 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +00001057 if (NotReady[i]->isAvailable)
1058 AvailableQueue->push(NotReady[i]);
1059 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001060 NotReady.clear();
1061
Evan Cheng5924bf72007-09-25 01:54:36 +00001062 if (!CurSU)
1063 Sequence.push_back(0);
Dan Gohman6e587262008-11-18 21:22:20 +00001064 else
Evan Cheng5924bf72007-09-25 01:54:36 +00001065 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +00001066 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001067 }
1068
Evan Chengd38c22b2006-05-11 23:55:42 +00001069 // Reverse the order if it is bottom up.
1070 std::reverse(Sequence.begin(), Sequence.end());
1071
Evan Chengd38c22b2006-05-11 23:55:42 +00001072#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +00001073 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +00001074#endif
1075}
1076
1077//===----------------------------------------------------------------------===//
1078// Top-Down Scheduling
1079//===----------------------------------------------------------------------===//
1080
1081/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001082/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman5ebdb982008-11-18 00:38:59 +00001083void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +00001084 --SuccSU->NumPredsLeft;
Evan Chengd38c22b2006-05-11 23:55:42 +00001085
1086#ifndef NDEBUG
Evan Cheng038dcc52007-09-28 19:24:24 +00001087 if (SuccSU->NumPredsLeft < 0) {
Dan Gohman5ebdb982008-11-18 00:38:59 +00001088 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001089 SuccSU->dump(this);
Bill Wendling22e978a2006-12-07 20:04:42 +00001090 cerr << " has been released too many times!\n";
Evan Chengd38c22b2006-05-11 23:55:42 +00001091 assert(0);
1092 }
1093#endif
1094
Dan Gohman5ebdb982008-11-18 00:38:59 +00001095 // Compute how many cycles it will be before this actually becomes
1096 // available. This is the max of the start time of all predecessors plus
1097 // their latencies.
1098 // If this is a token edge, we don't need to wait for the latency of the
1099 // preceeding instruction (e.g. a long-latency load) unless there is also
1100 // some other data dependence.
1101 unsigned PredDoneCycle = SU->Cycle;
1102 if (!isChain)
1103 PredDoneCycle += SU->Latency;
1104 else if (SU->Latency)
1105 PredDoneCycle += 1;
1106 SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle);
1107
Evan Cheng038dcc52007-09-28 19:24:24 +00001108 if (SuccSU->NumPredsLeft == 0) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001109 SuccSU->isAvailable = true;
1110 AvailableQueue->push(SuccSU);
1111 }
1112}
1113
Evan Chengd38c22b2006-05-11 23:55:42 +00001114/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1115/// count of its successors. If a successor pending count is zero, add it to
1116/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +00001117void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001118 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +00001119 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +00001120
Dan Gohman92a36d72008-11-17 21:31:02 +00001121 SU->Cycle = CurCycle;
1122 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001123
1124 // Top down: release successors
Chris Lattnerd86418a2006-08-17 00:09:56 +00001125 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1126 I != E; ++I)
Dan Gohman5ebdb982008-11-18 00:38:59 +00001127 ReleaseSucc(SU, I->Dep, I->isCtrl);
Dan Gohman92a36d72008-11-17 21:31:02 +00001128
Evan Chengd38c22b2006-05-11 23:55:42 +00001129 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +00001130 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001131}
1132
Dan Gohman54a187e2007-08-20 19:28:38 +00001133/// ListScheduleTopDown - The main loop of list scheduling for top-down
1134/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001135void ScheduleDAGRRList::ListScheduleTopDown() {
1136 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001137
1138 // All leaves to Available queue.
1139 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1140 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001141 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001142 AvailableQueue->push(&SUnits[i]);
1143 SUnits[i].isAvailable = true;
1144 }
1145 }
1146
Evan Chengd38c22b2006-05-11 23:55:42 +00001147 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001148 // priority. If it is not ready put it back. Schedule the node.
Evan Chengd38c22b2006-05-11 23:55:42 +00001149 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +00001150 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001151 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001152 SUnit *CurSU = AvailableQueue->pop();
1153 while (CurSU && CurSU->CycleBound > CurCycle) {
1154 NotReady.push_back(CurSU);
1155 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +00001156 }
1157
1158 // Add the nodes that aren't ready back onto the available list.
1159 AvailableQueue->push_all(NotReady);
1160 NotReady.clear();
1161
Evan Cheng5924bf72007-09-25 01:54:36 +00001162 if (!CurSU)
1163 Sequence.push_back(0);
Dan Gohman6e587262008-11-18 21:22:20 +00001164 else
Evan Cheng5924bf72007-09-25 01:54:36 +00001165 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +00001166 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +00001167 }
1168
Evan Chengd38c22b2006-05-11 23:55:42 +00001169#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +00001170 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +00001171#endif
1172}
1173
1174
Evan Chengd38c22b2006-05-11 23:55:42 +00001175//===----------------------------------------------------------------------===//
1176// RegReductionPriorityQueue Implementation
1177//===----------------------------------------------------------------------===//
1178//
1179// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1180// to reduce register pressure.
1181//
1182namespace {
1183 template<class SF>
1184 class RegReductionPriorityQueue;
1185
1186 /// Sorting functions for the Available queue.
1187 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1188 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1189 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1190 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1191
1192 bool operator()(const SUnit* left, const SUnit* right) const;
1193 };
1194
Evan Cheng7e4abde2008-07-02 09:23:51 +00001195 struct bu_ls_rr_fast_sort : public std::binary_function<SUnit*, SUnit*, bool>{
1196 RegReductionPriorityQueue<bu_ls_rr_fast_sort> *SPQ;
1197 bu_ls_rr_fast_sort(RegReductionPriorityQueue<bu_ls_rr_fast_sort> *spq)
1198 : SPQ(spq) {}
1199 bu_ls_rr_fast_sort(const bu_ls_rr_fast_sort &RHS) : SPQ(RHS.SPQ) {}
1200
1201 bool operator()(const SUnit* left, const SUnit* right) const;
1202 };
1203
Evan Chengd38c22b2006-05-11 23:55:42 +00001204 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1205 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1206 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1207 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
1208
1209 bool operator()(const SUnit* left, const SUnit* right) const;
1210 };
1211} // end anonymous namespace
1212
Evan Cheng961bbd32007-01-08 23:50:38 +00001213static inline bool isCopyFromLiveIn(const SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001214 SDNode *N = SU->getNode();
Evan Cheng8e136a92007-09-26 21:36:17 +00001215 return N && N->getOpcode() == ISD::CopyFromReg &&
Evan Cheng961bbd32007-01-08 23:50:38 +00001216 N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
1217}
1218
Evan Cheng7e4abde2008-07-02 09:23:51 +00001219/// CalcNodeBUSethiUllmanNumber - Compute Sethi Ullman number for bottom up
1220/// scheduling. Smaller number is the higher priority.
1221static unsigned
1222CalcNodeBUSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1223 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1224 if (SethiUllmanNumber != 0)
1225 return SethiUllmanNumber;
1226
1227 unsigned Extra = 0;
1228 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1229 I != E; ++I) {
1230 if (I->isCtrl) continue; // ignore chain preds
1231 SUnit *PredSU = I->Dep;
1232 unsigned PredSethiUllman = CalcNodeBUSethiUllmanNumber(PredSU, SUNumbers);
1233 if (PredSethiUllman > SethiUllmanNumber) {
1234 SethiUllmanNumber = PredSethiUllman;
1235 Extra = 0;
1236 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1237 ++Extra;
1238 }
1239
1240 SethiUllmanNumber += Extra;
1241
1242 if (SethiUllmanNumber == 0)
1243 SethiUllmanNumber = 1;
1244
1245 return SethiUllmanNumber;
1246}
1247
1248/// CalcNodeTDSethiUllmanNumber - Compute Sethi Ullman number for top down
1249/// scheduling. Smaller number is the higher priority.
1250static unsigned
1251CalcNodeTDSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
1252 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1253 if (SethiUllmanNumber != 0)
1254 return SethiUllmanNumber;
1255
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001256 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001257 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1258 SethiUllmanNumber = 0xffff;
1259 else if (SU->NumSuccsLeft == 0)
1260 // If SU does not have a use, i.e. it doesn't produce a value that would
1261 // be consumed (e.g. store), then it terminates a chain of computation.
1262 // Give it a small SethiUllman number so it will be scheduled right before
1263 // its predecessors that it doesn't lengthen their live ranges.
1264 SethiUllmanNumber = 0;
1265 else if (SU->NumPredsLeft == 0 &&
1266 (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
1267 SethiUllmanNumber = 0xffff;
1268 else {
1269 int Extra = 0;
1270 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1271 I != E; ++I) {
1272 if (I->isCtrl) continue; // ignore chain preds
1273 SUnit *PredSU = I->Dep;
1274 unsigned PredSethiUllman = CalcNodeTDSethiUllmanNumber(PredSU, SUNumbers);
1275 if (PredSethiUllman > SethiUllmanNumber) {
1276 SethiUllmanNumber = PredSethiUllman;
1277 Extra = 0;
1278 } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl)
1279 ++Extra;
1280 }
1281
1282 SethiUllmanNumber += Extra;
1283 }
1284
1285 return SethiUllmanNumber;
1286}
1287
1288
Evan Chengd38c22b2006-05-11 23:55:42 +00001289namespace {
1290 template<class SF>
Chris Lattner996795b2006-06-28 23:17:24 +00001291 class VISIBILITY_HIDDEN RegReductionPriorityQueue
1292 : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +00001293 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +00001294 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001295
Dan Gohman3f656df2008-11-20 02:45:51 +00001296 protected:
1297 // SUnits - The SUnits for the current graph.
1298 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +00001299
Dan Gohman3f656df2008-11-20 02:45:51 +00001300 const TargetInstrInfo *TII;
1301 const TargetRegisterInfo *TRI;
1302 ScheduleDAGRRList *scheduleDAG;
1303
1304 public:
1305 RegReductionPriorityQueue(const TargetInstrInfo *tii,
1306 const TargetRegisterInfo *tri) :
1307 Queue(SF(this)), currentQueueId(0),
1308 TII(tii), TRI(tri), scheduleDAG(NULL) {}
1309
1310 void initNodes(std::vector<SUnit> &sunits) {
1311 SUnits = &sunits;
1312 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001313
Dan Gohman50c76be2008-10-31 19:06:33 +00001314 virtual void addNode(const SUnit *SU) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001315
Dan Gohman50c76be2008-10-31 19:06:33 +00001316 virtual void updateNode(const SUnit *SU) = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +00001317
Dan Gohman3f656df2008-11-20 02:45:51 +00001318 virtual void releaseState() {
1319 SUnits = 0;
1320 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001321
Dan Gohman50c76be2008-10-31 19:06:33 +00001322 virtual unsigned getNodePriority(const SUnit *SU) const = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001323
Evan Cheng5924bf72007-09-25 01:54:36 +00001324 unsigned size() const { return Queue.size(); }
1325
Evan Chengd38c22b2006-05-11 23:55:42 +00001326 bool empty() const { return Queue.empty(); }
1327
1328 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001329 assert(!U->NodeQueueId && "Node in the queue already");
1330 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001331 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001332 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001333
Evan Chengd38c22b2006-05-11 23:55:42 +00001334 void push_all(const std::vector<SUnit *> &Nodes) {
1335 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001336 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001337 }
1338
1339 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001340 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001341 SUnit *V = Queue.top();
1342 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001343 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001344 return V;
1345 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001346
Evan Cheng5924bf72007-09-25 01:54:36 +00001347 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001348 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001349 assert(SU->NodeQueueId != 0 && "Not in queue!");
1350 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001351 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001352 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001353
1354 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1355 scheduleDAG = scheduleDag;
1356 }
1357
1358 protected:
1359 bool canClobber(const SUnit *SU, const SUnit *Op);
1360 void AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001361 };
1362
Chris Lattner996795b2006-06-28 23:17:24 +00001363 class VISIBILITY_HIDDEN BURegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001364 : public RegReductionPriorityQueue<bu_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001365 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001366 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001367
1368 public:
Dan Gohman3f656df2008-11-20 02:45:51 +00001369 BURegReductionPriorityQueue(const TargetInstrInfo *tii,
1370 const TargetRegisterInfo *tri)
1371 : RegReductionPriorityQueue<bu_ls_rr_sort>(tii, tri) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001372
Dan Gohman46520a22008-06-21 19:18:17 +00001373 void initNodes(std::vector<SUnit> &sunits) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001374 RegReductionPriorityQueue<bu_ls_rr_sort>::initNodes(sunits);
Evan Chengd38c22b2006-05-11 23:55:42 +00001375 // Add pseudo dependency edges for two-address nodes.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001376 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001377 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001378 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001379 }
1380
Evan Cheng5924bf72007-09-25 01:54:36 +00001381 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001382 unsigned SUSize = SethiUllmanNumbers.size();
1383 if (SUnits->size() > SUSize)
1384 SethiUllmanNumbers.resize(SUSize*2, 0);
1385 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001386 }
1387
1388 void updateNode(const SUnit *SU) {
1389 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001390 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001391 }
1392
Evan Chengd38c22b2006-05-11 23:55:42 +00001393 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001394 RegReductionPriorityQueue<bu_ls_rr_sort>::releaseState();
Evan Chengd38c22b2006-05-11 23:55:42 +00001395 SethiUllmanNumbers.clear();
1396 }
1397
Evan Cheng6730f032007-01-08 23:55:53 +00001398 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001399 assert(SU->NodeNum < SethiUllmanNumbers.size());
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001400 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001401 if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU))
1402 // CopyFromReg should be close to its def because it restricts
1403 // allocation choices. But if it is a livein then perhaps we want it
1404 // closer to its uses so it can be coalesced.
1405 return 0xffff;
1406 else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1407 // CopyToReg should be close to its uses to facilitate coalescing and
1408 // avoid spilling.
1409 return 0;
Evan Chengaa2d6ef2007-10-12 08:50:34 +00001410 else if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
1411 Opc == TargetInstrInfo::INSERT_SUBREG)
1412 // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
1413 // facilitate coalescing.
1414 return 0;
Evan Cheng961bbd32007-01-08 23:50:38 +00001415 else if (SU->NumSuccs == 0)
1416 // If SU does not have a use, i.e. it doesn't produce a value that would
1417 // be consumed (e.g. store), then it terminates a chain of computation.
1418 // Give it a large SethiUllman number so it will be scheduled right
1419 // before its predecessors that it doesn't lengthen their live ranges.
1420 return 0xffff;
1421 else if (SU->NumPreds == 0)
1422 // If SU does not have a def, schedule it close to its uses because it
1423 // does not lengthen any live ranges.
1424 return 0;
1425 else
1426 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001427 }
1428
1429 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001430 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001431 };
1432
1433
1434 class VISIBILITY_HIDDEN BURegReductionFastPriorityQueue
1435 : public RegReductionPriorityQueue<bu_ls_rr_fast_sort> {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001436 // SethiUllmanNumbers - The SethiUllman number for each node.
1437 std::vector<unsigned> SethiUllmanNumbers;
Dan Gohman3f656df2008-11-20 02:45:51 +00001438
Evan Cheng7e4abde2008-07-02 09:23:51 +00001439 public:
Dan Gohman3f656df2008-11-20 02:45:51 +00001440 BURegReductionFastPriorityQueue(const TargetInstrInfo *tii,
1441 const TargetRegisterInfo *tri)
1442 : RegReductionPriorityQueue<bu_ls_rr_fast_sort>(tii, tri) {}
Evan Cheng7e4abde2008-07-02 09:23:51 +00001443
1444 void initNodes(std::vector<SUnit> &sunits) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001445 RegReductionPriorityQueue<bu_ls_rr_fast_sort>::initNodes(sunits);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001446 // Calculate node priorities.
1447 CalculateSethiUllmanNumbers();
1448 }
1449
1450 void addNode(const SUnit *SU) {
1451 unsigned SUSize = SethiUllmanNumbers.size();
1452 if (SUnits->size() > SUSize)
1453 SethiUllmanNumbers.resize(SUSize*2, 0);
1454 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1455 }
1456
1457 void updateNode(const SUnit *SU) {
1458 SethiUllmanNumbers[SU->NodeNum] = 0;
1459 CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers);
1460 }
1461
1462 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001463 RegReductionPriorityQueue<bu_ls_rr_fast_sort>::releaseState();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001464 SethiUllmanNumbers.clear();
1465 }
1466
1467 unsigned getNodePriority(const SUnit *SU) const {
1468 return SethiUllmanNumbers[SU->NodeNum];
1469 }
1470
1471 private:
1472 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001473 };
1474
1475
Dan Gohman54a187e2007-08-20 19:28:38 +00001476 class VISIBILITY_HIDDEN TDRegReductionPriorityQueue
Dan Gohman4b49be12008-06-21 01:08:22 +00001477 : public RegReductionPriorityQueue<td_ls_rr_sort> {
Evan Chengd38c22b2006-05-11 23:55:42 +00001478 // SethiUllmanNumbers - The SethiUllman number for each node.
Evan Cheng961bbd32007-01-08 23:50:38 +00001479 std::vector<unsigned> SethiUllmanNumbers;
Evan Chengd38c22b2006-05-11 23:55:42 +00001480
1481 public:
Dan Gohman3f656df2008-11-20 02:45:51 +00001482 TDRegReductionPriorityQueue(const TargetInstrInfo *tii,
1483 const TargetRegisterInfo *tri)
1484 : RegReductionPriorityQueue<td_ls_rr_sort>(tii, tri) {}
Evan Chengd38c22b2006-05-11 23:55:42 +00001485
Dan Gohman46520a22008-06-21 19:18:17 +00001486 void initNodes(std::vector<SUnit> &sunits) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001487 RegReductionPriorityQueue<td_ls_rr_sort>::initNodes(sunits);
1488 // Add pseudo dependency edges for two-address nodes.
1489 AddPseudoTwoAddrDeps();
Evan Chengd38c22b2006-05-11 23:55:42 +00001490 // Calculate node priorities.
Evan Cheng6730f032007-01-08 23:55:53 +00001491 CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001492 }
1493
Evan Cheng5924bf72007-09-25 01:54:36 +00001494 void addNode(const SUnit *SU) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001495 unsigned SUSize = SethiUllmanNumbers.size();
1496 if (SUnits->size() > SUSize)
1497 SethiUllmanNumbers.resize(SUSize*2, 0);
1498 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001499 }
1500
1501 void updateNode(const SUnit *SU) {
1502 SethiUllmanNumbers[SU->NodeNum] = 0;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001503 CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers);
Evan Cheng5924bf72007-09-25 01:54:36 +00001504 }
1505
Evan Chengd38c22b2006-05-11 23:55:42 +00001506 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001507 RegReductionPriorityQueue<td_ls_rr_sort>::releaseState();
Evan Chengd38c22b2006-05-11 23:55:42 +00001508 SethiUllmanNumbers.clear();
1509 }
1510
Evan Cheng6730f032007-01-08 23:55:53 +00001511 unsigned getNodePriority(const SUnit *SU) const {
Evan Cheng961bbd32007-01-08 23:50:38 +00001512 assert(SU->NodeNum < SethiUllmanNumbers.size());
1513 return SethiUllmanNumbers[SU->NodeNum];
Evan Chengd38c22b2006-05-11 23:55:42 +00001514 }
1515
1516 private:
Evan Cheng6730f032007-01-08 23:55:53 +00001517 void CalculateSethiUllmanNumbers();
Evan Chengd38c22b2006-05-11 23:55:42 +00001518 };
1519}
1520
Evan Chengb9e3db62007-03-14 22:43:40 +00001521/// closestSucc - Returns the scheduled cycle of the successor which is
1522/// closet to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001523static unsigned closestSucc(const SUnit *SU) {
1524 unsigned MaxCycle = 0;
1525 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001526 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +00001527 unsigned Cycle = I->Dep->Cycle;
Evan Chengb9e3db62007-03-14 22:43:40 +00001528 // If there are bunch of CopyToRegs stacked up, they should be considered
1529 // to be at the same position.
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001530 if (I->Dep->getNode() && I->Dep->getNode()->getOpcode() == ISD::CopyToReg)
Evan Cheng0effc3a2007-09-19 01:38:40 +00001531 Cycle = closestSucc(I->Dep)+1;
Evan Chengb9e3db62007-03-14 22:43:40 +00001532 if (Cycle > MaxCycle)
1533 MaxCycle = Cycle;
1534 }
Evan Cheng28748552007-03-13 23:25:11 +00001535 return MaxCycle;
1536}
1537
Evan Cheng61bc51e2007-12-20 02:22:36 +00001538/// calcMaxScratches - Returns an cost estimate of the worse case requirement
1539/// for scratch registers. Live-in operands and live-out results don't count
1540/// since they are "fixed".
1541static unsigned calcMaxScratches(const SUnit *SU) {
1542 unsigned Scratches = 0;
1543 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1544 I != E; ++I) {
1545 if (I->isCtrl) continue; // ignore chain preds
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001546 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyFromReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001547 Scratches++;
1548 }
1549 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1550 I != E; ++I) {
1551 if (I->isCtrl) continue; // ignore chain succs
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001552 if (!I->Dep->getNode() || I->Dep->getNode()->getOpcode() != ISD::CopyToReg)
Evan Cheng61bc51e2007-12-20 02:22:36 +00001553 Scratches += 10;
1554 }
1555 return Scratches;
1556}
1557
Evan Chengd38c22b2006-05-11 23:55:42 +00001558// Bottom up
1559bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001560 unsigned LPriority = SPQ->getNodePriority(left);
1561 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001562 if (LPriority != RPriority)
1563 return LPriority > RPriority;
1564
1565 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1566 // e.g.
1567 // t1 = op t2, c1
1568 // t3 = op t4, c2
1569 //
1570 // and the following instructions are both ready.
1571 // t2 = op c3
1572 // t4 = op c4
1573 //
1574 // Then schedule t2 = op first.
1575 // i.e.
1576 // t4 = op c4
1577 // t2 = op c3
1578 // t1 = op t2, c1
1579 // t3 = op t4, c2
1580 //
1581 // This creates more short live intervals.
1582 unsigned LDist = closestSucc(left);
1583 unsigned RDist = closestSucc(right);
1584 if (LDist != RDist)
1585 return LDist < RDist;
1586
1587 // Intuitively, it's good to push down instructions whose results are
1588 // liveout so their long live ranges won't conflict with other values
1589 // which are needed inside the BB. Further prioritize liveout instructions
1590 // by the number of operands which are calculated within the BB.
1591 unsigned LScratch = calcMaxScratches(left);
1592 unsigned RScratch = calcMaxScratches(right);
1593 if (LScratch != RScratch)
1594 return LScratch > RScratch;
1595
1596 if (left->Height != right->Height)
1597 return left->Height > right->Height;
1598
1599 if (left->Depth != right->Depth)
1600 return left->Depth < right->Depth;
1601
1602 if (left->CycleBound != right->CycleBound)
1603 return left->CycleBound > right->CycleBound;
1604
Roman Levenstein6b371142008-04-29 09:07:59 +00001605 assert(left->NodeQueueId && right->NodeQueueId &&
1606 "NodeQueueId cannot be zero");
1607 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001608}
1609
Dan Gohman4b49be12008-06-21 01:08:22 +00001610bool
Evan Cheng7e4abde2008-07-02 09:23:51 +00001611bu_ls_rr_fast_sort::operator()(const SUnit *left, const SUnit *right) const {
1612 unsigned LPriority = SPQ->getNodePriority(left);
1613 unsigned RPriority = SPQ->getNodePriority(right);
1614 if (LPriority != RPriority)
1615 return LPriority > RPriority;
1616 assert(left->NodeQueueId && right->NodeQueueId &&
1617 "NodeQueueId cannot be zero");
1618 return (left->NodeQueueId > right->NodeQueueId);
1619}
1620
Dan Gohman3f656df2008-11-20 02:45:51 +00001621template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001622bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001623RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001624 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001625 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001626 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001627 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001628 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001629 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001630 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001631 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001632 if (DU->getNodeId() != -1 &&
1633 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001634 return true;
1635 }
1636 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001637 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001638 return false;
1639}
1640
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001641
Evan Chenga5e595d2007-09-28 22:32:30 +00001642/// hasCopyToRegUse - Return true if SU has a value successor that is a
1643/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001644static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001645 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1646 I != E; ++I) {
1647 if (I->isCtrl) continue;
Dan Gohmane955c482008-08-05 14:45:15 +00001648 const SUnit *SuccSU = I->Dep;
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001649 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001650 return true;
1651 }
1652 return false;
1653}
1654
Evan Chengf9891412007-12-20 09:25:31 +00001655/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001656/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001657static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001658 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001659 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001660 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001661 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1662 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001663 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001664 const unsigned *SUImpDefs =
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001665 TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Evan Chengf9891412007-12-20 09:25:31 +00001666 if (!SUImpDefs)
1667 return false;
1668 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001669 MVT VT = N->getValueType(i);
Evan Chengf9891412007-12-20 09:25:31 +00001670 if (VT == MVT::Flag || VT == MVT::Other)
1671 continue;
Dan Gohman6ab52a82008-09-17 15:25:49 +00001672 if (!N->hasAnyUseOfValue(i))
1673 continue;
Evan Chengf9891412007-12-20 09:25:31 +00001674 unsigned Reg = ImpDefs[i - NumDefs];
1675 for (;*SUImpDefs; ++SUImpDefs) {
1676 unsigned SUReg = *SUImpDefs;
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001677 if (TRI->regsOverlap(Reg, SUReg))
Evan Chengf9891412007-12-20 09:25:31 +00001678 return true;
1679 }
1680 }
1681 return false;
1682}
1683
Evan Chengd38c22b2006-05-11 23:55:42 +00001684/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1685/// it as a def&use operand. Add a pseudo control edge from it to the other
1686/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001687/// first (lower in the schedule). If both nodes are two-address, favor the
1688/// one that has a CopyToReg use (more likely to be a loop induction update).
1689/// If both are two-address, but one is commutable while the other is not
1690/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001691template<class SF>
1692void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001693 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001694 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001695 if (!SU->isTwoAddress)
1696 continue;
1697
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001698 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001699 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001700 continue;
1701
Dan Gohman17059682008-07-17 19:10:17 +00001702 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001703 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001704 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001705 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001706 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001707 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1708 continue;
1709 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1710 if (DU->getNodeId() == -1)
1711 continue;
1712 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1713 if (!DUSU) continue;
1714 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1715 E = DUSU->Succs.end(); I != E; ++I) {
1716 if (I->isCtrl) continue;
1717 SUnit *SuccSU = I->Dep;
1718 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001719 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001720 // Be conservative. Ignore if nodes aren't at roughly the same
1721 // depth and height.
1722 if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1)
1723 continue;
1724 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1725 continue;
1726 // Don't constrain nodes with physical register defs if the
1727 // predecessor can clobber them.
1728 if (SuccSU->hasPhysRegDefs) {
1729 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001730 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001731 }
1732 // Don't constraint extract_subreg / insert_subreg these may be
1733 // coalesced away. We don't them close to their uses.
1734 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1735 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
1736 SuccOpc == TargetInstrInfo::INSERT_SUBREG)
1737 continue;
1738 if ((!canClobber(SuccSU, DUSU) ||
1739 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1740 (!SU->isCommutable && SuccSU->isCommutable)) &&
1741 !scheduleDAG->IsReachable(SuccSU, SU)) {
1742 DOUT << "Adding an edge from SU # " << SU->NodeNum
1743 << " to SU #" << SuccSU->NodeNum << "\n";
1744 scheduleDAG->AddPred(SU, SuccSU, true, true);
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001745 }
1746 }
1747 }
1748 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001749}
1750
Evan Cheng6730f032007-01-08 23:55:53 +00001751/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1752/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001753void BURegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001754 SethiUllmanNumbers.assign(SUnits->size(), 0);
1755
1756 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001757 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
1758}
1759void BURegReductionFastPriorityQueue::CalculateSethiUllmanNumbers() {
1760 SethiUllmanNumbers.assign(SUnits->size(), 0);
1761
1762 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1763 CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001764}
1765
Roman Levenstein30d09512008-03-27 09:44:37 +00001766/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001767/// predecessors of the successors of the SUnit SU. Stop when the provided
1768/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001769static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1770 unsigned Limit) {
1771 unsigned Sum = 0;
1772 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1773 I != E; ++I) {
Dan Gohmane955c482008-08-05 14:45:15 +00001774 const SUnit *SuccSU = I->Dep;
Roman Levensteinbc674502008-03-27 09:14:57 +00001775 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1776 EE = SuccSU->Preds.end(); II != EE; ++II) {
1777 SUnit *PredSU = II->Dep;
Evan Cheng16d72072008-03-29 18:34:22 +00001778 if (!PredSU->isScheduled)
1779 if (++Sum > Limit)
1780 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001781 }
1782 }
1783 return Sum;
1784}
1785
Evan Chengd38c22b2006-05-11 23:55:42 +00001786
1787// Top down
1788bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001789 unsigned LPriority = SPQ->getNodePriority(left);
1790 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001791 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1792 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001793 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1794 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001795 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1796 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001797
1798 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1799 return false;
1800 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1801 return true;
1802
Evan Chengd38c22b2006-05-11 23:55:42 +00001803 if (LIsFloater)
1804 LBonus -= 2;
1805 if (RIsFloater)
1806 RBonus -= 2;
1807 if (left->NumSuccs == 1)
1808 LBonus += 2;
1809 if (right->NumSuccs == 1)
1810 RBonus += 2;
1811
Evan Cheng73bdf042008-03-01 00:39:47 +00001812 if (LPriority+LBonus != RPriority+RBonus)
1813 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001814
Evan Cheng73bdf042008-03-01 00:39:47 +00001815 if (left->Depth != right->Depth)
1816 return left->Depth < right->Depth;
1817
1818 if (left->NumSuccsLeft != right->NumSuccsLeft)
1819 return left->NumSuccsLeft > right->NumSuccsLeft;
1820
1821 if (left->CycleBound != right->CycleBound)
1822 return left->CycleBound > right->CycleBound;
1823
Roman Levenstein6b371142008-04-29 09:07:59 +00001824 assert(left->NodeQueueId && right->NodeQueueId &&
1825 "NodeQueueId cannot be zero");
1826 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001827}
1828
Evan Cheng6730f032007-01-08 23:55:53 +00001829/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1830/// scheduling units.
Dan Gohman4b49be12008-06-21 01:08:22 +00001831void TDRegReductionPriorityQueue::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001832 SethiUllmanNumbers.assign(SUnits->size(), 0);
1833
1834 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001835 CalcNodeTDSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001836}
1837
1838//===----------------------------------------------------------------------===//
1839// Public Constructor Functions
1840//===----------------------------------------------------------------------===//
1841
Jim Laskey03593f72006-08-01 18:29:48 +00001842llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
1843 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001844 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001845 MachineBasicBlock *BB,
1846 bool Fast) {
Dan Gohman5499e892008-11-11 17:50:47 +00001847 const TargetInstrInfo *TII = TM->getInstrInfo();
1848 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001849
Dan Gohman3f656df2008-11-20 02:45:51 +00001850 if (Fast)
1851 return new ScheduleDAGRRList(DAG, BB, *TM, true, true,
1852 new BURegReductionFastPriorityQueue(TII, TRI));
1853
Evan Cheng7e4abde2008-07-02 09:23:51 +00001854 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001855
Evan Cheng7e4abde2008-07-02 09:23:51 +00001856 ScheduleDAGRRList *SD =
Dan Gohman5a390b92008-11-13 21:21:28 +00001857 new ScheduleDAGRRList(DAG, BB, *TM, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001858 PQ->setScheduleDAG(SD);
1859 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001860}
1861
Jim Laskey03593f72006-08-01 18:29:48 +00001862llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
1863 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +00001864 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +00001865 MachineBasicBlock *BB,
1866 bool Fast) {
Dan Gohman3f656df2008-11-20 02:45:51 +00001867 const TargetInstrInfo *TII = TM->getInstrInfo();
1868 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
1869
1870 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1871
1872 ScheduleDAGRRList *SD = new ScheduleDAGRRList(DAG, BB, *TM, false, Fast, PQ);
1873 PQ->setScheduleDAG(SD);
1874 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001875}