blob: 3f1766d9e9f2be802252a81b3e1a46b18b09bf95 [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 Cheng3b245872010-02-05 01:27:11 +0000348static bool isOperandOf(const SUnit *SU, SDNode *N) {
349 for (const SDNode *SUNode = SU->getNode(); SUNode;
350 SUNode = SUNode->getFlaggedNode()) {
351 if (SUNode->isOperandOf(N))
352 return true;
353 }
354 return false;
355}
356
Evan Cheng5924bf72007-09-25 01:54:36 +0000357/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
358/// successors to the newly created node.
359SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000360 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000361 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000362
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000363 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000364 if (!N)
365 return NULL;
366
367 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000368 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000369 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000370 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000371 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000372 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000373 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000374 TryUnfold = true;
375 }
Evan Cheng79e97132007-10-05 01:39:18 +0000376 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000377 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000378 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000379 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000380 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000381 }
382
383 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000384 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000385 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000386 return NULL;
387
David Greenef34d7ac2010-01-05 01:24:54 +0000388 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000389 assert(NewNodes.size() == 2 && "Expected a load folding node!");
390
391 N = NewNodes[1];
392 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000393 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000394 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000395 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000396 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
397 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000398 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000399
Dan Gohmane52e0892008-11-11 21:34:44 +0000400 // LoadNode may already exist. This can happen when there is another
401 // load from the same location and producing the same type of value
402 // but it has different alignment or volatileness.
403 bool isNewLoad = true;
404 SUnit *LoadSU;
405 if (LoadNode->getNodeId() != -1) {
406 LoadSU = &SUnits[LoadNode->getNodeId()];
407 isNewLoad = false;
408 } else {
409 LoadSU = CreateNewSUnit(LoadNode);
410 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000411 ComputeLatency(LoadSU);
412 }
413
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000414 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000415 assert(N->getNodeId() == -1 && "Node already inserted!");
416 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000417
Dan Gohman17059682008-07-17 19:10:17 +0000418 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000419 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000420 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000421 NewSU->isTwoAddress = true;
422 break;
423 }
424 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000425 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000426 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000427 ComputeLatency(NewSU);
428
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000429 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000430 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000431 SmallVector<SDep, 4> ChainSuccs;
432 SmallVector<SDep, 4> LoadPreds;
433 SmallVector<SDep, 4> NodePreds;
434 SmallVector<SDep, 4> NodeSuccs;
435 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
436 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000437 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000438 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000439 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000440 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000441 else
Dan Gohman2d170892008-12-09 22:54:47 +0000442 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000443 }
444 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
445 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000446 if (I->isCtrl())
447 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000448 else
Dan Gohman2d170892008-12-09 22:54:47 +0000449 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000450 }
451
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000452 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000453 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
454 const SDep &Pred = ChainPreds[i];
455 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000456 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000457 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000458 }
Evan Cheng79e97132007-10-05 01:39:18 +0000459 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000460 const SDep &Pred = LoadPreds[i];
461 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000462 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000463 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000464 }
465 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000466 const SDep &Pred = NodePreds[i];
467 RemovePred(SU, Pred);
468 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000469 }
470 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000471 SDep D = NodeSuccs[i];
472 SUnit *SuccDep = D.getSUnit();
473 D.setSUnit(SU);
474 RemovePred(SuccDep, D);
475 D.setSUnit(NewSU);
476 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000477 }
478 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000479 SDep D = ChainSuccs[i];
480 SUnit *SuccDep = D.getSUnit();
481 D.setSUnit(SU);
482 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000483 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000484 D.setSUnit(LoadSU);
485 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000486 }
Evan Cheng79e97132007-10-05 01:39:18 +0000487 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000488
489 // Add a data dependency to reflect that NewSU reads the value defined
490 // by LoadSU.
491 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000492
Evan Cheng91e0fc92007-12-18 08:42:10 +0000493 if (isNewLoad)
494 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000495 AvailableQueue->addNode(NewSU);
496
497 ++NumUnfolds;
498
499 if (NewSU->NumSuccsLeft == 0) {
500 NewSU->isAvailable = true;
501 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000502 }
503 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000504 }
505
David Greenef34d7ac2010-01-05 01:24:54 +0000506 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000507 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000508
509 // New SUnit has the exact same predecessors.
510 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
511 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000512 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000513 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000514
515 // Only copy scheduled successors. Cut them from old node's successor
516 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000517 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000518 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
519 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000520 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000521 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000522 SUnit *SuccSU = I->getSUnit();
523 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000524 SDep D = *I;
525 D.setSUnit(NewSU);
526 AddPred(SuccSU, D);
527 D.setSUnit(SU);
528 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000529 }
530 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000531 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000532 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000533
534 AvailableQueue->updateNode(SU);
535 AvailableQueue->addNode(NewSU);
536
Evan Cheng1ec79b42007-09-27 07:09:03 +0000537 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000538 return NewSU;
539}
540
Evan Chengb2c42c62009-01-12 03:19:55 +0000541/// InsertCopiesAndMoveSuccs - Insert register copies and move all
542/// scheduled successors of the given SUnit to the last copy.
543void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
544 const TargetRegisterClass *DestRC,
545 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000546 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000547 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000548 CopyFromSU->CopySrcRC = SrcRC;
549 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000550
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000551 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000552 CopyToSU->CopySrcRC = DestRC;
553 CopyToSU->CopyDstRC = SrcRC;
554
555 // Only copy scheduled successors. Cut them from old node's successor
556 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000557 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000558 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
559 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000560 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000561 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000562 SUnit *SuccSU = I->getSUnit();
563 if (SuccSU->isScheduled) {
564 SDep D = *I;
565 D.setSUnit(CopyToSU);
566 AddPred(SuccSU, D);
567 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000568 }
569 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000570 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000571 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000572
Dan Gohman2d170892008-12-09 22:54:47 +0000573 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
574 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000575
576 AvailableQueue->updateNode(SU);
577 AvailableQueue->addNode(CopyFromSU);
578 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000579 Copies.push_back(CopyFromSU);
580 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000581
Evan Chengb2c42c62009-01-12 03:19:55 +0000582 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000583}
584
585/// getPhysicalRegisterVT - Returns the ValueType of the physical register
586/// definition of the specified node.
587/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000588static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000589 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000590 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000591 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000592 unsigned NumRes = TID.getNumDefs();
593 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000594 if (Reg == *ImpDef)
595 break;
596 ++NumRes;
597 }
598 return N->getValueType(NumRes);
599}
600
Evan Chengb8905c42009-03-04 01:41:49 +0000601/// CheckForLiveRegDef - Return true and update live register vector if the
602/// specified register def of the specified SUnit clobbers any "live" registers.
603static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
604 std::vector<SUnit*> &LiveRegDefs,
605 SmallSet<unsigned, 4> &RegAdded,
606 SmallVector<unsigned, 4> &LRegs,
607 const TargetRegisterInfo *TRI) {
608 bool Added = false;
609 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
610 if (RegAdded.insert(Reg)) {
611 LRegs.push_back(Reg);
612 Added = true;
613 }
614 }
615 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
616 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
617 if (RegAdded.insert(*Alias)) {
618 LRegs.push_back(*Alias);
619 Added = true;
620 }
621 }
622 return Added;
623}
624
Evan Cheng5924bf72007-09-25 01:54:36 +0000625/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
626/// scheduling of the given node to satisfy live physical register dependencies.
627/// If the specific node is the last one that's available to schedule, do
628/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000629bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
630 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000631 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000632 return false;
633
Evan Chenge6f92252007-09-27 18:46:06 +0000634 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000635 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000636 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
637 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000638 if (I->isAssignedRegDep())
639 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
640 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000641 }
642
Dan Gohman072734e2008-11-13 23:24:17 +0000643 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000644 if (Node->getOpcode() == ISD::INLINEASM) {
645 // Inline asm can clobber physical defs.
646 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000647 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000648 --NumOps; // Ignore the flag operand.
649
650 for (unsigned i = 2; i != NumOps;) {
651 unsigned Flags =
652 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng2e559232009-03-20 18:03:34 +0000653 unsigned NumVals = (Flags & 0xffff) >> 3;
Evan Chengb8905c42009-03-04 01:41:49 +0000654
655 ++i; // Skip the ID value.
656 if ((Flags & 7) == 2 || (Flags & 7) == 6) {
657 // Check for def of register or earlyclobber register.
658 for (; NumVals; --NumVals, ++i) {
659 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
660 if (TargetRegisterInfo::isPhysicalRegister(Reg))
661 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
662 }
663 } else
664 i += NumVals;
665 }
666 continue;
667 }
668
Dan Gohman072734e2008-11-13 23:24:17 +0000669 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000670 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000671 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000672 if (!TID.ImplicitDefs)
673 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000674 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
675 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000676 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000677 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000678}
679
Evan Cheng1ec79b42007-09-27 07:09:03 +0000680
Evan Chengd38c22b2006-05-11 23:55:42 +0000681/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
682/// schedulers.
683void ScheduleDAGRRList::ListScheduleBottomUp() {
684 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000685
686 // Release any predecessors of the special Exit node.
687 ReleasePredecessors(&ExitSU, CurCycle);
688
Evan Chengd38c22b2006-05-11 23:55:42 +0000689 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000690 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000691 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000692 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
693 RootSU->isAvailable = true;
694 AvailableQueue->push(RootSU);
695 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000696
697 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000698 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000699 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000700 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000701 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000702 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000703 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000704 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000705 SUnit *CurSU = AvailableQueue->pop();
706 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000707 SmallVector<unsigned, 4> LRegs;
708 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
709 break;
710 Delayed = true;
711 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000712
713 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
714 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000715 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000716 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000717
718 // All candidates are delayed due to live physical reg dependencies.
719 // Try backtracking, code duplication, or inserting cross class copies
720 // to resolve it.
721 if (Delayed && !CurSU) {
722 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
723 SUnit *TrySU = NotReady[i];
724 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
725
726 // Try unscheduling up to the point where it's safe to schedule
727 // this node.
728 unsigned LiveCycle = CurCycle;
729 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
730 unsigned Reg = LRegs[j];
731 unsigned LCycle = LiveRegCycles[Reg];
732 LiveCycle = std::min(LiveCycle, LCycle);
733 }
734 SUnit *OldSU = Sequence[LiveCycle];
735 if (!WillCreateCycle(TrySU, OldSU)) {
736 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
737 // Force the current node to be scheduled before the node that
738 // requires the physical reg dep.
739 if (OldSU->isAvailable) {
740 OldSU->isAvailable = false;
741 AvailableQueue->remove(OldSU);
742 }
Dan Gohman2d170892008-12-09 22:54:47 +0000743 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
744 /*Reg=*/0, /*isNormalMemory=*/false,
745 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000746 // If one or more successors has been unscheduled, then the current
747 // node is no longer avaialable. Schedule a successor that's now
748 // available instead.
749 if (!TrySU->isAvailable)
750 CurSU = AvailableQueue->pop();
751 else {
752 CurSU = TrySU;
753 TrySU->isPending = false;
754 NotReady.erase(NotReady.begin()+i);
755 }
756 break;
757 }
758 }
759
760 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000761 // Can't backtrack. If it's too expensive to copy the value, then try
762 // duplicate the nodes that produces these "too expensive to copy"
763 // values to break the dependency. In case even that doesn't work,
764 // insert cross class copies.
765 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000766 SUnit *TrySU = NotReady[0];
767 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
768 assert(LRegs.size() == 1 && "Can't handle this yet!");
769 unsigned Reg = LRegs[0];
770 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000771 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000772 const TargetRegisterClass *RC =
773 TRI->getPhysicalRegisterRegClass(Reg, VT);
774 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
775
776 // If cross copy register class is null, then it must be possible copy
777 // the value directly. Do not try duplicate the def.
778 SUnit *NewDef = 0;
779 if (DestRC)
780 NewDef = CopyAndMoveSuccessors(LRDef);
781 else
782 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000783 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000784 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000785 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000786 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
David Greenef34d7ac2010-01-05 01:24:54 +0000787 DEBUG(dbgs() << "Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000788 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000789 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000790 /*Reg=*/0, /*isNormalMemory=*/false,
791 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000792 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000793 NewDef = Copies.back();
794 }
795
David Greenef34d7ac2010-01-05 01:24:54 +0000796 DEBUG(dbgs() << "Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000797 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000798 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000799 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000800 /*Reg=*/0, /*isNormalMemory=*/false,
801 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000802 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000803 TrySU->isAvailable = false;
804 CurSU = NewDef;
805 }
806
Dan Gohman60d68442009-01-29 19:49:27 +0000807 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000808 }
809
Evan Chengd38c22b2006-05-11 23:55:42 +0000810 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000811 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
812 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000813 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000814 if (NotReady[i]->isAvailable)
815 AvailableQueue->push(NotReady[i]);
816 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000817 NotReady.clear();
818
Dan Gohmanc602dd42008-11-21 00:10:42 +0000819 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000820 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000821 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000822 }
823
Evan Chengd38c22b2006-05-11 23:55:42 +0000824 // Reverse the order if it is bottom up.
825 std::reverse(Sequence.begin(), Sequence.end());
826
Evan Chengd38c22b2006-05-11 23:55:42 +0000827#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000828 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000829#endif
830}
831
832//===----------------------------------------------------------------------===//
833// Top-Down Scheduling
834//===----------------------------------------------------------------------===//
835
836/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000837/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000838void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000839 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000840
Evan Chengd38c22b2006-05-11 23:55:42 +0000841#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000842 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000843 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000844 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000845 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000846 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000847 }
848#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000849 --SuccSU->NumPredsLeft;
850
Dan Gohmanb9543432009-02-10 23:27:53 +0000851 // If all the node's predecessors are scheduled, this node is ready
852 // to be scheduled. Ignore the special ExitSU node.
853 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000854 SuccSU->isAvailable = true;
855 AvailableQueue->push(SuccSU);
856 }
857}
858
Dan Gohmanb9543432009-02-10 23:27:53 +0000859void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
860 // Top down: release successors
861 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
862 I != E; ++I) {
863 assert(!I->isAssignedRegDep() &&
864 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
865
866 ReleaseSucc(SU, &*I);
867 }
868}
869
Evan Chengd38c22b2006-05-11 23:55:42 +0000870/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
871/// count of its successors. If a successor pending count is zero, add it to
872/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000873void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000874 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000875 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000876
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000877 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
878 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000879 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000880
Dan Gohmanb9543432009-02-10 23:27:53 +0000881 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000882 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000883 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000884}
885
Dan Gohman54a187e2007-08-20 19:28:38 +0000886/// ListScheduleTopDown - The main loop of list scheduling for top-down
887/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000888void ScheduleDAGRRList::ListScheduleTopDown() {
889 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000890
Dan Gohmanb9543432009-02-10 23:27:53 +0000891 // Release any successors of the special Entry node.
892 ReleaseSuccessors(&EntrySU);
893
Evan Chengd38c22b2006-05-11 23:55:42 +0000894 // All leaves to Available queue.
895 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
896 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000897 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000898 AvailableQueue->push(&SUnits[i]);
899 SUnits[i].isAvailable = true;
900 }
901 }
902
Evan Chengd38c22b2006-05-11 23:55:42 +0000903 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000904 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000905 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000906 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000907 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000908
Dan Gohmanc602dd42008-11-21 00:10:42 +0000909 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000910 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000911 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000912 }
913
Evan Chengd38c22b2006-05-11 23:55:42 +0000914#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000915 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000916#endif
917}
918
919
Evan Chengd38c22b2006-05-11 23:55:42 +0000920//===----------------------------------------------------------------------===//
921// RegReductionPriorityQueue Implementation
922//===----------------------------------------------------------------------===//
923//
924// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
925// to reduce register pressure.
926//
927namespace {
928 template<class SF>
929 class RegReductionPriorityQueue;
930
931 /// Sorting functions for the Available queue.
932 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
933 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
934 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
935 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
936
937 bool operator()(const SUnit* left, const SUnit* right) const;
938 };
939
940 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
941 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
942 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
943 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
944
945 bool operator()(const SUnit* left, const SUnit* right) const;
946 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000947
948 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
949 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
950 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
951 : SPQ(spq) {}
952 src_ls_rr_sort(const src_ls_rr_sort &RHS)
953 : SPQ(RHS.SPQ) {}
954
955 bool operator()(const SUnit* left, const SUnit* right) const;
956 };
Evan Chengd38c22b2006-05-11 23:55:42 +0000957} // end anonymous namespace
958
Dan Gohman186f65d2008-11-20 03:30:37 +0000959/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
960/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000961static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000962CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000963 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
964 if (SethiUllmanNumber != 0)
965 return SethiUllmanNumber;
966
967 unsigned Extra = 0;
968 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
969 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000970 if (I->isCtrl()) continue; // ignore chain preds
971 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000972 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000973 if (PredSethiUllman > SethiUllmanNumber) {
974 SethiUllmanNumber = PredSethiUllman;
975 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +0000976 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +0000977 ++Extra;
978 }
979
980 SethiUllmanNumber += Extra;
981
982 if (SethiUllmanNumber == 0)
983 SethiUllmanNumber = 1;
984
985 return SethiUllmanNumber;
986}
987
Evan Chengd38c22b2006-05-11 23:55:42 +0000988namespace {
989 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +0000990 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000991 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000992 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000993
Dan Gohman3f656df2008-11-20 02:45:51 +0000994 protected:
995 // SUnits - The SUnits for the current graph.
996 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000997
Dan Gohman3f656df2008-11-20 02:45:51 +0000998 const TargetInstrInfo *TII;
999 const TargetRegisterInfo *TRI;
1000 ScheduleDAGRRList *scheduleDAG;
1001
Dan Gohman186f65d2008-11-20 03:30:37 +00001002 // SethiUllmanNumbers - The SethiUllman number for each node.
1003 std::vector<unsigned> SethiUllmanNumbers;
1004
Dan Gohman3f656df2008-11-20 02:45:51 +00001005 public:
1006 RegReductionPriorityQueue(const TargetInstrInfo *tii,
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001007 const TargetRegisterInfo *tri)
1008 : Queue(SF(this)), currentQueueId(0),
1009 TII(tii), TRI(tri), scheduleDAG(NULL) {}
Dan Gohman3f656df2008-11-20 02:45:51 +00001010
1011 void initNodes(std::vector<SUnit> &sunits) {
1012 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001013 // Add pseudo dependency edges for two-address nodes.
1014 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001015 // Reroute edges to nodes with multiple uses.
1016 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001017 // Calculate node priorities.
1018 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001019 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001020
Dan Gohman186f65d2008-11-20 03:30:37 +00001021 void addNode(const SUnit *SU) {
1022 unsigned SUSize = SethiUllmanNumbers.size();
1023 if (SUnits->size() > SUSize)
1024 SethiUllmanNumbers.resize(SUSize*2, 0);
1025 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1026 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001027
Dan Gohman186f65d2008-11-20 03:30:37 +00001028 void updateNode(const SUnit *SU) {
1029 SethiUllmanNumbers[SU->NodeNum] = 0;
1030 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1031 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001032
Dan Gohman186f65d2008-11-20 03:30:37 +00001033 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001034 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001035 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001036 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001037
1038 unsigned getNodePriority(const SUnit *SU) const {
1039 assert(SU->NodeNum < SethiUllmanNumbers.size());
1040 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001041 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001042 // CopyToReg should be close to its uses to facilitate coalescing and
1043 // avoid spilling.
1044 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001045 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1046 Opc == TargetOpcode::SUBREG_TO_REG ||
1047 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001048 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1049 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001050 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001051 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1052 // If SU does not have a register use, i.e. it doesn't produce a value
1053 // that would be consumed (e.g. store), then it terminates a chain of
1054 // computation. Give it a large SethiUllman number so it will be
1055 // scheduled right before its predecessors that it doesn't lengthen
1056 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001057 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001058 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1059 // If SU does not have a register def, schedule it close to its uses
1060 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001061 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001062 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001063 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001064
1065 unsigned getNodeOrdering(const SUnit *SU) const {
1066 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1067 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001068
Evan Cheng5924bf72007-09-25 01:54:36 +00001069 unsigned size() const { return Queue.size(); }
1070
Evan Chengd38c22b2006-05-11 23:55:42 +00001071 bool empty() const { return Queue.empty(); }
1072
1073 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001074 assert(!U->NodeQueueId && "Node in the queue already");
1075 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001076 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001077 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001078
Evan Chengd38c22b2006-05-11 23:55:42 +00001079 void push_all(const std::vector<SUnit *> &Nodes) {
1080 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001081 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001082 }
1083
1084 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001085 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001086 SUnit *V = Queue.top();
1087 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001088 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001089 return V;
1090 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001091
Evan Cheng5924bf72007-09-25 01:54:36 +00001092 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001093 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001094 assert(SU->NodeQueueId != 0 && "Not in queue!");
1095 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001096 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001097 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001098
1099 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1100 scheduleDAG = scheduleDag;
1101 }
1102
1103 protected:
1104 bool canClobber(const SUnit *SU, const SUnit *Op);
1105 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001106 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001107 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001108 };
1109
Dan Gohman186f65d2008-11-20 03:30:37 +00001110 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1111 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001112
Dan Gohman186f65d2008-11-20 03:30:37 +00001113 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1114 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001115
1116 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1117 SrcRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001118}
1119
Evan Chengb9e3db62007-03-14 22:43:40 +00001120/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001121/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001122static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001123 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001124 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001125 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001126 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001127 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001128 // If there are bunch of CopyToRegs stacked up, they should be considered
1129 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001130 if (I->getSUnit()->getNode() &&
1131 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001132 Height = closestSucc(I->getSUnit())+1;
1133 if (Height > MaxHeight)
1134 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001135 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001136 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001137}
1138
Evan Cheng61bc51e2007-12-20 02:22:36 +00001139/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001140/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001141static unsigned calcMaxScratches(const SUnit *SU) {
1142 unsigned Scratches = 0;
1143 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001144 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001145 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001146 Scratches++;
1147 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001148 return Scratches;
1149}
1150
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001151template <typename RRSort>
1152static bool BURRSort(const SUnit *left, const SUnit *right,
1153 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001154 unsigned LPriority = SPQ->getNodePriority(left);
1155 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001156 if (LPriority != RPriority)
1157 return LPriority > RPriority;
1158
1159 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1160 // e.g.
1161 // t1 = op t2, c1
1162 // t3 = op t4, c2
1163 //
1164 // and the following instructions are both ready.
1165 // t2 = op c3
1166 // t4 = op c4
1167 //
1168 // Then schedule t2 = op first.
1169 // i.e.
1170 // t4 = op c4
1171 // t2 = op c3
1172 // t1 = op t2, c1
1173 // t3 = op t4, c2
1174 //
1175 // This creates more short live intervals.
1176 unsigned LDist = closestSucc(left);
1177 unsigned RDist = closestSucc(right);
1178 if (LDist != RDist)
1179 return LDist < RDist;
1180
Evan Cheng3a14efa2009-02-12 08:59:45 +00001181 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001182 unsigned LScratch = calcMaxScratches(left);
1183 unsigned RScratch = calcMaxScratches(right);
1184 if (LScratch != RScratch)
1185 return LScratch > RScratch;
1186
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001187 if (left->getHeight() != right->getHeight())
1188 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001189
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001190 if (left->getDepth() != right->getDepth())
1191 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001192
Roman Levenstein6b371142008-04-29 09:07:59 +00001193 assert(left->NodeQueueId && right->NodeQueueId &&
1194 "NodeQueueId cannot be zero");
1195 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001196}
1197
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001198// Bottom up
1199bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1200 return BURRSort(left, right, SPQ);
1201}
1202
1203// Source order, otherwise bottom up.
1204bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
1205 unsigned LOrder = SPQ->getNodeOrdering(left);
1206 unsigned ROrder = SPQ->getNodeOrdering(right);
1207
1208 // Prefer an ordering where the lower the non-zero order number, the higher
1209 // the preference.
1210 if ((LOrder || ROrder) && LOrder != ROrder)
1211 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1212
1213 return BURRSort(left, right, SPQ);
1214}
1215
Dan Gohman3f656df2008-11-20 02:45:51 +00001216template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001217bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001218RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001219 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001220 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001221 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001222 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001223 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001224 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001225 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001226 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001227 if (DU->getNodeId() != -1 &&
1228 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001229 return true;
1230 }
1231 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001232 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001233 return false;
1234}
1235
Evan Chenga5e595d2007-09-28 22:32:30 +00001236/// hasCopyToRegUse - Return true if SU has a value successor that is a
1237/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001238static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001239 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1240 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001241 if (I->isCtrl()) continue;
1242 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001243 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001244 return true;
1245 }
1246 return false;
1247}
1248
Evan Chengf9891412007-12-20 09:25:31 +00001249/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001250/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001251static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001252 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001253 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001254 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001255 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1256 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001257 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001258 for (const SDNode *SUNode = SU->getNode(); SUNode;
1259 SUNode = SUNode->getFlaggedNode()) {
1260 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001261 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001262 const unsigned *SUImpDefs =
1263 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1264 if (!SUImpDefs)
1265 return false;
1266 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001267 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001268 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001269 continue;
1270 if (!N->hasAnyUseOfValue(i))
1271 continue;
1272 unsigned Reg = ImpDefs[i - NumDefs];
1273 for (;*SUImpDefs; ++SUImpDefs) {
1274 unsigned SUReg = *SUImpDefs;
1275 if (TRI->regsOverlap(Reg, SUReg))
1276 return true;
1277 }
Evan Chengf9891412007-12-20 09:25:31 +00001278 }
1279 }
1280 return false;
1281}
1282
Dan Gohman9a658d72009-03-24 00:49:12 +00001283/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1284/// are not handled well by the general register pressure reduction
1285/// heuristics. When presented with code like this:
1286///
1287/// N
1288/// / |
1289/// / |
1290/// U store
1291/// |
1292/// ...
1293///
1294/// the heuristics tend to push the store up, but since the
1295/// operand of the store has another use (U), this would increase
1296/// the length of that other use (the U->N edge).
1297///
1298/// This function transforms code like the above to route U's
1299/// dependence through the store when possible, like this:
1300///
1301/// N
1302/// ||
1303/// ||
1304/// store
1305/// |
1306/// U
1307/// |
1308/// ...
1309///
1310/// This results in the store being scheduled immediately
1311/// after N, which shortens the U->N live range, reducing
1312/// register pressure.
1313///
1314template<class SF>
1315void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1316 // Visit all the nodes in topological order, working top-down.
1317 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1318 SUnit *SU = &(*SUnits)[i];
1319 // For now, only look at nodes with no data successors, such as stores.
1320 // These are especially important, due to the heuristics in
1321 // getNodePriority for nodes with no data successors.
1322 if (SU->NumSuccs != 0)
1323 continue;
1324 // For now, only look at nodes with exactly one data predecessor.
1325 if (SU->NumPreds != 1)
1326 continue;
1327 // Avoid prescheduling copies to virtual registers, which don't behave
1328 // like other nodes from the perspective of scheduling heuristics.
1329 if (SDNode *N = SU->getNode())
1330 if (N->getOpcode() == ISD::CopyToReg &&
1331 TargetRegisterInfo::isVirtualRegister
1332 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1333 continue;
1334
1335 // Locate the single data predecessor.
1336 SUnit *PredSU = 0;
1337 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1338 EE = SU->Preds.end(); II != EE; ++II)
1339 if (!II->isCtrl()) {
1340 PredSU = II->getSUnit();
1341 break;
1342 }
1343 assert(PredSU);
1344
1345 // Don't rewrite edges that carry physregs, because that requires additional
1346 // support infrastructure.
1347 if (PredSU->hasPhysRegDefs)
1348 continue;
1349 // Short-circuit the case where SU is PredSU's only data successor.
1350 if (PredSU->NumSuccs == 1)
1351 continue;
1352 // Avoid prescheduling to copies from virtual registers, which don't behave
1353 // like other nodes from the perspective of scheduling // heuristics.
1354 if (SDNode *N = SU->getNode())
1355 if (N->getOpcode() == ISD::CopyFromReg &&
1356 TargetRegisterInfo::isVirtualRegister
1357 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1358 continue;
1359
1360 // Perform checks on the successors of PredSU.
1361 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1362 EE = PredSU->Succs.end(); II != EE; ++II) {
1363 SUnit *PredSuccSU = II->getSUnit();
1364 if (PredSuccSU == SU) continue;
1365 // If PredSU has another successor with no data successors, for
1366 // now don't attempt to choose either over the other.
1367 if (PredSuccSU->NumSuccs == 0)
1368 goto outer_loop_continue;
1369 // Don't break physical register dependencies.
1370 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1371 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1372 goto outer_loop_continue;
1373 // Don't introduce graph cycles.
1374 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1375 goto outer_loop_continue;
1376 }
1377
1378 // Ok, the transformation is safe and the heuristics suggest it is
1379 // profitable. Update the graph.
David Greenef34d7ac2010-01-05 01:24:54 +00001380 DEBUG(dbgs() << "Prescheduling SU # " << SU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001381 << " next to PredSU # " << PredSU->NodeNum
1382 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001383 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1384 SDep Edge = PredSU->Succs[i];
1385 assert(!Edge.isAssignedRegDep());
1386 SUnit *SuccSU = Edge.getSUnit();
1387 if (SuccSU != SU) {
1388 Edge.setSUnit(PredSU);
1389 scheduleDAG->RemovePred(SuccSU, Edge);
1390 scheduleDAG->AddPred(SU, Edge);
1391 Edge.setSUnit(SU);
1392 scheduleDAG->AddPred(SuccSU, Edge);
1393 --i;
1394 }
1395 }
1396 outer_loop_continue:;
1397 }
1398}
1399
Evan Chengd38c22b2006-05-11 23:55:42 +00001400/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1401/// it as a def&use operand. Add a pseudo control edge from it to the other
1402/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001403/// first (lower in the schedule). If both nodes are two-address, favor the
1404/// one that has a CopyToReg use (more likely to be a loop induction update).
1405/// If both are two-address, but one is commutable while the other is not
1406/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001407template<class SF>
1408void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001409 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001410 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001411 if (!SU->isTwoAddress)
1412 continue;
1413
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001414 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001415 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001416 continue;
1417
Dan Gohman17059682008-07-17 19:10:17 +00001418 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001419 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001420 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001421 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001422 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001423 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1424 continue;
1425 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1426 if (DU->getNodeId() == -1)
1427 continue;
1428 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1429 if (!DUSU) continue;
1430 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1431 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001432 if (I->isCtrl()) continue;
1433 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001434 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001435 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001436 // Be conservative. Ignore if nodes aren't at roughly the same
1437 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001438 if (SuccSU->getHeight() < SU->getHeight() &&
1439 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001440 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001441 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1442 // constrains whatever is using the copy, instead of the copy
1443 // itself. In the case that the copy is coalesced, this
1444 // preserves the intent of the pseudo two-address heurietics.
1445 while (SuccSU->Succs.size() == 1 &&
1446 SuccSU->getNode()->isMachineOpcode() &&
1447 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001448 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001449 SuccSU = SuccSU->Succs.front().getSUnit();
1450 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001451 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1452 continue;
1453 // Don't constrain nodes with physical register defs if the
1454 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001455 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001456 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001457 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001458 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001459 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1460 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001461 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001462 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1463 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1464 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001465 continue;
1466 if ((!canClobber(SuccSU, DUSU) ||
1467 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1468 (!SU->isCommutable && SuccSU->isCommutable)) &&
1469 !scheduleDAG->IsReachable(SuccSU, SU)) {
David Greenef34d7ac2010-01-05 01:24:54 +00001470 DEBUG(dbgs() << "Adding a pseudo-two-addr edge from SU # "
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001471 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001472 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001473 /*Reg=*/0, /*isNormalMemory=*/false,
1474 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001475 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001476 }
1477 }
1478 }
1479 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001480}
1481
Evan Cheng6730f032007-01-08 23:55:53 +00001482/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1483/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001484template<class SF>
1485void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001486 SethiUllmanNumbers.assign(SUnits->size(), 0);
1487
1488 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001489 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001490}
Evan Chengd38c22b2006-05-11 23:55:42 +00001491
Roman Levenstein30d09512008-03-27 09:44:37 +00001492/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001493/// predecessors of the successors of the SUnit SU. Stop when the provided
1494/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001495static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1496 unsigned Limit) {
1497 unsigned Sum = 0;
1498 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1499 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001500 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001501 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1502 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001503 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001504 if (!PredSU->isScheduled)
1505 if (++Sum > Limit)
1506 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001507 }
1508 }
1509 return Sum;
1510}
1511
Evan Chengd38c22b2006-05-11 23:55:42 +00001512
1513// Top down
1514bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001515 unsigned LPriority = SPQ->getNodePriority(left);
1516 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001517 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1518 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001519 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1520 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001521 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1522 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001523
1524 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1525 return false;
1526 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1527 return true;
1528
Evan Chengd38c22b2006-05-11 23:55:42 +00001529 if (LIsFloater)
1530 LBonus -= 2;
1531 if (RIsFloater)
1532 RBonus -= 2;
1533 if (left->NumSuccs == 1)
1534 LBonus += 2;
1535 if (right->NumSuccs == 1)
1536 RBonus += 2;
1537
Evan Cheng73bdf042008-03-01 00:39:47 +00001538 if (LPriority+LBonus != RPriority+RBonus)
1539 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001540
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001541 if (left->getDepth() != right->getDepth())
1542 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001543
1544 if (left->NumSuccsLeft != right->NumSuccsLeft)
1545 return left->NumSuccsLeft > right->NumSuccsLeft;
1546
Roman Levenstein6b371142008-04-29 09:07:59 +00001547 assert(left->NodeQueueId && right->NodeQueueId &&
1548 "NodeQueueId cannot be zero");
1549 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001550}
1551
Evan Chengd38c22b2006-05-11 23:55:42 +00001552//===----------------------------------------------------------------------===//
1553// Public Constructor Functions
1554//===----------------------------------------------------------------------===//
1555
Dan Gohmandfaf6462009-02-11 04:27:20 +00001556llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001557llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001558 const TargetMachine &TM = IS->TM;
1559 const TargetInstrInfo *TII = TM.getInstrInfo();
1560 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001561
Evan Cheng7e4abde2008-07-02 09:23:51 +00001562 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001563
Evan Cheng7e4abde2008-07-02 09:23:51 +00001564 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001565 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001566 PQ->setScheduleDAG(SD);
1567 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001568}
1569
Dan Gohmandfaf6462009-02-11 04:27:20 +00001570llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001571llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001572 const TargetMachine &TM = IS->TM;
1573 const TargetInstrInfo *TII = TM.getInstrInfo();
1574 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001575
1576 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1577
Dan Gohman619ef482009-01-15 19:20:50 +00001578 ScheduleDAGRRList *SD =
1579 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001580 PQ->setScheduleDAG(SD);
1581 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001582}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001583
1584llvm::ScheduleDAGSDNodes *
1585llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1586 const TargetMachine &TM = IS->TM;
1587 const TargetInstrInfo *TII = TM.getInstrInfo();
1588 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1589
1590 SrcRegReductionPriorityQueue *PQ = new SrcRegReductionPriorityQueue(TII, TRI);
1591
1592 ScheduleDAGRRList *SD =
1593 new ScheduleDAGRRList(*IS->MF, true, PQ);
1594 PQ->setScheduleDAG(SD);
1595 return SD;
1596}