blob: 2ffd35034a9fb0b0f46108acce1fdef6f3d23997 [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman483377c2009-02-06 17:22:58 +000019#include "ScheduleDAGSDNodes.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000020#include "llvm/InlineAsm.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000021#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman619ef482009-01-15 19:20:50 +000022#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000023#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
Evan Chenga77f3d32010-07-21 06:09:07 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000028#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000029#include "llvm/ADT/Statistic.h"
Roman Levenstein6b371142008-04-29 09:07:59 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000033#include "llvm/Support/raw_ostream.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000034#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000035using namespace llvm;
36
Dan Gohmanfd227e92008-03-25 17:10:29 +000037STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000038STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000039STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000040STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041
Jim Laskey95eda5b2006-08-01 14:21:23 +000042static RegisterScheduler
43 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000044 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000045 createBURRListDAGScheduler);
46static RegisterScheduler
47 tdrListrDAGScheduler("list-tdrr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000048 "Top-down register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000049 createTDRRListDAGScheduler);
Bill Wendling8cbc25d2010-01-23 10:26:57 +000050static RegisterScheduler
51 sourceListDAGScheduler("source",
52 "Similar to list-burr but schedules in source "
53 "order when possible",
54 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000055
Evan Chengbdd062d2010-05-20 06:13:19 +000056static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000057 hybridListDAGScheduler("list-hybrid",
Evan Chengbdd062d2010-05-20 06:13:19 +000058 "Bottom-up rr list scheduling which avoid stalls for "
59 "long latency instructions",
60 createHybridListDAGScheduler);
61
Evan Chengd38c22b2006-05-11 23:55:42 +000062namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000063//===----------------------------------------------------------------------===//
64/// ScheduleDAGRRList - The actual register reduction list scheduler
65/// implementation. This supports both top-down and bottom-up scheduling.
66///
Nick Lewycky02d5f772009-10-25 06:33:48 +000067class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000068private:
69 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
70 /// it is top-down.
71 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000072
Evan Chengbdd062d2010-05-20 06:13:19 +000073 /// NeedLatency - True if the scheduler will make use of latency information.
74 ///
75 bool NeedLatency;
76
Evan Chengd38c22b2006-05-11 23:55:42 +000077 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000078 SchedulingPriorityQueue *AvailableQueue;
79
Dan Gohmanc07f6862008-09-23 18:50:48 +000080 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000081 /// that are "live". These nodes must be scheduled before any other nodes that
82 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000083 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000084 std::vector<SUnit*> LiveRegDefs;
85 std::vector<unsigned> LiveRegCycles;
86
Dan Gohmanad2134d2008-11-25 00:52:40 +000087 /// Topo - A topological ordering for SUnits which permits fast IsReachable
88 /// and similar queries.
89 ScheduleDAGTopologicalSort Topo;
90
Evan Chengd38c22b2006-05-11 23:55:42 +000091public:
Dan Gohman619ef482009-01-15 19:20:50 +000092 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000093 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +000094 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +000095 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +000096 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000097 }
98
99 ~ScheduleDAGRRList() {
100 delete AvailableQueue;
101 }
102
103 void Schedule();
104
Roman Levenstein733a4d62008-03-26 11:23:38 +0000105 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000106 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
107 return Topo.IsReachable(SU, TargetSU);
108 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000109
Dan Gohman60d68442009-01-29 19:49:27 +0000110 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000111 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
113 return Topo.WillCreateCycle(SU, TargetSU);
114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Dan Gohman2d170892008-12-09 22:54:47 +0000116 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000117 /// This returns true if this is a new predecessor.
118 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000119 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000120 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000121 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000122 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000123
Dan Gohman2d170892008-12-09 22:54:47 +0000124 /// RemovePred - removes a predecessor edge from SUnit SU.
125 /// This returns true if an edge was removed.
126 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000127 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000128 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000129 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000130 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000131
Evan Chengd38c22b2006-05-11 23:55:42 +0000132private:
Dan Gohman60d68442009-01-29 19:49:27 +0000133 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000134 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000135 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000136 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000137 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000138 void ScheduleNodeBottomUp(SUnit*, unsigned);
139 void ScheduleNodeTopDown(SUnit*, unsigned);
140 void UnscheduleNodeBottomUp(SUnit*);
141 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
142 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000143 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
144 const TargetRegisterClass*,
145 const TargetRegisterClass*,
146 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000147 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000148 void ListScheduleTopDown();
149 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150
151
152 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000153 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000154 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000155 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000157 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000158 if (NewNode->NodeNum >= NumSUnits)
159 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000160 return NewNode;
161 }
162
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 /// CreateClone - Creates a new SUnit from an existing one.
164 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000165 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000166 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000167 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000168 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000169 if (NewNode->NodeNum >= NumSUnits)
170 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 return NewNode;
172 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000173
Evan Chengbdd062d2010-05-20 06:13:19 +0000174 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
175 /// need actual latency information but the hybrid scheduler does.
176 bool ForceUnitLatencies() const {
177 return !NeedLatency;
178 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000179};
180} // end anonymous namespace
181
182
183/// Schedule - Schedule the DAG using list scheduling.
184void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000185 DEBUG(dbgs()
186 << "********** List Scheduling BB#" << BB->getNumber()
187 << " **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000188
Dan Gohmanc07f6862008-09-23 18:50:48 +0000189 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000190 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
191 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000192
Dan Gohman04543e72008-12-23 18:36:58 +0000193 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000194 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000195
Evan Chengd38c22b2006-05-11 23:55:42 +0000196 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000197 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000198 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000199
Dan Gohman46520a22008-06-21 19:18:17 +0000200 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000201
Evan Chengd38c22b2006-05-11 23:55:42 +0000202 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
203 if (isBottomUp)
204 ListScheduleBottomUp();
205 else
206 ListScheduleTopDown();
207
208 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000209}
Evan Chengd38c22b2006-05-11 23:55:42 +0000210
211//===----------------------------------------------------------------------===//
212// Bottom-Up Scheduling
213//===----------------------------------------------------------------------===//
214
Evan Chengd38c22b2006-05-11 23:55:42 +0000215/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000216/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000217void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000218 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000219
Evan Chengd38c22b2006-05-11 23:55:42 +0000220#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000221 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000222 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000223 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000224 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000225 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000226 }
227#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000228 --PredSU->NumSuccsLeft;
229
Evan Chengbdd062d2010-05-20 06:13:19 +0000230 if (!ForceUnitLatencies()) {
231 // Updating predecessor's height. This is now the cycle when the
232 // predecessor can be scheduled without causing a pipeline stall.
233 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
234 }
235
Dan Gohmanb9543432009-02-10 23:27:53 +0000236 // If all the node's successors are scheduled, this node is ready
237 // to be scheduled. Ignore the special EntrySU node.
238 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000239 PredSU->isAvailable = true;
240 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000241 }
242}
243
Dan Gohmanb9543432009-02-10 23:27:53 +0000244void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000245 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000246 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000247 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000248 ReleasePred(SU, &*I);
249 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000250 // This is a physical register dependency and it's impossible or
251 // expensive to copy the register. Make sure nothing that can
252 // clobber the register is scheduled between the predecessor and
253 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000254 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000255 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000256 LiveRegDefs[I->getReg()] = I->getSUnit();
257 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000258 }
259 }
260 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000261}
262
263/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
264/// count of its predecessors. If a predecessor pending count is zero, add it to
265/// the Available queue.
266void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000267 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000268 DEBUG(SU->dump(this));
269
Evan Chengbdd062d2010-05-20 06:13:19 +0000270#ifndef NDEBUG
271 if (CurCycle < SU->getHeight())
272 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
273#endif
274
275 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000276 SU->setHeightToAtLeast(CurCycle);
277 Sequence.push_back(SU);
278
Evan Cheng28590382010-07-21 23:53:58 +0000279 AvailableQueue->ScheduledNode(SU);
280
Dan Gohmanb9543432009-02-10 23:27:53 +0000281 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000282
283 // Release all the implicit physical register defs that are live.
284 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
285 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000286 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000287 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000288 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000289 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000290 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000291 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000292 LiveRegDefs[I->getReg()] = NULL;
293 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000294 }
295 }
296 }
297
Evan Chengd38c22b2006-05-11 23:55:42 +0000298 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000299}
300
Evan Cheng5924bf72007-09-25 01:54:36 +0000301/// CapturePred - This does the opposite of ReleasePred. Since SU is being
302/// unscheduled, incrcease the succ left count of its predecessors. Remove
303/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000304void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
305 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000306 if (PredSU->isAvailable) {
307 PredSU->isAvailable = false;
308 if (!PredSU->isPending)
309 AvailableQueue->remove(PredSU);
310 }
311
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000312 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000313 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000314}
315
316/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
317/// its predecessor states to reflect the change.
318void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000319 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000320 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000321
Evan Cheng5924bf72007-09-25 01:54:36 +0000322 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
323 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000324 CapturePred(&*I);
Evan Chengcc2efe12010-05-28 23:26:21 +0000325 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000326 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000327 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000328 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000329 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000330 LiveRegDefs[I->getReg()] = NULL;
331 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000332 }
333 }
334
335 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
336 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000337 if (I->isAssignedRegDep()) {
338 if (!LiveRegDefs[I->getReg()]) {
339 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000340 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000341 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000342 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
343 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000344 }
345 }
346
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000347 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000348 SU->isScheduled = false;
349 SU->isAvailable = true;
350 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000351 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000352}
353
Evan Cheng8e136a92007-09-26 21:36:17 +0000354/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000355/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000356void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
357 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000358 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000359 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000360 OldSU = Sequence.back();
361 Sequence.pop_back();
362 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000363 // Don't try to remove SU from AvailableQueue.
364 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000365 UnscheduleNodeBottomUp(OldSU);
366 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000367 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000368 }
369
Dan Gohman60d68442009-01-29 19:49:27 +0000370 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000371
372 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000373}
374
Evan Cheng3b245872010-02-05 01:27:11 +0000375static bool isOperandOf(const SUnit *SU, SDNode *N) {
376 for (const SDNode *SUNode = SU->getNode(); SUNode;
377 SUNode = SUNode->getFlaggedNode()) {
378 if (SUNode->isOperandOf(N))
379 return true;
380 }
381 return false;
382}
383
Evan Cheng5924bf72007-09-25 01:54:36 +0000384/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
385/// successors to the newly created node.
386SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000387 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000388 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000389
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000390 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000391 if (!N)
392 return NULL;
393
394 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000395 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000396 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000397 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000398 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000399 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000400 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000401 TryUnfold = true;
402 }
Evan Cheng79e97132007-10-05 01:39:18 +0000403 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000404 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000405 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000406 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000407 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000408 }
409
410 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000411 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000412 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000413 return NULL;
414
Evan Chengbdd062d2010-05-20 06:13:19 +0000415 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000416 assert(NewNodes.size() == 2 && "Expected a load folding node!");
417
418 N = NewNodes[1];
419 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000420 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000421 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000422 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000423 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
424 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000425 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000426
Dan Gohmane52e0892008-11-11 21:34:44 +0000427 // LoadNode may already exist. This can happen when there is another
428 // load from the same location and producing the same type of value
429 // but it has different alignment or volatileness.
430 bool isNewLoad = true;
431 SUnit *LoadSU;
432 if (LoadNode->getNodeId() != -1) {
433 LoadSU = &SUnits[LoadNode->getNodeId()];
434 isNewLoad = false;
435 } else {
436 LoadSU = CreateNewSUnit(LoadNode);
437 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000438 ComputeLatency(LoadSU);
439 }
440
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000441 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000442 assert(N->getNodeId() == -1 && "Node already inserted!");
443 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000444
Dan Gohman17059682008-07-17 19:10:17 +0000445 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000446 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000447 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000448 NewSU->isTwoAddress = true;
449 break;
450 }
451 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000452 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000453 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000454 ComputeLatency(NewSU);
455
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000456 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000457 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000458 SmallVector<SDep, 4> ChainSuccs;
459 SmallVector<SDep, 4> LoadPreds;
460 SmallVector<SDep, 4> NodePreds;
461 SmallVector<SDep, 4> NodeSuccs;
462 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
463 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000464 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000465 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000466 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000467 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000468 else
Dan Gohman2d170892008-12-09 22:54:47 +0000469 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000470 }
471 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
472 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000473 if (I->isCtrl())
474 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000475 else
Dan Gohman2d170892008-12-09 22:54:47 +0000476 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000477 }
478
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000479 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000480 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
481 const SDep &Pred = ChainPreds[i];
482 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000483 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000484 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000485 }
Evan Cheng79e97132007-10-05 01:39:18 +0000486 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000487 const SDep &Pred = LoadPreds[i];
488 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000489 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000490 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000491 }
492 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000493 const SDep &Pred = NodePreds[i];
494 RemovePred(SU, Pred);
495 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000496 }
497 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000498 SDep D = NodeSuccs[i];
499 SUnit *SuccDep = D.getSUnit();
500 D.setSUnit(SU);
501 RemovePred(SuccDep, D);
502 D.setSUnit(NewSU);
503 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000504 }
505 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000506 SDep D = ChainSuccs[i];
507 SUnit *SuccDep = D.getSUnit();
508 D.setSUnit(SU);
509 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000510 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000511 D.setSUnit(LoadSU);
512 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000513 }
Evan Cheng79e97132007-10-05 01:39:18 +0000514 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000515
516 // Add a data dependency to reflect that NewSU reads the value defined
517 // by LoadSU.
518 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000519
Evan Cheng91e0fc92007-12-18 08:42:10 +0000520 if (isNewLoad)
521 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000522 AvailableQueue->addNode(NewSU);
523
524 ++NumUnfolds;
525
526 if (NewSU->NumSuccsLeft == 0) {
527 NewSU->isAvailable = true;
528 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000529 }
530 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000531 }
532
Evan Chengbdd062d2010-05-20 06:13:19 +0000533 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000534 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000535
536 // New SUnit has the exact same predecessors.
537 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
538 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000539 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000540 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000541
542 // Only copy scheduled successors. Cut them from old node's successor
543 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000544 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000545 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
546 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000547 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000548 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000549 SUnit *SuccSU = I->getSUnit();
550 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000551 SDep D = *I;
552 D.setSUnit(NewSU);
553 AddPred(SuccSU, D);
554 D.setSUnit(SU);
555 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000556 }
557 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000558 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000559 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000560
561 AvailableQueue->updateNode(SU);
562 AvailableQueue->addNode(NewSU);
563
Evan Cheng1ec79b42007-09-27 07:09:03 +0000564 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000565 return NewSU;
566}
567
Evan Chengb2c42c62009-01-12 03:19:55 +0000568/// InsertCopiesAndMoveSuccs - Insert register copies and move all
569/// scheduled successors of the given SUnit to the last copy.
570void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
571 const TargetRegisterClass *DestRC,
572 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000573 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000574 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000575 CopyFromSU->CopySrcRC = SrcRC;
576 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000577
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000578 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000579 CopyToSU->CopySrcRC = DestRC;
580 CopyToSU->CopyDstRC = SrcRC;
581
582 // Only copy scheduled successors. Cut them from old node's successor
583 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000584 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000585 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
586 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000587 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000588 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000589 SUnit *SuccSU = I->getSUnit();
590 if (SuccSU->isScheduled) {
591 SDep D = *I;
592 D.setSUnit(CopyToSU);
593 AddPred(SuccSU, D);
594 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000595 }
596 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000597 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000598 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000599
Dan Gohman2d170892008-12-09 22:54:47 +0000600 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
601 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000602
603 AvailableQueue->updateNode(SU);
604 AvailableQueue->addNode(CopyFromSU);
605 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000606 Copies.push_back(CopyFromSU);
607 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000608
Evan Chengb2c42c62009-01-12 03:19:55 +0000609 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000610}
611
612/// getPhysicalRegisterVT - Returns the ValueType of the physical register
613/// definition of the specified node.
614/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000615static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000616 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000617 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000618 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000619 unsigned NumRes = TID.getNumDefs();
620 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000621 if (Reg == *ImpDef)
622 break;
623 ++NumRes;
624 }
625 return N->getValueType(NumRes);
626}
627
Evan Chengb8905c42009-03-04 01:41:49 +0000628/// CheckForLiveRegDef - Return true and update live register vector if the
629/// specified register def of the specified SUnit clobbers any "live" registers.
630static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
631 std::vector<SUnit*> &LiveRegDefs,
632 SmallSet<unsigned, 4> &RegAdded,
633 SmallVector<unsigned, 4> &LRegs,
634 const TargetRegisterInfo *TRI) {
635 bool Added = false;
636 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
637 if (RegAdded.insert(Reg)) {
638 LRegs.push_back(Reg);
639 Added = true;
640 }
641 }
642 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
643 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
644 if (RegAdded.insert(*Alias)) {
645 LRegs.push_back(*Alias);
646 Added = true;
647 }
648 }
649 return Added;
650}
651
Evan Cheng5924bf72007-09-25 01:54:36 +0000652/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
653/// scheduling of the given node to satisfy live physical register dependencies.
654/// If the specific node is the last one that's available to schedule, do
655/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000656bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
657 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000658 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000659 return false;
660
Evan Chenge6f92252007-09-27 18:46:06 +0000661 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000662 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000663 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
664 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000665 if (I->isAssignedRegDep())
666 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
667 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 }
669
Dan Gohman072734e2008-11-13 23:24:17 +0000670 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000671 if (Node->getOpcode() == ISD::INLINEASM) {
672 // Inline asm can clobber physical defs.
673 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000674 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000675 --NumOps; // Ignore the flag operand.
676
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000677 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000678 unsigned Flags =
679 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000680 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000681
682 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000683 if (InlineAsm::isRegDefKind(Flags) ||
684 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000685 // Check for def of register or earlyclobber register.
686 for (; NumVals; --NumVals, ++i) {
687 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
688 if (TargetRegisterInfo::isPhysicalRegister(Reg))
689 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
690 }
691 } else
692 i += NumVals;
693 }
694 continue;
695 }
696
Dan Gohman072734e2008-11-13 23:24:17 +0000697 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000698 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000699 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000700 if (!TID.ImplicitDefs)
701 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000702 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
703 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000704 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000705 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000706}
707
Evan Cheng1ec79b42007-09-27 07:09:03 +0000708
Evan Chengd38c22b2006-05-11 23:55:42 +0000709/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
710/// schedulers.
711void ScheduleDAGRRList::ListScheduleBottomUp() {
712 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000713
714 // Release any predecessors of the special Exit node.
715 ReleasePredecessors(&ExitSU, CurCycle);
716
Evan Chengd38c22b2006-05-11 23:55:42 +0000717 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000718 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000719 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000720 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
721 RootSU->isAvailable = true;
722 AvailableQueue->push(RootSU);
723 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000724
725 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000726 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000727 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000728 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000729 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000730 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000731 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000732 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000733 SUnit *CurSU = AvailableQueue->pop();
734 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000735 SmallVector<unsigned, 4> LRegs;
736 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
737 break;
738 Delayed = true;
739 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000740
741 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
742 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000743 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000744 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000745
746 // All candidates are delayed due to live physical reg dependencies.
747 // Try backtracking, code duplication, or inserting cross class copies
748 // to resolve it.
749 if (Delayed && !CurSU) {
750 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
751 SUnit *TrySU = NotReady[i];
752 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
753
754 // Try unscheduling up to the point where it's safe to schedule
755 // this node.
756 unsigned LiveCycle = CurCycle;
757 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
758 unsigned Reg = LRegs[j];
759 unsigned LCycle = LiveRegCycles[Reg];
760 LiveCycle = std::min(LiveCycle, LCycle);
761 }
762 SUnit *OldSU = Sequence[LiveCycle];
763 if (!WillCreateCycle(TrySU, OldSU)) {
764 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
765 // Force the current node to be scheduled before the node that
766 // requires the physical reg dep.
767 if (OldSU->isAvailable) {
768 OldSU->isAvailable = false;
769 AvailableQueue->remove(OldSU);
770 }
Dan Gohman2d170892008-12-09 22:54:47 +0000771 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
772 /*Reg=*/0, /*isNormalMemory=*/false,
773 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000774 // If one or more successors has been unscheduled, then the current
775 // node is no longer avaialable. Schedule a successor that's now
776 // available instead.
777 if (!TrySU->isAvailable)
778 CurSU = AvailableQueue->pop();
779 else {
780 CurSU = TrySU;
781 TrySU->isPending = false;
782 NotReady.erase(NotReady.begin()+i);
783 }
784 break;
785 }
786 }
787
788 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000789 // Can't backtrack. If it's too expensive to copy the value, then try
790 // duplicate the nodes that produces these "too expensive to copy"
791 // values to break the dependency. In case even that doesn't work,
792 // insert cross class copies.
793 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000794 SUnit *TrySU = NotReady[0];
795 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
796 assert(LRegs.size() == 1 && "Can't handle this yet!");
797 unsigned Reg = LRegs[0];
798 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000799 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000800 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000801 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000802 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
803
804 // If cross copy register class is null, then it must be possible copy
805 // the value directly. Do not try duplicate the def.
806 SUnit *NewDef = 0;
807 if (DestRC)
808 NewDef = CopyAndMoveSuccessors(LRDef);
809 else
810 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000811 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000812 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000813 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000814 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000815 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000816 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000817 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000818 /*Reg=*/0, /*isNormalMemory=*/false,
819 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000820 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000821 NewDef = Copies.back();
822 }
823
Evan Chengbdd062d2010-05-20 06:13:19 +0000824 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000825 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000826 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000827 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000828 /*Reg=*/0, /*isNormalMemory=*/false,
829 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000830 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000831 TrySU->isAvailable = false;
832 CurSU = NewDef;
833 }
834
Dan Gohman60d68442009-01-29 19:49:27 +0000835 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000836 }
837
Evan Chengd38c22b2006-05-11 23:55:42 +0000838 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000839 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
840 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000841 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000842 if (NotReady[i]->isAvailable)
843 AvailableQueue->push(NotReady[i]);
844 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000845 NotReady.clear();
846
Dan Gohmanc602dd42008-11-21 00:10:42 +0000847 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000848 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000849 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000850 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000851 }
852
Evan Chengd38c22b2006-05-11 23:55:42 +0000853 // Reverse the order if it is bottom up.
854 std::reverse(Sequence.begin(), Sequence.end());
855
Evan Chengd38c22b2006-05-11 23:55:42 +0000856#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000857 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000858#endif
859}
860
861//===----------------------------------------------------------------------===//
862// Top-Down Scheduling
863//===----------------------------------------------------------------------===//
864
865/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000866/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000867void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000868 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000869
Evan Chengd38c22b2006-05-11 23:55:42 +0000870#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000871 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000872 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000873 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000874 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000875 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000876 }
877#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000878 --SuccSU->NumPredsLeft;
879
Dan Gohmanb9543432009-02-10 23:27:53 +0000880 // If all the node's predecessors are scheduled, this node is ready
881 // to be scheduled. Ignore the special ExitSU node.
882 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000883 SuccSU->isAvailable = true;
884 AvailableQueue->push(SuccSU);
885 }
886}
887
Dan Gohmanb9543432009-02-10 23:27:53 +0000888void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
889 // Top down: release successors
890 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
891 I != E; ++I) {
892 assert(!I->isAssignedRegDep() &&
893 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
894
895 ReleaseSucc(SU, &*I);
896 }
897}
898
Evan Chengd38c22b2006-05-11 23:55:42 +0000899/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
900/// count of its successors. If a successor pending count is zero, add it to
901/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000902void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000903 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000904 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000905
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000906 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
907 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000908 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000909
Dan Gohmanb9543432009-02-10 23:27:53 +0000910 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000911 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000912 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000913}
914
Dan Gohman54a187e2007-08-20 19:28:38 +0000915/// ListScheduleTopDown - The main loop of list scheduling for top-down
916/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000917void ScheduleDAGRRList::ListScheduleTopDown() {
918 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000919 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000920
Dan Gohmanb9543432009-02-10 23:27:53 +0000921 // Release any successors of the special Entry node.
922 ReleaseSuccessors(&EntrySU);
923
Evan Chengd38c22b2006-05-11 23:55:42 +0000924 // All leaves to Available queue.
925 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
926 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000927 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000928 AvailableQueue->push(&SUnits[i]);
929 SUnits[i].isAvailable = true;
930 }
931 }
932
Evan Chengd38c22b2006-05-11 23:55:42 +0000933 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000934 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000935 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000936 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000937 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000938
Dan Gohmanc602dd42008-11-21 00:10:42 +0000939 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000940 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000941 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000942 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000943 }
944
Evan Chengd38c22b2006-05-11 23:55:42 +0000945#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000946 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000947#endif
948}
949
950
Evan Chengd38c22b2006-05-11 23:55:42 +0000951//===----------------------------------------------------------------------===//
952// RegReductionPriorityQueue Implementation
953//===----------------------------------------------------------------------===//
954//
955// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
956// to reduce register pressure.
957//
958namespace {
959 template<class SF>
960 class RegReductionPriorityQueue;
961
962 /// Sorting functions for the Available queue.
963 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
964 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
965 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
966 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
967
968 bool operator()(const SUnit* left, const SUnit* right) const;
969 };
970
971 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
972 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
973 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
974 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
975
976 bool operator()(const SUnit* left, const SUnit* right) const;
977 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000978
979 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
980 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
981 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
982 : SPQ(spq) {}
983 src_ls_rr_sort(const src_ls_rr_sort &RHS)
984 : SPQ(RHS.SPQ) {}
985
986 bool operator()(const SUnit* left, const SUnit* right) const;
987 };
Evan Chengbdd062d2010-05-20 06:13:19 +0000988
989 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
990 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
991 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
992 : SPQ(spq) {}
993 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
994 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +0000995
Evan Chengbdd062d2010-05-20 06:13:19 +0000996 bool operator()(const SUnit* left, const SUnit* right) const;
997 };
Evan Chengd38c22b2006-05-11 23:55:42 +0000998} // end anonymous namespace
999
Dan Gohman186f65d2008-11-20 03:30:37 +00001000/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1001/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001002static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001003CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001004 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1005 if (SethiUllmanNumber != 0)
1006 return SethiUllmanNumber;
1007
1008 unsigned Extra = 0;
1009 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1010 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001011 if (I->isCtrl()) continue; // ignore chain preds
1012 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001013 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001014 if (PredSethiUllman > SethiUllmanNumber) {
1015 SethiUllmanNumber = PredSethiUllman;
1016 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001017 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001018 ++Extra;
1019 }
1020
1021 SethiUllmanNumber += Extra;
1022
1023 if (SethiUllmanNumber == 0)
1024 SethiUllmanNumber = 1;
1025
1026 return SethiUllmanNumber;
1027}
1028
Evan Chengd38c22b2006-05-11 23:55:42 +00001029namespace {
1030 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001031 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001032 std::vector<SUnit*> Queue;
1033 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001034 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001035 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001036
Dan Gohman3f656df2008-11-20 02:45:51 +00001037 protected:
1038 // SUnits - The SUnits for the current graph.
1039 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001040
1041 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001042 const TargetInstrInfo *TII;
1043 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001044 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001045 ScheduleDAGRRList *scheduleDAG;
1046
Dan Gohman186f65d2008-11-20 03:30:37 +00001047 // SethiUllmanNumbers - The SethiUllman number for each node.
1048 std::vector<unsigned> SethiUllmanNumbers;
1049
Evan Chenga77f3d32010-07-21 06:09:07 +00001050 /// RegPressure - Tracking current reg pressure per register class.
1051 ///
Evan Cheng28590382010-07-21 23:53:58 +00001052 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001053
1054 /// RegLimit - Tracking the number of allocatable registers per register
1055 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001056 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001057
Dan Gohman3f656df2008-11-20 02:45:51 +00001058 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001059 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001060 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001061 const TargetInstrInfo *tii,
1062 const TargetRegisterInfo *tri,
1063 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001064 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001065 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001066 if (TracksRegPressure) {
1067 unsigned NumRC = TRI->getNumRegClasses();
1068 RegLimit.resize(NumRC);
1069 RegPressure.resize(NumRC);
1070 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1071 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1072 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1073 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001074 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001075 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001076 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001077
1078 void initNodes(std::vector<SUnit> &sunits) {
1079 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001080 // Add pseudo dependency edges for two-address nodes.
1081 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001082 // Reroute edges to nodes with multiple uses.
1083 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001084 // Calculate node priorities.
1085 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001086 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001087
Dan Gohman186f65d2008-11-20 03:30:37 +00001088 void addNode(const SUnit *SU) {
1089 unsigned SUSize = SethiUllmanNumbers.size();
1090 if (SUnits->size() > SUSize)
1091 SethiUllmanNumbers.resize(SUSize*2, 0);
1092 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1093 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001094
Dan Gohman186f65d2008-11-20 03:30:37 +00001095 void updateNode(const SUnit *SU) {
1096 SethiUllmanNumbers[SU->NodeNum] = 0;
1097 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1098 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001099
Dan Gohman186f65d2008-11-20 03:30:37 +00001100 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001101 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001102 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001103 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001104 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001105
1106 unsigned getNodePriority(const SUnit *SU) const {
1107 assert(SU->NodeNum < SethiUllmanNumbers.size());
1108 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001109 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001110 // CopyToReg should be close to its uses to facilitate coalescing and
1111 // avoid spilling.
1112 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001113 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1114 Opc == TargetOpcode::SUBREG_TO_REG ||
1115 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001116 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1117 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001118 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001119 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1120 // If SU does not have a register use, i.e. it doesn't produce a value
1121 // that would be consumed (e.g. store), then it terminates a chain of
1122 // computation. Give it a large SethiUllman number so it will be
1123 // scheduled right before its predecessors that it doesn't lengthen
1124 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001125 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001126 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1127 // If SU does not have a register def, schedule it close to its uses
1128 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001129 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001130 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001131 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001132
1133 unsigned getNodeOrdering(const SUnit *SU) const {
1134 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1135 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001136
Evan Chengd38c22b2006-05-11 23:55:42 +00001137 bool empty() const { return Queue.empty(); }
1138
1139 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001140 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001141 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001142 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001143 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001144
Evan Chengd38c22b2006-05-11 23:55:42 +00001145 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001146 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001147 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001148 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001149 E = Queue.end(); I != E; ++I)
1150 if (Picker(*Best, *I))
1151 Best = I;
1152 SUnit *V = *Best;
1153 if (Best != prior(Queue.end()))
1154 std::swap(*Best, Queue.back());
1155 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001156 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001157 return V;
1158 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001159
Evan Cheng5924bf72007-09-25 01:54:36 +00001160 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001161 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001162 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001163 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1164 SU);
1165 if (I != prior(Queue.end()))
1166 std::swap(*I, Queue.back());
1167 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001168 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001169 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001170
Evan Chengdf907f42010-07-23 22:39:59 +00001171 bool HighRegPressure(const SUnit *SU, unsigned &Excess) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001172 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001173 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001174
Evan Chengdf907f42010-07-23 22:39:59 +00001175 bool High = false;
1176 Excess = 0;
Evan Chenga77f3d32010-07-21 06:09:07 +00001177 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1178 I != E; ++I) {
1179 if (I->isCtrl())
1180 continue;
1181 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001182 const SDNode *PN = PredSU->getNode();
1183 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001184 if (PN->getOpcode() == ISD::CopyFromReg) {
1185 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001186 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1187 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chengdf907f42010-07-23 22:39:59 +00001188 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1189 High = true;
1190 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
1191 }
1192 }
1193 continue;
1194 }
1195 unsigned POpc = PN->getMachineOpcode();
1196 if (POpc == TargetOpcode::IMPLICIT_DEF)
1197 continue;
1198 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1199 EVT VT = PN->getOperand(0).getValueType();
1200 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1201 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1202 // Check if this increases register pressure of the specific register
1203 // class to the point where it would cause spills.
1204 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1205 High = true;
1206 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
1207 }
1208 continue;
1209 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1210 POpc == TargetOpcode::SUBREG_TO_REG) {
1211 EVT VT = PN->getValueType(0);
1212 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1213 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1214 // Check if this increases register pressure of the specific register
1215 // class to the point where it would cause spills.
1216 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1217 High = true;
1218 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
Evan Cheng28590382010-07-21 23:53:58 +00001219 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001220 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001221 }
1222 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001223 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001224 EVT VT = PN->getValueType(i);
1225 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001226 continue;
1227 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1228 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1229 // Check if this increases register pressure of the specific register
1230 // class to the point where it would cause spills.
Evan Chengdf907f42010-07-23 22:39:59 +00001231 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId]) {
1232 High = true;
1233 Excess += (RegPressure[RCId] + Cost) - RegLimit[RCId];
1234 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001235 }
1236 }
1237
Evan Chengdf907f42010-07-23 22:39:59 +00001238 return High;
Evan Chenga77f3d32010-07-21 06:09:07 +00001239 }
1240
Evan Chengbf32e542010-07-22 06:24:48 +00001241 void ScheduledNode(SUnit *SU) {
1242 if (!TracksRegPressure)
1243 return;
1244
Evan Chenga77f3d32010-07-21 06:09:07 +00001245 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001246 if (!N->isMachineOpcode()) {
1247 if (N->getOpcode() != ISD::CopyToReg)
1248 return;
1249 } else {
1250 unsigned Opc = N->getMachineOpcode();
1251 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1252 Opc == TargetOpcode::INSERT_SUBREG ||
1253 Opc == TargetOpcode::SUBREG_TO_REG ||
1254 Opc == TargetOpcode::REG_SEQUENCE ||
1255 Opc == TargetOpcode::IMPLICIT_DEF)
1256 return;
1257 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001258
1259 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1260 I != E; ++I) {
1261 if (I->isCtrl())
1262 continue;
1263 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001264 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001265 continue;
1266 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001267 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001268 if (PN->getOpcode() == ISD::CopyFromReg) {
1269 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001270 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1271 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1272 }
1273 continue;
1274 }
1275 unsigned POpc = PN->getMachineOpcode();
1276 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001277 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001278 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1279 EVT VT = PN->getOperand(0).getValueType();
1280 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1281 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1282 continue;
1283 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1284 POpc == TargetOpcode::SUBREG_TO_REG) {
1285 EVT VT = PN->getValueType(0);
1286 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1287 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1288 continue;
1289 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001290 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1291 for (unsigned i = 0; i != NumDefs; ++i) {
1292 EVT VT = PN->getValueType(i);
1293 if (!PN->hasAnyUseOfValue(i))
1294 continue;
1295 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1296 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1297 }
1298 }
1299
Evan Chengdf907f42010-07-23 22:39:59 +00001300 if (SU->NumSuccs) {
1301 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1302 for (unsigned i = 0; i != NumDefs; ++i) {
1303 EVT VT = N->getValueType(i);
1304 if (!N->hasAnyUseOfValue(i))
1305 continue;
1306 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1307 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1308 // Register pressure tracking is imprecise. This can happen.
1309 RegPressure[RCId] = 0;
1310 else
1311 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1312 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001313 }
Evan Chengbf32e542010-07-22 06:24:48 +00001314
1315 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001316 }
1317
Evan Chengbf32e542010-07-22 06:24:48 +00001318 void UnscheduledNode(SUnit *SU) {
1319 if (!TracksRegPressure)
1320 return;
1321
Evan Chenga77f3d32010-07-21 06:09:07 +00001322 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001323 if (!N->isMachineOpcode()) {
1324 if (N->getOpcode() != ISD::CopyToReg)
1325 return;
1326 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001327 unsigned Opc = N->getMachineOpcode();
Evan Chengdf907f42010-07-23 22:39:59 +00001328 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1329 Opc == TargetOpcode::INSERT_SUBREG ||
1330 Opc == TargetOpcode::SUBREG_TO_REG ||
Evan Chenga77f3d32010-07-21 06:09:07 +00001331 Opc == TargetOpcode::REG_SEQUENCE ||
1332 Opc == TargetOpcode::IMPLICIT_DEF)
1333 return;
1334
1335 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1336 I != E; ++I) {
1337 if (I->isCtrl())
1338 continue;
1339 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001340 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001341 continue;
1342 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001343 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001344 if (PN->getOpcode() == ISD::CopyFromReg) {
1345 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001346 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1347 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1348 }
1349 continue;
1350 }
1351 unsigned POpc = PN->getMachineOpcode();
1352 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001353 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001354 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1355 EVT VT = PN->getOperand(0).getValueType();
1356 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1357 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1358 continue;
1359 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1360 POpc == TargetOpcode::SUBREG_TO_REG) {
1361 EVT VT = PN->getValueType(0);
1362 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1363 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1364 continue;
1365 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001366 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1367 for (unsigned i = 0; i != NumDefs; ++i) {
1368 EVT VT = PN->getValueType(i);
1369 if (!PN->hasAnyUseOfValue(i))
1370 continue;
1371 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001372 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001373 // Register pressure tracking is imprecise. This can happen.
1374 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001375 else
1376 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001377 }
1378 }
1379
Evan Chengdf907f42010-07-23 22:39:59 +00001380 if (SU->NumSuccs) {
1381 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1382 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1383 EVT VT = N->getValueType(i);
1384 if (VT == MVT::Flag || VT == MVT::Other)
1385 continue;
1386 if (!N->hasAnyUseOfValue(i))
1387 continue;
1388 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1389 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1390 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001391 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001392
Evan Chenga77f3d32010-07-21 06:09:07 +00001393 dumpRegPressure();
1394 }
1395
Dan Gohman3f656df2008-11-20 02:45:51 +00001396 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1397 scheduleDAG = scheduleDag;
1398 }
1399
Evan Chenga77f3d32010-07-21 06:09:07 +00001400 void dumpRegPressure() const {
1401 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1402 E = TRI->regclass_end(); I != E; ++I) {
1403 const TargetRegisterClass *RC = *I;
1404 unsigned Id = RC->getID();
1405 unsigned RP = RegPressure[Id];
1406 if (!RP) continue;
1407 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1408 << '\n');
1409 }
1410 }
1411
Dan Gohman3f656df2008-11-20 02:45:51 +00001412 protected:
1413 bool canClobber(const SUnit *SU, const SUnit *Op);
1414 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001415 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001416 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001417 };
1418
Dan Gohman186f65d2008-11-20 03:30:37 +00001419 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1420 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001421
Dan Gohman186f65d2008-11-20 03:30:37 +00001422 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1423 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001424
1425 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1426 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001427
1428 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1429 HybridBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001430}
1431
Evan Chengb9e3db62007-03-14 22:43:40 +00001432/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001433/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001434static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001435 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001436 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001437 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001438 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001439 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001440 // If there are bunch of CopyToRegs stacked up, they should be considered
1441 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001442 if (I->getSUnit()->getNode() &&
1443 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001444 Height = closestSucc(I->getSUnit())+1;
1445 if (Height > MaxHeight)
1446 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001447 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001448 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001449}
1450
Evan Cheng61bc51e2007-12-20 02:22:36 +00001451/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001452/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001453static unsigned calcMaxScratches(const SUnit *SU) {
1454 unsigned Scratches = 0;
1455 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001456 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001457 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001458 Scratches++;
1459 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001460 return Scratches;
1461}
1462
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001463template <typename RRSort>
1464static bool BURRSort(const SUnit *left, const SUnit *right,
1465 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001466 unsigned LPriority = SPQ->getNodePriority(left);
1467 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001468 if (LPriority != RPriority)
1469 return LPriority > RPriority;
1470
1471 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1472 // e.g.
1473 // t1 = op t2, c1
1474 // t3 = op t4, c2
1475 //
1476 // and the following instructions are both ready.
1477 // t2 = op c3
1478 // t4 = op c4
1479 //
1480 // Then schedule t2 = op first.
1481 // i.e.
1482 // t4 = op c4
1483 // t2 = op c3
1484 // t1 = op t2, c1
1485 // t3 = op t4, c2
1486 //
1487 // This creates more short live intervals.
1488 unsigned LDist = closestSucc(left);
1489 unsigned RDist = closestSucc(right);
1490 if (LDist != RDist)
1491 return LDist < RDist;
1492
Evan Cheng3a14efa2009-02-12 08:59:45 +00001493 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001494 unsigned LScratch = calcMaxScratches(left);
1495 unsigned RScratch = calcMaxScratches(right);
1496 if (LScratch != RScratch)
1497 return LScratch > RScratch;
1498
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001499 if (left->getHeight() != right->getHeight())
1500 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001501
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001502 if (left->getDepth() != right->getDepth())
1503 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001504
Roman Levenstein6b371142008-04-29 09:07:59 +00001505 assert(left->NodeQueueId && right->NodeQueueId &&
1506 "NodeQueueId cannot be zero");
1507 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001508}
1509
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001510// Bottom up
1511bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1512 return BURRSort(left, right, SPQ);
1513}
1514
1515// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001516bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001517 unsigned LOrder = SPQ->getNodeOrdering(left);
1518 unsigned ROrder = SPQ->getNodeOrdering(right);
1519
1520 // Prefer an ordering where the lower the non-zero order number, the higher
1521 // the preference.
1522 if ((LOrder || ROrder) && LOrder != ROrder)
1523 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1524
1525 return BURRSort(left, right, SPQ);
1526}
1527
Evan Chengbdd062d2010-05-20 06:13:19 +00001528bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdf907f42010-07-23 22:39:59 +00001529 unsigned LExcess, RExcess;
1530 bool LHigh = SPQ->HighRegPressure(left, LExcess);
1531 bool RHigh = SPQ->HighRegPressure(right, RExcess);
Evan Cheng28590382010-07-21 23:53:58 +00001532 if (LHigh && !RHigh)
1533 return true;
1534 else if (!LHigh && RHigh)
1535 return false;
Evan Chengdf907f42010-07-23 22:39:59 +00001536 else if (LHigh && RHigh) {
1537 if (LExcess > RExcess)
1538 return true;
1539 else if (LExcess < RExcess)
1540 return false;
1541 // Otherwise schedule for register pressure reduction.
1542 } else {
Evan Cheng28590382010-07-21 23:53:58 +00001543 // Low register pressure situation, schedule for latency if possible.
1544 bool LStall = left->SchedulingPref == Sched::Latency &&
1545 SPQ->getCurCycle() < left->getHeight();
1546 bool RStall = right->SchedulingPref == Sched::Latency &&
1547 SPQ->getCurCycle() < right->getHeight();
1548 // If scheduling one of the node will cause a pipeline stall, delay it.
1549 // If scheduling either one of the node will cause a pipeline stall, sort
1550 // them according to their height.
1551 // If neither will cause a pipeline stall, try to reduce register pressure.
1552 if (LStall) {
1553 if (!RStall)
1554 return true;
1555 if (left->getHeight() != right->getHeight())
1556 return left->getHeight() > right->getHeight();
1557 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001558 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001559
Evan Cheng28590382010-07-21 23:53:58 +00001560 // If either node is scheduling for latency, sort them by height and latency
1561 // first.
1562 if (left->SchedulingPref == Sched::Latency ||
1563 right->SchedulingPref == Sched::Latency) {
1564 if (left->getHeight() != right->getHeight())
1565 return left->getHeight() > right->getHeight();
1566 if (left->Latency != right->Latency)
1567 return left->Latency > right->Latency;
1568 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001569 }
1570
Evan Chengbdd062d2010-05-20 06:13:19 +00001571 return BURRSort(left, right, SPQ);
1572}
1573
Dan Gohman3f656df2008-11-20 02:45:51 +00001574template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001575bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001576RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001577 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001578 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001579 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001580 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001581 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001582 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001583 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001584 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001585 if (DU->getNodeId() != -1 &&
1586 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001587 return true;
1588 }
1589 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001590 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001591 return false;
1592}
1593
Evan Chenga5e595d2007-09-28 22:32:30 +00001594/// hasCopyToRegUse - Return true if SU has a value successor that is a
1595/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001596static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001597 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1598 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001599 if (I->isCtrl()) continue;
1600 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001601 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001602 return true;
1603 }
1604 return false;
1605}
1606
Evan Chengf9891412007-12-20 09:25:31 +00001607/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001608/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001609static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001610 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001611 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001612 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001613 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1614 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001615 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001616 for (const SDNode *SUNode = SU->getNode(); SUNode;
1617 SUNode = SUNode->getFlaggedNode()) {
1618 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001619 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001620 const unsigned *SUImpDefs =
1621 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1622 if (!SUImpDefs)
1623 return false;
1624 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001625 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001626 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001627 continue;
1628 if (!N->hasAnyUseOfValue(i))
1629 continue;
1630 unsigned Reg = ImpDefs[i - NumDefs];
1631 for (;*SUImpDefs; ++SUImpDefs) {
1632 unsigned SUReg = *SUImpDefs;
1633 if (TRI->regsOverlap(Reg, SUReg))
1634 return true;
1635 }
Evan Chengf9891412007-12-20 09:25:31 +00001636 }
1637 }
1638 return false;
1639}
1640
Dan Gohman9a658d72009-03-24 00:49:12 +00001641/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1642/// are not handled well by the general register pressure reduction
1643/// heuristics. When presented with code like this:
1644///
1645/// N
1646/// / |
1647/// / |
1648/// U store
1649/// |
1650/// ...
1651///
1652/// the heuristics tend to push the store up, but since the
1653/// operand of the store has another use (U), this would increase
1654/// the length of that other use (the U->N edge).
1655///
1656/// This function transforms code like the above to route U's
1657/// dependence through the store when possible, like this:
1658///
1659/// N
1660/// ||
1661/// ||
1662/// store
1663/// |
1664/// U
1665/// |
1666/// ...
1667///
1668/// This results in the store being scheduled immediately
1669/// after N, which shortens the U->N live range, reducing
1670/// register pressure.
1671///
1672template<class SF>
1673void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1674 // Visit all the nodes in topological order, working top-down.
1675 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1676 SUnit *SU = &(*SUnits)[i];
1677 // For now, only look at nodes with no data successors, such as stores.
1678 // These are especially important, due to the heuristics in
1679 // getNodePriority for nodes with no data successors.
1680 if (SU->NumSuccs != 0)
1681 continue;
1682 // For now, only look at nodes with exactly one data predecessor.
1683 if (SU->NumPreds != 1)
1684 continue;
1685 // Avoid prescheduling copies to virtual registers, which don't behave
1686 // like other nodes from the perspective of scheduling heuristics.
1687 if (SDNode *N = SU->getNode())
1688 if (N->getOpcode() == ISD::CopyToReg &&
1689 TargetRegisterInfo::isVirtualRegister
1690 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1691 continue;
1692
1693 // Locate the single data predecessor.
1694 SUnit *PredSU = 0;
1695 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1696 EE = SU->Preds.end(); II != EE; ++II)
1697 if (!II->isCtrl()) {
1698 PredSU = II->getSUnit();
1699 break;
1700 }
1701 assert(PredSU);
1702
1703 // Don't rewrite edges that carry physregs, because that requires additional
1704 // support infrastructure.
1705 if (PredSU->hasPhysRegDefs)
1706 continue;
1707 // Short-circuit the case where SU is PredSU's only data successor.
1708 if (PredSU->NumSuccs == 1)
1709 continue;
1710 // Avoid prescheduling to copies from virtual registers, which don't behave
1711 // like other nodes from the perspective of scheduling // heuristics.
1712 if (SDNode *N = SU->getNode())
1713 if (N->getOpcode() == ISD::CopyFromReg &&
1714 TargetRegisterInfo::isVirtualRegister
1715 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1716 continue;
1717
1718 // Perform checks on the successors of PredSU.
1719 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1720 EE = PredSU->Succs.end(); II != EE; ++II) {
1721 SUnit *PredSuccSU = II->getSUnit();
1722 if (PredSuccSU == SU) continue;
1723 // If PredSU has another successor with no data successors, for
1724 // now don't attempt to choose either over the other.
1725 if (PredSuccSU->NumSuccs == 0)
1726 goto outer_loop_continue;
1727 // Don't break physical register dependencies.
1728 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1729 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1730 goto outer_loop_continue;
1731 // Don't introduce graph cycles.
1732 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1733 goto outer_loop_continue;
1734 }
1735
1736 // Ok, the transformation is safe and the heuristics suggest it is
1737 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001738 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1739 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001740 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001741 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1742 SDep Edge = PredSU->Succs[i];
1743 assert(!Edge.isAssignedRegDep());
1744 SUnit *SuccSU = Edge.getSUnit();
1745 if (SuccSU != SU) {
1746 Edge.setSUnit(PredSU);
1747 scheduleDAG->RemovePred(SuccSU, Edge);
1748 scheduleDAG->AddPred(SU, Edge);
1749 Edge.setSUnit(SU);
1750 scheduleDAG->AddPred(SuccSU, Edge);
1751 --i;
1752 }
1753 }
1754 outer_loop_continue:;
1755 }
1756}
1757
Evan Chengd38c22b2006-05-11 23:55:42 +00001758/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1759/// it as a def&use operand. Add a pseudo control edge from it to the other
1760/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001761/// first (lower in the schedule). If both nodes are two-address, favor the
1762/// one that has a CopyToReg use (more likely to be a loop induction update).
1763/// If both are two-address, but one is commutable while the other is not
1764/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001765template<class SF>
1766void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001767 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001768 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001769 if (!SU->isTwoAddress)
1770 continue;
1771
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001772 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001773 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001774 continue;
1775
Dan Gohman17059682008-07-17 19:10:17 +00001776 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001777 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001778 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001779 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001780 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001781 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1782 continue;
1783 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1784 if (DU->getNodeId() == -1)
1785 continue;
1786 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1787 if (!DUSU) continue;
1788 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1789 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001790 if (I->isCtrl()) continue;
1791 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001792 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001793 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001794 // Be conservative. Ignore if nodes aren't at roughly the same
1795 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001796 if (SuccSU->getHeight() < SU->getHeight() &&
1797 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001798 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001799 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1800 // constrains whatever is using the copy, instead of the copy
1801 // itself. In the case that the copy is coalesced, this
1802 // preserves the intent of the pseudo two-address heurietics.
1803 while (SuccSU->Succs.size() == 1 &&
1804 SuccSU->getNode()->isMachineOpcode() &&
1805 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001806 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001807 SuccSU = SuccSU->Succs.front().getSUnit();
1808 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001809 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1810 continue;
1811 // Don't constrain nodes with physical register defs if the
1812 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001813 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001814 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001815 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001816 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001817 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1818 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001819 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001820 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1821 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1822 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001823 continue;
1824 if ((!canClobber(SuccSU, DUSU) ||
1825 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1826 (!SU->isCommutable && SuccSU->isCommutable)) &&
1827 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001828 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001829 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001830 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001831 /*Reg=*/0, /*isNormalMemory=*/false,
1832 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001833 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001834 }
1835 }
1836 }
1837 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001838}
1839
Evan Cheng6730f032007-01-08 23:55:53 +00001840/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1841/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001842template<class SF>
1843void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001844 SethiUllmanNumbers.assign(SUnits->size(), 0);
1845
1846 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001847 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001848}
Evan Chengd38c22b2006-05-11 23:55:42 +00001849
Roman Levenstein30d09512008-03-27 09:44:37 +00001850/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001851/// predecessors of the successors of the SUnit SU. Stop when the provided
1852/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001853static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1854 unsigned Limit) {
1855 unsigned Sum = 0;
1856 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1857 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001858 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001859 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1860 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001861 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001862 if (!PredSU->isScheduled)
1863 if (++Sum > Limit)
1864 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001865 }
1866 }
1867 return Sum;
1868}
1869
Evan Chengd38c22b2006-05-11 23:55:42 +00001870
1871// Top down
1872bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001873 unsigned LPriority = SPQ->getNodePriority(left);
1874 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001875 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1876 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001877 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1878 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001879 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1880 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001881
1882 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1883 return false;
1884 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1885 return true;
1886
Evan Chengd38c22b2006-05-11 23:55:42 +00001887 if (LIsFloater)
1888 LBonus -= 2;
1889 if (RIsFloater)
1890 RBonus -= 2;
1891 if (left->NumSuccs == 1)
1892 LBonus += 2;
1893 if (right->NumSuccs == 1)
1894 RBonus += 2;
1895
Evan Cheng73bdf042008-03-01 00:39:47 +00001896 if (LPriority+LBonus != RPriority+RBonus)
1897 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001898
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001899 if (left->getDepth() != right->getDepth())
1900 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001901
1902 if (left->NumSuccsLeft != right->NumSuccsLeft)
1903 return left->NumSuccsLeft > right->NumSuccsLeft;
1904
Roman Levenstein6b371142008-04-29 09:07:59 +00001905 assert(left->NodeQueueId && right->NodeQueueId &&
1906 "NodeQueueId cannot be zero");
1907 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001908}
1909
Evan Chengd38c22b2006-05-11 23:55:42 +00001910//===----------------------------------------------------------------------===//
1911// Public Constructor Functions
1912//===----------------------------------------------------------------------===//
1913
Dan Gohmandfaf6462009-02-11 04:27:20 +00001914llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001915llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001916 const TargetMachine &TM = IS->TM;
1917 const TargetInstrInfo *TII = TM.getInstrInfo();
1918 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001919
Evan Chenga77f3d32010-07-21 06:09:07 +00001920 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00001921 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001922 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001923 PQ->setScheduleDAG(SD);
1924 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001925}
1926
Dan Gohmandfaf6462009-02-11 04:27:20 +00001927llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001928llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001929 const TargetMachine &TM = IS->TM;
1930 const TargetInstrInfo *TII = TM.getInstrInfo();
1931 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001932
Evan Chenga77f3d32010-07-21 06:09:07 +00001933 TDRegReductionPriorityQueue *PQ =
1934 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001935 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001936 PQ->setScheduleDAG(SD);
1937 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001938}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001939
1940llvm::ScheduleDAGSDNodes *
1941llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1942 const TargetMachine &TM = IS->TM;
1943 const TargetInstrInfo *TII = TM.getInstrInfo();
1944 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1945
Evan Chenga77f3d32010-07-21 06:09:07 +00001946 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00001947 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001948 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
1949 PQ->setScheduleDAG(SD);
1950 return SD;
1951}
1952
1953llvm::ScheduleDAGSDNodes *
1954llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1955 const TargetMachine &TM = IS->TM;
1956 const TargetInstrInfo *TII = TM.getInstrInfo();
1957 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00001958 const TargetLowering *TLI = &IS->getTargetLowering();
Evan Chengbdd062d2010-05-20 06:13:19 +00001959
Evan Chenga77f3d32010-07-21 06:09:07 +00001960 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00001961 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00001962 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001963 PQ->setScheduleDAG(SD);
1964 return SD;
1965}