blob: 7b82a47d5104f4d73bfc696044590fcf004140ef [file] [log] [blame]
Dan Gohman23785a12008-08-12 17:42:33 +00001//===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===//
Evan Chengd38c22b2006-05-11 23:55:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengd38c22b2006-05-11 23:55:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements bottom-up and top-down register pressure reduction list
11// schedulers, using standard algorithms. The basic approach uses a priority
12// queue of available nodes to schedule. One at a time, nodes are taken from
13// the priority queue (thus in priority order), checked for legality to
14// schedule, and emitted if legal.
15//
16//===----------------------------------------------------------------------===//
17
Dale Johannesen2182f062007-07-13 17:13:54 +000018#define DEBUG_TYPE "pre-RA-sched"
Jim Laskey29e635d2006-08-02 12:30:23 +000019#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "ScheduleDAGSDNodes.h"
21#include "llvm/ADT/STLExtras.h"
Evan Cheng5924bf72007-09-25 01:54:36 +000022#include "llvm/ADT/SmallSet.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000023#include "llvm/ADT/Statistic.h"
Weiming Zhao4a0b4fb2013-01-29 21:18:43 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/InlineAsm.h"
Chris Lattner3b9f02a2010-04-07 05:20:54 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000031#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetLowering.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengd38c22b2006-05-11 23:55:42 +000036#include <climits>
Evan Chengd38c22b2006-05-11 23:55:42 +000037using namespace llvm;
38
Dan Gohmanfd227e92008-03-25 17:10:29 +000039STATISTIC(NumBacktracks, "Number of times scheduler backtracked");
Evan Cheng79e97132007-10-05 01:39:18 +000040STATISTIC(NumUnfolds, "Number of nodes unfolded");
Evan Cheng1ec79b42007-09-27 07:09:03 +000041STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000042STATISTIC(NumPRCopies, "Number of physical register copies");
Evan Cheng1ec79b42007-09-27 07:09:03 +000043
Jim Laskey95eda5b2006-08-01 14:21:23 +000044static RegisterScheduler
45 burrListDAGScheduler("list-burr",
Dan Gohman9c4b7d52008-10-14 20:25:08 +000046 "Bottom-up register reduction list scheduling",
Jim Laskey95eda5b2006-08-01 14:21:23 +000047 createBURRListDAGScheduler);
48static RegisterScheduler
Bill Wendling8cbc25d2010-01-23 10:26:57 +000049 sourceListDAGScheduler("source",
50 "Similar to list-burr but schedules in source "
51 "order when possible",
52 createSourceListDAGScheduler);
Jim Laskey95eda5b2006-08-01 14:21:23 +000053
Evan Chengbdd062d2010-05-20 06:13:19 +000054static RegisterScheduler
Evan Cheng725211e2010-05-21 00:42:32 +000055 hybridListDAGScheduler("list-hybrid",
Evan Cheng37b740c2010-07-24 00:39:05 +000056 "Bottom-up register pressure aware list scheduling "
57 "which tries to balance latency and register pressure",
Evan Chengbdd062d2010-05-20 06:13:19 +000058 createHybridListDAGScheduler);
59
Evan Cheng37b740c2010-07-24 00:39:05 +000060static RegisterScheduler
61 ILPListDAGScheduler("list-ilp",
62 "Bottom-up register pressure aware list scheduling "
63 "which tries to balance ILP and register pressure",
64 createILPListDAGScheduler);
65
Andrew Trick47ff14b2011-01-21 05:51:33 +000066static cl::opt<bool> DisableSchedCycles(
Andrew Trickbd428ec2011-01-21 06:19:05 +000067 "disable-sched-cycles", cl::Hidden, cl::init(false),
Andrew Trick47ff14b2011-01-21 05:51:33 +000068 cl::desc("Disable cycle-level precision during preRA scheduling"));
Andrew Trick10ffc2b2010-12-24 05:03:26 +000069
Andrew Trick641e2d42011-03-05 08:00:22 +000070// Temporary sched=list-ilp flags until the heuristics are robust.
Andrew Trickbfbd9722011-04-14 05:15:06 +000071// Some options are also available under sched=list-hybrid.
Andrew Trick641e2d42011-03-05 08:00:22 +000072static cl::opt<bool> DisableSchedRegPressure(
73 "disable-sched-reg-pressure", cl::Hidden, cl::init(false),
74 cl::desc("Disable regpressure priority in sched=list-ilp"));
75static cl::opt<bool> DisableSchedLiveUses(
Andrew Trickdd017322011-03-06 00:03:32 +000076 "disable-sched-live-uses", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000077 cl::desc("Disable live use priority in sched=list-ilp"));
Andrew Trick2ad0b372011-04-07 19:54:57 +000078static cl::opt<bool> DisableSchedVRegCycle(
79 "disable-sched-vrcycle", cl::Hidden, cl::init(false),
80 cl::desc("Disable virtual register cycle interference checks"));
Andrew Trickbfbd9722011-04-14 05:15:06 +000081static cl::opt<bool> DisableSchedPhysRegJoin(
82 "disable-sched-physreg-join", cl::Hidden, cl::init(false),
83 cl::desc("Disable physreg def-use affinity"));
Andrew Trick641e2d42011-03-05 08:00:22 +000084static cl::opt<bool> DisableSchedStalls(
Andrew Trickdd017322011-03-06 00:03:32 +000085 "disable-sched-stalls", cl::Hidden, cl::init(true),
Andrew Trick641e2d42011-03-05 08:00:22 +000086 cl::desc("Disable no-stall priority in sched=list-ilp"));
87static cl::opt<bool> DisableSchedCriticalPath(
88 "disable-sched-critical-path", cl::Hidden, cl::init(false),
89 cl::desc("Disable critical path priority in sched=list-ilp"));
90static cl::opt<bool> DisableSchedHeight(
91 "disable-sched-height", cl::Hidden, cl::init(false),
92 cl::desc("Disable scheduled-height priority in sched=list-ilp"));
Evan Chengd33b2d62011-11-10 07:43:16 +000093static cl::opt<bool> Disable2AddrHack(
94 "disable-2addr-hack", cl::Hidden, cl::init(true),
95 cl::desc("Disable scheduler's two-address hack"));
Andrew Trick641e2d42011-03-05 08:00:22 +000096
97static cl::opt<int> MaxReorderWindow(
98 "max-sched-reorder", cl::Hidden, cl::init(6),
99 cl::desc("Number of instructions to allow ahead of the critical path "
100 "in sched=list-ilp"));
101
102static cl::opt<unsigned> AvgIPC(
103 "sched-avg-ipc", cl::Hidden, cl::init(1),
104 cl::desc("Average inst/cycle whan no target itinerary exists."));
105
Evan Chengd38c22b2006-05-11 23:55:42 +0000106namespace {
Evan Chengd38c22b2006-05-11 23:55:42 +0000107//===----------------------------------------------------------------------===//
108/// ScheduleDAGRRList - The actual register reduction list scheduler
109/// implementation. This supports both top-down and bottom-up scheduling.
110///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000111class ScheduleDAGRRList : public ScheduleDAGSDNodes {
Evan Chengd38c22b2006-05-11 23:55:42 +0000112private:
Evan Chengbdd062d2010-05-20 06:13:19 +0000113 /// NeedLatency - True if the scheduler will make use of latency information.
114 ///
115 bool NeedLatency;
116
Evan Chengd38c22b2006-05-11 23:55:42 +0000117 /// AvailableQueue - The priority queue to use for the available SUnits.
Evan Chengd38c22b2006-05-11 23:55:42 +0000118 SchedulingPriorityQueue *AvailableQueue;
119
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000120 /// PendingQueue - This contains all of the instructions whose operands have
121 /// been issued, but their results are not ready yet (due to the latency of
122 /// the operation). Once the operands becomes available, the instruction is
123 /// added to the AvailableQueue.
124 std::vector<SUnit*> PendingQueue;
125
126 /// HazardRec - The hazard recognizer to use.
127 ScheduleHazardRecognizer *HazardRec;
128
Andrew Trick528fad92010-12-23 05:42:20 +0000129 /// CurCycle - The current scheduler state corresponds to this cycle.
130 unsigned CurCycle;
131
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000132 /// MinAvailableCycle - Cycle of the soonest available instruction.
133 unsigned MinAvailableCycle;
134
Andrew Trick641e2d42011-03-05 08:00:22 +0000135 /// IssueCount - Count instructions issued in this cycle
136 /// Currently valid only for bottom-up scheduling.
137 unsigned IssueCount;
138
Dan Gohmanc07f6862008-09-23 18:50:48 +0000139 /// LiveRegDefs - A set of physical registers and their definition
Evan Cheng5924bf72007-09-25 01:54:36 +0000140 /// that are "live". These nodes must be scheduled before any other nodes that
141 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +0000142 unsigned NumLiveRegs;
Evan Cheng5924bf72007-09-25 01:54:36 +0000143 std::vector<SUnit*> LiveRegDefs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000144 std::vector<SUnit*> LiveRegGens;
Evan Cheng5924bf72007-09-25 01:54:36 +0000145
Andrew Trick7cf43612013-02-25 19:11:48 +0000146 // Collect interferences between physical register use/defs.
147 // Each interference is an SUnit and set of physical registers.
148 SmallVector<SUnit*, 4> Interferences;
149 typedef DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMapT;
150 LRegsMapT LRegsMap;
151
Dan Gohmanad2134d2008-11-25 00:52:40 +0000152 /// Topo - A topological ordering for SUnits which permits fast IsReachable
153 /// and similar queries.
154 ScheduleDAGTopologicalSort Topo;
155
Eli Friedmand5c173f2011-12-07 22:24:28 +0000156 // Hack to keep track of the inverse of FindCallSeqStart without more crazy
157 // DAG crawling.
158 DenseMap<SUnit*, SUnit*> CallSeqEndForStart;
159
Evan Chengd38c22b2006-05-11 23:55:42 +0000160public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000161 ScheduleDAGRRList(MachineFunction &mf, bool needlatency,
162 SchedulingPriorityQueue *availqueue,
163 CodeGenOpt::Level OptLevel)
Dan Gohman90fb5522011-10-20 21:44:34 +0000164 : ScheduleDAGSDNodes(mf),
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000165 NeedLatency(needlatency), AvailableQueue(availqueue), CurCycle(0),
Craig Topperc0196b12014-04-14 00:51:57 +0000166 Topo(SUnits, nullptr) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000167
168 const TargetMachine &tm = mf.getTarget();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000169 if (DisableSchedCycles || !NeedLatency)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000170 HazardRec = new ScheduleHazardRecognizer();
Andrew Trick47ff14b2011-01-21 05:51:33 +0000171 else
172 HazardRec = tm.getInstrInfo()->CreateTargetHazardRecognizer(&tm, this);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000173 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000174
175 ~ScheduleDAGRRList() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000176 delete HazardRec;
Evan Chengd38c22b2006-05-11 23:55:42 +0000177 delete AvailableQueue;
178 }
179
Craig Topper7b883b32014-03-08 06:31:39 +0000180 void Schedule() override;
Evan Chengd38c22b2006-05-11 23:55:42 +0000181
Andrew Trick9ccce772011-01-14 21:11:41 +0000182 ScheduleHazardRecognizer *getHazardRec() { return HazardRec; }
183
Roman Levenstein733a4d62008-03-26 11:23:38 +0000184 /// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000185 bool IsReachable(const SUnit *SU, const SUnit *TargetSU) {
186 return Topo.IsReachable(SU, TargetSU);
187 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000188
Dan Gohman60d68442009-01-29 19:49:27 +0000189 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000190 /// create a cycle.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000191 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
192 return Topo.WillCreateCycle(SU, TargetSU);
193 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000194
Dan Gohman2d170892008-12-09 22:54:47 +0000195 /// AddPred - adds a predecessor edge to SUnit SU.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000196 /// This returns true if this is a new predecessor.
197 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000198 void AddPred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000199 Topo.AddPred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000200 SU->addPred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000201 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000202
Dan Gohman2d170892008-12-09 22:54:47 +0000203 /// RemovePred - removes a predecessor edge from SUnit SU.
204 /// This returns true if an edge was removed.
205 /// Updates the topological ordering if required.
Dan Gohman17214e62008-12-16 01:00:55 +0000206 void RemovePred(SUnit *SU, const SDep &D) {
Dan Gohman2d170892008-12-09 22:54:47 +0000207 Topo.RemovePred(SU, D.getSUnit());
Dan Gohman17214e62008-12-16 01:00:55 +0000208 SU->removePred(D);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000209 }
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000210
Evan Chengd38c22b2006-05-11 23:55:42 +0000211private:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000212 bool isReady(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000213 return DisableSchedCycles || !AvailableQueue->hasReadyFilter() ||
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000214 AvailableQueue->isReady(SU);
215 }
216
Dan Gohman60d68442009-01-29 19:49:27 +0000217 void ReleasePred(SUnit *SU, const SDep *PredEdge);
Andrew Tricka52f3252010-12-23 04:16:14 +0000218 void ReleasePredecessors(SUnit *SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000219 void ReleasePending();
220 void AdvanceToCycle(unsigned NextCycle);
221 void AdvancePastStalls(SUnit *SU);
222 void EmitNode(SUnit *SU);
Andrew Trick528fad92010-12-23 05:42:20 +0000223 void ScheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000224 void CapturePred(SDep *PredEdge);
Evan Cheng8e136a92007-09-26 21:36:17 +0000225 void UnscheduleNodeBottomUp(SUnit*);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000226 void RestoreHazardCheckerBottomUp();
227 void BacktrackBottomUp(SUnit*, SUnit*);
Evan Cheng8e136a92007-09-26 21:36:17 +0000228 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000229 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
230 const TargetRegisterClass*,
231 const TargetRegisterClass*,
Craig Topperb94011f2013-07-14 04:42:23 +0000232 SmallVectorImpl<SUnit*>&);
233 bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000234
Andrew Trick7cf43612013-02-25 19:11:48 +0000235 void releaseInterferences(unsigned Reg = 0);
236
Andrew Trick528fad92010-12-23 05:42:20 +0000237 SUnit *PickNodeToScheduleBottomUp();
Evan Chengd38c22b2006-05-11 23:55:42 +0000238 void ListScheduleBottomUp();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000239
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000240 /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it.
Roman Levenstein733a4d62008-03-26 11:23:38 +0000241 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000242 SUnit *CreateNewSUnit(SDNode *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000243 unsigned NumSUnits = SUnits.size();
Andrew Trick52226d42012-03-07 23:00:49 +0000244 SUnit *NewNode = newSUnit(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000245 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000246 if (NewNode->NodeNum >= NumSUnits)
247 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000248 return NewNode;
249 }
250
Roman Levenstein733a4d62008-03-26 11:23:38 +0000251 /// CreateClone - Creates a new SUnit from an existing one.
252 /// Updates the topological ordering if required.
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000253 SUnit *CreateClone(SUnit *N) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000254 unsigned NumSUnits = SUnits.size();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000255 SUnit *NewNode = Clone(N);
Roman Levenstein733a4d62008-03-26 11:23:38 +0000256 // Update the topological ordering.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000257 if (NewNode->NodeNum >= NumSUnits)
258 Topo.InitDAGTopologicalSorting();
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000259 return NewNode;
260 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000261
Andrew Trick52226d42012-03-07 23:00:49 +0000262 /// forceUnitLatencies - Register-pressure-reducing scheduling doesn't
Evan Chengbdd062d2010-05-20 06:13:19 +0000263 /// need actual latency information but the hybrid scheduler does.
Craig Topper7b883b32014-03-08 06:31:39 +0000264 bool forceUnitLatencies() const override {
Evan Chengbdd062d2010-05-20 06:13:19 +0000265 return !NeedLatency;
266 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000267};
268} // end anonymous namespace
269
Owen Anderson96adc4a2011-06-15 23:35:18 +0000270/// GetCostForDef - Looks up the register class and cost for a given definition.
271/// Typically this just means looking up the representative register class,
Owen Andersonca2f78a2011-11-16 01:02:57 +0000272/// but for untyped values (MVT::Untyped) it means inspecting the node's
Owen Anderson96adc4a2011-06-15 23:35:18 +0000273/// opcode to determine what register class is being generated.
274static void GetCostForDef(const ScheduleDAGSDNodes::RegDefIter &RegDefPos,
275 const TargetLowering *TLI,
276 const TargetInstrInfo *TII,
277 const TargetRegisterInfo *TRI,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000278 unsigned &RegClass, unsigned &Cost,
279 const MachineFunction &MF) {
Patrik Hagglund05394352012-12-13 18:45:35 +0000280 MVT VT = RegDefPos.GetValue();
Owen Anderson96adc4a2011-06-15 23:35:18 +0000281
282 // Special handling for untyped values. These values can only come from
283 // the expansion of custom DAG-to-DAG patterns.
Owen Andersonca2f78a2011-11-16 01:02:57 +0000284 if (VT == MVT::Untyped) {
Owen Andersond1955e72011-06-21 22:54:23 +0000285 const SDNode *Node = RegDefPos.GetNode();
Owen Andersond1955e72011-06-21 22:54:23 +0000286
Weiming Zhao4a0b4fb2013-01-29 21:18:43 +0000287 // Special handling for CopyFromReg of untyped values.
288 if (!Node->isMachineOpcode() && Node->getOpcode() == ISD::CopyFromReg) {
289 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
290 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
291 RegClass = RC->getID();
292 Cost = 1;
293 return;
294 }
295
296 unsigned Opcode = Node->getMachineOpcode();
Owen Andersond1955e72011-06-21 22:54:23 +0000297 if (Opcode == TargetOpcode::REG_SEQUENCE) {
298 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
299 const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
300 RegClass = RC->getID();
301 Cost = 1;
302 return;
303 }
304
Owen Anderson96adc4a2011-06-15 23:35:18 +0000305 unsigned Idx = RegDefPos.GetIdx();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000306 const MCInstrDesc Desc = TII->get(Opcode);
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000307 const TargetRegisterClass *RC = TII->getRegClass(Desc, Idx, TRI, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +0000308 RegClass = RC->getID();
309 // FIXME: Cost arbitrarily set to 1 because there doesn't seem to be a
310 // better way to determine it.
311 Cost = 1;
312 } else {
313 RegClass = TLI->getRepRegClassFor(VT)->getID();
314 Cost = TLI->getRepRegClassCostFor(VT);
315 }
316}
Evan Chengd38c22b2006-05-11 23:55:42 +0000317
318/// Schedule - Schedule the DAG using list scheduling.
319void ScheduleDAGRRList::Schedule() {
Evan Chenga77f3d32010-07-21 06:09:07 +0000320 DEBUG(dbgs()
321 << "********** List Scheduling BB#" << BB->getNumber()
Evan Cheng6c1414f2010-10-29 18:09:28 +0000322 << " '" << BB->getName() << "' **********\n");
Evan Cheng5924bf72007-09-25 01:54:36 +0000323
Andrew Trick528fad92010-12-23 05:42:20 +0000324 CurCycle = 0;
Andrew Trick641e2d42011-03-05 08:00:22 +0000325 IssueCount = 0;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000326 MinAvailableCycle = DisableSchedCycles ? 0 : UINT_MAX;
Dan Gohmanc07f6862008-09-23 18:50:48 +0000327 NumLiveRegs = 0;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000328 // Allocate slots for each physical register, plus one for a special register
329 // to track the virtual resource of a calling sequence.
Craig Topperc0196b12014-04-14 00:51:57 +0000330 LiveRegDefs.resize(TRI->getNumRegs() + 1, nullptr);
331 LiveRegGens.resize(TRI->getNumRegs() + 1, nullptr);
Eli Friedmand5c173f2011-12-07 22:24:28 +0000332 CallSeqEndForStart.clear();
Andrew Trick7cf43612013-02-25 19:11:48 +0000333 assert(Interferences.empty() && LRegsMap.empty() && "stale Interferences");
Evan Cheng5924bf72007-09-25 01:54:36 +0000334
Dan Gohman04543e72008-12-23 18:36:58 +0000335 // Build the scheduling graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000336 BuildSchedGraph(nullptr);
Evan Chengd38c22b2006-05-11 23:55:42 +0000337
Evan Chengd38c22b2006-05-11 23:55:42 +0000338 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000339 SUnits[su].dumpAll(this));
Dan Gohmanad2134d2008-11-25 00:52:40 +0000340 Topo.InitDAGTopologicalSorting();
Evan Chengd38c22b2006-05-11 23:55:42 +0000341
Dan Gohman46520a22008-06-21 19:18:17 +0000342 AvailableQueue->initNodes(SUnits);
Andrew Trick2085a962010-12-21 22:25:04 +0000343
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000344 HazardRec->Reset();
345
Dan Gohman90fb5522011-10-20 21:44:34 +0000346 // Execute the actual scheduling loop.
347 ListScheduleBottomUp();
Andrew Trick2085a962010-12-21 22:25:04 +0000348
Evan Chengd38c22b2006-05-11 23:55:42 +0000349 AvailableQueue->releaseState();
Andrew Trickedee68c2012-03-07 05:21:40 +0000350
351 DEBUG({
352 dbgs() << "*** Final schedule ***\n";
353 dumpSchedule();
354 dbgs() << '\n';
355 });
Evan Chengafed73e2006-05-12 01:58:24 +0000356}
Evan Chengd38c22b2006-05-11 23:55:42 +0000357
358//===----------------------------------------------------------------------===//
359// Bottom-Up Scheduling
360//===----------------------------------------------------------------------===//
361
Evan Chengd38c22b2006-05-11 23:55:42 +0000362/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
Dan Gohman54a187e2007-08-20 19:28:38 +0000363/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman60d68442009-01-29 19:49:27 +0000364void ScheduleDAGRRList::ReleasePred(SUnit *SU, const SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000365 SUnit *PredSU = PredEdge->getSUnit();
Reid Klecknercea8dab2009-09-30 20:43:07 +0000366
Evan Chengd38c22b2006-05-11 23:55:42 +0000367#ifndef NDEBUG
Reid Klecknercea8dab2009-09-30 20:43:07 +0000368 if (PredSU->NumSuccsLeft == 0) {
David Greenef34d7ac2010-01-05 01:24:54 +0000369 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000370 PredSU->dump(this);
David Greenef34d7ac2010-01-05 01:24:54 +0000371 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000372 llvm_unreachable(nullptr);
Evan Chengd38c22b2006-05-11 23:55:42 +0000373 }
374#endif
Reid Klecknercea8dab2009-09-30 20:43:07 +0000375 --PredSU->NumSuccsLeft;
376
Andrew Trick52226d42012-03-07 23:00:49 +0000377 if (!forceUnitLatencies()) {
Evan Chengbdd062d2010-05-20 06:13:19 +0000378 // Updating predecessor's height. This is now the cycle when the
379 // predecessor can be scheduled without causing a pipeline stall.
380 PredSU->setHeightToAtLeast(SU->getHeight() + PredEdge->getLatency());
381 }
382
Dan Gohmanb9543432009-02-10 23:27:53 +0000383 // If all the node's successors are scheduled, this node is ready
384 // to be scheduled. Ignore the special EntrySU node.
385 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman4370f262008-04-15 01:22:18 +0000386 PredSU->isAvailable = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000387
388 unsigned Height = PredSU->getHeight();
389 if (Height < MinAvailableCycle)
390 MinAvailableCycle = Height;
391
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000392 if (isReady(PredSU)) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000393 AvailableQueue->push(PredSU);
394 }
395 // CapturePred and others may have left the node in the pending queue, avoid
396 // adding it twice.
397 else if (!PredSU->isPending) {
398 PredSU->isPending = true;
399 PendingQueue.push_back(PredSU);
400 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000401 }
402}
403
Dan Gohman198b7ff2011-11-03 21:49:52 +0000404/// IsChainDependent - Test if Outer is reachable from Inner through
405/// chain dependencies.
406static bool IsChainDependent(SDNode *Outer, SDNode *Inner,
407 unsigned NestLevel,
408 const TargetInstrInfo *TII) {
409 SDNode *N = Outer;
410 for (;;) {
411 if (N == Inner)
412 return true;
413 // For a TokenFactor, examine each operand. There may be multiple ways
414 // to get to the CALLSEQ_BEGIN, but we need to find the path with the
415 // most nesting in order to ensure that we find the corresponding match.
416 if (N->getOpcode() == ISD::TokenFactor) {
417 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
418 if (IsChainDependent(N->getOperand(i).getNode(), Inner, NestLevel, TII))
419 return true;
420 return false;
421 }
422 // Check for a lowered CALLSEQ_BEGIN or CALLSEQ_END.
423 if (N->isMachineOpcode()) {
424 if (N->getMachineOpcode() ==
425 (unsigned)TII->getCallFrameDestroyOpcode()) {
426 ++NestLevel;
427 } else if (N->getMachineOpcode() ==
428 (unsigned)TII->getCallFrameSetupOpcode()) {
429 if (NestLevel == 0)
430 return false;
431 --NestLevel;
432 }
433 }
434 // Otherwise, find the chain and continue climbing.
435 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
436 if (N->getOperand(i).getValueType() == MVT::Other) {
437 N = N->getOperand(i).getNode();
438 goto found_chain_operand;
439 }
440 return false;
441 found_chain_operand:;
442 if (N->getOpcode() == ISD::EntryToken)
443 return false;
444 }
445}
446
447/// FindCallSeqStart - Starting from the (lowered) CALLSEQ_END node, locate
448/// the corresponding (lowered) CALLSEQ_BEGIN node.
449///
450/// NestLevel and MaxNested are used in recursion to indcate the current level
451/// of nesting of CALLSEQ_BEGIN and CALLSEQ_END pairs, as well as the maximum
452/// level seen so far.
453///
454/// TODO: It would be better to give CALLSEQ_END an explicit operand to point
455/// to the corresponding CALLSEQ_BEGIN to avoid needing to search for it.
456static SDNode *
457FindCallSeqStart(SDNode *N, unsigned &NestLevel, unsigned &MaxNest,
458 const TargetInstrInfo *TII) {
459 for (;;) {
460 // For a TokenFactor, examine each operand. There may be multiple ways
461 // to get to the CALLSEQ_BEGIN, but we need to find the path with the
462 // most nesting in order to ensure that we find the corresponding match.
463 if (N->getOpcode() == ISD::TokenFactor) {
Craig Topperc0196b12014-04-14 00:51:57 +0000464 SDNode *Best = nullptr;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000465 unsigned BestMaxNest = MaxNest;
466 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
467 unsigned MyNestLevel = NestLevel;
468 unsigned MyMaxNest = MaxNest;
469 if (SDNode *New = FindCallSeqStart(N->getOperand(i).getNode(),
470 MyNestLevel, MyMaxNest, TII))
471 if (!Best || (MyMaxNest > BestMaxNest)) {
472 Best = New;
473 BestMaxNest = MyMaxNest;
474 }
475 }
476 assert(Best);
477 MaxNest = BestMaxNest;
478 return Best;
479 }
480 // Check for a lowered CALLSEQ_BEGIN or CALLSEQ_END.
481 if (N->isMachineOpcode()) {
482 if (N->getMachineOpcode() ==
483 (unsigned)TII->getCallFrameDestroyOpcode()) {
484 ++NestLevel;
485 MaxNest = std::max(MaxNest, NestLevel);
486 } else if (N->getMachineOpcode() ==
487 (unsigned)TII->getCallFrameSetupOpcode()) {
488 assert(NestLevel != 0);
489 --NestLevel;
490 if (NestLevel == 0)
491 return N;
492 }
493 }
494 // Otherwise, find the chain and continue climbing.
495 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
496 if (N->getOperand(i).getValueType() == MVT::Other) {
497 N = N->getOperand(i).getNode();
498 goto found_chain_operand;
499 }
Craig Topperc0196b12014-04-14 00:51:57 +0000500 return nullptr;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000501 found_chain_operand:;
502 if (N->getOpcode() == ISD::EntryToken)
Craig Topperc0196b12014-04-14 00:51:57 +0000503 return nullptr;
Dan Gohman198b7ff2011-11-03 21:49:52 +0000504 }
505}
506
Andrew Trick033efdf2010-12-23 03:15:51 +0000507/// Call ReleasePred for each predecessor, then update register live def/gen.
508/// Always update LiveRegDefs for a register dependence even if the current SU
509/// also defines the register. This effectively create one large live range
510/// across a sequence of two-address node. This is important because the
511/// entire chain must be scheduled together. Example:
512///
513/// flags = (3) add
514/// flags = (2) addc flags
515/// flags = (1) addc flags
516///
517/// results in
518///
519/// LiveRegDefs[flags] = 3
Andrew Tricka52f3252010-12-23 04:16:14 +0000520/// LiveRegGens[flags] = 1
Andrew Trick033efdf2010-12-23 03:15:51 +0000521///
522/// If (2) addc is unscheduled, then (1) addc must also be unscheduled to avoid
523/// interference on flags.
Andrew Tricka52f3252010-12-23 04:16:14 +0000524void ScheduleDAGRRList::ReleasePredecessors(SUnit *SU) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000525 // Bottom up: release predecessors
Chris Lattnerd86418a2006-08-17 00:09:56 +0000526 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Cheng5924bf72007-09-25 01:54:36 +0000527 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000528 ReleasePred(SU, &*I);
529 if (I->isAssignedRegDep()) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000530 // This is a physical register dependency and it's impossible or
Andrew Trick2085a962010-12-21 22:25:04 +0000531 // expensive to copy the register. Make sure nothing that can
Evan Cheng5924bf72007-09-25 01:54:36 +0000532 // clobber the register is scheduled between the predecessor and
533 // this node.
Andrew Tricka52f3252010-12-23 04:16:14 +0000534 SUnit *RegDef = LiveRegDefs[I->getReg()]; (void)RegDef;
Andrew Trick033efdf2010-12-23 03:15:51 +0000535 assert((!RegDef || RegDef == SU || RegDef == I->getSUnit()) &&
536 "interference on register dependence");
Andrew Tricka52f3252010-12-23 04:16:14 +0000537 LiveRegDefs[I->getReg()] = I->getSUnit();
538 if (!LiveRegGens[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000539 ++NumLiveRegs;
Andrew Tricka52f3252010-12-23 04:16:14 +0000540 LiveRegGens[I->getReg()] = SU;
Evan Cheng5924bf72007-09-25 01:54:36 +0000541 }
542 }
543 }
Dan Gohman198b7ff2011-11-03 21:49:52 +0000544
545 // If we're scheduling a lowered CALLSEQ_END, find the corresponding
546 // CALLSEQ_BEGIN. Inject an artificial physical register dependence between
547 // these nodes, to prevent other calls from being interscheduled with them.
548 unsigned CallResource = TRI->getNumRegs();
549 if (!LiveRegDefs[CallResource])
550 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode())
551 if (Node->isMachineOpcode() &&
552 Node->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
553 unsigned NestLevel = 0;
554 unsigned MaxNest = 0;
555 SDNode *N = FindCallSeqStart(Node, NestLevel, MaxNest, TII);
556
557 SUnit *Def = &SUnits[N->getNodeId()];
Eli Friedmand5c173f2011-12-07 22:24:28 +0000558 CallSeqEndForStart[Def] = SU;
559
Dan Gohman198b7ff2011-11-03 21:49:52 +0000560 ++NumLiveRegs;
561 LiveRegDefs[CallResource] = Def;
562 LiveRegGens[CallResource] = SU;
563 break;
564 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000565}
566
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000567/// Check to see if any of the pending instructions are ready to issue. If
568/// so, add them to the available queue.
569void ScheduleDAGRRList::ReleasePending() {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000570 if (DisableSchedCycles) {
Andrew Trick5ce945c2010-12-24 07:10:19 +0000571 assert(PendingQueue.empty() && "pending instrs not allowed in this mode");
572 return;
573 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000574
575 // If the available queue is empty, it is safe to reset MinAvailableCycle.
576 if (AvailableQueue->empty())
577 MinAvailableCycle = UINT_MAX;
578
579 // Check to see if any of the pending instructions are ready to issue. If
580 // so, add them to the available queue.
581 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman90fb5522011-10-20 21:44:34 +0000582 unsigned ReadyCycle = PendingQueue[i]->getHeight();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000583 if (ReadyCycle < MinAvailableCycle)
584 MinAvailableCycle = ReadyCycle;
585
586 if (PendingQueue[i]->isAvailable) {
587 if (!isReady(PendingQueue[i]))
588 continue;
589 AvailableQueue->push(PendingQueue[i]);
590 }
591 PendingQueue[i]->isPending = false;
592 PendingQueue[i] = PendingQueue.back();
593 PendingQueue.pop_back();
594 --i; --e;
595 }
596}
597
598/// Move the scheduler state forward by the specified number of Cycles.
599void ScheduleDAGRRList::AdvanceToCycle(unsigned NextCycle) {
600 if (NextCycle <= CurCycle)
601 return;
602
Andrew Trick641e2d42011-03-05 08:00:22 +0000603 IssueCount = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000604 AvailableQueue->setCurCycle(NextCycle);
Andrew Trick47ff14b2011-01-21 05:51:33 +0000605 if (!HazardRec->isEnabled()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000606 // Bypass lots of virtual calls in case of long latency.
607 CurCycle = NextCycle;
608 }
609 else {
610 for (; CurCycle != NextCycle; ++CurCycle) {
Dan Gohman90fb5522011-10-20 21:44:34 +0000611 HazardRec->RecedeCycle();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000612 }
613 }
614 // FIXME: Instead of visiting the pending Q each time, set a dirty flag on the
615 // available Q to release pending nodes at least once before popping.
616 ReleasePending();
617}
618
619/// Move the scheduler state forward until the specified node's dependents are
620/// ready and can be scheduled with no resource conflicts.
621void ScheduleDAGRRList::AdvancePastStalls(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000622 if (DisableSchedCycles)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000623 return;
624
Andrew Trickb53a00d2011-04-13 00:38:32 +0000625 // FIXME: Nodes such as CopyFromReg probably should not advance the current
626 // cycle. Otherwise, we can wrongly mask real stalls. If the non-machine node
627 // has predecessors the cycle will be advanced when they are scheduled.
628 // But given the crude nature of modeling latency though such nodes, we
629 // currently need to treat these nodes like real instructions.
630 // if (!SU->getNode() || !SU->getNode()->isMachineOpcode()) return;
631
Dan Gohman90fb5522011-10-20 21:44:34 +0000632 unsigned ReadyCycle = SU->getHeight();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000633
634 // Bump CurCycle to account for latency. We assume the latency of other
635 // available instructions may be hidden by the stall (not a full pipe stall).
636 // This updates the hazard recognizer's cycle before reserving resources for
637 // this instruction.
638 AdvanceToCycle(ReadyCycle);
639
640 // Calls are scheduled in their preceding cycle, so don't conflict with
641 // hazards from instructions after the call. EmitNode will reset the
642 // scoreboard state before emitting the call.
Dan Gohman90fb5522011-10-20 21:44:34 +0000643 if (SU->isCall)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000644 return;
645
646 // FIXME: For resource conflicts in very long non-pipelined stages, we
647 // should probably skip ahead here to avoid useless scoreboard checks.
648 int Stalls = 0;
649 while (true) {
650 ScheduleHazardRecognizer::HazardType HT =
Dan Gohman90fb5522011-10-20 21:44:34 +0000651 HazardRec->getHazardType(SU, -Stalls);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000652
653 if (HT == ScheduleHazardRecognizer::NoHazard)
654 break;
655
656 ++Stalls;
657 }
658 AdvanceToCycle(CurCycle + Stalls);
659}
660
661/// Record this SUnit in the HazardRecognizer.
662/// Does not update CurCycle.
663void ScheduleDAGRRList::EmitNode(SUnit *SU) {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000664 if (!HazardRec->isEnabled())
Andrew Trickc9405662010-12-24 06:46:50 +0000665 return;
666
667 // Check for phys reg copy.
668 if (!SU->getNode())
669 return;
670
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000671 switch (SU->getNode()->getOpcode()) {
672 default:
673 assert(SU->getNode()->isMachineOpcode() &&
674 "This target-independent node should not be scheduled.");
675 break;
676 case ISD::MERGE_VALUES:
677 case ISD::TokenFactor:
Nadav Rotem7c277da2012-09-06 09:17:37 +0000678 case ISD::LIFETIME_START:
679 case ISD::LIFETIME_END:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000680 case ISD::CopyToReg:
681 case ISD::CopyFromReg:
682 case ISD::EH_LABEL:
683 // Noops don't affect the scoreboard state. Copies are likely to be
684 // removed.
685 return;
686 case ISD::INLINEASM:
687 // For inline asm, clear the pipeline state.
688 HazardRec->Reset();
689 return;
690 }
Dan Gohman90fb5522011-10-20 21:44:34 +0000691 if (SU->isCall) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000692 // Calls are scheduled with their preceding instructions. For bottom-up
693 // scheduling, clear the pipeline state before emitting.
694 HazardRec->Reset();
695 }
696
697 HazardRec->EmitInstruction(SU);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000698}
699
Andrew Trickb53a00d2011-04-13 00:38:32 +0000700static void resetVRegCycle(SUnit *SU);
701
Dan Gohmanb9543432009-02-10 23:27:53 +0000702/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
703/// count of its predecessors. If a predecessor pending count is zero, add it to
704/// the Available queue.
Andrew Trick528fad92010-12-23 05:42:20 +0000705void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU) {
Andrew Trick1b60ad62011-04-12 20:14:07 +0000706 DEBUG(dbgs() << "\n*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000707 DEBUG(SU->dump(this));
708
Evan Chengbdd062d2010-05-20 06:13:19 +0000709#ifndef NDEBUG
710 if (CurCycle < SU->getHeight())
Andrew Trickb53a00d2011-04-13 00:38:32 +0000711 DEBUG(dbgs() << " Height [" << SU->getHeight()
712 << "] pipeline stall!\n");
Evan Chengbdd062d2010-05-20 06:13:19 +0000713#endif
714
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000715 // FIXME: Do not modify node height. It may interfere with
716 // backtracking. Instead add a "ready cycle" to SUnit. Before scheduling the
Eric Christopher1b4b1e52011-03-21 18:06:21 +0000717 // node its ready cycle can aid heuristics, and after scheduling it can
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000718 // indicate the scheduled cycle.
Dan Gohmanb9543432009-02-10 23:27:53 +0000719 SU->setHeightToAtLeast(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000720
Robert Wilhelmf0cfb832013-09-28 11:46:15 +0000721 // Reserve resources for the scheduled instruction.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000722 EmitNode(SU);
723
Dan Gohmanb9543432009-02-10 23:27:53 +0000724 Sequence.push_back(SU);
725
Andrew Trick52226d42012-03-07 23:00:49 +0000726 AvailableQueue->scheduledNode(SU);
Chris Lattner981afd22010-12-20 00:55:43 +0000727
Andrew Trick641e2d42011-03-05 08:00:22 +0000728 // If HazardRec is disabled, and each inst counts as one cycle, then
Andrew Trickb53a00d2011-04-13 00:38:32 +0000729 // advance CurCycle before ReleasePredecessors to avoid useless pushes to
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000730 // PendingQueue for schedulers that implement HasReadyFilter.
Andrew Trick641e2d42011-03-05 08:00:22 +0000731 if (!HazardRec->isEnabled() && AvgIPC < 2)
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000732 AdvanceToCycle(CurCycle + 1);
733
Andrew Trick033efdf2010-12-23 03:15:51 +0000734 // Update liveness of predecessors before successors to avoid treating a
735 // two-address node as a live range def.
Andrew Tricka52f3252010-12-23 04:16:14 +0000736 ReleasePredecessors(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000737
738 // Release all the implicit physical register defs that are live.
739 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
740 I != E; ++I) {
Andrew Trick033efdf2010-12-23 03:15:51 +0000741 // LiveRegDegs[I->getReg()] != SU when SU is a two-address node.
742 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] == SU) {
743 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
744 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000745 LiveRegDefs[I->getReg()] = nullptr;
746 LiveRegGens[I->getReg()] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000747 releaseInterferences(I->getReg());
Evan Cheng5924bf72007-09-25 01:54:36 +0000748 }
749 }
Dan Gohman198b7ff2011-11-03 21:49:52 +0000750 // Release the special call resource dependence, if this is the beginning
751 // of a call.
752 unsigned CallResource = TRI->getNumRegs();
753 if (LiveRegDefs[CallResource] == SU)
754 for (const SDNode *SUNode = SU->getNode(); SUNode;
755 SUNode = SUNode->getGluedNode()) {
756 if (SUNode->isMachineOpcode() &&
757 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameSetupOpcode()) {
758 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
759 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000760 LiveRegDefs[CallResource] = nullptr;
761 LiveRegGens[CallResource] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000762 releaseInterferences(CallResource);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000763 }
764 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000765
Andrew Trickb53a00d2011-04-13 00:38:32 +0000766 resetVRegCycle(SU);
767
Evan Chengd38c22b2006-05-11 23:55:42 +0000768 SU->isScheduled = true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000769
770 // Conditions under which the scheduler should eagerly advance the cycle:
771 // (1) No available instructions
772 // (2) All pipelines full, so available instructions must have hazards.
773 //
Andrew Trickb53a00d2011-04-13 00:38:32 +0000774 // If HazardRec is disabled, the cycle was pre-advanced before calling
775 // ReleasePredecessors. In that case, IssueCount should remain 0.
Andrew Trickc88b7ec2011-03-04 02:03:45 +0000776 //
777 // Check AvailableQueue after ReleasePredecessors in case of zero latency.
Andrew Trickb53a00d2011-04-13 00:38:32 +0000778 if (HazardRec->isEnabled() || AvgIPC > 1) {
779 if (SU->getNode() && SU->getNode()->isMachineOpcode())
780 ++IssueCount;
781 if ((HazardRec->isEnabled() && HazardRec->atIssueLimit())
782 || (!HazardRec->isEnabled() && IssueCount == AvgIPC))
783 AdvanceToCycle(CurCycle + 1);
784 }
Evan Chengd38c22b2006-05-11 23:55:42 +0000785}
786
Evan Cheng5924bf72007-09-25 01:54:36 +0000787/// CapturePred - This does the opposite of ReleasePred. Since SU is being
788/// unscheduled, incrcease the succ left count of its predecessors. Remove
789/// them from AvailableQueue if necessary.
Andrew Trick2085a962010-12-21 22:25:04 +0000790void ScheduleDAGRRList::CapturePred(SDep *PredEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000791 SUnit *PredSU = PredEdge->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000792 if (PredSU->isAvailable) {
793 PredSU->isAvailable = false;
794 if (!PredSU->isPending)
795 AvailableQueue->remove(PredSU);
796 }
797
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000798 assert(PredSU->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Evan Cheng038dcc52007-09-28 19:24:24 +0000799 ++PredSU->NumSuccsLeft;
Evan Cheng5924bf72007-09-25 01:54:36 +0000800}
801
802/// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and
803/// its predecessor states to reflect the change.
804void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) {
David Greenef34d7ac2010-01-05 01:24:54 +0000805 DEBUG(dbgs() << "*** Unscheduling [" << SU->getHeight() << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000806 DEBUG(SU->dump(this));
Evan Cheng5924bf72007-09-25 01:54:36 +0000807
Evan Cheng5924bf72007-09-25 01:54:36 +0000808 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
809 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000810 CapturePred(&*I);
Andrew Tricka52f3252010-12-23 04:16:14 +0000811 if (I->isAssignedRegDep() && SU == LiveRegGens[I->getReg()]){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000812 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000813 assert(LiveRegDefs[I->getReg()] == I->getSUnit() &&
Evan Cheng5924bf72007-09-25 01:54:36 +0000814 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000815 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000816 LiveRegDefs[I->getReg()] = nullptr;
817 LiveRegGens[I->getReg()] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000818 releaseInterferences(I->getReg());
Evan Cheng5924bf72007-09-25 01:54:36 +0000819 }
820 }
821
Dan Gohman198b7ff2011-11-03 21:49:52 +0000822 // Reclaim the special call resource dependence, if this is the beginning
823 // of a call.
824 unsigned CallResource = TRI->getNumRegs();
825 for (const SDNode *SUNode = SU->getNode(); SUNode;
826 SUNode = SUNode->getGluedNode()) {
827 if (SUNode->isMachineOpcode() &&
828 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameSetupOpcode()) {
829 ++NumLiveRegs;
830 LiveRegDefs[CallResource] = SU;
Eli Friedmand5c173f2011-12-07 22:24:28 +0000831 LiveRegGens[CallResource] = CallSeqEndForStart[SU];
Dan Gohman198b7ff2011-11-03 21:49:52 +0000832 }
833 }
834
835 // Release the special call resource dependence, if this is the end
836 // of a call.
837 if (LiveRegGens[CallResource] == SU)
838 for (const SDNode *SUNode = SU->getNode(); SUNode;
839 SUNode = SUNode->getGluedNode()) {
840 if (SUNode->isMachineOpcode() &&
841 SUNode->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
842 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
843 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000844 LiveRegDefs[CallResource] = nullptr;
845 LiveRegGens[CallResource] = nullptr;
Andrew Trick7cf43612013-02-25 19:11:48 +0000846 releaseInterferences(CallResource);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000847 }
848 }
849
Evan Cheng5924bf72007-09-25 01:54:36 +0000850 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
851 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000852 if (I->isAssignedRegDep()) {
Eli Friedman0bdc0832011-12-07 22:06:02 +0000853 if (!LiveRegDefs[I->getReg()])
854 ++NumLiveRegs;
Andrew Trick033efdf2010-12-23 03:15:51 +0000855 // This becomes the nearest def. Note that an earlier def may still be
856 // pending if this is a two-address node.
857 LiveRegDefs[I->getReg()] = SU;
Craig Topperc0196b12014-04-14 00:51:57 +0000858 if (LiveRegGens[I->getReg()] == nullptr ||
Andrew Tricka52f3252010-12-23 04:16:14 +0000859 I->getSUnit()->getHeight() < LiveRegGens[I->getReg()]->getHeight())
860 LiveRegGens[I->getReg()] = I->getSUnit();
Evan Cheng5924bf72007-09-25 01:54:36 +0000861 }
862 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000863 if (SU->getHeight() < MinAvailableCycle)
864 MinAvailableCycle = SU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000865
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000866 SU->setHeightDirty();
Evan Cheng5924bf72007-09-25 01:54:36 +0000867 SU->isScheduled = false;
868 SU->isAvailable = true;
Andrew Trick47ff14b2011-01-21 05:51:33 +0000869 if (!DisableSchedCycles && AvailableQueue->hasReadyFilter()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000870 // Don't make available until backtracking is complete.
871 SU->isPending = true;
872 PendingQueue.push_back(SU);
873 }
874 else {
875 AvailableQueue->push(SU);
876 }
Andrew Trick52226d42012-03-07 23:00:49 +0000877 AvailableQueue->unscheduledNode(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +0000878}
879
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000880/// After backtracking, the hazard checker needs to be restored to a state
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000881/// corresponding the current cycle.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000882void ScheduleDAGRRList::RestoreHazardCheckerBottomUp() {
883 HazardRec->Reset();
884
885 unsigned LookAhead = std::min((unsigned)Sequence.size(),
886 HazardRec->getMaxLookAhead());
887 if (LookAhead == 0)
888 return;
889
890 std::vector<SUnit*>::const_iterator I = (Sequence.end() - LookAhead);
891 unsigned HazardCycle = (*I)->getHeight();
892 for (std::vector<SUnit*>::const_iterator E = Sequence.end(); I != E; ++I) {
893 SUnit *SU = *I;
894 for (; SU->getHeight() > HazardCycle; ++HazardCycle) {
895 HazardRec->RecedeCycle();
896 }
897 EmitNode(SU);
898 }
899}
900
Evan Cheng8e136a92007-09-26 21:36:17 +0000901/// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in
Dan Gohman60d68442009-01-29 19:49:27 +0000902/// BTCycle in order to schedule a specific node.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000903void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, SUnit *BtSU) {
904 SUnit *OldSU = Sequence.back();
905 while (true) {
Evan Cheng5924bf72007-09-25 01:54:36 +0000906 Sequence.pop_back();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000907 // FIXME: use ready cycle instead of height
908 CurCycle = OldSU->getHeight();
Evan Cheng5924bf72007-09-25 01:54:36 +0000909 UnscheduleNodeBottomUp(OldSU);
Evan Chengbdd062d2010-05-20 06:13:19 +0000910 AvailableQueue->setCurCycle(CurCycle);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000911 if (OldSU == BtSU)
912 break;
913 OldSU = Sequence.back();
Evan Cheng5924bf72007-09-25 01:54:36 +0000914 }
915
Dan Gohman60d68442009-01-29 19:49:27 +0000916 assert(!SU->isSucc(OldSU) && "Something is wrong!");
Evan Cheng1ec79b42007-09-27 07:09:03 +0000917
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000918 RestoreHazardCheckerBottomUp();
919
Andrew Trick5ce945c2010-12-24 07:10:19 +0000920 ReleasePending();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000921
Evan Cheng1ec79b42007-09-27 07:09:03 +0000922 ++NumBacktracks;
Evan Cheng5924bf72007-09-25 01:54:36 +0000923}
924
Evan Cheng3b245872010-02-05 01:27:11 +0000925static bool isOperandOf(const SUnit *SU, SDNode *N) {
926 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +0000927 SUNode = SUNode->getGluedNode()) {
Evan Cheng3b245872010-02-05 01:27:11 +0000928 if (SUNode->isOperandOf(N))
929 return true;
930 }
931 return false;
932}
933
Evan Cheng5924bf72007-09-25 01:54:36 +0000934/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
935/// successors to the newly created node.
936SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000937 SDNode *N = SU->getNode();
Evan Cheng79e97132007-10-05 01:39:18 +0000938 if (!N)
Craig Topperc0196b12014-04-14 00:51:57 +0000939 return nullptr;
Evan Cheng79e97132007-10-05 01:39:18 +0000940
Andrew Trickc9405662010-12-24 06:46:50 +0000941 if (SU->getNode()->getGluedNode())
Craig Topperc0196b12014-04-14 00:51:57 +0000942 return nullptr;
Andrew Trickc9405662010-12-24 06:46:50 +0000943
Evan Cheng79e97132007-10-05 01:39:18 +0000944 SUnit *NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +0000945 bool TryUnfold = false;
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000946 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000947 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000948 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000949 return nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000950 else if (VT == MVT::Other)
Evan Cheng84d0ebc2007-10-05 01:42:35 +0000951 TryUnfold = true;
952 }
Evan Cheng79e97132007-10-05 01:39:18 +0000953 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000954 const SDValue &Op = N->getOperand(i);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000955 EVT VT = Op.getNode()->getValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000956 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000957 return nullptr;
Evan Cheng79e97132007-10-05 01:39:18 +0000958 }
959
960 if (TryUnfold) {
Dan Gohmane6e13482008-06-21 15:52:51 +0000961 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000962 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Craig Topperc0196b12014-04-14 00:51:57 +0000963 return nullptr;
Evan Cheng79e97132007-10-05 01:39:18 +0000964
Pete Cooper7c7ba1b2011-11-15 21:57:53 +0000965 // unfolding an x86 DEC64m operation results in store, dec, load which
966 // can't be handled here so quit
967 if (NewNodes.size() == 3)
Craig Topperc0196b12014-04-14 00:51:57 +0000968 return nullptr;
Pete Cooper7c7ba1b2011-11-15 21:57:53 +0000969
Evan Chengbdd062d2010-05-20 06:13:19 +0000970 DEBUG(dbgs() << "Unfolding SU #" << SU->NodeNum << "\n");
Evan Cheng79e97132007-10-05 01:39:18 +0000971 assert(NewNodes.size() == 2 && "Expected a load folding node!");
972
973 N = NewNodes[1];
974 SDNode *LoadNode = NewNodes[0];
Evan Cheng79e97132007-10-05 01:39:18 +0000975 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000976 unsigned OldNumVals = SU->getNode()->getNumValues();
Evan Cheng79e97132007-10-05 01:39:18 +0000977 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000978 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
979 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000980 SDValue(LoadNode, 1));
Evan Cheng79e97132007-10-05 01:39:18 +0000981
Dan Gohmane52e0892008-11-11 21:34:44 +0000982 // LoadNode may already exist. This can happen when there is another
983 // load from the same location and producing the same type of value
984 // but it has different alignment or volatileness.
985 bool isNewLoad = true;
986 SUnit *LoadSU;
987 if (LoadNode->getNodeId() != -1) {
988 LoadSU = &SUnits[LoadNode->getNodeId()];
989 isNewLoad = false;
990 } else {
991 LoadSU = CreateNewSUnit(LoadNode);
992 LoadNode->setNodeId(LoadSU->NodeNum);
Andrew Trickd0548ae2011-02-04 03:18:17 +0000993
994 InitNumRegDefsLeft(LoadSU);
Andrew Trick52226d42012-03-07 23:00:49 +0000995 computeLatency(LoadSU);
Dan Gohmane52e0892008-11-11 21:34:44 +0000996 }
997
Roman Levenstein7e71b4b2008-03-26 09:18:09 +0000998 SUnit *NewSU = CreateNewSUnit(N);
Dan Gohman46520a22008-06-21 19:18:17 +0000999 assert(N->getNodeId() == -1 && "Node already inserted!");
1000 N->setNodeId(NewSU->NodeNum);
Andrew Trick2085a962010-12-21 22:25:04 +00001001
Evan Cheng6cc775f2011-06-28 19:10:37 +00001002 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1003 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
1004 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Evan Cheng79e97132007-10-05 01:39:18 +00001005 NewSU->isTwoAddress = true;
1006 break;
1007 }
1008 }
Evan Cheng6cc775f2011-06-28 19:10:37 +00001009 if (MCID.isCommutable())
Evan Cheng79e97132007-10-05 01:39:18 +00001010 NewSU->isCommutable = true;
Andrew Trickd0548ae2011-02-04 03:18:17 +00001011
1012 InitNumRegDefsLeft(NewSU);
Andrew Trick52226d42012-03-07 23:00:49 +00001013 computeLatency(NewSU);
Evan Cheng79e97132007-10-05 01:39:18 +00001014
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001015 // Record all the edges to and from the old SU, by category.
Dan Gohman15af5522009-03-06 02:23:01 +00001016 SmallVector<SDep, 4> ChainPreds;
Evan Cheng79e97132007-10-05 01:39:18 +00001017 SmallVector<SDep, 4> ChainSuccs;
1018 SmallVector<SDep, 4> LoadPreds;
1019 SmallVector<SDep, 4> NodePreds;
1020 SmallVector<SDep, 4> NodeSuccs;
1021 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1022 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001023 if (I->isCtrl())
Dan Gohman15af5522009-03-06 02:23:01 +00001024 ChainPreds.push_back(*I);
Evan Cheng3b245872010-02-05 01:27:11 +00001025 else if (isOperandOf(I->getSUnit(), LoadNode))
Dan Gohman2d170892008-12-09 22:54:47 +00001026 LoadPreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001027 else
Dan Gohman2d170892008-12-09 22:54:47 +00001028 NodePreds.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001029 }
1030 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1031 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001032 if (I->isCtrl())
1033 ChainSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001034 else
Dan Gohman2d170892008-12-09 22:54:47 +00001035 NodeSuccs.push_back(*I);
Evan Cheng79e97132007-10-05 01:39:18 +00001036 }
1037
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001038 // Now assign edges to the newly-created nodes.
Dan Gohman15af5522009-03-06 02:23:01 +00001039 for (unsigned i = 0, e = ChainPreds.size(); i != e; ++i) {
1040 const SDep &Pred = ChainPreds[i];
1041 RemovePred(SU, Pred);
Dan Gohman4370f262008-04-15 01:22:18 +00001042 if (isNewLoad)
Dan Gohman15af5522009-03-06 02:23:01 +00001043 AddPred(LoadSU, Pred);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001044 }
Evan Cheng79e97132007-10-05 01:39:18 +00001045 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001046 const SDep &Pred = LoadPreds[i];
1047 RemovePred(SU, Pred);
Dan Gohman15af5522009-03-06 02:23:01 +00001048 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +00001049 AddPred(LoadSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +00001050 }
1051 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001052 const SDep &Pred = NodePreds[i];
1053 RemovePred(SU, Pred);
1054 AddPred(NewSU, Pred);
Evan Cheng79e97132007-10-05 01:39:18 +00001055 }
1056 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001057 SDep D = NodeSuccs[i];
1058 SUnit *SuccDep = D.getSUnit();
1059 D.setSUnit(SU);
1060 RemovePred(SuccDep, D);
1061 D.setSUnit(NewSU);
1062 AddPred(SuccDep, D);
Andrew Trickd0548ae2011-02-04 03:18:17 +00001063 // Balance register pressure.
1064 if (AvailableQueue->tracksRegPressure() && SuccDep->isScheduled
1065 && !D.isCtrl() && NewSU->NumRegDefsLeft > 0)
1066 --NewSU->NumRegDefsLeft;
Evan Cheng79e97132007-10-05 01:39:18 +00001067 }
1068 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +00001069 SDep D = ChainSuccs[i];
1070 SUnit *SuccDep = D.getSUnit();
1071 D.setSUnit(SU);
1072 RemovePred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001073 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +00001074 D.setSUnit(LoadSU);
1075 AddPred(SuccDep, D);
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001076 }
Andrew Trick2085a962010-12-21 22:25:04 +00001077 }
Dan Gohmaned0e8d42009-03-23 20:20:43 +00001078
1079 // Add a data dependency to reflect that NewSU reads the value defined
1080 // by LoadSU.
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001081 SDep D(LoadSU, SDep::Data, 0);
1082 D.setLatency(LoadSU->Latency);
1083 AddPred(NewSU, D);
Evan Cheng79e97132007-10-05 01:39:18 +00001084
Evan Cheng91e0fc92007-12-18 08:42:10 +00001085 if (isNewLoad)
1086 AvailableQueue->addNode(LoadSU);
Evan Cheng79e97132007-10-05 01:39:18 +00001087 AvailableQueue->addNode(NewSU);
1088
1089 ++NumUnfolds;
1090
1091 if (NewSU->NumSuccsLeft == 0) {
1092 NewSU->isAvailable = true;
1093 return NewSU;
Evan Cheng91e0fc92007-12-18 08:42:10 +00001094 }
1095 SU = NewSU;
Evan Cheng79e97132007-10-05 01:39:18 +00001096 }
1097
Evan Chengbdd062d2010-05-20 06:13:19 +00001098 DEBUG(dbgs() << " Duplicating SU #" << SU->NodeNum << "\n");
Roman Levenstein7e71b4b2008-03-26 09:18:09 +00001099 NewSU = CreateClone(SU);
Evan Cheng5924bf72007-09-25 01:54:36 +00001100
1101 // New SUnit has the exact same predecessors.
1102 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1103 I != E; ++I)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001104 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +00001105 AddPred(NewSU, *I);
Evan Cheng5924bf72007-09-25 01:54:36 +00001106
1107 // Only copy scheduled successors. Cut them from old node's successor
1108 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +00001109 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng5924bf72007-09-25 01:54:36 +00001110 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1111 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001112 if (I->isArtificial())
Evan Cheng5924bf72007-09-25 01:54:36 +00001113 continue;
Dan Gohman2d170892008-12-09 22:54:47 +00001114 SUnit *SuccSU = I->getSUnit();
1115 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +00001116 SDep D = *I;
1117 D.setSUnit(NewSU);
1118 AddPred(SuccSU, D);
1119 D.setSUnit(SU);
1120 DelDeps.push_back(std::make_pair(SuccSU, D));
Evan Cheng5924bf72007-09-25 01:54:36 +00001121 }
1122 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00001123 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +00001124 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng5924bf72007-09-25 01:54:36 +00001125
1126 AvailableQueue->updateNode(SU);
1127 AvailableQueue->addNode(NewSU);
1128
Evan Cheng1ec79b42007-09-27 07:09:03 +00001129 ++NumDups;
Evan Cheng5924bf72007-09-25 01:54:36 +00001130 return NewSU;
1131}
1132
Evan Chengb2c42c62009-01-12 03:19:55 +00001133/// InsertCopiesAndMoveSuccs - Insert register copies and move all
1134/// scheduled successors of the given SUnit to the last copy.
1135void ScheduleDAGRRList::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Craig Topperb94011f2013-07-14 04:42:23 +00001136 const TargetRegisterClass *DestRC,
1137 const TargetRegisterClass *SrcRC,
1138 SmallVectorImpl<SUnit*> &Copies) {
Craig Topperc0196b12014-04-14 00:51:57 +00001139 SUnit *CopyFromSU = CreateNewSUnit(nullptr);
Evan Cheng8e136a92007-09-26 21:36:17 +00001140 CopyFromSU->CopySrcRC = SrcRC;
1141 CopyFromSU->CopyDstRC = DestRC;
Evan Cheng8e136a92007-09-26 21:36:17 +00001142
Craig Topperc0196b12014-04-14 00:51:57 +00001143 SUnit *CopyToSU = CreateNewSUnit(nullptr);
Evan Cheng8e136a92007-09-26 21:36:17 +00001144 CopyToSU->CopySrcRC = DestRC;
1145 CopyToSU->CopyDstRC = SrcRC;
1146
1147 // Only copy scheduled successors. Cut them from old node's successor
1148 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +00001149 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Evan Cheng8e136a92007-09-26 21:36:17 +00001150 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1151 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001152 if (I->isArtificial())
Evan Cheng8e136a92007-09-26 21:36:17 +00001153 continue;
Dan Gohman2d170892008-12-09 22:54:47 +00001154 SUnit *SuccSU = I->getSUnit();
1155 if (SuccSU->isScheduled) {
1156 SDep D = *I;
1157 D.setSUnit(CopyToSU);
1158 AddPred(SuccSU, D);
1159 DelDeps.push_back(std::make_pair(SuccSU, *I));
Evan Cheng8e136a92007-09-26 21:36:17 +00001160 }
Andrew Trick13acae02011-03-23 20:42:39 +00001161 else {
1162 // Avoid scheduling the def-side copy before other successors. Otherwise
1163 // we could introduce another physreg interference on the copy and
1164 // continue inserting copies indefinitely.
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001165 AddPred(SuccSU, SDep(CopyFromSU, SDep::Artificial));
Andrew Trick13acae02011-03-23 20:42:39 +00001166 }
Evan Cheng8e136a92007-09-26 21:36:17 +00001167 }
Evan Chengb2c42c62009-01-12 03:19:55 +00001168 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +00001169 RemovePred(DelDeps[i].first, DelDeps[i].second);
Evan Cheng8e136a92007-09-26 21:36:17 +00001170
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001171 SDep FromDep(SU, SDep::Data, Reg);
1172 FromDep.setLatency(SU->Latency);
1173 AddPred(CopyFromSU, FromDep);
1174 SDep ToDep(CopyFromSU, SDep::Data, 0);
1175 ToDep.setLatency(CopyFromSU->Latency);
1176 AddPred(CopyToSU, ToDep);
Evan Cheng8e136a92007-09-26 21:36:17 +00001177
1178 AvailableQueue->updateNode(SU);
1179 AvailableQueue->addNode(CopyFromSU);
1180 AvailableQueue->addNode(CopyToSU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001181 Copies.push_back(CopyFromSU);
1182 Copies.push_back(CopyToSU);
Evan Cheng8e136a92007-09-26 21:36:17 +00001183
Evan Chengb2c42c62009-01-12 03:19:55 +00001184 ++NumPRCopies;
Evan Cheng8e136a92007-09-26 21:36:17 +00001185}
1186
1187/// getPhysicalRegisterVT - Returns the ValueType of the physical register
1188/// definition of the specified node.
1189/// FIXME: Move to SelectionDAG?
Owen Anderson53aa7a92009-08-10 22:56:29 +00001190static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Duncan Sands13237ac2008-06-06 12:08:01 +00001191 const TargetInstrInfo *TII) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001192 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1193 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
1194 unsigned NumRes = MCID.getNumDefs();
Craig Topper5a4bcc72012-03-08 08:22:45 +00001195 for (const uint16_t *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Evan Cheng8e136a92007-09-26 21:36:17 +00001196 if (Reg == *ImpDef)
1197 break;
1198 ++NumRes;
1199 }
1200 return N->getValueType(NumRes);
1201}
1202
Evan Chengb8905c42009-03-04 01:41:49 +00001203/// CheckForLiveRegDef - Return true and update live register vector if the
1204/// specified register def of the specified SUnit clobbers any "live" registers.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001205static void CheckForLiveRegDef(SUnit *SU, unsigned Reg,
Evan Chengb8905c42009-03-04 01:41:49 +00001206 std::vector<SUnit*> &LiveRegDefs,
1207 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +00001208 SmallVectorImpl<unsigned> &LRegs,
Evan Chengb8905c42009-03-04 01:41:49 +00001209 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001210 for (MCRegAliasIterator AliasI(Reg, TRI, true); AliasI.isValid(); ++AliasI) {
Andrew Trick12acde112010-12-23 03:43:21 +00001211
1212 // Check if Ref is live.
Andrew Trick0af2e472011-06-07 00:38:12 +00001213 if (!LiveRegDefs[*AliasI]) continue;
Andrew Trick12acde112010-12-23 03:43:21 +00001214
1215 // Allow multiple uses of the same def.
Andrew Trick0af2e472011-06-07 00:38:12 +00001216 if (LiveRegDefs[*AliasI] == SU) continue;
Andrew Trick12acde112010-12-23 03:43:21 +00001217
1218 // Add Reg to the set of interfering live regs.
Andrew Trick0af2e472011-06-07 00:38:12 +00001219 if (RegAdded.insert(*AliasI)) {
Andrew Trick0af2e472011-06-07 00:38:12 +00001220 LRegs.push_back(*AliasI);
1221 }
Evan Chengb8905c42009-03-04 01:41:49 +00001222 }
Evan Chengb8905c42009-03-04 01:41:49 +00001223}
1224
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001225/// CheckForLiveRegDefMasked - Check for any live physregs that are clobbered
1226/// by RegMask, and add them to LRegs.
1227static void CheckForLiveRegDefMasked(SUnit *SU, const uint32_t *RegMask,
1228 std::vector<SUnit*> &LiveRegDefs,
1229 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +00001230 SmallVectorImpl<unsigned> &LRegs) {
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001231 // Look at all live registers. Skip Reg0 and the special CallResource.
1232 for (unsigned i = 1, e = LiveRegDefs.size()-1; i != e; ++i) {
1233 if (!LiveRegDefs[i]) continue;
1234 if (LiveRegDefs[i] == SU) continue;
1235 if (!MachineOperand::clobbersPhysReg(RegMask, i)) continue;
1236 if (RegAdded.insert(i))
1237 LRegs.push_back(i);
1238 }
1239}
1240
1241/// getNodeRegMask - Returns the register mask attached to an SDNode, if any.
1242static const uint32_t *getNodeRegMask(const SDNode *N) {
1243 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1244 if (const RegisterMaskSDNode *Op =
1245 dyn_cast<RegisterMaskSDNode>(N->getOperand(i).getNode()))
1246 return Op->getRegMask();
Craig Topperc0196b12014-04-14 00:51:57 +00001247 return nullptr;
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001248}
1249
Evan Cheng5924bf72007-09-25 01:54:36 +00001250/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
1251/// scheduling of the given node to satisfy live physical register dependencies.
1252/// If the specific node is the last one that's available to schedule, do
1253/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
Chris Lattner0cfe8842010-12-20 00:51:56 +00001254bool ScheduleDAGRRList::
Craig Topperb94011f2013-07-14 04:42:23 +00001255DelayForLiveRegsBottomUp(SUnit *SU, SmallVectorImpl<unsigned> &LRegs) {
Dan Gohmanc07f6862008-09-23 18:50:48 +00001256 if (NumLiveRegs == 0)
Evan Cheng5924bf72007-09-25 01:54:36 +00001257 return false;
1258
Evan Chenge6f92252007-09-27 18:46:06 +00001259 SmallSet<unsigned, 4> RegAdded;
Evan Cheng5924bf72007-09-25 01:54:36 +00001260 // If this node would clobber any "live" register, then it's not ready.
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001261 //
1262 // If SU is the currently live definition of the same register that it uses,
1263 // then we are free to schedule it.
Evan Cheng5924bf72007-09-25 01:54:36 +00001264 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1265 I != E; ++I) {
Andrew Trickfbb3ed82010-12-21 22:27:44 +00001266 if (I->isAssignedRegDep() && LiveRegDefs[I->getReg()] != SU)
Evan Chengb8905c42009-03-04 01:41:49 +00001267 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
1268 RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001269 }
1270
Chris Lattner11a33812010-12-23 17:24:32 +00001271 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Evan Chengb8905c42009-03-04 01:41:49 +00001272 if (Node->getOpcode() == ISD::INLINEASM) {
1273 // Inline asm can clobber physical defs.
1274 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001275 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +00001276 --NumOps; // Ignore the glue operand.
Evan Chengb8905c42009-03-04 01:41:49 +00001277
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001278 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Evan Chengb8905c42009-03-04 01:41:49 +00001279 unsigned Flags =
1280 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001281 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Evan Chengb8905c42009-03-04 01:41:49 +00001282
1283 ++i; // Skip the ID value.
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001284 if (InlineAsm::isRegDefKind(Flags) ||
Jakob Stoklund Olesen537a3022011-06-27 04:08:33 +00001285 InlineAsm::isRegDefEarlyClobberKind(Flags) ||
1286 InlineAsm::isClobberKind(Flags)) {
Evan Chengb8905c42009-03-04 01:41:49 +00001287 // Check for def of register or earlyclobber register.
1288 for (; NumVals; --NumVals, ++i) {
1289 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1290 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1291 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
1292 }
1293 } else
1294 i += NumVals;
1295 }
1296 continue;
1297 }
1298
Dan Gohman072734e2008-11-13 23:24:17 +00001299 if (!Node->isMachineOpcode())
Evan Cheng5924bf72007-09-25 01:54:36 +00001300 continue;
Dan Gohman198b7ff2011-11-03 21:49:52 +00001301 // If we're in the middle of scheduling a call, don't begin scheduling
1302 // another call. Also, don't allow any physical registers to be live across
1303 // the call.
1304 if (Node->getMachineOpcode() == (unsigned)TII->getCallFrameDestroyOpcode()) {
1305 // Check the special calling-sequence resource.
1306 unsigned CallResource = TRI->getNumRegs();
1307 if (LiveRegDefs[CallResource]) {
1308 SDNode *Gen = LiveRegGens[CallResource]->getNode();
1309 while (SDNode *Glued = Gen->getGluedNode())
1310 Gen = Glued;
1311 if (!IsChainDependent(Gen, Node, 0, TII) && RegAdded.insert(CallResource))
1312 LRegs.push_back(CallResource);
1313 }
1314 }
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00001315 if (const uint32_t *RegMask = getNodeRegMask(Node))
1316 CheckForLiveRegDefMasked(SU, RegMask, LiveRegDefs, RegAdded, LRegs);
1317
Evan Cheng6cc775f2011-06-28 19:10:37 +00001318 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
1319 if (!MCID.ImplicitDefs)
Evan Cheng5924bf72007-09-25 01:54:36 +00001320 continue;
Craig Topper5a4bcc72012-03-08 08:22:45 +00001321 for (const uint16_t *Reg = MCID.getImplicitDefs(); *Reg; ++Reg)
Evan Chengb8905c42009-03-04 01:41:49 +00001322 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Evan Cheng5924bf72007-09-25 01:54:36 +00001323 }
Andrew Trick2085a962010-12-21 22:25:04 +00001324
Evan Cheng5924bf72007-09-25 01:54:36 +00001325 return !LRegs.empty();
Evan Chengd38c22b2006-05-11 23:55:42 +00001326}
1327
Andrew Trick7cf43612013-02-25 19:11:48 +00001328void ScheduleDAGRRList::releaseInterferences(unsigned Reg) {
1329 // Add the nodes that aren't ready back onto the available list.
1330 for (unsigned i = Interferences.size(); i > 0; --i) {
1331 SUnit *SU = Interferences[i-1];
1332 LRegsMapT::iterator LRegsPos = LRegsMap.find(SU);
1333 if (Reg) {
Craig Topperb94011f2013-07-14 04:42:23 +00001334 SmallVectorImpl<unsigned> &LRegs = LRegsPos->second;
Andrew Trick7cf43612013-02-25 19:11:48 +00001335 if (std::find(LRegs.begin(), LRegs.end(), Reg) == LRegs.end())
1336 continue;
1337 }
1338 SU->isPending = false;
1339 // The interfering node may no longer be available due to backtracking.
1340 // Furthermore, it may have been made available again, in which case it is
1341 // now already in the AvailableQueue.
1342 if (SU->isAvailable && !SU->NodeQueueId) {
1343 DEBUG(dbgs() << " Repushing SU #" << SU->NodeNum << '\n');
1344 AvailableQueue->push(SU);
1345 }
1346 if (i < Interferences.size())
1347 Interferences[i-1] = Interferences.back();
1348 Interferences.pop_back();
1349 LRegsMap.erase(LRegsPos);
1350 }
1351}
1352
Andrew Trick528fad92010-12-23 05:42:20 +00001353/// Return a node that can be scheduled in this cycle. Requirements:
1354/// (1) Ready: latency has been satisfied
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001355/// (2) No Hazards: resources are available
Andrew Trick528fad92010-12-23 05:42:20 +00001356/// (3) No Interferences: may unschedule to break register interferences.
1357SUnit *ScheduleDAGRRList::PickNodeToScheduleBottomUp() {
Craig Topperc0196b12014-04-14 00:51:57 +00001358 SUnit *CurSU = AvailableQueue->empty() ? nullptr : AvailableQueue->pop();
Andrew Trick528fad92010-12-23 05:42:20 +00001359 while (CurSU) {
1360 SmallVector<unsigned, 4> LRegs;
1361 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
1362 break;
Andrew Trick0f23b762013-03-07 19:21:08 +00001363 DEBUG(dbgs() << " Interfering reg " <<
1364 (LRegs[0] == TRI->getNumRegs() ? "CallResource"
1365 : TRI->getName(LRegs[0]))
1366 << " SU #" << CurSU->NodeNum << '\n');
Andrew Trick7cf43612013-02-25 19:11:48 +00001367 std::pair<LRegsMapT::iterator, bool> LRegsPair =
1368 LRegsMap.insert(std::make_pair(CurSU, LRegs));
1369 if (LRegsPair.second) {
1370 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
1371 Interferences.push_back(CurSU);
1372 }
1373 else {
1374 assert(CurSU->isPending && "Intereferences are pending");
1375 // Update the interference with current live regs.
1376 LRegsPair.first->second = LRegs;
1377 }
Andrew Trick528fad92010-12-23 05:42:20 +00001378 CurSU = AvailableQueue->pop();
1379 }
Andrew Trick7cf43612013-02-25 19:11:48 +00001380 if (CurSU)
Andrew Trick528fad92010-12-23 05:42:20 +00001381 return CurSU;
Andrew Trick528fad92010-12-23 05:42:20 +00001382
1383 // All candidates are delayed due to live physical reg dependencies.
1384 // Try backtracking, code duplication, or inserting cross class copies
1385 // to resolve it.
1386 for (unsigned i = 0, e = Interferences.size(); i != e; ++i) {
1387 SUnit *TrySU = Interferences[i];
Craig Topperb94011f2013-07-14 04:42:23 +00001388 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Andrew Trick528fad92010-12-23 05:42:20 +00001389
1390 // Try unscheduling up to the point where it's safe to schedule
1391 // this node.
Craig Topperc0196b12014-04-14 00:51:57 +00001392 SUnit *BtSU = nullptr;
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001393 unsigned LiveCycle = UINT_MAX;
Andrew Trick528fad92010-12-23 05:42:20 +00001394 for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) {
1395 unsigned Reg = LRegs[j];
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001396 if (LiveRegGens[Reg]->getHeight() < LiveCycle) {
1397 BtSU = LiveRegGens[Reg];
1398 LiveCycle = BtSU->getHeight();
1399 }
Andrew Trick528fad92010-12-23 05:42:20 +00001400 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001401 if (!WillCreateCycle(TrySU, BtSU)) {
Andrew Trick7cf43612013-02-25 19:11:48 +00001402 // BacktrackBottomUp mutates Interferences!
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001403 BacktrackBottomUp(TrySU, BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001404
1405 // Force the current node to be scheduled before the node that
1406 // requires the physical reg dep.
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001407 if (BtSU->isAvailable) {
1408 BtSU->isAvailable = false;
1409 if (!BtSU->isPending)
1410 AvailableQueue->remove(BtSU);
Andrew Trick528fad92010-12-23 05:42:20 +00001411 }
Andrew Trick7cf43612013-02-25 19:11:48 +00001412 DEBUG(dbgs() << "ARTIFICIAL edge from SU(" << BtSU->NodeNum << ") to SU("
1413 << TrySU->NodeNum << ")\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001414 AddPred(TrySU, SDep(BtSU, SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001415
1416 // If one or more successors has been unscheduled, then the current
Andrew Trick7cf43612013-02-25 19:11:48 +00001417 // node is no longer available.
1418 if (!TrySU->isAvailable)
Andrew Trick528fad92010-12-23 05:42:20 +00001419 CurSU = AvailableQueue->pop();
Andrew Trick528fad92010-12-23 05:42:20 +00001420 else {
Andrew Trick7cf43612013-02-25 19:11:48 +00001421 AvailableQueue->remove(TrySU);
Andrew Trick528fad92010-12-23 05:42:20 +00001422 CurSU = TrySU;
Andrew Trick528fad92010-12-23 05:42:20 +00001423 }
Andrew Trick7cf43612013-02-25 19:11:48 +00001424 // Interferences has been mutated. We must break.
Andrew Trick528fad92010-12-23 05:42:20 +00001425 break;
1426 }
1427 }
1428
1429 if (!CurSU) {
1430 // Can't backtrack. If it's too expensive to copy the value, then try
1431 // duplicate the nodes that produces these "too expensive to copy"
1432 // values to break the dependency. In case even that doesn't work,
1433 // insert cross class copies.
1434 // If it's not too expensive, i.e. cost != -1, issue copies.
1435 SUnit *TrySU = Interferences[0];
Craig Topperb94011f2013-07-14 04:42:23 +00001436 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Andrew Trick528fad92010-12-23 05:42:20 +00001437 assert(LRegs.size() == 1 && "Can't handle this yet!");
1438 unsigned Reg = LRegs[0];
1439 SUnit *LRDef = LiveRegDefs[Reg];
1440 EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
1441 const TargetRegisterClass *RC =
1442 TRI->getMinimalPhysRegClass(Reg, VT);
1443 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
1444
Evan Chengb4c6a342011-03-10 00:16:32 +00001445 // If cross copy register class is the same as RC, then it must be possible
1446 // copy the value directly. Do not try duplicate the def.
1447 // If cross copy register class is not the same as RC, then it's possible to
1448 // copy the value but it require cross register class copies and it is
1449 // expensive.
1450 // If cross copy register class is null, then it's not possible to copy
1451 // the value at all.
Craig Topperc0196b12014-04-14 00:51:57 +00001452 SUnit *NewDef = nullptr;
Evan Chengb4c6a342011-03-10 00:16:32 +00001453 if (DestRC != RC) {
Andrew Trick528fad92010-12-23 05:42:20 +00001454 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +00001455 if (!DestRC && !NewDef)
1456 report_fatal_error("Can't handle live physical register dependency!");
1457 }
Andrew Trick528fad92010-12-23 05:42:20 +00001458 if (!NewDef) {
1459 // Issue copies, these can be expensive cross register class copies.
1460 SmallVector<SUnit*, 2> Copies;
1461 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
1462 DEBUG(dbgs() << " Adding an edge from SU #" << TrySU->NodeNum
1463 << " to SU #" << Copies.front()->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001464 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001465 NewDef = Copies.back();
1466 }
1467
1468 DEBUG(dbgs() << " Adding an edge from SU #" << NewDef->NodeNum
1469 << " to SU #" << TrySU->NodeNum << "\n");
1470 LiveRegDefs[Reg] = NewDef;
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001471 AddPred(NewDef, SDep(TrySU, SDep::Artificial));
Andrew Trick528fad92010-12-23 05:42:20 +00001472 TrySU->isAvailable = false;
1473 CurSU = NewDef;
1474 }
Andrew Trick528fad92010-12-23 05:42:20 +00001475 assert(CurSU && "Unable to resolve live physical register dependencies!");
Andrew Trick528fad92010-12-23 05:42:20 +00001476 return CurSU;
1477}
Evan Cheng1ec79b42007-09-27 07:09:03 +00001478
Evan Chengd38c22b2006-05-11 23:55:42 +00001479/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
1480/// schedulers.
1481void ScheduleDAGRRList::ListScheduleBottomUp() {
Dan Gohmanb9543432009-02-10 23:27:53 +00001482 // Release any predecessors of the special Exit node.
Andrew Tricka52f3252010-12-23 04:16:14 +00001483 ReleasePredecessors(&ExitSU);
Dan Gohmanb9543432009-02-10 23:27:53 +00001484
Evan Chengd38c22b2006-05-11 23:55:42 +00001485 // Add root to Available queue.
Dan Gohman4370f262008-04-15 01:22:18 +00001486 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +00001487 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman4370f262008-04-15 01:22:18 +00001488 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
1489 RootSU->isAvailable = true;
1490 AvailableQueue->push(RootSU);
1491 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001492
1493 // While Available queue is not empty, grab the node with the highest
Dan Gohman54a187e2007-08-20 19:28:38 +00001494 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmane6e13482008-06-21 15:52:51 +00001495 Sequence.reserve(SUnits.size());
Andrew Trick7cf43612013-02-25 19:11:48 +00001496 while (!AvailableQueue->empty() || !Interferences.empty()) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00001497 DEBUG(dbgs() << "\nExamining Available:\n";
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001498 AvailableQueue->dump(this));
1499
Andrew Trick528fad92010-12-23 05:42:20 +00001500 // Pick the best node to schedule taking all constraints into
1501 // consideration.
1502 SUnit *SU = PickNodeToScheduleBottomUp();
Evan Cheng1ec79b42007-09-27 07:09:03 +00001503
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001504 AdvancePastStalls(SU);
Evan Cheng1ec79b42007-09-27 07:09:03 +00001505
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001506 ScheduleNodeBottomUp(SU);
1507
1508 while (AvailableQueue->empty() && !PendingQueue.empty()) {
1509 // Advance the cycle to free resources. Skip ahead to the next ready SU.
1510 assert(MinAvailableCycle < UINT_MAX && "MinAvailableCycle uninitialized");
1511 AdvanceToCycle(std::max(CurCycle + 1, MinAvailableCycle));
1512 }
Evan Chengd38c22b2006-05-11 23:55:42 +00001513 }
1514
Evan Chengd38c22b2006-05-11 23:55:42 +00001515 // Reverse the order if it is bottom up.
1516 std::reverse(Sequence.begin(), Sequence.end());
Andrew Trick2085a962010-12-21 22:25:04 +00001517
Evan Chengd38c22b2006-05-11 23:55:42 +00001518#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +00001519 VerifyScheduledSequence(/*isBottomUp=*/true);
Evan Chengd38c22b2006-05-11 23:55:42 +00001520#endif
1521}
1522
1523//===----------------------------------------------------------------------===//
Andrew Trick9ccce772011-01-14 21:11:41 +00001524// RegReductionPriorityQueue Definition
Evan Chengd38c22b2006-05-11 23:55:42 +00001525//===----------------------------------------------------------------------===//
1526//
1527// This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
1528// to reduce register pressure.
Andrew Trick2085a962010-12-21 22:25:04 +00001529//
Evan Chengd38c22b2006-05-11 23:55:42 +00001530namespace {
Andrew Trick9ccce772011-01-14 21:11:41 +00001531class RegReductionPQBase;
Andrew Trick2085a962010-12-21 22:25:04 +00001532
Andrew Trick9ccce772011-01-14 21:11:41 +00001533struct queue_sort : public std::binary_function<SUnit*, SUnit*, bool> {
1534 bool isReady(SUnit* SU, unsigned CurCycle) const { return true; }
1535};
1536
Andrew Trick3013b6a2011-06-15 17:16:12 +00001537#ifndef NDEBUG
1538template<class SF>
1539struct reverse_sort : public queue_sort {
1540 SF &SortFunc;
1541 reverse_sort(SF &sf) : SortFunc(sf) {}
Andrew Trick3013b6a2011-06-15 17:16:12 +00001542
1543 bool operator()(SUnit* left, SUnit* right) const {
1544 // reverse left/right rather than simply !SortFunc(left, right)
1545 // to expose different paths in the comparison logic.
1546 return SortFunc(right, left);
1547 }
1548};
1549#endif // NDEBUG
1550
Andrew Trick9ccce772011-01-14 21:11:41 +00001551/// bu_ls_rr_sort - Priority function for bottom up register pressure
1552// reduction scheduler.
1553struct bu_ls_rr_sort : public queue_sort {
1554 enum {
1555 IsBottomUp = true,
1556 HasReadyFilter = false
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001557 };
1558
Andrew Trick9ccce772011-01-14 21:11:41 +00001559 RegReductionPQBase *SPQ;
1560 bu_ls_rr_sort(RegReductionPQBase *spq) : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001561
Andrew Trick9ccce772011-01-14 21:11:41 +00001562 bool operator()(SUnit* left, SUnit* right) const;
1563};
Andrew Trick2085a962010-12-21 22:25:04 +00001564
Andrew Trick9ccce772011-01-14 21:11:41 +00001565// src_ls_rr_sort - Priority function for source order scheduler.
1566struct src_ls_rr_sort : public queue_sort {
1567 enum {
1568 IsBottomUp = true,
1569 HasReadyFilter = false
Evan Chengd38c22b2006-05-11 23:55:42 +00001570 };
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001571
Andrew Trick9ccce772011-01-14 21:11:41 +00001572 RegReductionPQBase *SPQ;
1573 src_ls_rr_sort(RegReductionPQBase *spq)
1574 : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001575
Andrew Trick9ccce772011-01-14 21:11:41 +00001576 bool operator()(SUnit* left, SUnit* right) const;
1577};
Andrew Trick2085a962010-12-21 22:25:04 +00001578
Andrew Trick9ccce772011-01-14 21:11:41 +00001579// hybrid_ls_rr_sort - Priority function for hybrid scheduler.
1580struct hybrid_ls_rr_sort : public queue_sort {
1581 enum {
1582 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001583 HasReadyFilter = false
Bill Wendling8cbc25d2010-01-23 10:26:57 +00001584 };
Evan Chengbdd062d2010-05-20 06:13:19 +00001585
Andrew Trick9ccce772011-01-14 21:11:41 +00001586 RegReductionPQBase *SPQ;
1587 hybrid_ls_rr_sort(RegReductionPQBase *spq)
1588 : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001589
Andrew Trick9ccce772011-01-14 21:11:41 +00001590 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Chenga77f3d32010-07-21 06:09:07 +00001591
Andrew Trick9ccce772011-01-14 21:11:41 +00001592 bool operator()(SUnit* left, SUnit* right) const;
1593};
1594
1595// ilp_ls_rr_sort - Priority function for ILP (instruction level parallelism)
1596// scheduler.
1597struct ilp_ls_rr_sort : public queue_sort {
1598 enum {
1599 IsBottomUp = true,
Andrew Trickc88b7ec2011-03-04 02:03:45 +00001600 HasReadyFilter = false
Evan Chengbdd062d2010-05-20 06:13:19 +00001601 };
Evan Cheng37b740c2010-07-24 00:39:05 +00001602
Andrew Trick9ccce772011-01-14 21:11:41 +00001603 RegReductionPQBase *SPQ;
1604 ilp_ls_rr_sort(RegReductionPQBase *spq)
1605 : SPQ(spq) {}
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001606
Andrew Trick9ccce772011-01-14 21:11:41 +00001607 bool isReady(SUnit *SU, unsigned CurCycle) const;
Evan Cheng37b740c2010-07-24 00:39:05 +00001608
Andrew Trick9ccce772011-01-14 21:11:41 +00001609 bool operator()(SUnit* left, SUnit* right) const;
1610};
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001611
Andrew Trick9ccce772011-01-14 21:11:41 +00001612class RegReductionPQBase : public SchedulingPriorityQueue {
1613protected:
1614 std::vector<SUnit*> Queue;
1615 unsigned CurQueueId;
1616 bool TracksRegPressure;
Evan Cheng8ab58a22012-03-22 19:31:17 +00001617 bool SrcOrder;
Andrew Trick9ccce772011-01-14 21:11:41 +00001618
1619 // SUnits - The SUnits for the current graph.
1620 std::vector<SUnit> *SUnits;
1621
1622 MachineFunction &MF;
1623 const TargetInstrInfo *TII;
1624 const TargetRegisterInfo *TRI;
1625 const TargetLowering *TLI;
1626 ScheduleDAGRRList *scheduleDAG;
1627
1628 // SethiUllmanNumbers - The SethiUllman number for each node.
1629 std::vector<unsigned> SethiUllmanNumbers;
1630
1631 /// RegPressure - Tracking current reg pressure per register class.
1632 ///
1633 std::vector<unsigned> RegPressure;
1634
1635 /// RegLimit - Tracking the number of allocatable registers per register
1636 /// class.
1637 std::vector<unsigned> RegLimit;
1638
1639public:
1640 RegReductionPQBase(MachineFunction &mf,
1641 bool hasReadyFilter,
1642 bool tracksrp,
Evan Cheng8ab58a22012-03-22 19:31:17 +00001643 bool srcorder,
Andrew Trick9ccce772011-01-14 21:11:41 +00001644 const TargetInstrInfo *tii,
1645 const TargetRegisterInfo *tri,
1646 const TargetLowering *tli)
1647 : SchedulingPriorityQueue(hasReadyFilter),
Evan Cheng8ab58a22012-03-22 19:31:17 +00001648 CurQueueId(0), TracksRegPressure(tracksrp), SrcOrder(srcorder),
Craig Topperc0196b12014-04-14 00:51:57 +00001649 MF(mf), TII(tii), TRI(tri), TLI(tli), scheduleDAG(nullptr) {
Andrew Trick9ccce772011-01-14 21:11:41 +00001650 if (TracksRegPressure) {
1651 unsigned NumRC = TRI->getNumRegClasses();
1652 RegLimit.resize(NumRC);
1653 RegPressure.resize(NumRC);
1654 std::fill(RegLimit.begin(), RegLimit.end(), 0);
1655 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1656 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1657 E = TRI->regclass_end(); I != E; ++I)
Cameron Zwarichdf616942011-03-07 21:56:36 +00001658 RegLimit[(*I)->getID()] = tri->getRegPressureLimit(*I, MF);
Andrew Trick9ccce772011-01-14 21:11:41 +00001659 }
1660 }
1661
1662 void setScheduleDAG(ScheduleDAGRRList *scheduleDag) {
1663 scheduleDAG = scheduleDag;
1664 }
1665
1666 ScheduleHazardRecognizer* getHazardRec() {
1667 return scheduleDAG->getHazardRec();
1668 }
1669
Craig Topper7b883b32014-03-08 06:31:39 +00001670 void initNodes(std::vector<SUnit> &sunits) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001671
Craig Topper7b883b32014-03-08 06:31:39 +00001672 void addNode(const SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001673
Craig Topper7b883b32014-03-08 06:31:39 +00001674 void updateNode(const SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001675
Craig Topper7b883b32014-03-08 06:31:39 +00001676 void releaseState() override {
Craig Topperc0196b12014-04-14 00:51:57 +00001677 SUnits = nullptr;
Andrew Trick9ccce772011-01-14 21:11:41 +00001678 SethiUllmanNumbers.clear();
1679 std::fill(RegPressure.begin(), RegPressure.end(), 0);
1680 }
1681
1682 unsigned getNodePriority(const SUnit *SU) const;
1683
1684 unsigned getNodeOrdering(const SUnit *SU) const {
Andrew Trick3bd8b7a2011-03-25 06:40:55 +00001685 if (!SU->getNode()) return 0;
1686
Andrew Tricke2431c62013-05-25 03:08:10 +00001687 return SU->getNode()->getIROrder();
Andrew Trick9ccce772011-01-14 21:11:41 +00001688 }
1689
Craig Topper7b883b32014-03-08 06:31:39 +00001690 bool empty() const override { return Queue.empty(); }
Andrew Trick9ccce772011-01-14 21:11:41 +00001691
Craig Topper7b883b32014-03-08 06:31:39 +00001692 void push(SUnit *U) override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001693 assert(!U->NodeQueueId && "Node in the queue already");
1694 U->NodeQueueId = ++CurQueueId;
1695 Queue.push_back(U);
1696 }
1697
Craig Topper7b883b32014-03-08 06:31:39 +00001698 void remove(SUnit *SU) override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001699 assert(!Queue.empty() && "Queue is empty!");
1700 assert(SU->NodeQueueId != 0 && "Not in queue!");
1701 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(),
1702 SU);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001703 if (I != std::prev(Queue.end()))
Andrew Trick9ccce772011-01-14 21:11:41 +00001704 std::swap(*I, Queue.back());
1705 Queue.pop_back();
1706 SU->NodeQueueId = 0;
1707 }
1708
Craig Topper7b883b32014-03-08 06:31:39 +00001709 bool tracksRegPressure() const override { return TracksRegPressure; }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001710
Andrew Trick9ccce772011-01-14 21:11:41 +00001711 void dumpRegPressure() const;
1712
1713 bool HighRegPressure(const SUnit *SU) const;
1714
Andrew Trick641e2d42011-03-05 08:00:22 +00001715 bool MayReduceRegPressure(SUnit *SU) const;
1716
1717 int RegPressureDiff(SUnit *SU, unsigned &LiveUses) const;
Andrew Trick9ccce772011-01-14 21:11:41 +00001718
Craig Topper7b883b32014-03-08 06:31:39 +00001719 void scheduledNode(SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001720
Craig Topper7b883b32014-03-08 06:31:39 +00001721 void unscheduledNode(SUnit *SU) override;
Andrew Trick9ccce772011-01-14 21:11:41 +00001722
1723protected:
1724 bool canClobber(const SUnit *SU, const SUnit *Op);
Duncan Sands635e4ef2011-11-09 14:20:48 +00001725 void AddPseudoTwoAddrDeps();
Andrew Trick9ccce772011-01-14 21:11:41 +00001726 void PrescheduleNodesWithMultipleUses();
1727 void CalculateSethiUllmanNumbers();
1728};
1729
1730template<class SF>
Andrew Trick3013b6a2011-06-15 17:16:12 +00001731static SUnit *popFromQueueImpl(std::vector<SUnit*> &Q, SF &Picker) {
1732 std::vector<SUnit *>::iterator Best = Q.begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001733 for (std::vector<SUnit *>::iterator I = std::next(Q.begin()),
Andrew Trick3013b6a2011-06-15 17:16:12 +00001734 E = Q.end(); I != E; ++I)
1735 if (Picker(*Best, *I))
1736 Best = I;
1737 SUnit *V = *Best;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001738 if (Best != std::prev(Q.end()))
Andrew Trick3013b6a2011-06-15 17:16:12 +00001739 std::swap(*Best, Q.back());
1740 Q.pop_back();
1741 return V;
1742}
Andrew Trick9ccce772011-01-14 21:11:41 +00001743
Andrew Trick3013b6a2011-06-15 17:16:12 +00001744template<class SF>
1745SUnit *popFromQueue(std::vector<SUnit*> &Q, SF &Picker, ScheduleDAG *DAG) {
1746#ifndef NDEBUG
1747 if (DAG->StressSched) {
1748 reverse_sort<SF> RPicker(Picker);
1749 return popFromQueueImpl(Q, RPicker);
1750 }
1751#endif
1752 (void)DAG;
1753 return popFromQueueImpl(Q, Picker);
1754}
1755
1756template<class SF>
1757class RegReductionPriorityQueue : public RegReductionPQBase {
Andrew Trick9ccce772011-01-14 21:11:41 +00001758 SF Picker;
1759
1760public:
1761 RegReductionPriorityQueue(MachineFunction &mf,
1762 bool tracksrp,
Evan Cheng8ab58a22012-03-22 19:31:17 +00001763 bool srcorder,
Andrew Trick9ccce772011-01-14 21:11:41 +00001764 const TargetInstrInfo *tii,
1765 const TargetRegisterInfo *tri,
1766 const TargetLowering *tli)
Evan Cheng8ab58a22012-03-22 19:31:17 +00001767 : RegReductionPQBase(mf, SF::HasReadyFilter, tracksrp, srcorder,
1768 tii, tri, tli),
Andrew Trick9ccce772011-01-14 21:11:41 +00001769 Picker(this) {}
1770
Craig Topper7b883b32014-03-08 06:31:39 +00001771 bool isBottomUp() const override { return SF::IsBottomUp; }
Andrew Trick9ccce772011-01-14 21:11:41 +00001772
Craig Topper7b883b32014-03-08 06:31:39 +00001773 bool isReady(SUnit *U) const override {
Andrew Trick9ccce772011-01-14 21:11:41 +00001774 return Picker.HasReadyFilter && Picker.isReady(U, getCurCycle());
1775 }
1776
Craig Topper7b883b32014-03-08 06:31:39 +00001777 SUnit *pop() override {
Craig Topperc0196b12014-04-14 00:51:57 +00001778 if (Queue.empty()) return nullptr;
Andrew Trick9ccce772011-01-14 21:11:41 +00001779
Andrew Trick3013b6a2011-06-15 17:16:12 +00001780 SUnit *V = popFromQueue(Queue, Picker, scheduleDAG);
Andrew Trick9ccce772011-01-14 21:11:41 +00001781 V->NodeQueueId = 0;
1782 return V;
1783 }
1784
Manman Ren19f49ac2012-09-11 22:23:19 +00001785#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick9ccce772011-01-14 21:11:41 +00001786 void dump(ScheduleDAG *DAG) const {
1787 // Emulate pop() without clobbering NodeQueueIds.
1788 std::vector<SUnit*> DumpQueue = Queue;
1789 SF DumpPicker = Picker;
1790 while (!DumpQueue.empty()) {
Andrew Trick3013b6a2011-06-15 17:16:12 +00001791 SUnit *SU = popFromQueue(DumpQueue, DumpPicker, scheduleDAG);
Dan Gohman90fb5522011-10-20 21:44:34 +00001792 dbgs() << "Height " << SU->getHeight() << ": ";
Andrew Trick9ccce772011-01-14 21:11:41 +00001793 SU->dump(DAG);
1794 }
1795 }
Manman Ren742534c2012-09-06 19:06:06 +00001796#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001797};
1798
1799typedef RegReductionPriorityQueue<bu_ls_rr_sort>
1800BURegReductionPriorityQueue;
1801
Andrew Trick9ccce772011-01-14 21:11:41 +00001802typedef RegReductionPriorityQueue<src_ls_rr_sort>
1803SrcRegReductionPriorityQueue;
1804
1805typedef RegReductionPriorityQueue<hybrid_ls_rr_sort>
1806HybridBURRPriorityQueue;
1807
1808typedef RegReductionPriorityQueue<ilp_ls_rr_sort>
1809ILPBURRPriorityQueue;
1810} // end anonymous namespace
1811
1812//===----------------------------------------------------------------------===//
1813// Static Node Priority for Register Pressure Reduction
1814//===----------------------------------------------------------------------===//
Evan Chengd38c22b2006-05-11 23:55:42 +00001815
Andrew Trickbfbd9722011-04-14 05:15:06 +00001816// Check for special nodes that bypass scheduling heuristics.
1817// Currently this pushes TokenFactor nodes down, but may be used for other
1818// pseudo-ops as well.
1819//
1820// Return -1 to schedule right above left, 1 for left above right.
1821// Return 0 if no bias exists.
1822static int checkSpecialNodes(const SUnit *left, const SUnit *right) {
1823 bool LSchedLow = left->isScheduleLow;
1824 bool RSchedLow = right->isScheduleLow;
1825 if (LSchedLow != RSchedLow)
1826 return LSchedLow < RSchedLow ? 1 : -1;
1827 return 0;
1828}
1829
Dan Gohman186f65d2008-11-20 03:30:37 +00001830/// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number.
1831/// Smaller number is the higher priority.
Evan Cheng7e4abde2008-07-02 09:23:51 +00001832static unsigned
Dan Gohman186f65d2008-11-20 03:30:37 +00001833CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
Evan Cheng7e4abde2008-07-02 09:23:51 +00001834 unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
1835 if (SethiUllmanNumber != 0)
1836 return SethiUllmanNumber;
1837
1838 unsigned Extra = 0;
1839 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
1840 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00001841 if (I->isCtrl()) continue; // ignore chain preds
1842 SUnit *PredSU = I->getSUnit();
Dan Gohman186f65d2008-11-20 03:30:37 +00001843 unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
Evan Cheng7e4abde2008-07-02 09:23:51 +00001844 if (PredSethiUllman > SethiUllmanNumber) {
1845 SethiUllmanNumber = PredSethiUllman;
1846 Extra = 0;
Evan Cheng3a14efa2009-02-12 08:59:45 +00001847 } else if (PredSethiUllman == SethiUllmanNumber)
Evan Cheng7e4abde2008-07-02 09:23:51 +00001848 ++Extra;
1849 }
1850
1851 SethiUllmanNumber += Extra;
1852
1853 if (SethiUllmanNumber == 0)
1854 SethiUllmanNumber = 1;
Andrew Trick2085a962010-12-21 22:25:04 +00001855
Evan Cheng7e4abde2008-07-02 09:23:51 +00001856 return SethiUllmanNumber;
1857}
1858
Andrew Trick9ccce772011-01-14 21:11:41 +00001859/// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all
1860/// scheduling units.
1861void RegReductionPQBase::CalculateSethiUllmanNumbers() {
1862 SethiUllmanNumbers.assign(SUnits->size(), 0);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00001863
Andrew Trick9ccce772011-01-14 21:11:41 +00001864 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
1865 CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers);
Evan Chengd38c22b2006-05-11 23:55:42 +00001866}
1867
Andrew Trick9ccce772011-01-14 21:11:41 +00001868void RegReductionPQBase::addNode(const SUnit *SU) {
1869 unsigned SUSize = SethiUllmanNumbers.size();
1870 if (SUnits->size() > SUSize)
1871 SethiUllmanNumbers.resize(SUSize*2, 0);
1872 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1873}
1874
1875void RegReductionPQBase::updateNode(const SUnit *SU) {
1876 SethiUllmanNumbers[SU->NodeNum] = 0;
1877 CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers);
1878}
1879
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001880// Lower priority means schedule further down. For bottom-up scheduling, lower
1881// priority SUs are scheduled before higher priority SUs.
Andrew Trick9ccce772011-01-14 21:11:41 +00001882unsigned RegReductionPQBase::getNodePriority(const SUnit *SU) const {
1883 assert(SU->NodeNum < SethiUllmanNumbers.size());
1884 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
1885 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
1886 // CopyToReg should be close to its uses to facilitate coalescing and
1887 // avoid spilling.
1888 return 0;
Christian Koniged34d0e2013-03-20 15:43:00 +00001889 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1890 Opc == TargetOpcode::SUBREG_TO_REG ||
1891 Opc == TargetOpcode::INSERT_SUBREG)
1892 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
1893 // close to their uses to facilitate coalescing.
1894 return 0;
Andrew Trick9ccce772011-01-14 21:11:41 +00001895 if (SU->NumSuccs == 0 && SU->NumPreds != 0)
1896 // If SU does not have a register use, i.e. it doesn't produce a value
1897 // that would be consumed (e.g. store), then it terminates a chain of
1898 // computation. Give it a large SethiUllman number so it will be
1899 // scheduled right before its predecessors that it doesn't lengthen
1900 // their live ranges.
1901 return 0xffff;
1902 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
1903 // If SU does not have a register def, schedule it close to its uses
1904 // because it does not lengthen any live ranges.
1905 return 0;
Evan Cheng1355bbd2011-04-26 21:31:35 +00001906#if 1
Andrew Trick9ccce772011-01-14 21:11:41 +00001907 return SethiUllmanNumbers[SU->NodeNum];
Evan Cheng1355bbd2011-04-26 21:31:35 +00001908#else
1909 unsigned Priority = SethiUllmanNumbers[SU->NodeNum];
1910 if (SU->isCallOp) {
1911 // FIXME: This assumes all of the defs are used as call operands.
1912 int NP = (int)Priority - SU->getNode()->getNumValues();
1913 return (NP > 0) ? NP : 0;
1914 }
1915 return Priority;
1916#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001917}
1918
1919//===----------------------------------------------------------------------===//
1920// Register Pressure Tracking
1921//===----------------------------------------------------------------------===//
1922
1923void RegReductionPQBase::dumpRegPressure() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001924#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick9ccce772011-01-14 21:11:41 +00001925 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
1926 E = TRI->regclass_end(); I != E; ++I) {
1927 const TargetRegisterClass *RC = *I;
1928 unsigned Id = RC->getID();
1929 unsigned RP = RegPressure[Id];
1930 if (!RP) continue;
1931 DEBUG(dbgs() << RC->getName() << ": " << RP << " / " << RegLimit[Id]
1932 << '\n');
1933 }
Manman Ren742534c2012-09-06 19:06:06 +00001934#endif
Andrew Trick9ccce772011-01-14 21:11:41 +00001935}
1936
1937bool RegReductionPQBase::HighRegPressure(const SUnit *SU) const {
1938 if (!TLI)
1939 return false;
1940
1941 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1942 I != E; ++I) {
1943 if (I->isCtrl())
1944 continue;
1945 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00001946 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1947 // to cover the number of registers defined (they are all live).
1948 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00001949 continue;
1950 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00001951 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
1952 RegDefPos.IsValid(); RegDefPos.Advance()) {
Owen Anderson96adc4a2011-06-15 23:35:18 +00001953 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001954 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00001955
Andrew Trick9ccce772011-01-14 21:11:41 +00001956 if ((RegPressure[RCId] + Cost) >= RegLimit[RCId])
1957 return true;
1958 }
1959 }
Andrew Trick9ccce772011-01-14 21:11:41 +00001960 return false;
1961}
1962
Andrew Trick641e2d42011-03-05 08:00:22 +00001963bool RegReductionPQBase::MayReduceRegPressure(SUnit *SU) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00001964 const SDNode *N = SU->getNode();
1965
1966 if (!N->isMachineOpcode() || !SU->NumSuccs)
1967 return false;
1968
1969 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
1970 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00001971 MVT VT = N->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00001972 if (!N->hasAnyUseOfValue(i))
1973 continue;
1974 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
1975 if (RegPressure[RCId] >= RegLimit[RCId])
1976 return true;
1977 }
1978 return false;
1979}
1980
Andrew Trick641e2d42011-03-05 08:00:22 +00001981// Compute the register pressure contribution by this instruction by count up
1982// for uses that are not live and down for defs. Only count register classes
1983// that are already under high pressure. As a side effect, compute the number of
1984// uses of registers that are already live.
1985//
1986// FIXME: This encompasses the logic in HighRegPressure and MayReduceRegPressure
1987// so could probably be factored.
1988int RegReductionPQBase::RegPressureDiff(SUnit *SU, unsigned &LiveUses) const {
1989 LiveUses = 0;
1990 int PDiff = 0;
1991 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
1992 I != E; ++I) {
1993 if (I->isCtrl())
1994 continue;
1995 SUnit *PredSU = I->getSUnit();
1996 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
1997 // to cover the number of registers defined (they are all live).
1998 if (PredSU->NumRegDefsLeft == 0) {
1999 if (PredSU->getNode()->isMachineOpcode())
2000 ++LiveUses;
2001 continue;
2002 }
2003 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
2004 RegDefPos.IsValid(); RegDefPos.Advance()) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002005 MVT VT = RegDefPos.GetValue();
Andrew Trick641e2d42011-03-05 08:00:22 +00002006 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2007 if (RegPressure[RCId] >= RegLimit[RCId])
2008 ++PDiff;
2009 }
2010 }
2011 const SDNode *N = SU->getNode();
2012
Eric Christopher7238cba2011-03-08 19:35:47 +00002013 if (!N || !N->isMachineOpcode() || !SU->NumSuccs)
Andrew Trick641e2d42011-03-05 08:00:22 +00002014 return PDiff;
2015
2016 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
2017 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002018 MVT VT = N->getSimpleValueType(i);
Andrew Trick641e2d42011-03-05 08:00:22 +00002019 if (!N->hasAnyUseOfValue(i))
2020 continue;
2021 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2022 if (RegPressure[RCId] >= RegLimit[RCId])
2023 --PDiff;
2024 }
2025 return PDiff;
2026}
2027
Andrew Trick52226d42012-03-07 23:00:49 +00002028void RegReductionPQBase::scheduledNode(SUnit *SU) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002029 if (!TracksRegPressure)
2030 return;
2031
Eric Christopher7238cba2011-03-08 19:35:47 +00002032 if (!SU->getNode())
2033 return;
Andrew Tricka8846e02011-03-23 20:40:18 +00002034
Andrew Trick9ccce772011-01-14 21:11:41 +00002035 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2036 I != E; ++I) {
2037 if (I->isCtrl())
2038 continue;
2039 SUnit *PredSU = I->getSUnit();
Andrew Trickd0548ae2011-02-04 03:18:17 +00002040 // NumRegDefsLeft is zero when enough uses of this node have been scheduled
2041 // to cover the number of registers defined (they are all live).
2042 if (PredSU->NumRegDefsLeft == 0) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002043 continue;
2044 }
Andrew Trickd0548ae2011-02-04 03:18:17 +00002045 // FIXME: The ScheduleDAG currently loses information about which of a
2046 // node's values is consumed by each dependence. Consequently, if the node
2047 // defines multiple register classes, we don't know which to pressurize
2048 // here. Instead the following loop consumes the register defs in an
2049 // arbitrary order. At least it handles the common case of clustered loads
2050 // to the same class. For precise liveness, each SDep needs to indicate the
2051 // result number. But that tightly couples the ScheduleDAG with the
2052 // SelectionDAG making updates tricky. A simpler hack would be to attach a
2053 // value type or register class to SDep.
2054 //
2055 // The most important aspect of register tracking is balancing the increase
2056 // here with the reduction further below. Note that this SU may use multiple
2057 // defs in PredSU. The can't be determined here, but we've already
2058 // compensated by reducing NumRegDefsLeft in PredSU during
2059 // ScheduleDAGSDNodes::AddSchedEdges.
2060 --PredSU->NumRegDefsLeft;
2061 unsigned SkipRegDefs = PredSU->NumRegDefsLeft;
2062 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(PredSU, scheduleDAG);
2063 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
2064 if (SkipRegDefs)
Andrew Trick9ccce772011-01-14 21:11:41 +00002065 continue;
Owen Anderson96adc4a2011-06-15 23:35:18 +00002066
2067 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002068 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00002069 RegPressure[RCId] += Cost;
Andrew Trickd0548ae2011-02-04 03:18:17 +00002070 break;
Andrew Trick9ccce772011-01-14 21:11:41 +00002071 }
2072 }
2073
Andrew Trickd0548ae2011-02-04 03:18:17 +00002074 // We should have this assert, but there may be dead SDNodes that never
2075 // materialize as SUnits, so they don't appear to generate liveness.
2076 //assert(SU->NumRegDefsLeft == 0 && "not all regdefs have scheduled uses");
2077 int SkipRegDefs = (int)SU->NumRegDefsLeft;
2078 for (ScheduleDAGSDNodes::RegDefIter RegDefPos(SU, scheduleDAG);
2079 RegDefPos.IsValid(); RegDefPos.Advance(), --SkipRegDefs) {
2080 if (SkipRegDefs > 0)
2081 continue;
Owen Anderson96adc4a2011-06-15 23:35:18 +00002082 unsigned RCId, Cost;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002083 GetCostForDef(RegDefPos, TLI, TII, TRI, RCId, Cost, MF);
Owen Anderson96adc4a2011-06-15 23:35:18 +00002084 if (RegPressure[RCId] < Cost) {
Andrew Trickd0548ae2011-02-04 03:18:17 +00002085 // Register pressure tracking is imprecise. This can happen. But we try
2086 // hard not to let it happen because it likely results in poor scheduling.
2087 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") has too many regdefs\n");
2088 RegPressure[RCId] = 0;
2089 }
2090 else {
Owen Anderson96adc4a2011-06-15 23:35:18 +00002091 RegPressure[RCId] -= Cost;
Andrew Trick9ccce772011-01-14 21:11:41 +00002092 }
2093 }
Andrew Trick9ccce772011-01-14 21:11:41 +00002094 dumpRegPressure();
2095}
2096
Andrew Trick52226d42012-03-07 23:00:49 +00002097void RegReductionPQBase::unscheduledNode(SUnit *SU) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002098 if (!TracksRegPressure)
2099 return;
2100
2101 const SDNode *N = SU->getNode();
Eric Christopher7238cba2011-03-08 19:35:47 +00002102 if (!N) return;
Andrew Tricka8846e02011-03-23 20:40:18 +00002103
Andrew Trick9ccce772011-01-14 21:11:41 +00002104 if (!N->isMachineOpcode()) {
2105 if (N->getOpcode() != ISD::CopyToReg)
2106 return;
2107 } else {
2108 unsigned Opc = N->getMachineOpcode();
2109 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2110 Opc == TargetOpcode::INSERT_SUBREG ||
2111 Opc == TargetOpcode::SUBREG_TO_REG ||
2112 Opc == TargetOpcode::REG_SEQUENCE ||
2113 Opc == TargetOpcode::IMPLICIT_DEF)
2114 return;
2115 }
2116
2117 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2118 I != E; ++I) {
2119 if (I->isCtrl())
2120 continue;
2121 SUnit *PredSU = I->getSUnit();
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002122 // NumSuccsLeft counts all deps. Don't compare it with NumSuccs which only
2123 // counts data deps.
2124 if (PredSU->NumSuccsLeft != PredSU->Succs.size())
Andrew Trick9ccce772011-01-14 21:11:41 +00002125 continue;
2126 const SDNode *PN = PredSU->getNode();
2127 if (!PN->isMachineOpcode()) {
2128 if (PN->getOpcode() == ISD::CopyFromReg) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002129 MVT VT = PN->getSimpleValueType(0);
Andrew Trick9ccce772011-01-14 21:11:41 +00002130 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2131 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2132 }
2133 continue;
2134 }
2135 unsigned POpc = PN->getMachineOpcode();
2136 if (POpc == TargetOpcode::IMPLICIT_DEF)
2137 continue;
Andrew Trick31f25bc2011-06-27 18:01:20 +00002138 if (POpc == TargetOpcode::EXTRACT_SUBREG ||
2139 POpc == TargetOpcode::INSERT_SUBREG ||
2140 POpc == TargetOpcode::SUBREG_TO_REG) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002141 MVT VT = PN->getSimpleValueType(0);
Andrew Trick9ccce772011-01-14 21:11:41 +00002142 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2143 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2144 continue;
2145 }
2146 unsigned NumDefs = TII->get(PN->getMachineOpcode()).getNumDefs();
2147 for (unsigned i = 0; i != NumDefs; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002148 MVT VT = PN->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00002149 if (!PN->hasAnyUseOfValue(i))
2150 continue;
2151 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2152 if (RegPressure[RCId] < TLI->getRepRegClassCostFor(VT))
2153 // Register pressure tracking is imprecise. This can happen.
2154 RegPressure[RCId] = 0;
2155 else
2156 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
2157 }
2158 }
2159
2160 // Check for isMachineOpcode() as PrescheduleNodesWithMultipleUses()
2161 // may transfer data dependencies to CopyToReg.
2162 if (SU->NumSuccs && N->isMachineOpcode()) {
2163 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
2164 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Patrik Hagglund05394352012-12-13 18:45:35 +00002165 MVT VT = N->getSimpleValueType(i);
Andrew Trick9ccce772011-01-14 21:11:41 +00002166 if (VT == MVT::Glue || VT == MVT::Other)
2167 continue;
2168 if (!N->hasAnyUseOfValue(i))
2169 continue;
2170 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
2171 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
2172 }
2173 }
2174
2175 dumpRegPressure();
2176}
2177
2178//===----------------------------------------------------------------------===//
2179// Dynamic Node Priority for Register Pressure Reduction
2180//===----------------------------------------------------------------------===//
2181
Evan Chengb9e3db62007-03-14 22:43:40 +00002182/// closestSucc - Returns the scheduled cycle of the successor which is
Dan Gohmana19c6622009-03-12 23:55:10 +00002183/// closest to the current cycle.
Evan Cheng28748552007-03-13 23:25:11 +00002184static unsigned closestSucc(const SUnit *SU) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002185 unsigned MaxHeight = 0;
Evan Cheng28748552007-03-13 23:25:11 +00002186 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Evan Chengb9e3db62007-03-14 22:43:40 +00002187 I != E; ++I) {
Evan Chengce3bbe52009-02-10 08:30:11 +00002188 if (I->isCtrl()) continue; // ignore chain succs
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002189 unsigned Height = I->getSUnit()->getHeight();
Evan Chengb9e3db62007-03-14 22:43:40 +00002190 // If there are bunch of CopyToRegs stacked up, they should be considered
2191 // to be at the same position.
Dan Gohman2d170892008-12-09 22:54:47 +00002192 if (I->getSUnit()->getNode() &&
2193 I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg)
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002194 Height = closestSucc(I->getSUnit())+1;
2195 if (Height > MaxHeight)
2196 MaxHeight = Height;
Evan Chengb9e3db62007-03-14 22:43:40 +00002197 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002198 return MaxHeight;
Evan Cheng28748552007-03-13 23:25:11 +00002199}
2200
Evan Cheng61bc51e2007-12-20 02:22:36 +00002201/// calcMaxScratches - Returns an cost estimate of the worse case requirement
Evan Cheng3a14efa2009-02-12 08:59:45 +00002202/// for scratch registers, i.e. number of data dependencies.
Evan Cheng61bc51e2007-12-20 02:22:36 +00002203static unsigned calcMaxScratches(const SUnit *SU) {
2204 unsigned Scratches = 0;
2205 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Evan Chengb5704992009-02-12 09:52:13 +00002206 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002207 if (I->isCtrl()) continue; // ignore chain preds
Evan Chengb5704992009-02-12 09:52:13 +00002208 Scratches++;
2209 }
Evan Cheng61bc51e2007-12-20 02:22:36 +00002210 return Scratches;
2211}
2212
Andrew Trickb53a00d2011-04-13 00:38:32 +00002213/// hasOnlyLiveInOpers - Return true if SU has only value predecessors that are
2214/// CopyFromReg from a virtual register.
2215static bool hasOnlyLiveInOpers(const SUnit *SU) {
2216 bool RetVal = false;
2217 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
2218 I != E; ++I) {
2219 if (I->isCtrl()) continue;
2220 const SUnit *PredSU = I->getSUnit();
2221 if (PredSU->getNode() &&
2222 PredSU->getNode()->getOpcode() == ISD::CopyFromReg) {
2223 unsigned Reg =
2224 cast<RegisterSDNode>(PredSU->getNode()->getOperand(1))->getReg();
2225 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2226 RetVal = true;
2227 continue;
2228 }
2229 }
2230 return false;
2231 }
2232 return RetVal;
2233}
2234
2235/// hasOnlyLiveOutUses - Return true if SU has only value successors that are
Evan Cheng6c1414f2010-10-29 18:09:28 +00002236/// CopyToReg to a virtual register. This SU def is probably a liveout and
2237/// it has no other use. It should be scheduled closer to the terminator.
2238static bool hasOnlyLiveOutUses(const SUnit *SU) {
2239 bool RetVal = false;
2240 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
2241 I != E; ++I) {
2242 if (I->isCtrl()) continue;
2243 const SUnit *SuccSU = I->getSUnit();
2244 if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) {
2245 unsigned Reg =
2246 cast<RegisterSDNode>(SuccSU->getNode()->getOperand(1))->getReg();
2247 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2248 RetVal = true;
2249 continue;
2250 }
2251 }
2252 return false;
2253 }
2254 return RetVal;
2255}
2256
Andrew Trickb53a00d2011-04-13 00:38:32 +00002257// Set isVRegCycle for a node with only live in opers and live out uses. Also
2258// set isVRegCycle for its CopyFromReg operands.
2259//
2260// This is only relevant for single-block loops, in which case the VRegCycle
2261// node is likely an induction variable in which the operand and target virtual
2262// registers should be coalesced (e.g. pre/post increment values). Setting the
2263// isVRegCycle flag helps the scheduler prioritize other uses of the same
2264// CopyFromReg so that this node becomes the virtual register "kill". This
2265// avoids interference between the values live in and out of the block and
2266// eliminates a copy inside the loop.
2267static void initVRegCycle(SUnit *SU) {
2268 if (DisableSchedVRegCycle)
2269 return;
2270
2271 if (!hasOnlyLiveInOpers(SU) || !hasOnlyLiveOutUses(SU))
2272 return;
2273
2274 DEBUG(dbgs() << "VRegCycle: SU(" << SU->NodeNum << ")\n");
2275
2276 SU->isVRegCycle = true;
2277
2278 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
Andrew Trickc5dd24a2011-04-12 19:54:36 +00002279 I != E; ++I) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00002280 if (I->isCtrl()) continue;
2281 I->getSUnit()->isVRegCycle = true;
Andrew Trickc5dd24a2011-04-12 19:54:36 +00002282 }
Andrew Trick1b60ad62011-04-12 20:14:07 +00002283}
2284
Andrew Trickb53a00d2011-04-13 00:38:32 +00002285// After scheduling the definition of a VRegCycle, clear the isVRegCycle flag of
2286// CopyFromReg operands. We should no longer penalize other uses of this VReg.
2287static void resetVRegCycle(SUnit *SU) {
2288 if (!SU->isVRegCycle)
2289 return;
2290
2291 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
2292 I != E; ++I) {
Andrew Trick1b60ad62011-04-12 20:14:07 +00002293 if (I->isCtrl()) continue; // ignore chain preds
Andrew Trickb53a00d2011-04-13 00:38:32 +00002294 SUnit *PredSU = I->getSUnit();
2295 if (PredSU->isVRegCycle) {
2296 assert(PredSU->getNode()->getOpcode() == ISD::CopyFromReg &&
2297 "VRegCycle def must be CopyFromReg");
2298 I->getSUnit()->isVRegCycle = 0;
2299 }
2300 }
2301}
2302
2303// Return true if this SUnit uses a CopyFromReg node marked as a VRegCycle. This
2304// means a node that defines the VRegCycle has not been scheduled yet.
2305static bool hasVRegCycleUse(const SUnit *SU) {
2306 // If this SU also defines the VReg, don't hoist it as a "use".
2307 if (SU->isVRegCycle)
2308 return false;
2309
2310 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
2311 I != E; ++I) {
2312 if (I->isCtrl()) continue; // ignore chain preds
2313 if (I->getSUnit()->isVRegCycle &&
2314 I->getSUnit()->getNode()->getOpcode() == ISD::CopyFromReg) {
2315 DEBUG(dbgs() << " VReg cycle use: SU (" << SU->NodeNum << ")\n");
2316 return true;
Andrew Trick2ad0b372011-04-07 19:54:57 +00002317 }
2318 }
2319 return false;
2320}
2321
Andrew Trick9ccce772011-01-14 21:11:41 +00002322// Check for either a dependence (latency) or resource (hazard) stall.
2323//
2324// Note: The ScheduleHazardRecognizer interface requires a non-const SU.
2325static bool BUHasStall(SUnit *SU, int Height, RegReductionPQBase *SPQ) {
2326 if ((int)SPQ->getCurCycle() < Height) return true;
2327 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2328 != ScheduleHazardRecognizer::NoHazard)
2329 return true;
2330 return false;
2331}
2332
2333// Return -1 if left has higher priority, 1 if right has higher priority.
2334// Return 0 if latency-based priority is equivalent.
2335static int BUCompareLatency(SUnit *left, SUnit *right, bool checkPref,
2336 RegReductionPQBase *SPQ) {
Andrew Trickb53a00d2011-04-13 00:38:32 +00002337 // Scheduling an instruction that uses a VReg whose postincrement has not yet
2338 // been scheduled will induce a copy. Model this as an extra cycle of latency.
2339 int LPenalty = hasVRegCycleUse(left) ? 1 : 0;
2340 int RPenalty = hasVRegCycleUse(right) ? 1 : 0;
2341 int LHeight = (int)left->getHeight() + LPenalty;
2342 int RHeight = (int)right->getHeight() + RPenalty;
Andrew Trick9ccce772011-01-14 21:11:41 +00002343
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002344 bool LStall = (!checkPref || left->SchedulingPref == Sched::ILP) &&
Andrew Trick9ccce772011-01-14 21:11:41 +00002345 BUHasStall(left, LHeight, SPQ);
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002346 bool RStall = (!checkPref || right->SchedulingPref == Sched::ILP) &&
Andrew Trick9ccce772011-01-14 21:11:41 +00002347 BUHasStall(right, RHeight, SPQ);
2348
2349 // If scheduling one of the node will cause a pipeline stall, delay it.
2350 // If scheduling either one of the node will cause a pipeline stall, sort
2351 // them according to their height.
2352 if (LStall) {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002353 if (!RStall)
Andrew Trick9ccce772011-01-14 21:11:41 +00002354 return 1;
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002355 if (LHeight != RHeight)
Andrew Trick9ccce772011-01-14 21:11:41 +00002356 return LHeight > RHeight ? 1 : -1;
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002357 } else if (RStall)
Andrew Trick9ccce772011-01-14 21:11:41 +00002358 return -1;
2359
Andrew Trick47ff14b2011-01-21 05:51:33 +00002360 // If either node is scheduling for latency, sort them by height/depth
Andrew Trick9ccce772011-01-14 21:11:41 +00002361 // and latency.
Dan Gohman4ed1afa2011-10-24 17:55:11 +00002362 if (!checkPref || (left->SchedulingPref == Sched::ILP ||
2363 right->SchedulingPref == Sched::ILP)) {
Andrew Tricka88d46e2012-06-05 03:44:34 +00002364 // If neither instruction stalls (!LStall && !RStall) and HazardRecognizer
2365 // is enabled, grouping instructions by cycle, then its height is already
2366 // covered so only its depth matters. We also reach this point if both stall
2367 // but have the same height.
2368 if (!SPQ->getHazardRec()->isEnabled()) {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002369 if (LHeight != RHeight)
Andrew Trick9ccce772011-01-14 21:11:41 +00002370 return LHeight > RHeight ? 1 : -1;
2371 }
Andrew Tricka88d46e2012-06-05 03:44:34 +00002372 int LDepth = left->getDepth() - LPenalty;
2373 int RDepth = right->getDepth() - RPenalty;
2374 if (LDepth != RDepth) {
2375 DEBUG(dbgs() << " Comparing latency of SU (" << left->NodeNum
2376 << ") depth " << LDepth << " vs SU (" << right->NodeNum
2377 << ") depth " << RDepth << "\n");
2378 return LDepth < RDepth ? 1 : -1;
Andrew Trick47ff14b2011-01-21 05:51:33 +00002379 }
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002380 if (left->Latency != right->Latency)
Andrew Trick9ccce772011-01-14 21:11:41 +00002381 return left->Latency > right->Latency ? 1 : -1;
2382 }
2383 return 0;
2384}
2385
2386static bool BURRSort(SUnit *left, SUnit *right, RegReductionPQBase *SPQ) {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002387 // Schedule physical register definitions close to their use. This is
2388 // motivated by microarchitectures that can fuse cmp+jump macro-ops. But as
2389 // long as shortening physreg live ranges is generally good, we can defer
2390 // creating a subtarget hook.
2391 if (!DisableSchedPhysRegJoin) {
2392 bool LHasPhysReg = left->hasPhysRegDefs;
2393 bool RHasPhysReg = right->hasPhysRegDefs;
2394 if (LHasPhysReg != RHasPhysReg) {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002395 #ifndef NDEBUG
Craig Topper06b3b662013-07-15 08:02:13 +00002396 static const char *const PhysRegMsg[] = { " has no physreg",
2397 " defines a physreg" };
Andrew Trickbfbd9722011-04-14 05:15:06 +00002398 #endif
2399 DEBUG(dbgs() << " SU (" << left->NodeNum << ") "
2400 << PhysRegMsg[LHasPhysReg] << " SU(" << right->NodeNum << ") "
2401 << PhysRegMsg[RHasPhysReg] << "\n");
2402 return LHasPhysReg < RHasPhysReg;
2403 }
2404 }
2405
Evan Cheng2f647542011-04-26 04:57:37 +00002406 // Prioritize by Sethi-Ulmann number and push CopyToReg nodes down.
Evan Cheng6730f032007-01-08 23:55:53 +00002407 unsigned LPriority = SPQ->getNodePriority(left);
2408 unsigned RPriority = SPQ->getNodePriority(right);
Evan Cheng1355bbd2011-04-26 21:31:35 +00002409
2410 // Be really careful about hoisting call operands above previous calls.
2411 // Only allows it if it would reduce register pressure.
2412 if (left->isCall && right->isCallOp) {
2413 unsigned RNumVals = right->getNode()->getNumValues();
2414 RPriority = (RPriority > RNumVals) ? (RPriority - RNumVals) : 0;
2415 }
2416 if (right->isCall && left->isCallOp) {
2417 unsigned LNumVals = left->getNode()->getNumValues();
2418 LPriority = (LPriority > LNumVals) ? (LPriority - LNumVals) : 0;
2419 }
2420
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002421 if (LPriority != RPriority)
Evan Cheng73bdf042008-03-01 00:39:47 +00002422 return LPriority > RPriority;
Andrew Trick52b3e382011-03-08 01:51:56 +00002423
Evan Cheng1355bbd2011-04-26 21:31:35 +00002424 // One or both of the nodes are calls and their sethi-ullman numbers are the
2425 // same, then keep source order.
2426 if (left->isCall || right->isCall) {
2427 unsigned LOrder = SPQ->getNodeOrdering(left);
2428 unsigned ROrder = SPQ->getNodeOrdering(right);
2429
2430 // Prefer an ordering where the lower the non-zero order number, the higher
2431 // the preference.
2432 if ((LOrder || ROrder) && LOrder != ROrder)
2433 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2434 }
2435
Evan Cheng73bdf042008-03-01 00:39:47 +00002436 // Try schedule def + use closer when Sethi-Ullman numbers are the same.
2437 // e.g.
2438 // t1 = op t2, c1
2439 // t3 = op t4, c2
2440 //
2441 // and the following instructions are both ready.
2442 // t2 = op c3
2443 // t4 = op c4
2444 //
2445 // Then schedule t2 = op first.
2446 // i.e.
2447 // t4 = op c4
2448 // t2 = op c3
2449 // t1 = op t2, c1
2450 // t3 = op t4, c2
2451 //
2452 // This creates more short live intervals.
2453 unsigned LDist = closestSucc(left);
2454 unsigned RDist = closestSucc(right);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002455 if (LDist != RDist)
Evan Cheng73bdf042008-03-01 00:39:47 +00002456 return LDist < RDist;
2457
Evan Cheng3a14efa2009-02-12 08:59:45 +00002458 // How many registers becomes live when the node is scheduled.
Evan Cheng73bdf042008-03-01 00:39:47 +00002459 unsigned LScratch = calcMaxScratches(left);
2460 unsigned RScratch = calcMaxScratches(right);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002461 if (LScratch != RScratch)
Evan Cheng73bdf042008-03-01 00:39:47 +00002462 return LScratch > RScratch;
2463
Evan Cheng1355bbd2011-04-26 21:31:35 +00002464 // Comparing latency against a call makes little sense unless the node
2465 // is register pressure-neutral.
2466 if ((left->isCall && RPriority > 0) || (right->isCall && LPriority > 0))
2467 return (left->NodeQueueId > right->NodeQueueId);
2468
2469 // Do not compare latencies when one or both of the nodes are calls.
2470 if (!DisableSchedCycles &&
2471 !(left->isCall || right->isCall)) {
Andrew Trick9ccce772011-01-14 21:11:41 +00002472 int result = BUCompareLatency(left, right, false /*checkPref*/, SPQ);
2473 if (result != 0)
2474 return result > 0;
2475 }
2476 else {
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002477 if (left->getHeight() != right->getHeight())
Andrew Trick9ccce772011-01-14 21:11:41 +00002478 return left->getHeight() > right->getHeight();
Andrew Trick2085a962010-12-21 22:25:04 +00002479
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002480 if (left->getDepth() != right->getDepth())
Andrew Trick9ccce772011-01-14 21:11:41 +00002481 return left->getDepth() < right->getDepth();
2482 }
Evan Cheng73bdf042008-03-01 00:39:47 +00002483
Andrew Trick2085a962010-12-21 22:25:04 +00002484 assert(left->NodeQueueId && right->NodeQueueId &&
Roman Levenstein6b371142008-04-29 09:07:59 +00002485 "NodeQueueId cannot be zero");
2486 return (left->NodeQueueId > right->NodeQueueId);
Evan Chengd38c22b2006-05-11 23:55:42 +00002487}
2488
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002489// Bottom up
Andrew Trick9ccce772011-01-14 21:11:41 +00002490bool bu_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002491 if (int res = checkSpecialNodes(left, right))
2492 return res > 0;
2493
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002494 return BURRSort(left, right, SPQ);
2495}
2496
2497// Source order, otherwise bottom up.
Andrew Trick9ccce772011-01-14 21:11:41 +00002498bool src_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002499 if (int res = checkSpecialNodes(left, right))
2500 return res > 0;
2501
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002502 unsigned LOrder = SPQ->getNodeOrdering(left);
2503 unsigned ROrder = SPQ->getNodeOrdering(right);
2504
2505 // Prefer an ordering where the lower the non-zero order number, the higher
2506 // the preference.
2507 if ((LOrder || ROrder) && LOrder != ROrder)
2508 return LOrder != 0 && (LOrder < ROrder || ROrder == 0);
2509
2510 return BURRSort(left, right, SPQ);
2511}
2512
Andrew Trick9ccce772011-01-14 21:11:41 +00002513// If the time between now and when the instruction will be ready can cover
2514// the spill code, then avoid adding it to the ready queue. This gives long
2515// stalls highest priority and allows hoisting across calls. It should also
2516// speed up processing the available queue.
2517bool hybrid_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
2518 static const unsigned ReadyDelay = 3;
2519
2520 if (SPQ->MayReduceRegPressure(SU)) return true;
2521
2522 if (SU->getHeight() > (CurCycle + ReadyDelay)) return false;
2523
2524 if (SPQ->getHazardRec()->getHazardType(SU, -ReadyDelay)
2525 != ScheduleHazardRecognizer::NoHazard)
2526 return false;
2527
2528 return true;
2529}
2530
2531// Return true if right should be scheduled with higher priority than left.
2532bool hybrid_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002533 if (int res = checkSpecialNodes(left, right))
2534 return res > 0;
2535
Evan Chengdebf9c52010-11-03 00:45:17 +00002536 if (left->isCall || right->isCall)
2537 // No way to compute latency of calls.
2538 return BURRSort(left, right, SPQ);
2539
Evan Chenge6d6c5d2010-07-26 21:49:07 +00002540 bool LHigh = SPQ->HighRegPressure(left);
2541 bool RHigh = SPQ->HighRegPressure(right);
Evan Cheng37b740c2010-07-24 00:39:05 +00002542 // Avoid causing spills. If register pressure is high, schedule for
2543 // register pressure reduction.
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002544 if (LHigh && !RHigh) {
2545 DEBUG(dbgs() << " pressure SU(" << left->NodeNum << ") > SU("
2546 << right->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002547 return true;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002548 }
2549 else if (!LHigh && RHigh) {
2550 DEBUG(dbgs() << " pressure SU(" << right->NodeNum << ") > SU("
2551 << left->NodeNum << ")\n");
Evan Cheng28590382010-07-21 23:53:58 +00002552 return false;
Andrew Trick2cd1f0b2011-01-20 06:21:59 +00002553 }
Andrew Trickb53a00d2011-04-13 00:38:32 +00002554 if (!LHigh && !RHigh) {
2555 int result = BUCompareLatency(left, right, true /*checkPref*/, SPQ);
2556 if (result != 0)
2557 return result > 0;
Evan Chengcc2efe12010-05-28 23:26:21 +00002558 }
Evan Chengbdd062d2010-05-20 06:13:19 +00002559 return BURRSort(left, right, SPQ);
2560}
2561
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002562// Schedule as many instructions in each cycle as possible. So don't make an
2563// instruction available unless it is ready in the current cycle.
2564bool ilp_ls_rr_sort::isReady(SUnit *SU, unsigned CurCycle) const {
Andrew Trick9ccce772011-01-14 21:11:41 +00002565 if (SU->getHeight() > CurCycle) return false;
2566
2567 if (SPQ->getHazardRec()->getHazardType(SU, 0)
2568 != ScheduleHazardRecognizer::NoHazard)
2569 return false;
2570
Andrew Trickc88b7ec2011-03-04 02:03:45 +00002571 return true;
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002572}
2573
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002574static bool canEnableCoalescing(SUnit *SU) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002575 unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0;
2576 if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
2577 // CopyToReg should be close to its uses to facilitate coalescing and
2578 // avoid spilling.
2579 return true;
2580
Christian Koniged34d0e2013-03-20 15:43:00 +00002581 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
2582 Opc == TargetOpcode::SUBREG_TO_REG ||
2583 Opc == TargetOpcode::INSERT_SUBREG)
2584 // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
2585 // close to their uses to facilitate coalescing.
2586 return true;
Andrew Trick52b3e382011-03-08 01:51:56 +00002587
2588 if (SU->NumPreds == 0 && SU->NumSuccs != 0)
2589 // If SU does not have a register def, schedule it close to its uses
2590 // because it does not lengthen any live ranges.
2591 return true;
2592
2593 return false;
2594}
2595
Andrew Trickb8390b72011-03-05 08:04:11 +00002596// list-ilp is currently an experimental scheduler that allows various
2597// heuristics to be enabled prior to the normal register reduction logic.
Andrew Trick9ccce772011-01-14 21:11:41 +00002598bool ilp_ls_rr_sort::operator()(SUnit *left, SUnit *right) const {
Andrew Trickbfbd9722011-04-14 05:15:06 +00002599 if (int res = checkSpecialNodes(left, right))
2600 return res > 0;
2601
Evan Chengdebf9c52010-11-03 00:45:17 +00002602 if (left->isCall || right->isCall)
2603 // No way to compute latency of calls.
2604 return BURRSort(left, right, SPQ);
2605
Andrew Trick52b3e382011-03-08 01:51:56 +00002606 unsigned LLiveUses = 0, RLiveUses = 0;
2607 int LPDiff = 0, RPDiff = 0;
2608 if (!DisableSchedRegPressure || !DisableSchedLiveUses) {
2609 LPDiff = SPQ->RegPressureDiff(left, LLiveUses);
2610 RPDiff = SPQ->RegPressureDiff(right, RLiveUses);
2611 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002612 if (!DisableSchedRegPressure && LPDiff != RPDiff) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002613 DEBUG(dbgs() << "RegPressureDiff SU(" << left->NodeNum << "): " << LPDiff
2614 << " != SU(" << right->NodeNum << "): " << RPDiff << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002615 return LPDiff > RPDiff;
2616 }
2617
Andrew Trick52b3e382011-03-08 01:51:56 +00002618 if (!DisableSchedRegPressure && (LPDiff > 0 || RPDiff > 0)) {
Benjamin Kramerb2e4d842011-03-09 16:19:12 +00002619 bool LReduce = canEnableCoalescing(left);
2620 bool RReduce = canEnableCoalescing(right);
Andrew Trick52b3e382011-03-08 01:51:56 +00002621 if (LReduce && !RReduce) return false;
2622 if (RReduce && !LReduce) return true;
2623 }
2624
2625 if (!DisableSchedLiveUses && (LLiveUses != RLiveUses)) {
2626 DEBUG(dbgs() << "Live uses SU(" << left->NodeNum << "): " << LLiveUses
2627 << " != SU(" << right->NodeNum << "): " << RLiveUses << "\n");
Andrew Trick641e2d42011-03-05 08:00:22 +00002628 return LLiveUses < RLiveUses;
2629 }
2630
Andrew Trick52b3e382011-03-08 01:51:56 +00002631 if (!DisableSchedStalls) {
2632 bool LStall = BUHasStall(left, left->getHeight(), SPQ);
2633 bool RStall = BUHasStall(right, right->getHeight(), SPQ);
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002634 if (LStall != RStall)
Andrew Trick52b3e382011-03-08 01:51:56 +00002635 return left->getHeight() > right->getHeight();
Andrew Trick641e2d42011-03-05 08:00:22 +00002636 }
2637
Andrew Trick25cedf32011-03-05 10:29:25 +00002638 if (!DisableSchedCriticalPath) {
2639 int spread = (int)left->getDepth() - (int)right->getDepth();
2640 if (std::abs(spread) > MaxReorderWindow) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002641 DEBUG(dbgs() << "Depth of SU(" << left->NodeNum << "): "
2642 << left->getDepth() << " != SU(" << right->NodeNum << "): "
2643 << right->getDepth() << "\n");
Andrew Trick25cedf32011-03-05 10:29:25 +00002644 return left->getDepth() < right->getDepth();
2645 }
Andrew Trick641e2d42011-03-05 08:00:22 +00002646 }
2647
2648 if (!DisableSchedHeight && left->getHeight() != right->getHeight()) {
Andrew Trick52b3e382011-03-08 01:51:56 +00002649 int spread = (int)left->getHeight() - (int)right->getHeight();
Nick Lewyckyd63851e2011-12-07 21:35:59 +00002650 if (std::abs(spread) > MaxReorderWindow)
Andrew Trick52b3e382011-03-08 01:51:56 +00002651 return left->getHeight() > right->getHeight();
Evan Cheng37b740c2010-07-24 00:39:05 +00002652 }
2653
2654 return BURRSort(left, right, SPQ);
2655}
2656
Andrew Trickb53a00d2011-04-13 00:38:32 +00002657void RegReductionPQBase::initNodes(std::vector<SUnit> &sunits) {
2658 SUnits = &sunits;
2659 // Add pseudo dependency edges for two-address nodes.
Evan Chengd33b2d62011-11-10 07:43:16 +00002660 if (!Disable2AddrHack)
2661 AddPseudoTwoAddrDeps();
Andrew Trickb53a00d2011-04-13 00:38:32 +00002662 // Reroute edges to nodes with multiple uses.
Evan Cheng8ab58a22012-03-22 19:31:17 +00002663 if (!TracksRegPressure && !SrcOrder)
Andrew Trickb53a00d2011-04-13 00:38:32 +00002664 PrescheduleNodesWithMultipleUses();
2665 // Calculate node priorities.
2666 CalculateSethiUllmanNumbers();
2667
2668 // For single block loops, mark nodes that look like canonical IV increments.
2669 if (scheduleDAG->BB->isSuccessor(scheduleDAG->BB)) {
2670 for (unsigned i = 0, e = sunits.size(); i != e; ++i) {
2671 initVRegCycle(&sunits[i]);
2672 }
2673 }
2674}
2675
Andrew Trick9ccce772011-01-14 21:11:41 +00002676//===----------------------------------------------------------------------===//
2677// Preschedule for Register Pressure
2678//===----------------------------------------------------------------------===//
2679
2680bool RegReductionPQBase::canClobber(const SUnit *SU, const SUnit *Op) {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002681 if (SU->isTwoAddress) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002682 unsigned Opc = SU->getNode()->getMachineOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002683 const MCInstrDesc &MCID = TII->get(Opc);
2684 unsigned NumRes = MCID.getNumDefs();
2685 unsigned NumOps = MCID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002686 for (unsigned i = 0; i != NumOps; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002687 if (MCID.getOperandConstraint(i+NumRes, MCOI::TIED_TO) != -1) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002688 SDNode *DU = SU->getNode()->getOperand(i).getNode();
Dan Gohman46520a22008-06-21 19:18:17 +00002689 if (DU->getNodeId() != -1 &&
2690 Op->OrigNode == &(*SUnits)[DU->getNodeId()])
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002691 return true;
2692 }
2693 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002694 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002695 return false;
2696}
2697
Andrew Trick832a6a192011-09-01 00:54:31 +00002698/// canClobberReachingPhysRegUse - True if SU would clobber one of it's
2699/// successor's explicit physregs whose definition can reach DepSU.
2700/// i.e. DepSU should not be scheduled above SU.
2701static bool canClobberReachingPhysRegUse(const SUnit *DepSU, const SUnit *SU,
2702 ScheduleDAGRRList *scheduleDAG,
2703 const TargetInstrInfo *TII,
2704 const TargetRegisterInfo *TRI) {
Craig Topper5a4bcc72012-03-08 08:22:45 +00002705 const uint16_t *ImpDefs
Andrew Trick832a6a192011-09-01 00:54:31 +00002706 = TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs();
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002707 const uint32_t *RegMask = getNodeRegMask(SU->getNode());
2708 if(!ImpDefs && !RegMask)
Andrew Trick832a6a192011-09-01 00:54:31 +00002709 return false;
2710
2711 for (SUnit::const_succ_iterator SI = SU->Succs.begin(), SE = SU->Succs.end();
2712 SI != SE; ++SI) {
2713 SUnit *SuccSU = SI->getSUnit();
2714 for (SUnit::const_pred_iterator PI = SuccSU->Preds.begin(),
2715 PE = SuccSU->Preds.end(); PI != PE; ++PI) {
2716 if (!PI->isAssignedRegDep())
2717 continue;
2718
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002719 if (RegMask && MachineOperand::clobbersPhysReg(RegMask, PI->getReg()) &&
2720 scheduleDAG->IsReachable(DepSU, PI->getSUnit()))
2721 return true;
2722
2723 if (ImpDefs)
Craig Topper5a4bcc72012-03-08 08:22:45 +00002724 for (const uint16_t *ImpDef = ImpDefs; *ImpDef; ++ImpDef)
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002725 // Return true if SU clobbers this physical register use and the
2726 // definition of the register reaches from DepSU. IsReachable queries
2727 // a topological forward sort of the DAG (following the successors).
2728 if (TRI->regsOverlap(*ImpDef, PI->getReg()) &&
2729 scheduleDAG->IsReachable(DepSU, PI->getSUnit()))
2730 return true;
Andrew Trick832a6a192011-09-01 00:54:31 +00002731 }
2732 }
2733 return false;
2734}
2735
Evan Chengf9891412007-12-20 09:25:31 +00002736/// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's
Dan Gohmanea045202008-06-21 22:05:24 +00002737/// physical register defs.
Dan Gohmane955c482008-08-05 14:45:15 +00002738static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU,
Evan Chengf9891412007-12-20 09:25:31 +00002739 const TargetInstrInfo *TII,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002740 const TargetRegisterInfo *TRI) {
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002741 SDNode *N = SuccSU->getNode();
Dan Gohman17059682008-07-17 19:10:17 +00002742 unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs();
Craig Topper5a4bcc72012-03-08 08:22:45 +00002743 const uint16_t *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs();
Dan Gohmanea045202008-06-21 22:05:24 +00002744 assert(ImpDefs && "Caller should check hasPhysRegDefs");
Dan Gohmana366da12009-03-23 16:23:01 +00002745 for (const SDNode *SUNode = SU->getNode(); SUNode;
Chris Lattner11a33812010-12-23 17:24:32 +00002746 SUNode = SUNode->getGluedNode()) {
Dan Gohmana366da12009-03-23 16:23:01 +00002747 if (!SUNode->isMachineOpcode())
Evan Chengf9891412007-12-20 09:25:31 +00002748 continue;
Craig Topper5a4bcc72012-03-08 08:22:45 +00002749 const uint16_t *SUImpDefs =
Dan Gohmana366da12009-03-23 16:23:01 +00002750 TII->get(SUNode->getMachineOpcode()).getImplicitDefs();
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002751 const uint32_t *SURegMask = getNodeRegMask(SUNode);
2752 if (!SUImpDefs && !SURegMask)
2753 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002754 for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002755 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002756 if (VT == MVT::Glue || VT == MVT::Other)
Dan Gohmana366da12009-03-23 16:23:01 +00002757 continue;
2758 if (!N->hasAnyUseOfValue(i))
2759 continue;
2760 unsigned Reg = ImpDefs[i - NumDefs];
Jakob Stoklund Olesen2ceea932012-02-13 23:25:24 +00002761 if (SURegMask && MachineOperand::clobbersPhysReg(SURegMask, Reg))
2762 return true;
2763 if (!SUImpDefs)
2764 continue;
Dan Gohmana366da12009-03-23 16:23:01 +00002765 for (;*SUImpDefs; ++SUImpDefs) {
2766 unsigned SUReg = *SUImpDefs;
2767 if (TRI->regsOverlap(Reg, SUReg))
2768 return true;
2769 }
Evan Chengf9891412007-12-20 09:25:31 +00002770 }
2771 }
2772 return false;
2773}
2774
Dan Gohman9a658d72009-03-24 00:49:12 +00002775/// PrescheduleNodesWithMultipleUses - Nodes with multiple uses
2776/// are not handled well by the general register pressure reduction
2777/// heuristics. When presented with code like this:
2778///
2779/// N
2780/// / |
2781/// / |
2782/// U store
2783/// |
2784/// ...
2785///
2786/// the heuristics tend to push the store up, but since the
2787/// operand of the store has another use (U), this would increase
2788/// the length of that other use (the U->N edge).
2789///
2790/// This function transforms code like the above to route U's
2791/// dependence through the store when possible, like this:
2792///
2793/// N
2794/// ||
2795/// ||
2796/// store
2797/// |
2798/// U
2799/// |
2800/// ...
2801///
2802/// This results in the store being scheduled immediately
2803/// after N, which shortens the U->N live range, reducing
2804/// register pressure.
2805///
Andrew Trick9ccce772011-01-14 21:11:41 +00002806void RegReductionPQBase::PrescheduleNodesWithMultipleUses() {
Dan Gohman9a658d72009-03-24 00:49:12 +00002807 // Visit all the nodes in topological order, working top-down.
2808 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
2809 SUnit *SU = &(*SUnits)[i];
2810 // For now, only look at nodes with no data successors, such as stores.
2811 // These are especially important, due to the heuristics in
2812 // getNodePriority for nodes with no data successors.
2813 if (SU->NumSuccs != 0)
2814 continue;
2815 // For now, only look at nodes with exactly one data predecessor.
2816 if (SU->NumPreds != 1)
2817 continue;
2818 // Avoid prescheduling copies to virtual registers, which don't behave
2819 // like other nodes from the perspective of scheduling heuristics.
2820 if (SDNode *N = SU->getNode())
2821 if (N->getOpcode() == ISD::CopyToReg &&
2822 TargetRegisterInfo::isVirtualRegister
2823 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2824 continue;
2825
2826 // Locate the single data predecessor.
Craig Topperc0196b12014-04-14 00:51:57 +00002827 SUnit *PredSU = nullptr;
Dan Gohman9a658d72009-03-24 00:49:12 +00002828 for (SUnit::const_pred_iterator II = SU->Preds.begin(),
2829 EE = SU->Preds.end(); II != EE; ++II)
2830 if (!II->isCtrl()) {
2831 PredSU = II->getSUnit();
2832 break;
2833 }
2834 assert(PredSU);
2835
2836 // Don't rewrite edges that carry physregs, because that requires additional
2837 // support infrastructure.
2838 if (PredSU->hasPhysRegDefs)
2839 continue;
2840 // Short-circuit the case where SU is PredSU's only data successor.
2841 if (PredSU->NumSuccs == 1)
2842 continue;
2843 // Avoid prescheduling to copies from virtual registers, which don't behave
Andrew Trickd0548ae2011-02-04 03:18:17 +00002844 // like other nodes from the perspective of scheduling heuristics.
Dan Gohman9a658d72009-03-24 00:49:12 +00002845 if (SDNode *N = SU->getNode())
2846 if (N->getOpcode() == ISD::CopyFromReg &&
2847 TargetRegisterInfo::isVirtualRegister
2848 (cast<RegisterSDNode>(N->getOperand(1))->getReg()))
2849 continue;
2850
2851 // Perform checks on the successors of PredSU.
2852 for (SUnit::const_succ_iterator II = PredSU->Succs.begin(),
2853 EE = PredSU->Succs.end(); II != EE; ++II) {
2854 SUnit *PredSuccSU = II->getSUnit();
2855 if (PredSuccSU == SU) continue;
2856 // If PredSU has another successor with no data successors, for
2857 // now don't attempt to choose either over the other.
2858 if (PredSuccSU->NumSuccs == 0)
2859 goto outer_loop_continue;
2860 // Don't break physical register dependencies.
2861 if (SU->hasPhysRegClobbers && PredSuccSU->hasPhysRegDefs)
2862 if (canClobberPhysRegDefs(PredSuccSU, SU, TII, TRI))
2863 goto outer_loop_continue;
2864 // Don't introduce graph cycles.
2865 if (scheduleDAG->IsReachable(SU, PredSuccSU))
2866 goto outer_loop_continue;
2867 }
2868
2869 // Ok, the transformation is safe and the heuristics suggest it is
2870 // profitable. Update the graph.
Evan Chengbdd062d2010-05-20 06:13:19 +00002871 DEBUG(dbgs() << " Prescheduling SU #" << SU->NodeNum
2872 << " next to PredSU #" << PredSU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002873 << " to guide scheduling in the presence of multiple uses\n");
Dan Gohman9a658d72009-03-24 00:49:12 +00002874 for (unsigned i = 0; i != PredSU->Succs.size(); ++i) {
2875 SDep Edge = PredSU->Succs[i];
2876 assert(!Edge.isAssignedRegDep());
2877 SUnit *SuccSU = Edge.getSUnit();
2878 if (SuccSU != SU) {
2879 Edge.setSUnit(PredSU);
2880 scheduleDAG->RemovePred(SuccSU, Edge);
2881 scheduleDAG->AddPred(SU, Edge);
2882 Edge.setSUnit(SU);
2883 scheduleDAG->AddPred(SuccSU, Edge);
2884 --i;
2885 }
2886 }
2887 outer_loop_continue:;
2888 }
2889}
2890
Evan Chengd38c22b2006-05-11 23:55:42 +00002891/// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
2892/// it as a def&use operand. Add a pseudo control edge from it to the other
2893/// node (if it won't create a cycle) so the two-address one will be scheduled
Evan Chenga5e595d2007-09-28 22:32:30 +00002894/// first (lower in the schedule). If both nodes are two-address, favor the
2895/// one that has a CopyToReg use (more likely to be a loop induction update).
2896/// If both are two-address, but one is commutable while the other is not
2897/// commutable, favor the one that's not commutable.
Duncan Sands635e4ef2011-11-09 14:20:48 +00002898void RegReductionPQBase::AddPseudoTwoAddrDeps() {
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002899 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
Dan Gohmane955c482008-08-05 14:45:15 +00002900 SUnit *SU = &(*SUnits)[i];
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002901 if (!SU->isTwoAddress)
2902 continue;
2903
Dan Gohman1ddfcba2008-11-13 21:36:12 +00002904 SDNode *Node = SU->getNode();
Chris Lattner11a33812010-12-23 17:24:32 +00002905 if (!Node || !Node->isMachineOpcode() || SU->getNode()->getGluedNode())
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002906 continue;
2907
Evan Cheng6c1414f2010-10-29 18:09:28 +00002908 bool isLiveOut = hasOnlyLiveOutUses(SU);
Dan Gohman17059682008-07-17 19:10:17 +00002909 unsigned Opc = Node->getMachineOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002910 const MCInstrDesc &MCID = TII->get(Opc);
2911 unsigned NumRes = MCID.getNumDefs();
2912 unsigned NumOps = MCID.getNumOperands() - NumRes;
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002913 for (unsigned j = 0; j != NumOps; ++j) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002914 if (MCID.getOperandConstraint(j+NumRes, MCOI::TIED_TO) == -1)
Dan Gohman82016c22008-11-19 02:00:32 +00002915 continue;
2916 SDNode *DU = SU->getNode()->getOperand(j).getNode();
2917 if (DU->getNodeId() == -1)
2918 continue;
2919 const SUnit *DUSU = &(*SUnits)[DU->getNodeId()];
2920 if (!DUSU) continue;
2921 for (SUnit::const_succ_iterator I = DUSU->Succs.begin(),
2922 E = DUSU->Succs.end(); I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +00002923 if (I->isCtrl()) continue;
2924 SUnit *SuccSU = I->getSUnit();
Dan Gohman82016c22008-11-19 02:00:32 +00002925 if (SuccSU == SU)
Evan Cheng1bf166312007-11-09 01:27:11 +00002926 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002927 // Be conservative. Ignore if nodes aren't at roughly the same
2928 // depth and height.
Dan Gohmandddc1ac2008-12-16 03:25:46 +00002929 if (SuccSU->getHeight() < SU->getHeight() &&
2930 (SU->getHeight() - SuccSU->getHeight()) > 1)
Dan Gohman82016c22008-11-19 02:00:32 +00002931 continue;
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002932 // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
2933 // constrains whatever is using the copy, instead of the copy
2934 // itself. In the case that the copy is coalesced, this
2935 // preserves the intent of the pseudo two-address heurietics.
2936 while (SuccSU->Succs.size() == 1 &&
2937 SuccSU->getNode()->isMachineOpcode() &&
2938 SuccSU->getNode()->getMachineOpcode() ==
Chris Lattnerb06015a2010-02-09 19:54:29 +00002939 TargetOpcode::COPY_TO_REGCLASS)
Dan Gohmaneefba6b2009-04-16 20:59:02 +00002940 SuccSU = SuccSU->Succs.front().getSUnit();
2941 // Don't constrain non-instruction nodes.
Dan Gohman82016c22008-11-19 02:00:32 +00002942 if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
2943 continue;
2944 // Don't constrain nodes with physical register defs if the
2945 // predecessor can clobber them.
Dan Gohmanf3746cb2009-03-24 00:50:07 +00002946 if (SuccSU->hasPhysRegDefs && SU->hasPhysRegClobbers) {
Dan Gohman82016c22008-11-19 02:00:32 +00002947 if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
Evan Cheng5924bf72007-09-25 01:54:36 +00002948 continue;
Dan Gohman82016c22008-11-19 02:00:32 +00002949 }
Dan Gohman3027bb62009-04-16 20:57:10 +00002950 // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
2951 // these may be coalesced away. We want them close to their uses.
Dan Gohman82016c22008-11-19 02:00:32 +00002952 unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
Chris Lattnerb06015a2010-02-09 19:54:29 +00002953 if (SuccOpc == TargetOpcode::EXTRACT_SUBREG ||
2954 SuccOpc == TargetOpcode::INSERT_SUBREG ||
2955 SuccOpc == TargetOpcode::SUBREG_TO_REG)
Dan Gohman82016c22008-11-19 02:00:32 +00002956 continue;
Andrew Trick832a6a192011-09-01 00:54:31 +00002957 if (!canClobberReachingPhysRegUse(SuccSU, SU, scheduleDAG, TII, TRI) &&
2958 (!canClobber(SuccSU, DUSU) ||
Evan Cheng6c1414f2010-10-29 18:09:28 +00002959 (isLiveOut && !hasOnlyLiveOutUses(SuccSU)) ||
Dan Gohman82016c22008-11-19 02:00:32 +00002960 (!SU->isCommutable && SuccSU->isCommutable)) &&
2961 !scheduleDAG->IsReachable(SuccSU, SU)) {
Evan Chengbdd062d2010-05-20 06:13:19 +00002962 DEBUG(dbgs() << " Adding a pseudo-two-addr edge from SU #"
Chris Lattner4dc3edd2009-08-23 06:35:02 +00002963 << SU->NodeNum << " to SU #" << SuccSU->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +00002964 scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Artificial));
Evan Chengfd2c5dd2006-11-04 09:44:31 +00002965 }
2966 }
2967 }
2968 }
Evan Chengd38c22b2006-05-11 23:55:42 +00002969}
2970
Evan Chengd38c22b2006-05-11 23:55:42 +00002971//===----------------------------------------------------------------------===//
2972// Public Constructor Functions
2973//===----------------------------------------------------------------------===//
2974
Dan Gohmandfaf6462009-02-11 04:27:20 +00002975llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002976llvm::createBURRListDAGScheduler(SelectionDAGISel *IS,
2977 CodeGenOpt::Level OptLevel) {
Dan Gohman619ef482009-01-15 19:20:50 +00002978 const TargetMachine &TM = IS->TM;
2979 const TargetInstrInfo *TII = TM.getInstrInfo();
2980 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002981
Evan Chenga77f3d32010-07-21 06:09:07 +00002982 BURegReductionPriorityQueue *PQ =
Craig Topperc0196b12014-04-14 00:51:57 +00002983 new BURegReductionPriorityQueue(*IS->MF, false, false, TII, TRI, nullptr);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002984 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Cheng7e4abde2008-07-02 09:23:51 +00002985 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00002986 return SD;
Evan Chengd38c22b2006-05-11 23:55:42 +00002987}
2988
Dan Gohmandfaf6462009-02-11 04:27:20 +00002989llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002990llvm::createSourceListDAGScheduler(SelectionDAGISel *IS,
2991 CodeGenOpt::Level OptLevel) {
Bill Wendling8cbc25d2010-01-23 10:26:57 +00002992 const TargetMachine &TM = IS->TM;
2993 const TargetInstrInfo *TII = TM.getInstrInfo();
2994 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Andrew Trick2085a962010-12-21 22:25:04 +00002995
Evan Chenga77f3d32010-07-21 06:09:07 +00002996 SrcRegReductionPriorityQueue *PQ =
Craig Topperc0196b12014-04-14 00:51:57 +00002997 new SrcRegReductionPriorityQueue(*IS->MF, false, true, TII, TRI, nullptr);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00002998 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, false, PQ, OptLevel);
Evan Chengbdd062d2010-05-20 06:13:19 +00002999 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003000 return SD;
Evan Chengbdd062d2010-05-20 06:13:19 +00003001}
3002
3003llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003004llvm::createHybridListDAGScheduler(SelectionDAGISel *IS,
3005 CodeGenOpt::Level OptLevel) {
Evan Chengbdd062d2010-05-20 06:13:19 +00003006 const TargetMachine &TM = IS->TM;
3007 const TargetInstrInfo *TII = TM.getInstrInfo();
3008 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Bill Wendlingf7719082013-06-06 00:43:09 +00003009 const TargetLowering *TLI = IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00003010
Evan Chenga77f3d32010-07-21 06:09:07 +00003011 HybridBURRPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00003012 new HybridBURRPriorityQueue(*IS->MF, true, false, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003013
3014 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Bill Wendling8cbc25d2010-01-23 10:26:57 +00003015 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003016 return SD;
Bill Wendling8cbc25d2010-01-23 10:26:57 +00003017}
Evan Cheng37b740c2010-07-24 00:39:05 +00003018
3019llvm::ScheduleDAGSDNodes *
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003020llvm::createILPListDAGScheduler(SelectionDAGISel *IS,
3021 CodeGenOpt::Level OptLevel) {
Evan Cheng37b740c2010-07-24 00:39:05 +00003022 const TargetMachine &TM = IS->TM;
3023 const TargetInstrInfo *TII = TM.getInstrInfo();
3024 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Bill Wendlingf7719082013-06-06 00:43:09 +00003025 const TargetLowering *TLI = IS->getTargetLowering();
Andrew Trick2085a962010-12-21 22:25:04 +00003026
Evan Cheng37b740c2010-07-24 00:39:05 +00003027 ILPBURRPriorityQueue *PQ =
Evan Cheng8ab58a22012-03-22 19:31:17 +00003028 new ILPBURRPriorityQueue(*IS->MF, true, false, TII, TRI, TLI);
Andrew Trick10ffc2b2010-12-24 05:03:26 +00003029 ScheduleDAGRRList *SD = new ScheduleDAGRRList(*IS->MF, true, PQ, OptLevel);
Evan Cheng37b740c2010-07-24 00:39:05 +00003030 PQ->setScheduleDAG(SD);
Andrew Trick2085a962010-12-21 22:25:04 +00003031 return SD;
Evan Cheng37b740c2010-07-24 00:39:05 +00003032}