blob: a925a79cc69c8eb5765541d020c0b7a99932f038 [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
Dan Gohmanc07f6862008-09-23 18:50:48 +000086 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000087 /// that are "live". These nodes must be scheduled before any other nodes that
88 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000089 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000090 std::vector<SUnit*> LiveRegDefs;
91 std::vector<unsigned> LiveRegCycles;
92
Dan Gohmanad2134d2008-11-25 00:52:40 +000093 /// Topo - A topological ordering for SUnits which permits fast IsReachable
94 /// and similar queries.
95 ScheduleDAGTopologicalSort Topo;
96
Evan Chengd38c22b2006-05-11 23:55:42 +000097public:
Dan Gohman619ef482009-01-15 19:20:50 +000098 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000099 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +0000100 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +0000101 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +0000102 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000103 }
104
105 ~ScheduleDAGRRList() {
106 delete AvailableQueue;
107 }
108
109 void Schedule();
110
Roman Levenstein733a4d62008-03-26 11:23:38 +0000111 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
113 return Topo.IsReachable(SU, TargetSU);
114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Dan Gohman60d68442009-01-29 19:49:27 +0000116 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000117 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000118 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
119 return Topo.WillCreateCycle(SU, TargetSU);
120 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000121
Dan Gohman2d170892008-12-09 22:54:47 +0000122 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000123 /// This returns true if this is a new predecessor.
124 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000125 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000126 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000127 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000128 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000129
Dan Gohman2d170892008-12-09 22:54:47 +0000130 /// RemovePred - removes a predecessor edge from SUnit SU.
131 /// This returns true if an edge was removed.
132 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000133 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000134 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000135 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000136 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137
Evan Chengd38c22b2006-05-11 23:55:42 +0000138private:
Dan Gohman60d68442009-01-29 19:49:27 +0000139 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000140 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000141 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000142 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000143 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000144 void ScheduleNodeBottomUp(SUnit*, unsigned);
145 void ScheduleNodeTopDown(SUnit*, unsigned);
146 void UnscheduleNodeBottomUp(SUnit*);
147 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
148 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000149 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
150 const TargetRegisterClass*,
151 const TargetRegisterClass*,
152 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000153 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000154 void ListScheduleTopDown();
155 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156
157
158 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000159 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000160 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000161 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000162 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000164 if (NewNode->NodeNum >= NumSUnits)
165 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000166 return NewNode;
167 }
168
Roman Levenstein733a4d62008-03-26 11:23:38 +0000169 /// CreateClone - Creates a new SUnit from an existing one.
170 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000172 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000173 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000174 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000175 if (NewNode->NodeNum >= NumSUnits)
176 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000177 return NewNode;
178 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000179
Evan Chengbdd062d2010-05-20 06:13:19 +0000180 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
181 /// need actual latency information but the hybrid scheduler does.
182 bool ForceUnitLatencies() const {
183 return !NeedLatency;
184 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000185};
186} // end anonymous namespace
187
188
189/// Schedule - Schedule the DAG using list scheduling.
190void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000191 DEBUG(dbgs()
192 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000193 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000194
Dan Gohmanc07f6862008-09-23 18:50:48 +0000195 NumLiveRegs = 0;
Andrew Trick2085a962010-12-21 22:25:04 +0000196 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000197 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000198
Dan Gohman04543e72008-12-23 18:36:58 +0000199 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000200 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000201
Evan Chengd38c22b2006-05-11 23:55:42 +0000202 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000203 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000204 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000205
Dan Gohman46520a22008-06-21 19:18:17 +0000206 AvailableQueue->initNodes(SUnits);
Andrew Trick2085a962010-12-21 22:25:04 +0000207
Evan Chengd38c22b2006-05-11 23:55:42 +0000208 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
209 if (isBottomUp)
210 ListScheduleBottomUp();
211 else
212 ListScheduleTopDown();
Andrew Trick2085a962010-12-21 22:25:04 +0000213
Evan Chengd38c22b2006-05-11 23:55:42 +0000214 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000215}
Evan Chengd38c22b2006-05-11 23:55:42 +0000216
217//===----------------------------------------------------------------------===//
218// Bottom-Up Scheduling
219//===----------------------------------------------------------------------===//
220
Evan Chengd38c22b2006-05-11 23:55:42 +0000221/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000222/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000223void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000224 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000225
Evan Chengd38c22b2006-05-11 23:55:42 +0000226#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000227 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000228 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000229 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000230 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000231 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000232 }
233#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000234 --PredSU->NumSuccsLeft;
235
Evan Chengbdd062d2010-05-20 06:13:19 +0000236 if (!ForceUnitLatencies()) {
237 // Updating predecessor's height. This is now the cycle when the
238 // predecessor can be scheduled without causing a pipeline stall.
239 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
240 }
241
Dan Gohmanb9543432009-02-10 23:27:53 +0000242 // If all the node's successors are scheduled, this node is ready
243 // to be scheduled. Ignore the special EntrySU node.
244 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000245 PredSU->isAvailable = true;
246 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000247 }
248}
249
Dan Gohmanb9543432009-02-10 23:27:53 +0000250void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000251 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000252 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000253 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000254 ReleasePred(SU, &*I);
255 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000256 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000257 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000258 // clobber the register is scheduled between the predecessor and
259 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000260 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000261 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000262 LiveRegDefs[I->getReg()] = I->getSUnit();
263 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000264 }
265 }
266 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000267}
268
269/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
270/// count of its predecessors. If a predecessor pending count is zero, add it to
271/// the Available queue.
272void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000273 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000274 DEBUG(SU->dump(this));
275
Evan Chengbdd062d2010-05-20 06:13:19 +0000276#ifndef NDEBUG
277 if (CurCycle < SU->getHeight())
278 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
279#endif
280
281 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000282 SU->setHeightToAtLeast(CurCycle);
283 Sequence.push_back(SU);
284
Evan Cheng28590382010-07-21 23:53:58 +0000285 AvailableQueue->ScheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000286
Dan Gohmanb9543432009-02-10 23:27:53 +0000287 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000288
289 // Release all the implicit physical register defs that are live.
290 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
291 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000292 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000293 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000294 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000295 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000296 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000297 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000298 LiveRegDefs[I->getReg()] = NULL;
299 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000300 }
301 }
302 }
303
Evan Chengd38c22b2006-05-11 23:55:42 +0000304 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000305}
306
Evan Cheng5924bf72007-09-25 01:54:36 +0000307/// CapturePred - This does the opposite of ReleasePred. Since SU is being
308/// unscheduled, incrcease the succ left count of its predecessors. Remove
309/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000310void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000311 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000312 if (PredSU->isAvailable) {
313 PredSU->isAvailable = false;
314 if (!PredSU->isPending)
315 AvailableQueue->remove(PredSU);
316 }
317
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000318 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000319 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000320}
321
322/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
323/// its predecessor states to reflect the change.
324void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000325 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000326 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000327
Evan Cheng5924bf72007-09-25 01:54:36 +0000328 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
329 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000330 CapturePred(&*I);
Evan Chengcc2efe12010-05-28 23:26:21 +0000331 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000332 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000333 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000334 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000335 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000336 LiveRegDefs[I->getReg()] = NULL;
337 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000338 }
339 }
340
341 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
342 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000343 if (I->isAssignedRegDep()) {
344 if (!LiveRegDefs[I->getReg()]) {
345 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000346 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000348 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
349 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000350 }
351 }
352
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000353 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000354 SU->isScheduled = false;
355 SU->isAvailable = true;
356 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000357 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000358}
359
Evan Cheng8e136a92007-09-26 21:36:17 +0000360/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000361/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000362void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
363 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000364 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000365 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000366 OldSU = Sequence.back();
367 Sequence.pop_back();
368 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000369 // Don't try to remove SU from AvailableQueue.
370 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000371 UnscheduleNodeBottomUp(OldSU);
372 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000373 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000374 }
375
Dan Gohman60d68442009-01-29 19:49:27 +0000376 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000377
378 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000379}
380
Evan Cheng3b245872010-02-05 01:27:11 +0000381static bool isOperandOf(const SUnit *SU, SDNode *N) {
382 for (const SDNode *SUNode = SU->getNode(); SUNode;
383 SUNode = SUNode->getFlaggedNode()) {
384 if (SUNode->isOperandOf(N))
385 return true;
386 }
387 return false;
388}
389
Evan Cheng5924bf72007-09-25 01:54:36 +0000390/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
391/// successors to the newly created node.
392SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000393 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000394 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000395
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000396 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000397 if (!N)
398 return NULL;
399
400 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000401 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000402 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000403 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000404 if (VT == MVT::Glue)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000405 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000406 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000407 TryUnfold = true;
408 }
Evan Cheng79e97132007-10-05 01:39:18 +0000409 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000410 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000411 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000412 if (VT == MVT::Glue)
Evan Cheng79e97132007-10-05 01:39:18 +0000413 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000414 }
415
416 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000417 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000418 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000419 return NULL;
420
Evan Chengbdd062d2010-05-20 06:13:19 +0000421 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000422 assert(NewNodes.size() == 2 && "Expected a load folding node!");
423
424 N = NewNodes[1];
425 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000426 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000427 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000428 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000429 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
430 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000431 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000432
Dan Gohmane52e0892008-11-11 21:34:44 +0000433 // LoadNode may already exist. This can happen when there is another
434 // load from the same location and producing the same type of value
435 // but it has different alignment or volatileness.
436 bool isNewLoad = true;
437 SUnit *LoadSU;
438 if (LoadNode->getNodeId() != -1) {
439 LoadSU = &SUnits[LoadNode->getNodeId()];
440 isNewLoad = false;
441 } else {
442 LoadSU = CreateNewSUnit(LoadNode);
443 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000444 ComputeLatency(LoadSU);
445 }
446
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000447 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000448 assert(N->getNodeId() == -1 && "Node already inserted!");
449 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +0000450
Dan Gohman17059682008-07-17 19:10:17 +0000451 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000452 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000453 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000454 NewSU->isTwoAddress = true;
455 break;
456 }
457 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000458 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000459 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000460 ComputeLatency(NewSU);
461
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000462 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000463 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000464 SmallVector<SDep, 4> ChainSuccs;
465 SmallVector<SDep, 4> LoadPreds;
466 SmallVector<SDep, 4> NodePreds;
467 SmallVector<SDep, 4> NodeSuccs;
468 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
469 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000470 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000471 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000472 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000473 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000474 else
Dan Gohman2d170892008-12-09 22:54:47 +0000475 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000476 }
477 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
478 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000479 if (I->isCtrl())
480 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000481 else
Dan Gohman2d170892008-12-09 22:54:47 +0000482 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000483 }
484
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000485 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000486 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
487 const SDep &Pred = ChainPreds[i];
488 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000489 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000490 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000491 }
Evan Cheng79e97132007-10-05 01:39:18 +0000492 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000493 const SDep &Pred = LoadPreds[i];
494 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000495 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000496 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000497 }
498 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000499 const SDep &Pred = NodePreds[i];
500 RemovePred(SU, Pred);
501 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000502 }
503 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000504 SDep D = NodeSuccs[i];
505 SUnit *SuccDep = D.getSUnit();
506 D.setSUnit(SU);
507 RemovePred(SuccDep, D);
508 D.setSUnit(NewSU);
509 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000510 }
511 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000512 SDep D = ChainSuccs[i];
513 SUnit *SuccDep = D.getSUnit();
514 D.setSUnit(SU);
515 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000516 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000517 D.setSUnit(LoadSU);
518 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000519 }
Andrew Trick2085a962010-12-21 22:25:04 +0000520 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000521
522 // Add a data dependency to reflect that NewSU reads the value defined
523 // by LoadSU.
524 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000525
Evan Cheng91e0fc92007-12-18 08:42:10 +0000526 if (isNewLoad)
527 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000528 AvailableQueue->addNode(NewSU);
529
530 ++NumUnfolds;
531
532 if (NewSU->NumSuccsLeft == 0) {
533 NewSU->isAvailable = true;
534 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000535 }
536 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000537 }
538
Evan Chengbdd062d2010-05-20 06:13:19 +0000539 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000541
542 // New SUnit has the exact same predecessors.
543 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
544 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000545 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000546 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000547
548 // Only copy scheduled successors. Cut them from old node's successor
549 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000550 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000551 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
552 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000553 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000554 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000555 SUnit *SuccSU = I->getSUnit();
556 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000557 SDep D = *I;
558 D.setSUnit(NewSU);
559 AddPred(SuccSU, D);
560 D.setSUnit(SU);
561 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000562 }
563 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000564 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000565 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000566
567 AvailableQueue->updateNode(SU);
568 AvailableQueue->addNode(NewSU);
569
Evan Cheng1ec79b42007-09-27 07:09:03 +0000570 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000571 return NewSU;
572}
573
Evan Chengb2c42c62009-01-12 03:19:55 +0000574/// InsertCopiesAndMoveSuccs - Insert register copies and move all
575/// scheduled successors of the given SUnit to the last copy.
576void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
577 const TargetRegisterClass *DestRC,
578 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000579 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000580 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000581 CopyFromSU->CopySrcRC = SrcRC;
582 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000583
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000584 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000585 CopyToSU->CopySrcRC = DestRC;
586 CopyToSU->CopyDstRC = SrcRC;
587
588 // Only copy scheduled successors. Cut them from old node's successor
589 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000590 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000591 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
592 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000593 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000594 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000595 SUnit *SuccSU = I->getSUnit();
596 if (SuccSU->isScheduled) {
597 SDep D = *I;
598 D.setSUnit(CopyToSU);
599 AddPred(SuccSU, D);
600 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000601 }
602 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000603 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000604 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000605
Dan Gohman2d170892008-12-09 22:54:47 +0000606 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
607 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000608
609 AvailableQueue->updateNode(SU);
610 AvailableQueue->addNode(CopyFromSU);
611 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000612 Copies.push_back(CopyFromSU);
613 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000614
Evan Chengb2c42c62009-01-12 03:19:55 +0000615 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000616}
617
618/// getPhysicalRegisterVT - Returns the ValueType of the physical register
619/// definition of the specified node.
620/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000621static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000622 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000623 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000624 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000625 unsigned NumRes = TID.getNumDefs();
626 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000627 if (Reg == *ImpDef)
628 break;
629 ++NumRes;
630 }
631 return N->getValueType(NumRes);
632}
633
Evan Chengb8905c42009-03-04 01:41:49 +0000634/// CheckForLiveRegDef - Return true and update live register vector if the
635/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000636static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +0000637 std::vector<SUnit*> &LiveRegDefs,
638 SmallSet<unsigned, 4> &RegAdded,
639 SmallVector<unsigned, 4> &LRegs,
640 const TargetRegisterInfo *TRI) {
Evan Chengb8905c42009-03-04 01:41:49 +0000641 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
Chris Lattner0cfe8842010-12-20 00:51:56 +0000642 if (RegAdded.insert(Reg))
Evan Chengb8905c42009-03-04 01:41:49 +0000643 LRegs.push_back(Reg);
Evan Chengb8905c42009-03-04 01:41:49 +0000644 }
645 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
646 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Chris Lattner0cfe8842010-12-20 00:51:56 +0000647 if (RegAdded.insert(*Alias))
Evan Chengb8905c42009-03-04 01:41:49 +0000648 LRegs.push_back(*Alias);
Evan Chengb8905c42009-03-04 01:41:49 +0000649 }
Evan Chengb8905c42009-03-04 01:41:49 +0000650}
651
Evan Cheng5924bf72007-09-25 01:54:36 +0000652/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
653/// scheduling of the given node to satisfy live physical register dependencies.
654/// If the specific node is the last one that's available to schedule, do
655/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000656bool ScheduleDAGRRList::
657DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000658 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000659 return false;
660
Evan Chenge6f92252007-09-27 18:46:06 +0000661 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000662 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000663 //
664 // If SU is the currently live definition of the same register that it uses,
665 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +0000666 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
667 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000668 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +0000669 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
670 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000671 }
672
Dan Gohman072734e2008-11-13 23:24:17 +0000673 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000674 if (Node->getOpcode() == ISD::INLINEASM) {
675 // Inline asm can clobber physical defs.
676 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000677 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Evan Chengb8905c42009-03-04 01:41:49 +0000678 --NumOps; // Ignore the flag operand.
679
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000680 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000681 unsigned Flags =
682 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000683 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000684
685 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000686 if (InlineAsm::isRegDefKind(Flags) ||
687 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000688 // Check for def of register or earlyclobber register.
689 for (; NumVals; --NumVals, ++i) {
690 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
691 if (TargetRegisterInfo::isPhysicalRegister(Reg))
692 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
693 }
694 } else
695 i += NumVals;
696 }
697 continue;
698 }
699
Dan Gohman072734e2008-11-13 23:24:17 +0000700 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000701 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000702 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000703 if (!TID.ImplicitDefs)
704 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000705 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
706 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000707 }
Andrew Trick2085a962010-12-21 22:25:04 +0000708
Evan Cheng5924bf72007-09-25 01:54:36 +0000709 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000710}
711
Evan Cheng1ec79b42007-09-27 07:09:03 +0000712
Evan Chengd38c22b2006-05-11 23:55:42 +0000713/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
714/// schedulers.
715void ScheduleDAGRRList::ListScheduleBottomUp() {
716 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000717
718 // Release any predecessors of the special Exit node.
719 ReleasePredecessors(&ExitSU, CurCycle);
720
Evan Chengd38c22b2006-05-11 23:55:42 +0000721 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000722 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000723 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000724 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
725 RootSU->isAvailable = true;
726 AvailableQueue->push(RootSU);
727 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000728
729 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000730 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000731 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000732 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000733 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000734 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000735 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000736 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000737 SUnit *CurSU = AvailableQueue->pop();
738 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000739 SmallVector<unsigned, 4> LRegs;
740 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
741 break;
742 Delayed = true;
743 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000744
745 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
746 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000747 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000748 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000749
750 // All candidates are delayed due to live physical reg dependencies.
751 // Try backtracking, code duplication, or inserting cross class copies
752 // to resolve it.
753 if (Delayed && !CurSU) {
754 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
755 SUnit *TrySU = NotReady[i];
756 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
757
758 // Try unscheduling up to the point where it's safe to schedule
759 // this node.
760 unsigned LiveCycle = CurCycle;
761 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
762 unsigned Reg = LRegs[j];
763 unsigned LCycle = LiveRegCycles[Reg];
764 LiveCycle = std::min(LiveCycle, LCycle);
765 }
766 SUnit *OldSU = Sequence[LiveCycle];
767 if (!WillCreateCycle(TrySU, OldSU)) {
768 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
769 // Force the current node to be scheduled before the node that
770 // requires the physical reg dep.
771 if (OldSU->isAvailable) {
772 OldSU->isAvailable = false;
773 AvailableQueue->remove(OldSU);
774 }
Dan Gohman2d170892008-12-09 22:54:47 +0000775 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
776 /*Reg=*/0, /*isNormalMemory=*/false,
777 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000778 // If one or more successors has been unscheduled, then the current
779 // node is no longer avaialable. Schedule a successor that's now
780 // available instead.
781 if (!TrySU->isAvailable)
782 CurSU = AvailableQueue->pop();
783 else {
784 CurSU = TrySU;
785 TrySU->isPending = false;
786 NotReady.erase(NotReady.begin()+i);
787 }
788 break;
789 }
790 }
791
792 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000793 // Can't backtrack. If it's too expensive to copy the value, then try
794 // duplicate the nodes that produces these "too expensive to copy"
795 // values to break the dependency. In case even that doesn't work,
796 // insert cross class copies.
797 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000798 SUnit *TrySU = NotReady[0];
799 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
800 assert(LRegs.size() == 1 && "Can't handle this yet!");
801 unsigned Reg = LRegs[0];
802 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000803 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000804 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000805 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000806 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
807
808 // If cross copy register class is null, then it must be possible copy
809 // the value directly. Do not try duplicate the def.
810 SUnit *NewDef = 0;
811 if (DestRC)
812 NewDef = CopyAndMoveSuccessors(LRDef);
813 else
814 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000815 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000816 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000817 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000818 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000819 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000820 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000821 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000822 /*Reg=*/0, /*isNormalMemory=*/false,
823 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000824 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000825 NewDef = Copies.back();
826 }
827
Evan Chengbdd062d2010-05-20 06:13:19 +0000828 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000829 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000830 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000831 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000832 /*Reg=*/0, /*isNormalMemory=*/false,
833 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000834 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000835 TrySU->isAvailable = false;
836 CurSU = NewDef;
837 }
838
Dan Gohman60d68442009-01-29 19:49:27 +0000839 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000840 }
841
Evan Chengd38c22b2006-05-11 23:55:42 +0000842 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000843 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
844 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000845 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000846 if (NotReady[i]->isAvailable)
847 AvailableQueue->push(NotReady[i]);
848 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000849 NotReady.clear();
850
Dan Gohmanc602dd42008-11-21 00:10:42 +0000851 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000852 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000853 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000854 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000855 }
856
Evan Chengd38c22b2006-05-11 23:55:42 +0000857 // Reverse the order if it is bottom up.
858 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +0000859
Evan Chengd38c22b2006-05-11 23:55:42 +0000860#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000861 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000862#endif
863}
864
865//===----------------------------------------------------------------------===//
866// Top-Down Scheduling
867//===----------------------------------------------------------------------===//
868
869/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000870/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000871void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000872 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000873
Evan Chengd38c22b2006-05-11 23:55:42 +0000874#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000875 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000876 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000877 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000878 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000879 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000880 }
881#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000882 --SuccSU->NumPredsLeft;
883
Dan Gohmanb9543432009-02-10 23:27:53 +0000884 // If all the node's predecessors are scheduled, this node is ready
885 // to be scheduled. Ignore the special ExitSU node.
886 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000887 SuccSU->isAvailable = true;
888 AvailableQueue->push(SuccSU);
889 }
890}
891
Dan Gohmanb9543432009-02-10 23:27:53 +0000892void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
893 // Top down: release successors
894 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
895 I != E; ++I) {
896 assert(!I->isAssignedRegDep() &&
897 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
898
899 ReleaseSucc(SU, &*I);
900 }
901}
902
Evan Chengd38c22b2006-05-11 23:55:42 +0000903/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
904/// count of its successors. If a successor pending count is zero, add it to
905/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000906void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000907 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000908 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000909
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000910 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
911 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000912 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000913
Dan Gohmanb9543432009-02-10 23:27:53 +0000914 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000915 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000916 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000917}
918
Dan Gohman54a187e2007-08-20 19:28:38 +0000919/// ListScheduleTopDown - The main loop of list scheduling for top-down
920/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000921void ScheduleDAGRRList::ListScheduleTopDown() {
922 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000923 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000924
Dan Gohmanb9543432009-02-10 23:27:53 +0000925 // Release any successors of the special Entry node.
926 ReleaseSuccessors(&EntrySU);
927
Evan Chengd38c22b2006-05-11 23:55:42 +0000928 // All leaves to Available queue.
929 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
930 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000931 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000932 AvailableQueue->push(&SUnits[i]);
933 SUnits[i].isAvailable = true;
934 }
935 }
Andrew Trick2085a962010-12-21 22:25:04 +0000936
Evan Chengd38c22b2006-05-11 23:55:42 +0000937 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000938 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000939 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000940 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000941 SUnit *CurSU = AvailableQueue->pop();
Andrew Trick2085a962010-12-21 22:25:04 +0000942
Dan Gohmanc602dd42008-11-21 00:10:42 +0000943 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000944 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000945 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000946 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000947 }
Andrew Trick2085a962010-12-21 22:25:04 +0000948
Evan Chengd38c22b2006-05-11 23:55:42 +0000949#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000950 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000951#endif
952}
953
954
Evan Chengd38c22b2006-05-11 23:55:42 +0000955//===----------------------------------------------------------------------===//
956// RegReductionPriorityQueue Implementation
957//===----------------------------------------------------------------------===//
958//
959// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
960// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +0000961//
Evan Chengd38c22b2006-05-11 23:55:42 +0000962namespace {
963 template<class SF>
964 class RegReductionPriorityQueue;
Andrew Trick2085a962010-12-21 22:25:04 +0000965
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000966 /// bu_ls_rr_sort - Priority function for bottom up register pressure
967 // reduction scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000968 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
969 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
970 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
971 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +0000972
Evan Chengd38c22b2006-05-11 23:55:42 +0000973 bool operator()(const SUnit* left, const SUnit* right) const;
974 };
975
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000976 // td_ls_rr_sort - Priority function for top down register pressure reduction
977 // scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000978 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
979 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
980 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
981 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +0000982
Evan Chengd38c22b2006-05-11 23:55:42 +0000983 bool operator()(const SUnit* left, const SUnit* right) const;
984 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000985
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000986 // src_ls_rr_sort - Priority function for source order scheduler.
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000987 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
988 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
989 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
990 : SPQ(spq) {}
991 src_ls_rr_sort(const src_ls_rr_sort &RHS)
992 : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +0000993
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000994 bool operator()(const SUnit* left, const SUnit* right) const;
995 };
Evan Chengbdd062d2010-05-20 06:13:19 +0000996
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000997 // hybrid_ls_rr_sort - Priority function for hybrid scheduler.
Evan Chengbdd062d2010-05-20 06:13:19 +0000998 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
999 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
1000 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
1001 : SPQ(spq) {}
1002 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1003 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001004
Evan Chengbdd062d2010-05-20 06:13:19 +00001005 bool operator()(const SUnit* left, const SUnit* right) const;
1006 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001007
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001008 // ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1009 // scheduler.
Evan Cheng37b740c2010-07-24 00:39:05 +00001010 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1011 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1012 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1013 : SPQ(spq) {}
1014 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1015 : SPQ(RHS.SPQ) {}
1016
1017 bool operator()(const SUnit* left, const SUnit* right) const;
1018 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001019} // end anonymous namespace
1020
Dan Gohman186f65d2008-11-20 03:30:37 +00001021/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1022/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001023static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001024CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001025 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1026 if (SethiUllmanNumber != 0)
1027 return SethiUllmanNumber;
1028
1029 unsigned Extra = 0;
1030 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1031 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001032 if (I->isCtrl()) continue; // ignore chain preds
1033 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001034 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001035 if (PredSethiUllman > SethiUllmanNumber) {
1036 SethiUllmanNumber = PredSethiUllman;
1037 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001038 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001039 ++Extra;
1040 }
1041
1042 SethiUllmanNumber += Extra;
1043
1044 if (SethiUllmanNumber == 0)
1045 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001046
Evan Cheng7e4abde2008-07-02 09:23:51 +00001047 return SethiUllmanNumber;
1048}
1049
Evan Chengd38c22b2006-05-11 23:55:42 +00001050namespace {
1051 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001052 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001053 std::vector<SUnit*> Queue;
1054 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001055 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001056 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001057
Dan Gohman3f656df2008-11-20 02:45:51 +00001058 protected:
1059 // SUnits - The SUnits for the current graph.
1060 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001061
1062 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001063 const TargetInstrInfo *TII;
1064 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001065 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001066 ScheduleDAGRRList *scheduleDAG;
1067
Dan Gohman186f65d2008-11-20 03:30:37 +00001068 // SethiUllmanNumbers - The SethiUllman number for each node.
1069 std::vector<unsigned> SethiUllmanNumbers;
1070
Evan Chenga77f3d32010-07-21 06:09:07 +00001071 /// RegPressure - Tracking current reg pressure per register class.
1072 ///
Evan Cheng28590382010-07-21 23:53:58 +00001073 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001074
1075 /// RegLimit - Tracking the number of allocatable registers per register
1076 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001077 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001078
Dan Gohman3f656df2008-11-20 02:45:51 +00001079 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001080 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001081 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001082 const TargetInstrInfo *tii,
1083 const TargetRegisterInfo *tri,
1084 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001085 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001086 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001087 if (TracksRegPressure) {
1088 unsigned NumRC = TRI->getNumRegClasses();
1089 RegLimit.resize(NumRC);
1090 RegPressure.resize(NumRC);
1091 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1092 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1093 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1094 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001095 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001096 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001097 }
Andrew Trick2085a962010-12-21 22:25:04 +00001098
Dan Gohman3f656df2008-11-20 02:45:51 +00001099 void initNodes(std::vector<SUnit> &sunits) {
1100 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001101 // Add pseudo dependency edges for two-address nodes.
1102 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001103 // Reroute edges to nodes with multiple uses.
1104 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001105 // Calculate node priorities.
1106 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001107 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001108
Dan Gohman186f65d2008-11-20 03:30:37 +00001109 void addNode(const SUnit *SU) {
1110 unsigned SUSize = SethiUllmanNumbers.size();
1111 if (SUnits->size() > SUSize)
1112 SethiUllmanNumbers.resize(SUSize*2, 0);
1113 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1114 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001115
Dan Gohman186f65d2008-11-20 03:30:37 +00001116 void updateNode(const SUnit *SU) {
1117 SethiUllmanNumbers[SU->NodeNum] = 0;
1118 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1119 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001120
Dan Gohman186f65d2008-11-20 03:30:37 +00001121 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001122 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001123 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001124 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001125 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001126
1127 unsigned getNodePriority(const SUnit *SU) const {
1128 assert(SU->NodeNum < SethiUllmanNumbers.size());
1129 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001130 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001131 // CopyToReg should be close to its uses to facilitate coalescing and
1132 // avoid spilling.
1133 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001134 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1135 Opc == TargetOpcode::SUBREG_TO_REG ||
1136 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001137 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1138 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001139 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001140 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1141 // If SU does not have a register use, i.e. it doesn't produce a value
1142 // that would be consumed (e.g. store), then it terminates a chain of
1143 // computation. Give it a large SethiUllman number so it will be
1144 // scheduled right before its predecessors that it doesn't lengthen
1145 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001146 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001147 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1148 // If SU does not have a register def, schedule it close to its uses
1149 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001150 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001151 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001152 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001153
1154 unsigned getNodeOrdering(const SUnit *SU) const {
1155 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1156 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001157
Evan Chengd38c22b2006-05-11 23:55:42 +00001158 bool empty() const { return Queue.empty(); }
Andrew Trick2085a962010-12-21 22:25:04 +00001159
Evan Chengd38c22b2006-05-11 23:55:42 +00001160 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001161 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001162 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001163 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001164 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001165
Evan Chengd38c22b2006-05-11 23:55:42 +00001166 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001167 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001168 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001169 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001170 E = Queue.end(); I != E; ++I)
1171 if (Picker(*Best, *I))
1172 Best = I;
1173 SUnit *V = *Best;
1174 if (Best != prior(Queue.end()))
1175 std::swap(*Best, Queue.back());
1176 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001177 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001178 return V;
1179 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001180
Evan Cheng5924bf72007-09-25 01:54:36 +00001181 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001182 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001183 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001184 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1185 SU);
1186 if (I != prior(Queue.end()))
1187 std::swap(*I, Queue.back());
1188 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001189 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001190 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001191
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001192 bool HighRegPressure(const SUnit *SU) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001193 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001194 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001195
Evan Chenga77f3d32010-07-21 06:09:07 +00001196 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1197 I != E; ++I) {
1198 if (I->isCtrl())
1199 continue;
1200 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001201 const SDNode *PN = PredSU->getNode();
1202 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001203 if (PN->getOpcode() == ISD::CopyFromReg) {
1204 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001205 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1206 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001207 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1208 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001209 }
1210 continue;
1211 }
1212 unsigned POpc = PN->getMachineOpcode();
1213 if (POpc == TargetOpcode::IMPLICIT_DEF)
1214 continue;
1215 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1216 EVT VT = PN->getOperand(0).getValueType();
1217 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1218 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1219 // Check if this increases register pressure of the specific register
1220 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001221 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1222 return true;
Andrew Trick2085a962010-12-21 22:25:04 +00001223 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001224 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1225 POpc == TargetOpcode::SUBREG_TO_REG) {
1226 EVT VT = PN->getValueType(0);
1227 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1228 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1229 // Check if this increases register pressure of the specific register
1230 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001231 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1232 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001233 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001234 }
1235 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001236 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001237 EVT VT = PN->getValueType(i);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001238 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1239 if (RegPressure[RCId] >= RegLimit[RCId])
1240 return true; // Reg pressure already high.
1241 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Cheng28590382010-07-21 23:53:58 +00001242 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001243 continue;
Evan Chenga77f3d32010-07-21 06:09:07 +00001244 // Check if this increases register pressure of the specific register
1245 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001246 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1247 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001248 }
1249 }
1250
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001251 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001252 }
1253
Evan Chengbf32e542010-07-22 06:24:48 +00001254 void ScheduledNode(SUnit *SU) {
1255 if (!TracksRegPressure)
1256 return;
1257
Evan Chenga77f3d32010-07-21 06:09:07 +00001258 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001259 if (!N->isMachineOpcode()) {
1260 if (N->getOpcode() != ISD::CopyToReg)
1261 return;
1262 } else {
1263 unsigned Opc = N->getMachineOpcode();
1264 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1265 Opc == TargetOpcode::INSERT_SUBREG ||
1266 Opc == TargetOpcode::SUBREG_TO_REG ||
1267 Opc == TargetOpcode::REG_SEQUENCE ||
1268 Opc == TargetOpcode::IMPLICIT_DEF)
1269 return;
1270 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001271
1272 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1273 I != E; ++I) {
1274 if (I->isCtrl())
1275 continue;
1276 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001277 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001278 continue;
1279 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001280 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001281 if (PN->getOpcode() == ISD::CopyFromReg) {
1282 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001283 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1284 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1285 }
1286 continue;
1287 }
1288 unsigned POpc = PN->getMachineOpcode();
1289 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001290 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001291 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1292 EVT VT = PN->getOperand(0).getValueType();
1293 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1294 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001295 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001296 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1297 POpc == TargetOpcode::SUBREG_TO_REG) {
1298 EVT VT = PN->getValueType(0);
1299 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1300 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1301 continue;
1302 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001303 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1304 for (unsigned i = 0; i != NumDefs; ++i) {
1305 EVT VT = PN->getValueType(i);
1306 if (!PN->hasAnyUseOfValue(i))
1307 continue;
1308 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1309 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1310 }
1311 }
1312
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001313 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1314 // may transfer data dependencies to CopyToReg.
1315 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001316 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1317 for (unsigned i = 0; i != NumDefs; ++i) {
1318 EVT VT = N->getValueType(i);
1319 if (!N->hasAnyUseOfValue(i))
1320 continue;
1321 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1322 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1323 // Register pressure tracking is imprecise. This can happen.
1324 RegPressure[RCId] = 0;
1325 else
1326 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1327 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001328 }
Evan Chengbf32e542010-07-22 06:24:48 +00001329
1330 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001331 }
1332
Evan Chengbf32e542010-07-22 06:24:48 +00001333 void UnscheduledNode(SUnit *SU) {
1334 if (!TracksRegPressure)
1335 return;
1336
Evan Chenga77f3d32010-07-21 06:09:07 +00001337 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001338 if (!N->isMachineOpcode()) {
1339 if (N->getOpcode() != ISD::CopyToReg)
1340 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001341 } else {
1342 unsigned Opc = N->getMachineOpcode();
1343 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1344 Opc == TargetOpcode::INSERT_SUBREG ||
1345 Opc == TargetOpcode::SUBREG_TO_REG ||
1346 Opc == TargetOpcode::REG_SEQUENCE ||
1347 Opc == TargetOpcode::IMPLICIT_DEF)
1348 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001349 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001350
1351 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1352 I != E; ++I) {
1353 if (I->isCtrl())
1354 continue;
1355 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001356 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001357 continue;
1358 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001359 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001360 if (PN->getOpcode() == ISD::CopyFromReg) {
1361 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001362 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1363 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1364 }
1365 continue;
1366 }
1367 unsigned POpc = PN->getMachineOpcode();
1368 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001369 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001370 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1371 EVT VT = PN->getOperand(0).getValueType();
1372 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1373 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001374 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001375 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1376 POpc == TargetOpcode::SUBREG_TO_REG) {
1377 EVT VT = PN->getValueType(0);
1378 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1379 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1380 continue;
1381 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001382 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1383 for (unsigned i = 0; i != NumDefs; ++i) {
1384 EVT VT = PN->getValueType(i);
1385 if (!PN->hasAnyUseOfValue(i))
1386 continue;
1387 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001388 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001389 // Register pressure tracking is imprecise. This can happen.
1390 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001391 else
1392 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001393 }
1394 }
1395
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001396 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1397 // may transfer data dependencies to CopyToReg.
1398 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001399 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1400 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1401 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001402 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengdf907f42010-07-23 22:39:59 +00001403 continue;
1404 if (!N->hasAnyUseOfValue(i))
1405 continue;
1406 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1407 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1408 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001409 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001410
Evan Chenga77f3d32010-07-21 06:09:07 +00001411 dumpRegPressure();
1412 }
1413
Andrew Trick2085a962010-12-21 22:25:04 +00001414 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1415 scheduleDAG = scheduleDag;
Dan Gohman3f656df2008-11-20 02:45:51 +00001416 }
1417
Evan Chenga77f3d32010-07-21 06:09:07 +00001418 void dumpRegPressure() const {
1419 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1420 E = TRI->regclass_end(); I != E; ++I) {
1421 const TargetRegisterClass *RC = *I;
1422 unsigned Id = RC->getID();
1423 unsigned RP = RegPressure[Id];
1424 if (!RP) continue;
1425 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1426 << '\n');
1427 }
1428 }
1429
Dan Gohman3f656df2008-11-20 02:45:51 +00001430 protected:
1431 bool canClobber(const SUnit *SU, const SUnit *Op);
1432 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001433 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001434 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001435 };
1436
Dan Gohman186f65d2008-11-20 03:30:37 +00001437 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1438 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001439
Dan Gohman186f65d2008-11-20 03:30:37 +00001440 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1441 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001442
1443 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1444 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001445
1446 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1447 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001448
1449 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1450 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001451}
1452
Evan Chengb9e3db62007-03-14 22:43:40 +00001453/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001454/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001455static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001456 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001457 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001458 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001459 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001460 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001461 // If there are bunch of CopyToRegs stacked up, they should be considered
1462 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001463 if (I->getSUnit()->getNode() &&
1464 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001465 Height = closestSucc(I->getSUnit())+1;
1466 if (Height > MaxHeight)
1467 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001468 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001469 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001470}
1471
Evan Cheng61bc51e2007-12-20 02:22:36 +00001472/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001473/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001474static unsigned calcMaxScratches(const SUnit *SU) {
1475 unsigned Scratches = 0;
1476 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001477 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001478 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001479 Scratches++;
1480 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001481 return Scratches;
1482}
1483
Evan Cheng6c1414f2010-10-29 18:09:28 +00001484/// hasOnlyLiveOutUse - Return true if SU has a single value successor that is a
1485/// CopyToReg to a virtual register. This SU def is probably a liveout and
1486/// it has no other use. It should be scheduled closer to the terminator.
1487static bool hasOnlyLiveOutUses(const SUnit *SU) {
1488 bool RetVal = false;
1489 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1490 I != E; ++I) {
1491 if (I->isCtrl()) continue;
1492 const SUnit *SuccSU = I->getSUnit();
1493 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
1494 unsigned Reg =
1495 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
1496 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1497 RetVal = true;
1498 continue;
1499 }
1500 }
1501 return false;
1502 }
1503 return RetVal;
1504}
1505
1506/// UnitsSharePred - Return true if the two scheduling units share a common
1507/// data predecessor.
1508static bool UnitsSharePred(const SUnit *left, const SUnit *right) {
1509 SmallSet<const SUnit*, 4> Preds;
1510 for (SUnit::const_pred_iterator I = left->Preds.begin(),E = left->Preds.end();
1511 I != E; ++I) {
1512 if (I->isCtrl()) continue; // ignore chain preds
1513 Preds.insert(I->getSUnit());
1514 }
1515 for (SUnit::const_pred_iterator I = right->Preds.begin(),E = right->Preds.end();
1516 I != E; ++I) {
1517 if (I->isCtrl()) continue; // ignore chain preds
1518 if (Preds.count(I->getSUnit()))
1519 return true;
1520 }
1521 return false;
1522}
1523
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001524template <typename RRSort>
1525static bool BURRSort(const SUnit *left, const SUnit *right,
1526 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001527 unsigned LPriority = SPQ->getNodePriority(left);
1528 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001529 if (LPriority != RPriority)
1530 return LPriority > RPriority;
1531
1532 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1533 // e.g.
1534 // t1 = op t2, c1
1535 // t3 = op t4, c2
1536 //
1537 // and the following instructions are both ready.
1538 // t2 = op c3
1539 // t4 = op c4
1540 //
1541 // Then schedule t2 = op first.
1542 // i.e.
1543 // t4 = op c4
1544 // t2 = op c3
1545 // t1 = op t2, c1
1546 // t3 = op t4, c2
1547 //
1548 // This creates more short live intervals.
1549 unsigned LDist = closestSucc(left);
1550 unsigned RDist = closestSucc(right);
1551 if (LDist != RDist)
1552 return LDist < RDist;
1553
Evan Cheng3a14efa2009-02-12 08:59:45 +00001554 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001555 unsigned LScratch = calcMaxScratches(left);
1556 unsigned RScratch = calcMaxScratches(right);
1557 if (LScratch != RScratch)
1558 return LScratch > RScratch;
1559
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001560 if (left->getHeight() != right->getHeight())
1561 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00001562
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001563 if (left->getDepth() != right->getDepth())
1564 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001565
Andrew Trick2085a962010-12-21 22:25:04 +00001566 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00001567 "NodeQueueId cannot be zero");
1568 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001569}
1570
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001571// Bottom up
1572bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1573 return BURRSort(left, right, SPQ);
1574}
1575
1576// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001577bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001578 unsigned LOrder = SPQ->getNodeOrdering(left);
1579 unsigned ROrder = SPQ->getNodeOrdering(right);
1580
1581 // Prefer an ordering where the lower the non-zero order number, the higher
1582 // the preference.
1583 if ((LOrder || ROrder) && LOrder != ROrder)
1584 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1585
1586 return BURRSort(left, right, SPQ);
1587}
1588
Evan Chengbdd062d2010-05-20 06:13:19 +00001589bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdebf9c52010-11-03 00:45:17 +00001590 if (left->isCall || right->isCall)
1591 // No way to compute latency of calls.
1592 return BURRSort(left, right, SPQ);
1593
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001594 bool LHigh = SPQ->HighRegPressure(left);
1595 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001596 // Avoid causing spills. If register pressure is high, schedule for
1597 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001598 if (LHigh && !RHigh)
1599 return true;
1600 else if (!LHigh && RHigh)
1601 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001602 else if (!LHigh && !RHigh) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001603 // If the two nodes share an operand and one of them has a single
1604 // use that is a live out copy, favor the one that is live out. Otherwise
1605 // it will be difficult to eliminate the copy if the instruction is a
1606 // loop induction variable update. e.g.
1607 // BB:
1608 // sub r1, r3, #1
1609 // str r0, [r2, r3]
1610 // mov r3, r1
1611 // cmp
1612 // bne BB
1613 bool SharePred = UnitsSharePred(left, right);
1614 // FIXME: Only adjust if BB is a loop back edge.
1615 // FIXME: What's the cost of a copy?
1616 int LBonus = (SharePred && hasOnlyLiveOutUses(left)) ? 1 : 0;
1617 int RBonus = (SharePred && hasOnlyLiveOutUses(right)) ? 1 : 0;
1618 int LHeight = (int)left->getHeight() - LBonus;
1619 int RHeight = (int)right->getHeight() - RBonus;
1620
Evan Cheng28590382010-07-21 23:53:58 +00001621 // Low register pressure situation, schedule for latency if possible.
1622 bool LStall = left->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001623 (int)SPQ->getCurCycle() < LHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001624 bool RStall = right->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001625 (int)SPQ->getCurCycle() < RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001626 // If scheduling one of the node will cause a pipeline stall, delay it.
1627 // If scheduling either one of the node will cause a pipeline stall, sort
1628 // them according to their height.
Evan Cheng28590382010-07-21 23:53:58 +00001629 if (LStall) {
1630 if (!RStall)
1631 return true;
Evan Cheng6c1414f2010-10-29 18:09:28 +00001632 if (LHeight != RHeight)
1633 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001634 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001635 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001636
Evan Cheng6c1414f2010-10-29 18:09:28 +00001637 // If either node is scheduling for latency, sort them by height
1638 // and latency.
Evan Cheng28590382010-07-21 23:53:58 +00001639 if (left->SchedulingPref == Sched::Latency ||
1640 right->SchedulingPref == Sched::Latency) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001641 if (LHeight != RHeight)
1642 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001643 if (left->Latency != right->Latency)
1644 return left->Latency > right->Latency;
1645 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001646 }
1647
Evan Chengbdd062d2010-05-20 06:13:19 +00001648 return BURRSort(left, right, SPQ);
1649}
1650
Evan Cheng37b740c2010-07-24 00:39:05 +00001651bool ilp_ls_rr_sort::operator()(const SUnit *left,
1652 const SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001653 if (left->isCall || right->isCall)
1654 // No way to compute latency of calls.
1655 return BURRSort(left, right, SPQ);
1656
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001657 bool LHigh = SPQ->HighRegPressure(left);
1658 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001659 // Avoid causing spills. If register pressure is high, schedule for
1660 // register pressure reduction.
1661 if (LHigh && !RHigh)
1662 return true;
1663 else if (!LHigh && RHigh)
1664 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001665 else if (!LHigh && !RHigh) {
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001666 // Low register pressure situation, schedule to maximize instruction level
1667 // parallelism.
Evan Cheng37b740c2010-07-24 00:39:05 +00001668 if (left->NumPreds > right->NumPreds)
1669 return false;
1670 else if (left->NumPreds < right->NumPreds)
1671 return false;
1672 }
1673
1674 return BURRSort(left, right, SPQ);
1675}
1676
Dan Gohman3f656df2008-11-20 02:45:51 +00001677template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001678bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001679RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001680 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001681 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001682 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001683 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001684 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001685 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001686 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001687 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001688 if (DU->getNodeId() != -1 &&
1689 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001690 return true;
1691 }
1692 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001693 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001694 return false;
1695}
1696
Evan Chengf9891412007-12-20 09:25:31 +00001697/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001698/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001699static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001700 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001701 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001702 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001703 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1704 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001705 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001706 for (const SDNode *SUNode = SU->getNode(); SUNode;
1707 SUNode = SUNode->getFlaggedNode()) {
1708 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001709 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001710 const unsigned *SUImpDefs =
1711 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1712 if (!SUImpDefs)
1713 return false;
1714 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001715 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001716 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001717 continue;
1718 if (!N->hasAnyUseOfValue(i))
1719 continue;
1720 unsigned Reg = ImpDefs[i - NumDefs];
1721 for (;*SUImpDefs; ++SUImpDefs) {
1722 unsigned SUReg = *SUImpDefs;
1723 if (TRI->regsOverlap(Reg, SUReg))
1724 return true;
1725 }
Evan Chengf9891412007-12-20 09:25:31 +00001726 }
1727 }
1728 return false;
1729}
1730
Dan Gohman9a658d72009-03-24 00:49:12 +00001731/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1732/// are not handled well by the general register pressure reduction
1733/// heuristics. When presented with code like this:
1734///
1735/// N
1736/// / |
1737/// / |
1738/// U store
1739/// |
1740/// ...
1741///
1742/// the heuristics tend to push the store up, but since the
1743/// operand of the store has another use (U), this would increase
1744/// the length of that other use (the U->N edge).
1745///
1746/// This function transforms code like the above to route U's
1747/// dependence through the store when possible, like this:
1748///
1749/// N
1750/// ||
1751/// ||
1752/// store
1753/// |
1754/// U
1755/// |
1756/// ...
1757///
1758/// This results in the store being scheduled immediately
1759/// after N, which shortens the U->N live range, reducing
1760/// register pressure.
1761///
1762template<class SF>
1763void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1764 // Visit all the nodes in topological order, working top-down.
1765 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1766 SUnit *SU = &(*SUnits)[i];
1767 // For now, only look at nodes with no data successors, such as stores.
1768 // These are especially important, due to the heuristics in
1769 // getNodePriority for nodes with no data successors.
1770 if (SU->NumSuccs != 0)
1771 continue;
1772 // For now, only look at nodes with exactly one data predecessor.
1773 if (SU->NumPreds != 1)
1774 continue;
1775 // Avoid prescheduling copies to virtual registers, which don't behave
1776 // like other nodes from the perspective of scheduling heuristics.
1777 if (SDNode *N = SU->getNode())
1778 if (N->getOpcode() == ISD::CopyToReg &&
1779 TargetRegisterInfo::isVirtualRegister
1780 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1781 continue;
1782
1783 // Locate the single data predecessor.
1784 SUnit *PredSU = 0;
1785 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1786 EE = SU->Preds.end(); II != EE; ++II)
1787 if (!II->isCtrl()) {
1788 PredSU = II->getSUnit();
1789 break;
1790 }
1791 assert(PredSU);
1792
1793 // Don't rewrite edges that carry physregs, because that requires additional
1794 // support infrastructure.
1795 if (PredSU->hasPhysRegDefs)
1796 continue;
1797 // Short-circuit the case where SU is PredSU's only data successor.
1798 if (PredSU->NumSuccs == 1)
1799 continue;
1800 // Avoid prescheduling to copies from virtual registers, which don't behave
1801 // like other nodes from the perspective of scheduling // heuristics.
1802 if (SDNode *N = SU->getNode())
1803 if (N->getOpcode() == ISD::CopyFromReg &&
1804 TargetRegisterInfo::isVirtualRegister
1805 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1806 continue;
1807
1808 // Perform checks on the successors of PredSU.
1809 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1810 EE = PredSU->Succs.end(); II != EE; ++II) {
1811 SUnit *PredSuccSU = II->getSUnit();
1812 if (PredSuccSU == SU) continue;
1813 // If PredSU has another successor with no data successors, for
1814 // now don't attempt to choose either over the other.
1815 if (PredSuccSU->NumSuccs == 0)
1816 goto outer_loop_continue;
1817 // Don't break physical register dependencies.
1818 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1819 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1820 goto outer_loop_continue;
1821 // Don't introduce graph cycles.
1822 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1823 goto outer_loop_continue;
1824 }
1825
1826 // Ok, the transformation is safe and the heuristics suggest it is
1827 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001828 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1829 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001830 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001831 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1832 SDep Edge = PredSU->Succs[i];
1833 assert(!Edge.isAssignedRegDep());
1834 SUnit *SuccSU = Edge.getSUnit();
1835 if (SuccSU != SU) {
1836 Edge.setSUnit(PredSU);
1837 scheduleDAG->RemovePred(SuccSU, Edge);
1838 scheduleDAG->AddPred(SU, Edge);
1839 Edge.setSUnit(SU);
1840 scheduleDAG->AddPred(SuccSU, Edge);
1841 --i;
1842 }
1843 }
1844 outer_loop_continue:;
1845 }
1846}
1847
Evan Chengd38c22b2006-05-11 23:55:42 +00001848/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1849/// it as a def&use operand. Add a pseudo control edge from it to the other
1850/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001851/// first (lower in the schedule). If both nodes are two-address, favor the
1852/// one that has a CopyToReg use (more likely to be a loop induction update).
1853/// If both are two-address, but one is commutable while the other is not
1854/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001855template<class SF>
1856void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001857 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001858 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001859 if (!SU->isTwoAddress)
1860 continue;
1861
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001862 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001863 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001864 continue;
1865
Evan Cheng6c1414f2010-10-29 18:09:28 +00001866 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00001867 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001868 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001869 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001870 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001871 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001872 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1873 continue;
1874 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1875 if (DU->getNodeId() == -1)
1876 continue;
1877 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1878 if (!DUSU) continue;
1879 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1880 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001881 if (I->isCtrl()) continue;
1882 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001883 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001884 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001885 // Be conservative. Ignore if nodes aren't at roughly the same
1886 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001887 if (SuccSU->getHeight() < SU->getHeight() &&
1888 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001889 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001890 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1891 // constrains whatever is using the copy, instead of the copy
1892 // itself. In the case that the copy is coalesced, this
1893 // preserves the intent of the pseudo two-address heurietics.
1894 while (SuccSU->Succs.size() == 1 &&
1895 SuccSU->getNode()->isMachineOpcode() &&
1896 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001897 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001898 SuccSU = SuccSU->Succs.front().getSUnit();
1899 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001900 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1901 continue;
1902 // Don't constrain nodes with physical register defs if the
1903 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001904 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001905 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001906 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001907 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001908 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1909 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001910 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001911 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1912 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1913 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001914 continue;
1915 if ((!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00001916 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00001917 (!SU->isCommutable && SuccSU->isCommutable)) &&
1918 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001919 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001920 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001921 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001922 /*Reg=*/0, /*isNormalMemory=*/false,
1923 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001924 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001925 }
1926 }
1927 }
1928 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001929}
1930
Evan Cheng6730f032007-01-08 23:55:53 +00001931/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1932/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001933template<class SF>
1934void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001935 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick2085a962010-12-21 22:25:04 +00001936
Evan Chengd38c22b2006-05-11 23:55:42 +00001937 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001938 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001939}
Evan Chengd38c22b2006-05-11 23:55:42 +00001940
Roman Levenstein30d09512008-03-27 09:44:37 +00001941/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001942/// predecessors of the successors of the SUnit SU. Stop when the provided
1943/// limit is exceeded.
Andrew Trick2085a962010-12-21 22:25:04 +00001944static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
Roman Levensteinbc674502008-03-27 09:14:57 +00001945 unsigned Limit) {
1946 unsigned Sum = 0;
1947 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1948 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001949 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001950 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1951 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001952 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001953 if (!PredSU->isScheduled)
1954 if (++Sum > Limit)
1955 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001956 }
1957 }
1958 return Sum;
1959}
1960
Evan Chengd38c22b2006-05-11 23:55:42 +00001961
1962// Top down
1963bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001964 unsigned LPriority = SPQ->getNodePriority(left);
1965 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001966 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1967 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001968 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1969 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001970 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1971 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001972
1973 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1974 return false;
1975 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1976 return true;
1977
Evan Chengd38c22b2006-05-11 23:55:42 +00001978 if (LIsFloater)
1979 LBonus -= 2;
1980 if (RIsFloater)
1981 RBonus -= 2;
1982 if (left->NumSuccs == 1)
1983 LBonus += 2;
1984 if (right->NumSuccs == 1)
1985 RBonus += 2;
1986
Evan Cheng73bdf042008-03-01 00:39:47 +00001987 if (LPriority+LBonus != RPriority+RBonus)
1988 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001989
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001990 if (left->getDepth() != right->getDepth())
1991 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001992
1993 if (left->NumSuccsLeft != right->NumSuccsLeft)
1994 return left->NumSuccsLeft > right->NumSuccsLeft;
1995
Andrew Trick2085a962010-12-21 22:25:04 +00001996 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00001997 "NodeQueueId cannot be zero");
1998 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001999}
2000
Evan Chengd38c22b2006-05-11 23:55:42 +00002001//===----------------------------------------------------------------------===//
2002// Public Constructor Functions
2003//===----------------------------------------------------------------------===//
2004
Dan Gohmandfaf6462009-02-11 04:27:20 +00002005llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002006llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002007 const TargetMachine &TM = IS->TM;
2008 const TargetInstrInfo *TII = TM.getInstrInfo();
2009 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002010
Evan Chenga77f3d32010-07-21 06:09:07 +00002011 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002012 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002013 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002014 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002015 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002016}
2017
Dan Gohmandfaf6462009-02-11 04:27:20 +00002018llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002019llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002020 const TargetMachine &TM = IS->TM;
2021 const TargetInstrInfo *TII = TM.getInstrInfo();
2022 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002023
Evan Chenga77f3d32010-07-21 06:09:07 +00002024 TDRegReductionPriorityQueue *PQ =
2025 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002026 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00002027 PQ->setScheduleDAG(SD);
2028 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002029}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002030
2031llvm::ScheduleDAGSDNodes *
2032llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2033 const TargetMachine &TM = IS->TM;
2034 const TargetInstrInfo *TII = TM.getInstrInfo();
2035 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002036
Evan Chenga77f3d32010-07-21 06:09:07 +00002037 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002038 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002039 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
2040 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002041 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00002042}
2043
2044llvm::ScheduleDAGSDNodes *
2045llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2046 const TargetMachine &TM = IS->TM;
2047 const TargetInstrInfo *TII = TM.getInstrInfo();
2048 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002049 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002050
Evan Chenga77f3d32010-07-21 06:09:07 +00002051 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002052 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002053 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002054 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002055 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002056}
Evan Cheng37b740c2010-07-24 00:39:05 +00002057
2058llvm::ScheduleDAGSDNodes *
2059llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2060 const TargetMachine &TM = IS->TM;
2061 const TargetInstrInfo *TII = TM.getInstrInfo();
2062 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2063 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002064
Evan Cheng37b740c2010-07-24 00:39:05 +00002065 ILPBURRPriorityQueue *PQ =
2066 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2067 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2068 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002069 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00002070}