blob: b5e4ad9cdee0d788f4257c222a067a8cf78df08c [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman483377c2009-02-06 17:22:58 +000019#include "ScheduleDAGSDNodes.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000020#include "llvm/InlineAsm.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000021#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman619ef482009-01-15 19:20:50 +000022#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000023#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
Evan Chenga77f3d32010-07-21 06:09:07 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000029#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000033#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000035using namespace llvm;
36
Dan Gohmanfd227e92008-03-25 17:10:29 +000037STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000038STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000040STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041
Jim Laskey95eda5b2006-08-01 14:21:23 +000042static RegisterScheduler
43 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
Bill Wendling8cbc25d2010-01-23 10:26:57 +000050static RegisterScheduler
51 sourceListDAGScheduler("source",
52 "Similar to list-burr but schedules in source "
53 "order when possible",
54 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000055
Evan Chengbdd062d2010-05-20 06:13:19 +000056static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000057 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000058 "Bottom-up register pressure aware list scheduling "
59 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000060 createHybridListDAGScheduler);
61
Evan Cheng37b740c2010-07-24 00:39:05 +000062static RegisterScheduler
63 ILPListDAGScheduler("list-ilp",
64 "Bottom-up register pressure aware list scheduling "
65 "which tries to balance ILP and register pressure",
66 createILPListDAGScheduler);
67
Evan Chengd38c22b2006-05-11 23:55:42 +000068namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000069//===----------------------------------------------------------------------===//
70/// ScheduleDAGRRList - The actual register reduction list scheduler
71/// implementation. This supports both top-down and bottom-up scheduling.
72///
Nick Lewycky02d5f772009-10-25 06:33:48 +000073class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000074private:
75 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
76 /// it is top-down.
77 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000078
Evan Chengbdd062d2010-05-20 06:13:19 +000079 /// NeedLatency - True if the scheduler will make use of latency information.
80 ///
81 bool NeedLatency;
82
Evan Chengd38c22b2006-05-11 23:55:42 +000083 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000084 SchedulingPriorityQueue *AvailableQueue;
85
Dan Gohmanc07f6862008-09-23 18:50:48 +000086 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000087 /// that are "live". These nodes must be scheduled before any other nodes that
88 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000089 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000090 std::vector<SUnit*> LiveRegDefs;
91 std::vector<unsigned> LiveRegCycles;
92
Dan Gohmanad2134d2008-11-25 00:52:40 +000093 /// Topo - A topological ordering for SUnits which permits fast IsReachable
94 /// and similar queries.
95 ScheduleDAGTopologicalSort Topo;
96
Evan Chengd38c22b2006-05-11 23:55:42 +000097public:
Dan Gohman619ef482009-01-15 19:20:50 +000098 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000099 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +0000100 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +0000101 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +0000102 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000103 }
104
105 ~ScheduleDAGRRList() {
106 delete AvailableQueue;
107 }
108
109 void Schedule();
110
Roman Levenstein733a4d62008-03-26 11:23:38 +0000111 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
113 return Topo.IsReachable(SU, TargetSU);
114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Dan Gohman60d68442009-01-29 19:49:27 +0000116 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000117 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000118 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
119 return Topo.WillCreateCycle(SU, TargetSU);
120 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000121
Dan Gohman2d170892008-12-09 22:54:47 +0000122 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000123 /// This returns true if this is a new predecessor.
124 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000125 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000126 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000127 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000128 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000129
Dan Gohman2d170892008-12-09 22:54:47 +0000130 /// RemovePred - removes a predecessor edge from SUnit SU.
131 /// This returns true if an edge was removed.
132 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000133 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000134 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000135 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000136 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137
Evan Chengd38c22b2006-05-11 23:55:42 +0000138private:
Dan Gohman60d68442009-01-29 19:49:27 +0000139 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000140 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000141 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000142 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000143 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000144 void ScheduleNodeBottomUp(SUnit*, unsigned);
145 void ScheduleNodeTopDown(SUnit*, unsigned);
146 void UnscheduleNodeBottomUp(SUnit*);
147 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
148 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000149 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
150 const TargetRegisterClass*,
151 const TargetRegisterClass*,
152 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000153 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000154 void ListScheduleTopDown();
155 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156
157
158 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000159 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000160 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000161 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000162 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000164 if (NewNode->NodeNum >= NumSUnits)
165 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000166 return NewNode;
167 }
168
Roman Levenstein733a4d62008-03-26 11:23:38 +0000169 /// CreateClone - Creates a new SUnit from an existing one.
170 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000172 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000173 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000174 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000175 if (NewNode->NodeNum >= NumSUnits)
176 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000177 return NewNode;
178 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000179
Evan Chengbdd062d2010-05-20 06:13:19 +0000180 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
181 /// need actual latency information but the hybrid scheduler does.
182 bool ForceUnitLatencies() const {
183 return !NeedLatency;
184 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000185};
186} // end anonymous namespace
187
188
189/// Schedule - Schedule the DAG using list scheduling.
190void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000191 DEBUG(dbgs()
192 << "********** List Scheduling BB#" << BB->getNumber()
193 << " **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000194
Dan Gohmanc07f6862008-09-23 18:50:48 +0000195 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000196 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
197 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000198
Dan Gohman04543e72008-12-23 18:36:58 +0000199 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000200 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000201
Evan Chengd38c22b2006-05-11 23:55:42 +0000202 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000203 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000204 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000205
Dan Gohman46520a22008-06-21 19:18:17 +0000206 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000207
Evan Chengd38c22b2006-05-11 23:55:42 +0000208 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
209 if (isBottomUp)
210 ListScheduleBottomUp();
211 else
212 ListScheduleTopDown();
213
214 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000215}
Evan Chengd38c22b2006-05-11 23:55:42 +0000216
217//===----------------------------------------------------------------------===//
218// Bottom-Up Scheduling
219//===----------------------------------------------------------------------===//
220
Evan Chengd38c22b2006-05-11 23:55:42 +0000221/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000222/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000223void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000224 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000225
Evan Chengd38c22b2006-05-11 23:55:42 +0000226#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000227 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000228 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000229 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000230 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000231 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000232 }
233#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000234 --PredSU->NumSuccsLeft;
235
Evan Chengbdd062d2010-05-20 06:13:19 +0000236 if (!ForceUnitLatencies()) {
237 // Updating predecessor's height. This is now the cycle when the
238 // predecessor can be scheduled without causing a pipeline stall.
239 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
240 }
241
Dan Gohmanb9543432009-02-10 23:27:53 +0000242 // If all the node's successors are scheduled, this node is ready
243 // to be scheduled. Ignore the special EntrySU node.
244 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000245 PredSU->isAvailable = true;
246 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000247 }
248}
249
Dan Gohmanb9543432009-02-10 23:27:53 +0000250void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000251 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000252 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000253 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000254 ReleasePred(SU, &*I);
255 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000256 // This is a physical register dependency and it's impossible or
257 // expensive to copy the register. Make sure nothing that can
258 // clobber the register is scheduled between the predecessor and
259 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000260 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000261 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000262 LiveRegDefs[I->getReg()] = I->getSUnit();
263 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000264 }
265 }
266 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000267}
268
269/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
270/// count of its predecessors. If a predecessor pending count is zero, add it to
271/// the Available queue.
272void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000273 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000274 DEBUG(SU->dump(this));
275
Evan Chengbdd062d2010-05-20 06:13:19 +0000276#ifndef NDEBUG
277 if (CurCycle < SU->getHeight())
278 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
279#endif
280
281 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000282 SU->setHeightToAtLeast(CurCycle);
283 Sequence.push_back(SU);
284
Evan Cheng28590382010-07-21 23:53:58 +0000285 AvailableQueue->ScheduledNode(SU);
286
Dan Gohmanb9543432009-02-10 23:27:53 +0000287 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000288
289 // Release all the implicit physical register defs that are live.
290 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
291 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000292 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000293 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000294 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000295 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000296 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000297 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000298 LiveRegDefs[I->getReg()] = NULL;
299 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000300 }
301 }
302 }
303
Evan Chengd38c22b2006-05-11 23:55:42 +0000304 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000305}
306
Evan Cheng5924bf72007-09-25 01:54:36 +0000307/// CapturePred - This does the opposite of ReleasePred. Since SU is being
308/// unscheduled, incrcease the succ left count of its predecessors. Remove
309/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000310void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
311 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000312 if (PredSU->isAvailable) {
313 PredSU->isAvailable = false;
314 if (!PredSU->isPending)
315 AvailableQueue->remove(PredSU);
316 }
317
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000318 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000319 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000320}
321
322/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
323/// its predecessor states to reflect the change.
324void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000325 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000326 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000327
Evan Cheng5924bf72007-09-25 01:54:36 +0000328 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
329 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000330 CapturePred(&*I);
Evan Chengcc2efe12010-05-28 23:26:21 +0000331 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000332 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000333 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000334 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000335 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000336 LiveRegDefs[I->getReg()] = NULL;
337 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000338 }
339 }
340
341 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
342 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000343 if (I->isAssignedRegDep()) {
344 if (!LiveRegDefs[I->getReg()]) {
345 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000346 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000348 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
349 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000350 }
351 }
352
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000353 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000354 SU->isScheduled = false;
355 SU->isAvailable = true;
356 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000357 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000358}
359
Evan Cheng8e136a92007-09-26 21:36:17 +0000360/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000361/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000362void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
363 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000364 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000365 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000366 OldSU = Sequence.back();
367 Sequence.pop_back();
368 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000369 // Don't try to remove SU from AvailableQueue.
370 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000371 UnscheduleNodeBottomUp(OldSU);
372 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000373 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000374 }
375
Dan Gohman60d68442009-01-29 19:49:27 +0000376 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000377
378 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000379}
380
Evan Cheng3b245872010-02-05 01:27:11 +0000381static bool isOperandOf(const SUnit *SU, SDNode *N) {
382 for (const SDNode *SUNode = SU->getNode(); SUNode;
383 SUNode = SUNode->getFlaggedNode()) {
384 if (SUNode->isOperandOf(N))
385 return true;
386 }
387 return false;
388}
389
Evan Cheng5924bf72007-09-25 01:54:36 +0000390/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
391/// successors to the newly created node.
392SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000393 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000394 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000395
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000396 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000397 if (!N)
398 return NULL;
399
400 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000401 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000402 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000403 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000404 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000405 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000406 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000407 TryUnfold = true;
408 }
Evan Cheng79e97132007-10-05 01:39:18 +0000409 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000410 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000411 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000412 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000413 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000414 }
415
416 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000417 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000418 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000419 return NULL;
420
Evan Chengbdd062d2010-05-20 06:13:19 +0000421 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000422 assert(NewNodes.size() == 2 && "Expected a load folding node!");
423
424 N = NewNodes[1];
425 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000426 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000427 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000428 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000429 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
430 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000431 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000432
Dan Gohmane52e0892008-11-11 21:34:44 +0000433 // LoadNode may already exist. This can happen when there is another
434 // load from the same location and producing the same type of value
435 // but it has different alignment or volatileness.
436 bool isNewLoad = true;
437 SUnit *LoadSU;
438 if (LoadNode->getNodeId() != -1) {
439 LoadSU = &SUnits[LoadNode->getNodeId()];
440 isNewLoad = false;
441 } else {
442 LoadSU = CreateNewSUnit(LoadNode);
443 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000444 ComputeLatency(LoadSU);
445 }
446
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000447 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000448 assert(N->getNodeId() == -1 && "Node already inserted!");
449 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000450
Dan Gohman17059682008-07-17 19:10:17 +0000451 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000452 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000453 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000454 NewSU->isTwoAddress = true;
455 break;
456 }
457 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000458 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000459 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000460 ComputeLatency(NewSU);
461
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000462 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000463 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000464 SmallVector<SDep, 4> ChainSuccs;
465 SmallVector<SDep, 4> LoadPreds;
466 SmallVector<SDep, 4> NodePreds;
467 SmallVector<SDep, 4> NodeSuccs;
468 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
469 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000470 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000471 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000472 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000473 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000474 else
Dan Gohman2d170892008-12-09 22:54:47 +0000475 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000476 }
477 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
478 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000479 if (I->isCtrl())
480 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000481 else
Dan Gohman2d170892008-12-09 22:54:47 +0000482 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000483 }
484
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000485 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000486 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
487 const SDep &Pred = ChainPreds[i];
488 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000489 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000490 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000491 }
Evan Cheng79e97132007-10-05 01:39:18 +0000492 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000493 const SDep &Pred = LoadPreds[i];
494 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000495 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000496 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000497 }
498 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000499 const SDep &Pred = NodePreds[i];
500 RemovePred(SU, Pred);
501 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000502 }
503 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000504 SDep D = NodeSuccs[i];
505 SUnit *SuccDep = D.getSUnit();
506 D.setSUnit(SU);
507 RemovePred(SuccDep, D);
508 D.setSUnit(NewSU);
509 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000510 }
511 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000512 SDep D = ChainSuccs[i];
513 SUnit *SuccDep = D.getSUnit();
514 D.setSUnit(SU);
515 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000516 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000517 D.setSUnit(LoadSU);
518 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000519 }
Evan Cheng79e97132007-10-05 01:39:18 +0000520 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000521
522 // Add a data dependency to reflect that NewSU reads the value defined
523 // by LoadSU.
524 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000525
Evan Cheng91e0fc92007-12-18 08:42:10 +0000526 if (isNewLoad)
527 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000528 AvailableQueue->addNode(NewSU);
529
530 ++NumUnfolds;
531
532 if (NewSU->NumSuccsLeft == 0) {
533 NewSU->isAvailable = true;
534 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000535 }
536 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000537 }
538
Evan Chengbdd062d2010-05-20 06:13:19 +0000539 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000541
542 // New SUnit has the exact same predecessors.
543 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
544 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000545 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000546 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000547
548 // Only copy scheduled successors. Cut them from old node's successor
549 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000550 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000551 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
552 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000553 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000554 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000555 SUnit *SuccSU = I->getSUnit();
556 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000557 SDep D = *I;
558 D.setSUnit(NewSU);
559 AddPred(SuccSU, D);
560 D.setSUnit(SU);
561 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000562 }
563 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000564 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000565 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000566
567 AvailableQueue->updateNode(SU);
568 AvailableQueue->addNode(NewSU);
569
Evan Cheng1ec79b42007-09-27 07:09:03 +0000570 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000571 return NewSU;
572}
573
Evan Chengb2c42c62009-01-12 03:19:55 +0000574/// InsertCopiesAndMoveSuccs - Insert register copies and move all
575/// scheduled successors of the given SUnit to the last copy.
576void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
577 const TargetRegisterClass *DestRC,
578 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000579 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000580 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000581 CopyFromSU->CopySrcRC = SrcRC;
582 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000583
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000584 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000585 CopyToSU->CopySrcRC = DestRC;
586 CopyToSU->CopyDstRC = SrcRC;
587
588 // Only copy scheduled successors. Cut them from old node's successor
589 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000590 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000591 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
592 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000593 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000594 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000595 SUnit *SuccSU = I->getSUnit();
596 if (SuccSU->isScheduled) {
597 SDep D = *I;
598 D.setSUnit(CopyToSU);
599 AddPred(SuccSU, D);
600 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000601 }
602 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000603 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000604 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000605
Dan Gohman2d170892008-12-09 22:54:47 +0000606 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
607 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000608
609 AvailableQueue->updateNode(SU);
610 AvailableQueue->addNode(CopyFromSU);
611 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000612 Copies.push_back(CopyFromSU);
613 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000614
Evan Chengb2c42c62009-01-12 03:19:55 +0000615 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000616}
617
618/// getPhysicalRegisterVT - Returns the ValueType of the physical register
619/// definition of the specified node.
620/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000621static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000622 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000623 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000624 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000625 unsigned NumRes = TID.getNumDefs();
626 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000627 if (Reg == *ImpDef)
628 break;
629 ++NumRes;
630 }
631 return N->getValueType(NumRes);
632}
633
Evan Chengb8905c42009-03-04 01:41:49 +0000634/// CheckForLiveRegDef - Return true and update live register vector if the
635/// specified register def of the specified SUnit clobbers any "live" registers.
636static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
637 std::vector<SUnit*> &LiveRegDefs,
638 SmallSet<unsigned, 4> &RegAdded,
639 SmallVector<unsigned, 4> &LRegs,
640 const TargetRegisterInfo *TRI) {
641 bool Added = false;
642 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
643 if (RegAdded.insert(Reg)) {
644 LRegs.push_back(Reg);
645 Added = true;
646 }
647 }
648 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
649 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
650 if (RegAdded.insert(*Alias)) {
651 LRegs.push_back(*Alias);
652 Added = true;
653 }
654 }
655 return Added;
656}
657
Evan Cheng5924bf72007-09-25 01:54:36 +0000658/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
659/// scheduling of the given node to satisfy live physical register dependencies.
660/// If the specific node is the last one that's available to schedule, do
661/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000662bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
663 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000664 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000665 return false;
666
Evan Chenge6f92252007-09-27 18:46:06 +0000667 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000669 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
670 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000671 if (I->isAssignedRegDep())
672 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
673 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000674 }
675
Dan Gohman072734e2008-11-13 23:24:17 +0000676 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000677 if (Node->getOpcode() == ISD::INLINEASM) {
678 // Inline asm can clobber physical defs.
679 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000680 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000681 --NumOps; // Ignore the flag operand.
682
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000683 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000684 unsigned Flags =
685 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000686 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000687
688 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000689 if (InlineAsm::isRegDefKind(Flags) ||
690 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000691 // Check for def of register or earlyclobber register.
692 for (; NumVals; --NumVals, ++i) {
693 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
694 if (TargetRegisterInfo::isPhysicalRegister(Reg))
695 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
696 }
697 } else
698 i += NumVals;
699 }
700 continue;
701 }
702
Dan Gohman072734e2008-11-13 23:24:17 +0000703 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000704 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000705 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000706 if (!TID.ImplicitDefs)
707 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000708 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
709 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000710 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000711 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000712}
713
Evan Cheng1ec79b42007-09-27 07:09:03 +0000714
Evan Chengd38c22b2006-05-11 23:55:42 +0000715/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
716/// schedulers.
717void ScheduleDAGRRList::ListScheduleBottomUp() {
718 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000719
720 // Release any predecessors of the special Exit node.
721 ReleasePredecessors(&ExitSU, CurCycle);
722
Evan Chengd38c22b2006-05-11 23:55:42 +0000723 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000724 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000725 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000726 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
727 RootSU->isAvailable = true;
728 AvailableQueue->push(RootSU);
729 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000730
731 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000732 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000733 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000734 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000735 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000736 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000737 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000738 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000739 SUnit *CurSU = AvailableQueue->pop();
740 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000741 SmallVector<unsigned, 4> LRegs;
742 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
743 break;
744 Delayed = true;
745 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000746
747 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
748 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000749 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000750 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000751
752 // All candidates are delayed due to live physical reg dependencies.
753 // Try backtracking, code duplication, or inserting cross class copies
754 // to resolve it.
755 if (Delayed && !CurSU) {
756 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
757 SUnit *TrySU = NotReady[i];
758 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
759
760 // Try unscheduling up to the point where it's safe to schedule
761 // this node.
762 unsigned LiveCycle = CurCycle;
763 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
764 unsigned Reg = LRegs[j];
765 unsigned LCycle = LiveRegCycles[Reg];
766 LiveCycle = std::min(LiveCycle, LCycle);
767 }
768 SUnit *OldSU = Sequence[LiveCycle];
769 if (!WillCreateCycle(TrySU, OldSU)) {
770 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
771 // Force the current node to be scheduled before the node that
772 // requires the physical reg dep.
773 if (OldSU->isAvailable) {
774 OldSU->isAvailable = false;
775 AvailableQueue->remove(OldSU);
776 }
Dan Gohman2d170892008-12-09 22:54:47 +0000777 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
778 /*Reg=*/0, /*isNormalMemory=*/false,
779 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000780 // If one or more successors has been unscheduled, then the current
781 // node is no longer avaialable. Schedule a successor that's now
782 // available instead.
783 if (!TrySU->isAvailable)
784 CurSU = AvailableQueue->pop();
785 else {
786 CurSU = TrySU;
787 TrySU->isPending = false;
788 NotReady.erase(NotReady.begin()+i);
789 }
790 break;
791 }
792 }
793
794 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000795 // Can't backtrack. If it's too expensive to copy the value, then try
796 // duplicate the nodes that produces these "too expensive to copy"
797 // values to break the dependency. In case even that doesn't work,
798 // insert cross class copies.
799 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000800 SUnit *TrySU = NotReady[0];
801 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
802 assert(LRegs.size() == 1 && "Can't handle this yet!");
803 unsigned Reg = LRegs[0];
804 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000805 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000806 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000807 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000808 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
809
810 // If cross copy register class is null, then it must be possible copy
811 // the value directly. Do not try duplicate the def.
812 SUnit *NewDef = 0;
813 if (DestRC)
814 NewDef = CopyAndMoveSuccessors(LRDef);
815 else
816 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000817 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000818 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000819 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000820 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000821 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000822 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000823 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000824 /*Reg=*/0, /*isNormalMemory=*/false,
825 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000826 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000827 NewDef = Copies.back();
828 }
829
Evan Chengbdd062d2010-05-20 06:13:19 +0000830 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000831 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000832 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000833 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000834 /*Reg=*/0, /*isNormalMemory=*/false,
835 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000836 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000837 TrySU->isAvailable = false;
838 CurSU = NewDef;
839 }
840
Dan Gohman60d68442009-01-29 19:49:27 +0000841 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000842 }
843
Evan Chengd38c22b2006-05-11 23:55:42 +0000844 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000845 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
846 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000847 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000848 if (NotReady[i]->isAvailable)
849 AvailableQueue->push(NotReady[i]);
850 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000851 NotReady.clear();
852
Dan Gohmanc602dd42008-11-21 00:10:42 +0000853 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000854 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000855 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000856 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000857 }
858
Evan Chengd38c22b2006-05-11 23:55:42 +0000859 // Reverse the order if it is bottom up.
860 std::reverse(Sequence.begin(), Sequence.end());
861
Evan Chengd38c22b2006-05-11 23:55:42 +0000862#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000863 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000864#endif
865}
866
867//===----------------------------------------------------------------------===//
868// Top-Down Scheduling
869//===----------------------------------------------------------------------===//
870
871/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000872/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000873void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000874 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000875
Evan Chengd38c22b2006-05-11 23:55:42 +0000876#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000877 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000878 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000879 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000880 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000881 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000882 }
883#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000884 --SuccSU->NumPredsLeft;
885
Dan Gohmanb9543432009-02-10 23:27:53 +0000886 // If all the node's predecessors are scheduled, this node is ready
887 // to be scheduled. Ignore the special ExitSU node.
888 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000889 SuccSU->isAvailable = true;
890 AvailableQueue->push(SuccSU);
891 }
892}
893
Dan Gohmanb9543432009-02-10 23:27:53 +0000894void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
895 // Top down: release successors
896 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
897 I != E; ++I) {
898 assert(!I->isAssignedRegDep() &&
899 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
900
901 ReleaseSucc(SU, &*I);
902 }
903}
904
Evan Chengd38c22b2006-05-11 23:55:42 +0000905/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
906/// count of its successors. If a successor pending count is zero, add it to
907/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000908void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000909 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000910 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000911
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000912 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
913 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000914 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000915
Dan Gohmanb9543432009-02-10 23:27:53 +0000916 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000917 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000918 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000919}
920
Dan Gohman54a187e2007-08-20 19:28:38 +0000921/// ListScheduleTopDown - The main loop of list scheduling for top-down
922/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000923void ScheduleDAGRRList::ListScheduleTopDown() {
924 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000925 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000926
Dan Gohmanb9543432009-02-10 23:27:53 +0000927 // Release any successors of the special Entry node.
928 ReleaseSuccessors(&EntrySU);
929
Evan Chengd38c22b2006-05-11 23:55:42 +0000930 // All leaves to Available queue.
931 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
932 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000933 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000934 AvailableQueue->push(&SUnits[i]);
935 SUnits[i].isAvailable = true;
936 }
937 }
938
Evan Chengd38c22b2006-05-11 23:55:42 +0000939 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000940 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000941 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000942 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000943 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000944
Dan Gohmanc602dd42008-11-21 00:10:42 +0000945 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000946 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000947 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000948 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000949 }
950
Evan Chengd38c22b2006-05-11 23:55:42 +0000951#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000952 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000953#endif
954}
955
956
Evan Chengd38c22b2006-05-11 23:55:42 +0000957//===----------------------------------------------------------------------===//
958// RegReductionPriorityQueue Implementation
959//===----------------------------------------------------------------------===//
960//
961// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
962// to reduce register pressure.
963//
964namespace {
965 template<class SF>
966 class RegReductionPriorityQueue;
967
968 /// Sorting functions for the Available queue.
969 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
970 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
971 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
972 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
973
974 bool operator()(const SUnit* left, const SUnit* right) const;
975 };
976
977 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
978 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
979 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
980 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
981
982 bool operator()(const SUnit* left, const SUnit* right) const;
983 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000984
985 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
986 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
987 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
988 : SPQ(spq) {}
989 src_ls_rr_sort(const src_ls_rr_sort &RHS)
990 : SPQ(RHS.SPQ) {}
991
992 bool operator()(const SUnit* left, const SUnit* right) const;
993 };
Evan Chengbdd062d2010-05-20 06:13:19 +0000994
995 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
996 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
997 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
998 : SPQ(spq) {}
999 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1000 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001001
Evan Chengbdd062d2010-05-20 06:13:19 +00001002 bool operator()(const SUnit* left, const SUnit* right) const;
1003 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001004
1005 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1006 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1007 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1008 : SPQ(spq) {}
1009 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1010 : SPQ(RHS.SPQ) {}
1011
1012 bool operator()(const SUnit* left, const SUnit* right) const;
1013 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001014} // end anonymous namespace
1015
Dan Gohman186f65d2008-11-20 03:30:37 +00001016/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1017/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001018static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001019CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001020 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1021 if (SethiUllmanNumber != 0)
1022 return SethiUllmanNumber;
1023
1024 unsigned Extra = 0;
1025 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1026 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001027 if (I->isCtrl()) continue; // ignore chain preds
1028 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001029 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001030 if (PredSethiUllman > SethiUllmanNumber) {
1031 SethiUllmanNumber = PredSethiUllman;
1032 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001033 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001034 ++Extra;
1035 }
1036
1037 SethiUllmanNumber += Extra;
1038
1039 if (SethiUllmanNumber == 0)
1040 SethiUllmanNumber = 1;
1041
1042 return SethiUllmanNumber;
1043}
1044
Evan Chengd38c22b2006-05-11 23:55:42 +00001045namespace {
1046 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001047 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001048 std::vector<SUnit*> Queue;
1049 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001050 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001051 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001052
Dan Gohman3f656df2008-11-20 02:45:51 +00001053 protected:
1054 // SUnits - The SUnits for the current graph.
1055 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001056
1057 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001058 const TargetInstrInfo *TII;
1059 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001060 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001061 ScheduleDAGRRList *scheduleDAG;
1062
Dan Gohman186f65d2008-11-20 03:30:37 +00001063 // SethiUllmanNumbers - The SethiUllman number for each node.
1064 std::vector<unsigned> SethiUllmanNumbers;
1065
Evan Chenga77f3d32010-07-21 06:09:07 +00001066 /// RegPressure - Tracking current reg pressure per register class.
1067 ///
Evan Cheng28590382010-07-21 23:53:58 +00001068 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001069
1070 /// RegLimit - Tracking the number of allocatable registers per register
1071 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001072 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001073
Dan Gohman3f656df2008-11-20 02:45:51 +00001074 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001075 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001076 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001077 const TargetInstrInfo *tii,
1078 const TargetRegisterInfo *tri,
1079 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001080 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001081 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001082 if (TracksRegPressure) {
1083 unsigned NumRC = TRI->getNumRegClasses();
1084 RegLimit.resize(NumRC);
1085 RegPressure.resize(NumRC);
1086 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1087 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1088 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1089 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001090 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001091 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001092 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001093
1094 void initNodes(std::vector<SUnit> &sunits) {
1095 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001096 // Add pseudo dependency edges for two-address nodes.
1097 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001098 // Reroute edges to nodes with multiple uses.
1099 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001100 // Calculate node priorities.
1101 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001102 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001103
Dan Gohman186f65d2008-11-20 03:30:37 +00001104 void addNode(const SUnit *SU) {
1105 unsigned SUSize = SethiUllmanNumbers.size();
1106 if (SUnits->size() > SUSize)
1107 SethiUllmanNumbers.resize(SUSize*2, 0);
1108 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1109 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001110
Dan Gohman186f65d2008-11-20 03:30:37 +00001111 void updateNode(const SUnit *SU) {
1112 SethiUllmanNumbers[SU->NodeNum] = 0;
1113 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1114 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001115
Dan Gohman186f65d2008-11-20 03:30:37 +00001116 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001117 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001118 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001119 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001120 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001121
1122 unsigned getNodePriority(const SUnit *SU) const {
1123 assert(SU->NodeNum < SethiUllmanNumbers.size());
1124 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001125 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001126 // CopyToReg should be close to its uses to facilitate coalescing and
1127 // avoid spilling.
1128 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001129 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1130 Opc == TargetOpcode::SUBREG_TO_REG ||
1131 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001132 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1133 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001134 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001135 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1136 // If SU does not have a register use, i.e. it doesn't produce a value
1137 // that would be consumed (e.g. store), then it terminates a chain of
1138 // computation. Give it a large SethiUllman number so it will be
1139 // scheduled right before its predecessors that it doesn't lengthen
1140 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001141 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001142 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1143 // If SU does not have a register def, schedule it close to its uses
1144 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001145 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001146 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001147 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001148
1149 unsigned getNodeOrdering(const SUnit *SU) const {
1150 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1151 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001152
Evan Chengd38c22b2006-05-11 23:55:42 +00001153 bool empty() const { return Queue.empty(); }
1154
1155 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001156 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001157 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001158 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001159 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001160
Evan Chengd38c22b2006-05-11 23:55:42 +00001161 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001162 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001163 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001164 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001165 E = Queue.end(); I != E; ++I)
1166 if (Picker(*Best, *I))
1167 Best = I;
1168 SUnit *V = *Best;
1169 if (Best != prior(Queue.end()))
1170 std::swap(*Best, Queue.back());
1171 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001172 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001173 return V;
1174 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001175
Evan Cheng5924bf72007-09-25 01:54:36 +00001176 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001177 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001178 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001179 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1180 SU);
1181 if (I != prior(Queue.end()))
1182 std::swap(*I, Queue.back());
1183 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001184 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001185 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001186
Evan Chengdf907f42010-07-23 22:39:59 +00001187 bool HighRegPressure(const SUnit *SU, unsigned &Excess) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001188 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001189 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001190
Evan Chengdf907f42010-07-23 22:39:59 +00001191 bool High = false;
1192 Excess = 0;
Evan Chenga77f3d32010-07-21 06:09:07 +00001193 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1194 I != E; ++I) {
1195 if (I->isCtrl())
1196 continue;
1197 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001198 const SDNode *PN = PredSU->getNode();
1199 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001200 if (PN->getOpcode() == ISD::CopyFromReg) {
1201 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001202 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1203 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chengdf907f42010-07-23 22:39:59 +00001204 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1205 High = true;
1206 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
1207 }
1208 }
1209 continue;
1210 }
1211 unsigned POpc = PN->getMachineOpcode();
1212 if (POpc == TargetOpcode::IMPLICIT_DEF)
1213 continue;
1214 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1215 EVT VT = PN->getOperand(0).getValueType();
1216 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1217 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1218 // Check if this increases register pressure of the specific register
1219 // class to the point where it would cause spills.
1220 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1221 High = true;
1222 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
1223 }
1224 continue;
1225 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1226 POpc == TargetOpcode::SUBREG_TO_REG) {
1227 EVT VT = PN->getValueType(0);
1228 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1229 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1230 // Check if this increases register pressure of the specific register
1231 // class to the point where it would cause spills.
1232 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1233 High = true;
1234 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
Evan Cheng28590382010-07-21 23:53:58 +00001235 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001236 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001237 }
1238 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001239 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001240 EVT VT = PN->getValueType(i);
1241 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001242 continue;
1243 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1244 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1245 // Check if this increases register pressure of the specific register
1246 // class to the point where it would cause spills.
Evan Chengdf907f42010-07-23 22:39:59 +00001247 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1248 High = true;
1249 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
1250 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001251 }
1252 }
1253
Evan Chengdf907f42010-07-23 22:39:59 +00001254 return High;
Evan Chenga77f3d32010-07-21 06:09:07 +00001255 }
1256
Evan Chengbf32e542010-07-22 06:24:48 +00001257 void ScheduledNode(SUnit *SU) {
1258 if (!TracksRegPressure)
1259 return;
1260
Evan Chenga77f3d32010-07-21 06:09:07 +00001261 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001262 if (!N->isMachineOpcode()) {
1263 if (N->getOpcode() != ISD::CopyToReg)
1264 return;
1265 } else {
1266 unsigned Opc = N->getMachineOpcode();
1267 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1268 Opc == TargetOpcode::INSERT_SUBREG ||
1269 Opc == TargetOpcode::SUBREG_TO_REG ||
1270 Opc == TargetOpcode::REG_SEQUENCE ||
1271 Opc == TargetOpcode::IMPLICIT_DEF)
1272 return;
1273 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001274
1275 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1276 I != E; ++I) {
1277 if (I->isCtrl())
1278 continue;
1279 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001280 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001281 continue;
1282 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001283 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001284 if (PN->getOpcode() == ISD::CopyFromReg) {
1285 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001286 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1287 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1288 }
1289 continue;
1290 }
1291 unsigned POpc = PN->getMachineOpcode();
1292 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001293 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001294 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1295 EVT VT = PN->getOperand(0).getValueType();
1296 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1297 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1298 continue;
1299 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1300 POpc == TargetOpcode::SUBREG_TO_REG) {
1301 EVT VT = PN->getValueType(0);
1302 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1303 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1304 continue;
1305 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001306 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1307 for (unsigned i = 0; i != NumDefs; ++i) {
1308 EVT VT = PN->getValueType(i);
1309 if (!PN->hasAnyUseOfValue(i))
1310 continue;
1311 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1312 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1313 }
1314 }
1315
Evan Chengdf907f42010-07-23 22:39:59 +00001316 if (SU->NumSuccs) {
1317 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1318 for (unsigned i = 0; i != NumDefs; ++i) {
1319 EVT VT = N->getValueType(i);
1320 if (!N->hasAnyUseOfValue(i))
1321 continue;
1322 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1323 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1324 // Register pressure tracking is imprecise. This can happen.
1325 RegPressure[RCId] = 0;
1326 else
1327 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1328 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001329 }
Evan Chengbf32e542010-07-22 06:24:48 +00001330
1331 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001332 }
1333
Evan Chengbf32e542010-07-22 06:24:48 +00001334 void UnscheduledNode(SUnit *SU) {
1335 if (!TracksRegPressure)
1336 return;
1337
Evan Chenga77f3d32010-07-21 06:09:07 +00001338 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001339 if (!N->isMachineOpcode()) {
1340 if (N->getOpcode() != ISD::CopyToReg)
1341 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001342 } else {
1343 unsigned Opc = N->getMachineOpcode();
1344 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1345 Opc == TargetOpcode::INSERT_SUBREG ||
1346 Opc == TargetOpcode::SUBREG_TO_REG ||
1347 Opc == TargetOpcode::REG_SEQUENCE ||
1348 Opc == TargetOpcode::IMPLICIT_DEF)
1349 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001350 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001351
1352 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1353 I != E; ++I) {
1354 if (I->isCtrl())
1355 continue;
1356 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001357 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001358 continue;
1359 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001360 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001361 if (PN->getOpcode() == ISD::CopyFromReg) {
1362 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001363 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1364 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1365 }
1366 continue;
1367 }
1368 unsigned POpc = PN->getMachineOpcode();
1369 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001370 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001371 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1372 EVT VT = PN->getOperand(0).getValueType();
1373 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1374 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1375 continue;
1376 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1377 POpc == TargetOpcode::SUBREG_TO_REG) {
1378 EVT VT = PN->getValueType(0);
1379 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1380 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1381 continue;
1382 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001383 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1384 for (unsigned i = 0; i != NumDefs; ++i) {
1385 EVT VT = PN->getValueType(i);
1386 if (!PN->hasAnyUseOfValue(i))
1387 continue;
1388 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001389 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001390 // Register pressure tracking is imprecise. This can happen.
1391 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001392 else
1393 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001394 }
1395 }
1396
Evan Chengdf907f42010-07-23 22:39:59 +00001397 if (SU->NumSuccs) {
1398 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1399 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1400 EVT VT = N->getValueType(i);
1401 if (VT == MVT::Flag || VT == MVT::Other)
1402 continue;
1403 if (!N->hasAnyUseOfValue(i))
1404 continue;
1405 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1406 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1407 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001408 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001409
Evan Chenga77f3d32010-07-21 06:09:07 +00001410 dumpRegPressure();
1411 }
1412
Dan Gohman3f656df2008-11-20 02:45:51 +00001413 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1414 scheduleDAG = scheduleDag;
1415 }
1416
Evan Chenga77f3d32010-07-21 06:09:07 +00001417 void dumpRegPressure() const {
1418 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1419 E = TRI->regclass_end(); I != E; ++I) {
1420 const TargetRegisterClass *RC = *I;
1421 unsigned Id = RC->getID();
1422 unsigned RP = RegPressure[Id];
1423 if (!RP) continue;
1424 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1425 << '\n');
1426 }
1427 }
1428
Dan Gohman3f656df2008-11-20 02:45:51 +00001429 protected:
1430 bool canClobber(const SUnit *SU, const SUnit *Op);
1431 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001432 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001433 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001434 };
1435
Dan Gohman186f65d2008-11-20 03:30:37 +00001436 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1437 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001438
Dan Gohman186f65d2008-11-20 03:30:37 +00001439 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1440 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001441
1442 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1443 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001444
1445 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1446 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001447
1448 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1449 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001450}
1451
Evan Chengb9e3db62007-03-14 22:43:40 +00001452/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001453/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001454static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001455 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001456 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001457 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001458 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001459 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001460 // If there are bunch of CopyToRegs stacked up, they should be considered
1461 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001462 if (I->getSUnit()->getNode() &&
1463 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001464 Height = closestSucc(I->getSUnit())+1;
1465 if (Height > MaxHeight)
1466 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001467 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001468 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001469}
1470
Evan Cheng61bc51e2007-12-20 02:22:36 +00001471/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001472/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001473static unsigned calcMaxScratches(const SUnit *SU) {
1474 unsigned Scratches = 0;
1475 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001476 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001477 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001478 Scratches++;
1479 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001480 return Scratches;
1481}
1482
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001483template <typename RRSort>
1484static bool BURRSort(const SUnit *left, const SUnit *right,
1485 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001486 unsigned LPriority = SPQ->getNodePriority(left);
1487 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001488 if (LPriority != RPriority)
1489 return LPriority > RPriority;
1490
1491 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1492 // e.g.
1493 // t1 = op t2, c1
1494 // t3 = op t4, c2
1495 //
1496 // and the following instructions are both ready.
1497 // t2 = op c3
1498 // t4 = op c4
1499 //
1500 // Then schedule t2 = op first.
1501 // i.e.
1502 // t4 = op c4
1503 // t2 = op c3
1504 // t1 = op t2, c1
1505 // t3 = op t4, c2
1506 //
1507 // This creates more short live intervals.
1508 unsigned LDist = closestSucc(left);
1509 unsigned RDist = closestSucc(right);
1510 if (LDist != RDist)
1511 return LDist < RDist;
1512
Evan Cheng3a14efa2009-02-12 08:59:45 +00001513 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001514 unsigned LScratch = calcMaxScratches(left);
1515 unsigned RScratch = calcMaxScratches(right);
1516 if (LScratch != RScratch)
1517 return LScratch > RScratch;
1518
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001519 if (left->getHeight() != right->getHeight())
1520 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001521
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001522 if (left->getDepth() != right->getDepth())
1523 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001524
Roman Levenstein6b371142008-04-29 09:07:59 +00001525 assert(left->NodeQueueId && right->NodeQueueId &&
1526 "NodeQueueId cannot be zero");
1527 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001528}
1529
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001530// Bottom up
1531bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1532 return BURRSort(left, right, SPQ);
1533}
1534
1535// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001536bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001537 unsigned LOrder = SPQ->getNodeOrdering(left);
1538 unsigned ROrder = SPQ->getNodeOrdering(right);
1539
1540 // Prefer an ordering where the lower the non-zero order number, the higher
1541 // the preference.
1542 if ((LOrder || ROrder) && LOrder != ROrder)
1543 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1544
1545 return BURRSort(left, right, SPQ);
1546}
1547
Evan Chengbdd062d2010-05-20 06:13:19 +00001548bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdf907f42010-07-23 22:39:59 +00001549 unsigned LExcess, RExcess;
1550 bool LHigh = SPQ->HighRegPressure(left, LExcess);
1551 bool RHigh = SPQ->HighRegPressure(right, RExcess);
Evan Cheng37b740c2010-07-24 00:39:05 +00001552 // Avoid causing spills. If register pressure is high, schedule for
1553 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001554 if (LHigh && !RHigh)
1555 return true;
1556 else if (!LHigh && RHigh)
1557 return false;
Evan Chengdf907f42010-07-23 22:39:59 +00001558 else if (LHigh && RHigh) {
1559 if (LExcess > RExcess)
1560 return true;
1561 else if (LExcess < RExcess)
1562 return false;
Evan Chengdf907f42010-07-23 22:39:59 +00001563 } else {
Evan Cheng28590382010-07-21 23:53:58 +00001564 // Low register pressure situation, schedule for latency if possible.
1565 bool LStall = left->SchedulingPref == Sched::Latency &&
1566 SPQ->getCurCycle() < left->getHeight();
1567 bool RStall = right->SchedulingPref == Sched::Latency &&
1568 SPQ->getCurCycle() < right->getHeight();
1569 // If scheduling one of the node will cause a pipeline stall, delay it.
1570 // If scheduling either one of the node will cause a pipeline stall, sort
1571 // them according to their height.
1572 // If neither will cause a pipeline stall, try to reduce register pressure.
1573 if (LStall) {
1574 if (!RStall)
1575 return true;
1576 if (left->getHeight() != right->getHeight())
1577 return left->getHeight() > right->getHeight();
1578 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001579 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001580
Evan Cheng28590382010-07-21 23:53:58 +00001581 // If either node is scheduling for latency, sort them by height and latency
1582 // first.
1583 if (left->SchedulingPref == Sched::Latency ||
1584 right->SchedulingPref == Sched::Latency) {
1585 if (left->getHeight() != right->getHeight())
1586 return left->getHeight() > right->getHeight();
1587 if (left->Latency != right->Latency)
1588 return left->Latency > right->Latency;
1589 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001590 }
1591
Evan Chengbdd062d2010-05-20 06:13:19 +00001592 return BURRSort(left, right, SPQ);
1593}
1594
Evan Cheng37b740c2010-07-24 00:39:05 +00001595bool ilp_ls_rr_sort::operator()(const SUnit *left,
1596 const SUnit *right) const {
1597 unsigned LExcess, RExcess;
1598 bool LHigh = SPQ->HighRegPressure(left, LExcess);
1599 bool RHigh = SPQ->HighRegPressure(right, RExcess);
1600 // Avoid causing spills. If register pressure is high, schedule for
1601 // register pressure reduction.
1602 if (LHigh && !RHigh)
1603 return true;
1604 else if (!LHigh && RHigh)
1605 return false;
1606 else if (LHigh && RHigh) {
1607 if (LExcess > RExcess)
1608 return true;
1609 else if (LExcess < RExcess)
1610 return false;
1611 } else {
1612 // Low register pressure situation, schedule for ILP.
1613 if (left->NumPreds > right->NumPreds)
1614 return false;
1615 else if (left->NumPreds < right->NumPreds)
1616 return false;
1617 }
1618
1619 return BURRSort(left, right, SPQ);
1620}
1621
Dan Gohman3f656df2008-11-20 02:45:51 +00001622template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001623bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001624RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001625 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001626 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001627 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001628 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001629 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001630 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001631 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001632 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001633 if (DU->getNodeId() != -1 &&
1634 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001635 return true;
1636 }
1637 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001638 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001639 return false;
1640}
1641
Evan Chenga5e595d2007-09-28 22:32:30 +00001642/// hasCopyToRegUse - Return true if SU has a value successor that is a
1643/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001644static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001645 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1646 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001647 if (I->isCtrl()) continue;
1648 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001649 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001650 return true;
1651 }
1652 return false;
1653}
1654
Evan Chengf9891412007-12-20 09:25:31 +00001655/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001656/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001657static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001658 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001659 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001660 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001661 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1662 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001663 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001664 for (const SDNode *SUNode = SU->getNode(); SUNode;
1665 SUNode = SUNode->getFlaggedNode()) {
1666 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001667 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001668 const unsigned *SUImpDefs =
1669 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1670 if (!SUImpDefs)
1671 return false;
1672 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001673 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001674 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001675 continue;
1676 if (!N->hasAnyUseOfValue(i))
1677 continue;
1678 unsigned Reg = ImpDefs[i - NumDefs];
1679 for (;*SUImpDefs; ++SUImpDefs) {
1680 unsigned SUReg = *SUImpDefs;
1681 if (TRI->regsOverlap(Reg, SUReg))
1682 return true;
1683 }
Evan Chengf9891412007-12-20 09:25:31 +00001684 }
1685 }
1686 return false;
1687}
1688
Dan Gohman9a658d72009-03-24 00:49:12 +00001689/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1690/// are not handled well by the general register pressure reduction
1691/// heuristics. When presented with code like this:
1692///
1693/// N
1694/// / |
1695/// / |
1696/// U store
1697/// |
1698/// ...
1699///
1700/// the heuristics tend to push the store up, but since the
1701/// operand of the store has another use (U), this would increase
1702/// the length of that other use (the U->N edge).
1703///
1704/// This function transforms code like the above to route U's
1705/// dependence through the store when possible, like this:
1706///
1707/// N
1708/// ||
1709/// ||
1710/// store
1711/// |
1712/// U
1713/// |
1714/// ...
1715///
1716/// This results in the store being scheduled immediately
1717/// after N, which shortens the U->N live range, reducing
1718/// register pressure.
1719///
1720template<class SF>
1721void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1722 // Visit all the nodes in topological order, working top-down.
1723 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1724 SUnit *SU = &(*SUnits)[i];
1725 // For now, only look at nodes with no data successors, such as stores.
1726 // These are especially important, due to the heuristics in
1727 // getNodePriority for nodes with no data successors.
1728 if (SU->NumSuccs != 0)
1729 continue;
1730 // For now, only look at nodes with exactly one data predecessor.
1731 if (SU->NumPreds != 1)
1732 continue;
1733 // Avoid prescheduling copies to virtual registers, which don't behave
1734 // like other nodes from the perspective of scheduling heuristics.
1735 if (SDNode *N = SU->getNode())
1736 if (N->getOpcode() == ISD::CopyToReg &&
1737 TargetRegisterInfo::isVirtualRegister
1738 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1739 continue;
1740
1741 // Locate the single data predecessor.
1742 SUnit *PredSU = 0;
1743 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1744 EE = SU->Preds.end(); II != EE; ++II)
1745 if (!II->isCtrl()) {
1746 PredSU = II->getSUnit();
1747 break;
1748 }
1749 assert(PredSU);
1750
1751 // Don't rewrite edges that carry physregs, because that requires additional
1752 // support infrastructure.
1753 if (PredSU->hasPhysRegDefs)
1754 continue;
1755 // Short-circuit the case where SU is PredSU's only data successor.
1756 if (PredSU->NumSuccs == 1)
1757 continue;
1758 // Avoid prescheduling to copies from virtual registers, which don't behave
1759 // like other nodes from the perspective of scheduling // heuristics.
1760 if (SDNode *N = SU->getNode())
1761 if (N->getOpcode() == ISD::CopyFromReg &&
1762 TargetRegisterInfo::isVirtualRegister
1763 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1764 continue;
1765
1766 // Perform checks on the successors of PredSU.
1767 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1768 EE = PredSU->Succs.end(); II != EE; ++II) {
1769 SUnit *PredSuccSU = II->getSUnit();
1770 if (PredSuccSU == SU) continue;
1771 // If PredSU has another successor with no data successors, for
1772 // now don't attempt to choose either over the other.
1773 if (PredSuccSU->NumSuccs == 0)
1774 goto outer_loop_continue;
1775 // Don't break physical register dependencies.
1776 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1777 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1778 goto outer_loop_continue;
1779 // Don't introduce graph cycles.
1780 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1781 goto outer_loop_continue;
1782 }
1783
1784 // Ok, the transformation is safe and the heuristics suggest it is
1785 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001786 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1787 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001788 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001789 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1790 SDep Edge = PredSU->Succs[i];
1791 assert(!Edge.isAssignedRegDep());
1792 SUnit *SuccSU = Edge.getSUnit();
1793 if (SuccSU != SU) {
1794 Edge.setSUnit(PredSU);
1795 scheduleDAG->RemovePred(SuccSU, Edge);
1796 scheduleDAG->AddPred(SU, Edge);
1797 Edge.setSUnit(SU);
1798 scheduleDAG->AddPred(SuccSU, Edge);
1799 --i;
1800 }
1801 }
1802 outer_loop_continue:;
1803 }
1804}
1805
Evan Chengd38c22b2006-05-11 23:55:42 +00001806/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1807/// it as a def&use operand. Add a pseudo control edge from it to the other
1808/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001809/// first (lower in the schedule). If both nodes are two-address, favor the
1810/// one that has a CopyToReg use (more likely to be a loop induction update).
1811/// If both are two-address, but one is commutable while the other is not
1812/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001813template<class SF>
1814void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001815 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001816 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001817 if (!SU->isTwoAddress)
1818 continue;
1819
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001820 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001821 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001822 continue;
1823
Dan Gohman17059682008-07-17 19:10:17 +00001824 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001825 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001826 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001827 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001828 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001829 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1830 continue;
1831 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1832 if (DU->getNodeId() == -1)
1833 continue;
1834 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1835 if (!DUSU) continue;
1836 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1837 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001838 if (I->isCtrl()) continue;
1839 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001840 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001841 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001842 // Be conservative. Ignore if nodes aren't at roughly the same
1843 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001844 if (SuccSU->getHeight() < SU->getHeight() &&
1845 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001846 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001847 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1848 // constrains whatever is using the copy, instead of the copy
1849 // itself. In the case that the copy is coalesced, this
1850 // preserves the intent of the pseudo two-address heurietics.
1851 while (SuccSU->Succs.size() == 1 &&
1852 SuccSU->getNode()->isMachineOpcode() &&
1853 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001854 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001855 SuccSU = SuccSU->Succs.front().getSUnit();
1856 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001857 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1858 continue;
1859 // Don't constrain nodes with physical register defs if the
1860 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001861 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001862 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001863 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001864 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001865 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1866 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001867 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001868 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1869 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1870 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001871 continue;
1872 if ((!canClobber(SuccSU, DUSU) ||
1873 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1874 (!SU->isCommutable && SuccSU->isCommutable)) &&
1875 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001876 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001877 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001878 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001879 /*Reg=*/0, /*isNormalMemory=*/false,
1880 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001881 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001882 }
1883 }
1884 }
1885 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001886}
1887
Evan Cheng6730f032007-01-08 23:55:53 +00001888/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1889/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001890template<class SF>
1891void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001892 SethiUllmanNumbers.assign(SUnits->size(), 0);
1893
1894 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001895 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001896}
Evan Chengd38c22b2006-05-11 23:55:42 +00001897
Roman Levenstein30d09512008-03-27 09:44:37 +00001898/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001899/// predecessors of the successors of the SUnit SU. Stop when the provided
1900/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001901static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1902 unsigned Limit) {
1903 unsigned Sum = 0;
1904 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1905 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001906 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001907 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1908 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001909 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001910 if (!PredSU->isScheduled)
1911 if (++Sum > Limit)
1912 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001913 }
1914 }
1915 return Sum;
1916}
1917
Evan Chengd38c22b2006-05-11 23:55:42 +00001918
1919// Top down
1920bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001921 unsigned LPriority = SPQ->getNodePriority(left);
1922 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001923 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1924 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001925 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1926 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001927 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1928 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001929
1930 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1931 return false;
1932 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1933 return true;
1934
Evan Chengd38c22b2006-05-11 23:55:42 +00001935 if (LIsFloater)
1936 LBonus -= 2;
1937 if (RIsFloater)
1938 RBonus -= 2;
1939 if (left->NumSuccs == 1)
1940 LBonus += 2;
1941 if (right->NumSuccs == 1)
1942 RBonus += 2;
1943
Evan Cheng73bdf042008-03-01 00:39:47 +00001944 if (LPriority+LBonus != RPriority+RBonus)
1945 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001946
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001947 if (left->getDepth() != right->getDepth())
1948 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001949
1950 if (left->NumSuccsLeft != right->NumSuccsLeft)
1951 return left->NumSuccsLeft > right->NumSuccsLeft;
1952
Roman Levenstein6b371142008-04-29 09:07:59 +00001953 assert(left->NodeQueueId && right->NodeQueueId &&
1954 "NodeQueueId cannot be zero");
1955 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001956}
1957
Evan Chengd38c22b2006-05-11 23:55:42 +00001958//===----------------------------------------------------------------------===//
1959// Public Constructor Functions
1960//===----------------------------------------------------------------------===//
1961
Dan Gohmandfaf6462009-02-11 04:27:20 +00001962llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001963llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001964 const TargetMachine &TM = IS->TM;
1965 const TargetInstrInfo *TII = TM.getInstrInfo();
1966 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001967
Evan Chenga77f3d32010-07-21 06:09:07 +00001968 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00001969 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001970 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001971 PQ->setScheduleDAG(SD);
1972 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001973}
1974
Dan Gohmandfaf6462009-02-11 04:27:20 +00001975llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001976llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001977 const TargetMachine &TM = IS->TM;
1978 const TargetInstrInfo *TII = TM.getInstrInfo();
1979 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001980
Evan Chenga77f3d32010-07-21 06:09:07 +00001981 TDRegReductionPriorityQueue *PQ =
1982 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001983 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001984 PQ->setScheduleDAG(SD);
1985 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001986}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001987
1988llvm::ScheduleDAGSDNodes *
1989llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1990 const TargetMachine &TM = IS->TM;
1991 const TargetInstrInfo *TII = TM.getInstrInfo();
1992 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1993
Evan Chenga77f3d32010-07-21 06:09:07 +00001994 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00001995 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001996 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
1997 PQ->setScheduleDAG(SD);
1998 return SD;
1999}
2000
2001llvm::ScheduleDAGSDNodes *
2002llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2003 const TargetMachine &TM = IS->TM;
2004 const TargetInstrInfo *TII = TM.getInstrInfo();
2005 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002006 const TargetLowering *TLI = &IS->getTargetLowering();
Evan Chengbdd062d2010-05-20 06:13:19 +00002007
Evan Chenga77f3d32010-07-21 06:09:07 +00002008 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002009 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002010 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002011 PQ->setScheduleDAG(SD);
2012 return SD;
2013}
Evan Cheng37b740c2010-07-24 00:39:05 +00002014
2015llvm::ScheduleDAGSDNodes *
2016llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2017 const TargetMachine &TM = IS->TM;
2018 const TargetInstrInfo *TII = TM.getInstrInfo();
2019 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2020 const TargetLowering *TLI = &IS->getTargetLowering();
2021
2022 ILPBURRPriorityQueue *PQ =
2023 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2024 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2025 PQ->setScheduleDAG(SD);
2026 return SD;
2027}