blob: da028500bd0a10c93944d30f21c828dedf7c3555 [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"
Dan Gohmana4db3352008-06-21 18:35:25 +000027#include "llvm/ADT/PriorityQueue.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 Chengd38c22b2006-05-11 23:55:42 +000056namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +000057//===----------------------------------------------------------------------===//
58/// ScheduleDAGRRList - The actual register reduction list scheduler
59/// implementation. This supports both top-down and bottom-up scheduling.
60///
Nick Lewycky02d5f772009-10-25 06:33:48 +000061class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +000062private:
63 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
64 /// it is top-down.
65 bool isBottomUp;
Evan Cheng2c977312008-07-01 18:05:03 +000066
Evan Chengd38c22b2006-05-11 23:55:42 +000067 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +000068 SchedulingPriorityQueue *AvailableQueue;
69
Dan Gohmanc07f6862008-09-23 18:50:48 +000070 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +000071 /// that are "live". These nodes must be scheduled before any other nodes that
72 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000073 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +000074 std::vector<SUnit*> LiveRegDefs;
75 std::vector<unsigned> LiveRegCycles;
76
Dan Gohmanad2134d2008-11-25 00:52:40 +000077 /// Topo - A topological ordering for SUnits which permits fast IsReachable
78 /// and similar queries.
79 ScheduleDAGTopologicalSort Topo;
80
Evan Chengd38c22b2006-05-11 23:55:42 +000081public:
Dan Gohman619ef482009-01-15 19:20:50 +000082 ScheduleDAGRRList(MachineFunction &mf,
83 bool isbottomup,
Evan Cheng2c977312008-07-01 18:05:03 +000084 SchedulingPriorityQueue *availqueue)
Dan Gohman619ef482009-01-15 19:20:50 +000085 : ScheduleDAGSDNodes(mf), isBottomUp(isbottomup),
Dan Gohmanad2134d2008-11-25 00:52:40 +000086 AvailableQueue(availqueue), Topo(SUnits) {
Evan Chengd38c22b2006-05-11 23:55:42 +000087 }
88
89 ~ScheduleDAGRRList() {
90 delete AvailableQueue;
91 }
92
93 void Schedule();
94
Roman Levenstein733a4d62008-03-26 11:23:38 +000095 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +000096 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
97 return Topo.IsReachable(SU, TargetSU);
98 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +000099
Dan Gohman60d68442009-01-29 19:49:27 +0000100 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000101 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000102 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
103 return Topo.WillCreateCycle(SU, TargetSU);
104 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000105
Dan Gohman2d170892008-12-09 22:54:47 +0000106 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000107 /// This returns true if this is a new predecessor.
108 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000109 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000110 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000111 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000112 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000113
Dan Gohman2d170892008-12-09 22:54:47 +0000114 /// RemovePred - removes a predecessor edge from SUnit SU.
115 /// This returns true if an edge was removed.
116 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000117 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000118 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000119 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000120 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000121
Evan Chengd38c22b2006-05-11 23:55:42 +0000122private:
Dan Gohman60d68442009-01-29 19:49:27 +0000123 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000124 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman60d68442009-01-29 19:49:27 +0000125 void ReleaseSucc(SUnit *SU, const SDep *SuccEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000126 void ReleaseSuccessors(SUnit *SU);
Dan Gohman2d170892008-12-09 22:54:47 +0000127 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000128 void ScheduleNodeBottomUp(SUnit*, unsigned);
129 void ScheduleNodeTopDown(SUnit*, unsigned);
130 void UnscheduleNodeBottomUp(SUnit*);
131 void BacktrackBottomUp(SUnit*, unsigned, unsigned&);
132 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000133 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
134 const TargetRegisterClass*,
135 const TargetRegisterClass*,
136 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000137 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Evan Chengd38c22b2006-05-11 23:55:42 +0000138 void ListScheduleTopDown();
139 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000140
141
142 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000143 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000144 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000145 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000146 SUnit *NewNode = NewSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000147 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000148 if (NewNode->NodeNum >= NumSUnits)
149 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000150 return NewNode;
151 }
152
Roman Levenstein733a4d62008-03-26 11:23:38 +0000153 /// CreateClone - Creates a new SUnit from an existing one.
154 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000155 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000156 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000157 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000158 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000159 if (NewNode->NodeNum >= NumSUnits)
160 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000161 return NewNode;
162 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000163
164 /// ForceUnitLatencies - Return true, since register-pressure-reducing
165 /// scheduling doesn't need actual latency information.
166 bool ForceUnitLatencies() const { return true; }
Evan Chengd38c22b2006-05-11 23:55:42 +0000167};
168} // end anonymous namespace
169
170
171/// Schedule - Schedule the DAG using list scheduling.
172void ScheduleDAGRRList::Schedule() {
David Greenef34d7ac2010-01-05 01:24:54 +0000173 DEBUG(dbgs() << "********** List Scheduling **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000174
Dan Gohmanc07f6862008-09-23 18:50:48 +0000175 NumLiveRegs = 0;
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000176 LiveRegDefs.resize(TRI->getNumRegs(), NULL);
177 LiveRegCycles.resize(TRI->getNumRegs(), 0);
Evan Cheng5924bf72007-09-25 01:54:36 +0000178
Dan Gohman04543e72008-12-23 18:36:58 +0000179 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000180 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000181
Evan Chengd38c22b2006-05-11 23:55:42 +0000182 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000183 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000184 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000185
Dan Gohman46520a22008-06-21 19:18:17 +0000186 AvailableQueue->initNodes(SUnits);
Dan Gohman54a187e2007-08-20 19:28:38 +0000187
Evan Chengd38c22b2006-05-11 23:55:42 +0000188 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
189 if (isBottomUp)
190 ListScheduleBottomUp();
191 else
192 ListScheduleTopDown();
193
194 AvailableQueue->releaseState();
Evan Chengafed73e2006-05-12 01:58:24 +0000195}
Evan Chengd38c22b2006-05-11 23:55:42 +0000196
197//===----------------------------------------------------------------------===//
198// Bottom-Up Scheduling
199//===----------------------------------------------------------------------===//
200
Evan Chengd38c22b2006-05-11 23:55:42 +0000201/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000202/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000203void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000204 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000205
Evan Chengd38c22b2006-05-11 23:55:42 +0000206#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000207 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000208 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000209 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000210 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000211 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000212 }
213#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000214 --PredSU->NumSuccsLeft;
215
Dan Gohmanb9543432009-02-10 23:27:53 +0000216 // If all the node's successors are scheduled, this node is ready
217 // to be scheduled. Ignore the special EntrySU node.
218 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000219 PredSU->isAvailable = true;
220 AvailableQueue->push(PredSU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000221 }
222}
223
Dan Gohmanb9543432009-02-10 23:27:53 +0000224void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000225 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000226 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000227 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000228 ReleasePred(SU, &*I);
229 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000230 // This is a physical register dependency and it's impossible or
231 // expensive to copy the register. Make sure nothing that can
232 // clobber the register is scheduled between the predecessor and
233 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000234 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000235 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000236 LiveRegDefs[I->getReg()] = I->getSUnit();
237 LiveRegCycles[I->getReg()] = CurCycle;
Evan Cheng5924bf72007-09-25 01:54:36 +0000238 }
239 }
240 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000241}
242
243/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
244/// count of its predecessors. If a predecessor pending count is zero, add it to
245/// the Available queue.
246void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000247 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000248 DEBUG(SU->dump(this));
249
250 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
251 SU->setHeightToAtLeast(CurCycle);
252 Sequence.push_back(SU);
253
254 ReleasePredecessors(SU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000255
256 // Release all the implicit physical register defs that are live.
257 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
258 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000259 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000260 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000261 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000262 assert(LiveRegDefs[I->getReg()] == SU &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000263 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000264 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000265 LiveRegDefs[I->getReg()] = NULL;
266 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000267 }
268 }
269 }
270
Evan Chengd38c22b2006-05-11 23:55:42 +0000271 SU->isScheduled = true;
Dan Gohman6e587262008-11-18 21:22:20 +0000272 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000273}
274
Evan Cheng5924bf72007-09-25 01:54:36 +0000275/// CapturePred - This does the opposite of ReleasePred. Since SU is being
276/// unscheduled, incrcease the succ left count of its predecessors. Remove
277/// them from AvailableQueue if necessary.
Dan Gohman2d170892008-12-09 22:54:47 +0000278void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
279 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000280 if (PredSU->isAvailable) {
281 PredSU->isAvailable = false;
282 if (!PredSU->isPending)
283 AvailableQueue->remove(PredSU);
284 }
285
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000286 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000287 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000288}
289
290/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
291/// its predecessor states to reflect the change.
292void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000293 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000294 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000295
296 AvailableQueue->UnscheduledNode(SU);
297
298 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
299 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000300 CapturePred(&*I);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000301 if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000302 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000303 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000304 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000305 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000306 LiveRegDefs[I->getReg()] = NULL;
307 LiveRegCycles[I->getReg()] = 0;
Evan Cheng5924bf72007-09-25 01:54:36 +0000308 }
309 }
310
311 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
312 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000313 if (I->isAssignedRegDep()) {
314 if (!LiveRegDefs[I->getReg()]) {
315 LiveRegDefs[I->getReg()] = SU;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000316 ++NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000317 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000318 if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()])
319 LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000320 }
321 }
322
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000323 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000324 SU->isScheduled = false;
325 SU->isAvailable = true;
326 AvailableQueue->push(SU);
327}
328
Evan Cheng8e136a92007-09-26 21:36:17 +0000329/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000330/// BTCycle in order to schedule a specific node.
Evan Cheng8e136a92007-09-26 21:36:17 +0000331void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle,
332 unsigned &CurCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000333 SUnit *OldSU = NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000334 while (CurCycle > BtCycle) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000335 OldSU = Sequence.back();
336 Sequence.pop_back();
337 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000338 // Don't try to remove SU from AvailableQueue.
339 SU->isAvailable = false;
Evan Cheng5924bf72007-09-25 01:54:36 +0000340 UnscheduleNodeBottomUp(OldSU);
341 --CurCycle;
342 }
343
Dan Gohman60d68442009-01-29 19:49:27 +0000344 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000345
346 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000347}
348
Evan Cheng3b245872010-02-05 01:27:11 +0000349static bool isOperandOf(const SUnit *SU, SDNode *N) {
350 for (const SDNode *SUNode = SU->getNode(); SUNode;
351 SUNode = SUNode->getFlaggedNode()) {
352 if (SUNode->isOperandOf(N))
353 return true;
354 }
355 return false;
356}
357
Evan Cheng5924bf72007-09-25 01:54:36 +0000358/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
359/// successors to the newly created node.
360SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman072734e2008-11-13 23:24:17 +0000361 if (SU->getNode()->getFlaggedNode())
Evan Cheng79e97132007-10-05 01:39:18 +0000362 return NULL;
Evan Cheng8e136a92007-09-26 21:36:17 +0000363
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000364 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000365 if (!N)
366 return NULL;
367
368 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000369 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000370 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000371 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +0000372 if (VT == MVT::Flag)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000373 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000374 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000375 TryUnfold = true;
376 }
Evan Cheng79e97132007-10-05 01:39:18 +0000377 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000378 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000379 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Owen Anderson9f944592009-08-11 20:47:22 +0000380 if (VT == MVT::Flag)
Evan Cheng79e97132007-10-05 01:39:18 +0000381 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000382 }
383
384 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000385 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000386 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000387 return NULL;
388
David Greenef34d7ac2010-01-05 01:24:54 +0000389 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000390 assert(NewNodes.size() == 2 && "Expected a load folding node!");
391
392 N = NewNodes[1];
393 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000394 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000395 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000396 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000397 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
398 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000399 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000400
Dan Gohmane52e0892008-11-11 21:34:44 +0000401 // LoadNode may already exist. This can happen when there is another
402 // load from the same location and producing the same type of value
403 // but it has different alignment or volatileness.
404 bool isNewLoad = true;
405 SUnit *LoadSU;
406 if (LoadNode->getNodeId() != -1) {
407 LoadSU = &SUnits[LoadNode->getNodeId()];
408 isNewLoad = false;
409 } else {
410 LoadSU = CreateNewSUnit(LoadNode);
411 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohmane52e0892008-11-11 21:34:44 +0000412 ComputeLatency(LoadSU);
413 }
414
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000415 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000416 assert(N->getNodeId() == -1 && "Node already inserted!");
417 N->setNodeId(NewSU->NodeNum);
Dan Gohmane6e13482008-06-21 15:52:51 +0000418
Dan Gohman17059682008-07-17 19:10:17 +0000419 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Dan Gohman856c0122008-02-16 00:25:40 +0000420 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000421 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000422 NewSU->isTwoAddress = true;
423 break;
424 }
425 }
Chris Lattnerfd2e3382008-01-07 06:47:00 +0000426 if (TID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +0000427 NewSU->isCommutable = true;
Evan Cheng79e97132007-10-05 01:39:18 +0000428 ComputeLatency(NewSU);
429
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000430 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +0000431 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +0000432 SmallVector<SDep, 4> ChainSuccs;
433 SmallVector<SDep, 4> LoadPreds;
434 SmallVector<SDep, 4> NodePreds;
435 SmallVector<SDep, 4> NodeSuccs;
436 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
437 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000438 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +0000439 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +0000440 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +0000441 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000442 else
Dan Gohman2d170892008-12-09 22:54:47 +0000443 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000444 }
445 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
446 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000447 if (I->isCtrl())
448 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000449 else
Dan Gohman2d170892008-12-09 22:54:47 +0000450 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +0000451 }
452
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000453 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +0000454 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
455 const SDep &Pred = ChainPreds[i];
456 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +0000457 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +0000458 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000459 }
Evan Cheng79e97132007-10-05 01:39:18 +0000460 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000461 const SDep &Pred = LoadPreds[i];
462 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +0000463 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000464 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000465 }
466 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000467 const SDep &Pred = NodePreds[i];
468 RemovePred(SU, Pred);
469 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +0000470 }
471 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000472 SDep D = NodeSuccs[i];
473 SUnit *SuccDep = D.getSUnit();
474 D.setSUnit(SU);
475 RemovePred(SuccDep, D);
476 D.setSUnit(NewSU);
477 AddPred(SuccDep, D);
Evan Cheng79e97132007-10-05 01:39:18 +0000478 }
479 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000480 SDep D = ChainSuccs[i];
481 SUnit *SuccDep = D.getSUnit();
482 D.setSUnit(SU);
483 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000484 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000485 D.setSUnit(LoadSU);
486 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000487 }
Evan Cheng79e97132007-10-05 01:39:18 +0000488 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +0000489
490 // Add a data dependency to reflect that NewSU reads the value defined
491 // by LoadSU.
492 AddPred(NewSU, SDep(LoadSU, SDep::Data, LoadSU->Latency));
Evan Cheng79e97132007-10-05 01:39:18 +0000493
Evan Cheng91e0fc92007-12-18 08:42:10 +0000494 if (isNewLoad)
495 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +0000496 AvailableQueue->addNode(NewSU);
497
498 ++NumUnfolds;
499
500 if (NewSU->NumSuccsLeft == 0) {
501 NewSU->isAvailable = true;
502 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +0000503 }
504 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000505 }
506
David Greenef34d7ac2010-01-05 01:24:54 +0000507 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000508 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000509
510 // New SUnit has the exact same predecessors.
511 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
512 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000513 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000514 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +0000515
516 // Only copy scheduled successors. Cut them from old node's successor
517 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000518 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +0000519 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
520 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000521 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +0000522 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000523 SUnit *SuccSU = I->getSUnit();
524 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000525 SDep D = *I;
526 D.setSUnit(NewSU);
527 AddPred(SuccSU, D);
528 D.setSUnit(SU);
529 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +0000530 }
531 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000532 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000533 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +0000534
535 AvailableQueue->updateNode(SU);
536 AvailableQueue->addNode(NewSU);
537
Evan Cheng1ec79b42007-09-27 07:09:03 +0000538 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +0000539 return NewSU;
540}
541
Evan Chengb2c42c62009-01-12 03:19:55 +0000542/// InsertCopiesAndMoveSuccs - Insert register copies and move all
543/// scheduled successors of the given SUnit to the last copy.
544void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
545 const TargetRegisterClass *DestRC,
546 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +0000547 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000548 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000549 CopyFromSU->CopySrcRC = SrcRC;
550 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +0000551
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000552 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +0000553 CopyToSU->CopySrcRC = DestRC;
554 CopyToSU->CopyDstRC = SrcRC;
555
556 // Only copy scheduled successors. Cut them from old node's successor
557 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000558 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +0000559 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
560 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000561 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +0000562 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000563 SUnit *SuccSU = I->getSUnit();
564 if (SuccSU->isScheduled) {
565 SDep D = *I;
566 D.setSUnit(CopyToSU);
567 AddPred(SuccSU, D);
568 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +0000569 }
570 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000571 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000572 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +0000573
Dan Gohman2d170892008-12-09 22:54:47 +0000574 AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg));
575 AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0));
Evan Cheng8e136a92007-09-26 21:36:17 +0000576
577 AvailableQueue->updateNode(SU);
578 AvailableQueue->addNode(CopyFromSU);
579 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000580 Copies.push_back(CopyFromSU);
581 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +0000582
Evan Chengb2c42c62009-01-12 03:19:55 +0000583 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +0000584}
585
586/// getPhysicalRegisterVT - Returns the ValueType of the physical register
587/// definition of the specified node.
588/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +0000589static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +0000590 const TargetInstrInfo *TII) {
Dan Gohman17059682008-07-17 19:10:17 +0000591 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Cheng8e136a92007-09-26 21:36:17 +0000592 assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!");
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000593 unsigned NumRes = TID.getNumDefs();
594 for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +0000595 if (Reg == *ImpDef)
596 break;
597 ++NumRes;
598 }
599 return N->getValueType(NumRes);
600}
601
Evan Chengb8905c42009-03-04 01:41:49 +0000602/// CheckForLiveRegDef - Return true and update live register vector if the
603/// specified register def of the specified SUnit clobbers any "live" registers.
604static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
605 std::vector<SUnit*> &LiveRegDefs,
606 SmallSet<unsigned, 4> &RegAdded,
607 SmallVector<unsigned, 4> &LRegs,
608 const TargetRegisterInfo *TRI) {
609 bool Added = false;
610 if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != SU) {
611 if (RegAdded.insert(Reg)) {
612 LRegs.push_back(Reg);
613 Added = true;
614 }
615 }
616 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
617 if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) {
618 if (RegAdded.insert(*Alias)) {
619 LRegs.push_back(*Alias);
620 Added = true;
621 }
622 }
623 return Added;
624}
625
Evan Cheng5924bf72007-09-25 01:54:36 +0000626/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
627/// scheduling of the given node to satisfy live physical register dependencies.
628/// If the specific node is the last one that's available to schedule, do
629/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000630bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU,
631 SmallVector<unsigned, 4> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000632 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +0000633 return false;
634
Evan Chenge6f92252007-09-27 18:46:06 +0000635 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +0000636 // If this node would clobber any "live" register, then it's not ready.
Evan Cheng5924bf72007-09-25 01:54:36 +0000637 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
638 I != E; ++I) {
Evan Chengb8905c42009-03-04 01:41:49 +0000639 if (I->isAssignedRegDep())
640 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
641 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000642 }
643
Dan Gohman072734e2008-11-13 23:24:17 +0000644 for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +0000645 if (Node->getOpcode() == ISD::INLINEASM) {
646 // Inline asm can clobber physical defs.
647 unsigned NumOps = Node->getNumOperands();
Owen Anderson9f944592009-08-11 20:47:22 +0000648 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
Evan Chengb8905c42009-03-04 01:41:49 +0000649 --NumOps; // Ignore the flag operand.
650
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000651 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +0000652 unsigned Flags =
653 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000654 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +0000655
656 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +0000657 if (InlineAsm::isRegDefKind(Flags) ||
658 InlineAsm::isRegDefEarlyClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +0000659 // Check for def of register or earlyclobber register.
660 for (; NumVals; --NumVals, ++i) {
661 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
662 if (TargetRegisterInfo::isPhysicalRegister(Reg))
663 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
664 }
665 } else
666 i += NumVals;
667 }
668 continue;
669 }
670
Dan Gohman072734e2008-11-13 23:24:17 +0000671 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +0000672 continue;
Dan Gohman17059682008-07-17 19:10:17 +0000673 const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode());
Evan Cheng5924bf72007-09-25 01:54:36 +0000674 if (!TID.ImplicitDefs)
675 continue;
Evan Chengb8905c42009-03-04 01:41:49 +0000676 for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg)
677 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +0000678 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000679 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +0000680}
681
Evan Cheng1ec79b42007-09-27 07:09:03 +0000682
Evan Chengd38c22b2006-05-11 23:55:42 +0000683/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
684/// schedulers.
685void ScheduleDAGRRList::ListScheduleBottomUp() {
686 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000687
688 // Release any predecessors of the special Exit node.
689 ReleasePredecessors(&ExitSU, CurCycle);
690
Evan Chengd38c22b2006-05-11 23:55:42 +0000691 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +0000692 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000693 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +0000694 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
695 RootSU->isAvailable = true;
696 AvailableQueue->push(RootSU);
697 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000698
699 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000700 // priority. If it is not ready put it back. Schedule the node.
Evan Cheng5924bf72007-09-25 01:54:36 +0000701 SmallVector<SUnit*, 4> NotReady;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000702 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
Dan Gohmane6e13482008-06-21 15:52:51 +0000703 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000704 while (!AvailableQueue->empty()) {
Evan Cheng1ec79b42007-09-27 07:09:03 +0000705 bool Delayed = false;
Dan Gohmanfa63cc42008-06-23 21:15:00 +0000706 LRegsMap.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000707 SUnit *CurSU = AvailableQueue->pop();
708 while (CurSU) {
Dan Gohman63be5312008-11-21 01:30:54 +0000709 SmallVector<unsigned, 4> LRegs;
710 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
711 break;
712 Delayed = true;
713 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000714
715 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
716 NotReady.push_back(CurSU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000717 CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000718 }
Evan Cheng1ec79b42007-09-27 07:09:03 +0000719
720 // All candidates are delayed due to live physical reg dependencies.
721 // Try backtracking, code duplication, or inserting cross class copies
722 // to resolve it.
723 if (Delayed && !CurSU) {
724 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
725 SUnit *TrySU = NotReady[i];
726 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
727
728 // Try unscheduling up to the point where it's safe to schedule
729 // this node.
730 unsigned LiveCycle = CurCycle;
731 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
732 unsigned Reg = LRegs[j];
733 unsigned LCycle = LiveRegCycles[Reg];
734 LiveCycle = std::min(LiveCycle, LCycle);
735 }
736 SUnit *OldSU = Sequence[LiveCycle];
737 if (!WillCreateCycle(TrySU, OldSU)) {
738 BacktrackBottomUp(TrySU, LiveCycle, CurCycle);
739 // Force the current node to be scheduled before the node that
740 // requires the physical reg dep.
741 if (OldSU->isAvailable) {
742 OldSU->isAvailable = false;
743 AvailableQueue->remove(OldSU);
744 }
Dan Gohman2d170892008-12-09 22:54:47 +0000745 AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1,
746 /*Reg=*/0, /*isNormalMemory=*/false,
747 /*isMustAlias=*/false, /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000748 // If one or more successors has been unscheduled, then the current
749 // node is no longer avaialable. Schedule a successor that's now
750 // available instead.
751 if (!TrySU->isAvailable)
752 CurSU = AvailableQueue->pop();
753 else {
754 CurSU = TrySU;
755 TrySU->isPending = false;
756 NotReady.erase(NotReady.begin()+i);
757 }
758 break;
759 }
760 }
761
762 if (!CurSU) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000763 // Can't backtrack. If it's too expensive to copy the value, then try
764 // duplicate the nodes that produces these "too expensive to copy"
765 // values to break the dependency. In case even that doesn't work,
766 // insert cross class copies.
767 // If it's not too expensive, i.e. cost != -1, issue copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000768 SUnit *TrySU = NotReady[0];
769 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
770 assert(LRegs.size() == 1 && "Can't handle this yet!");
771 unsigned Reg = LRegs[0];
772 SUnit *LRDef = LiveRegDefs[Reg];
Owen Anderson53aa7a92009-08-10 22:56:29 +0000773 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000774 const TargetRegisterClass *RC =
775 TRI->getPhysicalRegisterRegClass(Reg, VT);
776 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
777
778 // If cross copy register class is null, then it must be possible copy
779 // the value directly. Do not try duplicate the def.
780 SUnit *NewDef = 0;
781 if (DestRC)
782 NewDef = CopyAndMoveSuccessors(LRDef);
783 else
784 DestRC = RC;
Evan Cheng79e97132007-10-05 01:39:18 +0000785 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000786 // Issue copies, these can be expensive cross register class copies.
Evan Cheng1ec79b42007-09-27 07:09:03 +0000787 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000788 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
David Greenef34d7ac2010-01-05 01:24:54 +0000789 DEBUG(dbgs() << "Adding an edge from SU #" << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000790 << " to SU #" << Copies.front()->NodeNum << "\n");
Dan Gohman2d170892008-12-09 22:54:47 +0000791 AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000792 /*Reg=*/0, /*isNormalMemory=*/false,
793 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000794 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000795 NewDef = Copies.back();
796 }
797
David Greenef34d7ac2010-01-05 01:24:54 +0000798 DEBUG(dbgs() << "Adding an edge from SU #" << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000799 << " to SU #" << TrySU->NodeNum << "\n");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000800 LiveRegDefs[Reg] = NewDef;
Dan Gohman2d170892008-12-09 22:54:47 +0000801 AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1,
Dan Gohmanbf8e5202009-01-06 01:28:56 +0000802 /*Reg=*/0, /*isNormalMemory=*/false,
803 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +0000804 /*isArtificial=*/true));
Evan Cheng1ec79b42007-09-27 07:09:03 +0000805 TrySU->isAvailable = false;
806 CurSU = NewDef;
807 }
808
Dan Gohman60d68442009-01-29 19:49:27 +0000809 assert(CurSU && "Unable to resolve live physical register dependencies!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000810 }
811
Evan Chengd38c22b2006-05-11 23:55:42 +0000812 // Add the nodes that aren't ready back onto the available list.
Evan Cheng5924bf72007-09-25 01:54:36 +0000813 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
814 NotReady[i]->isPending = false;
Evan Cheng1ec79b42007-09-27 07:09:03 +0000815 // May no longer be available due to backtracking.
Evan Cheng5924bf72007-09-25 01:54:36 +0000816 if (NotReady[i]->isAvailable)
817 AvailableQueue->push(NotReady[i]);
818 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000819 NotReady.clear();
820
Dan Gohmanc602dd42008-11-21 00:10:42 +0000821 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000822 ScheduleNodeBottomUp(CurSU, CurCycle);
Evan Cheng5924bf72007-09-25 01:54:36 +0000823 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000824 }
825
Evan Chengd38c22b2006-05-11 23:55:42 +0000826 // Reverse the order if it is bottom up.
827 std::reverse(Sequence.begin(), Sequence.end());
828
Evan Chengd38c22b2006-05-11 23:55:42 +0000829#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000830 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000831#endif
832}
833
834//===----------------------------------------------------------------------===//
835// Top-Down Scheduling
836//===----------------------------------------------------------------------===//
837
838/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000839/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000840void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, const SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000841 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000842
Evan Chengd38c22b2006-05-11 23:55:42 +0000843#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000844 if (SuccSU->NumPredsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000845 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000846 SuccSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000847 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000848 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000849 }
850#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000851 --SuccSU->NumPredsLeft;
852
Dan Gohmanb9543432009-02-10 23:27:53 +0000853 // If all the node's predecessors are scheduled, this node is ready
854 // to be scheduled. Ignore the special ExitSU node.
855 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000856 SuccSU->isAvailable = true;
857 AvailableQueue->push(SuccSU);
858 }
859}
860
Dan Gohmanb9543432009-02-10 23:27:53 +0000861void ScheduleDAGRRList::ReleaseSuccessors(SUnit *SU) {
862 // Top down: release successors
863 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
864 I != E; ++I) {
865 assert(!I->isAssignedRegDep() &&
866 "The list-tdrr scheduler doesn't yet support physreg dependencies!");
867
868 ReleaseSucc(SU, &*I);
869 }
870}
871
Evan Chengd38c22b2006-05-11 23:55:42 +0000872/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
873/// count of its successors. If a successor pending count is zero, add it to
874/// the Available queue.
Evan Chengd12c97d2006-05-30 18:05:39 +0000875void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenef34d7ac2010-01-05 01:24:54 +0000876 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000877 DEBUG(SU->dump(this));
Evan Chengd38c22b2006-05-11 23:55:42 +0000878
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000879 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
880 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000881 Sequence.push_back(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000882
Dan Gohmanb9543432009-02-10 23:27:53 +0000883 ReleaseSuccessors(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000884 SU->isScheduled = true;
Dan Gohman92a36d72008-11-17 21:31:02 +0000885 AvailableQueue->ScheduledNode(SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000886}
887
Dan Gohman54a187e2007-08-20 19:28:38 +0000888/// ListScheduleTopDown - The main loop of list scheduling for top-down
889/// schedulers.
Evan Chengd38c22b2006-05-11 23:55:42 +0000890void ScheduleDAGRRList::ListScheduleTopDown() {
891 unsigned CurCycle = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +0000892
Dan Gohmanb9543432009-02-10 23:27:53 +0000893 // Release any successors of the special Entry node.
894 ReleaseSuccessors(&EntrySU);
895
Evan Chengd38c22b2006-05-11 23:55:42 +0000896 // All leaves to Available queue.
897 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
898 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000899 if (SUnits[i].Preds.empty()) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000900 AvailableQueue->push(&SUnits[i]);
901 SUnits[i].isAvailable = true;
902 }
903 }
904
Evan Chengd38c22b2006-05-11 23:55:42 +0000905 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +0000906 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +0000907 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +0000908 while (!AvailableQueue->empty()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000909 SUnit *CurSU = AvailableQueue->pop();
Evan Chengd38c22b2006-05-11 23:55:42 +0000910
Dan Gohmanc602dd42008-11-21 00:10:42 +0000911 if (CurSU)
Evan Cheng5924bf72007-09-25 01:54:36 +0000912 ScheduleNodeTopDown(CurSU, CurCycle);
Dan Gohman4370f262008-04-15 01:22:18 +0000913 ++CurCycle;
Evan Chengd38c22b2006-05-11 23:55:42 +0000914 }
915
Evan Chengd38c22b2006-05-11 23:55:42 +0000916#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000917 VerifySchedule(isBottomUp);
Evan Chengd38c22b2006-05-11 23:55:42 +0000918#endif
919}
920
921
Evan Chengd38c22b2006-05-11 23:55:42 +0000922//===----------------------------------------------------------------------===//
923// RegReductionPriorityQueue Implementation
924//===----------------------------------------------------------------------===//
925//
926// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
927// to reduce register pressure.
928//
929namespace {
930 template<class SF>
931 class RegReductionPriorityQueue;
932
933 /// Sorting functions for the Available queue.
934 struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
935 RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
936 bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
937 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
938
939 bool operator()(const SUnit* left, const SUnit* right) const;
940 };
941
942 struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
943 RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
944 td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
945 td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
946
947 bool operator()(const SUnit* left, const SUnit* right) const;
948 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +0000949
950 struct src_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
951 RegReductionPriorityQueue<src_ls_rr_sort> *SPQ;
952 src_ls_rr_sort(RegReductionPriorityQueue<src_ls_rr_sort> *spq)
953 : SPQ(spq) {}
954 src_ls_rr_sort(const src_ls_rr_sort &RHS)
955 : SPQ(RHS.SPQ) {}
956
957 bool operator()(const SUnit* left, const SUnit* right) const;
958 };
Evan Chengd38c22b2006-05-11 23:55:42 +0000959} // end anonymous namespace
960
Dan Gohman186f65d2008-11-20 03:30:37 +0000961/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
962/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +0000963static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +0000964CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +0000965 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
966 if (SethiUllmanNumber != 0)
967 return SethiUllmanNumber;
968
969 unsigned Extra = 0;
970 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
971 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000972 if (I->isCtrl()) continue; // ignore chain preds
973 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +0000974 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +0000975 if (PredSethiUllman > SethiUllmanNumber) {
976 SethiUllmanNumber = PredSethiUllman;
977 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +0000978 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +0000979 ++Extra;
980 }
981
982 SethiUllmanNumber += Extra;
983
984 if (SethiUllmanNumber == 0)
985 SethiUllmanNumber = 1;
986
987 return SethiUllmanNumber;
988}
989
Evan Chengd38c22b2006-05-11 23:55:42 +0000990namespace {
991 template<class SF>
Nick Lewycky02d5f772009-10-25 06:33:48 +0000992 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
Dan Gohmana4db3352008-06-21 18:35:25 +0000993 PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue;
Roman Levenstein6b371142008-04-29 09:07:59 +0000994 unsigned currentQueueId;
Evan Chengd38c22b2006-05-11 23:55:42 +0000995
Dan Gohman3f656df2008-11-20 02:45:51 +0000996 protected:
997 // SUnits - The SUnits for the current graph.
998 std::vector<SUnit> *SUnits;
Evan Chengd38c22b2006-05-11 23:55:42 +0000999
Dan Gohman3f656df2008-11-20 02:45:51 +00001000 const TargetInstrInfo *TII;
1001 const TargetRegisterInfo *TRI;
1002 ScheduleDAGRRList *scheduleDAG;
1003
Dan Gohman186f65d2008-11-20 03:30:37 +00001004 // SethiUllmanNumbers - The SethiUllman number for each node.
1005 std::vector<unsigned> SethiUllmanNumbers;
1006
Dan Gohman3f656df2008-11-20 02:45:51 +00001007 public:
1008 RegReductionPriorityQueue(const TargetInstrInfo *tii,
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001009 const TargetRegisterInfo *tri)
1010 : Queue(SF(this)), currentQueueId(0),
1011 TII(tii), TRI(tri), scheduleDAG(NULL) {}
Dan Gohman3f656df2008-11-20 02:45:51 +00001012
1013 void initNodes(std::vector<SUnit> &sunits) {
1014 SUnits = &sunits;
Dan Gohman186f65d2008-11-20 03:30:37 +00001015 // Add pseudo dependency edges for two-address nodes.
1016 AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001017 // Reroute edges to nodes with multiple uses.
1018 PrescheduleNodesWithMultipleUses();
Dan Gohman186f65d2008-11-20 03:30:37 +00001019 // Calculate node priorities.
1020 CalculateSethiUllmanNumbers();
Dan Gohman3f656df2008-11-20 02:45:51 +00001021 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001022
Dan Gohman186f65d2008-11-20 03:30:37 +00001023 void addNode(const SUnit *SU) {
1024 unsigned SUSize = SethiUllmanNumbers.size();
1025 if (SUnits->size() > SUSize)
1026 SethiUllmanNumbers.resize(SUSize*2, 0);
1027 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1028 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001029
Dan Gohman186f65d2008-11-20 03:30:37 +00001030 void updateNode(const SUnit *SU) {
1031 SethiUllmanNumbers[SU->NodeNum] = 0;
1032 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1033 }
Evan Cheng5924bf72007-09-25 01:54:36 +00001034
Dan Gohman186f65d2008-11-20 03:30:37 +00001035 void releaseState() {
Dan Gohman3f656df2008-11-20 02:45:51 +00001036 SUnits = 0;
Dan Gohman186f65d2008-11-20 03:30:37 +00001037 SethiUllmanNumbers.clear();
Dan Gohman3f656df2008-11-20 02:45:51 +00001038 }
Dan Gohman186f65d2008-11-20 03:30:37 +00001039
1040 unsigned getNodePriority(const SUnit *SU) const {
1041 assert(SU->NodeNum < SethiUllmanNumbers.size());
1042 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001043 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
Dan Gohman186f65d2008-11-20 03:30:37 +00001044 // CopyToReg should be close to its uses to facilitate coalescing and
1045 // avoid spilling.
1046 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +00001047 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1048 Opc == TargetOpcode::SUBREG_TO_REG ||
1049 Opc == TargetOpcode::INSERT_SUBREG)
Dan Gohman3027bb62009-04-16 20:57:10 +00001050 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1051 // close to their uses to facilitate coalescing.
Dan Gohman186f65d2008-11-20 03:30:37 +00001052 return 0;
Dan Gohman6571ef32009-02-11 21:29:39 +00001053 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1054 // If SU does not have a register use, i.e. it doesn't produce a value
1055 // that would be consumed (e.g. store), then it terminates a chain of
1056 // computation. Give it a large SethiUllman number so it will be
1057 // scheduled right before its predecessors that it doesn't lengthen
1058 // their live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001059 return 0xffff;
Dan Gohman6571ef32009-02-11 21:29:39 +00001060 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1061 // If SU does not have a register def, schedule it close to its uses
1062 // because it does not lengthen any live ranges.
Dan Gohman186f65d2008-11-20 03:30:37 +00001063 return 0;
Dan Gohman261ee6b2009-01-07 22:30:55 +00001064 return SethiUllmanNumbers[SU->NodeNum];
Dan Gohman186f65d2008-11-20 03:30:37 +00001065 }
Bill Wendling0a7056f2010-01-05 23:48:12 +00001066
1067 unsigned getNodeOrdering(const SUnit *SU) const {
1068 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1069 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001070
Evan Cheng5924bf72007-09-25 01:54:36 +00001071 unsigned size() const { return Queue.size(); }
1072
Evan Chengd38c22b2006-05-11 23:55:42 +00001073 bool empty() const { return Queue.empty(); }
1074
1075 void push(SUnit *U) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001076 assert(!U->NodeQueueId && "Node in the queue already");
1077 U->NodeQueueId = ++currentQueueId;
Dan Gohmana4db3352008-06-21 18:35:25 +00001078 Queue.push(U);
Evan Chengd38c22b2006-05-11 23:55:42 +00001079 }
Roman Levenstein6b371142008-04-29 09:07:59 +00001080
Evan Chengd38c22b2006-05-11 23:55:42 +00001081 void push_all(const std::vector<SUnit *> &Nodes) {
1082 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Roman Levenstein6b371142008-04-29 09:07:59 +00001083 push(Nodes[i]);
Evan Chengd38c22b2006-05-11 23:55:42 +00001084 }
1085
1086 SUnit *pop() {
Evan Chengd12c97d2006-05-30 18:05:39 +00001087 if (empty()) return NULL;
Dan Gohmana4db3352008-06-21 18:35:25 +00001088 SUnit *V = Queue.top();
1089 Queue.pop();
Roman Levenstein6b371142008-04-29 09:07:59 +00001090 V->NodeQueueId = 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001091 return V;
1092 }
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001093
Evan Cheng5924bf72007-09-25 01:54:36 +00001094 void remove(SUnit *SU) {
Roman Levenstein6b371142008-04-29 09:07:59 +00001095 assert(!Queue.empty() && "Queue is empty!");
Dan Gohmana4db3352008-06-21 18:35:25 +00001096 assert(SU->NodeQueueId != 0 && "Not in queue!");
1097 Queue.erase_one(SU);
Roman Levenstein6b371142008-04-29 09:07:59 +00001098 SU->NodeQueueId = 0;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001099 }
Dan Gohman3f656df2008-11-20 02:45:51 +00001100
1101 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1102 scheduleDAG = scheduleDag;
1103 }
1104
1105 protected:
1106 bool canClobber(const SUnit *SU, const SUnit *Op);
1107 void AddPseudoTwoAddrDeps();
Dan Gohman9a658d72009-03-24 00:49:12 +00001108 void PrescheduleNodesWithMultipleUses();
Evan Cheng6730f032007-01-08 23:55:53 +00001109 void CalculateSethiUllmanNumbers();
Evan Cheng7e4abde2008-07-02 09:23:51 +00001110 };
1111
Dan Gohman186f65d2008-11-20 03:30:37 +00001112 typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1113 BURegReductionPriorityQueue;
Evan Cheng7e4abde2008-07-02 09:23:51 +00001114
Dan Gohman186f65d2008-11-20 03:30:37 +00001115 typedef RegReductionPriorityQueue<td_ls_rr_sort>
1116 TDRegReductionPriorityQueue;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001117
1118 typedef RegReductionPriorityQueue<src_ls_rr_sort>
1119 SrcRegReductionPriorityQueue;
Evan Chengd38c22b2006-05-11 23:55:42 +00001120}
1121
Evan Chengb9e3db62007-03-14 22:43:40 +00001122/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00001123/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00001124static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001125 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00001126 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00001127 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00001128 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001129 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00001130 // If there are bunch of CopyToRegs stacked up, they should be considered
1131 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00001132 if (I->getSUnit()->getNode() &&
1133 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001134 Height = closestSucc(I->getSUnit())+1;
1135 if (Height > MaxHeight)
1136 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00001137 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001138 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00001139}
1140
Evan Cheng61bc51e2007-12-20 02:22:36 +00001141/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00001142/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00001143static unsigned calcMaxScratches(const SUnit *SU) {
1144 unsigned Scratches = 0;
1145 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00001146 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001147 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00001148 Scratches++;
1149 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00001150 return Scratches;
1151}
1152
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001153template <typename RRSort>
1154static bool BURRSort(const SUnit *left, const SUnit *right,
1155 const RegReductionPriorityQueue<RRSort> *SPQ) {
Evan Cheng6730f032007-01-08 23:55:53 +00001156 unsigned LPriority = SPQ->getNodePriority(left);
1157 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng73bdf042008-03-01 00:39:47 +00001158 if (LPriority != RPriority)
1159 return LPriority > RPriority;
1160
1161 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
1162 // e.g.
1163 // t1 = op t2, c1
1164 // t3 = op t4, c2
1165 //
1166 // and the following instructions are both ready.
1167 // t2 = op c3
1168 // t4 = op c4
1169 //
1170 // Then schedule t2 = op first.
1171 // i.e.
1172 // t4 = op c4
1173 // t2 = op c3
1174 // t1 = op t2, c1
1175 // t3 = op t4, c2
1176 //
1177 // This creates more short live intervals.
1178 unsigned LDist = closestSucc(left);
1179 unsigned RDist = closestSucc(right);
1180 if (LDist != RDist)
1181 return LDist < RDist;
1182
Evan Cheng3a14efa2009-02-12 08:59:45 +00001183 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00001184 unsigned LScratch = calcMaxScratches(left);
1185 unsigned RScratch = calcMaxScratches(right);
1186 if (LScratch != RScratch)
1187 return LScratch > RScratch;
1188
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001189 if (left->getHeight() != right->getHeight())
1190 return left->getHeight() > right->getHeight();
Evan Cheng73bdf042008-03-01 00:39:47 +00001191
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001192 if (left->getDepth() != right->getDepth())
1193 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001194
Roman Levenstein6b371142008-04-29 09:07:59 +00001195 assert(left->NodeQueueId && right->NodeQueueId &&
1196 "NodeQueueId cannot be zero");
1197 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001198}
1199
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001200// Bottom up
1201bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
1202 return BURRSort(left, right, SPQ);
1203}
1204
1205// Source order, otherwise bottom up.
1206bool src_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const{
1207 unsigned LOrder = SPQ->getNodeOrdering(left);
1208 unsigned ROrder = SPQ->getNodeOrdering(right);
1209
1210 // Prefer an ordering where the lower the non-zero order number, the higher
1211 // the preference.
1212 if ((LOrder || ROrder) && LOrder != ROrder)
1213 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
1214
1215 return BURRSort(left, right, SPQ);
1216}
1217
Dan Gohman3f656df2008-11-20 02:45:51 +00001218template<class SF>
Evan Cheng7e4abde2008-07-02 09:23:51 +00001219bool
Dan Gohman3f656df2008-11-20 02:45:51 +00001220RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001221 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001222 unsigned Opc = SU->getNode()->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001223 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001224 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001225 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001226 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001227 if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001228 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00001229 if (DU->getNodeId() != -1 &&
1230 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001231 return true;
1232 }
1233 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001234 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001235 return false;
1236}
1237
Evan Chenga5e595d2007-09-28 22:32:30 +00001238/// hasCopyToRegUse - Return true if SU has a value successor that is a
1239/// CopyToReg node.
Dan Gohmane955c482008-08-05 14:45:15 +00001240static bool hasCopyToRegUse(const SUnit *SU) {
Evan Chenga5e595d2007-09-28 22:32:30 +00001241 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1242 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001243 if (I->isCtrl()) continue;
1244 const SUnit *SuccSU = I->getSUnit();
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001245 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg)
Evan Chenga5e595d2007-09-28 22:32:30 +00001246 return true;
1247 }
1248 return false;
1249}
1250
Evan Chengf9891412007-12-20 09:25:31 +00001251/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00001252/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00001253static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00001254 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001255 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001256 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00001257 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1258 const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00001259 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00001260 for (const SDNode *SUNode = SU->getNode(); SUNode;
1261 SUNode = SUNode->getFlaggedNode()) {
1262 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00001263 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00001264 const unsigned *SUImpDefs =
1265 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
1266 if (!SUImpDefs)
1267 return false;
1268 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001269 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00001270 if (VT == MVT::Flag || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00001271 continue;
1272 if (!N->hasAnyUseOfValue(i))
1273 continue;
1274 unsigned Reg = ImpDefs[i - NumDefs];
1275 for (;*SUImpDefs; ++SUImpDefs) {
1276 unsigned SUReg = *SUImpDefs;
1277 if (TRI->regsOverlap(Reg, SUReg))
1278 return true;
1279 }
Evan Chengf9891412007-12-20 09:25:31 +00001280 }
1281 }
1282 return false;
1283}
1284
Dan Gohman9a658d72009-03-24 00:49:12 +00001285/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
1286/// are not handled well by the general register pressure reduction
1287/// heuristics. When presented with code like this:
1288///
1289/// N
1290/// / |
1291/// / |
1292/// U store
1293/// |
1294/// ...
1295///
1296/// the heuristics tend to push the store up, but since the
1297/// operand of the store has another use (U), this would increase
1298/// the length of that other use (the U->N edge).
1299///
1300/// This function transforms code like the above to route U's
1301/// dependence through the store when possible, like this:
1302///
1303/// N
1304/// ||
1305/// ||
1306/// store
1307/// |
1308/// U
1309/// |
1310/// ...
1311///
1312/// This results in the store being scheduled immediately
1313/// after N, which shortens the U->N live range, reducing
1314/// register pressure.
1315///
1316template<class SF>
1317void RegReductionPriorityQueue<SF>::PrescheduleNodesWithMultipleUses() {
1318 // Visit all the nodes in topological order, working top-down.
1319 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
1320 SUnit *SU = &(*SUnits)[i];
1321 // For now, only look at nodes with no data successors, such as stores.
1322 // These are especially important, due to the heuristics in
1323 // getNodePriority for nodes with no data successors.
1324 if (SU->NumSuccs != 0)
1325 continue;
1326 // For now, only look at nodes with exactly one data predecessor.
1327 if (SU->NumPreds != 1)
1328 continue;
1329 // Avoid prescheduling copies to virtual registers, which don't behave
1330 // like other nodes from the perspective of scheduling heuristics.
1331 if (SDNode *N = SU->getNode())
1332 if (N->getOpcode() == ISD::CopyToReg &&
1333 TargetRegisterInfo::isVirtualRegister
1334 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1335 continue;
1336
1337 // Locate the single data predecessor.
1338 SUnit *PredSU = 0;
1339 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
1340 EE = SU->Preds.end(); II != EE; ++II)
1341 if (!II->isCtrl()) {
1342 PredSU = II->getSUnit();
1343 break;
1344 }
1345 assert(PredSU);
1346
1347 // Don't rewrite edges that carry physregs, because that requires additional
1348 // support infrastructure.
1349 if (PredSU->hasPhysRegDefs)
1350 continue;
1351 // Short-circuit the case where SU is PredSU's only data successor.
1352 if (PredSU->NumSuccs == 1)
1353 continue;
1354 // Avoid prescheduling to copies from virtual registers, which don't behave
1355 // like other nodes from the perspective of scheduling // heuristics.
1356 if (SDNode *N = SU->getNode())
1357 if (N->getOpcode() == ISD::CopyFromReg &&
1358 TargetRegisterInfo::isVirtualRegister
1359 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
1360 continue;
1361
1362 // Perform checks on the successors of PredSU.
1363 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
1364 EE = PredSU->Succs.end(); II != EE; ++II) {
1365 SUnit *PredSuccSU = II->getSUnit();
1366 if (PredSuccSU == SU) continue;
1367 // If PredSU has another successor with no data successors, for
1368 // now don't attempt to choose either over the other.
1369 if (PredSuccSU->NumSuccs == 0)
1370 goto outer_loop_continue;
1371 // Don't break physical register dependencies.
1372 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
1373 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
1374 goto outer_loop_continue;
1375 // Don't introduce graph cycles.
1376 if (scheduleDAG->IsReachable(SU, PredSuccSU))
1377 goto outer_loop_continue;
1378 }
1379
1380 // Ok, the transformation is safe and the heuristics suggest it is
1381 // profitable. Update the graph.
David Greenef34d7ac2010-01-05 01:24:54 +00001382 DEBUG(dbgs() << "Prescheduling SU # " << SU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001383 << " next to PredSU # " << PredSU->NodeNum
1384 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00001385 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
1386 SDep Edge = PredSU->Succs[i];
1387 assert(!Edge.isAssignedRegDep());
1388 SUnit *SuccSU = Edge.getSUnit();
1389 if (SuccSU != SU) {
1390 Edge.setSUnit(PredSU);
1391 scheduleDAG->RemovePred(SuccSU, Edge);
1392 scheduleDAG->AddPred(SU, Edge);
1393 Edge.setSUnit(SU);
1394 scheduleDAG->AddPred(SuccSU, Edge);
1395 --i;
1396 }
1397 }
1398 outer_loop_continue:;
1399 }
1400}
1401
Evan Chengd38c22b2006-05-11 23:55:42 +00001402/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
1403/// it as a def&use operand. Add a pseudo control edge from it to the other
1404/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00001405/// first (lower in the schedule). If both nodes are two-address, favor the
1406/// one that has a CopyToReg use (more likely to be a loop induction update).
1407/// If both are two-address, but one is commutable while the other is not
1408/// commutable, favor the one that's not commutable.
Dan Gohman3f656df2008-11-20 02:45:51 +00001409template<class SF>
1410void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001411 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00001412 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001413 if (!SU->isTwoAddress)
1414 continue;
1415
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001416 SDNode *Node = SU->getNode();
Dan Gohman072734e2008-11-13 23:24:17 +00001417 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001418 continue;
1419
Dan Gohman17059682008-07-17 19:10:17 +00001420 unsigned Opc = Node->getMachineOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00001421 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattnerfd2e3382008-01-07 06:47:00 +00001422 unsigned NumRes = TID.getNumDefs();
Dan Gohman0340d1e2008-02-15 20:50:13 +00001423 unsigned NumOps = TID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001424 for (unsigned j = 0; j != NumOps; ++j) {
Dan Gohman82016c22008-11-19 02:00:32 +00001425 if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1)
1426 continue;
1427 SDNode *DU = SU->getNode()->getOperand(j).getNode();
1428 if (DU->getNodeId() == -1)
1429 continue;
1430 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
1431 if (!DUSU) continue;
1432 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
1433 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001434 if (I->isCtrl()) continue;
1435 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00001436 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00001437 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001438 // Be conservative. Ignore if nodes aren't at roughly the same
1439 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001440 if (SuccSU->getHeight() < SU->getHeight() &&
1441 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00001442 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001443 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
1444 // constrains whatever is using the copy, instead of the copy
1445 // itself. In the case that the copy is coalesced, this
1446 // preserves the intent of the pseudo two-address heurietics.
1447 while (SuccSU->Succs.size() == 1 &&
1448 SuccSU->getNode()->isMachineOpcode() &&
1449 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00001450 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00001451 SuccSU = SuccSU->Succs.front().getSUnit();
1452 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00001453 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
1454 continue;
1455 // Don't constrain nodes with physical register defs if the
1456 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00001457 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00001458 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00001459 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00001460 }
Dan Gohman3027bb62009-04-16 20:57:10 +00001461 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
1462 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00001463 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00001464 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
1465 SuccOpc == TargetOpcode::INSERT_SUBREG ||
1466 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00001467 continue;
1468 if ((!canClobber(SuccSU, DUSU) ||
1469 (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||
1470 (!SU->isCommutable && SuccSU->isCommutable)) &&
1471 !scheduleDAG->IsReachable(SuccSU, SU)) {
David Greenef34d7ac2010-01-05 01:24:54 +00001472 DEBUG(dbgs() << "Adding a pseudo-two-addr edge from SU # "
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001473 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Dan Gohman79c35162009-01-06 01:19:04 +00001474 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0,
Dan Gohmanbf8e5202009-01-06 01:28:56 +00001475 /*Reg=*/0, /*isNormalMemory=*/false,
1476 /*isMustAlias=*/false,
Dan Gohman2d170892008-12-09 22:54:47 +00001477 /*isArtificial=*/true));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00001478 }
1479 }
1480 }
1481 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001482}
1483
Evan Cheng6730f032007-01-08 23:55:53 +00001484/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1485/// scheduling units.
Dan Gohman186f65d2008-11-20 03:30:37 +00001486template<class SF>
1487void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() {
Evan Chengd38c22b2006-05-11 23:55:42 +00001488 SethiUllmanNumbers.assign(SUnits->size(), 0);
1489
1490 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
Dan Gohman186f65d2008-11-20 03:30:37 +00001491 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001492}
Evan Chengd38c22b2006-05-11 23:55:42 +00001493
Roman Levenstein30d09512008-03-27 09:44:37 +00001494/// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled
Roman Levensteinbc674502008-03-27 09:14:57 +00001495/// predecessors of the successors of the SUnit SU. Stop when the provided
1496/// limit is exceeded.
Roman Levensteinbc674502008-03-27 09:14:57 +00001497static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU,
1498 unsigned Limit) {
1499 unsigned Sum = 0;
1500 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1501 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001502 const SUnit *SuccSU = I->getSUnit();
Roman Levensteinbc674502008-03-27 09:14:57 +00001503 for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(),
1504 EE = SuccSU->Preds.end(); II != EE; ++II) {
Dan Gohman2d170892008-12-09 22:54:47 +00001505 SUnit *PredSU = II->getSUnit();
Evan Cheng16d72072008-03-29 18:34:22 +00001506 if (!PredSU->isScheduled)
1507 if (++Sum > Limit)
1508 return Sum;
Roman Levensteinbc674502008-03-27 09:14:57 +00001509 }
1510 }
1511 return Sum;
1512}
1513
Evan Chengd38c22b2006-05-11 23:55:42 +00001514
1515// Top down
1516bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
Evan Cheng6730f032007-01-08 23:55:53 +00001517 unsigned LPriority = SPQ->getNodePriority(left);
1518 unsigned RPriority = SPQ->getNodePriority(right);
Dan Gohman1ddfcba2008-11-13 21:36:12 +00001519 bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode();
1520 bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode();
Evan Chengd38c22b2006-05-11 23:55:42 +00001521 bool LIsFloater = LIsTarget && left->NumPreds == 0;
1522 bool RIsFloater = RIsTarget && right->NumPreds == 0;
Roman Levensteinbc674502008-03-27 09:14:57 +00001523 unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0;
1524 unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0;
Evan Chengd38c22b2006-05-11 23:55:42 +00001525
1526 if (left->NumSuccs == 0 && right->NumSuccs != 0)
1527 return false;
1528 else if (left->NumSuccs != 0 && right->NumSuccs == 0)
1529 return true;
1530
Evan Chengd38c22b2006-05-11 23:55:42 +00001531 if (LIsFloater)
1532 LBonus -= 2;
1533 if (RIsFloater)
1534 RBonus -= 2;
1535 if (left->NumSuccs == 1)
1536 LBonus += 2;
1537 if (right->NumSuccs == 1)
1538 RBonus += 2;
1539
Evan Cheng73bdf042008-03-01 00:39:47 +00001540 if (LPriority+LBonus != RPriority+RBonus)
1541 return LPriority+LBonus < RPriority+RBonus;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001542
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001543 if (left->getDepth() != right->getDepth())
1544 return left->getDepth() < right->getDepth();
Evan Cheng73bdf042008-03-01 00:39:47 +00001545
1546 if (left->NumSuccsLeft != right->NumSuccsLeft)
1547 return left->NumSuccsLeft > right->NumSuccsLeft;
1548
Roman Levenstein6b371142008-04-29 09:07:59 +00001549 assert(left->NodeQueueId && right->NodeQueueId &&
1550 "NodeQueueId cannot be zero");
1551 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00001552}
1553
Evan Chengd38c22b2006-05-11 23:55:42 +00001554//===----------------------------------------------------------------------===//
1555// Public Constructor Functions
1556//===----------------------------------------------------------------------===//
1557
Dan Gohmandfaf6462009-02-11 04:27:20 +00001558llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001559llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001560 const TargetMachine &TM = IS->TM;
1561 const TargetInstrInfo *TII = TM.getInstrInfo();
1562 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001563
Evan Cheng7e4abde2008-07-02 09:23:51 +00001564 BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001565
Evan Cheng7e4abde2008-07-02 09:23:51 +00001566 ScheduleDAGRRList *SD =
Dan Gohman619ef482009-01-15 19:20:50 +00001567 new ScheduleDAGRRList(*IS->MF, true, PQ);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001568 PQ->setScheduleDAG(SD);
1569 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001570}
1571
Dan Gohmandfaf6462009-02-11 04:27:20 +00001572llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +00001573llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +00001574 const TargetMachine &TM = IS->TM;
1575 const TargetInstrInfo *TII = TM.getInstrInfo();
1576 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Dan Gohman3f656df2008-11-20 02:45:51 +00001577
1578 TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI);
1579
Dan Gohman619ef482009-01-15 19:20:50 +00001580 ScheduleDAGRRList *SD =
1581 new ScheduleDAGRRList(*IS->MF, false, PQ);
Dan Gohman3f656df2008-11-20 02:45:51 +00001582 PQ->setScheduleDAG(SD);
1583 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00001584}
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001585
1586llvm::ScheduleDAGSDNodes *
1587llvm::createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
1588 const TargetMachine &TM = IS->TM;
1589 const TargetInstrInfo *TII = TM.getInstrInfo();
1590 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
1591
1592 SrcRegReductionPriorityQueue *PQ = new SrcRegReductionPriorityQueue(TII, TRI);
1593
1594 ScheduleDAGRRList *SD =
1595 new ScheduleDAGRRList(*IS->MF, true, PQ);
1596 PQ->setScheduleDAG(SD);
1597 return SD;
1598}