blob: ca723bea8873e403757414837686800533d93d51 [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 Cheng5924bf72007-09-25 01:54:36 +000027#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000028#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000029#include "llvm/ADT/STLExtras.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000032#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000033#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000034using namespace llvm;
35
Dan Gohmanfd227e92008-03-25 17:10:29 +000036STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000037STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000038STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000039STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000040
Jim Laskey95eda5b2006-08-01 14:21:23 +000041static RegisterScheduler
42 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000043 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000044 createBURRListDAGScheduler);
45static RegisterScheduler
46 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000047 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000048 createTDRRListDAGScheduler);
Bill Wendling8cbc25d2010-01-23 10:26:57 +000049static RegisterScheduler
50 sourceListDAGScheduler("source",
51 "Similar to list-burr but schedules in source "
52 "order when possible",
53 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000054
Evan Chengbdd062d2010-05-20 06:13:19 +000055static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000056 hybridListDAGScheduler("list-hybrid",
Evan Chengbdd062d2010-05-20 06:13:19 +000057 "Bottom-up rr list scheduling which avoid stalls for "
58 "long latency instructions",
59 createHybridListDAGScheduler);
60
Evan Chengd38c22b2006-05-11 23:55:42 +000061namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000062//===----------------------------------------------------------------------===//
63/// ScheduleDAGRRList - The actual register reduction list scheduler
64/// implementation. This supports both top-down and bottom-up scheduling.
65///
Nick Lewycky02d5f772009-10-25 06:33:48 +000066class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000067private:
68 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
69 /// it is top-down.
70 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000071
Evan Chengbdd062d2010-05-20 06:13:19 +000072 /// NeedLatency - True if the scheduler will make use of latency information.
73 ///
74 bool NeedLatency;
75
Evan Chengd38c22b2006-05-11 23:55:42 +000076 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000077 SchedulingPriorityQueue *AvailableQueue;
78
Dan Gohmanc07f6862008-09-23 18:50:48 +000079 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000080 /// that are "live". These nodes must be scheduled before any other nodes that
81 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000082 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000083 std::vector<SUnit*> LiveRegDefs;
84 std::vector<unsigned> LiveRegCycles;
85
Dan Gohmanad2134d2008-11-25 00:52:40 +000086 /// Topo - A topological ordering for SUnits which permits fast IsReachable
87 /// and similar queries.
88 ScheduleDAGTopologicalSort Topo;
89
Evan Chengd38c22b2006-05-11 23:55:42 +000090public:
Dan Gohman619ef482009-01-15 19:20:50 +000091 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000092 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +000093 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +000094 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +000095 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000096 }
97
98 ~ScheduleDAGRRList() {
99 delete AvailableQueue;
100 }
101
102 void Schedule();
103
Roman Levenstein733a4d62008-03-26 11:23:38 +0000104 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000105 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
106 return Topo.IsReachable(SU, TargetSU);
107 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000108
Dan Gohman60d68442009-01-29 19:49:27 +0000109 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000110 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000111 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
112 return Topo.WillCreateCycle(SU, TargetSU);
113 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000114
Dan Gohman2d170892008-12-09 22:54:47 +0000115 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000116 /// This returns true if this is a new predecessor.
117 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000118 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000119 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000120 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000121 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000122
Dan Gohman2d170892008-12-09 22:54:47 +0000123 /// RemovePred - removes a predecessor edge from SUnit SU.
124 /// This returns true if an edge was removed.
125 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000126 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000127 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000128 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000129 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000130
Evan Chengd38c22b2006-05-11 23:55:42 +0000131private:
Dan Gohman60d68442009-01-29 19:49:27 +0000132 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000133 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000134 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000135 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000136 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000137 void ScheduleNodeBottomUp(SUnit*, unsigned);
138 void ScheduleNodeTopDown(SUnit*, unsigned);
139 void UnscheduleNodeBottomUp(SUnit*);
140 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
141 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000142 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
143 const TargetRegisterClass*,
144 const TargetRegisterClass*,
145 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000146 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000147 void ListScheduleTopDown();
148 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000149
150
151 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000152 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000153 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000154 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000155 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000156 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000157 if (NewNode->NodeNum >= NumSUnits)
158 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000159 return NewNode;
160 }
161
Roman Levenstein733a4d62008-03-26 11:23:38 +0000162 /// CreateClone - Creates a new SUnit from an existing one.
163 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000164 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000165 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000166 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000167 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000168 if (NewNode->NodeNum >= NumSUnits)
169 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000170 return NewNode;
171 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000172
Evan Chengbdd062d2010-05-20 06:13:19 +0000173 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
174 /// need actual latency information but the hybrid scheduler does.
175 bool ForceUnitLatencies() const {
176 return !NeedLatency;
177 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000178};
179} // end anonymous namespace
180
181
182/// Schedule - Schedule the DAG using list scheduling.
183void ScheduleDAGRRList::Schedule() {
David Greenef34d7ac2010-01-05 01:24:54 +0000184 DEBUG(dbgs() << "********** List Scheduling **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000185
Dan Gohmanc07f6862008-09-23 18:50:48 +0000186 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000187 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
188 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000189
Dan Gohman04543e72008-12-23 18:36:58 +0000190 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000191 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000192
Evan Chengd38c22b2006-05-11 23:55:42 +0000193 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000194 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000195 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000196
Dan Gohman46520a22008-06-21 19:18:17 +0000197 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000198
Evan Chengd38c22b2006-05-11 23:55:42 +0000199 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
200 if (isBottomUp)
201 ListScheduleBottomUp();
202 else
203 ListScheduleTopDown();
204
205 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000206}
Evan Chengd38c22b2006-05-11 23:55:42 +0000207
208//===----------------------------------------------------------------------===//
209// Bottom-Up Scheduling
210//===----------------------------------------------------------------------===//
211
Evan Chengd38c22b2006-05-11 23:55:42 +0000212/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000213/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000214void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000215 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000216
Evan Chengd38c22b2006-05-11 23:55:42 +0000217#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000218 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000219 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000220 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000221 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000222 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000223 }
224#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000225 --PredSU->NumSuccsLeft;
226
Evan Chengbdd062d2010-05-20 06:13:19 +0000227 if (!ForceUnitLatencies()) {
228 // Updating predecessor's height. This is now the cycle when the
229 // predecessor can be scheduled without causing a pipeline stall.
230 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
231 }
232
Dan Gohmanb9543432009-02-10 23:27:53 +0000233 // If all the node's successors are scheduled, this node is ready
234 // to be scheduled. Ignore the special EntrySU node.
235 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000236 PredSU->isAvailable = true;
237 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000238 }
239}
240
Dan Gohmanb9543432009-02-10 23:27:53 +0000241void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000242 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000243 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000244 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000245 ReleasePred(SU, &*I);
246 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000247 // This is a physical register dependency and it's impossible or
248 // expensive to copy the register. Make sure nothing that can
249 // clobber the register is scheduled between the predecessor and
250 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000251 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000252 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000253 LiveRegDefs[I->getReg()] = I->getSUnit();
254 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000255 }
256 }
257 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000258}
259
260/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
261/// count of its predecessors. If a predecessor pending count is zero, add it to
262/// the Available queue.
263void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000264 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000265 DEBUG(SU->dump(this));
266
Evan Chengbdd062d2010-05-20 06:13:19 +0000267#ifndef NDEBUG
268 if (CurCycle < SU->getHeight())
269 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
270#endif
271
272 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000273 SU->setHeightToAtLeast(CurCycle);
274 Sequence.push_back(SU);
275
276 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000277
278 // Release all the implicit physical register defs that are live.
279 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
280 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000281 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000282 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000283 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000284 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000285 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000286 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000287 LiveRegDefs[I->getReg()] = NULL;
288 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000289 }
290 }
291 }
292
Evan Chengd38c22b2006-05-11 23:55:42 +0000293 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000294 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000295}
296
Evan Cheng5924bf72007-09-25 01:54:36 +0000297/// CapturePred - This does the opposite of ReleasePred. Since SU is being
298/// unscheduled, incrcease the succ left count of its predecessors. Remove
299/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000300void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
301 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000302 if (PredSU->isAvailable) {
303 PredSU->isAvailable = false;
304 if (!PredSU->isPending)
305 AvailableQueue->remove(PredSU);
306 }
307
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000308 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000309 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000310}
311
312/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
313/// its predecessor states to reflect the change.
314void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000315 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000316 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000317
318 AvailableQueue->UnscheduledNode(SU);
319
320 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
321 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000322 CapturePred(&*I);
Evan Chengcc2efe12010-05-28 23:26:21 +0000323 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000324 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000325 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000326 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000327 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000328 LiveRegDefs[I->getReg()] = NULL;
329 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000330 }
331 }
332
333 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
334 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000335 if (I->isAssignedRegDep()) {
336 if (!LiveRegDefs[I->getReg()]) {
337 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000338 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000339 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000340 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
341 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000342 }
343 }
344
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000345 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000346 SU->isScheduled = false;
347 SU->isAvailable = true;
348 AvailableQueue->push(SU);
349}
350
Evan Cheng8e136a92007-09-26 21:36:17 +0000351/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000352/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000353void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
354 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000355 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000356 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000357 OldSU = Sequence.back();
358 Sequence.pop_back();
359 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000360 // Don't try to remove SU from AvailableQueue.
361 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000362 UnscheduleNodeBottomUp(OldSU);
363 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000364 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000365 }
366
Dan Gohman60d68442009-01-29 19:49:27 +0000367 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000368
369 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000370}
371
Evan Cheng3b245872010-02-05 01:27:11 +0000372static bool isOperandOf(const SUnit *SU, SDNode *N) {
373 for (const SDNode *SUNode = SU->getNode(); SUNode;
374 SUNode = SUNode->getFlaggedNode()) {
375 if (SUNode->isOperandOf(N))
376 return true;
377 }
378 return false;
379}
380
Evan Cheng5924bf72007-09-25 01:54:36 +0000381/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
382/// successors to the newly created node.
383SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000384 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000385 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000386
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000387 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000388 if (!N)
389 return NULL;
390
391 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000392 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000393 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000394 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000395 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000396 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000397 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000398 TryUnfold = true;
399 }
Evan Cheng79e97132007-10-05 01:39:18 +0000400 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000401 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000402 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000403 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000404 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000405 }
406
407 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000408 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000409 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000410 return NULL;
411
Evan Chengbdd062d2010-05-20 06:13:19 +0000412 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000413 assert(NewNodes.size() == 2 && "Expected a load folding node!");
414
415 N = NewNodes[1];
416 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000417 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000418 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000419 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000420 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
421 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000422 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000423
Dan Gohmane52e0892008-11-11 21:34:44 +0000424 // LoadNode may already exist. This can happen when there is another
425 // load from the same location and producing the same type of value
426 // but it has different alignment or volatileness.
427 bool isNewLoad = true;
428 SUnit *LoadSU;
429 if (LoadNode->getNodeId() != -1) {
430 LoadSU = &SUnits[LoadNode->getNodeId()];
431 isNewLoad = false;
432 } else {
433 LoadSU = CreateNewSUnit(LoadNode);
434 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000435 ComputeLatency(LoadSU);
436 }
437
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000438 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000439 assert(N->getNodeId() == -1 && "Node already inserted!");
440 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000441
Dan Gohman17059682008-07-17 19:10:17 +0000442 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000443 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000444 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000445 NewSU->isTwoAddress = true;
446 break;
447 }
448 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000449 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000450 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000451 ComputeLatency(NewSU);
452
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000453 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000454 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000455 SmallVector<SDep, 4> ChainSuccs;
456 SmallVector<SDep, 4> LoadPreds;
457 SmallVector<SDep, 4> NodePreds;
458 SmallVector<SDep, 4> NodeSuccs;
459 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
460 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000461 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000462 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000463 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000464 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000465 else
Dan Gohman2d170892008-12-09 22:54:47 +0000466 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000467 }
468 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
469 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000470 if (I->isCtrl())
471 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000472 else
Dan Gohman2d170892008-12-09 22:54:47 +0000473 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000474 }
475
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000476 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000477 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
478 const SDep &Pred = ChainPreds[i];
479 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000480 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000481 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000482 }
Evan Cheng79e97132007-10-05 01:39:18 +0000483 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000484 const SDep &Pred = LoadPreds[i];
485 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000486 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000487 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000488 }
489 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000490 const SDep &Pred = NodePreds[i];
491 RemovePred(SU, Pred);
492 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000493 }
494 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000495 SDep D = NodeSuccs[i];
496 SUnit *SuccDep = D.getSUnit();
497 D.setSUnit(SU);
498 RemovePred(SuccDep, D);
499 D.setSUnit(NewSU);
500 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000501 }
502 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000503 SDep D = ChainSuccs[i];
504 SUnit *SuccDep = D.getSUnit();
505 D.setSUnit(SU);
506 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000507 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000508 D.setSUnit(LoadSU);
509 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000510 }
Evan Cheng79e97132007-10-05 01:39:18 +0000511 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000512
513 // Add a data dependency to reflect that NewSU reads the value defined
514 // by LoadSU.
515 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000516
Evan Cheng91e0fc92007-12-18 08:42:10 +0000517 if (isNewLoad)
518 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000519 AvailableQueue->addNode(NewSU);
520
521 ++NumUnfolds;
522
523 if (NewSU->NumSuccsLeft == 0) {
524 NewSU->isAvailable = true;
525 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000526 }
527 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000528 }
529
Evan Chengbdd062d2010-05-20 06:13:19 +0000530 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000531 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000532
533 // New SUnit has the exact same predecessors.
534 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
535 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000536 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000537 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000538
539 // Only copy scheduled successors. Cut them from old node's successor
540 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000541 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000542 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
543 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000544 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000545 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000546 SUnit *SuccSU = I->getSUnit();
547 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000548 SDep D = *I;
549 D.setSUnit(NewSU);
550 AddPred(SuccSU, D);
551 D.setSUnit(SU);
552 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000553 }
554 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000555 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000556 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000557
558 AvailableQueue->updateNode(SU);
559 AvailableQueue->addNode(NewSU);
560
Evan Cheng1ec79b42007-09-27 07:09:03 +0000561 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000562 return NewSU;
563}
564
Evan Chengb2c42c62009-01-12 03:19:55 +0000565/// InsertCopiesAndMoveSuccs - Insert register copies and move all
566/// scheduled successors of the given SUnit to the last copy.
567void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
568 const TargetRegisterClass *DestRC,
569 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000570 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000571 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000572 CopyFromSU->CopySrcRC = SrcRC;
573 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000574
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000575 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000576 CopyToSU->CopySrcRC = DestRC;
577 CopyToSU->CopyDstRC = SrcRC;
578
579 // Only copy scheduled successors. Cut them from old node's successor
580 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000581 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000582 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
583 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000584 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000585 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000586 SUnit *SuccSU = I->getSUnit();
587 if (SuccSU->isScheduled) {
588 SDep D = *I;
589 D.setSUnit(CopyToSU);
590 AddPred(SuccSU, D);
591 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000592 }
593 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000594 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000595 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000596
Dan Gohman2d170892008-12-09 22:54:47 +0000597 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
598 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000599
600 AvailableQueue->updateNode(SU);
601 AvailableQueue->addNode(CopyFromSU);
602 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000603 Copies.push_back(CopyFromSU);
604 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000605
Evan Chengb2c42c62009-01-12 03:19:55 +0000606 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000607}
608
609/// getPhysicalRegisterVT - Returns the ValueType of the physical register
610/// definition of the specified node.
611/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000612static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000613 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000614 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000615 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000616 unsigned NumRes = TID.getNumDefs();
617 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000618 if (Reg == *ImpDef)
619 break;
620 ++NumRes;
621 }
622 return N->getValueType(NumRes);
623}
624
Evan Chengb8905c42009-03-04 01:41:49 +0000625/// CheckForLiveRegDef - Return true and update live register vector if the
626/// specified register def of the specified SUnit clobbers any "live" registers.
627static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
628 std::vector<SUnit*> &LiveRegDefs,
629 SmallSet<unsigned, 4> &RegAdded,
630 SmallVector<unsigned, 4> &LRegs,
631 const TargetRegisterInfo *TRI) {
632 bool Added = false;
633 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
634 if (RegAdded.insert(Reg)) {
635 LRegs.push_back(Reg);
636 Added = true;
637 }
638 }
639 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
640 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
641 if (RegAdded.insert(*Alias)) {
642 LRegs.push_back(*Alias);
643 Added = true;
644 }
645 }
646 return Added;
647}
648
Evan Cheng5924bf72007-09-25 01:54:36 +0000649/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
650/// scheduling of the given node to satisfy live physical register dependencies.
651/// If the specific node is the last one that's available to schedule, do
652/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000653bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
654 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000655 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000656 return false;
657
Evan Chenge6f92252007-09-27 18:46:06 +0000658 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000659 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000660 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
661 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000662 if (I->isAssignedRegDep())
663 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
664 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000665 }
666
Dan Gohman072734e2008-11-13 23:24:17 +0000667 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000668 if (Node->getOpcode() == ISD::INLINEASM) {
669 // Inline asm can clobber physical defs.
670 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000671 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000672 --NumOps; // Ignore the flag operand.
673
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000674 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000675 unsigned Flags =
676 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000677 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000678
679 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000680 if (InlineAsm::isRegDefKind(Flags) ||
681 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000682 // Check for def of register or earlyclobber register.
683 for (; NumVals; --NumVals, ++i) {
684 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
685 if (TargetRegisterInfo::isPhysicalRegister(Reg))
686 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
687 }
688 } else
689 i += NumVals;
690 }
691 continue;
692 }
693
Dan Gohman072734e2008-11-13 23:24:17 +0000694 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000695 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000696 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000697 if (!TID.ImplicitDefs)
698 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000699 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
700 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000701 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000702 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000703}
704
Evan Cheng1ec79b42007-09-27 07:09:03 +0000705
Evan Chengd38c22b2006-05-11 23:55:42 +0000706/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
707/// schedulers.
708void ScheduleDAGRRList::ListScheduleBottomUp() {
709 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000710
711 // Release any predecessors of the special Exit node.
712 ReleasePredecessors(&ExitSU, CurCycle);
713
Evan Chengd38c22b2006-05-11 23:55:42 +0000714 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000715 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000716 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000717 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
718 RootSU->isAvailable = true;
719 AvailableQueue->push(RootSU);
720 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000721
722 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000723 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000724 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000725 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000726 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000727 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000728 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000729 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000730 SUnit *CurSU = AvailableQueue->pop();
731 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000732 SmallVector<unsigned, 4> LRegs;
733 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
734 break;
735 Delayed = true;
736 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000737
738 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
739 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000740 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000741 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000742
743 // All candidates are delayed due to live physical reg dependencies.
744 // Try backtracking, code duplication, or inserting cross class copies
745 // to resolve it.
746 if (Delayed && !CurSU) {
747 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
748 SUnit *TrySU = NotReady[i];
749 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
750
751 // Try unscheduling up to the point where it's safe to schedule
752 // this node.
753 unsigned LiveCycle = CurCycle;
754 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
755 unsigned Reg = LRegs[j];
756 unsigned LCycle = LiveRegCycles[Reg];
757 LiveCycle = std::min(LiveCycle, LCycle);
758 }
759 SUnit *OldSU = Sequence[LiveCycle];
760 if (!WillCreateCycle(TrySU, OldSU)) {
761 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
762 // Force the current node to be scheduled before the node that
763 // requires the physical reg dep.
764 if (OldSU->isAvailable) {
765 OldSU->isAvailable = false;
766 AvailableQueue->remove(OldSU);
767 }
Dan Gohman2d170892008-12-09 22:54:47 +0000768 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
769 /*Reg=*/0, /*isNormalMemory=*/false,
770 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000771 // If one or more successors has been unscheduled, then the current
772 // node is no longer avaialable. Schedule a successor that's now
773 // available instead.
774 if (!TrySU->isAvailable)
775 CurSU = AvailableQueue->pop();
776 else {
777 CurSU = TrySU;
778 TrySU->isPending = false;
779 NotReady.erase(NotReady.begin()+i);
780 }
781 break;
782 }
783 }
784
785 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000786 // Can't backtrack. If it's too expensive to copy the value, then try
787 // duplicate the nodes that produces these "too expensive to copy"
788 // values to break the dependency. In case even that doesn't work,
789 // insert cross class copies.
790 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000791 SUnit *TrySU = NotReady[0];
792 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
793 assert(LRegs.size() == 1 && "Can't handle this yet!");
794 unsigned Reg = LRegs[0];
795 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000796 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000797 const TargetRegisterClass *RC =
798 TRI->getPhysicalRegisterRegClass(Reg, VT);
799 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
800
801 // If cross copy register class is null, then it must be possible copy
802 // the value directly. Do not try duplicate the def.
803 SUnit *NewDef = 0;
804 if (DestRC)
805 NewDef = CopyAndMoveSuccessors(LRDef);
806 else
807 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000808 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000809 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000810 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000811 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000812 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000813 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000814 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000815 /*Reg=*/0, /*isNormalMemory=*/false,
816 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000817 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000818 NewDef = Copies.back();
819 }
820
Evan Chengbdd062d2010-05-20 06:13:19 +0000821 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000822 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000823 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000824 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000825 /*Reg=*/0, /*isNormalMemory=*/false,
826 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000827 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000828 TrySU->isAvailable = false;
829 CurSU = NewDef;
830 }
831
Dan Gohman60d68442009-01-29 19:49:27 +0000832 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000833 }
834
Evan Chengd38c22b2006-05-11 23:55:42 +0000835 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000836 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
837 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000838 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000839 if (NotReady[i]->isAvailable)
840 AvailableQueue->push(NotReady[i]);
841 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000842 NotReady.clear();
843
Dan Gohmanc602dd42008-11-21 00:10:42 +0000844 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000845 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000846 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000847 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000848 }
849
Evan Chengd38c22b2006-05-11 23:55:42 +0000850 // Reverse the order if it is bottom up.
851 std::reverse(Sequence.begin(), Sequence.end());
852
Evan Chengd38c22b2006-05-11 23:55:42 +0000853#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000854 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000855#endif
856}
857
858//===----------------------------------------------------------------------===//
859// Top-Down Scheduling
860//===----------------------------------------------------------------------===//
861
862/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000863/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000864void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000865 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000866
Evan Chengd38c22b2006-05-11 23:55:42 +0000867#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000868 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000869 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000870 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000871 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000872 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000873 }
874#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000875 --SuccSU->NumPredsLeft;
876
Dan Gohmanb9543432009-02-10 23:27:53 +0000877 // If all the node's predecessors are scheduled, this node is ready
878 // to be scheduled. Ignore the special ExitSU node.
879 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000880 SuccSU->isAvailable = true;
881 AvailableQueue->push(SuccSU);
882 }
883}
884
Dan Gohmanb9543432009-02-10 23:27:53 +0000885void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
886 // Top down: release successors
887 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
888 I != E; ++I) {
889 assert(!I->isAssignedRegDep() &&
890 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
891
892 ReleaseSucc(SU, &*I);
893 }
894}
895
Evan Chengd38c22b2006-05-11 23:55:42 +0000896/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
897/// count of its successors. If a successor pending count is zero, add it to
898/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000899void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000900 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000901 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000902
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000903 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
904 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000905 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000906
Dan Gohmanb9543432009-02-10 23:27:53 +0000907 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000908 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000909 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000910}
911
Dan Gohman54a187e2007-08-20 19:28:38 +0000912/// ListScheduleTopDown - The main loop of list scheduling for top-down
913/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000914void ScheduleDAGRRList::ListScheduleTopDown() {
915 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000916 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000917
Dan Gohmanb9543432009-02-10 23:27:53 +0000918 // Release any successors of the special Entry node.
919 ReleaseSuccessors(&EntrySU);
920
Evan Chengd38c22b2006-05-11 23:55:42 +0000921 // All leaves to Available queue.
922 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
923 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000924 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000925 AvailableQueue->push(&SUnits[i]);
926 SUnits[i].isAvailable = true;
927 }
928 }
929
Evan Chengd38c22b2006-05-11 23:55:42 +0000930 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000931 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000932 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000933 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000934 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000935
Dan Gohmanc602dd42008-11-21 00:10:42 +0000936 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000937 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000938 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000939 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000940 }
941
Evan Chengd38c22b2006-05-11 23:55:42 +0000942#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000943 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000944#endif
945}
946
947
Evan Chengd38c22b2006-05-11 23:55:42 +0000948//===----------------------------------------------------------------------===//
949// RegReductionPriorityQueue Implementation
950//===----------------------------------------------------------------------===//
951//
952// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
953// to reduce register pressure.
954//
955namespace {
956 template<class SF>
957 class RegReductionPriorityQueue;
958
959 /// Sorting functions for the Available queue.
960 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
961 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
962 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
963 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
964
965 bool operator()(const SUnit* left, const SUnit* right) const;
966 };
967
968 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
969 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
970 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
971 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
972
973 bool operator()(const SUnit* left, const SUnit* right) const;
974 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000975
976 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
977 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
978 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
979 : SPQ(spq) {}
980 src_ls_rr_sort(const src_ls_rr_sort &RHS)
981 : SPQ(RHS.SPQ) {}
982
983 bool operator()(const SUnit* left, const SUnit* right) const;
984 };
Evan Chengbdd062d2010-05-20 06:13:19 +0000985
986 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
987 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
988 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
989 : SPQ(spq) {}
990 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
991 : SPQ(RHS.SPQ) {}
992
993 bool operator()(const SUnit* left, const SUnit* right) const;
994 };
Evan Chengd38c22b2006-05-11 23:55:42 +0000995} // end anonymous namespace
996
Dan Gohman186f65d2008-11-20 03:30:37 +0000997/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
998/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000999static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001000CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001001 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1002 if (SethiUllmanNumber != 0)
1003 return SethiUllmanNumber;
1004
1005 unsigned Extra = 0;
1006 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1007 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001008 if (I->isCtrl()) continue; // ignore chain preds
1009 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001010 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001011 if (PredSethiUllman > SethiUllmanNumber) {
1012 SethiUllmanNumber = PredSethiUllman;
1013 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001014 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001015 ++Extra;
1016 }
1017
1018 SethiUllmanNumber += Extra;
1019
1020 if (SethiUllmanNumber == 0)
1021 SethiUllmanNumber = 1;
1022
1023 return SethiUllmanNumber;
1024}
1025
Evan Chengd38c22b2006-05-11 23:55:42 +00001026namespace {
1027 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001028 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001029 std::vector<SUnit*> Queue;
1030 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001031 unsigned CurQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +00001032
Dan Gohman3f656df2008-11-20 02:45:51 +00001033 protected:
1034 // SUnits - The SUnits for the current graph.
1035 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +00001036
Dan Gohman3f656df2008-11-20 02:45:51 +00001037 const TargetInstrInfo *TII;
1038 const TargetRegisterInfo *TRI;
1039 ScheduleDAGRRList *scheduleDAG;
1040
Dan Gohman186f65d2008-11-20 03:30:37 +00001041 // SethiUllmanNumbers - The SethiUllman number for each node.
1042 std::vector<unsigned> SethiUllmanNumbers;
1043
Dan Gohman3f656df2008-11-20 02:45:51 +00001044 public:
1045 RegReductionPriorityQueue(const TargetInstrInfo *tii,
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001046 const TargetRegisterInfo *tri)
Dan Gohman52c27382010-05-26 18:52:00 +00001047 : Picker(this), CurQueueId(0),
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001048 TII(tii), TRI(tri), scheduleDAG(NULL) {}
Dan Gohman3f656df2008-11-20 02:45:51 +00001049
1050 void initNodes(std::vector<SUnit> &sunits) {
1051 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001052 // Add pseudo dependency edges for two-address nodes.
1053 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001054 // Reroute edges to nodes with multiple uses.
1055 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001056 // Calculate node priorities.
1057 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001058 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001059
Dan Gohman186f65d2008-11-20 03:30:37 +00001060 void addNode(const SUnit *SU) {
1061 unsigned SUSize = SethiUllmanNumbers.size();
1062 if (SUnits->size() > SUSize)
1063 SethiUllmanNumbers.resize(SUSize*2, 0);
1064 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1065 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001066
Dan Gohman186f65d2008-11-20 03:30:37 +00001067 void updateNode(const SUnit *SU) {
1068 SethiUllmanNumbers[SU->NodeNum] = 0;
1069 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1070 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001071
Dan Gohman186f65d2008-11-20 03:30:37 +00001072 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001073 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001074 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001075 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001076
1077 unsigned getNodePriority(const SUnit *SU) const {
1078 assert(SU->NodeNum < SethiUllmanNumbers.size());
1079 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001080 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001081 // CopyToReg should be close to its uses to facilitate coalescing and
1082 // avoid spilling.
1083 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001084 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1085 Opc == TargetOpcode::SUBREG_TO_REG ||
1086 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001087 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1088 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001089 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001090 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1091 // If SU does not have a register use, i.e. it doesn't produce a value
1092 // that would be consumed (e.g. store), then it terminates a chain of
1093 // computation. Give it a large SethiUllman number so it will be
1094 // scheduled right before its predecessors that it doesn't lengthen
1095 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001096 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001097 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1098 // If SU does not have a register def, schedule it close to its uses
1099 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001100 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001101 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001102 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001103
1104 unsigned getNodeOrdering(const SUnit *SU) const {
1105 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1106 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001107
Evan Chengd38c22b2006-05-11 23:55:42 +00001108 bool empty() const { return Queue.empty(); }
1109
1110 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001111 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001112 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001113 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001114 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001115
Evan Chengd38c22b2006-05-11 23:55:42 +00001116 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001117 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001118 std::vector<SUnit *>::iterator Best = Queue.begin();
1119 for (std::vector<SUnit *>::iterator I = next(Queue.begin()),
1120 E = Queue.end(); I != E; ++I)
1121 if (Picker(*Best, *I))
1122 Best = I;
1123 SUnit *V = *Best;
1124 if (Best != prior(Queue.end()))
1125 std::swap(*Best, Queue.back());
1126 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001127 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001128 return V;
1129 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001130
Evan Cheng5924bf72007-09-25 01:54:36 +00001131 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001132 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001133 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001134 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1135 SU);
1136 if (I != prior(Queue.end()))
1137 std::swap(*I, Queue.back());
1138 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001139 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001140 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001141
1142 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1143 scheduleDAG = scheduleDag;
1144 }
1145
1146 protected:
1147 bool canClobber(const SUnit *SU, const SUnit *Op);
1148 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001149 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001150 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001151 };
1152
Dan Gohman186f65d2008-11-20 03:30:37 +00001153 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1154 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001155
Dan Gohman186f65d2008-11-20 03:30:37 +00001156 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1157 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001158
1159 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1160 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001161
1162 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1163 HybridBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001164}
1165
Evan Chengb9e3db62007-03-14 22:43:40 +00001166/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001167/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001168static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001169 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001170 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001171 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001172 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001173 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001174 // If there are bunch of CopyToRegs stacked up, they should be considered
1175 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001176 if (I->getSUnit()->getNode() &&
1177 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001178 Height = closestSucc(I->getSUnit())+1;
1179 if (Height > MaxHeight)
1180 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001181 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001182 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001183}
1184
Evan Cheng61bc51e2007-12-20 02:22:36 +00001185/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001186/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001187static unsigned calcMaxScratches(const SUnit *SU) {
1188 unsigned Scratches = 0;
1189 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001190 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001191 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001192 Scratches++;
1193 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001194 return Scratches;
1195}
1196
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001197template <typename RRSort>
1198static bool BURRSort(const SUnit *left, const SUnit *right,
1199 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001200 unsigned LPriority = SPQ->getNodePriority(left);
1201 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001202 if (LPriority != RPriority)
1203 return LPriority > RPriority;
1204
1205 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1206 // e.g.
1207 // t1 = op t2, c1
1208 // t3 = op t4, c2
1209 //
1210 // and the following instructions are both ready.
1211 // t2 = op c3
1212 // t4 = op c4
1213 //
1214 // Then schedule t2 = op first.
1215 // i.e.
1216 // t4 = op c4
1217 // t2 = op c3
1218 // t1 = op t2, c1
1219 // t3 = op t4, c2
1220 //
1221 // This creates more short live intervals.
1222 unsigned LDist = closestSucc(left);
1223 unsigned RDist = closestSucc(right);
1224 if (LDist != RDist)
1225 return LDist < RDist;
1226
Evan Cheng3a14efa2009-02-12 08:59:45 +00001227 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001228 unsigned LScratch = calcMaxScratches(left);
1229 unsigned RScratch = calcMaxScratches(right);
1230 if (LScratch != RScratch)
1231 return LScratch > RScratch;
1232
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001233 if (left->getHeight() != right->getHeight())
1234 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001235
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001236 if (left->getDepth() != right->getDepth())
1237 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001238
Roman Levenstein6b371142008-04-29 09:07:59 +00001239 assert(left->NodeQueueId && right->NodeQueueId &&
1240 "NodeQueueId cannot be zero");
1241 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001242}
1243
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001244// Bottom up
1245bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1246 return BURRSort(left, right, SPQ);
1247}
1248
1249// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001250bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001251 unsigned LOrder = SPQ->getNodeOrdering(left);
1252 unsigned ROrder = SPQ->getNodeOrdering(right);
1253
1254 // Prefer an ordering where the lower the non-zero order number, the higher
1255 // the preference.
1256 if ((LOrder || ROrder) && LOrder != ROrder)
1257 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1258
1259 return BURRSort(left, right, SPQ);
1260}
1261
Evan Chengbdd062d2010-05-20 06:13:19 +00001262bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Cheng4401f882010-05-20 23:26:43 +00001263 bool LStall = left->SchedulingPref == Sched::Latency &&
1264 SPQ->getCurCycle() < left->getHeight();
1265 bool RStall = right->SchedulingPref == Sched::Latency &&
1266 SPQ->getCurCycle() < right->getHeight();
Evan Chengbdd062d2010-05-20 06:13:19 +00001267 // If scheduling one of the node will cause a pipeline stall, delay it.
1268 // If scheduling either one of the node will cause a pipeline stall, sort them
1269 // according to their height.
1270 // If neither will cause a pipeline stall, try to reduce register pressure.
1271 if (LStall) {
1272 if (!RStall)
1273 return true;
1274 if (left->getHeight() != right->getHeight())
1275 return left->getHeight() > right->getHeight();
1276 } else if (RStall)
1277 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001278
1279 // If either node is scheduling for latency, sort them by height and latency
1280 // first.
1281 if (left->SchedulingPref == Sched::Latency ||
1282 right->SchedulingPref == Sched::Latency) {
1283 if (left->getHeight() != right->getHeight())
1284 return left->getHeight() > right->getHeight();
1285 if (left->Latency != right->Latency)
1286 return left->Latency > right->Latency;
1287 }
1288
Evan Chengbdd062d2010-05-20 06:13:19 +00001289 return BURRSort(left, right, SPQ);
1290}
1291
Dan Gohman3f656df2008-11-20 02:45:51 +00001292template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001293bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001294RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001295 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001296 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001297 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001298 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001299 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001300 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001301 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001302 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001303 if (DU->getNodeId() != -1 &&
1304 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001305 return true;
1306 }
1307 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001308 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001309 return false;
1310}
1311
Evan Chenga5e595d2007-09-28 22:32:30 +00001312/// hasCopyToRegUse - Return true if SU has a value successor that is a
1313/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001314static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001315 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1316 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001317 if (I->isCtrl()) continue;
1318 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001319 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001320 return true;
1321 }
1322 return false;
1323}
1324
Evan Chengf9891412007-12-20 09:25:31 +00001325/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001326/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001327static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001328 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001329 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001330 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001331 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1332 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001333 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001334 for (const SDNode *SUNode = SU->getNode(); SUNode;
1335 SUNode = SUNode->getFlaggedNode()) {
1336 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001337 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001338 const unsigned *SUImpDefs =
1339 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1340 if (!SUImpDefs)
1341 return false;
1342 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001343 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001344 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001345 continue;
1346 if (!N->hasAnyUseOfValue(i))
1347 continue;
1348 unsigned Reg = ImpDefs[i - NumDefs];
1349 for (;*SUImpDefs; ++SUImpDefs) {
1350 unsigned SUReg = *SUImpDefs;
1351 if (TRI->regsOverlap(Reg, SUReg))
1352 return true;
1353 }
Evan Chengf9891412007-12-20 09:25:31 +00001354 }
1355 }
1356 return false;
1357}
1358
Dan Gohman9a658d72009-03-24 00:49:12 +00001359/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1360/// are not handled well by the general register pressure reduction
1361/// heuristics. When presented with code like this:
1362///
1363/// N
1364/// / |
1365/// / |
1366/// U store
1367/// |
1368/// ...
1369///
1370/// the heuristics tend to push the store up, but since the
1371/// operand of the store has another use (U), this would increase
1372/// the length of that other use (the U->N edge).
1373///
1374/// This function transforms code like the above to route U's
1375/// dependence through the store when possible, like this:
1376///
1377/// N
1378/// ||
1379/// ||
1380/// store
1381/// |
1382/// U
1383/// |
1384/// ...
1385///
1386/// This results in the store being scheduled immediately
1387/// after N, which shortens the U->N live range, reducing
1388/// register pressure.
1389///
1390template<class SF>
1391void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1392 // Visit all the nodes in topological order, working top-down.
1393 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1394 SUnit *SU = &(*SUnits)[i];
1395 // For now, only look at nodes with no data successors, such as stores.
1396 // These are especially important, due to the heuristics in
1397 // getNodePriority for nodes with no data successors.
1398 if (SU->NumSuccs != 0)
1399 continue;
1400 // For now, only look at nodes with exactly one data predecessor.
1401 if (SU->NumPreds != 1)
1402 continue;
1403 // Avoid prescheduling copies to virtual registers, which don't behave
1404 // like other nodes from the perspective of scheduling heuristics.
1405 if (SDNode *N = SU->getNode())
1406 if (N->getOpcode() == ISD::CopyToReg &&
1407 TargetRegisterInfo::isVirtualRegister
1408 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1409 continue;
1410
1411 // Locate the single data predecessor.
1412 SUnit *PredSU = 0;
1413 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1414 EE = SU->Preds.end(); II != EE; ++II)
1415 if (!II->isCtrl()) {
1416 PredSU = II->getSUnit();
1417 break;
1418 }
1419 assert(PredSU);
1420
1421 // Don't rewrite edges that carry physregs, because that requires additional
1422 // support infrastructure.
1423 if (PredSU->hasPhysRegDefs)
1424 continue;
1425 // Short-circuit the case where SU is PredSU's only data successor.
1426 if (PredSU->NumSuccs == 1)
1427 continue;
1428 // Avoid prescheduling to copies from virtual registers, which don't behave
1429 // like other nodes from the perspective of scheduling // heuristics.
1430 if (SDNode *N = SU->getNode())
1431 if (N->getOpcode() == ISD::CopyFromReg &&
1432 TargetRegisterInfo::isVirtualRegister
1433 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1434 continue;
1435
1436 // Perform checks on the successors of PredSU.
1437 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1438 EE = PredSU->Succs.end(); II != EE; ++II) {
1439 SUnit *PredSuccSU = II->getSUnit();
1440 if (PredSuccSU == SU) continue;
1441 // If PredSU has another successor with no data successors, for
1442 // now don't attempt to choose either over the other.
1443 if (PredSuccSU->NumSuccs == 0)
1444 goto outer_loop_continue;
1445 // Don't break physical register dependencies.
1446 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1447 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1448 goto outer_loop_continue;
1449 // Don't introduce graph cycles.
1450 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1451 goto outer_loop_continue;
1452 }
1453
1454 // Ok, the transformation is safe and the heuristics suggest it is
1455 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001456 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1457 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001458 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001459 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1460 SDep Edge = PredSU->Succs[i];
1461 assert(!Edge.isAssignedRegDep());
1462 SUnit *SuccSU = Edge.getSUnit();
1463 if (SuccSU != SU) {
1464 Edge.setSUnit(PredSU);
1465 scheduleDAG->RemovePred(SuccSU, Edge);
1466 scheduleDAG->AddPred(SU, Edge);
1467 Edge.setSUnit(SU);
1468 scheduleDAG->AddPred(SuccSU, Edge);
1469 --i;
1470 }
1471 }
1472 outer_loop_continue:;
1473 }
1474}
1475
Evan Chengd38c22b2006-05-11 23:55:42 +00001476/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1477/// it as a def&use operand. Add a pseudo control edge from it to the other
1478/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001479/// first (lower in the schedule). If both nodes are two-address, favor the
1480/// one that has a CopyToReg use (more likely to be a loop induction update).
1481/// If both are two-address, but one is commutable while the other is not
1482/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001483template<class SF>
1484void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001485 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001486 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001487 if (!SU->isTwoAddress)
1488 continue;
1489
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001490 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001491 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001492 continue;
1493
Dan Gohman17059682008-07-17 19:10:17 +00001494 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001495 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001496 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001497 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001498 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001499 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1500 continue;
1501 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1502 if (DU->getNodeId() == -1)
1503 continue;
1504 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1505 if (!DUSU) continue;
1506 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1507 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001508 if (I->isCtrl()) continue;
1509 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001510 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001511 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001512 // Be conservative. Ignore if nodes aren't at roughly the same
1513 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001514 if (SuccSU->getHeight() < SU->getHeight() &&
1515 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001516 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001517 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1518 // constrains whatever is using the copy, instead of the copy
1519 // itself. In the case that the copy is coalesced, this
1520 // preserves the intent of the pseudo two-address heurietics.
1521 while (SuccSU->Succs.size() == 1 &&
1522 SuccSU->getNode()->isMachineOpcode() &&
1523 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001524 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001525 SuccSU = SuccSU->Succs.front().getSUnit();
1526 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001527 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1528 continue;
1529 // Don't constrain nodes with physical register defs if the
1530 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001531 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001532 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001533 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001534 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001535 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1536 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001537 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001538 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1539 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1540 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001541 continue;
1542 if ((!canClobber(SuccSU, DUSU) ||
1543 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1544 (!SU->isCommutable && SuccSU->isCommutable)) &&
1545 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001546 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001547 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001548 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001549 /*Reg=*/0, /*isNormalMemory=*/false,
1550 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001551 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001552 }
1553 }
1554 }
1555 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001556}
1557
Evan Cheng6730f032007-01-08 23:55:53 +00001558/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1559/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001560template<class SF>
1561void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001562 SethiUllmanNumbers.assign(SUnits->size(), 0);
1563
1564 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001565 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001566}
Evan Chengd38c22b2006-05-11 23:55:42 +00001567
Roman Levenstein30d09512008-03-27 09:44:37 +00001568/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001569/// predecessors of the successors of the SUnit SU. Stop when the provided
1570/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001571static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1572 unsigned Limit) {
1573 unsigned Sum = 0;
1574 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1575 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001576 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001577 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1578 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001579 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001580 if (!PredSU->isScheduled)
1581 if (++Sum > Limit)
1582 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001583 }
1584 }
1585 return Sum;
1586}
1587
Evan Chengd38c22b2006-05-11 23:55:42 +00001588
1589// Top down
1590bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001591 unsigned LPriority = SPQ->getNodePriority(left);
1592 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001593 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1594 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001595 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1596 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001597 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1598 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001599
1600 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1601 return false;
1602 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1603 return true;
1604
Evan Chengd38c22b2006-05-11 23:55:42 +00001605 if (LIsFloater)
1606 LBonus -= 2;
1607 if (RIsFloater)
1608 RBonus -= 2;
1609 if (left->NumSuccs == 1)
1610 LBonus += 2;
1611 if (right->NumSuccs == 1)
1612 RBonus += 2;
1613
Evan Cheng73bdf042008-03-01 00:39:47 +00001614 if (LPriority+LBonus != RPriority+RBonus)
1615 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001616
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001617 if (left->getDepth() != right->getDepth())
1618 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001619
1620 if (left->NumSuccsLeft != right->NumSuccsLeft)
1621 return left->NumSuccsLeft > right->NumSuccsLeft;
1622
Roman Levenstein6b371142008-04-29 09:07:59 +00001623 assert(left->NodeQueueId && right->NodeQueueId &&
1624 "NodeQueueId cannot be zero");
1625 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001626}
1627
Evan Chengd38c22b2006-05-11 23:55:42 +00001628//===----------------------------------------------------------------------===//
1629// Public Constructor Functions
1630//===----------------------------------------------------------------------===//
1631
Dan Gohmandfaf6462009-02-11 04:27:20 +00001632llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001633llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001634 const TargetMachine &TM = IS->TM;
1635 const TargetInstrInfo *TII = TM.getInstrInfo();
1636 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001637
Evan Cheng7e4abde2008-07-02 09:23:51 +00001638 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001639
Evan Chengbdd062d2010-05-20 06:13:19 +00001640 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001641 PQ->setScheduleDAG(SD);
1642 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001643}
1644
Dan Gohmandfaf6462009-02-11 04:27:20 +00001645llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001646llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001647 const TargetMachine &TM = IS->TM;
1648 const TargetInstrInfo *TII = TM.getInstrInfo();
1649 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001650
1651 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1652
Evan Chengbdd062d2010-05-20 06:13:19 +00001653 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001654 PQ->setScheduleDAG(SD);
1655 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001656}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001657
1658llvm::ScheduleDAGSDNodes *
1659llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1660 const TargetMachine &TM = IS->TM;
1661 const TargetInstrInfo *TII = TM.getInstrInfo();
1662 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1663
1664 SrcRegReductionPriorityQueue *PQ = new SrcRegReductionPriorityQueue(TII, TRI);
1665
Evan Chengbdd062d2010-05-20 06:13:19 +00001666 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
1667 PQ->setScheduleDAG(SD);
1668 return SD;
1669}
1670
1671llvm::ScheduleDAGSDNodes *
1672llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1673 const TargetMachine &TM = IS->TM;
1674 const TargetInstrInfo *TII = TM.getInstrInfo();
1675 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1676
1677 HybridBURRPriorityQueue *PQ = new HybridBURRPriorityQueue(TII, TRI);
1678
1679 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001680 PQ->setScheduleDAG(SD);
1681 return SD;
1682}