blob: 793fa3d5998eb46a5fd940f39a684a5ade57e35d [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
Andrew Trick033efdf2010-12-23 03:15:51 +0000250/// Call ReleasePred for each predecessor, then update register live def/gen.
251/// Always update LiveRegDefs for a register dependence even if the current SU
252/// also defines the register. This effectively create one large live range
253/// across a sequence of two-address node. This is important because the
254/// entire chain must be scheduled together. Example:
255///
256/// flags = (3) add
257/// flags = (2) addc flags
258/// flags = (1) addc flags
259///
260/// results in
261///
262/// LiveRegDefs[flags] = 3
263/// LiveRegCycles[flags] = 1
264///
265/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
266/// interference on flags.
Dan Gohmanb9543432009-02-10 23:27:53 +0000267void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000268 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000269 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000270 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000271 ReleasePred(SU, &*I);
272 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000273 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000274 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000275 // clobber the register is scheduled between the predecessor and
276 // this node.
Andrew Trick033efdf2010-12-23 03:15:51 +0000277 SUnit *&RegDef = LiveRegDefs[I->getReg()];
278 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
279 "interference on register dependence");
280 RegDef = I->getSUnit();
281 if (!LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000282 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000283 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000284 }
285 }
286 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000287}
288
289/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
290/// count of its predecessors. If a predecessor pending count is zero, add it to
291/// the Available queue.
292void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000293 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000294 DEBUG(SU->dump(this));
295
Evan Chengbdd062d2010-05-20 06:13:19 +0000296#ifndef NDEBUG
297 if (CurCycle < SU->getHeight())
298 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
299#endif
300
301 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000302 SU->setHeightToAtLeast(CurCycle);
303 Sequence.push_back(SU);
304
Evan Cheng28590382010-07-21 23:53:58 +0000305 AvailableQueue->ScheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000306
Andrew Trick033efdf2010-12-23 03:15:51 +0000307 // Update liveness of predecessors before successors to avoid treating a
308 // two-address node as a live range def.
Dan Gohmanb9543432009-02-10 23:27:53 +0000309 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000310
311 // Release all the implicit physical register defs that are live.
312 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
313 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000314 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
315 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
316 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
317 --NumLiveRegs;
318 LiveRegDefs[I->getReg()] = NULL;
319 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000320 }
321 }
322
Evan Chengd38c22b2006-05-11 23:55:42 +0000323 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000324}
325
Evan Cheng5924bf72007-09-25 01:54:36 +0000326/// CapturePred - This does the opposite of ReleasePred. Since SU is being
327/// unscheduled, incrcease the succ left count of its predecessors. Remove
328/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000329void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000330 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000331 if (PredSU->isAvailable) {
332 PredSU->isAvailable = false;
333 if (!PredSU->isPending)
334 AvailableQueue->remove(PredSU);
335 }
336
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000337 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000338 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000339}
340
341/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
342/// its predecessor states to reflect the change.
343void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000344 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000345 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000346
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
348 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000349 CapturePred(&*I);
Evan Chengcc2efe12010-05-28 23:26:21 +0000350 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000351 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000352 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000353 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000354 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000355 LiveRegDefs[I->getReg()] = NULL;
356 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000357 }
358 }
359
360 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
361 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000362 if (I->isAssignedRegDep()) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000363 // This becomes the nearest def. Note that an earlier def may still be
364 // pending if this is a two-address node.
365 LiveRegDefs[I->getReg()] = SU;
Dan Gohman2d170892008-12-09 22:54:47 +0000366 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000367 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000368 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000369 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
370 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000371 }
372 }
373
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000374 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000375 SU->isScheduled = false;
376 SU->isAvailable = true;
377 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000378 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000379}
380
Evan Cheng8e136a92007-09-26 21:36:17 +0000381/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000382/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000383void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
384 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000385 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000386 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000387 OldSU = Sequence.back();
388 Sequence.pop_back();
389 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000390 // Don't try to remove SU from AvailableQueue.
391 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000392 UnscheduleNodeBottomUp(OldSU);
393 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000394 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000395 }
396
Dan Gohman60d68442009-01-29 19:49:27 +0000397 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000398
399 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000400}
401
Evan Cheng3b245872010-02-05 01:27:11 +0000402static bool isOperandOf(const SUnit *SU, SDNode *N) {
403 for (const SDNode *SUNode = SU->getNode(); SUNode;
404 SUNode = SUNode->getFlaggedNode()) {
405 if (SUNode->isOperandOf(N))
406 return true;
407 }
408 return false;
409}
410
Evan Cheng5924bf72007-09-25 01:54:36 +0000411/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
412/// successors to the newly created node.
413SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000414 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000415 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000416
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000417 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000418 if (!N)
419 return NULL;
420
421 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000422 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000423 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000424 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000425 if (VT == MVT::Glue)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000426 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000427 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000428 TryUnfold = true;
429 }
Evan Cheng79e97132007-10-05 01:39:18 +0000430 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000431 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000432 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000433 if (VT == MVT::Glue)
Evan Cheng79e97132007-10-05 01:39:18 +0000434 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000435 }
436
437 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000438 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000439 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000440 return NULL;
441
Evan Chengbdd062d2010-05-20 06:13:19 +0000442 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000443 assert(NewNodes.size() == 2 && "Expected a load folding node!");
444
445 N = NewNodes[1];
446 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000447 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000448 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000449 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000450 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
451 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000452 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000453
Dan Gohmane52e0892008-11-11 21:34:44 +0000454 // LoadNode may already exist. This can happen when there is another
455 // load from the same location and producing the same type of value
456 // but it has different alignment or volatileness.
457 bool isNewLoad = true;
458 SUnit *LoadSU;
459 if (LoadNode->getNodeId() != -1) {
460 LoadSU = &SUnits[LoadNode->getNodeId()];
461 isNewLoad = false;
462 } else {
463 LoadSU = CreateNewSUnit(LoadNode);
464 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000465 ComputeLatency(LoadSU);
466 }
467
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000468 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000469 assert(N->getNodeId() == -1 && "Node already inserted!");
470 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +0000471
Dan Gohman17059682008-07-17 19:10:17 +0000472 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000473 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000474 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000475 NewSU->isTwoAddress = true;
476 break;
477 }
478 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000479 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000480 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000481 ComputeLatency(NewSU);
482
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000483 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000484 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000485 SmallVector<SDep, 4> ChainSuccs;
486 SmallVector<SDep, 4> LoadPreds;
487 SmallVector<SDep, 4> NodePreds;
488 SmallVector<SDep, 4> NodeSuccs;
489 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
490 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000491 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000492 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000493 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000494 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000495 else
Dan Gohman2d170892008-12-09 22:54:47 +0000496 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000497 }
498 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
499 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000500 if (I->isCtrl())
501 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000502 else
Dan Gohman2d170892008-12-09 22:54:47 +0000503 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000504 }
505
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000506 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000507 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
508 const SDep &Pred = ChainPreds[i];
509 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000510 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000511 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000512 }
Evan Cheng79e97132007-10-05 01:39:18 +0000513 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000514 const SDep &Pred = LoadPreds[i];
515 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000516 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000517 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000518 }
519 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000520 const SDep &Pred = NodePreds[i];
521 RemovePred(SU, Pred);
522 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000523 }
524 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000525 SDep D = NodeSuccs[i];
526 SUnit *SuccDep = D.getSUnit();
527 D.setSUnit(SU);
528 RemovePred(SuccDep, D);
529 D.setSUnit(NewSU);
530 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000531 }
532 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000533 SDep D = ChainSuccs[i];
534 SUnit *SuccDep = D.getSUnit();
535 D.setSUnit(SU);
536 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000537 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000538 D.setSUnit(LoadSU);
539 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540 }
Andrew Trick2085a962010-12-21 22:25:04 +0000541 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000542
543 // Add a data dependency to reflect that NewSU reads the value defined
544 // by LoadSU.
545 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000546
Evan Cheng91e0fc92007-12-18 08:42:10 +0000547 if (isNewLoad)
548 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000549 AvailableQueue->addNode(NewSU);
550
551 ++NumUnfolds;
552
553 if (NewSU->NumSuccsLeft == 0) {
554 NewSU->isAvailable = true;
555 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000556 }
557 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000558 }
559
Evan Chengbdd062d2010-05-20 06:13:19 +0000560 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000561 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000562
563 // New SUnit has the exact same predecessors.
564 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
565 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000566 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000567 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000568
569 // Only copy scheduled successors. Cut them from old node's successor
570 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000571 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000572 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
573 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000574 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000575 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000576 SUnit *SuccSU = I->getSUnit();
577 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000578 SDep D = *I;
579 D.setSUnit(NewSU);
580 AddPred(SuccSU, D);
581 D.setSUnit(SU);
582 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000583 }
584 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000585 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000586 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000587
588 AvailableQueue->updateNode(SU);
589 AvailableQueue->addNode(NewSU);
590
Evan Cheng1ec79b42007-09-27 07:09:03 +0000591 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000592 return NewSU;
593}
594
Evan Chengb2c42c62009-01-12 03:19:55 +0000595/// InsertCopiesAndMoveSuccs - Insert register copies and move all
596/// scheduled successors of the given SUnit to the last copy.
597void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
598 const TargetRegisterClass *DestRC,
599 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000600 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000601 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000602 CopyFromSU->CopySrcRC = SrcRC;
603 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000604
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000605 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000606 CopyToSU->CopySrcRC = DestRC;
607 CopyToSU->CopyDstRC = SrcRC;
608
609 // Only copy scheduled successors. Cut them from old node's successor
610 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000611 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000612 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
613 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000614 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000615 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000616 SUnit *SuccSU = I->getSUnit();
617 if (SuccSU->isScheduled) {
618 SDep D = *I;
619 D.setSUnit(CopyToSU);
620 AddPred(SuccSU, D);
621 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000622 }
623 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000624 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000625 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000626
Dan Gohman2d170892008-12-09 22:54:47 +0000627 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
628 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000629
630 AvailableQueue->updateNode(SU);
631 AvailableQueue->addNode(CopyFromSU);
632 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000633 Copies.push_back(CopyFromSU);
634 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000635
Evan Chengb2c42c62009-01-12 03:19:55 +0000636 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000637}
638
639/// getPhysicalRegisterVT - Returns the ValueType of the physical register
640/// definition of the specified node.
641/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000642static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000643 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000644 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000645 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000646 unsigned NumRes = TID.getNumDefs();
647 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000648 if (Reg == *ImpDef)
649 break;
650 ++NumRes;
651 }
652 return N->getValueType(NumRes);
653}
654
Evan Chengb8905c42009-03-04 01:41:49 +0000655/// CheckForLiveRegDef - Return true and update live register vector if the
656/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000657static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +0000658 std::vector<SUnit*> &LiveRegDefs,
659 SmallSet<unsigned, 4> &RegAdded,
660 SmallVector<unsigned, 4> &LRegs,
661 const TargetRegisterInfo *TRI) {
Evan Chengb8905c42009-03-04 01:41:49 +0000662 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
Chris Lattner0cfe8842010-12-20 00:51:56 +0000663 if (RegAdded.insert(Reg))
Evan Chengb8905c42009-03-04 01:41:49 +0000664 LRegs.push_back(Reg);
Evan Chengb8905c42009-03-04 01:41:49 +0000665 }
666 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
667 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Chris Lattner0cfe8842010-12-20 00:51:56 +0000668 if (RegAdded.insert(*Alias))
Evan Chengb8905c42009-03-04 01:41:49 +0000669 LRegs.push_back(*Alias);
Evan Chengb8905c42009-03-04 01:41:49 +0000670 }
Evan Chengb8905c42009-03-04 01:41:49 +0000671}
672
Evan Cheng5924bf72007-09-25 01:54:36 +0000673/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
674/// scheduling of the given node to satisfy live physical register dependencies.
675/// If the specific node is the last one that's available to schedule, do
676/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000677bool ScheduleDAGRRList::
678DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000679 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000680 return false;
681
Evan Chenge6f92252007-09-27 18:46:06 +0000682 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000683 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000684 //
685 // If SU is the currently live definition of the same register that it uses,
686 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +0000687 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
688 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000689 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +0000690 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
691 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000692 }
693
Dan Gohman072734e2008-11-13 23:24:17 +0000694 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000695 if (Node->getOpcode() == ISD::INLINEASM) {
696 // Inline asm can clobber physical defs.
697 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000698 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Evan Chengb8905c42009-03-04 01:41:49 +0000699 --NumOps; // Ignore the flag operand.
700
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000701 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000702 unsigned Flags =
703 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000704 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000705
706 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000707 if (InlineAsm::isRegDefKind(Flags) ||
708 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000709 // Check for def of register or earlyclobber register.
710 for (; NumVals; --NumVals, ++i) {
711 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
712 if (TargetRegisterInfo::isPhysicalRegister(Reg))
713 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
714 }
715 } else
716 i += NumVals;
717 }
718 continue;
719 }
720
Dan Gohman072734e2008-11-13 23:24:17 +0000721 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000722 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000723 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000724 if (!TID.ImplicitDefs)
725 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000726 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
727 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000728 }
Andrew Trick2085a962010-12-21 22:25:04 +0000729
Evan Cheng5924bf72007-09-25 01:54:36 +0000730 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000731}
732
Evan Cheng1ec79b42007-09-27 07:09:03 +0000733
Evan Chengd38c22b2006-05-11 23:55:42 +0000734/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
735/// schedulers.
736void ScheduleDAGRRList::ListScheduleBottomUp() {
737 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000738
739 // Release any predecessors of the special Exit node.
740 ReleasePredecessors(&ExitSU, CurCycle);
741
Evan Chengd38c22b2006-05-11 23:55:42 +0000742 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000743 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000744 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000745 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
746 RootSU->isAvailable = true;
747 AvailableQueue->push(RootSU);
748 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000749
750 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000751 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000752 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000753 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000754 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000755 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000756 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000757 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000758 SUnit *CurSU = AvailableQueue->pop();
759 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000760 SmallVector<unsigned, 4> LRegs;
761 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
762 break;
763 Delayed = true;
764 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000765
766 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
767 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000768 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000769 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000770
771 // All candidates are delayed due to live physical reg dependencies.
772 // Try backtracking, code duplication, or inserting cross class copies
773 // to resolve it.
774 if (Delayed && !CurSU) {
775 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
776 SUnit *TrySU = NotReady[i];
777 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
778
779 // Try unscheduling up to the point where it's safe to schedule
780 // this node.
781 unsigned LiveCycle = CurCycle;
782 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
783 unsigned Reg = LRegs[j];
784 unsigned LCycle = LiveRegCycles[Reg];
785 LiveCycle = std::min(LiveCycle, LCycle);
786 }
787 SUnit *OldSU = Sequence[LiveCycle];
788 if (!WillCreateCycle(TrySU, OldSU)) {
789 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
790 // Force the current node to be scheduled before the node that
791 // requires the physical reg dep.
792 if (OldSU->isAvailable) {
793 OldSU->isAvailable = false;
794 AvailableQueue->remove(OldSU);
795 }
Dan Gohman2d170892008-12-09 22:54:47 +0000796 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
797 /*Reg=*/0, /*isNormalMemory=*/false,
798 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000799 // If one or more successors has been unscheduled, then the current
800 // node is no longer avaialable. Schedule a successor that's now
801 // available instead.
802 if (!TrySU->isAvailable)
803 CurSU = AvailableQueue->pop();
804 else {
805 CurSU = TrySU;
806 TrySU->isPending = false;
807 NotReady.erase(NotReady.begin()+i);
808 }
809 break;
810 }
811 }
812
813 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000814 // Can't backtrack. If it's too expensive to copy the value, then try
815 // duplicate the nodes that produces these "too expensive to copy"
816 // values to break the dependency. In case even that doesn't work,
817 // insert cross class copies.
818 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000819 SUnit *TrySU = NotReady[0];
820 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
821 assert(LRegs.size() == 1 && "Can't handle this yet!");
822 unsigned Reg = LRegs[0];
823 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000824 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000825 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000826 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000827 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
828
829 // If cross copy register class is null, then it must be possible copy
830 // the value directly. Do not try duplicate the def.
831 SUnit *NewDef = 0;
832 if (DestRC)
833 NewDef = CopyAndMoveSuccessors(LRDef);
834 else
835 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000836 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000837 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000838 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000839 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000840 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000841 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000842 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000843 /*Reg=*/0, /*isNormalMemory=*/false,
844 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000845 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000846 NewDef = Copies.back();
847 }
848
Evan Chengbdd062d2010-05-20 06:13:19 +0000849 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000850 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000851 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000852 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000853 /*Reg=*/0, /*isNormalMemory=*/false,
854 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000855 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000856 TrySU->isAvailable = false;
857 CurSU = NewDef;
858 }
859
Dan Gohman60d68442009-01-29 19:49:27 +0000860 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000861 }
862
Evan Chengd38c22b2006-05-11 23:55:42 +0000863 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000864 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
865 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000866 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000867 if (NotReady[i]->isAvailable)
868 AvailableQueue->push(NotReady[i]);
869 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000870 NotReady.clear();
871
Dan Gohmanc602dd42008-11-21 00:10:42 +0000872 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000873 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000874 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000875 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000876 }
877
Evan Chengd38c22b2006-05-11 23:55:42 +0000878 // Reverse the order if it is bottom up.
879 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +0000880
Evan Chengd38c22b2006-05-11 23:55:42 +0000881#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000882 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000883#endif
884}
885
886//===----------------------------------------------------------------------===//
887// Top-Down Scheduling
888//===----------------------------------------------------------------------===//
889
890/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000891/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000892void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000893 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000894
Evan Chengd38c22b2006-05-11 23:55:42 +0000895#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000896 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000897 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000898 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000899 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000900 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000901 }
902#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000903 --SuccSU->NumPredsLeft;
904
Dan Gohmanb9543432009-02-10 23:27:53 +0000905 // If all the node's predecessors are scheduled, this node is ready
906 // to be scheduled. Ignore the special ExitSU node.
907 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000908 SuccSU->isAvailable = true;
909 AvailableQueue->push(SuccSU);
910 }
911}
912
Dan Gohmanb9543432009-02-10 23:27:53 +0000913void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
914 // Top down: release successors
915 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
916 I != E; ++I) {
917 assert(!I->isAssignedRegDep() &&
918 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
919
920 ReleaseSucc(SU, &*I);
921 }
922}
923
Evan Chengd38c22b2006-05-11 23:55:42 +0000924/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
925/// count of its successors. If a successor pending count is zero, add it to
926/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000927void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000928 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000929 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000930
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000931 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
932 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000933 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000934
Dan Gohmanb9543432009-02-10 23:27:53 +0000935 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000936 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000937 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000938}
939
Dan Gohman54a187e2007-08-20 19:28:38 +0000940/// ListScheduleTopDown - The main loop of list scheduling for top-down
941/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000942void ScheduleDAGRRList::ListScheduleTopDown() {
943 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000944 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000945
Dan Gohmanb9543432009-02-10 23:27:53 +0000946 // Release any successors of the special Entry node.
947 ReleaseSuccessors(&EntrySU);
948
Evan Chengd38c22b2006-05-11 23:55:42 +0000949 // All leaves to Available queue.
950 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
951 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000952 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000953 AvailableQueue->push(&SUnits[i]);
954 SUnits[i].isAvailable = true;
955 }
956 }
Andrew Trick2085a962010-12-21 22:25:04 +0000957
Evan Chengd38c22b2006-05-11 23:55:42 +0000958 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000959 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000960 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000961 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000962 SUnit *CurSU = AvailableQueue->pop();
Andrew Trick2085a962010-12-21 22:25:04 +0000963
Dan Gohmanc602dd42008-11-21 00:10:42 +0000964 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000965 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000966 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000967 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000968 }
Andrew Trick2085a962010-12-21 22:25:04 +0000969
Evan Chengd38c22b2006-05-11 23:55:42 +0000970#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000971 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000972#endif
973}
974
975
Evan Chengd38c22b2006-05-11 23:55:42 +0000976//===----------------------------------------------------------------------===//
977// RegReductionPriorityQueue Implementation
978//===----------------------------------------------------------------------===//
979//
980// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
981// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +0000982//
Evan Chengd38c22b2006-05-11 23:55:42 +0000983namespace {
984 template<class SF>
985 class RegReductionPriorityQueue;
Andrew Trick2085a962010-12-21 22:25:04 +0000986
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000987 /// bu_ls_rr_sort - Priority function for bottom up register pressure
988 // reduction scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000989 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
990 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
991 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
992 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +0000993
Evan Chengd38c22b2006-05-11 23:55:42 +0000994 bool operator()(const SUnit* left, const SUnit* right) const;
995 };
996
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000997 // td_ls_rr_sort - Priority function for top down register pressure reduction
998 // scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000999 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1000 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1001 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1002 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001003
Evan Chengd38c22b2006-05-11 23:55:42 +00001004 bool operator()(const SUnit* left, const SUnit* right) const;
1005 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001006
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001007 // src_ls_rr_sort - Priority function for source order scheduler.
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001008 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1009 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
1010 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
1011 : SPQ(spq) {}
1012 src_ls_rr_sort(const src_ls_rr_sort &RHS)
1013 : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001014
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001015 bool operator()(const SUnit* left, const SUnit* right) const;
1016 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001017
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001018 // hybrid_ls_rr_sort - Priority function for hybrid scheduler.
Evan Chengbdd062d2010-05-20 06:13:19 +00001019 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1020 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
1021 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
1022 : SPQ(spq) {}
1023 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1024 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001025
Evan Chengbdd062d2010-05-20 06:13:19 +00001026 bool operator()(const SUnit* left, const SUnit* right) const;
1027 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001028
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001029 // ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1030 // scheduler.
Evan Cheng37b740c2010-07-24 00:39:05 +00001031 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1032 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1033 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1034 : SPQ(spq) {}
1035 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1036 : SPQ(RHS.SPQ) {}
1037
1038 bool operator()(const SUnit* left, const SUnit* right) const;
1039 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001040} // end anonymous namespace
1041
Dan Gohman186f65d2008-11-20 03:30:37 +00001042/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1043/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001044static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001045CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001046 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1047 if (SethiUllmanNumber != 0)
1048 return SethiUllmanNumber;
1049
1050 unsigned Extra = 0;
1051 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1052 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001053 if (I->isCtrl()) continue; // ignore chain preds
1054 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001055 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001056 if (PredSethiUllman > SethiUllmanNumber) {
1057 SethiUllmanNumber = PredSethiUllman;
1058 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001059 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001060 ++Extra;
1061 }
1062
1063 SethiUllmanNumber += Extra;
1064
1065 if (SethiUllmanNumber == 0)
1066 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001067
Evan Cheng7e4abde2008-07-02 09:23:51 +00001068 return SethiUllmanNumber;
1069}
1070
Evan Chengd38c22b2006-05-11 23:55:42 +00001071namespace {
1072 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001073 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001074 std::vector<SUnit*> Queue;
1075 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001076 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001077 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001078
Dan Gohman3f656df2008-11-20 02:45:51 +00001079 protected:
1080 // SUnits - The SUnits for the current graph.
1081 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001082
1083 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001084 const TargetInstrInfo *TII;
1085 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001086 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001087 ScheduleDAGRRList *scheduleDAG;
1088
Dan Gohman186f65d2008-11-20 03:30:37 +00001089 // SethiUllmanNumbers - The SethiUllman number for each node.
1090 std::vector<unsigned> SethiUllmanNumbers;
1091
Evan Chenga77f3d32010-07-21 06:09:07 +00001092 /// RegPressure - Tracking current reg pressure per register class.
1093 ///
Evan Cheng28590382010-07-21 23:53:58 +00001094 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001095
1096 /// RegLimit - Tracking the number of allocatable registers per register
1097 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001098 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001099
Dan Gohman3f656df2008-11-20 02:45:51 +00001100 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001101 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001102 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001103 const TargetInstrInfo *tii,
1104 const TargetRegisterInfo *tri,
1105 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001106 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001107 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001108 if (TracksRegPressure) {
1109 unsigned NumRC = TRI->getNumRegClasses();
1110 RegLimit.resize(NumRC);
1111 RegPressure.resize(NumRC);
1112 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1113 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1114 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1115 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001116 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001117 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001118 }
Andrew Trick2085a962010-12-21 22:25:04 +00001119
Dan Gohman3f656df2008-11-20 02:45:51 +00001120 void initNodes(std::vector<SUnit> &sunits) {
1121 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001122 // Add pseudo dependency edges for two-address nodes.
1123 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001124 // Reroute edges to nodes with multiple uses.
1125 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001126 // Calculate node priorities.
1127 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001128 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001129
Dan Gohman186f65d2008-11-20 03:30:37 +00001130 void addNode(const SUnit *SU) {
1131 unsigned SUSize = SethiUllmanNumbers.size();
1132 if (SUnits->size() > SUSize)
1133 SethiUllmanNumbers.resize(SUSize*2, 0);
1134 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1135 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001136
Dan Gohman186f65d2008-11-20 03:30:37 +00001137 void updateNode(const SUnit *SU) {
1138 SethiUllmanNumbers[SU->NodeNum] = 0;
1139 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1140 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001141
Dan Gohman186f65d2008-11-20 03:30:37 +00001142 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001143 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001144 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001145 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001146 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001147
1148 unsigned getNodePriority(const SUnit *SU) const {
1149 assert(SU->NodeNum < SethiUllmanNumbers.size());
1150 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001151 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001152 // CopyToReg should be close to its uses to facilitate coalescing and
1153 // avoid spilling.
1154 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001155 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1156 Opc == TargetOpcode::SUBREG_TO_REG ||
1157 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001158 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1159 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001160 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001161 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1162 // If SU does not have a register use, i.e. it doesn't produce a value
1163 // that would be consumed (e.g. store), then it terminates a chain of
1164 // computation. Give it a large SethiUllman number so it will be
1165 // scheduled right before its predecessors that it doesn't lengthen
1166 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001167 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001168 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1169 // If SU does not have a register def, schedule it close to its uses
1170 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001171 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001172 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001173 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001174
1175 unsigned getNodeOrdering(const SUnit *SU) const {
1176 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1177 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001178
Evan Chengd38c22b2006-05-11 23:55:42 +00001179 bool empty() const { return Queue.empty(); }
Andrew Trick2085a962010-12-21 22:25:04 +00001180
Evan Chengd38c22b2006-05-11 23:55:42 +00001181 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001182 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001183 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001184 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001185 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001186
Evan Chengd38c22b2006-05-11 23:55:42 +00001187 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001188 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001189 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001190 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001191 E = Queue.end(); I != E; ++I)
1192 if (Picker(*Best, *I))
1193 Best = I;
1194 SUnit *V = *Best;
1195 if (Best != prior(Queue.end()))
1196 std::swap(*Best, Queue.back());
1197 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001198 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001199 return V;
1200 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001201
Evan Cheng5924bf72007-09-25 01:54:36 +00001202 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001203 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001204 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001205 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1206 SU);
1207 if (I != prior(Queue.end()))
1208 std::swap(*I, Queue.back());
1209 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001210 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001211 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001212
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001213 bool HighRegPressure(const SUnit *SU) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001214 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001215 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001216
Evan Chenga77f3d32010-07-21 06:09:07 +00001217 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1218 I != E; ++I) {
1219 if (I->isCtrl())
1220 continue;
1221 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001222 const SDNode *PN = PredSU->getNode();
1223 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001224 if (PN->getOpcode() == ISD::CopyFromReg) {
1225 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001226 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1227 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001228 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1229 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001230 }
1231 continue;
1232 }
1233 unsigned POpc = PN->getMachineOpcode();
1234 if (POpc == TargetOpcode::IMPLICIT_DEF)
1235 continue;
1236 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1237 EVT VT = PN->getOperand(0).getValueType();
1238 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1239 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1240 // Check if this increases register pressure of the specific register
1241 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001242 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1243 return true;
Andrew Trick2085a962010-12-21 22:25:04 +00001244 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001245 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1246 POpc == TargetOpcode::SUBREG_TO_REG) {
1247 EVT VT = PN->getValueType(0);
1248 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1249 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1250 // Check if this increases register pressure of the specific register
1251 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001252 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1253 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001254 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001255 }
1256 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001257 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001258 EVT VT = PN->getValueType(i);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001259 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1260 if (RegPressure[RCId] >= RegLimit[RCId])
1261 return true; // Reg pressure already high.
1262 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Cheng28590382010-07-21 23:53:58 +00001263 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001264 continue;
Evan Chenga77f3d32010-07-21 06:09:07 +00001265 // Check if this increases register pressure of the specific register
1266 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001267 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1268 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001269 }
1270 }
1271
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001272 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001273 }
1274
Evan Chengbf32e542010-07-22 06:24:48 +00001275 void ScheduledNode(SUnit *SU) {
1276 if (!TracksRegPressure)
1277 return;
1278
Evan Chenga77f3d32010-07-21 06:09:07 +00001279 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001280 if (!N->isMachineOpcode()) {
1281 if (N->getOpcode() != ISD::CopyToReg)
1282 return;
1283 } else {
1284 unsigned Opc = N->getMachineOpcode();
1285 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1286 Opc == TargetOpcode::INSERT_SUBREG ||
1287 Opc == TargetOpcode::SUBREG_TO_REG ||
1288 Opc == TargetOpcode::REG_SEQUENCE ||
1289 Opc == TargetOpcode::IMPLICIT_DEF)
1290 return;
1291 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001292
1293 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1294 I != E; ++I) {
1295 if (I->isCtrl())
1296 continue;
1297 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001298 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001299 continue;
1300 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001301 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001302 if (PN->getOpcode() == ISD::CopyFromReg) {
1303 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001304 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1305 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1306 }
1307 continue;
1308 }
1309 unsigned POpc = PN->getMachineOpcode();
1310 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001311 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001312 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1313 EVT VT = PN->getOperand(0).getValueType();
1314 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1315 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001316 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001317 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1318 POpc == TargetOpcode::SUBREG_TO_REG) {
1319 EVT VT = PN->getValueType(0);
1320 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1321 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1322 continue;
1323 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001324 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1325 for (unsigned i = 0; i != NumDefs; ++i) {
1326 EVT VT = PN->getValueType(i);
1327 if (!PN->hasAnyUseOfValue(i))
1328 continue;
1329 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1330 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1331 }
1332 }
1333
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001334 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1335 // may transfer data dependencies to CopyToReg.
1336 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001337 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1338 for (unsigned i = 0; i != NumDefs; ++i) {
1339 EVT VT = N->getValueType(i);
1340 if (!N->hasAnyUseOfValue(i))
1341 continue;
1342 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1343 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1344 // Register pressure tracking is imprecise. This can happen.
1345 RegPressure[RCId] = 0;
1346 else
1347 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1348 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001349 }
Evan Chengbf32e542010-07-22 06:24:48 +00001350
1351 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001352 }
1353
Evan Chengbf32e542010-07-22 06:24:48 +00001354 void UnscheduledNode(SUnit *SU) {
1355 if (!TracksRegPressure)
1356 return;
1357
Evan Chenga77f3d32010-07-21 06:09:07 +00001358 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001359 if (!N->isMachineOpcode()) {
1360 if (N->getOpcode() != ISD::CopyToReg)
1361 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001362 } else {
1363 unsigned Opc = N->getMachineOpcode();
1364 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1365 Opc == TargetOpcode::INSERT_SUBREG ||
1366 Opc == TargetOpcode::SUBREG_TO_REG ||
1367 Opc == TargetOpcode::REG_SEQUENCE ||
1368 Opc == TargetOpcode::IMPLICIT_DEF)
1369 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001370 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001371
1372 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1373 I != E; ++I) {
1374 if (I->isCtrl())
1375 continue;
1376 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001377 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001378 continue;
1379 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001380 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001381 if (PN->getOpcode() == ISD::CopyFromReg) {
1382 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001383 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1384 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1385 }
1386 continue;
1387 }
1388 unsigned POpc = PN->getMachineOpcode();
1389 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001390 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001391 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1392 EVT VT = PN->getOperand(0).getValueType();
1393 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1394 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001395 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001396 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1397 POpc == TargetOpcode::SUBREG_TO_REG) {
1398 EVT VT = PN->getValueType(0);
1399 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1400 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1401 continue;
1402 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001403 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1404 for (unsigned i = 0; i != NumDefs; ++i) {
1405 EVT VT = PN->getValueType(i);
1406 if (!PN->hasAnyUseOfValue(i))
1407 continue;
1408 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001409 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001410 // Register pressure tracking is imprecise. This can happen.
1411 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001412 else
1413 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001414 }
1415 }
1416
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001417 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1418 // may transfer data dependencies to CopyToReg.
1419 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001420 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1421 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1422 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001423 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengdf907f42010-07-23 22:39:59 +00001424 continue;
1425 if (!N->hasAnyUseOfValue(i))
1426 continue;
1427 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1428 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1429 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001430 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001431
Evan Chenga77f3d32010-07-21 06:09:07 +00001432 dumpRegPressure();
1433 }
1434
Andrew Trick2085a962010-12-21 22:25:04 +00001435 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1436 scheduleDAG = scheduleDag;
Dan Gohman3f656df2008-11-20 02:45:51 +00001437 }
1438
Evan Chenga77f3d32010-07-21 06:09:07 +00001439 void dumpRegPressure() const {
1440 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1441 E = TRI->regclass_end(); I != E; ++I) {
1442 const TargetRegisterClass *RC = *I;
1443 unsigned Id = RC->getID();
1444 unsigned RP = RegPressure[Id];
1445 if (!RP) continue;
1446 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1447 << '\n');
1448 }
1449 }
1450
Dan Gohman3f656df2008-11-20 02:45:51 +00001451 protected:
1452 bool canClobber(const SUnit *SU, const SUnit *Op);
1453 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001454 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001455 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001456 };
1457
Dan Gohman186f65d2008-11-20 03:30:37 +00001458 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1459 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001460
Dan Gohman186f65d2008-11-20 03:30:37 +00001461 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1462 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001463
1464 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1465 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001466
1467 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1468 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001469
1470 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1471 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001472}
1473
Evan Chengb9e3db62007-03-14 22:43:40 +00001474/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001475/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001476static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001477 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001478 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001479 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001480 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001481 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001482 // If there are bunch of CopyToRegs stacked up, they should be considered
1483 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001484 if (I->getSUnit()->getNode() &&
1485 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001486 Height = closestSucc(I->getSUnit())+1;
1487 if (Height > MaxHeight)
1488 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001489 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001490 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001491}
1492
Evan Cheng61bc51e2007-12-20 02:22:36 +00001493/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001494/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001495static unsigned calcMaxScratches(const SUnit *SU) {
1496 unsigned Scratches = 0;
1497 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001498 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001499 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001500 Scratches++;
1501 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001502 return Scratches;
1503}
1504
Evan Cheng6c1414f2010-10-29 18:09:28 +00001505/// hasOnlyLiveOutUse - Return true if SU has a single value successor that is a
1506/// CopyToReg to a virtual register. This SU def is probably a liveout and
1507/// it has no other use. It should be scheduled closer to the terminator.
1508static bool hasOnlyLiveOutUses(const SUnit *SU) {
1509 bool RetVal = false;
1510 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1511 I != E; ++I) {
1512 if (I->isCtrl()) continue;
1513 const SUnit *SuccSU = I->getSUnit();
1514 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
1515 unsigned Reg =
1516 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
1517 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1518 RetVal = true;
1519 continue;
1520 }
1521 }
1522 return false;
1523 }
1524 return RetVal;
1525}
1526
1527/// UnitsSharePred - Return true if the two scheduling units share a common
1528/// data predecessor.
1529static bool UnitsSharePred(const SUnit *left, const SUnit *right) {
1530 SmallSet<const SUnit*, 4> Preds;
1531 for (SUnit::const_pred_iterator I = left->Preds.begin(),E = left->Preds.end();
1532 I != E; ++I) {
1533 if (I->isCtrl()) continue; // ignore chain preds
1534 Preds.insert(I->getSUnit());
1535 }
1536 for (SUnit::const_pred_iterator I = right->Preds.begin(),E = right->Preds.end();
1537 I != E; ++I) {
1538 if (I->isCtrl()) continue; // ignore chain preds
1539 if (Preds.count(I->getSUnit()))
1540 return true;
1541 }
1542 return false;
1543}
1544
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001545template <typename RRSort>
1546static bool BURRSort(const SUnit *left, const SUnit *right,
1547 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001548 unsigned LPriority = SPQ->getNodePriority(left);
1549 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001550 if (LPriority != RPriority)
1551 return LPriority > RPriority;
1552
1553 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1554 // e.g.
1555 // t1 = op t2, c1
1556 // t3 = op t4, c2
1557 //
1558 // and the following instructions are both ready.
1559 // t2 = op c3
1560 // t4 = op c4
1561 //
1562 // Then schedule t2 = op first.
1563 // i.e.
1564 // t4 = op c4
1565 // t2 = op c3
1566 // t1 = op t2, c1
1567 // t3 = op t4, c2
1568 //
1569 // This creates more short live intervals.
1570 unsigned LDist = closestSucc(left);
1571 unsigned RDist = closestSucc(right);
1572 if (LDist != RDist)
1573 return LDist < RDist;
1574
Evan Cheng3a14efa2009-02-12 08:59:45 +00001575 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001576 unsigned LScratch = calcMaxScratches(left);
1577 unsigned RScratch = calcMaxScratches(right);
1578 if (LScratch != RScratch)
1579 return LScratch > RScratch;
1580
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001581 if (left->getHeight() != right->getHeight())
1582 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00001583
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001584 if (left->getDepth() != right->getDepth())
1585 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001586
Andrew Trick2085a962010-12-21 22:25:04 +00001587 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00001588 "NodeQueueId cannot be zero");
1589 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001590}
1591
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001592// Bottom up
1593bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1594 return BURRSort(left, right, SPQ);
1595}
1596
1597// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001598bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001599 unsigned LOrder = SPQ->getNodeOrdering(left);
1600 unsigned ROrder = SPQ->getNodeOrdering(right);
1601
1602 // Prefer an ordering where the lower the non-zero order number, the higher
1603 // the preference.
1604 if ((LOrder || ROrder) && LOrder != ROrder)
1605 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1606
1607 return BURRSort(left, right, SPQ);
1608}
1609
Evan Chengbdd062d2010-05-20 06:13:19 +00001610bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdebf9c52010-11-03 00:45:17 +00001611 if (left->isCall || right->isCall)
1612 // No way to compute latency of calls.
1613 return BURRSort(left, right, SPQ);
1614
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001615 bool LHigh = SPQ->HighRegPressure(left);
1616 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001617 // Avoid causing spills. If register pressure is high, schedule for
1618 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001619 if (LHigh && !RHigh)
1620 return true;
1621 else if (!LHigh && RHigh)
1622 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001623 else if (!LHigh && !RHigh) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001624 // If the two nodes share an operand and one of them has a single
1625 // use that is a live out copy, favor the one that is live out. Otherwise
1626 // it will be difficult to eliminate the copy if the instruction is a
1627 // loop induction variable update. e.g.
1628 // BB:
1629 // sub r1, r3, #1
1630 // str r0, [r2, r3]
1631 // mov r3, r1
1632 // cmp
1633 // bne BB
1634 bool SharePred = UnitsSharePred(left, right);
1635 // FIXME: Only adjust if BB is a loop back edge.
1636 // FIXME: What's the cost of a copy?
1637 int LBonus = (SharePred && hasOnlyLiveOutUses(left)) ? 1 : 0;
1638 int RBonus = (SharePred && hasOnlyLiveOutUses(right)) ? 1 : 0;
1639 int LHeight = (int)left->getHeight() - LBonus;
1640 int RHeight = (int)right->getHeight() - RBonus;
1641
Evan Cheng28590382010-07-21 23:53:58 +00001642 // Low register pressure situation, schedule for latency if possible.
1643 bool LStall = left->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001644 (int)SPQ->getCurCycle() < LHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001645 bool RStall = right->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001646 (int)SPQ->getCurCycle() < RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001647 // If scheduling one of the node will cause a pipeline stall, delay it.
1648 // If scheduling either one of the node will cause a pipeline stall, sort
1649 // them according to their height.
Evan Cheng28590382010-07-21 23:53:58 +00001650 if (LStall) {
1651 if (!RStall)
1652 return true;
Evan Cheng6c1414f2010-10-29 18:09:28 +00001653 if (LHeight != RHeight)
1654 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001655 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001656 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001657
Evan Cheng6c1414f2010-10-29 18:09:28 +00001658 // If either node is scheduling for latency, sort them by height
1659 // and latency.
Evan Cheng28590382010-07-21 23:53:58 +00001660 if (left->SchedulingPref == Sched::Latency ||
1661 right->SchedulingPref == Sched::Latency) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001662 if (LHeight != RHeight)
1663 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001664 if (left->Latency != right->Latency)
1665 return left->Latency > right->Latency;
1666 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001667 }
1668
Evan Chengbdd062d2010-05-20 06:13:19 +00001669 return BURRSort(left, right, SPQ);
1670}
1671
Evan Cheng37b740c2010-07-24 00:39:05 +00001672bool ilp_ls_rr_sort::operator()(const SUnit *left,
1673 const SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001674 if (left->isCall || right->isCall)
1675 // No way to compute latency of calls.
1676 return BURRSort(left, right, SPQ);
1677
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001678 bool LHigh = SPQ->HighRegPressure(left);
1679 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001680 // Avoid causing spills. If register pressure is high, schedule for
1681 // register pressure reduction.
1682 if (LHigh && !RHigh)
1683 return true;
1684 else if (!LHigh && RHigh)
1685 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001686 else if (!LHigh && !RHigh) {
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001687 // Low register pressure situation, schedule to maximize instruction level
1688 // parallelism.
Evan Cheng37b740c2010-07-24 00:39:05 +00001689 if (left->NumPreds > right->NumPreds)
1690 return false;
1691 else if (left->NumPreds < right->NumPreds)
1692 return false;
1693 }
1694
1695 return BURRSort(left, right, SPQ);
1696}
1697
Dan Gohman3f656df2008-11-20 02:45:51 +00001698template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001699bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001700RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001701 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001702 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001703 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001704 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001705 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001706 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001707 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001708 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001709 if (DU->getNodeId() != -1 &&
1710 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001711 return true;
1712 }
1713 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001714 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001715 return false;
1716}
1717
Evan Chengf9891412007-12-20 09:25:31 +00001718/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001719/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001720static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001721 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001722 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001723 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001724 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1725 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001726 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001727 for (const SDNode *SUNode = SU->getNode(); SUNode;
1728 SUNode = SUNode->getFlaggedNode()) {
1729 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001730 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001731 const unsigned *SUImpDefs =
1732 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1733 if (!SUImpDefs)
1734 return false;
1735 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001736 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001737 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001738 continue;
1739 if (!N->hasAnyUseOfValue(i))
1740 continue;
1741 unsigned Reg = ImpDefs[i - NumDefs];
1742 for (;*SUImpDefs; ++SUImpDefs) {
1743 unsigned SUReg = *SUImpDefs;
1744 if (TRI->regsOverlap(Reg, SUReg))
1745 return true;
1746 }
Evan Chengf9891412007-12-20 09:25:31 +00001747 }
1748 }
1749 return false;
1750}
1751
Dan Gohman9a658d72009-03-24 00:49:12 +00001752/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1753/// are not handled well by the general register pressure reduction
1754/// heuristics. When presented with code like this:
1755///
1756/// N
1757/// / |
1758/// / |
1759/// U store
1760/// |
1761/// ...
1762///
1763/// the heuristics tend to push the store up, but since the
1764/// operand of the store has another use (U), this would increase
1765/// the length of that other use (the U->N edge).
1766///
1767/// This function transforms code like the above to route U's
1768/// dependence through the store when possible, like this:
1769///
1770/// N
1771/// ||
1772/// ||
1773/// store
1774/// |
1775/// U
1776/// |
1777/// ...
1778///
1779/// This results in the store being scheduled immediately
1780/// after N, which shortens the U->N live range, reducing
1781/// register pressure.
1782///
1783template<class SF>
1784void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1785 // Visit all the nodes in topological order, working top-down.
1786 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1787 SUnit *SU = &(*SUnits)[i];
1788 // For now, only look at nodes with no data successors, such as stores.
1789 // These are especially important, due to the heuristics in
1790 // getNodePriority for nodes with no data successors.
1791 if (SU->NumSuccs != 0)
1792 continue;
1793 // For now, only look at nodes with exactly one data predecessor.
1794 if (SU->NumPreds != 1)
1795 continue;
1796 // Avoid prescheduling copies to virtual registers, which don't behave
1797 // like other nodes from the perspective of scheduling heuristics.
1798 if (SDNode *N = SU->getNode())
1799 if (N->getOpcode() == ISD::CopyToReg &&
1800 TargetRegisterInfo::isVirtualRegister
1801 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1802 continue;
1803
1804 // Locate the single data predecessor.
1805 SUnit *PredSU = 0;
1806 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1807 EE = SU->Preds.end(); II != EE; ++II)
1808 if (!II->isCtrl()) {
1809 PredSU = II->getSUnit();
1810 break;
1811 }
1812 assert(PredSU);
1813
1814 // Don't rewrite edges that carry physregs, because that requires additional
1815 // support infrastructure.
1816 if (PredSU->hasPhysRegDefs)
1817 continue;
1818 // Short-circuit the case where SU is PredSU's only data successor.
1819 if (PredSU->NumSuccs == 1)
1820 continue;
1821 // Avoid prescheduling to copies from virtual registers, which don't behave
1822 // like other nodes from the perspective of scheduling // heuristics.
1823 if (SDNode *N = SU->getNode())
1824 if (N->getOpcode() == ISD::CopyFromReg &&
1825 TargetRegisterInfo::isVirtualRegister
1826 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1827 continue;
1828
1829 // Perform checks on the successors of PredSU.
1830 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1831 EE = PredSU->Succs.end(); II != EE; ++II) {
1832 SUnit *PredSuccSU = II->getSUnit();
1833 if (PredSuccSU == SU) continue;
1834 // If PredSU has another successor with no data successors, for
1835 // now don't attempt to choose either over the other.
1836 if (PredSuccSU->NumSuccs == 0)
1837 goto outer_loop_continue;
1838 // Don't break physical register dependencies.
1839 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1840 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1841 goto outer_loop_continue;
1842 // Don't introduce graph cycles.
1843 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1844 goto outer_loop_continue;
1845 }
1846
1847 // Ok, the transformation is safe and the heuristics suggest it is
1848 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001849 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1850 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001851 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001852 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1853 SDep Edge = PredSU->Succs[i];
1854 assert(!Edge.isAssignedRegDep());
1855 SUnit *SuccSU = Edge.getSUnit();
1856 if (SuccSU != SU) {
1857 Edge.setSUnit(PredSU);
1858 scheduleDAG->RemovePred(SuccSU, Edge);
1859 scheduleDAG->AddPred(SU, Edge);
1860 Edge.setSUnit(SU);
1861 scheduleDAG->AddPred(SuccSU, Edge);
1862 --i;
1863 }
1864 }
1865 outer_loop_continue:;
1866 }
1867}
1868
Evan Chengd38c22b2006-05-11 23:55:42 +00001869/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1870/// it as a def&use operand. Add a pseudo control edge from it to the other
1871/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001872/// first (lower in the schedule). If both nodes are two-address, favor the
1873/// one that has a CopyToReg use (more likely to be a loop induction update).
1874/// If both are two-address, but one is commutable while the other is not
1875/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001876template<class SF>
1877void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001878 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001879 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001880 if (!SU->isTwoAddress)
1881 continue;
1882
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001883 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001884 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001885 continue;
1886
Evan Cheng6c1414f2010-10-29 18:09:28 +00001887 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00001888 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001889 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001890 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001891 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001892 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001893 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1894 continue;
1895 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1896 if (DU->getNodeId() == -1)
1897 continue;
1898 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1899 if (!DUSU) continue;
1900 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1901 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001902 if (I->isCtrl()) continue;
1903 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001904 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001905 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001906 // Be conservative. Ignore if nodes aren't at roughly the same
1907 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001908 if (SuccSU->getHeight() < SU->getHeight() &&
1909 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001910 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001911 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1912 // constrains whatever is using the copy, instead of the copy
1913 // itself. In the case that the copy is coalesced, this
1914 // preserves the intent of the pseudo two-address heurietics.
1915 while (SuccSU->Succs.size() == 1 &&
1916 SuccSU->getNode()->isMachineOpcode() &&
1917 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001918 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001919 SuccSU = SuccSU->Succs.front().getSUnit();
1920 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001921 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1922 continue;
1923 // Don't constrain nodes with physical register defs if the
1924 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001925 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001926 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001927 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001928 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001929 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1930 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001931 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001932 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1933 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1934 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001935 continue;
1936 if ((!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00001937 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00001938 (!SU->isCommutable && SuccSU->isCommutable)) &&
1939 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001940 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001941 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001942 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001943 /*Reg=*/0, /*isNormalMemory=*/false,
1944 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001945 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001946 }
1947 }
1948 }
1949 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001950}
1951
Evan Cheng6730f032007-01-08 23:55:53 +00001952/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1953/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001954template<class SF>
1955void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001956 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick2085a962010-12-21 22:25:04 +00001957
Evan Chengd38c22b2006-05-11 23:55:42 +00001958 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001959 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001960}
Evan Chengd38c22b2006-05-11 23:55:42 +00001961
Roman Levenstein30d09512008-03-27 09:44:37 +00001962/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001963/// predecessors of the successors of the SUnit SU. Stop when the provided
1964/// limit is exceeded.
Andrew Trick2085a962010-12-21 22:25:04 +00001965static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
Roman Levensteinbc674502008-03-27 09:14:57 +00001966 unsigned Limit) {
1967 unsigned Sum = 0;
1968 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1969 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001970 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001971 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1972 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001973 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001974 if (!PredSU->isScheduled)
1975 if (++Sum > Limit)
1976 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001977 }
1978 }
1979 return Sum;
1980}
1981
Evan Chengd38c22b2006-05-11 23:55:42 +00001982
1983// Top down
1984bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001985 unsigned LPriority = SPQ->getNodePriority(left);
1986 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001987 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1988 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001989 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1990 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001991 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1992 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001993
1994 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1995 return false;
1996 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1997 return true;
1998
Evan Chengd38c22b2006-05-11 23:55:42 +00001999 if (LIsFloater)
2000 LBonus -= 2;
2001 if (RIsFloater)
2002 RBonus -= 2;
2003 if (left->NumSuccs == 1)
2004 LBonus += 2;
2005 if (right->NumSuccs == 1)
2006 RBonus += 2;
2007
Evan Cheng73bdf042008-03-01 00:39:47 +00002008 if (LPriority+LBonus != RPriority+RBonus)
2009 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00002010
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002011 if (left->getDepth() != right->getDepth())
2012 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00002013
2014 if (left->NumSuccsLeft != right->NumSuccsLeft)
2015 return left->NumSuccsLeft > right->NumSuccsLeft;
2016
Andrew Trick2085a962010-12-21 22:25:04 +00002017 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002018 "NodeQueueId cannot be zero");
2019 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002020}
2021
Evan Chengd38c22b2006-05-11 23:55:42 +00002022//===----------------------------------------------------------------------===//
2023// Public Constructor Functions
2024//===----------------------------------------------------------------------===//
2025
Dan Gohmandfaf6462009-02-11 04:27:20 +00002026llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002027llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002028 const TargetMachine &TM = IS->TM;
2029 const TargetInstrInfo *TII = TM.getInstrInfo();
2030 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002031
Evan Chenga77f3d32010-07-21 06:09:07 +00002032 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002033 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002034 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002035 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002036 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002037}
2038
Dan Gohmandfaf6462009-02-11 04:27:20 +00002039llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002040llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002041 const TargetMachine &TM = IS->TM;
2042 const TargetInstrInfo *TII = TM.getInstrInfo();
2043 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002044
Evan Chenga77f3d32010-07-21 06:09:07 +00002045 TDRegReductionPriorityQueue *PQ =
2046 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002047 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00002048 PQ->setScheduleDAG(SD);
2049 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002050}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002051
2052llvm::ScheduleDAGSDNodes *
2053llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2054 const TargetMachine &TM = IS->TM;
2055 const TargetInstrInfo *TII = TM.getInstrInfo();
2056 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002057
Evan Chenga77f3d32010-07-21 06:09:07 +00002058 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002059 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002060 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
2061 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002062 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00002063}
2064
2065llvm::ScheduleDAGSDNodes *
2066llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2067 const TargetMachine &TM = IS->TM;
2068 const TargetInstrInfo *TII = TM.getInstrInfo();
2069 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002070 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002071
Evan Chenga77f3d32010-07-21 06:09:07 +00002072 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002073 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002074 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002075 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002076 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002077}
Evan Cheng37b740c2010-07-24 00:39:05 +00002078
2079llvm::ScheduleDAGSDNodes *
2080llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2081 const TargetMachine &TM = IS->TM;
2082 const TargetInstrInfo *TII = TM.getInstrInfo();
2083 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2084 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002085
Evan Cheng37b740c2010-07-24 00:39:05 +00002086 ILPBURRPriorityQueue *PQ =
2087 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2088 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2089 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002090 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00002091}