blob: 10d1adf799c26694bd04425baf02db91f961a7f0 [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"
Jim Laskey29e635d2006-08-02 12:30:23 +000019#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "ScheduleDAGSDNodes.h"
21#include "llvm/ADT/STLExtras.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000022#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/ADT/Statistic.h"
Weiming Zhao4a0b4fb2013-01-29 21:18:43 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/InlineAsm.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000031#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetLowering.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000036#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000037using namespace llvm;
38
Dan Gohmanfd227e92008-03-25 17:10:29 +000039STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000040STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000042STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000043
Jim Laskey95eda5b2006-08-01 14:21:23 +000044static RegisterScheduler
45 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000046 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000047 createBURRListDAGScheduler);
48static RegisterScheduler
Bill Wendling8cbc25d2010-01-23 10:26:57 +000049 sourceListDAGScheduler("source",
50 "Similar to list-burr but schedules in source "
51 "order when possible",
52 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000053
Evan Chengbdd062d2010-05-20 06:13:19 +000054static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000055 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000056 "Bottom-up register pressure aware list scheduling "
57 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000058 createHybridListDAGScheduler);
59
Evan Cheng37b740c2010-07-24 00:39:05 +000060static RegisterScheduler
61 ILPListDAGScheduler("list-ilp",
62 "Bottom-up register pressure aware list scheduling "
63 "which tries to balance ILP and register pressure",
64 createILPListDAGScheduler);
65
Andrew Trick47ff14b2011-01-21 05:51:33 +000066static cl::opt<bool> DisableSchedCycles(
Andrew Trickbd428ec2011-01-21 06:19:05 +000067 "disable-sched-cycles", cl::Hidden, cl::init(false),
Andrew Trick47ff14b2011-01-21 05:51:33 +000068 cl::desc("Disable cycle-level precision during preRA scheduling"));
Andrew Trick10ffc2b2010-12-24 05:03:26 +000069
Andrew Trick641e2d42011-03-05 08:00:22 +000070// Temporary sched=list-ilp flags until the heuristics are robust.
Andrew Trickbfbd9722011-04-14 05:15:06 +000071// Some options are also available under sched=list-hybrid.
Andrew Trick641e2d42011-03-05 08:00:22 +000072static cl::opt<bool> DisableSchedRegPressure(
73 "disable-sched-reg-pressure", cl::Hidden, cl::init(false),
74 cl::desc("Disable regpressure priority in sched=list-ilp"));
75static cl::opt<bool> DisableSchedLiveUses(
Andrew Trickdd017322011-03-06 00:03:32 +000076 "disable-sched-live-uses", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000077 cl::desc("Disable live use priority in sched=list-ilp"));
Andrew Trick2ad0b372011-04-07 19:54:57 +000078static cl::opt<bool> DisableSchedVRegCycle(
79 "disable-sched-vrcycle", cl::Hidden, cl::init(false),
80 cl::desc("Disable virtual register cycle interference checks"));
Andrew Trickbfbd9722011-04-14 05:15:06 +000081static cl::opt<bool> DisableSchedPhysRegJoin(
82 "disable-sched-physreg-join", cl::Hidden, cl::init(false),
83 cl::desc("Disable physreg def-use affinity"));
Andrew Trick641e2d42011-03-05 08:00:22 +000084static cl::opt<bool> DisableSchedStalls(
Andrew Trickdd017322011-03-06 00:03:32 +000085 "disable-sched-stalls", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000086 cl::desc("Disable no-stall priority in sched=list-ilp"));
87static cl::opt<bool> DisableSchedCriticalPath(
88 "disable-sched-critical-path", cl::Hidden, cl::init(false),
89 cl::desc("Disable critical path priority in sched=list-ilp"));
90static cl::opt<bool> DisableSchedHeight(
91 "disable-sched-height", cl::Hidden, cl::init(false),
92 cl::desc("Disable scheduled-height priority in sched=list-ilp"));
Evan Chengd33b2d62011-11-10 07:43:16 +000093static cl::opt<bool> Disable2AddrHack(
94 "disable-2addr-hack", cl::Hidden, cl::init(true),
95 cl::desc("Disable scheduler's two-address hack"));
Andrew Trick641e2d42011-03-05 08:00:22 +000096
97static cl::opt<int> MaxReorderWindow(
98 "max-sched-reorder", cl::Hidden, cl::init(6),
99 cl::desc("Number of instructions to allow ahead of the critical path "
100 "in sched=list-ilp"));
101
102static cl::opt<unsigned> AvgIPC(
103 "sched-avg-ipc", cl::Hidden, cl::init(1),
104 cl::desc("Average inst/cycle whan no target itinerary exists."));
105
Evan Chengd38c22b2006-05-11 23:55:42 +0000106namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +0000107//===----------------------------------------------------------------------===//
108/// ScheduleDAGRRList - The actual register reduction list scheduler
109/// implementation. This supports both top-down and bottom-up scheduling.
110///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000111class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +0000112private:
Evan Chengbdd062d2010-05-20 06:13:19 +0000113 /// NeedLatency - True if the scheduler will make use of latency information.
114 ///
115 bool NeedLatency;
116
Evan Chengd38c22b2006-05-11 23:55:42 +0000117 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +0000118 SchedulingPriorityQueue *AvailableQueue;
119
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000120 /// PendingQueue - This contains all of the instructions whose operands have
121 /// been issued, but their results are not ready yet (due to the latency of
122 /// the operation). Once the operands becomes available, the instruction is
123 /// added to the AvailableQueue.
124 std::vector<SUnit*> PendingQueue;
125
126 /// HazardRec - The hazard recognizer to use.
127 ScheduleHazardRecognizer *HazardRec;
128
Andrew Trick528fad92010-12-23 05:42:20 +0000129 /// CurCycle - The current scheduler state corresponds to this cycle.
130 unsigned CurCycle;
131
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000132 /// MinAvailableCycle - Cycle of the soonest available instruction.
133 unsigned MinAvailableCycle;
134
Andrew Trick641e2d42011-03-05 08:00:22 +0000135 /// IssueCount - Count instructions issued in this cycle
136 /// Currently valid only for bottom-up scheduling.
137 unsigned IssueCount;
138
Dan Gohmanc07f6862008-09-23 18:50:48 +0000139 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +0000140 /// that are "live". These nodes must be scheduled before any other nodes that
141 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000142 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000143 std::vector<SUnit*> LiveRegDefs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000144 std::vector<SUnit*> LiveRegGens;
Evan Cheng5924bf72007-09-25 01:54:36 +0000145
Dan Gohmanad2134d2008-11-25 00:52:40 +0000146 /// Topo - A topological ordering for SUnits which permits fast IsReachable
147 /// and similar queries.
148 ScheduleDAGTopologicalSort Topo;
149
Eli Friedmand5c173f2011-12-07 22:24:28 +0000150 // Hack to keep track of the inverse of FindCallSeqStart without more crazy
151 // DAG crawling.
152 DenseMap<SUnit*, SUnit*> CallSeqEndForStart;
153
Evan Chengd38c22b2006-05-11 23:55:42 +0000154public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000155 ScheduleDAGRRList(MachineFunction &mf, bool needlatency,
156 SchedulingPriorityQueue *availqueue,
157 CodeGenOpt::Level OptLevel)
Dan Gohman90fb5522011-10-20 21:44:34 +0000158 : ScheduleDAGSDNodes(mf),
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000159 NeedLatency(needlatency), AvailableQueue(availqueue), CurCycle(0),
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000160 Topo(SUnits, NULL) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000161
162 const TargetMachine &tm = mf.getTarget();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000163 if (DisableSchedCycles || !NeedLatency)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000164 HazardRec = new ScheduleHazardRecognizer();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000165 else
166 HazardRec = tm.getInstrInfo()->CreateTargetHazardRecognizer(&tm, this);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000167 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000168
169 ~ScheduleDAGRRList() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000170 delete HazardRec;
Evan Chengd38c22b2006-05-11 23:55:42 +0000171 delete AvailableQueue;
172 }
173
174 void Schedule();
175
Andrew Trick9ccce772011-01-14 21:11:41 +0000176 ScheduleHazardRecognizer *getHazardRec() { return HazardRec; }
177
Roman Levenstein733a4d62008-03-26 11:23:38 +0000178 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000179 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
180 return Topo.IsReachable(SU, TargetSU);
181 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000182
Dan Gohman60d68442009-01-29 19:49:27 +0000183 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000184 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000185 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
186 return Topo.WillCreateCycle(SU, TargetSU);
187 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000188
Dan Gohman2d170892008-12-09 22:54:47 +0000189 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000190 /// This returns true if this is a new predecessor.
191 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000192 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000193 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000194 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000195 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000196
Dan Gohman2d170892008-12-09 22:54:47 +0000197 /// RemovePred - removes a predecessor edge from SUnit SU.
198 /// This returns true if an edge was removed.
199 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000200 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000201 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000202 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000203 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000204
Evan Chengd38c22b2006-05-11 23:55:42 +0000205private:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000206 bool isReady(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000207 return DisableSchedCycles || !AvailableQueue->hasReadyFilter() ||
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000208 AvailableQueue->isReady(SU);
209 }
210
Dan Gohman60d68442009-01-29 19:49:27 +0000211 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Andrew Tricka52f3252010-12-23 04:16:14 +0000212 void ReleasePredecessors(SUnit *SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000213 void ReleasePending();
214 void AdvanceToCycle(unsigned NextCycle);
215 void AdvancePastStalls(SUnit *SU);
216 void EmitNode(SUnit *SU);
Andrew Trick528fad92010-12-23 05:42:20 +0000217 void ScheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000218 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000219 void UnscheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000220 void RestoreHazardCheckerBottomUp();
221 void BacktrackBottomUp(SUnit*, SUnit*);
Evan Cheng8e136a92007-09-26 21:36:17 +0000222 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000223 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
224 const TargetRegisterClass*,
225 const TargetRegisterClass*,
226 SmallVector<SUnit*, 2>&);
Evan Cheng1ec79b42007-09-27 07:09:03 +0000227 bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000228
Andrew Trick528fad92010-12-23 05:42:20 +0000229 SUnit *PickNodeToScheduleBottomUp();
Evan Chengd38c22b2006-05-11 23:55:42 +0000230 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000231
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000232 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000233 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000234 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000235 unsigned NumSUnits = SUnits.size();
Andrew Trick52226d42012-03-07 23:00:49 +0000236 SUnit *NewNode = newSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000237 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000238 if (NewNode->NodeNum >= NumSUnits)
239 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000240 return NewNode;
241 }
242
Roman Levenstein733a4d62008-03-26 11:23:38 +0000243 /// CreateClone - Creates a new SUnit from an existing one.
244 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000245 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000246 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000247 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000248 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000249 if (NewNode->NodeNum >= NumSUnits)
250 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000251 return NewNode;
252 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000253
Andrew Trick52226d42012-03-07 23:00:49 +0000254 /// forceUnitLatencies - Register-pressure-reducing scheduling doesn't
Evan Chengbdd062d2010-05-20 06:13:19 +0000255 /// need actual latency information but the hybrid scheduler does.
Andrew Trick52226d42012-03-07 23:00:49 +0000256 bool forceUnitLatencies() const {
Evan Chengbdd062d2010-05-20 06:13:19 +0000257 return !NeedLatency;
258 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000259};
260} // end anonymous namespace
261
Owen Anderson96adc4a2011-06-15 23:35:18 +0000262/// GetCostForDef - Looks up the register class and cost for a given definition.
263/// Typically this just means looking up the representative register class,
Owen Andersonca2f78a2011-11-16 01:02:57 +0000264/// but for untyped values (MVT::Untyped) it means inspecting the node's
Owen Anderson96adc4a2011-06-15 23:35:18 +0000265/// opcode to determine what register class is being generated.
266static void GetCostForDef(const ScheduleDAGSDNodes::RegDefIter &RegDefPos,
267 const TargetLowering *TLI,
268 const TargetInstrInfo *TII,
269 const TargetRegisterInfo *TRI,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000270 unsigned &RegClass, unsigned &Cost,
271 const MachineFunction &MF) {
Patrik Hagglund05394352012-12-13 18:45:35 +0000272 MVT VT = RegDefPos.GetValue();
Owen Anderson96adc4a2011-06-15 23:35:18 +0000273
274 // Special handling for untyped values. These values can only come from
275 // the expansion of custom DAG-to-DAG patterns.
Owen Andersonca2f78a2011-11-16 01:02:57 +0000276 if (VT == MVT::Untyped) {
Owen Andersond1955e72011-06-21 22:54:23 +0000277 const SDNode *Node = RegDefPos.GetNode();
Owen Andersond1955e72011-06-21 22:54:23 +0000278
Weiming Zhao4a0b4fb2013-01-29 21:18:43 +0000279 // Special handling for CopyFromReg of untyped values.
280 if (!Node->isMachineOpcode() && Node->getOpcode() == ISD::CopyFromReg) {
281 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
282 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
283 RegClass = RC->getID();
284 Cost = 1;
285 return;
286 }
287
288 unsigned Opcode = Node->getMachineOpcode();
Owen Andersond1955e72011-06-21 22:54:23 +0000289 if (Opcode == TargetOpcode::REG_SEQUENCE) {
290 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
291 const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
292 RegClass = RC->getID();
293 Cost = 1;
294 return;
295 }
296
Owen Anderson96adc4a2011-06-15 23:35:18 +0000297 unsigned Idx = RegDefPos.GetIdx();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000298 const MCInstrDesc Desc = TII->get(Opcode);
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000299 const TargetRegisterClass *RC = TII->getRegClass(Desc, Idx, TRI, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +0000300 RegClass = RC->getID();
301 // FIXME: Cost arbitrarily set to 1 because there doesn't seem to be a
302 // better way to determine it.
303 Cost = 1;
304 } else {
305 RegClass = TLI->getRepRegClassFor(VT)->getID();
306 Cost = TLI->getRepRegClassCostFor(VT);
307 }
308}
Evan Chengd38c22b2006-05-11 23:55:42 +0000309
310/// Schedule - Schedule the DAG using list scheduling.
311void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000312 DEBUG(dbgs()
313 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000314 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000315
Andrew Trick528fad92010-12-23 05:42:20 +0000316 CurCycle = 0;
Andrew Trick641e2d42011-03-05 08:00:22 +0000317 IssueCount = 0;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000318 MinAvailableCycle = DisableSchedCycles ? 0 : UINT_MAX;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000319 NumLiveRegs = 0;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000320 // Allocate slots for each physical register, plus one for a special register
321 // to track the virtual resource of a calling sequence.
322 LiveRegDefs.resize(TRI->getNumRegs() + 1, NULL);
323 LiveRegGens.resize(TRI->getNumRegs() + 1, NULL);
Eli Friedmand5c173f2011-12-07 22:24:28 +0000324 CallSeqEndForStart.clear();
Evan Cheng5924bf72007-09-25 01:54:36 +0000325
Dan Gohman04543e72008-12-23 18:36:58 +0000326 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +0000327 BuildSchedGraph(NULL);
Evan Chengd38c22b2006-05-11 23:55:42 +0000328
Evan Chengd38c22b2006-05-11 23:55:42 +0000329 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000330 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000331 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000332
Dan Gohman46520a22008-06-21 19:18:17 +0000333 AvailableQueue->initNodes(SUnits);
Andrew Trick2085a962010-12-21 22:25:04 +0000334
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000335 HazardRec->Reset();
336
Dan Gohman90fb5522011-10-20 21:44:34 +0000337 // Execute the actual scheduling loop.
338 ListScheduleBottomUp();
Andrew Trick2085a962010-12-21 22:25:04 +0000339
Evan Chengd38c22b2006-05-11 23:55:42 +0000340 AvailableQueue->releaseState();
Andrew Trickedee68c2012-03-07 05:21:40 +0000341
342 DEBUG({
343 dbgs() << "*** Final schedule ***\n";
344 dumpSchedule();
345 dbgs() << '\n';
346 });
Evan Chengafed73e2006-05-12 01:58:24 +0000347}
Evan Chengd38c22b2006-05-11 23:55:42 +0000348
349//===----------------------------------------------------------------------===//
350// Bottom-Up Scheduling
351//===----------------------------------------------------------------------===//
352
Evan Chengd38c22b2006-05-11 23:55:42 +0000353/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000354/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000355void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000356 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000357
Evan Chengd38c22b2006-05-11 23:55:42 +0000358#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000359 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000360 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000361 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000362 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000363 llvm_unreachable(0);
Evan Chengd38c22b2006-05-11 23:55:42 +0000364 }
365#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000366 --PredSU->NumSuccsLeft;
367
Andrew Trick52226d42012-03-07 23:00:49 +0000368 if (!forceUnitLatencies()) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000369 // Updating predecessor's height. This is now the cycle when the
370 // predecessor can be scheduled without causing a pipeline stall.
371 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
372 }
373
Dan Gohmanb9543432009-02-10 23:27:53 +0000374 // If all the node's successors are scheduled, this node is ready
375 // to be scheduled. Ignore the special EntrySU node.
376 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000377 PredSU->isAvailable = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000378
379 unsigned Height = PredSU->getHeight();
380 if (Height < MinAvailableCycle)
381 MinAvailableCycle = Height;
382
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000383 if (isReady(PredSU)) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000384 AvailableQueue->push(PredSU);
385 }
386 // CapturePred and others may have left the node in the pending queue, avoid
387 // adding it twice.
388 else if (!PredSU->isPending) {
389 PredSU->isPending = true;
390 PendingQueue.push_back(PredSU);
391 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000392 }
393}
394
Dan Gohman198b7ff2011-11-03 21:49:52 +0000395/// IsChainDependent - Test if Outer is reachable from Inner through
396/// chain dependencies.
397static bool IsChainDependent(SDNode *Outer, SDNode *Inner,
398 unsigned NestLevel,
399 const TargetInstrInfo *TII) {
400 SDNode *N = Outer;
401 for (;;) {
402 if (N == Inner)
403 return true;
404 // For a TokenFactor, examine each operand. There may be multiple ways
405 // to get to the CALLSEQ_BEGIN, but we need to find the path with the
406 // most nesting in order to ensure that we find the corresponding match.
407 if (N->getOpcode() == ISD::TokenFactor) {
408 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
409 if (IsChainDependent(N->getOperand(i).getNode(), Inner, NestLevel, TII))
410 return true;
411 return false;
412 }
413 // Check for a lowered CALLSEQ_BEGIN or CALLSEQ_END.
414 if (N->isMachineOpcode()) {
415 if (N->getMachineOpcode() ==
416 (unsigned)TII->getCallFrameDestroyOpcode()) {
417 ++NestLevel;
418 } else if (N->getMachineOpcode() ==
419 (unsigned)TII->getCallFrameSetupOpcode()) {
420 if (NestLevel == 0)
421 return false;
422 --NestLevel;
423 }
424 }
425 // Otherwise, find the chain and continue climbing.
426 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
427 if (N->getOperand(i).getValueType() == MVT::Other) {
428 N = N->getOperand(i).getNode();
429 goto found_chain_operand;
430 }
431 return false;
432 found_chain_operand:;
433 if (N->getOpcode() == ISD::EntryToken)
434 return false;
435 }
436}
437
438/// FindCallSeqStart - Starting from the (lowered) CALLSEQ_END node, locate
439/// the corresponding (lowered) CALLSEQ_BEGIN node.
440///
441/// NestLevel and MaxNested are used in recursion to indcate the current level
442/// of nesting of CALLSEQ_BEGIN and CALLSEQ_END pairs, as well as the maximum
443/// level seen so far.
444///
445/// TODO: It would be better to give CALLSEQ_END an explicit operand to point
446/// to the corresponding CALLSEQ_BEGIN to avoid needing to search for it.
447static SDNode *
448FindCallSeqStart(SDNode *N, unsigned &NestLevel, unsigned &MaxNest,
449 const TargetInstrInfo *TII) {
450 for (;;) {
451 // For a TokenFactor, examine each operand. There may be multiple ways
452 // to get to the CALLSEQ_BEGIN, but we need to find the path with the
453 // most nesting in order to ensure that we find the corresponding match.
454 if (N->getOpcode() == ISD::TokenFactor) {
455 SDNode *Best = 0;
456 unsigned BestMaxNest = MaxNest;
457 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
458 unsigned MyNestLevel = NestLevel;
459 unsigned MyMaxNest = MaxNest;
460 if (SDNode *New = FindCallSeqStart(N->getOperand(i).getNode(),
461 MyNestLevel, MyMaxNest, TII))
462 if (!Best || (MyMaxNest > BestMaxNest)) {
463 Best = New;
464 BestMaxNest = MyMaxNest;
465 }
466 }
467 assert(Best);
468 MaxNest = BestMaxNest;
469 return Best;
470 }
471 // Check for a lowered CALLSEQ_BEGIN or CALLSEQ_END.
472 if (N->isMachineOpcode()) {
473 if (N->getMachineOpcode() ==
474 (unsigned)TII->getCallFrameDestroyOpcode()) {
475 ++NestLevel;
476 MaxNest = std::max(MaxNest, NestLevel);
477 } else if (N->getMachineOpcode() ==
478 (unsigned)TII->getCallFrameSetupOpcode()) {
479 assert(NestLevel != 0);
480 --NestLevel;
481 if (NestLevel == 0)
482 return N;
483 }
484 }
485 // Otherwise, find the chain and continue climbing.
486 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
487 if (N->getOperand(i).getValueType() == MVT::Other) {
488 N = N->getOperand(i).getNode();
489 goto found_chain_operand;
490 }
491 return 0;
492 found_chain_operand:;
493 if (N->getOpcode() == ISD::EntryToken)
494 return 0;
495 }
496}
497
Andrew Trick033efdf2010-12-23 03:15:51 +0000498/// Call ReleasePred for each predecessor, then update register live def/gen.
499/// Always update LiveRegDefs for a register dependence even if the current SU
500/// also defines the register. This effectively create one large live range
501/// across a sequence of two-address node. This is important because the
502/// entire chain must be scheduled together. Example:
503///
504/// flags = (3) add
505/// flags = (2) addc flags
506/// flags = (1) addc flags
507///
508/// results in
509///
510/// LiveRegDefs[flags] = 3
Andrew Tricka52f3252010-12-23 04:16:14 +0000511/// LiveRegGens[flags] = 1
Andrew Trick033efdf2010-12-23 03:15:51 +0000512///
513/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
514/// interference on flags.
Andrew Tricka52f3252010-12-23 04:16:14 +0000515void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000516 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000517 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000518 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000519 ReleasePred(SU, &*I);
520 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000521 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000522 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000523 // clobber the register is scheduled between the predecessor and
524 // this node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000525 SUnit *RegDef = LiveRegDefs[I->getReg()]; (void)RegDef;
Andrew Trick033efdf2010-12-23 03:15:51 +0000526 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
527 "interference on register dependence");
Andrew Tricka52f3252010-12-23 04:16:14 +0000528 LiveRegDefs[I->getReg()] = I->getSUnit();
529 if (!LiveRegGens[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000530 ++NumLiveRegs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000531 LiveRegGens[I->getReg()] = SU;
Evan Cheng5924bf72007-09-25 01:54:36 +0000532 }
533 }
534 }
Dan Gohman198b7ff2011-11-03 21:49:52 +0000535
536 // If we're scheduling a lowered CALLSEQ_END, find the corresponding
537 // CALLSEQ_BEGIN. Inject an artificial physical register dependence between
538 // these nodes, to prevent other calls from being interscheduled with them.
539 unsigned CallResource = TRI->getNumRegs();
540 if (!LiveRegDefs[CallResource])
541 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode())
542 if (Node->isMachineOpcode() &&
543 Node->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
544 unsigned NestLevel = 0;
545 unsigned MaxNest = 0;
546 SDNode *N = FindCallSeqStart(Node, NestLevel, MaxNest, TII);
547
548 SUnit *Def = &SUnits[N->getNodeId()];
Eli Friedmand5c173f2011-12-07 22:24:28 +0000549 CallSeqEndForStart[Def] = SU;
550
Dan Gohman198b7ff2011-11-03 21:49:52 +0000551 ++NumLiveRegs;
552 LiveRegDefs[CallResource] = Def;
553 LiveRegGens[CallResource] = SU;
554 break;
555 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000556}
557
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000558/// Check to see if any of the pending instructions are ready to issue. If
559/// so, add them to the available queue.
560void ScheduleDAGRRList::ReleasePending() {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000561 if (DisableSchedCycles) {
Andrew Trick5ce945c2010-12-24 07:10:19 +0000562 assert(PendingQueue.empty() && "pending instrs not allowed in this mode");
563 return;
564 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000565
566 // If the available queue is empty, it is safe to reset MinAvailableCycle.
567 if (AvailableQueue->empty())
568 MinAvailableCycle = UINT_MAX;
569
570 // Check to see if any of the pending instructions are ready to issue. If
571 // so, add them to the available queue.
572 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman90fb5522011-10-20 21:44:34 +0000573 unsigned ReadyCycle = PendingQueue[i]->getHeight();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000574 if (ReadyCycle < MinAvailableCycle)
575 MinAvailableCycle = ReadyCycle;
576
577 if (PendingQueue[i]->isAvailable) {
578 if (!isReady(PendingQueue[i]))
579 continue;
580 AvailableQueue->push(PendingQueue[i]);
581 }
582 PendingQueue[i]->isPending = false;
583 PendingQueue[i] = PendingQueue.back();
584 PendingQueue.pop_back();
585 --i; --e;
586 }
587}
588
589/// Move the scheduler state forward by the specified number of Cycles.
590void ScheduleDAGRRList::AdvanceToCycle(unsigned NextCycle) {
591 if (NextCycle <= CurCycle)
592 return;
593
Andrew Trick641e2d42011-03-05 08:00:22 +0000594 IssueCount = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000595 AvailableQueue->setCurCycle(NextCycle);
Andrew Trick47ff14b2011-01-21 05:51:33 +0000596 if (!HazardRec->isEnabled()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000597 // Bypass lots of virtual calls in case of long latency.
598 CurCycle = NextCycle;
599 }
600 else {
601 for (; CurCycle != NextCycle; ++CurCycle) {
Dan Gohman90fb5522011-10-20 21:44:34 +0000602 HazardRec->RecedeCycle();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000603 }
604 }
605 // FIXME: Instead of visiting the pending Q each time, set a dirty flag on the
606 // available Q to release pending nodes at least once before popping.
607 ReleasePending();
608}
609
610/// Move the scheduler state forward until the specified node's dependents are
611/// ready and can be scheduled with no resource conflicts.
612void ScheduleDAGRRList::AdvancePastStalls(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000613 if (DisableSchedCycles)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000614 return;
615
Andrew Trickb53a00d2011-04-13 00:38:32 +0000616 // FIXME: Nodes such as CopyFromReg probably should not advance the current
617 // cycle. Otherwise, we can wrongly mask real stalls. If the non-machine node
618 // has predecessors the cycle will be advanced when they are scheduled.
619 // But given the crude nature of modeling latency though such nodes, we
620 // currently need to treat these nodes like real instructions.
621 // if (!SU->getNode() || !SU->getNode()->isMachineOpcode()) return;
622
Dan Gohman90fb5522011-10-20 21:44:34 +0000623 unsigned ReadyCycle = SU->getHeight();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000624
625 // Bump CurCycle to account for latency. We assume the latency of other
626 // available instructions may be hidden by the stall (not a full pipe stall).
627 // This updates the hazard recognizer's cycle before reserving resources for
628 // this instruction.
629 AdvanceToCycle(ReadyCycle);
630
631 // Calls are scheduled in their preceding cycle, so don't conflict with
632 // hazards from instructions after the call. EmitNode will reset the
633 // scoreboard state before emitting the call.
Dan Gohman90fb5522011-10-20 21:44:34 +0000634 if (SU->isCall)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000635 return;
636
637 // FIXME: For resource conflicts in very long non-pipelined stages, we
638 // should probably skip ahead here to avoid useless scoreboard checks.
639 int Stalls = 0;
640 while (true) {
641 ScheduleHazardRecognizer::HazardType HT =
Dan Gohman90fb5522011-10-20 21:44:34 +0000642 HazardRec->getHazardType(SU, -Stalls);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000643
644 if (HT == ScheduleHazardRecognizer::NoHazard)
645 break;
646
647 ++Stalls;
648 }
649 AdvanceToCycle(CurCycle + Stalls);
650}
651
652/// Record this SUnit in the HazardRecognizer.
653/// Does not update CurCycle.
654void ScheduleDAGRRList::EmitNode(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000655 if (!HazardRec->isEnabled())
Andrew Trickc9405662010-12-24 06:46:50 +0000656 return;
657
658 // Check for phys reg copy.
659 if (!SU->getNode())
660 return;
661
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000662 switch (SU->getNode()->getOpcode()) {
663 default:
664 assert(SU->getNode()->isMachineOpcode() &&
665 "This target-independent node should not be scheduled.");
666 break;
667 case ISD::MERGE_VALUES:
668 case ISD::TokenFactor:
Nadav Rotem7c277da2012-09-06 09:17:37 +0000669 case ISD::LIFETIME_START:
670 case ISD::LIFETIME_END:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000671 case ISD::CopyToReg:
672 case ISD::CopyFromReg:
673 case ISD::EH_LABEL:
674 // Noops don't affect the scoreboard state. Copies are likely to be
675 // removed.
676 return;
677 case ISD::INLINEASM:
678 // For inline asm, clear the pipeline state.
679 HazardRec->Reset();
680 return;
681 }
Dan Gohman90fb5522011-10-20 21:44:34 +0000682 if (SU->isCall) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000683 // Calls are scheduled with their preceding instructions. For bottom-up
684 // scheduling, clear the pipeline state before emitting.
685 HazardRec->Reset();
686 }
687
688 HazardRec->EmitInstruction(SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000689}
690
Andrew Trickb53a00d2011-04-13 00:38:32 +0000691static void resetVRegCycle(SUnit *SU);
692
Dan Gohmanb9543432009-02-10 23:27:53 +0000693/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
694/// count of its predecessors. If a predecessor pending count is zero, add it to
695/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +0000696void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU) {
Andrew Trick1b60ad62011-04-12 20:14:07 +0000697 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000698 DEBUG(SU->dump(this));
699
Evan Chengbdd062d2010-05-20 06:13:19 +0000700#ifndef NDEBUG
701 if (CurCycle < SU->getHeight())
Andrew Trickb53a00d2011-04-13 00:38:32 +0000702 DEBUG(dbgs() << " Height [" << SU->getHeight()
703 << "] pipeline stall!\n");
Evan Chengbdd062d2010-05-20 06:13:19 +0000704#endif
705
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000706 // FIXME: Do not modify node height. It may interfere with
707 // backtracking. Instead add a "ready cycle" to SUnit. Before scheduling the
Eric Christopher1b4b1e52011-03-21 18:06:21 +0000708 // node its ready cycle can aid heuristics, and after scheduling it can
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000709 // indicate the scheduled cycle.
Dan Gohmanb9543432009-02-10 23:27:53 +0000710 SU->setHeightToAtLeast(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000711
712 // Reserve resources for the scheduled intruction.
713 EmitNode(SU);
714
Dan Gohmanb9543432009-02-10 23:27:53 +0000715 Sequence.push_back(SU);
716
Andrew Trick52226d42012-03-07 23:00:49 +0000717 AvailableQueue->scheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000718
Andrew Trick641e2d42011-03-05 08:00:22 +0000719 // If HazardRec is disabled, and each inst counts as one cycle, then
Andrew Trickb53a00d2011-04-13 00:38:32 +0000720 // advance CurCycle before ReleasePredecessors to avoid useless pushes to
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000721 // PendingQueue for schedulers that implement HasReadyFilter.
Andrew Trick641e2d42011-03-05 08:00:22 +0000722 if (!HazardRec->isEnabled() && AvgIPC < 2)
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000723 AdvanceToCycle(CurCycle + 1);
724
Andrew Trick033efdf2010-12-23 03:15:51 +0000725 // Update liveness of predecessors before successors to avoid treating a
726 // two-address node as a live range def.
Andrew Tricka52f3252010-12-23 04:16:14 +0000727 ReleasePredecessors(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000728
729 // Release all the implicit physical register defs that are live.
730 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
731 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000732 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
733 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
734 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
735 --NumLiveRegs;
736 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000737 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000738 }
739 }
Dan Gohman198b7ff2011-11-03 21:49:52 +0000740 // Release the special call resource dependence, if this is the beginning
741 // of a call.
742 unsigned CallResource = TRI->getNumRegs();
743 if (LiveRegDefs[CallResource] == SU)
744 for (const SDNode *SUNode = SU->getNode(); SUNode;
745 SUNode = SUNode->getGluedNode()) {
746 if (SUNode->isMachineOpcode() &&
747 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameSetupOpcode()) {
748 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
749 --NumLiveRegs;
750 LiveRegDefs[CallResource] = NULL;
751 LiveRegGens[CallResource] = NULL;
752 }
753 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000754
Andrew Trickb53a00d2011-04-13 00:38:32 +0000755 resetVRegCycle(SU);
756
Evan Chengd38c22b2006-05-11 23:55:42 +0000757 SU->isScheduled = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000758
759 // Conditions under which the scheduler should eagerly advance the cycle:
760 // (1) No available instructions
761 // (2) All pipelines full, so available instructions must have hazards.
762 //
Andrew Trickb53a00d2011-04-13 00:38:32 +0000763 // If HazardRec is disabled, the cycle was pre-advanced before calling
764 // ReleasePredecessors. In that case, IssueCount should remain 0.
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000765 //
766 // Check AvailableQueue after ReleasePredecessors in case of zero latency.
Andrew Trickb53a00d2011-04-13 00:38:32 +0000767 if (HazardRec->isEnabled() || AvgIPC > 1) {
768 if (SU->getNode() && SU->getNode()->isMachineOpcode())
769 ++IssueCount;
770 if ((HazardRec->isEnabled() && HazardRec->atIssueLimit())
771 || (!HazardRec->isEnabled() && IssueCount == AvgIPC))
772 AdvanceToCycle(CurCycle + 1);
773 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000774}
775
Evan Cheng5924bf72007-09-25 01:54:36 +0000776/// CapturePred - This does the opposite of ReleasePred. Since SU is being
777/// unscheduled, incrcease the succ left count of its predecessors. Remove
778/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000779void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000780 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000781 if (PredSU->isAvailable) {
782 PredSU->isAvailable = false;
783 if (!PredSU->isPending)
784 AvailableQueue->remove(PredSU);
785 }
786
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000787 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000788 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000789}
790
791/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
792/// its predecessor states to reflect the change.
793void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000794 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000795 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000796
Evan Cheng5924bf72007-09-25 01:54:36 +0000797 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
798 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000799 CapturePred(&*I);
Andrew Tricka52f3252010-12-23 04:16:14 +0000800 if (I->isAssignedRegDep() && SU == LiveRegGens[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000801 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000802 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000803 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000804 --NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000805 LiveRegDefs[I->getReg()] = NULL;
Andrew Tricka52f3252010-12-23 04:16:14 +0000806 LiveRegGens[I->getReg()] = NULL;
Evan Cheng5924bf72007-09-25 01:54:36 +0000807 }
808 }
809
Dan Gohman198b7ff2011-11-03 21:49:52 +0000810 // Reclaim the special call resource dependence, if this is the beginning
811 // of a call.
812 unsigned CallResource = TRI->getNumRegs();
813 for (const SDNode *SUNode = SU->getNode(); SUNode;
814 SUNode = SUNode->getGluedNode()) {
815 if (SUNode->isMachineOpcode() &&
816 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameSetupOpcode()) {
817 ++NumLiveRegs;
818 LiveRegDefs[CallResource] = SU;
Eli Friedmand5c173f2011-12-07 22:24:28 +0000819 LiveRegGens[CallResource] = CallSeqEndForStart[SU];
Dan Gohman198b7ff2011-11-03 21:49:52 +0000820 }
821 }
822
823 // Release the special call resource dependence, if this is the end
824 // of a call.
825 if (LiveRegGens[CallResource] == SU)
826 for (const SDNode *SUNode = SU->getNode(); SUNode;
827 SUNode = SUNode->getGluedNode()) {
828 if (SUNode->isMachineOpcode() &&
829 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
830 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
831 --NumLiveRegs;
832 LiveRegDefs[CallResource] = NULL;
833 LiveRegGens[CallResource] = NULL;
834 }
835 }
836
Evan Cheng5924bf72007-09-25 01:54:36 +0000837 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
838 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000839 if (I->isAssignedRegDep()) {
Eli Friedman0bdc0832011-12-07 22:06:02 +0000840 if (!LiveRegDefs[I->getReg()])
841 ++NumLiveRegs;
Andrew Trick033efdf2010-12-23 03:15:51 +0000842 // This becomes the nearest def. Note that an earlier def may still be
843 // pending if this is a two-address node.
844 LiveRegDefs[I->getReg()] = SU;
Andrew Tricka52f3252010-12-23 04:16:14 +0000845 if (LiveRegGens[I->getReg()] == NULL ||
846 I->getSUnit()->getHeight() < LiveRegGens[I->getReg()]->getHeight())
847 LiveRegGens[I->getReg()] = I->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000848 }
849 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000850 if (SU->getHeight() < MinAvailableCycle)
851 MinAvailableCycle = SU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000852
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000853 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000854 SU->isScheduled = false;
855 SU->isAvailable = true;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000856 if (!DisableSchedCycles && AvailableQueue->hasReadyFilter()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000857 // Don't make available until backtracking is complete.
858 SU->isPending = true;
859 PendingQueue.push_back(SU);
860 }
861 else {
862 AvailableQueue->push(SU);
863 }
Andrew Trick52226d42012-03-07 23:00:49 +0000864 AvailableQueue->unscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000865}
866
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000867/// After backtracking, the hazard checker needs to be restored to a state
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000868/// corresponding the current cycle.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000869void ScheduleDAGRRList::RestoreHazardCheckerBottomUp() {
870 HazardRec->Reset();
871
872 unsigned LookAhead = std::min((unsigned)Sequence.size(),
873 HazardRec->getMaxLookAhead());
874 if (LookAhead == 0)
875 return;
876
877 std::vector<SUnit*>::const_iterator I = (Sequence.end() - LookAhead);
878 unsigned HazardCycle = (*I)->getHeight();
879 for (std::vector<SUnit*>::const_iterator E = Sequence.end(); I != E; ++I) {
880 SUnit *SU = *I;
881 for (; SU->getHeight() > HazardCycle; ++HazardCycle) {
882 HazardRec->RecedeCycle();
883 }
884 EmitNode(SU);
885 }
886}
887
Evan Cheng8e136a92007-09-26 21:36:17 +0000888/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000889/// BTCycle in order to schedule a specific node.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000890void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, SUnit *BtSU) {
891 SUnit *OldSU = Sequence.back();
892 while (true) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000893 Sequence.pop_back();
894 if (SU->isSucc(OldSU))
Evan Cheng8e136a92007-09-26 21:36:17 +0000895 // Don't try to remove SU from AvailableQueue.
896 SU->isAvailable = false;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000897 // FIXME: use ready cycle instead of height
898 CurCycle = OldSU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000899 UnscheduleNodeBottomUp(OldSU);
Evan Chengbdd062d2010-05-20 06:13:19 +0000900 AvailableQueue->setCurCycle(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000901 if (OldSU == BtSU)
902 break;
903 OldSU = Sequence.back();
Evan Cheng5924bf72007-09-25 01:54:36 +0000904 }
905
Dan Gohman60d68442009-01-29 19:49:27 +0000906 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000907
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000908 RestoreHazardCheckerBottomUp();
909
Andrew Trick5ce945c2010-12-24 07:10:19 +0000910 ReleasePending();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000911
Evan Cheng1ec79b42007-09-27 07:09:03 +0000912 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000913}
914
Evan Cheng3b245872010-02-05 01:27:11 +0000915static bool isOperandOf(const SUnit *SU, SDNode *N) {
916 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +0000917 SUNode = SUNode->getGluedNode()) {
Evan Cheng3b245872010-02-05 01:27:11 +0000918 if (SUNode->isOperandOf(N))
919 return true;
920 }
921 return false;
922}
923
Evan Cheng5924bf72007-09-25 01:54:36 +0000924/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
925/// successors to the newly created node.
926SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000927 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000928 if (!N)
929 return NULL;
930
Andrew Trickc9405662010-12-24 06:46:50 +0000931 if (SU->getNode()->getGluedNode())
932 return NULL;
933
Evan Cheng79e97132007-10-05 01:39:18 +0000934 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000935 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000936 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000937 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000938 if (VT == MVT::Glue)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000939 return NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000940 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000941 TryUnfold = true;
942 }
Evan Cheng79e97132007-10-05 01:39:18 +0000943 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000944 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000945 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000946 if (VT == MVT::Glue)
Evan Cheng79e97132007-10-05 01:39:18 +0000947 return NULL;
Evan Cheng79e97132007-10-05 01:39:18 +0000948 }
949
950 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000951 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000952 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Evan Cheng79e97132007-10-05 01:39:18 +0000953 return NULL;
954
Pete Cooper7c7ba1b2011-11-15 21:57:53 +0000955 // unfolding an x86 DEC64m operation results in store, dec, load which
956 // can't be handled here so quit
957 if (NewNodes.size() == 3)
958 return NULL;
959
Evan Chengbdd062d2010-05-20 06:13:19 +0000960 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000961 assert(NewNodes.size() == 2 && "Expected a load folding node!");
962
963 N = NewNodes[1];
964 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000965 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000966 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000967 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000968 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
969 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000970 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000971
Dan Gohmane52e0892008-11-11 21:34:44 +0000972 // LoadNode may already exist. This can happen when there is another
973 // load from the same location and producing the same type of value
974 // but it has different alignment or volatileness.
975 bool isNewLoad = true;
976 SUnit *LoadSU;
977 if (LoadNode->getNodeId() != -1) {
978 LoadSU = &SUnits[LoadNode->getNodeId()];
979 isNewLoad = false;
980 } else {
981 LoadSU = CreateNewSUnit(LoadNode);
982 LoadNode->setNodeId(LoadSU->NodeNum);
Andrew Trickd0548ae2011-02-04 03:18:17 +0000983
984 InitNumRegDefsLeft(LoadSU);
Andrew Trick52226d42012-03-07 23:00:49 +0000985 computeLatency(LoadSU);
Dan Gohmane52e0892008-11-11 21:34:44 +0000986 }
987
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000988 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000989 assert(N->getNodeId() == -1 && "Node already inserted!");
990 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +0000991
Evan Cheng6cc775f2011-06-28 19:10:37 +0000992 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
993 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
994 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +0000995 NewSU->isTwoAddress = true;
996 break;
997 }
998 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000999 if (MCID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +00001000 NewSU->isCommutable = true;
Andrew Trickd0548ae2011-02-04 03:18:17 +00001001
1002 InitNumRegDefsLeft(NewSU);
Andrew Trick52226d42012-03-07 23:00:49 +00001003 computeLatency(NewSU);
Evan Cheng79e97132007-10-05 01:39:18 +00001004
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001005 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +00001006 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +00001007 SmallVector<SDep, 4> ChainSuccs;
1008 SmallVector<SDep, 4> LoadPreds;
1009 SmallVector<SDep, 4> NodePreds;
1010 SmallVector<SDep, 4> NodeSuccs;
1011 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1012 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001013 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +00001014 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +00001015 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +00001016 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001017 else
Dan Gohman2d170892008-12-09 22:54:47 +00001018 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001019 }
1020 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1021 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001022 if (I->isCtrl())
1023 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001024 else
Dan Gohman2d170892008-12-09 22:54:47 +00001025 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001026 }
1027
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001028 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +00001029 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
1030 const SDep &Pred = ChainPreds[i];
1031 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +00001032 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +00001033 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001034 }
Evan Cheng79e97132007-10-05 01:39:18 +00001035 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001036 const SDep &Pred = LoadPreds[i];
1037 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +00001038 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +00001039 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +00001040 }
1041 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001042 const SDep &Pred = NodePreds[i];
1043 RemovePred(SU, Pred);
1044 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +00001045 }
1046 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001047 SDep D = NodeSuccs[i];
1048 SUnit *SuccDep = D.getSUnit();
1049 D.setSUnit(SU);
1050 RemovePred(SuccDep, D);
1051 D.setSUnit(NewSU);
1052 AddPred(SuccDep, D);
Andrew Trickd0548ae2011-02-04 03:18:17 +00001053 // Balance register pressure.
1054 if (AvailableQueue->tracksRegPressure() && SuccDep->isScheduled
1055 && !D.isCtrl() && NewSU->NumRegDefsLeft > 0)
1056 --NewSU->NumRegDefsLeft;
Evan Cheng79e97132007-10-05 01:39:18 +00001057 }
1058 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001059 SDep D = ChainSuccs[i];
1060 SUnit *SuccDep = D.getSUnit();
1061 D.setSUnit(SU);
1062 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001063 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +00001064 D.setSUnit(LoadSU);
1065 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001066 }
Andrew Trick2085a962010-12-21 22:25:04 +00001067 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001068
1069 // Add a data dependency to reflect that NewSU reads the value defined
1070 // by LoadSU.
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001071 SDep D(LoadSU, SDep::Data, 0);
1072 D.setLatency(LoadSU->Latency);
1073 AddPred(NewSU, D);
Evan Cheng79e97132007-10-05 01:39:18 +00001074
Evan Cheng91e0fc92007-12-18 08:42:10 +00001075 if (isNewLoad)
1076 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +00001077 AvailableQueue->addNode(NewSU);
1078
1079 ++NumUnfolds;
1080
1081 if (NewSU->NumSuccsLeft == 0) {
1082 NewSU->isAvailable = true;
1083 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +00001084 }
1085 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +00001086 }
1087
Evan Chengbdd062d2010-05-20 06:13:19 +00001088 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001089 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +00001090
1091 // New SUnit has the exact same predecessors.
1092 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1093 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001094 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +00001095 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +00001096
1097 // Only copy scheduled successors. Cut them from old node's successor
1098 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +00001099 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +00001100 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1101 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001102 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +00001103 continue;
Dan Gohman2d170892008-12-09 22:54:47 +00001104 SUnit *SuccSU = I->getSUnit();
1105 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +00001106 SDep D = *I;
1107 D.setSUnit(NewSU);
1108 AddPred(SuccSU, D);
1109 D.setSUnit(SU);
1110 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +00001111 }
1112 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001113 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +00001114 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +00001115
1116 AvailableQueue->updateNode(SU);
1117 AvailableQueue->addNode(NewSU);
1118
Evan Cheng1ec79b42007-09-27 07:09:03 +00001119 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +00001120 return NewSU;
1121}
1122
Evan Chengb2c42c62009-01-12 03:19:55 +00001123/// InsertCopiesAndMoveSuccs - Insert register copies and move all
1124/// scheduled successors of the given SUnit to the last copy.
1125void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
1126 const TargetRegisterClass *DestRC,
1127 const TargetRegisterClass *SrcRC,
Evan Cheng1ec79b42007-09-27 07:09:03 +00001128 SmallVector<SUnit*, 2> &Copies) {
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001129 SUnit *CopyFromSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +00001130 CopyFromSU->CopySrcRC = SrcRC;
1131 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +00001132
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001133 SUnit *CopyToSU = CreateNewSUnit(NULL);
Evan Cheng8e136a92007-09-26 21:36:17 +00001134 CopyToSU->CopySrcRC = DestRC;
1135 CopyToSU->CopyDstRC = SrcRC;
1136
1137 // Only copy scheduled successors. Cut them from old node's successor
1138 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +00001139 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +00001140 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1141 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001142 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +00001143 continue;
Dan Gohman2d170892008-12-09 22:54:47 +00001144 SUnit *SuccSU = I->getSUnit();
1145 if (SuccSU->isScheduled) {
1146 SDep D = *I;
1147 D.setSUnit(CopyToSU);
1148 AddPred(SuccSU, D);
1149 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +00001150 }
Andrew Trick13acae02011-03-23 20:42:39 +00001151 else {
1152 // Avoid scheduling the def-side copy before other successors. Otherwise
1153 // we could introduce another physreg interference on the copy and
1154 // continue inserting copies indefinitely.
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001155 AddPred(SuccSU, SDep(CopyFromSU, SDep::Artificial));
Andrew Trick13acae02011-03-23 20:42:39 +00001156 }
Evan Cheng8e136a92007-09-26 21:36:17 +00001157 }
Evan Chengb2c42c62009-01-12 03:19:55 +00001158 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +00001159 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +00001160
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001161 SDep FromDep(SU, SDep::Data, Reg);
1162 FromDep.setLatency(SU->Latency);
1163 AddPred(CopyFromSU, FromDep);
1164 SDep ToDep(CopyFromSU, SDep::Data, 0);
1165 ToDep.setLatency(CopyFromSU->Latency);
1166 AddPred(CopyToSU, ToDep);
Evan Cheng8e136a92007-09-26 21:36:17 +00001167
1168 AvailableQueue->updateNode(SU);
1169 AvailableQueue->addNode(CopyFromSU);
1170 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001171 Copies.push_back(CopyFromSU);
1172 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +00001173
Evan Chengb2c42c62009-01-12 03:19:55 +00001174 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +00001175}
1176
1177/// getPhysicalRegisterVT - Returns the ValueType of the physical register
1178/// definition of the specified node.
1179/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +00001180static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +00001181 const TargetInstrInfo *TII) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001182 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1183 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
1184 unsigned NumRes = MCID.getNumDefs();
Craig Topper5a4bcc72012-03-08 08:22:45 +00001185 for (const uint16_t *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +00001186 if (Reg == *ImpDef)
1187 break;
1188 ++NumRes;
1189 }
1190 return N->getValueType(NumRes);
1191}
1192
Evan Chengb8905c42009-03-04 01:41:49 +00001193/// CheckForLiveRegDef - Return true and update live register vector if the
1194/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001195static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +00001196 std::vector<SUnit*> &LiveRegDefs,
1197 SmallSet<unsigned, 4> &RegAdded,
1198 SmallVector<unsigned, 4> &LRegs,
1199 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001200 for (MCRegAliasIterator AliasI(Reg, TRI, true); AliasI.isValid(); ++AliasI) {
Andrew Trick12acde112010-12-23 03:43:21 +00001201
1202 // Check if Ref is live.
Andrew Trick0af2e472011-06-07 00:38:12 +00001203 if (!LiveRegDefs[*AliasI]) continue;
Andrew Trick12acde112010-12-23 03:43:21 +00001204
1205 // Allow multiple uses of the same def.
Andrew Trick0af2e472011-06-07 00:38:12 +00001206 if (LiveRegDefs[*AliasI] == SU) continue;
Andrew Trick12acde112010-12-23 03:43:21 +00001207
1208 // Add Reg to the set of interfering live regs.
Andrew Trick0af2e472011-06-07 00:38:12 +00001209 if (RegAdded.insert(*AliasI)) {
Andrew Trick0af2e472011-06-07 00:38:12 +00001210 LRegs.push_back(*AliasI);
1211 }
Evan Chengb8905c42009-03-04 01:41:49 +00001212 }
Evan Chengb8905c42009-03-04 01:41:49 +00001213}
1214
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001215/// CheckForLiveRegDefMasked - Check for any live physregs that are clobbered
1216/// by RegMask, and add them to LRegs.
1217static void CheckForLiveRegDefMasked(SUnit *SU, const uint32_t *RegMask,
1218 std::vector<SUnit*> &LiveRegDefs,
1219 SmallSet<unsigned, 4> &RegAdded,
1220 SmallVector<unsigned, 4> &LRegs) {
1221 // Look at all live registers. Skip Reg0 and the special CallResource.
1222 for (unsigned i = 1, e = LiveRegDefs.size()-1; i != e; ++i) {
1223 if (!LiveRegDefs[i]) continue;
1224 if (LiveRegDefs[i] == SU) continue;
1225 if (!MachineOperand::clobbersPhysReg(RegMask, i)) continue;
1226 if (RegAdded.insert(i))
1227 LRegs.push_back(i);
1228 }
1229}
1230
1231/// getNodeRegMask - Returns the register mask attached to an SDNode, if any.
1232static const uint32_t *getNodeRegMask(const SDNode *N) {
1233 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1234 if (const RegisterMaskSDNode *Op =
1235 dyn_cast<RegisterMaskSDNode>(N->getOperand(i).getNode()))
1236 return Op->getRegMask();
1237 return NULL;
1238}
1239
Evan Cheng5924bf72007-09-25 01:54:36 +00001240/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
1241/// scheduling of the given node to satisfy live physical register dependencies.
1242/// If the specific node is the last one that's available to schedule, do
1243/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001244bool ScheduleDAGRRList::
1245DelayForLiveRegsBottomUp(SUnit *SU, SmallVector<unsigned, 4> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +00001246 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +00001247 return false;
1248
Evan Chenge6f92252007-09-27 18:46:06 +00001249 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +00001250 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001251 //
1252 // If SU is the currently live definition of the same register that it uses,
1253 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +00001254 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1255 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001256 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +00001257 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
1258 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001259 }
1260
Chris Lattner11a33812010-12-23 17:24:32 +00001261 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +00001262 if (Node->getOpcode() == ISD::INLINEASM) {
1263 // Inline asm can clobber physical defs.
1264 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001265 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +00001266 --NumOps; // Ignore the glue operand.
Evan Chengb8905c42009-03-04 01:41:49 +00001267
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001268 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +00001269 unsigned Flags =
1270 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001271 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +00001272
1273 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001274 if (InlineAsm::isRegDefKind(Flags) ||
Jakob Stoklund Olesen537a3022011-06-27 04:08:33 +00001275 InlineAsm::isRegDefEarlyClobberKind(Flags) ||
1276 InlineAsm::isClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +00001277 // Check for def of register or earlyclobber register.
1278 for (; NumVals; --NumVals, ++i) {
1279 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1280 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1281 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
1282 }
1283 } else
1284 i += NumVals;
1285 }
1286 continue;
1287 }
1288
Dan Gohman072734e2008-11-13 23:24:17 +00001289 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +00001290 continue;
Dan Gohman198b7ff2011-11-03 21:49:52 +00001291 // If we're in the middle of scheduling a call, don't begin scheduling
1292 // another call. Also, don't allow any physical registers to be live across
1293 // the call.
1294 if (Node->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
1295 // Check the special calling-sequence resource.
1296 unsigned CallResource = TRI->getNumRegs();
1297 if (LiveRegDefs[CallResource]) {
1298 SDNode *Gen = LiveRegGens[CallResource]->getNode();
1299 while (SDNode *Glued = Gen->getGluedNode())
1300 Gen = Glued;
1301 if (!IsChainDependent(Gen, Node, 0, TII) && RegAdded.insert(CallResource))
1302 LRegs.push_back(CallResource);
1303 }
1304 }
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001305 if (const uint32_t *RegMask = getNodeRegMask(Node))
1306 CheckForLiveRegDefMasked(SU, RegMask, LiveRegDefs, RegAdded, LRegs);
1307
Evan Cheng6cc775f2011-06-28 19:10:37 +00001308 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
1309 if (!MCID.ImplicitDefs)
Evan Cheng5924bf72007-09-25 01:54:36 +00001310 continue;
Craig Topper5a4bcc72012-03-08 08:22:45 +00001311 for (const uint16_t *Reg = MCID.getImplicitDefs(); *Reg; ++Reg)
Evan Chengb8905c42009-03-04 01:41:49 +00001312 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001313 }
Andrew Trick2085a962010-12-21 22:25:04 +00001314
Evan Cheng5924bf72007-09-25 01:54:36 +00001315 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +00001316}
1317
Andrew Trick528fad92010-12-23 05:42:20 +00001318/// Return a node that can be scheduled in this cycle. Requirements:
1319/// (1) Ready: latency has been satisfied
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001320/// (2) No Hazards: resources are available
Andrew Trick528fad92010-12-23 05:42:20 +00001321/// (3) No Interferences: may unschedule to break register interferences.
1322SUnit *ScheduleDAGRRList::PickNodeToScheduleBottomUp() {
1323 SmallVector<SUnit*, 4> Interferences;
1324 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
1325
1326 SUnit *CurSU = AvailableQueue->pop();
1327 while (CurSU) {
1328 SmallVector<unsigned, 4> LRegs;
1329 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
1330 break;
1331 LRegsMap.insert(std::make_pair(CurSU, LRegs));
1332
1333 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
1334 Interferences.push_back(CurSU);
1335 CurSU = AvailableQueue->pop();
1336 }
1337 if (CurSU) {
1338 // Add the nodes that aren't ready back onto the available list.
1339 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1340 Interferences[i]->isPending = false;
1341 assert(Interferences[i]->isAvailable && "must still be available");
1342 AvailableQueue->push(Interferences[i]);
1343 }
1344 return CurSU;
1345 }
1346
1347 // All candidates are delayed due to live physical reg dependencies.
1348 // Try backtracking, code duplication, or inserting cross class copies
1349 // to resolve it.
1350 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1351 SUnit *TrySU = Interferences[i];
1352 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1353
1354 // Try unscheduling up to the point where it's safe to schedule
1355 // this node.
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001356 SUnit *BtSU = NULL;
1357 unsigned LiveCycle = UINT_MAX;
Andrew Trick528fad92010-12-23 05:42:20 +00001358 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
1359 unsigned Reg = LRegs[j];
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001360 if (LiveRegGens[Reg]->getHeight() < LiveCycle) {
1361 BtSU = LiveRegGens[Reg];
1362 LiveCycle = BtSU->getHeight();
1363 }
Andrew Trick528fad92010-12-23 05:42:20 +00001364 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001365 if (!WillCreateCycle(TrySU, BtSU)) {
1366 BacktrackBottomUp(TrySU, BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001367
1368 // Force the current node to be scheduled before the node that
1369 // requires the physical reg dep.
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001370 if (BtSU->isAvailable) {
1371 BtSU->isAvailable = false;
1372 if (!BtSU->isPending)
1373 AvailableQueue->remove(BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001374 }
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001375 AddPred(TrySU, SDep(BtSU, SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001376
1377 // If one or more successors has been unscheduled, then the current
1378 // node is no longer avaialable. Schedule a successor that's now
1379 // available instead.
1380 if (!TrySU->isAvailable) {
1381 CurSU = AvailableQueue->pop();
1382 }
1383 else {
1384 CurSU = TrySU;
1385 TrySU->isPending = false;
1386 Interferences.erase(Interferences.begin()+i);
1387 }
1388 break;
1389 }
1390 }
1391
1392 if (!CurSU) {
1393 // Can't backtrack. If it's too expensive to copy the value, then try
1394 // duplicate the nodes that produces these "too expensive to copy"
1395 // values to break the dependency. In case even that doesn't work,
1396 // insert cross class copies.
1397 // If it's not too expensive, i.e. cost != -1, issue copies.
1398 SUnit *TrySU = Interferences[0];
1399 SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU];
1400 assert(LRegs.size() == 1 && "Can't handle this yet!");
1401 unsigned Reg = LRegs[0];
1402 SUnit *LRDef = LiveRegDefs[Reg];
1403 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
1404 const TargetRegisterClass *RC =
1405 TRI->getMinimalPhysRegClass(Reg, VT);
1406 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
1407
Evan Chengb4c6a342011-03-10 00:16:32 +00001408 // If cross copy register class is the same as RC, then it must be possible
1409 // copy the value directly. Do not try duplicate the def.
1410 // If cross copy register class is not the same as RC, then it's possible to
1411 // copy the value but it require cross register class copies and it is
1412 // expensive.
1413 // If cross copy register class is null, then it's not possible to copy
1414 // the value at all.
Andrew Trick528fad92010-12-23 05:42:20 +00001415 SUnit *NewDef = 0;
Evan Chengb4c6a342011-03-10 00:16:32 +00001416 if (DestRC != RC) {
Andrew Trick528fad92010-12-23 05:42:20 +00001417 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +00001418 if (!DestRC && !NewDef)
1419 report_fatal_error("Can't handle live physical register dependency!");
1420 }
Andrew Trick528fad92010-12-23 05:42:20 +00001421 if (!NewDef) {
1422 // Issue copies, these can be expensive cross register class copies.
1423 SmallVector<SUnit*, 2> Copies;
1424 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1425 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
1426 << " to SU #" << Copies.front()->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001427 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001428 NewDef = Copies.back();
1429 }
1430
1431 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
1432 << " to SU #" << TrySU->NodeNum << "\n");
1433 LiveRegDefs[Reg] = NewDef;
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001434 AddPred(NewDef, SDep(TrySU, SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001435 TrySU->isAvailable = false;
1436 CurSU = NewDef;
1437 }
1438
1439 assert(CurSU && "Unable to resolve live physical register dependencies!");
1440
1441 // Add the nodes that aren't ready back onto the available list.
1442 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1443 Interferences[i]->isPending = false;
1444 // May no longer be available due to backtracking.
1445 if (Interferences[i]->isAvailable) {
1446 AvailableQueue->push(Interferences[i]);
1447 }
1448 }
1449 return CurSU;
1450}
Evan Cheng1ec79b42007-09-27 07:09:03 +00001451
Evan Chengd38c22b2006-05-11 23:55:42 +00001452/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
1453/// schedulers.
1454void ScheduleDAGRRList::ListScheduleBottomUp() {
Dan Gohmanb9543432009-02-10 23:27:53 +00001455 // Release any predecessors of the special Exit node.
Andrew Tricka52f3252010-12-23 04:16:14 +00001456 ReleasePredecessors(&ExitSU);
Dan Gohmanb9543432009-02-10 23:27:53 +00001457
Evan Chengd38c22b2006-05-11 23:55:42 +00001458 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +00001459 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +00001460 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +00001461 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
1462 RootSU->isAvailable = true;
1463 AvailableQueue->push(RootSU);
1464 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001465
1466 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001467 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +00001468 Sequence.reserve(SUnits.size());
Evan Chengd38c22b2006-05-11 23:55:42 +00001469 while (!AvailableQueue->empty()) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00001470 DEBUG(dbgs() << "\nExamining Available:\n";
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001471 AvailableQueue->dump(this));
1472
Andrew Trick528fad92010-12-23 05:42:20 +00001473 // Pick the best node to schedule taking all constraints into
1474 // consideration.
1475 SUnit *SU = PickNodeToScheduleBottomUp();
Evan Cheng1ec79b42007-09-27 07:09:03 +00001476
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001477 AdvancePastStalls(SU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001478
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001479 ScheduleNodeBottomUp(SU);
1480
1481 while (AvailableQueue->empty() && !PendingQueue.empty()) {
1482 // Advance the cycle to free resources. Skip ahead to the next ready SU.
1483 assert(MinAvailableCycle < UINT_MAX && "MinAvailableCycle uninitialized");
1484 AdvanceToCycle(std::max(CurCycle + 1, MinAvailableCycle));
1485 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001486 }
1487
Evan Chengd38c22b2006-05-11 23:55:42 +00001488 // Reverse the order if it is bottom up.
1489 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +00001490
Evan Chengd38c22b2006-05-11 23:55:42 +00001491#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +00001492 VerifyScheduledSequence(/*isBottomUp=*/true);
Evan Chengd38c22b2006-05-11 23:55:42 +00001493#endif
1494}
1495
1496//===----------------------------------------------------------------------===//
Andrew Trick9ccce772011-01-14 21:11:41 +00001497// RegReductionPriorityQueue Definition
Evan Chengd38c22b2006-05-11 23:55:42 +00001498//===----------------------------------------------------------------------===//
1499//
1500// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1501// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +00001502//
Evan Chengd38c22b2006-05-11 23:55:42 +00001503namespace {
Andrew Trick9ccce772011-01-14 21:11:41 +00001504class RegReductionPQBase;
Andrew Trick2085a962010-12-21 22:25:04 +00001505
Andrew Trick9ccce772011-01-14 21:11:41 +00001506struct queue_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1507 bool isReady(SUnit* SU, unsigned CurCycle) const { return true; }
1508};
1509
Andrew Trick3013b6a2011-06-15 17:16:12 +00001510#ifndef NDEBUG
1511template<class SF>
1512struct reverse_sort : public queue_sort {
1513 SF &SortFunc;
1514 reverse_sort(SF &sf) : SortFunc(sf) {}
1515 reverse_sort(const reverse_sort &RHS) : SortFunc(RHS.SortFunc) {}
1516
1517 bool operator()(SUnit* left, SUnit* right) const {
1518 // reverse left/right rather than simply !SortFunc(left, right)
1519 // to expose different paths in the comparison logic.
1520 return SortFunc(right, left);
1521 }
1522};
1523#endif // NDEBUG
1524
Andrew Trick9ccce772011-01-14 21:11:41 +00001525/// bu_ls_rr_sort - Priority function for bottom up register pressure
1526// reduction scheduler.
1527struct bu_ls_rr_sort : public queue_sort {
1528 enum {
1529 IsBottomUp = true,
1530 HasReadyFilter = false
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001531 };
1532
Andrew Trick9ccce772011-01-14 21:11:41 +00001533 RegReductionPQBase *SPQ;
1534 bu_ls_rr_sort(RegReductionPQBase *spq) : SPQ(spq) {}
1535 bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001536
Andrew Trick9ccce772011-01-14 21:11:41 +00001537 bool operator()(SUnit* left, SUnit* right) const;
1538};
Andrew Trick2085a962010-12-21 22:25:04 +00001539
Andrew Trick9ccce772011-01-14 21:11:41 +00001540// src_ls_rr_sort - Priority function for source order scheduler.
1541struct src_ls_rr_sort : public queue_sort {
1542 enum {
1543 IsBottomUp = true,
1544 HasReadyFilter = false
Evan Chengd38c22b2006-05-11 23:55:42 +00001545 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001546
Andrew Trick9ccce772011-01-14 21:11:41 +00001547 RegReductionPQBase *SPQ;
1548 src_ls_rr_sort(RegReductionPQBase *spq)
1549 : SPQ(spq) {}
1550 src_ls_rr_sort(const src_ls_rr_sort &RHS)
1551 : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001552
Andrew Trick9ccce772011-01-14 21:11:41 +00001553 bool operator()(SUnit* left, SUnit* right) const;
1554};
Andrew Trick2085a962010-12-21 22:25:04 +00001555
Andrew Trick9ccce772011-01-14 21:11:41 +00001556// hybrid_ls_rr_sort - Priority function for hybrid scheduler.
1557struct hybrid_ls_rr_sort : public queue_sort {
1558 enum {
1559 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001560 HasReadyFilter = false
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001561 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001562
Andrew Trick9ccce772011-01-14 21:11:41 +00001563 RegReductionPQBase *SPQ;
1564 hybrid_ls_rr_sort(RegReductionPQBase *spq)
1565 : SPQ(spq) {}
1566 hybrid_ls_rr_sort(const hybrid_ls_rr_sort &RHS)
1567 : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001568
Andrew Trick9ccce772011-01-14 21:11:41 +00001569 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Chenga77f3d32010-07-21 06:09:07 +00001570
Andrew Trick9ccce772011-01-14 21:11:41 +00001571 bool operator()(SUnit* left, SUnit* right) const;
1572};
1573
1574// ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1575// scheduler.
1576struct ilp_ls_rr_sort : public queue_sort {
1577 enum {
1578 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001579 HasReadyFilter = false
Evan Chengbdd062d2010-05-20 06:13:19 +00001580 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001581
Andrew Trick9ccce772011-01-14 21:11:41 +00001582 RegReductionPQBase *SPQ;
1583 ilp_ls_rr_sort(RegReductionPQBase *spq)
1584 : SPQ(spq) {}
1585 ilp_ls_rr_sort(const ilp_ls_rr_sort &RHS)
1586 : SPQ(RHS.SPQ) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001587
Andrew Trick9ccce772011-01-14 21:11:41 +00001588 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Cheng37b740c2010-07-24 00:39:05 +00001589
Andrew Trick9ccce772011-01-14 21:11:41 +00001590 bool operator()(SUnit* left, SUnit* right) const;
1591};
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001592
Andrew Trick9ccce772011-01-14 21:11:41 +00001593class RegReductionPQBase : public SchedulingPriorityQueue {
1594protected:
1595 std::vector<SUnit*> Queue;
1596 unsigned CurQueueId;
1597 bool TracksRegPressure;
Evan Cheng8ab58a22012-03-22 19:31:17 +00001598 bool SrcOrder;
Andrew Trick9ccce772011-01-14 21:11:41 +00001599
1600 // SUnits - The SUnits for the current graph.
1601 std::vector<SUnit> *SUnits;
1602
1603 MachineFunction &MF;
1604 const TargetInstrInfo *TII;
1605 const TargetRegisterInfo *TRI;
1606 const TargetLowering *TLI;
1607 ScheduleDAGRRList *scheduleDAG;
1608
1609 // SethiUllmanNumbers - The SethiUllman number for each node.
1610 std::vector<unsigned> SethiUllmanNumbers;
1611
1612 /// RegPressure - Tracking current reg pressure per register class.
1613 ///
1614 std::vector<unsigned> RegPressure;
1615
1616 /// RegLimit - Tracking the number of allocatable registers per register
1617 /// class.
1618 std::vector<unsigned> RegLimit;
1619
1620public:
1621 RegReductionPQBase(MachineFunction &mf,
1622 bool hasReadyFilter,
1623 bool tracksrp,
Evan Cheng8ab58a22012-03-22 19:31:17 +00001624 bool srcorder,
Andrew Trick9ccce772011-01-14 21:11:41 +00001625 const TargetInstrInfo *tii,
1626 const TargetRegisterInfo *tri,
1627 const TargetLowering *tli)
1628 : SchedulingPriorityQueue(hasReadyFilter),
Evan Cheng8ab58a22012-03-22 19:31:17 +00001629 CurQueueId(0), TracksRegPressure(tracksrp), SrcOrder(srcorder),
Andrew Trick9ccce772011-01-14 21:11:41 +00001630 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(NULL) {
1631 if (TracksRegPressure) {
1632 unsigned NumRC = TRI->getNumRegClasses();
1633 RegLimit.resize(NumRC);
1634 RegPressure.resize(NumRC);
1635 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1636 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1637 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1638 E = TRI->regclass_end(); I != E; ++I)
Cameron Zwarichdf616942011-03-07 21:56:36 +00001639 RegLimit[(*I)->getID()] = tri->getRegPressureLimit(*I, MF);
Andrew Trick9ccce772011-01-14 21:11:41 +00001640 }
1641 }
1642
1643 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1644 scheduleDAG = scheduleDag;
1645 }
1646
1647 ScheduleHazardRecognizer* getHazardRec() {
1648 return scheduleDAG->getHazardRec();
1649 }
1650
1651 void initNodes(std::vector<SUnit> &sunits);
1652
1653 void addNode(const SUnit *SU);
1654
1655 void updateNode(const SUnit *SU);
1656
1657 void releaseState() {
1658 SUnits = 0;
1659 SethiUllmanNumbers.clear();
1660 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1661 }
1662
1663 unsigned getNodePriority(const SUnit *SU) const;
1664
1665 unsigned getNodeOrdering(const SUnit *SU) const {
Andrew Trick3bd8b7a2011-03-25 06:40:55 +00001666 if (!SU->getNode()) return 0;
1667
Andrew Trick9ccce772011-01-14 21:11:41 +00001668 return scheduleDAG->DAG->GetOrdering(SU->getNode());
1669 }
1670
1671 bool empty() const { return Queue.empty(); }
1672
1673 void push(SUnit *U) {
1674 assert(!U->NodeQueueId && "Node in the queue already");
1675 U->NodeQueueId = ++CurQueueId;
1676 Queue.push_back(U);
1677 }
1678
1679 void remove(SUnit *SU) {
1680 assert(!Queue.empty() && "Queue is empty!");
1681 assert(SU->NodeQueueId != 0 && "Not in queue!");
1682 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1683 SU);
1684 if (I != prior(Queue.end()))
1685 std::swap(*I, Queue.back());
1686 Queue.pop_back();
1687 SU->NodeQueueId = 0;
1688 }
1689
Andrew Trickd0548ae2011-02-04 03:18:17 +00001690 bool tracksRegPressure() const { return TracksRegPressure; }
1691
Andrew Trick9ccce772011-01-14 21:11:41 +00001692 void dumpRegPressure() const;
1693
1694 bool HighRegPressure(const SUnit *SU) const;
1695
Andrew Trick641e2d42011-03-05 08:00:22 +00001696 bool MayReduceRegPressure(SUnit *SU) const;
1697
1698 int RegPressureDiff(SUnit *SU, unsigned &LiveUses) const;
Andrew Trick9ccce772011-01-14 21:11:41 +00001699
Andrew Trick52226d42012-03-07 23:00:49 +00001700 void scheduledNode(SUnit *SU);
Andrew Trick9ccce772011-01-14 21:11:41 +00001701
Andrew Trick52226d42012-03-07 23:00:49 +00001702 void unscheduledNode(SUnit *SU);
Andrew Trick9ccce772011-01-14 21:11:41 +00001703
1704protected:
1705 bool canClobber(const SUnit *SU, const SUnit *Op);
Duncan Sands635e4ef2011-11-09 14:20:48 +00001706 void AddPseudoTwoAddrDeps();
Andrew Trick9ccce772011-01-14 21:11:41 +00001707 void PrescheduleNodesWithMultipleUses();
1708 void CalculateSethiUllmanNumbers();
1709};
1710
1711template<class SF>
Andrew Trick3013b6a2011-06-15 17:16:12 +00001712static SUnit *popFromQueueImpl(std::vector<SUnit*> &Q, SF &Picker) {
1713 std::vector<SUnit *>::iterator Best = Q.begin();
1714 for (std::vector<SUnit *>::iterator I = llvm::next(Q.begin()),
1715 E = Q.end(); I != E; ++I)
1716 if (Picker(*Best, *I))
1717 Best = I;
1718 SUnit *V = *Best;
1719 if (Best != prior(Q.end()))
1720 std::swap(*Best, Q.back());
1721 Q.pop_back();
1722 return V;
1723}
Andrew Trick9ccce772011-01-14 21:11:41 +00001724
Andrew Trick3013b6a2011-06-15 17:16:12 +00001725template<class SF>
1726SUnit *popFromQueue(std::vector<SUnit*> &Q, SF &Picker, ScheduleDAG *DAG) {
1727#ifndef NDEBUG
1728 if (DAG->StressSched) {
1729 reverse_sort<SF> RPicker(Picker);
1730 return popFromQueueImpl(Q, RPicker);
1731 }
1732#endif
1733 (void)DAG;
1734 return popFromQueueImpl(Q, Picker);
1735}
1736
1737template<class SF>
1738class RegReductionPriorityQueue : public RegReductionPQBase {
Andrew Trick9ccce772011-01-14 21:11:41 +00001739 SF Picker;
1740
1741public:
1742 RegReductionPriorityQueue(MachineFunction &mf,
1743 bool tracksrp,
Evan Cheng8ab58a22012-03-22 19:31:17 +00001744 bool srcorder,
Andrew Trick9ccce772011-01-14 21:11:41 +00001745 const TargetInstrInfo *tii,
1746 const TargetRegisterInfo *tri,
1747 const TargetLowering *tli)
Evan Cheng8ab58a22012-03-22 19:31:17 +00001748 : RegReductionPQBase(mf, SF::HasReadyFilter, tracksrp, srcorder,
1749 tii, tri, tli),
Andrew Trick9ccce772011-01-14 21:11:41 +00001750 Picker(this) {}
1751
1752 bool isBottomUp() const { return SF::IsBottomUp; }
1753
1754 bool isReady(SUnit *U) const {
1755 return Picker.HasReadyFilter && Picker.isReady(U, getCurCycle());
1756 }
1757
1758 SUnit *pop() {
1759 if (Queue.empty()) return NULL;
1760
Andrew Trick3013b6a2011-06-15 17:16:12 +00001761 SUnit *V = popFromQueue(Queue, Picker, scheduleDAG);
Andrew Trick9ccce772011-01-14 21:11:41 +00001762 V->NodeQueueId = 0;
1763 return V;
1764 }
1765
Manman Ren19f49ac2012-09-11 22:23:19 +00001766#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick9ccce772011-01-14 21:11:41 +00001767 void dump(ScheduleDAG *DAG) const {
1768 // Emulate pop() without clobbering NodeQueueIds.
1769 std::vector<SUnit*> DumpQueue = Queue;
1770 SF DumpPicker = Picker;
1771 while (!DumpQueue.empty()) {
Andrew Trick3013b6a2011-06-15 17:16:12 +00001772 SUnit *SU = popFromQueue(DumpQueue, DumpPicker, scheduleDAG);
Dan Gohman90fb5522011-10-20 21:44:34 +00001773 dbgs() << "Height " << SU->getHeight() << ": ";
Andrew Trick9ccce772011-01-14 21:11:41 +00001774 SU->dump(DAG);
1775 }
1776 }
Manman Ren742534c2012-09-06 19:06:06 +00001777#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001778};
1779
1780typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1781BURegReductionPriorityQueue;
1782
Andrew Trick9ccce772011-01-14 21:11:41 +00001783typedef RegReductionPriorityQueue<src_ls_rr_sort>
1784SrcRegReductionPriorityQueue;
1785
1786typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1787HybridBURRPriorityQueue;
1788
1789typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1790ILPBURRPriorityQueue;
1791} // end anonymous namespace
1792
1793//===----------------------------------------------------------------------===//
1794// Static Node Priority for Register Pressure Reduction
1795//===----------------------------------------------------------------------===//
Evan Chengd38c22b2006-05-11 23:55:42 +00001796
Andrew Trickbfbd9722011-04-14 05:15:06 +00001797// Check for special nodes that bypass scheduling heuristics.
1798// Currently this pushes TokenFactor nodes down, but may be used for other
1799// pseudo-ops as well.
1800//
1801// Return -1 to schedule right above left, 1 for left above right.
1802// Return 0 if no bias exists.
1803static int checkSpecialNodes(const SUnit *left, const SUnit *right) {
1804 bool LSchedLow = left->isScheduleLow;
1805 bool RSchedLow = right->isScheduleLow;
1806 if (LSchedLow != RSchedLow)
1807 return LSchedLow < RSchedLow ? 1 : -1;
1808 return 0;
1809}
1810
Dan Gohman186f65d2008-11-20 03:30:37 +00001811/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1812/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001813static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001814CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001815 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1816 if (SethiUllmanNumber != 0)
1817 return SethiUllmanNumber;
1818
1819 unsigned Extra = 0;
1820 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1821 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001822 if (I->isCtrl()) continue; // ignore chain preds
1823 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001824 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001825 if (PredSethiUllman > SethiUllmanNumber) {
1826 SethiUllmanNumber = PredSethiUllman;
1827 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001828 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001829 ++Extra;
1830 }
1831
1832 SethiUllmanNumber += Extra;
1833
1834 if (SethiUllmanNumber == 0)
1835 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001836
Evan Cheng7e4abde2008-07-02 09:23:51 +00001837 return SethiUllmanNumber;
1838}
1839
Andrew Trick9ccce772011-01-14 21:11:41 +00001840/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1841/// scheduling units.
1842void RegReductionPQBase::CalculateSethiUllmanNumbers() {
1843 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001844
Andrew Trick9ccce772011-01-14 21:11:41 +00001845 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1846 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001847}
1848
Andrew Trick9ccce772011-01-14 21:11:41 +00001849void RegReductionPQBase::addNode(const SUnit *SU) {
1850 unsigned SUSize = SethiUllmanNumbers.size();
1851 if (SUnits->size() > SUSize)
1852 SethiUllmanNumbers.resize(SUSize*2, 0);
1853 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1854}
1855
1856void RegReductionPQBase::updateNode(const SUnit *SU) {
1857 SethiUllmanNumbers[SU->NodeNum] = 0;
1858 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1859}
1860
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001861// Lower priority means schedule further down. For bottom-up scheduling, lower
1862// priority SUs are scheduled before higher priority SUs.
Andrew Trick9ccce772011-01-14 21:11:41 +00001863unsigned RegReductionPQBase::getNodePriority(const SUnit *SU) const {
1864 assert(SU->NodeNum < SethiUllmanNumbers.size());
1865 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1866 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1867 // CopyToReg should be close to its uses to facilitate coalescing and
1868 // avoid spilling.
1869 return 0;
1870 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1871 Opc == TargetOpcode::SUBREG_TO_REG ||
1872 Opc == TargetOpcode::INSERT_SUBREG)
1873 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1874 // close to their uses to facilitate coalescing.
1875 return 0;
1876 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1877 // If SU does not have a register use, i.e. it doesn't produce a value
1878 // that would be consumed (e.g. store), then it terminates a chain of
1879 // computation. Give it a large SethiUllman number so it will be
1880 // scheduled right before its predecessors that it doesn't lengthen
1881 // their live ranges.
1882 return 0xffff;
1883 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1884 // If SU does not have a register def, schedule it close to its uses
1885 // because it does not lengthen any live ranges.
1886 return 0;
Evan Cheng1355bbd2011-04-26 21:31:35 +00001887#if 1
Andrew Trick9ccce772011-01-14 21:11:41 +00001888 return SethiUllmanNumbers[SU->NodeNum];
Evan Cheng1355bbd2011-04-26 21:31:35 +00001889#else
1890 unsigned Priority = SethiUllmanNumbers[SU->NodeNum];
1891 if (SU->isCallOp) {
1892 // FIXME: This assumes all of the defs are used as call operands.
1893 int NP = (int)Priority - SU->getNode()->getNumValues();
1894 return (NP > 0) ? NP : 0;
1895 }
1896 return Priority;
1897#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001898}
1899
1900//===----------------------------------------------------------------------===//
1901// Register Pressure Tracking
1902//===----------------------------------------------------------------------===//
1903
1904void RegReductionPQBase::dumpRegPressure() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001905#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick9ccce772011-01-14 21:11:41 +00001906 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1907 E = TRI->regclass_end(); I != E; ++I) {
1908 const TargetRegisterClass *RC = *I;
1909 unsigned Id = RC->getID();
1910 unsigned RP = RegPressure[Id];
1911 if (!RP) continue;
1912 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1913 << '\n');
1914 }
Manman Ren742534c2012-09-06 19:06:06 +00001915#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001916}
1917
1918bool RegReductionPQBase::HighRegPressure(const SUnit *SU) const {
1919 if (!TLI)
1920 return false;
1921
1922 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1923 I != E; ++I) {
1924 if (I->isCtrl())
1925 continue;
1926 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00001927 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1928 // to cover the number of registers defined (they are all live).
1929 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001930 continue;
1931 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001932 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1933 RegDefPos.IsValid(); RegDefPos.Advance()) {
Owen Anderson96adc4a2011-06-15 23:35:18 +00001934 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001935 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00001936
Andrew Trick9ccce772011-01-14 21:11:41 +00001937 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1938 return true;
1939 }
1940 }
Andrew Trick9ccce772011-01-14 21:11:41 +00001941 return false;
1942}
1943
Andrew Trick641e2d42011-03-05 08:00:22 +00001944bool RegReductionPQBase::MayReduceRegPressure(SUnit *SU) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00001945 const SDNode *N = SU->getNode();
1946
1947 if (!N->isMachineOpcode() || !SU->NumSuccs)
1948 return false;
1949
1950 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1951 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00001952 MVT VT = N->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00001953 if (!N->hasAnyUseOfValue(i))
1954 continue;
1955 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1956 if (RegPressure[RCId] >= RegLimit[RCId])
1957 return true;
1958 }
1959 return false;
1960}
1961
Andrew Trick641e2d42011-03-05 08:00:22 +00001962// Compute the register pressure contribution by this instruction by count up
1963// for uses that are not live and down for defs. Only count register classes
1964// that are already under high pressure. As a side effect, compute the number of
1965// uses of registers that are already live.
1966//
1967// FIXME: This encompasses the logic in HighRegPressure and MayReduceRegPressure
1968// so could probably be factored.
1969int RegReductionPQBase::RegPressureDiff(SUnit *SU, unsigned &LiveUses) const {
1970 LiveUses = 0;
1971 int PDiff = 0;
1972 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1973 I != E; ++I) {
1974 if (I->isCtrl())
1975 continue;
1976 SUnit *PredSU = I->getSUnit();
1977 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1978 // to cover the number of registers defined (they are all live).
1979 if (PredSU->NumRegDefsLeft == 0) {
1980 if (PredSU->getNode()->isMachineOpcode())
1981 ++LiveUses;
1982 continue;
1983 }
1984 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1985 RegDefPos.IsValid(); RegDefPos.Advance()) {
Patrik Hagglund05394352012-12-13 18:45:35 +00001986 MVT VT = RegDefPos.GetValue();
Andrew Trick641e2d42011-03-05 08:00:22 +00001987 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1988 if (RegPressure[RCId] >= RegLimit[RCId])
1989 ++PDiff;
1990 }
1991 }
1992 const SDNode *N = SU->getNode();
1993
Eric Christopher7238cba2011-03-08 19:35:47 +00001994 if (!N || !N->isMachineOpcode() || !SU->NumSuccs)
Andrew Trick641e2d42011-03-05 08:00:22 +00001995 return PDiff;
1996
1997 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1998 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00001999 MVT VT = N->getSimpleValueType(i);
Andrew Trick641e2d42011-03-05 08:00:22 +00002000 if (!N->hasAnyUseOfValue(i))
2001 continue;
2002 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2003 if (RegPressure[RCId] >= RegLimit[RCId])
2004 --PDiff;
2005 }
2006 return PDiff;
2007}
2008
Andrew Trick52226d42012-03-07 23:00:49 +00002009void RegReductionPQBase::scheduledNode(SUnit *SU) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002010 if (!TracksRegPressure)
2011 return;
2012
Eric Christopher7238cba2011-03-08 19:35:47 +00002013 if (!SU->getNode())
2014 return;
Andrew Tricka8846e02011-03-23 20:40:18 +00002015
Andrew Trick9ccce772011-01-14 21:11:41 +00002016 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2017 I != E; ++I) {
2018 if (I->isCtrl())
2019 continue;
2020 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00002021 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
2022 // to cover the number of registers defined (they are all live).
2023 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002024 continue;
2025 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00002026 // FIXME: The ScheduleDAG currently loses information about which of a
2027 // node's values is consumed by each dependence. Consequently, if the node
2028 // defines multiple register classes, we don't know which to pressurize
2029 // here. Instead the following loop consumes the register defs in an
2030 // arbitrary order. At least it handles the common case of clustered loads
2031 // to the same class. For precise liveness, each SDep needs to indicate the
2032 // result number. But that tightly couples the ScheduleDAG with the
2033 // SelectionDAG making updates tricky. A simpler hack would be to attach a
2034 // value type or register class to SDep.
2035 //
2036 // The most important aspect of register tracking is balancing the increase
2037 // here with the reduction further below. Note that this SU may use multiple
2038 // defs in PredSU. The can't be determined here, but we've already
2039 // compensated by reducing NumRegDefsLeft in PredSU during
2040 // ScheduleDAGSDNodes::AddSchedEdges.
2041 --PredSU->NumRegDefsLeft;
2042 unsigned SkipRegDefs = PredSU->NumRegDefsLeft;
2043 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
2044 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
2045 if (SkipRegDefs)
Andrew Trick9ccce772011-01-14 21:11:41 +00002046 continue;
Owen Anderson96adc4a2011-06-15 23:35:18 +00002047
2048 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002049 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00002050 RegPressure[RCId] += Cost;
Andrew Trickd0548ae2011-02-04 03:18:17 +00002051 break;
Andrew Trick9ccce772011-01-14 21:11:41 +00002052 }
2053 }
2054
Andrew Trickd0548ae2011-02-04 03:18:17 +00002055 // We should have this assert, but there may be dead SDNodes that never
2056 // materialize as SUnits, so they don't appear to generate liveness.
2057 //assert(SU->NumRegDefsLeft == 0 && "not all regdefs have scheduled uses");
2058 int SkipRegDefs = (int)SU->NumRegDefsLeft;
2059 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(SU, scheduleDAG);
2060 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
2061 if (SkipRegDefs > 0)
2062 continue;
Owen Anderson96adc4a2011-06-15 23:35:18 +00002063 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002064 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00002065 if (RegPressure[RCId] < Cost) {
Andrew Trickd0548ae2011-02-04 03:18:17 +00002066 // Register pressure tracking is imprecise. This can happen. But we try
2067 // hard not to let it happen because it likely results in poor scheduling.
2068 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") has too many regdefs\n");
2069 RegPressure[RCId] = 0;
2070 }
2071 else {
Owen Anderson96adc4a2011-06-15 23:35:18 +00002072 RegPressure[RCId] -= Cost;
Andrew Trick9ccce772011-01-14 21:11:41 +00002073 }
2074 }
Andrew Trick9ccce772011-01-14 21:11:41 +00002075 dumpRegPressure();
2076}
2077
Andrew Trick52226d42012-03-07 23:00:49 +00002078void RegReductionPQBase::unscheduledNode(SUnit *SU) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002079 if (!TracksRegPressure)
2080 return;
2081
2082 const SDNode *N = SU->getNode();
Eric Christopher7238cba2011-03-08 19:35:47 +00002083 if (!N) return;
Andrew Tricka8846e02011-03-23 20:40:18 +00002084
Andrew Trick9ccce772011-01-14 21:11:41 +00002085 if (!N->isMachineOpcode()) {
2086 if (N->getOpcode() != ISD::CopyToReg)
2087 return;
2088 } else {
2089 unsigned Opc = N->getMachineOpcode();
2090 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2091 Opc == TargetOpcode::INSERT_SUBREG ||
2092 Opc == TargetOpcode::SUBREG_TO_REG ||
2093 Opc == TargetOpcode::REG_SEQUENCE ||
2094 Opc == TargetOpcode::IMPLICIT_DEF)
2095 return;
2096 }
2097
2098 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2099 I != E; ++I) {
2100 if (I->isCtrl())
2101 continue;
2102 SUnit *PredSU = I->getSUnit();
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002103 // NumSuccsLeft counts all deps. Don't compare it with NumSuccs which only
2104 // counts data deps.
2105 if (PredSU->NumSuccsLeft != PredSU->Succs.size())
Andrew Trick9ccce772011-01-14 21:11:41 +00002106 continue;
2107 const SDNode *PN = PredSU->getNode();
2108 if (!PN->isMachineOpcode()) {
2109 if (PN->getOpcode() == ISD::CopyFromReg) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002110 MVT VT = PN->getSimpleValueType(0);
Andrew Trick9ccce772011-01-14 21:11:41 +00002111 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2112 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2113 }
2114 continue;
2115 }
2116 unsigned POpc = PN->getMachineOpcode();
2117 if (POpc == TargetOpcode::IMPLICIT_DEF)
2118 continue;
Andrew Trick31f25bc2011-06-27 18:01:20 +00002119 if (POpc == TargetOpcode::EXTRACT_SUBREG ||
2120 POpc == TargetOpcode::INSERT_SUBREG ||
2121 POpc == TargetOpcode::SUBREG_TO_REG) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002122 MVT VT = PN->getSimpleValueType(0);
Andrew Trick9ccce772011-01-14 21:11:41 +00002123 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2124 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2125 continue;
2126 }
2127 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
2128 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002129 MVT VT = PN->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00002130 if (!PN->hasAnyUseOfValue(i))
2131 continue;
2132 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2133 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
2134 // Register pressure tracking is imprecise. This can happen.
2135 RegPressure[RCId] = 0;
2136 else
2137 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
2138 }
2139 }
2140
2141 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
2142 // may transfer data dependencies to CopyToReg.
2143 if (SU->NumSuccs && N->isMachineOpcode()) {
2144 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
2145 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002146 MVT VT = N->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00002147 if (VT == MVT::Glue || VT == MVT::Other)
2148 continue;
2149 if (!N->hasAnyUseOfValue(i))
2150 continue;
2151 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2152 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2153 }
2154 }
2155
2156 dumpRegPressure();
2157}
2158
2159//===----------------------------------------------------------------------===//
2160// Dynamic Node Priority for Register Pressure Reduction
2161//===----------------------------------------------------------------------===//
2162
Evan Chengb9e3db62007-03-14 22:43:40 +00002163/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00002164/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00002165static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002166 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00002167 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00002168 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00002169 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002170 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00002171 // If there are bunch of CopyToRegs stacked up, they should be considered
2172 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00002173 if (I->getSUnit()->getNode() &&
2174 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002175 Height = closestSucc(I->getSUnit())+1;
2176 if (Height > MaxHeight)
2177 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00002178 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002179 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00002180}
2181
Evan Cheng61bc51e2007-12-20 02:22:36 +00002182/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00002183/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00002184static unsigned calcMaxScratches(const SUnit *SU) {
2185 unsigned Scratches = 0;
2186 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00002187 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002188 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00002189 Scratches++;
2190 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00002191 return Scratches;
2192}
2193
Andrew Trickb53a00d2011-04-13 00:38:32 +00002194/// hasOnlyLiveInOpers - Return true if SU has only value predecessors that are
2195/// CopyFromReg from a virtual register.
2196static bool hasOnlyLiveInOpers(const SUnit *SU) {
2197 bool RetVal = false;
2198 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2199 I != E; ++I) {
2200 if (I->isCtrl()) continue;
2201 const SUnit *PredSU = I->getSUnit();
2202 if (PredSU->getNode() &&
2203 PredSU->getNode()->getOpcode() == ISD::CopyFromReg) {
2204 unsigned Reg =
2205 cast<RegisterSDNode>(PredSU->getNode()->getOperand(1))->getReg();
2206 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2207 RetVal = true;
2208 continue;
2209 }
2210 }
2211 return false;
2212 }
2213 return RetVal;
2214}
2215
2216/// hasOnlyLiveOutUses - Return true if SU has only value successors that are
Evan Cheng6c1414f2010-10-29 18:09:28 +00002217/// CopyToReg to a virtual register. This SU def is probably a liveout and
2218/// it has no other use. It should be scheduled closer to the terminator.
2219static bool hasOnlyLiveOutUses(const SUnit *SU) {
2220 bool RetVal = false;
2221 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
2222 I != E; ++I) {
2223 if (I->isCtrl()) continue;
2224 const SUnit *SuccSU = I->getSUnit();
2225 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
2226 unsigned Reg =
2227 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
2228 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2229 RetVal = true;
2230 continue;
2231 }
2232 }
2233 return false;
2234 }
2235 return RetVal;
2236}
2237
Andrew Trickb53a00d2011-04-13 00:38:32 +00002238// Set isVRegCycle for a node with only live in opers and live out uses. Also
2239// set isVRegCycle for its CopyFromReg operands.
2240//
2241// This is only relevant for single-block loops, in which case the VRegCycle
2242// node is likely an induction variable in which the operand and target virtual
2243// registers should be coalesced (e.g. pre/post increment values). Setting the
2244// isVRegCycle flag helps the scheduler prioritize other uses of the same
2245// CopyFromReg so that this node becomes the virtual register "kill". This
2246// avoids interference between the values live in and out of the block and
2247// eliminates a copy inside the loop.
2248static void initVRegCycle(SUnit *SU) {
2249 if (DisableSchedVRegCycle)
2250 return;
2251
2252 if (!hasOnlyLiveInOpers(SU) || !hasOnlyLiveOutUses(SU))
2253 return;
2254
2255 DEBUG(dbgs() << "VRegCycle: SU(" << SU->NodeNum << ")\n");
2256
2257 SU->isVRegCycle = true;
2258
2259 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Andrew Trickc5dd24a2011-04-12 19:54:36 +00002260 I != E; ++I) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00002261 if (I->isCtrl()) continue;
2262 I->getSUnit()->isVRegCycle = true;
Andrew Trickc5dd24a2011-04-12 19:54:36 +00002263 }
Andrew Trick1b60ad62011-04-12 20:14:07 +00002264}
2265
Andrew Trickb53a00d2011-04-13 00:38:32 +00002266// After scheduling the definition of a VRegCycle, clear the isVRegCycle flag of
2267// CopyFromReg operands. We should no longer penalize other uses of this VReg.
2268static void resetVRegCycle(SUnit *SU) {
2269 if (!SU->isVRegCycle)
2270 return;
2271
2272 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
2273 I != E; ++I) {
Andrew Trick1b60ad62011-04-12 20:14:07 +00002274 if (I->isCtrl()) continue; // ignore chain preds
Andrew Trickb53a00d2011-04-13 00:38:32 +00002275 SUnit *PredSU = I->getSUnit();
2276 if (PredSU->isVRegCycle) {
2277 assert(PredSU->getNode()->getOpcode() == ISD::CopyFromReg &&
2278 "VRegCycle def must be CopyFromReg");
2279 I->getSUnit()->isVRegCycle = 0;
2280 }
2281 }
2282}
2283
2284// Return true if this SUnit uses a CopyFromReg node marked as a VRegCycle. This
2285// means a node that defines the VRegCycle has not been scheduled yet.
2286static bool hasVRegCycleUse(const SUnit *SU) {
2287 // If this SU also defines the VReg, don't hoist it as a "use".
2288 if (SU->isVRegCycle)
2289 return false;
2290
2291 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
2292 I != E; ++I) {
2293 if (I->isCtrl()) continue; // ignore chain preds
2294 if (I->getSUnit()->isVRegCycle &&
2295 I->getSUnit()->getNode()->getOpcode() == ISD::CopyFromReg) {
2296 DEBUG(dbgs() << " VReg cycle use: SU (" << SU->NodeNum << ")\n");
2297 return true;
Andrew Trick2ad0b372011-04-07 19:54:57 +00002298 }
2299 }
2300 return false;
2301}
2302
Andrew Trick9ccce772011-01-14 21:11:41 +00002303// Check for either a dependence (latency) or resource (hazard) stall.
2304//
2305// Note: The ScheduleHazardRecognizer interface requires a non-const SU.
2306static bool BUHasStall(SUnit *SU, int Height, RegReductionPQBase *SPQ) {
2307 if ((int)SPQ->getCurCycle() < Height) return true;
2308 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2309 != ScheduleHazardRecognizer::NoHazard)
2310 return true;
2311 return false;
2312}
2313
2314// Return -1 if left has higher priority, 1 if right has higher priority.
2315// Return 0 if latency-based priority is equivalent.
2316static int BUCompareLatency(SUnit *left, SUnit *right, bool checkPref,
2317 RegReductionPQBase *SPQ) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00002318 // Scheduling an instruction that uses a VReg whose postincrement has not yet
2319 // been scheduled will induce a copy. Model this as an extra cycle of latency.
2320 int LPenalty = hasVRegCycleUse(left) ? 1 : 0;
2321 int RPenalty = hasVRegCycleUse(right) ? 1 : 0;
2322 int LHeight = (int)left->getHeight() + LPenalty;
2323 int RHeight = (int)right->getHeight() + RPenalty;
Andrew Trick9ccce772011-01-14 21:11:41 +00002324
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002325 bool LStall = (!checkPref || left->SchedulingPref == Sched::ILP) &&
Andrew Trick9ccce772011-01-14 21:11:41 +00002326 BUHasStall(left, LHeight, SPQ);
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002327 bool RStall = (!checkPref || right->SchedulingPref == Sched::ILP) &&
Andrew Trick9ccce772011-01-14 21:11:41 +00002328 BUHasStall(right, RHeight, SPQ);
2329
2330 // If scheduling one of the node will cause a pipeline stall, delay it.
2331 // If scheduling either one of the node will cause a pipeline stall, sort
2332 // them according to their height.
2333 if (LStall) {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002334 if (!RStall)
Andrew Trick9ccce772011-01-14 21:11:41 +00002335 return 1;
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002336 if (LHeight != RHeight)
Andrew Trick9ccce772011-01-14 21:11:41 +00002337 return LHeight > RHeight ? 1 : -1;
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002338 } else if (RStall)
Andrew Trick9ccce772011-01-14 21:11:41 +00002339 return -1;
2340
Andrew Trick47ff14b2011-01-21 05:51:33 +00002341 // If either node is scheduling for latency, sort them by height/depth
Andrew Trick9ccce772011-01-14 21:11:41 +00002342 // and latency.
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002343 if (!checkPref || (left->SchedulingPref == Sched::ILP ||
2344 right->SchedulingPref == Sched::ILP)) {
Andrew Tricka88d46e2012-06-05 03:44:34 +00002345 // If neither instruction stalls (!LStall && !RStall) and HazardRecognizer
2346 // is enabled, grouping instructions by cycle, then its height is already
2347 // covered so only its depth matters. We also reach this point if both stall
2348 // but have the same height.
2349 if (!SPQ->getHazardRec()->isEnabled()) {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002350 if (LHeight != RHeight)
Andrew Trick9ccce772011-01-14 21:11:41 +00002351 return LHeight > RHeight ? 1 : -1;
2352 }
Andrew Tricka88d46e2012-06-05 03:44:34 +00002353 int LDepth = left->getDepth() - LPenalty;
2354 int RDepth = right->getDepth() - RPenalty;
2355 if (LDepth != RDepth) {
2356 DEBUG(dbgs() << " Comparing latency of SU (" << left->NodeNum
2357 << ") depth " << LDepth << " vs SU (" << right->NodeNum
2358 << ") depth " << RDepth << "\n");
2359 return LDepth < RDepth ? 1 : -1;
Andrew Trick47ff14b2011-01-21 05:51:33 +00002360 }
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002361 if (left->Latency != right->Latency)
Andrew Trick9ccce772011-01-14 21:11:41 +00002362 return left->Latency > right->Latency ? 1 : -1;
2363 }
2364 return 0;
2365}
2366
2367static bool BURRSort(SUnit *left, SUnit *right, RegReductionPQBase *SPQ) {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002368 // Schedule physical register definitions close to their use. This is
2369 // motivated by microarchitectures that can fuse cmp+jump macro-ops. But as
2370 // long as shortening physreg live ranges is generally good, we can defer
2371 // creating a subtarget hook.
2372 if (!DisableSchedPhysRegJoin) {
2373 bool LHasPhysReg = left->hasPhysRegDefs;
2374 bool RHasPhysReg = right->hasPhysRegDefs;
2375 if (LHasPhysReg != RHasPhysReg) {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002376 #ifndef NDEBUG
Craig Topper95207192012-05-24 06:35:32 +00002377 const char *const PhysRegMsg[] = {" has no physreg"," defines a physreg"};
Andrew Trickbfbd9722011-04-14 05:15:06 +00002378 #endif
2379 DEBUG(dbgs() << " SU (" << left->NodeNum << ") "
2380 << PhysRegMsg[LHasPhysReg] << " SU(" << right->NodeNum << ") "
2381 << PhysRegMsg[RHasPhysReg] << "\n");
2382 return LHasPhysReg < RHasPhysReg;
2383 }
2384 }
2385
Evan Cheng2f647542011-04-26 04:57:37 +00002386 // Prioritize by Sethi-Ulmann number and push CopyToReg nodes down.
Evan Cheng6730f032007-01-08 23:55:53 +00002387 unsigned LPriority = SPQ->getNodePriority(left);
2388 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng1355bbd2011-04-26 21:31:35 +00002389
2390 // Be really careful about hoisting call operands above previous calls.
2391 // Only allows it if it would reduce register pressure.
2392 if (left->isCall && right->isCallOp) {
2393 unsigned RNumVals = right->getNode()->getNumValues();
2394 RPriority = (RPriority > RNumVals) ? (RPriority - RNumVals) : 0;
2395 }
2396 if (right->isCall && left->isCallOp) {
2397 unsigned LNumVals = left->getNode()->getNumValues();
2398 LPriority = (LPriority > LNumVals) ? (LPriority - LNumVals) : 0;
2399 }
2400
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002401 if (LPriority != RPriority)
Evan Cheng73bdf042008-03-01 00:39:47 +00002402 return LPriority > RPriority;
Andrew Trick52b3e382011-03-08 01:51:56 +00002403
Evan Cheng1355bbd2011-04-26 21:31:35 +00002404 // One or both of the nodes are calls and their sethi-ullman numbers are the
2405 // same, then keep source order.
2406 if (left->isCall || right->isCall) {
2407 unsigned LOrder = SPQ->getNodeOrdering(left);
2408 unsigned ROrder = SPQ->getNodeOrdering(right);
2409
2410 // Prefer an ordering where the lower the non-zero order number, the higher
2411 // the preference.
2412 if ((LOrder || ROrder) && LOrder != ROrder)
2413 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2414 }
2415
Evan Cheng73bdf042008-03-01 00:39:47 +00002416 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
2417 // e.g.
2418 // t1 = op t2, c1
2419 // t3 = op t4, c2
2420 //
2421 // and the following instructions are both ready.
2422 // t2 = op c3
2423 // t4 = op c4
2424 //
2425 // Then schedule t2 = op first.
2426 // i.e.
2427 // t4 = op c4
2428 // t2 = op c3
2429 // t1 = op t2, c1
2430 // t3 = op t4, c2
2431 //
2432 // This creates more short live intervals.
2433 unsigned LDist = closestSucc(left);
2434 unsigned RDist = closestSucc(right);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002435 if (LDist != RDist)
Evan Cheng73bdf042008-03-01 00:39:47 +00002436 return LDist < RDist;
2437
Evan Cheng3a14efa2009-02-12 08:59:45 +00002438 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00002439 unsigned LScratch = calcMaxScratches(left);
2440 unsigned RScratch = calcMaxScratches(right);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002441 if (LScratch != RScratch)
Evan Cheng73bdf042008-03-01 00:39:47 +00002442 return LScratch > RScratch;
2443
Evan Cheng1355bbd2011-04-26 21:31:35 +00002444 // Comparing latency against a call makes little sense unless the node
2445 // is register pressure-neutral.
2446 if ((left->isCall && RPriority > 0) || (right->isCall && LPriority > 0))
2447 return (left->NodeQueueId > right->NodeQueueId);
2448
2449 // Do not compare latencies when one or both of the nodes are calls.
2450 if (!DisableSchedCycles &&
2451 !(left->isCall || right->isCall)) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002452 int result = BUCompareLatency(left, right, false /*checkPref*/, SPQ);
2453 if (result != 0)
2454 return result > 0;
2455 }
2456 else {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002457 if (left->getHeight() != right->getHeight())
Andrew Trick9ccce772011-01-14 21:11:41 +00002458 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00002459
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002460 if (left->getDepth() != right->getDepth())
Andrew Trick9ccce772011-01-14 21:11:41 +00002461 return left->getDepth() < right->getDepth();
2462 }
Evan Cheng73bdf042008-03-01 00:39:47 +00002463
Andrew Trick2085a962010-12-21 22:25:04 +00002464 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002465 "NodeQueueId cannot be zero");
2466 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002467}
2468
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002469// Bottom up
Andrew Trick9ccce772011-01-14 21:11:41 +00002470bool bu_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002471 if (int res = checkSpecialNodes(left, right))
2472 return res > 0;
2473
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002474 return BURRSort(left, right, SPQ);
2475}
2476
2477// Source order, otherwise bottom up.
Andrew Trick9ccce772011-01-14 21:11:41 +00002478bool src_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002479 if (int res = checkSpecialNodes(left, right))
2480 return res > 0;
2481
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002482 unsigned LOrder = SPQ->getNodeOrdering(left);
2483 unsigned ROrder = SPQ->getNodeOrdering(right);
2484
2485 // Prefer an ordering where the lower the non-zero order number, the higher
2486 // the preference.
2487 if ((LOrder || ROrder) && LOrder != ROrder)
2488 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2489
2490 return BURRSort(left, right, SPQ);
2491}
2492
Andrew Trick9ccce772011-01-14 21:11:41 +00002493// If the time between now and when the instruction will be ready can cover
2494// the spill code, then avoid adding it to the ready queue. This gives long
2495// stalls highest priority and allows hoisting across calls. It should also
2496// speed up processing the available queue.
2497bool hybrid_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
2498 static const unsigned ReadyDelay = 3;
2499
2500 if (SPQ->MayReduceRegPressure(SU)) return true;
2501
2502 if (SU->getHeight() > (CurCycle + ReadyDelay)) return false;
2503
2504 if (SPQ->getHazardRec()->getHazardType(SU, -ReadyDelay)
2505 != ScheduleHazardRecognizer::NoHazard)
2506 return false;
2507
2508 return true;
2509}
2510
2511// Return true if right should be scheduled with higher priority than left.
2512bool hybrid_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002513 if (int res = checkSpecialNodes(left, right))
2514 return res > 0;
2515
Evan Chengdebf9c52010-11-03 00:45:17 +00002516 if (left->isCall || right->isCall)
2517 // No way to compute latency of calls.
2518 return BURRSort(left, right, SPQ);
2519
Evan Chenge6d6c5d2010-07-26 21:49:07 +00002520 bool LHigh = SPQ->HighRegPressure(left);
2521 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00002522 // Avoid causing spills. If register pressure is high, schedule for
2523 // register pressure reduction.
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002524 if (LHigh && !RHigh) {
2525 DEBUG(dbgs() << " pressure SU(" << left->NodeNum << ") > SU("
2526 << right->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002527 return true;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002528 }
2529 else if (!LHigh && RHigh) {
2530 DEBUG(dbgs() << " pressure SU(" << right->NodeNum << ") > SU("
2531 << left->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002532 return false;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002533 }
Andrew Trickb53a00d2011-04-13 00:38:32 +00002534 if (!LHigh && !RHigh) {
2535 int result = BUCompareLatency(left, right, true /*checkPref*/, SPQ);
2536 if (result != 0)
2537 return result > 0;
Evan Chengcc2efe12010-05-28 23:26:21 +00002538 }
Evan Chengbdd062d2010-05-20 06:13:19 +00002539 return BURRSort(left, right, SPQ);
2540}
2541
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002542// Schedule as many instructions in each cycle as possible. So don't make an
2543// instruction available unless it is ready in the current cycle.
2544bool ilp_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00002545 if (SU->getHeight() > CurCycle) return false;
2546
2547 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2548 != ScheduleHazardRecognizer::NoHazard)
2549 return false;
2550
Andrew Trickc88b7ec2011-03-04 02:03:45 +00002551 return true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002552}
2553
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002554static bool canEnableCoalescing(SUnit *SU) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002555 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
2556 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
2557 // CopyToReg should be close to its uses to facilitate coalescing and
2558 // avoid spilling.
2559 return true;
2560
2561 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2562 Opc == TargetOpcode::SUBREG_TO_REG ||
2563 Opc == TargetOpcode::INSERT_SUBREG)
2564 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
2565 // close to their uses to facilitate coalescing.
2566 return true;
2567
2568 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
2569 // If SU does not have a register def, schedule it close to its uses
2570 // because it does not lengthen any live ranges.
2571 return true;
2572
2573 return false;
2574}
2575
Andrew Trickb8390b72011-03-05 08:04:11 +00002576// list-ilp is currently an experimental scheduler that allows various
2577// heuristics to be enabled prior to the normal register reduction logic.
Andrew Trick9ccce772011-01-14 21:11:41 +00002578bool ilp_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002579 if (int res = checkSpecialNodes(left, right))
2580 return res > 0;
2581
Evan Chengdebf9c52010-11-03 00:45:17 +00002582 if (left->isCall || right->isCall)
2583 // No way to compute latency of calls.
2584 return BURRSort(left, right, SPQ);
2585
Andrew Trick52b3e382011-03-08 01:51:56 +00002586 unsigned LLiveUses = 0, RLiveUses = 0;
2587 int LPDiff = 0, RPDiff = 0;
2588 if (!DisableSchedRegPressure || !DisableSchedLiveUses) {
2589 LPDiff = SPQ->RegPressureDiff(left, LLiveUses);
2590 RPDiff = SPQ->RegPressureDiff(right, RLiveUses);
2591 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002592 if (!DisableSchedRegPressure && LPDiff != RPDiff) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002593 DEBUG(dbgs() << "RegPressureDiff SU(" << left->NodeNum << "): " << LPDiff
2594 << " != SU(" << right->NodeNum << "): " << RPDiff << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002595 return LPDiff > RPDiff;
2596 }
2597
Andrew Trick52b3e382011-03-08 01:51:56 +00002598 if (!DisableSchedRegPressure && (LPDiff > 0 || RPDiff > 0)) {
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002599 bool LReduce = canEnableCoalescing(left);
2600 bool RReduce = canEnableCoalescing(right);
Andrew Trick52b3e382011-03-08 01:51:56 +00002601 if (LReduce && !RReduce) return false;
2602 if (RReduce && !LReduce) return true;
2603 }
2604
2605 if (!DisableSchedLiveUses && (LLiveUses != RLiveUses)) {
2606 DEBUG(dbgs() << "Live uses SU(" << left->NodeNum << "): " << LLiveUses
2607 << " != SU(" << right->NodeNum << "): " << RLiveUses << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002608 return LLiveUses < RLiveUses;
2609 }
2610
Andrew Trick52b3e382011-03-08 01:51:56 +00002611 if (!DisableSchedStalls) {
2612 bool LStall = BUHasStall(left, left->getHeight(), SPQ);
2613 bool RStall = BUHasStall(right, right->getHeight(), SPQ);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002614 if (LStall != RStall)
Andrew Trick52b3e382011-03-08 01:51:56 +00002615 return left->getHeight() > right->getHeight();
Andrew Trick641e2d42011-03-05 08:00:22 +00002616 }
2617
Andrew Trick25cedf32011-03-05 10:29:25 +00002618 if (!DisableSchedCriticalPath) {
2619 int spread = (int)left->getDepth() - (int)right->getDepth();
2620 if (std::abs(spread) > MaxReorderWindow) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002621 DEBUG(dbgs() << "Depth of SU(" << left->NodeNum << "): "
2622 << left->getDepth() << " != SU(" << right->NodeNum << "): "
2623 << right->getDepth() << "\n");
Andrew Trick25cedf32011-03-05 10:29:25 +00002624 return left->getDepth() < right->getDepth();
2625 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002626 }
2627
2628 if (!DisableSchedHeight && left->getHeight() != right->getHeight()) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002629 int spread = (int)left->getHeight() - (int)right->getHeight();
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002630 if (std::abs(spread) > MaxReorderWindow)
Andrew Trick52b3e382011-03-08 01:51:56 +00002631 return left->getHeight() > right->getHeight();
Evan Cheng37b740c2010-07-24 00:39:05 +00002632 }
2633
2634 return BURRSort(left, right, SPQ);
2635}
2636
Andrew Trickb53a00d2011-04-13 00:38:32 +00002637void RegReductionPQBase::initNodes(std::vector<SUnit> &sunits) {
2638 SUnits = &sunits;
2639 // Add pseudo dependency edges for two-address nodes.
Evan Chengd33b2d62011-11-10 07:43:16 +00002640 if (!Disable2AddrHack)
2641 AddPseudoTwoAddrDeps();
Andrew Trickb53a00d2011-04-13 00:38:32 +00002642 // Reroute edges to nodes with multiple uses.
Evan Cheng8ab58a22012-03-22 19:31:17 +00002643 if (!TracksRegPressure && !SrcOrder)
Andrew Trickb53a00d2011-04-13 00:38:32 +00002644 PrescheduleNodesWithMultipleUses();
2645 // Calculate node priorities.
2646 CalculateSethiUllmanNumbers();
2647
2648 // For single block loops, mark nodes that look like canonical IV increments.
2649 if (scheduleDAG->BB->isSuccessor(scheduleDAG->BB)) {
2650 for (unsigned i = 0, e = sunits.size(); i != e; ++i) {
2651 initVRegCycle(&sunits[i]);
2652 }
2653 }
2654}
2655
Andrew Trick9ccce772011-01-14 21:11:41 +00002656//===----------------------------------------------------------------------===//
2657// Preschedule for Register Pressure
2658//===----------------------------------------------------------------------===//
2659
2660bool RegReductionPQBase::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002661 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002662 unsigned Opc = SU->getNode()->getMachineOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002663 const MCInstrDesc &MCID = TII->get(Opc);
2664 unsigned NumRes = MCID.getNumDefs();
2665 unsigned NumOps = MCID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002666 for (unsigned i = 0; i != NumOps; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002667 if (MCID.getOperandConstraint(i+NumRes, MCOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002668 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00002669 if (DU->getNodeId() != -1 &&
2670 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002671 return true;
2672 }
2673 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002674 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002675 return false;
2676}
2677
Andrew Trick832a6a192011-09-01 00:54:31 +00002678/// canClobberReachingPhysRegUse - True if SU would clobber one of it's
2679/// successor's explicit physregs whose definition can reach DepSU.
2680/// i.e. DepSU should not be scheduled above SU.
2681static bool canClobberReachingPhysRegUse(const SUnit *DepSU, const SUnit *SU,
2682 ScheduleDAGRRList *scheduleDAG,
2683 const TargetInstrInfo *TII,
2684 const TargetRegisterInfo *TRI) {
Craig Topper5a4bcc72012-03-08 08:22:45 +00002685 const uint16_t *ImpDefs
Andrew Trick832a6a192011-09-01 00:54:31 +00002686 = TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002687 const uint32_t *RegMask = getNodeRegMask(SU->getNode());
2688 if(!ImpDefs && !RegMask)
Andrew Trick832a6a192011-09-01 00:54:31 +00002689 return false;
2690
2691 for (SUnit::const_succ_iterator SI = SU->Succs.begin(), SE = SU->Succs.end();
2692 SI != SE; ++SI) {
2693 SUnit *SuccSU = SI->getSUnit();
2694 for (SUnit::const_pred_iterator PI = SuccSU->Preds.begin(),
2695 PE = SuccSU->Preds.end(); PI != PE; ++PI) {
2696 if (!PI->isAssignedRegDep())
2697 continue;
2698
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002699 if (RegMask && MachineOperand::clobbersPhysReg(RegMask, PI->getReg()) &&
2700 scheduleDAG->IsReachable(DepSU, PI->getSUnit()))
2701 return true;
2702
2703 if (ImpDefs)
Craig Topper5a4bcc72012-03-08 08:22:45 +00002704 for (const uint16_t *ImpDef = ImpDefs; *ImpDef; ++ImpDef)
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002705 // Return true if SU clobbers this physical register use and the
2706 // definition of the register reaches from DepSU. IsReachable queries
2707 // a topological forward sort of the DAG (following the successors).
2708 if (TRI->regsOverlap(*ImpDef, PI->getReg()) &&
2709 scheduleDAG->IsReachable(DepSU, PI->getSUnit()))
2710 return true;
Andrew Trick832a6a192011-09-01 00:54:31 +00002711 }
2712 }
2713 return false;
2714}
2715
Evan Chengf9891412007-12-20 09:25:31 +00002716/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00002717/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00002718static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00002719 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002720 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002721 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00002722 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
Craig Topper5a4bcc72012-03-08 08:22:45 +00002723 const uint16_t *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00002724 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00002725 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +00002726 SUNode = SUNode->getGluedNode()) {
Dan Gohmana366da12009-03-23 16:23:01 +00002727 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00002728 continue;
Craig Topper5a4bcc72012-03-08 08:22:45 +00002729 const uint16_t *SUImpDefs =
Dan Gohmana366da12009-03-23 16:23:01 +00002730 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002731 const uint32_t *SURegMask = getNodeRegMask(SUNode);
2732 if (!SUImpDefs && !SURegMask)
2733 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002734 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002735 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002736 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00002737 continue;
2738 if (!N->hasAnyUseOfValue(i))
2739 continue;
2740 unsigned Reg = ImpDefs[i - NumDefs];
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002741 if (SURegMask && MachineOperand::clobbersPhysReg(SURegMask, Reg))
2742 return true;
2743 if (!SUImpDefs)
2744 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002745 for (;*SUImpDefs; ++SUImpDefs) {
2746 unsigned SUReg = *SUImpDefs;
2747 if (TRI->regsOverlap(Reg, SUReg))
2748 return true;
2749 }
Evan Chengf9891412007-12-20 09:25:31 +00002750 }
2751 }
2752 return false;
2753}
2754
Dan Gohman9a658d72009-03-24 00:49:12 +00002755/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
2756/// are not handled well by the general register pressure reduction
2757/// heuristics. When presented with code like this:
2758///
2759/// N
2760/// / |
2761/// / |
2762/// U store
2763/// |
2764/// ...
2765///
2766/// the heuristics tend to push the store up, but since the
2767/// operand of the store has another use (U), this would increase
2768/// the length of that other use (the U->N edge).
2769///
2770/// This function transforms code like the above to route U's
2771/// dependence through the store when possible, like this:
2772///
2773/// N
2774/// ||
2775/// ||
2776/// store
2777/// |
2778/// U
2779/// |
2780/// ...
2781///
2782/// This results in the store being scheduled immediately
2783/// after N, which shortens the U->N live range, reducing
2784/// register pressure.
2785///
Andrew Trick9ccce772011-01-14 21:11:41 +00002786void RegReductionPQBase::PrescheduleNodesWithMultipleUses() {
Dan Gohman9a658d72009-03-24 00:49:12 +00002787 // Visit all the nodes in topological order, working top-down.
2788 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
2789 SUnit *SU = &(*SUnits)[i];
2790 // For now, only look at nodes with no data successors, such as stores.
2791 // These are especially important, due to the heuristics in
2792 // getNodePriority for nodes with no data successors.
2793 if (SU->NumSuccs != 0)
2794 continue;
2795 // For now, only look at nodes with exactly one data predecessor.
2796 if (SU->NumPreds != 1)
2797 continue;
2798 // Avoid prescheduling copies to virtual registers, which don't behave
2799 // like other nodes from the perspective of scheduling heuristics.
2800 if (SDNode *N = SU->getNode())
2801 if (N->getOpcode() == ISD::CopyToReg &&
2802 TargetRegisterInfo::isVirtualRegister
2803 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2804 continue;
2805
2806 // Locate the single data predecessor.
2807 SUnit *PredSU = 0;
2808 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
2809 EE = SU->Preds.end(); II != EE; ++II)
2810 if (!II->isCtrl()) {
2811 PredSU = II->getSUnit();
2812 break;
2813 }
2814 assert(PredSU);
2815
2816 // Don't rewrite edges that carry physregs, because that requires additional
2817 // support infrastructure.
2818 if (PredSU->hasPhysRegDefs)
2819 continue;
2820 // Short-circuit the case where SU is PredSU's only data successor.
2821 if (PredSU->NumSuccs == 1)
2822 continue;
2823 // Avoid prescheduling to copies from virtual registers, which don't behave
Andrew Trickd0548ae2011-02-04 03:18:17 +00002824 // like other nodes from the perspective of scheduling heuristics.
Dan Gohman9a658d72009-03-24 00:49:12 +00002825 if (SDNode *N = SU->getNode())
2826 if (N->getOpcode() == ISD::CopyFromReg &&
2827 TargetRegisterInfo::isVirtualRegister
2828 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2829 continue;
2830
2831 // Perform checks on the successors of PredSU.
2832 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
2833 EE = PredSU->Succs.end(); II != EE; ++II) {
2834 SUnit *PredSuccSU = II->getSUnit();
2835 if (PredSuccSU == SU) continue;
2836 // If PredSU has another successor with no data successors, for
2837 // now don't attempt to choose either over the other.
2838 if (PredSuccSU->NumSuccs == 0)
2839 goto outer_loop_continue;
2840 // Don't break physical register dependencies.
2841 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
2842 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
2843 goto outer_loop_continue;
2844 // Don't introduce graph cycles.
2845 if (scheduleDAG->IsReachable(SU, PredSuccSU))
2846 goto outer_loop_continue;
2847 }
2848
2849 // Ok, the transformation is safe and the heuristics suggest it is
2850 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00002851 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
2852 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002853 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00002854 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
2855 SDep Edge = PredSU->Succs[i];
2856 assert(!Edge.isAssignedRegDep());
2857 SUnit *SuccSU = Edge.getSUnit();
2858 if (SuccSU != SU) {
2859 Edge.setSUnit(PredSU);
2860 scheduleDAG->RemovePred(SuccSU, Edge);
2861 scheduleDAG->AddPred(SU, Edge);
2862 Edge.setSUnit(SU);
2863 scheduleDAG->AddPred(SuccSU, Edge);
2864 --i;
2865 }
2866 }
2867 outer_loop_continue:;
2868 }
2869}
2870
Evan Chengd38c22b2006-05-11 23:55:42 +00002871/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
2872/// it as a def&use operand. Add a pseudo control edge from it to the other
2873/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00002874/// first (lower in the schedule). If both nodes are two-address, favor the
2875/// one that has a CopyToReg use (more likely to be a loop induction update).
2876/// If both are two-address, but one is commutable while the other is not
2877/// commutable, favor the one that's not commutable.
Duncan Sands635e4ef2011-11-09 14:20:48 +00002878void RegReductionPQBase::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002879 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00002880 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002881 if (!SU->isTwoAddress)
2882 continue;
2883
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002884 SDNode *Node = SU->getNode();
Chris Lattner11a33812010-12-23 17:24:32 +00002885 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getGluedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002886 continue;
2887
Evan Cheng6c1414f2010-10-29 18:09:28 +00002888 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00002889 unsigned Opc = Node->getMachineOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002890 const MCInstrDesc &MCID = TII->get(Opc);
2891 unsigned NumRes = MCID.getNumDefs();
2892 unsigned NumOps = MCID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002893 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002894 if (MCID.getOperandConstraint(j+NumRes, MCOI::TIED_TO) == -1)
Dan Gohman82016c22008-11-19 02:00:32 +00002895 continue;
2896 SDNode *DU = SU->getNode()->getOperand(j).getNode();
2897 if (DU->getNodeId() == -1)
2898 continue;
2899 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
2900 if (!DUSU) continue;
2901 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
2902 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002903 if (I->isCtrl()) continue;
2904 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00002905 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00002906 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002907 // Be conservative. Ignore if nodes aren't at roughly the same
2908 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002909 if (SuccSU->getHeight() < SU->getHeight() &&
2910 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00002911 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002912 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
2913 // constrains whatever is using the copy, instead of the copy
2914 // itself. In the case that the copy is coalesced, this
2915 // preserves the intent of the pseudo two-address heurietics.
2916 while (SuccSU->Succs.size() == 1 &&
2917 SuccSU->getNode()->isMachineOpcode() &&
2918 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00002919 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002920 SuccSU = SuccSU->Succs.front().getSUnit();
2921 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00002922 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
2923 continue;
2924 // Don't constrain nodes with physical register defs if the
2925 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00002926 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00002927 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00002928 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002929 }
Dan Gohman3027bb62009-04-16 20:57:10 +00002930 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
2931 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00002932 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00002933 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
2934 SuccOpc == TargetOpcode::INSERT_SUBREG ||
2935 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00002936 continue;
Andrew Trick832a6a192011-09-01 00:54:31 +00002937 if (!canClobberReachingPhysRegUse(SuccSU, SU, scheduleDAG, TII, TRI) &&
2938 (!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00002939 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00002940 (!SU->isCommutable && SuccSU->isCommutable)) &&
2941 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00002942 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002943 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00002944 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Artificial));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002945 }
2946 }
2947 }
2948 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002949}
2950
Evan Chengd38c22b2006-05-11 23:55:42 +00002951//===----------------------------------------------------------------------===//
2952// Public Constructor Functions
2953//===----------------------------------------------------------------------===//
2954
Dan Gohmandfaf6462009-02-11 04:27:20 +00002955llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002956llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
2957 CodeGenOpt::Level OptLevel) {
Dan Gohman619ef482009-01-15 19:20:50 +00002958 const TargetMachine &TM = IS->TM;
2959 const TargetInstrInfo *TII = TM.getInstrInfo();
2960 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002961
Evan Chenga77f3d32010-07-21 06:09:07 +00002962 BURegReductionPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00002963 new BURegReductionPriorityQueue(*IS->MF, false, false, TII, TRI, 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002964 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002965 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002966 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002967}
2968
Dan Gohmandfaf6462009-02-11 04:27:20 +00002969llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002970llvm::createSourceListDAGScheduler(SelectionDAGISel *IS,
2971 CodeGenOpt::Level OptLevel) {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002972 const TargetMachine &TM = IS->TM;
2973 const TargetInstrInfo *TII = TM.getInstrInfo();
2974 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002975
Evan Chenga77f3d32010-07-21 06:09:07 +00002976 SrcRegReductionPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00002977 new SrcRegReductionPriorityQueue(*IS->MF, false, true, TII, TRI, 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002978 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Chengbdd062d2010-05-20 06:13:19 +00002979 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002980 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00002981}
2982
2983llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002984llvm::createHybridListDAGScheduler(SelectionDAGISel *IS,
2985 CodeGenOpt::Level OptLevel) {
Evan Chengbdd062d2010-05-20 06:13:19 +00002986 const TargetMachine &TM = IS->TM;
2987 const TargetInstrInfo *TII = TM.getInstrInfo();
2988 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Evan Chenga77f3d32010-07-21 06:09:07 +00002989 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00002990
Evan Chenga77f3d32010-07-21 06:09:07 +00002991 HybridBURRPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00002992 new HybridBURRPriorityQueue(*IS->MF, true, false, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002993
2994 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002995 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002996 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002997}
Evan Cheng37b740c2010-07-24 00:39:05 +00002998
2999llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003000llvm::createILPListDAGScheduler(SelectionDAGISel *IS,
3001 CodeGenOpt::Level OptLevel) {
Evan Cheng37b740c2010-07-24 00:39:05 +00003002 const TargetMachine &TM = IS->TM;
3003 const TargetInstrInfo *TII = TM.getInstrInfo();
3004 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
3005 const TargetLowering *TLI = &IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00003006
Evan Cheng37b740c2010-07-24 00:39:05 +00003007 ILPBURRPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00003008 new ILPBURRPriorityQueue(*IS->MF, true, false, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003009 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Evan Cheng37b740c2010-07-24 00:39:05 +00003010 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003011 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00003012}