blob: dea59937198bb9cc6072ac270618e937ea596364 [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"
Jim Laskey29e635d2006-08-02 12:30:23 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman619ef482009-01-15 19:20:50 +000021#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Dan Gohmana4db3352008-06-21 18:35:25 +000028#include "llvm/ADT/PriorityQueue.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000029#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000030#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000031#include "llvm/ADT/STLExtras.h"
Chris 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 Chengd38c22b2006-05-11 23:55:42 +000055namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000056//===----------------------------------------------------------------------===//
57/// ScheduleDAGRRList - The actual register reduction list scheduler
58/// implementation. This supports both top-down and bottom-up scheduling.
59///
Nick Lewycky02d5f772009-10-25 06:33:48 +000060class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000061private:
62 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
63 /// it is top-down.
64 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000065
Evan Chengd38c22b2006-05-11 23:55:42 +000066 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000067 SchedulingPriorityQueue *AvailableQueue;
68
Dan Gohmanc07f6862008-09-23 18:50:48 +000069 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000070 /// that are "live". These nodes must be scheduled before any other nodes that
71 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000072 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000073 std::vector<SUnit*> LiveRegDefs;
74 std::vector<unsigned> LiveRegCycles;
75
Dan Gohmanad2134d2008-11-25 00:52:40 +000076 /// Topo - A topological ordering for SUnits which permits fast IsReachable
77 /// and similar queries.
78 ScheduleDAGTopologicalSort Topo;
79
Evan Chengd38c22b2006-05-11 23:55:42 +000080public:
Dan Gohman619ef482009-01-15 19:20:50 +000081 ScheduleDAGRRList(MachineFunction &mf,
82 bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000083 SchedulingPriorityQueue *availqueue)
Dan Gohman619ef482009-01-15 19:20:50 +000084 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup),
Dan Gohmanad2134d2008-11-25 00:52:40 +000085 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000086 }
87
88 ~ScheduleDAGRRList() {
89 delete AvailableQueue;
90 }
91
92 void Schedule();
93
Roman Levenstein733a4d62008-03-26 11:23:38 +000094 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +000095 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
96 return Topo.IsReachable(SU, TargetSU);
97 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000098
Dan Gohman60d68442009-01-29 19:49:27 +000099 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000100 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000101 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
102 return Topo.WillCreateCycle(SU, TargetSU);
103 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000104
Dan Gohman2d170892008-12-09 22:54:47 +0000105 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000106 /// This returns true if this is a new predecessor.
107 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000108 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000109 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000110 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000111 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000112
Dan Gohman2d170892008-12-09 22:54:47 +0000113 /// RemovePred - removes a predecessor edge from SUnit SU.
114 /// This returns true if an edge was removed.
115 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000116 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000117 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000118 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000119 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000120
Evan Chengd38c22b2006-05-11 23:55:42 +0000121private:
Dan Gohman60d68442009-01-29 19:49:27 +0000122 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000123 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000124 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000125 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000126 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000127 void ScheduleNodeBottomUp(SUnit*, unsigned);
128 void ScheduleNodeTopDown(SUnit*, unsigned);
129 void UnscheduleNodeBottomUp(SUnit*);
130 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
131 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000132 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
133 const TargetRegisterClass*,
134 const TargetRegisterClass*,
135 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000136 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000137 void ListScheduleTopDown();
138 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000139
140
141 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000142 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000143 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000144 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000145 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000146 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000147 if (NewNode->NodeNum >= NumSUnits)
148 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000149 return NewNode;
150 }
151
Roman Levenstein733a4d62008-03-26 11:23:38 +0000152 /// CreateClone - Creates a new SUnit from an existing one.
153 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000154 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000155 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156 SUnit *NewNode = Clone(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 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000162
163 /// ForceUnitLatencies - Return true, since register-pressure-reducing
164 /// scheduling doesn't need actual latency information.
165 bool ForceUnitLatencies() const { return true; }
Evan Chengd38c22b2006-05-11 23:55:42 +0000166};
167} // end anonymous namespace
168
169
170/// Schedule - Schedule the DAG using list scheduling.
171void ScheduleDAGRRList::Schedule() {
David Greenef34d7ac2010-01-05 01:24:54 +0000172 DEBUG(dbgs() << "********** List Scheduling **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000173
Dan Gohmanc07f6862008-09-23 18:50:48 +0000174 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000175 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
176 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000177
Dan Gohman04543e72008-12-23 18:36:58 +0000178 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000179 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000180
Evan Chengd38c22b2006-05-11 23:55:42 +0000181 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000182 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000183 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000184
Dan Gohman46520a22008-06-21 19:18:17 +0000185 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000186
Evan Chengd38c22b2006-05-11 23:55:42 +0000187 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
188 if (isBottomUp)
189 ListScheduleBottomUp();
190 else
191 ListScheduleTopDown();
192
193 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000194}
Evan Chengd38c22b2006-05-11 23:55:42 +0000195
196//===----------------------------------------------------------------------===//
197// Bottom-Up Scheduling
198//===----------------------------------------------------------------------===//
199
Evan Chengd38c22b2006-05-11 23:55:42 +0000200/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000201/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000202void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000203 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000204
Evan Chengd38c22b2006-05-11 23:55:42 +0000205#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000206 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000207 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000208 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000209 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000210 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000211 }
212#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000213 --PredSU->NumSuccsLeft;
214
Dan Gohmanb9543432009-02-10 23:27:53 +0000215 // If all the node's successors are scheduled, this node is ready
216 // to be scheduled. Ignore the special EntrySU node.
217 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000218 PredSU->isAvailable = true;
219 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000220 }
221}
222
Dan Gohmanb9543432009-02-10 23:27:53 +0000223void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000224 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000225 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000226 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000227 ReleasePred(SU, &*I);
228 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000229 // This is a physical register dependency and it's impossible or
230 // expensive to copy the register. Make sure nothing that can
231 // clobber the register is scheduled between the predecessor and
232 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000233 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000234 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000235 LiveRegDefs[I->getReg()] = I->getSUnit();
236 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000237 }
238 }
239 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000240}
241
242/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
243/// count of its predecessors. If a predecessor pending count is zero, add it to
244/// the Available queue.
245void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000246 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000247 DEBUG(SU->dump(this));
248
249 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
250 SU->setHeightToAtLeast(CurCycle);
251 Sequence.push_back(SU);
252
253 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000254
255 // Release all the implicit physical register defs that are live.
256 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
257 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000258 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000259 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000260 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000261 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000262 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000263 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000264 LiveRegDefs[I->getReg()] = NULL;
265 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000266 }
267 }
268 }
269
Evan Chengd38c22b2006-05-11 23:55:42 +0000270 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000271 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000272}
273
Evan Cheng5924bf72007-09-25 01:54:36 +0000274/// CapturePred - This does the opposite of ReleasePred. Since SU is being
275/// unscheduled, incrcease the succ left count of its predecessors. Remove
276/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000277void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
278 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000279 if (PredSU->isAvailable) {
280 PredSU->isAvailable = false;
281 if (!PredSU->isPending)
282 AvailableQueue->remove(PredSU);
283 }
284
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000285 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000286 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000287}
288
289/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
290/// its predecessor states to reflect the change.
291void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000292 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000293 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000294
295 AvailableQueue->UnscheduledNode(SU);
296
297 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
298 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000299 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000300 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000301 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000302 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000303 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000304 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000305 LiveRegDefs[I->getReg()] = NULL;
306 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000307 }
308 }
309
310 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
311 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000312 if (I->isAssignedRegDep()) {
313 if (!LiveRegDefs[I->getReg()]) {
314 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000315 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000316 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000317 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
318 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000319 }
320 }
321
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000322 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000323 SU->isScheduled = false;
324 SU->isAvailable = true;
325 AvailableQueue->push(SU);
326}
327
Evan Cheng8e136a92007-09-26 21:36:17 +0000328/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000329/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000330void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
331 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000332 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000333 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000334 OldSU = Sequence.back();
335 Sequence.pop_back();
336 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000337 // Don't try to remove SU from AvailableQueue.
338 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000339 UnscheduleNodeBottomUp(OldSU);
340 --CurCycle;
341 }
342
Dan Gohman60d68442009-01-29 19:49:27 +0000343 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000344
345 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000346}
347
Evan Cheng5924bf72007-09-25 01:54:36 +0000348/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
349/// successors to the newly created node.
350SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000351 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000352 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000353
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000354 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000355 if (!N)
356 return NULL;
357
358 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000359 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000360 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000361 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000362 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000363 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000364 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000365 TryUnfold = true;
366 }
Evan Cheng79e97132007-10-05 01:39:18 +0000367 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000368 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000369 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000370 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000371 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000372 }
373
374 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000375 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000376 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000377 return NULL;
378
David Greenef34d7ac2010-01-05 01:24:54 +0000379 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000380 assert(NewNodes.size() == 2 && "Expected a load folding node!");
381
382 N = NewNodes[1];
383 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000384 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000385 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000386 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000387 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
388 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000389 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000390
Dan Gohmane52e0892008-11-11 21:34:44 +0000391 // LoadNode may already exist. This can happen when there is another
392 // load from the same location and producing the same type of value
393 // but it has different alignment or volatileness.
394 bool isNewLoad = true;
395 SUnit *LoadSU;
396 if (LoadNode->getNodeId() != -1) {
397 LoadSU = &SUnits[LoadNode->getNodeId()];
398 isNewLoad = false;
399 } else {
400 LoadSU = CreateNewSUnit(LoadNode);
401 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000402 ComputeLatency(LoadSU);
403 }
404
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000405 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000406 assert(N->getNodeId() == -1 && "Node already inserted!");
407 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000408
Dan Gohman17059682008-07-17 19:10:17 +0000409 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000410 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000411 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000412 NewSU->isTwoAddress = true;
413 break;
414 }
415 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000416 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000417 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000418 ComputeLatency(NewSU);
419
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000420 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000421 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000422 SmallVector<SDep, 4> ChainSuccs;
423 SmallVector<SDep, 4> LoadPreds;
424 SmallVector<SDep, 4> NodePreds;
425 SmallVector<SDep, 4> NodeSuccs;
426 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
427 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000428 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000429 ChainPreds.push_back(*I);
Dan Gohman2d170892008-12-09 22:54:47 +0000430 else if (I->getSUnit()->getNode() &&
431 I->getSUnit()->getNode()->isOperandOf(LoadNode))
432 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000433 else
Dan Gohman2d170892008-12-09 22:54:47 +0000434 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000435 }
436 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
437 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000438 if (I->isCtrl())
439 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000440 else
Dan Gohman2d170892008-12-09 22:54:47 +0000441 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000442 }
443
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000444 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000445 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
446 const SDep &Pred = ChainPreds[i];
447 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000448 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000449 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000450 }
Evan Cheng79e97132007-10-05 01:39:18 +0000451 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000452 const SDep &Pred = LoadPreds[i];
453 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000454 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000455 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000456 }
457 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000458 const SDep &Pred = NodePreds[i];
459 RemovePred(SU, Pred);
460 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000461 }
462 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000463 SDep D = NodeSuccs[i];
464 SUnit *SuccDep = D.getSUnit();
465 D.setSUnit(SU);
466 RemovePred(SuccDep, D);
467 D.setSUnit(NewSU);
468 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000469 }
470 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000471 SDep D = ChainSuccs[i];
472 SUnit *SuccDep = D.getSUnit();
473 D.setSUnit(SU);
474 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000475 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000476 D.setSUnit(LoadSU);
477 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000478 }
Evan Cheng79e97132007-10-05 01:39:18 +0000479 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000480
481 // Add a data dependency to reflect that NewSU reads the value defined
482 // by LoadSU.
483 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000484
Evan Cheng91e0fc92007-12-18 08:42:10 +0000485 if (isNewLoad)
486 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000487 AvailableQueue->addNode(NewSU);
488
489 ++NumUnfolds;
490
491 if (NewSU->NumSuccsLeft == 0) {
492 NewSU->isAvailable = true;
493 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000494 }
495 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000496 }
497
David Greenef34d7ac2010-01-05 01:24:54 +0000498 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000499 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000500
501 // New SUnit has the exact same predecessors.
502 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
503 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000504 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000505 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000506
507 // Only copy scheduled successors. Cut them from old node's successor
508 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000509 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000510 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
511 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000512 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000513 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000514 SUnit *SuccSU = I->getSUnit();
515 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000516 SDep D = *I;
517 D.setSUnit(NewSU);
518 AddPred(SuccSU, D);
519 D.setSUnit(SU);
520 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000521 }
522 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000523 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000524 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000525
526 AvailableQueue->updateNode(SU);
527 AvailableQueue->addNode(NewSU);
528
Evan Cheng1ec79b42007-09-27 07:09:03 +0000529 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000530 return NewSU;
531}
532
Evan Chengb2c42c62009-01-12 03:19:55 +0000533/// InsertCopiesAndMoveSuccs - Insert register copies and move all
534/// scheduled successors of the given SUnit to the last copy.
535void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
536 const TargetRegisterClass *DestRC,
537 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000538 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000539 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000540 CopyFromSU->CopySrcRC = SrcRC;
541 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000542
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000543 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000544 CopyToSU->CopySrcRC = DestRC;
545 CopyToSU->CopyDstRC = SrcRC;
546
547 // Only copy scheduled successors. Cut them from old node's successor
548 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000549 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000550 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
551 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000552 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000553 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000554 SUnit *SuccSU = I->getSUnit();
555 if (SuccSU->isScheduled) {
556 SDep D = *I;
557 D.setSUnit(CopyToSU);
558 AddPred(SuccSU, D);
559 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000560 }
561 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000562 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000563 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000564
Dan Gohman2d170892008-12-09 22:54:47 +0000565 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
566 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000567
568 AvailableQueue->updateNode(SU);
569 AvailableQueue->addNode(CopyFromSU);
570 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000571 Copies.push_back(CopyFromSU);
572 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000573
Evan Chengb2c42c62009-01-12 03:19:55 +0000574 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000575}
576
577/// getPhysicalRegisterVT - Returns the ValueType of the physical register
578/// definition of the specified node.
579/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000580static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000581 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000582 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000583 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000584 unsigned NumRes = TID.getNumDefs();
585 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000586 if (Reg == *ImpDef)
587 break;
588 ++NumRes;
589 }
590 return N->getValueType(NumRes);
591}
592
Evan Chengb8905c42009-03-04 01:41:49 +0000593/// CheckForLiveRegDef - Return true and update live register vector if the
594/// specified register def of the specified SUnit clobbers any "live" registers.
595static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
596 std::vector<SUnit*> &LiveRegDefs,
597 SmallSet<unsigned, 4> &RegAdded,
598 SmallVector<unsigned, 4> &LRegs,
599 const TargetRegisterInfo *TRI) {
600 bool Added = false;
601 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
602 if (RegAdded.insert(Reg)) {
603 LRegs.push_back(Reg);
604 Added = true;
605 }
606 }
607 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
608 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
609 if (RegAdded.insert(*Alias)) {
610 LRegs.push_back(*Alias);
611 Added = true;
612 }
613 }
614 return Added;
615}
616
Evan Cheng5924bf72007-09-25 01:54:36 +0000617/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
618/// scheduling of the given node to satisfy live physical register dependencies.
619/// If the specific node is the last one that's available to schedule, do
620/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000621bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
622 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000623 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000624 return false;
625
Evan Chenge6f92252007-09-27 18:46:06 +0000626 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000627 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000628 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
629 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000630 if (I->isAssignedRegDep())
631 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
632 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000633 }
634
Dan Gohman072734e2008-11-13 23:24:17 +0000635 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000636 if (Node->getOpcode() == ISD::INLINEASM) {
637 // Inline asm can clobber physical defs.
638 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000639 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000640 --NumOps; // Ignore the flag operand.
641
642 for (unsigned i = 2; i != NumOps;) {
643 unsigned Flags =
644 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng2e559232009-03-20 18:03:34 +0000645 unsigned NumVals = (Flags & 0xffff) >> 3;
Evan Chengb8905c42009-03-04 01:41:49 +0000646
647 ++i; // Skip the ID value.
648 if ((Flags & 7) == 2 || (Flags & 7) == 6) {
649 // Check for def of register or earlyclobber register.
650 for (; NumVals; --NumVals, ++i) {
651 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
652 if (TargetRegisterInfo::isPhysicalRegister(Reg))
653 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
654 }
655 } else
656 i += NumVals;
657 }
658 continue;
659 }
660
Dan Gohman072734e2008-11-13 23:24:17 +0000661 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000662 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000663 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000664 if (!TID.ImplicitDefs)
665 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000666 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
667 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000669 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000670}
671
Evan Cheng1ec79b42007-09-27 07:09:03 +0000672
Evan Chengd38c22b2006-05-11 23:55:42 +0000673/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
674/// schedulers.
675void ScheduleDAGRRList::ListScheduleBottomUp() {
676 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000677
678 // Release any predecessors of the special Exit node.
679 ReleasePredecessors(&ExitSU, CurCycle);
680
Evan Chengd38c22b2006-05-11 23:55:42 +0000681 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000682 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000683 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000684 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
685 RootSU->isAvailable = true;
686 AvailableQueue->push(RootSU);
687 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000688
689 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000690 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000691 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000692 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000693 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000694 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000695 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000696 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000697 SUnit *CurSU = AvailableQueue->pop();
698 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000699 SmallVector<unsigned, 4> LRegs;
700 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
701 break;
702 Delayed = true;
703 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000704
705 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
706 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000707 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000708 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000709
710 // All candidates are delayed due to live physical reg dependencies.
711 // Try backtracking, code duplication, or inserting cross class copies
712 // to resolve it.
713 if (Delayed && !CurSU) {
714 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
715 SUnit *TrySU = NotReady[i];
716 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
717
718 // Try unscheduling up to the point where it's safe to schedule
719 // this node.
720 unsigned LiveCycle = CurCycle;
721 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
722 unsigned Reg = LRegs[j];
723 unsigned LCycle = LiveRegCycles[Reg];
724 LiveCycle = std::min(LiveCycle, LCycle);
725 }
726 SUnit *OldSU = Sequence[LiveCycle];
727 if (!WillCreateCycle(TrySU, OldSU)) {
728 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
729 // Force the current node to be scheduled before the node that
730 // requires the physical reg dep.
731 if (OldSU->isAvailable) {
732 OldSU->isAvailable = false;
733 AvailableQueue->remove(OldSU);
734 }
Dan Gohman2d170892008-12-09 22:54:47 +0000735 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
736 /*Reg=*/0, /*isNormalMemory=*/false,
737 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000738 // If one or more successors has been unscheduled, then the current
739 // node is no longer avaialable. Schedule a successor that's now
740 // available instead.
741 if (!TrySU->isAvailable)
742 CurSU = AvailableQueue->pop();
743 else {
744 CurSU = TrySU;
745 TrySU->isPending = false;
746 NotReady.erase(NotReady.begin()+i);
747 }
748 break;
749 }
750 }
751
752 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000753 // Can't backtrack. If it's too expensive to copy the value, then try
754 // duplicate the nodes that produces these "too expensive to copy"
755 // values to break the dependency. In case even that doesn't work,
756 // insert cross class copies.
757 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000758 SUnit *TrySU = NotReady[0];
759 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
760 assert(LRegs.size() == 1 && "Can't handle this yet!");
761 unsigned Reg = LRegs[0];
762 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000763 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000764 const TargetRegisterClass *RC =
765 TRI->getPhysicalRegisterRegClass(Reg, VT);
766 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
767
768 // If cross copy register class is null, then it must be possible copy
769 // the value directly. Do not try duplicate the def.
770 SUnit *NewDef = 0;
771 if (DestRC)
772 NewDef = CopyAndMoveSuccessors(LRDef);
773 else
774 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000775 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000776 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000777 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000778 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
David Greenef34d7ac2010-01-05 01:24:54 +0000779 DEBUG(dbgs() << "Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000780 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000781 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000782 /*Reg=*/0, /*isNormalMemory=*/false,
783 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000784 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000785 NewDef = Copies.back();
786 }
787
David Greenef34d7ac2010-01-05 01:24:54 +0000788 DEBUG(dbgs() << "Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000789 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000790 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000791 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000792 /*Reg=*/0, /*isNormalMemory=*/false,
793 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000794 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000795 TrySU->isAvailable = false;
796 CurSU = NewDef;
797 }
798
Dan Gohman60d68442009-01-29 19:49:27 +0000799 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000800 }
801
Evan Chengd38c22b2006-05-11 23:55:42 +0000802 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000803 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
804 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000805 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000806 if (NotReady[i]->isAvailable)
807 AvailableQueue->push(NotReady[i]);
808 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000809 NotReady.clear();
810
Dan Gohmanc602dd42008-11-21 00:10:42 +0000811 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000812 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000813 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000814 }
815
Evan Chengd38c22b2006-05-11 23:55:42 +0000816 // Reverse the order if it is bottom up.
817 std::reverse(Sequence.begin(), Sequence.end());
818
Evan Chengd38c22b2006-05-11 23:55:42 +0000819#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000820 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000821#endif
822}
823
824//===----------------------------------------------------------------------===//
825// Top-Down Scheduling
826//===----------------------------------------------------------------------===//
827
828/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000829/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000830void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000831 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000832
Evan Chengd38c22b2006-05-11 23:55:42 +0000833#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000834 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000835 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000836 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000837 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000838 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000839 }
840#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000841 --SuccSU->NumPredsLeft;
842
Dan Gohmanb9543432009-02-10 23:27:53 +0000843 // If all the node's predecessors are scheduled, this node is ready
844 // to be scheduled. Ignore the special ExitSU node.
845 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000846 SuccSU->isAvailable = true;
847 AvailableQueue->push(SuccSU);
848 }
849}
850
Dan Gohmanb9543432009-02-10 23:27:53 +0000851void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
852 // Top down: release successors
853 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
854 I != E; ++I) {
855 assert(!I->isAssignedRegDep() &&
856 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
857
858 ReleaseSucc(SU, &*I);
859 }
860}
861
Evan Chengd38c22b2006-05-11 23:55:42 +0000862/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
863/// count of its successors. If a successor pending count is zero, add it to
864/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000865void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000866 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000867 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000868
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000869 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
870 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000871 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000872
Dan Gohmanb9543432009-02-10 23:27:53 +0000873 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000874 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000875 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000876}
877
Dan Gohman54a187e2007-08-20 19:28:38 +0000878/// ListScheduleTopDown - The main loop of list scheduling for top-down
879/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000880void ScheduleDAGRRList::ListScheduleTopDown() {
881 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000882
Dan Gohmanb9543432009-02-10 23:27:53 +0000883 // Release any successors of the special Entry node.
884 ReleaseSuccessors(&EntrySU);
885
Evan Chengd38c22b2006-05-11 23:55:42 +0000886 // All leaves to Available queue.
887 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
888 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000889 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000890 AvailableQueue->push(&SUnits[i]);
891 SUnits[i].isAvailable = true;
892 }
893 }
894
Evan Chengd38c22b2006-05-11 23:55:42 +0000895 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000896 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000897 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000898 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000899 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000900
Dan Gohmanc602dd42008-11-21 00:10:42 +0000901 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000902 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000903 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000904 }
905
Evan Chengd38c22b2006-05-11 23:55:42 +0000906#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000907 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000908#endif
909}
910
911
Evan Chengd38c22b2006-05-11 23:55:42 +0000912//===----------------------------------------------------------------------===//
913// RegReductionPriorityQueue Implementation
914//===----------------------------------------------------------------------===//
915//
916// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
917// to reduce register pressure.
918//
919namespace {
920 template<class SF>
921 class RegReductionPriorityQueue;
922
923 /// Sorting functions for the Available queue.
924 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
925 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
926 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
927 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
928
929 bool operator()(const SUnit* left, const SUnit* right) const;
930 };
931
932 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
933 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
934 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
935 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
936
937 bool operator()(const SUnit* left, const SUnit* right) const;
938 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000939
940 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
941 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
942 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
943 : SPQ(spq) {}
944 src_ls_rr_sort(const src_ls_rr_sort &RHS)
945 : SPQ(RHS.SPQ) {}
946
947 bool operator()(const SUnit* left, const SUnit* right) const;
948 };
Evan Chengd38c22b2006-05-11 23:55:42 +0000949} // end anonymous namespace
950
Dan Gohman186f65d2008-11-20 03:30:37 +0000951/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
952/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000953static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000954CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000955 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
956 if (SethiUllmanNumber != 0)
957 return SethiUllmanNumber;
958
959 unsigned Extra = 0;
960 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
961 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000962 if (I->isCtrl()) continue; // ignore chain preds
963 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000964 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000965 if (PredSethiUllman > SethiUllmanNumber) {
966 SethiUllmanNumber = PredSethiUllman;
967 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +0000968 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +0000969 ++Extra;
970 }
971
972 SethiUllmanNumber += Extra;
973
974 if (SethiUllmanNumber == 0)
975 SethiUllmanNumber = 1;
976
977 return SethiUllmanNumber;
978}
979
Evan Chengd38c22b2006-05-11 23:55:42 +0000980namespace {
981 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +0000982 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000983 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000984 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000985
Dan Gohman3f656df2008-11-20 02:45:51 +0000986 protected:
987 // SUnits - The SUnits for the current graph.
988 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000989
Dan Gohman3f656df2008-11-20 02:45:51 +0000990 const TargetInstrInfo *TII;
991 const TargetRegisterInfo *TRI;
992 ScheduleDAGRRList *scheduleDAG;
993
Dan Gohman186f65d2008-11-20 03:30:37 +0000994 // SethiUllmanNumbers - The SethiUllman number for each node.
995 std::vector<unsigned> SethiUllmanNumbers;
996
Dan Gohman3f656df2008-11-20 02:45:51 +0000997 public:
998 RegReductionPriorityQueue(const TargetInstrInfo *tii,
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000999 const TargetRegisterInfo *tri)
1000 : Queue(SF(this)), currentQueueId(0),
1001 TII(tii), TRI(tri), scheduleDAG(NULL) {}
Dan Gohman3f656df2008-11-20 02:45:51 +00001002
1003 void initNodes(std::vector<SUnit> &sunits) {
1004 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001005 // Add pseudo dependency edges for two-address nodes.
1006 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001007 // Reroute edges to nodes with multiple uses.
1008 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001009 // Calculate node priorities.
1010 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001011 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001012
Dan Gohman186f65d2008-11-20 03:30:37 +00001013 void addNode(const SUnit *SU) {
1014 unsigned SUSize = SethiUllmanNumbers.size();
1015 if (SUnits->size() > SUSize)
1016 SethiUllmanNumbers.resize(SUSize*2, 0);
1017 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1018 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001019
Dan Gohman186f65d2008-11-20 03:30:37 +00001020 void updateNode(const SUnit *SU) {
1021 SethiUllmanNumbers[SU->NodeNum] = 0;
1022 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1023 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001024
Dan Gohman186f65d2008-11-20 03:30:37 +00001025 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001026 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001027 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001028 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001029
1030 unsigned getNodePriority(const SUnit *SU) const {
1031 assert(SU->NodeNum < SethiUllmanNumbers.size());
1032 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001033 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001034 // CopyToReg should be close to its uses to facilitate coalescing and
1035 // avoid spilling.
1036 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001037 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Dan Gohman3027bb62009-04-16 20:57:10 +00001038 Opc == TargetInstrInfo::SUBREG_TO_REG ||
Dan Gohman261ee6b2009-01-07 22:30:55 +00001039 Opc == TargetInstrInfo::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001040 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1041 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001042 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001043 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1044 // If SU does not have a register use, i.e. it doesn't produce a value
1045 // that would be consumed (e.g. store), then it terminates a chain of
1046 // computation. Give it a large SethiUllman number so it will be
1047 // scheduled right before its predecessors that it doesn't lengthen
1048 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001049 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001050 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1051 // If SU does not have a register def, schedule it close to its uses
1052 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001053 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001054 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001055 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001056
1057 unsigned getNodeOrdering(const SUnit *SU) const {
1058 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1059 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001060
Evan Cheng5924bf72007-09-25 01:54:36 +00001061 unsigned size() const { return Queue.size(); }
1062
Evan Chengd38c22b2006-05-11 23:55:42 +00001063 bool empty() const { return Queue.empty(); }
1064
1065 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001066 assert(!U->NodeQueueId && "Node in the queue already");
1067 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001068 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001069 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001070
Evan Chengd38c22b2006-05-11 23:55:42 +00001071 void push_all(const std::vector<SUnit *> &Nodes) {
1072 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001073 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001074 }
1075
1076 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001077 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001078 SUnit *V = Queue.top();
1079 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001080 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001081 return V;
1082 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001083
Evan Cheng5924bf72007-09-25 01:54:36 +00001084 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001085 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001086 assert(SU->NodeQueueId != 0 && "Not in queue!");
1087 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001088 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001089 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001090
1091 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1092 scheduleDAG = scheduleDag;
1093 }
1094
1095 protected:
1096 bool canClobber(const SUnit *SU, const SUnit *Op);
1097 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001098 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001099 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001100 };
1101
Dan Gohman186f65d2008-11-20 03:30:37 +00001102 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1103 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001104
Dan Gohman186f65d2008-11-20 03:30:37 +00001105 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1106 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001107
1108 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1109 SrcRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001110}
1111
Evan Chengb9e3db62007-03-14 22:43:40 +00001112/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001113/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001114static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001115 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001116 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001117 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001118 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001119 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001120 // If there are bunch of CopyToRegs stacked up, they should be considered
1121 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001122 if (I->getSUnit()->getNode() &&
1123 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001124 Height = closestSucc(I->getSUnit())+1;
1125 if (Height > MaxHeight)
1126 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001127 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001128 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001129}
1130
Evan Cheng61bc51e2007-12-20 02:22:36 +00001131/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001132/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001133static unsigned calcMaxScratches(const SUnit *SU) {
1134 unsigned Scratches = 0;
1135 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001136 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001137 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001138 Scratches++;
1139 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001140 return Scratches;
1141}
1142
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001143template <typename RRSort>
1144static bool BURRSort(const SUnit *left, const SUnit *right,
1145 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001146 unsigned LPriority = SPQ->getNodePriority(left);
1147 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001148 if (LPriority != RPriority)
1149 return LPriority > RPriority;
1150
1151 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1152 // e.g.
1153 // t1 = op t2, c1
1154 // t3 = op t4, c2
1155 //
1156 // and the following instructions are both ready.
1157 // t2 = op c3
1158 // t4 = op c4
1159 //
1160 // Then schedule t2 = op first.
1161 // i.e.
1162 // t4 = op c4
1163 // t2 = op c3
1164 // t1 = op t2, c1
1165 // t3 = op t4, c2
1166 //
1167 // This creates more short live intervals.
1168 unsigned LDist = closestSucc(left);
1169 unsigned RDist = closestSucc(right);
1170 if (LDist != RDist)
1171 return LDist < RDist;
1172
Evan Cheng3a14efa2009-02-12 08:59:45 +00001173 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001174 unsigned LScratch = calcMaxScratches(left);
1175 unsigned RScratch = calcMaxScratches(right);
1176 if (LScratch != RScratch)
1177 return LScratch > RScratch;
1178
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001179 if (left->getHeight() != right->getHeight())
1180 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001181
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001182 if (left->getDepth() != right->getDepth())
1183 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001184
Roman Levenstein6b371142008-04-29 09:07:59 +00001185 assert(left->NodeQueueId && right->NodeQueueId &&
1186 "NodeQueueId cannot be zero");
1187 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001188}
1189
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001190// Bottom up
1191bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1192 return BURRSort(left, right, SPQ);
1193}
1194
1195// Source order, otherwise bottom up.
1196bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
1197 unsigned LOrder = SPQ->getNodeOrdering(left);
1198 unsigned ROrder = SPQ->getNodeOrdering(right);
1199
1200 // Prefer an ordering where the lower the non-zero order number, the higher
1201 // the preference.
1202 if ((LOrder || ROrder) && LOrder != ROrder)
1203 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1204
1205 return BURRSort(left, right, SPQ);
1206}
1207
Dan Gohman3f656df2008-11-20 02:45:51 +00001208template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001209bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001210RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001211 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001212 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001213 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001214 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001215 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001216 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001217 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001218 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001219 if (DU->getNodeId() != -1 &&
1220 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001221 return true;
1222 }
1223 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001224 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001225 return false;
1226}
1227
Evan Chenga5e595d2007-09-28 22:32:30 +00001228/// hasCopyToRegUse - Return true if SU has a value successor that is a
1229/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001230static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001231 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1232 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001233 if (I->isCtrl()) continue;
1234 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001235 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001236 return true;
1237 }
1238 return false;
1239}
1240
Evan Chengf9891412007-12-20 09:25:31 +00001241/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001242/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001243static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001244 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001245 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001246 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001247 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1248 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001249 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001250 for (const SDNode *SUNode = SU->getNode(); SUNode;
1251 SUNode = SUNode->getFlaggedNode()) {
1252 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001253 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001254 const unsigned *SUImpDefs =
1255 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1256 if (!SUImpDefs)
1257 return false;
1258 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001259 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001260 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001261 continue;
1262 if (!N->hasAnyUseOfValue(i))
1263 continue;
1264 unsigned Reg = ImpDefs[i - NumDefs];
1265 for (;*SUImpDefs; ++SUImpDefs) {
1266 unsigned SUReg = *SUImpDefs;
1267 if (TRI->regsOverlap(Reg, SUReg))
1268 return true;
1269 }
Evan Chengf9891412007-12-20 09:25:31 +00001270 }
1271 }
1272 return false;
1273}
1274
Dan Gohman9a658d72009-03-24 00:49:12 +00001275/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1276/// are not handled well by the general register pressure reduction
1277/// heuristics. When presented with code like this:
1278///
1279/// N
1280/// / |
1281/// / |
1282/// U store
1283/// |
1284/// ...
1285///
1286/// the heuristics tend to push the store up, but since the
1287/// operand of the store has another use (U), this would increase
1288/// the length of that other use (the U->N edge).
1289///
1290/// This function transforms code like the above to route U's
1291/// dependence through the store when possible, like this:
1292///
1293/// N
1294/// ||
1295/// ||
1296/// store
1297/// |
1298/// U
1299/// |
1300/// ...
1301///
1302/// This results in the store being scheduled immediately
1303/// after N, which shortens the U->N live range, reducing
1304/// register pressure.
1305///
1306template<class SF>
1307void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1308 // Visit all the nodes in topological order, working top-down.
1309 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1310 SUnit *SU = &(*SUnits)[i];
1311 // For now, only look at nodes with no data successors, such as stores.
1312 // These are especially important, due to the heuristics in
1313 // getNodePriority for nodes with no data successors.
1314 if (SU->NumSuccs != 0)
1315 continue;
1316 // For now, only look at nodes with exactly one data predecessor.
1317 if (SU->NumPreds != 1)
1318 continue;
1319 // Avoid prescheduling copies to virtual registers, which don't behave
1320 // like other nodes from the perspective of scheduling heuristics.
1321 if (SDNode *N = SU->getNode())
1322 if (N->getOpcode() == ISD::CopyToReg &&
1323 TargetRegisterInfo::isVirtualRegister
1324 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1325 continue;
1326
1327 // Locate the single data predecessor.
1328 SUnit *PredSU = 0;
1329 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1330 EE = SU->Preds.end(); II != EE; ++II)
1331 if (!II->isCtrl()) {
1332 PredSU = II->getSUnit();
1333 break;
1334 }
1335 assert(PredSU);
1336
1337 // Don't rewrite edges that carry physregs, because that requires additional
1338 // support infrastructure.
1339 if (PredSU->hasPhysRegDefs)
1340 continue;
1341 // Short-circuit the case where SU is PredSU's only data successor.
1342 if (PredSU->NumSuccs == 1)
1343 continue;
1344 // Avoid prescheduling to copies from virtual registers, which don't behave
1345 // like other nodes from the perspective of scheduling // heuristics.
1346 if (SDNode *N = SU->getNode())
1347 if (N->getOpcode() == ISD::CopyFromReg &&
1348 TargetRegisterInfo::isVirtualRegister
1349 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1350 continue;
1351
1352 // Perform checks on the successors of PredSU.
1353 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1354 EE = PredSU->Succs.end(); II != EE; ++II) {
1355 SUnit *PredSuccSU = II->getSUnit();
1356 if (PredSuccSU == SU) continue;
1357 // If PredSU has another successor with no data successors, for
1358 // now don't attempt to choose either over the other.
1359 if (PredSuccSU->NumSuccs == 0)
1360 goto outer_loop_continue;
1361 // Don't break physical register dependencies.
1362 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1363 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1364 goto outer_loop_continue;
1365 // Don't introduce graph cycles.
1366 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1367 goto outer_loop_continue;
1368 }
1369
1370 // Ok, the transformation is safe and the heuristics suggest it is
1371 // profitable. Update the graph.
David Greenef34d7ac2010-01-05 01:24:54 +00001372 DEBUG(dbgs() << "Prescheduling SU # " << SU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001373 << " next to PredSU # " << PredSU->NodeNum
1374 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001375 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1376 SDep Edge = PredSU->Succs[i];
1377 assert(!Edge.isAssignedRegDep());
1378 SUnit *SuccSU = Edge.getSUnit();
1379 if (SuccSU != SU) {
1380 Edge.setSUnit(PredSU);
1381 scheduleDAG->RemovePred(SuccSU, Edge);
1382 scheduleDAG->AddPred(SU, Edge);
1383 Edge.setSUnit(SU);
1384 scheduleDAG->AddPred(SuccSU, Edge);
1385 --i;
1386 }
1387 }
1388 outer_loop_continue:;
1389 }
1390}
1391
Evan Chengd38c22b2006-05-11 23:55:42 +00001392/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1393/// it as a def&use operand. Add a pseudo control edge from it to the other
1394/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001395/// first (lower in the schedule). If both nodes are two-address, favor the
1396/// one that has a CopyToReg use (more likely to be a loop induction update).
1397/// If both are two-address, but one is commutable while the other is not
1398/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001399template<class SF>
1400void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001401 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001402 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001403 if (!SU->isTwoAddress)
1404 continue;
1405
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001406 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001407 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001408 continue;
1409
Dan Gohman17059682008-07-17 19:10:17 +00001410 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001411 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001412 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001413 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001414 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001415 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1416 continue;
1417 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1418 if (DU->getNodeId() == -1)
1419 continue;
1420 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1421 if (!DUSU) continue;
1422 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1423 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001424 if (I->isCtrl()) continue;
1425 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001426 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001427 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001428 // Be conservative. Ignore if nodes aren't at roughly the same
1429 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001430 if (SuccSU->getHeight() < SU->getHeight() &&
1431 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001432 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001433 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1434 // constrains whatever is using the copy, instead of the copy
1435 // itself. In the case that the copy is coalesced, this
1436 // preserves the intent of the pseudo two-address heurietics.
1437 while (SuccSU->Succs.size() == 1 &&
1438 SuccSU->getNode()->isMachineOpcode() &&
1439 SuccSU->getNode()->getMachineOpcode() ==
1440 TargetInstrInfo::COPY_TO_REGCLASS)
1441 SuccSU = SuccSU->Succs.front().getSUnit();
1442 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001443 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1444 continue;
1445 // Don't constrain nodes with physical register defs if the
1446 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001447 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001448 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001449 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001450 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001451 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1452 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001453 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
1454 if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
Dan Gohman3027bb62009-04-16 20:57:10 +00001455 SuccOpc == TargetInstrInfo::INSERT_SUBREG ||
1456 SuccOpc == TargetInstrInfo::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001457 continue;
1458 if ((!canClobber(SuccSU, DUSU) ||
1459 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1460 (!SU->isCommutable && SuccSU->isCommutable)) &&
1461 !scheduleDAG->IsReachable(SuccSU, SU)) {
David Greenef34d7ac2010-01-05 01:24:54 +00001462 DEBUG(dbgs() << "Adding a pseudo-two-addr edge from SU # "
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001463 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001464 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001465 /*Reg=*/0, /*isNormalMemory=*/false,
1466 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001467 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001468 }
1469 }
1470 }
1471 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001472}
1473
Evan Cheng6730f032007-01-08 23:55:53 +00001474/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1475/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001476template<class SF>
1477void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001478 SethiUllmanNumbers.assign(SUnits->size(), 0);
1479
1480 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001481 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001482}
Evan Chengd38c22b2006-05-11 23:55:42 +00001483
Roman Levenstein30d09512008-03-27 09:44:37 +00001484/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001485/// predecessors of the successors of the SUnit SU. Stop when the provided
1486/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001487static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1488 unsigned Limit) {
1489 unsigned Sum = 0;
1490 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1491 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001492 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001493 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1494 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001495 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001496 if (!PredSU->isScheduled)
1497 if (++Sum > Limit)
1498 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001499 }
1500 }
1501 return Sum;
1502}
1503
Evan Chengd38c22b2006-05-11 23:55:42 +00001504
1505// Top down
1506bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001507 unsigned LPriority = SPQ->getNodePriority(left);
1508 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001509 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1510 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001511 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1512 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001513 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1514 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001515
1516 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1517 return false;
1518 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1519 return true;
1520
Evan Chengd38c22b2006-05-11 23:55:42 +00001521 if (LIsFloater)
1522 LBonus -= 2;
1523 if (RIsFloater)
1524 RBonus -= 2;
1525 if (left->NumSuccs == 1)
1526 LBonus += 2;
1527 if (right->NumSuccs == 1)
1528 RBonus += 2;
1529
Evan Cheng73bdf042008-03-01 00:39:47 +00001530 if (LPriority+LBonus != RPriority+RBonus)
1531 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001532
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001533 if (left->getDepth() != right->getDepth())
1534 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001535
1536 if (left->NumSuccsLeft != right->NumSuccsLeft)
1537 return left->NumSuccsLeft > right->NumSuccsLeft;
1538
Roman Levenstein6b371142008-04-29 09:07:59 +00001539 assert(left->NodeQueueId && right->NodeQueueId &&
1540 "NodeQueueId cannot be zero");
1541 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001542}
1543
Evan Chengd38c22b2006-05-11 23:55:42 +00001544//===----------------------------------------------------------------------===//
1545// Public Constructor Functions
1546//===----------------------------------------------------------------------===//
1547
Dan Gohmandfaf6462009-02-11 04:27:20 +00001548llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001549llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001550 const TargetMachine &TM = IS->TM;
1551 const TargetInstrInfo *TII = TM.getInstrInfo();
1552 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001553
Evan Cheng7e4abde2008-07-02 09:23:51 +00001554 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001555
Evan Cheng7e4abde2008-07-02 09:23:51 +00001556 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001557 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001558 PQ->setScheduleDAG(SD);
1559 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001560}
1561
Dan Gohmandfaf6462009-02-11 04:27:20 +00001562llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001563llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001564 const TargetMachine &TM = IS->TM;
1565 const TargetInstrInfo *TII = TM.getInstrInfo();
1566 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001567
1568 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1569
Dan Gohman619ef482009-01-15 19:20:50 +00001570 ScheduleDAGRRList *SD =
1571 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001572 PQ->setScheduleDAG(SD);
1573 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001574}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001575
1576llvm::ScheduleDAGSDNodes *
1577llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1578 const TargetMachine &TM = IS->TM;
1579 const TargetInstrInfo *TII = TM.getInstrInfo();
1580 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1581
1582 SrcRegReductionPriorityQueue *PQ = new SrcRegReductionPriorityQueue(TII, TRI);
1583
1584 ScheduleDAGRRList *SD =
1585 new ScheduleDAGRRList(*IS->MF, true, PQ);
1586 PQ->setScheduleDAG(SD);
1587 return SD;
1588}