blob: 8fc6eacbf6604af1701240ebd559d3598497f5b5 [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 Gohman483377c2009-02-06 17:22:58 +000019#include "ScheduleDAGSDNodes.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000020#include "llvm/InlineAsm.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000021#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman619ef482009-01-15 19:20:50 +000022#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000023#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
Evan Chenga77f3d32010-07-21 06:09:07 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000029#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000033#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000035using 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");
Evan Chengb2c42c62009-01-12 03:19:55 +000040STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041
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);
Bill Wendling8cbc25d2010-01-23 10:26:57 +000050static RegisterScheduler
51 sourceListDAGScheduler("source",
52 "Similar to list-burr but schedules in source "
53 "order when possible",
54 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000055
Evan Chengbdd062d2010-05-20 06:13:19 +000056static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000057 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000058 "Bottom-up register pressure aware list scheduling "
59 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000060 createHybridListDAGScheduler);
61
Evan Cheng37b740c2010-07-24 00:39:05 +000062static RegisterScheduler
63 ILPListDAGScheduler("list-ilp",
64 "Bottom-up register pressure aware list scheduling "
65 "which tries to balance ILP and register pressure",
66 createILPListDAGScheduler);
67
Evan Chengd38c22b2006-05-11 23:55:42 +000068namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000069//===----------------------------------------------------------------------===//
70/// ScheduleDAGRRList - The actual register reduction list scheduler
71/// implementation. This supports both top-down and bottom-up scheduling.
72///
Nick Lewycky02d5f772009-10-25 06:33:48 +000073class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000074private:
75 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
76 /// it is top-down.
77 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000078
Evan Chengbdd062d2010-05-20 06:13:19 +000079 /// NeedLatency - True if the scheduler will make use of latency information.
80 ///
81 bool NeedLatency;
82
Evan Chengd38c22b2006-05-11 23:55:42 +000083 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000084 SchedulingPriorityQueue *AvailableQueue;
85
Andrew Trick528fad92010-12-23 05:42:20 +000086 /// CurCycle - The current scheduler state corresponds to this cycle.
87 unsigned CurCycle;
88
Dan Gohmanc07f6862008-09-23 18:50:48 +000089 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000090 /// that are "live". These nodes must be scheduled before any other nodes that
91 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000092 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000093 std::vector<SUnit*> LiveRegDefs;
Andrew Tricka52f3252010-12-23 04:16:14 +000094 std::vector<SUnit*> LiveRegGens;
Evan Cheng5924bf72007-09-25 01:54:36 +000095
Dan Gohmanad2134d2008-11-25 00:52:40 +000096 /// Topo - A topological ordering for SUnits which permits fast IsReachable
97 /// and similar queries.
98 ScheduleDAGTopologicalSort Topo;
99
Evan Chengd38c22b2006-05-11 23:55:42 +0000100public:
Dan Gohman619ef482009-01-15 19:20:50 +0000101 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +0000102 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +0000103 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +0000104 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Andrew Trick528fad92010-12-23 05:42:20 +0000105 AvailableQueue(availqueue), CurCycle(0), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000106 }
107
108 ~ScheduleDAGRRList() {
109 delete AvailableQueue;
110 }
111
112 void Schedule();
113
Roman Levenstein733a4d62008-03-26 11:23:38 +0000114 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000115 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
116 return Topo.IsReachable(SU, TargetSU);
117 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000118
Dan Gohman60d68442009-01-29 19:49:27 +0000119 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000120 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000121 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
122 return Topo.WillCreateCycle(SU, TargetSU);
123 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000124
Dan Gohman2d170892008-12-09 22:54:47 +0000125 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000126 /// This returns true if this is a new predecessor.
127 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000128 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000129 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000130 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000131 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000132
Dan Gohman2d170892008-12-09 22:54:47 +0000133 /// RemovePred - removes a predecessor edge from SUnit SU.
134 /// This returns true if an edge was removed.
135 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000136 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000137 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000138 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000139 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000140
Evan Chengd38c22b2006-05-11 23:55:42 +0000141private:
Dan Gohman60d68442009-01-29 19:49:27 +0000142 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Andrew Tricka52f3252010-12-23 04:16:14 +0000143 void ReleasePredecessors(SUnit *SU);
Dan Gohman60d68442009-01-29 19:49:27 +0000144 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000145 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000146 void CapturePred(SDep *PredEdge);
Andrew Trick528fad92010-12-23 05:42:20 +0000147 void ScheduleNodeBottomUp(SUnit*);
Evan Cheng8e136a92007-09-26 21:36:17 +0000148 void UnscheduleNodeBottomUp(SUnit*);
Andrew Trick528fad92010-12-23 05:42:20 +0000149 void BacktrackBottomUp(SUnit*, unsigned);
Evan Cheng8e136a92007-09-26 21:36:17 +0000150 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000151 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
152 const TargetRegisterClass*,
153 const TargetRegisterClass*,
154 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000155 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Andrew Trick528fad92010-12-23 05:42:20 +0000156 SUnit *PickNodeToScheduleBottomUp();
Evan Chengd38c22b2006-05-11 23:55:42 +0000157 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000158
Andrew Trick528fad92010-12-23 05:42:20 +0000159 void ScheduleNodeTopDown(SUnit*);
160 void ListScheduleTopDown();
161
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000162
163 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000164 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000165 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000166 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000167 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000168 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000169 if (NewNode->NodeNum >= NumSUnits)
170 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 return NewNode;
172 }
173
Roman Levenstein733a4d62008-03-26 11:23:38 +0000174 /// CreateClone - Creates a new SUnit from an existing one.
175 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000176 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000177 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000178 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000179 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000180 if (NewNode->NodeNum >= NumSUnits)
181 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000182 return NewNode;
183 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000184
Evan Chengbdd062d2010-05-20 06:13:19 +0000185 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
186 /// need actual latency information but the hybrid scheduler does.
187 bool ForceUnitLatencies() const {
188 return !NeedLatency;
189 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000190};
191} // end anonymous namespace
192
193
194/// Schedule - Schedule the DAG using list scheduling.
195void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000196 DEBUG(dbgs()
197 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000198 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000199
Andrew Trick528fad92010-12-23 05:42:20 +0000200 CurCycle = 0;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000201 NumLiveRegs = 0;
Andrew Trick2085a962010-12-21 22:25:04 +0000202 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
Andrew Tricka52f3252010-12-23 04:16:14 +0000203 LiveRegGens.resize(TRI->getNumRegs(), NULL);
Evan Cheng5924bf72007-09-25 01:54:36 +0000204
Dan Gohman04543e72008-12-23 18:36:58 +0000205 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000206 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000207
Evan Chengd38c22b2006-05-11 23:55:42 +0000208 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000209 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000210 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000211
Dan Gohman46520a22008-06-21 19:18:17 +0000212 AvailableQueue->initNodes(SUnits);
Andrew Trick2085a962010-12-21 22:25:04 +0000213
Evan Chengd38c22b2006-05-11 23:55:42 +0000214 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
215 if (isBottomUp)
216 ListScheduleBottomUp();
217 else
218 ListScheduleTopDown();
Andrew Trick2085a962010-12-21 22:25:04 +0000219
Evan Chengd38c22b2006-05-11 23:55:42 +0000220 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000221}
Evan Chengd38c22b2006-05-11 23:55:42 +0000222
223//===----------------------------------------------------------------------===//
224// Bottom-Up Scheduling
225//===----------------------------------------------------------------------===//
226
Evan Chengd38c22b2006-05-11 23:55:42 +0000227/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000228/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000229void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000230 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000231
Evan Chengd38c22b2006-05-11 23:55:42 +0000232#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000233 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000234 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000235 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000236 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000237 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000238 }
239#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000240 --PredSU->NumSuccsLeft;
241
Evan Chengbdd062d2010-05-20 06:13:19 +0000242 if (!ForceUnitLatencies()) {
243 // Updating predecessor's height. This is now the cycle when the
244 // predecessor can be scheduled without causing a pipeline stall.
245 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
246 }
247
Dan Gohmanb9543432009-02-10 23:27:53 +0000248 // If all the node's successors are scheduled, this node is ready
249 // to be scheduled. Ignore the special EntrySU node.
250 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000251 PredSU->isAvailable = true;
252 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000253 }
254}
255
Andrew Trick033efdf2010-12-23 03:15:51 +0000256/// Call ReleasePred for each predecessor, then update register live def/gen.
257/// Always update LiveRegDefs for a register dependence even if the current SU
258/// also defines the register. This effectively create one large live range
259/// across a sequence of two-address node. This is important because the
260/// entire chain must be scheduled together. Example:
261///
262/// flags = (3) add
263/// flags = (2) addc flags
264/// flags = (1) addc flags
265///
266/// results in
267///
268/// LiveRegDefs[flags] = 3
Andrew Tricka52f3252010-12-23 04:16:14 +0000269/// LiveRegGens[flags] = 1
Andrew Trick033efdf2010-12-23 03:15:51 +0000270///
271/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
272/// interference on flags.
Andrew Tricka52f3252010-12-23 04:16:14 +0000273void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000274 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000275 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000276 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000277 ReleasePred(SU, &*I);
278 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000279 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000280 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000281 // clobber the register is scheduled between the predecessor and
282 // this node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000283 SUnit *RegDef = LiveRegDefs[I->getReg()]; (void)RegDef;
Andrew Trick033efdf2010-12-23 03:15:51 +0000284 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
285 "interference on register dependence");
Andrew Tricka52f3252010-12-23 04:16:14 +0000286 LiveRegDefs[I->getReg()] = I->getSUnit();
287 if (!LiveRegGens[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000288 ++NumLiveRegs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000289 LiveRegGens[I->getReg()] = SU;
Evan Cheng5924bf72007-09-25 01:54:36 +0000290 }
291 }
292 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000293}
294
295/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
296/// count of its predecessors. If a predecessor pending count is zero, add it to
297/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +0000298void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000299 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000300 DEBUG(SU->dump(this));
301
Evan Chengbdd062d2010-05-20 06:13:19 +0000302#ifndef NDEBUG
303 if (CurCycle < SU->getHeight())
304 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
305#endif
306
307 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000308 SU->setHeightToAtLeast(CurCycle);
309 Sequence.push_back(SU);
310
Evan Cheng28590382010-07-21 23:53:58 +0000311 AvailableQueue->ScheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000312
Andrew Trick033efdf2010-12-23 03:15:51 +0000313 // Update liveness of predecessors before successors to avoid treating a
314 // two-address node as a live range def.
Andrew Tricka52f3252010-12-23 04:16:14 +0000315 ReleasePredecessors(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000316
317 // Release all the implicit physical register defs that are live.
318 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
319 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000320 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
321 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
322 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
323 --NumLiveRegs;
324 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000325 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000326 }
327 }
328
Evan Chengd38c22b2006-05-11 23:55:42 +0000329 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000330}
331
Evan Cheng5924bf72007-09-25 01:54:36 +0000332/// CapturePred - This does the opposite of ReleasePred. Since SU is being
333/// unscheduled, incrcease the succ left count of its predecessors. Remove
334/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000335void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000336 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000337 if (PredSU->isAvailable) {
338 PredSU->isAvailable = false;
339 if (!PredSU->isPending)
340 AvailableQueue->remove(PredSU);
341 }
342
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000343 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000344 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000345}
346
347/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
348/// its predecessor states to reflect the change.
349void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000350 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000351 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000352
Evan Cheng5924bf72007-09-25 01:54:36 +0000353 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
354 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000355 CapturePred(&*I);
Andrew Tricka52f3252010-12-23 04:16:14 +0000356 if (I->isAssignedRegDep() && SU == LiveRegGens[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000357 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000358 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000359 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000360 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000361 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000362 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000363 }
364 }
365
366 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
367 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000368 if (I->isAssignedRegDep()) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000369 // This becomes the nearest def. Note that an earlier def may still be
370 // pending if this is a two-address node.
371 LiveRegDefs[I->getReg()] = SU;
Dan Gohman2d170892008-12-09 22:54:47 +0000372 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000373 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000374 }
Andrew Tricka52f3252010-12-23 04:16:14 +0000375 if (LiveRegGens[I->getReg()] == NULL ||
376 I->getSUnit()->getHeight() < LiveRegGens[I->getReg()]->getHeight())
377 LiveRegGens[I->getReg()] = I->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000378 }
379 }
380
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000381 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000382 SU->isScheduled = false;
383 SU->isAvailable = true;
384 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000385 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000386}
387
Evan Cheng8e136a92007-09-26 21:36:17 +0000388/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000389/// BTCycle in order to schedule a specific node.
Andrew Trick528fad92010-12-23 05:42:20 +0000390void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000391 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000392 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000393 OldSU = Sequence.back();
394 Sequence.pop_back();
395 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000396 // Don't try to remove SU from AvailableQueue.
397 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000398 UnscheduleNodeBottomUp(OldSU);
399 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000400 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000401 }
402
Dan Gohman60d68442009-01-29 19:49:27 +0000403 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000404
405 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000406}
407
Evan Cheng3b245872010-02-05 01:27:11 +0000408static bool isOperandOf(const SUnit *SU, SDNode *N) {
409 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +0000410 SUNode = SUNode->getGluedNode()) {
Evan Cheng3b245872010-02-05 01:27:11 +0000411 if (SUNode->isOperandOf(N))
412 return true;
413 }
414 return false;
415}
416
Evan Cheng5924bf72007-09-25 01:54:36 +0000417/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
418/// successors to the newly created node.
419SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Chris Lattner11a33812010-12-23 17:24:32 +0000420 if (SU->getNode()->getGluedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000421 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000422
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000423 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000424 if (!N)
425 return NULL;
426
427 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000428 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000429 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000430 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000431 if (VT == MVT::Glue)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000432 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000433 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000434 TryUnfold = true;
435 }
Evan Cheng79e97132007-10-05 01:39:18 +0000436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000437 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000438 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000439 if (VT == MVT::Glue)
Evan Cheng79e97132007-10-05 01:39:18 +0000440 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000441 }
442
443 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000444 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000445 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000446 return NULL;
447
Evan Chengbdd062d2010-05-20 06:13:19 +0000448 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000449 assert(NewNodes.size() == 2 && "Expected a load folding node!");
450
451 N = NewNodes[1];
452 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000453 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000454 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000455 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000456 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
457 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000458 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000459
Dan Gohmane52e0892008-11-11 21:34:44 +0000460 // LoadNode may already exist. This can happen when there is another
461 // load from the same location and producing the same type of value
462 // but it has different alignment or volatileness.
463 bool isNewLoad = true;
464 SUnit *LoadSU;
465 if (LoadNode->getNodeId() != -1) {
466 LoadSU = &SUnits[LoadNode->getNodeId()];
467 isNewLoad = false;
468 } else {
469 LoadSU = CreateNewSUnit(LoadNode);
470 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000471 ComputeLatency(LoadSU);
472 }
473
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000474 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000475 assert(N->getNodeId() == -1 && "Node already inserted!");
476 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +0000477
Dan Gohman17059682008-07-17 19:10:17 +0000478 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000479 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000480 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000481 NewSU->isTwoAddress = true;
482 break;
483 }
484 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000485 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000486 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000487 ComputeLatency(NewSU);
488
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000489 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000490 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000491 SmallVector<SDep, 4> ChainSuccs;
492 SmallVector<SDep, 4> LoadPreds;
493 SmallVector<SDep, 4> NodePreds;
494 SmallVector<SDep, 4> NodeSuccs;
495 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
496 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000497 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000498 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000499 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000500 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000501 else
Dan Gohman2d170892008-12-09 22:54:47 +0000502 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000503 }
504 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
505 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000506 if (I->isCtrl())
507 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000508 else
Dan Gohman2d170892008-12-09 22:54:47 +0000509 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000510 }
511
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000512 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000513 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
514 const SDep &Pred = ChainPreds[i];
515 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000516 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000517 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000518 }
Evan Cheng79e97132007-10-05 01:39:18 +0000519 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000520 const SDep &Pred = LoadPreds[i];
521 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000522 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000523 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000524 }
525 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000526 const SDep &Pred = NodePreds[i];
527 RemovePred(SU, Pred);
528 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000529 }
530 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000531 SDep D = NodeSuccs[i];
532 SUnit *SuccDep = D.getSUnit();
533 D.setSUnit(SU);
534 RemovePred(SuccDep, D);
535 D.setSUnit(NewSU);
536 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000537 }
538 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000539 SDep D = ChainSuccs[i];
540 SUnit *SuccDep = D.getSUnit();
541 D.setSUnit(SU);
542 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000543 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000544 D.setSUnit(LoadSU);
545 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000546 }
Andrew Trick2085a962010-12-21 22:25:04 +0000547 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000548
549 // Add a data dependency to reflect that NewSU reads the value defined
550 // by LoadSU.
551 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000552
Evan Cheng91e0fc92007-12-18 08:42:10 +0000553 if (isNewLoad)
554 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000555 AvailableQueue->addNode(NewSU);
556
557 ++NumUnfolds;
558
559 if (NewSU->NumSuccsLeft == 0) {
560 NewSU->isAvailable = true;
561 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000562 }
563 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000564 }
565
Evan Chengbdd062d2010-05-20 06:13:19 +0000566 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000567 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000568
569 // New SUnit has the exact same predecessors.
570 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
571 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000572 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000573 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000574
575 // Only copy scheduled successors. Cut them from old node's successor
576 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000577 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000578 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
579 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000580 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000581 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000582 SUnit *SuccSU = I->getSUnit();
583 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000584 SDep D = *I;
585 D.setSUnit(NewSU);
586 AddPred(SuccSU, D);
587 D.setSUnit(SU);
588 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000589 }
590 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000591 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000592 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000593
594 AvailableQueue->updateNode(SU);
595 AvailableQueue->addNode(NewSU);
596
Evan Cheng1ec79b42007-09-27 07:09:03 +0000597 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000598 return NewSU;
599}
600
Evan Chengb2c42c62009-01-12 03:19:55 +0000601/// InsertCopiesAndMoveSuccs - Insert register copies and move all
602/// scheduled successors of the given SUnit to the last copy.
603void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
604 const TargetRegisterClass *DestRC,
605 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000606 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000607 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000608 CopyFromSU->CopySrcRC = SrcRC;
609 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000610
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000611 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000612 CopyToSU->CopySrcRC = DestRC;
613 CopyToSU->CopyDstRC = SrcRC;
614
615 // Only copy scheduled successors. Cut them from old node's successor
616 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000617 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000618 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
619 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000620 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000621 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000622 SUnit *SuccSU = I->getSUnit();
623 if (SuccSU->isScheduled) {
624 SDep D = *I;
625 D.setSUnit(CopyToSU);
626 AddPred(SuccSU, D);
627 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000628 }
629 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000630 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000631 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000632
Dan Gohman2d170892008-12-09 22:54:47 +0000633 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
634 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000635
636 AvailableQueue->updateNode(SU);
637 AvailableQueue->addNode(CopyFromSU);
638 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000639 Copies.push_back(CopyFromSU);
640 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000641
Evan Chengb2c42c62009-01-12 03:19:55 +0000642 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000643}
644
645/// getPhysicalRegisterVT - Returns the ValueType of the physical register
646/// definition of the specified node.
647/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000648static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000649 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000650 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000651 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000652 unsigned NumRes = TID.getNumDefs();
653 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000654 if (Reg == *ImpDef)
655 break;
656 ++NumRes;
657 }
658 return N->getValueType(NumRes);
659}
660
Evan Chengb8905c42009-03-04 01:41:49 +0000661/// CheckForLiveRegDef - Return true and update live register vector if the
662/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000663static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +0000664 std::vector<SUnit*> &LiveRegDefs,
665 SmallSet<unsigned, 4> &RegAdded,
666 SmallVector<unsigned, 4> &LRegs,
667 const TargetRegisterInfo *TRI) {
Andrew Trick12acde112010-12-23 03:43:21 +0000668 for (const unsigned *AliasI = TRI->getOverlaps(Reg); *AliasI; ++AliasI) {
669
670 // Check if Ref is live.
671 if (!LiveRegDefs[Reg]) continue;
672
673 // Allow multiple uses of the same def.
674 if (LiveRegDefs[Reg] == SU) continue;
675
676 // Add Reg to the set of interfering live regs.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000677 if (RegAdded.insert(Reg))
Evan Chengb8905c42009-03-04 01:41:49 +0000678 LRegs.push_back(Reg);
Evan Chengb8905c42009-03-04 01:41:49 +0000679 }
Evan Chengb8905c42009-03-04 01:41:49 +0000680}
681
Evan Cheng5924bf72007-09-25 01:54:36 +0000682/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
683/// scheduling of the given node to satisfy live physical register dependencies.
684/// If the specific node is the last one that's available to schedule, do
685/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000686bool ScheduleDAGRRList::
687DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000688 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000689 return false;
690
Evan Chenge6f92252007-09-27 18:46:06 +0000691 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000692 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000693 //
694 // If SU is the currently live definition of the same register that it uses,
695 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +0000696 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
697 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000698 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +0000699 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
700 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000701 }
702
Chris Lattner11a33812010-12-23 17:24:32 +0000703 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000704 if (Node->getOpcode() == ISD::INLINEASM) {
705 // Inline asm can clobber physical defs.
706 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000707 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +0000708 --NumOps; // Ignore the glue operand.
Evan Chengb8905c42009-03-04 01:41:49 +0000709
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000710 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000711 unsigned Flags =
712 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000713 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000714
715 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000716 if (InlineAsm::isRegDefKind(Flags) ||
717 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000718 // Check for def of register or earlyclobber register.
719 for (; NumVals; --NumVals, ++i) {
720 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
721 if (TargetRegisterInfo::isPhysicalRegister(Reg))
722 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
723 }
724 } else
725 i += NumVals;
726 }
727 continue;
728 }
729
Dan Gohman072734e2008-11-13 23:24:17 +0000730 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000731 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000732 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000733 if (!TID.ImplicitDefs)
734 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000735 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
736 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000737 }
Andrew Trick2085a962010-12-21 22:25:04 +0000738
Evan Cheng5924bf72007-09-25 01:54:36 +0000739 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000740}
741
Andrew Trick528fad92010-12-23 05:42:20 +0000742/// Return a node that can be scheduled in this cycle. Requirements:
743/// (1) Ready: latency has been satisfied
744/// (2) No Hazards: resources are available (TBD)
745/// (3) No Interferences: may unschedule to break register interferences.
746SUnit *ScheduleDAGRRList::PickNodeToScheduleBottomUp() {
747 SmallVector<SUnit*, 4> Interferences;
748 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
749
750 SUnit *CurSU = AvailableQueue->pop();
751 while (CurSU) {
752 SmallVector<unsigned, 4> LRegs;
753 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
754 break;
755 LRegsMap.insert(std::make_pair(CurSU, LRegs));
756
757 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
758 Interferences.push_back(CurSU);
759 CurSU = AvailableQueue->pop();
760 }
761 if (CurSU) {
762 // Add the nodes that aren't ready back onto the available list.
763 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
764 Interferences[i]->isPending = false;
765 assert(Interferences[i]->isAvailable && "must still be available");
766 AvailableQueue->push(Interferences[i]);
767 }
768 return CurSU;
769 }
770
771 // All candidates are delayed due to live physical reg dependencies.
772 // Try backtracking, code duplication, or inserting cross class copies
773 // to resolve it.
774 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
775 SUnit *TrySU = Interferences[i];
776 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
777
778 // Try unscheduling up to the point where it's safe to schedule
779 // this node.
780 unsigned LiveCycle = CurCycle;
781 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
782 unsigned Reg = LRegs[j];
783 unsigned LCycle = LiveRegGens[Reg]->getHeight();
784 LiveCycle = std::min(LiveCycle, LCycle);
785 }
786 SUnit *OldSU = Sequence[LiveCycle];
787 if (!WillCreateCycle(TrySU, OldSU)) {
788 BacktrackBottomUp(TrySU, LiveCycle);
789
790 // Force the current node to be scheduled before the node that
791 // requires the physical reg dep.
792 if (OldSU->isAvailable) {
793 OldSU->isAvailable = false;
794 if (!OldSU->isPending)
795 AvailableQueue->remove(OldSU);
796 }
797 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
798 /*Reg=*/0, /*isNormalMemory=*/false,
799 /*isMustAlias=*/false, /*isArtificial=*/true));
800
801 // If one or more successors has been unscheduled, then the current
802 // node is no longer avaialable. Schedule a successor that's now
803 // available instead.
804 if (!TrySU->isAvailable) {
805 CurSU = AvailableQueue->pop();
806 }
807 else {
808 CurSU = TrySU;
809 TrySU->isPending = false;
810 Interferences.erase(Interferences.begin()+i);
811 }
812 break;
813 }
814 }
815
816 if (!CurSU) {
817 // Can't backtrack. If it's too expensive to copy the value, then try
818 // duplicate the nodes that produces these "too expensive to copy"
819 // values to break the dependency. In case even that doesn't work,
820 // insert cross class copies.
821 // If it's not too expensive, i.e. cost != -1, issue copies.
822 SUnit *TrySU = Interferences[0];
823 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
824 assert(LRegs.size() == 1 && "Can't handle this yet!");
825 unsigned Reg = LRegs[0];
826 SUnit *LRDef = LiveRegDefs[Reg];
827 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
828 const TargetRegisterClass *RC =
829 TRI->getMinimalPhysRegClass(Reg, VT);
830 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
831
832 // If cross copy register class is null, then it must be possible copy
833 // the value directly. Do not try duplicate the def.
834 SUnit *NewDef = 0;
835 if (DestRC)
836 NewDef = CopyAndMoveSuccessors(LRDef);
837 else
838 DestRC = RC;
839 if (!NewDef) {
840 // Issue copies, these can be expensive cross register class copies.
841 SmallVector<SUnit*, 2> Copies;
842 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
843 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
844 << " to SU #" << Copies.front()->NodeNum << "\n");
845 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
846 /*Reg=*/0, /*isNormalMemory=*/false,
847 /*isMustAlias=*/false,
848 /*isArtificial=*/true));
849 NewDef = Copies.back();
850 }
851
852 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
853 << " to SU #" << TrySU->NodeNum << "\n");
854 LiveRegDefs[Reg] = NewDef;
855 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
856 /*Reg=*/0, /*isNormalMemory=*/false,
857 /*isMustAlias=*/false,
858 /*isArtificial=*/true));
859 TrySU->isAvailable = false;
860 CurSU = NewDef;
861 }
862
863 assert(CurSU && "Unable to resolve live physical register dependencies!");
864
865 // Add the nodes that aren't ready back onto the available list.
866 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
867 Interferences[i]->isPending = false;
868 // May no longer be available due to backtracking.
869 if (Interferences[i]->isAvailable) {
870 AvailableQueue->push(Interferences[i]);
871 }
872 }
873 return CurSU;
874}
Evan Cheng1ec79b42007-09-27 07:09:03 +0000875
Evan Chengd38c22b2006-05-11 23:55:42 +0000876/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
877/// schedulers.
878void ScheduleDAGRRList::ListScheduleBottomUp() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000879 // Release any predecessors of the special Exit node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000880 ReleasePredecessors(&ExitSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000881
Evan Chengd38c22b2006-05-11 23:55:42 +0000882 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000883 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000884 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000885 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
886 RootSU->isAvailable = true;
887 AvailableQueue->push(RootSU);
888 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000889
890 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000891 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000892 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000893 while (!AvailableQueue->empty()) {
Andrew Trick528fad92010-12-23 05:42:20 +0000894 // Pick the best node to schedule taking all constraints into
895 // consideration.
896 SUnit *SU = PickNodeToScheduleBottomUp();
Evan Cheng1ec79b42007-09-27 07:09:03 +0000897
Andrew Trick528fad92010-12-23 05:42:20 +0000898 if (SU)
899 ScheduleNodeBottomUp(SU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000900
Evan Cheng5924bf72007-09-25 01:54:36 +0000901 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000902 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000903 }
904
Evan Chengd38c22b2006-05-11 23:55:42 +0000905 // Reverse the order if it is bottom up.
906 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +0000907
Evan Chengd38c22b2006-05-11 23:55:42 +0000908#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000909 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000910#endif
911}
912
913//===----------------------------------------------------------------------===//
914// Top-Down Scheduling
915//===----------------------------------------------------------------------===//
916
917/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000918/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000919void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000920 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000921
Evan Chengd38c22b2006-05-11 23:55:42 +0000922#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000923 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000924 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000925 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000926 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000927 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000928 }
929#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000930 --SuccSU->NumPredsLeft;
931
Dan Gohmanb9543432009-02-10 23:27:53 +0000932 // If all the node's predecessors are scheduled, this node is ready
933 // to be scheduled. Ignore the special ExitSU node.
934 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000935 SuccSU->isAvailable = true;
936 AvailableQueue->push(SuccSU);
937 }
938}
939
Dan Gohmanb9543432009-02-10 23:27:53 +0000940void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
941 // Top down: release successors
942 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
943 I != E; ++I) {
944 assert(!I->isAssignedRegDep() &&
945 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
946
947 ReleaseSucc(SU, &*I);
948 }
949}
950
Evan Chengd38c22b2006-05-11 23:55:42 +0000951/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
952/// count of its successors. If a successor pending count is zero, add it to
953/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +0000954void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000955 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000956 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000957
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000958 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
959 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000960 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000961
Dan Gohmanb9543432009-02-10 23:27:53 +0000962 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000963 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000964 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000965}
966
Dan Gohman54a187e2007-08-20 19:28:38 +0000967/// ListScheduleTopDown - The main loop of list scheduling for top-down
968/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000969void ScheduleDAGRRList::ListScheduleTopDown() {
970 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000971 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000972
Dan Gohmanb9543432009-02-10 23:27:53 +0000973 // Release any successors of the special Entry node.
974 ReleaseSuccessors(&EntrySU);
975
Evan Chengd38c22b2006-05-11 23:55:42 +0000976 // All leaves to Available queue.
977 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
978 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000979 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000980 AvailableQueue->push(&SUnits[i]);
981 SUnits[i].isAvailable = true;
982 }
983 }
Andrew Trick2085a962010-12-21 22:25:04 +0000984
Evan Chengd38c22b2006-05-11 23:55:42 +0000985 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000986 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000987 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000988 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000989 SUnit *CurSU = AvailableQueue->pop();
Andrew Trick2085a962010-12-21 22:25:04 +0000990
Dan Gohmanc602dd42008-11-21 00:10:42 +0000991 if (CurSU)
Andrew Trick528fad92010-12-23 05:42:20 +0000992 ScheduleNodeTopDown(CurSU);
Dan Gohman4370f262008-04-15 01:22:18 +0000993 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000994 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000995 }
Andrew Trick2085a962010-12-21 22:25:04 +0000996
Evan Chengd38c22b2006-05-11 23:55:42 +0000997#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000998 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000999#endif
1000}
1001
1002
Evan Chengd38c22b2006-05-11 23:55:42 +00001003//===----------------------------------------------------------------------===//
1004// RegReductionPriorityQueue Implementation
1005//===----------------------------------------------------------------------===//
1006//
1007// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1008// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +00001009//
Evan Chengd38c22b2006-05-11 23:55:42 +00001010namespace {
1011 template<class SF>
1012 class RegReductionPriorityQueue;
Andrew Trick2085a962010-12-21 22:25:04 +00001013
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001014 /// bu_ls_rr_sort - Priority function for bottom up register pressure
1015 // reduction scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +00001016 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1017 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
1018 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
1019 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001020
Evan Chengd38c22b2006-05-11 23:55:42 +00001021 bool operator()(const SUnit* left, const SUnit* right) const;
1022 };
1023
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001024 // td_ls_rr_sort - Priority function for top down register pressure reduction
1025 // scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +00001026 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1027 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1028 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1029 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001030
Evan Chengd38c22b2006-05-11 23:55:42 +00001031 bool operator()(const SUnit* left, const SUnit* right) const;
1032 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001033
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001034 // src_ls_rr_sort - Priority function for source order scheduler.
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001035 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1036 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
1037 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
1038 : SPQ(spq) {}
1039 src_ls_rr_sort(const src_ls_rr_sort &RHS)
1040 : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001041
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001042 bool operator()(const SUnit* left, const SUnit* right) const;
1043 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001044
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001045 // hybrid_ls_rr_sort - Priority function for hybrid scheduler.
Evan Chengbdd062d2010-05-20 06:13:19 +00001046 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1047 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
1048 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
1049 : SPQ(spq) {}
1050 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1051 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001052
Evan Chengbdd062d2010-05-20 06:13:19 +00001053 bool operator()(const SUnit* left, const SUnit* right) const;
1054 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001055
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001056 // ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1057 // scheduler.
Evan Cheng37b740c2010-07-24 00:39:05 +00001058 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1059 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1060 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1061 : SPQ(spq) {}
1062 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1063 : SPQ(RHS.SPQ) {}
1064
1065 bool operator()(const SUnit* left, const SUnit* right) const;
1066 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001067} // end anonymous namespace
1068
Dan Gohman186f65d2008-11-20 03:30:37 +00001069/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1070/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001071static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001072CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001073 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1074 if (SethiUllmanNumber != 0)
1075 return SethiUllmanNumber;
1076
1077 unsigned Extra = 0;
1078 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1079 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001080 if (I->isCtrl()) continue; // ignore chain preds
1081 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001082 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001083 if (PredSethiUllman > SethiUllmanNumber) {
1084 SethiUllmanNumber = PredSethiUllman;
1085 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001086 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001087 ++Extra;
1088 }
1089
1090 SethiUllmanNumber += Extra;
1091
1092 if (SethiUllmanNumber == 0)
1093 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001094
Evan Cheng7e4abde2008-07-02 09:23:51 +00001095 return SethiUllmanNumber;
1096}
1097
Evan Chengd38c22b2006-05-11 23:55:42 +00001098namespace {
1099 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001100 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001101 std::vector<SUnit*> Queue;
1102 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001103 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001104 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001105
Dan Gohman3f656df2008-11-20 02:45:51 +00001106 protected:
1107 // SUnits - The SUnits for the current graph.
1108 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001109
1110 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001111 const TargetInstrInfo *TII;
1112 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001113 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001114 ScheduleDAGRRList *scheduleDAG;
1115
Dan Gohman186f65d2008-11-20 03:30:37 +00001116 // SethiUllmanNumbers - The SethiUllman number for each node.
1117 std::vector<unsigned> SethiUllmanNumbers;
1118
Evan Chenga77f3d32010-07-21 06:09:07 +00001119 /// RegPressure - Tracking current reg pressure per register class.
1120 ///
Evan Cheng28590382010-07-21 23:53:58 +00001121 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001122
1123 /// RegLimit - Tracking the number of allocatable registers per register
1124 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001125 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001126
Dan Gohman3f656df2008-11-20 02:45:51 +00001127 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001128 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001129 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001130 const TargetInstrInfo *tii,
1131 const TargetRegisterInfo *tri,
1132 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001133 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001134 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001135 if (TracksRegPressure) {
1136 unsigned NumRC = TRI->getNumRegClasses();
1137 RegLimit.resize(NumRC);
1138 RegPressure.resize(NumRC);
1139 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1140 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1141 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1142 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001143 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001144 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001145 }
Andrew Trick2085a962010-12-21 22:25:04 +00001146
Dan Gohman3f656df2008-11-20 02:45:51 +00001147 void initNodes(std::vector<SUnit> &sunits) {
1148 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001149 // Add pseudo dependency edges for two-address nodes.
1150 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001151 // Reroute edges to nodes with multiple uses.
1152 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001153 // Calculate node priorities.
1154 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001155 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001156
Dan Gohman186f65d2008-11-20 03:30:37 +00001157 void addNode(const SUnit *SU) {
1158 unsigned SUSize = SethiUllmanNumbers.size();
1159 if (SUnits->size() > SUSize)
1160 SethiUllmanNumbers.resize(SUSize*2, 0);
1161 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1162 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001163
Dan Gohman186f65d2008-11-20 03:30:37 +00001164 void updateNode(const SUnit *SU) {
1165 SethiUllmanNumbers[SU->NodeNum] = 0;
1166 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1167 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001168
Dan Gohman186f65d2008-11-20 03:30:37 +00001169 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001170 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001171 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001172 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001173 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001174
1175 unsigned getNodePriority(const SUnit *SU) const {
1176 assert(SU->NodeNum < SethiUllmanNumbers.size());
1177 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001178 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001179 // CopyToReg should be close to its uses to facilitate coalescing and
1180 // avoid spilling.
1181 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001182 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1183 Opc == TargetOpcode::SUBREG_TO_REG ||
1184 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001185 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1186 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001187 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001188 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1189 // If SU does not have a register use, i.e. it doesn't produce a value
1190 // that would be consumed (e.g. store), then it terminates a chain of
1191 // computation. Give it a large SethiUllman number so it will be
1192 // scheduled right before its predecessors that it doesn't lengthen
1193 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001194 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001195 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1196 // If SU does not have a register def, schedule it close to its uses
1197 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001198 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001199 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001200 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001201
1202 unsigned getNodeOrdering(const SUnit *SU) const {
1203 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1204 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001205
Evan Chengd38c22b2006-05-11 23:55:42 +00001206 bool empty() const { return Queue.empty(); }
Andrew Trick2085a962010-12-21 22:25:04 +00001207
Evan Chengd38c22b2006-05-11 23:55:42 +00001208 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001209 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001210 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001211 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001212 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001213
Evan Chengd38c22b2006-05-11 23:55:42 +00001214 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001215 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001216 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001217 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001218 E = Queue.end(); I != E; ++I)
1219 if (Picker(*Best, *I))
1220 Best = I;
1221 SUnit *V = *Best;
1222 if (Best != prior(Queue.end()))
1223 std::swap(*Best, Queue.back());
1224 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001225 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001226 return V;
1227 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001228
Evan Cheng5924bf72007-09-25 01:54:36 +00001229 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001230 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001231 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001232 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1233 SU);
1234 if (I != prior(Queue.end()))
1235 std::swap(*I, Queue.back());
1236 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001237 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001238 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001239
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001240 bool HighRegPressure(const SUnit *SU) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001241 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001242 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001243
Evan Chenga77f3d32010-07-21 06:09:07 +00001244 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1245 I != E; ++I) {
1246 if (I->isCtrl())
1247 continue;
1248 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001249 const SDNode *PN = PredSU->getNode();
1250 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001251 if (PN->getOpcode() == ISD::CopyFromReg) {
1252 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001253 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1254 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001255 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1256 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001257 }
1258 continue;
1259 }
1260 unsigned POpc = PN->getMachineOpcode();
1261 if (POpc == TargetOpcode::IMPLICIT_DEF)
1262 continue;
1263 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1264 EVT VT = PN->getOperand(0).getValueType();
1265 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1266 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1267 // Check if this increases register pressure of the specific register
1268 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001269 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1270 return true;
Andrew Trick2085a962010-12-21 22:25:04 +00001271 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001272 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1273 POpc == TargetOpcode::SUBREG_TO_REG) {
1274 EVT VT = PN->getValueType(0);
1275 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1276 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1277 // Check if this increases register pressure of the specific register
1278 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001279 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1280 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001281 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001282 }
1283 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001284 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001285 EVT VT = PN->getValueType(i);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001286 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1287 if (RegPressure[RCId] >= RegLimit[RCId])
1288 return true; // Reg pressure already high.
1289 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Cheng28590382010-07-21 23:53:58 +00001290 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001291 continue;
Evan Chenga77f3d32010-07-21 06:09:07 +00001292 // Check if this increases register pressure of the specific register
1293 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001294 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1295 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001296 }
1297 }
1298
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001299 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001300 }
1301
Evan Chengbf32e542010-07-22 06:24:48 +00001302 void ScheduledNode(SUnit *SU) {
1303 if (!TracksRegPressure)
1304 return;
1305
Evan Chenga77f3d32010-07-21 06:09:07 +00001306 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001307 if (!N->isMachineOpcode()) {
1308 if (N->getOpcode() != ISD::CopyToReg)
1309 return;
1310 } else {
1311 unsigned Opc = N->getMachineOpcode();
1312 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1313 Opc == TargetOpcode::INSERT_SUBREG ||
1314 Opc == TargetOpcode::SUBREG_TO_REG ||
1315 Opc == TargetOpcode::REG_SEQUENCE ||
1316 Opc == TargetOpcode::IMPLICIT_DEF)
1317 return;
1318 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001319
1320 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1321 I != E; ++I) {
1322 if (I->isCtrl())
1323 continue;
1324 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001325 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001326 continue;
1327 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001328 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001329 if (PN->getOpcode() == ISD::CopyFromReg) {
1330 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001331 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1332 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1333 }
1334 continue;
1335 }
1336 unsigned POpc = PN->getMachineOpcode();
1337 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001338 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001339 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1340 EVT VT = PN->getOperand(0).getValueType();
1341 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1342 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001343 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001344 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1345 POpc == TargetOpcode::SUBREG_TO_REG) {
1346 EVT VT = PN->getValueType(0);
1347 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1348 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1349 continue;
1350 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001351 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1352 for (unsigned i = 0; i != NumDefs; ++i) {
1353 EVT VT = PN->getValueType(i);
1354 if (!PN->hasAnyUseOfValue(i))
1355 continue;
1356 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1357 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1358 }
1359 }
1360
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001361 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1362 // may transfer data dependencies to CopyToReg.
1363 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001364 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1365 for (unsigned i = 0; i != NumDefs; ++i) {
1366 EVT VT = N->getValueType(i);
1367 if (!N->hasAnyUseOfValue(i))
1368 continue;
1369 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1370 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1371 // Register pressure tracking is imprecise. This can happen.
1372 RegPressure[RCId] = 0;
1373 else
1374 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1375 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001376 }
Evan Chengbf32e542010-07-22 06:24:48 +00001377
1378 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001379 }
1380
Evan Chengbf32e542010-07-22 06:24:48 +00001381 void UnscheduledNode(SUnit *SU) {
1382 if (!TracksRegPressure)
1383 return;
1384
Evan Chenga77f3d32010-07-21 06:09:07 +00001385 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001386 if (!N->isMachineOpcode()) {
1387 if (N->getOpcode() != ISD::CopyToReg)
1388 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001389 } else {
1390 unsigned Opc = N->getMachineOpcode();
1391 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1392 Opc == TargetOpcode::INSERT_SUBREG ||
1393 Opc == TargetOpcode::SUBREG_TO_REG ||
1394 Opc == TargetOpcode::REG_SEQUENCE ||
1395 Opc == TargetOpcode::IMPLICIT_DEF)
1396 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001397 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001398
1399 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1400 I != E; ++I) {
1401 if (I->isCtrl())
1402 continue;
1403 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001404 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001405 continue;
1406 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001407 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001408 if (PN->getOpcode() == ISD::CopyFromReg) {
1409 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001410 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1411 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1412 }
1413 continue;
1414 }
1415 unsigned POpc = PN->getMachineOpcode();
1416 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001417 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001418 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1419 EVT VT = PN->getOperand(0).getValueType();
1420 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1421 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001422 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001423 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1424 POpc == TargetOpcode::SUBREG_TO_REG) {
1425 EVT VT = PN->getValueType(0);
1426 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1427 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1428 continue;
1429 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001430 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1431 for (unsigned i = 0; i != NumDefs; ++i) {
1432 EVT VT = PN->getValueType(i);
1433 if (!PN->hasAnyUseOfValue(i))
1434 continue;
1435 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001436 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001437 // Register pressure tracking is imprecise. This can happen.
1438 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001439 else
1440 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001441 }
1442 }
1443
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001444 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1445 // may transfer data dependencies to CopyToReg.
1446 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001447 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1448 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1449 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001450 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengdf907f42010-07-23 22:39:59 +00001451 continue;
1452 if (!N->hasAnyUseOfValue(i))
1453 continue;
1454 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1455 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1456 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001457 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001458
Evan Chenga77f3d32010-07-21 06:09:07 +00001459 dumpRegPressure();
1460 }
1461
Andrew Trick2085a962010-12-21 22:25:04 +00001462 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1463 scheduleDAG = scheduleDag;
Dan Gohman3f656df2008-11-20 02:45:51 +00001464 }
1465
Evan Chenga77f3d32010-07-21 06:09:07 +00001466 void dumpRegPressure() const {
1467 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1468 E = TRI->regclass_end(); I != E; ++I) {
1469 const TargetRegisterClass *RC = *I;
1470 unsigned Id = RC->getID();
1471 unsigned RP = RegPressure[Id];
1472 if (!RP) continue;
1473 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1474 << '\n');
1475 }
1476 }
1477
Dan Gohman3f656df2008-11-20 02:45:51 +00001478 protected:
1479 bool canClobber(const SUnit *SU, const SUnit *Op);
1480 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001481 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001482 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001483 };
1484
Dan Gohman186f65d2008-11-20 03:30:37 +00001485 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1486 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001487
Dan Gohman186f65d2008-11-20 03:30:37 +00001488 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1489 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001490
1491 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1492 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001493
1494 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1495 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001496
1497 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1498 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001499}
1500
Evan Chengb9e3db62007-03-14 22:43:40 +00001501/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001502/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001503static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001504 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001505 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001506 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001507 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001508 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001509 // If there are bunch of CopyToRegs stacked up, they should be considered
1510 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001511 if (I->getSUnit()->getNode() &&
1512 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001513 Height = closestSucc(I->getSUnit())+1;
1514 if (Height > MaxHeight)
1515 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001516 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001517 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001518}
1519
Evan Cheng61bc51e2007-12-20 02:22:36 +00001520/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001521/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001522static unsigned calcMaxScratches(const SUnit *SU) {
1523 unsigned Scratches = 0;
1524 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001525 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001526 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001527 Scratches++;
1528 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001529 return Scratches;
1530}
1531
Evan Cheng6c1414f2010-10-29 18:09:28 +00001532/// hasOnlyLiveOutUse - Return true if SU has a single value successor that is a
1533/// CopyToReg to a virtual register. This SU def is probably a liveout and
1534/// it has no other use. It should be scheduled closer to the terminator.
1535static bool hasOnlyLiveOutUses(const SUnit *SU) {
1536 bool RetVal = false;
1537 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1538 I != E; ++I) {
1539 if (I->isCtrl()) continue;
1540 const SUnit *SuccSU = I->getSUnit();
1541 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
1542 unsigned Reg =
1543 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
1544 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1545 RetVal = true;
1546 continue;
1547 }
1548 }
1549 return false;
1550 }
1551 return RetVal;
1552}
1553
1554/// UnitsSharePred - Return true if the two scheduling units share a common
1555/// data predecessor.
1556static bool UnitsSharePred(const SUnit *left, const SUnit *right) {
1557 SmallSet<const SUnit*, 4> Preds;
1558 for (SUnit::const_pred_iterator I = left->Preds.begin(),E = left->Preds.end();
1559 I != E; ++I) {
1560 if (I->isCtrl()) continue; // ignore chain preds
1561 Preds.insert(I->getSUnit());
1562 }
1563 for (SUnit::const_pred_iterator I = right->Preds.begin(),E = right->Preds.end();
1564 I != E; ++I) {
1565 if (I->isCtrl()) continue; // ignore chain preds
1566 if (Preds.count(I->getSUnit()))
1567 return true;
1568 }
1569 return false;
1570}
1571
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001572template <typename RRSort>
1573static bool BURRSort(const SUnit *left, const SUnit *right,
1574 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001575 unsigned LPriority = SPQ->getNodePriority(left);
1576 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001577 if (LPriority != RPriority)
1578 return LPriority > RPriority;
1579
1580 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1581 // e.g.
1582 // t1 = op t2, c1
1583 // t3 = op t4, c2
1584 //
1585 // and the following instructions are both ready.
1586 // t2 = op c3
1587 // t4 = op c4
1588 //
1589 // Then schedule t2 = op first.
1590 // i.e.
1591 // t4 = op c4
1592 // t2 = op c3
1593 // t1 = op t2, c1
1594 // t3 = op t4, c2
1595 //
1596 // This creates more short live intervals.
1597 unsigned LDist = closestSucc(left);
1598 unsigned RDist = closestSucc(right);
1599 if (LDist != RDist)
1600 return LDist < RDist;
1601
Evan Cheng3a14efa2009-02-12 08:59:45 +00001602 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001603 unsigned LScratch = calcMaxScratches(left);
1604 unsigned RScratch = calcMaxScratches(right);
1605 if (LScratch != RScratch)
1606 return LScratch > RScratch;
1607
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001608 if (left->getHeight() != right->getHeight())
1609 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00001610
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001611 if (left->getDepth() != right->getDepth())
1612 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001613
Andrew Trick2085a962010-12-21 22:25:04 +00001614 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00001615 "NodeQueueId cannot be zero");
1616 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001617}
1618
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001619// Bottom up
1620bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1621 return BURRSort(left, right, SPQ);
1622}
1623
1624// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001625bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001626 unsigned LOrder = SPQ->getNodeOrdering(left);
1627 unsigned ROrder = SPQ->getNodeOrdering(right);
1628
1629 // Prefer an ordering where the lower the non-zero order number, the higher
1630 // the preference.
1631 if ((LOrder || ROrder) && LOrder != ROrder)
1632 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1633
1634 return BURRSort(left, right, SPQ);
1635}
1636
Evan Chengbdd062d2010-05-20 06:13:19 +00001637bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdebf9c52010-11-03 00:45:17 +00001638 if (left->isCall || right->isCall)
1639 // No way to compute latency of calls.
1640 return BURRSort(left, right, SPQ);
1641
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001642 bool LHigh = SPQ->HighRegPressure(left);
1643 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001644 // Avoid causing spills. If register pressure is high, schedule for
1645 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001646 if (LHigh && !RHigh)
1647 return true;
1648 else if (!LHigh && RHigh)
1649 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001650 else if (!LHigh && !RHigh) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001651 // If the two nodes share an operand and one of them has a single
1652 // use that is a live out copy, favor the one that is live out. Otherwise
1653 // it will be difficult to eliminate the copy if the instruction is a
1654 // loop induction variable update. e.g.
1655 // BB:
1656 // sub r1, r3, #1
1657 // str r0, [r2, r3]
1658 // mov r3, r1
1659 // cmp
1660 // bne BB
1661 bool SharePred = UnitsSharePred(left, right);
1662 // FIXME: Only adjust if BB is a loop back edge.
1663 // FIXME: What's the cost of a copy?
1664 int LBonus = (SharePred && hasOnlyLiveOutUses(left)) ? 1 : 0;
1665 int RBonus = (SharePred && hasOnlyLiveOutUses(right)) ? 1 : 0;
1666 int LHeight = (int)left->getHeight() - LBonus;
1667 int RHeight = (int)right->getHeight() - RBonus;
1668
Evan Cheng28590382010-07-21 23:53:58 +00001669 // Low register pressure situation, schedule for latency if possible.
1670 bool LStall = left->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001671 (int)SPQ->getCurCycle() < LHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001672 bool RStall = right->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001673 (int)SPQ->getCurCycle() < RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001674 // If scheduling one of the node will cause a pipeline stall, delay it.
1675 // If scheduling either one of the node will cause a pipeline stall, sort
1676 // them according to their height.
Evan Cheng28590382010-07-21 23:53:58 +00001677 if (LStall) {
1678 if (!RStall)
1679 return true;
Evan Cheng6c1414f2010-10-29 18:09:28 +00001680 if (LHeight != RHeight)
1681 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001682 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001683 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001684
Evan Cheng6c1414f2010-10-29 18:09:28 +00001685 // If either node is scheduling for latency, sort them by height
1686 // and latency.
Evan Cheng28590382010-07-21 23:53:58 +00001687 if (left->SchedulingPref == Sched::Latency ||
1688 right->SchedulingPref == Sched::Latency) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001689 if (LHeight != RHeight)
1690 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001691 if (left->Latency != right->Latency)
1692 return left->Latency > right->Latency;
1693 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001694 }
1695
Evan Chengbdd062d2010-05-20 06:13:19 +00001696 return BURRSort(left, right, SPQ);
1697}
1698
Evan Cheng37b740c2010-07-24 00:39:05 +00001699bool ilp_ls_rr_sort::operator()(const SUnit *left,
1700 const SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001701 if (left->isCall || right->isCall)
1702 // No way to compute latency of calls.
1703 return BURRSort(left, right, SPQ);
1704
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001705 bool LHigh = SPQ->HighRegPressure(left);
1706 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001707 // Avoid causing spills. If register pressure is high, schedule for
1708 // register pressure reduction.
1709 if (LHigh && !RHigh)
1710 return true;
1711 else if (!LHigh && RHigh)
1712 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001713 else if (!LHigh && !RHigh) {
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001714 // Low register pressure situation, schedule to maximize instruction level
1715 // parallelism.
Evan Cheng37b740c2010-07-24 00:39:05 +00001716 if (left->NumPreds > right->NumPreds)
1717 return false;
1718 else if (left->NumPreds < right->NumPreds)
1719 return false;
1720 }
1721
1722 return BURRSort(left, right, SPQ);
1723}
1724
Dan Gohman3f656df2008-11-20 02:45:51 +00001725template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001726bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001727RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001728 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001729 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001730 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001731 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001732 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001733 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001734 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001735 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001736 if (DU->getNodeId() != -1 &&
1737 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001738 return true;
1739 }
1740 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001741 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001742 return false;
1743}
1744
Evan Chengf9891412007-12-20 09:25:31 +00001745/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001746/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001747static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001748 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001749 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001750 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001751 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1752 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001753 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001754 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +00001755 SUNode = SUNode->getGluedNode()) {
Dan Gohmana366da12009-03-23 16:23:01 +00001756 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001757 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001758 const unsigned *SUImpDefs =
1759 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1760 if (!SUImpDefs)
1761 return false;
1762 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001763 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001764 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001765 continue;
1766 if (!N->hasAnyUseOfValue(i))
1767 continue;
1768 unsigned Reg = ImpDefs[i - NumDefs];
1769 for (;*SUImpDefs; ++SUImpDefs) {
1770 unsigned SUReg = *SUImpDefs;
1771 if (TRI->regsOverlap(Reg, SUReg))
1772 return true;
1773 }
Evan Chengf9891412007-12-20 09:25:31 +00001774 }
1775 }
1776 return false;
1777}
1778
Dan Gohman9a658d72009-03-24 00:49:12 +00001779/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1780/// are not handled well by the general register pressure reduction
1781/// heuristics. When presented with code like this:
1782///
1783/// N
1784/// / |
1785/// / |
1786/// U store
1787/// |
1788/// ...
1789///
1790/// the heuristics tend to push the store up, but since the
1791/// operand of the store has another use (U), this would increase
1792/// the length of that other use (the U->N edge).
1793///
1794/// This function transforms code like the above to route U's
1795/// dependence through the store when possible, like this:
1796///
1797/// N
1798/// ||
1799/// ||
1800/// store
1801/// |
1802/// U
1803/// |
1804/// ...
1805///
1806/// This results in the store being scheduled immediately
1807/// after N, which shortens the U->N live range, reducing
1808/// register pressure.
1809///
1810template<class SF>
1811void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1812 // Visit all the nodes in topological order, working top-down.
1813 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1814 SUnit *SU = &(*SUnits)[i];
1815 // For now, only look at nodes with no data successors, such as stores.
1816 // These are especially important, due to the heuristics in
1817 // getNodePriority for nodes with no data successors.
1818 if (SU->NumSuccs != 0)
1819 continue;
1820 // For now, only look at nodes with exactly one data predecessor.
1821 if (SU->NumPreds != 1)
1822 continue;
1823 // Avoid prescheduling copies to virtual registers, which don't behave
1824 // like other nodes from the perspective of scheduling heuristics.
1825 if (SDNode *N = SU->getNode())
1826 if (N->getOpcode() == ISD::CopyToReg &&
1827 TargetRegisterInfo::isVirtualRegister
1828 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1829 continue;
1830
1831 // Locate the single data predecessor.
1832 SUnit *PredSU = 0;
1833 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1834 EE = SU->Preds.end(); II != EE; ++II)
1835 if (!II->isCtrl()) {
1836 PredSU = II->getSUnit();
1837 break;
1838 }
1839 assert(PredSU);
1840
1841 // Don't rewrite edges that carry physregs, because that requires additional
1842 // support infrastructure.
1843 if (PredSU->hasPhysRegDefs)
1844 continue;
1845 // Short-circuit the case where SU is PredSU's only data successor.
1846 if (PredSU->NumSuccs == 1)
1847 continue;
1848 // Avoid prescheduling to copies from virtual registers, which don't behave
1849 // like other nodes from the perspective of scheduling // heuristics.
1850 if (SDNode *N = SU->getNode())
1851 if (N->getOpcode() == ISD::CopyFromReg &&
1852 TargetRegisterInfo::isVirtualRegister
1853 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1854 continue;
1855
1856 // Perform checks on the successors of PredSU.
1857 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1858 EE = PredSU->Succs.end(); II != EE; ++II) {
1859 SUnit *PredSuccSU = II->getSUnit();
1860 if (PredSuccSU == SU) continue;
1861 // If PredSU has another successor with no data successors, for
1862 // now don't attempt to choose either over the other.
1863 if (PredSuccSU->NumSuccs == 0)
1864 goto outer_loop_continue;
1865 // Don't break physical register dependencies.
1866 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1867 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1868 goto outer_loop_continue;
1869 // Don't introduce graph cycles.
1870 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1871 goto outer_loop_continue;
1872 }
1873
1874 // Ok, the transformation is safe and the heuristics suggest it is
1875 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001876 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1877 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001878 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001879 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1880 SDep Edge = PredSU->Succs[i];
1881 assert(!Edge.isAssignedRegDep());
1882 SUnit *SuccSU = Edge.getSUnit();
1883 if (SuccSU != SU) {
1884 Edge.setSUnit(PredSU);
1885 scheduleDAG->RemovePred(SuccSU, Edge);
1886 scheduleDAG->AddPred(SU, Edge);
1887 Edge.setSUnit(SU);
1888 scheduleDAG->AddPred(SuccSU, Edge);
1889 --i;
1890 }
1891 }
1892 outer_loop_continue:;
1893 }
1894}
1895
Evan Chengd38c22b2006-05-11 23:55:42 +00001896/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1897/// it as a def&use operand. Add a pseudo control edge from it to the other
1898/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001899/// first (lower in the schedule). If both nodes are two-address, favor the
1900/// one that has a CopyToReg use (more likely to be a loop induction update).
1901/// If both are two-address, but one is commutable while the other is not
1902/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001903template<class SF>
1904void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001905 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001906 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001907 if (!SU->isTwoAddress)
1908 continue;
1909
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001910 SDNode *Node = SU->getNode();
Chris Lattner11a33812010-12-23 17:24:32 +00001911 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getGluedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001912 continue;
1913
Evan Cheng6c1414f2010-10-29 18:09:28 +00001914 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00001915 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001916 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001917 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001918 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001919 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001920 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1921 continue;
1922 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1923 if (DU->getNodeId() == -1)
1924 continue;
1925 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1926 if (!DUSU) continue;
1927 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1928 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001929 if (I->isCtrl()) continue;
1930 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001931 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001932 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001933 // Be conservative. Ignore if nodes aren't at roughly the same
1934 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001935 if (SuccSU->getHeight() < SU->getHeight() &&
1936 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001937 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001938 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1939 // constrains whatever is using the copy, instead of the copy
1940 // itself. In the case that the copy is coalesced, this
1941 // preserves the intent of the pseudo two-address heurietics.
1942 while (SuccSU->Succs.size() == 1 &&
1943 SuccSU->getNode()->isMachineOpcode() &&
1944 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001945 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001946 SuccSU = SuccSU->Succs.front().getSUnit();
1947 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001948 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1949 continue;
1950 // Don't constrain nodes with physical register defs if the
1951 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001952 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001953 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001954 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001955 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001956 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1957 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001958 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001959 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1960 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1961 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001962 continue;
1963 if ((!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00001964 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00001965 (!SU->isCommutable && SuccSU->isCommutable)) &&
1966 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001967 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001968 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001969 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001970 /*Reg=*/0, /*isNormalMemory=*/false,
1971 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001972 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001973 }
1974 }
1975 }
1976 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001977}
1978
Evan Cheng6730f032007-01-08 23:55:53 +00001979/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1980/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001981template<class SF>
1982void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001983 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick2085a962010-12-21 22:25:04 +00001984
Evan Chengd38c22b2006-05-11 23:55:42 +00001985 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001986 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001987}
Evan Chengd38c22b2006-05-11 23:55:42 +00001988
Roman Levenstein30d09512008-03-27 09:44:37 +00001989/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001990/// predecessors of the successors of the SUnit SU. Stop when the provided
1991/// limit is exceeded.
Andrew Trick2085a962010-12-21 22:25:04 +00001992static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
Roman Levensteinbc674502008-03-27 09:14:57 +00001993 unsigned Limit) {
1994 unsigned Sum = 0;
1995 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1996 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001997 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001998 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1999 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00002000 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00002001 if (!PredSU->isScheduled)
2002 if (++Sum > Limit)
2003 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00002004 }
2005 }
2006 return Sum;
2007}
2008
Evan Chengd38c22b2006-05-11 23:55:42 +00002009
2010// Top down
2011bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00002012 unsigned LPriority = SPQ->getNodePriority(left);
2013 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002014 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
2015 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00002016 bool LIsFloater = LIsTarget && left->NumPreds == 0;
2017 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00002018 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
2019 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00002020
2021 if (left->NumSuccs == 0 && right->NumSuccs != 0)
2022 return false;
2023 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
2024 return true;
2025
Evan Chengd38c22b2006-05-11 23:55:42 +00002026 if (LIsFloater)
2027 LBonus -= 2;
2028 if (RIsFloater)
2029 RBonus -= 2;
2030 if (left->NumSuccs == 1)
2031 LBonus += 2;
2032 if (right->NumSuccs == 1)
2033 RBonus += 2;
2034
Evan Cheng73bdf042008-03-01 00:39:47 +00002035 if (LPriority+LBonus != RPriority+RBonus)
2036 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00002037
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002038 if (left->getDepth() != right->getDepth())
2039 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00002040
2041 if (left->NumSuccsLeft != right->NumSuccsLeft)
2042 return left->NumSuccsLeft > right->NumSuccsLeft;
2043
Andrew Trick2085a962010-12-21 22:25:04 +00002044 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002045 "NodeQueueId cannot be zero");
2046 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002047}
2048
Evan Chengd38c22b2006-05-11 23:55:42 +00002049//===----------------------------------------------------------------------===//
2050// Public Constructor Functions
2051//===----------------------------------------------------------------------===//
2052
Dan Gohmandfaf6462009-02-11 04:27:20 +00002053llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002054llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002055 const TargetMachine &TM = IS->TM;
2056 const TargetInstrInfo *TII = TM.getInstrInfo();
2057 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002058
Evan Chenga77f3d32010-07-21 06:09:07 +00002059 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002060 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002061 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002062 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002063 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002064}
2065
Dan Gohmandfaf6462009-02-11 04:27:20 +00002066llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002067llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002068 const TargetMachine &TM = IS->TM;
2069 const TargetInstrInfo *TII = TM.getInstrInfo();
2070 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002071
Evan Chenga77f3d32010-07-21 06:09:07 +00002072 TDRegReductionPriorityQueue *PQ =
2073 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002074 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00002075 PQ->setScheduleDAG(SD);
2076 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002077}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002078
2079llvm::ScheduleDAGSDNodes *
2080llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2081 const TargetMachine &TM = IS->TM;
2082 const TargetInstrInfo *TII = TM.getInstrInfo();
2083 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002084
Evan Chenga77f3d32010-07-21 06:09:07 +00002085 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002086 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002087 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
2088 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002089 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00002090}
2091
2092llvm::ScheduleDAGSDNodes *
2093llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2094 const TargetMachine &TM = IS->TM;
2095 const TargetInstrInfo *TII = TM.getInstrInfo();
2096 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002097 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002098
Evan Chenga77f3d32010-07-21 06:09:07 +00002099 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002100 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002101 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002102 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002103 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002104}
Evan Cheng37b740c2010-07-24 00:39:05 +00002105
2106llvm::ScheduleDAGSDNodes *
2107llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2108 const TargetMachine &TM = IS->TM;
2109 const TargetInstrInfo *TII = TM.getInstrInfo();
2110 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2111 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002112
Evan Cheng37b740c2010-07-24 00:39:05 +00002113 ILPBURRPriorityQueue *PQ =
2114 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2115 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2116 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002117 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00002118}