blob: 6c5b9b4451571b5703aaa09c372bea9603c19e8f [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"
Dan Gohmana4db3352008-06-21 18:35:25 +000027#include "llvm/ADT/PriorityQueue.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 Chengbdd062d2010-05-20 06:13:19 +000058 "Bottom-up rr list scheduling which avoid stalls for "
59 "long latency instructions",
60 createHybridListDAGScheduler);
61
Evan Chengd38c22b2006-05-11 23:55:42 +000062namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000063//===----------------------------------------------------------------------===//
64/// ScheduleDAGRRList - The actual register reduction list scheduler
65/// implementation. This supports both top-down and bottom-up scheduling.
66///
Nick Lewycky02d5f772009-10-25 06:33:48 +000067class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000068private:
69 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
70 /// it is top-down.
71 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000072
Evan Chengbdd062d2010-05-20 06:13:19 +000073 /// NeedLatency - True if the scheduler will make use of latency information.
74 ///
75 bool NeedLatency;
76
Evan Chengd38c22b2006-05-11 23:55:42 +000077 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000078 SchedulingPriorityQueue *AvailableQueue;
79
Dan Gohmanc07f6862008-09-23 18:50:48 +000080 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000081 /// that are "live". These nodes must be scheduled before any other nodes that
82 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000083 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000084 std::vector<SUnit*> LiveRegDefs;
85 std::vector<unsigned> LiveRegCycles;
86
Dan Gohmanad2134d2008-11-25 00:52:40 +000087 /// Topo - A topological ordering for SUnits which permits fast IsReachable
88 /// and similar queries.
89 ScheduleDAGTopologicalSort Topo;
90
Evan Chengd38c22b2006-05-11 23:55:42 +000091public:
Dan Gohman619ef482009-01-15 19:20:50 +000092 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000093 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +000094 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +000095 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +000096 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000097 }
98
99 ~ScheduleDAGRRList() {
100 delete AvailableQueue;
101 }
102
103 void Schedule();
104
Roman Levenstein733a4d62008-03-26 11:23:38 +0000105 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000106 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
107 return Topo.IsReachable(SU, TargetSU);
108 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000109
Dan Gohman60d68442009-01-29 19:49:27 +0000110 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000111 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
113 return Topo.WillCreateCycle(SU, TargetSU);
114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Dan Gohman2d170892008-12-09 22:54:47 +0000116 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000117 /// This returns true if this is a new predecessor.
118 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000119 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000120 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000121 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000122 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000123
Dan Gohman2d170892008-12-09 22:54:47 +0000124 /// RemovePred - removes a predecessor edge from SUnit SU.
125 /// This returns true if an edge was removed.
126 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000127 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000128 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000129 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000130 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000131
Evan Chengd38c22b2006-05-11 23:55:42 +0000132private:
Dan Gohman60d68442009-01-29 19:49:27 +0000133 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000134 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000135 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000136 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000137 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000138 void ScheduleNodeBottomUp(SUnit*, unsigned);
139 void ScheduleNodeTopDown(SUnit*, unsigned);
140 void UnscheduleNodeBottomUp(SUnit*);
141 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
142 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000143 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
144 const TargetRegisterClass*,
145 const TargetRegisterClass*,
146 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000147 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000148 void ListScheduleTopDown();
149 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150
151
152 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000153 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000154 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000155 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000157 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000158 if (NewNode->NodeNum >= NumSUnits)
159 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000160 return NewNode;
161 }
162
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 /// CreateClone - Creates a new SUnit from an existing one.
164 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000165 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000166 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000167 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000168 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000169 if (NewNode->NodeNum >= NumSUnits)
170 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 return NewNode;
172 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000173
Evan Chengbdd062d2010-05-20 06:13:19 +0000174 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
175 /// need actual latency information but the hybrid scheduler does.
176 bool ForceUnitLatencies() const {
177 return !NeedLatency;
178 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000179};
180} // end anonymous namespace
181
182
183/// Schedule - Schedule the DAG using list scheduling.
184void ScheduleDAGRRList::Schedule() {
David Greenef34d7ac2010-01-05 01:24:54 +0000185 DEBUG(dbgs() << "********** List Scheduling **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000186
Dan Gohmanc07f6862008-09-23 18:50:48 +0000187 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000188 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
189 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000190
Dan Gohman04543e72008-12-23 18:36:58 +0000191 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000192 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000193
Evan Chengd38c22b2006-05-11 23:55:42 +0000194 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000195 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000196 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000197
Dan Gohman46520a22008-06-21 19:18:17 +0000198 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000199
Evan Chengd38c22b2006-05-11 23:55:42 +0000200 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
201 if (isBottomUp)
202 ListScheduleBottomUp();
203 else
204 ListScheduleTopDown();
205
206 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000207}
Evan Chengd38c22b2006-05-11 23:55:42 +0000208
209//===----------------------------------------------------------------------===//
210// Bottom-Up Scheduling
211//===----------------------------------------------------------------------===//
212
Evan Chengd38c22b2006-05-11 23:55:42 +0000213/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000214/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000215void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000216 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000217
Evan Chengd38c22b2006-05-11 23:55:42 +0000218#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000219 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000220 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000221 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000222 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000223 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000224 }
225#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000226 --PredSU->NumSuccsLeft;
227
Evan Chengbdd062d2010-05-20 06:13:19 +0000228 if (!ForceUnitLatencies()) {
229 // Updating predecessor's height. This is now the cycle when the
230 // predecessor can be scheduled without causing a pipeline stall.
231 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
232 }
233
Dan Gohmanb9543432009-02-10 23:27:53 +0000234 // If all the node's successors are scheduled, this node is ready
235 // to be scheduled. Ignore the special EntrySU node.
236 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000237 PredSU->isAvailable = true;
238 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000239 }
240}
241
Dan Gohmanb9543432009-02-10 23:27:53 +0000242void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000243 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000244 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000245 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000246 ReleasePred(SU, &*I);
247 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000248 // This is a physical register dependency and it's impossible or
249 // expensive to copy the register. Make sure nothing that can
250 // clobber the register is scheduled between the predecessor and
251 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000252 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000253 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000254 LiveRegDefs[I->getReg()] = I->getSUnit();
255 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000256 }
257 }
258 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000259}
260
261/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
262/// count of its predecessors. If a predecessor pending count is zero, add it to
263/// the Available queue.
264void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000265 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000266 DEBUG(SU->dump(this));
267
Evan Chengbdd062d2010-05-20 06:13:19 +0000268#ifndef NDEBUG
269 if (CurCycle < SU->getHeight())
270 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
271#endif
272
273 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000274 SU->setHeightToAtLeast(CurCycle);
275 Sequence.push_back(SU);
276
277 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000278
279 // Release all the implicit physical register defs that are live.
280 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
281 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000282 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000283 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000284 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000285 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000286 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000287 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000288 LiveRegDefs[I->getReg()] = NULL;
289 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000290 }
291 }
292 }
293
Evan Chengd38c22b2006-05-11 23:55:42 +0000294 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000295 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000296}
297
Evan Cheng5924bf72007-09-25 01:54:36 +0000298/// CapturePred - This does the opposite of ReleasePred. Since SU is being
299/// unscheduled, incrcease the succ left count of its predecessors. Remove
300/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000301void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
302 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000303 if (PredSU->isAvailable) {
304 PredSU->isAvailable = false;
305 if (!PredSU->isPending)
306 AvailableQueue->remove(PredSU);
307 }
308
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000309 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000310 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000311}
312
313/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
314/// its predecessor states to reflect the change.
315void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000316 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000317 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000318
319 AvailableQueue->UnscheduledNode(SU);
320
321 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
322 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000323 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000324 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000325 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000326 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000327 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000328 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000329 LiveRegDefs[I->getReg()] = NULL;
330 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000331 }
332 }
333
334 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
335 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000336 if (I->isAssignedRegDep()) {
337 if (!LiveRegDefs[I->getReg()]) {
338 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000339 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000340 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000341 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
342 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000343 }
344 }
345
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000346 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 SU->isScheduled = false;
348 SU->isAvailable = true;
349 AvailableQueue->push(SU);
350}
351
Evan Cheng8e136a92007-09-26 21:36:17 +0000352/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000353/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000354void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
355 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000356 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000357 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000358 OldSU = Sequence.back();
359 Sequence.pop_back();
360 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000361 // Don't try to remove SU from AvailableQueue.
362 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000363 UnscheduleNodeBottomUp(OldSU);
364 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000365 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000366 }
367
Dan Gohman60d68442009-01-29 19:49:27 +0000368 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000369
370 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000371}
372
Evan Cheng3b245872010-02-05 01:27:11 +0000373static bool isOperandOf(const SUnit *SU, SDNode *N) {
374 for (const SDNode *SUNode = SU->getNode(); SUNode;
375 SUNode = SUNode->getFlaggedNode()) {
376 if (SUNode->isOperandOf(N))
377 return true;
378 }
379 return false;
380}
381
Evan Cheng5924bf72007-09-25 01:54:36 +0000382/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
383/// successors to the newly created node.
384SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000385 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000386 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000387
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000388 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000389 if (!N)
390 return NULL;
391
392 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000393 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000394 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000395 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000396 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000397 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000398 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000399 TryUnfold = true;
400 }
Evan Cheng79e97132007-10-05 01:39:18 +0000401 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000402 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000403 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000404 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000405 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000406 }
407
408 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000409 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000410 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000411 return NULL;
412
Evan Chengbdd062d2010-05-20 06:13:19 +0000413 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000414 assert(NewNodes.size() == 2 && "Expected a load folding node!");
415
416 N = NewNodes[1];
417 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000418 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000419 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000420 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000421 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
422 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000423 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000424
Dan Gohmane52e0892008-11-11 21:34:44 +0000425 // LoadNode may already exist. This can happen when there is another
426 // load from the same location and producing the same type of value
427 // but it has different alignment or volatileness.
428 bool isNewLoad = true;
429 SUnit *LoadSU;
430 if (LoadNode->getNodeId() != -1) {
431 LoadSU = &SUnits[LoadNode->getNodeId()];
432 isNewLoad = false;
433 } else {
434 LoadSU = CreateNewSUnit(LoadNode);
435 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000436 ComputeLatency(LoadSU);
437 }
438
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000439 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000440 assert(N->getNodeId() == -1 && "Node already inserted!");
441 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000442
Dan Gohman17059682008-07-17 19:10:17 +0000443 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000444 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000445 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000446 NewSU->isTwoAddress = true;
447 break;
448 }
449 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000450 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000451 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000452 ComputeLatency(NewSU);
453
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000454 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000455 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000456 SmallVector<SDep, 4> ChainSuccs;
457 SmallVector<SDep, 4> LoadPreds;
458 SmallVector<SDep, 4> NodePreds;
459 SmallVector<SDep, 4> NodeSuccs;
460 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
461 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000462 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000463 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000464 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000465 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000466 else
Dan Gohman2d170892008-12-09 22:54:47 +0000467 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000468 }
469 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
470 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000471 if (I->isCtrl())
472 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000473 else
Dan Gohman2d170892008-12-09 22:54:47 +0000474 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000475 }
476
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000477 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000478 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
479 const SDep &Pred = ChainPreds[i];
480 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000481 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000482 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000483 }
Evan Cheng79e97132007-10-05 01:39:18 +0000484 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000485 const SDep &Pred = LoadPreds[i];
486 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000487 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000488 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000489 }
490 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000491 const SDep &Pred = NodePreds[i];
492 RemovePred(SU, Pred);
493 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000494 }
495 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000496 SDep D = NodeSuccs[i];
497 SUnit *SuccDep = D.getSUnit();
498 D.setSUnit(SU);
499 RemovePred(SuccDep, D);
500 D.setSUnit(NewSU);
501 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000502 }
503 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000504 SDep D = ChainSuccs[i];
505 SUnit *SuccDep = D.getSUnit();
506 D.setSUnit(SU);
507 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000508 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000509 D.setSUnit(LoadSU);
510 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000511 }
Evan Cheng79e97132007-10-05 01:39:18 +0000512 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000513
514 // Add a data dependency to reflect that NewSU reads the value defined
515 // by LoadSU.
516 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000517
Evan Cheng91e0fc92007-12-18 08:42:10 +0000518 if (isNewLoad)
519 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000520 AvailableQueue->addNode(NewSU);
521
522 ++NumUnfolds;
523
524 if (NewSU->NumSuccsLeft == 0) {
525 NewSU->isAvailable = true;
526 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000527 }
528 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000529 }
530
Evan Chengbdd062d2010-05-20 06:13:19 +0000531 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000532 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000533
534 // New SUnit has the exact same predecessors.
535 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
536 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000537 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000538 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000539
540 // Only copy scheduled successors. Cut them from old node's successor
541 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000542 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000543 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
544 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000545 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000546 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000547 SUnit *SuccSU = I->getSUnit();
548 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000549 SDep D = *I;
550 D.setSUnit(NewSU);
551 AddPred(SuccSU, D);
552 D.setSUnit(SU);
553 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000554 }
555 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000556 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000557 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000558
559 AvailableQueue->updateNode(SU);
560 AvailableQueue->addNode(NewSU);
561
Evan Cheng1ec79b42007-09-27 07:09:03 +0000562 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000563 return NewSU;
564}
565
Evan Chengb2c42c62009-01-12 03:19:55 +0000566/// InsertCopiesAndMoveSuccs - Insert register copies and move all
567/// scheduled successors of the given SUnit to the last copy.
568void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
569 const TargetRegisterClass *DestRC,
570 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000571 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000572 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000573 CopyFromSU->CopySrcRC = SrcRC;
574 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000575
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000576 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000577 CopyToSU->CopySrcRC = DestRC;
578 CopyToSU->CopyDstRC = SrcRC;
579
580 // Only copy scheduled successors. Cut them from old node's successor
581 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000582 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000583 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
584 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000585 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000586 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000587 SUnit *SuccSU = I->getSUnit();
588 if (SuccSU->isScheduled) {
589 SDep D = *I;
590 D.setSUnit(CopyToSU);
591 AddPred(SuccSU, D);
592 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000593 }
594 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000595 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000596 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000597
Dan Gohman2d170892008-12-09 22:54:47 +0000598 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
599 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000600
601 AvailableQueue->updateNode(SU);
602 AvailableQueue->addNode(CopyFromSU);
603 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000604 Copies.push_back(CopyFromSU);
605 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000606
Evan Chengb2c42c62009-01-12 03:19:55 +0000607 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000608}
609
610/// getPhysicalRegisterVT - Returns the ValueType of the physical register
611/// definition of the specified node.
612/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000613static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000614 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000615 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000616 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000617 unsigned NumRes = TID.getNumDefs();
618 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000619 if (Reg == *ImpDef)
620 break;
621 ++NumRes;
622 }
623 return N->getValueType(NumRes);
624}
625
Evan Chengb8905c42009-03-04 01:41:49 +0000626/// CheckForLiveRegDef - Return true and update live register vector if the
627/// specified register def of the specified SUnit clobbers any "live" registers.
628static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
629 std::vector<SUnit*> &LiveRegDefs,
630 SmallSet<unsigned, 4> &RegAdded,
631 SmallVector<unsigned, 4> &LRegs,
632 const TargetRegisterInfo *TRI) {
633 bool Added = false;
634 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
635 if (RegAdded.insert(Reg)) {
636 LRegs.push_back(Reg);
637 Added = true;
638 }
639 }
640 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
641 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
642 if (RegAdded.insert(*Alias)) {
643 LRegs.push_back(*Alias);
644 Added = true;
645 }
646 }
647 return Added;
648}
649
Evan Cheng5924bf72007-09-25 01:54:36 +0000650/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
651/// scheduling of the given node to satisfy live physical register dependencies.
652/// If the specific node is the last one that's available to schedule, do
653/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000654bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
655 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000656 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000657 return false;
658
Evan Chenge6f92252007-09-27 18:46:06 +0000659 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000660 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000661 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
662 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000663 if (I->isAssignedRegDep())
664 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
665 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000666 }
667
Dan Gohman072734e2008-11-13 23:24:17 +0000668 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000669 if (Node->getOpcode() == ISD::INLINEASM) {
670 // Inline asm can clobber physical defs.
671 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000672 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000673 --NumOps; // Ignore the flag operand.
674
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000675 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000676 unsigned Flags =
677 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000678 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000679
680 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000681 if (InlineAsm::isRegDefKind(Flags) ||
682 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000683 // Check for def of register or earlyclobber register.
684 for (; NumVals; --NumVals, ++i) {
685 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
686 if (TargetRegisterInfo::isPhysicalRegister(Reg))
687 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
688 }
689 } else
690 i += NumVals;
691 }
692 continue;
693 }
694
Dan Gohman072734e2008-11-13 23:24:17 +0000695 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000696 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000697 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000698 if (!TID.ImplicitDefs)
699 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000700 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
701 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000702 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000703 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000704}
705
Evan Cheng1ec79b42007-09-27 07:09:03 +0000706
Evan Chengd38c22b2006-05-11 23:55:42 +0000707/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
708/// schedulers.
709void ScheduleDAGRRList::ListScheduleBottomUp() {
710 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000711
712 // Release any predecessors of the special Exit node.
713 ReleasePredecessors(&ExitSU, CurCycle);
714
Evan Chengd38c22b2006-05-11 23:55:42 +0000715 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000716 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000717 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000718 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
719 RootSU->isAvailable = true;
720 AvailableQueue->push(RootSU);
721 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000722
723 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000724 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000725 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000726 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000727 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000728 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000729 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000730 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000731 SUnit *CurSU = AvailableQueue->pop();
732 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000733 SmallVector<unsigned, 4> LRegs;
734 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
735 break;
736 Delayed = true;
737 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000738
739 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
740 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000741 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000742 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000743
744 // All candidates are delayed due to live physical reg dependencies.
745 // Try backtracking, code duplication, or inserting cross class copies
746 // to resolve it.
747 if (Delayed && !CurSU) {
748 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
749 SUnit *TrySU = NotReady[i];
750 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
751
752 // Try unscheduling up to the point where it's safe to schedule
753 // this node.
754 unsigned LiveCycle = CurCycle;
755 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
756 unsigned Reg = LRegs[j];
757 unsigned LCycle = LiveRegCycles[Reg];
758 LiveCycle = std::min(LiveCycle, LCycle);
759 }
760 SUnit *OldSU = Sequence[LiveCycle];
761 if (!WillCreateCycle(TrySU, OldSU)) {
762 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
763 // Force the current node to be scheduled before the node that
764 // requires the physical reg dep.
765 if (OldSU->isAvailable) {
766 OldSU->isAvailable = false;
767 AvailableQueue->remove(OldSU);
768 }
Dan Gohman2d170892008-12-09 22:54:47 +0000769 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
770 /*Reg=*/0, /*isNormalMemory=*/false,
771 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000772 // If one or more successors has been unscheduled, then the current
773 // node is no longer avaialable. Schedule a successor that's now
774 // available instead.
775 if (!TrySU->isAvailable)
776 CurSU = AvailableQueue->pop();
777 else {
778 CurSU = TrySU;
779 TrySU->isPending = false;
780 NotReady.erase(NotReady.begin()+i);
781 }
782 break;
783 }
784 }
785
786 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000787 // Can't backtrack. If it's too expensive to copy the value, then try
788 // duplicate the nodes that produces these "too expensive to copy"
789 // values to break the dependency. In case even that doesn't work,
790 // insert cross class copies.
791 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000792 SUnit *TrySU = NotReady[0];
793 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
794 assert(LRegs.size() == 1 && "Can't handle this yet!");
795 unsigned Reg = LRegs[0];
796 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000797 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000798 const TargetRegisterClass *RC =
799 TRI->getPhysicalRegisterRegClass(Reg, VT);
800 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
801
802 // If cross copy register class is null, then it must be possible copy
803 // the value directly. Do not try duplicate the def.
804 SUnit *NewDef = 0;
805 if (DestRC)
806 NewDef = CopyAndMoveSuccessors(LRDef);
807 else
808 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000809 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000810 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000811 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000812 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000813 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000814 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000815 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000816 /*Reg=*/0, /*isNormalMemory=*/false,
817 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000818 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000819 NewDef = Copies.back();
820 }
821
Evan Chengbdd062d2010-05-20 06:13:19 +0000822 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000823 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000824 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000825 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000826 /*Reg=*/0, /*isNormalMemory=*/false,
827 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000828 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000829 TrySU->isAvailable = false;
830 CurSU = NewDef;
831 }
832
Dan Gohman60d68442009-01-29 19:49:27 +0000833 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000834 }
835
Evan Chengd38c22b2006-05-11 23:55:42 +0000836 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000837 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
838 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000839 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000840 if (NotReady[i]->isAvailable)
841 AvailableQueue->push(NotReady[i]);
842 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000843 NotReady.clear();
844
Dan Gohmanc602dd42008-11-21 00:10:42 +0000845 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000846 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000847 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000848 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000849 }
850
Evan Chengd38c22b2006-05-11 23:55:42 +0000851 // Reverse the order if it is bottom up.
852 std::reverse(Sequence.begin(), Sequence.end());
853
Evan Chengd38c22b2006-05-11 23:55:42 +0000854#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000855 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000856#endif
857}
858
859//===----------------------------------------------------------------------===//
860// Top-Down Scheduling
861//===----------------------------------------------------------------------===//
862
863/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000864/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000865void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000866 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000867
Evan Chengd38c22b2006-05-11 23:55:42 +0000868#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000869 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000870 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000871 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000872 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000873 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000874 }
875#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000876 --SuccSU->NumPredsLeft;
877
Dan Gohmanb9543432009-02-10 23:27:53 +0000878 // If all the node's predecessors are scheduled, this node is ready
879 // to be scheduled. Ignore the special ExitSU node.
880 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000881 SuccSU->isAvailable = true;
882 AvailableQueue->push(SuccSU);
883 }
884}
885
Dan Gohmanb9543432009-02-10 23:27:53 +0000886void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
887 // Top down: release successors
888 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
889 I != E; ++I) {
890 assert(!I->isAssignedRegDep() &&
891 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
892
893 ReleaseSucc(SU, &*I);
894 }
895}
896
Evan Chengd38c22b2006-05-11 23:55:42 +0000897/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
898/// count of its successors. If a successor pending count is zero, add it to
899/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000900void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000901 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000902 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000903
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000904 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
905 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000906 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000907
Dan Gohmanb9543432009-02-10 23:27:53 +0000908 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000909 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000910 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000911}
912
Dan Gohman54a187e2007-08-20 19:28:38 +0000913/// ListScheduleTopDown - The main loop of list scheduling for top-down
914/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000915void ScheduleDAGRRList::ListScheduleTopDown() {
916 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000917 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000918
Dan Gohmanb9543432009-02-10 23:27:53 +0000919 // Release any successors of the special Entry node.
920 ReleaseSuccessors(&EntrySU);
921
Evan Chengd38c22b2006-05-11 23:55:42 +0000922 // All leaves to Available queue.
923 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
924 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000925 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000926 AvailableQueue->push(&SUnits[i]);
927 SUnits[i].isAvailable = true;
928 }
929 }
930
Evan Chengd38c22b2006-05-11 23:55:42 +0000931 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000932 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000933 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000934 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000935 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000936
Dan Gohmanc602dd42008-11-21 00:10:42 +0000937 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000938 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000939 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000940 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000941 }
942
Evan Chengd38c22b2006-05-11 23:55:42 +0000943#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000944 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000945#endif
946}
947
948
Evan Chengd38c22b2006-05-11 23:55:42 +0000949//===----------------------------------------------------------------------===//
950// RegReductionPriorityQueue Implementation
951//===----------------------------------------------------------------------===//
952//
953// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
954// to reduce register pressure.
955//
956namespace {
957 template<class SF>
958 class RegReductionPriorityQueue;
959
960 /// Sorting functions for the Available queue.
961 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
962 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
963 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
964 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
965
966 bool operator()(const SUnit* left, const SUnit* right) const;
967 };
968
969 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
970 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
971 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
972 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
973
974 bool operator()(const SUnit* left, const SUnit* right) const;
975 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000976
977 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
978 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
979 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
980 : SPQ(spq) {}
981 src_ls_rr_sort(const src_ls_rr_sort &RHS)
982 : SPQ(RHS.SPQ) {}
983
984 bool operator()(const SUnit* left, const SUnit* right) const;
985 };
Evan Chengbdd062d2010-05-20 06:13:19 +0000986
987 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
988 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
989 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
990 : SPQ(spq) {}
991 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
992 : SPQ(RHS.SPQ) {}
993
994 bool operator()(const SUnit* left, const SUnit* right) const;
995 };
Evan Chengd38c22b2006-05-11 23:55:42 +0000996} // end anonymous namespace
997
Dan Gohman186f65d2008-11-20 03:30:37 +0000998/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
999/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001000static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001001CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001002 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1003 if (SethiUllmanNumber != 0)
1004 return SethiUllmanNumber;
1005
1006 unsigned Extra = 0;
1007 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1008 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001009 if (I->isCtrl()) continue; // ignore chain preds
1010 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001011 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001012 if (PredSethiUllman > SethiUllmanNumber) {
1013 SethiUllmanNumber = PredSethiUllman;
1014 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001015 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001016 ++Extra;
1017 }
1018
1019 SethiUllmanNumber += Extra;
1020
1021 if (SethiUllmanNumber == 0)
1022 SethiUllmanNumber = 1;
1023
1024 return SethiUllmanNumber;
1025}
1026
Evan Chengd38c22b2006-05-11 23:55:42 +00001027namespace {
1028 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001029 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +00001030 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
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)
Evan Chengbdd062d2010-05-20 06:13:19 +00001047 : Queue(SF(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 Gohmana4db3352008-06-21 18:35:25 +00001113 Queue.push(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 Gohmana4db3352008-06-21 18:35:25 +00001118 SUnit *V = Queue.top();
1119 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001120 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001121 return V;
1122 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001123
Evan Cheng5924bf72007-09-25 01:54:36 +00001124 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001125 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001126 assert(SU->NodeQueueId != 0 && "Not in queue!");
1127 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001128 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001129 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001130
1131 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1132 scheduleDAG = scheduleDag;
1133 }
1134
1135 protected:
1136 bool canClobber(const SUnit *SU, const SUnit *Op);
1137 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001138 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001139 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001140 };
1141
Dan Gohman186f65d2008-11-20 03:30:37 +00001142 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1143 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001144
Dan Gohman186f65d2008-11-20 03:30:37 +00001145 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1146 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001147
1148 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1149 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001150
1151 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1152 HybridBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001153}
1154
Evan Chengb9e3db62007-03-14 22:43:40 +00001155/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001156/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001157static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001158 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001159 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001160 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001161 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001162 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001163 // If there are bunch of CopyToRegs stacked up, they should be considered
1164 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001165 if (I->getSUnit()->getNode() &&
1166 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001167 Height = closestSucc(I->getSUnit())+1;
1168 if (Height > MaxHeight)
1169 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001170 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001171 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001172}
1173
Evan Cheng61bc51e2007-12-20 02:22:36 +00001174/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001175/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001176static unsigned calcMaxScratches(const SUnit *SU) {
1177 unsigned Scratches = 0;
1178 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001179 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001180 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001181 Scratches++;
1182 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001183 return Scratches;
1184}
1185
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001186template <typename RRSort>
1187static bool BURRSort(const SUnit *left, const SUnit *right,
1188 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001189 unsigned LPriority = SPQ->getNodePriority(left);
1190 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001191 if (LPriority != RPriority)
1192 return LPriority > RPriority;
1193
1194 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1195 // e.g.
1196 // t1 = op t2, c1
1197 // t3 = op t4, c2
1198 //
1199 // and the following instructions are both ready.
1200 // t2 = op c3
1201 // t4 = op c4
1202 //
1203 // Then schedule t2 = op first.
1204 // i.e.
1205 // t4 = op c4
1206 // t2 = op c3
1207 // t1 = op t2, c1
1208 // t3 = op t4, c2
1209 //
1210 // This creates more short live intervals.
1211 unsigned LDist = closestSucc(left);
1212 unsigned RDist = closestSucc(right);
1213 if (LDist != RDist)
1214 return LDist < RDist;
1215
Evan Cheng3a14efa2009-02-12 08:59:45 +00001216 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001217 unsigned LScratch = calcMaxScratches(left);
1218 unsigned RScratch = calcMaxScratches(right);
1219 if (LScratch != RScratch)
1220 return LScratch > RScratch;
1221
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001222 if (left->getHeight() != right->getHeight())
1223 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001224
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001225 if (left->getDepth() != right->getDepth())
1226 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001227
Roman Levenstein6b371142008-04-29 09:07:59 +00001228 assert(left->NodeQueueId && right->NodeQueueId &&
1229 "NodeQueueId cannot be zero");
1230 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001231}
1232
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001233// Bottom up
1234bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1235 return BURRSort(left, right, SPQ);
1236}
1237
1238// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001239bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001240 unsigned LOrder = SPQ->getNodeOrdering(left);
1241 unsigned ROrder = SPQ->getNodeOrdering(right);
1242
1243 // Prefer an ordering where the lower the non-zero order number, the higher
1244 // the preference.
1245 if ((LOrder || ROrder) && LOrder != ROrder)
1246 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1247
1248 return BURRSort(left, right, SPQ);
1249}
1250
Evan Chengbdd062d2010-05-20 06:13:19 +00001251bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Cheng4401f882010-05-20 23:26:43 +00001252 bool LStall = left->SchedulingPref == Sched::Latency &&
1253 SPQ->getCurCycle() < left->getHeight();
1254 bool RStall = right->SchedulingPref == Sched::Latency &&
1255 SPQ->getCurCycle() < right->getHeight();
Evan Chengbdd062d2010-05-20 06:13:19 +00001256 // If scheduling one of the node will cause a pipeline stall, delay it.
1257 // If scheduling either one of the node will cause a pipeline stall, sort them
1258 // according to their height.
1259 // If neither will cause a pipeline stall, try to reduce register pressure.
1260 if (LStall) {
1261 if (!RStall)
1262 return true;
1263 if (left->getHeight() != right->getHeight())
1264 return left->getHeight() > right->getHeight();
1265 } else if (RStall)
1266 return false;
1267 return BURRSort(left, right, SPQ);
1268}
1269
Dan Gohman3f656df2008-11-20 02:45:51 +00001270template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001271bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001272RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001273 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001274 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001275 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001276 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001277 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001278 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001279 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001280 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001281 if (DU->getNodeId() != -1 &&
1282 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001283 return true;
1284 }
1285 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001286 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001287 return false;
1288}
1289
Evan Chenga5e595d2007-09-28 22:32:30 +00001290/// hasCopyToRegUse - Return true if SU has a value successor that is a
1291/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001292static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001293 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1294 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001295 if (I->isCtrl()) continue;
1296 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001297 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001298 return true;
1299 }
1300 return false;
1301}
1302
Evan Chengf9891412007-12-20 09:25:31 +00001303/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001304/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001305static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001306 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001307 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001308 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001309 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1310 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001311 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001312 for (const SDNode *SUNode = SU->getNode(); SUNode;
1313 SUNode = SUNode->getFlaggedNode()) {
1314 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001315 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001316 const unsigned *SUImpDefs =
1317 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1318 if (!SUImpDefs)
1319 return false;
1320 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001321 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001322 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001323 continue;
1324 if (!N->hasAnyUseOfValue(i))
1325 continue;
1326 unsigned Reg = ImpDefs[i - NumDefs];
1327 for (;*SUImpDefs; ++SUImpDefs) {
1328 unsigned SUReg = *SUImpDefs;
1329 if (TRI->regsOverlap(Reg, SUReg))
1330 return true;
1331 }
Evan Chengf9891412007-12-20 09:25:31 +00001332 }
1333 }
1334 return false;
1335}
1336
Dan Gohman9a658d72009-03-24 00:49:12 +00001337/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1338/// are not handled well by the general register pressure reduction
1339/// heuristics. When presented with code like this:
1340///
1341/// N
1342/// / |
1343/// / |
1344/// U store
1345/// |
1346/// ...
1347///
1348/// the heuristics tend to push the store up, but since the
1349/// operand of the store has another use (U), this would increase
1350/// the length of that other use (the U->N edge).
1351///
1352/// This function transforms code like the above to route U's
1353/// dependence through the store when possible, like this:
1354///
1355/// N
1356/// ||
1357/// ||
1358/// store
1359/// |
1360/// U
1361/// |
1362/// ...
1363///
1364/// This results in the store being scheduled immediately
1365/// after N, which shortens the U->N live range, reducing
1366/// register pressure.
1367///
1368template<class SF>
1369void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1370 // Visit all the nodes in topological order, working top-down.
1371 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1372 SUnit *SU = &(*SUnits)[i];
1373 // For now, only look at nodes with no data successors, such as stores.
1374 // These are especially important, due to the heuristics in
1375 // getNodePriority for nodes with no data successors.
1376 if (SU->NumSuccs != 0)
1377 continue;
1378 // For now, only look at nodes with exactly one data predecessor.
1379 if (SU->NumPreds != 1)
1380 continue;
1381 // Avoid prescheduling copies to virtual registers, which don't behave
1382 // like other nodes from the perspective of scheduling heuristics.
1383 if (SDNode *N = SU->getNode())
1384 if (N->getOpcode() == ISD::CopyToReg &&
1385 TargetRegisterInfo::isVirtualRegister
1386 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1387 continue;
1388
1389 // Locate the single data predecessor.
1390 SUnit *PredSU = 0;
1391 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1392 EE = SU->Preds.end(); II != EE; ++II)
1393 if (!II->isCtrl()) {
1394 PredSU = II->getSUnit();
1395 break;
1396 }
1397 assert(PredSU);
1398
1399 // Don't rewrite edges that carry physregs, because that requires additional
1400 // support infrastructure.
1401 if (PredSU->hasPhysRegDefs)
1402 continue;
1403 // Short-circuit the case where SU is PredSU's only data successor.
1404 if (PredSU->NumSuccs == 1)
1405 continue;
1406 // Avoid prescheduling to copies from virtual registers, which don't behave
1407 // like other nodes from the perspective of scheduling // heuristics.
1408 if (SDNode *N = SU->getNode())
1409 if (N->getOpcode() == ISD::CopyFromReg &&
1410 TargetRegisterInfo::isVirtualRegister
1411 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1412 continue;
1413
1414 // Perform checks on the successors of PredSU.
1415 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1416 EE = PredSU->Succs.end(); II != EE; ++II) {
1417 SUnit *PredSuccSU = II->getSUnit();
1418 if (PredSuccSU == SU) continue;
1419 // If PredSU has another successor with no data successors, for
1420 // now don't attempt to choose either over the other.
1421 if (PredSuccSU->NumSuccs == 0)
1422 goto outer_loop_continue;
1423 // Don't break physical register dependencies.
1424 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1425 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1426 goto outer_loop_continue;
1427 // Don't introduce graph cycles.
1428 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1429 goto outer_loop_continue;
1430 }
1431
1432 // Ok, the transformation is safe and the heuristics suggest it is
1433 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001434 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1435 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001436 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001437 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1438 SDep Edge = PredSU->Succs[i];
1439 assert(!Edge.isAssignedRegDep());
1440 SUnit *SuccSU = Edge.getSUnit();
1441 if (SuccSU != SU) {
1442 Edge.setSUnit(PredSU);
1443 scheduleDAG->RemovePred(SuccSU, Edge);
1444 scheduleDAG->AddPred(SU, Edge);
1445 Edge.setSUnit(SU);
1446 scheduleDAG->AddPred(SuccSU, Edge);
1447 --i;
1448 }
1449 }
1450 outer_loop_continue:;
1451 }
1452}
1453
Evan Chengd38c22b2006-05-11 23:55:42 +00001454/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1455/// it as a def&use operand. Add a pseudo control edge from it to the other
1456/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001457/// first (lower in the schedule). If both nodes are two-address, favor the
1458/// one that has a CopyToReg use (more likely to be a loop induction update).
1459/// If both are two-address, but one is commutable while the other is not
1460/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001461template<class SF>
1462void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001463 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001464 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001465 if (!SU->isTwoAddress)
1466 continue;
1467
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001468 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001469 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001470 continue;
1471
Dan Gohman17059682008-07-17 19:10:17 +00001472 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001473 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001474 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001475 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001476 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001477 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1478 continue;
1479 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1480 if (DU->getNodeId() == -1)
1481 continue;
1482 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1483 if (!DUSU) continue;
1484 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1485 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001486 if (I->isCtrl()) continue;
1487 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001488 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001489 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001490 // Be conservative. Ignore if nodes aren't at roughly the same
1491 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001492 if (SuccSU->getHeight() < SU->getHeight() &&
1493 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001494 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001495 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1496 // constrains whatever is using the copy, instead of the copy
1497 // itself. In the case that the copy is coalesced, this
1498 // preserves the intent of the pseudo two-address heurietics.
1499 while (SuccSU->Succs.size() == 1 &&
1500 SuccSU->getNode()->isMachineOpcode() &&
1501 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001502 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001503 SuccSU = SuccSU->Succs.front().getSUnit();
1504 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001505 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1506 continue;
1507 // Don't constrain nodes with physical register defs if the
1508 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001509 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001510 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001511 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001512 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001513 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1514 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001515 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001516 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1517 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1518 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001519 continue;
1520 if ((!canClobber(SuccSU, DUSU) ||
1521 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1522 (!SU->isCommutable && SuccSU->isCommutable)) &&
1523 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001524 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001525 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001526 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001527 /*Reg=*/0, /*isNormalMemory=*/false,
1528 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001529 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001530 }
1531 }
1532 }
1533 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001534}
1535
Evan Cheng6730f032007-01-08 23:55:53 +00001536/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1537/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001538template<class SF>
1539void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001540 SethiUllmanNumbers.assign(SUnits->size(), 0);
1541
1542 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001543 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001544}
Evan Chengd38c22b2006-05-11 23:55:42 +00001545
Roman Levenstein30d09512008-03-27 09:44:37 +00001546/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001547/// predecessors of the successors of the SUnit SU. Stop when the provided
1548/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001549static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1550 unsigned Limit) {
1551 unsigned Sum = 0;
1552 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1553 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001554 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001555 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1556 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001557 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001558 if (!PredSU->isScheduled)
1559 if (++Sum > Limit)
1560 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001561 }
1562 }
1563 return Sum;
1564}
1565
Evan Chengd38c22b2006-05-11 23:55:42 +00001566
1567// Top down
1568bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001569 unsigned LPriority = SPQ->getNodePriority(left);
1570 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001571 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1572 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001573 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1574 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001575 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1576 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001577
1578 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1579 return false;
1580 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1581 return true;
1582
Evan Chengd38c22b2006-05-11 23:55:42 +00001583 if (LIsFloater)
1584 LBonus -= 2;
1585 if (RIsFloater)
1586 RBonus -= 2;
1587 if (left->NumSuccs == 1)
1588 LBonus += 2;
1589 if (right->NumSuccs == 1)
1590 RBonus += 2;
1591
Evan Cheng73bdf042008-03-01 00:39:47 +00001592 if (LPriority+LBonus != RPriority+RBonus)
1593 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001594
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001595 if (left->getDepth() != right->getDepth())
1596 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001597
1598 if (left->NumSuccsLeft != right->NumSuccsLeft)
1599 return left->NumSuccsLeft > right->NumSuccsLeft;
1600
Roman Levenstein6b371142008-04-29 09:07:59 +00001601 assert(left->NodeQueueId && right->NodeQueueId &&
1602 "NodeQueueId cannot be zero");
1603 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001604}
1605
Evan Chengd38c22b2006-05-11 23:55:42 +00001606//===----------------------------------------------------------------------===//
1607// Public Constructor Functions
1608//===----------------------------------------------------------------------===//
1609
Dan Gohmandfaf6462009-02-11 04:27:20 +00001610llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001611llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001612 const TargetMachine &TM = IS->TM;
1613 const TargetInstrInfo *TII = TM.getInstrInfo();
1614 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001615
Evan Cheng7e4abde2008-07-02 09:23:51 +00001616 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001617
Evan Chengbdd062d2010-05-20 06:13:19 +00001618 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001619 PQ->setScheduleDAG(SD);
1620 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001621}
1622
Dan Gohmandfaf6462009-02-11 04:27:20 +00001623llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001624llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001625 const TargetMachine &TM = IS->TM;
1626 const TargetInstrInfo *TII = TM.getInstrInfo();
1627 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001628
1629 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1630
Evan Chengbdd062d2010-05-20 06:13:19 +00001631 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001632 PQ->setScheduleDAG(SD);
1633 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001634}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001635
1636llvm::ScheduleDAGSDNodes *
1637llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1638 const TargetMachine &TM = IS->TM;
1639 const TargetInstrInfo *TII = TM.getInstrInfo();
1640 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1641
1642 SrcRegReductionPriorityQueue *PQ = new SrcRegReductionPriorityQueue(TII, TRI);
1643
Evan Chengbdd062d2010-05-20 06:13:19 +00001644 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
1645 PQ->setScheduleDAG(SD);
1646 return SD;
1647}
1648
1649llvm::ScheduleDAGSDNodes *
1650llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1651 const TargetMachine &TM = IS->TM;
1652 const TargetInstrInfo *TII = TM.getInstrInfo();
1653 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1654
1655 HybridBURRPriorityQueue *PQ = new HybridBURRPriorityQueue(TII, TRI);
1656
1657 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001658 PQ->setScheduleDAG(SD);
1659 return SD;
1660}