blob: 4c3e4e3b076801d44e08320e3d403dedb7933405 [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 Cheng37b740c2010-07-24 00:39:05 +000058 "Bottom-up register pressure aware list scheduling "
59 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000060 createHybridListDAGScheduler);
61
Evan Cheng37b740c2010-07-24 00:39:05 +000062static RegisterScheduler
63 ILPListDAGScheduler("list-ilp",
64 "Bottom-up register pressure aware list scheduling "
65 "which tries to balance ILP and register pressure",
66 createILPListDAGScheduler);
67
Evan Chengd38c22b2006-05-11 23:55:42 +000068namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000069//===----------------------------------------------------------------------===//
70/// ScheduleDAGRRList - The actual register reduction list scheduler
71/// implementation. This supports both top-down and bottom-up scheduling.
72///
Nick Lewycky02d5f772009-10-25 06:33:48 +000073class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000074private:
75 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
76 /// it is top-down.
77 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000078
Evan Chengbdd062d2010-05-20 06:13:19 +000079 /// NeedLatency - True if the scheduler will make use of latency information.
80 ///
81 bool NeedLatency;
82
Evan Chengd38c22b2006-05-11 23:55:42 +000083 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000084 SchedulingPriorityQueue *AvailableQueue;
85
Dan Gohmanc07f6862008-09-23 18:50:48 +000086 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000087 /// that are "live". These nodes must be scheduled before any other nodes that
88 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000089 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000090 std::vector<SUnit*> LiveRegDefs;
91 std::vector<unsigned> LiveRegCycles;
92
Dan Gohmanad2134d2008-11-25 00:52:40 +000093 /// Topo - A topological ordering for SUnits which permits fast IsReachable
94 /// and similar queries.
95 ScheduleDAGTopologicalSort Topo;
96
Evan Chengd38c22b2006-05-11 23:55:42 +000097public:
Dan Gohman619ef482009-01-15 19:20:50 +000098 ScheduleDAGRRList(MachineFunction &mf,
Evan Chengbdd062d2010-05-20 06:13:19 +000099 bool isbottomup, bool needlatency,
Evan Cheng2c977312008-07-01 18:05:03 +0000100 SchedulingPriorityQueue *availqueue)
Evan Chengbdd062d2010-05-20 06:13:19 +0000101 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup), NeedLatency(needlatency),
Dan Gohmanad2134d2008-11-25 00:52:40 +0000102 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000103 }
104
105 ~ScheduleDAGRRList() {
106 delete AvailableQueue;
107 }
108
109 void Schedule();
110
Roman Levenstein733a4d62008-03-26 11:23:38 +0000111 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
113 return Topo.IsReachable(SU, TargetSU);
114 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000115
Dan Gohman60d68442009-01-29 19:49:27 +0000116 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000117 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000118 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
119 return Topo.WillCreateCycle(SU, TargetSU);
120 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000121
Dan Gohman2d170892008-12-09 22:54:47 +0000122 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000123 /// This returns true if this is a new predecessor.
124 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000125 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000126 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000127 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000128 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000129
Dan Gohman2d170892008-12-09 22:54:47 +0000130 /// RemovePred - removes a predecessor edge from SUnit SU.
131 /// This returns true if an edge was removed.
132 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000133 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000134 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000135 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000136 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000137
Evan Chengd38c22b2006-05-11 23:55:42 +0000138private:
Dan Gohman60d68442009-01-29 19:49:27 +0000139 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000140 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000141 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000142 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000143 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000144 void ScheduleNodeBottomUp(SUnit*, unsigned);
145 void ScheduleNodeTopDown(SUnit*, unsigned);
146 void UnscheduleNodeBottomUp(SUnit*);
147 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
148 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000149 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
150 const TargetRegisterClass*,
151 const TargetRegisterClass*,
152 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000153 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000154 void ListScheduleTopDown();
155 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000156
157
158 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000159 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000160 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000161 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000162 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000163 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000164 if (NewNode->NodeNum >= NumSUnits)
165 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000166 return NewNode;
167 }
168
Roman Levenstein733a4d62008-03-26 11:23:38 +0000169 /// CreateClone - Creates a new SUnit from an existing one.
170 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000171 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000172 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000173 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000174 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000175 if (NewNode->NodeNum >= NumSUnits)
176 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000177 return NewNode;
178 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000179
Evan Chengbdd062d2010-05-20 06:13:19 +0000180 /// ForceUnitLatencies - Register-pressure-reducing scheduling doesn't
181 /// need actual latency information but the hybrid scheduler does.
182 bool ForceUnitLatencies() const {
183 return !NeedLatency;
184 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000185};
186} // end anonymous namespace
187
188
189/// Schedule - Schedule the DAG using list scheduling.
190void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000191 DEBUG(dbgs()
192 << "********** List Scheduling BB#" << BB->getNumber()
193 << " **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000194
Dan Gohmanc07f6862008-09-23 18:50:48 +0000195 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000196 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
197 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000198
Dan Gohman04543e72008-12-23 18:36:58 +0000199 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000200 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000201
Evan Chengd38c22b2006-05-11 23:55:42 +0000202 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000203 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000204 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000205
Dan Gohman46520a22008-06-21 19:18:17 +0000206 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000207
Evan Chengd38c22b2006-05-11 23:55:42 +0000208 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
209 if (isBottomUp)
210 ListScheduleBottomUp();
211 else
212 ListScheduleTopDown();
213
214 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000215}
Evan Chengd38c22b2006-05-11 23:55:42 +0000216
217//===----------------------------------------------------------------------===//
218// Bottom-Up Scheduling
219//===----------------------------------------------------------------------===//
220
Evan Chengd38c22b2006-05-11 23:55:42 +0000221/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000222/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000223void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000224 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000225
Evan Chengd38c22b2006-05-11 23:55:42 +0000226#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000227 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000228 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000229 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000230 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000231 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000232 }
233#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000234 --PredSU->NumSuccsLeft;
235
Evan Chengbdd062d2010-05-20 06:13:19 +0000236 if (!ForceUnitLatencies()) {
237 // Updating predecessor's height. This is now the cycle when the
238 // predecessor can be scheduled without causing a pipeline stall.
239 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
240 }
241
Dan Gohmanb9543432009-02-10 23:27:53 +0000242 // If all the node's successors are scheduled, this node is ready
243 // to be scheduled. Ignore the special EntrySU node.
244 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000245 PredSU->isAvailable = true;
246 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000247 }
248}
249
Dan Gohmanb9543432009-02-10 23:27:53 +0000250void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000251 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000252 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000253 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000254 ReleasePred(SU, &*I);
255 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000256 // This is a physical register dependency and it's impossible or
257 // expensive to copy the register. Make sure nothing that can
258 // clobber the register is scheduled between the predecessor and
259 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000260 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000261 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000262 LiveRegDefs[I->getReg()] = I->getSUnit();
263 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000264 }
265 }
266 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000267}
268
269/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
270/// count of its predecessors. If a predecessor pending count is zero, add it to
271/// the Available queue.
272void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000273 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000274 DEBUG(SU->dump(this));
275
Evan Chengbdd062d2010-05-20 06:13:19 +0000276#ifndef NDEBUG
277 if (CurCycle < SU->getHeight())
278 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
279#endif
280
281 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000282 SU->setHeightToAtLeast(CurCycle);
283 Sequence.push_back(SU);
284
Evan Cheng28590382010-07-21 23:53:58 +0000285 AvailableQueue->ScheduledNode(SU);
286
Dan Gohmanb9543432009-02-10 23:27:53 +0000287 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000288
289 // Release all the implicit physical register defs that are live.
290 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
291 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000292 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000293 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000294 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000295 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000296 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000297 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000298 LiveRegDefs[I->getReg()] = NULL;
299 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000300 }
301 }
302 }
303
Evan Chengd38c22b2006-05-11 23:55:42 +0000304 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000305}
306
Evan Cheng5924bf72007-09-25 01:54:36 +0000307/// CapturePred - This does the opposite of ReleasePred. Since SU is being
308/// unscheduled, incrcease the succ left count of its predecessors. Remove
309/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000310void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
311 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000312 if (PredSU->isAvailable) {
313 PredSU->isAvailable = false;
314 if (!PredSU->isPending)
315 AvailableQueue->remove(PredSU);
316 }
317
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000318 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000319 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000320}
321
322/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
323/// its predecessor states to reflect the change.
324void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000325 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000326 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000327
Evan Cheng5924bf72007-09-25 01:54:36 +0000328 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
329 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000330 CapturePred(&*I);
Evan Chengcc2efe12010-05-28 23:26:21 +0000331 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000332 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000333 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000334 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000335 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000336 LiveRegDefs[I->getReg()] = NULL;
337 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000338 }
339 }
340
341 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
342 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000343 if (I->isAssignedRegDep()) {
344 if (!LiveRegDefs[I->getReg()]) {
345 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000346 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000348 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
349 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000350 }
351 }
352
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000353 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000354 SU->isScheduled = false;
355 SU->isAvailable = true;
356 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000357 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000358}
359
Evan Cheng8e136a92007-09-26 21:36:17 +0000360/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000361/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000362void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
363 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000364 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000365 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000366 OldSU = Sequence.back();
367 Sequence.pop_back();
368 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000369 // Don't try to remove SU from AvailableQueue.
370 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000371 UnscheduleNodeBottomUp(OldSU);
372 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000373 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000374 }
375
Dan Gohman60d68442009-01-29 19:49:27 +0000376 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000377
378 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000379}
380
Evan Cheng3b245872010-02-05 01:27:11 +0000381static bool isOperandOf(const SUnit *SU, SDNode *N) {
382 for (const SDNode *SUNode = SU->getNode(); SUNode;
383 SUNode = SUNode->getFlaggedNode()) {
384 if (SUNode->isOperandOf(N))
385 return true;
386 }
387 return false;
388}
389
Evan Cheng5924bf72007-09-25 01:54:36 +0000390/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
391/// successors to the newly created node.
392SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000393 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000394 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000395
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000396 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000397 if (!N)
398 return NULL;
399
400 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000401 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000402 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000403 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000404 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000405 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000406 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000407 TryUnfold = true;
408 }
Evan Cheng79e97132007-10-05 01:39:18 +0000409 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000410 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000411 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000412 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000413 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000414 }
415
416 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000417 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000418 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000419 return NULL;
420
Evan Chengbdd062d2010-05-20 06:13:19 +0000421 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000422 assert(NewNodes.size() == 2 && "Expected a load folding node!");
423
424 N = NewNodes[1];
425 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000426 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000427 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000428 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000429 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
430 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000431 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000432
Dan Gohmane52e0892008-11-11 21:34:44 +0000433 // LoadNode may already exist. This can happen when there is another
434 // load from the same location and producing the same type of value
435 // but it has different alignment or volatileness.
436 bool isNewLoad = true;
437 SUnit *LoadSU;
438 if (LoadNode->getNodeId() != -1) {
439 LoadSU = &SUnits[LoadNode->getNodeId()];
440 isNewLoad = false;
441 } else {
442 LoadSU = CreateNewSUnit(LoadNode);
443 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000444 ComputeLatency(LoadSU);
445 }
446
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000447 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000448 assert(N->getNodeId() == -1 && "Node already inserted!");
449 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000450
Dan Gohman17059682008-07-17 19:10:17 +0000451 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000452 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000453 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000454 NewSU->isTwoAddress = true;
455 break;
456 }
457 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000458 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000459 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000460 ComputeLatency(NewSU);
461
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000462 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000463 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000464 SmallVector<SDep, 4> ChainSuccs;
465 SmallVector<SDep, 4> LoadPreds;
466 SmallVector<SDep, 4> NodePreds;
467 SmallVector<SDep, 4> NodeSuccs;
468 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
469 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000470 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000471 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000472 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000473 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000474 else
Dan Gohman2d170892008-12-09 22:54:47 +0000475 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000476 }
477 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
478 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000479 if (I->isCtrl())
480 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000481 else
Dan Gohman2d170892008-12-09 22:54:47 +0000482 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000483 }
484
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000485 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000486 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
487 const SDep &Pred = ChainPreds[i];
488 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000489 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000490 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000491 }
Evan Cheng79e97132007-10-05 01:39:18 +0000492 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000493 const SDep &Pred = LoadPreds[i];
494 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000495 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000496 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000497 }
498 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000499 const SDep &Pred = NodePreds[i];
500 RemovePred(SU, Pred);
501 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000502 }
503 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000504 SDep D = NodeSuccs[i];
505 SUnit *SuccDep = D.getSUnit();
506 D.setSUnit(SU);
507 RemovePred(SuccDep, D);
508 D.setSUnit(NewSU);
509 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000510 }
511 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000512 SDep D = ChainSuccs[i];
513 SUnit *SuccDep = D.getSUnit();
514 D.setSUnit(SU);
515 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000516 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000517 D.setSUnit(LoadSU);
518 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000519 }
Evan Cheng79e97132007-10-05 01:39:18 +0000520 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000521
522 // Add a data dependency to reflect that NewSU reads the value defined
523 // by LoadSU.
524 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000525
Evan Cheng91e0fc92007-12-18 08:42:10 +0000526 if (isNewLoad)
527 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000528 AvailableQueue->addNode(NewSU);
529
530 ++NumUnfolds;
531
532 if (NewSU->NumSuccsLeft == 0) {
533 NewSU->isAvailable = true;
534 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000535 }
536 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000537 }
538
Evan Chengbdd062d2010-05-20 06:13:19 +0000539 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000540 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000541
542 // New SUnit has the exact same predecessors.
543 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
544 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000545 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000546 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000547
548 // Only copy scheduled successors. Cut them from old node's successor
549 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000550 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000551 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
552 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000553 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000554 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000555 SUnit *SuccSU = I->getSUnit();
556 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000557 SDep D = *I;
558 D.setSUnit(NewSU);
559 AddPred(SuccSU, D);
560 D.setSUnit(SU);
561 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000562 }
563 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000564 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000565 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000566
567 AvailableQueue->updateNode(SU);
568 AvailableQueue->addNode(NewSU);
569
Evan Cheng1ec79b42007-09-27 07:09:03 +0000570 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000571 return NewSU;
572}
573
Evan Chengb2c42c62009-01-12 03:19:55 +0000574/// InsertCopiesAndMoveSuccs - Insert register copies and move all
575/// scheduled successors of the given SUnit to the last copy.
576void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
577 const TargetRegisterClass *DestRC,
578 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000579 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000580 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000581 CopyFromSU->CopySrcRC = SrcRC;
582 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000583
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000584 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000585 CopyToSU->CopySrcRC = DestRC;
586 CopyToSU->CopyDstRC = SrcRC;
587
588 // Only copy scheduled successors. Cut them from old node's successor
589 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000590 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000591 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
592 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000593 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000594 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000595 SUnit *SuccSU = I->getSUnit();
596 if (SuccSU->isScheduled) {
597 SDep D = *I;
598 D.setSUnit(CopyToSU);
599 AddPred(SuccSU, D);
600 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000601 }
602 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000603 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000604 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000605
Dan Gohman2d170892008-12-09 22:54:47 +0000606 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
607 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000608
609 AvailableQueue->updateNode(SU);
610 AvailableQueue->addNode(CopyFromSU);
611 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000612 Copies.push_back(CopyFromSU);
613 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000614
Evan Chengb2c42c62009-01-12 03:19:55 +0000615 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000616}
617
618/// getPhysicalRegisterVT - Returns the ValueType of the physical register
619/// definition of the specified node.
620/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000621static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000622 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000623 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000624 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000625 unsigned NumRes = TID.getNumDefs();
626 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000627 if (Reg == *ImpDef)
628 break;
629 ++NumRes;
630 }
631 return N->getValueType(NumRes);
632}
633
Evan Chengb8905c42009-03-04 01:41:49 +0000634/// CheckForLiveRegDef - Return true and update live register vector if the
635/// specified register def of the specified SUnit clobbers any "live" registers.
636static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
637 std::vector<SUnit*> &LiveRegDefs,
638 SmallSet<unsigned, 4> &RegAdded,
639 SmallVector<unsigned, 4> &LRegs,
640 const TargetRegisterInfo *TRI) {
641 bool Added = false;
642 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
643 if (RegAdded.insert(Reg)) {
644 LRegs.push_back(Reg);
645 Added = true;
646 }
647 }
648 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
649 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
650 if (RegAdded.insert(*Alias)) {
651 LRegs.push_back(*Alias);
652 Added = true;
653 }
654 }
655 return Added;
656}
657
Evan Cheng5924bf72007-09-25 01:54:36 +0000658/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
659/// scheduling of the given node to satisfy live physical register dependencies.
660/// If the specific node is the last one that's available to schedule, do
661/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000662bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
663 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000664 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000665 return false;
666
Evan Chenge6f92252007-09-27 18:46:06 +0000667 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000668 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000669 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
670 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000671 if (I->isAssignedRegDep())
672 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
673 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000674 }
675
Dan Gohman072734e2008-11-13 23:24:17 +0000676 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000677 if (Node->getOpcode() == ISD::INLINEASM) {
678 // Inline asm can clobber physical defs.
679 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000680 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000681 --NumOps; // Ignore the flag operand.
682
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000683 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000684 unsigned Flags =
685 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000686 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000687
688 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000689 if (InlineAsm::isRegDefKind(Flags) ||
690 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000691 // Check for def of register or earlyclobber register.
692 for (; NumVals; --NumVals, ++i) {
693 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
694 if (TargetRegisterInfo::isPhysicalRegister(Reg))
695 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
696 }
697 } else
698 i += NumVals;
699 }
700 continue;
701 }
702
Dan Gohman072734e2008-11-13 23:24:17 +0000703 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000704 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000705 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000706 if (!TID.ImplicitDefs)
707 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000708 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
709 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000710 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000711 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000712}
713
Evan Cheng1ec79b42007-09-27 07:09:03 +0000714
Evan Chengd38c22b2006-05-11 23:55:42 +0000715/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
716/// schedulers.
717void ScheduleDAGRRList::ListScheduleBottomUp() {
718 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000719
720 // Release any predecessors of the special Exit node.
721 ReleasePredecessors(&ExitSU, CurCycle);
722
Evan Chengd38c22b2006-05-11 23:55:42 +0000723 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000724 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000725 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000726 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
727 RootSU->isAvailable = true;
728 AvailableQueue->push(RootSU);
729 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000730
731 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000732 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000733 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000734 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000735 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000736 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000737 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000738 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000739 SUnit *CurSU = AvailableQueue->pop();
740 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000741 SmallVector<unsigned, 4> LRegs;
742 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
743 break;
744 Delayed = true;
745 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000746
747 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
748 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000749 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000750 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000751
752 // All candidates are delayed due to live physical reg dependencies.
753 // Try backtracking, code duplication, or inserting cross class copies
754 // to resolve it.
755 if (Delayed && !CurSU) {
756 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
757 SUnit *TrySU = NotReady[i];
758 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
759
760 // Try unscheduling up to the point where it's safe to schedule
761 // this node.
762 unsigned LiveCycle = CurCycle;
763 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
764 unsigned Reg = LRegs[j];
765 unsigned LCycle = LiveRegCycles[Reg];
766 LiveCycle = std::min(LiveCycle, LCycle);
767 }
768 SUnit *OldSU = Sequence[LiveCycle];
769 if (!WillCreateCycle(TrySU, OldSU)) {
770 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
771 // Force the current node to be scheduled before the node that
772 // requires the physical reg dep.
773 if (OldSU->isAvailable) {
774 OldSU->isAvailable = false;
775 AvailableQueue->remove(OldSU);
776 }
Dan Gohman2d170892008-12-09 22:54:47 +0000777 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
778 /*Reg=*/0, /*isNormalMemory=*/false,
779 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000780 // If one or more successors has been unscheduled, then the current
781 // node is no longer avaialable. Schedule a successor that's now
782 // available instead.
783 if (!TrySU->isAvailable)
784 CurSU = AvailableQueue->pop();
785 else {
786 CurSU = TrySU;
787 TrySU->isPending = false;
788 NotReady.erase(NotReady.begin()+i);
789 }
790 break;
791 }
792 }
793
794 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000795 // Can't backtrack. If it's too expensive to copy the value, then try
796 // duplicate the nodes that produces these "too expensive to copy"
797 // values to break the dependency. In case even that doesn't work,
798 // insert cross class copies.
799 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000800 SUnit *TrySU = NotReady[0];
801 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
802 assert(LRegs.size() == 1 && "Can't handle this yet!");
803 unsigned Reg = LRegs[0];
804 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000805 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000806 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000807 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000808 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
809
810 // If cross copy register class is null, then it must be possible copy
811 // the value directly. Do not try duplicate the def.
812 SUnit *NewDef = 0;
813 if (DestRC)
814 NewDef = CopyAndMoveSuccessors(LRDef);
815 else
816 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000817 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000818 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000819 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000820 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000821 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000822 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000823 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000824 /*Reg=*/0, /*isNormalMemory=*/false,
825 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000826 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000827 NewDef = Copies.back();
828 }
829
Evan Chengbdd062d2010-05-20 06:13:19 +0000830 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000831 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000832 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000833 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000834 /*Reg=*/0, /*isNormalMemory=*/false,
835 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000836 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000837 TrySU->isAvailable = false;
838 CurSU = NewDef;
839 }
840
Dan Gohman60d68442009-01-29 19:49:27 +0000841 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000842 }
843
Evan Chengd38c22b2006-05-11 23:55:42 +0000844 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000845 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
846 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000847 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000848 if (NotReady[i]->isAvailable)
849 AvailableQueue->push(NotReady[i]);
850 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000851 NotReady.clear();
852
Dan Gohmanc602dd42008-11-21 00:10:42 +0000853 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000854 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000855 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000856 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000857 }
858
Evan Chengd38c22b2006-05-11 23:55:42 +0000859 // Reverse the order if it is bottom up.
860 std::reverse(Sequence.begin(), Sequence.end());
861
Evan Chengd38c22b2006-05-11 23:55:42 +0000862#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000863 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000864#endif
865}
866
867//===----------------------------------------------------------------------===//
868// Top-Down Scheduling
869//===----------------------------------------------------------------------===//
870
871/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000872/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000873void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000874 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000875
Evan Chengd38c22b2006-05-11 23:55:42 +0000876#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000877 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000878 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000879 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000880 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000881 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000882 }
883#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000884 --SuccSU->NumPredsLeft;
885
Dan Gohmanb9543432009-02-10 23:27:53 +0000886 // If all the node's predecessors are scheduled, this node is ready
887 // to be scheduled. Ignore the special ExitSU node.
888 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000889 SuccSU->isAvailable = true;
890 AvailableQueue->push(SuccSU);
891 }
892}
893
Dan Gohmanb9543432009-02-10 23:27:53 +0000894void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
895 // Top down: release successors
896 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
897 I != E; ++I) {
898 assert(!I->isAssignedRegDep() &&
899 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
900
901 ReleaseSucc(SU, &*I);
902 }
903}
904
Evan Chengd38c22b2006-05-11 23:55:42 +0000905/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
906/// count of its successors. If a successor pending count is zero, add it to
907/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000908void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000909 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000910 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000911
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000912 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
913 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000914 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000915
Dan Gohmanb9543432009-02-10 23:27:53 +0000916 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000917 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000918 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000919}
920
Dan Gohman54a187e2007-08-20 19:28:38 +0000921/// ListScheduleTopDown - The main loop of list scheduling for top-down
922/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000923void ScheduleDAGRRList::ListScheduleTopDown() {
924 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000925 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000926
Dan Gohmanb9543432009-02-10 23:27:53 +0000927 // Release any successors of the special Entry node.
928 ReleaseSuccessors(&EntrySU);
929
Evan Chengd38c22b2006-05-11 23:55:42 +0000930 // All leaves to Available queue.
931 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
932 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000933 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000934 AvailableQueue->push(&SUnits[i]);
935 SUnits[i].isAvailable = true;
936 }
937 }
938
Evan Chengd38c22b2006-05-11 23:55:42 +0000939 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000940 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000941 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000942 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000943 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000944
Dan Gohmanc602dd42008-11-21 00:10:42 +0000945 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000946 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000947 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000948 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000949 }
950
Evan Chengd38c22b2006-05-11 23:55:42 +0000951#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000952 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000953#endif
954}
955
956
Evan Chengd38c22b2006-05-11 23:55:42 +0000957//===----------------------------------------------------------------------===//
958// RegReductionPriorityQueue Implementation
959//===----------------------------------------------------------------------===//
960//
961// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
962// to reduce register pressure.
963//
964namespace {
965 template<class SF>
966 class RegReductionPriorityQueue;
967
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000968 /// bu_ls_rr_sort - Priority function for bottom up register pressure
969 // reduction scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000970 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
971 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
972 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
973 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
974
975 bool operator()(const SUnit* left, const SUnit* right) const;
976 };
977
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000978 // td_ls_rr_sort - Priority function for top down register pressure reduction
979 // scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000980 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
981 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
982 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
983 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
984
985 bool operator()(const SUnit* left, const SUnit* right) const;
986 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000987
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000988 // src_ls_rr_sort - Priority function for source order scheduler.
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000989 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
990 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
991 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
992 : SPQ(spq) {}
993 src_ls_rr_sort(const src_ls_rr_sort &RHS)
994 : SPQ(RHS.SPQ) {}
995
996 bool operator()(const SUnit* left, const SUnit* right) const;
997 };
Evan Chengbdd062d2010-05-20 06:13:19 +0000998
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000999 // hybrid_ls_rr_sort - Priority function for hybrid scheduler.
Evan Chengbdd062d2010-05-20 06:13:19 +00001000 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1001 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
1002 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
1003 : SPQ(spq) {}
1004 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1005 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001006
Evan Chengbdd062d2010-05-20 06:13:19 +00001007 bool operator()(const SUnit* left, const SUnit* right) const;
1008 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001009
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001010 // ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1011 // scheduler.
Evan Cheng37b740c2010-07-24 00:39:05 +00001012 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1013 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1014 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1015 : SPQ(spq) {}
1016 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1017 : SPQ(RHS.SPQ) {}
1018
1019 bool operator()(const SUnit* left, const SUnit* right) const;
1020 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001021} // end anonymous namespace
1022
Dan Gohman186f65d2008-11-20 03:30:37 +00001023/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1024/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001025static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001026CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001027 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1028 if (SethiUllmanNumber != 0)
1029 return SethiUllmanNumber;
1030
1031 unsigned Extra = 0;
1032 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1033 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001034 if (I->isCtrl()) continue; // ignore chain preds
1035 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001036 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001037 if (PredSethiUllman > SethiUllmanNumber) {
1038 SethiUllmanNumber = PredSethiUllman;
1039 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001040 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001041 ++Extra;
1042 }
1043
1044 SethiUllmanNumber += Extra;
1045
1046 if (SethiUllmanNumber == 0)
1047 SethiUllmanNumber = 1;
1048
1049 return SethiUllmanNumber;
1050}
1051
Evan Chengd38c22b2006-05-11 23:55:42 +00001052namespace {
1053 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001054 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001055 std::vector<SUnit*> Queue;
1056 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001057 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001058 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001059
Dan Gohman3f656df2008-11-20 02:45:51 +00001060 protected:
1061 // SUnits - The SUnits for the current graph.
1062 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001063
1064 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001065 const TargetInstrInfo *TII;
1066 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001067 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001068 ScheduleDAGRRList *scheduleDAG;
1069
Dan Gohman186f65d2008-11-20 03:30:37 +00001070 // SethiUllmanNumbers - The SethiUllman number for each node.
1071 std::vector<unsigned> SethiUllmanNumbers;
1072
Evan Chenga77f3d32010-07-21 06:09:07 +00001073 /// RegPressure - Tracking current reg pressure per register class.
1074 ///
Evan Cheng28590382010-07-21 23:53:58 +00001075 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001076
1077 /// RegLimit - Tracking the number of allocatable registers per register
1078 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001079 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001080
Dan Gohman3f656df2008-11-20 02:45:51 +00001081 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001082 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001083 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001084 const TargetInstrInfo *tii,
1085 const TargetRegisterInfo *tri,
1086 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001087 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001088 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001089 if (TracksRegPressure) {
1090 unsigned NumRC = TRI->getNumRegClasses();
1091 RegLimit.resize(NumRC);
1092 RegPressure.resize(NumRC);
1093 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1094 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1095 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1096 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001097 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001098 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001099 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001100
1101 void initNodes(std::vector<SUnit> &sunits) {
1102 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001103 // Add pseudo dependency edges for two-address nodes.
1104 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001105 // Reroute edges to nodes with multiple uses.
1106 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001107 // Calculate node priorities.
1108 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001109 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001110
Dan Gohman186f65d2008-11-20 03:30:37 +00001111 void addNode(const SUnit *SU) {
1112 unsigned SUSize = SethiUllmanNumbers.size();
1113 if (SUnits->size() > SUSize)
1114 SethiUllmanNumbers.resize(SUSize*2, 0);
1115 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1116 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001117
Dan Gohman186f65d2008-11-20 03:30:37 +00001118 void updateNode(const SUnit *SU) {
1119 SethiUllmanNumbers[SU->NodeNum] = 0;
1120 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1121 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001122
Dan Gohman186f65d2008-11-20 03:30:37 +00001123 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001124 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001125 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001126 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001127 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001128
1129 unsigned getNodePriority(const SUnit *SU) const {
1130 assert(SU->NodeNum < SethiUllmanNumbers.size());
1131 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001132 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001133 // CopyToReg should be close to its uses to facilitate coalescing and
1134 // avoid spilling.
1135 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001136 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1137 Opc == TargetOpcode::SUBREG_TO_REG ||
1138 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001139 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1140 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001141 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001142 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1143 // If SU does not have a register use, i.e. it doesn't produce a value
1144 // that would be consumed (e.g. store), then it terminates a chain of
1145 // computation. Give it a large SethiUllman number so it will be
1146 // scheduled right before its predecessors that it doesn't lengthen
1147 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001148 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001149 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1150 // If SU does not have a register def, schedule it close to its uses
1151 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001152 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001153 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001154 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001155
1156 unsigned getNodeOrdering(const SUnit *SU) const {
1157 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1158 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001159
Evan Chengd38c22b2006-05-11 23:55:42 +00001160 bool empty() const { return Queue.empty(); }
1161
1162 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001163 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001164 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001165 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001166 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001167
Evan Chengd38c22b2006-05-11 23:55:42 +00001168 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001169 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001170 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001171 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001172 E = Queue.end(); I != E; ++I)
1173 if (Picker(*Best, *I))
1174 Best = I;
1175 SUnit *V = *Best;
1176 if (Best != prior(Queue.end()))
1177 std::swap(*Best, Queue.back());
1178 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001179 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001180 return V;
1181 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001182
Evan Cheng5924bf72007-09-25 01:54:36 +00001183 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001184 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001185 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001186 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1187 SU);
1188 if (I != prior(Queue.end()))
1189 std::swap(*I, Queue.back());
1190 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001191 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001192 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001193
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001194 bool HighRegPressure(const SUnit *SU) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001195 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001196 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001197
Evan Chenga77f3d32010-07-21 06:09:07 +00001198 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1199 I != E; ++I) {
1200 if (I->isCtrl())
1201 continue;
1202 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001203 const SDNode *PN = PredSU->getNode();
1204 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001205 if (PN->getOpcode() == ISD::CopyFromReg) {
1206 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001207 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1208 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001209 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1210 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001211 }
1212 continue;
1213 }
1214 unsigned POpc = PN->getMachineOpcode();
1215 if (POpc == TargetOpcode::IMPLICIT_DEF)
1216 continue;
1217 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1218 EVT VT = PN->getOperand(0).getValueType();
1219 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1220 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1221 // Check if this increases register pressure of the specific register
1222 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001223 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1224 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001225 continue;
1226 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1227 POpc == TargetOpcode::SUBREG_TO_REG) {
1228 EVT VT = PN->getValueType(0);
1229 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1230 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1231 // Check if this increases register pressure of the specific register
1232 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001233 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1234 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001235 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001236 }
1237 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001238 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001239 EVT VT = PN->getValueType(i);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001240 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1241 if (RegPressure[RCId] >= RegLimit[RCId])
1242 return true; // Reg pressure already high.
1243 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Cheng28590382010-07-21 23:53:58 +00001244 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001245 continue;
Evan Chenga77f3d32010-07-21 06:09:07 +00001246 // Check if this increases register pressure of the specific register
1247 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001248 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1249 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001250 }
1251 }
1252
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001253 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001254 }
1255
Evan Chengbf32e542010-07-22 06:24:48 +00001256 void ScheduledNode(SUnit *SU) {
1257 if (!TracksRegPressure)
1258 return;
1259
Evan Chenga77f3d32010-07-21 06:09:07 +00001260 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001261 if (!N->isMachineOpcode()) {
1262 if (N->getOpcode() != ISD::CopyToReg)
1263 return;
1264 } else {
1265 unsigned Opc = N->getMachineOpcode();
1266 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1267 Opc == TargetOpcode::INSERT_SUBREG ||
1268 Opc == TargetOpcode::SUBREG_TO_REG ||
1269 Opc == TargetOpcode::REG_SEQUENCE ||
1270 Opc == TargetOpcode::IMPLICIT_DEF)
1271 return;
1272 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001273
1274 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1275 I != E; ++I) {
1276 if (I->isCtrl())
1277 continue;
1278 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001279 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001280 continue;
1281 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001282 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001283 if (PN->getOpcode() == ISD::CopyFromReg) {
1284 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001285 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1286 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1287 }
1288 continue;
1289 }
1290 unsigned POpc = PN->getMachineOpcode();
1291 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001292 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001293 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1294 EVT VT = PN->getOperand(0).getValueType();
1295 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1296 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1297 continue;
1298 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1299 POpc == TargetOpcode::SUBREG_TO_REG) {
1300 EVT VT = PN->getValueType(0);
1301 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1302 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1303 continue;
1304 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001305 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1306 for (unsigned i = 0; i != NumDefs; ++i) {
1307 EVT VT = PN->getValueType(i);
1308 if (!PN->hasAnyUseOfValue(i))
1309 continue;
1310 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1311 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1312 }
1313 }
1314
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001315 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1316 // may transfer data dependencies to CopyToReg.
1317 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001318 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1319 for (unsigned i = 0; i != NumDefs; ++i) {
1320 EVT VT = N->getValueType(i);
1321 if (!N->hasAnyUseOfValue(i))
1322 continue;
1323 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1324 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1325 // Register pressure tracking is imprecise. This can happen.
1326 RegPressure[RCId] = 0;
1327 else
1328 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1329 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001330 }
Evan Chengbf32e542010-07-22 06:24:48 +00001331
1332 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001333 }
1334
Evan Chengbf32e542010-07-22 06:24:48 +00001335 void UnscheduledNode(SUnit *SU) {
1336 if (!TracksRegPressure)
1337 return;
1338
Evan Chenga77f3d32010-07-21 06:09:07 +00001339 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001340 if (!N->isMachineOpcode()) {
1341 if (N->getOpcode() != ISD::CopyToReg)
1342 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001343 } else {
1344 unsigned Opc = N->getMachineOpcode();
1345 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1346 Opc == TargetOpcode::INSERT_SUBREG ||
1347 Opc == TargetOpcode::SUBREG_TO_REG ||
1348 Opc == TargetOpcode::REG_SEQUENCE ||
1349 Opc == TargetOpcode::IMPLICIT_DEF)
1350 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001351 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001352
1353 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1354 I != E; ++I) {
1355 if (I->isCtrl())
1356 continue;
1357 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001358 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001359 continue;
1360 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001361 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001362 if (PN->getOpcode() == ISD::CopyFromReg) {
1363 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001364 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1365 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1366 }
1367 continue;
1368 }
1369 unsigned POpc = PN->getMachineOpcode();
1370 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001371 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001372 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1373 EVT VT = PN->getOperand(0).getValueType();
1374 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1375 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1376 continue;
1377 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1378 POpc == TargetOpcode::SUBREG_TO_REG) {
1379 EVT VT = PN->getValueType(0);
1380 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1381 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1382 continue;
1383 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001384 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1385 for (unsigned i = 0; i != NumDefs; ++i) {
1386 EVT VT = PN->getValueType(i);
1387 if (!PN->hasAnyUseOfValue(i))
1388 continue;
1389 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001390 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001391 // Register pressure tracking is imprecise. This can happen.
1392 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001393 else
1394 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001395 }
1396 }
1397
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001398 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1399 // may transfer data dependencies to CopyToReg.
1400 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001401 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1402 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1403 EVT VT = N->getValueType(i);
1404 if (VT == MVT::Flag || VT == MVT::Other)
1405 continue;
1406 if (!N->hasAnyUseOfValue(i))
1407 continue;
1408 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1409 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1410 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001411 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001412
Evan Chenga77f3d32010-07-21 06:09:07 +00001413 dumpRegPressure();
1414 }
1415
Dan Gohman3f656df2008-11-20 02:45:51 +00001416 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1417 scheduleDAG = scheduleDag;
1418 }
1419
Evan Chenga77f3d32010-07-21 06:09:07 +00001420 void dumpRegPressure() const {
1421 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1422 E = TRI->regclass_end(); I != E; ++I) {
1423 const TargetRegisterClass *RC = *I;
1424 unsigned Id = RC->getID();
1425 unsigned RP = RegPressure[Id];
1426 if (!RP) continue;
1427 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1428 << '\n');
1429 }
1430 }
1431
Dan Gohman3f656df2008-11-20 02:45:51 +00001432 protected:
1433 bool canClobber(const SUnit *SU, const SUnit *Op);
1434 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001435 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001436 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001437 };
1438
Dan Gohman186f65d2008-11-20 03:30:37 +00001439 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1440 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001441
Dan Gohman186f65d2008-11-20 03:30:37 +00001442 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1443 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001444
1445 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1446 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001447
1448 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1449 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001450
1451 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1452 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001453}
1454
Evan Chengb9e3db62007-03-14 22:43:40 +00001455/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001456/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001457static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001458 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001459 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001460 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001461 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001462 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001463 // If there are bunch of CopyToRegs stacked up, they should be considered
1464 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001465 if (I->getSUnit()->getNode() &&
1466 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001467 Height = closestSucc(I->getSUnit())+1;
1468 if (Height > MaxHeight)
1469 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001470 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001471 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001472}
1473
Evan Cheng61bc51e2007-12-20 02:22:36 +00001474/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001475/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001476static unsigned calcMaxScratches(const SUnit *SU) {
1477 unsigned Scratches = 0;
1478 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001479 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001480 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001481 Scratches++;
1482 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001483 return Scratches;
1484}
1485
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001486template <typename RRSort>
1487static bool BURRSort(const SUnit *left, const SUnit *right,
1488 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001489 unsigned LPriority = SPQ->getNodePriority(left);
1490 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001491 if (LPriority != RPriority)
1492 return LPriority > RPriority;
1493
1494 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1495 // e.g.
1496 // t1 = op t2, c1
1497 // t3 = op t4, c2
1498 //
1499 // and the following instructions are both ready.
1500 // t2 = op c3
1501 // t4 = op c4
1502 //
1503 // Then schedule t2 = op first.
1504 // i.e.
1505 // t4 = op c4
1506 // t2 = op c3
1507 // t1 = op t2, c1
1508 // t3 = op t4, c2
1509 //
1510 // This creates more short live intervals.
1511 unsigned LDist = closestSucc(left);
1512 unsigned RDist = closestSucc(right);
1513 if (LDist != RDist)
1514 return LDist < RDist;
1515
Evan Cheng3a14efa2009-02-12 08:59:45 +00001516 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001517 unsigned LScratch = calcMaxScratches(left);
1518 unsigned RScratch = calcMaxScratches(right);
1519 if (LScratch != RScratch)
1520 return LScratch > RScratch;
1521
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001522 if (left->getHeight() != right->getHeight())
1523 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001524
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001525 if (left->getDepth() != right->getDepth())
1526 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001527
Roman Levenstein6b371142008-04-29 09:07:59 +00001528 assert(left->NodeQueueId && right->NodeQueueId &&
1529 "NodeQueueId cannot be zero");
1530 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001531}
1532
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001533// Bottom up
1534bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1535 return BURRSort(left, right, SPQ);
1536}
1537
1538// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001539bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001540 unsigned LOrder = SPQ->getNodeOrdering(left);
1541 unsigned ROrder = SPQ->getNodeOrdering(right);
1542
1543 // Prefer an ordering where the lower the non-zero order number, the higher
1544 // the preference.
1545 if ((LOrder || ROrder) && LOrder != ROrder)
1546 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1547
1548 return BURRSort(left, right, SPQ);
1549}
1550
Evan Chengbdd062d2010-05-20 06:13:19 +00001551bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001552 bool LHigh = SPQ->HighRegPressure(left);
1553 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001554 // Avoid causing spills. If register pressure is high, schedule for
1555 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001556 if (LHigh && !RHigh)
1557 return true;
1558 else if (!LHigh && RHigh)
1559 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001560 else if (!LHigh && !RHigh) {
Evan Cheng28590382010-07-21 23:53:58 +00001561 // Low register pressure situation, schedule for latency if possible.
1562 bool LStall = left->SchedulingPref == Sched::Latency &&
1563 SPQ->getCurCycle() < left->getHeight();
1564 bool RStall = right->SchedulingPref == Sched::Latency &&
1565 SPQ->getCurCycle() < right->getHeight();
1566 // If scheduling one of the node will cause a pipeline stall, delay it.
1567 // If scheduling either one of the node will cause a pipeline stall, sort
1568 // them according to their height.
1569 // If neither will cause a pipeline stall, try to reduce register pressure.
1570 if (LStall) {
1571 if (!RStall)
1572 return true;
1573 if (left->getHeight() != right->getHeight())
1574 return left->getHeight() > right->getHeight();
1575 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001576 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001577
Evan Cheng28590382010-07-21 23:53:58 +00001578 // If either node is scheduling for latency, sort them by height and latency
1579 // first.
1580 if (left->SchedulingPref == Sched::Latency ||
1581 right->SchedulingPref == Sched::Latency) {
1582 if (left->getHeight() != right->getHeight())
1583 return left->getHeight() > right->getHeight();
1584 if (left->Latency != right->Latency)
1585 return left->Latency > right->Latency;
1586 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001587 }
1588
Evan Chengbdd062d2010-05-20 06:13:19 +00001589 return BURRSort(left, right, SPQ);
1590}
1591
Evan Cheng37b740c2010-07-24 00:39:05 +00001592bool ilp_ls_rr_sort::operator()(const SUnit *left,
1593 const SUnit *right) const {
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001594 bool LHigh = SPQ->HighRegPressure(left);
1595 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001596 // Avoid causing spills. If register pressure is high, schedule for
1597 // register pressure reduction.
1598 if (LHigh && !RHigh)
1599 return true;
1600 else if (!LHigh && RHigh)
1601 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001602 else if (!LHigh && !RHigh) {
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001603 // Low register pressure situation, schedule to maximize instruction level
1604 // parallelism.
Evan Cheng37b740c2010-07-24 00:39:05 +00001605 if (left->NumPreds > right->NumPreds)
1606 return false;
1607 else if (left->NumPreds < right->NumPreds)
1608 return false;
1609 }
1610
1611 return BURRSort(left, right, SPQ);
1612}
1613
Dan Gohman3f656df2008-11-20 02:45:51 +00001614template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001615bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001616RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001617 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001618 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001619 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001620 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001621 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001622 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001623 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001624 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001625 if (DU->getNodeId() != -1 &&
1626 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001627 return true;
1628 }
1629 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001630 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001631 return false;
1632}
1633
Evan Chenga5e595d2007-09-28 22:32:30 +00001634/// hasCopyToRegUse - Return true if SU has a value successor that is a
1635/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001636static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001637 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1638 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001639 if (I->isCtrl()) continue;
1640 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001641 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001642 return true;
1643 }
1644 return false;
1645}
1646
Evan Chengf9891412007-12-20 09:25:31 +00001647/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001648/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001649static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001650 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001651 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001652 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001653 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1654 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001655 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001656 for (const SDNode *SUNode = SU->getNode(); SUNode;
1657 SUNode = SUNode->getFlaggedNode()) {
1658 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001659 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001660 const unsigned *SUImpDefs =
1661 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1662 if (!SUImpDefs)
1663 return false;
1664 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001665 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001666 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001667 continue;
1668 if (!N->hasAnyUseOfValue(i))
1669 continue;
1670 unsigned Reg = ImpDefs[i - NumDefs];
1671 for (;*SUImpDefs; ++SUImpDefs) {
1672 unsigned SUReg = *SUImpDefs;
1673 if (TRI->regsOverlap(Reg, SUReg))
1674 return true;
1675 }
Evan Chengf9891412007-12-20 09:25:31 +00001676 }
1677 }
1678 return false;
1679}
1680
Dan Gohman9a658d72009-03-24 00:49:12 +00001681/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1682/// are not handled well by the general register pressure reduction
1683/// heuristics. When presented with code like this:
1684///
1685/// N
1686/// / |
1687/// / |
1688/// U store
1689/// |
1690/// ...
1691///
1692/// the heuristics tend to push the store up, but since the
1693/// operand of the store has another use (U), this would increase
1694/// the length of that other use (the U->N edge).
1695///
1696/// This function transforms code like the above to route U's
1697/// dependence through the store when possible, like this:
1698///
1699/// N
1700/// ||
1701/// ||
1702/// store
1703/// |
1704/// U
1705/// |
1706/// ...
1707///
1708/// This results in the store being scheduled immediately
1709/// after N, which shortens the U->N live range, reducing
1710/// register pressure.
1711///
1712template<class SF>
1713void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1714 // Visit all the nodes in topological order, working top-down.
1715 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1716 SUnit *SU = &(*SUnits)[i];
1717 // For now, only look at nodes with no data successors, such as stores.
1718 // These are especially important, due to the heuristics in
1719 // getNodePriority for nodes with no data successors.
1720 if (SU->NumSuccs != 0)
1721 continue;
1722 // For now, only look at nodes with exactly one data predecessor.
1723 if (SU->NumPreds != 1)
1724 continue;
1725 // Avoid prescheduling copies to virtual registers, which don't behave
1726 // like other nodes from the perspective of scheduling heuristics.
1727 if (SDNode *N = SU->getNode())
1728 if (N->getOpcode() == ISD::CopyToReg &&
1729 TargetRegisterInfo::isVirtualRegister
1730 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1731 continue;
1732
1733 // Locate the single data predecessor.
1734 SUnit *PredSU = 0;
1735 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1736 EE = SU->Preds.end(); II != EE; ++II)
1737 if (!II->isCtrl()) {
1738 PredSU = II->getSUnit();
1739 break;
1740 }
1741 assert(PredSU);
1742
1743 // Don't rewrite edges that carry physregs, because that requires additional
1744 // support infrastructure.
1745 if (PredSU->hasPhysRegDefs)
1746 continue;
1747 // Short-circuit the case where SU is PredSU's only data successor.
1748 if (PredSU->NumSuccs == 1)
1749 continue;
1750 // Avoid prescheduling to copies from virtual registers, which don't behave
1751 // like other nodes from the perspective of scheduling // heuristics.
1752 if (SDNode *N = SU->getNode())
1753 if (N->getOpcode() == ISD::CopyFromReg &&
1754 TargetRegisterInfo::isVirtualRegister
1755 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1756 continue;
1757
1758 // Perform checks on the successors of PredSU.
1759 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1760 EE = PredSU->Succs.end(); II != EE; ++II) {
1761 SUnit *PredSuccSU = II->getSUnit();
1762 if (PredSuccSU == SU) continue;
1763 // If PredSU has another successor with no data successors, for
1764 // now don't attempt to choose either over the other.
1765 if (PredSuccSU->NumSuccs == 0)
1766 goto outer_loop_continue;
1767 // Don't break physical register dependencies.
1768 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1769 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1770 goto outer_loop_continue;
1771 // Don't introduce graph cycles.
1772 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1773 goto outer_loop_continue;
1774 }
1775
1776 // Ok, the transformation is safe and the heuristics suggest it is
1777 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001778 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1779 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001780 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001781 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1782 SDep Edge = PredSU->Succs[i];
1783 assert(!Edge.isAssignedRegDep());
1784 SUnit *SuccSU = Edge.getSUnit();
1785 if (SuccSU != SU) {
1786 Edge.setSUnit(PredSU);
1787 scheduleDAG->RemovePred(SuccSU, Edge);
1788 scheduleDAG->AddPred(SU, Edge);
1789 Edge.setSUnit(SU);
1790 scheduleDAG->AddPred(SuccSU, Edge);
1791 --i;
1792 }
1793 }
1794 outer_loop_continue:;
1795 }
1796}
1797
Evan Chengd38c22b2006-05-11 23:55:42 +00001798/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1799/// it as a def&use operand. Add a pseudo control edge from it to the other
1800/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001801/// first (lower in the schedule). If both nodes are two-address, favor the
1802/// one that has a CopyToReg use (more likely to be a loop induction update).
1803/// If both are two-address, but one is commutable while the other is not
1804/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001805template<class SF>
1806void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001807 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001808 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001809 if (!SU->isTwoAddress)
1810 continue;
1811
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001812 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001813 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001814 continue;
1815
Dan Gohman17059682008-07-17 19:10:17 +00001816 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001817 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001818 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001819 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001820 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001821 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1822 continue;
1823 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1824 if (DU->getNodeId() == -1)
1825 continue;
1826 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1827 if (!DUSU) continue;
1828 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1829 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001830 if (I->isCtrl()) continue;
1831 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001832 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001833 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001834 // Be conservative. Ignore if nodes aren't at roughly the same
1835 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001836 if (SuccSU->getHeight() < SU->getHeight() &&
1837 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001838 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001839 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1840 // constrains whatever is using the copy, instead of the copy
1841 // itself. In the case that the copy is coalesced, this
1842 // preserves the intent of the pseudo two-address heurietics.
1843 while (SuccSU->Succs.size() == 1 &&
1844 SuccSU->getNode()->isMachineOpcode() &&
1845 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001846 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001847 SuccSU = SuccSU->Succs.front().getSUnit();
1848 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001849 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1850 continue;
1851 // Don't constrain nodes with physical register defs if the
1852 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001853 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001854 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001855 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001856 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001857 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1858 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001859 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001860 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1861 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1862 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001863 continue;
1864 if ((!canClobber(SuccSU, DUSU) ||
1865 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1866 (!SU->isCommutable && SuccSU->isCommutable)) &&
1867 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001868 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001869 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001870 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001871 /*Reg=*/0, /*isNormalMemory=*/false,
1872 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001873 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001874 }
1875 }
1876 }
1877 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001878}
1879
Evan Cheng6730f032007-01-08 23:55:53 +00001880/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1881/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001882template<class SF>
1883void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001884 SethiUllmanNumbers.assign(SUnits->size(), 0);
1885
1886 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001887 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001888}
Evan Chengd38c22b2006-05-11 23:55:42 +00001889
Roman Levenstein30d09512008-03-27 09:44:37 +00001890/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001891/// predecessors of the successors of the SUnit SU. Stop when the provided
1892/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001893static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1894 unsigned Limit) {
1895 unsigned Sum = 0;
1896 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1897 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001898 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001899 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1900 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001901 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001902 if (!PredSU->isScheduled)
1903 if (++Sum > Limit)
1904 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001905 }
1906 }
1907 return Sum;
1908}
1909
Evan Chengd38c22b2006-05-11 23:55:42 +00001910
1911// Top down
1912bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001913 unsigned LPriority = SPQ->getNodePriority(left);
1914 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001915 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1916 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001917 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1918 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001919 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1920 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001921
1922 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1923 return false;
1924 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1925 return true;
1926
Evan Chengd38c22b2006-05-11 23:55:42 +00001927 if (LIsFloater)
1928 LBonus -= 2;
1929 if (RIsFloater)
1930 RBonus -= 2;
1931 if (left->NumSuccs == 1)
1932 LBonus += 2;
1933 if (right->NumSuccs == 1)
1934 RBonus += 2;
1935
Evan Cheng73bdf042008-03-01 00:39:47 +00001936 if (LPriority+LBonus != RPriority+RBonus)
1937 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001938
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001939 if (left->getDepth() != right->getDepth())
1940 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001941
1942 if (left->NumSuccsLeft != right->NumSuccsLeft)
1943 return left->NumSuccsLeft > right->NumSuccsLeft;
1944
Roman Levenstein6b371142008-04-29 09:07:59 +00001945 assert(left->NodeQueueId && right->NodeQueueId &&
1946 "NodeQueueId cannot be zero");
1947 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001948}
1949
Evan Chengd38c22b2006-05-11 23:55:42 +00001950//===----------------------------------------------------------------------===//
1951// Public Constructor Functions
1952//===----------------------------------------------------------------------===//
1953
Dan Gohmandfaf6462009-02-11 04:27:20 +00001954llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001955llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001956 const TargetMachine &TM = IS->TM;
1957 const TargetInstrInfo *TII = TM.getInstrInfo();
1958 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001959
Evan Chenga77f3d32010-07-21 06:09:07 +00001960 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00001961 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001962 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001963 PQ->setScheduleDAG(SD);
1964 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001965}
1966
Dan Gohmandfaf6462009-02-11 04:27:20 +00001967llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001968llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001969 const TargetMachine &TM = IS->TM;
1970 const TargetInstrInfo *TII = TM.getInstrInfo();
1971 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001972
Evan Chenga77f3d32010-07-21 06:09:07 +00001973 TDRegReductionPriorityQueue *PQ =
1974 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001975 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001976 PQ->setScheduleDAG(SD);
1977 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001978}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001979
1980llvm::ScheduleDAGSDNodes *
1981llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1982 const TargetMachine &TM = IS->TM;
1983 const TargetInstrInfo *TII = TM.getInstrInfo();
1984 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1985
Evan Chenga77f3d32010-07-21 06:09:07 +00001986 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00001987 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00001988 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
1989 PQ->setScheduleDAG(SD);
1990 return SD;
1991}
1992
1993llvm::ScheduleDAGSDNodes *
1994llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1995 const TargetMachine &TM = IS->TM;
1996 const TargetInstrInfo *TII = TM.getInstrInfo();
1997 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00001998 const TargetLowering *TLI = &IS->getTargetLowering();
Evan Chengbdd062d2010-05-20 06:13:19 +00001999
Evan Chenga77f3d32010-07-21 06:09:07 +00002000 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002001 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002002 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002003 PQ->setScheduleDAG(SD);
2004 return SD;
2005}
Evan Cheng37b740c2010-07-24 00:39:05 +00002006
2007llvm::ScheduleDAGSDNodes *
2008llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2009 const TargetMachine &TM = IS->TM;
2010 const TargetInstrInfo *TII = TM.getInstrInfo();
2011 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2012 const TargetLowering *TLI = &IS->getTargetLowering();
2013
2014 ILPBURRPriorityQueue *PQ =
2015 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2016 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2017 PQ->setScheduleDAG(SD);
2018 return SD;
2019}