blob: 13cfae7515b839483c6baf9593d64b96a344262b [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
Jim Laskey29e635d2006-08-02 12:30:23 +000018#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "ScheduleDAGSDNodes.h"
20#include "llvm/ADT/STLExtras.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000021#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000022#include "llvm/ADT/Statistic.h"
Weiming Zhao4a0b4fb2013-01-29 21:18:43 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/InlineAsm.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000030#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetLowering.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000035#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000036using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "pre-RA-sched"
39
Dan Gohmanfd227e92008-03-25 17:10:29 +000040STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000041STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000042STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000043STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000044
Jim Laskey95eda5b2006-08-01 14:21:23 +000045static RegisterScheduler
46 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000047 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000048 createBURRListDAGScheduler);
49static RegisterScheduler
Bill Wendling8cbc25d2010-01-23 10:26:57 +000050 sourceListDAGScheduler("source",
51 "Similar to list-burr but schedules in source "
52 "order when possible",
53 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000054
Evan Chengbdd062d2010-05-20 06:13:19 +000055static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000056 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000057 "Bottom-up register pressure aware list scheduling "
58 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000059 createHybridListDAGScheduler);
60
Evan Cheng37b740c2010-07-24 00:39:05 +000061static RegisterScheduler
62 ILPListDAGScheduler("list-ilp",
63 "Bottom-up register pressure aware list scheduling "
64 "which tries to balance ILP and register pressure",
65 createILPListDAGScheduler);
66
Andrew Trick47ff14b2011-01-21 05:51:33 +000067static cl::opt<bool> DisableSchedCycles(
Andrew Trickbd428ec2011-01-21 06:19:05 +000068 "disable-sched-cycles", cl::Hidden, cl::init(false),
Andrew Trick47ff14b2011-01-21 05:51:33 +000069 cl::desc("Disable cycle-level precision during preRA scheduling"));
Andrew Trick10ffc2b2010-12-24 05:03:26 +000070
Andrew Trick641e2d42011-03-05 08:00:22 +000071// Temporary sched=list-ilp flags until the heuristics are robust.
Andrew Trickbfbd9722011-04-14 05:15:06 +000072// Some options are also available under sched=list-hybrid.
Andrew Trick641e2d42011-03-05 08:00:22 +000073static cl::opt<bool> DisableSchedRegPressure(
74 "disable-sched-reg-pressure", cl::Hidden, cl::init(false),
75 cl::desc("Disable regpressure priority in sched=list-ilp"));
76static cl::opt<bool> DisableSchedLiveUses(
Andrew Trickdd017322011-03-06 00:03:32 +000077 "disable-sched-live-uses", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000078 cl::desc("Disable live use priority in sched=list-ilp"));
Andrew Trick2ad0b372011-04-07 19:54:57 +000079static cl::opt<bool> DisableSchedVRegCycle(
80 "disable-sched-vrcycle", cl::Hidden, cl::init(false),
81 cl::desc("Disable virtual register cycle interference checks"));
Andrew Trickbfbd9722011-04-14 05:15:06 +000082static cl::opt<bool> DisableSchedPhysRegJoin(
83 "disable-sched-physreg-join", cl::Hidden, cl::init(false),
84 cl::desc("Disable physreg def-use affinity"));
Andrew Trick641e2d42011-03-05 08:00:22 +000085static cl::opt<bool> DisableSchedStalls(
Andrew Trickdd017322011-03-06 00:03:32 +000086 "disable-sched-stalls", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000087 cl::desc("Disable no-stall priority in sched=list-ilp"));
88static cl::opt<bool> DisableSchedCriticalPath(
89 "disable-sched-critical-path", cl::Hidden, cl::init(false),
90 cl::desc("Disable critical path priority in sched=list-ilp"));
91static cl::opt<bool> DisableSchedHeight(
92 "disable-sched-height", cl::Hidden, cl::init(false),
93 cl::desc("Disable scheduled-height priority in sched=list-ilp"));
Evan Chengd33b2d62011-11-10 07:43:16 +000094static cl::opt<bool> Disable2AddrHack(
95 "disable-2addr-hack", cl::Hidden, cl::init(true),
96 cl::desc("Disable scheduler's two-address hack"));
Andrew Trick641e2d42011-03-05 08:00:22 +000097
98static cl::opt<int> MaxReorderWindow(
99 "max-sched-reorder", cl::Hidden, cl::init(6),
100 cl::desc("Number of instructions to allow ahead of the critical path "
101 "in sched=list-ilp"));
102
103static cl::opt<unsigned> AvgIPC(
104 "sched-avg-ipc", cl::Hidden, cl::init(1),
105 cl::desc("Average inst/cycle whan no target itinerary exists."));
106
Evan Chengd38c22b2006-05-11 23:55:42 +0000107namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +0000108//===----------------------------------------------------------------------===//
109/// ScheduleDAGRRList - The actual register reduction list scheduler
110/// implementation. This supports both top-down and bottom-up scheduling.
111///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000112class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +0000113private:
Evan Chengbdd062d2010-05-20 06:13:19 +0000114 /// NeedLatency - True if the scheduler will make use of latency information.
115 ///
116 bool NeedLatency;
117
Evan Chengd38c22b2006-05-11 23:55:42 +0000118 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +0000119 SchedulingPriorityQueue *AvailableQueue;
120
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000121 /// PendingQueue - This contains all of the instructions whose operands have
122 /// been issued, but their results are not ready yet (due to the latency of
123 /// the operation). Once the operands becomes available, the instruction is
124 /// added to the AvailableQueue.
125 std::vector<SUnit*> PendingQueue;
126
127 /// HazardRec - The hazard recognizer to use.
128 ScheduleHazardRecognizer *HazardRec;
129
Andrew Trick528fad92010-12-23 05:42:20 +0000130 /// CurCycle - The current scheduler state corresponds to this cycle.
131 unsigned CurCycle;
132
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000133 /// MinAvailableCycle - Cycle of the soonest available instruction.
134 unsigned MinAvailableCycle;
135
Andrew Trick641e2d42011-03-05 08:00:22 +0000136 /// IssueCount - Count instructions issued in this cycle
137 /// Currently valid only for bottom-up scheduling.
138 unsigned IssueCount;
139
Dan Gohmanc07f6862008-09-23 18:50:48 +0000140 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +0000141 /// that are "live". These nodes must be scheduled before any other nodes that
142 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000143 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000144 std::vector<SUnit*> LiveRegDefs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000145 std::vector<SUnit*> LiveRegGens;
Evan Cheng5924bf72007-09-25 01:54:36 +0000146
Andrew Trick7cf43612013-02-25 19:11:48 +0000147 // Collect interferences between physical register use/defs.
148 // Each interference is an SUnit and set of physical registers.
149 SmallVector<SUnit*, 4> Interferences;
150 typedef DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMapT;
151 LRegsMapT LRegsMap;
152
Dan Gohmanad2134d2008-11-25 00:52:40 +0000153 /// Topo - A topological ordering for SUnits which permits fast IsReachable
154 /// and similar queries.
155 ScheduleDAGTopologicalSort Topo;
156
Eli Friedmand5c173f2011-12-07 22:24:28 +0000157 // Hack to keep track of the inverse of FindCallSeqStart without more crazy
158 // DAG crawling.
159 DenseMap<SUnit*, SUnit*> CallSeqEndForStart;
160
Evan Chengd38c22b2006-05-11 23:55:42 +0000161public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000162 ScheduleDAGRRList(MachineFunction &mf, bool needlatency,
163 SchedulingPriorityQueue *availqueue,
164 CodeGenOpt::Level OptLevel)
Dan Gohman90fb5522011-10-20 21:44:34 +0000165 : ScheduleDAGSDNodes(mf),
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000166 NeedLatency(needlatency), AvailableQueue(availqueue), CurCycle(0),
Craig Topperc0196b12014-04-14 00:51:57 +0000167 Topo(SUnits, nullptr) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000168
169 const TargetMachine &tm = mf.getTarget();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000170 if (DisableSchedCycles || !NeedLatency)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000171 HazardRec = new ScheduleHazardRecognizer();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000172 else
Eric Christopherf047bfd2014-06-13 22:38:52 +0000173 HazardRec = tm.getInstrInfo()->CreateTargetHazardRecognizer(
174 tm.getSubtargetImpl(), this);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000175 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000176
177 ~ScheduleDAGRRList() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000178 delete HazardRec;
Evan Chengd38c22b2006-05-11 23:55:42 +0000179 delete AvailableQueue;
180 }
181
Craig Topper7b883b32014-03-08 06:31:39 +0000182 void Schedule() override;
Evan Chengd38c22b2006-05-11 23:55:42 +0000183
Andrew Trick9ccce772011-01-14 21:11:41 +0000184 ScheduleHazardRecognizer *getHazardRec() { return HazardRec; }
185
Roman Levenstein733a4d62008-03-26 11:23:38 +0000186 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000187 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
188 return Topo.IsReachable(SU, TargetSU);
189 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000190
Dan Gohman60d68442009-01-29 19:49:27 +0000191 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000192 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000193 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
194 return Topo.WillCreateCycle(SU, TargetSU);
195 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000196
Dan Gohman2d170892008-12-09 22:54:47 +0000197 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000198 /// This returns true if this is a new predecessor.
199 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000200 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000201 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000202 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000203 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000204
Dan Gohman2d170892008-12-09 22:54:47 +0000205 /// RemovePred - removes a predecessor edge from SUnit SU.
206 /// This returns true if an edge was removed.
207 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000208 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000209 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000210 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000211 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000212
Evan Chengd38c22b2006-05-11 23:55:42 +0000213private:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000214 bool isReady(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000215 return DisableSchedCycles || !AvailableQueue->hasReadyFilter() ||
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000216 AvailableQueue->isReady(SU);
217 }
218
Dan Gohman60d68442009-01-29 19:49:27 +0000219 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Andrew Tricka52f3252010-12-23 04:16:14 +0000220 void ReleasePredecessors(SUnit *SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000221 void ReleasePending();
222 void AdvanceToCycle(unsigned NextCycle);
223 void AdvancePastStalls(SUnit *SU);
224 void EmitNode(SUnit *SU);
Andrew Trick528fad92010-12-23 05:42:20 +0000225 void ScheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000226 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000227 void UnscheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000228 void RestoreHazardCheckerBottomUp();
229 void BacktrackBottomUp(SUnit*, SUnit*);
Evan Cheng8e136a92007-09-26 21:36:17 +0000230 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000231 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
232 const TargetRegisterClass*,
233 const TargetRegisterClass*,
Craig Topperb94011f2013-07-14 04:42:23 +0000234 SmallVectorImpl<SUnit*>&);
235 bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000236
Andrew Trick7cf43612013-02-25 19:11:48 +0000237 void releaseInterferences(unsigned Reg = 0);
238
Andrew Trick528fad92010-12-23 05:42:20 +0000239 SUnit *PickNodeToScheduleBottomUp();
Evan Chengd38c22b2006-05-11 23:55:42 +0000240 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000241
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000242 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000243 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000244 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000245 unsigned NumSUnits = SUnits.size();
Andrew Trick52226d42012-03-07 23:00:49 +0000246 SUnit *NewNode = newSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000247 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000248 if (NewNode->NodeNum >= NumSUnits)
249 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000250 return NewNode;
251 }
252
Roman Levenstein733a4d62008-03-26 11:23:38 +0000253 /// CreateClone - Creates a new SUnit from an existing one.
254 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000255 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000256 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000257 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000258 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000259 if (NewNode->NodeNum >= NumSUnits)
260 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000261 return NewNode;
262 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000263
Andrew Trick52226d42012-03-07 23:00:49 +0000264 /// forceUnitLatencies - Register-pressure-reducing scheduling doesn't
Evan Chengbdd062d2010-05-20 06:13:19 +0000265 /// need actual latency information but the hybrid scheduler does.
Craig Topper7b883b32014-03-08 06:31:39 +0000266 bool forceUnitLatencies() const override {
Evan Chengbdd062d2010-05-20 06:13:19 +0000267 return !NeedLatency;
268 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000269};
270} // end anonymous namespace
271
Owen Anderson96adc4a2011-06-15 23:35:18 +0000272/// GetCostForDef - Looks up the register class and cost for a given definition.
273/// Typically this just means looking up the representative register class,
Owen Andersonca2f78a2011-11-16 01:02:57 +0000274/// but for untyped values (MVT::Untyped) it means inspecting the node's
Owen Anderson96adc4a2011-06-15 23:35:18 +0000275/// opcode to determine what register class is being generated.
276static void GetCostForDef(const ScheduleDAGSDNodes::RegDefIter &RegDefPos,
277 const TargetLowering *TLI,
278 const TargetInstrInfo *TII,
279 const TargetRegisterInfo *TRI,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000280 unsigned &RegClass, unsigned &Cost,
281 const MachineFunction &MF) {
Patrik Hagglund05394352012-12-13 18:45:35 +0000282 MVT VT = RegDefPos.GetValue();
Owen Anderson96adc4a2011-06-15 23:35:18 +0000283
284 // Special handling for untyped values. These values can only come from
285 // the expansion of custom DAG-to-DAG patterns.
Owen Andersonca2f78a2011-11-16 01:02:57 +0000286 if (VT == MVT::Untyped) {
Owen Andersond1955e72011-06-21 22:54:23 +0000287 const SDNode *Node = RegDefPos.GetNode();
Owen Andersond1955e72011-06-21 22:54:23 +0000288
Weiming Zhao4a0b4fb2013-01-29 21:18:43 +0000289 // Special handling for CopyFromReg of untyped values.
290 if (!Node->isMachineOpcode() && Node->getOpcode() == ISD::CopyFromReg) {
291 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
292 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
293 RegClass = RC->getID();
294 Cost = 1;
295 return;
296 }
297
298 unsigned Opcode = Node->getMachineOpcode();
Owen Andersond1955e72011-06-21 22:54:23 +0000299 if (Opcode == TargetOpcode::REG_SEQUENCE) {
300 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
301 const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
302 RegClass = RC->getID();
303 Cost = 1;
304 return;
305 }
306
Owen Anderson96adc4a2011-06-15 23:35:18 +0000307 unsigned Idx = RegDefPos.GetIdx();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000308 const MCInstrDesc Desc = TII->get(Opcode);
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000309 const TargetRegisterClass *RC = TII->getRegClass(Desc, Idx, TRI, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +0000310 RegClass = RC->getID();
311 // FIXME: Cost arbitrarily set to 1 because there doesn't seem to be a
312 // better way to determine it.
313 Cost = 1;
314 } else {
315 RegClass = TLI->getRepRegClassFor(VT)->getID();
316 Cost = TLI->getRepRegClassCostFor(VT);
317 }
318}
Evan Chengd38c22b2006-05-11 23:55:42 +0000319
320/// Schedule - Schedule the DAG using list scheduling.
321void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000322 DEBUG(dbgs()
323 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000324 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000325
Andrew Trick528fad92010-12-23 05:42:20 +0000326 CurCycle = 0;
Andrew Trick641e2d42011-03-05 08:00:22 +0000327 IssueCount = 0;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000328 MinAvailableCycle = DisableSchedCycles ? 0 : UINT_MAX;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000329 NumLiveRegs = 0;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000330 // Allocate slots for each physical register, plus one for a special register
331 // to track the virtual resource of a calling sequence.
Craig Topperc0196b12014-04-14 00:51:57 +0000332 LiveRegDefs.resize(TRI->getNumRegs() + 1, nullptr);
333 LiveRegGens.resize(TRI->getNumRegs() + 1, nullptr);
Eli Friedmand5c173f2011-12-07 22:24:28 +0000334 CallSeqEndForStart.clear();
Andrew Trick7cf43612013-02-25 19:11:48 +0000335 assert(Interferences.empty() && LRegsMap.empty() && "stale Interferences");
Evan Cheng5924bf72007-09-25 01:54:36 +0000336
Dan Gohman04543e72008-12-23 18:36:58 +0000337 // Build the scheduling graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000338 BuildSchedGraph(nullptr);
Evan Chengd38c22b2006-05-11 23:55:42 +0000339
Evan Chengd38c22b2006-05-11 23:55:42 +0000340 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000341 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000342 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000343
Dan Gohman46520a22008-06-21 19:18:17 +0000344 AvailableQueue->initNodes(SUnits);
Andrew Trick2085a962010-12-21 22:25:04 +0000345
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000346 HazardRec->Reset();
347
Dan Gohman90fb5522011-10-20 21:44:34 +0000348 // Execute the actual scheduling loop.
349 ListScheduleBottomUp();
Andrew Trick2085a962010-12-21 22:25:04 +0000350
Evan Chengd38c22b2006-05-11 23:55:42 +0000351 AvailableQueue->releaseState();
Andrew Trickedee68c2012-03-07 05:21:40 +0000352
353 DEBUG({
354 dbgs() << "*** Final schedule ***\n";
355 dumpSchedule();
356 dbgs() << '\n';
357 });
Evan Chengafed73e2006-05-12 01:58:24 +0000358}
Evan Chengd38c22b2006-05-11 23:55:42 +0000359
360//===----------------------------------------------------------------------===//
361// Bottom-Up Scheduling
362//===----------------------------------------------------------------------===//
363
Evan Chengd38c22b2006-05-11 23:55:42 +0000364/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000365/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000366void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000367 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000368
Evan Chengd38c22b2006-05-11 23:55:42 +0000369#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000370 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000371 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000372 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000373 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000374 llvm_unreachable(nullptr);
Evan Chengd38c22b2006-05-11 23:55:42 +0000375 }
376#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000377 --PredSU->NumSuccsLeft;
378
Andrew Trick52226d42012-03-07 23:00:49 +0000379 if (!forceUnitLatencies()) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000380 // Updating predecessor's height. This is now the cycle when the
381 // predecessor can be scheduled without causing a pipeline stall.
382 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
383 }
384
Dan Gohmanb9543432009-02-10 23:27:53 +0000385 // If all the node's successors are scheduled, this node is ready
386 // to be scheduled. Ignore the special EntrySU node.
387 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000388 PredSU->isAvailable = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000389
390 unsigned Height = PredSU->getHeight();
391 if (Height < MinAvailableCycle)
392 MinAvailableCycle = Height;
393
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000394 if (isReady(PredSU)) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000395 AvailableQueue->push(PredSU);
396 }
397 // CapturePred and others may have left the node in the pending queue, avoid
398 // adding it twice.
399 else if (!PredSU->isPending) {
400 PredSU->isPending = true;
401 PendingQueue.push_back(PredSU);
402 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000403 }
404}
405
Dan Gohman198b7ff2011-11-03 21:49:52 +0000406/// IsChainDependent - Test if Outer is reachable from Inner through
407/// chain dependencies.
408static bool IsChainDependent(SDNode *Outer, SDNode *Inner,
409 unsigned NestLevel,
410 const TargetInstrInfo *TII) {
411 SDNode *N = Outer;
412 for (;;) {
413 if (N == Inner)
414 return true;
415 // For a TokenFactor, examine each operand. There may be multiple ways
416 // to get to the CALLSEQ_BEGIN, but we need to find the path with the
417 // most nesting in order to ensure that we find the corresponding match.
418 if (N->getOpcode() == ISD::TokenFactor) {
419 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
420 if (IsChainDependent(N->getOperand(i).getNode(), Inner, NestLevel, TII))
421 return true;
422 return false;
423 }
424 // Check for a lowered CALLSEQ_BEGIN or CALLSEQ_END.
425 if (N->isMachineOpcode()) {
426 if (N->getMachineOpcode() ==
427 (unsigned)TII->getCallFrameDestroyOpcode()) {
428 ++NestLevel;
429 } else if (N->getMachineOpcode() ==
430 (unsigned)TII->getCallFrameSetupOpcode()) {
431 if (NestLevel == 0)
432 return false;
433 --NestLevel;
434 }
435 }
436 // Otherwise, find the chain and continue climbing.
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 if (N->getOperand(i).getValueType() == MVT::Other) {
439 N = N->getOperand(i).getNode();
440 goto found_chain_operand;
441 }
442 return false;
443 found_chain_operand:;
444 if (N->getOpcode() == ISD::EntryToken)
445 return false;
446 }
447}
448
449/// FindCallSeqStart - Starting from the (lowered) CALLSEQ_END node, locate
450/// the corresponding (lowered) CALLSEQ_BEGIN node.
451///
452/// NestLevel and MaxNested are used in recursion to indcate the current level
453/// of nesting of CALLSEQ_BEGIN and CALLSEQ_END pairs, as well as the maximum
454/// level seen so far.
455///
456/// TODO: It would be better to give CALLSEQ_END an explicit operand to point
457/// to the corresponding CALLSEQ_BEGIN to avoid needing to search for it.
458static SDNode *
459FindCallSeqStart(SDNode *N, unsigned &NestLevel, unsigned &MaxNest,
460 const TargetInstrInfo *TII) {
461 for (;;) {
462 // For a TokenFactor, examine each operand. There may be multiple ways
463 // to get to the CALLSEQ_BEGIN, but we need to find the path with the
464 // most nesting in order to ensure that we find the corresponding match.
465 if (N->getOpcode() == ISD::TokenFactor) {
Craig Topperc0196b12014-04-14 00:51:57 +0000466 SDNode *Best = nullptr;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000467 unsigned BestMaxNest = MaxNest;
468 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
469 unsigned MyNestLevel = NestLevel;
470 unsigned MyMaxNest = MaxNest;
471 if (SDNode *New = FindCallSeqStart(N->getOperand(i).getNode(),
472 MyNestLevel, MyMaxNest, TII))
473 if (!Best || (MyMaxNest > BestMaxNest)) {
474 Best = New;
475 BestMaxNest = MyMaxNest;
476 }
477 }
478 assert(Best);
479 MaxNest = BestMaxNest;
480 return Best;
481 }
482 // Check for a lowered CALLSEQ_BEGIN or CALLSEQ_END.
483 if (N->isMachineOpcode()) {
484 if (N->getMachineOpcode() ==
485 (unsigned)TII->getCallFrameDestroyOpcode()) {
486 ++NestLevel;
487 MaxNest = std::max(MaxNest, NestLevel);
488 } else if (N->getMachineOpcode() ==
489 (unsigned)TII->getCallFrameSetupOpcode()) {
490 assert(NestLevel != 0);
491 --NestLevel;
492 if (NestLevel == 0)
493 return N;
494 }
495 }
496 // Otherwise, find the chain and continue climbing.
497 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
498 if (N->getOperand(i).getValueType() == MVT::Other) {
499 N = N->getOperand(i).getNode();
500 goto found_chain_operand;
501 }
Craig Topperc0196b12014-04-14 00:51:57 +0000502 return nullptr;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000503 found_chain_operand:;
504 if (N->getOpcode() == ISD::EntryToken)
Craig Topperc0196b12014-04-14 00:51:57 +0000505 return nullptr;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000506 }
507}
508
Andrew Trick033efdf2010-12-23 03:15:51 +0000509/// Call ReleasePred for each predecessor, then update register live def/gen.
510/// Always update LiveRegDefs for a register dependence even if the current SU
511/// also defines the register. This effectively create one large live range
512/// across a sequence of two-address node. This is important because the
513/// entire chain must be scheduled together. Example:
514///
515/// flags = (3) add
516/// flags = (2) addc flags
517/// flags = (1) addc flags
518///
519/// results in
520///
521/// LiveRegDefs[flags] = 3
Andrew Tricka52f3252010-12-23 04:16:14 +0000522/// LiveRegGens[flags] = 1
Andrew Trick033efdf2010-12-23 03:15:51 +0000523///
524/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
525/// interference on flags.
Andrew Tricka52f3252010-12-23 04:16:14 +0000526void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000527 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000528 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000529 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000530 ReleasePred(SU, &*I);
531 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000532 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000533 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000534 // clobber the register is scheduled between the predecessor and
535 // this node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000536 SUnit *RegDef = LiveRegDefs[I->getReg()]; (void)RegDef;
Andrew Trick033efdf2010-12-23 03:15:51 +0000537 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
538 "interference on register dependence");
Andrew Tricka52f3252010-12-23 04:16:14 +0000539 LiveRegDefs[I->getReg()] = I->getSUnit();
540 if (!LiveRegGens[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000541 ++NumLiveRegs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000542 LiveRegGens[I->getReg()] = SU;
Evan Cheng5924bf72007-09-25 01:54:36 +0000543 }
544 }
545 }
Dan Gohman198b7ff2011-11-03 21:49:52 +0000546
547 // If we're scheduling a lowered CALLSEQ_END, find the corresponding
548 // CALLSEQ_BEGIN. Inject an artificial physical register dependence between
549 // these nodes, to prevent other calls from being interscheduled with them.
550 unsigned CallResource = TRI->getNumRegs();
551 if (!LiveRegDefs[CallResource])
552 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode())
553 if (Node->isMachineOpcode() &&
554 Node->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
555 unsigned NestLevel = 0;
556 unsigned MaxNest = 0;
557 SDNode *N = FindCallSeqStart(Node, NestLevel, MaxNest, TII);
558
559 SUnit *Def = &SUnits[N->getNodeId()];
Eli Friedmand5c173f2011-12-07 22:24:28 +0000560 CallSeqEndForStart[Def] = SU;
561
Dan Gohman198b7ff2011-11-03 21:49:52 +0000562 ++NumLiveRegs;
563 LiveRegDefs[CallResource] = Def;
564 LiveRegGens[CallResource] = SU;
565 break;
566 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000567}
568
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000569/// Check to see if any of the pending instructions are ready to issue. If
570/// so, add them to the available queue.
571void ScheduleDAGRRList::ReleasePending() {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000572 if (DisableSchedCycles) {
Andrew Trick5ce945c2010-12-24 07:10:19 +0000573 assert(PendingQueue.empty() && "pending instrs not allowed in this mode");
574 return;
575 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000576
577 // If the available queue is empty, it is safe to reset MinAvailableCycle.
578 if (AvailableQueue->empty())
579 MinAvailableCycle = UINT_MAX;
580
581 // Check to see if any of the pending instructions are ready to issue. If
582 // so, add them to the available queue.
583 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman90fb5522011-10-20 21:44:34 +0000584 unsigned ReadyCycle = PendingQueue[i]->getHeight();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000585 if (ReadyCycle < MinAvailableCycle)
586 MinAvailableCycle = ReadyCycle;
587
588 if (PendingQueue[i]->isAvailable) {
589 if (!isReady(PendingQueue[i]))
590 continue;
591 AvailableQueue->push(PendingQueue[i]);
592 }
593 PendingQueue[i]->isPending = false;
594 PendingQueue[i] = PendingQueue.back();
595 PendingQueue.pop_back();
596 --i; --e;
597 }
598}
599
600/// Move the scheduler state forward by the specified number of Cycles.
601void ScheduleDAGRRList::AdvanceToCycle(unsigned NextCycle) {
602 if (NextCycle <= CurCycle)
603 return;
604
Andrew Trick641e2d42011-03-05 08:00:22 +0000605 IssueCount = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000606 AvailableQueue->setCurCycle(NextCycle);
Andrew Trick47ff14b2011-01-21 05:51:33 +0000607 if (!HazardRec->isEnabled()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000608 // Bypass lots of virtual calls in case of long latency.
609 CurCycle = NextCycle;
610 }
611 else {
612 for (; CurCycle != NextCycle; ++CurCycle) {
Dan Gohman90fb5522011-10-20 21:44:34 +0000613 HazardRec->RecedeCycle();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000614 }
615 }
616 // FIXME: Instead of visiting the pending Q each time, set a dirty flag on the
617 // available Q to release pending nodes at least once before popping.
618 ReleasePending();
619}
620
621/// Move the scheduler state forward until the specified node's dependents are
622/// ready and can be scheduled with no resource conflicts.
623void ScheduleDAGRRList::AdvancePastStalls(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000624 if (DisableSchedCycles)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000625 return;
626
Andrew Trickb53a00d2011-04-13 00:38:32 +0000627 // FIXME: Nodes such as CopyFromReg probably should not advance the current
628 // cycle. Otherwise, we can wrongly mask real stalls. If the non-machine node
629 // has predecessors the cycle will be advanced when they are scheduled.
630 // But given the crude nature of modeling latency though such nodes, we
631 // currently need to treat these nodes like real instructions.
632 // if (!SU->getNode() || !SU->getNode()->isMachineOpcode()) return;
633
Dan Gohman90fb5522011-10-20 21:44:34 +0000634 unsigned ReadyCycle = SU->getHeight();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000635
636 // Bump CurCycle to account for latency. We assume the latency of other
637 // available instructions may be hidden by the stall (not a full pipe stall).
638 // This updates the hazard recognizer's cycle before reserving resources for
639 // this instruction.
640 AdvanceToCycle(ReadyCycle);
641
642 // Calls are scheduled in their preceding cycle, so don't conflict with
643 // hazards from instructions after the call. EmitNode will reset the
644 // scoreboard state before emitting the call.
Dan Gohman90fb5522011-10-20 21:44:34 +0000645 if (SU->isCall)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000646 return;
647
648 // FIXME: For resource conflicts in very long non-pipelined stages, we
649 // should probably skip ahead here to avoid useless scoreboard checks.
650 int Stalls = 0;
651 while (true) {
652 ScheduleHazardRecognizer::HazardType HT =
Dan Gohman90fb5522011-10-20 21:44:34 +0000653 HazardRec->getHazardType(SU, -Stalls);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000654
655 if (HT == ScheduleHazardRecognizer::NoHazard)
656 break;
657
658 ++Stalls;
659 }
660 AdvanceToCycle(CurCycle + Stalls);
661}
662
663/// Record this SUnit in the HazardRecognizer.
664/// Does not update CurCycle.
665void ScheduleDAGRRList::EmitNode(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000666 if (!HazardRec->isEnabled())
Andrew Trickc9405662010-12-24 06:46:50 +0000667 return;
668
669 // Check for phys reg copy.
670 if (!SU->getNode())
671 return;
672
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000673 switch (SU->getNode()->getOpcode()) {
674 default:
675 assert(SU->getNode()->isMachineOpcode() &&
676 "This target-independent node should not be scheduled.");
677 break;
678 case ISD::MERGE_VALUES:
679 case ISD::TokenFactor:
Nadav Rotem7c277da2012-09-06 09:17:37 +0000680 case ISD::LIFETIME_START:
681 case ISD::LIFETIME_END:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000682 case ISD::CopyToReg:
683 case ISD::CopyFromReg:
684 case ISD::EH_LABEL:
685 // Noops don't affect the scoreboard state. Copies are likely to be
686 // removed.
687 return;
688 case ISD::INLINEASM:
689 // For inline asm, clear the pipeline state.
690 HazardRec->Reset();
691 return;
692 }
Dan Gohman90fb5522011-10-20 21:44:34 +0000693 if (SU->isCall) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000694 // Calls are scheduled with their preceding instructions. For bottom-up
695 // scheduling, clear the pipeline state before emitting.
696 HazardRec->Reset();
697 }
698
699 HazardRec->EmitInstruction(SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000700}
701
Andrew Trickb53a00d2011-04-13 00:38:32 +0000702static void resetVRegCycle(SUnit *SU);
703
Dan Gohmanb9543432009-02-10 23:27:53 +0000704/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
705/// count of its predecessors. If a predecessor pending count is zero, add it to
706/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +0000707void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU) {
Andrew Trick1b60ad62011-04-12 20:14:07 +0000708 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000709 DEBUG(SU->dump(this));
710
Evan Chengbdd062d2010-05-20 06:13:19 +0000711#ifndef NDEBUG
712 if (CurCycle < SU->getHeight())
Andrew Trickb53a00d2011-04-13 00:38:32 +0000713 DEBUG(dbgs() << " Height [" << SU->getHeight()
714 << "] pipeline stall!\n");
Evan Chengbdd062d2010-05-20 06:13:19 +0000715#endif
716
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000717 // FIXME: Do not modify node height. It may interfere with
718 // backtracking. Instead add a "ready cycle" to SUnit. Before scheduling the
Eric Christopher1b4b1e52011-03-21 18:06:21 +0000719 // node its ready cycle can aid heuristics, and after scheduling it can
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000720 // indicate the scheduled cycle.
Dan Gohmanb9543432009-02-10 23:27:53 +0000721 SU->setHeightToAtLeast(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000722
Robert Wilhelmf0cfb832013-09-28 11:46:15 +0000723 // Reserve resources for the scheduled instruction.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000724 EmitNode(SU);
725
Dan Gohmanb9543432009-02-10 23:27:53 +0000726 Sequence.push_back(SU);
727
Andrew Trick52226d42012-03-07 23:00:49 +0000728 AvailableQueue->scheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000729
Andrew Trick641e2d42011-03-05 08:00:22 +0000730 // If HazardRec is disabled, and each inst counts as one cycle, then
Andrew Trickb53a00d2011-04-13 00:38:32 +0000731 // advance CurCycle before ReleasePredecessors to avoid useless pushes to
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000732 // PendingQueue for schedulers that implement HasReadyFilter.
Andrew Trick641e2d42011-03-05 08:00:22 +0000733 if (!HazardRec->isEnabled() && AvgIPC < 2)
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000734 AdvanceToCycle(CurCycle + 1);
735
Andrew Trick033efdf2010-12-23 03:15:51 +0000736 // Update liveness of predecessors before successors to avoid treating a
737 // two-address node as a live range def.
Andrew Tricka52f3252010-12-23 04:16:14 +0000738 ReleasePredecessors(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000739
740 // Release all the implicit physical register defs that are live.
741 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
742 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000743 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
744 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
745 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
746 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000747 LiveRegDefs[I->getReg()] = nullptr;
748 LiveRegGens[I->getReg()] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000749 releaseInterferences(I->getReg());
Evan Cheng5924bf72007-09-25 01:54:36 +0000750 }
751 }
Dan Gohman198b7ff2011-11-03 21:49:52 +0000752 // Release the special call resource dependence, if this is the beginning
753 // of a call.
754 unsigned CallResource = TRI->getNumRegs();
755 if (LiveRegDefs[CallResource] == SU)
756 for (const SDNode *SUNode = SU->getNode(); SUNode;
757 SUNode = SUNode->getGluedNode()) {
758 if (SUNode->isMachineOpcode() &&
759 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameSetupOpcode()) {
760 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
761 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000762 LiveRegDefs[CallResource] = nullptr;
763 LiveRegGens[CallResource] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000764 releaseInterferences(CallResource);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000765 }
766 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000767
Andrew Trickb53a00d2011-04-13 00:38:32 +0000768 resetVRegCycle(SU);
769
Evan Chengd38c22b2006-05-11 23:55:42 +0000770 SU->isScheduled = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000771
772 // Conditions under which the scheduler should eagerly advance the cycle:
773 // (1) No available instructions
774 // (2) All pipelines full, so available instructions must have hazards.
775 //
Andrew Trickb53a00d2011-04-13 00:38:32 +0000776 // If HazardRec is disabled, the cycle was pre-advanced before calling
777 // ReleasePredecessors. In that case, IssueCount should remain 0.
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000778 //
779 // Check AvailableQueue after ReleasePredecessors in case of zero latency.
Andrew Trickb53a00d2011-04-13 00:38:32 +0000780 if (HazardRec->isEnabled() || AvgIPC > 1) {
781 if (SU->getNode() && SU->getNode()->isMachineOpcode())
782 ++IssueCount;
783 if ((HazardRec->isEnabled() && HazardRec->atIssueLimit())
784 || (!HazardRec->isEnabled() && IssueCount == AvgIPC))
785 AdvanceToCycle(CurCycle + 1);
786 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000787}
788
Evan Cheng5924bf72007-09-25 01:54:36 +0000789/// CapturePred - This does the opposite of ReleasePred. Since SU is being
790/// unscheduled, incrcease the succ left count of its predecessors. Remove
791/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000792void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000793 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000794 if (PredSU->isAvailable) {
795 PredSU->isAvailable = false;
796 if (!PredSU->isPending)
797 AvailableQueue->remove(PredSU);
798 }
799
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000800 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000801 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000802}
803
804/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
805/// its predecessor states to reflect the change.
806void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000807 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000808 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000809
Evan Cheng5924bf72007-09-25 01:54:36 +0000810 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
811 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000812 CapturePred(&*I);
Andrew Tricka52f3252010-12-23 04:16:14 +0000813 if (I->isAssignedRegDep() && SU == LiveRegGens[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000814 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000815 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000816 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000817 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000818 LiveRegDefs[I->getReg()] = nullptr;
819 LiveRegGens[I->getReg()] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000820 releaseInterferences(I->getReg());
Evan Cheng5924bf72007-09-25 01:54:36 +0000821 }
822 }
823
Dan Gohman198b7ff2011-11-03 21:49:52 +0000824 // Reclaim the special call resource dependence, if this is the beginning
825 // of a call.
826 unsigned CallResource = TRI->getNumRegs();
827 for (const SDNode *SUNode = SU->getNode(); SUNode;
828 SUNode = SUNode->getGluedNode()) {
829 if (SUNode->isMachineOpcode() &&
830 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameSetupOpcode()) {
831 ++NumLiveRegs;
832 LiveRegDefs[CallResource] = SU;
Eli Friedmand5c173f2011-12-07 22:24:28 +0000833 LiveRegGens[CallResource] = CallSeqEndForStart[SU];
Dan Gohman198b7ff2011-11-03 21:49:52 +0000834 }
835 }
836
837 // Release the special call resource dependence, if this is the end
838 // of a call.
839 if (LiveRegGens[CallResource] == SU)
840 for (const SDNode *SUNode = SU->getNode(); SUNode;
841 SUNode = SUNode->getGluedNode()) {
842 if (SUNode->isMachineOpcode() &&
843 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
844 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
845 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000846 LiveRegDefs[CallResource] = nullptr;
847 LiveRegGens[CallResource] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000848 releaseInterferences(CallResource);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000849 }
850 }
851
Evan Cheng5924bf72007-09-25 01:54:36 +0000852 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
853 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000854 if (I->isAssignedRegDep()) {
Eli Friedman0bdc0832011-12-07 22:06:02 +0000855 if (!LiveRegDefs[I->getReg()])
856 ++NumLiveRegs;
Andrew Trick033efdf2010-12-23 03:15:51 +0000857 // This becomes the nearest def. Note that an earlier def may still be
858 // pending if this is a two-address node.
859 LiveRegDefs[I->getReg()] = SU;
Craig Topperc0196b12014-04-14 00:51:57 +0000860 if (LiveRegGens[I->getReg()] == nullptr ||
Andrew Tricka52f3252010-12-23 04:16:14 +0000861 I->getSUnit()->getHeight() < LiveRegGens[I->getReg()]->getHeight())
862 LiveRegGens[I->getReg()] = I->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000863 }
864 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000865 if (SU->getHeight() < MinAvailableCycle)
866 MinAvailableCycle = SU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000867
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000868 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000869 SU->isScheduled = false;
870 SU->isAvailable = true;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000871 if (!DisableSchedCycles && AvailableQueue->hasReadyFilter()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000872 // Don't make available until backtracking is complete.
873 SU->isPending = true;
874 PendingQueue.push_back(SU);
875 }
876 else {
877 AvailableQueue->push(SU);
878 }
Andrew Trick52226d42012-03-07 23:00:49 +0000879 AvailableQueue->unscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000880}
881
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000882/// After backtracking, the hazard checker needs to be restored to a state
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000883/// corresponding the current cycle.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000884void ScheduleDAGRRList::RestoreHazardCheckerBottomUp() {
885 HazardRec->Reset();
886
887 unsigned LookAhead = std::min((unsigned)Sequence.size(),
888 HazardRec->getMaxLookAhead());
889 if (LookAhead == 0)
890 return;
891
892 std::vector<SUnit*>::const_iterator I = (Sequence.end() - LookAhead);
893 unsigned HazardCycle = (*I)->getHeight();
894 for (std::vector<SUnit*>::const_iterator E = Sequence.end(); I != E; ++I) {
895 SUnit *SU = *I;
896 for (; SU->getHeight() > HazardCycle; ++HazardCycle) {
897 HazardRec->RecedeCycle();
898 }
899 EmitNode(SU);
900 }
901}
902
Evan Cheng8e136a92007-09-26 21:36:17 +0000903/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000904/// BTCycle in order to schedule a specific node.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000905void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, SUnit *BtSU) {
906 SUnit *OldSU = Sequence.back();
907 while (true) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000908 Sequence.pop_back();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000909 // FIXME: use ready cycle instead of height
910 CurCycle = OldSU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000911 UnscheduleNodeBottomUp(OldSU);
Evan Chengbdd062d2010-05-20 06:13:19 +0000912 AvailableQueue->setCurCycle(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000913 if (OldSU == BtSU)
914 break;
915 OldSU = Sequence.back();
Evan Cheng5924bf72007-09-25 01:54:36 +0000916 }
917
Dan Gohman60d68442009-01-29 19:49:27 +0000918 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000919
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000920 RestoreHazardCheckerBottomUp();
921
Andrew Trick5ce945c2010-12-24 07:10:19 +0000922 ReleasePending();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000923
Evan Cheng1ec79b42007-09-27 07:09:03 +0000924 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000925}
926
Evan Cheng3b245872010-02-05 01:27:11 +0000927static bool isOperandOf(const SUnit *SU, SDNode *N) {
928 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +0000929 SUNode = SUNode->getGluedNode()) {
Evan Cheng3b245872010-02-05 01:27:11 +0000930 if (SUNode->isOperandOf(N))
931 return true;
932 }
933 return false;
934}
935
Evan Cheng5924bf72007-09-25 01:54:36 +0000936/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
937/// successors to the newly created node.
938SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000939 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000940 if (!N)
Craig Topperc0196b12014-04-14 00:51:57 +0000941 return nullptr;
Evan Cheng79e97132007-10-05 01:39:18 +0000942
Andrew Trickc9405662010-12-24 06:46:50 +0000943 if (SU->getNode()->getGluedNode())
Craig Topperc0196b12014-04-14 00:51:57 +0000944 return nullptr;
Andrew Trickc9405662010-12-24 06:46:50 +0000945
Evan Cheng79e97132007-10-05 01:39:18 +0000946 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000947 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000948 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000949 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000950 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000951 return nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000952 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000953 TryUnfold = true;
954 }
Evan Cheng79e97132007-10-05 01:39:18 +0000955 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000956 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000957 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000958 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000959 return nullptr;
Evan Cheng79e97132007-10-05 01:39:18 +0000960 }
961
962 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000963 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000964 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Craig Topperc0196b12014-04-14 00:51:57 +0000965 return nullptr;
Evan Cheng79e97132007-10-05 01:39:18 +0000966
Pete Cooper7c7ba1b2011-11-15 21:57:53 +0000967 // unfolding an x86 DEC64m operation results in store, dec, load which
968 // can't be handled here so quit
969 if (NewNodes.size() == 3)
Craig Topperc0196b12014-04-14 00:51:57 +0000970 return nullptr;
Pete Cooper7c7ba1b2011-11-15 21:57:53 +0000971
Evan Chengbdd062d2010-05-20 06:13:19 +0000972 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000973 assert(NewNodes.size() == 2 && "Expected a load folding node!");
974
975 N = NewNodes[1];
976 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000977 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000978 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000979 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000980 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
981 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000982 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000983
Dan Gohmane52e0892008-11-11 21:34:44 +0000984 // LoadNode may already exist. This can happen when there is another
985 // load from the same location and producing the same type of value
986 // but it has different alignment or volatileness.
987 bool isNewLoad = true;
988 SUnit *LoadSU;
989 if (LoadNode->getNodeId() != -1) {
990 LoadSU = &SUnits[LoadNode->getNodeId()];
991 isNewLoad = false;
992 } else {
993 LoadSU = CreateNewSUnit(LoadNode);
994 LoadNode->setNodeId(LoadSU->NodeNum);
Andrew Trickd0548ae2011-02-04 03:18:17 +0000995
996 InitNumRegDefsLeft(LoadSU);
Andrew Trick52226d42012-03-07 23:00:49 +0000997 computeLatency(LoadSU);
Dan Gohmane52e0892008-11-11 21:34:44 +0000998 }
999
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001000 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +00001001 assert(N->getNodeId() == -1 && "Node already inserted!");
1002 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +00001003
Evan Cheng6cc775f2011-06-28 19:10:37 +00001004 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1005 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
1006 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +00001007 NewSU->isTwoAddress = true;
1008 break;
1009 }
1010 }
Evan Cheng6cc775f2011-06-28 19:10:37 +00001011 if (MCID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +00001012 NewSU->isCommutable = true;
Andrew Trickd0548ae2011-02-04 03:18:17 +00001013
1014 InitNumRegDefsLeft(NewSU);
Andrew Trick52226d42012-03-07 23:00:49 +00001015 computeLatency(NewSU);
Evan Cheng79e97132007-10-05 01:39:18 +00001016
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001017 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +00001018 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +00001019 SmallVector<SDep, 4> ChainSuccs;
1020 SmallVector<SDep, 4> LoadPreds;
1021 SmallVector<SDep, 4> NodePreds;
1022 SmallVector<SDep, 4> NodeSuccs;
1023 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1024 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001025 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +00001026 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +00001027 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +00001028 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001029 else
Dan Gohman2d170892008-12-09 22:54:47 +00001030 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001031 }
1032 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1033 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001034 if (I->isCtrl())
1035 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001036 else
Dan Gohman2d170892008-12-09 22:54:47 +00001037 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001038 }
1039
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001040 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +00001041 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
1042 const SDep &Pred = ChainPreds[i];
1043 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +00001044 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +00001045 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001046 }
Evan Cheng79e97132007-10-05 01:39:18 +00001047 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001048 const SDep &Pred = LoadPreds[i];
1049 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +00001050 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +00001051 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +00001052 }
1053 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001054 const SDep &Pred = NodePreds[i];
1055 RemovePred(SU, Pred);
1056 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +00001057 }
1058 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001059 SDep D = NodeSuccs[i];
1060 SUnit *SuccDep = D.getSUnit();
1061 D.setSUnit(SU);
1062 RemovePred(SuccDep, D);
1063 D.setSUnit(NewSU);
1064 AddPred(SuccDep, D);
Andrew Trickd0548ae2011-02-04 03:18:17 +00001065 // Balance register pressure.
1066 if (AvailableQueue->tracksRegPressure() && SuccDep->isScheduled
1067 && !D.isCtrl() && NewSU->NumRegDefsLeft > 0)
1068 --NewSU->NumRegDefsLeft;
Evan Cheng79e97132007-10-05 01:39:18 +00001069 }
1070 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001071 SDep D = ChainSuccs[i];
1072 SUnit *SuccDep = D.getSUnit();
1073 D.setSUnit(SU);
1074 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001075 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +00001076 D.setSUnit(LoadSU);
1077 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001078 }
Andrew Trick2085a962010-12-21 22:25:04 +00001079 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001080
1081 // Add a data dependency to reflect that NewSU reads the value defined
1082 // by LoadSU.
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001083 SDep D(LoadSU, SDep::Data, 0);
1084 D.setLatency(LoadSU->Latency);
1085 AddPred(NewSU, D);
Evan Cheng79e97132007-10-05 01:39:18 +00001086
Evan Cheng91e0fc92007-12-18 08:42:10 +00001087 if (isNewLoad)
1088 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +00001089 AvailableQueue->addNode(NewSU);
1090
1091 ++NumUnfolds;
1092
1093 if (NewSU->NumSuccsLeft == 0) {
1094 NewSU->isAvailable = true;
1095 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +00001096 }
1097 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +00001098 }
1099
Evan Chengbdd062d2010-05-20 06:13:19 +00001100 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001101 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +00001102
1103 // New SUnit has the exact same predecessors.
1104 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1105 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001106 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +00001107 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +00001108
1109 // Only copy scheduled successors. Cut them from old node's successor
1110 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +00001111 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +00001112 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1113 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001114 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +00001115 continue;
Dan Gohman2d170892008-12-09 22:54:47 +00001116 SUnit *SuccSU = I->getSUnit();
1117 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +00001118 SDep D = *I;
1119 D.setSUnit(NewSU);
1120 AddPred(SuccSU, D);
1121 D.setSUnit(SU);
1122 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +00001123 }
1124 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001125 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +00001126 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +00001127
1128 AvailableQueue->updateNode(SU);
1129 AvailableQueue->addNode(NewSU);
1130
Evan Cheng1ec79b42007-09-27 07:09:03 +00001131 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +00001132 return NewSU;
1133}
1134
Evan Chengb2c42c62009-01-12 03:19:55 +00001135/// InsertCopiesAndMoveSuccs - Insert register copies and move all
1136/// scheduled successors of the given SUnit to the last copy.
1137void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Craig Topperb94011f2013-07-14 04:42:23 +00001138 const TargetRegisterClass *DestRC,
1139 const TargetRegisterClass *SrcRC,
1140 SmallVectorImpl<SUnit*> &Copies) {
Craig Topperc0196b12014-04-14 00:51:57 +00001141 SUnit *CopyFromSU = CreateNewSUnit(nullptr);
Evan Cheng8e136a92007-09-26 21:36:17 +00001142 CopyFromSU->CopySrcRC = SrcRC;
1143 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +00001144
Craig Topperc0196b12014-04-14 00:51:57 +00001145 SUnit *CopyToSU = CreateNewSUnit(nullptr);
Evan Cheng8e136a92007-09-26 21:36:17 +00001146 CopyToSU->CopySrcRC = DestRC;
1147 CopyToSU->CopyDstRC = SrcRC;
1148
1149 // Only copy scheduled successors. Cut them from old node's successor
1150 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +00001151 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +00001152 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1153 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001154 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +00001155 continue;
Dan Gohman2d170892008-12-09 22:54:47 +00001156 SUnit *SuccSU = I->getSUnit();
1157 if (SuccSU->isScheduled) {
1158 SDep D = *I;
1159 D.setSUnit(CopyToSU);
1160 AddPred(SuccSU, D);
1161 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +00001162 }
Andrew Trick13acae02011-03-23 20:42:39 +00001163 else {
1164 // Avoid scheduling the def-side copy before other successors. Otherwise
1165 // we could introduce another physreg interference on the copy and
1166 // continue inserting copies indefinitely.
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001167 AddPred(SuccSU, SDep(CopyFromSU, SDep::Artificial));
Andrew Trick13acae02011-03-23 20:42:39 +00001168 }
Evan Cheng8e136a92007-09-26 21:36:17 +00001169 }
Evan Chengb2c42c62009-01-12 03:19:55 +00001170 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +00001171 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +00001172
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001173 SDep FromDep(SU, SDep::Data, Reg);
1174 FromDep.setLatency(SU->Latency);
1175 AddPred(CopyFromSU, FromDep);
1176 SDep ToDep(CopyFromSU, SDep::Data, 0);
1177 ToDep.setLatency(CopyFromSU->Latency);
1178 AddPred(CopyToSU, ToDep);
Evan Cheng8e136a92007-09-26 21:36:17 +00001179
1180 AvailableQueue->updateNode(SU);
1181 AvailableQueue->addNode(CopyFromSU);
1182 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001183 Copies.push_back(CopyFromSU);
1184 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +00001185
Evan Chengb2c42c62009-01-12 03:19:55 +00001186 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +00001187}
1188
1189/// getPhysicalRegisterVT - Returns the ValueType of the physical register
1190/// definition of the specified node.
1191/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +00001192static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +00001193 const TargetInstrInfo *TII) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001194 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1195 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
1196 unsigned NumRes = MCID.getNumDefs();
Craig Topper5a4bcc72012-03-08 08:22:45 +00001197 for (const uint16_t *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +00001198 if (Reg == *ImpDef)
1199 break;
1200 ++NumRes;
1201 }
1202 return N->getValueType(NumRes);
1203}
1204
Evan Chengb8905c42009-03-04 01:41:49 +00001205/// CheckForLiveRegDef - Return true and update live register vector if the
1206/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001207static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +00001208 std::vector<SUnit*> &LiveRegDefs,
1209 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +00001210 SmallVectorImpl<unsigned> &LRegs,
Evan Chengb8905c42009-03-04 01:41:49 +00001211 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001212 for (MCRegAliasIterator AliasI(Reg, TRI, true); AliasI.isValid(); ++AliasI) {
Andrew Trick12acde112010-12-23 03:43:21 +00001213
1214 // Check if Ref is live.
Andrew Trick0af2e472011-06-07 00:38:12 +00001215 if (!LiveRegDefs[*AliasI]) continue;
Andrew Trick12acde112010-12-23 03:43:21 +00001216
1217 // Allow multiple uses of the same def.
Andrew Trick0af2e472011-06-07 00:38:12 +00001218 if (LiveRegDefs[*AliasI] == SU) continue;
Andrew Trick12acde112010-12-23 03:43:21 +00001219
1220 // Add Reg to the set of interfering live regs.
Andrew Trick0af2e472011-06-07 00:38:12 +00001221 if (RegAdded.insert(*AliasI)) {
Andrew Trick0af2e472011-06-07 00:38:12 +00001222 LRegs.push_back(*AliasI);
1223 }
Evan Chengb8905c42009-03-04 01:41:49 +00001224 }
Evan Chengb8905c42009-03-04 01:41:49 +00001225}
1226
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001227/// CheckForLiveRegDefMasked - Check for any live physregs that are clobbered
1228/// by RegMask, and add them to LRegs.
1229static void CheckForLiveRegDefMasked(SUnit *SU, const uint32_t *RegMask,
1230 std::vector<SUnit*> &LiveRegDefs,
1231 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +00001232 SmallVectorImpl<unsigned> &LRegs) {
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001233 // Look at all live registers. Skip Reg0 and the special CallResource.
1234 for (unsigned i = 1, e = LiveRegDefs.size()-1; i != e; ++i) {
1235 if (!LiveRegDefs[i]) continue;
1236 if (LiveRegDefs[i] == SU) continue;
1237 if (!MachineOperand::clobbersPhysReg(RegMask, i)) continue;
1238 if (RegAdded.insert(i))
1239 LRegs.push_back(i);
1240 }
1241}
1242
1243/// getNodeRegMask - Returns the register mask attached to an SDNode, if any.
1244static const uint32_t *getNodeRegMask(const SDNode *N) {
1245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1246 if (const RegisterMaskSDNode *Op =
1247 dyn_cast<RegisterMaskSDNode>(N->getOperand(i).getNode()))
1248 return Op->getRegMask();
Craig Topperc0196b12014-04-14 00:51:57 +00001249 return nullptr;
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001250}
1251
Evan Cheng5924bf72007-09-25 01:54:36 +00001252/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
1253/// scheduling of the given node to satisfy live physical register dependencies.
1254/// If the specific node is the last one that's available to schedule, do
1255/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001256bool ScheduleDAGRRList::
Craig Topperb94011f2013-07-14 04:42:23 +00001257DelayForLiveRegsBottomUp(SUnit *SU, SmallVectorImpl<unsigned> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +00001258 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +00001259 return false;
1260
Evan Chenge6f92252007-09-27 18:46:06 +00001261 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +00001262 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001263 //
1264 // If SU is the currently live definition of the same register that it uses,
1265 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +00001266 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1267 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001268 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +00001269 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
1270 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001271 }
1272
Chris Lattner11a33812010-12-23 17:24:32 +00001273 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +00001274 if (Node->getOpcode() == ISD::INLINEASM) {
1275 // Inline asm can clobber physical defs.
1276 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001277 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +00001278 --NumOps; // Ignore the glue operand.
Evan Chengb8905c42009-03-04 01:41:49 +00001279
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001280 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +00001281 unsigned Flags =
1282 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001283 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +00001284
1285 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001286 if (InlineAsm::isRegDefKind(Flags) ||
Jakob Stoklund Olesen537a3022011-06-27 04:08:33 +00001287 InlineAsm::isRegDefEarlyClobberKind(Flags) ||
1288 InlineAsm::isClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +00001289 // Check for def of register or earlyclobber register.
1290 for (; NumVals; --NumVals, ++i) {
1291 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1292 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1293 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
1294 }
1295 } else
1296 i += NumVals;
1297 }
1298 continue;
1299 }
1300
Dan Gohman072734e2008-11-13 23:24:17 +00001301 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +00001302 continue;
Dan Gohman198b7ff2011-11-03 21:49:52 +00001303 // If we're in the middle of scheduling a call, don't begin scheduling
1304 // another call. Also, don't allow any physical registers to be live across
1305 // the call.
1306 if (Node->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
1307 // Check the special calling-sequence resource.
1308 unsigned CallResource = TRI->getNumRegs();
1309 if (LiveRegDefs[CallResource]) {
1310 SDNode *Gen = LiveRegGens[CallResource]->getNode();
1311 while (SDNode *Glued = Gen->getGluedNode())
1312 Gen = Glued;
1313 if (!IsChainDependent(Gen, Node, 0, TII) && RegAdded.insert(CallResource))
1314 LRegs.push_back(CallResource);
1315 }
1316 }
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001317 if (const uint32_t *RegMask = getNodeRegMask(Node))
1318 CheckForLiveRegDefMasked(SU, RegMask, LiveRegDefs, RegAdded, LRegs);
1319
Evan Cheng6cc775f2011-06-28 19:10:37 +00001320 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
1321 if (!MCID.ImplicitDefs)
Evan Cheng5924bf72007-09-25 01:54:36 +00001322 continue;
Craig Topper5a4bcc72012-03-08 08:22:45 +00001323 for (const uint16_t *Reg = MCID.getImplicitDefs(); *Reg; ++Reg)
Evan Chengb8905c42009-03-04 01:41:49 +00001324 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001325 }
Andrew Trick2085a962010-12-21 22:25:04 +00001326
Evan Cheng5924bf72007-09-25 01:54:36 +00001327 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +00001328}
1329
Andrew Trick7cf43612013-02-25 19:11:48 +00001330void ScheduleDAGRRList::releaseInterferences(unsigned Reg) {
1331 // Add the nodes that aren't ready back onto the available list.
1332 for (unsigned i = Interferences.size(); i > 0; --i) {
1333 SUnit *SU = Interferences[i-1];
1334 LRegsMapT::iterator LRegsPos = LRegsMap.find(SU);
1335 if (Reg) {
Craig Topperb94011f2013-07-14 04:42:23 +00001336 SmallVectorImpl<unsigned> &LRegs = LRegsPos->second;
Andrew Trick7cf43612013-02-25 19:11:48 +00001337 if (std::find(LRegs.begin(), LRegs.end(), Reg) == LRegs.end())
1338 continue;
1339 }
1340 SU->isPending = false;
1341 // The interfering node may no longer be available due to backtracking.
1342 // Furthermore, it may have been made available again, in which case it is
1343 // now already in the AvailableQueue.
1344 if (SU->isAvailable && !SU->NodeQueueId) {
1345 DEBUG(dbgs() << " Repushing SU #" << SU->NodeNum << '\n');
1346 AvailableQueue->push(SU);
1347 }
1348 if (i < Interferences.size())
1349 Interferences[i-1] = Interferences.back();
1350 Interferences.pop_back();
1351 LRegsMap.erase(LRegsPos);
1352 }
1353}
1354
Andrew Trick528fad92010-12-23 05:42:20 +00001355/// Return a node that can be scheduled in this cycle. Requirements:
1356/// (1) Ready: latency has been satisfied
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001357/// (2) No Hazards: resources are available
Andrew Trick528fad92010-12-23 05:42:20 +00001358/// (3) No Interferences: may unschedule to break register interferences.
1359SUnit *ScheduleDAGRRList::PickNodeToScheduleBottomUp() {
Craig Topperc0196b12014-04-14 00:51:57 +00001360 SUnit *CurSU = AvailableQueue->empty() ? nullptr : AvailableQueue->pop();
Andrew Trick528fad92010-12-23 05:42:20 +00001361 while (CurSU) {
1362 SmallVector<unsigned, 4> LRegs;
1363 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
1364 break;
Andrew Trick0f23b762013-03-07 19:21:08 +00001365 DEBUG(dbgs() << " Interfering reg " <<
1366 (LRegs[0] == TRI->getNumRegs() ? "CallResource"
1367 : TRI->getName(LRegs[0]))
1368 << " SU #" << CurSU->NodeNum << '\n');
Andrew Trick7cf43612013-02-25 19:11:48 +00001369 std::pair<LRegsMapT::iterator, bool> LRegsPair =
1370 LRegsMap.insert(std::make_pair(CurSU, LRegs));
1371 if (LRegsPair.second) {
1372 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
1373 Interferences.push_back(CurSU);
1374 }
1375 else {
1376 assert(CurSU->isPending && "Intereferences are pending");
1377 // Update the interference with current live regs.
1378 LRegsPair.first->second = LRegs;
1379 }
Andrew Trick528fad92010-12-23 05:42:20 +00001380 CurSU = AvailableQueue->pop();
1381 }
Andrew Trick7cf43612013-02-25 19:11:48 +00001382 if (CurSU)
Andrew Trick528fad92010-12-23 05:42:20 +00001383 return CurSU;
Andrew Trick528fad92010-12-23 05:42:20 +00001384
1385 // All candidates are delayed due to live physical reg dependencies.
1386 // Try backtracking, code duplication, or inserting cross class copies
1387 // to resolve it.
1388 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1389 SUnit *TrySU = Interferences[i];
Craig Topperb94011f2013-07-14 04:42:23 +00001390 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Andrew Trick528fad92010-12-23 05:42:20 +00001391
1392 // Try unscheduling up to the point where it's safe to schedule
1393 // this node.
Craig Topperc0196b12014-04-14 00:51:57 +00001394 SUnit *BtSU = nullptr;
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001395 unsigned LiveCycle = UINT_MAX;
Andrew Trick528fad92010-12-23 05:42:20 +00001396 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
1397 unsigned Reg = LRegs[j];
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001398 if (LiveRegGens[Reg]->getHeight() < LiveCycle) {
1399 BtSU = LiveRegGens[Reg];
1400 LiveCycle = BtSU->getHeight();
1401 }
Andrew Trick528fad92010-12-23 05:42:20 +00001402 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001403 if (!WillCreateCycle(TrySU, BtSU)) {
Andrew Trick7cf43612013-02-25 19:11:48 +00001404 // BacktrackBottomUp mutates Interferences!
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001405 BacktrackBottomUp(TrySU, BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001406
1407 // Force the current node to be scheduled before the node that
1408 // requires the physical reg dep.
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001409 if (BtSU->isAvailable) {
1410 BtSU->isAvailable = false;
1411 if (!BtSU->isPending)
1412 AvailableQueue->remove(BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001413 }
Andrew Trick7cf43612013-02-25 19:11:48 +00001414 DEBUG(dbgs() << "ARTIFICIAL edge from SU(" << BtSU->NodeNum << ") to SU("
1415 << TrySU->NodeNum << ")\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001416 AddPred(TrySU, SDep(BtSU, SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001417
1418 // If one or more successors has been unscheduled, then the current
Andrew Trick7cf43612013-02-25 19:11:48 +00001419 // node is no longer available.
1420 if (!TrySU->isAvailable)
Andrew Trick528fad92010-12-23 05:42:20 +00001421 CurSU = AvailableQueue->pop();
Andrew Trick528fad92010-12-23 05:42:20 +00001422 else {
Andrew Trick7cf43612013-02-25 19:11:48 +00001423 AvailableQueue->remove(TrySU);
Andrew Trick528fad92010-12-23 05:42:20 +00001424 CurSU = TrySU;
Andrew Trick528fad92010-12-23 05:42:20 +00001425 }
Andrew Trick7cf43612013-02-25 19:11:48 +00001426 // Interferences has been mutated. We must break.
Andrew Trick528fad92010-12-23 05:42:20 +00001427 break;
1428 }
1429 }
1430
1431 if (!CurSU) {
1432 // Can't backtrack. If it's too expensive to copy the value, then try
1433 // duplicate the nodes that produces these "too expensive to copy"
1434 // values to break the dependency. In case even that doesn't work,
1435 // insert cross class copies.
1436 // If it's not too expensive, i.e. cost != -1, issue copies.
1437 SUnit *TrySU = Interferences[0];
Craig Topperb94011f2013-07-14 04:42:23 +00001438 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Andrew Trick528fad92010-12-23 05:42:20 +00001439 assert(LRegs.size() == 1 && "Can't handle this yet!");
1440 unsigned Reg = LRegs[0];
1441 SUnit *LRDef = LiveRegDefs[Reg];
1442 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
1443 const TargetRegisterClass *RC =
1444 TRI->getMinimalPhysRegClass(Reg, VT);
1445 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
1446
Evan Chengb4c6a342011-03-10 00:16:32 +00001447 // If cross copy register class is the same as RC, then it must be possible
1448 // copy the value directly. Do not try duplicate the def.
1449 // If cross copy register class is not the same as RC, then it's possible to
1450 // copy the value but it require cross register class copies and it is
1451 // expensive.
1452 // If cross copy register class is null, then it's not possible to copy
1453 // the value at all.
Craig Topperc0196b12014-04-14 00:51:57 +00001454 SUnit *NewDef = nullptr;
Evan Chengb4c6a342011-03-10 00:16:32 +00001455 if (DestRC != RC) {
Andrew Trick528fad92010-12-23 05:42:20 +00001456 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +00001457 if (!DestRC && !NewDef)
1458 report_fatal_error("Can't handle live physical register dependency!");
1459 }
Andrew Trick528fad92010-12-23 05:42:20 +00001460 if (!NewDef) {
1461 // Issue copies, these can be expensive cross register class copies.
1462 SmallVector<SUnit*, 2> Copies;
1463 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1464 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
1465 << " to SU #" << Copies.front()->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001466 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001467 NewDef = Copies.back();
1468 }
1469
1470 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
1471 << " to SU #" << TrySU->NodeNum << "\n");
1472 LiveRegDefs[Reg] = NewDef;
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001473 AddPred(NewDef, SDep(TrySU, SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001474 TrySU->isAvailable = false;
1475 CurSU = NewDef;
1476 }
Andrew Trick528fad92010-12-23 05:42:20 +00001477 assert(CurSU && "Unable to resolve live physical register dependencies!");
Andrew Trick528fad92010-12-23 05:42:20 +00001478 return CurSU;
1479}
Evan Cheng1ec79b42007-09-27 07:09:03 +00001480
Evan Chengd38c22b2006-05-11 23:55:42 +00001481/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
1482/// schedulers.
1483void ScheduleDAGRRList::ListScheduleBottomUp() {
Dan Gohmanb9543432009-02-10 23:27:53 +00001484 // Release any predecessors of the special Exit node.
Andrew Tricka52f3252010-12-23 04:16:14 +00001485 ReleasePredecessors(&ExitSU);
Dan Gohmanb9543432009-02-10 23:27:53 +00001486
Evan Chengd38c22b2006-05-11 23:55:42 +00001487 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +00001488 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +00001489 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +00001490 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
1491 RootSU->isAvailable = true;
1492 AvailableQueue->push(RootSU);
1493 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001494
1495 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001496 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +00001497 Sequence.reserve(SUnits.size());
Andrew Trick7cf43612013-02-25 19:11:48 +00001498 while (!AvailableQueue->empty() || !Interferences.empty()) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00001499 DEBUG(dbgs() << "\nExamining Available:\n";
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001500 AvailableQueue->dump(this));
1501
Andrew Trick528fad92010-12-23 05:42:20 +00001502 // Pick the best node to schedule taking all constraints into
1503 // consideration.
1504 SUnit *SU = PickNodeToScheduleBottomUp();
Evan Cheng1ec79b42007-09-27 07:09:03 +00001505
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001506 AdvancePastStalls(SU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001507
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001508 ScheduleNodeBottomUp(SU);
1509
1510 while (AvailableQueue->empty() && !PendingQueue.empty()) {
1511 // Advance the cycle to free resources. Skip ahead to the next ready SU.
1512 assert(MinAvailableCycle < UINT_MAX && "MinAvailableCycle uninitialized");
1513 AdvanceToCycle(std::max(CurCycle + 1, MinAvailableCycle));
1514 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001515 }
1516
Evan Chengd38c22b2006-05-11 23:55:42 +00001517 // Reverse the order if it is bottom up.
1518 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +00001519
Evan Chengd38c22b2006-05-11 23:55:42 +00001520#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +00001521 VerifyScheduledSequence(/*isBottomUp=*/true);
Evan Chengd38c22b2006-05-11 23:55:42 +00001522#endif
1523}
1524
1525//===----------------------------------------------------------------------===//
Andrew Trick9ccce772011-01-14 21:11:41 +00001526// RegReductionPriorityQueue Definition
Evan Chengd38c22b2006-05-11 23:55:42 +00001527//===----------------------------------------------------------------------===//
1528//
1529// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1530// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +00001531//
Evan Chengd38c22b2006-05-11 23:55:42 +00001532namespace {
Andrew Trick9ccce772011-01-14 21:11:41 +00001533class RegReductionPQBase;
Andrew Trick2085a962010-12-21 22:25:04 +00001534
Andrew Trick9ccce772011-01-14 21:11:41 +00001535struct queue_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1536 bool isReady(SUnit* SU, unsigned CurCycle) const { return true; }
1537};
1538
Andrew Trick3013b6a2011-06-15 17:16:12 +00001539#ifndef NDEBUG
1540template<class SF>
1541struct reverse_sort : public queue_sort {
1542 SF &SortFunc;
1543 reverse_sort(SF &sf) : SortFunc(sf) {}
Andrew Trick3013b6a2011-06-15 17:16:12 +00001544
1545 bool operator()(SUnit* left, SUnit* right) const {
1546 // reverse left/right rather than simply !SortFunc(left, right)
1547 // to expose different paths in the comparison logic.
1548 return SortFunc(right, left);
1549 }
1550};
1551#endif // NDEBUG
1552
Andrew Trick9ccce772011-01-14 21:11:41 +00001553/// bu_ls_rr_sort - Priority function for bottom up register pressure
1554// reduction scheduler.
1555struct bu_ls_rr_sort : public queue_sort {
1556 enum {
1557 IsBottomUp = true,
1558 HasReadyFilter = false
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001559 };
1560
Andrew Trick9ccce772011-01-14 21:11:41 +00001561 RegReductionPQBase *SPQ;
1562 bu_ls_rr_sort(RegReductionPQBase *spq) : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001563
Andrew Trick9ccce772011-01-14 21:11:41 +00001564 bool operator()(SUnit* left, SUnit* right) const;
1565};
Andrew Trick2085a962010-12-21 22:25:04 +00001566
Andrew Trick9ccce772011-01-14 21:11:41 +00001567// src_ls_rr_sort - Priority function for source order scheduler.
1568struct src_ls_rr_sort : public queue_sort {
1569 enum {
1570 IsBottomUp = true,
1571 HasReadyFilter = false
Evan Chengd38c22b2006-05-11 23:55:42 +00001572 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001573
Andrew Trick9ccce772011-01-14 21:11:41 +00001574 RegReductionPQBase *SPQ;
1575 src_ls_rr_sort(RegReductionPQBase *spq)
1576 : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001577
Andrew Trick9ccce772011-01-14 21:11:41 +00001578 bool operator()(SUnit* left, SUnit* right) const;
1579};
Andrew Trick2085a962010-12-21 22:25:04 +00001580
Andrew Trick9ccce772011-01-14 21:11:41 +00001581// hybrid_ls_rr_sort - Priority function for hybrid scheduler.
1582struct hybrid_ls_rr_sort : public queue_sort {
1583 enum {
1584 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001585 HasReadyFilter = false
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001586 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001587
Andrew Trick9ccce772011-01-14 21:11:41 +00001588 RegReductionPQBase *SPQ;
1589 hybrid_ls_rr_sort(RegReductionPQBase *spq)
1590 : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001591
Andrew Trick9ccce772011-01-14 21:11:41 +00001592 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Chenga77f3d32010-07-21 06:09:07 +00001593
Andrew Trick9ccce772011-01-14 21:11:41 +00001594 bool operator()(SUnit* left, SUnit* right) const;
1595};
1596
1597// ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1598// scheduler.
1599struct ilp_ls_rr_sort : public queue_sort {
1600 enum {
1601 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001602 HasReadyFilter = false
Evan Chengbdd062d2010-05-20 06:13:19 +00001603 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001604
Andrew Trick9ccce772011-01-14 21:11:41 +00001605 RegReductionPQBase *SPQ;
1606 ilp_ls_rr_sort(RegReductionPQBase *spq)
1607 : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001608
Andrew Trick9ccce772011-01-14 21:11:41 +00001609 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Cheng37b740c2010-07-24 00:39:05 +00001610
Andrew Trick9ccce772011-01-14 21:11:41 +00001611 bool operator()(SUnit* left, SUnit* right) const;
1612};
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001613
Andrew Trick9ccce772011-01-14 21:11:41 +00001614class RegReductionPQBase : public SchedulingPriorityQueue {
1615protected:
1616 std::vector<SUnit*> Queue;
1617 unsigned CurQueueId;
1618 bool TracksRegPressure;
Evan Cheng8ab58a22012-03-22 19:31:17 +00001619 bool SrcOrder;
Andrew Trick9ccce772011-01-14 21:11:41 +00001620
1621 // SUnits - The SUnits for the current graph.
1622 std::vector<SUnit> *SUnits;
1623
1624 MachineFunction &MF;
1625 const TargetInstrInfo *TII;
1626 const TargetRegisterInfo *TRI;
1627 const TargetLowering *TLI;
1628 ScheduleDAGRRList *scheduleDAG;
1629
1630 // SethiUllmanNumbers - The SethiUllman number for each node.
1631 std::vector<unsigned> SethiUllmanNumbers;
1632
1633 /// RegPressure - Tracking current reg pressure per register class.
1634 ///
1635 std::vector<unsigned> RegPressure;
1636
1637 /// RegLimit - Tracking the number of allocatable registers per register
1638 /// class.
1639 std::vector<unsigned> RegLimit;
1640
1641public:
1642 RegReductionPQBase(MachineFunction &mf,
1643 bool hasReadyFilter,
1644 bool tracksrp,
Evan Cheng8ab58a22012-03-22 19:31:17 +00001645 bool srcorder,
Andrew Trick9ccce772011-01-14 21:11:41 +00001646 const TargetInstrInfo *tii,
1647 const TargetRegisterInfo *tri,
1648 const TargetLowering *tli)
1649 : SchedulingPriorityQueue(hasReadyFilter),
Evan Cheng8ab58a22012-03-22 19:31:17 +00001650 CurQueueId(0), TracksRegPressure(tracksrp), SrcOrder(srcorder),
Craig Topperc0196b12014-04-14 00:51:57 +00001651 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(nullptr) {
Andrew Trick9ccce772011-01-14 21:11:41 +00001652 if (TracksRegPressure) {
1653 unsigned NumRC = TRI->getNumRegClasses();
1654 RegLimit.resize(NumRC);
1655 RegPressure.resize(NumRC);
1656 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1657 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1658 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1659 E = TRI->regclass_end(); I != E; ++I)
Cameron Zwarichdf616942011-03-07 21:56:36 +00001660 RegLimit[(*I)->getID()] = tri->getRegPressureLimit(*I, MF);
Andrew Trick9ccce772011-01-14 21:11:41 +00001661 }
1662 }
1663
1664 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1665 scheduleDAG = scheduleDag;
1666 }
1667
1668 ScheduleHazardRecognizer* getHazardRec() {
1669 return scheduleDAG->getHazardRec();
1670 }
1671
Craig Topper7b883b32014-03-08 06:31:39 +00001672 void initNodes(std::vector<SUnit> &sunits) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001673
Craig Topper7b883b32014-03-08 06:31:39 +00001674 void addNode(const SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001675
Craig Topper7b883b32014-03-08 06:31:39 +00001676 void updateNode(const SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001677
Craig Topper7b883b32014-03-08 06:31:39 +00001678 void releaseState() override {
Craig Topperc0196b12014-04-14 00:51:57 +00001679 SUnits = nullptr;
Andrew Trick9ccce772011-01-14 21:11:41 +00001680 SethiUllmanNumbers.clear();
1681 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1682 }
1683
1684 unsigned getNodePriority(const SUnit *SU) const;
1685
1686 unsigned getNodeOrdering(const SUnit *SU) const {
Andrew Trick3bd8b7a2011-03-25 06:40:55 +00001687 if (!SU->getNode()) return 0;
1688
Andrew Tricke2431c62013-05-25 03:08:10 +00001689 return SU->getNode()->getIROrder();
Andrew Trick9ccce772011-01-14 21:11:41 +00001690 }
1691
Craig Topper7b883b32014-03-08 06:31:39 +00001692 bool empty() const override { return Queue.empty(); }
Andrew Trick9ccce772011-01-14 21:11:41 +00001693
Craig Topper7b883b32014-03-08 06:31:39 +00001694 void push(SUnit *U) override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001695 assert(!U->NodeQueueId && "Node in the queue already");
1696 U->NodeQueueId = ++CurQueueId;
1697 Queue.push_back(U);
1698 }
1699
Craig Topper7b883b32014-03-08 06:31:39 +00001700 void remove(SUnit *SU) override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001701 assert(!Queue.empty() && "Queue is empty!");
1702 assert(SU->NodeQueueId != 0 && "Not in queue!");
1703 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1704 SU);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001705 if (I != std::prev(Queue.end()))
Andrew Trick9ccce772011-01-14 21:11:41 +00001706 std::swap(*I, Queue.back());
1707 Queue.pop_back();
1708 SU->NodeQueueId = 0;
1709 }
1710
Craig Topper7b883b32014-03-08 06:31:39 +00001711 bool tracksRegPressure() const override { return TracksRegPressure; }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001712
Andrew Trick9ccce772011-01-14 21:11:41 +00001713 void dumpRegPressure() const;
1714
1715 bool HighRegPressure(const SUnit *SU) const;
1716
Andrew Trick641e2d42011-03-05 08:00:22 +00001717 bool MayReduceRegPressure(SUnit *SU) const;
1718
1719 int RegPressureDiff(SUnit *SU, unsigned &LiveUses) const;
Andrew Trick9ccce772011-01-14 21:11:41 +00001720
Craig Topper7b883b32014-03-08 06:31:39 +00001721 void scheduledNode(SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001722
Craig Topper7b883b32014-03-08 06:31:39 +00001723 void unscheduledNode(SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001724
1725protected:
1726 bool canClobber(const SUnit *SU, const SUnit *Op);
Duncan Sands635e4ef2011-11-09 14:20:48 +00001727 void AddPseudoTwoAddrDeps();
Andrew Trick9ccce772011-01-14 21:11:41 +00001728 void PrescheduleNodesWithMultipleUses();
1729 void CalculateSethiUllmanNumbers();
1730};
1731
1732template<class SF>
Andrew Trick3013b6a2011-06-15 17:16:12 +00001733static SUnit *popFromQueueImpl(std::vector<SUnit*> &Q, SF &Picker) {
1734 std::vector<SUnit *>::iterator Best = Q.begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001735 for (std::vector<SUnit *>::iterator I = std::next(Q.begin()),
Andrew Trick3013b6a2011-06-15 17:16:12 +00001736 E = Q.end(); I != E; ++I)
1737 if (Picker(*Best, *I))
1738 Best = I;
1739 SUnit *V = *Best;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001740 if (Best != std::prev(Q.end()))
Andrew Trick3013b6a2011-06-15 17:16:12 +00001741 std::swap(*Best, Q.back());
1742 Q.pop_back();
1743 return V;
1744}
Andrew Trick9ccce772011-01-14 21:11:41 +00001745
Andrew Trick3013b6a2011-06-15 17:16:12 +00001746template<class SF>
1747SUnit *popFromQueue(std::vector<SUnit*> &Q, SF &Picker, ScheduleDAG *DAG) {
1748#ifndef NDEBUG
1749 if (DAG->StressSched) {
1750 reverse_sort<SF> RPicker(Picker);
1751 return popFromQueueImpl(Q, RPicker);
1752 }
1753#endif
1754 (void)DAG;
1755 return popFromQueueImpl(Q, Picker);
1756}
1757
1758template<class SF>
1759class RegReductionPriorityQueue : public RegReductionPQBase {
Andrew Trick9ccce772011-01-14 21:11:41 +00001760 SF Picker;
1761
1762public:
1763 RegReductionPriorityQueue(MachineFunction &mf,
1764 bool tracksrp,
Evan Cheng8ab58a22012-03-22 19:31:17 +00001765 bool srcorder,
Andrew Trick9ccce772011-01-14 21:11:41 +00001766 const TargetInstrInfo *tii,
1767 const TargetRegisterInfo *tri,
1768 const TargetLowering *tli)
Evan Cheng8ab58a22012-03-22 19:31:17 +00001769 : RegReductionPQBase(mf, SF::HasReadyFilter, tracksrp, srcorder,
1770 tii, tri, tli),
Andrew Trick9ccce772011-01-14 21:11:41 +00001771 Picker(this) {}
1772
Craig Topper7b883b32014-03-08 06:31:39 +00001773 bool isBottomUp() const override { return SF::IsBottomUp; }
Andrew Trick9ccce772011-01-14 21:11:41 +00001774
Craig Topper7b883b32014-03-08 06:31:39 +00001775 bool isReady(SUnit *U) const override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001776 return Picker.HasReadyFilter && Picker.isReady(U, getCurCycle());
1777 }
1778
Craig Topper7b883b32014-03-08 06:31:39 +00001779 SUnit *pop() override {
Craig Topperc0196b12014-04-14 00:51:57 +00001780 if (Queue.empty()) return nullptr;
Andrew Trick9ccce772011-01-14 21:11:41 +00001781
Andrew Trick3013b6a2011-06-15 17:16:12 +00001782 SUnit *V = popFromQueue(Queue, Picker, scheduleDAG);
Andrew Trick9ccce772011-01-14 21:11:41 +00001783 V->NodeQueueId = 0;
1784 return V;
1785 }
1786
Manman Ren19f49ac2012-09-11 22:23:19 +00001787#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Craig Topper9d74a5a2014-04-29 07:58:41 +00001788 void dump(ScheduleDAG *DAG) const override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001789 // Emulate pop() without clobbering NodeQueueIds.
1790 std::vector<SUnit*> DumpQueue = Queue;
1791 SF DumpPicker = Picker;
1792 while (!DumpQueue.empty()) {
Andrew Trick3013b6a2011-06-15 17:16:12 +00001793 SUnit *SU = popFromQueue(DumpQueue, DumpPicker, scheduleDAG);
Dan Gohman90fb5522011-10-20 21:44:34 +00001794 dbgs() << "Height " << SU->getHeight() << ": ";
Andrew Trick9ccce772011-01-14 21:11:41 +00001795 SU->dump(DAG);
1796 }
1797 }
Manman Ren742534c2012-09-06 19:06:06 +00001798#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001799};
1800
1801typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1802BURegReductionPriorityQueue;
1803
Andrew Trick9ccce772011-01-14 21:11:41 +00001804typedef RegReductionPriorityQueue<src_ls_rr_sort>
1805SrcRegReductionPriorityQueue;
1806
1807typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1808HybridBURRPriorityQueue;
1809
1810typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1811ILPBURRPriorityQueue;
1812} // end anonymous namespace
1813
1814//===----------------------------------------------------------------------===//
1815// Static Node Priority for Register Pressure Reduction
1816//===----------------------------------------------------------------------===//
Evan Chengd38c22b2006-05-11 23:55:42 +00001817
Andrew Trickbfbd9722011-04-14 05:15:06 +00001818// Check for special nodes that bypass scheduling heuristics.
1819// Currently this pushes TokenFactor nodes down, but may be used for other
1820// pseudo-ops as well.
1821//
1822// Return -1 to schedule right above left, 1 for left above right.
1823// Return 0 if no bias exists.
1824static int checkSpecialNodes(const SUnit *left, const SUnit *right) {
1825 bool LSchedLow = left->isScheduleLow;
1826 bool RSchedLow = right->isScheduleLow;
1827 if (LSchedLow != RSchedLow)
1828 return LSchedLow < RSchedLow ? 1 : -1;
1829 return 0;
1830}
1831
Dan Gohman186f65d2008-11-20 03:30:37 +00001832/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1833/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001834static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001835CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001836 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1837 if (SethiUllmanNumber != 0)
1838 return SethiUllmanNumber;
1839
1840 unsigned Extra = 0;
1841 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1842 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001843 if (I->isCtrl()) continue; // ignore chain preds
1844 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001845 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001846 if (PredSethiUllman > SethiUllmanNumber) {
1847 SethiUllmanNumber = PredSethiUllman;
1848 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001849 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001850 ++Extra;
1851 }
1852
1853 SethiUllmanNumber += Extra;
1854
1855 if (SethiUllmanNumber == 0)
1856 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001857
Evan Cheng7e4abde2008-07-02 09:23:51 +00001858 return SethiUllmanNumber;
1859}
1860
Andrew Trick9ccce772011-01-14 21:11:41 +00001861/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1862/// scheduling units.
1863void RegReductionPQBase::CalculateSethiUllmanNumbers() {
1864 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001865
Andrew Trick9ccce772011-01-14 21:11:41 +00001866 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1867 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001868}
1869
Andrew Trick9ccce772011-01-14 21:11:41 +00001870void RegReductionPQBase::addNode(const SUnit *SU) {
1871 unsigned SUSize = SethiUllmanNumbers.size();
1872 if (SUnits->size() > SUSize)
1873 SethiUllmanNumbers.resize(SUSize*2, 0);
1874 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1875}
1876
1877void RegReductionPQBase::updateNode(const SUnit *SU) {
1878 SethiUllmanNumbers[SU->NodeNum] = 0;
1879 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1880}
1881
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001882// Lower priority means schedule further down. For bottom-up scheduling, lower
1883// priority SUs are scheduled before higher priority SUs.
Andrew Trick9ccce772011-01-14 21:11:41 +00001884unsigned RegReductionPQBase::getNodePriority(const SUnit *SU) const {
1885 assert(SU->NodeNum < SethiUllmanNumbers.size());
1886 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1887 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1888 // CopyToReg should be close to its uses to facilitate coalescing and
1889 // avoid spilling.
1890 return 0;
Christian Koniged34d0e2013-03-20 15:43:00 +00001891 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1892 Opc == TargetOpcode::SUBREG_TO_REG ||
1893 Opc == TargetOpcode::INSERT_SUBREG)
1894 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1895 // close to their uses to facilitate coalescing.
1896 return 0;
Andrew Trick9ccce772011-01-14 21:11:41 +00001897 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1898 // If SU does not have a register use, i.e. it doesn't produce a value
1899 // that would be consumed (e.g. store), then it terminates a chain of
1900 // computation. Give it a large SethiUllman number so it will be
1901 // scheduled right before its predecessors that it doesn't lengthen
1902 // their live ranges.
1903 return 0xffff;
1904 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1905 // If SU does not have a register def, schedule it close to its uses
1906 // because it does not lengthen any live ranges.
1907 return 0;
Evan Cheng1355bbd2011-04-26 21:31:35 +00001908#if 1
Andrew Trick9ccce772011-01-14 21:11:41 +00001909 return SethiUllmanNumbers[SU->NodeNum];
Evan Cheng1355bbd2011-04-26 21:31:35 +00001910#else
1911 unsigned Priority = SethiUllmanNumbers[SU->NodeNum];
1912 if (SU->isCallOp) {
1913 // FIXME: This assumes all of the defs are used as call operands.
1914 int NP = (int)Priority - SU->getNode()->getNumValues();
1915 return (NP > 0) ? NP : 0;
1916 }
1917 return Priority;
1918#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001919}
1920
1921//===----------------------------------------------------------------------===//
1922// Register Pressure Tracking
1923//===----------------------------------------------------------------------===//
1924
1925void RegReductionPQBase::dumpRegPressure() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001926#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick9ccce772011-01-14 21:11:41 +00001927 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1928 E = TRI->regclass_end(); I != E; ++I) {
1929 const TargetRegisterClass *RC = *I;
1930 unsigned Id = RC->getID();
1931 unsigned RP = RegPressure[Id];
1932 if (!RP) continue;
1933 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1934 << '\n');
1935 }
Manman Ren742534c2012-09-06 19:06:06 +00001936#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001937}
1938
1939bool RegReductionPQBase::HighRegPressure(const SUnit *SU) const {
1940 if (!TLI)
1941 return false;
1942
1943 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1944 I != E; ++I) {
1945 if (I->isCtrl())
1946 continue;
1947 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00001948 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1949 // to cover the number of registers defined (they are all live).
1950 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001951 continue;
1952 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001953 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1954 RegDefPos.IsValid(); RegDefPos.Advance()) {
Owen Anderson96adc4a2011-06-15 23:35:18 +00001955 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001956 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00001957
Andrew Trick9ccce772011-01-14 21:11:41 +00001958 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1959 return true;
1960 }
1961 }
Andrew Trick9ccce772011-01-14 21:11:41 +00001962 return false;
1963}
1964
Andrew Trick641e2d42011-03-05 08:00:22 +00001965bool RegReductionPQBase::MayReduceRegPressure(SUnit *SU) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00001966 const SDNode *N = SU->getNode();
1967
1968 if (!N->isMachineOpcode() || !SU->NumSuccs)
1969 return false;
1970
1971 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1972 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00001973 MVT VT = N->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00001974 if (!N->hasAnyUseOfValue(i))
1975 continue;
1976 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1977 if (RegPressure[RCId] >= RegLimit[RCId])
1978 return true;
1979 }
1980 return false;
1981}
1982
Andrew Trick641e2d42011-03-05 08:00:22 +00001983// Compute the register pressure contribution by this instruction by count up
1984// for uses that are not live and down for defs. Only count register classes
1985// that are already under high pressure. As a side effect, compute the number of
1986// uses of registers that are already live.
1987//
1988// FIXME: This encompasses the logic in HighRegPressure and MayReduceRegPressure
1989// so could probably be factored.
1990int RegReductionPQBase::RegPressureDiff(SUnit *SU, unsigned &LiveUses) const {
1991 LiveUses = 0;
1992 int PDiff = 0;
1993 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1994 I != E; ++I) {
1995 if (I->isCtrl())
1996 continue;
1997 SUnit *PredSU = I->getSUnit();
1998 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1999 // to cover the number of registers defined (they are all live).
2000 if (PredSU->NumRegDefsLeft == 0) {
2001 if (PredSU->getNode()->isMachineOpcode())
2002 ++LiveUses;
2003 continue;
2004 }
2005 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
2006 RegDefPos.IsValid(); RegDefPos.Advance()) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002007 MVT VT = RegDefPos.GetValue();
Andrew Trick641e2d42011-03-05 08:00:22 +00002008 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2009 if (RegPressure[RCId] >= RegLimit[RCId])
2010 ++PDiff;
2011 }
2012 }
2013 const SDNode *N = SU->getNode();
2014
Eric Christopher7238cba2011-03-08 19:35:47 +00002015 if (!N || !N->isMachineOpcode() || !SU->NumSuccs)
Andrew Trick641e2d42011-03-05 08:00:22 +00002016 return PDiff;
2017
2018 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
2019 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002020 MVT VT = N->getSimpleValueType(i);
Andrew Trick641e2d42011-03-05 08:00:22 +00002021 if (!N->hasAnyUseOfValue(i))
2022 continue;
2023 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2024 if (RegPressure[RCId] >= RegLimit[RCId])
2025 --PDiff;
2026 }
2027 return PDiff;
2028}
2029
Andrew Trick52226d42012-03-07 23:00:49 +00002030void RegReductionPQBase::scheduledNode(SUnit *SU) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002031 if (!TracksRegPressure)
2032 return;
2033
Eric Christopher7238cba2011-03-08 19:35:47 +00002034 if (!SU->getNode())
2035 return;
Andrew Tricka8846e02011-03-23 20:40:18 +00002036
Andrew Trick9ccce772011-01-14 21:11:41 +00002037 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2038 I != E; ++I) {
2039 if (I->isCtrl())
2040 continue;
2041 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00002042 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
2043 // to cover the number of registers defined (they are all live).
2044 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002045 continue;
2046 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00002047 // FIXME: The ScheduleDAG currently loses information about which of a
2048 // node's values is consumed by each dependence. Consequently, if the node
2049 // defines multiple register classes, we don't know which to pressurize
2050 // here. Instead the following loop consumes the register defs in an
2051 // arbitrary order. At least it handles the common case of clustered loads
2052 // to the same class. For precise liveness, each SDep needs to indicate the
2053 // result number. But that tightly couples the ScheduleDAG with the
2054 // SelectionDAG making updates tricky. A simpler hack would be to attach a
2055 // value type or register class to SDep.
2056 //
2057 // The most important aspect of register tracking is balancing the increase
2058 // here with the reduction further below. Note that this SU may use multiple
2059 // defs in PredSU. The can't be determined here, but we've already
2060 // compensated by reducing NumRegDefsLeft in PredSU during
2061 // ScheduleDAGSDNodes::AddSchedEdges.
2062 --PredSU->NumRegDefsLeft;
2063 unsigned SkipRegDefs = PredSU->NumRegDefsLeft;
2064 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
2065 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
2066 if (SkipRegDefs)
Andrew Trick9ccce772011-01-14 21:11:41 +00002067 continue;
Owen Anderson96adc4a2011-06-15 23:35:18 +00002068
2069 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002070 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00002071 RegPressure[RCId] += Cost;
Andrew Trickd0548ae2011-02-04 03:18:17 +00002072 break;
Andrew Trick9ccce772011-01-14 21:11:41 +00002073 }
2074 }
2075
Andrew Trickd0548ae2011-02-04 03:18:17 +00002076 // We should have this assert, but there may be dead SDNodes that never
2077 // materialize as SUnits, so they don't appear to generate liveness.
2078 //assert(SU->NumRegDefsLeft == 0 && "not all regdefs have scheduled uses");
2079 int SkipRegDefs = (int)SU->NumRegDefsLeft;
2080 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(SU, scheduleDAG);
2081 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
2082 if (SkipRegDefs > 0)
2083 continue;
Owen Anderson96adc4a2011-06-15 23:35:18 +00002084 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002085 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00002086 if (RegPressure[RCId] < Cost) {
Andrew Trickd0548ae2011-02-04 03:18:17 +00002087 // Register pressure tracking is imprecise. This can happen. But we try
2088 // hard not to let it happen because it likely results in poor scheduling.
2089 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") has too many regdefs\n");
2090 RegPressure[RCId] = 0;
2091 }
2092 else {
Owen Anderson96adc4a2011-06-15 23:35:18 +00002093 RegPressure[RCId] -= Cost;
Andrew Trick9ccce772011-01-14 21:11:41 +00002094 }
2095 }
Andrew Trick9ccce772011-01-14 21:11:41 +00002096 dumpRegPressure();
2097}
2098
Andrew Trick52226d42012-03-07 23:00:49 +00002099void RegReductionPQBase::unscheduledNode(SUnit *SU) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002100 if (!TracksRegPressure)
2101 return;
2102
2103 const SDNode *N = SU->getNode();
Eric Christopher7238cba2011-03-08 19:35:47 +00002104 if (!N) return;
Andrew Tricka8846e02011-03-23 20:40:18 +00002105
Andrew Trick9ccce772011-01-14 21:11:41 +00002106 if (!N->isMachineOpcode()) {
2107 if (N->getOpcode() != ISD::CopyToReg)
2108 return;
2109 } else {
2110 unsigned Opc = N->getMachineOpcode();
2111 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2112 Opc == TargetOpcode::INSERT_SUBREG ||
2113 Opc == TargetOpcode::SUBREG_TO_REG ||
2114 Opc == TargetOpcode::REG_SEQUENCE ||
2115 Opc == TargetOpcode::IMPLICIT_DEF)
2116 return;
2117 }
2118
2119 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2120 I != E; ++I) {
2121 if (I->isCtrl())
2122 continue;
2123 SUnit *PredSU = I->getSUnit();
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002124 // NumSuccsLeft counts all deps. Don't compare it with NumSuccs which only
2125 // counts data deps.
2126 if (PredSU->NumSuccsLeft != PredSU->Succs.size())
Andrew Trick9ccce772011-01-14 21:11:41 +00002127 continue;
2128 const SDNode *PN = PredSU->getNode();
2129 if (!PN->isMachineOpcode()) {
2130 if (PN->getOpcode() == ISD::CopyFromReg) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002131 MVT VT = PN->getSimpleValueType(0);
Andrew Trick9ccce772011-01-14 21:11:41 +00002132 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2133 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2134 }
2135 continue;
2136 }
2137 unsigned POpc = PN->getMachineOpcode();
2138 if (POpc == TargetOpcode::IMPLICIT_DEF)
2139 continue;
Andrew Trick31f25bc2011-06-27 18:01:20 +00002140 if (POpc == TargetOpcode::EXTRACT_SUBREG ||
2141 POpc == TargetOpcode::INSERT_SUBREG ||
2142 POpc == TargetOpcode::SUBREG_TO_REG) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002143 MVT VT = PN->getSimpleValueType(0);
Andrew Trick9ccce772011-01-14 21:11:41 +00002144 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2145 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2146 continue;
2147 }
2148 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
2149 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002150 MVT VT = PN->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00002151 if (!PN->hasAnyUseOfValue(i))
2152 continue;
2153 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2154 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
2155 // Register pressure tracking is imprecise. This can happen.
2156 RegPressure[RCId] = 0;
2157 else
2158 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
2159 }
2160 }
2161
2162 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
2163 // may transfer data dependencies to CopyToReg.
2164 if (SU->NumSuccs && N->isMachineOpcode()) {
2165 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
2166 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002167 MVT VT = N->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00002168 if (VT == MVT::Glue || VT == MVT::Other)
2169 continue;
2170 if (!N->hasAnyUseOfValue(i))
2171 continue;
2172 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2173 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2174 }
2175 }
2176
2177 dumpRegPressure();
2178}
2179
2180//===----------------------------------------------------------------------===//
2181// Dynamic Node Priority for Register Pressure Reduction
2182//===----------------------------------------------------------------------===//
2183
Evan Chengb9e3db62007-03-14 22:43:40 +00002184/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00002185/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00002186static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002187 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00002188 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00002189 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00002190 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002191 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00002192 // If there are bunch of CopyToRegs stacked up, they should be considered
2193 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00002194 if (I->getSUnit()->getNode() &&
2195 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002196 Height = closestSucc(I->getSUnit())+1;
2197 if (Height > MaxHeight)
2198 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00002199 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002200 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00002201}
2202
Evan Cheng61bc51e2007-12-20 02:22:36 +00002203/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00002204/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00002205static unsigned calcMaxScratches(const SUnit *SU) {
2206 unsigned Scratches = 0;
2207 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00002208 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002209 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00002210 Scratches++;
2211 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00002212 return Scratches;
2213}
2214
Andrew Trickb53a00d2011-04-13 00:38:32 +00002215/// hasOnlyLiveInOpers - Return true if SU has only value predecessors that are
2216/// CopyFromReg from a virtual register.
2217static bool hasOnlyLiveInOpers(const SUnit *SU) {
2218 bool RetVal = false;
2219 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2220 I != E; ++I) {
2221 if (I->isCtrl()) continue;
2222 const SUnit *PredSU = I->getSUnit();
2223 if (PredSU->getNode() &&
2224 PredSU->getNode()->getOpcode() == ISD::CopyFromReg) {
2225 unsigned Reg =
2226 cast<RegisterSDNode>(PredSU->getNode()->getOperand(1))->getReg();
2227 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2228 RetVal = true;
2229 continue;
2230 }
2231 }
2232 return false;
2233 }
2234 return RetVal;
2235}
2236
2237/// hasOnlyLiveOutUses - Return true if SU has only value successors that are
Evan Cheng6c1414f2010-10-29 18:09:28 +00002238/// CopyToReg to a virtual register. This SU def is probably a liveout and
2239/// it has no other use. It should be scheduled closer to the terminator.
2240static bool hasOnlyLiveOutUses(const SUnit *SU) {
2241 bool RetVal = false;
2242 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
2243 I != E; ++I) {
2244 if (I->isCtrl()) continue;
2245 const SUnit *SuccSU = I->getSUnit();
2246 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
2247 unsigned Reg =
2248 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
2249 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2250 RetVal = true;
2251 continue;
2252 }
2253 }
2254 return false;
2255 }
2256 return RetVal;
2257}
2258
Andrew Trickb53a00d2011-04-13 00:38:32 +00002259// Set isVRegCycle for a node with only live in opers and live out uses. Also
2260// set isVRegCycle for its CopyFromReg operands.
2261//
2262// This is only relevant for single-block loops, in which case the VRegCycle
2263// node is likely an induction variable in which the operand and target virtual
2264// registers should be coalesced (e.g. pre/post increment values). Setting the
2265// isVRegCycle flag helps the scheduler prioritize other uses of the same
2266// CopyFromReg so that this node becomes the virtual register "kill". This
2267// avoids interference between the values live in and out of the block and
2268// eliminates a copy inside the loop.
2269static void initVRegCycle(SUnit *SU) {
2270 if (DisableSchedVRegCycle)
2271 return;
2272
2273 if (!hasOnlyLiveInOpers(SU) || !hasOnlyLiveOutUses(SU))
2274 return;
2275
2276 DEBUG(dbgs() << "VRegCycle: SU(" << SU->NodeNum << ")\n");
2277
2278 SU->isVRegCycle = true;
2279
2280 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Andrew Trickc5dd24a2011-04-12 19:54:36 +00002281 I != E; ++I) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00002282 if (I->isCtrl()) continue;
2283 I->getSUnit()->isVRegCycle = true;
Andrew Trickc5dd24a2011-04-12 19:54:36 +00002284 }
Andrew Trick1b60ad62011-04-12 20:14:07 +00002285}
2286
Andrew Trickb53a00d2011-04-13 00:38:32 +00002287// After scheduling the definition of a VRegCycle, clear the isVRegCycle flag of
2288// CopyFromReg operands. We should no longer penalize other uses of this VReg.
2289static void resetVRegCycle(SUnit *SU) {
2290 if (!SU->isVRegCycle)
2291 return;
2292
2293 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
2294 I != E; ++I) {
Andrew Trick1b60ad62011-04-12 20:14:07 +00002295 if (I->isCtrl()) continue; // ignore chain preds
Andrew Trickb53a00d2011-04-13 00:38:32 +00002296 SUnit *PredSU = I->getSUnit();
2297 if (PredSU->isVRegCycle) {
2298 assert(PredSU->getNode()->getOpcode() == ISD::CopyFromReg &&
2299 "VRegCycle def must be CopyFromReg");
2300 I->getSUnit()->isVRegCycle = 0;
2301 }
2302 }
2303}
2304
2305// Return true if this SUnit uses a CopyFromReg node marked as a VRegCycle. This
2306// means a node that defines the VRegCycle has not been scheduled yet.
2307static bool hasVRegCycleUse(const SUnit *SU) {
2308 // If this SU also defines the VReg, don't hoist it as a "use".
2309 if (SU->isVRegCycle)
2310 return false;
2311
2312 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
2313 I != E; ++I) {
2314 if (I->isCtrl()) continue; // ignore chain preds
2315 if (I->getSUnit()->isVRegCycle &&
2316 I->getSUnit()->getNode()->getOpcode() == ISD::CopyFromReg) {
2317 DEBUG(dbgs() << " VReg cycle use: SU (" << SU->NodeNum << ")\n");
2318 return true;
Andrew Trick2ad0b372011-04-07 19:54:57 +00002319 }
2320 }
2321 return false;
2322}
2323
Andrew Trick9ccce772011-01-14 21:11:41 +00002324// Check for either a dependence (latency) or resource (hazard) stall.
2325//
2326// Note: The ScheduleHazardRecognizer interface requires a non-const SU.
2327static bool BUHasStall(SUnit *SU, int Height, RegReductionPQBase *SPQ) {
2328 if ((int)SPQ->getCurCycle() < Height) return true;
2329 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2330 != ScheduleHazardRecognizer::NoHazard)
2331 return true;
2332 return false;
2333}
2334
2335// Return -1 if left has higher priority, 1 if right has higher priority.
2336// Return 0 if latency-based priority is equivalent.
2337static int BUCompareLatency(SUnit *left, SUnit *right, bool checkPref,
2338 RegReductionPQBase *SPQ) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00002339 // Scheduling an instruction that uses a VReg whose postincrement has not yet
2340 // been scheduled will induce a copy. Model this as an extra cycle of latency.
2341 int LPenalty = hasVRegCycleUse(left) ? 1 : 0;
2342 int RPenalty = hasVRegCycleUse(right) ? 1 : 0;
2343 int LHeight = (int)left->getHeight() + LPenalty;
2344 int RHeight = (int)right->getHeight() + RPenalty;
Andrew Trick9ccce772011-01-14 21:11:41 +00002345
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002346 bool LStall = (!checkPref || left->SchedulingPref == Sched::ILP) &&
Andrew Trick9ccce772011-01-14 21:11:41 +00002347 BUHasStall(left, LHeight, SPQ);
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002348 bool RStall = (!checkPref || right->SchedulingPref == Sched::ILP) &&
Andrew Trick9ccce772011-01-14 21:11:41 +00002349 BUHasStall(right, RHeight, SPQ);
2350
2351 // If scheduling one of the node will cause a pipeline stall, delay it.
2352 // If scheduling either one of the node will cause a pipeline stall, sort
2353 // them according to their height.
2354 if (LStall) {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002355 if (!RStall)
Andrew Trick9ccce772011-01-14 21:11:41 +00002356 return 1;
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002357 if (LHeight != RHeight)
Andrew Trick9ccce772011-01-14 21:11:41 +00002358 return LHeight > RHeight ? 1 : -1;
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002359 } else if (RStall)
Andrew Trick9ccce772011-01-14 21:11:41 +00002360 return -1;
2361
Andrew Trick47ff14b2011-01-21 05:51:33 +00002362 // If either node is scheduling for latency, sort them by height/depth
Andrew Trick9ccce772011-01-14 21:11:41 +00002363 // and latency.
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002364 if (!checkPref || (left->SchedulingPref == Sched::ILP ||
2365 right->SchedulingPref == Sched::ILP)) {
Andrew Tricka88d46e2012-06-05 03:44:34 +00002366 // If neither instruction stalls (!LStall && !RStall) and HazardRecognizer
2367 // is enabled, grouping instructions by cycle, then its height is already
2368 // covered so only its depth matters. We also reach this point if both stall
2369 // but have the same height.
2370 if (!SPQ->getHazardRec()->isEnabled()) {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002371 if (LHeight != RHeight)
Andrew Trick9ccce772011-01-14 21:11:41 +00002372 return LHeight > RHeight ? 1 : -1;
2373 }
Andrew Tricka88d46e2012-06-05 03:44:34 +00002374 int LDepth = left->getDepth() - LPenalty;
2375 int RDepth = right->getDepth() - RPenalty;
2376 if (LDepth != RDepth) {
2377 DEBUG(dbgs() << " Comparing latency of SU (" << left->NodeNum
2378 << ") depth " << LDepth << " vs SU (" << right->NodeNum
2379 << ") depth " << RDepth << "\n");
2380 return LDepth < RDepth ? 1 : -1;
Andrew Trick47ff14b2011-01-21 05:51:33 +00002381 }
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002382 if (left->Latency != right->Latency)
Andrew Trick9ccce772011-01-14 21:11:41 +00002383 return left->Latency > right->Latency ? 1 : -1;
2384 }
2385 return 0;
2386}
2387
2388static bool BURRSort(SUnit *left, SUnit *right, RegReductionPQBase *SPQ) {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002389 // Schedule physical register definitions close to their use. This is
2390 // motivated by microarchitectures that can fuse cmp+jump macro-ops. But as
2391 // long as shortening physreg live ranges is generally good, we can defer
2392 // creating a subtarget hook.
2393 if (!DisableSchedPhysRegJoin) {
2394 bool LHasPhysReg = left->hasPhysRegDefs;
2395 bool RHasPhysReg = right->hasPhysRegDefs;
2396 if (LHasPhysReg != RHasPhysReg) {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002397 #ifndef NDEBUG
Craig Topper06b3b662013-07-15 08:02:13 +00002398 static const char *const PhysRegMsg[] = { " has no physreg",
2399 " defines a physreg" };
Andrew Trickbfbd9722011-04-14 05:15:06 +00002400 #endif
2401 DEBUG(dbgs() << " SU (" << left->NodeNum << ") "
2402 << PhysRegMsg[LHasPhysReg] << " SU(" << right->NodeNum << ") "
2403 << PhysRegMsg[RHasPhysReg] << "\n");
2404 return LHasPhysReg < RHasPhysReg;
2405 }
2406 }
2407
Evan Cheng2f647542011-04-26 04:57:37 +00002408 // Prioritize by Sethi-Ulmann number and push CopyToReg nodes down.
Evan Cheng6730f032007-01-08 23:55:53 +00002409 unsigned LPriority = SPQ->getNodePriority(left);
2410 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng1355bbd2011-04-26 21:31:35 +00002411
2412 // Be really careful about hoisting call operands above previous calls.
2413 // Only allows it if it would reduce register pressure.
2414 if (left->isCall && right->isCallOp) {
2415 unsigned RNumVals = right->getNode()->getNumValues();
2416 RPriority = (RPriority > RNumVals) ? (RPriority - RNumVals) : 0;
2417 }
2418 if (right->isCall && left->isCallOp) {
2419 unsigned LNumVals = left->getNode()->getNumValues();
2420 LPriority = (LPriority > LNumVals) ? (LPriority - LNumVals) : 0;
2421 }
2422
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002423 if (LPriority != RPriority)
Evan Cheng73bdf042008-03-01 00:39:47 +00002424 return LPriority > RPriority;
Andrew Trick52b3e382011-03-08 01:51:56 +00002425
Evan Cheng1355bbd2011-04-26 21:31:35 +00002426 // One or both of the nodes are calls and their sethi-ullman numbers are the
2427 // same, then keep source order.
2428 if (left->isCall || right->isCall) {
2429 unsigned LOrder = SPQ->getNodeOrdering(left);
2430 unsigned ROrder = SPQ->getNodeOrdering(right);
2431
2432 // Prefer an ordering where the lower the non-zero order number, the higher
2433 // the preference.
2434 if ((LOrder || ROrder) && LOrder != ROrder)
2435 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2436 }
2437
Evan Cheng73bdf042008-03-01 00:39:47 +00002438 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
2439 // e.g.
2440 // t1 = op t2, c1
2441 // t3 = op t4, c2
2442 //
2443 // and the following instructions are both ready.
2444 // t2 = op c3
2445 // t4 = op c4
2446 //
2447 // Then schedule t2 = op first.
2448 // i.e.
2449 // t4 = op c4
2450 // t2 = op c3
2451 // t1 = op t2, c1
2452 // t3 = op t4, c2
2453 //
2454 // This creates more short live intervals.
2455 unsigned LDist = closestSucc(left);
2456 unsigned RDist = closestSucc(right);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002457 if (LDist != RDist)
Evan Cheng73bdf042008-03-01 00:39:47 +00002458 return LDist < RDist;
2459
Evan Cheng3a14efa2009-02-12 08:59:45 +00002460 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00002461 unsigned LScratch = calcMaxScratches(left);
2462 unsigned RScratch = calcMaxScratches(right);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002463 if (LScratch != RScratch)
Evan Cheng73bdf042008-03-01 00:39:47 +00002464 return LScratch > RScratch;
2465
Evan Cheng1355bbd2011-04-26 21:31:35 +00002466 // Comparing latency against a call makes little sense unless the node
2467 // is register pressure-neutral.
2468 if ((left->isCall && RPriority > 0) || (right->isCall && LPriority > 0))
2469 return (left->NodeQueueId > right->NodeQueueId);
2470
2471 // Do not compare latencies when one or both of the nodes are calls.
2472 if (!DisableSchedCycles &&
2473 !(left->isCall || right->isCall)) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002474 int result = BUCompareLatency(left, right, false /*checkPref*/, SPQ);
2475 if (result != 0)
2476 return result > 0;
2477 }
2478 else {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002479 if (left->getHeight() != right->getHeight())
Andrew Trick9ccce772011-01-14 21:11:41 +00002480 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00002481
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002482 if (left->getDepth() != right->getDepth())
Andrew Trick9ccce772011-01-14 21:11:41 +00002483 return left->getDepth() < right->getDepth();
2484 }
Evan Cheng73bdf042008-03-01 00:39:47 +00002485
Andrew Trick2085a962010-12-21 22:25:04 +00002486 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002487 "NodeQueueId cannot be zero");
2488 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002489}
2490
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002491// Bottom up
Andrew Trick9ccce772011-01-14 21:11:41 +00002492bool bu_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002493 if (int res = checkSpecialNodes(left, right))
2494 return res > 0;
2495
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002496 return BURRSort(left, right, SPQ);
2497}
2498
2499// Source order, otherwise bottom up.
Andrew Trick9ccce772011-01-14 21:11:41 +00002500bool src_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002501 if (int res = checkSpecialNodes(left, right))
2502 return res > 0;
2503
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002504 unsigned LOrder = SPQ->getNodeOrdering(left);
2505 unsigned ROrder = SPQ->getNodeOrdering(right);
2506
2507 // Prefer an ordering where the lower the non-zero order number, the higher
2508 // the preference.
2509 if ((LOrder || ROrder) && LOrder != ROrder)
2510 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2511
2512 return BURRSort(left, right, SPQ);
2513}
2514
Andrew Trick9ccce772011-01-14 21:11:41 +00002515// If the time between now and when the instruction will be ready can cover
2516// the spill code, then avoid adding it to the ready queue. This gives long
2517// stalls highest priority and allows hoisting across calls. It should also
2518// speed up processing the available queue.
2519bool hybrid_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
2520 static const unsigned ReadyDelay = 3;
2521
2522 if (SPQ->MayReduceRegPressure(SU)) return true;
2523
2524 if (SU->getHeight() > (CurCycle + ReadyDelay)) return false;
2525
2526 if (SPQ->getHazardRec()->getHazardType(SU, -ReadyDelay)
2527 != ScheduleHazardRecognizer::NoHazard)
2528 return false;
2529
2530 return true;
2531}
2532
2533// Return true if right should be scheduled with higher priority than left.
2534bool hybrid_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002535 if (int res = checkSpecialNodes(left, right))
2536 return res > 0;
2537
Evan Chengdebf9c52010-11-03 00:45:17 +00002538 if (left->isCall || right->isCall)
2539 // No way to compute latency of calls.
2540 return BURRSort(left, right, SPQ);
2541
Evan Chenge6d6c5d2010-07-26 21:49:07 +00002542 bool LHigh = SPQ->HighRegPressure(left);
2543 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00002544 // Avoid causing spills. If register pressure is high, schedule for
2545 // register pressure reduction.
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002546 if (LHigh && !RHigh) {
2547 DEBUG(dbgs() << " pressure SU(" << left->NodeNum << ") > SU("
2548 << right->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002549 return true;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002550 }
2551 else if (!LHigh && RHigh) {
2552 DEBUG(dbgs() << " pressure SU(" << right->NodeNum << ") > SU("
2553 << left->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002554 return false;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002555 }
Andrew Trickb53a00d2011-04-13 00:38:32 +00002556 if (!LHigh && !RHigh) {
2557 int result = BUCompareLatency(left, right, true /*checkPref*/, SPQ);
2558 if (result != 0)
2559 return result > 0;
Evan Chengcc2efe12010-05-28 23:26:21 +00002560 }
Evan Chengbdd062d2010-05-20 06:13:19 +00002561 return BURRSort(left, right, SPQ);
2562}
2563
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002564// Schedule as many instructions in each cycle as possible. So don't make an
2565// instruction available unless it is ready in the current cycle.
2566bool ilp_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00002567 if (SU->getHeight() > CurCycle) return false;
2568
2569 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2570 != ScheduleHazardRecognizer::NoHazard)
2571 return false;
2572
Andrew Trickc88b7ec2011-03-04 02:03:45 +00002573 return true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002574}
2575
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002576static bool canEnableCoalescing(SUnit *SU) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002577 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
2578 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
2579 // CopyToReg should be close to its uses to facilitate coalescing and
2580 // avoid spilling.
2581 return true;
2582
Christian Koniged34d0e2013-03-20 15:43:00 +00002583 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2584 Opc == TargetOpcode::SUBREG_TO_REG ||
2585 Opc == TargetOpcode::INSERT_SUBREG)
2586 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
2587 // close to their uses to facilitate coalescing.
2588 return true;
Andrew Trick52b3e382011-03-08 01:51:56 +00002589
2590 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
2591 // If SU does not have a register def, schedule it close to its uses
2592 // because it does not lengthen any live ranges.
2593 return true;
2594
2595 return false;
2596}
2597
Andrew Trickb8390b72011-03-05 08:04:11 +00002598// list-ilp is currently an experimental scheduler that allows various
2599// heuristics to be enabled prior to the normal register reduction logic.
Andrew Trick9ccce772011-01-14 21:11:41 +00002600bool ilp_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002601 if (int res = checkSpecialNodes(left, right))
2602 return res > 0;
2603
Evan Chengdebf9c52010-11-03 00:45:17 +00002604 if (left->isCall || right->isCall)
2605 // No way to compute latency of calls.
2606 return BURRSort(left, right, SPQ);
2607
Andrew Trick52b3e382011-03-08 01:51:56 +00002608 unsigned LLiveUses = 0, RLiveUses = 0;
2609 int LPDiff = 0, RPDiff = 0;
2610 if (!DisableSchedRegPressure || !DisableSchedLiveUses) {
2611 LPDiff = SPQ->RegPressureDiff(left, LLiveUses);
2612 RPDiff = SPQ->RegPressureDiff(right, RLiveUses);
2613 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002614 if (!DisableSchedRegPressure && LPDiff != RPDiff) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002615 DEBUG(dbgs() << "RegPressureDiff SU(" << left->NodeNum << "): " << LPDiff
2616 << " != SU(" << right->NodeNum << "): " << RPDiff << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002617 return LPDiff > RPDiff;
2618 }
2619
Andrew Trick52b3e382011-03-08 01:51:56 +00002620 if (!DisableSchedRegPressure && (LPDiff > 0 || RPDiff > 0)) {
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002621 bool LReduce = canEnableCoalescing(left);
2622 bool RReduce = canEnableCoalescing(right);
Andrew Trick52b3e382011-03-08 01:51:56 +00002623 if (LReduce && !RReduce) return false;
2624 if (RReduce && !LReduce) return true;
2625 }
2626
2627 if (!DisableSchedLiveUses && (LLiveUses != RLiveUses)) {
2628 DEBUG(dbgs() << "Live uses SU(" << left->NodeNum << "): " << LLiveUses
2629 << " != SU(" << right->NodeNum << "): " << RLiveUses << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002630 return LLiveUses < RLiveUses;
2631 }
2632
Andrew Trick52b3e382011-03-08 01:51:56 +00002633 if (!DisableSchedStalls) {
2634 bool LStall = BUHasStall(left, left->getHeight(), SPQ);
2635 bool RStall = BUHasStall(right, right->getHeight(), SPQ);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002636 if (LStall != RStall)
Andrew Trick52b3e382011-03-08 01:51:56 +00002637 return left->getHeight() > right->getHeight();
Andrew Trick641e2d42011-03-05 08:00:22 +00002638 }
2639
Andrew Trick25cedf32011-03-05 10:29:25 +00002640 if (!DisableSchedCriticalPath) {
2641 int spread = (int)left->getDepth() - (int)right->getDepth();
2642 if (std::abs(spread) > MaxReorderWindow) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002643 DEBUG(dbgs() << "Depth of SU(" << left->NodeNum << "): "
2644 << left->getDepth() << " != SU(" << right->NodeNum << "): "
2645 << right->getDepth() << "\n");
Andrew Trick25cedf32011-03-05 10:29:25 +00002646 return left->getDepth() < right->getDepth();
2647 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002648 }
2649
2650 if (!DisableSchedHeight && left->getHeight() != right->getHeight()) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002651 int spread = (int)left->getHeight() - (int)right->getHeight();
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002652 if (std::abs(spread) > MaxReorderWindow)
Andrew Trick52b3e382011-03-08 01:51:56 +00002653 return left->getHeight() > right->getHeight();
Evan Cheng37b740c2010-07-24 00:39:05 +00002654 }
2655
2656 return BURRSort(left, right, SPQ);
2657}
2658
Andrew Trickb53a00d2011-04-13 00:38:32 +00002659void RegReductionPQBase::initNodes(std::vector<SUnit> &sunits) {
2660 SUnits = &sunits;
2661 // Add pseudo dependency edges for two-address nodes.
Evan Chengd33b2d62011-11-10 07:43:16 +00002662 if (!Disable2AddrHack)
2663 AddPseudoTwoAddrDeps();
Andrew Trickb53a00d2011-04-13 00:38:32 +00002664 // Reroute edges to nodes with multiple uses.
Evan Cheng8ab58a22012-03-22 19:31:17 +00002665 if (!TracksRegPressure && !SrcOrder)
Andrew Trickb53a00d2011-04-13 00:38:32 +00002666 PrescheduleNodesWithMultipleUses();
2667 // Calculate node priorities.
2668 CalculateSethiUllmanNumbers();
2669
2670 // For single block loops, mark nodes that look like canonical IV increments.
2671 if (scheduleDAG->BB->isSuccessor(scheduleDAG->BB)) {
2672 for (unsigned i = 0, e = sunits.size(); i != e; ++i) {
2673 initVRegCycle(&sunits[i]);
2674 }
2675 }
2676}
2677
Andrew Trick9ccce772011-01-14 21:11:41 +00002678//===----------------------------------------------------------------------===//
2679// Preschedule for Register Pressure
2680//===----------------------------------------------------------------------===//
2681
2682bool RegReductionPQBase::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002683 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002684 unsigned Opc = SU->getNode()->getMachineOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002685 const MCInstrDesc &MCID = TII->get(Opc);
2686 unsigned NumRes = MCID.getNumDefs();
2687 unsigned NumOps = MCID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002688 for (unsigned i = 0; i != NumOps; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002689 if (MCID.getOperandConstraint(i+NumRes, MCOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002690 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00002691 if (DU->getNodeId() != -1 &&
2692 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002693 return true;
2694 }
2695 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002696 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002697 return false;
2698}
2699
Andrew Trick832a6a192011-09-01 00:54:31 +00002700/// canClobberReachingPhysRegUse - True if SU would clobber one of it's
2701/// successor's explicit physregs whose definition can reach DepSU.
2702/// i.e. DepSU should not be scheduled above SU.
2703static bool canClobberReachingPhysRegUse(const SUnit *DepSU, const SUnit *SU,
2704 ScheduleDAGRRList *scheduleDAG,
2705 const TargetInstrInfo *TII,
2706 const TargetRegisterInfo *TRI) {
Craig Topper5a4bcc72012-03-08 08:22:45 +00002707 const uint16_t *ImpDefs
Andrew Trick832a6a192011-09-01 00:54:31 +00002708 = TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002709 const uint32_t *RegMask = getNodeRegMask(SU->getNode());
2710 if(!ImpDefs && !RegMask)
Andrew Trick832a6a192011-09-01 00:54:31 +00002711 return false;
2712
2713 for (SUnit::const_succ_iterator SI = SU->Succs.begin(), SE = SU->Succs.end();
2714 SI != SE; ++SI) {
2715 SUnit *SuccSU = SI->getSUnit();
2716 for (SUnit::const_pred_iterator PI = SuccSU->Preds.begin(),
2717 PE = SuccSU->Preds.end(); PI != PE; ++PI) {
2718 if (!PI->isAssignedRegDep())
2719 continue;
2720
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002721 if (RegMask && MachineOperand::clobbersPhysReg(RegMask, PI->getReg()) &&
2722 scheduleDAG->IsReachable(DepSU, PI->getSUnit()))
2723 return true;
2724
2725 if (ImpDefs)
Craig Topper5a4bcc72012-03-08 08:22:45 +00002726 for (const uint16_t *ImpDef = ImpDefs; *ImpDef; ++ImpDef)
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002727 // Return true if SU clobbers this physical register use and the
2728 // definition of the register reaches from DepSU. IsReachable queries
2729 // a topological forward sort of the DAG (following the successors).
2730 if (TRI->regsOverlap(*ImpDef, PI->getReg()) &&
2731 scheduleDAG->IsReachable(DepSU, PI->getSUnit()))
2732 return true;
Andrew Trick832a6a192011-09-01 00:54:31 +00002733 }
2734 }
2735 return false;
2736}
2737
Evan Chengf9891412007-12-20 09:25:31 +00002738/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00002739/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00002740static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00002741 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002742 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002743 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00002744 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
Craig Topper5a4bcc72012-03-08 08:22:45 +00002745 const uint16_t *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00002746 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00002747 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +00002748 SUNode = SUNode->getGluedNode()) {
Dan Gohmana366da12009-03-23 16:23:01 +00002749 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00002750 continue;
Craig Topper5a4bcc72012-03-08 08:22:45 +00002751 const uint16_t *SUImpDefs =
Dan Gohmana366da12009-03-23 16:23:01 +00002752 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002753 const uint32_t *SURegMask = getNodeRegMask(SUNode);
2754 if (!SUImpDefs && !SURegMask)
2755 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002756 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002757 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002758 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00002759 continue;
2760 if (!N->hasAnyUseOfValue(i))
2761 continue;
2762 unsigned Reg = ImpDefs[i - NumDefs];
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002763 if (SURegMask && MachineOperand::clobbersPhysReg(SURegMask, Reg))
2764 return true;
2765 if (!SUImpDefs)
2766 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002767 for (;*SUImpDefs; ++SUImpDefs) {
2768 unsigned SUReg = *SUImpDefs;
2769 if (TRI->regsOverlap(Reg, SUReg))
2770 return true;
2771 }
Evan Chengf9891412007-12-20 09:25:31 +00002772 }
2773 }
2774 return false;
2775}
2776
Dan Gohman9a658d72009-03-24 00:49:12 +00002777/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
2778/// are not handled well by the general register pressure reduction
2779/// heuristics. When presented with code like this:
2780///
2781/// N
2782/// / |
2783/// / |
2784/// U store
2785/// |
2786/// ...
2787///
2788/// the heuristics tend to push the store up, but since the
2789/// operand of the store has another use (U), this would increase
2790/// the length of that other use (the U->N edge).
2791///
2792/// This function transforms code like the above to route U's
2793/// dependence through the store when possible, like this:
2794///
2795/// N
2796/// ||
2797/// ||
2798/// store
2799/// |
2800/// U
2801/// |
2802/// ...
2803///
2804/// This results in the store being scheduled immediately
2805/// after N, which shortens the U->N live range, reducing
2806/// register pressure.
2807///
Andrew Trick9ccce772011-01-14 21:11:41 +00002808void RegReductionPQBase::PrescheduleNodesWithMultipleUses() {
Dan Gohman9a658d72009-03-24 00:49:12 +00002809 // Visit all the nodes in topological order, working top-down.
2810 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
2811 SUnit *SU = &(*SUnits)[i];
2812 // For now, only look at nodes with no data successors, such as stores.
2813 // These are especially important, due to the heuristics in
2814 // getNodePriority for nodes with no data successors.
2815 if (SU->NumSuccs != 0)
2816 continue;
2817 // For now, only look at nodes with exactly one data predecessor.
2818 if (SU->NumPreds != 1)
2819 continue;
2820 // Avoid prescheduling copies to virtual registers, which don't behave
2821 // like other nodes from the perspective of scheduling heuristics.
2822 if (SDNode *N = SU->getNode())
2823 if (N->getOpcode() == ISD::CopyToReg &&
2824 TargetRegisterInfo::isVirtualRegister
2825 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2826 continue;
2827
2828 // Locate the single data predecessor.
Craig Topperc0196b12014-04-14 00:51:57 +00002829 SUnit *PredSU = nullptr;
Dan Gohman9a658d72009-03-24 00:49:12 +00002830 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
2831 EE = SU->Preds.end(); II != EE; ++II)
2832 if (!II->isCtrl()) {
2833 PredSU = II->getSUnit();
2834 break;
2835 }
2836 assert(PredSU);
2837
2838 // Don't rewrite edges that carry physregs, because that requires additional
2839 // support infrastructure.
2840 if (PredSU->hasPhysRegDefs)
2841 continue;
2842 // Short-circuit the case where SU is PredSU's only data successor.
2843 if (PredSU->NumSuccs == 1)
2844 continue;
2845 // Avoid prescheduling to copies from virtual registers, which don't behave
Andrew Trickd0548ae2011-02-04 03:18:17 +00002846 // like other nodes from the perspective of scheduling heuristics.
Dan Gohman9a658d72009-03-24 00:49:12 +00002847 if (SDNode *N = SU->getNode())
2848 if (N->getOpcode() == ISD::CopyFromReg &&
2849 TargetRegisterInfo::isVirtualRegister
2850 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2851 continue;
2852
2853 // Perform checks on the successors of PredSU.
2854 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
2855 EE = PredSU->Succs.end(); II != EE; ++II) {
2856 SUnit *PredSuccSU = II->getSUnit();
2857 if (PredSuccSU == SU) continue;
2858 // If PredSU has another successor with no data successors, for
2859 // now don't attempt to choose either over the other.
2860 if (PredSuccSU->NumSuccs == 0)
2861 goto outer_loop_continue;
2862 // Don't break physical register dependencies.
2863 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
2864 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
2865 goto outer_loop_continue;
2866 // Don't introduce graph cycles.
2867 if (scheduleDAG->IsReachable(SU, PredSuccSU))
2868 goto outer_loop_continue;
2869 }
2870
2871 // Ok, the transformation is safe and the heuristics suggest it is
2872 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00002873 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
2874 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002875 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00002876 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
2877 SDep Edge = PredSU->Succs[i];
2878 assert(!Edge.isAssignedRegDep());
2879 SUnit *SuccSU = Edge.getSUnit();
2880 if (SuccSU != SU) {
2881 Edge.setSUnit(PredSU);
2882 scheduleDAG->RemovePred(SuccSU, Edge);
2883 scheduleDAG->AddPred(SU, Edge);
2884 Edge.setSUnit(SU);
2885 scheduleDAG->AddPred(SuccSU, Edge);
2886 --i;
2887 }
2888 }
2889 outer_loop_continue:;
2890 }
2891}
2892
Evan Chengd38c22b2006-05-11 23:55:42 +00002893/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
2894/// it as a def&use operand. Add a pseudo control edge from it to the other
2895/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00002896/// first (lower in the schedule). If both nodes are two-address, favor the
2897/// one that has a CopyToReg use (more likely to be a loop induction update).
2898/// If both are two-address, but one is commutable while the other is not
2899/// commutable, favor the one that's not commutable.
Duncan Sands635e4ef2011-11-09 14:20:48 +00002900void RegReductionPQBase::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002901 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00002902 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002903 if (!SU->isTwoAddress)
2904 continue;
2905
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002906 SDNode *Node = SU->getNode();
Chris Lattner11a33812010-12-23 17:24:32 +00002907 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getGluedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002908 continue;
2909
Evan Cheng6c1414f2010-10-29 18:09:28 +00002910 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00002911 unsigned Opc = Node->getMachineOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002912 const MCInstrDesc &MCID = TII->get(Opc);
2913 unsigned NumRes = MCID.getNumDefs();
2914 unsigned NumOps = MCID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002915 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002916 if (MCID.getOperandConstraint(j+NumRes, MCOI::TIED_TO) == -1)
Dan Gohman82016c22008-11-19 02:00:32 +00002917 continue;
2918 SDNode *DU = SU->getNode()->getOperand(j).getNode();
2919 if (DU->getNodeId() == -1)
2920 continue;
2921 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
2922 if (!DUSU) continue;
2923 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
2924 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002925 if (I->isCtrl()) continue;
2926 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00002927 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00002928 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002929 // Be conservative. Ignore if nodes aren't at roughly the same
2930 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002931 if (SuccSU->getHeight() < SU->getHeight() &&
2932 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00002933 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002934 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
2935 // constrains whatever is using the copy, instead of the copy
2936 // itself. In the case that the copy is coalesced, this
2937 // preserves the intent of the pseudo two-address heurietics.
2938 while (SuccSU->Succs.size() == 1 &&
2939 SuccSU->getNode()->isMachineOpcode() &&
2940 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00002941 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002942 SuccSU = SuccSU->Succs.front().getSUnit();
2943 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00002944 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
2945 continue;
2946 // Don't constrain nodes with physical register defs if the
2947 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00002948 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00002949 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00002950 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002951 }
Dan Gohman3027bb62009-04-16 20:57:10 +00002952 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
2953 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00002954 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00002955 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
2956 SuccOpc == TargetOpcode::INSERT_SUBREG ||
2957 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00002958 continue;
Andrew Trick832a6a192011-09-01 00:54:31 +00002959 if (!canClobberReachingPhysRegUse(SuccSU, SU, scheduleDAG, TII, TRI) &&
2960 (!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00002961 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00002962 (!SU->isCommutable && SuccSU->isCommutable)) &&
2963 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00002964 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002965 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00002966 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Artificial));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002967 }
2968 }
2969 }
2970 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002971}
2972
Evan Chengd38c22b2006-05-11 23:55:42 +00002973//===----------------------------------------------------------------------===//
2974// Public Constructor Functions
2975//===----------------------------------------------------------------------===//
2976
Dan Gohmandfaf6462009-02-11 04:27:20 +00002977llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002978llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
2979 CodeGenOpt::Level OptLevel) {
Dan Gohman619ef482009-01-15 19:20:50 +00002980 const TargetMachine &TM = IS->TM;
2981 const TargetInstrInfo *TII = TM.getInstrInfo();
2982 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002983
Evan Chenga77f3d32010-07-21 06:09:07 +00002984 BURegReductionPriorityQueue *PQ =
Craig Topperc0196b12014-04-14 00:51:57 +00002985 new BURegReductionPriorityQueue(*IS->MF, false, false, TII, TRI, nullptr);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002986 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002987 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002988 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002989}
2990
Dan Gohmandfaf6462009-02-11 04:27:20 +00002991llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002992llvm::createSourceListDAGScheduler(SelectionDAGISel *IS,
2993 CodeGenOpt::Level OptLevel) {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002994 const TargetMachine &TM = IS->TM;
2995 const TargetInstrInfo *TII = TM.getInstrInfo();
2996 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002997
Evan Chenga77f3d32010-07-21 06:09:07 +00002998 SrcRegReductionPriorityQueue *PQ =
Craig Topperc0196b12014-04-14 00:51:57 +00002999 new SrcRegReductionPriorityQueue(*IS->MF, false, true, TII, TRI, nullptr);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003000 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Chengbdd062d2010-05-20 06:13:19 +00003001 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003002 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00003003}
3004
3005llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003006llvm::createHybridListDAGScheduler(SelectionDAGISel *IS,
3007 CodeGenOpt::Level OptLevel) {
Evan Chengbdd062d2010-05-20 06:13:19 +00003008 const TargetMachine &TM = IS->TM;
3009 const TargetInstrInfo *TII = TM.getInstrInfo();
3010 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Bill Wendlingf7719082013-06-06 00:43:09 +00003011 const TargetLowering *TLI = IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00003012
Evan Chenga77f3d32010-07-21 06:09:07 +00003013 HybridBURRPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00003014 new HybridBURRPriorityQueue(*IS->MF, true, false, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003015
3016 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00003017 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003018 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00003019}
Evan Cheng37b740c2010-07-24 00:39:05 +00003020
3021llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003022llvm::createILPListDAGScheduler(SelectionDAGISel *IS,
3023 CodeGenOpt::Level OptLevel) {
Evan Cheng37b740c2010-07-24 00:39:05 +00003024 const TargetMachine &TM = IS->TM;
3025 const TargetInstrInfo *TII = TM.getInstrInfo();
3026 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Bill Wendlingf7719082013-06-06 00:43:09 +00003027 const TargetLowering *TLI = IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00003028
Evan Cheng37b740c2010-07-24 00:39:05 +00003029 ILPBURRPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00003030 new ILPBURRPriorityQueue(*IS->MF, true, false, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003031 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Evan Cheng37b740c2010-07-24 00:39:05 +00003032 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003033 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00003034}