blob: 72bfe7195d6c1066eaaba7de10c2109f2e02a88f [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman483377c2009-02-06 17:22:58 +000019#include "ScheduleDAGSDNodes.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000020#include "llvm/InlineAsm.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000021#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman619ef482009-01-15 19:20:50 +000022#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000023#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
Evan Chenga77f3d32010-07-21 06:09:07 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000029#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000033#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000035using namespace llvm;
36
Dan Gohmanfd227e92008-03-25 17:10:29 +000037STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000038STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000040STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041
Jim Laskey95eda5b2006-08-01 14:21:23 +000042static RegisterScheduler
43 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
Bill Wendling8cbc25d2010-01-23 10:26:57 +000050static RegisterScheduler
51 sourceListDAGScheduler("source",
52 "Similar to list-burr but schedules in source "
53 "order when possible",
54 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000055
Evan Chengbdd062d2010-05-20 06:13:19 +000056static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000057 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000058 "Bottom-up register pressure aware list scheduling "
59 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000060 createHybridListDAGScheduler);
61
Evan Cheng37b740c2010-07-24 00:39:05 +000062static RegisterScheduler
63 ILPListDAGScheduler("list-ilp",
64 "Bottom-up register pressure aware list scheduling "
65 "which tries to balance ILP and register pressure",
66 createILPListDAGScheduler);
67
Evan Chengd38c22b2006-05-11 23:55:42 +000068namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000069//===----------------------------------------------------------------------===//
70/// ScheduleDAGRRList - The actual register reduction list scheduler
71/// implementation. This supports both top-down and bottom-up scheduling.
72///
Nick Lewycky02d5f772009-10-25 06:33:48 +000073class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000074private:
75 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
76 /// it is top-down.
77 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000078
Evan Chengbdd062d2010-05-20 06:13:19 +000079 /// NeedLatency - True if the scheduler will make use of latency information.
80 ///
81 bool NeedLatency;
82
Evan Chengd38c22b2006-05-11 23:55:42 +000083 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000084 SchedulingPriorityQueue *AvailableQueue;
85
Dan Gohmanc07f6862008-09-23 18:50:48 +000086 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000087 /// that are "live". These nodes must be scheduled before any other nodes that
88 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000089 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000090 std::vector<SUnit*> LiveRegDefs;
91 std::vector<unsigned> LiveRegCycles;
92
Dan Gohmanad2134d2008-11-25 00:52:40 +000093 /// Topo - A topological ordering for SUnits which permits fast IsReachable
94 /// and similar queries.
95 ScheduleDAGTopologicalSort Topo;
96
Evan Chengd38c22b2006-05-11 23:55:42 +000097public:
Dan Gohman619ef482009-01-15 19:20:50 +000098 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000099 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +0000100 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +0000101 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +0000102 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000103 }
104
105 ~ScheduleDAGRRList() {
106 delete AvailableQueue;
107 }
108
109 void Schedule();
110
Roman Levenstein733a4d62008-03-26 11:23:38 +0000111 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
113 return Topo.IsReachable(SU, TargetSU);
114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Dan Gohman60d68442009-01-29 19:49:27 +0000116 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000117 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000118 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
119 return Topo.WillCreateCycle(SU, TargetSU);
120 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000121
Dan Gohman2d170892008-12-09 22:54:47 +0000122 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000123 /// This returns true if this is a new predecessor.
124 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000125 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000126 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000127 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000128 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000129
Dan Gohman2d170892008-12-09 22:54:47 +0000130 /// RemovePred - removes a predecessor edge from SUnit SU.
131 /// This returns true if an edge was removed.
132 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000133 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000134 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000135 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000136 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137
Evan Chengd38c22b2006-05-11 23:55:42 +0000138private:
Dan Gohman60d68442009-01-29 19:49:27 +0000139 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000140 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000141 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000142 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000143 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000144 void ScheduleNodeBottomUp(SUnit*, unsigned);
145 void ScheduleNodeTopDown(SUnit*, unsigned);
146 void UnscheduleNodeBottomUp(SUnit*);
147 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
148 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000149 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
150 const TargetRegisterClass*,
151 const TargetRegisterClass*,
152 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000153 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000154 void ListScheduleTopDown();
155 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156
157
158 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000159 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000160 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000161 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000162 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000164 if (NewNode->NodeNum >= NumSUnits)
165 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000166 return NewNode;
167 }
168
Roman Levenstein733a4d62008-03-26 11:23:38 +0000169 /// CreateClone - Creates a new SUnit from an existing one.
170 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000172 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000173 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000174 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000175 if (NewNode->NodeNum >= NumSUnits)
176 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000177 return NewNode;
178 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000179
Evan Chengbdd062d2010-05-20 06:13:19 +0000180 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
181 /// need actual latency information but the hybrid scheduler does.
182 bool ForceUnitLatencies() const {
183 return !NeedLatency;
184 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000185};
186} // end anonymous namespace
187
188
189/// Schedule - Schedule the DAG using list scheduling.
190void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000191 DEBUG(dbgs()
192 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000193 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000194
Dan Gohmanc07f6862008-09-23 18:50:48 +0000195 NumLiveRegs = 0;
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);
Chris Lattner981afd22010-12-20 00:55:43 +0000286
Dan Gohmanb9543432009-02-10 23:27:53 +0000287 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000288
289 // Release all the implicit physical register defs that are live.
290 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
291 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000292 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000293 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000294 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000295 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000296 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000297 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000298 LiveRegDefs[I->getReg()] = NULL;
299 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000300 }
301 }
302 }
303
Evan Chengd38c22b2006-05-11 23:55:42 +0000304 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000305}
306
Evan Cheng5924bf72007-09-25 01:54:36 +0000307/// CapturePred - This does the opposite of ReleasePred. Since SU is being
308/// unscheduled, incrcease the succ left count of its predecessors. Remove
309/// them from AvailableQueue if necessary.
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.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000636static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +0000637 std::vector<SUnit*> &LiveRegDefs,
638 SmallSet<unsigned, 4> &RegAdded,
639 SmallVector<unsigned, 4> &LRegs,
640 const TargetRegisterInfo *TRI) {
Evan Chengb8905c42009-03-04 01:41:49 +0000641 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
Chris Lattner0cfe8842010-12-20 00:51:56 +0000642 if (RegAdded.insert(Reg))
Evan Chengb8905c42009-03-04 01:41:49 +0000643 LRegs.push_back(Reg);
Evan Chengb8905c42009-03-04 01:41:49 +0000644 }
645 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
646 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
Chris Lattner0cfe8842010-12-20 00:51:56 +0000647 if (RegAdded.insert(*Alias))
Evan Chengb8905c42009-03-04 01:41:49 +0000648 LRegs.push_back(*Alias);
Evan Chengb8905c42009-03-04 01:41:49 +0000649 }
Evan Chengb8905c42009-03-04 01:41:49 +0000650}
651
Evan Cheng5924bf72007-09-25 01:54:36 +0000652/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
653/// scheduling of the given node to satisfy live physical register dependencies.
654/// If the specific node is the last one that's available to schedule, do
655/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000656bool ScheduleDAGRRList::
657DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000658 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000659 return false;
660
Evan Chenge6f92252007-09-27 18:46:06 +0000661 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000662 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000663 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
664 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000665 if (I->isAssignedRegDep())
666 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
667 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 }
669
Dan Gohman072734e2008-11-13 23:24:17 +0000670 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000671 if (Node->getOpcode() == ISD::INLINEASM) {
672 // Inline asm can clobber physical defs.
673 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000674 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000675 --NumOps; // Ignore the flag operand.
676
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000677 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000678 unsigned Flags =
679 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000680 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000681
682 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000683 if (InlineAsm::isRegDefKind(Flags) ||
684 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000685 // Check for def of register or earlyclobber register.
686 for (; NumVals; --NumVals, ++i) {
687 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
688 if (TargetRegisterInfo::isPhysicalRegister(Reg))
689 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
690 }
691 } else
692 i += NumVals;
693 }
694 continue;
695 }
696
Dan Gohman072734e2008-11-13 23:24:17 +0000697 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000698 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000699 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000700 if (!TID.ImplicitDefs)
701 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000702 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
703 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000704 }
Chris Lattner0cfe8842010-12-20 00:51:56 +0000705
706
Chris Lattner981afd22010-12-20 00:55:43 +0000707 // Okay, we now know all of the live registers that are defined by an
708 // immediate predecessor. It is ok to kill these registers if we are also
709 // using it.
710 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
711 I != E; ++I) {
712 if (I->isAssignedRegDep() &&
713 LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
714 unsigned Reg = I->getReg();
715 if (RegAdded.erase(Reg))
716 LRegs.erase(std::find(LRegs.begin(), LRegs.end(), Reg));
717 }
718 }
719
Evan Cheng5924bf72007-09-25 01:54:36 +0000720 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000721}
722
Evan Cheng1ec79b42007-09-27 07:09:03 +0000723
Evan Chengd38c22b2006-05-11 23:55:42 +0000724/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
725/// schedulers.
726void ScheduleDAGRRList::ListScheduleBottomUp() {
727 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000728
729 // Release any predecessors of the special Exit node.
730 ReleasePredecessors(&ExitSU, CurCycle);
731
Evan Chengd38c22b2006-05-11 23:55:42 +0000732 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000733 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000734 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000735 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
736 RootSU->isAvailable = true;
737 AvailableQueue->push(RootSU);
738 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000739
740 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000741 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000742 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000743 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000744 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000745 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000746 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000747 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000748 SUnit *CurSU = AvailableQueue->pop();
749 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000750 SmallVector<unsigned, 4> LRegs;
751 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
752 break;
753 Delayed = true;
754 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000755
756 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
757 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000758 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000759 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000760
761 // All candidates are delayed due to live physical reg dependencies.
762 // Try backtracking, code duplication, or inserting cross class copies
763 // to resolve it.
764 if (Delayed && !CurSU) {
765 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
766 SUnit *TrySU = NotReady[i];
767 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
768
769 // Try unscheduling up to the point where it's safe to schedule
770 // this node.
771 unsigned LiveCycle = CurCycle;
772 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
773 unsigned Reg = LRegs[j];
774 unsigned LCycle = LiveRegCycles[Reg];
775 LiveCycle = std::min(LiveCycle, LCycle);
776 }
777 SUnit *OldSU = Sequence[LiveCycle];
778 if (!WillCreateCycle(TrySU, OldSU)) {
779 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
780 // Force the current node to be scheduled before the node that
781 // requires the physical reg dep.
782 if (OldSU->isAvailable) {
783 OldSU->isAvailable = false;
784 AvailableQueue->remove(OldSU);
785 }
Dan Gohman2d170892008-12-09 22:54:47 +0000786 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
787 /*Reg=*/0, /*isNormalMemory=*/false,
788 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000789 // If one or more successors has been unscheduled, then the current
790 // node is no longer avaialable. Schedule a successor that's now
791 // available instead.
792 if (!TrySU->isAvailable)
793 CurSU = AvailableQueue->pop();
794 else {
795 CurSU = TrySU;
796 TrySU->isPending = false;
797 NotReady.erase(NotReady.begin()+i);
798 }
799 break;
800 }
801 }
802
803 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000804 // Can't backtrack. If it's too expensive to copy the value, then try
805 // duplicate the nodes that produces these "too expensive to copy"
806 // values to break the dependency. In case even that doesn't work,
807 // insert cross class copies.
808 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000809 SUnit *TrySU = NotReady[0];
810 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
811 assert(LRegs.size() == 1 && "Can't handle this yet!");
812 unsigned Reg = LRegs[0];
813 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000814 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000815 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000816 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000817 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
818
819 // If cross copy register class is null, then it must be possible copy
820 // the value directly. Do not try duplicate the def.
821 SUnit *NewDef = 0;
822 if (DestRC)
823 NewDef = CopyAndMoveSuccessors(LRDef);
824 else
825 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000826 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000827 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000828 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000829 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000830 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000831 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000832 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000833 /*Reg=*/0, /*isNormalMemory=*/false,
834 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000835 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000836 NewDef = Copies.back();
837 }
838
Evan Chengbdd062d2010-05-20 06:13:19 +0000839 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000840 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000841 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000842 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000843 /*Reg=*/0, /*isNormalMemory=*/false,
844 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000845 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000846 TrySU->isAvailable = false;
847 CurSU = NewDef;
848 }
849
Dan Gohman60d68442009-01-29 19:49:27 +0000850 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000851 }
852
Evan Chengd38c22b2006-05-11 23:55:42 +0000853 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000854 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
855 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000856 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000857 if (NotReady[i]->isAvailable)
858 AvailableQueue->push(NotReady[i]);
859 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000860 NotReady.clear();
861
Dan Gohmanc602dd42008-11-21 00:10:42 +0000862 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000863 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000864 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000865 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000866 }
867
Evan Chengd38c22b2006-05-11 23:55:42 +0000868 // Reverse the order if it is bottom up.
869 std::reverse(Sequence.begin(), Sequence.end());
870
Evan Chengd38c22b2006-05-11 23:55:42 +0000871#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000872 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000873#endif
874}
875
876//===----------------------------------------------------------------------===//
877// Top-Down Scheduling
878//===----------------------------------------------------------------------===//
879
880/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000881/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000882void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000883 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000884
Evan Chengd38c22b2006-05-11 23:55:42 +0000885#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000886 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000887 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000888 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000889 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000890 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000891 }
892#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000893 --SuccSU->NumPredsLeft;
894
Dan Gohmanb9543432009-02-10 23:27:53 +0000895 // If all the node's predecessors are scheduled, this node is ready
896 // to be scheduled. Ignore the special ExitSU node.
897 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000898 SuccSU->isAvailable = true;
899 AvailableQueue->push(SuccSU);
900 }
901}
902
Dan Gohmanb9543432009-02-10 23:27:53 +0000903void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
904 // Top down: release successors
905 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
906 I != E; ++I) {
907 assert(!I->isAssignedRegDep() &&
908 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
909
910 ReleaseSucc(SU, &*I);
911 }
912}
913
Evan Chengd38c22b2006-05-11 23:55:42 +0000914/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
915/// count of its successors. If a successor pending count is zero, add it to
916/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000917void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000918 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000919 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000920
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000921 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
922 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000923 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000924
Dan Gohmanb9543432009-02-10 23:27:53 +0000925 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000926 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000927 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000928}
929
Dan Gohman54a187e2007-08-20 19:28:38 +0000930/// ListScheduleTopDown - The main loop of list scheduling for top-down
931/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000932void ScheduleDAGRRList::ListScheduleTopDown() {
933 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000934 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000935
Dan Gohmanb9543432009-02-10 23:27:53 +0000936 // Release any successors of the special Entry node.
937 ReleaseSuccessors(&EntrySU);
938
Evan Chengd38c22b2006-05-11 23:55:42 +0000939 // All leaves to Available queue.
940 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
941 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000942 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000943 AvailableQueue->push(&SUnits[i]);
944 SUnits[i].isAvailable = true;
945 }
946 }
947
Evan Chengd38c22b2006-05-11 23:55:42 +0000948 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000949 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000950 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000951 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000952 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000953
Dan Gohmanc602dd42008-11-21 00:10:42 +0000954 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000955 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000956 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000957 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000958 }
959
Evan Chengd38c22b2006-05-11 23:55:42 +0000960#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000961 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000962#endif
963}
964
965
Evan Chengd38c22b2006-05-11 23:55:42 +0000966//===----------------------------------------------------------------------===//
967// RegReductionPriorityQueue Implementation
968//===----------------------------------------------------------------------===//
969//
970// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
971// to reduce register pressure.
972//
973namespace {
974 template<class SF>
975 class RegReductionPriorityQueue;
976
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000977 /// bu_ls_rr_sort - Priority function for bottom up register pressure
978 // reduction scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000979 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
980 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
981 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
982 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
983
984 bool operator()(const SUnit* left, const SUnit* right) const;
985 };
986
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000987 // td_ls_rr_sort - Priority function for top down register pressure reduction
988 // scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000989 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
990 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
991 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
992 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
993
994 bool operator()(const SUnit* left, const SUnit* right) const;
995 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000996
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000997 // src_ls_rr_sort - Priority function for source order scheduler.
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000998 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
999 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
1000 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
1001 : SPQ(spq) {}
1002 src_ls_rr_sort(const src_ls_rr_sort &RHS)
1003 : SPQ(RHS.SPQ) {}
1004
1005 bool operator()(const SUnit* left, const SUnit* right) const;
1006 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001007
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001008 // hybrid_ls_rr_sort - Priority function for hybrid scheduler.
Evan Chengbdd062d2010-05-20 06:13:19 +00001009 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1010 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
1011 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
1012 : SPQ(spq) {}
1013 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1014 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001015
Evan Chengbdd062d2010-05-20 06:13:19 +00001016 bool operator()(const SUnit* left, const SUnit* right) const;
1017 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001018
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001019 // ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1020 // scheduler.
Evan Cheng37b740c2010-07-24 00:39:05 +00001021 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1022 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1023 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1024 : SPQ(spq) {}
1025 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1026 : SPQ(RHS.SPQ) {}
1027
1028 bool operator()(const SUnit* left, const SUnit* right) const;
1029 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001030} // end anonymous namespace
1031
Dan Gohman186f65d2008-11-20 03:30:37 +00001032/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1033/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001034static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001035CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001036 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1037 if (SethiUllmanNumber != 0)
1038 return SethiUllmanNumber;
1039
1040 unsigned Extra = 0;
1041 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1042 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001043 if (I->isCtrl()) continue; // ignore chain preds
1044 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001045 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001046 if (PredSethiUllman > SethiUllmanNumber) {
1047 SethiUllmanNumber = PredSethiUllman;
1048 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001049 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001050 ++Extra;
1051 }
1052
1053 SethiUllmanNumber += Extra;
1054
1055 if (SethiUllmanNumber == 0)
1056 SethiUllmanNumber = 1;
1057
1058 return SethiUllmanNumber;
1059}
1060
Evan Chengd38c22b2006-05-11 23:55:42 +00001061namespace {
1062 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001063 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001064 std::vector<SUnit*> Queue;
1065 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001066 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001067 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001068
Dan Gohman3f656df2008-11-20 02:45:51 +00001069 protected:
1070 // SUnits - The SUnits for the current graph.
1071 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001072
1073 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001074 const TargetInstrInfo *TII;
1075 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001076 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001077 ScheduleDAGRRList *scheduleDAG;
1078
Dan Gohman186f65d2008-11-20 03:30:37 +00001079 // SethiUllmanNumbers - The SethiUllman number for each node.
1080 std::vector<unsigned> SethiUllmanNumbers;
1081
Evan Chenga77f3d32010-07-21 06:09:07 +00001082 /// RegPressure - Tracking current reg pressure per register class.
1083 ///
Evan Cheng28590382010-07-21 23:53:58 +00001084 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001085
1086 /// RegLimit - Tracking the number of allocatable registers per register
1087 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001088 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001089
Dan Gohman3f656df2008-11-20 02:45:51 +00001090 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001091 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001092 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001093 const TargetInstrInfo *tii,
1094 const TargetRegisterInfo *tri,
1095 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001096 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001097 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001098 if (TracksRegPressure) {
1099 unsigned NumRC = TRI->getNumRegClasses();
1100 RegLimit.resize(NumRC);
1101 RegPressure.resize(NumRC);
1102 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1103 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1104 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1105 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001106 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001107 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001108 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001109
1110 void initNodes(std::vector<SUnit> &sunits) {
1111 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001112 // Add pseudo dependency edges for two-address nodes.
1113 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001114 // Reroute edges to nodes with multiple uses.
1115 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001116 // Calculate node priorities.
1117 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001118 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001119
Dan Gohman186f65d2008-11-20 03:30:37 +00001120 void addNode(const SUnit *SU) {
1121 unsigned SUSize = SethiUllmanNumbers.size();
1122 if (SUnits->size() > SUSize)
1123 SethiUllmanNumbers.resize(SUSize*2, 0);
1124 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1125 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001126
Dan Gohman186f65d2008-11-20 03:30:37 +00001127 void updateNode(const SUnit *SU) {
1128 SethiUllmanNumbers[SU->NodeNum] = 0;
1129 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1130 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001131
Dan Gohman186f65d2008-11-20 03:30:37 +00001132 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001133 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001134 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001135 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001136 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001137
1138 unsigned getNodePriority(const SUnit *SU) const {
1139 assert(SU->NodeNum < SethiUllmanNumbers.size());
1140 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001141 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001142 // CopyToReg should be close to its uses to facilitate coalescing and
1143 // avoid spilling.
1144 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001145 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1146 Opc == TargetOpcode::SUBREG_TO_REG ||
1147 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001148 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1149 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001150 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001151 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1152 // If SU does not have a register use, i.e. it doesn't produce a value
1153 // that would be consumed (e.g. store), then it terminates a chain of
1154 // computation. Give it a large SethiUllman number so it will be
1155 // scheduled right before its predecessors that it doesn't lengthen
1156 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001157 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001158 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1159 // If SU does not have a register def, schedule it close to its uses
1160 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001161 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001162 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001163 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001164
1165 unsigned getNodeOrdering(const SUnit *SU) const {
1166 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1167 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001168
Evan Chengd38c22b2006-05-11 23:55:42 +00001169 bool empty() const { return Queue.empty(); }
1170
1171 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001172 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001173 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001174 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001175 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001176
Evan Chengd38c22b2006-05-11 23:55:42 +00001177 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001178 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001179 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001180 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001181 E = Queue.end(); I != E; ++I)
1182 if (Picker(*Best, *I))
1183 Best = I;
1184 SUnit *V = *Best;
1185 if (Best != prior(Queue.end()))
1186 std::swap(*Best, Queue.back());
1187 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001188 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001189 return V;
1190 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001191
Evan Cheng5924bf72007-09-25 01:54:36 +00001192 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001193 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001194 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001195 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1196 SU);
1197 if (I != prior(Queue.end()))
1198 std::swap(*I, Queue.back());
1199 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001200 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001201 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001202
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001203 bool HighRegPressure(const SUnit *SU) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001204 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001205 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001206
Evan Chenga77f3d32010-07-21 06:09:07 +00001207 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1208 I != E; ++I) {
1209 if (I->isCtrl())
1210 continue;
1211 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001212 const SDNode *PN = PredSU->getNode();
1213 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001214 if (PN->getOpcode() == ISD::CopyFromReg) {
1215 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001216 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1217 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001218 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1219 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001220 }
1221 continue;
1222 }
1223 unsigned POpc = PN->getMachineOpcode();
1224 if (POpc == TargetOpcode::IMPLICIT_DEF)
1225 continue;
1226 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1227 EVT VT = PN->getOperand(0).getValueType();
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.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001232 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1233 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001234 continue;
1235 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1236 POpc == TargetOpcode::SUBREG_TO_REG) {
1237 EVT VT = PN->getValueType(0);
1238 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1239 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1240 // Check if this increases register pressure of the specific register
1241 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001242 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1243 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001244 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001245 }
1246 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001247 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001248 EVT VT = PN->getValueType(i);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001249 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1250 if (RegPressure[RCId] >= RegLimit[RCId])
1251 return true; // Reg pressure already high.
1252 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Cheng28590382010-07-21 23:53:58 +00001253 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001254 continue;
Evan Chenga77f3d32010-07-21 06:09:07 +00001255 // Check if this increases register pressure of the specific register
1256 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001257 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1258 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001259 }
1260 }
1261
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001262 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001263 }
1264
Evan Chengbf32e542010-07-22 06:24:48 +00001265 void ScheduledNode(SUnit *SU) {
1266 if (!TracksRegPressure)
1267 return;
1268
Evan Chenga77f3d32010-07-21 06:09:07 +00001269 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001270 if (!N->isMachineOpcode()) {
1271 if (N->getOpcode() != ISD::CopyToReg)
1272 return;
1273 } else {
1274 unsigned Opc = N->getMachineOpcode();
1275 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1276 Opc == TargetOpcode::INSERT_SUBREG ||
1277 Opc == TargetOpcode::SUBREG_TO_REG ||
1278 Opc == TargetOpcode::REG_SEQUENCE ||
1279 Opc == TargetOpcode::IMPLICIT_DEF)
1280 return;
1281 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001282
1283 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1284 I != E; ++I) {
1285 if (I->isCtrl())
1286 continue;
1287 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001288 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001289 continue;
1290 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001291 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001292 if (PN->getOpcode() == ISD::CopyFromReg) {
1293 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001294 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1295 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1296 }
1297 continue;
1298 }
1299 unsigned POpc = PN->getMachineOpcode();
1300 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001301 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001302 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1303 EVT VT = PN->getOperand(0).getValueType();
1304 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1305 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1306 continue;
1307 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1308 POpc == TargetOpcode::SUBREG_TO_REG) {
1309 EVT VT = PN->getValueType(0);
1310 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1311 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1312 continue;
1313 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001314 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1315 for (unsigned i = 0; i != NumDefs; ++i) {
1316 EVT VT = PN->getValueType(i);
1317 if (!PN->hasAnyUseOfValue(i))
1318 continue;
1319 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1320 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1321 }
1322 }
1323
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001324 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1325 // may transfer data dependencies to CopyToReg.
1326 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001327 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1328 for (unsigned i = 0; i != NumDefs; ++i) {
1329 EVT VT = N->getValueType(i);
1330 if (!N->hasAnyUseOfValue(i))
1331 continue;
1332 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1333 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1334 // Register pressure tracking is imprecise. This can happen.
1335 RegPressure[RCId] = 0;
1336 else
1337 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1338 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001339 }
Evan Chengbf32e542010-07-22 06:24:48 +00001340
1341 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001342 }
1343
Evan Chengbf32e542010-07-22 06:24:48 +00001344 void UnscheduledNode(SUnit *SU) {
1345 if (!TracksRegPressure)
1346 return;
1347
Evan Chenga77f3d32010-07-21 06:09:07 +00001348 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001349 if (!N->isMachineOpcode()) {
1350 if (N->getOpcode() != ISD::CopyToReg)
1351 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001352 } else {
1353 unsigned Opc = N->getMachineOpcode();
1354 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1355 Opc == TargetOpcode::INSERT_SUBREG ||
1356 Opc == TargetOpcode::SUBREG_TO_REG ||
1357 Opc == TargetOpcode::REG_SEQUENCE ||
1358 Opc == TargetOpcode::IMPLICIT_DEF)
1359 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001360 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001361
1362 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1363 I != E; ++I) {
1364 if (I->isCtrl())
1365 continue;
1366 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001367 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001368 continue;
1369 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001370 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001371 if (PN->getOpcode() == ISD::CopyFromReg) {
1372 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001373 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1374 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1375 }
1376 continue;
1377 }
1378 unsigned POpc = PN->getMachineOpcode();
1379 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001380 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001381 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1382 EVT VT = PN->getOperand(0).getValueType();
1383 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1384 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1385 continue;
1386 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1387 POpc == TargetOpcode::SUBREG_TO_REG) {
1388 EVT VT = PN->getValueType(0);
1389 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1390 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1391 continue;
1392 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001393 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1394 for (unsigned i = 0; i != NumDefs; ++i) {
1395 EVT VT = PN->getValueType(i);
1396 if (!PN->hasAnyUseOfValue(i))
1397 continue;
1398 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001399 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001400 // Register pressure tracking is imprecise. This can happen.
1401 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001402 else
1403 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001404 }
1405 }
1406
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001407 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1408 // may transfer data dependencies to CopyToReg.
1409 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001410 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1411 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1412 EVT VT = N->getValueType(i);
1413 if (VT == MVT::Flag || VT == MVT::Other)
1414 continue;
1415 if (!N->hasAnyUseOfValue(i))
1416 continue;
1417 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1418 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1419 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001420 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001421
Evan Chenga77f3d32010-07-21 06:09:07 +00001422 dumpRegPressure();
1423 }
1424
Dan Gohman3f656df2008-11-20 02:45:51 +00001425 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1426 scheduleDAG = scheduleDag;
1427 }
1428
Evan Chenga77f3d32010-07-21 06:09:07 +00001429 void dumpRegPressure() const {
1430 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1431 E = TRI->regclass_end(); I != E; ++I) {
1432 const TargetRegisterClass *RC = *I;
1433 unsigned Id = RC->getID();
1434 unsigned RP = RegPressure[Id];
1435 if (!RP) continue;
1436 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1437 << '\n');
1438 }
1439 }
1440
Dan Gohman3f656df2008-11-20 02:45:51 +00001441 protected:
1442 bool canClobber(const SUnit *SU, const SUnit *Op);
1443 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001444 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001445 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001446 };
1447
Dan Gohman186f65d2008-11-20 03:30:37 +00001448 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1449 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001450
Dan Gohman186f65d2008-11-20 03:30:37 +00001451 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1452 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001453
1454 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1455 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001456
1457 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1458 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001459
1460 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1461 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001462}
1463
Evan Chengb9e3db62007-03-14 22:43:40 +00001464/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001465/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001466static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001467 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001468 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001469 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001470 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001471 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001472 // If there are bunch of CopyToRegs stacked up, they should be considered
1473 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001474 if (I->getSUnit()->getNode() &&
1475 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001476 Height = closestSucc(I->getSUnit())+1;
1477 if (Height > MaxHeight)
1478 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001479 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001480 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001481}
1482
Evan Cheng61bc51e2007-12-20 02:22:36 +00001483/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001484/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001485static unsigned calcMaxScratches(const SUnit *SU) {
1486 unsigned Scratches = 0;
1487 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001488 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001489 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001490 Scratches++;
1491 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001492 return Scratches;
1493}
1494
Evan Cheng6c1414f2010-10-29 18:09:28 +00001495/// hasOnlyLiveOutUse - Return true if SU has a single value successor that is a
1496/// CopyToReg to a virtual register. This SU def is probably a liveout and
1497/// it has no other use. It should be scheduled closer to the terminator.
1498static bool hasOnlyLiveOutUses(const SUnit *SU) {
1499 bool RetVal = false;
1500 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1501 I != E; ++I) {
1502 if (I->isCtrl()) continue;
1503 const SUnit *SuccSU = I->getSUnit();
1504 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
1505 unsigned Reg =
1506 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
1507 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1508 RetVal = true;
1509 continue;
1510 }
1511 }
1512 return false;
1513 }
1514 return RetVal;
1515}
1516
1517/// UnitsSharePred - Return true if the two scheduling units share a common
1518/// data predecessor.
1519static bool UnitsSharePred(const SUnit *left, const SUnit *right) {
1520 SmallSet<const SUnit*, 4> Preds;
1521 for (SUnit::const_pred_iterator I = left->Preds.begin(),E = left->Preds.end();
1522 I != E; ++I) {
1523 if (I->isCtrl()) continue; // ignore chain preds
1524 Preds.insert(I->getSUnit());
1525 }
1526 for (SUnit::const_pred_iterator I = right->Preds.begin(),E = right->Preds.end();
1527 I != E; ++I) {
1528 if (I->isCtrl()) continue; // ignore chain preds
1529 if (Preds.count(I->getSUnit()))
1530 return true;
1531 }
1532 return false;
1533}
1534
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001535template <typename RRSort>
1536static bool BURRSort(const SUnit *left, const SUnit *right,
1537 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001538 unsigned LPriority = SPQ->getNodePriority(left);
1539 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001540 if (LPriority != RPriority)
1541 return LPriority > RPriority;
1542
1543 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1544 // e.g.
1545 // t1 = op t2, c1
1546 // t3 = op t4, c2
1547 //
1548 // and the following instructions are both ready.
1549 // t2 = op c3
1550 // t4 = op c4
1551 //
1552 // Then schedule t2 = op first.
1553 // i.e.
1554 // t4 = op c4
1555 // t2 = op c3
1556 // t1 = op t2, c1
1557 // t3 = op t4, c2
1558 //
1559 // This creates more short live intervals.
1560 unsigned LDist = closestSucc(left);
1561 unsigned RDist = closestSucc(right);
1562 if (LDist != RDist)
1563 return LDist < RDist;
1564
Evan Cheng3a14efa2009-02-12 08:59:45 +00001565 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001566 unsigned LScratch = calcMaxScratches(left);
1567 unsigned RScratch = calcMaxScratches(right);
1568 if (LScratch != RScratch)
1569 return LScratch > RScratch;
1570
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001571 if (left->getHeight() != right->getHeight())
1572 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001573
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001574 if (left->getDepth() != right->getDepth())
1575 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001576
Roman Levenstein6b371142008-04-29 09:07:59 +00001577 assert(left->NodeQueueId && right->NodeQueueId &&
1578 "NodeQueueId cannot be zero");
1579 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001580}
1581
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001582// Bottom up
1583bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1584 return BURRSort(left, right, SPQ);
1585}
1586
1587// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001588bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001589 unsigned LOrder = SPQ->getNodeOrdering(left);
1590 unsigned ROrder = SPQ->getNodeOrdering(right);
1591
1592 // Prefer an ordering where the lower the non-zero order number, the higher
1593 // the preference.
1594 if ((LOrder || ROrder) && LOrder != ROrder)
1595 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1596
1597 return BURRSort(left, right, SPQ);
1598}
1599
Evan Chengbdd062d2010-05-20 06:13:19 +00001600bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdebf9c52010-11-03 00:45:17 +00001601 if (left->isCall || right->isCall)
1602 // No way to compute latency of calls.
1603 return BURRSort(left, right, SPQ);
1604
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001605 bool LHigh = SPQ->HighRegPressure(left);
1606 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001607 // Avoid causing spills. If register pressure is high, schedule for
1608 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001609 if (LHigh && !RHigh)
1610 return true;
1611 else if (!LHigh && RHigh)
1612 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001613 else if (!LHigh && !RHigh) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001614 // If the two nodes share an operand and one of them has a single
1615 // use that is a live out copy, favor the one that is live out. Otherwise
1616 // it will be difficult to eliminate the copy if the instruction is a
1617 // loop induction variable update. e.g.
1618 // BB:
1619 // sub r1, r3, #1
1620 // str r0, [r2, r3]
1621 // mov r3, r1
1622 // cmp
1623 // bne BB
1624 bool SharePred = UnitsSharePred(left, right);
1625 // FIXME: Only adjust if BB is a loop back edge.
1626 // FIXME: What's the cost of a copy?
1627 int LBonus = (SharePred && hasOnlyLiveOutUses(left)) ? 1 : 0;
1628 int RBonus = (SharePred && hasOnlyLiveOutUses(right)) ? 1 : 0;
1629 int LHeight = (int)left->getHeight() - LBonus;
1630 int RHeight = (int)right->getHeight() - RBonus;
1631
Evan Cheng28590382010-07-21 23:53:58 +00001632 // Low register pressure situation, schedule for latency if possible.
1633 bool LStall = left->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001634 (int)SPQ->getCurCycle() < LHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001635 bool RStall = right->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001636 (int)SPQ->getCurCycle() < RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001637 // If scheduling one of the node will cause a pipeline stall, delay it.
1638 // If scheduling either one of the node will cause a pipeline stall, sort
1639 // them according to their height.
Evan Cheng28590382010-07-21 23:53:58 +00001640 if (LStall) {
1641 if (!RStall)
1642 return true;
Evan Cheng6c1414f2010-10-29 18:09:28 +00001643 if (LHeight != RHeight)
1644 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001645 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001646 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001647
Evan Cheng6c1414f2010-10-29 18:09:28 +00001648 // If either node is scheduling for latency, sort them by height
1649 // and latency.
Evan Cheng28590382010-07-21 23:53:58 +00001650 if (left->SchedulingPref == Sched::Latency ||
1651 right->SchedulingPref == Sched::Latency) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001652 if (LHeight != RHeight)
1653 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001654 if (left->Latency != right->Latency)
1655 return left->Latency > right->Latency;
1656 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001657 }
1658
Evan Chengbdd062d2010-05-20 06:13:19 +00001659 return BURRSort(left, right, SPQ);
1660}
1661
Evan Cheng37b740c2010-07-24 00:39:05 +00001662bool ilp_ls_rr_sort::operator()(const SUnit *left,
1663 const SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001664 if (left->isCall || right->isCall)
1665 // No way to compute latency of calls.
1666 return BURRSort(left, right, SPQ);
1667
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001668 bool LHigh = SPQ->HighRegPressure(left);
1669 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001670 // Avoid causing spills. If register pressure is high, schedule for
1671 // register pressure reduction.
1672 if (LHigh && !RHigh)
1673 return true;
1674 else if (!LHigh && RHigh)
1675 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001676 else if (!LHigh && !RHigh) {
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001677 // Low register pressure situation, schedule to maximize instruction level
1678 // parallelism.
Evan Cheng37b740c2010-07-24 00:39:05 +00001679 if (left->NumPreds > right->NumPreds)
1680 return false;
1681 else if (left->NumPreds < right->NumPreds)
1682 return false;
1683 }
1684
1685 return BURRSort(left, right, SPQ);
1686}
1687
Dan Gohman3f656df2008-11-20 02:45:51 +00001688template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001689bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001690RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001691 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001692 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001693 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001694 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001695 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001696 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001697 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001698 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001699 if (DU->getNodeId() != -1 &&
1700 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001701 return true;
1702 }
1703 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001704 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001705 return false;
1706}
1707
Evan Chengf9891412007-12-20 09:25:31 +00001708/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001709/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001710static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001711 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001712 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001713 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001714 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1715 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001716 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001717 for (const SDNode *SUNode = SU->getNode(); SUNode;
1718 SUNode = SUNode->getFlaggedNode()) {
1719 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001720 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001721 const unsigned *SUImpDefs =
1722 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1723 if (!SUImpDefs)
1724 return false;
1725 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001726 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001727 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001728 continue;
1729 if (!N->hasAnyUseOfValue(i))
1730 continue;
1731 unsigned Reg = ImpDefs[i - NumDefs];
1732 for (;*SUImpDefs; ++SUImpDefs) {
1733 unsigned SUReg = *SUImpDefs;
1734 if (TRI->regsOverlap(Reg, SUReg))
1735 return true;
1736 }
Evan Chengf9891412007-12-20 09:25:31 +00001737 }
1738 }
1739 return false;
1740}
1741
Dan Gohman9a658d72009-03-24 00:49:12 +00001742/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1743/// are not handled well by the general register pressure reduction
1744/// heuristics. When presented with code like this:
1745///
1746/// N
1747/// / |
1748/// / |
1749/// U store
1750/// |
1751/// ...
1752///
1753/// the heuristics tend to push the store up, but since the
1754/// operand of the store has another use (U), this would increase
1755/// the length of that other use (the U->N edge).
1756///
1757/// This function transforms code like the above to route U's
1758/// dependence through the store when possible, like this:
1759///
1760/// N
1761/// ||
1762/// ||
1763/// store
1764/// |
1765/// U
1766/// |
1767/// ...
1768///
1769/// This results in the store being scheduled immediately
1770/// after N, which shortens the U->N live range, reducing
1771/// register pressure.
1772///
1773template<class SF>
1774void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1775 // Visit all the nodes in topological order, working top-down.
1776 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1777 SUnit *SU = &(*SUnits)[i];
1778 // For now, only look at nodes with no data successors, such as stores.
1779 // These are especially important, due to the heuristics in
1780 // getNodePriority for nodes with no data successors.
1781 if (SU->NumSuccs != 0)
1782 continue;
1783 // For now, only look at nodes with exactly one data predecessor.
1784 if (SU->NumPreds != 1)
1785 continue;
1786 // Avoid prescheduling copies to virtual registers, which don't behave
1787 // like other nodes from the perspective of scheduling heuristics.
1788 if (SDNode *N = SU->getNode())
1789 if (N->getOpcode() == ISD::CopyToReg &&
1790 TargetRegisterInfo::isVirtualRegister
1791 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1792 continue;
1793
1794 // Locate the single data predecessor.
1795 SUnit *PredSU = 0;
1796 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1797 EE = SU->Preds.end(); II != EE; ++II)
1798 if (!II->isCtrl()) {
1799 PredSU = II->getSUnit();
1800 break;
1801 }
1802 assert(PredSU);
1803
1804 // Don't rewrite edges that carry physregs, because that requires additional
1805 // support infrastructure.
1806 if (PredSU->hasPhysRegDefs)
1807 continue;
1808 // Short-circuit the case where SU is PredSU's only data successor.
1809 if (PredSU->NumSuccs == 1)
1810 continue;
1811 // Avoid prescheduling to copies from virtual registers, which don't behave
1812 // like other nodes from the perspective of scheduling // heuristics.
1813 if (SDNode *N = SU->getNode())
1814 if (N->getOpcode() == ISD::CopyFromReg &&
1815 TargetRegisterInfo::isVirtualRegister
1816 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1817 continue;
1818
1819 // Perform checks on the successors of PredSU.
1820 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1821 EE = PredSU->Succs.end(); II != EE; ++II) {
1822 SUnit *PredSuccSU = II->getSUnit();
1823 if (PredSuccSU == SU) continue;
1824 // If PredSU has another successor with no data successors, for
1825 // now don't attempt to choose either over the other.
1826 if (PredSuccSU->NumSuccs == 0)
1827 goto outer_loop_continue;
1828 // Don't break physical register dependencies.
1829 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1830 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1831 goto outer_loop_continue;
1832 // Don't introduce graph cycles.
1833 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1834 goto outer_loop_continue;
1835 }
1836
1837 // Ok, the transformation is safe and the heuristics suggest it is
1838 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001839 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1840 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001841 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001842 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1843 SDep Edge = PredSU->Succs[i];
1844 assert(!Edge.isAssignedRegDep());
1845 SUnit *SuccSU = Edge.getSUnit();
1846 if (SuccSU != SU) {
1847 Edge.setSUnit(PredSU);
1848 scheduleDAG->RemovePred(SuccSU, Edge);
1849 scheduleDAG->AddPred(SU, Edge);
1850 Edge.setSUnit(SU);
1851 scheduleDAG->AddPred(SuccSU, Edge);
1852 --i;
1853 }
1854 }
1855 outer_loop_continue:;
1856 }
1857}
1858
Evan Chengd38c22b2006-05-11 23:55:42 +00001859/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1860/// it as a def&use operand. Add a pseudo control edge from it to the other
1861/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001862/// first (lower in the schedule). If both nodes are two-address, favor the
1863/// one that has a CopyToReg use (more likely to be a loop induction update).
1864/// If both are two-address, but one is commutable while the other is not
1865/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001866template<class SF>
1867void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001868 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001869 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001870 if (!SU->isTwoAddress)
1871 continue;
1872
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001873 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001874 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001875 continue;
1876
Evan Cheng6c1414f2010-10-29 18:09:28 +00001877 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00001878 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001879 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001880 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001881 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001882 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001883 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1884 continue;
1885 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1886 if (DU->getNodeId() == -1)
1887 continue;
1888 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1889 if (!DUSU) continue;
1890 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1891 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001892 if (I->isCtrl()) continue;
1893 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001894 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001895 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001896 // Be conservative. Ignore if nodes aren't at roughly the same
1897 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001898 if (SuccSU->getHeight() < SU->getHeight() &&
1899 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001900 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001901 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1902 // constrains whatever is using the copy, instead of the copy
1903 // itself. In the case that the copy is coalesced, this
1904 // preserves the intent of the pseudo two-address heurietics.
1905 while (SuccSU->Succs.size() == 1 &&
1906 SuccSU->getNode()->isMachineOpcode() &&
1907 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001908 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001909 SuccSU = SuccSU->Succs.front().getSUnit();
1910 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001911 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1912 continue;
1913 // Don't constrain nodes with physical register defs if the
1914 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001915 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001916 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001917 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001918 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001919 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1920 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001921 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001922 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1923 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1924 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001925 continue;
1926 if ((!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00001927 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00001928 (!SU->isCommutable && SuccSU->isCommutable)) &&
1929 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001930 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001931 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001932 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001933 /*Reg=*/0, /*isNormalMemory=*/false,
1934 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001935 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001936 }
1937 }
1938 }
1939 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001940}
1941
Evan Cheng6730f032007-01-08 23:55:53 +00001942/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1943/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001944template<class SF>
1945void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001946 SethiUllmanNumbers.assign(SUnits->size(), 0);
1947
1948 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001949 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001950}
Evan Chengd38c22b2006-05-11 23:55:42 +00001951
Roman Levenstein30d09512008-03-27 09:44:37 +00001952/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001953/// predecessors of the successors of the SUnit SU. Stop when the provided
1954/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001955static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1956 unsigned Limit) {
1957 unsigned Sum = 0;
1958 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1959 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001960 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001961 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1962 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001963 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001964 if (!PredSU->isScheduled)
1965 if (++Sum > Limit)
1966 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001967 }
1968 }
1969 return Sum;
1970}
1971
Evan Chengd38c22b2006-05-11 23:55:42 +00001972
1973// Top down
1974bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001975 unsigned LPriority = SPQ->getNodePriority(left);
1976 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001977 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1978 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001979 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1980 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001981 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1982 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001983
1984 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1985 return false;
1986 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1987 return true;
1988
Evan Chengd38c22b2006-05-11 23:55:42 +00001989 if (LIsFloater)
1990 LBonus -= 2;
1991 if (RIsFloater)
1992 RBonus -= 2;
1993 if (left->NumSuccs == 1)
1994 LBonus += 2;
1995 if (right->NumSuccs == 1)
1996 RBonus += 2;
1997
Evan Cheng73bdf042008-03-01 00:39:47 +00001998 if (LPriority+LBonus != RPriority+RBonus)
1999 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00002000
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002001 if (left->getDepth() != right->getDepth())
2002 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00002003
2004 if (left->NumSuccsLeft != right->NumSuccsLeft)
2005 return left->NumSuccsLeft > right->NumSuccsLeft;
2006
Roman Levenstein6b371142008-04-29 09:07:59 +00002007 assert(left->NodeQueueId && right->NodeQueueId &&
2008 "NodeQueueId cannot be zero");
2009 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002010}
2011
Evan Chengd38c22b2006-05-11 23:55:42 +00002012//===----------------------------------------------------------------------===//
2013// Public Constructor Functions
2014//===----------------------------------------------------------------------===//
2015
Dan Gohmandfaf6462009-02-11 04:27:20 +00002016llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002017llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002018 const TargetMachine &TM = IS->TM;
2019 const TargetInstrInfo *TII = TM.getInstrInfo();
2020 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00002021
Evan Chenga77f3d32010-07-21 06:09:07 +00002022 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002023 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002024 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002025 PQ->setScheduleDAG(SD);
2026 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002027}
2028
Dan Gohmandfaf6462009-02-11 04:27:20 +00002029llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002030llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002031 const TargetMachine &TM = IS->TM;
2032 const TargetInstrInfo *TII = TM.getInstrInfo();
2033 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00002034
Evan Chenga77f3d32010-07-21 06:09:07 +00002035 TDRegReductionPriorityQueue *PQ =
2036 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002037 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00002038 PQ->setScheduleDAG(SD);
2039 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002040}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002041
2042llvm::ScheduleDAGSDNodes *
2043llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2044 const TargetMachine &TM = IS->TM;
2045 const TargetInstrInfo *TII = TM.getInstrInfo();
2046 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2047
Evan Chenga77f3d32010-07-21 06:09:07 +00002048 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002049 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002050 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
2051 PQ->setScheduleDAG(SD);
2052 return SD;
2053}
2054
2055llvm::ScheduleDAGSDNodes *
2056llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2057 const TargetMachine &TM = IS->TM;
2058 const TargetInstrInfo *TII = TM.getInstrInfo();
2059 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002060 const TargetLowering *TLI = &IS->getTargetLowering();
Evan Chengbdd062d2010-05-20 06:13:19 +00002061
Evan Chenga77f3d32010-07-21 06:09:07 +00002062 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002063 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002064 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002065 PQ->setScheduleDAG(SD);
2066 return SD;
2067}
Evan Cheng37b740c2010-07-24 00:39:05 +00002068
2069llvm::ScheduleDAGSDNodes *
2070llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2071 const TargetMachine &TM = IS->TM;
2072 const TargetInstrInfo *TII = TM.getInstrInfo();
2073 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2074 const TargetLowering *TLI = &IS->getTargetLowering();
2075
2076 ILPBURRPriorityQueue *PQ =
2077 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2078 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2079 PQ->setScheduleDAG(SD);
2080 return SD;
2081}