blob: f5a5db89fa0c9c848f1fddcedb785e015eb4f119 [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"
Andrew Trick10ffc2b2010-12-24 05:03:26 +000023#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetInstrInfo.h"
Evan Chenga77f3d32010-07-21 06:09:07 +000028#include "llvm/Target/TargetLowering.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000029#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000030#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000034#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000035#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000036using namespace llvm;
37
Dan Gohmanfd227e92008-03-25 17:10:29 +000038STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000039STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000040STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000041STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000042
Jim Laskey95eda5b2006-08-01 14:21:23 +000043static RegisterScheduler
44 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000045 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000046 createBURRListDAGScheduler);
47static RegisterScheduler
48 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000049 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000050 createTDRRListDAGScheduler);
Bill Wendling8cbc25d2010-01-23 10:26:57 +000051static RegisterScheduler
52 sourceListDAGScheduler("source",
53 "Similar to list-burr but schedules in source "
54 "order when possible",
55 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000056
Evan Chengbdd062d2010-05-20 06:13:19 +000057static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000058 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000059 "Bottom-up register pressure aware list scheduling "
60 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000061 createHybridListDAGScheduler);
62
Evan Cheng37b740c2010-07-24 00:39:05 +000063static RegisterScheduler
64 ILPListDAGScheduler("list-ilp",
65 "Bottom-up register pressure aware list scheduling "
66 "which tries to balance ILP and register pressure",
67 createILPListDAGScheduler);
68
Andrew Trick47ff14b2011-01-21 05:51:33 +000069static cl::opt<bool> DisableSchedCycles(
Andrew Trickbd428ec2011-01-21 06:19:05 +000070 "disable-sched-cycles", cl::Hidden, cl::init(false),
Andrew Trick47ff14b2011-01-21 05:51:33 +000071 cl::desc("Disable cycle-level precision during preRA scheduling"));
Andrew Trick10ffc2b2010-12-24 05:03:26 +000072
Andrew Trick641e2d42011-03-05 08:00:22 +000073// Temporary sched=list-ilp flags until the heuristics are robust.
74static cl::opt<bool> DisableSchedRegPressure(
75 "disable-sched-reg-pressure", cl::Hidden, cl::init(false),
76 cl::desc("Disable regpressure priority in sched=list-ilp"));
77static cl::opt<bool> DisableSchedLiveUses(
Andrew Trickdd017322011-03-06 00:03:32 +000078 "disable-sched-live-uses", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000079 cl::desc("Disable live use priority in sched=list-ilp"));
80static cl::opt<bool> DisableSchedStalls(
Andrew Trickdd017322011-03-06 00:03:32 +000081 "disable-sched-stalls", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000082 cl::desc("Disable no-stall priority in sched=list-ilp"));
83static cl::opt<bool> DisableSchedCriticalPath(
84 "disable-sched-critical-path", cl::Hidden, cl::init(false),
85 cl::desc("Disable critical path priority in sched=list-ilp"));
86static cl::opt<bool> DisableSchedHeight(
87 "disable-sched-height", cl::Hidden, cl::init(false),
88 cl::desc("Disable scheduled-height priority in sched=list-ilp"));
89
90static cl::opt<int> MaxReorderWindow(
91 "max-sched-reorder", cl::Hidden, cl::init(6),
92 cl::desc("Number of instructions to allow ahead of the critical path "
93 "in sched=list-ilp"));
94
95static cl::opt<unsigned> AvgIPC(
96 "sched-avg-ipc", cl::Hidden, cl::init(1),
97 cl::desc("Average inst/cycle whan no target itinerary exists."));
98
99#ifndef NDEBUG
100namespace {
101 // For sched=list-ilp, Count the number of times each factor comes into play.
Andrew Trick52b3e382011-03-08 01:51:56 +0000102 enum { FactPressureDiff, FactRegUses, FactHeight, FactDepth, FactStatic,
103 FactOther, NumFactors };
Andrew Trick641e2d42011-03-05 08:00:22 +0000104}
105static const char *FactorName[NumFactors] =
Andrew Trick52b3e382011-03-08 01:51:56 +0000106{"PressureDiff", "RegUses", "Height", "Depth","Static", "Other"};
Andrew Trick641e2d42011-03-05 08:00:22 +0000107static int FactorCount[NumFactors];
108#endif //!NDEBUG
109
Evan Chengd38c22b2006-05-11 23:55:42 +0000110namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +0000111//===----------------------------------------------------------------------===//
112/// ScheduleDAGRRList - The actual register reduction list scheduler
113/// implementation. This supports both top-down and bottom-up scheduling.
114///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000115class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +0000116private:
117 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
118 /// it is top-down.
119 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +0000120
Evan Chengbdd062d2010-05-20 06:13:19 +0000121 /// NeedLatency - True if the scheduler will make use of latency information.
122 ///
123 bool NeedLatency;
124
Evan Chengd38c22b2006-05-11 23:55:42 +0000125 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +0000126 SchedulingPriorityQueue *AvailableQueue;
127
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000128 /// PendingQueue - This contains all of the instructions whose operands have
129 /// been issued, but their results are not ready yet (due to the latency of
130 /// the operation). Once the operands becomes available, the instruction is
131 /// added to the AvailableQueue.
132 std::vector<SUnit*> PendingQueue;
133
134 /// HazardRec - The hazard recognizer to use.
135 ScheduleHazardRecognizer *HazardRec;
136
Andrew Trick528fad92010-12-23 05:42:20 +0000137 /// CurCycle - The current scheduler state corresponds to this cycle.
138 unsigned CurCycle;
139
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000140 /// MinAvailableCycle - Cycle of the soonest available instruction.
141 unsigned MinAvailableCycle;
142
Andrew Trick641e2d42011-03-05 08:00:22 +0000143 /// IssueCount - Count instructions issued in this cycle
144 /// Currently valid only for bottom-up scheduling.
145 unsigned IssueCount;
146
Dan Gohmanc07f6862008-09-23 18:50:48 +0000147 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +0000148 /// that are "live". These nodes must be scheduled before any other nodes that
149 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000150 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000151 std::vector<SUnit*> LiveRegDefs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000152 std::vector<SUnit*> LiveRegGens;
Evan Cheng5924bf72007-09-25 01:54:36 +0000153
Dan Gohmanad2134d2008-11-25 00:52:40 +0000154 /// Topo - A topological ordering for SUnits which permits fast IsReachable
155 /// and similar queries.
156 ScheduleDAGTopologicalSort Topo;
157
Evan Chengd38c22b2006-05-11 23:55:42 +0000158public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000159 ScheduleDAGRRList(MachineFunction &mf, bool needlatency,
160 SchedulingPriorityQueue *availqueue,
161 CodeGenOpt::Level OptLevel)
162 : ScheduleDAGSDNodes(mf), isBottomUp(availqueue->isBottomUp()),
163 NeedLatency(needlatency), AvailableQueue(availqueue), CurCycle(0),
164 Topo(SUnits) {
165
166 const TargetMachine &tm = mf.getTarget();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000167 if (DisableSchedCycles || !NeedLatency)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000168 HazardRec = new ScheduleHazardRecognizer();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000169 else
170 HazardRec = tm.getInstrInfo()->CreateTargetHazardRecognizer(&tm, this);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000171 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000172
173 ~ScheduleDAGRRList() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000174 delete HazardRec;
Evan Chengd38c22b2006-05-11 23:55:42 +0000175 delete AvailableQueue;
176 }
177
178 void Schedule();
179
Andrew Trick9ccce772011-01-14 21:11:41 +0000180 ScheduleHazardRecognizer *getHazardRec() { return HazardRec; }
181
Roman Levenstein733a4d62008-03-26 11:23:38 +0000182 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000183 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
184 return Topo.IsReachable(SU, TargetSU);
185 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000186
Dan Gohman60d68442009-01-29 19:49:27 +0000187 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000188 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000189 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
190 return Topo.WillCreateCycle(SU, TargetSU);
191 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000192
Dan Gohman2d170892008-12-09 22:54:47 +0000193 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000194 /// This returns true if this is a new predecessor.
195 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000196 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000197 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000198 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000199 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000200
Dan Gohman2d170892008-12-09 22:54:47 +0000201 /// RemovePred - removes a predecessor edge from SUnit SU.
202 /// This returns true if an edge was removed.
203 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000204 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000205 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000206 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000207 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000208
Evan Chengd38c22b2006-05-11 23:55:42 +0000209private:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000210 bool isReady(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000211 return DisableSchedCycles || !AvailableQueue->hasReadyFilter() ||
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000212 AvailableQueue->isReady(SU);
213 }
214
Dan Gohman60d68442009-01-29 19:49:27 +0000215 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Andrew Tricka52f3252010-12-23 04:16:14 +0000216 void ReleasePredecessors(SUnit *SU);
Dan Gohman60d68442009-01-29 19:49:27 +0000217 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000218 void ReleaseSuccessors(SUnit *SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000219 void ReleasePending();
220 void AdvanceToCycle(unsigned NextCycle);
221 void AdvancePastStalls(SUnit *SU);
222 void EmitNode(SUnit *SU);
Andrew Trick528fad92010-12-23 05:42:20 +0000223 void ScheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000224 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000225 void UnscheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000226 void RestoreHazardCheckerBottomUp();
227 void BacktrackBottomUp(SUnit*, SUnit*);
Evan Cheng8e136a92007-09-26 21:36:17 +0000228 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000229 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
230 const TargetRegisterClass*,
231 const TargetRegisterClass*,
232 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000233 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000234
Andrew Trick528fad92010-12-23 05:42:20 +0000235 SUnit *PickNodeToScheduleBottomUp();
Evan Chengd38c22b2006-05-11 23:55:42 +0000236 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000237
Andrew Trick528fad92010-12-23 05:42:20 +0000238 void ScheduleNodeTopDown(SUnit*);
239 void ListScheduleTopDown();
240
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000241
242 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000243 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000244 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000245 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000246 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000247 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000248 if (NewNode->NodeNum >= NumSUnits)
249 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000250 return NewNode;
251 }
252
Roman Levenstein733a4d62008-03-26 11:23:38 +0000253 /// CreateClone - Creates a new SUnit from an existing one.
254 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000255 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000256 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000257 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000258 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000259 if (NewNode->NodeNum >= NumSUnits)
260 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000261 return NewNode;
262 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000263
Evan Chengbdd062d2010-05-20 06:13:19 +0000264 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
265 /// need actual latency information but the hybrid scheduler does.
266 bool ForceUnitLatencies() const {
267 return !NeedLatency;
268 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000269};
270} // end anonymous namespace
271
272
273/// Schedule - Schedule the DAG using list scheduling.
274void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000275 DEBUG(dbgs()
276 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000277 << " '" << BB->getName() << "' **********\n");
Andrew Trick641e2d42011-03-05 08:00:22 +0000278#ifndef NDEBUG
279 for (int i = 0; i < NumFactors; ++i) {
280 FactorCount[i] = 0;
281 }
282#endif //!NDEBUG
Evan Cheng5924bf72007-09-25 01:54:36 +0000283
Andrew Trick528fad92010-12-23 05:42:20 +0000284 CurCycle = 0;
Andrew Trick641e2d42011-03-05 08:00:22 +0000285 IssueCount = 0;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000286 MinAvailableCycle = DisableSchedCycles ? 0 : UINT_MAX;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000287 NumLiveRegs = 0;
Andrew Trick2085a962010-12-21 22:25:04 +0000288 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
Andrew Tricka52f3252010-12-23 04:16:14 +0000289 LiveRegGens.resize(TRI->getNumRegs(), NULL);
Evan Cheng5924bf72007-09-25 01:54:36 +0000290
Dan Gohman04543e72008-12-23 18:36:58 +0000291 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000292 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000293
Evan Chengd38c22b2006-05-11 23:55:42 +0000294 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000295 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000296 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000297
Dan Gohman46520a22008-06-21 19:18:17 +0000298 AvailableQueue->initNodes(SUnits);
Andrew Trick2085a962010-12-21 22:25:04 +0000299
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000300 HazardRec->Reset();
301
Evan Chengd38c22b2006-05-11 23:55:42 +0000302 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
303 if (isBottomUp)
304 ListScheduleBottomUp();
305 else
306 ListScheduleTopDown();
Andrew Trick2085a962010-12-21 22:25:04 +0000307
Andrew Trick641e2d42011-03-05 08:00:22 +0000308#ifndef NDEBUG
309 for (int i = 0; i < NumFactors; ++i) {
310 DEBUG(dbgs() << FactorName[i] << "\t" << FactorCount[i] << "\n");
311 }
312#endif // !NDEBUG
Evan Chengd38c22b2006-05-11 23:55:42 +0000313 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000314}
Evan Chengd38c22b2006-05-11 23:55:42 +0000315
316//===----------------------------------------------------------------------===//
317// Bottom-Up Scheduling
318//===----------------------------------------------------------------------===//
319
Evan Chengd38c22b2006-05-11 23:55:42 +0000320/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000321/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000322void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000323 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000324
Evan Chengd38c22b2006-05-11 23:55:42 +0000325#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000326 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000327 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000328 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000329 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000330 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000331 }
332#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000333 --PredSU->NumSuccsLeft;
334
Evan Chengbdd062d2010-05-20 06:13:19 +0000335 if (!ForceUnitLatencies()) {
336 // Updating predecessor's height. This is now the cycle when the
337 // predecessor can be scheduled without causing a pipeline stall.
338 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
339 }
340
Dan Gohmanb9543432009-02-10 23:27:53 +0000341 // If all the node's successors are scheduled, this node is ready
342 // to be scheduled. Ignore the special EntrySU node.
343 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000344 PredSU->isAvailable = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000345
346 unsigned Height = PredSU->getHeight();
347 if (Height < MinAvailableCycle)
348 MinAvailableCycle = Height;
349
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000350 if (isReady(PredSU)) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000351 AvailableQueue->push(PredSU);
352 }
353 // CapturePred and others may have left the node in the pending queue, avoid
354 // adding it twice.
355 else if (!PredSU->isPending) {
356 PredSU->isPending = true;
357 PendingQueue.push_back(PredSU);
358 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000359 }
360}
361
Andrew Trick033efdf2010-12-23 03:15:51 +0000362/// Call ReleasePred for each predecessor, then update register live def/gen.
363/// Always update LiveRegDefs for a register dependence even if the current SU
364/// also defines the register. This effectively create one large live range
365/// across a sequence of two-address node. This is important because the
366/// entire chain must be scheduled together. Example:
367///
368/// flags = (3) add
369/// flags = (2) addc flags
370/// flags = (1) addc flags
371///
372/// results in
373///
374/// LiveRegDefs[flags] = 3
Andrew Tricka52f3252010-12-23 04:16:14 +0000375/// LiveRegGens[flags] = 1
Andrew Trick033efdf2010-12-23 03:15:51 +0000376///
377/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
378/// interference on flags.
Andrew Tricka52f3252010-12-23 04:16:14 +0000379void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000380 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000381 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000382 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000383 ReleasePred(SU, &*I);
384 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000385 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000386 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000387 // clobber the register is scheduled between the predecessor and
388 // this node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000389 SUnit *RegDef = LiveRegDefs[I->getReg()]; (void)RegDef;
Andrew Trick033efdf2010-12-23 03:15:51 +0000390 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
391 "interference on register dependence");
Andrew Tricka52f3252010-12-23 04:16:14 +0000392 LiveRegDefs[I->getReg()] = I->getSUnit();
393 if (!LiveRegGens[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000394 ++NumLiveRegs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000395 LiveRegGens[I->getReg()] = SU;
Evan Cheng5924bf72007-09-25 01:54:36 +0000396 }
397 }
398 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000399}
400
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000401/// Check to see if any of the pending instructions are ready to issue. If
402/// so, add them to the available queue.
403void ScheduleDAGRRList::ReleasePending() {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000404 if (DisableSchedCycles) {
Andrew Trick5ce945c2010-12-24 07:10:19 +0000405 assert(PendingQueue.empty() && "pending instrs not allowed in this mode");
406 return;
407 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000408
409 // If the available queue is empty, it is safe to reset MinAvailableCycle.
410 if (AvailableQueue->empty())
411 MinAvailableCycle = UINT_MAX;
412
413 // Check to see if any of the pending instructions are ready to issue. If
414 // so, add them to the available queue.
415 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
416 unsigned ReadyCycle =
417 isBottomUp ? PendingQueue[i]->getHeight() : PendingQueue[i]->getDepth();
418 if (ReadyCycle < MinAvailableCycle)
419 MinAvailableCycle = ReadyCycle;
420
421 if (PendingQueue[i]->isAvailable) {
422 if (!isReady(PendingQueue[i]))
423 continue;
424 AvailableQueue->push(PendingQueue[i]);
425 }
426 PendingQueue[i]->isPending = false;
427 PendingQueue[i] = PendingQueue.back();
428 PendingQueue.pop_back();
429 --i; --e;
430 }
431}
432
433/// Move the scheduler state forward by the specified number of Cycles.
434void ScheduleDAGRRList::AdvanceToCycle(unsigned NextCycle) {
435 if (NextCycle <= CurCycle)
436 return;
437
Andrew Trick641e2d42011-03-05 08:00:22 +0000438 IssueCount = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000439 AvailableQueue->setCurCycle(NextCycle);
Andrew Trick47ff14b2011-01-21 05:51:33 +0000440 if (!HazardRec->isEnabled()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000441 // Bypass lots of virtual calls in case of long latency.
442 CurCycle = NextCycle;
443 }
444 else {
445 for (; CurCycle != NextCycle; ++CurCycle) {
446 if (isBottomUp)
447 HazardRec->RecedeCycle();
448 else
449 HazardRec->AdvanceCycle();
450 }
451 }
452 // FIXME: Instead of visiting the pending Q each time, set a dirty flag on the
453 // available Q to release pending nodes at least once before popping.
454 ReleasePending();
455}
456
457/// Move the scheduler state forward until the specified node's dependents are
458/// ready and can be scheduled with no resource conflicts.
459void ScheduleDAGRRList::AdvancePastStalls(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000460 if (DisableSchedCycles)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000461 return;
462
463 unsigned ReadyCycle = isBottomUp ? SU->getHeight() : SU->getDepth();
464
465 // Bump CurCycle to account for latency. We assume the latency of other
466 // available instructions may be hidden by the stall (not a full pipe stall).
467 // This updates the hazard recognizer's cycle before reserving resources for
468 // this instruction.
469 AdvanceToCycle(ReadyCycle);
470
471 // Calls are scheduled in their preceding cycle, so don't conflict with
472 // hazards from instructions after the call. EmitNode will reset the
473 // scoreboard state before emitting the call.
474 if (isBottomUp && SU->isCall)
475 return;
476
477 // FIXME: For resource conflicts in very long non-pipelined stages, we
478 // should probably skip ahead here to avoid useless scoreboard checks.
479 int Stalls = 0;
480 while (true) {
481 ScheduleHazardRecognizer::HazardType HT =
482 HazardRec->getHazardType(SU, isBottomUp ? -Stalls : Stalls);
483
484 if (HT == ScheduleHazardRecognizer::NoHazard)
485 break;
486
487 ++Stalls;
488 }
489 AdvanceToCycle(CurCycle + Stalls);
490}
491
492/// Record this SUnit in the HazardRecognizer.
493/// Does not update CurCycle.
494void ScheduleDAGRRList::EmitNode(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000495 if (!HazardRec->isEnabled())
Andrew Trickc9405662010-12-24 06:46:50 +0000496 return;
497
498 // Check for phys reg copy.
499 if (!SU->getNode())
500 return;
501
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000502 switch (SU->getNode()->getOpcode()) {
503 default:
504 assert(SU->getNode()->isMachineOpcode() &&
505 "This target-independent node should not be scheduled.");
506 break;
507 case ISD::MERGE_VALUES:
508 case ISD::TokenFactor:
509 case ISD::CopyToReg:
510 case ISD::CopyFromReg:
511 case ISD::EH_LABEL:
512 // Noops don't affect the scoreboard state. Copies are likely to be
513 // removed.
514 return;
515 case ISD::INLINEASM:
516 // For inline asm, clear the pipeline state.
517 HazardRec->Reset();
518 return;
519 }
520 if (isBottomUp && SU->isCall) {
521 // Calls are scheduled with their preceding instructions. For bottom-up
522 // scheduling, clear the pipeline state before emitting.
523 HazardRec->Reset();
524 }
525
526 HazardRec->EmitInstruction(SU);
527
528 if (!isBottomUp && SU->isCall) {
529 HazardRec->Reset();
530 }
531}
532
Dan Gohmanb9543432009-02-10 23:27:53 +0000533/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
534/// count of its predecessors. If a predecessor pending count is zero, add it to
535/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +0000536void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000537 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000538 DEBUG(SU->dump(this));
539
Evan Chengbdd062d2010-05-20 06:13:19 +0000540#ifndef NDEBUG
541 if (CurCycle < SU->getHeight())
542 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
543#endif
544
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000545 // FIXME: Do not modify node height. It may interfere with
546 // backtracking. Instead add a "ready cycle" to SUnit. Before scheduling the
Eric Christopher1b4b1e52011-03-21 18:06:21 +0000547 // node its ready cycle can aid heuristics, and after scheduling it can
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000548 // indicate the scheduled cycle.
Dan Gohmanb9543432009-02-10 23:27:53 +0000549 SU->setHeightToAtLeast(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000550
551 // Reserve resources for the scheduled intruction.
552 EmitNode(SU);
553
Dan Gohmanb9543432009-02-10 23:27:53 +0000554 Sequence.push_back(SU);
555
Evan Cheng28590382010-07-21 23:53:58 +0000556 AvailableQueue->ScheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000557
Andrew Trick641e2d42011-03-05 08:00:22 +0000558 // If HazardRec is disabled, and each inst counts as one cycle, then
559 // advance CurCycle before ReleasePredecessors to avoid useles pushed to
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000560 // PendingQueue for schedulers that implement HasReadyFilter.
Andrew Trick641e2d42011-03-05 08:00:22 +0000561 if (!HazardRec->isEnabled() && AvgIPC < 2)
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000562 AdvanceToCycle(CurCycle + 1);
563
Andrew Trick033efdf2010-12-23 03:15:51 +0000564 // Update liveness of predecessors before successors to avoid treating a
565 // two-address node as a live range def.
Andrew Tricka52f3252010-12-23 04:16:14 +0000566 ReleasePredecessors(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000567
568 // Release all the implicit physical register defs that are live.
569 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
570 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000571 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
572 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
573 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
574 --NumLiveRegs;
575 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000576 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000577 }
578 }
579
Evan Chengd38c22b2006-05-11 23:55:42 +0000580 SU->isScheduled = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000581
582 // Conditions under which the scheduler should eagerly advance the cycle:
583 // (1) No available instructions
584 // (2) All pipelines full, so available instructions must have hazards.
585 //
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000586 // If HazardRec is disabled, the cycle was advanced earlier.
587 //
588 // Check AvailableQueue after ReleasePredecessors in case of zero latency.
Andrew Trick641e2d42011-03-05 08:00:22 +0000589 ++IssueCount;
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000590 if ((HazardRec->isEnabled() && HazardRec->atIssueLimit())
Andrew Trick641e2d42011-03-05 08:00:22 +0000591 || (!HazardRec->isEnabled() && AvgIPC > 1 && IssueCount == AvgIPC)
Andrew Trick47ff14b2011-01-21 05:51:33 +0000592 || AvailableQueue->empty())
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000593 AdvanceToCycle(CurCycle + 1);
Evan Chengd38c22b2006-05-11 23:55:42 +0000594}
595
Evan Cheng5924bf72007-09-25 01:54:36 +0000596/// CapturePred - This does the opposite of ReleasePred. Since SU is being
597/// unscheduled, incrcease the succ left count of its predecessors. Remove
598/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000599void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000600 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000601 if (PredSU->isAvailable) {
602 PredSU->isAvailable = false;
603 if (!PredSU->isPending)
604 AvailableQueue->remove(PredSU);
605 }
606
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000607 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000608 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000609}
610
611/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
612/// its predecessor states to reflect the change.
613void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000614 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000615 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000616
Evan Cheng5924bf72007-09-25 01:54:36 +0000617 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
618 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000619 CapturePred(&*I);
Andrew Tricka52f3252010-12-23 04:16:14 +0000620 if (I->isAssignedRegDep() && SU == LiveRegGens[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000621 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000622 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000623 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000624 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000625 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000626 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000627 }
628 }
629
630 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
631 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000632 if (I->isAssignedRegDep()) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000633 // This becomes the nearest def. Note that an earlier def may still be
634 // pending if this is a two-address node.
635 LiveRegDefs[I->getReg()] = SU;
Dan Gohman2d170892008-12-09 22:54:47 +0000636 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000637 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000638 }
Andrew Tricka52f3252010-12-23 04:16:14 +0000639 if (LiveRegGens[I->getReg()] == NULL ||
640 I->getSUnit()->getHeight() < LiveRegGens[I->getReg()]->getHeight())
641 LiveRegGens[I->getReg()] = I->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000642 }
643 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000644 if (SU->getHeight() < MinAvailableCycle)
645 MinAvailableCycle = SU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000646
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000647 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000648 SU->isScheduled = false;
649 SU->isAvailable = true;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000650 if (!DisableSchedCycles && AvailableQueue->hasReadyFilter()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000651 // Don't make available until backtracking is complete.
652 SU->isPending = true;
653 PendingQueue.push_back(SU);
654 }
655 else {
656 AvailableQueue->push(SU);
657 }
Evan Cheng28590382010-07-21 23:53:58 +0000658 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000659}
660
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000661/// After backtracking, the hazard checker needs to be restored to a state
662/// corresponding the the current cycle.
663void ScheduleDAGRRList::RestoreHazardCheckerBottomUp() {
664 HazardRec->Reset();
665
666 unsigned LookAhead = std::min((unsigned)Sequence.size(),
667 HazardRec->getMaxLookAhead());
668 if (LookAhead == 0)
669 return;
670
671 std::vector<SUnit*>::const_iterator I = (Sequence.end() - LookAhead);
672 unsigned HazardCycle = (*I)->getHeight();
673 for (std::vector<SUnit*>::const_iterator E = Sequence.end(); I != E; ++I) {
674 SUnit *SU = *I;
675 for (; SU->getHeight() > HazardCycle; ++HazardCycle) {
676 HazardRec->RecedeCycle();
677 }
678 EmitNode(SU);
679 }
680}
681
Evan Cheng8e136a92007-09-26 21:36:17 +0000682/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000683/// BTCycle in order to schedule a specific node.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000684void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, SUnit *BtSU) {
685 SUnit *OldSU = Sequence.back();
686 while (true) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000687 Sequence.pop_back();
688 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000689 // Don't try to remove SU from AvailableQueue.
690 SU->isAvailable = false;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000691 // FIXME: use ready cycle instead of height
692 CurCycle = OldSU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000693 UnscheduleNodeBottomUp(OldSU);
Evan Chengbdd062d2010-05-20 06:13:19 +0000694 AvailableQueue->setCurCycle(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000695 if (OldSU == BtSU)
696 break;
697 OldSU = Sequence.back();
Evan Cheng5924bf72007-09-25 01:54:36 +0000698 }
699
Dan Gohman60d68442009-01-29 19:49:27 +0000700 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000701
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000702 RestoreHazardCheckerBottomUp();
703
Andrew Trick5ce945c2010-12-24 07:10:19 +0000704 ReleasePending();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000705
Evan Cheng1ec79b42007-09-27 07:09:03 +0000706 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000707}
708
Evan Cheng3b245872010-02-05 01:27:11 +0000709static bool isOperandOf(const SUnit *SU, SDNode *N) {
710 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +0000711 SUNode = SUNode->getGluedNode()) {
Evan Cheng3b245872010-02-05 01:27:11 +0000712 if (SUNode->isOperandOf(N))
713 return true;
714 }
715 return false;
716}
717
Evan Cheng5924bf72007-09-25 01:54:36 +0000718/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
719/// successors to the newly created node.
720SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000721 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000722 if (!N)
723 return NULL;
724
Andrew Trickc9405662010-12-24 06:46:50 +0000725 if (SU->getNode()->getGluedNode())
726 return NULL;
727
Evan Cheng79e97132007-10-05 01:39:18 +0000728 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000729 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000730 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000731 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000732 if (VT == MVT::Glue)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000733 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000734 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000735 TryUnfold = true;
736 }
Evan Cheng79e97132007-10-05 01:39:18 +0000737 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000738 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000739 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000740 if (VT == MVT::Glue)
Evan Cheng79e97132007-10-05 01:39:18 +0000741 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000742 }
743
744 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000745 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000746 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000747 return NULL;
748
Evan Chengbdd062d2010-05-20 06:13:19 +0000749 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000750 assert(NewNodes.size() == 2 && "Expected a load folding node!");
751
752 N = NewNodes[1];
753 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000754 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000755 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000756 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000757 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
758 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000759 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000760
Dan Gohmane52e0892008-11-11 21:34:44 +0000761 // LoadNode may already exist. This can happen when there is another
762 // load from the same location and producing the same type of value
763 // but it has different alignment or volatileness.
764 bool isNewLoad = true;
765 SUnit *LoadSU;
766 if (LoadNode->getNodeId() != -1) {
767 LoadSU = &SUnits[LoadNode->getNodeId()];
768 isNewLoad = false;
769 } else {
770 LoadSU = CreateNewSUnit(LoadNode);
771 LoadNode->setNodeId(LoadSU->NodeNum);
Andrew Trickd0548ae2011-02-04 03:18:17 +0000772
773 InitNumRegDefsLeft(LoadSU);
Dan Gohmane52e0892008-11-11 21:34:44 +0000774 ComputeLatency(LoadSU);
775 }
776
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000777 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000778 assert(N->getNodeId() == -1 && "Node already inserted!");
779 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +0000780
Dan Gohman17059682008-07-17 19:10:17 +0000781 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000782 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000783 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000784 NewSU->isTwoAddress = true;
785 break;
786 }
787 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000788 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000789 NewSU->isCommutable = true;
Andrew Trickd0548ae2011-02-04 03:18:17 +0000790
791 InitNumRegDefsLeft(NewSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000792 ComputeLatency(NewSU);
793
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000794 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000795 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000796 SmallVector<SDep, 4> ChainSuccs;
797 SmallVector<SDep, 4> LoadPreds;
798 SmallVector<SDep, 4> NodePreds;
799 SmallVector<SDep, 4> NodeSuccs;
800 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
801 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000802 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000803 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000804 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000805 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000806 else
Dan Gohman2d170892008-12-09 22:54:47 +0000807 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000808 }
809 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
810 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000811 if (I->isCtrl())
812 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000813 else
Dan Gohman2d170892008-12-09 22:54:47 +0000814 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000815 }
816
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000817 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000818 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
819 const SDep &Pred = ChainPreds[i];
820 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000821 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000822 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000823 }
Evan Cheng79e97132007-10-05 01:39:18 +0000824 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000825 const SDep &Pred = LoadPreds[i];
826 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000827 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000828 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000829 }
830 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000831 const SDep &Pred = NodePreds[i];
832 RemovePred(SU, Pred);
833 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000834 }
835 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000836 SDep D = NodeSuccs[i];
837 SUnit *SuccDep = D.getSUnit();
838 D.setSUnit(SU);
839 RemovePred(SuccDep, D);
840 D.setSUnit(NewSU);
841 AddPred(SuccDep, D);
Andrew Trickd0548ae2011-02-04 03:18:17 +0000842 // Balance register pressure.
843 if (AvailableQueue->tracksRegPressure() && SuccDep->isScheduled
844 && !D.isCtrl() && NewSU->NumRegDefsLeft > 0)
845 --NewSU->NumRegDefsLeft;
Evan Cheng79e97132007-10-05 01:39:18 +0000846 }
847 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000848 SDep D = ChainSuccs[i];
849 SUnit *SuccDep = D.getSUnit();
850 D.setSUnit(SU);
851 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000852 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000853 D.setSUnit(LoadSU);
854 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000855 }
Andrew Trick2085a962010-12-21 22:25:04 +0000856 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000857
858 // Add a data dependency to reflect that NewSU reads the value defined
859 // by LoadSU.
860 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000861
Evan Cheng91e0fc92007-12-18 08:42:10 +0000862 if (isNewLoad)
863 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000864 AvailableQueue->addNode(NewSU);
865
866 ++NumUnfolds;
867
868 if (NewSU->NumSuccsLeft == 0) {
869 NewSU->isAvailable = true;
870 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000871 }
872 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000873 }
874
Evan Chengbdd062d2010-05-20 06:13:19 +0000875 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000876 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000877
878 // New SUnit has the exact same predecessors.
879 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
880 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000881 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000882 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000883
884 // Only copy scheduled successors. Cut them from old node's successor
885 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000886 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000887 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
888 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000889 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000890 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000891 SUnit *SuccSU = I->getSUnit();
892 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000893 SDep D = *I;
894 D.setSUnit(NewSU);
895 AddPred(SuccSU, D);
896 D.setSUnit(SU);
897 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000898 }
899 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000900 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000901 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000902
903 AvailableQueue->updateNode(SU);
904 AvailableQueue->addNode(NewSU);
905
Evan Cheng1ec79b42007-09-27 07:09:03 +0000906 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000907 return NewSU;
908}
909
Evan Chengb2c42c62009-01-12 03:19:55 +0000910/// InsertCopiesAndMoveSuccs - Insert register copies and move all
911/// scheduled successors of the given SUnit to the last copy.
912void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
913 const TargetRegisterClass *DestRC,
914 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000915 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000916 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000917 CopyFromSU->CopySrcRC = SrcRC;
918 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000919
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000920 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000921 CopyToSU->CopySrcRC = DestRC;
922 CopyToSU->CopyDstRC = SrcRC;
923
924 // Only copy scheduled successors. Cut them from old node's successor
925 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000926 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000927 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
928 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000929 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000930 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000931 SUnit *SuccSU = I->getSUnit();
932 if (SuccSU->isScheduled) {
933 SDep D = *I;
934 D.setSUnit(CopyToSU);
935 AddPred(SuccSU, D);
936 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000937 }
Andrew Trick13acae02011-03-23 20:42:39 +0000938 else {
939 // Avoid scheduling the def-side copy before other successors. Otherwise
940 // we could introduce another physreg interference on the copy and
941 // continue inserting copies indefinitely.
942 SDep D(CopyFromSU, SDep::Order, /*Latency=*/0,
943 /*Reg=*/0, /*isNormalMemory=*/false,
944 /*isMustAlias=*/false, /*isArtificial=*/true);
945 AddPred(SuccSU, D);
946 }
Evan Cheng8e136a92007-09-26 21:36:17 +0000947 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000948 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000949 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000950
Dan Gohman2d170892008-12-09 22:54:47 +0000951 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
952 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000953
954 AvailableQueue->updateNode(SU);
955 AvailableQueue->addNode(CopyFromSU);
956 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000957 Copies.push_back(CopyFromSU);
958 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000959
Evan Chengb2c42c62009-01-12 03:19:55 +0000960 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000961}
962
963/// getPhysicalRegisterVT - Returns the ValueType of the physical register
964/// definition of the specified node.
965/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000966static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000967 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000968 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000969 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000970 unsigned NumRes = TID.getNumDefs();
971 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000972 if (Reg == *ImpDef)
973 break;
974 ++NumRes;
975 }
976 return N->getValueType(NumRes);
977}
978
Evan Chengb8905c42009-03-04 01:41:49 +0000979/// CheckForLiveRegDef - Return true and update live register vector if the
980/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000981static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +0000982 std::vector<SUnit*> &LiveRegDefs,
983 SmallSet<unsigned, 4> &RegAdded,
984 SmallVector<unsigned, 4> &LRegs,
985 const TargetRegisterInfo *TRI) {
Andrew Trick12acde112010-12-23 03:43:21 +0000986 for (const unsigned *AliasI = TRI->getOverlaps(Reg); *AliasI; ++AliasI) {
987
988 // Check if Ref is live.
989 if (!LiveRegDefs[Reg]) continue;
990
991 // Allow multiple uses of the same def.
992 if (LiveRegDefs[Reg] == SU) continue;
993
994 // Add Reg to the set of interfering live regs.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000995 if (RegAdded.insert(Reg))
Evan Chengb8905c42009-03-04 01:41:49 +0000996 LRegs.push_back(Reg);
Evan Chengb8905c42009-03-04 01:41:49 +0000997 }
Evan Chengb8905c42009-03-04 01:41:49 +0000998}
999
Evan Cheng5924bf72007-09-25 01:54:36 +00001000/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
1001/// scheduling of the given node to satisfy live physical register dependencies.
1002/// If the specific node is the last one that's available to schedule, do
1003/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001004bool ScheduleDAGRRList::
1005DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +00001006 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +00001007 return false;
1008
Evan Chenge6f92252007-09-27 18:46:06 +00001009 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +00001010 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001011 //
1012 // If SU is the currently live definition of the same register that it uses,
1013 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +00001014 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1015 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001016 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +00001017 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
1018 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001019 }
1020
Chris Lattner11a33812010-12-23 17:24:32 +00001021 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +00001022 if (Node->getOpcode() == ISD::INLINEASM) {
1023 // Inline asm can clobber physical defs.
1024 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001025 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +00001026 --NumOps; // Ignore the glue operand.
Evan Chengb8905c42009-03-04 01:41:49 +00001027
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001028 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +00001029 unsigned Flags =
1030 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001031 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +00001032
1033 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001034 if (InlineAsm::isRegDefKind(Flags) ||
1035 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +00001036 // Check for def of register or earlyclobber register.
1037 for (; NumVals; --NumVals, ++i) {
1038 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1039 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1040 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
1041 }
1042 } else
1043 i += NumVals;
1044 }
1045 continue;
1046 }
1047
Dan Gohman072734e2008-11-13 23:24:17 +00001048 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +00001049 continue;
Dan Gohman17059682008-07-17 19:10:17 +00001050 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +00001051 if (!TID.ImplicitDefs)
1052 continue;
Evan Chengb8905c42009-03-04 01:41:49 +00001053 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
1054 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001055 }
Andrew Trick2085a962010-12-21 22:25:04 +00001056
Evan Cheng5924bf72007-09-25 01:54:36 +00001057 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +00001058}
1059
Andrew Trick528fad92010-12-23 05:42:20 +00001060/// Return a node that can be scheduled in this cycle. Requirements:
1061/// (1) Ready: latency has been satisfied
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001062/// (2) No Hazards: resources are available
Andrew Trick528fad92010-12-23 05:42:20 +00001063/// (3) No Interferences: may unschedule to break register interferences.
1064SUnit *ScheduleDAGRRList::PickNodeToScheduleBottomUp() {
1065 SmallVector<SUnit*, 4> Interferences;
1066 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
1067
1068 SUnit *CurSU = AvailableQueue->pop();
1069 while (CurSU) {
1070 SmallVector<unsigned, 4> LRegs;
1071 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
1072 break;
1073 LRegsMap.insert(std::make_pair(CurSU, LRegs));
1074
1075 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
1076 Interferences.push_back(CurSU);
1077 CurSU = AvailableQueue->pop();
1078 }
1079 if (CurSU) {
1080 // Add the nodes that aren't ready back onto the available list.
1081 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1082 Interferences[i]->isPending = false;
1083 assert(Interferences[i]->isAvailable && "must still be available");
1084 AvailableQueue->push(Interferences[i]);
1085 }
1086 return CurSU;
1087 }
1088
1089 // All candidates are delayed due to live physical reg dependencies.
1090 // Try backtracking, code duplication, or inserting cross class copies
1091 // to resolve it.
1092 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1093 SUnit *TrySU = Interferences[i];
1094 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1095
1096 // Try unscheduling up to the point where it's safe to schedule
1097 // this node.
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001098 SUnit *BtSU = NULL;
1099 unsigned LiveCycle = UINT_MAX;
Andrew Trick528fad92010-12-23 05:42:20 +00001100 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
1101 unsigned Reg = LRegs[j];
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001102 if (LiveRegGens[Reg]->getHeight() < LiveCycle) {
1103 BtSU = LiveRegGens[Reg];
1104 LiveCycle = BtSU->getHeight();
1105 }
Andrew Trick528fad92010-12-23 05:42:20 +00001106 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001107 if (!WillCreateCycle(TrySU, BtSU)) {
1108 BacktrackBottomUp(TrySU, BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001109
1110 // Force the current node to be scheduled before the node that
1111 // requires the physical reg dep.
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001112 if (BtSU->isAvailable) {
1113 BtSU->isAvailable = false;
1114 if (!BtSU->isPending)
1115 AvailableQueue->remove(BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001116 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001117 AddPred(TrySU, SDep(BtSU, SDep::Order, /*Latency=*/1,
Andrew Trick528fad92010-12-23 05:42:20 +00001118 /*Reg=*/0, /*isNormalMemory=*/false,
1119 /*isMustAlias=*/false, /*isArtificial=*/true));
1120
1121 // If one or more successors has been unscheduled, then the current
1122 // node is no longer avaialable. Schedule a successor that's now
1123 // available instead.
1124 if (!TrySU->isAvailable) {
1125 CurSU = AvailableQueue->pop();
1126 }
1127 else {
1128 CurSU = TrySU;
1129 TrySU->isPending = false;
1130 Interferences.erase(Interferences.begin()+i);
1131 }
1132 break;
1133 }
1134 }
1135
1136 if (!CurSU) {
1137 // Can't backtrack. If it's too expensive to copy the value, then try
1138 // duplicate the nodes that produces these "too expensive to copy"
1139 // values to break the dependency. In case even that doesn't work,
1140 // insert cross class copies.
1141 // If it's not too expensive, i.e. cost != -1, issue copies.
1142 SUnit *TrySU = Interferences[0];
1143 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1144 assert(LRegs.size() == 1 && "Can't handle this yet!");
1145 unsigned Reg = LRegs[0];
1146 SUnit *LRDef = LiveRegDefs[Reg];
1147 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
1148 const TargetRegisterClass *RC =
1149 TRI->getMinimalPhysRegClass(Reg, VT);
1150 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
1151
Evan Chengb4c6a342011-03-10 00:16:32 +00001152 // If cross copy register class is the same as RC, then it must be possible
1153 // copy the value directly. Do not try duplicate the def.
1154 // If cross copy register class is not the same as RC, then it's possible to
1155 // copy the value but it require cross register class copies and it is
1156 // expensive.
1157 // If cross copy register class is null, then it's not possible to copy
1158 // the value at all.
Andrew Trick528fad92010-12-23 05:42:20 +00001159 SUnit *NewDef = 0;
Evan Chengb4c6a342011-03-10 00:16:32 +00001160 if (DestRC != RC) {
Andrew Trick528fad92010-12-23 05:42:20 +00001161 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +00001162 if (!DestRC && !NewDef)
1163 report_fatal_error("Can't handle live physical register dependency!");
1164 }
Andrew Trick528fad92010-12-23 05:42:20 +00001165 if (!NewDef) {
1166 // Issue copies, these can be expensive cross register class copies.
1167 SmallVector<SUnit*, 2> Copies;
1168 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1169 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
1170 << " to SU #" << Copies.front()->NodeNum << "\n");
1171 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
1172 /*Reg=*/0, /*isNormalMemory=*/false,
1173 /*isMustAlias=*/false,
1174 /*isArtificial=*/true));
1175 NewDef = Copies.back();
1176 }
1177
1178 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
1179 << " to SU #" << TrySU->NodeNum << "\n");
1180 LiveRegDefs[Reg] = NewDef;
1181 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
1182 /*Reg=*/0, /*isNormalMemory=*/false,
1183 /*isMustAlias=*/false,
1184 /*isArtificial=*/true));
1185 TrySU->isAvailable = false;
1186 CurSU = NewDef;
1187 }
1188
1189 assert(CurSU && "Unable to resolve live physical register dependencies!");
1190
1191 // Add the nodes that aren't ready back onto the available list.
1192 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1193 Interferences[i]->isPending = false;
1194 // May no longer be available due to backtracking.
1195 if (Interferences[i]->isAvailable) {
1196 AvailableQueue->push(Interferences[i]);
1197 }
1198 }
1199 return CurSU;
1200}
Evan Cheng1ec79b42007-09-27 07:09:03 +00001201
Evan Chengd38c22b2006-05-11 23:55:42 +00001202/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
1203/// schedulers.
1204void ScheduleDAGRRList::ListScheduleBottomUp() {
Dan Gohmanb9543432009-02-10 23:27:53 +00001205 // Release any predecessors of the special Exit node.
Andrew Tricka52f3252010-12-23 04:16:14 +00001206 ReleasePredecessors(&ExitSU);
Dan Gohmanb9543432009-02-10 23:27:53 +00001207
Evan Chengd38c22b2006-05-11 23:55:42 +00001208 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +00001209 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +00001210 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +00001211 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
1212 RootSU->isAvailable = true;
1213 AvailableQueue->push(RootSU);
1214 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001215
1216 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001217 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +00001218 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001219 while (!AvailableQueue->empty()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001220 DEBUG(dbgs() << "\n*** Examining Available\n";
1221 AvailableQueue->dump(this));
1222
Andrew Trick528fad92010-12-23 05:42:20 +00001223 // Pick the best node to schedule taking all constraints into
1224 // consideration.
1225 SUnit *SU = PickNodeToScheduleBottomUp();
Evan Cheng1ec79b42007-09-27 07:09:03 +00001226
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001227 AdvancePastStalls(SU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001228
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001229 ScheduleNodeBottomUp(SU);
1230
1231 while (AvailableQueue->empty() && !PendingQueue.empty()) {
1232 // Advance the cycle to free resources. Skip ahead to the next ready SU.
1233 assert(MinAvailableCycle < UINT_MAX && "MinAvailableCycle uninitialized");
1234 AdvanceToCycle(std::max(CurCycle + 1, MinAvailableCycle));
1235 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001236 }
1237
Evan Chengd38c22b2006-05-11 23:55:42 +00001238 // Reverse the order if it is bottom up.
1239 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +00001240
Evan Chengd38c22b2006-05-11 23:55:42 +00001241#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +00001242 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +00001243#endif
1244}
1245
1246//===----------------------------------------------------------------------===//
1247// Top-Down Scheduling
1248//===----------------------------------------------------------------------===//
1249
1250/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +00001251/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +00001252void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +00001253 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +00001254
Evan Chengd38c22b2006-05-11 23:55:42 +00001255#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +00001256 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +00001257 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +00001258 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +00001259 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001260 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +00001261 }
1262#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +00001263 --SuccSU->NumPredsLeft;
1264
Dan Gohmanb9543432009-02-10 23:27:53 +00001265 // If all the node's predecessors are scheduled, this node is ready
1266 // to be scheduled. Ignore the special ExitSU node.
1267 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001268 SuccSU->isAvailable = true;
1269 AvailableQueue->push(SuccSU);
1270 }
1271}
1272
Dan Gohmanb9543432009-02-10 23:27:53 +00001273void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
1274 // Top down: release successors
1275 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1276 I != E; ++I) {
1277 assert(!I->isAssignedRegDep() &&
1278 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
1279
1280 ReleaseSucc(SU, &*I);
1281 }
1282}
1283
Evan Chengd38c22b2006-05-11 23:55:42 +00001284/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1285/// count of its successors. If a successor pending count is zero, add it to
1286/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +00001287void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +00001288 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +00001289 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +00001290
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001291 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
1292 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +00001293 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001294
Dan Gohmanb9543432009-02-10 23:27:53 +00001295 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001296 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +00001297 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +00001298}
1299
Dan Gohman54a187e2007-08-20 19:28:38 +00001300/// ListScheduleTopDown - The main loop of list scheduling for top-down
1301/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +00001302void ScheduleDAGRRList::ListScheduleTopDown() {
Evan Chengbdd062d2010-05-20 06:13:19 +00001303 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +00001304
Dan Gohmanb9543432009-02-10 23:27:53 +00001305 // Release any successors of the special Entry node.
1306 ReleaseSuccessors(&EntrySU);
1307
Evan Chengd38c22b2006-05-11 23:55:42 +00001308 // All leaves to Available queue.
1309 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1310 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +00001311 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +00001312 AvailableQueue->push(&SUnits[i]);
1313 SUnits[i].isAvailable = true;
1314 }
1315 }
Andrew Trick2085a962010-12-21 22:25:04 +00001316
Evan Chengd38c22b2006-05-11 23:55:42 +00001317 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001318 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +00001319 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001320 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +00001321 SUnit *CurSU = AvailableQueue->pop();
Andrew Trick2085a962010-12-21 22:25:04 +00001322
Dan Gohmanc602dd42008-11-21 00:10:42 +00001323 if (CurSU)
Andrew Trick528fad92010-12-23 05:42:20 +00001324 ScheduleNodeTopDown(CurSU);
Dan Gohman4370f262008-04-15 01:22:18 +00001325 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +00001326 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +00001327 }
Andrew Trick2085a962010-12-21 22:25:04 +00001328
Evan Chengd38c22b2006-05-11 23:55:42 +00001329#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +00001330 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +00001331#endif
1332}
1333
1334
Evan Chengd38c22b2006-05-11 23:55:42 +00001335//===----------------------------------------------------------------------===//
Andrew Trick9ccce772011-01-14 21:11:41 +00001336// RegReductionPriorityQueue Definition
Evan Chengd38c22b2006-05-11 23:55:42 +00001337//===----------------------------------------------------------------------===//
1338//
1339// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1340// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +00001341//
Evan Chengd38c22b2006-05-11 23:55:42 +00001342namespace {
Andrew Trick9ccce772011-01-14 21:11:41 +00001343class RegReductionPQBase;
Andrew Trick2085a962010-12-21 22:25:04 +00001344
Andrew Trick9ccce772011-01-14 21:11:41 +00001345struct queue_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1346 bool isReady(SUnit* SU, unsigned CurCycle) const { return true; }
1347};
1348
1349/// bu_ls_rr_sort - Priority function for bottom up register pressure
1350// reduction scheduler.
1351struct bu_ls_rr_sort : public queue_sort {
1352 enum {
1353 IsBottomUp = true,
1354 HasReadyFilter = false
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001355 };
1356
Andrew Trick9ccce772011-01-14 21:11:41 +00001357 RegReductionPQBase *SPQ;
1358 bu_ls_rr_sort(RegReductionPQBase *spq) : SPQ(spq) {}
1359 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001360
Andrew Trick9ccce772011-01-14 21:11:41 +00001361 bool operator()(SUnit* left, SUnit* right) const;
1362};
Andrew Trick2085a962010-12-21 22:25:04 +00001363
Andrew Trick9ccce772011-01-14 21:11:41 +00001364// td_ls_rr_sort - Priority function for top down register pressure reduction
1365// scheduler.
1366struct td_ls_rr_sort : public queue_sort {
1367 enum {
1368 IsBottomUp = false,
1369 HasReadyFilter = false
Evan Chengd38c22b2006-05-11 23:55:42 +00001370 };
1371
Andrew Trick9ccce772011-01-14 21:11:41 +00001372 RegReductionPQBase *SPQ;
1373 td_ls_rr_sort(RegReductionPQBase *spq) : SPQ(spq) {}
1374 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001375
Andrew Trick9ccce772011-01-14 21:11:41 +00001376 bool operator()(const SUnit* left, const SUnit* right) const;
1377};
Andrew Trick2085a962010-12-21 22:25:04 +00001378
Andrew Trick9ccce772011-01-14 21:11:41 +00001379// src_ls_rr_sort - Priority function for source order scheduler.
1380struct src_ls_rr_sort : public queue_sort {
1381 enum {
1382 IsBottomUp = true,
1383 HasReadyFilter = false
Evan Chengd38c22b2006-05-11 23:55:42 +00001384 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001385
Andrew Trick9ccce772011-01-14 21:11:41 +00001386 RegReductionPQBase *SPQ;
1387 src_ls_rr_sort(RegReductionPQBase *spq)
1388 : SPQ(spq) {}
1389 src_ls_rr_sort(const src_ls_rr_sort &RHS)
1390 : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001391
Andrew Trick9ccce772011-01-14 21:11:41 +00001392 bool operator()(SUnit* left, SUnit* right) const;
1393};
Andrew Trick2085a962010-12-21 22:25:04 +00001394
Andrew Trick9ccce772011-01-14 21:11:41 +00001395// hybrid_ls_rr_sort - Priority function for hybrid scheduler.
1396struct hybrid_ls_rr_sort : public queue_sort {
1397 enum {
1398 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001399 HasReadyFilter = false
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001400 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001401
Andrew Trick9ccce772011-01-14 21:11:41 +00001402 RegReductionPQBase *SPQ;
1403 hybrid_ls_rr_sort(RegReductionPQBase *spq)
1404 : SPQ(spq) {}
1405 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1406 : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001407
Andrew Trick9ccce772011-01-14 21:11:41 +00001408 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Chenga77f3d32010-07-21 06:09:07 +00001409
Andrew Trick9ccce772011-01-14 21:11:41 +00001410 bool operator()(SUnit* left, SUnit* right) const;
1411};
1412
1413// ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1414// scheduler.
1415struct ilp_ls_rr_sort : public queue_sort {
1416 enum {
1417 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001418 HasReadyFilter = false
Evan Chengbdd062d2010-05-20 06:13:19 +00001419 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001420
Andrew Trick9ccce772011-01-14 21:11:41 +00001421 RegReductionPQBase *SPQ;
1422 ilp_ls_rr_sort(RegReductionPQBase *spq)
1423 : SPQ(spq) {}
1424 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1425 : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001426
Andrew Trick9ccce772011-01-14 21:11:41 +00001427 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Cheng37b740c2010-07-24 00:39:05 +00001428
Andrew Trick9ccce772011-01-14 21:11:41 +00001429 bool operator()(SUnit* left, SUnit* right) const;
1430};
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001431
Andrew Trick9ccce772011-01-14 21:11:41 +00001432class RegReductionPQBase : public SchedulingPriorityQueue {
1433protected:
1434 std::vector<SUnit*> Queue;
1435 unsigned CurQueueId;
1436 bool TracksRegPressure;
1437
1438 // SUnits - The SUnits for the current graph.
1439 std::vector<SUnit> *SUnits;
1440
1441 MachineFunction &MF;
1442 const TargetInstrInfo *TII;
1443 const TargetRegisterInfo *TRI;
1444 const TargetLowering *TLI;
1445 ScheduleDAGRRList *scheduleDAG;
1446
1447 // SethiUllmanNumbers - The SethiUllman number for each node.
1448 std::vector<unsigned> SethiUllmanNumbers;
1449
1450 /// RegPressure - Tracking current reg pressure per register class.
1451 ///
1452 std::vector<unsigned> RegPressure;
1453
1454 /// RegLimit - Tracking the number of allocatable registers per register
1455 /// class.
1456 std::vector<unsigned> RegLimit;
1457
1458public:
1459 RegReductionPQBase(MachineFunction &mf,
1460 bool hasReadyFilter,
1461 bool tracksrp,
1462 const TargetInstrInfo *tii,
1463 const TargetRegisterInfo *tri,
1464 const TargetLowering *tli)
1465 : SchedulingPriorityQueue(hasReadyFilter),
1466 CurQueueId(0), TracksRegPressure(tracksrp),
1467 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
1468 if (TracksRegPressure) {
1469 unsigned NumRC = TRI->getNumRegClasses();
1470 RegLimit.resize(NumRC);
1471 RegPressure.resize(NumRC);
1472 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1473 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1474 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1475 E = TRI->regclass_end(); I != E; ++I)
Cameron Zwarichdf616942011-03-07 21:56:36 +00001476 RegLimit[(*I)->getID()] = tri->getRegPressureLimit(*I, MF);
Andrew Trick9ccce772011-01-14 21:11:41 +00001477 }
1478 }
1479
1480 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1481 scheduleDAG = scheduleDag;
1482 }
1483
1484 ScheduleHazardRecognizer* getHazardRec() {
1485 return scheduleDAG->getHazardRec();
1486 }
1487
1488 void initNodes(std::vector<SUnit> &sunits);
1489
1490 void addNode(const SUnit *SU);
1491
1492 void updateNode(const SUnit *SU);
1493
1494 void releaseState() {
1495 SUnits = 0;
1496 SethiUllmanNumbers.clear();
1497 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1498 }
1499
1500 unsigned getNodePriority(const SUnit *SU) const;
1501
1502 unsigned getNodeOrdering(const SUnit *SU) const {
Andrew Trick3bd8b7a2011-03-25 06:40:55 +00001503 if (!SU->getNode()) return 0;
1504
Andrew Trick9ccce772011-01-14 21:11:41 +00001505 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1506 }
1507
1508 bool empty() const { return Queue.empty(); }
1509
1510 void push(SUnit *U) {
1511 assert(!U->NodeQueueId && "Node in the queue already");
1512 U->NodeQueueId = ++CurQueueId;
1513 Queue.push_back(U);
1514 }
1515
1516 void remove(SUnit *SU) {
1517 assert(!Queue.empty() && "Queue is empty!");
1518 assert(SU->NodeQueueId != 0 && "Not in queue!");
1519 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1520 SU);
1521 if (I != prior(Queue.end()))
1522 std::swap(*I, Queue.back());
1523 Queue.pop_back();
1524 SU->NodeQueueId = 0;
1525 }
1526
Andrew Trickd0548ae2011-02-04 03:18:17 +00001527 bool tracksRegPressure() const { return TracksRegPressure; }
1528
Andrew Trick9ccce772011-01-14 21:11:41 +00001529 void dumpRegPressure() const;
1530
1531 bool HighRegPressure(const SUnit *SU) const;
1532
Andrew Trick641e2d42011-03-05 08:00:22 +00001533 bool MayReduceRegPressure(SUnit *SU) const;
1534
1535 int RegPressureDiff(SUnit *SU, unsigned &LiveUses) const;
Andrew Trick9ccce772011-01-14 21:11:41 +00001536
1537 void ScheduledNode(SUnit *SU);
1538
1539 void UnscheduledNode(SUnit *SU);
1540
1541protected:
1542 bool canClobber(const SUnit *SU, const SUnit *Op);
1543 void AddPseudoTwoAddrDeps();
1544 void PrescheduleNodesWithMultipleUses();
1545 void CalculateSethiUllmanNumbers();
1546};
1547
1548template<class SF>
1549class RegReductionPriorityQueue : public RegReductionPQBase {
1550 static SUnit *popFromQueue(std::vector<SUnit*> &Q, SF &Picker) {
1551 std::vector<SUnit *>::iterator Best = Q.begin();
1552 for (std::vector<SUnit *>::iterator I = llvm::next(Q.begin()),
1553 E = Q.end(); I != E; ++I)
1554 if (Picker(*Best, *I))
1555 Best = I;
1556 SUnit *V = *Best;
1557 if (Best != prior(Q.end()))
1558 std::swap(*Best, Q.back());
1559 Q.pop_back();
1560 return V;
1561 }
1562
1563 SF Picker;
1564
1565public:
1566 RegReductionPriorityQueue(MachineFunction &mf,
1567 bool tracksrp,
1568 const TargetInstrInfo *tii,
1569 const TargetRegisterInfo *tri,
1570 const TargetLowering *tli)
1571 : RegReductionPQBase(mf, SF::HasReadyFilter, tracksrp, tii, tri, tli),
1572 Picker(this) {}
1573
1574 bool isBottomUp() const { return SF::IsBottomUp; }
1575
1576 bool isReady(SUnit *U) const {
1577 return Picker.HasReadyFilter && Picker.isReady(U, getCurCycle());
1578 }
1579
1580 SUnit *pop() {
1581 if (Queue.empty()) return NULL;
1582
1583 SUnit *V = popFromQueue(Queue, Picker);
1584 V->NodeQueueId = 0;
1585 return V;
1586 }
1587
1588 void dump(ScheduleDAG *DAG) const {
1589 // Emulate pop() without clobbering NodeQueueIds.
1590 std::vector<SUnit*> DumpQueue = Queue;
1591 SF DumpPicker = Picker;
1592 while (!DumpQueue.empty()) {
1593 SUnit *SU = popFromQueue(DumpQueue, DumpPicker);
1594 if (isBottomUp())
1595 dbgs() << "Height " << SU->getHeight() << ": ";
1596 else
1597 dbgs() << "Depth " << SU->getDepth() << ": ";
1598 SU->dump(DAG);
1599 }
1600 }
1601};
1602
1603typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1604BURegReductionPriorityQueue;
1605
1606typedef RegReductionPriorityQueue<td_ls_rr_sort>
1607TDRegReductionPriorityQueue;
1608
1609typedef RegReductionPriorityQueue<src_ls_rr_sort>
1610SrcRegReductionPriorityQueue;
1611
1612typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1613HybridBURRPriorityQueue;
1614
1615typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1616ILPBURRPriorityQueue;
1617} // end anonymous namespace
1618
1619//===----------------------------------------------------------------------===//
1620// Static Node Priority for Register Pressure Reduction
1621//===----------------------------------------------------------------------===//
Evan Chengd38c22b2006-05-11 23:55:42 +00001622
Dan Gohman186f65d2008-11-20 03:30:37 +00001623/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1624/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001625static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001626CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001627 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1628 if (SethiUllmanNumber != 0)
1629 return SethiUllmanNumber;
1630
1631 unsigned Extra = 0;
1632 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1633 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001634 if (I->isCtrl()) continue; // ignore chain preds
1635 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001636 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001637 if (PredSethiUllman > SethiUllmanNumber) {
1638 SethiUllmanNumber = PredSethiUllman;
1639 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001640 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001641 ++Extra;
1642 }
1643
1644 SethiUllmanNumber += Extra;
1645
1646 if (SethiUllmanNumber == 0)
1647 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001648
Evan Cheng7e4abde2008-07-02 09:23:51 +00001649 return SethiUllmanNumber;
1650}
1651
Andrew Trick9ccce772011-01-14 21:11:41 +00001652/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1653/// scheduling units.
1654void RegReductionPQBase::CalculateSethiUllmanNumbers() {
1655 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001656
Andrew Trick9ccce772011-01-14 21:11:41 +00001657 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1658 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001659}
1660
Andrew Trick9ccce772011-01-14 21:11:41 +00001661void RegReductionPQBase::initNodes(std::vector<SUnit> &sunits) {
1662 SUnits = &sunits;
1663 // Add pseudo dependency edges for two-address nodes.
1664 AddPseudoTwoAddrDeps();
1665 // Reroute edges to nodes with multiple uses.
Andrew Trickd0548ae2011-02-04 03:18:17 +00001666 if (!TracksRegPressure)
1667 PrescheduleNodesWithMultipleUses();
Andrew Trick9ccce772011-01-14 21:11:41 +00001668 // Calculate node priorities.
1669 CalculateSethiUllmanNumbers();
1670}
1671
1672void RegReductionPQBase::addNode(const SUnit *SU) {
1673 unsigned SUSize = SethiUllmanNumbers.size();
1674 if (SUnits->size() > SUSize)
1675 SethiUllmanNumbers.resize(SUSize*2, 0);
1676 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1677}
1678
1679void RegReductionPQBase::updateNode(const SUnit *SU) {
1680 SethiUllmanNumbers[SU->NodeNum] = 0;
1681 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1682}
1683
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001684// Lower priority means schedule further down. For bottom-up scheduling, lower
1685// priority SUs are scheduled before higher priority SUs.
Andrew Trick9ccce772011-01-14 21:11:41 +00001686unsigned RegReductionPQBase::getNodePriority(const SUnit *SU) const {
1687 assert(SU->NodeNum < SethiUllmanNumbers.size());
1688 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1689 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1690 // CopyToReg should be close to its uses to facilitate coalescing and
1691 // avoid spilling.
1692 return 0;
1693 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1694 Opc == TargetOpcode::SUBREG_TO_REG ||
1695 Opc == TargetOpcode::INSERT_SUBREG)
1696 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1697 // close to their uses to facilitate coalescing.
1698 return 0;
1699 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1700 // If SU does not have a register use, i.e. it doesn't produce a value
1701 // that would be consumed (e.g. store), then it terminates a chain of
1702 // computation. Give it a large SethiUllman number so it will be
1703 // scheduled right before its predecessors that it doesn't lengthen
1704 // their live ranges.
1705 return 0xffff;
1706 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1707 // If SU does not have a register def, schedule it close to its uses
1708 // because it does not lengthen any live ranges.
1709 return 0;
1710 return SethiUllmanNumbers[SU->NodeNum];
1711}
1712
1713//===----------------------------------------------------------------------===//
1714// Register Pressure Tracking
1715//===----------------------------------------------------------------------===//
1716
1717void RegReductionPQBase::dumpRegPressure() const {
1718 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1719 E = TRI->regclass_end(); I != E; ++I) {
1720 const TargetRegisterClass *RC = *I;
1721 unsigned Id = RC->getID();
1722 unsigned RP = RegPressure[Id];
1723 if (!RP) continue;
1724 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1725 << '\n');
1726 }
1727}
1728
1729bool RegReductionPQBase::HighRegPressure(const SUnit *SU) const {
1730 if (!TLI)
1731 return false;
1732
1733 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1734 I != E; ++I) {
1735 if (I->isCtrl())
1736 continue;
1737 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00001738 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1739 // to cover the number of registers defined (they are all live).
1740 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001741 continue;
1742 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001743 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1744 RegDefPos.IsValid(); RegDefPos.Advance()) {
1745 EVT VT = RegDefPos.GetValue();
Andrew Trick9ccce772011-01-14 21:11:41 +00001746 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1747 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Andrew Trick9ccce772011-01-14 21:11:41 +00001748 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1749 return true;
1750 }
1751 }
Andrew Trick9ccce772011-01-14 21:11:41 +00001752 return false;
1753}
1754
Andrew Trick641e2d42011-03-05 08:00:22 +00001755bool RegReductionPQBase::MayReduceRegPressure(SUnit *SU) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00001756 const SDNode *N = SU->getNode();
1757
1758 if (!N->isMachineOpcode() || !SU->NumSuccs)
1759 return false;
1760
1761 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1762 for (unsigned i = 0; i != NumDefs; ++i) {
1763 EVT VT = N->getValueType(i);
1764 if (!N->hasAnyUseOfValue(i))
1765 continue;
1766 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1767 if (RegPressure[RCId] >= RegLimit[RCId])
1768 return true;
1769 }
1770 return false;
1771}
1772
Andrew Trick641e2d42011-03-05 08:00:22 +00001773// Compute the register pressure contribution by this instruction by count up
1774// for uses that are not live and down for defs. Only count register classes
1775// that are already under high pressure. As a side effect, compute the number of
1776// uses of registers that are already live.
1777//
1778// FIXME: This encompasses the logic in HighRegPressure and MayReduceRegPressure
1779// so could probably be factored.
1780int RegReductionPQBase::RegPressureDiff(SUnit *SU, unsigned &LiveUses) const {
1781 LiveUses = 0;
1782 int PDiff = 0;
1783 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1784 I != E; ++I) {
1785 if (I->isCtrl())
1786 continue;
1787 SUnit *PredSU = I->getSUnit();
1788 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1789 // to cover the number of registers defined (they are all live).
1790 if (PredSU->NumRegDefsLeft == 0) {
1791 if (PredSU->getNode()->isMachineOpcode())
1792 ++LiveUses;
1793 continue;
1794 }
1795 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1796 RegDefPos.IsValid(); RegDefPos.Advance()) {
1797 EVT VT = RegDefPos.GetValue();
1798 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1799 if (RegPressure[RCId] >= RegLimit[RCId])
1800 ++PDiff;
1801 }
1802 }
1803 const SDNode *N = SU->getNode();
1804
Eric Christopher7238cba2011-03-08 19:35:47 +00001805 if (!N || !N->isMachineOpcode() || !SU->NumSuccs)
Andrew Trick641e2d42011-03-05 08:00:22 +00001806 return PDiff;
1807
1808 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1809 for (unsigned i = 0; i != NumDefs; ++i) {
1810 EVT VT = N->getValueType(i);
1811 if (!N->hasAnyUseOfValue(i))
1812 continue;
1813 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1814 if (RegPressure[RCId] >= RegLimit[RCId])
1815 --PDiff;
1816 }
1817 return PDiff;
1818}
1819
Andrew Trick9ccce772011-01-14 21:11:41 +00001820void RegReductionPQBase::ScheduledNode(SUnit *SU) {
1821 if (!TracksRegPressure)
1822 return;
1823
Eric Christopher7238cba2011-03-08 19:35:47 +00001824 if (!SU->getNode())
1825 return;
Andrew Tricka8846e02011-03-23 20:40:18 +00001826
Andrew Trick9ccce772011-01-14 21:11:41 +00001827 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1828 I != E; ++I) {
1829 if (I->isCtrl())
1830 continue;
1831 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00001832 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1833 // to cover the number of registers defined (they are all live).
1834 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick9ccce772011-01-14 21:11:41 +00001835 continue;
1836 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001837 // FIXME: The ScheduleDAG currently loses information about which of a
1838 // node's values is consumed by each dependence. Consequently, if the node
1839 // defines multiple register classes, we don't know which to pressurize
1840 // here. Instead the following loop consumes the register defs in an
1841 // arbitrary order. At least it handles the common case of clustered loads
1842 // to the same class. For precise liveness, each SDep needs to indicate the
1843 // result number. But that tightly couples the ScheduleDAG with the
1844 // SelectionDAG making updates tricky. A simpler hack would be to attach a
1845 // value type or register class to SDep.
1846 //
1847 // The most important aspect of register tracking is balancing the increase
1848 // here with the reduction further below. Note that this SU may use multiple
1849 // defs in PredSU. The can't be determined here, but we've already
1850 // compensated by reducing NumRegDefsLeft in PredSU during
1851 // ScheduleDAGSDNodes::AddSchedEdges.
1852 --PredSU->NumRegDefsLeft;
1853 unsigned SkipRegDefs = PredSU->NumRegDefsLeft;
1854 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1855 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
1856 if (SkipRegDefs)
Andrew Trick9ccce772011-01-14 21:11:41 +00001857 continue;
Andrew Trickd0548ae2011-02-04 03:18:17 +00001858 EVT VT = RegDefPos.GetValue();
Andrew Trick9ccce772011-01-14 21:11:41 +00001859 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1860 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trickd0548ae2011-02-04 03:18:17 +00001861 break;
Andrew Trick9ccce772011-01-14 21:11:41 +00001862 }
1863 }
1864
Andrew Trickd0548ae2011-02-04 03:18:17 +00001865 // We should have this assert, but there may be dead SDNodes that never
1866 // materialize as SUnits, so they don't appear to generate liveness.
1867 //assert(SU->NumRegDefsLeft == 0 && "not all regdefs have scheduled uses");
1868 int SkipRegDefs = (int)SU->NumRegDefsLeft;
1869 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(SU, scheduleDAG);
1870 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
1871 if (SkipRegDefs > 0)
1872 continue;
1873 EVT VT = RegDefPos.GetValue();
1874 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1875 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT)) {
1876 // Register pressure tracking is imprecise. This can happen. But we try
1877 // hard not to let it happen because it likely results in poor scheduling.
1878 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") has too many regdefs\n");
1879 RegPressure[RCId] = 0;
1880 }
1881 else {
1882 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Andrew Trick9ccce772011-01-14 21:11:41 +00001883 }
1884 }
Andrew Trick9ccce772011-01-14 21:11:41 +00001885 dumpRegPressure();
1886}
1887
1888void RegReductionPQBase::UnscheduledNode(SUnit *SU) {
1889 if (!TracksRegPressure)
1890 return;
1891
1892 const SDNode *N = SU->getNode();
Eric Christopher7238cba2011-03-08 19:35:47 +00001893 if (!N) return;
Andrew Tricka8846e02011-03-23 20:40:18 +00001894
Andrew Trick9ccce772011-01-14 21:11:41 +00001895 if (!N->isMachineOpcode()) {
1896 if (N->getOpcode() != ISD::CopyToReg)
1897 return;
1898 } else {
1899 unsigned Opc = N->getMachineOpcode();
1900 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1901 Opc == TargetOpcode::INSERT_SUBREG ||
1902 Opc == TargetOpcode::SUBREG_TO_REG ||
1903 Opc == TargetOpcode::REG_SEQUENCE ||
1904 Opc == TargetOpcode::IMPLICIT_DEF)
1905 return;
1906 }
1907
1908 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1909 I != E; ++I) {
1910 if (I->isCtrl())
1911 continue;
1912 SUnit *PredSU = I->getSUnit();
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001913 // NumSuccsLeft counts all deps. Don't compare it with NumSuccs which only
1914 // counts data deps.
1915 if (PredSU->NumSuccsLeft != PredSU->Succs.size())
Andrew Trick9ccce772011-01-14 21:11:41 +00001916 continue;
1917 const SDNode *PN = PredSU->getNode();
1918 if (!PN->isMachineOpcode()) {
1919 if (PN->getOpcode() == ISD::CopyFromReg) {
1920 EVT VT = PN->getValueType(0);
1921 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1922 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1923 }
1924 continue;
1925 }
1926 unsigned POpc = PN->getMachineOpcode();
1927 if (POpc == TargetOpcode::IMPLICIT_DEF)
1928 continue;
1929 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1930 EVT VT = PN->getOperand(0).getValueType();
1931 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1932 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1933 continue;
1934 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1935 POpc == TargetOpcode::SUBREG_TO_REG) {
1936 EVT VT = PN->getValueType(0);
1937 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1938 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1939 continue;
1940 }
1941 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1942 for (unsigned i = 0; i != NumDefs; ++i) {
1943 EVT VT = PN->getValueType(i);
1944 if (!PN->hasAnyUseOfValue(i))
1945 continue;
1946 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1947 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1948 // Register pressure tracking is imprecise. This can happen.
1949 RegPressure[RCId] = 0;
1950 else
1951 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1952 }
1953 }
1954
1955 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1956 // may transfer data dependencies to CopyToReg.
1957 if (SU->NumSuccs && N->isMachineOpcode()) {
1958 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1959 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1960 EVT VT = N->getValueType(i);
1961 if (VT == MVT::Glue || VT == MVT::Other)
1962 continue;
1963 if (!N->hasAnyUseOfValue(i))
1964 continue;
1965 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1966 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1967 }
1968 }
1969
1970 dumpRegPressure();
1971}
1972
1973//===----------------------------------------------------------------------===//
1974// Dynamic Node Priority for Register Pressure Reduction
1975//===----------------------------------------------------------------------===//
1976
Evan Chengb9e3db62007-03-14 22:43:40 +00001977/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001978/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001979static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001980 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001981 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001982 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001983 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001984 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001985 // If there are bunch of CopyToRegs stacked up, they should be considered
1986 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001987 if (I->getSUnit()->getNode() &&
1988 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001989 Height = closestSucc(I->getSUnit())+1;
1990 if (Height > MaxHeight)
1991 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001992 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001993 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001994}
1995
Evan Cheng61bc51e2007-12-20 02:22:36 +00001996/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001997/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001998static unsigned calcMaxScratches(const SUnit *SU) {
1999 unsigned Scratches = 0;
2000 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00002001 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002002 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00002003 Scratches++;
2004 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00002005 return Scratches;
2006}
2007
Evan Cheng6c1414f2010-10-29 18:09:28 +00002008/// hasOnlyLiveOutUse - Return true if SU has a single value successor that is a
2009/// CopyToReg to a virtual register. This SU def is probably a liveout and
2010/// it has no other use. It should be scheduled closer to the terminator.
2011static bool hasOnlyLiveOutUses(const SUnit *SU) {
2012 bool RetVal = false;
2013 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
2014 I != E; ++I) {
2015 if (I->isCtrl()) continue;
2016 const SUnit *SuccSU = I->getSUnit();
2017 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
2018 unsigned Reg =
2019 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
2020 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2021 RetVal = true;
2022 continue;
2023 }
2024 }
2025 return false;
2026 }
2027 return RetVal;
2028}
2029
2030/// UnitsSharePred - Return true if the two scheduling units share a common
2031/// data predecessor.
2032static bool UnitsSharePred(const SUnit *left, const SUnit *right) {
2033 SmallSet<const SUnit*, 4> Preds;
2034 for (SUnit::const_pred_iterator I = left->Preds.begin(),E = left->Preds.end();
2035 I != E; ++I) {
2036 if (I->isCtrl()) continue; // ignore chain preds
2037 Preds.insert(I->getSUnit());
2038 }
2039 for (SUnit::const_pred_iterator I = right->Preds.begin(),E = right->Preds.end();
2040 I != E; ++I) {
2041 if (I->isCtrl()) continue; // ignore chain preds
2042 if (Preds.count(I->getSUnit()))
2043 return true;
2044 }
2045 return false;
2046}
2047
Andrew Trick9ccce772011-01-14 21:11:41 +00002048// Check for either a dependence (latency) or resource (hazard) stall.
2049//
2050// Note: The ScheduleHazardRecognizer interface requires a non-const SU.
2051static bool BUHasStall(SUnit *SU, int Height, RegReductionPQBase *SPQ) {
2052 if ((int)SPQ->getCurCycle() < Height) return true;
2053 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2054 != ScheduleHazardRecognizer::NoHazard)
2055 return true;
2056 return false;
2057}
2058
2059// Return -1 if left has higher priority, 1 if right has higher priority.
2060// Return 0 if latency-based priority is equivalent.
2061static int BUCompareLatency(SUnit *left, SUnit *right, bool checkPref,
2062 RegReductionPQBase *SPQ) {
2063 // If the two nodes share an operand and one of them has a single
2064 // use that is a live out copy, favor the one that is live out. Otherwise
2065 // it will be difficult to eliminate the copy if the instruction is a
2066 // loop induction variable update. e.g.
2067 // BB:
2068 // sub r1, r3, #1
2069 // str r0, [r2, r3]
2070 // mov r3, r1
2071 // cmp
2072 // bne BB
2073 bool SharePred = UnitsSharePred(left, right);
2074 // FIXME: Only adjust if BB is a loop back edge.
2075 // FIXME: What's the cost of a copy?
2076 int LBonus = (SharePred && hasOnlyLiveOutUses(left)) ? 1 : 0;
2077 int RBonus = (SharePred && hasOnlyLiveOutUses(right)) ? 1 : 0;
2078 int LHeight = (int)left->getHeight() - LBonus;
2079 int RHeight = (int)right->getHeight() - RBonus;
2080
2081 bool LStall = (!checkPref || left->SchedulingPref == Sched::Latency) &&
2082 BUHasStall(left, LHeight, SPQ);
2083 bool RStall = (!checkPref || right->SchedulingPref == Sched::Latency) &&
2084 BUHasStall(right, RHeight, SPQ);
2085
2086 // If scheduling one of the node will cause a pipeline stall, delay it.
2087 // If scheduling either one of the node will cause a pipeline stall, sort
2088 // them according to their height.
2089 if (LStall) {
2090 if (!RStall)
2091 return 1;
2092 if (LHeight != RHeight)
2093 return LHeight > RHeight ? 1 : -1;
2094 } else if (RStall)
2095 return -1;
2096
Andrew Trick47ff14b2011-01-21 05:51:33 +00002097 // If either node is scheduling for latency, sort them by height/depth
Andrew Trick9ccce772011-01-14 21:11:41 +00002098 // and latency.
2099 if (!checkPref || (left->SchedulingPref == Sched::Latency ||
2100 right->SchedulingPref == Sched::Latency)) {
Andrew Trick47ff14b2011-01-21 05:51:33 +00002101 if (DisableSchedCycles) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002102 if (LHeight != RHeight)
2103 return LHeight > RHeight ? 1 : -1;
2104 }
Andrew Trick47ff14b2011-01-21 05:51:33 +00002105 else {
2106 // If neither instruction stalls (!LStall && !RStall) then
Eric Christopher9cb33de2011-03-06 21:13:45 +00002107 // its height is already covered so only its depth matters. We also reach
Andrew Trick47ff14b2011-01-21 05:51:33 +00002108 // this if both stall but have the same height.
2109 unsigned LDepth = left->getDepth();
2110 unsigned RDepth = right->getDepth();
2111 if (LDepth != RDepth) {
2112 DEBUG(dbgs() << " Comparing latency of SU (" << left->NodeNum
2113 << ") depth " << LDepth << " vs SU (" << right->NodeNum
2114 << ") depth " << RDepth << "\n");
2115 return LDepth < RDepth ? 1 : -1;
2116 }
2117 }
Andrew Trick9ccce772011-01-14 21:11:41 +00002118 if (left->Latency != right->Latency)
2119 return left->Latency > right->Latency ? 1 : -1;
2120 }
2121 return 0;
2122}
2123
2124static bool BURRSort(SUnit *left, SUnit *right, RegReductionPQBase *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00002125 unsigned LPriority = SPQ->getNodePriority(left);
2126 unsigned RPriority = SPQ->getNodePriority(right);
Andrew Trick641e2d42011-03-05 08:00:22 +00002127 if (LPriority != RPriority) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002128 DEBUG(++FactorCount[FactStatic]);
Evan Cheng73bdf042008-03-01 00:39:47 +00002129 return LPriority > RPriority;
Andrew Trick641e2d42011-03-05 08:00:22 +00002130 }
Andrew Trick52b3e382011-03-08 01:51:56 +00002131 DEBUG(++FactorCount[FactOther]);
2132
Evan Cheng73bdf042008-03-01 00:39:47 +00002133 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
2134 // e.g.
2135 // t1 = op t2, c1
2136 // t3 = op t4, c2
2137 //
2138 // and the following instructions are both ready.
2139 // t2 = op c3
2140 // t4 = op c4
2141 //
2142 // Then schedule t2 = op first.
2143 // i.e.
2144 // t4 = op c4
2145 // t2 = op c3
2146 // t1 = op t2, c1
2147 // t3 = op t4, c2
2148 //
2149 // This creates more short live intervals.
2150 unsigned LDist = closestSucc(left);
2151 unsigned RDist = closestSucc(right);
2152 if (LDist != RDist)
2153 return LDist < RDist;
2154
Evan Cheng3a14efa2009-02-12 08:59:45 +00002155 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00002156 unsigned LScratch = calcMaxScratches(left);
2157 unsigned RScratch = calcMaxScratches(right);
2158 if (LScratch != RScratch)
2159 return LScratch > RScratch;
2160
Andrew Trick47ff14b2011-01-21 05:51:33 +00002161 if (!DisableSchedCycles) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002162 int result = BUCompareLatency(left, right, false /*checkPref*/, SPQ);
2163 if (result != 0)
2164 return result > 0;
2165 }
2166 else {
2167 if (left->getHeight() != right->getHeight())
2168 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00002169
Andrew Trick9ccce772011-01-14 21:11:41 +00002170 if (left->getDepth() != right->getDepth())
2171 return left->getDepth() < right->getDepth();
2172 }
Evan Cheng73bdf042008-03-01 00:39:47 +00002173
Andrew Trick2085a962010-12-21 22:25:04 +00002174 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002175 "NodeQueueId cannot be zero");
2176 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002177}
2178
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002179// Bottom up
Andrew Trick9ccce772011-01-14 21:11:41 +00002180bool bu_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002181 return BURRSort(left, right, SPQ);
2182}
2183
2184// Source order, otherwise bottom up.
Andrew Trick9ccce772011-01-14 21:11:41 +00002185bool src_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002186 unsigned LOrder = SPQ->getNodeOrdering(left);
2187 unsigned ROrder = SPQ->getNodeOrdering(right);
2188
2189 // Prefer an ordering where the lower the non-zero order number, the higher
2190 // the preference.
2191 if ((LOrder || ROrder) && LOrder != ROrder)
2192 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2193
2194 return BURRSort(left, right, SPQ);
2195}
2196
Andrew Trick9ccce772011-01-14 21:11:41 +00002197// If the time between now and when the instruction will be ready can cover
2198// the spill code, then avoid adding it to the ready queue. This gives long
2199// stalls highest priority and allows hoisting across calls. It should also
2200// speed up processing the available queue.
2201bool hybrid_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
2202 static const unsigned ReadyDelay = 3;
2203
2204 if (SPQ->MayReduceRegPressure(SU)) return true;
2205
2206 if (SU->getHeight() > (CurCycle + ReadyDelay)) return false;
2207
2208 if (SPQ->getHazardRec()->getHazardType(SU, -ReadyDelay)
2209 != ScheduleHazardRecognizer::NoHazard)
2210 return false;
2211
2212 return true;
2213}
2214
2215// Return true if right should be scheduled with higher priority than left.
2216bool hybrid_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00002217 if (left->isCall || right->isCall)
2218 // No way to compute latency of calls.
2219 return BURRSort(left, right, SPQ);
2220
Evan Chenge6d6c5d2010-07-26 21:49:07 +00002221 bool LHigh = SPQ->HighRegPressure(left);
2222 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00002223 // Avoid causing spills. If register pressure is high, schedule for
2224 // register pressure reduction.
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002225 if (LHigh && !RHigh) {
2226 DEBUG(dbgs() << " pressure SU(" << left->NodeNum << ") > SU("
2227 << right->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002228 return true;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002229 }
2230 else if (!LHigh && RHigh) {
2231 DEBUG(dbgs() << " pressure SU(" << right->NodeNum << ") > SU("
2232 << left->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002233 return false;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002234 }
Evan Chenge6d6c5d2010-07-26 21:49:07 +00002235 else if (!LHigh && !RHigh) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002236 int result = BUCompareLatency(left, right, true /*checkPref*/, SPQ);
2237 if (result != 0)
2238 return result > 0;
Evan Chengcc2efe12010-05-28 23:26:21 +00002239 }
Evan Chengbdd062d2010-05-20 06:13:19 +00002240 return BURRSort(left, right, SPQ);
2241}
2242
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002243// Schedule as many instructions in each cycle as possible. So don't make an
2244// instruction available unless it is ready in the current cycle.
2245bool ilp_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00002246 if (SU->getHeight() > CurCycle) return false;
2247
2248 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2249 != ScheduleHazardRecognizer::NoHazard)
2250 return false;
2251
Andrew Trickc88b7ec2011-03-04 02:03:45 +00002252 return true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002253}
2254
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002255static bool canEnableCoalescing(SUnit *SU) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002256 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
2257 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
2258 // CopyToReg should be close to its uses to facilitate coalescing and
2259 // avoid spilling.
2260 return true;
2261
2262 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2263 Opc == TargetOpcode::SUBREG_TO_REG ||
2264 Opc == TargetOpcode::INSERT_SUBREG)
2265 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
2266 // close to their uses to facilitate coalescing.
2267 return true;
2268
2269 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
2270 // If SU does not have a register def, schedule it close to its uses
2271 // because it does not lengthen any live ranges.
2272 return true;
2273
2274 return false;
2275}
2276
Andrew Trickb8390b72011-03-05 08:04:11 +00002277// list-ilp is currently an experimental scheduler that allows various
2278// heuristics to be enabled prior to the normal register reduction logic.
Andrew Trick9ccce772011-01-14 21:11:41 +00002279bool ilp_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00002280 if (left->isCall || right->isCall)
2281 // No way to compute latency of calls.
2282 return BURRSort(left, right, SPQ);
2283
Andrew Trick52b3e382011-03-08 01:51:56 +00002284 unsigned LLiveUses = 0, RLiveUses = 0;
2285 int LPDiff = 0, RPDiff = 0;
2286 if (!DisableSchedRegPressure || !DisableSchedLiveUses) {
2287 LPDiff = SPQ->RegPressureDiff(left, LLiveUses);
2288 RPDiff = SPQ->RegPressureDiff(right, RLiveUses);
2289 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002290 if (!DisableSchedRegPressure && LPDiff != RPDiff) {
2291 DEBUG(++FactorCount[FactPressureDiff]);
Andrew Trick52b3e382011-03-08 01:51:56 +00002292 DEBUG(dbgs() << "RegPressureDiff SU(" << left->NodeNum << "): " << LPDiff
2293 << " != SU(" << right->NodeNum << "): " << RPDiff << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002294 return LPDiff > RPDiff;
2295 }
2296
Andrew Trick52b3e382011-03-08 01:51:56 +00002297 if (!DisableSchedRegPressure && (LPDiff > 0 || RPDiff > 0)) {
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002298 bool LReduce = canEnableCoalescing(left);
2299 bool RReduce = canEnableCoalescing(right);
Andrew Trick52b3e382011-03-08 01:51:56 +00002300 DEBUG(if (LReduce != RReduce) ++FactorCount[FactPressureDiff]);
2301 if (LReduce && !RReduce) return false;
2302 if (RReduce && !LReduce) return true;
2303 }
2304
2305 if (!DisableSchedLiveUses && (LLiveUses != RLiveUses)) {
2306 DEBUG(dbgs() << "Live uses SU(" << left->NodeNum << "): " << LLiveUses
2307 << " != SU(" << right->NodeNum << "): " << RLiveUses << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002308 DEBUG(++FactorCount[FactRegUses]);
2309 return LLiveUses < RLiveUses;
2310 }
2311
Andrew Trick52b3e382011-03-08 01:51:56 +00002312 if (!DisableSchedStalls) {
2313 bool LStall = BUHasStall(left, left->getHeight(), SPQ);
2314 bool RStall = BUHasStall(right, right->getHeight(), SPQ);
2315 if (LStall != RStall) {
2316 DEBUG(++FactorCount[FactHeight]);
2317 return left->getHeight() > right->getHeight();
2318 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002319 }
2320
Andrew Trick25cedf32011-03-05 10:29:25 +00002321 if (!DisableSchedCriticalPath) {
2322 int spread = (int)left->getDepth() - (int)right->getDepth();
2323 if (std::abs(spread) > MaxReorderWindow) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002324 DEBUG(dbgs() << "Depth of SU(" << left->NodeNum << "): "
2325 << left->getDepth() << " != SU(" << right->NodeNum << "): "
2326 << right->getDepth() << "\n");
Andrew Trick25cedf32011-03-05 10:29:25 +00002327 DEBUG(++FactorCount[FactDepth]);
2328 return left->getDepth() < right->getDepth();
2329 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002330 }
2331
2332 if (!DisableSchedHeight && left->getHeight() != right->getHeight()) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002333 int spread = (int)left->getHeight() - (int)right->getHeight();
2334 if (std::abs(spread) > MaxReorderWindow) {
2335 DEBUG(++FactorCount[FactHeight]);
2336 return left->getHeight() > right->getHeight();
2337 }
Evan Cheng37b740c2010-07-24 00:39:05 +00002338 }
2339
2340 return BURRSort(left, right, SPQ);
2341}
2342
Andrew Trick9ccce772011-01-14 21:11:41 +00002343//===----------------------------------------------------------------------===//
2344// Preschedule for Register Pressure
2345//===----------------------------------------------------------------------===//
2346
2347bool RegReductionPQBase::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002348 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002349 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00002350 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00002351 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00002352 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002353 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00002354 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002355 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00002356 if (DU->getNodeId() != -1 &&
2357 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002358 return true;
2359 }
2360 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002361 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002362 return false;
2363}
2364
Evan Chengf9891412007-12-20 09:25:31 +00002365/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00002366/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00002367static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00002368 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002369 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002370 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00002371 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
2372 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00002373 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00002374 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +00002375 SUNode = SUNode->getGluedNode()) {
Dan Gohmana366da12009-03-23 16:23:01 +00002376 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00002377 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002378 const unsigned *SUImpDefs =
2379 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
2380 if (!SUImpDefs)
2381 return false;
2382 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002383 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002384 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00002385 continue;
2386 if (!N->hasAnyUseOfValue(i))
2387 continue;
2388 unsigned Reg = ImpDefs[i - NumDefs];
2389 for (;*SUImpDefs; ++SUImpDefs) {
2390 unsigned SUReg = *SUImpDefs;
2391 if (TRI->regsOverlap(Reg, SUReg))
2392 return true;
2393 }
Evan Chengf9891412007-12-20 09:25:31 +00002394 }
2395 }
2396 return false;
2397}
2398
Dan Gohman9a658d72009-03-24 00:49:12 +00002399/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
2400/// are not handled well by the general register pressure reduction
2401/// heuristics. When presented with code like this:
2402///
2403/// N
2404/// / |
2405/// / |
2406/// U store
2407/// |
2408/// ...
2409///
2410/// the heuristics tend to push the store up, but since the
2411/// operand of the store has another use (U), this would increase
2412/// the length of that other use (the U->N edge).
2413///
2414/// This function transforms code like the above to route U's
2415/// dependence through the store when possible, like this:
2416///
2417/// N
2418/// ||
2419/// ||
2420/// store
2421/// |
2422/// U
2423/// |
2424/// ...
2425///
2426/// This results in the store being scheduled immediately
2427/// after N, which shortens the U->N live range, reducing
2428/// register pressure.
2429///
Andrew Trick9ccce772011-01-14 21:11:41 +00002430void RegReductionPQBase::PrescheduleNodesWithMultipleUses() {
Dan Gohman9a658d72009-03-24 00:49:12 +00002431 // Visit all the nodes in topological order, working top-down.
2432 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
2433 SUnit *SU = &(*SUnits)[i];
2434 // For now, only look at nodes with no data successors, such as stores.
2435 // These are especially important, due to the heuristics in
2436 // getNodePriority for nodes with no data successors.
2437 if (SU->NumSuccs != 0)
2438 continue;
2439 // For now, only look at nodes with exactly one data predecessor.
2440 if (SU->NumPreds != 1)
2441 continue;
2442 // Avoid prescheduling copies to virtual registers, which don't behave
2443 // like other nodes from the perspective of scheduling heuristics.
2444 if (SDNode *N = SU->getNode())
2445 if (N->getOpcode() == ISD::CopyToReg &&
2446 TargetRegisterInfo::isVirtualRegister
2447 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2448 continue;
2449
2450 // Locate the single data predecessor.
2451 SUnit *PredSU = 0;
2452 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
2453 EE = SU->Preds.end(); II != EE; ++II)
2454 if (!II->isCtrl()) {
2455 PredSU = II->getSUnit();
2456 break;
2457 }
2458 assert(PredSU);
2459
2460 // Don't rewrite edges that carry physregs, because that requires additional
2461 // support infrastructure.
2462 if (PredSU->hasPhysRegDefs)
2463 continue;
2464 // Short-circuit the case where SU is PredSU's only data successor.
2465 if (PredSU->NumSuccs == 1)
2466 continue;
2467 // Avoid prescheduling to copies from virtual registers, which don't behave
Andrew Trickd0548ae2011-02-04 03:18:17 +00002468 // like other nodes from the perspective of scheduling heuristics.
Dan Gohman9a658d72009-03-24 00:49:12 +00002469 if (SDNode *N = SU->getNode())
2470 if (N->getOpcode() == ISD::CopyFromReg &&
2471 TargetRegisterInfo::isVirtualRegister
2472 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2473 continue;
2474
2475 // Perform checks on the successors of PredSU.
2476 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
2477 EE = PredSU->Succs.end(); II != EE; ++II) {
2478 SUnit *PredSuccSU = II->getSUnit();
2479 if (PredSuccSU == SU) continue;
2480 // If PredSU has another successor with no data successors, for
2481 // now don't attempt to choose either over the other.
2482 if (PredSuccSU->NumSuccs == 0)
2483 goto outer_loop_continue;
2484 // Don't break physical register dependencies.
2485 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
2486 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
2487 goto outer_loop_continue;
2488 // Don't introduce graph cycles.
2489 if (scheduleDAG->IsReachable(SU, PredSuccSU))
2490 goto outer_loop_continue;
2491 }
2492
2493 // Ok, the transformation is safe and the heuristics suggest it is
2494 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00002495 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
2496 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002497 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00002498 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
2499 SDep Edge = PredSU->Succs[i];
2500 assert(!Edge.isAssignedRegDep());
2501 SUnit *SuccSU = Edge.getSUnit();
2502 if (SuccSU != SU) {
2503 Edge.setSUnit(PredSU);
2504 scheduleDAG->RemovePred(SuccSU, Edge);
2505 scheduleDAG->AddPred(SU, Edge);
2506 Edge.setSUnit(SU);
2507 scheduleDAG->AddPred(SuccSU, Edge);
2508 --i;
2509 }
2510 }
2511 outer_loop_continue:;
2512 }
2513}
2514
Evan Chengd38c22b2006-05-11 23:55:42 +00002515/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
2516/// it as a def&use operand. Add a pseudo control edge from it to the other
2517/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00002518/// first (lower in the schedule). If both nodes are two-address, favor the
2519/// one that has a CopyToReg use (more likely to be a loop induction update).
2520/// If both are two-address, but one is commutable while the other is not
2521/// commutable, favor the one that's not commutable.
Andrew Trick9ccce772011-01-14 21:11:41 +00002522void RegReductionPQBase::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002523 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00002524 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002525 if (!SU->isTwoAddress)
2526 continue;
2527
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002528 SDNode *Node = SU->getNode();
Chris Lattner11a33812010-12-23 17:24:32 +00002529 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getGluedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002530 continue;
2531
Evan Cheng6c1414f2010-10-29 18:09:28 +00002532 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00002533 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00002534 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00002535 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00002536 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002537 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00002538 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
2539 continue;
2540 SDNode *DU = SU->getNode()->getOperand(j).getNode();
2541 if (DU->getNodeId() == -1)
2542 continue;
2543 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
2544 if (!DUSU) continue;
2545 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
2546 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002547 if (I->isCtrl()) continue;
2548 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00002549 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00002550 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002551 // Be conservative. Ignore if nodes aren't at roughly the same
2552 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002553 if (SuccSU->getHeight() < SU->getHeight() &&
2554 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00002555 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002556 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
2557 // constrains whatever is using the copy, instead of the copy
2558 // itself. In the case that the copy is coalesced, this
2559 // preserves the intent of the pseudo two-address heurietics.
2560 while (SuccSU->Succs.size() == 1 &&
2561 SuccSU->getNode()->isMachineOpcode() &&
2562 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00002563 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002564 SuccSU = SuccSU->Succs.front().getSUnit();
2565 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00002566 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
2567 continue;
2568 // Don't constrain nodes with physical register defs if the
2569 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00002570 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00002571 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00002572 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002573 }
Dan Gohman3027bb62009-04-16 20:57:10 +00002574 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
2575 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00002576 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00002577 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
2578 SuccOpc == TargetOpcode::INSERT_SUBREG ||
2579 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00002580 continue;
2581 if ((!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00002582 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00002583 (!SU->isCommutable && SuccSU->isCommutable)) &&
2584 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00002585 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002586 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00002587 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00002588 /*Reg=*/0, /*isNormalMemory=*/false,
2589 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00002590 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002591 }
2592 }
2593 }
2594 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002595}
2596
Roman Levenstein30d09512008-03-27 09:44:37 +00002597/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00002598/// predecessors of the successors of the SUnit SU. Stop when the provided
2599/// limit is exceeded.
Andrew Trick2085a962010-12-21 22:25:04 +00002600static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
Roman Levensteinbc674502008-03-27 09:14:57 +00002601 unsigned Limit) {
2602 unsigned Sum = 0;
2603 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
2604 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002605 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00002606 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
2607 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00002608 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00002609 if (!PredSU->isScheduled)
2610 if (++Sum > Limit)
2611 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00002612 }
2613 }
2614 return Sum;
2615}
2616
Evan Chengd38c22b2006-05-11 23:55:42 +00002617
2618// Top down
2619bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00002620 unsigned LPriority = SPQ->getNodePriority(left);
2621 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002622 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
2623 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00002624 bool LIsFloater = LIsTarget && left->NumPreds == 0;
2625 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00002626 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
2627 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00002628
2629 if (left->NumSuccs == 0 && right->NumSuccs != 0)
2630 return false;
2631 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
2632 return true;
2633
Evan Chengd38c22b2006-05-11 23:55:42 +00002634 if (LIsFloater)
2635 LBonus -= 2;
2636 if (RIsFloater)
2637 RBonus -= 2;
2638 if (left->NumSuccs == 1)
2639 LBonus += 2;
2640 if (right->NumSuccs == 1)
2641 RBonus += 2;
2642
Evan Cheng73bdf042008-03-01 00:39:47 +00002643 if (LPriority+LBonus != RPriority+RBonus)
2644 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00002645
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002646 if (left->getDepth() != right->getDepth())
2647 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00002648
2649 if (left->NumSuccsLeft != right->NumSuccsLeft)
2650 return left->NumSuccsLeft > right->NumSuccsLeft;
2651
Andrew Trick2085a962010-12-21 22:25:04 +00002652 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002653 "NodeQueueId cannot be zero");
2654 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002655}
2656
Evan Chengd38c22b2006-05-11 23:55:42 +00002657//===----------------------------------------------------------------------===//
2658// Public Constructor Functions
2659//===----------------------------------------------------------------------===//
2660
Dan Gohmandfaf6462009-02-11 04:27:20 +00002661llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002662llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
2663 CodeGenOpt::Level OptLevel) {
Dan Gohman619ef482009-01-15 19:20:50 +00002664 const TargetMachine &TM = IS->TM;
2665 const TargetInstrInfo *TII = TM.getInstrInfo();
2666 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002667
Evan Chenga77f3d32010-07-21 06:09:07 +00002668 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002669 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002670 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002671 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002672 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002673}
2674
Dan Gohmandfaf6462009-02-11 04:27:20 +00002675llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002676llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS,
2677 CodeGenOpt::Level OptLevel) {
Dan Gohman619ef482009-01-15 19:20:50 +00002678 const TargetMachine &TM = IS->TM;
2679 const TargetInstrInfo *TII = TM.getInstrInfo();
2680 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002681
Evan Chenga77f3d32010-07-21 06:09:07 +00002682 TDRegReductionPriorityQueue *PQ =
2683 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002684 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Dan Gohman3f656df2008-11-20 02:45:51 +00002685 PQ->setScheduleDAG(SD);
2686 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002687}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002688
2689llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002690llvm::createSourceListDAGScheduler(SelectionDAGISel *IS,
2691 CodeGenOpt::Level OptLevel) {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002692 const TargetMachine &TM = IS->TM;
2693 const TargetInstrInfo *TII = TM.getInstrInfo();
2694 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002695
Evan Chenga77f3d32010-07-21 06:09:07 +00002696 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002697 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002698 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Chengbdd062d2010-05-20 06:13:19 +00002699 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002700 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00002701}
2702
2703llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002704llvm::createHybridListDAGScheduler(SelectionDAGISel *IS,
2705 CodeGenOpt::Level OptLevel) {
Evan Chengbdd062d2010-05-20 06:13:19 +00002706 const TargetMachine &TM = IS->TM;
2707 const TargetInstrInfo *TII = TM.getInstrInfo();
2708 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002709 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002710
Evan Chenga77f3d32010-07-21 06:09:07 +00002711 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002712 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002713
2714 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002715 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002716 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002717}
Evan Cheng37b740c2010-07-24 00:39:05 +00002718
2719llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002720llvm::createILPListDAGScheduler(SelectionDAGISel *IS,
2721 CodeGenOpt::Level OptLevel) {
Evan Cheng37b740c2010-07-24 00:39:05 +00002722 const TargetMachine &TM = IS->TM;
2723 const TargetInstrInfo *TII = TM.getInstrInfo();
2724 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2725 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002726
Evan Cheng37b740c2010-07-24 00:39:05 +00002727 ILPBURRPriorityQueue *PQ =
2728 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002729 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Evan Cheng37b740c2010-07-24 00:39:05 +00002730 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002731 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00002732}