blob: 3650b43a69d4e650aedf0f2a13552541c86c8493 [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;
Andrew Tricka52f3252010-12-23 04:16:14 +000091 std::vector<SUnit*> LiveRegGens;
Evan Cheng5924bf72007-09-25 01:54:36 +000092
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);
Andrew Tricka52f3252010-12-23 04:16:14 +0000140 void ReleasePredecessors(SUnit *SU);
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()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000193 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000194
Dan Gohmanc07f6862008-09-23 18:50:48 +0000195 NumLiveRegs = 0;
Andrew Trick2085a962010-12-21 22:25:04 +0000196 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
Andrew Tricka52f3252010-12-23 04:16:14 +0000197 LiveRegGens.resize(TRI->getNumRegs(), NULL);
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);
Andrew Trick2085a962010-12-21 22:25:04 +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();
Andrew Trick2085a962010-12-21 22:25:04 +0000213
Evan Chengd38c22b2006-05-11 23:55:42 +0000214 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
Andrew Trick033efdf2010-12-23 03:15:51 +0000250/// Call ReleasePred for each predecessor, then update register live def/gen.
251/// Always update LiveRegDefs for a register dependence even if the current SU
252/// also defines the register. This effectively create one large live range
253/// across a sequence of two-address node. This is important because the
254/// entire chain must be scheduled together. Example:
255///
256/// flags = (3) add
257/// flags = (2) addc flags
258/// flags = (1) addc flags
259///
260/// results in
261///
262/// LiveRegDefs[flags] = 3
Andrew Tricka52f3252010-12-23 04:16:14 +0000263/// LiveRegGens[flags] = 1
Andrew Trick033efdf2010-12-23 03:15:51 +0000264///
265/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
266/// interference on flags.
Andrew Tricka52f3252010-12-23 04:16:14 +0000267void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000268 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000269 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000270 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000271 ReleasePred(SU, &*I);
272 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000273 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000274 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000275 // clobber the register is scheduled between the predecessor and
276 // this node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000277 SUnit *RegDef = LiveRegDefs[I->getReg()]; (void)RegDef;
Andrew Trick033efdf2010-12-23 03:15:51 +0000278 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
279 "interference on register dependence");
Andrew Tricka52f3252010-12-23 04:16:14 +0000280 LiveRegDefs[I->getReg()] = I->getSUnit();
281 if (!LiveRegGens[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000282 ++NumLiveRegs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000283 LiveRegGens[I->getReg()] = SU;
Evan Cheng5924bf72007-09-25 01:54:36 +0000284 }
285 }
286 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000287}
288
289/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
290/// count of its predecessors. If a predecessor pending count is zero, add it to
291/// the Available queue.
292void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000293 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000294 DEBUG(SU->dump(this));
295
Evan Chengbdd062d2010-05-20 06:13:19 +0000296#ifndef NDEBUG
297 if (CurCycle < SU->getHeight())
298 DEBUG(dbgs() << " Height [" << SU->getHeight() << "] pipeline stall!\n");
299#endif
300
301 // FIXME: Handle noop hazard.
Dan Gohmanb9543432009-02-10 23:27:53 +0000302 SU->setHeightToAtLeast(CurCycle);
303 Sequence.push_back(SU);
304
Evan Cheng28590382010-07-21 23:53:58 +0000305 AvailableQueue->ScheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000306
Andrew Trick033efdf2010-12-23 03:15:51 +0000307 // Update liveness of predecessors before successors to avoid treating a
308 // two-address node as a live range def.
Andrew Tricka52f3252010-12-23 04:16:14 +0000309 ReleasePredecessors(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000310
311 // Release all the implicit physical register defs that are live.
312 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
313 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000314 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
315 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
316 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
317 --NumLiveRegs;
318 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000319 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000320 }
321 }
322
Evan Chengd38c22b2006-05-11 23:55:42 +0000323 SU->isScheduled = true;
Evan Chengd38c22b2006-05-11 23:55:42 +0000324}
325
Evan Cheng5924bf72007-09-25 01:54:36 +0000326/// CapturePred - This does the opposite of ReleasePred. Since SU is being
327/// unscheduled, incrcease the succ left count of its predecessors. Remove
328/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000329void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000330 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000331 if (PredSU->isAvailable) {
332 PredSU->isAvailable = false;
333 if (!PredSU->isPending)
334 AvailableQueue->remove(PredSU);
335 }
336
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000337 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000338 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000339}
340
341/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
342/// its predecessor states to reflect the change.
343void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000344 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000345 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000346
Evan Cheng5924bf72007-09-25 01:54:36 +0000347 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
348 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000349 CapturePred(&*I);
Andrew Tricka52f3252010-12-23 04:16:14 +0000350 if (I->isAssignedRegDep() && SU == LiveRegGens[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000351 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000352 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000353 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000354 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000355 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000356 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000357 }
358 }
359
360 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
361 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000362 if (I->isAssignedRegDep()) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000363 // This becomes the nearest def. Note that an earlier def may still be
364 // pending if this is a two-address node.
365 LiveRegDefs[I->getReg()] = SU;
Dan Gohman2d170892008-12-09 22:54:47 +0000366 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000367 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000368 }
Andrew Tricka52f3252010-12-23 04:16:14 +0000369 if (LiveRegGens[I->getReg()] == NULL ||
370 I->getSUnit()->getHeight() < LiveRegGens[I->getReg()]->getHeight())
371 LiveRegGens[I->getReg()] = I->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000372 }
373 }
374
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000375 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000376 SU->isScheduled = false;
377 SU->isAvailable = true;
378 AvailableQueue->push(SU);
Evan Cheng28590382010-07-21 23:53:58 +0000379 AvailableQueue->UnscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000380}
381
Evan Cheng8e136a92007-09-26 21:36:17 +0000382/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000383/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000384void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
385 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000386 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000387 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000388 OldSU = Sequence.back();
389 Sequence.pop_back();
390 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000391 // Don't try to remove SU from AvailableQueue.
392 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000393 UnscheduleNodeBottomUp(OldSU);
394 --CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000395 AvailableQueue->setCurCycle(CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000396 }
397
Dan Gohman60d68442009-01-29 19:49:27 +0000398 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000399
400 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000401}
402
Evan Cheng3b245872010-02-05 01:27:11 +0000403static bool isOperandOf(const SUnit *SU, SDNode *N) {
404 for (const SDNode *SUNode = SU->getNode(); SUNode;
405 SUNode = SUNode->getFlaggedNode()) {
406 if (SUNode->isOperandOf(N))
407 return true;
408 }
409 return false;
410}
411
Evan Cheng5924bf72007-09-25 01:54:36 +0000412/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
413/// successors to the newly created node.
414SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000415 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000416 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000417
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000418 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000419 if (!N)
420 return NULL;
421
422 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000423 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000424 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000425 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000426 if (VT == MVT::Glue)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000427 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000428 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000429 TryUnfold = true;
430 }
Evan Cheng79e97132007-10-05 01:39:18 +0000431 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000432 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000433 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000434 if (VT == MVT::Glue)
Evan Cheng79e97132007-10-05 01:39:18 +0000435 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000436 }
437
438 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000439 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000440 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000441 return NULL;
442
Evan Chengbdd062d2010-05-20 06:13:19 +0000443 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000444 assert(NewNodes.size() == 2 && "Expected a load folding node!");
445
446 N = NewNodes[1];
447 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000448 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000449 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000450 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000451 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
452 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000453 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000454
Dan Gohmane52e0892008-11-11 21:34:44 +0000455 // LoadNode may already exist. This can happen when there is another
456 // load from the same location and producing the same type of value
457 // but it has different alignment or volatileness.
458 bool isNewLoad = true;
459 SUnit *LoadSU;
460 if (LoadNode->getNodeId() != -1) {
461 LoadSU = &SUnits[LoadNode->getNodeId()];
462 isNewLoad = false;
463 } else {
464 LoadSU = CreateNewSUnit(LoadNode);
465 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000466 ComputeLatency(LoadSU);
467 }
468
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000469 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000470 assert(N->getNodeId() == -1 && "Node already inserted!");
471 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +0000472
Dan Gohman17059682008-07-17 19:10:17 +0000473 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000474 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000475 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000476 NewSU->isTwoAddress = true;
477 break;
478 }
479 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000480 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000481 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000482 ComputeLatency(NewSU);
483
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000484 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000485 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000486 SmallVector<SDep, 4> ChainSuccs;
487 SmallVector<SDep, 4> LoadPreds;
488 SmallVector<SDep, 4> NodePreds;
489 SmallVector<SDep, 4> NodeSuccs;
490 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
491 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000492 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000493 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000494 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000495 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000496 else
Dan Gohman2d170892008-12-09 22:54:47 +0000497 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000498 }
499 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
500 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000501 if (I->isCtrl())
502 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000503 else
Dan Gohman2d170892008-12-09 22:54:47 +0000504 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000505 }
506
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000507 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000508 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
509 const SDep &Pred = ChainPreds[i];
510 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000511 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000512 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000513 }
Evan Cheng79e97132007-10-05 01:39:18 +0000514 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000515 const SDep &Pred = LoadPreds[i];
516 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000517 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000518 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000519 }
520 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000521 const SDep &Pred = NodePreds[i];
522 RemovePred(SU, Pred);
523 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000524 }
525 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000526 SDep D = NodeSuccs[i];
527 SUnit *SuccDep = D.getSUnit();
528 D.setSUnit(SU);
529 RemovePred(SuccDep, D);
530 D.setSUnit(NewSU);
531 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000532 }
533 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000534 SDep D = ChainSuccs[i];
535 SUnit *SuccDep = D.getSUnit();
536 D.setSUnit(SU);
537 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000538 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000539 D.setSUnit(LoadSU);
540 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000541 }
Andrew Trick2085a962010-12-21 22:25:04 +0000542 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000543
544 // Add a data dependency to reflect that NewSU reads the value defined
545 // by LoadSU.
546 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000547
Evan Cheng91e0fc92007-12-18 08:42:10 +0000548 if (isNewLoad)
549 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000550 AvailableQueue->addNode(NewSU);
551
552 ++NumUnfolds;
553
554 if (NewSU->NumSuccsLeft == 0) {
555 NewSU->isAvailable = true;
556 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000557 }
558 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000559 }
560
Evan Chengbdd062d2010-05-20 06:13:19 +0000561 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000562 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000563
564 // New SUnit has the exact same predecessors.
565 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
566 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000567 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000568 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000569
570 // Only copy scheduled successors. Cut them from old node's successor
571 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000572 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000573 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
574 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000575 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000576 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000577 SUnit *SuccSU = I->getSUnit();
578 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000579 SDep D = *I;
580 D.setSUnit(NewSU);
581 AddPred(SuccSU, D);
582 D.setSUnit(SU);
583 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000584 }
585 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000586 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000587 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000588
589 AvailableQueue->updateNode(SU);
590 AvailableQueue->addNode(NewSU);
591
Evan Cheng1ec79b42007-09-27 07:09:03 +0000592 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000593 return NewSU;
594}
595
Evan Chengb2c42c62009-01-12 03:19:55 +0000596/// InsertCopiesAndMoveSuccs - Insert register copies and move all
597/// scheduled successors of the given SUnit to the last copy.
598void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
599 const TargetRegisterClass *DestRC,
600 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000601 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000602 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000603 CopyFromSU->CopySrcRC = SrcRC;
604 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000605
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000606 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000607 CopyToSU->CopySrcRC = DestRC;
608 CopyToSU->CopyDstRC = SrcRC;
609
610 // Only copy scheduled successors. Cut them from old node's successor
611 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000612 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000613 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
614 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000615 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000616 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000617 SUnit *SuccSU = I->getSUnit();
618 if (SuccSU->isScheduled) {
619 SDep D = *I;
620 D.setSUnit(CopyToSU);
621 AddPred(SuccSU, D);
622 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000623 }
624 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000625 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000626 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000627
Dan Gohman2d170892008-12-09 22:54:47 +0000628 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
629 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000630
631 AvailableQueue->updateNode(SU);
632 AvailableQueue->addNode(CopyFromSU);
633 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000634 Copies.push_back(CopyFromSU);
635 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000636
Evan Chengb2c42c62009-01-12 03:19:55 +0000637 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000638}
639
640/// getPhysicalRegisterVT - Returns the ValueType of the physical register
641/// definition of the specified node.
642/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000643static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000644 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000645 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000646 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000647 unsigned NumRes = TID.getNumDefs();
648 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000649 if (Reg == *ImpDef)
650 break;
651 ++NumRes;
652 }
653 return N->getValueType(NumRes);
654}
655
Evan Chengb8905c42009-03-04 01:41:49 +0000656/// CheckForLiveRegDef - Return true and update live register vector if the
657/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000658static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +0000659 std::vector<SUnit*> &LiveRegDefs,
660 SmallSet<unsigned, 4> &RegAdded,
661 SmallVector<unsigned, 4> &LRegs,
662 const TargetRegisterInfo *TRI) {
Andrew Trick12acde112010-12-23 03:43:21 +0000663 for (const unsigned *AliasI = TRI->getOverlaps(Reg); *AliasI; ++AliasI) {
664
665 // Check if Ref is live.
666 if (!LiveRegDefs[Reg]) continue;
667
668 // Allow multiple uses of the same def.
669 if (LiveRegDefs[Reg] == SU) continue;
670
671 // Add Reg to the set of interfering live regs.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000672 if (RegAdded.insert(Reg))
Evan Chengb8905c42009-03-04 01:41:49 +0000673 LRegs.push_back(Reg);
Evan Chengb8905c42009-03-04 01:41:49 +0000674 }
Evan Chengb8905c42009-03-04 01:41:49 +0000675}
676
Evan Cheng5924bf72007-09-25 01:54:36 +0000677/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
678/// scheduling of the given node to satisfy live physical register dependencies.
679/// If the specific node is the last one that's available to schedule, do
680/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +0000681bool ScheduleDAGRRList::
682DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000683 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000684 return false;
685
Evan Chenge6f92252007-09-27 18:46:06 +0000686 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000687 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000688 //
689 // If SU is the currently live definition of the same register that it uses,
690 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +0000691 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
692 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +0000693 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +0000694 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
695 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000696 }
697
Dan Gohman072734e2008-11-13 23:24:17 +0000698 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000699 if (Node->getOpcode() == ISD::INLINEASM) {
700 // Inline asm can clobber physical defs.
701 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000702 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Evan Chengb8905c42009-03-04 01:41:49 +0000703 --NumOps; // Ignore the flag operand.
704
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000705 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000706 unsigned Flags =
707 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000708 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000709
710 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000711 if (InlineAsm::isRegDefKind(Flags) ||
712 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000713 // Check for def of register or earlyclobber register.
714 for (; NumVals; --NumVals, ++i) {
715 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
716 if (TargetRegisterInfo::isPhysicalRegister(Reg))
717 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
718 }
719 } else
720 i += NumVals;
721 }
722 continue;
723 }
724
Dan Gohman072734e2008-11-13 23:24:17 +0000725 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000726 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000727 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000728 if (!TID.ImplicitDefs)
729 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000730 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
731 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000732 }
Andrew Trick2085a962010-12-21 22:25:04 +0000733
Evan Cheng5924bf72007-09-25 01:54:36 +0000734 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000735}
736
Evan Cheng1ec79b42007-09-27 07:09:03 +0000737
Evan Chengd38c22b2006-05-11 23:55:42 +0000738/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
739/// schedulers.
740void ScheduleDAGRRList::ListScheduleBottomUp() {
741 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000742
743 // Release any predecessors of the special Exit node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000744 ReleasePredecessors(&ExitSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000745
Evan Chengd38c22b2006-05-11 23:55:42 +0000746 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000747 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000748 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000749 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
750 RootSU->isAvailable = true;
751 AvailableQueue->push(RootSU);
752 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000753
754 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000755 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000756 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000757 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000758 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000759 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000760 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000761 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000762 SUnit *CurSU = AvailableQueue->pop();
763 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000764 SmallVector<unsigned, 4> LRegs;
765 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
766 break;
767 Delayed = true;
768 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000769
770 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
771 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000772 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000773 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000774
775 // All candidates are delayed due to live physical reg dependencies.
776 // Try backtracking, code duplication, or inserting cross class copies
777 // to resolve it.
778 if (Delayed && !CurSU) {
779 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
780 SUnit *TrySU = NotReady[i];
781 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
782
783 // Try unscheduling up to the point where it's safe to schedule
784 // this node.
785 unsigned LiveCycle = CurCycle;
786 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
787 unsigned Reg = LRegs[j];
Andrew Tricka52f3252010-12-23 04:16:14 +0000788 unsigned LCycle = LiveRegGens[Reg]->getHeight();
Evan Cheng1ec79b42007-09-27 07:09:03 +0000789 LiveCycle = std::min(LiveCycle, LCycle);
790 }
791 SUnit *OldSU = Sequence[LiveCycle];
792 if (!WillCreateCycle(TrySU, OldSU)) {
793 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
794 // Force the current node to be scheduled before the node that
795 // requires the physical reg dep.
796 if (OldSU->isAvailable) {
797 OldSU->isAvailable = false;
798 AvailableQueue->remove(OldSU);
799 }
Dan Gohman2d170892008-12-09 22:54:47 +0000800 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
801 /*Reg=*/0, /*isNormalMemory=*/false,
802 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000803 // If one or more successors has been unscheduled, then the current
804 // node is no longer avaialable. Schedule a successor that's now
805 // available instead.
806 if (!TrySU->isAvailable)
807 CurSU = AvailableQueue->pop();
808 else {
809 CurSU = TrySU;
810 TrySU->isPending = false;
811 NotReady.erase(NotReady.begin()+i);
812 }
813 break;
814 }
815 }
816
817 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000818 // Can't backtrack. If it's too expensive to copy the value, then try
819 // duplicate the nodes that produces these "too expensive to copy"
820 // values to break the dependency. In case even that doesn't work,
821 // insert cross class copies.
822 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000823 SUnit *TrySU = NotReady[0];
824 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
825 assert(LRegs.size() == 1 && "Can't handle this yet!");
826 unsigned Reg = LRegs[0];
827 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000828 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000829 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000830 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000831 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
832
833 // If cross copy register class is null, then it must be possible copy
834 // the value directly. Do not try duplicate the def.
835 SUnit *NewDef = 0;
836 if (DestRC)
837 NewDef = CopyAndMoveSuccessors(LRDef);
838 else
839 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000840 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000841 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000842 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000843 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Evan Chengbdd062d2010-05-20 06:13:19 +0000844 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000845 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000846 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000847 /*Reg=*/0, /*isNormalMemory=*/false,
848 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000849 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000850 NewDef = Copies.back();
851 }
852
Evan Chengbdd062d2010-05-20 06:13:19 +0000853 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000854 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000855 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000856 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000857 /*Reg=*/0, /*isNormalMemory=*/false,
858 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000859 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000860 TrySU->isAvailable = false;
861 CurSU = NewDef;
862 }
863
Dan Gohman60d68442009-01-29 19:49:27 +0000864 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000865 }
866
Evan Chengd38c22b2006-05-11 23:55:42 +0000867 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000868 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
869 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000870 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000871 if (NotReady[i]->isAvailable)
872 AvailableQueue->push(NotReady[i]);
873 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000874 NotReady.clear();
875
Dan Gohmanc602dd42008-11-21 00:10:42 +0000876 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000877 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000878 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000879 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000880 }
881
Evan Chengd38c22b2006-05-11 23:55:42 +0000882 // Reverse the order if it is bottom up.
883 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +0000884
Evan Chengd38c22b2006-05-11 23:55:42 +0000885#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000886 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000887#endif
888}
889
890//===----------------------------------------------------------------------===//
891// Top-Down Scheduling
892//===----------------------------------------------------------------------===//
893
894/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000895/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000896void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000897 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000898
Evan Chengd38c22b2006-05-11 23:55:42 +0000899#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000900 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000901 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000902 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000903 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000904 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000905 }
906#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000907 --SuccSU->NumPredsLeft;
908
Dan Gohmanb9543432009-02-10 23:27:53 +0000909 // If all the node's predecessors are scheduled, this node is ready
910 // to be scheduled. Ignore the special ExitSU node.
911 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000912 SuccSU->isAvailable = true;
913 AvailableQueue->push(SuccSU);
914 }
915}
916
Dan Gohmanb9543432009-02-10 23:27:53 +0000917void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
918 // Top down: release successors
919 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
920 I != E; ++I) {
921 assert(!I->isAssignedRegDep() &&
922 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
923
924 ReleaseSucc(SU, &*I);
925 }
926}
927
Evan Chengd38c22b2006-05-11 23:55:42 +0000928/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
929/// count of its successors. If a successor pending count is zero, add it to
930/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000931void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000932 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000933 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000934
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000935 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
936 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000937 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000938
Dan Gohmanb9543432009-02-10 23:27:53 +0000939 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000940 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000941 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000942}
943
Dan Gohman54a187e2007-08-20 19:28:38 +0000944/// ListScheduleTopDown - The main loop of list scheduling for top-down
945/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000946void ScheduleDAGRRList::ListScheduleTopDown() {
947 unsigned CurCycle = 0;
Evan Chengbdd062d2010-05-20 06:13:19 +0000948 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000949
Dan Gohmanb9543432009-02-10 23:27:53 +0000950 // Release any successors of the special Entry node.
951 ReleaseSuccessors(&EntrySU);
952
Evan Chengd38c22b2006-05-11 23:55:42 +0000953 // All leaves to Available queue.
954 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
955 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000956 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000957 AvailableQueue->push(&SUnits[i]);
958 SUnits[i].isAvailable = true;
959 }
960 }
Andrew Trick2085a962010-12-21 22:25:04 +0000961
Evan Chengd38c22b2006-05-11 23:55:42 +0000962 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000963 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000964 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000965 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000966 SUnit *CurSU = AvailableQueue->pop();
Andrew Trick2085a962010-12-21 22:25:04 +0000967
Dan Gohmanc602dd42008-11-21 00:10:42 +0000968 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000969 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000970 ++CurCycle;
Evan Chengbdd062d2010-05-20 06:13:19 +0000971 AvailableQueue->setCurCycle(CurCycle);
Evan Chengd38c22b2006-05-11 23:55:42 +0000972 }
Andrew Trick2085a962010-12-21 22:25:04 +0000973
Evan Chengd38c22b2006-05-11 23:55:42 +0000974#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000975 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000976#endif
977}
978
979
Evan Chengd38c22b2006-05-11 23:55:42 +0000980//===----------------------------------------------------------------------===//
981// RegReductionPriorityQueue Implementation
982//===----------------------------------------------------------------------===//
983//
984// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
985// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +0000986//
Evan Chengd38c22b2006-05-11 23:55:42 +0000987namespace {
988 template<class SF>
989 class RegReductionPriorityQueue;
Andrew Trick2085a962010-12-21 22:25:04 +0000990
Evan Cheng8ae3eca2010-07-25 18:59:43 +0000991 /// bu_ls_rr_sort - Priority function for bottom up register pressure
992 // reduction scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +0000993 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
994 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
995 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
996 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +0000997
Evan Chengd38c22b2006-05-11 23:55:42 +0000998 bool operator()(const SUnit* left, const SUnit* right) const;
999 };
1000
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001001 // td_ls_rr_sort - Priority function for top down register pressure reduction
1002 // scheduler.
Evan Chengd38c22b2006-05-11 23:55:42 +00001003 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1004 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
1005 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
1006 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001007
Evan Chengd38c22b2006-05-11 23:55:42 +00001008 bool operator()(const SUnit* left, const SUnit* right) const;
1009 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001010
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001011 // src_ls_rr_sort - Priority function for source order scheduler.
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001012 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1013 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
1014 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
1015 : SPQ(spq) {}
1016 src_ls_rr_sort(const src_ls_rr_sort &RHS)
1017 : SPQ(RHS.SPQ) {}
Andrew Trick2085a962010-12-21 22:25:04 +00001018
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001019 bool operator()(const SUnit* left, const SUnit* right) const;
1020 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001021
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001022 // hybrid_ls_rr_sort - Priority function for hybrid scheduler.
Evan Chengbdd062d2010-05-20 06:13:19 +00001023 struct hybrid_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1024 RegReductionPriorityQueue<hybrid_ls_rr_sort> *SPQ;
1025 hybrid_ls_rr_sort(RegReductionPriorityQueue<hybrid_ls_rr_sort> *spq)
1026 : SPQ(spq) {}
1027 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1028 : SPQ(RHS.SPQ) {}
Evan Chenga77f3d32010-07-21 06:09:07 +00001029
Evan Chengbdd062d2010-05-20 06:13:19 +00001030 bool operator()(const SUnit* left, const SUnit* right) const;
1031 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001032
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001033 // ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1034 // scheduler.
Evan Cheng37b740c2010-07-24 00:39:05 +00001035 struct ilp_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1036 RegReductionPriorityQueue<ilp_ls_rr_sort> *SPQ;
1037 ilp_ls_rr_sort(RegReductionPriorityQueue<ilp_ls_rr_sort> *spq)
1038 : SPQ(spq) {}
1039 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1040 : SPQ(RHS.SPQ) {}
1041
1042 bool operator()(const SUnit* left, const SUnit* right) const;
1043 };
Evan Chengd38c22b2006-05-11 23:55:42 +00001044} // end anonymous namespace
1045
Dan Gohman186f65d2008-11-20 03:30:37 +00001046/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1047/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001048static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001049CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001050 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1051 if (SethiUllmanNumber != 0)
1052 return SethiUllmanNumber;
1053
1054 unsigned Extra = 0;
1055 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1056 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001057 if (I->isCtrl()) continue; // ignore chain preds
1058 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001059 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001060 if (PredSethiUllman > SethiUllmanNumber) {
1061 SethiUllmanNumber = PredSethiUllman;
1062 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001063 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001064 ++Extra;
1065 }
1066
1067 SethiUllmanNumber += Extra;
1068
1069 if (SethiUllmanNumber == 0)
1070 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001071
Evan Cheng7e4abde2008-07-02 09:23:51 +00001072 return SethiUllmanNumber;
1073}
1074
Evan Chengd38c22b2006-05-11 23:55:42 +00001075namespace {
1076 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +00001077 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohman52c27382010-05-26 18:52:00 +00001078 std::vector<SUnit*> Queue;
1079 SF Picker;
Evan Chengbdd062d2010-05-20 06:13:19 +00001080 unsigned CurQueueId;
Evan Chengbf32e542010-07-22 06:24:48 +00001081 bool TracksRegPressure;
Evan Chengd38c22b2006-05-11 23:55:42 +00001082
Dan Gohman3f656df2008-11-20 02:45:51 +00001083 protected:
1084 // SUnits - The SUnits for the current graph.
1085 std::vector<SUnit> *SUnits;
Evan Chenga77f3d32010-07-21 06:09:07 +00001086
1087 MachineFunction &MF;
Dan Gohman3f656df2008-11-20 02:45:51 +00001088 const TargetInstrInfo *TII;
1089 const TargetRegisterInfo *TRI;
Evan Chenga77f3d32010-07-21 06:09:07 +00001090 const TargetLowering *TLI;
Dan Gohman3f656df2008-11-20 02:45:51 +00001091 ScheduleDAGRRList *scheduleDAG;
1092
Dan Gohman186f65d2008-11-20 03:30:37 +00001093 // SethiUllmanNumbers - The SethiUllman number for each node.
1094 std::vector<unsigned> SethiUllmanNumbers;
1095
Evan Chenga77f3d32010-07-21 06:09:07 +00001096 /// RegPressure - Tracking current reg pressure per register class.
1097 ///
Evan Cheng28590382010-07-21 23:53:58 +00001098 std::vector<unsigned> RegPressure;
Evan Chenga77f3d32010-07-21 06:09:07 +00001099
1100 /// RegLimit - Tracking the number of allocatable registers per register
1101 /// class.
Evan Cheng28590382010-07-21 23:53:58 +00001102 std::vector<unsigned> RegLimit;
Evan Chenga77f3d32010-07-21 06:09:07 +00001103
Dan Gohman3f656df2008-11-20 02:45:51 +00001104 public:
Evan Chenga77f3d32010-07-21 06:09:07 +00001105 RegReductionPriorityQueue(MachineFunction &mf,
Evan Chengbf32e542010-07-22 06:24:48 +00001106 bool tracksrp,
Evan Chenga77f3d32010-07-21 06:09:07 +00001107 const TargetInstrInfo *tii,
1108 const TargetRegisterInfo *tri,
1109 const TargetLowering *tli)
Evan Chengbf32e542010-07-22 06:24:48 +00001110 : Picker(this), CurQueueId(0), TracksRegPressure(tracksrp),
Evan Chenga77f3d32010-07-21 06:09:07 +00001111 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
Evan Chengbf32e542010-07-22 06:24:48 +00001112 if (TracksRegPressure) {
1113 unsigned NumRC = TRI->getNumRegClasses();
1114 RegLimit.resize(NumRC);
1115 RegPressure.resize(NumRC);
1116 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1117 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1118 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1119 E = TRI->regclass_end(); I != E; ++I)
Evan Chengdf907f42010-07-23 22:39:59 +00001120 RegLimit[(*I)->getID()] = tli->getRegPressureLimit(*I, MF);
Evan Chengbf32e542010-07-22 06:24:48 +00001121 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001122 }
Andrew Trick2085a962010-12-21 22:25:04 +00001123
Dan Gohman3f656df2008-11-20 02:45:51 +00001124 void initNodes(std::vector<SUnit> &sunits) {
1125 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001126 // Add pseudo dependency edges for two-address nodes.
1127 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001128 // Reroute edges to nodes with multiple uses.
1129 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001130 // Calculate node priorities.
1131 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001132 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001133
Dan Gohman186f65d2008-11-20 03:30:37 +00001134 void addNode(const SUnit *SU) {
1135 unsigned SUSize = SethiUllmanNumbers.size();
1136 if (SUnits->size() > SUSize)
1137 SethiUllmanNumbers.resize(SUSize*2, 0);
1138 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1139 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001140
Dan Gohman186f65d2008-11-20 03:30:37 +00001141 void updateNode(const SUnit *SU) {
1142 SethiUllmanNumbers[SU->NodeNum] = 0;
1143 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1144 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001145
Dan Gohman186f65d2008-11-20 03:30:37 +00001146 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001147 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001148 SethiUllmanNumbers.clear();
Evan Chenga77f3d32010-07-21 06:09:07 +00001149 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Dan Gohman3f656df2008-11-20 02:45:51 +00001150 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001151
1152 unsigned getNodePriority(const SUnit *SU) const {
1153 assert(SU->NodeNum < SethiUllmanNumbers.size());
1154 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001155 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001156 // CopyToReg should be close to its uses to facilitate coalescing and
1157 // avoid spilling.
1158 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001159 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1160 Opc == TargetOpcode::SUBREG_TO_REG ||
1161 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001162 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1163 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001164 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001165 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1166 // If SU does not have a register use, i.e. it doesn't produce a value
1167 // that would be consumed (e.g. store), then it terminates a chain of
1168 // computation. Give it a large SethiUllman number so it will be
1169 // scheduled right before its predecessors that it doesn't lengthen
1170 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001171 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001172 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1173 // If SU does not have a register def, schedule it close to its uses
1174 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001175 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001176 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001177 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001178
1179 unsigned getNodeOrdering(const SUnit *SU) const {
1180 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1181 }
Evan Chengbdd062d2010-05-20 06:13:19 +00001182
Evan Chengd38c22b2006-05-11 23:55:42 +00001183 bool empty() const { return Queue.empty(); }
Andrew Trick2085a962010-12-21 22:25:04 +00001184
Evan Chengd38c22b2006-05-11 23:55:42 +00001185 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001186 assert(!U->NodeQueueId && "Node in the queue already");
Evan Chengbdd062d2010-05-20 06:13:19 +00001187 U->NodeQueueId = ++CurQueueId;
Dan Gohman52c27382010-05-26 18:52:00 +00001188 Queue.push_back(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001189 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001190
Evan Chengd38c22b2006-05-11 23:55:42 +00001191 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001192 if (empty()) return NULL;
Dan Gohman52c27382010-05-26 18:52:00 +00001193 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentesa97311f2010-05-30 13:14:21 +00001194 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +00001195 E = Queue.end(); I != E; ++I)
1196 if (Picker(*Best, *I))
1197 Best = I;
1198 SUnit *V = *Best;
1199 if (Best != prior(Queue.end()))
1200 std::swap(*Best, Queue.back());
1201 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001202 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001203 return V;
1204 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001205
Evan Cheng5924bf72007-09-25 01:54:36 +00001206 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001207 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001208 assert(SU->NodeQueueId != 0 && "Not in queue!");
Dan Gohman52c27382010-05-26 18:52:00 +00001209 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1210 SU);
1211 if (I != prior(Queue.end()))
1212 std::swap(*I, Queue.back());
1213 Queue.pop_back();
Roman Levenstein6b371142008-04-29 09:07:59 +00001214 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001215 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001216
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001217 bool HighRegPressure(const SUnit *SU) const {
Evan Chenga77f3d32010-07-21 06:09:07 +00001218 if (!TLI)
Evan Cheng28590382010-07-21 23:53:58 +00001219 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001220
Evan Chenga77f3d32010-07-21 06:09:07 +00001221 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1222 I != E; ++I) {
1223 if (I->isCtrl())
1224 continue;
1225 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001226 const SDNode *PN = PredSU->getNode();
1227 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001228 if (PN->getOpcode() == ISD::CopyFromReg) {
1229 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001230 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1231 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001232 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1233 return true;
Evan Chengdf907f42010-07-23 22:39:59 +00001234 }
1235 continue;
1236 }
1237 unsigned POpc = PN->getMachineOpcode();
1238 if (POpc == TargetOpcode::IMPLICIT_DEF)
1239 continue;
1240 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1241 EVT VT = PN->getOperand(0).getValueType();
1242 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1243 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1244 // Check if this increases register pressure of the specific register
1245 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001246 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1247 return true;
Andrew Trick2085a962010-12-21 22:25:04 +00001248 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001249 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1250 POpc == TargetOpcode::SUBREG_TO_REG) {
1251 EVT VT = PN->getValueType(0);
1252 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1253 unsigned Cost = TLI->getRepRegClassCostFor(VT);
1254 // Check if this increases register pressure of the specific register
1255 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001256 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1257 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001258 continue;
Evan Cheng28590382010-07-21 23:53:58 +00001259 }
1260 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
Evan Chenga77f3d32010-07-21 06:09:07 +00001261 for (unsigned i = 0; i != NumDefs; ++i) {
Evan Cheng28590382010-07-21 23:53:58 +00001262 EVT VT = PN->getValueType(i);
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001263 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1264 if (RegPressure[RCId] >= RegLimit[RCId])
1265 return true; // Reg pressure already high.
1266 unsigned Cost = TLI->getRepRegClassCostFor(VT);
Evan Cheng28590382010-07-21 23:53:58 +00001267 if (!PN->hasAnyUseOfValue(i))
Evan Chenga77f3d32010-07-21 06:09:07 +00001268 continue;
Evan Chenga77f3d32010-07-21 06:09:07 +00001269 // Check if this increases register pressure of the specific register
1270 // class to the point where it would cause spills.
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001271 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1272 return true;
Evan Chenga77f3d32010-07-21 06:09:07 +00001273 }
1274 }
1275
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001276 return false;
Evan Chenga77f3d32010-07-21 06:09:07 +00001277 }
1278
Evan Chengbf32e542010-07-22 06:24:48 +00001279 void ScheduledNode(SUnit *SU) {
1280 if (!TracksRegPressure)
1281 return;
1282
Evan Chenga77f3d32010-07-21 06:09:07 +00001283 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001284 if (!N->isMachineOpcode()) {
1285 if (N->getOpcode() != ISD::CopyToReg)
1286 return;
1287 } else {
1288 unsigned Opc = N->getMachineOpcode();
1289 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1290 Opc == TargetOpcode::INSERT_SUBREG ||
1291 Opc == TargetOpcode::SUBREG_TO_REG ||
1292 Opc == TargetOpcode::REG_SEQUENCE ||
1293 Opc == TargetOpcode::IMPLICIT_DEF)
1294 return;
1295 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001296
1297 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1298 I != E; ++I) {
1299 if (I->isCtrl())
1300 continue;
1301 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001302 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001303 continue;
1304 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001305 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001306 if (PN->getOpcode() == ISD::CopyFromReg) {
1307 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001308 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1309 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1310 }
1311 continue;
1312 }
1313 unsigned POpc = PN->getMachineOpcode();
1314 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001315 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001316 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1317 EVT VT = PN->getOperand(0).getValueType();
1318 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1319 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001320 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001321 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1322 POpc == TargetOpcode::SUBREG_TO_REG) {
1323 EVT VT = PN->getValueType(0);
1324 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1325 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1326 continue;
1327 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001328 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1329 for (unsigned i = 0; i != NumDefs; ++i) {
1330 EVT VT = PN->getValueType(i);
1331 if (!PN->hasAnyUseOfValue(i))
1332 continue;
1333 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1334 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1335 }
1336 }
1337
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001338 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1339 // may transfer data dependencies to CopyToReg.
1340 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001341 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1342 for (unsigned i = 0; i != NumDefs; ++i) {
1343 EVT VT = N->getValueType(i);
1344 if (!N->hasAnyUseOfValue(i))
1345 continue;
1346 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1347 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
1348 // Register pressure tracking is imprecise. This can happen.
1349 RegPressure[RCId] = 0;
1350 else
1351 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
1352 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001353 }
Evan Chengbf32e542010-07-22 06:24:48 +00001354
1355 dumpRegPressure();
Evan Chenga77f3d32010-07-21 06:09:07 +00001356 }
1357
Evan Chengbf32e542010-07-22 06:24:48 +00001358 void UnscheduledNode(SUnit *SU) {
1359 if (!TracksRegPressure)
1360 return;
1361
Evan Chenga77f3d32010-07-21 06:09:07 +00001362 const SDNode *N = SU->getNode();
Evan Chengdf907f42010-07-23 22:39:59 +00001363 if (!N->isMachineOpcode()) {
1364 if (N->getOpcode() != ISD::CopyToReg)
1365 return;
Evan Cheng37b740c2010-07-24 00:39:05 +00001366 } else {
1367 unsigned Opc = N->getMachineOpcode();
1368 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1369 Opc == TargetOpcode::INSERT_SUBREG ||
1370 Opc == TargetOpcode::SUBREG_TO_REG ||
1371 Opc == TargetOpcode::REG_SEQUENCE ||
1372 Opc == TargetOpcode::IMPLICIT_DEF)
1373 return;
Evan Chengdf907f42010-07-23 22:39:59 +00001374 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001375
1376 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1377 I != E; ++I) {
1378 if (I->isCtrl())
1379 continue;
1380 SUnit *PredSU = I->getSUnit();
Evan Cheng28590382010-07-21 23:53:58 +00001381 if (PredSU->NumSuccsLeft != PredSU->NumSuccs)
Evan Chenga77f3d32010-07-21 06:09:07 +00001382 continue;
1383 const SDNode *PN = PredSU->getNode();
Evan Cheng28590382010-07-21 23:53:58 +00001384 if (!PN->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001385 if (PN->getOpcode() == ISD::CopyFromReg) {
1386 EVT VT = PN->getValueType(0);
Evan Cheng28590382010-07-21 23:53:58 +00001387 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1388 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1389 }
1390 continue;
1391 }
1392 unsigned POpc = PN->getMachineOpcode();
1393 if (POpc == TargetOpcode::IMPLICIT_DEF)
Evan Chenga77f3d32010-07-21 06:09:07 +00001394 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001395 if (POpc == TargetOpcode::EXTRACT_SUBREG) {
1396 EVT VT = PN->getOperand(0).getValueType();
1397 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1398 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
Andrew Trick2085a962010-12-21 22:25:04 +00001399 continue;
Evan Chengdf907f42010-07-23 22:39:59 +00001400 } else if (POpc == TargetOpcode::INSERT_SUBREG ||
1401 POpc == TargetOpcode::SUBREG_TO_REG) {
1402 EVT VT = PN->getValueType(0);
1403 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1404 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1405 continue;
1406 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001407 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
1408 for (unsigned i = 0; i != NumDefs; ++i) {
1409 EVT VT = PN->getValueType(i);
1410 if (!PN->hasAnyUseOfValue(i))
1411 continue;
1412 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng28590382010-07-21 23:53:58 +00001413 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
Evan Chenga77f3d32010-07-21 06:09:07 +00001414 // Register pressure tracking is imprecise. This can happen.
1415 RegPressure[RCId] = 0;
Evan Cheng28590382010-07-21 23:53:58 +00001416 else
1417 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
Evan Chenga77f3d32010-07-21 06:09:07 +00001418 }
1419 }
1420
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001421 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
1422 // may transfer data dependencies to CopyToReg.
1423 if (SU->NumSuccs && N->isMachineOpcode()) {
Evan Chengdf907f42010-07-23 22:39:59 +00001424 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1425 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
1426 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001427 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengdf907f42010-07-23 22:39:59 +00001428 continue;
1429 if (!N->hasAnyUseOfValue(i))
1430 continue;
1431 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1432 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
1433 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001434 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001435
Evan Chenga77f3d32010-07-21 06:09:07 +00001436 dumpRegPressure();
1437 }
1438
Andrew Trick2085a962010-12-21 22:25:04 +00001439 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1440 scheduleDAG = scheduleDag;
Dan Gohman3f656df2008-11-20 02:45:51 +00001441 }
1442
Evan Chenga77f3d32010-07-21 06:09:07 +00001443 void dumpRegPressure() const {
1444 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1445 E = TRI->regclass_end(); I != E; ++I) {
1446 const TargetRegisterClass *RC = *I;
1447 unsigned Id = RC->getID();
1448 unsigned RP = RegPressure[Id];
1449 if (!RP) continue;
1450 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1451 << '\n');
1452 }
1453 }
1454
Dan Gohman3f656df2008-11-20 02:45:51 +00001455 protected:
1456 bool canClobber(const SUnit *SU, const SUnit *Op);
1457 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001458 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001459 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001460 };
1461
Dan Gohman186f65d2008-11-20 03:30:37 +00001462 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1463 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001464
Dan Gohman186f65d2008-11-20 03:30:37 +00001465 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1466 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001467
1468 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1469 SrcRegReductionPriorityQueue;
Evan Chengbdd062d2010-05-20 06:13:19 +00001470
1471 typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1472 HybridBURRPriorityQueue;
Evan Cheng37b740c2010-07-24 00:39:05 +00001473
1474 typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1475 ILPBURRPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001476}
1477
Evan Chengb9e3db62007-03-14 22:43:40 +00001478/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001479/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001480static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001481 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001482 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001483 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001484 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001485 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001486 // If there are bunch of CopyToRegs stacked up, they should be considered
1487 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001488 if (I->getSUnit()->getNode() &&
1489 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001490 Height = closestSucc(I->getSUnit())+1;
1491 if (Height > MaxHeight)
1492 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001493 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001494 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001495}
1496
Evan Cheng61bc51e2007-12-20 02:22:36 +00001497/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001498/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001499static unsigned calcMaxScratches(const SUnit *SU) {
1500 unsigned Scratches = 0;
1501 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001502 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001503 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001504 Scratches++;
1505 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001506 return Scratches;
1507}
1508
Evan Cheng6c1414f2010-10-29 18:09:28 +00001509/// hasOnlyLiveOutUse - Return true if SU has a single value successor that is a
1510/// CopyToReg to a virtual register. This SU def is probably a liveout and
1511/// it has no other use. It should be scheduled closer to the terminator.
1512static bool hasOnlyLiveOutUses(const SUnit *SU) {
1513 bool RetVal = false;
1514 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1515 I != E; ++I) {
1516 if (I->isCtrl()) continue;
1517 const SUnit *SuccSU = I->getSUnit();
1518 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
1519 unsigned Reg =
1520 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
1521 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1522 RetVal = true;
1523 continue;
1524 }
1525 }
1526 return false;
1527 }
1528 return RetVal;
1529}
1530
1531/// UnitsSharePred - Return true if the two scheduling units share a common
1532/// data predecessor.
1533static bool UnitsSharePred(const SUnit *left, const SUnit *right) {
1534 SmallSet<const SUnit*, 4> Preds;
1535 for (SUnit::const_pred_iterator I = left->Preds.begin(),E = left->Preds.end();
1536 I != E; ++I) {
1537 if (I->isCtrl()) continue; // ignore chain preds
1538 Preds.insert(I->getSUnit());
1539 }
1540 for (SUnit::const_pred_iterator I = right->Preds.begin(),E = right->Preds.end();
1541 I != E; ++I) {
1542 if (I->isCtrl()) continue; // ignore chain preds
1543 if (Preds.count(I->getSUnit()))
1544 return true;
1545 }
1546 return false;
1547}
1548
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001549template <typename RRSort>
1550static bool BURRSort(const SUnit *left, const SUnit *right,
1551 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001552 unsigned LPriority = SPQ->getNodePriority(left);
1553 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001554 if (LPriority != RPriority)
1555 return LPriority > RPriority;
1556
1557 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1558 // e.g.
1559 // t1 = op t2, c1
1560 // t3 = op t4, c2
1561 //
1562 // and the following instructions are both ready.
1563 // t2 = op c3
1564 // t4 = op c4
1565 //
1566 // Then schedule t2 = op first.
1567 // i.e.
1568 // t4 = op c4
1569 // t2 = op c3
1570 // t1 = op t2, c1
1571 // t3 = op t4, c2
1572 //
1573 // This creates more short live intervals.
1574 unsigned LDist = closestSucc(left);
1575 unsigned RDist = closestSucc(right);
1576 if (LDist != RDist)
1577 return LDist < RDist;
1578
Evan Cheng3a14efa2009-02-12 08:59:45 +00001579 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001580 unsigned LScratch = calcMaxScratches(left);
1581 unsigned RScratch = calcMaxScratches(right);
1582 if (LScratch != RScratch)
1583 return LScratch > RScratch;
1584
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001585 if (left->getHeight() != right->getHeight())
1586 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00001587
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001588 if (left->getDepth() != right->getDepth())
1589 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001590
Andrew Trick2085a962010-12-21 22:25:04 +00001591 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00001592 "NodeQueueId cannot be zero");
1593 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001594}
1595
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001596// Bottom up
1597bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1598 return BURRSort(left, right, SPQ);
1599}
1600
1601// Source order, otherwise bottom up.
Evan Chengbdd062d2010-05-20 06:13:19 +00001602bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001603 unsigned LOrder = SPQ->getNodeOrdering(left);
1604 unsigned ROrder = SPQ->getNodeOrdering(right);
1605
1606 // Prefer an ordering where the lower the non-zero order number, the higher
1607 // the preference.
1608 if ((LOrder || ROrder) && LOrder != ROrder)
1609 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1610
1611 return BURRSort(left, right, SPQ);
1612}
1613
Evan Chengbdd062d2010-05-20 06:13:19 +00001614bool hybrid_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
Evan Chengdebf9c52010-11-03 00:45:17 +00001615 if (left->isCall || right->isCall)
1616 // No way to compute latency of calls.
1617 return BURRSort(left, right, SPQ);
1618
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001619 bool LHigh = SPQ->HighRegPressure(left);
1620 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001621 // Avoid causing spills. If register pressure is high, schedule for
1622 // register pressure reduction.
Evan Cheng28590382010-07-21 23:53:58 +00001623 if (LHigh && !RHigh)
1624 return true;
1625 else if (!LHigh && RHigh)
1626 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001627 else if (!LHigh && !RHigh) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001628 // If the two nodes share an operand and one of them has a single
1629 // use that is a live out copy, favor the one that is live out. Otherwise
1630 // it will be difficult to eliminate the copy if the instruction is a
1631 // loop induction variable update. e.g.
1632 // BB:
1633 // sub r1, r3, #1
1634 // str r0, [r2, r3]
1635 // mov r3, r1
1636 // cmp
1637 // bne BB
1638 bool SharePred = UnitsSharePred(left, right);
1639 // FIXME: Only adjust if BB is a loop back edge.
1640 // FIXME: What's the cost of a copy?
1641 int LBonus = (SharePred && hasOnlyLiveOutUses(left)) ? 1 : 0;
1642 int RBonus = (SharePred && hasOnlyLiveOutUses(right)) ? 1 : 0;
1643 int LHeight = (int)left->getHeight() - LBonus;
1644 int RHeight = (int)right->getHeight() - RBonus;
1645
Evan Cheng28590382010-07-21 23:53:58 +00001646 // Low register pressure situation, schedule for latency if possible.
1647 bool LStall = left->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001648 (int)SPQ->getCurCycle() < LHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001649 bool RStall = right->SchedulingPref == Sched::Latency &&
Evan Cheng6c1414f2010-10-29 18:09:28 +00001650 (int)SPQ->getCurCycle() < RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001651 // If scheduling one of the node will cause a pipeline stall, delay it.
1652 // If scheduling either one of the node will cause a pipeline stall, sort
1653 // them according to their height.
Evan Cheng28590382010-07-21 23:53:58 +00001654 if (LStall) {
1655 if (!RStall)
1656 return true;
Evan Cheng6c1414f2010-10-29 18:09:28 +00001657 if (LHeight != RHeight)
1658 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001659 } else if (RStall)
Evan Chengbdd062d2010-05-20 06:13:19 +00001660 return false;
Evan Chengcc2efe12010-05-28 23:26:21 +00001661
Evan Cheng6c1414f2010-10-29 18:09:28 +00001662 // If either node is scheduling for latency, sort them by height
1663 // and latency.
Evan Cheng28590382010-07-21 23:53:58 +00001664 if (left->SchedulingPref == Sched::Latency ||
1665 right->SchedulingPref == Sched::Latency) {
Evan Cheng6c1414f2010-10-29 18:09:28 +00001666 if (LHeight != RHeight)
1667 return LHeight > RHeight;
Evan Cheng28590382010-07-21 23:53:58 +00001668 if (left->Latency != right->Latency)
1669 return left->Latency > right->Latency;
1670 }
Evan Chengcc2efe12010-05-28 23:26:21 +00001671 }
1672
Evan Chengbdd062d2010-05-20 06:13:19 +00001673 return BURRSort(left, right, SPQ);
1674}
1675
Evan Cheng37b740c2010-07-24 00:39:05 +00001676bool ilp_ls_rr_sort::operator()(const SUnit *left,
1677 const SUnit *right) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001678 if (left->isCall || right->isCall)
1679 // No way to compute latency of calls.
1680 return BURRSort(left, right, SPQ);
1681
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001682 bool LHigh = SPQ->HighRegPressure(left);
1683 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00001684 // Avoid causing spills. If register pressure is high, schedule for
1685 // register pressure reduction.
1686 if (LHigh && !RHigh)
1687 return true;
1688 else if (!LHigh && RHigh)
1689 return false;
Evan Chenge6d6c5d2010-07-26 21:49:07 +00001690 else if (!LHigh && !RHigh) {
Evan Cheng8ae3eca2010-07-25 18:59:43 +00001691 // Low register pressure situation, schedule to maximize instruction level
1692 // parallelism.
Evan Cheng37b740c2010-07-24 00:39:05 +00001693 if (left->NumPreds > right->NumPreds)
1694 return false;
1695 else if (left->NumPreds < right->NumPreds)
1696 return false;
1697 }
1698
1699 return BURRSort(left, right, SPQ);
1700}
1701
Dan Gohman3f656df2008-11-20 02:45:51 +00001702template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001703bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001704RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001705 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001706 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001707 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001708 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001709 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001710 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001711 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001712 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001713 if (DU->getNodeId() != -1 &&
1714 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001715 return true;
1716 }
1717 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001718 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001719 return false;
1720}
1721
Evan Chengf9891412007-12-20 09:25:31 +00001722/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001723/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001724static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001725 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001726 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001727 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001728 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1729 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001730 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001731 for (const SDNode *SUNode = SU->getNode(); SUNode;
1732 SUNode = SUNode->getFlaggedNode()) {
1733 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001734 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001735 const unsigned *SUImpDefs =
1736 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1737 if (!SUImpDefs)
1738 return false;
1739 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001740 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001741 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001742 continue;
1743 if (!N->hasAnyUseOfValue(i))
1744 continue;
1745 unsigned Reg = ImpDefs[i - NumDefs];
1746 for (;*SUImpDefs; ++SUImpDefs) {
1747 unsigned SUReg = *SUImpDefs;
1748 if (TRI->regsOverlap(Reg, SUReg))
1749 return true;
1750 }
Evan Chengf9891412007-12-20 09:25:31 +00001751 }
1752 }
1753 return false;
1754}
1755
Dan Gohman9a658d72009-03-24 00:49:12 +00001756/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1757/// are not handled well by the general register pressure reduction
1758/// heuristics. When presented with code like this:
1759///
1760/// N
1761/// / |
1762/// / |
1763/// U store
1764/// |
1765/// ...
1766///
1767/// the heuristics tend to push the store up, but since the
1768/// operand of the store has another use (U), this would increase
1769/// the length of that other use (the U->N edge).
1770///
1771/// This function transforms code like the above to route U's
1772/// dependence through the store when possible, like this:
1773///
1774/// N
1775/// ||
1776/// ||
1777/// store
1778/// |
1779/// U
1780/// |
1781/// ...
1782///
1783/// This results in the store being scheduled immediately
1784/// after N, which shortens the U->N live range, reducing
1785/// register pressure.
1786///
1787template<class SF>
1788void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1789 // Visit all the nodes in topological order, working top-down.
1790 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1791 SUnit *SU = &(*SUnits)[i];
1792 // For now, only look at nodes with no data successors, such as stores.
1793 // These are especially important, due to the heuristics in
1794 // getNodePriority for nodes with no data successors.
1795 if (SU->NumSuccs != 0)
1796 continue;
1797 // For now, only look at nodes with exactly one data predecessor.
1798 if (SU->NumPreds != 1)
1799 continue;
1800 // Avoid prescheduling copies to virtual registers, which don't behave
1801 // like other nodes from the perspective of scheduling heuristics.
1802 if (SDNode *N = SU->getNode())
1803 if (N->getOpcode() == ISD::CopyToReg &&
1804 TargetRegisterInfo::isVirtualRegister
1805 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1806 continue;
1807
1808 // Locate the single data predecessor.
1809 SUnit *PredSU = 0;
1810 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1811 EE = SU->Preds.end(); II != EE; ++II)
1812 if (!II->isCtrl()) {
1813 PredSU = II->getSUnit();
1814 break;
1815 }
1816 assert(PredSU);
1817
1818 // Don't rewrite edges that carry physregs, because that requires additional
1819 // support infrastructure.
1820 if (PredSU->hasPhysRegDefs)
1821 continue;
1822 // Short-circuit the case where SU is PredSU's only data successor.
1823 if (PredSU->NumSuccs == 1)
1824 continue;
1825 // Avoid prescheduling to copies from virtual registers, which don't behave
1826 // like other nodes from the perspective of scheduling // heuristics.
1827 if (SDNode *N = SU->getNode())
1828 if (N->getOpcode() == ISD::CopyFromReg &&
1829 TargetRegisterInfo::isVirtualRegister
1830 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1831 continue;
1832
1833 // Perform checks on the successors of PredSU.
1834 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1835 EE = PredSU->Succs.end(); II != EE; ++II) {
1836 SUnit *PredSuccSU = II->getSUnit();
1837 if (PredSuccSU == SU) continue;
1838 // If PredSU has another successor with no data successors, for
1839 // now don't attempt to choose either over the other.
1840 if (PredSuccSU->NumSuccs == 0)
1841 goto outer_loop_continue;
1842 // Don't break physical register dependencies.
1843 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1844 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1845 goto outer_loop_continue;
1846 // Don't introduce graph cycles.
1847 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1848 goto outer_loop_continue;
1849 }
1850
1851 // Ok, the transformation is safe and the heuristics suggest it is
1852 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00001853 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
1854 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001855 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001856 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1857 SDep Edge = PredSU->Succs[i];
1858 assert(!Edge.isAssignedRegDep());
1859 SUnit *SuccSU = Edge.getSUnit();
1860 if (SuccSU != SU) {
1861 Edge.setSUnit(PredSU);
1862 scheduleDAG->RemovePred(SuccSU, Edge);
1863 scheduleDAG->AddPred(SU, Edge);
1864 Edge.setSUnit(SU);
1865 scheduleDAG->AddPred(SuccSU, Edge);
1866 --i;
1867 }
1868 }
1869 outer_loop_continue:;
1870 }
1871}
1872
Evan Chengd38c22b2006-05-11 23:55:42 +00001873/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1874/// it as a def&use operand. Add a pseudo control edge from it to the other
1875/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001876/// first (lower in the schedule). If both nodes are two-address, favor the
1877/// one that has a CopyToReg use (more likely to be a loop induction update).
1878/// If both are two-address, but one is commutable while the other is not
1879/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001880template<class SF>
1881void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001882 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001883 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001884 if (!SU->isTwoAddress)
1885 continue;
1886
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001887 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001888 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001889 continue;
1890
Evan Cheng6c1414f2010-10-29 18:09:28 +00001891 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00001892 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001893 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001894 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001895 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001896 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001897 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1898 continue;
1899 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1900 if (DU->getNodeId() == -1)
1901 continue;
1902 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1903 if (!DUSU) continue;
1904 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1905 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001906 if (I->isCtrl()) continue;
1907 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001908 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001909 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001910 // Be conservative. Ignore if nodes aren't at roughly the same
1911 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001912 if (SuccSU->getHeight() < SU->getHeight() &&
1913 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001914 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001915 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1916 // constrains whatever is using the copy, instead of the copy
1917 // itself. In the case that the copy is coalesced, this
1918 // preserves the intent of the pseudo two-address heurietics.
1919 while (SuccSU->Succs.size() == 1 &&
1920 SuccSU->getNode()->isMachineOpcode() &&
1921 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001922 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001923 SuccSU = SuccSU->Succs.front().getSUnit();
1924 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001925 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1926 continue;
1927 // Don't constrain nodes with physical register defs if the
1928 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001929 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001930 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001931 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001932 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001933 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1934 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001935 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001936 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1937 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1938 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001939 continue;
1940 if ((!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00001941 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00001942 (!SU->isCommutable && SuccSU->isCommutable)) &&
1943 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00001944 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001945 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001946 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001947 /*Reg=*/0, /*isNormalMemory=*/false,
1948 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001949 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001950 }
1951 }
1952 }
1953 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001954}
1955
Evan Cheng6730f032007-01-08 23:55:53 +00001956/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1957/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001958template<class SF>
1959void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001960 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick2085a962010-12-21 22:25:04 +00001961
Evan Chengd38c22b2006-05-11 23:55:42 +00001962 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001963 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001964}
Evan Chengd38c22b2006-05-11 23:55:42 +00001965
Roman Levenstein30d09512008-03-27 09:44:37 +00001966/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001967/// predecessors of the successors of the SUnit SU. Stop when the provided
1968/// limit is exceeded.
Andrew Trick2085a962010-12-21 22:25:04 +00001969static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
Roman Levensteinbc674502008-03-27 09:14:57 +00001970 unsigned Limit) {
1971 unsigned Sum = 0;
1972 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1973 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001974 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001975 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1976 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001977 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001978 if (!PredSU->isScheduled)
1979 if (++Sum > Limit)
1980 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001981 }
1982 }
1983 return Sum;
1984}
1985
Evan Chengd38c22b2006-05-11 23:55:42 +00001986
1987// Top down
1988bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001989 unsigned LPriority = SPQ->getNodePriority(left);
1990 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001991 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1992 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001993 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1994 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001995 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1996 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001997
1998 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1999 return false;
2000 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
2001 return true;
2002
Evan Chengd38c22b2006-05-11 23:55:42 +00002003 if (LIsFloater)
2004 LBonus -= 2;
2005 if (RIsFloater)
2006 RBonus -= 2;
2007 if (left->NumSuccs == 1)
2008 LBonus += 2;
2009 if (right->NumSuccs == 1)
2010 RBonus += 2;
2011
Evan Cheng73bdf042008-03-01 00:39:47 +00002012 if (LPriority+LBonus != RPriority+RBonus)
2013 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00002014
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002015 if (left->getDepth() != right->getDepth())
2016 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00002017
2018 if (left->NumSuccsLeft != right->NumSuccsLeft)
2019 return left->NumSuccsLeft > right->NumSuccsLeft;
2020
Andrew Trick2085a962010-12-21 22:25:04 +00002021 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002022 "NodeQueueId cannot be zero");
2023 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002024}
2025
Evan Chengd38c22b2006-05-11 23:55:42 +00002026//===----------------------------------------------------------------------===//
2027// Public Constructor Functions
2028//===----------------------------------------------------------------------===//
2029
Dan Gohmandfaf6462009-02-11 04:27:20 +00002030llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002031llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002032 const TargetMachine &TM = IS->TM;
2033 const TargetInstrInfo *TII = TM.getInstrInfo();
2034 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002035
Evan Chenga77f3d32010-07-21 06:09:07 +00002036 BURegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002037 new BURegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002038 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002039 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002040 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002041}
2042
Dan Gohmandfaf6462009-02-11 04:27:20 +00002043llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00002044llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00002045 const TargetMachine &TM = IS->TM;
2046 const TargetInstrInfo *TII = TM.getInstrInfo();
2047 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002048
Evan Chenga77f3d32010-07-21 06:09:07 +00002049 TDRegReductionPriorityQueue *PQ =
2050 new TDRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002051 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00002052 PQ->setScheduleDAG(SD);
2053 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002054}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002055
2056llvm::ScheduleDAGSDNodes *
2057llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2058 const TargetMachine &TM = IS->TM;
2059 const TargetInstrInfo *TII = TM.getInstrInfo();
2060 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002061
Evan Chenga77f3d32010-07-21 06:09:07 +00002062 SrcRegReductionPriorityQueue *PQ =
Evan Chengbf32e542010-07-22 06:24:48 +00002063 new SrcRegReductionPriorityQueue(*IS->MF, false, TII, TRI, 0);
Evan Chengbdd062d2010-05-20 06:13:19 +00002064 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, false, PQ);
2065 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002066 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00002067}
2068
2069llvm::ScheduleDAGSDNodes *
2070llvm::createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2071 const TargetMachine &TM = IS->TM;
2072 const TargetInstrInfo *TII = TM.getInstrInfo();
2073 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002074 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002075
Evan Chenga77f3d32010-07-21 06:09:07 +00002076 HybridBURRPriorityQueue *PQ =
Evan Chengdf907f42010-07-23 22:39:59 +00002077 new HybridBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
Evan Chengbdd062d2010-05-20 06:13:19 +00002078 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002079 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002080 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002081}
Evan Cheng37b740c2010-07-24 00:39:05 +00002082
2083llvm::ScheduleDAGSDNodes *
2084llvm::createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
2085 const TargetMachine &TM = IS->TM;
2086 const TargetInstrInfo *TII = TM.getInstrInfo();
2087 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
2088 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002089
Evan Cheng37b740c2010-07-24 00:39:05 +00002090 ILPBURRPriorityQueue *PQ =
2091 new ILPBURRPriorityQueue(*IS->MF, true, TII, TRI, TLI);
2092 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, true, PQ);
2093 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002094 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00002095}