blob: 01a2286b8d66a958a68f2c8c6c166383d4c1c8b3 [file] [log] [blame]
Andrew Trick6a50baa2012-05-17 22:37:09 +00001//===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===//
Andrew Tricke77e84e2012-01-13 06:30:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// MachineScheduler schedules machine instructions after phi elimination. It
11// preserves LiveIntervals so it can be invoked before register allocation.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/CodeGen/MachineScheduler.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000016#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/PriorityQueue.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/iterator_range.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000024#include "llvm/CodeGen/LiveInterval.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000026#include "llvm/CodeGen/MachineBasicBlock.h"
Jakub Staszakdf17ddd2013-03-10 13:11:23 +000027#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstr.h"
Jakub Staszakdf17ddd2013-03-10 13:11:23 +000031#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000032#include "llvm/CodeGen/MachineOperand.h"
33#include "llvm/CodeGen/MachinePassRegistry.h"
Andrew Trick736dd9a2013-06-21 18:32:58 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000035#include "llvm/CodeGen/MachineValueType.h"
Andrew Tricke77e84e2012-01-13 06:30:30 +000036#include "llvm/CodeGen/Passes.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000037#include "llvm/CodeGen/RegisterClassInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000038#include "llvm/CodeGen/RegisterPressure.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000039#include "llvm/CodeGen/ScheduleDAG.h"
40#include "llvm/CodeGen/ScheduleDAGInstrs.h"
41#include "llvm/CodeGen/ScheduleDAGMutation.h"
Andrew Trickcd1c2f92012-11-28 05:13:24 +000042#include "llvm/CodeGen/ScheduleDFS.h"
Andrew Trick61f1a272012-05-24 22:11:09 +000043#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000044#include "llvm/CodeGen/SlotIndexes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000045#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000046#include "llvm/CodeGen/TargetSchedule.h"
47#include "llvm/MC/LaneBitmask.h"
48#include "llvm/Pass.h"
Andrew Tricke77e84e2012-01-13 06:30:30 +000049#include "llvm/Support/CommandLine.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000050#include "llvm/Support/Compiler.h"
Andrew Tricke77e84e2012-01-13 06:30:30 +000051#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
Andrew Trickea9fd952013-01-25 07:45:29 +000053#include "llvm/Support/GraphWriter.h"
Andrew Tricke77e84e2012-01-13 06:30:30 +000054#include "llvm/Support/raw_ostream.h"
Jakub Staszak80df8b82013-06-14 00:00:13 +000055#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000056#include "llvm/Target/TargetLowering.h"
57#include "llvm/Target/TargetRegisterInfo.h"
58#include "llvm/Target/TargetSubtargetInfo.h"
59#include <algorithm>
60#include <cassert>
61#include <cstdint>
62#include <iterator>
63#include <limits>
64#include <memory>
65#include <string>
66#include <tuple>
67#include <utility>
68#include <vector>
Andrew Trick7ccdc5c2012-01-17 06:55:07 +000069
Andrew Tricke77e84e2012-01-13 06:30:30 +000070using namespace llvm;
71
Matthias Braun1527baa2017-05-25 21:26:32 +000072#define DEBUG_TYPE "machine-scheduler"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000073
Andrew Trick7a8e1002012-09-11 00:39:15 +000074namespace llvm {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000075
Andrew Trick7a8e1002012-09-11 00:39:15 +000076cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden,
77 cl::desc("Force top-down list scheduling"));
78cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden,
79 cl::desc("Force bottom-up list scheduling"));
Gerolf Hoflehnerb5220dc2014-08-07 21:49:44 +000080cl::opt<bool>
81DumpCriticalPathLength("misched-dcpl", cl::Hidden,
82 cl::desc("Print critical path length to stdout"));
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000083
84} // end namespace llvm
Andrew Trick8823dec2012-03-14 04:00:41 +000085
Andrew Tricka5f19562012-03-07 00:18:25 +000086#ifndef NDEBUG
87static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden,
88 cl::desc("Pop up a window to show MISched dags after they are processed"));
Lang Hamesdd98c492012-03-19 18:38:38 +000089
Matthias Braund78ee542015-09-17 21:09:59 +000090/// In some situations a few uninteresting nodes depend on nearly all other
91/// nodes in the graph, provide a cutoff to hide them.
92static cl::opt<unsigned> ViewMISchedCutoff("view-misched-cutoff", cl::Hidden,
93 cl::desc("Hide nodes with more predecessor/successor than cutoff"));
94
Lang Hamesdd98c492012-03-19 18:38:38 +000095static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden,
96 cl::desc("Stop scheduling after N instructions"), cl::init(~0U));
Andrew Trick33e05d72013-12-28 21:57:02 +000097
98static cl::opt<std::string> SchedOnlyFunc("misched-only-func", cl::Hidden,
99 cl::desc("Only schedule this function"));
100static cl::opt<unsigned> SchedOnlyBlock("misched-only-block", cl::Hidden,
101 cl::desc("Only schedule this MBB#"));
Andrew Tricka5f19562012-03-07 00:18:25 +0000102#else
103static bool ViewMISchedDAGs = false;
104#endif // NDEBUG
105
Matthias Braun6493bc22016-04-22 19:09:17 +0000106/// Avoid quadratic complexity in unusually large basic blocks by limiting the
107/// size of the ready lists.
108static cl::opt<unsigned> ReadyListLimit("misched-limit", cl::Hidden,
109 cl::desc("Limit ready list to N instructions"), cl::init(256));
110
Andrew Trickb6e74712013-09-04 20:59:59 +0000111static cl::opt<bool> EnableRegPressure("misched-regpressure", cl::Hidden,
112 cl::desc("Enable register pressure scheduling."), cl::init(true));
113
Andrew Trickc01b0042013-08-23 17:48:43 +0000114static cl::opt<bool> EnableCyclicPath("misched-cyclicpath", cl::Hidden,
Andrew Trick6c88b352013-09-09 23:31:14 +0000115 cl::desc("Enable cyclic critical path analysis."), cl::init(true));
Andrew Trickc01b0042013-08-23 17:48:43 +0000116
Jun Bum Lim4c5bd582016-04-15 14:58:38 +0000117static cl::opt<bool> EnableMemOpCluster("misched-cluster", cl::Hidden,
118 cl::desc("Enable memop clustering."),
119 cl::init(true));
Andrew Tricka7714a02012-11-12 19:40:10 +0000120
Andrew Trick48f2a722013-03-08 05:40:34 +0000121static cl::opt<bool> VerifyScheduling("verify-misched", cl::Hidden,
122 cl::desc("Verify machine instrs before and after machine scheduling"));
123
Andrew Trick44f750a2013-01-25 04:01:04 +0000124// DAG subtrees must have at least this many nodes.
125static const unsigned MinSubtreeSize = 8;
126
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000127// Pin the vtables to this file.
128void MachineSchedStrategy::anchor() {}
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000129
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000130void ScheduleDAGMutation::anchor() {}
131
Andrew Trick63440872012-01-14 02:17:06 +0000132//===----------------------------------------------------------------------===//
133// Machine Instruction Scheduling Pass and Registry
134//===----------------------------------------------------------------------===//
135
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000136MachineSchedContext::MachineSchedContext() {
Andrew Trick4d4b5462012-04-24 20:36:19 +0000137 RegClassInfo = new RegisterClassInfo();
138}
139
140MachineSchedContext::~MachineSchedContext() {
141 delete RegClassInfo;
142}
143
Andrew Tricke77e84e2012-01-13 06:30:30 +0000144namespace {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000145
Andrew Trickd7f890e2013-12-28 21:56:47 +0000146/// Base class for a machine scheduler class that can run at any point.
147class MachineSchedulerBase : public MachineSchedContext,
148 public MachineFunctionPass {
149public:
150 MachineSchedulerBase(char &ID): MachineFunctionPass(ID) {}
151
Craig Topperc0196b12014-04-14 00:51:57 +0000152 void print(raw_ostream &O, const Module* = nullptr) const override;
Andrew Trickd7f890e2013-12-28 21:56:47 +0000153
154protected:
Matthias Braun93563e72015-11-03 01:53:29 +0000155 void scheduleRegions(ScheduleDAGInstrs &Scheduler, bool FixKillFlags);
Andrew Trickd7f890e2013-12-28 21:56:47 +0000156};
157
Andrew Tricke1c034f2012-01-17 06:55:03 +0000158/// MachineScheduler runs after coalescing and before register allocation.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000159class MachineScheduler : public MachineSchedulerBase {
Andrew Tricke77e84e2012-01-13 06:30:30 +0000160public:
Andrew Tricke1c034f2012-01-17 06:55:03 +0000161 MachineScheduler();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000162
Craig Topper4584cd52014-03-07 09:26:03 +0000163 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Tricke77e84e2012-01-13 06:30:30 +0000164
Craig Topper4584cd52014-03-07 09:26:03 +0000165 bool runOnMachineFunction(MachineFunction&) override;
Andrew Tricke77e84e2012-01-13 06:30:30 +0000166
Andrew Tricke77e84e2012-01-13 06:30:30 +0000167 static char ID; // Class identification, replacement for typeinfo
Andrew Trick978674b2013-09-20 05:14:41 +0000168
169protected:
170 ScheduleDAGInstrs *createMachineScheduler();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000171};
Andrew Trick17080b92013-12-28 21:56:51 +0000172
173/// PostMachineScheduler runs after shortly before code emission.
174class PostMachineScheduler : public MachineSchedulerBase {
175public:
176 PostMachineScheduler();
177
Craig Topper4584cd52014-03-07 09:26:03 +0000178 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick17080b92013-12-28 21:56:51 +0000179
Craig Topper4584cd52014-03-07 09:26:03 +0000180 bool runOnMachineFunction(MachineFunction&) override;
Andrew Trick17080b92013-12-28 21:56:51 +0000181
182 static char ID; // Class identification, replacement for typeinfo
183
184protected:
185 ScheduleDAGInstrs *createPostMachineScheduler();
186};
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000187
188} // end anonymous namespace
Andrew Tricke77e84e2012-01-13 06:30:30 +0000189
Andrew Tricke1c034f2012-01-17 06:55:03 +0000190char MachineScheduler::ID = 0;
Andrew Tricke77e84e2012-01-13 06:30:30 +0000191
Andrew Tricke1c034f2012-01-17 06:55:03 +0000192char &llvm::MachineSchedulerID = MachineScheduler::ID;
Andrew Tricke77e84e2012-01-13 06:30:30 +0000193
Matthias Braun1527baa2017-05-25 21:26:32 +0000194INITIALIZE_PASS_BEGIN(MachineScheduler, DEBUG_TYPE,
Andrew Tricke77e84e2012-01-13 06:30:30 +0000195 "Machine Instruction Scheduler", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000196INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Davide Italiano6a1209e2017-03-24 20:52:56 +0000197INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Andrew Tricke77e84e2012-01-13 06:30:30 +0000198INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
199INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Matthias Braun1527baa2017-05-25 21:26:32 +0000200INITIALIZE_PASS_END(MachineScheduler, DEBUG_TYPE,
Andrew Tricke77e84e2012-01-13 06:30:30 +0000201 "Machine Instruction Scheduler", false, false)
202
Andrew Tricke1c034f2012-01-17 06:55:03 +0000203MachineScheduler::MachineScheduler()
Andrew Trickd7f890e2013-12-28 21:56:47 +0000204: MachineSchedulerBase(ID) {
Andrew Tricke1c034f2012-01-17 06:55:03 +0000205 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Tricke77e84e2012-01-13 06:30:30 +0000206}
207
Andrew Tricke1c034f2012-01-17 06:55:03 +0000208void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Tricke77e84e2012-01-13 06:30:30 +0000209 AU.setPreservesCFG();
210 AU.addRequiredID(MachineDominatorsID);
211 AU.addRequired<MachineLoopInfo>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000212 AU.addRequired<AAResultsWrapperPass>();
Andrew Trick45300682012-03-09 00:52:20 +0000213 AU.addRequired<TargetPassConfig>();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000214 AU.addRequired<SlotIndexes>();
215 AU.addPreserved<SlotIndexes>();
216 AU.addRequired<LiveIntervals>();
217 AU.addPreserved<LiveIntervals>();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000218 MachineFunctionPass::getAnalysisUsage(AU);
219}
220
Andrew Trick17080b92013-12-28 21:56:51 +0000221char PostMachineScheduler::ID = 0;
222
223char &llvm::PostMachineSchedulerID = PostMachineScheduler::ID;
224
225INITIALIZE_PASS(PostMachineScheduler, "postmisched",
Saleem Abdulrasool7230b372013-12-28 22:47:55 +0000226 "PostRA Machine Instruction Scheduler", false, false)
Andrew Trick17080b92013-12-28 21:56:51 +0000227
228PostMachineScheduler::PostMachineScheduler()
229: MachineSchedulerBase(ID) {
230 initializePostMachineSchedulerPass(*PassRegistry::getPassRegistry());
231}
232
233void PostMachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
234 AU.setPreservesCFG();
235 AU.addRequiredID(MachineDominatorsID);
236 AU.addRequired<MachineLoopInfo>();
237 AU.addRequired<TargetPassConfig>();
238 MachineFunctionPass::getAnalysisUsage(AU);
239}
240
Andrew Tricke77e84e2012-01-13 06:30:30 +0000241MachinePassRegistry MachineSchedRegistry::Registry;
242
Andrew Trick45300682012-03-09 00:52:20 +0000243/// A dummy default scheduler factory indicates whether the scheduler
244/// is overridden on the command line.
245static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) {
Craig Topperc0196b12014-04-14 00:51:57 +0000246 return nullptr;
Andrew Trick45300682012-03-09 00:52:20 +0000247}
Andrew Tricke77e84e2012-01-13 06:30:30 +0000248
249/// MachineSchedOpt allows command line selection of the scheduler.
250static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000251 RegisterPassParser<MachineSchedRegistry>>
Andrew Tricke77e84e2012-01-13 06:30:30 +0000252MachineSchedOpt("misched",
Andrew Trick45300682012-03-09 00:52:20 +0000253 cl::init(&useDefaultMachineSched), cl::Hidden,
Andrew Tricke77e84e2012-01-13 06:30:30 +0000254 cl::desc("Machine instruction scheduler to use"));
255
Andrew Trick45300682012-03-09 00:52:20 +0000256static MachineSchedRegistry
Andrew Trick8823dec2012-03-14 04:00:41 +0000257DefaultSchedRegistry("default", "Use the target's default scheduler choice.",
Andrew Trick45300682012-03-09 00:52:20 +0000258 useDefaultMachineSched);
259
Eric Christopher5f141b02015-03-11 22:56:10 +0000260static cl::opt<bool> EnableMachineSched(
261 "enable-misched",
262 cl::desc("Enable the machine instruction scheduling pass."), cl::init(true),
263 cl::Hidden);
264
Chad Rosier816a1ab2016-01-20 23:08:32 +0000265static cl::opt<bool> EnablePostRAMachineSched(
266 "enable-post-misched",
267 cl::desc("Enable the post-ra machine instruction scheduling pass."),
268 cl::init(true), cl::Hidden);
269
Andrew Trickcc45a282012-04-24 18:04:34 +0000270/// Decrement this iterator until reaching the top or a non-debug instr.
Andrew Trick2bc74c22013-08-30 04:36:57 +0000271static MachineBasicBlock::const_iterator
272priorNonDebug(MachineBasicBlock::const_iterator I,
273 MachineBasicBlock::const_iterator Beg) {
Andrew Trickcc45a282012-04-24 18:04:34 +0000274 assert(I != Beg && "reached the top of the region, cannot decrement");
275 while (--I != Beg) {
276 if (!I->isDebugValue())
277 break;
278 }
279 return I;
280}
281
Andrew Trick2bc74c22013-08-30 04:36:57 +0000282/// Non-const version.
283static MachineBasicBlock::iterator
284priorNonDebug(MachineBasicBlock::iterator I,
285 MachineBasicBlock::const_iterator Beg) {
Duncan P. N. Exon Smithdcbce9c2016-08-16 23:34:07 +0000286 return priorNonDebug(MachineBasicBlock::const_iterator(I), Beg)
287 .getNonConstIterator();
Andrew Trick2bc74c22013-08-30 04:36:57 +0000288}
289
Andrew Trickcc45a282012-04-24 18:04:34 +0000290/// If this iterator is a debug value, increment until reaching the End or a
291/// non-debug instruction.
Andrew Trick2c4f8b72013-08-31 05:17:58 +0000292static MachineBasicBlock::const_iterator
293nextIfDebug(MachineBasicBlock::const_iterator I,
294 MachineBasicBlock::const_iterator End) {
Andrew Trick463b2f12012-05-17 18:35:03 +0000295 for(; I != End; ++I) {
Andrew Trickcc45a282012-04-24 18:04:34 +0000296 if (!I->isDebugValue())
297 break;
298 }
299 return I;
300}
301
Andrew Trick2c4f8b72013-08-31 05:17:58 +0000302/// Non-const version.
303static MachineBasicBlock::iterator
304nextIfDebug(MachineBasicBlock::iterator I,
305 MachineBasicBlock::const_iterator End) {
Duncan P. N. Exon Smithdcbce9c2016-08-16 23:34:07 +0000306 return nextIfDebug(MachineBasicBlock::const_iterator(I), End)
307 .getNonConstIterator();
Andrew Trick2c4f8b72013-08-31 05:17:58 +0000308}
309
Andrew Trickdc4c1ad2013-09-24 17:11:19 +0000310/// Instantiate a ScheduleDAGInstrs that will be owned by the caller.
Andrew Trick978674b2013-09-20 05:14:41 +0000311ScheduleDAGInstrs *MachineScheduler::createMachineScheduler() {
312 // Select the scheduler, or set the default.
313 MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt;
314 if (Ctor != useDefaultMachineSched)
315 return Ctor(this);
316
317 // Get the default scheduler set by the target for this function.
318 ScheduleDAGInstrs *Scheduler = PassConfig->createMachineScheduler(this);
319 if (Scheduler)
320 return Scheduler;
321
322 // Default to GenericScheduler.
Andrew Trickd14d7c22013-12-28 21:56:57 +0000323 return createGenericSchedLive(this);
Andrew Trick978674b2013-09-20 05:14:41 +0000324}
325
Andrew Trick17080b92013-12-28 21:56:51 +0000326/// Instantiate a ScheduleDAGInstrs for PostRA scheduling that will be owned by
327/// the caller. We don't have a command line option to override the postRA
328/// scheduler. The Target must configure it.
329ScheduleDAGInstrs *PostMachineScheduler::createPostMachineScheduler() {
330 // Get the postRA scheduler set by the target for this function.
331 ScheduleDAGInstrs *Scheduler = PassConfig->createPostMachineScheduler(this);
332 if (Scheduler)
333 return Scheduler;
334
335 // Default to GenericScheduler.
Andrew Trickd14d7c22013-12-28 21:56:57 +0000336 return createGenericSchedPostRA(this);
Andrew Trick17080b92013-12-28 21:56:51 +0000337}
338
Andrew Trick72515be2012-03-14 04:00:38 +0000339/// Top-level MachineScheduler pass driver.
340///
341/// Visit blocks in function order. Divide each block into scheduling regions
Andrew Trick8823dec2012-03-14 04:00:41 +0000342/// and visit them bottom-up. Visiting regions bottom-up is not required, but is
343/// consistent with the DAG builder, which traverses the interior of the
344/// scheduling regions bottom-up.
Andrew Trick72515be2012-03-14 04:00:38 +0000345///
346/// This design avoids exposing scheduling boundaries to the DAG builder,
Andrew Trick8823dec2012-03-14 04:00:41 +0000347/// simplifying the DAG builder's support for "special" target instructions.
348/// At the same time the design allows target schedulers to operate across
Andrew Trick72515be2012-03-14 04:00:38 +0000349/// scheduling boundaries, for example to bundle the boudary instructions
350/// without reordering them. This creates complexity, because the target
351/// scheduler must update the RegionBegin and RegionEnd positions cached by
352/// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler
353/// design would be to split blocks at scheduling boundaries, but LLVM has a
354/// general bias against block splitting purely for implementation simplicity.
Andrew Tricke1c034f2012-01-17 06:55:03 +0000355bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000356 if (skipFunction(*mf.getFunction()))
Chad Rosier6338d7c2016-01-20 22:38:25 +0000357 return false;
358
Eric Christopher5f141b02015-03-11 22:56:10 +0000359 if (EnableMachineSched.getNumOccurrences()) {
360 if (!EnableMachineSched)
361 return false;
362 } else if (!mf.getSubtarget().enableMachineScheduler())
363 return false;
364
Matthias Braundc7580a2015-10-29 03:57:28 +0000365 DEBUG(dbgs() << "Before MISched:\n"; mf.print(dbgs()));
Andrew Trickc5d70082012-05-10 21:06:21 +0000366
Andrew Tricke77e84e2012-01-13 06:30:30 +0000367 // Initialize the context of the pass.
368 MF = &mf;
369 MLI = &getAnalysis<MachineLoopInfo>();
370 MDT = &getAnalysis<MachineDominatorTree>();
Andrew Trick45300682012-03-09 00:52:20 +0000371 PassConfig = &getAnalysis<TargetPassConfig>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000372 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Andrew Trick02a80da2012-03-08 01:41:12 +0000373
Lang Hamesad33d5a2012-01-27 22:36:19 +0000374 LIS = &getAnalysis<LiveIntervals>();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000375
Andrew Trick48f2a722013-03-08 05:40:34 +0000376 if (VerifyScheduling) {
Andrew Trick97064962013-07-25 07:26:26 +0000377 DEBUG(LIS->dump());
Andrew Trick48f2a722013-03-08 05:40:34 +0000378 MF->verify(this, "Before machine scheduling.");
379 }
Andrew Trick4d4b5462012-04-24 20:36:19 +0000380 RegClassInfo->runOnMachineFunction(*MF);
Andrew Trick88639922012-04-24 17:56:43 +0000381
Andrew Trick978674b2013-09-20 05:14:41 +0000382 // Instantiate the selected scheduler for this target, function, and
383 // optimization level.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000384 std::unique_ptr<ScheduleDAGInstrs> Scheduler(createMachineScheduler());
Matthias Braun93563e72015-11-03 01:53:29 +0000385 scheduleRegions(*Scheduler, false);
Andrew Trickd7f890e2013-12-28 21:56:47 +0000386
387 DEBUG(LIS->dump());
388 if (VerifyScheduling)
389 MF->verify(this, "After machine scheduling.");
390 return true;
391}
392
Andrew Trick17080b92013-12-28 21:56:51 +0000393bool PostMachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000394 if (skipFunction(*mf.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000395 return false;
396
Chad Rosier816a1ab2016-01-20 23:08:32 +0000397 if (EnablePostRAMachineSched.getNumOccurrences()) {
398 if (!EnablePostRAMachineSched)
399 return false;
400 } else if (!mf.getSubtarget().enablePostRAScheduler()) {
Andrew Trick8d2ee372014-06-04 07:06:27 +0000401 DEBUG(dbgs() << "Subtarget disables post-MI-sched.\n");
402 return false;
403 }
Andrew Trick17080b92013-12-28 21:56:51 +0000404 DEBUG(dbgs() << "Before post-MI-sched:\n"; mf.print(dbgs()));
405
406 // Initialize the context of the pass.
407 MF = &mf;
408 PassConfig = &getAnalysis<TargetPassConfig>();
409
410 if (VerifyScheduling)
411 MF->verify(this, "Before post machine scheduling.");
412
413 // Instantiate the selected scheduler for this target, function, and
414 // optimization level.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000415 std::unique_ptr<ScheduleDAGInstrs> Scheduler(createPostMachineScheduler());
Matthias Braun93563e72015-11-03 01:53:29 +0000416 scheduleRegions(*Scheduler, true);
Andrew Trick17080b92013-12-28 21:56:51 +0000417
418 if (VerifyScheduling)
419 MF->verify(this, "After post machine scheduling.");
420 return true;
421}
422
Andrew Trickd14d7c22013-12-28 21:56:57 +0000423/// Return true of the given instruction should not be included in a scheduling
424/// region.
425///
426/// MachineScheduler does not currently support scheduling across calls. To
427/// handle calls, the DAG builder needs to be modified to create register
428/// anti/output dependencies on the registers clobbered by the call's regmask
429/// operand. In PreRA scheduling, the stack pointer adjustment already prevents
430/// scheduling across calls. In PostRA scheduling, we need the isCall to enforce
431/// the boundary, but there would be no benefit to postRA scheduling across
432/// calls this late anyway.
433static bool isSchedBoundary(MachineBasicBlock::iterator MI,
434 MachineBasicBlock *MBB,
435 MachineFunction *MF,
Matthias Braun93563e72015-11-03 01:53:29 +0000436 const TargetInstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000437 return MI->isCall() || TII->isSchedulingBoundary(*MI, MBB, *MF);
Andrew Trickd14d7c22013-12-28 21:56:57 +0000438}
439
Andrew Trickd7f890e2013-12-28 21:56:47 +0000440/// Main driver for both MachineScheduler and PostMachineScheduler.
Matthias Braun93563e72015-11-03 01:53:29 +0000441void MachineSchedulerBase::scheduleRegions(ScheduleDAGInstrs &Scheduler,
442 bool FixKillFlags) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000443 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000444
445 // Visit all machine basic blocks.
Andrew Trick88639922012-04-24 17:56:43 +0000446 //
447 // TODO: Visit blocks in global postorder or postorder within the bottom-up
448 // loop tree. Then we can optionally compute global RegPressure.
Andrew Tricke77e84e2012-01-13 06:30:30 +0000449 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
450 MBB != MBBEnd; ++MBB) {
451
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000452 Scheduler.startBlock(&*MBB);
Andrew Trickedfe2ec2012-03-09 08:02:51 +0000453
Andrew Trick33e05d72013-12-28 21:57:02 +0000454#ifndef NDEBUG
455 if (SchedOnlyFunc.getNumOccurrences() && SchedOnlyFunc != MF->getName())
456 continue;
457 if (SchedOnlyBlock.getNumOccurrences()
458 && (int)SchedOnlyBlock != MBB->getNumber())
459 continue;
460#endif
461
Andrew Trick7e120f42012-01-14 02:17:09 +0000462 // Break the block into scheduling regions [I, RegionEnd), and schedule each
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000463 // region as soon as it is discovered. RegionEnd points the scheduling
Andrew Trickaf1bee72012-03-09 22:34:56 +0000464 // boundary at the bottom of the region. The DAG does not include RegionEnd,
465 // but the region does (i.e. the next RegionEnd is above the previous
466 // RegionBegin). If the current block has no terminator then RegionEnd ==
467 // MBB->end() for the bottom region.
468 //
469 // The Scheduler may insert instructions during either schedule() or
470 // exitRegion(), even for empty regions. So the local iterators 'I' and
471 // 'RegionEnd' are invalid across these calls.
Andrew Trickd14d7c22013-12-28 21:56:57 +0000472 //
473 // MBB::size() uses instr_iterator to count. Here we need a bundle to count
474 // as a single instruction.
Andrew Tricka21daf72012-03-09 03:46:39 +0000475 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
Andrew Trickd7f890e2013-12-28 21:56:47 +0000476 RegionEnd != MBB->begin(); RegionEnd = Scheduler.begin()) {
Andrew Trick88639922012-04-24 17:56:43 +0000477
Andrew Trickedfe2ec2012-03-09 08:02:51 +0000478 // Avoid decrementing RegionEnd for blocks with no terminator.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000479 if (RegionEnd != MBB->end() ||
Matthias Braun93563e72015-11-03 01:53:29 +0000480 isSchedBoundary(&*std::prev(RegionEnd), &*MBB, MF, TII)) {
Andrew Trickedfe2ec2012-03-09 08:02:51 +0000481 --RegionEnd;
Andrew Trickedfe2ec2012-03-09 08:02:51 +0000482 }
483
Andrew Trick7e120f42012-01-14 02:17:09 +0000484 // The next region starts above the previous region. Look backward in the
485 // instruction stream until we find the nearest boundary.
Andrew Tricka53e1012013-08-23 17:48:33 +0000486 unsigned NumRegionInstrs = 0;
Andrew Trick7e120f42012-01-14 02:17:09 +0000487 MachineBasicBlock::iterator I = RegionEnd;
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000488 for (; I != MBB->begin(); --I) {
Duncan P. N. Exon Smith38eea4a2016-08-11 20:03:09 +0000489 MachineInstr &MI = *std::prev(I);
490 if (isSchedBoundary(&MI, &*MBB, MF, TII))
Andrew Trick7e120f42012-01-14 02:17:09 +0000491 break;
Duncan P. N. Exon Smith38eea4a2016-08-11 20:03:09 +0000492 if (!MI.isDebugValue())
Andrea Di Biagiod65fd9f2014-12-12 15:09:58 +0000493 ++NumRegionInstrs;
Andrew Trick7e120f42012-01-14 02:17:09 +0000494 }
Andrew Trick60cf03e2012-03-07 05:21:52 +0000495 // Notify the scheduler of the region, even if we may skip scheduling
496 // it. Perhaps it still needs to be bundled.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000497 Scheduler.enterRegion(&*MBB, I, RegionEnd, NumRegionInstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000498
499 // Skip empty scheduling regions (0 or 1 schedulable instructions).
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000500 if (I == RegionEnd || I == std::prev(RegionEnd)) {
Andrew Trick60cf03e2012-03-07 05:21:52 +0000501 // Close the current region. Bundle the terminator if needed.
Andrew Trickaf1bee72012-03-09 22:34:56 +0000502 // This invalidates 'RegionEnd' and 'I'.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000503 Scheduler.exitRegion();
Andrew Trick7ccdc5c2012-01-17 06:55:07 +0000504 continue;
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000505 }
Matthias Braun93563e72015-11-03 01:53:29 +0000506 DEBUG(dbgs() << "********** MI Scheduling **********\n");
Craig Toppera538d832012-08-22 06:07:19 +0000507 DEBUG(dbgs() << MF->getName()
Andrew Trick54b2ce32013-01-25 07:45:31 +0000508 << ":BB#" << MBB->getNumber() << " " << MBB->getName()
509 << "\n From: " << *I << " To: ";
Andrew Tricke57583a2012-02-08 02:17:21 +0000510 if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
511 else dbgs() << "End";
Matthias Braun858d1df2016-05-20 19:46:13 +0000512 dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
Gerolf Hoflehnerb5220dc2014-08-07 21:49:44 +0000513 if (DumpCriticalPathLength) {
514 errs() << MF->getName();
515 errs() << ":BB# " << MBB->getNumber();
516 errs() << " " << MBB->getName() << " \n";
517 }
Andrew Trick7ccdc5c2012-01-17 06:55:07 +0000518
Andrew Trick1c0ec452012-03-09 03:46:42 +0000519 // Schedule a region: possibly reorder instructions.
Andrew Trickaf1bee72012-03-09 22:34:56 +0000520 // This invalidates 'RegionEnd' and 'I'.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000521 Scheduler.schedule();
Andrew Trick1c0ec452012-03-09 03:46:42 +0000522
523 // Close the current region.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000524 Scheduler.exitRegion();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000525
526 // Scheduling has invalidated the current iterator 'I'. Ask the
527 // scheduler for the top of it's scheduled region.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000528 RegionEnd = Scheduler.begin();
Andrew Trick7e120f42012-01-14 02:17:09 +0000529 }
Andrew Trickd7f890e2013-12-28 21:56:47 +0000530 Scheduler.finishBlock();
Matthias Braun93563e72015-11-03 01:53:29 +0000531 // FIXME: Ideally, no further passes should rely on kill flags. However,
532 // thumb2 size reduction is currently an exception, so the PostMIScheduler
533 // needs to do this.
534 if (FixKillFlags)
Matthias Braun868bbd42017-05-27 02:50:50 +0000535 Scheduler.fixupKills(*MBB);
Andrew Tricke77e84e2012-01-13 06:30:30 +0000536 }
Andrew Trickd7f890e2013-12-28 21:56:47 +0000537 Scheduler.finalizeSchedule();
Andrew Tricke77e84e2012-01-13 06:30:30 +0000538}
539
Andrew Trickd7f890e2013-12-28 21:56:47 +0000540void MachineSchedulerBase::print(raw_ostream &O, const Module* m) const {
Andrew Tricke77e84e2012-01-13 06:30:30 +0000541 // unimplemented
542}
543
Matthias Braun8c209aa2017-01-28 02:02:38 +0000544#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
545LLVM_DUMP_METHOD void ReadyQueue::dump() {
James Y Knighte72b0db2015-09-18 18:52:20 +0000546 dbgs() << "Queue " << Name << ": ";
Andrew Trick7a8e1002012-09-11 00:39:15 +0000547 for (unsigned i = 0, e = Queue.size(); i < e; ++i)
548 dbgs() << Queue[i]->NodeNum << " ";
549 dbgs() << "\n";
550}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000551#endif
Andrew Trick8823dec2012-03-14 04:00:41 +0000552
553//===----------------------------------------------------------------------===//
Andrew Trickd7f890e2013-12-28 21:56:47 +0000554// ScheduleDAGMI - Basic machine instruction scheduling. This is
555// independent of PreRA/PostRA scheduling and involves no extra book-keeping for
556// virtual registers.
557// ===----------------------------------------------------------------------===/
Andrew Trick8823dec2012-03-14 04:00:41 +0000558
David Blaikie422b93d2014-04-21 20:32:32 +0000559// Provide a vtable anchor.
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000560ScheduleDAGMI::~ScheduleDAGMI() = default;
Andrew Trick44f750a2013-01-25 04:01:04 +0000561
Andrew Trick85a1d4c2013-04-24 15:54:43 +0000562bool ScheduleDAGMI::canAddEdge(SUnit *SuccSU, SUnit *PredSU) {
563 return SuccSU == &ExitSU || !Topo.IsReachable(PredSU, SuccSU);
564}
565
Andrew Tricka7714a02012-11-12 19:40:10 +0000566bool ScheduleDAGMI::addEdge(SUnit *SuccSU, const SDep &PredDep) {
Andrew Trick263280242012-11-12 19:52:20 +0000567 if (SuccSU != &ExitSU) {
568 // Do not use WillCreateCycle, it assumes SD scheduling.
569 // If Pred is reachable from Succ, then the edge creates a cycle.
570 if (Topo.IsReachable(PredDep.getSUnit(), SuccSU))
571 return false;
572 Topo.AddPred(SuccSU, PredDep.getSUnit());
573 }
Andrew Tricka7714a02012-11-12 19:40:10 +0000574 SuccSU->addPred(PredDep, /*Required=*/!PredDep.isArtificial());
575 // Return true regardless of whether a new edge needed to be inserted.
576 return true;
577}
578
Andrew Trick02a80da2012-03-08 01:41:12 +0000579/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
580/// NumPredsLeft reaches zero, release the successor node.
Andrew Trick61f1a272012-05-24 22:11:09 +0000581///
582/// FIXME: Adjust SuccSU height based on MinLatency.
Andrew Trick8823dec2012-03-14 04:00:41 +0000583void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) {
Andrew Trick02a80da2012-03-08 01:41:12 +0000584 SUnit *SuccSU = SuccEdge->getSUnit();
585
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000586 if (SuccEdge->isWeak()) {
587 --SuccSU->WeakPredsLeft;
Andrew Tricka7714a02012-11-12 19:40:10 +0000588 if (SuccEdge->isCluster())
589 NextClusterSucc = SuccSU;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000590 return;
591 }
Andrew Trick02a80da2012-03-08 01:41:12 +0000592#ifndef NDEBUG
593 if (SuccSU->NumPredsLeft == 0) {
594 dbgs() << "*** Scheduling failed! ***\n";
595 SuccSU->dump(this);
596 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000597 llvm_unreachable(nullptr);
Andrew Trick02a80da2012-03-08 01:41:12 +0000598 }
599#endif
Andrew Trick7f1ebbe2014-06-07 01:48:43 +0000600 // SU->TopReadyCycle was set to CurrCycle when it was scheduled. However,
601 // CurrCycle may have advanced since then.
602 if (SuccSU->TopReadyCycle < SU->TopReadyCycle + SuccEdge->getLatency())
603 SuccSU->TopReadyCycle = SU->TopReadyCycle + SuccEdge->getLatency();
604
Andrew Trick02a80da2012-03-08 01:41:12 +0000605 --SuccSU->NumPredsLeft;
606 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Andrew Trick8823dec2012-03-14 04:00:41 +0000607 SchedImpl->releaseTopNode(SuccSU);
Andrew Trick02a80da2012-03-08 01:41:12 +0000608}
609
610/// releaseSuccessors - Call releaseSucc on each of SU's successors.
Andrew Trick8823dec2012-03-14 04:00:41 +0000611void ScheduleDAGMI::releaseSuccessors(SUnit *SU) {
Andrew Trick02a80da2012-03-08 01:41:12 +0000612 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
613 I != E; ++I) {
614 releaseSucc(SU, &*I);
615 }
616}
617
Andrew Trick8823dec2012-03-14 04:00:41 +0000618/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When
619/// NumSuccsLeft reaches zero, release the predecessor node.
Andrew Trick61f1a272012-05-24 22:11:09 +0000620///
621/// FIXME: Adjust PredSU height based on MinLatency.
Andrew Trick8823dec2012-03-14 04:00:41 +0000622void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) {
623 SUnit *PredSU = PredEdge->getSUnit();
624
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000625 if (PredEdge->isWeak()) {
626 --PredSU->WeakSuccsLeft;
Andrew Tricka7714a02012-11-12 19:40:10 +0000627 if (PredEdge->isCluster())
628 NextClusterPred = PredSU;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000629 return;
630 }
Andrew Trick8823dec2012-03-14 04:00:41 +0000631#ifndef NDEBUG
632 if (PredSU->NumSuccsLeft == 0) {
633 dbgs() << "*** Scheduling failed! ***\n";
634 PredSU->dump(this);
635 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000636 llvm_unreachable(nullptr);
Andrew Trick8823dec2012-03-14 04:00:41 +0000637 }
638#endif
Andrew Trick7f1ebbe2014-06-07 01:48:43 +0000639 // SU->BotReadyCycle was set to CurrCycle when it was scheduled. However,
640 // CurrCycle may have advanced since then.
641 if (PredSU->BotReadyCycle < SU->BotReadyCycle + PredEdge->getLatency())
642 PredSU->BotReadyCycle = SU->BotReadyCycle + PredEdge->getLatency();
643
Andrew Trick8823dec2012-03-14 04:00:41 +0000644 --PredSU->NumSuccsLeft;
645 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU)
646 SchedImpl->releaseBottomNode(PredSU);
647}
648
649/// releasePredecessors - Call releasePred on each of SU's predecessors.
650void ScheduleDAGMI::releasePredecessors(SUnit *SU) {
651 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
652 I != E; ++I) {
653 releasePred(SU, &*I);
654 }
655}
656
Andrew Trickd7f890e2013-12-28 21:56:47 +0000657/// enterRegion - Called back from MachineScheduler::runOnMachineFunction after
658/// crossing a scheduling boundary. [begin, end) includes all instructions in
659/// the region, including the boundary itself and single-instruction regions
660/// that don't get scheduled.
661void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb,
662 MachineBasicBlock::iterator begin,
663 MachineBasicBlock::iterator end,
664 unsigned regioninstrs)
665{
666 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
667
668 SchedImpl->initPolicy(begin, end, regioninstrs);
669}
670
Andrew Tricke833e1c2013-04-13 06:07:40 +0000671/// This is normally called from the main scheduler loop but may also be invoked
672/// by the scheduling strategy to perform additional code motion.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000673void ScheduleDAGMI::moveInstruction(
674 MachineInstr *MI, MachineBasicBlock::iterator InsertPos) {
Andrew Trick463b2f12012-05-17 18:35:03 +0000675 // Advance RegionBegin if the first instruction moves down.
Andrew Trick54f7def2012-03-21 04:12:10 +0000676 if (&*RegionBegin == MI)
Andrew Trick463b2f12012-05-17 18:35:03 +0000677 ++RegionBegin;
678
679 // Update the instruction stream.
Andrew Trick8823dec2012-03-14 04:00:41 +0000680 BB->splice(InsertPos, BB, MI);
Andrew Trick463b2f12012-05-17 18:35:03 +0000681
682 // Update LiveIntervals
Andrew Trickd7f890e2013-12-28 21:56:47 +0000683 if (LIS)
Duncan P. N. Exon Smithbe8f8c42016-02-27 20:14:29 +0000684 LIS->handleMove(*MI, /*UpdateFlags=*/true);
Andrew Trick463b2f12012-05-17 18:35:03 +0000685
686 // Recede RegionBegin if an instruction moves above the first.
Andrew Trick8823dec2012-03-14 04:00:41 +0000687 if (RegionBegin == InsertPos)
688 RegionBegin = MI;
689}
690
Andrew Trickde670c02012-03-21 04:12:07 +0000691bool ScheduleDAGMI::checkSchedLimit() {
692#ifndef NDEBUG
693 if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) {
694 CurrentTop = CurrentBottom;
695 return false;
696 }
697 ++NumInstrsScheduled;
698#endif
699 return true;
700}
701
Andrew Trickd7f890e2013-12-28 21:56:47 +0000702/// Per-region scheduling driver, called back from
703/// MachineScheduler::runOnMachineFunction. This is a simplified driver that
704/// does not consider liveness or register pressure. It is useful for PostRA
705/// scheduling and potentially other custom schedulers.
706void ScheduleDAGMI::schedule() {
James Y Knighte72b0db2015-09-18 18:52:20 +0000707 DEBUG(dbgs() << "ScheduleDAGMI::schedule starting\n");
708 DEBUG(SchedImpl->dumpPolicy());
709
Andrew Trickd7f890e2013-12-28 21:56:47 +0000710 // Build the DAG.
711 buildSchedGraph(AA);
712
713 Topo.InitDAGTopologicalSorting();
714
715 postprocessDAG();
716
717 SmallVector<SUnit*, 8> TopRoots, BotRoots;
718 findRootsAndBiasEdges(TopRoots, BotRoots);
719
720 // Initialize the strategy before modifying the DAG.
721 // This may initialize a DFSResult to be used for queue priority.
722 SchedImpl->initialize(this);
723
Matthias Braun69f1d122016-11-11 22:37:28 +0000724 DEBUG(
725 if (EntrySU.getInstr() != nullptr)
726 EntrySU.dumpAll(this);
727 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
728 SUnits[su].dumpAll(this);
729 if (ExitSU.getInstr() != nullptr)
730 ExitSU.dumpAll(this);
731 );
Andrew Trickd7f890e2013-12-28 21:56:47 +0000732 if (ViewMISchedDAGs) viewGraph();
733
734 // Initialize ready queues now that the DAG and priority data are finalized.
735 initQueues(TopRoots, BotRoots);
736
737 bool IsTopNode = false;
James Y Knighte72b0db2015-09-18 18:52:20 +0000738 while (true) {
739 DEBUG(dbgs() << "** ScheduleDAGMI::schedule picking next node\n");
740 SUnit *SU = SchedImpl->pickNode(IsTopNode);
741 if (!SU) break;
742
Andrew Trickd7f890e2013-12-28 21:56:47 +0000743 assert(!SU->isScheduled && "Node already scheduled");
744 if (!checkSchedLimit())
745 break;
746
747 MachineInstr *MI = SU->getInstr();
748 if (IsTopNode) {
749 assert(SU->isTopReady() && "node still has unscheduled dependencies");
750 if (&*CurrentTop == MI)
751 CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom);
752 else
753 moveInstruction(MI, CurrentTop);
Matthias Braunb550b762016-04-21 01:54:13 +0000754 } else {
Andrew Trickd7f890e2013-12-28 21:56:47 +0000755 assert(SU->isBottomReady() && "node still has unscheduled dependencies");
756 MachineBasicBlock::iterator priorII =
757 priorNonDebug(CurrentBottom, CurrentTop);
758 if (&*priorII == MI)
759 CurrentBottom = priorII;
760 else {
761 if (&*CurrentTop == MI)
762 CurrentTop = nextIfDebug(++CurrentTop, priorII);
763 moveInstruction(MI, CurrentBottom);
764 CurrentBottom = MI;
765 }
766 }
Andrew Trick7f1ebbe2014-06-07 01:48:43 +0000767 // Notify the scheduling strategy before updating the DAG.
Andrew Trick491e34a2014-06-12 22:36:28 +0000768 // This sets the scheduled node's ReadyCycle to CurrCycle. When updateQueues
Andrew Trick7f1ebbe2014-06-07 01:48:43 +0000769 // runs, it can then use the accurate ReadyCycle time to determine whether
770 // newly released nodes can move to the readyQ.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000771 SchedImpl->schedNode(SU, IsTopNode);
Andrew Trick7f1ebbe2014-06-07 01:48:43 +0000772
773 updateQueues(SU, IsTopNode);
Andrew Trickd7f890e2013-12-28 21:56:47 +0000774 }
775 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
776
777 placeDebugValues();
778
779 DEBUG({
780 unsigned BBNum = begin()->getParent()->getNumber();
781 dbgs() << "*** Final schedule for BB#" << BBNum << " ***\n";
782 dumpSchedule();
783 dbgs() << '\n';
784 });
785}
786
787/// Apply each ScheduleDAGMutation step in order.
788void ScheduleDAGMI::postprocessDAG() {
789 for (unsigned i = 0, e = Mutations.size(); i < e; ++i) {
790 Mutations[i]->apply(this);
791 }
792}
793
794void ScheduleDAGMI::
795findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots,
796 SmallVectorImpl<SUnit*> &BotRoots) {
797 for (std::vector<SUnit>::iterator
798 I = SUnits.begin(), E = SUnits.end(); I != E; ++I) {
799 SUnit *SU = &(*I);
800 assert(!SU->isBoundaryNode() && "Boundary node should not be in SUnits");
801
802 // Order predecessors so DFSResult follows the critical path.
803 SU->biasCriticalPath();
804
805 // A SUnit is ready to top schedule if it has no predecessors.
806 if (!I->NumPredsLeft)
807 TopRoots.push_back(SU);
808 // A SUnit is ready to bottom schedule if it has no successors.
809 if (!I->NumSuccsLeft)
810 BotRoots.push_back(SU);
811 }
812 ExitSU.biasCriticalPath();
813}
814
815/// Identify DAG roots and setup scheduler queues.
816void ScheduleDAGMI::initQueues(ArrayRef<SUnit*> TopRoots,
817 ArrayRef<SUnit*> BotRoots) {
Craig Topperc0196b12014-04-14 00:51:57 +0000818 NextClusterSucc = nullptr;
819 NextClusterPred = nullptr;
Andrew Trickd7f890e2013-12-28 21:56:47 +0000820
821 // Release all DAG roots for scheduling, not including EntrySU/ExitSU.
822 //
823 // Nodes with unreleased weak edges can still be roots.
824 // Release top roots in forward order.
825 for (SmallVectorImpl<SUnit*>::const_iterator
826 I = TopRoots.begin(), E = TopRoots.end(); I != E; ++I) {
827 SchedImpl->releaseTopNode(*I);
828 }
829 // Release bottom roots in reverse order so the higher priority nodes appear
830 // first. This is more natural and slightly more efficient.
831 for (SmallVectorImpl<SUnit*>::const_reverse_iterator
832 I = BotRoots.rbegin(), E = BotRoots.rend(); I != E; ++I) {
833 SchedImpl->releaseBottomNode(*I);
834 }
835
836 releaseSuccessors(&EntrySU);
837 releasePredecessors(&ExitSU);
838
839 SchedImpl->registerRoots();
840
841 // Advance past initial DebugValues.
842 CurrentTop = nextIfDebug(RegionBegin, RegionEnd);
843 CurrentBottom = RegionEnd;
844}
845
846/// Update scheduler queues after scheduling an instruction.
847void ScheduleDAGMI::updateQueues(SUnit *SU, bool IsTopNode) {
848 // Release dependent instructions for scheduling.
849 if (IsTopNode)
850 releaseSuccessors(SU);
851 else
852 releasePredecessors(SU);
853
854 SU->isScheduled = true;
855}
856
857/// Reinsert any remaining debug_values, just like the PostRA scheduler.
858void ScheduleDAGMI::placeDebugValues() {
859 // If first instruction was a DBG_VALUE then put it back.
860 if (FirstDbgValue) {
861 BB->splice(RegionBegin, BB, FirstDbgValue);
862 RegionBegin = FirstDbgValue;
863 }
864
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000865 for (std::vector<std::pair<MachineInstr *, MachineInstr *>>::iterator
Andrew Trickd7f890e2013-12-28 21:56:47 +0000866 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000867 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Trickd7f890e2013-12-28 21:56:47 +0000868 MachineInstr *DbgValue = P.first;
869 MachineBasicBlock::iterator OrigPrevMI = P.second;
870 if (&*RegionBegin == DbgValue)
871 ++RegionBegin;
872 BB->splice(++OrigPrevMI, BB, DbgValue);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000873 if (OrigPrevMI == std::prev(RegionEnd))
Andrew Trickd7f890e2013-12-28 21:56:47 +0000874 RegionEnd = DbgValue;
875 }
876 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000877 FirstDbgValue = nullptr;
Andrew Trickd7f890e2013-12-28 21:56:47 +0000878}
879
880#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000881LLVM_DUMP_METHOD void ScheduleDAGMI::dumpSchedule() const {
Andrew Trickd7f890e2013-12-28 21:56:47 +0000882 for (MachineBasicBlock::iterator MI = begin(), ME = end(); MI != ME; ++MI) {
883 if (SUnit *SU = getSUnit(&(*MI)))
884 SU->dump(this);
885 else
886 dbgs() << "Missing SUnit\n";
887 }
888}
889#endif
890
891//===----------------------------------------------------------------------===//
892// ScheduleDAGMILive - Base class for MachineInstr scheduling with LiveIntervals
893// preservation.
894//===----------------------------------------------------------------------===//
895
896ScheduleDAGMILive::~ScheduleDAGMILive() {
897 delete DFSResult;
898}
899
Matthias Braun40639882016-11-11 22:37:31 +0000900void ScheduleDAGMILive::collectVRegUses(SUnit &SU) {
901 const MachineInstr &MI = *SU.getInstr();
902 for (const MachineOperand &MO : MI.operands()) {
903 if (!MO.isReg())
904 continue;
905 if (!MO.readsReg())
906 continue;
907 if (TrackLaneMasks && !MO.isUse())
908 continue;
909
910 unsigned Reg = MO.getReg();
911 if (!TargetRegisterInfo::isVirtualRegister(Reg))
912 continue;
913
914 // Ignore re-defs.
915 if (TrackLaneMasks) {
916 bool FoundDef = false;
917 for (const MachineOperand &MO2 : MI.operands()) {
918 if (MO2.isReg() && MO2.isDef() && MO2.getReg() == Reg && !MO2.isDead()) {
919 FoundDef = true;
920 break;
921 }
922 }
923 if (FoundDef)
924 continue;
925 }
926
927 // Record this local VReg use.
928 VReg2SUnitMultiMap::iterator UI = VRegUses.find(Reg);
929 for (; UI != VRegUses.end(); ++UI) {
930 if (UI->SU == &SU)
931 break;
932 }
933 if (UI == VRegUses.end())
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000934 VRegUses.insert(VReg2SUnit(Reg, LaneBitmask::getNone(), &SU));
Matthias Braun40639882016-11-11 22:37:31 +0000935 }
936}
937
Andrew Trick88639922012-04-24 17:56:43 +0000938/// enterRegion - Called back from MachineScheduler::runOnMachineFunction after
939/// crossing a scheduling boundary. [begin, end) includes all instructions in
940/// the region, including the boundary itself and single-instruction regions
941/// that don't get scheduled.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000942void ScheduleDAGMILive::enterRegion(MachineBasicBlock *bb,
Andrew Trick88639922012-04-24 17:56:43 +0000943 MachineBasicBlock::iterator begin,
944 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000945 unsigned regioninstrs)
Andrew Trick88639922012-04-24 17:56:43 +0000946{
Andrew Trickd7f890e2013-12-28 21:56:47 +0000947 // ScheduleDAGMI initializes SchedImpl's per-region policy.
948 ScheduleDAGMI::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick4add42f2012-05-10 21:06:10 +0000949
950 // For convenience remember the end of the liveness region.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000951 LiveRegionEnd = (RegionEnd == bb->end()) ? RegionEnd : std::next(RegionEnd);
Andrew Trick75e411c2013-09-06 17:32:34 +0000952
Andrew Trickb248b4a2013-09-06 17:32:47 +0000953 SUPressureDiffs.clear();
954
Andrew Trick75e411c2013-09-06 17:32:34 +0000955 ShouldTrackPressure = SchedImpl->shouldTrackPressure();
Matthias Braund4f64092016-01-20 00:23:32 +0000956 ShouldTrackLaneMasks = SchedImpl->shouldTrackLaneMasks();
957
Matthias Braunf9acaca2016-05-31 22:38:06 +0000958 assert((!ShouldTrackLaneMasks || ShouldTrackPressure) &&
959 "ShouldTrackLaneMasks requires ShouldTrackPressure");
Andrew Trick4add42f2012-05-10 21:06:10 +0000960}
961
962// Setup the register pressure trackers for the top scheduled top and bottom
963// scheduled regions.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000964void ScheduleDAGMILive::initRegPressure() {
Matthias Braun40639882016-11-11 22:37:31 +0000965 VRegUses.clear();
966 VRegUses.setUniverse(MRI.getNumVirtRegs());
967 for (SUnit &SU : SUnits)
968 collectVRegUses(SU);
969
Matthias Braund4f64092016-01-20 00:23:32 +0000970 TopRPTracker.init(&MF, RegClassInfo, LIS, BB, RegionBegin,
971 ShouldTrackLaneMasks, false);
972 BotRPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd,
973 ShouldTrackLaneMasks, false);
Andrew Trick4add42f2012-05-10 21:06:10 +0000974
975 // Close the RPTracker to finalize live ins.
976 RPTracker.closeRegion();
977
Andrew Trick9c17eab2013-07-30 19:59:12 +0000978 DEBUG(RPTracker.dump());
Andrew Trick79d3eec2012-05-24 22:11:14 +0000979
Andrew Trick4add42f2012-05-10 21:06:10 +0000980 // Initialize the live ins and live outs.
Matthias Braun3e86de12015-09-17 21:12:24 +0000981 TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs);
982 BotRPTracker.addLiveRegs(RPTracker.getPressure().LiveOutRegs);
Andrew Trick4add42f2012-05-10 21:06:10 +0000983
984 // Close one end of the tracker so we can call
985 // getMaxUpward/DownwardPressureDelta before advancing across any
986 // instructions. This converts currently live regs into live ins/outs.
987 TopRPTracker.closeTop();
988 BotRPTracker.closeBottom();
989
Andrew Trick9c17eab2013-07-30 19:59:12 +0000990 BotRPTracker.initLiveThru(RPTracker);
991 if (!BotRPTracker.getLiveThru().empty()) {
992 TopRPTracker.initLiveThru(BotRPTracker.getLiveThru());
993 DEBUG(dbgs() << "Live Thru: ";
994 dumpRegSetPressure(BotRPTracker.getLiveThru(), TRI));
995 };
996
Andrew Trick2bc74c22013-08-30 04:36:57 +0000997 // For each live out vreg reduce the pressure change associated with other
998 // uses of the same vreg below the live-out reaching def.
Matthias Braun3e86de12015-09-17 21:12:24 +0000999 updatePressureDiffs(RPTracker.getPressure().LiveOutRegs);
Andrew Trick2bc74c22013-08-30 04:36:57 +00001000
Andrew Trick4add42f2012-05-10 21:06:10 +00001001 // Account for liveness generated by the region boundary.
Andrew Trick2bc74c22013-08-30 04:36:57 +00001002 if (LiveRegionEnd != RegionEnd) {
Matthias Braun5d458612016-01-20 00:23:26 +00001003 SmallVector<RegisterMaskPair, 8> LiveUses;
Andrew Trick2bc74c22013-08-30 04:36:57 +00001004 BotRPTracker.recede(&LiveUses);
1005 updatePressureDiffs(LiveUses);
1006 }
Andrew Trick4add42f2012-05-10 21:06:10 +00001007
Matthias Braune6edd482015-11-13 22:30:31 +00001008 DEBUG(
1009 dbgs() << "Top Pressure:\n";
1010 dumpRegSetPressure(TopRPTracker.getRegSetPressureAtPos(), TRI);
1011 dbgs() << "Bottom Pressure:\n";
1012 dumpRegSetPressure(BotRPTracker.getRegSetPressureAtPos(), TRI);
1013 );
1014
Andrew Trick4add42f2012-05-10 21:06:10 +00001015 assert(BotRPTracker.getPos() == RegionEnd && "Can't find the region bottom");
Andrew Trick22025772012-05-17 18:35:10 +00001016
1017 // Cache the list of excess pressure sets in this region. This will also track
1018 // the max pressure in the scheduled code for these sets.
1019 RegionCriticalPSets.clear();
Jakub Staszakc641ada2013-01-25 21:44:27 +00001020 const std::vector<unsigned> &RegionPressure =
1021 RPTracker.getPressure().MaxSetPressure;
Andrew Trick22025772012-05-17 18:35:10 +00001022 for (unsigned i = 0, e = RegionPressure.size(); i < e; ++i) {
Andrew Trick736dd9a2013-06-21 18:32:58 +00001023 unsigned Limit = RegClassInfo->getRegPressureSetLimit(i);
Andrew Trickb55db582013-06-21 18:33:01 +00001024 if (RegionPressure[i] > Limit) {
1025 DEBUG(dbgs() << TRI->getRegPressureSetName(i)
1026 << " Limit " << Limit
1027 << " Actual " << RegionPressure[i] << "\n");
Andrew Trick1a831342013-08-30 03:49:48 +00001028 RegionCriticalPSets.push_back(PressureChange(i));
Andrew Trickb55db582013-06-21 18:33:01 +00001029 }
Andrew Trick22025772012-05-17 18:35:10 +00001030 }
1031 DEBUG(dbgs() << "Excess PSets: ";
1032 for (unsigned i = 0, e = RegionCriticalPSets.size(); i != e; ++i)
1033 dbgs() << TRI->getRegPressureSetName(
Andrew Trick1a831342013-08-30 03:49:48 +00001034 RegionCriticalPSets[i].getPSet()) << " ";
Andrew Trick22025772012-05-17 18:35:10 +00001035 dbgs() << "\n");
1036}
1037
Andrew Trickd7f890e2013-12-28 21:56:47 +00001038void ScheduleDAGMILive::
Andrew Trickb248b4a2013-09-06 17:32:47 +00001039updateScheduledPressure(const SUnit *SU,
1040 const std::vector<unsigned> &NewMaxPressure) {
1041 const PressureDiff &PDiff = getPressureDiff(SU);
1042 unsigned CritIdx = 0, CritEnd = RegionCriticalPSets.size();
1043 for (PressureDiff::const_iterator I = PDiff.begin(), E = PDiff.end();
1044 I != E; ++I) {
1045 if (!I->isValid())
1046 break;
1047 unsigned ID = I->getPSet();
1048 while (CritIdx != CritEnd && RegionCriticalPSets[CritIdx].getPSet() < ID)
1049 ++CritIdx;
1050 if (CritIdx != CritEnd && RegionCriticalPSets[CritIdx].getPSet() == ID) {
1051 if ((int)NewMaxPressure[ID] > RegionCriticalPSets[CritIdx].getUnitInc()
Simon Pilgrim858d8e62017-02-23 12:00:34 +00001052 && NewMaxPressure[ID] <= (unsigned)std::numeric_limits<int16_t>::max())
Andrew Trickb248b4a2013-09-06 17:32:47 +00001053 RegionCriticalPSets[CritIdx].setUnitInc(NewMaxPressure[ID]);
1054 }
1055 unsigned Limit = RegClassInfo->getRegPressureSetLimit(ID);
1056 if (NewMaxPressure[ID] >= Limit - 2) {
1057 DEBUG(dbgs() << " " << TRI->getRegPressureSetName(ID) << ": "
Andrew Trick569dc65a2015-05-17 23:40:31 +00001058 << NewMaxPressure[ID]
1059 << ((NewMaxPressure[ID] > Limit) ? " > " : " <= ") << Limit
1060 << "(+ " << BotRPTracker.getLiveThru()[ID] << " livethru)\n");
Andrew Trickb248b4a2013-09-06 17:32:47 +00001061 }
Andrew Trick22025772012-05-17 18:35:10 +00001062 }
Andrew Trick88639922012-04-24 17:56:43 +00001063}
1064
Andrew Trick2bc74c22013-08-30 04:36:57 +00001065/// Update the PressureDiff array for liveness after scheduling this
1066/// instruction.
Matthias Braun5d458612016-01-20 00:23:26 +00001067void ScheduleDAGMILive::updatePressureDiffs(
1068 ArrayRef<RegisterMaskPair> LiveUses) {
1069 for (const RegisterMaskPair &P : LiveUses) {
Matthias Braun5d458612016-01-20 00:23:26 +00001070 unsigned Reg = P.RegUnit;
Matthias Braund4f64092016-01-20 00:23:32 +00001071 /// FIXME: Currently assuming single-use physregs.
Andrew Trick2bc74c22013-08-30 04:36:57 +00001072 if (!TRI->isVirtualRegister(Reg))
1073 continue;
Andrew Trickffdbefb2013-09-06 17:32:39 +00001074
Matthias Braund4f64092016-01-20 00:23:32 +00001075 if (ShouldTrackLaneMasks) {
1076 // If the register has just become live then other uses won't change
1077 // this fact anymore => decrement pressure.
1078 // If the register has just become dead then other uses make it come
1079 // back to life => increment pressure.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001080 bool Decrement = P.LaneMask.any();
Matthias Braund4f64092016-01-20 00:23:32 +00001081
1082 for (const VReg2SUnit &V2SU
1083 : make_range(VRegUses.find(Reg), VRegUses.end())) {
1084 SUnit &SU = *V2SU.SU;
1085 if (SU.isScheduled || &SU == &ExitSU)
1086 continue;
1087
1088 PressureDiff &PDiff = getPressureDiff(&SU);
Stanislav Mekhanoshin42259cf2017-02-24 21:56:16 +00001089 PDiff.addPressureChange(Reg, Decrement, &MRI);
Matthias Braund4f64092016-01-20 00:23:32 +00001090 DEBUG(
1091 dbgs() << " UpdateRegP: SU(" << SU.NodeNum << ") "
1092 << PrintReg(Reg, TRI) << ':' << PrintLaneMask(P.LaneMask)
1093 << ' ' << *SU.getInstr();
1094 dbgs() << " to ";
1095 PDiff.dump(*TRI);
1096 );
1097 }
1098 } else {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +00001099 assert(P.LaneMask.any());
Matthias Braund4f64092016-01-20 00:23:32 +00001100 DEBUG(dbgs() << " LiveReg: " << PrintVRegOrUnit(Reg, TRI) << "\n");
1101 // This may be called before CurrentBottom has been initialized. However,
1102 // BotRPTracker must have a valid position. We want the value live into the
1103 // instruction or live out of the block, so ask for the previous
1104 // instruction's live-out.
1105 const LiveInterval &LI = LIS->getInterval(Reg);
1106 VNInfo *VNI;
1107 MachineBasicBlock::const_iterator I =
1108 nextIfDebug(BotRPTracker.getPos(), BB->end());
1109 if (I == BB->end())
1110 VNI = LI.getVNInfoBefore(LIS->getMBBEndIdx(BB));
1111 else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001112 LiveQueryResult LRQ = LI.Query(LIS->getInstructionIndex(*I));
Matthias Braund4f64092016-01-20 00:23:32 +00001113 VNI = LRQ.valueIn();
1114 }
1115 // RegisterPressureTracker guarantees that readsReg is true for LiveUses.
1116 assert(VNI && "No live value at use.");
1117 for (const VReg2SUnit &V2SU
1118 : make_range(VRegUses.find(Reg), VRegUses.end())) {
1119 SUnit *SU = V2SU.SU;
1120 // If this use comes before the reaching def, it cannot be a last use,
1121 // so decrease its pressure change.
1122 if (!SU->isScheduled && SU != &ExitSU) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001123 LiveQueryResult LRQ =
1124 LI.Query(LIS->getInstructionIndex(*SU->getInstr()));
Matthias Braund4f64092016-01-20 00:23:32 +00001125 if (LRQ.valueIn() == VNI) {
1126 PressureDiff &PDiff = getPressureDiff(SU);
Stanislav Mekhanoshin42259cf2017-02-24 21:56:16 +00001127 PDiff.addPressureChange(Reg, true, &MRI);
Matthias Braund4f64092016-01-20 00:23:32 +00001128 DEBUG(
1129 dbgs() << " UpdateRegP: SU(" << SU->NodeNum << ") "
1130 << *SU->getInstr();
1131 dbgs() << " to ";
1132 PDiff.dump(*TRI);
1133 );
1134 }
Matthias Braun9198c672015-11-06 20:59:02 +00001135 }
Andrew Trick2bc74c22013-08-30 04:36:57 +00001136 }
1137 }
1138 }
1139}
1140
Andrew Trick8823dec2012-03-14 04:00:41 +00001141/// schedule - Called back from MachineScheduler::runOnMachineFunction
Andrew Trick88639922012-04-24 17:56:43 +00001142/// after setting up the current scheduling region. [RegionBegin, RegionEnd)
1143/// only includes instructions that have DAG nodes, not scheduling boundaries.
Andrew Trick7a8e1002012-09-11 00:39:15 +00001144///
1145/// This is a skeletal driver, with all the functionality pushed into helpers,
Nick Lewycky06b0ea22015-08-18 22:41:58 +00001146/// so that it can be easily extended by experimental schedulers. Generally,
Andrew Trick7a8e1002012-09-11 00:39:15 +00001147/// implementing MachineSchedStrategy should be sufficient to implement a new
1148/// scheduling algorithm. However, if a scheduler further subclasses
Andrew Trickd7f890e2013-12-28 21:56:47 +00001149/// ScheduleDAGMILive then it will want to override this virtual method in order
1150/// to update any specialized state.
1151void ScheduleDAGMILive::schedule() {
James Y Knighte72b0db2015-09-18 18:52:20 +00001152 DEBUG(dbgs() << "ScheduleDAGMILive::schedule starting\n");
1153 DEBUG(SchedImpl->dumpPolicy());
Andrew Trick7a8e1002012-09-11 00:39:15 +00001154 buildDAGWithRegPressure();
1155
Andrew Tricka7714a02012-11-12 19:40:10 +00001156 Topo.InitDAGTopologicalSorting();
1157
Andrew Tricka2733e92012-09-14 17:22:42 +00001158 postprocessDAG();
1159
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001160 SmallVector<SUnit*, 8> TopRoots, BotRoots;
1161 findRootsAndBiasEdges(TopRoots, BotRoots);
1162
1163 // Initialize the strategy before modifying the DAG.
1164 // This may initialize a DFSResult to be used for queue priority.
1165 SchedImpl->initialize(this);
1166
Matthias Braun9198c672015-11-06 20:59:02 +00001167 DEBUG(
Matthias Braun69f1d122016-11-11 22:37:28 +00001168 if (EntrySU.getInstr() != nullptr)
1169 EntrySU.dumpAll(this);
Matthias Braun9198c672015-11-06 20:59:02 +00001170 for (const SUnit &SU : SUnits) {
1171 SU.dumpAll(this);
1172 if (ShouldTrackPressure) {
1173 dbgs() << " Pressure Diff : ";
1174 getPressureDiff(&SU).dump(*TRI);
1175 }
Javed Absar3d594372017-03-27 20:46:37 +00001176 dbgs() << " Single Issue : ";
1177 if (SchedModel.mustBeginGroup(SU.getInstr()) &&
1178 SchedModel.mustEndGroup(SU.getInstr()))
1179 dbgs() << "true;";
1180 else
1181 dbgs() << "false;";
Matthias Braun9198c672015-11-06 20:59:02 +00001182 dbgs() << '\n';
1183 }
Matthias Braun69f1d122016-11-11 22:37:28 +00001184 if (ExitSU.getInstr() != nullptr)
1185 ExitSU.dumpAll(this);
Matthias Braun9198c672015-11-06 20:59:02 +00001186 );
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001187 if (ViewMISchedDAGs) viewGraph();
Andrew Trick7a8e1002012-09-11 00:39:15 +00001188
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001189 // Initialize ready queues now that the DAG and priority data are finalized.
1190 initQueues(TopRoots, BotRoots);
Andrew Trick7a8e1002012-09-11 00:39:15 +00001191
1192 bool IsTopNode = false;
James Y Knighte72b0db2015-09-18 18:52:20 +00001193 while (true) {
1194 DEBUG(dbgs() << "** ScheduleDAGMILive::schedule picking next node\n");
1195 SUnit *SU = SchedImpl->pickNode(IsTopNode);
1196 if (!SU) break;
1197
Andrew Trick984d98b2012-10-08 18:53:53 +00001198 assert(!SU->isScheduled && "Node already scheduled");
Andrew Trick7a8e1002012-09-11 00:39:15 +00001199 if (!checkSchedLimit())
1200 break;
1201
1202 scheduleMI(SU, IsTopNode);
1203
Andrew Trickd7f890e2013-12-28 21:56:47 +00001204 if (DFSResult) {
1205 unsigned SubtreeID = DFSResult->getSubtreeID(SU);
1206 if (!ScheduledTrees.test(SubtreeID)) {
1207 ScheduledTrees.set(SubtreeID);
1208 DFSResult->scheduleTree(SubtreeID);
1209 SchedImpl->scheduleTree(SubtreeID);
1210 }
1211 }
1212
1213 // Notify the scheduling strategy after updating the DAG.
1214 SchedImpl->schedNode(SU, IsTopNode);
Andrew Trick43adfb32015-03-27 06:10:13 +00001215
1216 updateQueues(SU, IsTopNode);
Andrew Trick7a8e1002012-09-11 00:39:15 +00001217 }
1218 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
1219
1220 placeDebugValues();
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001221
1222 DEBUG({
Andrew Trickcf7e6972012-11-28 03:42:47 +00001223 unsigned BBNum = begin()->getParent()->getNumber();
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001224 dbgs() << "*** Final schedule for BB#" << BBNum << " ***\n";
1225 dumpSchedule();
1226 dbgs() << '\n';
1227 });
Andrew Trick7a8e1002012-09-11 00:39:15 +00001228}
1229
1230/// Build the DAG and setup three register pressure trackers.
Andrew Trickd7f890e2013-12-28 21:56:47 +00001231void ScheduleDAGMILive::buildDAGWithRegPressure() {
Andrew Trickb6e74712013-09-04 20:59:59 +00001232 if (!ShouldTrackPressure) {
1233 RPTracker.reset();
1234 RegionCriticalPSets.clear();
1235 buildSchedGraph(AA);
1236 return;
1237 }
1238
Andrew Trick4add42f2012-05-10 21:06:10 +00001239 // Initialize the register pressure tracker used by buildSchedGraph.
Andrew Trick9c17eab2013-07-30 19:59:12 +00001240 RPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd,
Matthias Braund4f64092016-01-20 00:23:32 +00001241 ShouldTrackLaneMasks, /*TrackUntiedDefs=*/true);
Andrew Trick88639922012-04-24 17:56:43 +00001242
Andrew Trick4add42f2012-05-10 21:06:10 +00001243 // Account for liveness generate by the region boundary.
1244 if (LiveRegionEnd != RegionEnd)
1245 RPTracker.recede();
1246
1247 // Build the DAG, and compute current register pressure.
Matthias Braund4f64092016-01-20 00:23:32 +00001248 buildSchedGraph(AA, &RPTracker, &SUPressureDiffs, LIS, ShouldTrackLaneMasks);
Andrew Trick02a80da2012-03-08 01:41:12 +00001249
Andrew Trick4add42f2012-05-10 21:06:10 +00001250 // Initialize top/bottom trackers after computing region pressure.
1251 initRegPressure();
Andrew Trick7a8e1002012-09-11 00:39:15 +00001252}
Andrew Trick4add42f2012-05-10 21:06:10 +00001253
Andrew Trickd7f890e2013-12-28 21:56:47 +00001254void ScheduleDAGMILive::computeDFSResult() {
Andrew Trick44f750a2013-01-25 04:01:04 +00001255 if (!DFSResult)
1256 DFSResult = new SchedDFSResult(/*BottomU*/true, MinSubtreeSize);
1257 DFSResult->clear();
Andrew Trick44f750a2013-01-25 04:01:04 +00001258 ScheduledTrees.clear();
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001259 DFSResult->resize(SUnits.size());
1260 DFSResult->compute(SUnits);
Andrew Trick44f750a2013-01-25 04:01:04 +00001261 ScheduledTrees.resize(DFSResult->getNumSubtrees());
1262}
1263
Andrew Trick483f4192013-08-29 18:04:49 +00001264/// Compute the max cyclic critical path through the DAG. The scheduling DAG
1265/// only provides the critical path for single block loops. To handle loops that
1266/// span blocks, we could use the vreg path latencies provided by
1267/// MachineTraceMetrics instead. However, MachineTraceMetrics is not currently
1268/// available for use in the scheduler.
1269///
1270/// The cyclic path estimation identifies a def-use pair that crosses the back
Andrew Trickef80f502013-08-30 02:02:12 +00001271/// edge and considers the depth and height of the nodes. For example, consider
Andrew Trick483f4192013-08-29 18:04:49 +00001272/// the following instruction sequence where each instruction has unit latency
1273/// and defines an epomymous virtual register:
1274///
1275/// a->b(a,c)->c(b)->d(c)->exit
1276///
1277/// The cyclic critical path is a two cycles: b->c->b
1278/// The acyclic critical path is four cycles: a->b->c->d->exit
1279/// LiveOutHeight = height(c) = len(c->d->exit) = 2
1280/// LiveOutDepth = depth(c) + 1 = len(a->b->c) + 1 = 3
1281/// LiveInHeight = height(b) + 1 = len(b->c->d->exit) + 1 = 4
1282/// LiveInDepth = depth(b) = len(a->b) = 1
1283///
1284/// LiveOutDepth - LiveInDepth = 3 - 1 = 2
1285/// LiveInHeight - LiveOutHeight = 4 - 2 = 2
1286/// CyclicCriticalPath = min(2, 2) = 2
Andrew Trickd7f890e2013-12-28 21:56:47 +00001287///
1288/// This could be relevant to PostRA scheduling, but is currently implemented
1289/// assuming LiveIntervals.
1290unsigned ScheduleDAGMILive::computeCyclicCriticalPath() {
Andrew Trick483f4192013-08-29 18:04:49 +00001291 // This only applies to single block loop.
1292 if (!BB->isSuccessor(BB))
1293 return 0;
1294
1295 unsigned MaxCyclicLatency = 0;
1296 // Visit each live out vreg def to find def/use pairs that cross iterations.
Matthias Braun5d458612016-01-20 00:23:26 +00001297 for (const RegisterMaskPair &P : RPTracker.getPressure().LiveOutRegs) {
1298 unsigned Reg = P.RegUnit;
Andrew Trick483f4192013-08-29 18:04:49 +00001299 if (!TRI->isVirtualRegister(Reg))
1300 continue;
1301 const LiveInterval &LI = LIS->getInterval(Reg);
1302 const VNInfo *DefVNI = LI.getVNInfoBefore(LIS->getMBBEndIdx(BB));
1303 if (!DefVNI)
1304 continue;
1305
1306 MachineInstr *DefMI = LIS->getInstructionFromIndex(DefVNI->def);
1307 const SUnit *DefSU = getSUnit(DefMI);
1308 if (!DefSU)
1309 continue;
1310
1311 unsigned LiveOutHeight = DefSU->getHeight();
1312 unsigned LiveOutDepth = DefSU->getDepth() + DefSU->Latency;
1313 // Visit all local users of the vreg def.
Matthias Braunb0c437b2015-10-29 03:57:17 +00001314 for (const VReg2SUnit &V2SU
1315 : make_range(VRegUses.find(Reg), VRegUses.end())) {
1316 SUnit *SU = V2SU.SU;
1317 if (SU == &ExitSU)
Andrew Trick483f4192013-08-29 18:04:49 +00001318 continue;
1319
1320 // Only consider uses of the phi.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001321 LiveQueryResult LRQ = LI.Query(LIS->getInstructionIndex(*SU->getInstr()));
Andrew Trick483f4192013-08-29 18:04:49 +00001322 if (!LRQ.valueIn()->isPHIDef())
1323 continue;
1324
1325 // Assume that a path spanning two iterations is a cycle, which could
1326 // overestimate in strange cases. This allows cyclic latency to be
1327 // estimated as the minimum slack of the vreg's depth or height.
1328 unsigned CyclicLatency = 0;
Matthias Braunb0c437b2015-10-29 03:57:17 +00001329 if (LiveOutDepth > SU->getDepth())
1330 CyclicLatency = LiveOutDepth - SU->getDepth();
Andrew Trick483f4192013-08-29 18:04:49 +00001331
Matthias Braunb0c437b2015-10-29 03:57:17 +00001332 unsigned LiveInHeight = SU->getHeight() + DefSU->Latency;
Andrew Trick483f4192013-08-29 18:04:49 +00001333 if (LiveInHeight > LiveOutHeight) {
1334 if (LiveInHeight - LiveOutHeight < CyclicLatency)
1335 CyclicLatency = LiveInHeight - LiveOutHeight;
Matthias Braunb550b762016-04-21 01:54:13 +00001336 } else
Andrew Trick483f4192013-08-29 18:04:49 +00001337 CyclicLatency = 0;
1338
1339 DEBUG(dbgs() << "Cyclic Path: SU(" << DefSU->NodeNum << ") -> SU("
Matthias Braunb0c437b2015-10-29 03:57:17 +00001340 << SU->NodeNum << ") = " << CyclicLatency << "c\n");
Andrew Trick483f4192013-08-29 18:04:49 +00001341 if (CyclicLatency > MaxCyclicLatency)
1342 MaxCyclicLatency = CyclicLatency;
1343 }
1344 }
1345 DEBUG(dbgs() << "Cyclic Critical Path: " << MaxCyclicLatency << "c\n");
1346 return MaxCyclicLatency;
1347}
1348
Krzysztof Parzyszek7ea9a522016-04-28 19:17:44 +00001349/// Release ExitSU predecessors and setup scheduler queues. Re-position
1350/// the Top RP tracker in case the region beginning has changed.
1351void ScheduleDAGMILive::initQueues(ArrayRef<SUnit*> TopRoots,
1352 ArrayRef<SUnit*> BotRoots) {
1353 ScheduleDAGMI::initQueues(TopRoots, BotRoots);
1354 if (ShouldTrackPressure) {
1355 assert(TopRPTracker.getPos() == RegionBegin && "bad initial Top tracker");
1356 TopRPTracker.setPos(CurrentTop);
1357 }
1358}
1359
Andrew Trick7a8e1002012-09-11 00:39:15 +00001360/// Move an instruction and update register pressure.
Andrew Trickd7f890e2013-12-28 21:56:47 +00001361void ScheduleDAGMILive::scheduleMI(SUnit *SU, bool IsTopNode) {
Andrew Trick7a8e1002012-09-11 00:39:15 +00001362 // Move the instruction to its new location in the instruction stream.
1363 MachineInstr *MI = SU->getInstr();
Andrew Trick02a80da2012-03-08 01:41:12 +00001364
Andrew Trick7a8e1002012-09-11 00:39:15 +00001365 if (IsTopNode) {
1366 assert(SU->isTopReady() && "node still has unscheduled dependencies");
1367 if (&*CurrentTop == MI)
1368 CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom);
Andrew Trick8823dec2012-03-14 04:00:41 +00001369 else {
Andrew Trick7a8e1002012-09-11 00:39:15 +00001370 moveInstruction(MI, CurrentTop);
1371 TopRPTracker.setPos(MI);
Andrew Trick8823dec2012-03-14 04:00:41 +00001372 }
Andrew Trickc3ea0052012-04-24 18:04:37 +00001373
Andrew Trickb6e74712013-09-04 20:59:59 +00001374 if (ShouldTrackPressure) {
1375 // Update top scheduled pressure.
Matthias Braund4f64092016-01-20 00:23:32 +00001376 RegisterOperands RegOpers;
1377 RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
1378 if (ShouldTrackLaneMasks) {
1379 // Adjust liveness and add missing dead+read-undef flags.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001380 SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
Matthias Braund4f64092016-01-20 00:23:32 +00001381 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
1382 } else {
1383 // Adjust for missing dead-def flags.
1384 RegOpers.detectDeadDefs(*MI, *LIS);
1385 }
1386
1387 TopRPTracker.advance(RegOpers);
Andrew Trickb6e74712013-09-04 20:59:59 +00001388 assert(TopRPTracker.getPos() == CurrentTop && "out of sync");
Matthias Braun9198c672015-11-06 20:59:02 +00001389 DEBUG(
1390 dbgs() << "Top Pressure:\n";
1391 dumpRegSetPressure(TopRPTracker.getRegSetPressureAtPos(), TRI);
1392 );
1393
Andrew Trickb248b4a2013-09-06 17:32:47 +00001394 updateScheduledPressure(SU, TopRPTracker.getPressure().MaxSetPressure);
Andrew Trickb6e74712013-09-04 20:59:59 +00001395 }
Matthias Braunb550b762016-04-21 01:54:13 +00001396 } else {
Andrew Trick7a8e1002012-09-11 00:39:15 +00001397 assert(SU->isBottomReady() && "node still has unscheduled dependencies");
1398 MachineBasicBlock::iterator priorII =
1399 priorNonDebug(CurrentBottom, CurrentTop);
1400 if (&*priorII == MI)
1401 CurrentBottom = priorII;
1402 else {
1403 if (&*CurrentTop == MI) {
1404 CurrentTop = nextIfDebug(++CurrentTop, priorII);
1405 TopRPTracker.setPos(CurrentTop);
1406 }
1407 moveInstruction(MI, CurrentBottom);
1408 CurrentBottom = MI;
1409 }
Andrew Trickb6e74712013-09-04 20:59:59 +00001410 if (ShouldTrackPressure) {
Matthias Braund4f64092016-01-20 00:23:32 +00001411 RegisterOperands RegOpers;
1412 RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
1413 if (ShouldTrackLaneMasks) {
1414 // Adjust liveness and add missing dead+read-undef flags.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001415 SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
Matthias Braund4f64092016-01-20 00:23:32 +00001416 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
1417 } else {
1418 // Adjust for missing dead-def flags.
1419 RegOpers.detectDeadDefs(*MI, *LIS);
1420 }
1421
1422 BotRPTracker.recedeSkipDebugValues();
Matthias Braun5d458612016-01-20 00:23:26 +00001423 SmallVector<RegisterMaskPair, 8> LiveUses;
Matthias Braund4f64092016-01-20 00:23:32 +00001424 BotRPTracker.recede(RegOpers, &LiveUses);
Andrew Trickb6e74712013-09-04 20:59:59 +00001425 assert(BotRPTracker.getPos() == CurrentBottom && "out of sync");
Matthias Braun9198c672015-11-06 20:59:02 +00001426 DEBUG(
1427 dbgs() << "Bottom Pressure:\n";
1428 dumpRegSetPressure(BotRPTracker.getRegSetPressureAtPos(), TRI);
1429 );
1430
Andrew Trickb248b4a2013-09-06 17:32:47 +00001431 updateScheduledPressure(SU, BotRPTracker.getPressure().MaxSetPressure);
Andrew Trickb6e74712013-09-04 20:59:59 +00001432 updatePressureDiffs(LiveUses);
Andrew Trickb6e74712013-09-04 20:59:59 +00001433 }
Andrew Trick7a8e1002012-09-11 00:39:15 +00001434 }
1435}
1436
Andrew Trick263280242012-11-12 19:52:20 +00001437//===----------------------------------------------------------------------===//
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001438// BaseMemOpClusterMutation - DAG post-processing to cluster loads or stores.
Andrew Trick263280242012-11-12 19:52:20 +00001439//===----------------------------------------------------------------------===//
1440
Andrew Tricka7714a02012-11-12 19:40:10 +00001441namespace {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001442
Andrew Tricka7714a02012-11-12 19:40:10 +00001443/// \brief Post-process the DAG to create cluster edges between neighboring
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001444/// loads or between neighboring stores.
1445class BaseMemOpClusterMutation : public ScheduleDAGMutation {
1446 struct MemOpInfo {
Andrew Tricka7714a02012-11-12 19:40:10 +00001447 SUnit *SU;
1448 unsigned BaseReg;
Chad Rosierc27a18f2016-03-09 16:00:35 +00001449 int64_t Offset;
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001450
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001451 MemOpInfo(SUnit *su, unsigned reg, int64_t ofs)
1452 : SU(su), BaseReg(reg), Offset(ofs) {}
Benjamin Kramerb0f74b22014-03-07 21:35:39 +00001453
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001454 bool operator<(const MemOpInfo&RHS) const {
Mandeep Singh Grange82678a2016-10-18 00:11:19 +00001455 return std::tie(BaseReg, Offset, SU->NodeNum) <
1456 std::tie(RHS.BaseReg, RHS.Offset, RHS.SU->NodeNum);
Benjamin Kramerb0f74b22014-03-07 21:35:39 +00001457 }
Andrew Tricka7714a02012-11-12 19:40:10 +00001458 };
Andrew Tricka7714a02012-11-12 19:40:10 +00001459
1460 const TargetInstrInfo *TII;
1461 const TargetRegisterInfo *TRI;
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001462 bool IsLoad;
1463
Andrew Tricka7714a02012-11-12 19:40:10 +00001464public:
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001465 BaseMemOpClusterMutation(const TargetInstrInfo *tii,
1466 const TargetRegisterInfo *tri, bool IsLoad)
1467 : TII(tii), TRI(tri), IsLoad(IsLoad) {}
Andrew Tricka7714a02012-11-12 19:40:10 +00001468
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +00001469 void apply(ScheduleDAGInstrs *DAGInstrs) override;
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001470
Andrew Tricka7714a02012-11-12 19:40:10 +00001471protected:
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001472 void clusterNeighboringMemOps(ArrayRef<SUnit *> MemOps, ScheduleDAGMI *DAG);
1473};
1474
1475class StoreClusterMutation : public BaseMemOpClusterMutation {
1476public:
1477 StoreClusterMutation(const TargetInstrInfo *tii,
1478 const TargetRegisterInfo *tri)
1479 : BaseMemOpClusterMutation(tii, tri, false) {}
1480};
1481
1482class LoadClusterMutation : public BaseMemOpClusterMutation {
1483public:
1484 LoadClusterMutation(const TargetInstrInfo *tii, const TargetRegisterInfo *tri)
1485 : BaseMemOpClusterMutation(tii, tri, true) {}
Andrew Tricka7714a02012-11-12 19:40:10 +00001486};
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001487
1488} // end anonymous namespace
Andrew Tricka7714a02012-11-12 19:40:10 +00001489
Tom Stellard68726a52016-08-19 19:59:18 +00001490namespace llvm {
1491
1492std::unique_ptr<ScheduleDAGMutation>
1493createLoadClusterDAGMutation(const TargetInstrInfo *TII,
1494 const TargetRegisterInfo *TRI) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001495 return EnableMemOpCluster ? llvm::make_unique<LoadClusterMutation>(TII, TRI)
Matthias Braun115efcd2016-11-28 20:11:54 +00001496 : nullptr;
Tom Stellard68726a52016-08-19 19:59:18 +00001497}
1498
1499std::unique_ptr<ScheduleDAGMutation>
1500createStoreClusterDAGMutation(const TargetInstrInfo *TII,
1501 const TargetRegisterInfo *TRI) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001502 return EnableMemOpCluster ? llvm::make_unique<StoreClusterMutation>(TII, TRI)
Matthias Braun115efcd2016-11-28 20:11:54 +00001503 : nullptr;
Tom Stellard68726a52016-08-19 19:59:18 +00001504}
1505
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001506} // end namespace llvm
Tom Stellard68726a52016-08-19 19:59:18 +00001507
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001508void BaseMemOpClusterMutation::clusterNeighboringMemOps(
1509 ArrayRef<SUnit *> MemOps, ScheduleDAGMI *DAG) {
1510 SmallVector<MemOpInfo, 32> MemOpRecords;
1511 for (unsigned Idx = 0, End = MemOps.size(); Idx != End; ++Idx) {
1512 SUnit *SU = MemOps[Idx];
Andrew Tricka7714a02012-11-12 19:40:10 +00001513 unsigned BaseReg;
Chad Rosierc27a18f2016-03-09 16:00:35 +00001514 int64_t Offset;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001515 if (TII->getMemOpBaseRegImmOfs(*SU->getInstr(), BaseReg, Offset, TRI))
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001516 MemOpRecords.push_back(MemOpInfo(SU, BaseReg, Offset));
Andrew Tricka7714a02012-11-12 19:40:10 +00001517 }
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001518 if (MemOpRecords.size() < 2)
Andrew Tricka7714a02012-11-12 19:40:10 +00001519 return;
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001520
1521 std::sort(MemOpRecords.begin(), MemOpRecords.end());
Andrew Tricka7714a02012-11-12 19:40:10 +00001522 unsigned ClusterLength = 1;
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001523 for (unsigned Idx = 0, End = MemOpRecords.size(); Idx < (End - 1); ++Idx) {
1524 if (MemOpRecords[Idx].BaseReg != MemOpRecords[Idx+1].BaseReg) {
Andrew Tricka7714a02012-11-12 19:40:10 +00001525 ClusterLength = 1;
1526 continue;
1527 }
1528
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001529 SUnit *SUa = MemOpRecords[Idx].SU;
1530 SUnit *SUb = MemOpRecords[Idx+1].SU;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001531 if (TII->shouldClusterMemOps(*SUa->getInstr(), *SUb->getInstr(),
1532 ClusterLength) &&
1533 DAG->addEdge(SUb, SDep(SUa, SDep::Cluster))) {
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001534 DEBUG(dbgs() << "Cluster ld/st SU(" << SUa->NodeNum << ") - SU("
Andrew Tricka7714a02012-11-12 19:40:10 +00001535 << SUb->NodeNum << ")\n");
1536 // Copy successor edges from SUa to SUb. Interleaving computation
1537 // dependent on SUa can prevent load combining due to register reuse.
1538 // Predecessor edges do not need to be copied from SUb to SUa since nearby
1539 // loads should have effectively the same inputs.
1540 for (SUnit::const_succ_iterator
1541 SI = SUa->Succs.begin(), SE = SUa->Succs.end(); SI != SE; ++SI) {
1542 if (SI->getSUnit() == SUb)
1543 continue;
1544 DEBUG(dbgs() << " Copy Succ SU(" << SI->getSUnit()->NodeNum << ")\n");
1545 DAG->addEdge(SI->getSUnit(), SDep(SUb, SDep::Artificial));
1546 }
1547 ++ClusterLength;
Matthias Braunb550b762016-04-21 01:54:13 +00001548 } else
Andrew Tricka7714a02012-11-12 19:40:10 +00001549 ClusterLength = 1;
1550 }
1551}
1552
1553/// \brief Callback from DAG postProcessing to create cluster edges for loads.
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001554void BaseMemOpClusterMutation::apply(ScheduleDAGInstrs *DAGInstrs) {
1555
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +00001556 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
1557
Andrew Tricka7714a02012-11-12 19:40:10 +00001558 // Map DAG NodeNum to store chain ID.
1559 DenseMap<unsigned, unsigned> StoreChainIDs;
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001560 // Map each store chain to a set of dependent MemOps.
Andrew Tricka7714a02012-11-12 19:40:10 +00001561 SmallVector<SmallVector<SUnit*,4>, 32> StoreChainDependents;
1562 for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) {
1563 SUnit *SU = &DAG->SUnits[Idx];
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001564 if ((IsLoad && !SU->getInstr()->mayLoad()) ||
1565 (!IsLoad && !SU->getInstr()->mayStore()))
Andrew Tricka7714a02012-11-12 19:40:10 +00001566 continue;
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001567
Andrew Tricka7714a02012-11-12 19:40:10 +00001568 unsigned ChainPredID = DAG->SUnits.size();
1569 for (SUnit::const_pred_iterator
1570 PI = SU->Preds.begin(), PE = SU->Preds.end(); PI != PE; ++PI) {
1571 if (PI->isCtrl()) {
1572 ChainPredID = PI->getSUnit()->NodeNum;
1573 break;
1574 }
1575 }
1576 // Check if this chain-like pred has been seen
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001577 // before. ChainPredID==MaxNodeID at the top of the schedule.
Andrew Tricka7714a02012-11-12 19:40:10 +00001578 unsigned NumChains = StoreChainDependents.size();
1579 std::pair<DenseMap<unsigned, unsigned>::iterator, bool> Result =
1580 StoreChainIDs.insert(std::make_pair(ChainPredID, NumChains));
1581 if (Result.second)
1582 StoreChainDependents.resize(NumChains + 1);
1583 StoreChainDependents[Result.first->second].push_back(SU);
1584 }
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001585
Andrew Tricka7714a02012-11-12 19:40:10 +00001586 // Iterate over the store chains.
1587 for (unsigned Idx = 0, End = StoreChainDependents.size(); Idx != End; ++Idx)
Jun Bum Lim4c5bd582016-04-15 14:58:38 +00001588 clusterNeighboringMemOps(StoreChainDependents[Idx], DAG);
Andrew Tricka7714a02012-11-12 19:40:10 +00001589}
1590
Andrew Trick02a80da2012-03-08 01:41:12 +00001591//===----------------------------------------------------------------------===//
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001592// CopyConstrain - DAG post-processing to encourage copy elimination.
1593//===----------------------------------------------------------------------===//
1594
1595namespace {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001596
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001597/// \brief Post-process the DAG to create weak edges from all uses of a copy to
1598/// the one use that defines the copy's source vreg, most likely an induction
1599/// variable increment.
1600class CopyConstrain : public ScheduleDAGMutation {
1601 // Transient state.
1602 SlotIndex RegionBeginIdx;
Andrew Trick2e875172013-04-24 23:19:56 +00001603 // RegionEndIdx is the slot index of the last non-debug instruction in the
1604 // scheduling region. So we may have RegionBeginIdx == RegionEndIdx.
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001605 SlotIndex RegionEndIdx;
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001606
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001607public:
1608 CopyConstrain(const TargetInstrInfo *, const TargetRegisterInfo *) {}
1609
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +00001610 void apply(ScheduleDAGInstrs *DAGInstrs) override;
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001611
1612protected:
Andrew Trickd7f890e2013-12-28 21:56:47 +00001613 void constrainLocalCopy(SUnit *CopySU, ScheduleDAGMILive *DAG);
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001614};
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001615
1616} // end anonymous namespace
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001617
Tom Stellard68726a52016-08-19 19:59:18 +00001618namespace llvm {
1619
1620std::unique_ptr<ScheduleDAGMutation>
1621createCopyConstrainDAGMutation(const TargetInstrInfo *TII,
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001622 const TargetRegisterInfo *TRI) {
1623 return llvm::make_unique<CopyConstrain>(TII, TRI);
Tom Stellard68726a52016-08-19 19:59:18 +00001624}
1625
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001626} // end namespace llvm
Tom Stellard68726a52016-08-19 19:59:18 +00001627
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001628/// constrainLocalCopy handles two possibilities:
1629/// 1) Local src:
1630/// I0: = dst
1631/// I1: src = ...
1632/// I2: = dst
1633/// I3: dst = src (copy)
1634/// (create pred->succ edges I0->I1, I2->I1)
1635///
1636/// 2) Local copy:
1637/// I0: dst = src (copy)
1638/// I1: = dst
1639/// I2: src = ...
1640/// I3: = dst
1641/// (create pred->succ edges I1->I2, I3->I2)
1642///
1643/// Although the MachineScheduler is currently constrained to single blocks,
1644/// this algorithm should handle extended blocks. An EBB is a set of
1645/// contiguously numbered blocks such that the previous block in the EBB is
1646/// always the single predecessor.
Andrew Trickd7f890e2013-12-28 21:56:47 +00001647void CopyConstrain::constrainLocalCopy(SUnit *CopySU, ScheduleDAGMILive *DAG) {
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001648 LiveIntervals *LIS = DAG->getLIS();
1649 MachineInstr *Copy = CopySU->getInstr();
1650
1651 // Check for pure vreg copies.
Matthias Braun7511abd2016-04-04 21:23:46 +00001652 const MachineOperand &SrcOp = Copy->getOperand(1);
1653 unsigned SrcReg = SrcOp.getReg();
1654 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) || !SrcOp.readsReg())
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001655 return;
1656
Matthias Braun7511abd2016-04-04 21:23:46 +00001657 const MachineOperand &DstOp = Copy->getOperand(0);
1658 unsigned DstReg = DstOp.getReg();
1659 if (!TargetRegisterInfo::isVirtualRegister(DstReg) || DstOp.isDead())
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001660 return;
1661
1662 // Check if either the dest or source is local. If it's live across a back
1663 // edge, it's not local. Note that if both vregs are live across the back
1664 // edge, we cannot successfully contrain the copy without cyclic scheduling.
Michael Kuperstein54c61ed2015-01-19 07:30:47 +00001665 // If both the copy's source and dest are local live intervals, then we
1666 // should treat the dest as the global for the purpose of adding
1667 // constraints. This adds edges from source's other uses to the copy.
1668 unsigned LocalReg = SrcReg;
1669 unsigned GlobalReg = DstReg;
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001670 LiveInterval *LocalLI = &LIS->getInterval(LocalReg);
1671 if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx)) {
Michael Kuperstein54c61ed2015-01-19 07:30:47 +00001672 LocalReg = DstReg;
1673 GlobalReg = SrcReg;
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001674 LocalLI = &LIS->getInterval(LocalReg);
1675 if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx))
1676 return;
1677 }
1678 LiveInterval *GlobalLI = &LIS->getInterval(GlobalReg);
1679
1680 // Find the global segment after the start of the local LI.
1681 LiveInterval::iterator GlobalSegment = GlobalLI->find(LocalLI->beginIndex());
1682 // If GlobalLI does not overlap LocalLI->start, then a copy directly feeds a
1683 // local live range. We could create edges from other global uses to the local
1684 // start, but the coalescer should have already eliminated these cases, so
1685 // don't bother dealing with it.
1686 if (GlobalSegment == GlobalLI->end())
1687 return;
1688
1689 // If GlobalSegment is killed at the LocalLI->start, the call to find()
1690 // returned the next global segment. But if GlobalSegment overlaps with
1691 // LocalLI->start, then advance to the next segement. If a hole in GlobalLI
1692 // exists in LocalLI's vicinity, GlobalSegment will be the end of the hole.
1693 if (GlobalSegment->contains(LocalLI->beginIndex()))
1694 ++GlobalSegment;
1695
1696 if (GlobalSegment == GlobalLI->end())
1697 return;
1698
1699 // Check if GlobalLI contains a hole in the vicinity of LocalLI.
1700 if (GlobalSegment != GlobalLI->begin()) {
1701 // Two address defs have no hole.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001702 if (SlotIndex::isSameInstr(std::prev(GlobalSegment)->end,
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001703 GlobalSegment->start)) {
1704 return;
1705 }
Andrew Trickd9761772013-07-30 19:59:08 +00001706 // If the prior global segment may be defined by the same two-address
1707 // instruction that also defines LocalLI, then can't make a hole here.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001708 if (SlotIndex::isSameInstr(std::prev(GlobalSegment)->start,
Andrew Trickd9761772013-07-30 19:59:08 +00001709 LocalLI->beginIndex())) {
1710 return;
1711 }
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001712 // If GlobalLI has a prior segment, it must be live into the EBB. Otherwise
1713 // it would be a disconnected component in the live range.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001714 assert(std::prev(GlobalSegment)->start < LocalLI->beginIndex() &&
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001715 "Disconnected LRG within the scheduling region.");
1716 }
1717 MachineInstr *GlobalDef = LIS->getInstructionFromIndex(GlobalSegment->start);
1718 if (!GlobalDef)
1719 return;
1720
1721 SUnit *GlobalSU = DAG->getSUnit(GlobalDef);
1722 if (!GlobalSU)
1723 return;
1724
1725 // GlobalDef is the bottom of the GlobalLI hole. Open the hole by
1726 // constraining the uses of the last local def to precede GlobalDef.
1727 SmallVector<SUnit*,8> LocalUses;
1728 const VNInfo *LastLocalVN = LocalLI->getVNInfoBefore(LocalLI->endIndex());
1729 MachineInstr *LastLocalDef = LIS->getInstructionFromIndex(LastLocalVN->def);
1730 SUnit *LastLocalSU = DAG->getSUnit(LastLocalDef);
1731 for (SUnit::const_succ_iterator
1732 I = LastLocalSU->Succs.begin(), E = LastLocalSU->Succs.end();
1733 I != E; ++I) {
1734 if (I->getKind() != SDep::Data || I->getReg() != LocalReg)
1735 continue;
1736 if (I->getSUnit() == GlobalSU)
1737 continue;
1738 if (!DAG->canAddEdge(GlobalSU, I->getSUnit()))
1739 return;
1740 LocalUses.push_back(I->getSUnit());
1741 }
1742 // Open the top of the GlobalLI hole by constraining any earlier global uses
1743 // to precede the start of LocalLI.
1744 SmallVector<SUnit*,8> GlobalUses;
1745 MachineInstr *FirstLocalDef =
1746 LIS->getInstructionFromIndex(LocalLI->beginIndex());
1747 SUnit *FirstLocalSU = DAG->getSUnit(FirstLocalDef);
1748 for (SUnit::const_pred_iterator
1749 I = GlobalSU->Preds.begin(), E = GlobalSU->Preds.end(); I != E; ++I) {
1750 if (I->getKind() != SDep::Anti || I->getReg() != GlobalReg)
1751 continue;
1752 if (I->getSUnit() == FirstLocalSU)
1753 continue;
1754 if (!DAG->canAddEdge(FirstLocalSU, I->getSUnit()))
1755 return;
1756 GlobalUses.push_back(I->getSUnit());
1757 }
1758 DEBUG(dbgs() << "Constraining copy SU(" << CopySU->NodeNum << ")\n");
1759 // Add the weak edges.
1760 for (SmallVectorImpl<SUnit*>::const_iterator
1761 I = LocalUses.begin(), E = LocalUses.end(); I != E; ++I) {
1762 DEBUG(dbgs() << " Local use SU(" << (*I)->NodeNum << ") -> SU("
1763 << GlobalSU->NodeNum << ")\n");
1764 DAG->addEdge(GlobalSU, SDep(*I, SDep::Weak));
1765 }
1766 for (SmallVectorImpl<SUnit*>::const_iterator
1767 I = GlobalUses.begin(), E = GlobalUses.end(); I != E; ++I) {
1768 DEBUG(dbgs() << " Global use SU(" << (*I)->NodeNum << ") -> SU("
1769 << FirstLocalSU->NodeNum << ")\n");
1770 DAG->addEdge(FirstLocalSU, SDep(*I, SDep::Weak));
1771 }
1772}
1773
1774/// \brief Callback from DAG postProcessing to create weak edges to encourage
1775/// copy elimination.
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +00001776void CopyConstrain::apply(ScheduleDAGInstrs *DAGInstrs) {
1777 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
Andrew Trickd7f890e2013-12-28 21:56:47 +00001778 assert(DAG->hasVRegLiveness() && "Expect VRegs with LiveIntervals");
1779
Andrew Trick2e875172013-04-24 23:19:56 +00001780 MachineBasicBlock::iterator FirstPos = nextIfDebug(DAG->begin(), DAG->end());
1781 if (FirstPos == DAG->end())
1782 return;
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001783 RegionBeginIdx = DAG->getLIS()->getInstructionIndex(*FirstPos);
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001784 RegionEndIdx = DAG->getLIS()->getInstructionIndex(
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001785 *priorNonDebug(DAG->end(), DAG->begin()));
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001786
1787 for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) {
1788 SUnit *SU = &DAG->SUnits[Idx];
1789 if (!SU->getInstr()->isCopy())
1790 continue;
1791
Andrew Trickd7f890e2013-12-28 21:56:47 +00001792 constrainLocalCopy(SU, static_cast<ScheduleDAGMILive*>(DAG));
Andrew Trick85a1d4c2013-04-24 15:54:43 +00001793 }
1794}
1795
1796//===----------------------------------------------------------------------===//
Andrew Trickfc127d12013-12-07 05:59:44 +00001797// MachineSchedStrategy helpers used by GenericScheduler, GenericPostScheduler
1798// and possibly other custom schedulers.
Andrew Trickd14d7c22013-12-28 21:56:57 +00001799//===----------------------------------------------------------------------===//
Andrew Tricke1c034f2012-01-17 06:55:03 +00001800
Andrew Trick5a22df42013-12-05 17:56:02 +00001801static const unsigned InvalidCycle = ~0U;
1802
Andrew Trickfc127d12013-12-07 05:59:44 +00001803SchedBoundary::~SchedBoundary() { delete HazardRec; }
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001804
Andrew Trickfc127d12013-12-07 05:59:44 +00001805void SchedBoundary::reset() {
1806 // A new HazardRec is created for each DAG and owned by SchedBoundary.
1807 // Destroying and reconstructing it is very expensive though. So keep
1808 // invalid, placeholder HazardRecs.
1809 if (HazardRec && HazardRec->isEnabled()) {
1810 delete HazardRec;
Craig Topperc0196b12014-04-14 00:51:57 +00001811 HazardRec = nullptr;
Andrew Trickfc127d12013-12-07 05:59:44 +00001812 }
1813 Available.clear();
1814 Pending.clear();
1815 CheckPending = false;
Andrew Trickfc127d12013-12-07 05:59:44 +00001816 CurrCycle = 0;
1817 CurrMOps = 0;
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001818 MinReadyCycle = std::numeric_limits<unsigned>::max();
Andrew Trickfc127d12013-12-07 05:59:44 +00001819 ExpectedLatency = 0;
1820 DependentLatency = 0;
1821 RetiredMOps = 0;
1822 MaxExecutedResCount = 0;
1823 ZoneCritResIdx = 0;
1824 IsResourceLimited = false;
1825 ReservedCycles.clear();
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001826#ifndef NDEBUG
Andrew Trickd14d7c22013-12-28 21:56:57 +00001827 // Track the maximum number of stall cycles that could arise either from the
1828 // latency of a DAG edge or the number of cycles that a processor resource is
1829 // reserved (SchedBoundary::ReservedCycles).
Andrew Trick7f1ebbe2014-06-07 01:48:43 +00001830 MaxObservedStall = 0;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001831#endif
Andrew Trickfc127d12013-12-07 05:59:44 +00001832 // Reserve a zero-count for invalid CritResIdx.
1833 ExecutedResCounts.resize(1);
1834 assert(!ExecutedResCounts[0] && "nonzero count for bad resource");
1835}
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001836
Andrew Trickfc127d12013-12-07 05:59:44 +00001837void SchedRemainder::
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001838init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel) {
1839 reset();
1840 if (!SchedModel->hasInstrSchedModel())
1841 return;
1842 RemainingCounts.resize(SchedModel->getNumProcResourceKinds());
1843 for (std::vector<SUnit>::iterator
1844 I = DAG->SUnits.begin(), E = DAG->SUnits.end(); I != E; ++I) {
1845 const MCSchedClassDesc *SC = DAG->getSchedClass(&*I);
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001846 RemIssueCount += SchedModel->getNumMicroOps(I->getInstr(), SC)
1847 * SchedModel->getMicroOpFactor();
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001848 for (TargetSchedModel::ProcResIter
1849 PI = SchedModel->getWriteProcResBegin(SC),
1850 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
1851 unsigned PIdx = PI->ProcResourceIdx;
1852 unsigned Factor = SchedModel->getResourceFactor(PIdx);
1853 RemainingCounts[PIdx] += (Factor * PI->Cycles);
1854 }
1855 }
1856}
1857
Andrew Trickfc127d12013-12-07 05:59:44 +00001858void SchedBoundary::
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001859init(ScheduleDAGMI *dag, const TargetSchedModel *smodel, SchedRemainder *rem) {
1860 reset();
1861 DAG = dag;
1862 SchedModel = smodel;
1863 Rem = rem;
Andrew Trick5a22df42013-12-05 17:56:02 +00001864 if (SchedModel->hasInstrSchedModel()) {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001865 ExecutedResCounts.resize(SchedModel->getNumProcResourceKinds());
Andrew Trick5a22df42013-12-05 17:56:02 +00001866 ReservedCycles.resize(SchedModel->getNumProcResourceKinds(), InvalidCycle);
1867 }
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001868}
1869
Andrew Trick880e5732013-12-05 17:55:58 +00001870/// Compute the stall cycles based on this SUnit's ready time. Heuristics treat
1871/// these "soft stalls" differently than the hard stall cycles based on CPU
1872/// resources and computed by checkHazard(). A fully in-order model
1873/// (MicroOpBufferSize==0) will not make use of this since instructions are not
1874/// available for scheduling until they are ready. However, a weaker in-order
1875/// model may use this for heuristics. For example, if a processor has in-order
1876/// behavior when reading certain resources, this may come into play.
Andrew Trickfc127d12013-12-07 05:59:44 +00001877unsigned SchedBoundary::getLatencyStallCycles(SUnit *SU) {
Andrew Trick880e5732013-12-05 17:55:58 +00001878 if (!SU->isUnbuffered)
1879 return 0;
1880
1881 unsigned ReadyCycle = (isTop() ? SU->TopReadyCycle : SU->BotReadyCycle);
1882 if (ReadyCycle > CurrCycle)
1883 return ReadyCycle - CurrCycle;
1884 return 0;
1885}
1886
Andrew Trick5a22df42013-12-05 17:56:02 +00001887/// Compute the next cycle at which the given processor resource can be
1888/// scheduled.
Andrew Trickfc127d12013-12-07 05:59:44 +00001889unsigned SchedBoundary::
Andrew Trick5a22df42013-12-05 17:56:02 +00001890getNextResourceCycle(unsigned PIdx, unsigned Cycles) {
1891 unsigned NextUnreserved = ReservedCycles[PIdx];
1892 // If this resource has never been used, always return cycle zero.
1893 if (NextUnreserved == InvalidCycle)
1894 return 0;
1895 // For bottom-up scheduling add the cycles needed for the current operation.
1896 if (!isTop())
1897 NextUnreserved += Cycles;
1898 return NextUnreserved;
1899}
1900
Andrew Trick8c9e6722012-06-29 03:23:24 +00001901/// Does this SU have a hazard within the current instruction group.
1902///
1903/// The scheduler supports two modes of hazard recognition. The first is the
1904/// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that
1905/// supports highly complicated in-order reservation tables
1906/// (ScoreboardHazardRecognizer) and arbitraty target-specific logic.
1907///
1908/// The second is a streamlined mechanism that checks for hazards based on
1909/// simple counters that the scheduler itself maintains. It explicitly checks
1910/// for instruction dispatch limitations, including the number of micro-ops that
1911/// can dispatch per cycle.
1912///
1913/// TODO: Also check whether the SU must start a new group.
Andrew Trickfc127d12013-12-07 05:59:44 +00001914bool SchedBoundary::checkHazard(SUnit *SU) {
Andrew Trickd14d7c22013-12-28 21:56:57 +00001915 if (HazardRec->isEnabled()
1916 && HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard) {
1917 return true;
1918 }
Javed Absar3d594372017-03-27 20:46:37 +00001919
Andrew Trickdd79f0f2012-10-10 05:43:09 +00001920 unsigned uops = SchedModel->getNumMicroOps(SU->getInstr());
Andrew Tricke2ff5752013-06-15 04:49:49 +00001921 if ((CurrMOps > 0) && (CurrMOps + uops > SchedModel->getIssueWidth())) {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001922 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") uops="
1923 << SchedModel->getNumMicroOps(SU->getInstr()) << '\n');
Andrew Trick8c9e6722012-06-29 03:23:24 +00001924 return true;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001925 }
Javed Absar3d594372017-03-27 20:46:37 +00001926
1927 if (CurrMOps > 0 &&
1928 ((isTop() && SchedModel->mustBeginGroup(SU->getInstr())) ||
1929 (!isTop() && SchedModel->mustEndGroup(SU->getInstr())))) {
1930 DEBUG(dbgs() << " hazard: SU(" << SU->NodeNum << ") must "
1931 << (isTop()? "begin" : "end") << " group\n");
1932 return true;
1933 }
1934
Andrew Trick5a22df42013-12-05 17:56:02 +00001935 if (SchedModel->hasInstrSchedModel() && SU->hasReservedResource) {
1936 const MCSchedClassDesc *SC = DAG->getSchedClass(SU);
1937 for (TargetSchedModel::ProcResIter
1938 PI = SchedModel->getWriteProcResBegin(SC),
1939 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
Andrew Trick56327222014-06-27 04:57:05 +00001940 unsigned NRCycle = getNextResourceCycle(PI->ProcResourceIdx, PI->Cycles);
1941 if (NRCycle > CurrCycle) {
Andrew Trick040c0da2014-06-27 05:09:36 +00001942#ifndef NDEBUG
Chad Rosieraba845e2014-07-02 16:46:08 +00001943 MaxObservedStall = std::max(PI->Cycles, MaxObservedStall);
Andrew Trick040c0da2014-06-27 05:09:36 +00001944#endif
Andrew Trick56327222014-06-27 04:57:05 +00001945 DEBUG(dbgs() << " SU(" << SU->NodeNum << ") "
1946 << SchedModel->getResourceName(PI->ProcResourceIdx)
1947 << "=" << NRCycle << "c\n");
Andrew Trick5a22df42013-12-05 17:56:02 +00001948 return true;
Andrew Trick56327222014-06-27 04:57:05 +00001949 }
Andrew Trick5a22df42013-12-05 17:56:02 +00001950 }
1951 }
Andrew Trick8c9e6722012-06-29 03:23:24 +00001952 return false;
1953}
1954
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001955// Find the unscheduled node in ReadySUs with the highest latency.
Andrew Trickfc127d12013-12-07 05:59:44 +00001956unsigned SchedBoundary::
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001957findMaxLatency(ArrayRef<SUnit*> ReadySUs) {
Craig Topperc0196b12014-04-14 00:51:57 +00001958 SUnit *LateSU = nullptr;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001959 unsigned RemLatency = 0;
1960 for (ArrayRef<SUnit*>::iterator I = ReadySUs.begin(), E = ReadySUs.end();
Andrew Trickd6d5ad32012-12-18 20:52:56 +00001961 I != E; ++I) {
1962 unsigned L = getUnscheduledLatency(*I);
Andrew Trickf5b8ef22013-06-15 04:49:44 +00001963 if (L > RemLatency) {
Andrew Trickd6d5ad32012-12-18 20:52:56 +00001964 RemLatency = L;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001965 LateSU = *I;
Andrew Trickf5b8ef22013-06-15 04:49:44 +00001966 }
Andrew Trickd6d5ad32012-12-18 20:52:56 +00001967 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001968 if (LateSU) {
1969 DEBUG(dbgs() << Available.getName() << " RemLatency SU("
1970 << LateSU->NodeNum << ") " << RemLatency << "c\n");
Andrew Trickd6d5ad32012-12-18 20:52:56 +00001971 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001972 return RemLatency;
1973}
Andrew Trickf5b8ef22013-06-15 04:49:44 +00001974
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001975// Count resources in this zone and the remaining unscheduled
1976// instruction. Return the max count, scaled. Set OtherCritIdx to the critical
1977// resource index, or zero if the zone is issue limited.
Andrew Trickfc127d12013-12-07 05:59:44 +00001978unsigned SchedBoundary::
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001979getOtherResourceCount(unsigned &OtherCritIdx) {
Alexey Samsonov64c391d2013-07-19 08:55:18 +00001980 OtherCritIdx = 0;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001981 if (!SchedModel->hasInstrSchedModel())
1982 return 0;
1983
1984 unsigned OtherCritCount = Rem->RemIssueCount
1985 + (RetiredMOps * SchedModel->getMicroOpFactor());
1986 DEBUG(dbgs() << " " << Available.getName() << " + Remain MOps: "
1987 << OtherCritCount / SchedModel->getMicroOpFactor() << '\n');
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001988 for (unsigned PIdx = 1, PEnd = SchedModel->getNumProcResourceKinds();
1989 PIdx != PEnd; ++PIdx) {
1990 unsigned OtherCount = getResourceCount(PIdx) + Rem->RemainingCounts[PIdx];
1991 if (OtherCount > OtherCritCount) {
1992 OtherCritCount = OtherCount;
1993 OtherCritIdx = PIdx;
1994 }
Andrew Trick3ca33ac2012-11-07 07:05:09 +00001995 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00001996 if (OtherCritIdx) {
1997 DEBUG(dbgs() << " " << Available.getName() << " + Remain CritRes: "
1998 << OtherCritCount / SchedModel->getResourceFactor(OtherCritIdx)
Andrew Trickfc127d12013-12-07 05:59:44 +00001999 << " " << SchedModel->getResourceName(OtherCritIdx) << "\n");
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002000 }
2001 return OtherCritCount;
2002}
2003
Andrew Trickfc127d12013-12-07 05:59:44 +00002004void SchedBoundary::releaseNode(SUnit *SU, unsigned ReadyCycle) {
Andrew Trick7f1ebbe2014-06-07 01:48:43 +00002005 assert(SU->getInstr() && "Scheduled SUnit must have instr");
2006
2007#ifndef NDEBUG
Andrew Trick491e34a2014-06-12 22:36:28 +00002008 // ReadyCycle was been bumped up to the CurrCycle when this node was
2009 // scheduled, but CurrCycle may have been eagerly advanced immediately after
2010 // scheduling, so may now be greater than ReadyCycle.
2011 if (ReadyCycle > CurrCycle)
2012 MaxObservedStall = std::max(ReadyCycle - CurrCycle, MaxObservedStall);
Andrew Trick7f1ebbe2014-06-07 01:48:43 +00002013#endif
2014
Andrew Trick61f1a272012-05-24 22:11:09 +00002015 if (ReadyCycle < MinReadyCycle)
2016 MinReadyCycle = ReadyCycle;
2017
2018 // Check for interlocks first. For the purpose of other heuristics, an
2019 // instruction that cannot issue appears as if it's not in the ReadyQueue.
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002020 bool IsBuffered = SchedModel->getMicroOpBufferSize() != 0;
Matthias Braun6493bc22016-04-22 19:09:17 +00002021 if ((!IsBuffered && ReadyCycle > CurrCycle) || checkHazard(SU) ||
2022 Available.size() >= ReadyListLimit)
Andrew Trick61f1a272012-05-24 22:11:09 +00002023 Pending.push(SU);
2024 else
2025 Available.push(SU);
2026}
2027
2028/// Move the boundary of scheduled code by one cycle.
Andrew Trickfc127d12013-12-07 05:59:44 +00002029void SchedBoundary::bumpCycle(unsigned NextCycle) {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002030 if (SchedModel->getMicroOpBufferSize() == 0) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00002031 assert(MinReadyCycle < std::numeric_limits<unsigned>::max() &&
2032 "MinReadyCycle uninitialized");
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002033 if (MinReadyCycle > NextCycle)
2034 NextCycle = MinReadyCycle;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002035 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002036 // Update the current micro-ops, which will issue in the next cycle.
2037 unsigned DecMOps = SchedModel->getIssueWidth() * (NextCycle - CurrCycle);
2038 CurrMOps = (CurrMOps <= DecMOps) ? 0 : CurrMOps - DecMOps;
2039
2040 // Decrement DependentLatency based on the next cycle.
Andrew Trickf5b8ef22013-06-15 04:49:44 +00002041 if ((NextCycle - CurrCycle) > DependentLatency)
2042 DependentLatency = 0;
2043 else
2044 DependentLatency -= (NextCycle - CurrCycle);
Andrew Trick61f1a272012-05-24 22:11:09 +00002045
2046 if (!HazardRec->isEnabled()) {
Andrew Trick45446062012-06-05 21:11:27 +00002047 // Bypass HazardRec virtual calls.
Andrew Trick61f1a272012-05-24 22:11:09 +00002048 CurrCycle = NextCycle;
Matthias Braunb550b762016-04-21 01:54:13 +00002049 } else {
Andrew Trick45446062012-06-05 21:11:27 +00002050 // Bypass getHazardType calls in case of long latency.
Andrew Trick61f1a272012-05-24 22:11:09 +00002051 for (; CurrCycle != NextCycle; ++CurrCycle) {
2052 if (isTop())
2053 HazardRec->AdvanceCycle();
2054 else
2055 HazardRec->RecedeCycle();
2056 }
2057 }
2058 CheckPending = true;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002059 unsigned LFactor = SchedModel->getLatencyFactor();
2060 IsResourceLimited =
2061 (int)(getCriticalCount() - (getScheduledLatency() * LFactor))
2062 > (int)LFactor;
Andrew Trick61f1a272012-05-24 22:11:09 +00002063
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002064 DEBUG(dbgs() << "Cycle: " << CurrCycle << ' ' << Available.getName() << '\n');
2065}
2066
Andrew Trickfc127d12013-12-07 05:59:44 +00002067void SchedBoundary::incExecutedResources(unsigned PIdx, unsigned Count) {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002068 ExecutedResCounts[PIdx] += Count;
2069 if (ExecutedResCounts[PIdx] > MaxExecutedResCount)
2070 MaxExecutedResCount = ExecutedResCounts[PIdx];
Andrew Trick61f1a272012-05-24 22:11:09 +00002071}
2072
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002073/// Add the given processor resource to this scheduled zone.
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002074///
2075/// \param Cycles indicates the number of consecutive (non-pipelined) cycles
2076/// during which this resource is consumed.
2077///
2078/// \return the next cycle at which the instruction may execute without
2079/// oversubscribing resources.
Andrew Trickfc127d12013-12-07 05:59:44 +00002080unsigned SchedBoundary::
Andrew Trick5a22df42013-12-05 17:56:02 +00002081countResource(unsigned PIdx, unsigned Cycles, unsigned NextCycle) {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002082 unsigned Factor = SchedModel->getResourceFactor(PIdx);
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002083 unsigned Count = Factor * Cycles;
Andrew Trickfc127d12013-12-07 05:59:44 +00002084 DEBUG(dbgs() << " " << SchedModel->getResourceName(PIdx)
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002085 << " +" << Cycles << "x" << Factor << "u\n");
2086
2087 // Update Executed resources counts.
2088 incExecutedResources(PIdx, Count);
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002089 assert(Rem->RemainingCounts[PIdx] >= Count && "resource double counted");
2090 Rem->RemainingCounts[PIdx] -= Count;
2091
Andrew Trickb13ef172013-07-19 00:20:07 +00002092 // Check if this resource exceeds the current critical resource. If so, it
2093 // becomes the critical resource.
2094 if (ZoneCritResIdx != PIdx && (getResourceCount(PIdx) > getCriticalCount())) {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002095 ZoneCritResIdx = PIdx;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002096 DEBUG(dbgs() << " *** Critical resource "
Andrew Trickfc127d12013-12-07 05:59:44 +00002097 << SchedModel->getResourceName(PIdx) << ": "
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002098 << getResourceCount(PIdx) / SchedModel->getLatencyFactor() << "c\n");
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002099 }
Andrew Trick5a22df42013-12-05 17:56:02 +00002100 // For reserved resources, record the highest cycle using the resource.
2101 unsigned NextAvailable = getNextResourceCycle(PIdx, Cycles);
2102 if (NextAvailable > CurrCycle) {
2103 DEBUG(dbgs() << " Resource conflict: "
2104 << SchedModel->getProcResource(PIdx)->Name << " reserved until @"
2105 << NextAvailable << "\n");
2106 }
2107 return NextAvailable;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002108}
2109
Andrew Trick45446062012-06-05 21:11:27 +00002110/// Move the boundary of scheduled code by one SUnit.
Andrew Trickfc127d12013-12-07 05:59:44 +00002111void SchedBoundary::bumpNode(SUnit *SU) {
Andrew Trick45446062012-06-05 21:11:27 +00002112 // Update the reservation table.
2113 if (HazardRec->isEnabled()) {
2114 if (!isTop() && SU->isCall) {
2115 // Calls are scheduled with their preceding instructions. For bottom-up
2116 // scheduling, clear the pipeline state before emitting.
2117 HazardRec->Reset();
2118 }
2119 HazardRec->EmitInstruction(SU);
2120 }
Andrew Trick5a22df42013-12-05 17:56:02 +00002121 // checkHazard should prevent scheduling multiple instructions per cycle that
2122 // exceed the issue width.
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002123 const MCSchedClassDesc *SC = DAG->getSchedClass(SU);
2124 unsigned IncMOps = SchedModel->getNumMicroOps(SU->getInstr());
Daniel Jasper0d92abd2013-12-06 08:58:22 +00002125 assert(
2126 (CurrMOps == 0 || (CurrMOps + IncMOps) <= SchedModel->getIssueWidth()) &&
Andrew Trickf7760a22013-12-06 17:19:20 +00002127 "Cannot schedule this instruction's MicroOps in the current cycle.");
Andrew Trick5a22df42013-12-05 17:56:02 +00002128
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002129 unsigned ReadyCycle = (isTop() ? SU->TopReadyCycle : SU->BotReadyCycle);
2130 DEBUG(dbgs() << " Ready @" << ReadyCycle << "c\n");
2131
Andrew Trick5a22df42013-12-05 17:56:02 +00002132 unsigned NextCycle = CurrCycle;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002133 switch (SchedModel->getMicroOpBufferSize()) {
2134 case 0:
2135 assert(ReadyCycle <= CurrCycle && "Broken PendingQueue");
2136 break;
2137 case 1:
2138 if (ReadyCycle > NextCycle) {
2139 NextCycle = ReadyCycle;
2140 DEBUG(dbgs() << " *** Stall until: " << ReadyCycle << "\n");
2141 }
2142 break;
2143 default:
2144 // We don't currently model the OOO reorder buffer, so consider all
Andrew Trick880e5732013-12-05 17:55:58 +00002145 // scheduled MOps to be "retired". We do loosely model in-order resource
2146 // latency. If this instruction uses an in-order resource, account for any
2147 // likely stall cycles.
2148 if (SU->isUnbuffered && ReadyCycle > NextCycle)
2149 NextCycle = ReadyCycle;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002150 break;
2151 }
2152 RetiredMOps += IncMOps;
2153
2154 // Update resource counts and critical resource.
2155 if (SchedModel->hasInstrSchedModel()) {
2156 unsigned DecRemIssue = IncMOps * SchedModel->getMicroOpFactor();
2157 assert(Rem->RemIssueCount >= DecRemIssue && "MOps double counted");
2158 Rem->RemIssueCount -= DecRemIssue;
2159 if (ZoneCritResIdx) {
2160 // Scale scheduled micro-ops for comparing with the critical resource.
2161 unsigned ScaledMOps =
2162 RetiredMOps * SchedModel->getMicroOpFactor();
2163
2164 // If scaled micro-ops are now more than the previous critical resource by
2165 // a full cycle, then micro-ops issue becomes critical.
2166 if ((int)(ScaledMOps - getResourceCount(ZoneCritResIdx))
2167 >= (int)SchedModel->getLatencyFactor()) {
2168 ZoneCritResIdx = 0;
2169 DEBUG(dbgs() << " *** Critical resource NumMicroOps: "
2170 << ScaledMOps / SchedModel->getLatencyFactor() << "c\n");
2171 }
2172 }
2173 for (TargetSchedModel::ProcResIter
2174 PI = SchedModel->getWriteProcResBegin(SC),
2175 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
2176 unsigned RCycle =
Andrew Trick5a22df42013-12-05 17:56:02 +00002177 countResource(PI->ProcResourceIdx, PI->Cycles, NextCycle);
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002178 if (RCycle > NextCycle)
2179 NextCycle = RCycle;
2180 }
Andrew Trick5a22df42013-12-05 17:56:02 +00002181 if (SU->hasReservedResource) {
2182 // For reserved resources, record the highest cycle using the resource.
2183 // For top-down scheduling, this is the cycle in which we schedule this
2184 // instruction plus the number of cycles the operations reserves the
2185 // resource. For bottom-up is it simply the instruction's cycle.
2186 for (TargetSchedModel::ProcResIter
2187 PI = SchedModel->getWriteProcResBegin(SC),
2188 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
2189 unsigned PIdx = PI->ProcResourceIdx;
Andrew Trickd14d7c22013-12-28 21:56:57 +00002190 if (SchedModel->getProcResource(PIdx)->BufferSize == 0) {
Chad Rosieraba845e2014-07-02 16:46:08 +00002191 if (isTop()) {
2192 ReservedCycles[PIdx] =
2193 std::max(getNextResourceCycle(PIdx, 0), NextCycle + PI->Cycles);
2194 }
2195 else
2196 ReservedCycles[PIdx] = NextCycle;
Andrew Trickd14d7c22013-12-28 21:56:57 +00002197 }
Andrew Trick5a22df42013-12-05 17:56:02 +00002198 }
2199 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002200 }
2201 // Update ExpectedLatency and DependentLatency.
2202 unsigned &TopLatency = isTop() ? ExpectedLatency : DependentLatency;
2203 unsigned &BotLatency = isTop() ? DependentLatency : ExpectedLatency;
2204 if (SU->getDepth() > TopLatency) {
2205 TopLatency = SU->getDepth();
2206 DEBUG(dbgs() << " " << Available.getName()
2207 << " TopLatency SU(" << SU->NodeNum << ") " << TopLatency << "c\n");
2208 }
2209 if (SU->getHeight() > BotLatency) {
2210 BotLatency = SU->getHeight();
2211 DEBUG(dbgs() << " " << Available.getName()
2212 << " BotLatency SU(" << SU->NodeNum << ") " << BotLatency << "c\n");
2213 }
2214 // If we stall for any reason, bump the cycle.
2215 if (NextCycle > CurrCycle) {
2216 bumpCycle(NextCycle);
Matthias Braunb550b762016-04-21 01:54:13 +00002217 } else {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002218 // After updating ZoneCritResIdx and ExpectedLatency, check if we're
Alp Tokercb402912014-01-24 17:20:08 +00002219 // resource limited. If a stall occurred, bumpCycle does this.
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002220 unsigned LFactor = SchedModel->getLatencyFactor();
2221 IsResourceLimited =
2222 (int)(getCriticalCount() - (getScheduledLatency() * LFactor))
2223 > (int)LFactor;
2224 }
Andrew Trick5a22df42013-12-05 17:56:02 +00002225 // Update CurrMOps after calling bumpCycle to handle stalls, since bumpCycle
2226 // resets CurrMOps. Loop to handle instructions with more MOps than issue in
2227 // one cycle. Since we commonly reach the max MOps here, opportunistically
2228 // bump the cycle to avoid uselessly checking everything in the readyQ.
2229 CurrMOps += IncMOps;
Javed Absar3d594372017-03-27 20:46:37 +00002230
2231 // Bump the cycle count for issue group constraints.
2232 // This must be done after NextCycle has been adjust for all other stalls.
2233 // Calling bumpCycle(X) will reduce CurrMOps by one issue group and set
2234 // currCycle to X.
2235 if ((isTop() && SchedModel->mustEndGroup(SU->getInstr())) ||
2236 (!isTop() && SchedModel->mustBeginGroup(SU->getInstr()))) {
2237 DEBUG(dbgs() << " Bump cycle to "
2238 << (isTop() ? "end" : "begin") << " group\n");
2239 bumpCycle(++NextCycle);
2240 }
2241
Andrew Trick5a22df42013-12-05 17:56:02 +00002242 while (CurrMOps >= SchedModel->getIssueWidth()) {
Andrew Trick5a22df42013-12-05 17:56:02 +00002243 DEBUG(dbgs() << " *** Max MOps " << CurrMOps
2244 << " at cycle " << CurrCycle << '\n');
Andrew Trickd14d7c22013-12-28 21:56:57 +00002245 bumpCycle(++NextCycle);
Andrew Trick5a22df42013-12-05 17:56:02 +00002246 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002247 DEBUG(dumpScheduledState());
Andrew Trick45446062012-06-05 21:11:27 +00002248}
2249
Andrew Trick61f1a272012-05-24 22:11:09 +00002250/// Release pending ready nodes in to the available queue. This makes them
2251/// visible to heuristics.
Andrew Trickfc127d12013-12-07 05:59:44 +00002252void SchedBoundary::releasePending() {
Andrew Trick61f1a272012-05-24 22:11:09 +00002253 // If the available queue is empty, it is safe to reset MinReadyCycle.
2254 if (Available.empty())
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00002255 MinReadyCycle = std::numeric_limits<unsigned>::max();
Andrew Trick61f1a272012-05-24 22:11:09 +00002256
2257 // Check to see if any of the pending instructions are ready to issue. If
2258 // so, add them to the available queue.
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002259 bool IsBuffered = SchedModel->getMicroOpBufferSize() != 0;
Andrew Trick61f1a272012-05-24 22:11:09 +00002260 for (unsigned i = 0, e = Pending.size(); i != e; ++i) {
2261 SUnit *SU = *(Pending.begin()+i);
Andrew Trick45446062012-06-05 21:11:27 +00002262 unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle;
Andrew Trick61f1a272012-05-24 22:11:09 +00002263
2264 if (ReadyCycle < MinReadyCycle)
2265 MinReadyCycle = ReadyCycle;
2266
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002267 if (!IsBuffered && ReadyCycle > CurrCycle)
Andrew Trick61f1a272012-05-24 22:11:09 +00002268 continue;
2269
Andrew Trick8c9e6722012-06-29 03:23:24 +00002270 if (checkHazard(SU))
Andrew Trick61f1a272012-05-24 22:11:09 +00002271 continue;
2272
Matthias Braun6493bc22016-04-22 19:09:17 +00002273 if (Available.size() >= ReadyListLimit)
2274 break;
2275
Andrew Trick61f1a272012-05-24 22:11:09 +00002276 Available.push(SU);
2277 Pending.remove(Pending.begin()+i);
2278 --i; --e;
2279 }
2280 CheckPending = false;
2281}
2282
2283/// Remove SU from the ready set for this boundary.
Andrew Trickfc127d12013-12-07 05:59:44 +00002284void SchedBoundary::removeReady(SUnit *SU) {
Andrew Trick61f1a272012-05-24 22:11:09 +00002285 if (Available.isInQueue(SU))
2286 Available.remove(Available.find(SU));
2287 else {
2288 assert(Pending.isInQueue(SU) && "bad ready count");
2289 Pending.remove(Pending.find(SU));
2290 }
2291}
2292
2293/// If this queue only has one ready candidate, return it. As a side effect,
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002294/// defer any nodes that now hit a hazard, and advance the cycle until at least
2295/// one node is ready. If multiple instructions are ready, return NULL.
Andrew Trickfc127d12013-12-07 05:59:44 +00002296SUnit *SchedBoundary::pickOnlyChoice() {
Andrew Trick61f1a272012-05-24 22:11:09 +00002297 if (CheckPending)
2298 releasePending();
2299
Andrew Tricke2ff5752013-06-15 04:49:49 +00002300 if (CurrMOps > 0) {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002301 // Defer any ready instrs that now have a hazard.
2302 for (ReadyQueue::iterator I = Available.begin(); I != Available.end();) {
2303 if (checkHazard(*I)) {
2304 Pending.push(*I);
2305 I = Available.remove(I);
2306 continue;
2307 }
2308 ++I;
2309 }
2310 }
Andrew Trick61f1a272012-05-24 22:11:09 +00002311 for (unsigned i = 0; Available.empty(); ++i) {
Chad Rosieraba845e2014-07-02 16:46:08 +00002312// FIXME: Re-enable assert once PR20057 is resolved.
2313// assert(i <= (HazardRec->getMaxLookAhead() + MaxObservedStall) &&
2314// "permanent hazard");
2315 (void)i;
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002316 bumpCycle(CurrCycle + 1);
Andrew Trick61f1a272012-05-24 22:11:09 +00002317 releasePending();
2318 }
Matthias Braund29d31e2016-06-23 21:27:38 +00002319
2320 DEBUG(Pending.dump());
2321 DEBUG(Available.dump());
2322
Andrew Trick61f1a272012-05-24 22:11:09 +00002323 if (Available.size() == 1)
2324 return *Available.begin();
Craig Topperc0196b12014-04-14 00:51:57 +00002325 return nullptr;
Andrew Trick61f1a272012-05-24 22:11:09 +00002326}
2327
Matthias Braun8c209aa2017-01-28 02:02:38 +00002328#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002329// This is useful information to dump after bumpNode.
2330// Note that the Queue contents are more useful before pickNodeFromQueue.
Matthias Braun8c209aa2017-01-28 02:02:38 +00002331LLVM_DUMP_METHOD void SchedBoundary::dumpScheduledState() {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002332 unsigned ResFactor;
2333 unsigned ResCount;
2334 if (ZoneCritResIdx) {
2335 ResFactor = SchedModel->getResourceFactor(ZoneCritResIdx);
2336 ResCount = getResourceCount(ZoneCritResIdx);
Matthias Braunb550b762016-04-21 01:54:13 +00002337 } else {
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002338 ResFactor = SchedModel->getMicroOpFactor();
2339 ResCount = RetiredMOps * SchedModel->getMicroOpFactor();
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002340 }
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002341 unsigned LFactor = SchedModel->getLatencyFactor();
2342 dbgs() << Available.getName() << " @" << CurrCycle << "c\n"
2343 << " Retired: " << RetiredMOps;
2344 dbgs() << "\n Executed: " << getExecutedCount() / LFactor << "c";
2345 dbgs() << "\n Critical: " << ResCount / LFactor << "c, "
Andrew Trickfc127d12013-12-07 05:59:44 +00002346 << ResCount / ResFactor << " "
2347 << SchedModel->getResourceName(ZoneCritResIdx)
Andrew Trickf78e7fa2013-06-15 05:39:19 +00002348 << "\n ExpectedLatency: " << ExpectedLatency << "c\n"
2349 << (IsResourceLimited ? " - Resource" : " - Latency")
2350 << " limited.\n";
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002351}
Andrew Trick8e8415f2013-06-15 05:46:47 +00002352#endif
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002353
Andrew Trickfc127d12013-12-07 05:59:44 +00002354//===----------------------------------------------------------------------===//
Andrew Trickd14d7c22013-12-28 21:56:57 +00002355// GenericScheduler - Generic implementation of MachineSchedStrategy.
Andrew Trickfc127d12013-12-07 05:59:44 +00002356//===----------------------------------------------------------------------===//
2357
Andrew Trickd14d7c22013-12-28 21:56:57 +00002358void GenericSchedulerBase::SchedCandidate::
2359initResourceDelta(const ScheduleDAGMI *DAG,
2360 const TargetSchedModel *SchedModel) {
2361 if (!Policy.ReduceResIdx && !Policy.DemandResIdx)
2362 return;
2363
2364 const MCSchedClassDesc *SC = DAG->getSchedClass(SU);
2365 for (TargetSchedModel::ProcResIter
2366 PI = SchedModel->getWriteProcResBegin(SC),
2367 PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
2368 if (PI->ProcResourceIdx == Policy.ReduceResIdx)
2369 ResDelta.CritResources += PI->Cycles;
2370 if (PI->ProcResourceIdx == Policy.DemandResIdx)
2371 ResDelta.DemandedResources += PI->Cycles;
2372 }
2373}
2374
2375/// Set the CandPolicy given a scheduling zone given the current resources and
2376/// latencies inside and outside the zone.
Matthias Braunb550b762016-04-21 01:54:13 +00002377void GenericSchedulerBase::setPolicy(CandPolicy &Policy, bool IsPostRA,
Andrew Trickd14d7c22013-12-28 21:56:57 +00002378 SchedBoundary &CurrZone,
2379 SchedBoundary *OtherZone) {
Eric Christopher572e03a2015-06-19 01:53:21 +00002380 // Apply preemptive heuristics based on the total latency and resources
Andrew Trickd14d7c22013-12-28 21:56:57 +00002381 // inside and outside this zone. Potential stalls should be considered before
2382 // following this policy.
2383
2384 // Compute remaining latency. We need this both to determine whether the
2385 // overall schedule has become latency-limited and whether the instructions
2386 // outside this zone are resource or latency limited.
2387 //
2388 // The "dependent" latency is updated incrementally during scheduling as the
2389 // max height/depth of scheduled nodes minus the cycles since it was
2390 // scheduled:
2391 // DLat = max (N.depth - (CurrCycle - N.ReadyCycle) for N in Zone
2392 //
2393 // The "independent" latency is the max ready queue depth:
2394 // ILat = max N.depth for N in Available|Pending
2395 //
2396 // RemainingLatency is the greater of independent and dependent latency.
2397 unsigned RemLatency = CurrZone.getDependentLatency();
2398 RemLatency = std::max(RemLatency,
2399 CurrZone.findMaxLatency(CurrZone.Available.elements()));
2400 RemLatency = std::max(RemLatency,
2401 CurrZone.findMaxLatency(CurrZone.Pending.elements()));
2402
2403 // Compute the critical resource outside the zone.
Andrew Trick7afe4812013-12-28 22:25:57 +00002404 unsigned OtherCritIdx = 0;
Andrew Trickd14d7c22013-12-28 21:56:57 +00002405 unsigned OtherCount =
2406 OtherZone ? OtherZone->getOtherResourceCount(OtherCritIdx) : 0;
2407
2408 bool OtherResLimited = false;
2409 if (SchedModel->hasInstrSchedModel()) {
2410 unsigned LFactor = SchedModel->getLatencyFactor();
2411 OtherResLimited = (int)(OtherCount - (RemLatency * LFactor)) > (int)LFactor;
2412 }
2413 // Schedule aggressively for latency in PostRA mode. We don't check for
2414 // acyclic latency during PostRA, and highly out-of-order processors will
2415 // skip PostRA scheduling.
2416 if (!OtherResLimited) {
2417 if (IsPostRA || (RemLatency + CurrZone.getCurrCycle() > Rem.CriticalPath)) {
2418 Policy.ReduceLatency |= true;
2419 DEBUG(dbgs() << " " << CurrZone.Available.getName()
2420 << " RemainingLatency " << RemLatency << " + "
2421 << CurrZone.getCurrCycle() << "c > CritPath "
2422 << Rem.CriticalPath << "\n");
2423 }
2424 }
2425 // If the same resource is limiting inside and outside the zone, do nothing.
2426 if (CurrZone.getZoneCritResIdx() == OtherCritIdx)
2427 return;
2428
2429 DEBUG(
2430 if (CurrZone.isResourceLimited()) {
2431 dbgs() << " " << CurrZone.Available.getName() << " ResourceLimited: "
2432 << SchedModel->getResourceName(CurrZone.getZoneCritResIdx())
2433 << "\n";
2434 }
2435 if (OtherResLimited)
2436 dbgs() << " RemainingLimit: "
2437 << SchedModel->getResourceName(OtherCritIdx) << "\n";
2438 if (!CurrZone.isResourceLimited() && !OtherResLimited)
2439 dbgs() << " Latency limited both directions.\n");
2440
2441 if (CurrZone.isResourceLimited() && !Policy.ReduceResIdx)
2442 Policy.ReduceResIdx = CurrZone.getZoneCritResIdx();
2443
2444 if (OtherResLimited)
2445 Policy.DemandResIdx = OtherCritIdx;
2446}
2447
2448#ifndef NDEBUG
2449const char *GenericSchedulerBase::getReasonStr(
2450 GenericSchedulerBase::CandReason Reason) {
2451 switch (Reason) {
2452 case NoCand: return "NOCAND ";
Matthias Braun49cb6e92016-05-27 22:14:26 +00002453 case Only1: return "ONLY1 ";
2454 case PhysRegCopy: return "PREG-COPY ";
Andrew Trickd14d7c22013-12-28 21:56:57 +00002455 case RegExcess: return "REG-EXCESS";
2456 case RegCritical: return "REG-CRIT ";
2457 case Stall: return "STALL ";
2458 case Cluster: return "CLUSTER ";
2459 case Weak: return "WEAK ";
2460 case RegMax: return "REG-MAX ";
2461 case ResourceReduce: return "RES-REDUCE";
2462 case ResourceDemand: return "RES-DEMAND";
2463 case TopDepthReduce: return "TOP-DEPTH ";
2464 case TopPathReduce: return "TOP-PATH ";
2465 case BotHeightReduce:return "BOT-HEIGHT";
2466 case BotPathReduce: return "BOT-PATH ";
2467 case NextDefUse: return "DEF-USE ";
2468 case NodeOrder: return "ORDER ";
2469 };
2470 llvm_unreachable("Unknown reason!");
2471}
2472
2473void GenericSchedulerBase::traceCandidate(const SchedCandidate &Cand) {
2474 PressureChange P;
2475 unsigned ResIdx = 0;
2476 unsigned Latency = 0;
2477 switch (Cand.Reason) {
2478 default:
2479 break;
2480 case RegExcess:
2481 P = Cand.RPDelta.Excess;
2482 break;
2483 case RegCritical:
2484 P = Cand.RPDelta.CriticalMax;
2485 break;
2486 case RegMax:
2487 P = Cand.RPDelta.CurrentMax;
2488 break;
2489 case ResourceReduce:
2490 ResIdx = Cand.Policy.ReduceResIdx;
2491 break;
2492 case ResourceDemand:
2493 ResIdx = Cand.Policy.DemandResIdx;
2494 break;
2495 case TopDepthReduce:
2496 Latency = Cand.SU->getDepth();
2497 break;
2498 case TopPathReduce:
2499 Latency = Cand.SU->getHeight();
2500 break;
2501 case BotHeightReduce:
2502 Latency = Cand.SU->getHeight();
2503 break;
2504 case BotPathReduce:
2505 Latency = Cand.SU->getDepth();
2506 break;
2507 }
James Y Knighte72b0db2015-09-18 18:52:20 +00002508 dbgs() << " Cand SU(" << Cand.SU->NodeNum << ") " << getReasonStr(Cand.Reason);
Andrew Trickd14d7c22013-12-28 21:56:57 +00002509 if (P.isValid())
2510 dbgs() << " " << TRI->getRegPressureSetName(P.getPSet())
2511 << ":" << P.getUnitInc() << " ";
2512 else
2513 dbgs() << " ";
2514 if (ResIdx)
2515 dbgs() << " " << SchedModel->getProcResource(ResIdx)->Name << " ";
2516 else
2517 dbgs() << " ";
2518 if (Latency)
2519 dbgs() << " " << Latency << " cycles ";
2520 else
2521 dbgs() << " ";
2522 dbgs() << '\n';
2523}
2524#endif
2525
2526/// Return true if this heuristic determines order.
2527static bool tryLess(int TryVal, int CandVal,
2528 GenericSchedulerBase::SchedCandidate &TryCand,
2529 GenericSchedulerBase::SchedCandidate &Cand,
2530 GenericSchedulerBase::CandReason Reason) {
2531 if (TryVal < CandVal) {
2532 TryCand.Reason = Reason;
2533 return true;
2534 }
2535 if (TryVal > CandVal) {
2536 if (Cand.Reason > Reason)
2537 Cand.Reason = Reason;
2538 return true;
2539 }
Andrew Trickd14d7c22013-12-28 21:56:57 +00002540 return false;
2541}
2542
2543static bool tryGreater(int TryVal, int CandVal,
2544 GenericSchedulerBase::SchedCandidate &TryCand,
2545 GenericSchedulerBase::SchedCandidate &Cand,
2546 GenericSchedulerBase::CandReason Reason) {
2547 if (TryVal > CandVal) {
2548 TryCand.Reason = Reason;
2549 return true;
2550 }
2551 if (TryVal < CandVal) {
2552 if (Cand.Reason > Reason)
2553 Cand.Reason = Reason;
2554 return true;
2555 }
Andrew Trickd14d7c22013-12-28 21:56:57 +00002556 return false;
2557}
2558
2559static bool tryLatency(GenericSchedulerBase::SchedCandidate &TryCand,
2560 GenericSchedulerBase::SchedCandidate &Cand,
2561 SchedBoundary &Zone) {
2562 if (Zone.isTop()) {
2563 if (Cand.SU->getDepth() > Zone.getScheduledLatency()) {
2564 if (tryLess(TryCand.SU->getDepth(), Cand.SU->getDepth(),
2565 TryCand, Cand, GenericSchedulerBase::TopDepthReduce))
2566 return true;
2567 }
2568 if (tryGreater(TryCand.SU->getHeight(), Cand.SU->getHeight(),
2569 TryCand, Cand, GenericSchedulerBase::TopPathReduce))
2570 return true;
Matthias Braunb550b762016-04-21 01:54:13 +00002571 } else {
Andrew Trickd14d7c22013-12-28 21:56:57 +00002572 if (Cand.SU->getHeight() > Zone.getScheduledLatency()) {
2573 if (tryLess(TryCand.SU->getHeight(), Cand.SU->getHeight(),
2574 TryCand, Cand, GenericSchedulerBase::BotHeightReduce))
2575 return true;
2576 }
2577 if (tryGreater(TryCand.SU->getDepth(), Cand.SU->getDepth(),
2578 TryCand, Cand, GenericSchedulerBase::BotPathReduce))
2579 return true;
2580 }
2581 return false;
2582}
2583
Matthias Braun49cb6e92016-05-27 22:14:26 +00002584static void tracePick(GenericSchedulerBase::CandReason Reason, bool IsTop) {
2585 DEBUG(dbgs() << "Pick " << (IsTop ? "Top " : "Bot ")
2586 << GenericSchedulerBase::getReasonStr(Reason) << '\n');
2587}
2588
Matthias Braun6ad3d052016-06-25 00:23:00 +00002589static void tracePick(const GenericSchedulerBase::SchedCandidate &Cand) {
2590 tracePick(Cand.Reason, Cand.AtTop);
Andrew Trickd14d7c22013-12-28 21:56:57 +00002591}
2592
Andrew Trickfc127d12013-12-07 05:59:44 +00002593void GenericScheduler::initialize(ScheduleDAGMI *dag) {
Andrew Trickd7f890e2013-12-28 21:56:47 +00002594 assert(dag->hasVRegLiveness() &&
2595 "(PreRA)GenericScheduler needs vreg liveness");
2596 DAG = static_cast<ScheduleDAGMILive*>(dag);
Andrew Trickfc127d12013-12-07 05:59:44 +00002597 SchedModel = DAG->getSchedModel();
2598 TRI = DAG->TRI;
2599
2600 Rem.init(DAG, SchedModel);
2601 Top.init(DAG, SchedModel, &Rem);
2602 Bot.init(DAG, SchedModel, &Rem);
2603
2604 // Initialize resource counts.
2605
2606 // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or
2607 // are disabled, then these HazardRecs will be disabled.
2608 const InstrItineraryData *Itin = SchedModel->getInstrItineraries();
Andrew Trickfc127d12013-12-07 05:59:44 +00002609 if (!Top.HazardRec) {
2610 Top.HazardRec =
Eric Christopher99556d72014-10-14 06:56:25 +00002611 DAG->MF.getSubtarget().getInstrInfo()->CreateTargetMIHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +00002612 Itin, DAG);
Andrew Trickfc127d12013-12-07 05:59:44 +00002613 }
2614 if (!Bot.HazardRec) {
2615 Bot.HazardRec =
Eric Christopher99556d72014-10-14 06:56:25 +00002616 DAG->MF.getSubtarget().getInstrInfo()->CreateTargetMIHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +00002617 Itin, DAG);
Andrew Trickfc127d12013-12-07 05:59:44 +00002618 }
Matthias Brauncc676c42016-06-25 02:03:36 +00002619 TopCand.SU = nullptr;
2620 BotCand.SU = nullptr;
Andrew Trickfc127d12013-12-07 05:59:44 +00002621}
2622
2623/// Initialize the per-region scheduling policy.
2624void GenericScheduler::initPolicy(MachineBasicBlock::iterator Begin,
2625 MachineBasicBlock::iterator End,
2626 unsigned NumRegionInstrs) {
Eric Christopher99556d72014-10-14 06:56:25 +00002627 const MachineFunction &MF = *Begin->getParent()->getParent();
2628 const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
Andrew Trickfc127d12013-12-07 05:59:44 +00002629
2630 // Avoid setting up the register pressure tracker for small regions to save
2631 // compile time. As a rough heuristic, only track pressure when the number of
2632 // schedulable instructions exceeds half the integer register file.
Andrew Trick350ff2c2014-01-21 21:27:37 +00002633 RegionPolicy.ShouldTrackPressure = true;
Andrew Trick46753512014-01-22 03:38:55 +00002634 for (unsigned VT = MVT::i32; VT > (unsigned)MVT::i1; --VT) {
2635 MVT::SimpleValueType LegalIntVT = (MVT::SimpleValueType)VT;
2636 if (TLI->isTypeLegal(LegalIntVT)) {
Andrew Trick350ff2c2014-01-21 21:27:37 +00002637 unsigned NIntRegs = Context->RegClassInfo->getNumAllocatableRegs(
Andrew Trick46753512014-01-22 03:38:55 +00002638 TLI->getRegClassFor(LegalIntVT));
Andrew Trick350ff2c2014-01-21 21:27:37 +00002639 RegionPolicy.ShouldTrackPressure = NumRegionInstrs > (NIntRegs / 2);
2640 }
2641 }
Andrew Trickfc127d12013-12-07 05:59:44 +00002642
2643 // For generic targets, we default to bottom-up, because it's simpler and more
2644 // compile-time optimizations have been implemented in that direction.
2645 RegionPolicy.OnlyBottomUp = true;
2646
2647 // Allow the subtarget to override default policy.
Duncan P. N. Exon Smith63298722016-07-01 00:23:27 +00002648 MF.getSubtarget().overrideSchedPolicy(RegionPolicy, NumRegionInstrs);
Andrew Trickfc127d12013-12-07 05:59:44 +00002649
2650 // After subtarget overrides, apply command line options.
2651 if (!EnableRegPressure)
2652 RegionPolicy.ShouldTrackPressure = false;
2653
2654 // Check -misched-topdown/bottomup can force or unforce scheduling direction.
2655 // e.g. -misched-bottomup=false allows scheduling in both directions.
2656 assert((!ForceTopDown || !ForceBottomUp) &&
2657 "-misched-topdown incompatible with -misched-bottomup");
2658 if (ForceBottomUp.getNumOccurrences() > 0) {
2659 RegionPolicy.OnlyBottomUp = ForceBottomUp;
2660 if (RegionPolicy.OnlyBottomUp)
2661 RegionPolicy.OnlyTopDown = false;
2662 }
2663 if (ForceTopDown.getNumOccurrences() > 0) {
2664 RegionPolicy.OnlyTopDown = ForceTopDown;
2665 if (RegionPolicy.OnlyTopDown)
2666 RegionPolicy.OnlyBottomUp = false;
2667 }
2668}
2669
James Y Knighte72b0db2015-09-18 18:52:20 +00002670void GenericScheduler::dumpPolicy() {
Matthias Braun8c209aa2017-01-28 02:02:38 +00002671 // Cannot completely remove virtual function even in release mode.
2672#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
James Y Knighte72b0db2015-09-18 18:52:20 +00002673 dbgs() << "GenericScheduler RegionPolicy: "
2674 << " ShouldTrackPressure=" << RegionPolicy.ShouldTrackPressure
2675 << " OnlyTopDown=" << RegionPolicy.OnlyTopDown
2676 << " OnlyBottomUp=" << RegionPolicy.OnlyBottomUp
2677 << "\n";
Matthias Braun8c209aa2017-01-28 02:02:38 +00002678#endif
James Y Knighte72b0db2015-09-18 18:52:20 +00002679}
2680
Andrew Trickfc127d12013-12-07 05:59:44 +00002681/// Set IsAcyclicLatencyLimited if the acyclic path is longer than the cyclic
2682/// critical path by more cycles than it takes to drain the instruction buffer.
2683/// We estimate an upper bounds on in-flight instructions as:
2684///
2685/// CyclesPerIteration = max( CyclicPath, Loop-Resource-Height )
2686/// InFlightIterations = AcyclicPath / CyclesPerIteration
2687/// InFlightResources = InFlightIterations * LoopResources
2688///
2689/// TODO: Check execution resources in addition to IssueCount.
2690void GenericScheduler::checkAcyclicLatency() {
2691 if (Rem.CyclicCritPath == 0 || Rem.CyclicCritPath >= Rem.CriticalPath)
2692 return;
2693
2694 // Scaled number of cycles per loop iteration.
2695 unsigned IterCount =
2696 std::max(Rem.CyclicCritPath * SchedModel->getLatencyFactor(),
2697 Rem.RemIssueCount);
2698 // Scaled acyclic critical path.
2699 unsigned AcyclicCount = Rem.CriticalPath * SchedModel->getLatencyFactor();
2700 // InFlightCount = (AcyclicPath / IterCycles) * InstrPerLoop
2701 unsigned InFlightCount =
2702 (AcyclicCount * Rem.RemIssueCount + IterCount-1) / IterCount;
2703 unsigned BufferLimit =
2704 SchedModel->getMicroOpBufferSize() * SchedModel->getMicroOpFactor();
2705
2706 Rem.IsAcyclicLatencyLimited = InFlightCount > BufferLimit;
2707
2708 DEBUG(dbgs() << "IssueCycles="
2709 << Rem.RemIssueCount / SchedModel->getLatencyFactor() << "c "
2710 << "IterCycles=" << IterCount / SchedModel->getLatencyFactor()
2711 << "c NumIters=" << (AcyclicCount + IterCount-1) / IterCount
2712 << " InFlight=" << InFlightCount / SchedModel->getMicroOpFactor()
2713 << "m BufferLim=" << SchedModel->getMicroOpBufferSize() << "m\n";
2714 if (Rem.IsAcyclicLatencyLimited)
2715 dbgs() << " ACYCLIC LATENCY LIMIT\n");
2716}
2717
2718void GenericScheduler::registerRoots() {
2719 Rem.CriticalPath = DAG->ExitSU.getDepth();
2720
2721 // Some roots may not feed into ExitSU. Check all of them in case.
2722 for (std::vector<SUnit*>::const_iterator
2723 I = Bot.Available.begin(), E = Bot.Available.end(); I != E; ++I) {
2724 if ((*I)->getDepth() > Rem.CriticalPath)
2725 Rem.CriticalPath = (*I)->getDepth();
2726 }
Gerolf Hoflehnerb5220dc2014-08-07 21:49:44 +00002727 DEBUG(dbgs() << "Critical Path(GS-RR ): " << Rem.CriticalPath << '\n');
2728 if (DumpCriticalPathLength) {
2729 errs() << "Critical Path(GS-RR ): " << Rem.CriticalPath << " \n";
2730 }
Andrew Trickfc127d12013-12-07 05:59:44 +00002731
Matthias Braun99551052017-04-12 18:09:05 +00002732 if (EnableCyclicPath && SchedModel->getMicroOpBufferSize() > 0) {
Andrew Trickfc127d12013-12-07 05:59:44 +00002733 Rem.CyclicCritPath = DAG->computeCyclicCriticalPath();
2734 checkAcyclicLatency();
2735 }
2736}
2737
Andrew Trick1a831342013-08-30 03:49:48 +00002738static bool tryPressure(const PressureChange &TryP,
2739 const PressureChange &CandP,
Andrew Trickd14d7c22013-12-28 21:56:57 +00002740 GenericSchedulerBase::SchedCandidate &TryCand,
2741 GenericSchedulerBase::SchedCandidate &Cand,
Tom Stellard5ce53062015-12-16 18:31:01 +00002742 GenericSchedulerBase::CandReason Reason,
2743 const TargetRegisterInfo *TRI,
2744 const MachineFunction &MF) {
Andrew Trickb1a45b62013-08-30 04:27:29 +00002745 // If one candidate decreases and the other increases, go with it.
2746 // Invalid candidates have UnitInc==0.
Hal Finkel7a87f8a2014-10-10 17:06:20 +00002747 if (tryGreater(TryP.getUnitInc() < 0, CandP.getUnitInc() < 0, TryCand, Cand,
2748 Reason)) {
Andrew Trickb1a45b62013-08-30 04:27:29 +00002749 return true;
2750 }
Matthias Braun6ad3d052016-06-25 00:23:00 +00002751 // Do not compare the magnitude of pressure changes between top and bottom
2752 // boundary.
2753 if (Cand.AtTop != TryCand.AtTop)
2754 return false;
2755
2756 // If both candidates affect the same set in the same boundary, go with the
2757 // smallest increase.
2758 unsigned TryPSet = TryP.getPSetOrMax();
2759 unsigned CandPSet = CandP.getPSetOrMax();
2760 if (TryPSet == CandPSet) {
2761 return tryLess(TryP.getUnitInc(), CandP.getUnitInc(), TryCand, Cand,
2762 Reason);
2763 }
Tom Stellard5ce53062015-12-16 18:31:01 +00002764
2765 int TryRank = TryP.isValid() ? TRI->getRegPressureSetScore(MF, TryPSet) :
2766 std::numeric_limits<int>::max();
2767
2768 int CandRank = CandP.isValid() ? TRI->getRegPressureSetScore(MF, CandPSet) :
2769 std::numeric_limits<int>::max();
2770
Andrew Trick401b6952013-07-25 07:26:35 +00002771 // If the candidates are decreasing pressure, reverse priority.
Andrew Trick1a831342013-08-30 03:49:48 +00002772 if (TryP.getUnitInc() < 0)
Andrew Trick401b6952013-07-25 07:26:35 +00002773 std::swap(TryRank, CandRank);
2774 return tryGreater(TryRank, CandRank, TryCand, Cand, Reason);
2775}
2776
Andrew Tricka7714a02012-11-12 19:40:10 +00002777static unsigned getWeakLeft(const SUnit *SU, bool isTop) {
2778 return (isTop) ? SU->WeakPredsLeft : SU->WeakSuccsLeft;
2779}
2780
Andrew Tricke833e1c2013-04-13 06:07:40 +00002781/// Minimize physical register live ranges. Regalloc wants them adjacent to
2782/// their physreg def/use.
2783///
2784/// FIXME: This is an unnecessary check on the critical path. Most are root/leaf
2785/// copies which can be prescheduled. The rest (e.g. x86 MUL) could be bundled
2786/// with the operation that produces or consumes the physreg. We'll do this when
2787/// regalloc has support for parallel copies.
2788static int biasPhysRegCopy(const SUnit *SU, bool isTop) {
2789 const MachineInstr *MI = SU->getInstr();
2790 if (!MI->isCopy())
2791 return 0;
2792
2793 unsigned ScheduledOper = isTop ? 1 : 0;
2794 unsigned UnscheduledOper = isTop ? 0 : 1;
2795 // If we have already scheduled the physreg produce/consumer, immediately
2796 // schedule the copy.
2797 if (TargetRegisterInfo::isPhysicalRegister(
2798 MI->getOperand(ScheduledOper).getReg()))
2799 return 1;
2800 // If the physreg is at the boundary, defer it. Otherwise schedule it
2801 // immediately to free the dependent. We can hoist the copy later.
2802 bool AtBoundary = isTop ? !SU->NumSuccsLeft : !SU->NumPredsLeft;
2803 if (TargetRegisterInfo::isPhysicalRegister(
2804 MI->getOperand(UnscheduledOper).getReg()))
2805 return AtBoundary ? -1 : 1;
2806 return 0;
2807}
2808
Matthias Braun4f573772016-04-22 19:10:15 +00002809void GenericScheduler::initCandidate(SchedCandidate &Cand, SUnit *SU,
2810 bool AtTop,
2811 const RegPressureTracker &RPTracker,
2812 RegPressureTracker &TempTracker) {
2813 Cand.SU = SU;
Matthias Braun6ad3d052016-06-25 00:23:00 +00002814 Cand.AtTop = AtTop;
Matthias Braun4f573772016-04-22 19:10:15 +00002815 if (DAG->isTrackingPressure()) {
2816 if (AtTop) {
2817 TempTracker.getMaxDownwardPressureDelta(
2818 Cand.SU->getInstr(),
2819 Cand.RPDelta,
2820 DAG->getRegionCriticalPSets(),
2821 DAG->getRegPressure().MaxSetPressure);
2822 } else {
2823 if (VerifyScheduling) {
2824 TempTracker.getMaxUpwardPressureDelta(
2825 Cand.SU->getInstr(),
2826 &DAG->getPressureDiff(Cand.SU),
2827 Cand.RPDelta,
2828 DAG->getRegionCriticalPSets(),
2829 DAG->getRegPressure().MaxSetPressure);
2830 } else {
2831 RPTracker.getUpwardPressureDelta(
2832 Cand.SU->getInstr(),
2833 DAG->getPressureDiff(Cand.SU),
2834 Cand.RPDelta,
2835 DAG->getRegionCriticalPSets(),
2836 DAG->getRegPressure().MaxSetPressure);
2837 }
2838 }
2839 }
2840 DEBUG(if (Cand.RPDelta.Excess.isValid())
2841 dbgs() << " Try SU(" << Cand.SU->NodeNum << ") "
2842 << TRI->getRegPressureSetName(Cand.RPDelta.Excess.getPSet())
2843 << ":" << Cand.RPDelta.Excess.getUnitInc() << "\n");
2844}
2845
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002846/// Apply a set of heursitics to a new candidate. Heuristics are currently
2847/// hierarchical. This may be more efficient than a graduated cost model because
2848/// we don't need to evaluate all aspects of the model for each node in the
2849/// queue. But it's really done to make the heuristics easier to debug and
2850/// statistically analyze.
2851///
2852/// \param Cand provides the policy and current best candidate.
2853/// \param TryCand refers to the next SUnit candidate, otherwise uninitialized.
Matthias Braun6ad3d052016-06-25 00:23:00 +00002854/// \param Zone describes the scheduled zone that we are extending, or nullptr
2855// if Cand is from a different zone than TryCand.
Andrew Trick665d3ec2013-09-19 23:10:59 +00002856void GenericScheduler::tryCandidate(SchedCandidate &Cand,
Andrew Trickbb1247b2013-12-05 17:55:47 +00002857 SchedCandidate &TryCand,
Matthias Braun6ad3d052016-06-25 00:23:00 +00002858 SchedBoundary *Zone) {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002859 // Initialize the candidate if needed.
2860 if (!Cand.isValid()) {
2861 TryCand.Reason = NodeOrder;
2862 return;
2863 }
Andrew Tricke833e1c2013-04-13 06:07:40 +00002864
Matthias Braun6ad3d052016-06-25 00:23:00 +00002865 if (tryGreater(biasPhysRegCopy(TryCand.SU, TryCand.AtTop),
2866 biasPhysRegCopy(Cand.SU, Cand.AtTop),
Andrew Tricke833e1c2013-04-13 06:07:40 +00002867 TryCand, Cand, PhysRegCopy))
2868 return;
2869
Andrew Tricke02d5da2015-05-17 23:40:27 +00002870 // Avoid exceeding the target's limit.
Andrew Trick66c3dfb2013-09-04 21:00:11 +00002871 if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.Excess,
2872 Cand.RPDelta.Excess,
Tom Stellard5ce53062015-12-16 18:31:01 +00002873 TryCand, Cand, RegExcess, TRI,
2874 DAG->MF))
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002875 return;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002876
2877 // Avoid increasing the max critical pressure in the scheduled region.
Andrew Trick66c3dfb2013-09-04 21:00:11 +00002878 if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CriticalMax,
2879 Cand.RPDelta.CriticalMax,
Tom Stellard5ce53062015-12-16 18:31:01 +00002880 TryCand, Cand, RegCritical, TRI,
2881 DAG->MF))
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002882 return;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002883
Matthias Braun6ad3d052016-06-25 00:23:00 +00002884 // We only compare a subset of features when comparing nodes between
2885 // Top and Bottom boundary. Some properties are simply incomparable, in many
2886 // other instances we should only override the other boundary if something
2887 // is a clear good pick on one boundary. Skip heuristics that are more
2888 // "tie-breaking" in nature.
2889 bool SameBoundary = Zone != nullptr;
2890 if (SameBoundary) {
2891 // For loops that are acyclic path limited, aggressively schedule for
Jonas Paulssonbaeb4022016-11-04 08:31:14 +00002892 // latency. Within an single cycle, whenever CurrMOps > 0, allow normal
2893 // heuristics to take precedence.
Matthias Braun6ad3d052016-06-25 00:23:00 +00002894 if (Rem.IsAcyclicLatencyLimited && !Zone->getCurrMOps() &&
2895 tryLatency(TryCand, Cand, *Zone))
2896 return;
Andrew Trickddffae92013-09-06 17:32:36 +00002897
Matthias Braun6ad3d052016-06-25 00:23:00 +00002898 // Prioritize instructions that read unbuffered resources by stall cycles.
2899 if (tryLess(Zone->getLatencyStallCycles(TryCand.SU),
2900 Zone->getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
2901 return;
2902 }
Andrew Trick880e5732013-12-05 17:55:58 +00002903
Andrew Tricka7714a02012-11-12 19:40:10 +00002904 // Keep clustered nodes together to encourage downstream peephole
2905 // optimizations which may reduce resource requirements.
2906 //
2907 // This is a best effort to set things up for a post-RA pass. Optimizations
2908 // like generating loads of multiple registers should ideally be done within
2909 // the scheduler pass by combining the loads during DAG postprocessing.
Matthias Braun6ad3d052016-06-25 00:23:00 +00002910 const SUnit *CandNextClusterSU =
2911 Cand.AtTop ? DAG->getNextClusterSucc() : DAG->getNextClusterPred();
2912 const SUnit *TryCandNextClusterSU =
2913 TryCand.AtTop ? DAG->getNextClusterSucc() : DAG->getNextClusterPred();
2914 if (tryGreater(TryCand.SU == TryCandNextClusterSU,
2915 Cand.SU == CandNextClusterSU,
Andrew Tricka7714a02012-11-12 19:40:10 +00002916 TryCand, Cand, Cluster))
2917 return;
Andrew Trick85a1d4c2013-04-24 15:54:43 +00002918
Matthias Braun6ad3d052016-06-25 00:23:00 +00002919 if (SameBoundary) {
2920 // Weak edges are for clustering and other constraints.
2921 if (tryLess(getWeakLeft(TryCand.SU, TryCand.AtTop),
2922 getWeakLeft(Cand.SU, Cand.AtTop),
2923 TryCand, Cand, Weak))
2924 return;
Andrew Tricka7714a02012-11-12 19:40:10 +00002925 }
Matthias Braun6ad3d052016-06-25 00:23:00 +00002926
Andrew Trick71f08a32013-06-17 21:45:13 +00002927 // Avoid increasing the max pressure of the entire region.
Andrew Trick66c3dfb2013-09-04 21:00:11 +00002928 if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CurrentMax,
2929 Cand.RPDelta.CurrentMax,
Tom Stellard5ce53062015-12-16 18:31:01 +00002930 TryCand, Cand, RegMax, TRI,
2931 DAG->MF))
Andrew Trick71f08a32013-06-17 21:45:13 +00002932 return;
2933
Matthias Braun6ad3d052016-06-25 00:23:00 +00002934 if (SameBoundary) {
2935 // Avoid critical resource consumption and balance the schedule.
2936 TryCand.initResourceDelta(DAG, SchedModel);
2937 if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
2938 TryCand, Cand, ResourceReduce))
2939 return;
2940 if (tryGreater(TryCand.ResDelta.DemandedResources,
2941 Cand.ResDelta.DemandedResources,
2942 TryCand, Cand, ResourceDemand))
2943 return;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002944
Matthias Braun6ad3d052016-06-25 00:23:00 +00002945 // Avoid serializing long latency dependence chains.
2946 // For acyclic path limited loops, latency was already checked above.
2947 if (!RegionPolicy.DisableLatencyHeuristic && TryCand.Policy.ReduceLatency &&
2948 !Rem.IsAcyclicLatencyLimited && tryLatency(TryCand, Cand, *Zone))
2949 return;
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002950
Matthias Braun6ad3d052016-06-25 00:23:00 +00002951 // Fall through to original instruction order.
2952 if ((Zone->isTop() && TryCand.SU->NodeNum < Cand.SU->NodeNum)
2953 || (!Zone->isTop() && TryCand.SU->NodeNum > Cand.SU->NodeNum)) {
2954 TryCand.Reason = NodeOrder;
2955 }
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002956 }
2957}
Andrew Trick419eae22012-05-10 21:06:19 +00002958
Andrew Trickc573cd92013-09-06 17:32:44 +00002959/// Pick the best candidate from the queue.
Andrew Trick7ee9de52012-05-10 21:06:16 +00002960///
2961/// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during
2962/// DAG building. To adjust for the current scheduling location we need to
2963/// maintain the number of vreg uses remaining to be top-scheduled.
Andrew Trick665d3ec2013-09-19 23:10:59 +00002964void GenericScheduler::pickNodeFromQueue(SchedBoundary &Zone,
Matthias Braun6ad3d052016-06-25 00:23:00 +00002965 const CandPolicy &ZonePolicy,
Andrew Trickbb1247b2013-12-05 17:55:47 +00002966 const RegPressureTracker &RPTracker,
2967 SchedCandidate &Cand) {
Andrew Trick7ee9de52012-05-10 21:06:16 +00002968 // getMaxPressureDelta temporarily modifies the tracker.
2969 RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
2970
Matthias Braund29d31e2016-06-23 21:27:38 +00002971 ReadyQueue &Q = Zone.Available;
Andrew Trickdd375dd2012-05-24 22:11:03 +00002972 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
Andrew Trick7ee9de52012-05-10 21:06:16 +00002973
Matthias Braun6ad3d052016-06-25 00:23:00 +00002974 SchedCandidate TryCand(ZonePolicy);
Matthias Braun4f573772016-04-22 19:10:15 +00002975 initCandidate(TryCand, *I, Zone.isTop(), RPTracker, TempTracker);
Matthias Braun6ad3d052016-06-25 00:23:00 +00002976 // Pass SchedBoundary only when comparing nodes from the same boundary.
2977 SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
2978 tryCandidate(Cand, TryCand, ZoneArg);
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002979 if (TryCand.Reason != NoCand) {
2980 // Initialize resource delta if needed in case future heuristics query it.
2981 if (TryCand.ResDelta == SchedResourceDelta())
2982 TryCand.initResourceDelta(DAG, SchedModel);
2983 Cand.setBest(TryCand);
Andrew Trick419d4912013-04-05 00:31:29 +00002984 DEBUG(traceCandidate(Cand));
Andrew Trick22025772012-05-17 18:35:10 +00002985 }
Andrew Trick7ee9de52012-05-10 21:06:16 +00002986 }
Andrew Trick3ca33ac2012-11-07 07:05:09 +00002987}
2988
Andrew Trick22025772012-05-17 18:35:10 +00002989/// Pick the best candidate node from either the top or bottom queue.
Andrew Trick665d3ec2013-09-19 23:10:59 +00002990SUnit *GenericScheduler::pickNodeBidirectional(bool &IsTopNode) {
Andrew Trick22025772012-05-17 18:35:10 +00002991 // Schedule as far as possible in the direction of no choice. This is most
2992 // efficient, but also provides the best heuristics for CriticalPSets.
Andrew Trick61f1a272012-05-24 22:11:09 +00002993 if (SUnit *SU = Bot.pickOnlyChoice()) {
Andrew Trick22025772012-05-17 18:35:10 +00002994 IsTopNode = false;
Matthias Braun49cb6e92016-05-27 22:14:26 +00002995 tracePick(Only1, false);
Andrew Trick61f1a272012-05-24 22:11:09 +00002996 return SU;
Andrew Trick22025772012-05-17 18:35:10 +00002997 }
Andrew Trick61f1a272012-05-24 22:11:09 +00002998 if (SUnit *SU = Top.pickOnlyChoice()) {
Andrew Trick22025772012-05-17 18:35:10 +00002999 IsTopNode = true;
Matthias Braun49cb6e92016-05-27 22:14:26 +00003000 tracePick(Only1, true);
Andrew Trick61f1a272012-05-24 22:11:09 +00003001 return SU;
Andrew Trick22025772012-05-17 18:35:10 +00003002 }
Andrew Trickfc127d12013-12-07 05:59:44 +00003003 // Set the bottom-up policy based on the state of the current bottom zone and
3004 // the instructions outside the zone, including the top zone.
Matthias Braun6ad3d052016-06-25 00:23:00 +00003005 CandPolicy BotPolicy;
3006 setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
Andrew Trickfc127d12013-12-07 05:59:44 +00003007 // Set the top-down policy based on the state of the current top zone and
3008 // the instructions outside the zone, including the bottom zone.
Matthias Braun6ad3d052016-06-25 00:23:00 +00003009 CandPolicy TopPolicy;
3010 setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
Andrew Trick3ca33ac2012-11-07 07:05:09 +00003011
Matthias Brauncc676c42016-06-25 02:03:36 +00003012 // See if BotCand is still valid (because we previously scheduled from Top).
Matthias Braund29d31e2016-06-23 21:27:38 +00003013 DEBUG(dbgs() << "Picking from Bot:\n");
Matthias Brauncc676c42016-06-25 02:03:36 +00003014 if (!BotCand.isValid() || BotCand.SU->isScheduled ||
3015 BotCand.Policy != BotPolicy) {
3016 BotCand.reset(CandPolicy());
3017 pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
3018 assert(BotCand.Reason != NoCand && "failed to find the first candidate");
3019 } else {
3020 DEBUG(traceCandidate(BotCand));
3021#ifndef NDEBUG
3022 if (VerifyScheduling) {
3023 SchedCandidate TCand;
3024 TCand.reset(CandPolicy());
3025 pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), TCand);
3026 assert(TCand.SU == BotCand.SU &&
3027 "Last pick result should correspond to re-picking right now");
3028 }
3029#endif
3030 }
Andrew Trick22025772012-05-17 18:35:10 +00003031
Andrew Trick22025772012-05-17 18:35:10 +00003032 // Check if the top Q has a better candidate.
Matthias Braund29d31e2016-06-23 21:27:38 +00003033 DEBUG(dbgs() << "Picking from Top:\n");
Matthias Brauncc676c42016-06-25 02:03:36 +00003034 if (!TopCand.isValid() || TopCand.SU->isScheduled ||
3035 TopCand.Policy != TopPolicy) {
3036 TopCand.reset(CandPolicy());
3037 pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
3038 assert(TopCand.Reason != NoCand && "failed to find the first candidate");
3039 } else {
3040 DEBUG(traceCandidate(TopCand));
3041#ifndef NDEBUG
3042 if (VerifyScheduling) {
3043 SchedCandidate TCand;
3044 TCand.reset(CandPolicy());
3045 pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TCand);
3046 assert(TCand.SU == TopCand.SU &&
3047 "Last pick result should correspond to re-picking right now");
3048 }
3049#endif
3050 }
3051
3052 // Pick best from BotCand and TopCand.
3053 assert(BotCand.isValid());
3054 assert(TopCand.isValid());
3055 SchedCandidate Cand = BotCand;
3056 TopCand.Reason = NoCand;
3057 tryCandidate(Cand, TopCand, nullptr);
3058 if (TopCand.Reason != NoCand) {
3059 Cand.setBest(TopCand);
3060 DEBUG(traceCandidate(Cand));
3061 }
Andrew Trick22025772012-05-17 18:35:10 +00003062
Matthias Braun6ad3d052016-06-25 00:23:00 +00003063 IsTopNode = Cand.AtTop;
3064 tracePick(Cand);
3065 return Cand.SU;
Andrew Trick22025772012-05-17 18:35:10 +00003066}
3067
3068/// Pick the best node to balance the schedule. Implements MachineSchedStrategy.
Andrew Trick665d3ec2013-09-19 23:10:59 +00003069SUnit *GenericScheduler::pickNode(bool &IsTopNode) {
Andrew Trick7ee9de52012-05-10 21:06:16 +00003070 if (DAG->top() == DAG->bottom()) {
Andrew Trick61f1a272012-05-24 22:11:09 +00003071 assert(Top.Available.empty() && Top.Pending.empty() &&
3072 Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
Craig Topperc0196b12014-04-14 00:51:57 +00003073 return nullptr;
Andrew Trick7ee9de52012-05-10 21:06:16 +00003074 }
Andrew Trick7ee9de52012-05-10 21:06:16 +00003075 SUnit *SU;
Andrew Trick984d98b2012-10-08 18:53:53 +00003076 do {
Andrew Trick75e411c2013-09-06 17:32:34 +00003077 if (RegionPolicy.OnlyTopDown) {
Andrew Trick984d98b2012-10-08 18:53:53 +00003078 SU = Top.pickOnlyChoice();
3079 if (!SU) {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00003080 CandPolicy NoPolicy;
Matthias Brauncc676c42016-06-25 02:03:36 +00003081 TopCand.reset(NoPolicy);
Matthias Braun6ad3d052016-06-25 00:23:00 +00003082 pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
Andrew Trick1ab16d92013-09-04 21:00:13 +00003083 assert(TopCand.Reason != NoCand && "failed to find a candidate");
Matthias Braun6ad3d052016-06-25 00:23:00 +00003084 tracePick(TopCand);
Andrew Trick984d98b2012-10-08 18:53:53 +00003085 SU = TopCand.SU;
3086 }
3087 IsTopNode = true;
Matthias Braunb550b762016-04-21 01:54:13 +00003088 } else if (RegionPolicy.OnlyBottomUp) {
Andrew Trick984d98b2012-10-08 18:53:53 +00003089 SU = Bot.pickOnlyChoice();
3090 if (!SU) {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00003091 CandPolicy NoPolicy;
Matthias Brauncc676c42016-06-25 02:03:36 +00003092 BotCand.reset(NoPolicy);
Matthias Braun6ad3d052016-06-25 00:23:00 +00003093 pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
Andrew Trick1ab16d92013-09-04 21:00:13 +00003094 assert(BotCand.Reason != NoCand && "failed to find a candidate");
Matthias Braun6ad3d052016-06-25 00:23:00 +00003095 tracePick(BotCand);
Andrew Trick984d98b2012-10-08 18:53:53 +00003096 SU = BotCand.SU;
3097 }
3098 IsTopNode = false;
Matthias Braunb550b762016-04-21 01:54:13 +00003099 } else {
Andrew Trick3ca33ac2012-11-07 07:05:09 +00003100 SU = pickNodeBidirectional(IsTopNode);
Andrew Trick984d98b2012-10-08 18:53:53 +00003101 }
3102 } while (SU->isScheduled);
3103
Andrew Trick61f1a272012-05-24 22:11:09 +00003104 if (SU->isTopReady())
3105 Top.removeReady(SU);
3106 if (SU->isBottomReady())
3107 Bot.removeReady(SU);
Andrew Trick4e7f6a72012-05-25 02:02:39 +00003108
Andrew Trick1f0bb692013-04-13 06:07:49 +00003109 DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr());
Andrew Trick7ee9de52012-05-10 21:06:16 +00003110 return SU;
3111}
3112
Andrew Trick665d3ec2013-09-19 23:10:59 +00003113void GenericScheduler::reschedulePhysRegCopies(SUnit *SU, bool isTop) {
Andrew Tricke833e1c2013-04-13 06:07:40 +00003114 MachineBasicBlock::iterator InsertPos = SU->getInstr();
3115 if (!isTop)
3116 ++InsertPos;
3117 SmallVectorImpl<SDep> &Deps = isTop ? SU->Preds : SU->Succs;
3118
3119 // Find already scheduled copies with a single physreg dependence and move
3120 // them just above the scheduled instruction.
3121 for (SmallVectorImpl<SDep>::iterator I = Deps.begin(), E = Deps.end();
3122 I != E; ++I) {
3123 if (I->getKind() != SDep::Data || !TRI->isPhysicalRegister(I->getReg()))
3124 continue;
3125 SUnit *DepSU = I->getSUnit();
3126 if (isTop ? DepSU->Succs.size() > 1 : DepSU->Preds.size() > 1)
3127 continue;
3128 MachineInstr *Copy = DepSU->getInstr();
3129 if (!Copy->isCopy())
3130 continue;
3131 DEBUG(dbgs() << " Rescheduling physreg copy ";
3132 I->getSUnit()->dump(DAG));
3133 DAG->moveInstruction(Copy, InsertPos);
3134 }
3135}
3136
Andrew Trick61f1a272012-05-24 22:11:09 +00003137/// Update the scheduler's state after scheduling a node. This is the same node
Andrew Trickd14d7c22013-12-28 21:56:57 +00003138/// that was just returned by pickNode(). However, ScheduleDAGMILive needs to
3139/// update it's state based on the current cycle before MachineSchedStrategy
3140/// does.
Andrew Tricke833e1c2013-04-13 06:07:40 +00003141///
3142/// FIXME: Eventually, we may bundle physreg copies rather than rescheduling
3143/// them here. See comments in biasPhysRegCopy.
Andrew Trick665d3ec2013-09-19 23:10:59 +00003144void GenericScheduler::schedNode(SUnit *SU, bool IsTopNode) {
Andrew Trick45446062012-06-05 21:11:27 +00003145 if (IsTopNode) {
Andrew Trickfc127d12013-12-07 05:59:44 +00003146 SU->TopReadyCycle = std::max(SU->TopReadyCycle, Top.getCurrCycle());
Andrew Trickce27bb92012-06-29 03:23:22 +00003147 Top.bumpNode(SU);
Andrew Tricke833e1c2013-04-13 06:07:40 +00003148 if (SU->hasPhysRegUses)
3149 reschedulePhysRegCopies(SU, true);
Matthias Braunb550b762016-04-21 01:54:13 +00003150 } else {
Andrew Trickfc127d12013-12-07 05:59:44 +00003151 SU->BotReadyCycle = std::max(SU->BotReadyCycle, Bot.getCurrCycle());
Andrew Trickce27bb92012-06-29 03:23:22 +00003152 Bot.bumpNode(SU);
Andrew Tricke833e1c2013-04-13 06:07:40 +00003153 if (SU->hasPhysRegDefs)
3154 reschedulePhysRegCopies(SU, false);
Andrew Trick61f1a272012-05-24 22:11:09 +00003155 }
3156}
3157
Andrew Trick8823dec2012-03-14 04:00:41 +00003158/// Create the standard converging machine scheduler. This will be used as the
3159/// default scheduler if the target does not set a default.
Matthias Braun115efcd2016-11-28 20:11:54 +00003160ScheduleDAGMILive *llvm::createGenericSchedLive(MachineSchedContext *C) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003161 ScheduleDAGMILive *DAG =
3162 new ScheduleDAGMILive(C, llvm::make_unique<GenericScheduler>(C));
Andrew Tricka7714a02012-11-12 19:40:10 +00003163 // Register DAG post-processors.
Andrew Trick85a1d4c2013-04-24 15:54:43 +00003164 //
3165 // FIXME: extend the mutation API to allow earlier mutations to instantiate
3166 // data and pass it to later mutations. Have a single mutation that gathers
3167 // the interesting nodes in one pass.
Tom Stellard68726a52016-08-19 19:59:18 +00003168 DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
Andrew Tricka7714a02012-11-12 19:40:10 +00003169 return DAG;
Andrew Tricke1c034f2012-01-17 06:55:03 +00003170}
Andrew Trickd14d7c22013-12-28 21:56:57 +00003171
Matthias Braun115efcd2016-11-28 20:11:54 +00003172static ScheduleDAGInstrs *createConveringSched(MachineSchedContext *C) {
3173 return createGenericSchedLive(C);
3174}
3175
Andrew Tricke1c034f2012-01-17 06:55:03 +00003176static MachineSchedRegistry
Andrew Trick665d3ec2013-09-19 23:10:59 +00003177GenericSchedRegistry("converge", "Standard converging scheduler.",
Matthias Braun115efcd2016-11-28 20:11:54 +00003178 createConveringSched);
Andrew Trickd14d7c22013-12-28 21:56:57 +00003179
3180//===----------------------------------------------------------------------===//
3181// PostGenericScheduler - Generic PostRA implementation of MachineSchedStrategy.
3182//===----------------------------------------------------------------------===//
3183
Andrew Trick3ccf71d2014-06-04 07:06:18 +00003184void PostGenericScheduler::initialize(ScheduleDAGMI *Dag) {
3185 DAG = Dag;
3186 SchedModel = DAG->getSchedModel();
3187 TRI = DAG->TRI;
Andrew Trickd14d7c22013-12-28 21:56:57 +00003188
Andrew Trick3ccf71d2014-06-04 07:06:18 +00003189 Rem.init(DAG, SchedModel);
3190 Top.init(DAG, SchedModel, &Rem);
3191 BotRoots.clear();
Andrew Trickd14d7c22013-12-28 21:56:57 +00003192
Andrew Trick3ccf71d2014-06-04 07:06:18 +00003193 // Initialize the HazardRecognizers. If itineraries don't exist, are empty,
3194 // or are disabled, then these HazardRecs will be disabled.
3195 const InstrItineraryData *Itin = SchedModel->getInstrItineraries();
Andrew Trick3ccf71d2014-06-04 07:06:18 +00003196 if (!Top.HazardRec) {
3197 Top.HazardRec =
Eric Christopher99556d72014-10-14 06:56:25 +00003198 DAG->MF.getSubtarget().getInstrInfo()->CreateTargetMIHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +00003199 Itin, DAG);
Andrew Trickd14d7c22013-12-28 21:56:57 +00003200 }
Andrew Trick3ccf71d2014-06-04 07:06:18 +00003201}
Andrew Trickd14d7c22013-12-28 21:56:57 +00003202
Andrew Trickd14d7c22013-12-28 21:56:57 +00003203void PostGenericScheduler::registerRoots() {
3204 Rem.CriticalPath = DAG->ExitSU.getDepth();
3205
3206 // Some roots may not feed into ExitSU. Check all of them in case.
3207 for (SmallVectorImpl<SUnit*>::const_iterator
3208 I = BotRoots.begin(), E = BotRoots.end(); I != E; ++I) {
3209 if ((*I)->getDepth() > Rem.CriticalPath)
3210 Rem.CriticalPath = (*I)->getDepth();
3211 }
Gerolf Hoflehnerb5220dc2014-08-07 21:49:44 +00003212 DEBUG(dbgs() << "Critical Path: (PGS-RR) " << Rem.CriticalPath << '\n');
3213 if (DumpCriticalPathLength) {
3214 errs() << "Critical Path(PGS-RR ): " << Rem.CriticalPath << " \n";
3215 }
Andrew Trickd14d7c22013-12-28 21:56:57 +00003216}
3217
3218/// Apply a set of heursitics to a new candidate for PostRA scheduling.
3219///
3220/// \param Cand provides the policy and current best candidate.
3221/// \param TryCand refers to the next SUnit candidate, otherwise uninitialized.
3222void PostGenericScheduler::tryCandidate(SchedCandidate &Cand,
3223 SchedCandidate &TryCand) {
3224
3225 // Initialize the candidate if needed.
3226 if (!Cand.isValid()) {
3227 TryCand.Reason = NodeOrder;
3228 return;
3229 }
3230
3231 // Prioritize instructions that read unbuffered resources by stall cycles.
3232 if (tryLess(Top.getLatencyStallCycles(TryCand.SU),
3233 Top.getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
3234 return;
3235
Florian Hahnabb42182017-05-23 09:33:34 +00003236 // Keep clustered nodes together.
3237 if (tryGreater(TryCand.SU == DAG->getNextClusterSucc(),
3238 Cand.SU == DAG->getNextClusterSucc(),
3239 TryCand, Cand, Cluster))
3240 return;
3241
Andrew Trickd14d7c22013-12-28 21:56:57 +00003242 // Avoid critical resource consumption and balance the schedule.
3243 if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
3244 TryCand, Cand, ResourceReduce))
3245 return;
3246 if (tryGreater(TryCand.ResDelta.DemandedResources,
3247 Cand.ResDelta.DemandedResources,
3248 TryCand, Cand, ResourceDemand))
3249 return;
3250
3251 // Avoid serializing long latency dependence chains.
3252 if (Cand.Policy.ReduceLatency && tryLatency(TryCand, Cand, Top)) {
3253 return;
3254 }
3255
3256 // Fall through to original instruction order.
3257 if (TryCand.SU->NodeNum < Cand.SU->NodeNum)
3258 TryCand.Reason = NodeOrder;
3259}
3260
3261void PostGenericScheduler::pickNodeFromQueue(SchedCandidate &Cand) {
3262 ReadyQueue &Q = Top.Available;
Andrew Trickd14d7c22013-12-28 21:56:57 +00003263 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
3264 SchedCandidate TryCand(Cand.Policy);
3265 TryCand.SU = *I;
Matthias Braun6ad3d052016-06-25 00:23:00 +00003266 TryCand.AtTop = true;
Andrew Trickd14d7c22013-12-28 21:56:57 +00003267 TryCand.initResourceDelta(DAG, SchedModel);
3268 tryCandidate(Cand, TryCand);
3269 if (TryCand.Reason != NoCand) {
3270 Cand.setBest(TryCand);
3271 DEBUG(traceCandidate(Cand));
3272 }
3273 }
3274}
3275
3276/// Pick the next node to schedule.
3277SUnit *PostGenericScheduler::pickNode(bool &IsTopNode) {
3278 if (DAG->top() == DAG->bottom()) {
3279 assert(Top.Available.empty() && Top.Pending.empty() && "ReadyQ garbage");
Craig Topperc0196b12014-04-14 00:51:57 +00003280 return nullptr;
Andrew Trickd14d7c22013-12-28 21:56:57 +00003281 }
3282 SUnit *SU;
3283 do {
3284 SU = Top.pickOnlyChoice();
Matthias Braun49cb6e92016-05-27 22:14:26 +00003285 if (SU) {
3286 tracePick(Only1, true);
3287 } else {
Andrew Trickd14d7c22013-12-28 21:56:57 +00003288 CandPolicy NoPolicy;
3289 SchedCandidate TopCand(NoPolicy);
3290 // Set the top-down policy based on the state of the current top zone and
3291 // the instructions outside the zone, including the bottom zone.
Craig Topperc0196b12014-04-14 00:51:57 +00003292 setPolicy(TopCand.Policy, /*IsPostRA=*/true, Top, nullptr);
Andrew Trickd14d7c22013-12-28 21:56:57 +00003293 pickNodeFromQueue(TopCand);
3294 assert(TopCand.Reason != NoCand && "failed to find a candidate");
Matthias Braun6ad3d052016-06-25 00:23:00 +00003295 tracePick(TopCand);
Andrew Trickd14d7c22013-12-28 21:56:57 +00003296 SU = TopCand.SU;
3297 }
3298 } while (SU->isScheduled);
3299
3300 IsTopNode = true;
3301 Top.removeReady(SU);
3302
3303 DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr());
3304 return SU;
3305}
3306
3307/// Called after ScheduleDAGMI has scheduled an instruction and updated
3308/// scheduled/remaining flags in the DAG nodes.
3309void PostGenericScheduler::schedNode(SUnit *SU, bool IsTopNode) {
3310 SU->TopReadyCycle = std::max(SU->TopReadyCycle, Top.getCurrCycle());
3311 Top.bumpNode(SU);
3312}
3313
Matthias Braun115efcd2016-11-28 20:11:54 +00003314ScheduleDAGMI *llvm::createGenericSchedPostRA(MachineSchedContext *C) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003315 return new ScheduleDAGMI(C, llvm::make_unique<PostGenericScheduler>(C),
Jonas Paulsson28f29482016-11-09 09:59:27 +00003316 /*RemoveKillFlags=*/true);
Andrew Trickd14d7c22013-12-28 21:56:57 +00003317}
Andrew Tricke1c034f2012-01-17 06:55:03 +00003318
3319//===----------------------------------------------------------------------===//
Andrew Trick90f711d2012-10-15 18:02:27 +00003320// ILP Scheduler. Currently for experimental analysis of heuristics.
3321//===----------------------------------------------------------------------===//
3322
3323namespace {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003324
Andrew Trick90f711d2012-10-15 18:02:27 +00003325/// \brief Order nodes by the ILP metric.
3326struct ILPOrder {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003327 const SchedDFSResult *DFSResult = nullptr;
3328 const BitVector *ScheduledTrees = nullptr;
Andrew Trick90f711d2012-10-15 18:02:27 +00003329 bool MaximizeILP;
3330
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003331 ILPOrder(bool MaxILP) : MaximizeILP(MaxILP) {}
Andrew Trick90f711d2012-10-15 18:02:27 +00003332
3333 /// \brief Apply a less-than relation on node priority.
Andrew Trick48d392e2012-11-28 05:13:28 +00003334 ///
3335 /// (Return true if A comes after B in the Q.)
Andrew Trick90f711d2012-10-15 18:02:27 +00003336 bool operator()(const SUnit *A, const SUnit *B) const {
Andrew Trick48d392e2012-11-28 05:13:28 +00003337 unsigned SchedTreeA = DFSResult->getSubtreeID(A);
3338 unsigned SchedTreeB = DFSResult->getSubtreeID(B);
3339 if (SchedTreeA != SchedTreeB) {
3340 // Unscheduled trees have lower priority.
3341 if (ScheduledTrees->test(SchedTreeA) != ScheduledTrees->test(SchedTreeB))
3342 return ScheduledTrees->test(SchedTreeB);
3343
3344 // Trees with shallower connections have have lower priority.
3345 if (DFSResult->getSubtreeLevel(SchedTreeA)
3346 != DFSResult->getSubtreeLevel(SchedTreeB)) {
3347 return DFSResult->getSubtreeLevel(SchedTreeA)
3348 < DFSResult->getSubtreeLevel(SchedTreeB);
3349 }
3350 }
Andrew Trick90f711d2012-10-15 18:02:27 +00003351 if (MaximizeILP)
Andrew Trick48d392e2012-11-28 05:13:28 +00003352 return DFSResult->getILP(A) < DFSResult->getILP(B);
Andrew Trick90f711d2012-10-15 18:02:27 +00003353 else
Andrew Trick48d392e2012-11-28 05:13:28 +00003354 return DFSResult->getILP(A) > DFSResult->getILP(B);
Andrew Trick90f711d2012-10-15 18:02:27 +00003355 }
3356};
3357
3358/// \brief Schedule based on the ILP metric.
3359class ILPScheduler : public MachineSchedStrategy {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003360 ScheduleDAGMILive *DAG = nullptr;
Andrew Trick90f711d2012-10-15 18:02:27 +00003361 ILPOrder Cmp;
3362
3363 std::vector<SUnit*> ReadyQ;
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003364
Andrew Trick90f711d2012-10-15 18:02:27 +00003365public:
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003366 ILPScheduler(bool MaximizeILP) : Cmp(MaximizeILP) {}
Andrew Trick90f711d2012-10-15 18:02:27 +00003367
Craig Topper4584cd52014-03-07 09:26:03 +00003368 void initialize(ScheduleDAGMI *dag) override {
Andrew Trickd7f890e2013-12-28 21:56:47 +00003369 assert(dag->hasVRegLiveness() && "ILPScheduler needs vreg liveness");
3370 DAG = static_cast<ScheduleDAGMILive*>(dag);
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00003371 DAG->computeDFSResult();
Andrew Trick44f750a2013-01-25 04:01:04 +00003372 Cmp.DFSResult = DAG->getDFSResult();
3373 Cmp.ScheduledTrees = &DAG->getScheduledTrees();
Andrew Trick90f711d2012-10-15 18:02:27 +00003374 ReadyQ.clear();
Andrew Trick90f711d2012-10-15 18:02:27 +00003375 }
3376
Craig Topper4584cd52014-03-07 09:26:03 +00003377 void registerRoots() override {
Benjamin Krameraa598b32012-11-29 14:36:26 +00003378 // Restore the heap in ReadyQ with the updated DFS results.
3379 std::make_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
Andrew Trick90f711d2012-10-15 18:02:27 +00003380 }
3381
3382 /// Implement MachineSchedStrategy interface.
3383 /// -----------------------------------------
3384
Andrew Trick48d392e2012-11-28 05:13:28 +00003385 /// Callback to select the highest priority node from the ready Q.
Craig Topper4584cd52014-03-07 09:26:03 +00003386 SUnit *pickNode(bool &IsTopNode) override {
Craig Topperc0196b12014-04-14 00:51:57 +00003387 if (ReadyQ.empty()) return nullptr;
Matt Arsenault4ab769f2013-03-21 00:57:21 +00003388 std::pop_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
Andrew Trick90f711d2012-10-15 18:02:27 +00003389 SUnit *SU = ReadyQ.back();
3390 ReadyQ.pop_back();
3391 IsTopNode = false;
Andrew Trick1f0bb692013-04-13 06:07:49 +00003392 DEBUG(dbgs() << "Pick node " << "SU(" << SU->NodeNum << ") "
Andrew Trick44f750a2013-01-25 04:01:04 +00003393 << " ILP: " << DAG->getDFSResult()->getILP(SU)
3394 << " Tree: " << DAG->getDFSResult()->getSubtreeID(SU) << " @"
3395 << DAG->getDFSResult()->getSubtreeLevel(
Andrew Trick1f0bb692013-04-13 06:07:49 +00003396 DAG->getDFSResult()->getSubtreeID(SU)) << '\n'
3397 << "Scheduling " << *SU->getInstr());
Andrew Trick90f711d2012-10-15 18:02:27 +00003398 return SU;
3399 }
3400
Andrew Trick44f750a2013-01-25 04:01:04 +00003401 /// \brief Scheduler callback to notify that a new subtree is scheduled.
Craig Topper4584cd52014-03-07 09:26:03 +00003402 void scheduleTree(unsigned SubtreeID) override {
Andrew Trick44f750a2013-01-25 04:01:04 +00003403 std::make_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
3404 }
3405
Andrew Trick48d392e2012-11-28 05:13:28 +00003406 /// Callback after a node is scheduled. Mark a newly scheduled tree, notify
3407 /// DFSResults, and resort the priority Q.
Craig Topper4584cd52014-03-07 09:26:03 +00003408 void schedNode(SUnit *SU, bool IsTopNode) override {
Andrew Trick48d392e2012-11-28 05:13:28 +00003409 assert(!IsTopNode && "SchedDFSResult needs bottom-up");
Andrew Trick48d392e2012-11-28 05:13:28 +00003410 }
Andrew Trick90f711d2012-10-15 18:02:27 +00003411
Craig Topper4584cd52014-03-07 09:26:03 +00003412 void releaseTopNode(SUnit *) override { /*only called for top roots*/ }
Andrew Trick90f711d2012-10-15 18:02:27 +00003413
Craig Topper4584cd52014-03-07 09:26:03 +00003414 void releaseBottomNode(SUnit *SU) override {
Andrew Trick90f711d2012-10-15 18:02:27 +00003415 ReadyQ.push_back(SU);
3416 std::push_heap(ReadyQ.begin(), ReadyQ.end(), Cmp);
3417 }
3418};
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003419
3420} // end anonymous namespace
Andrew Trick90f711d2012-10-15 18:02:27 +00003421
3422static ScheduleDAGInstrs *createILPMaxScheduler(MachineSchedContext *C) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003423 return new ScheduleDAGMILive(C, llvm::make_unique<ILPScheduler>(true));
Andrew Trick90f711d2012-10-15 18:02:27 +00003424}
3425static ScheduleDAGInstrs *createILPMinScheduler(MachineSchedContext *C) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003426 return new ScheduleDAGMILive(C, llvm::make_unique<ILPScheduler>(false));
Andrew Trick90f711d2012-10-15 18:02:27 +00003427}
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003428
Andrew Trick90f711d2012-10-15 18:02:27 +00003429static MachineSchedRegistry ILPMaxRegistry(
3430 "ilpmax", "Schedule bottom-up for max ILP", createILPMaxScheduler);
3431static MachineSchedRegistry ILPMinRegistry(
3432 "ilpmin", "Schedule bottom-up for min ILP", createILPMinScheduler);
3433
3434//===----------------------------------------------------------------------===//
Andrew Trick63440872012-01-14 02:17:06 +00003435// Machine Instruction Shuffler for Correctness Testing
3436//===----------------------------------------------------------------------===//
3437
Andrew Tricke77e84e2012-01-13 06:30:30 +00003438#ifndef NDEBUG
3439namespace {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003440
Andrew Trick8823dec2012-03-14 04:00:41 +00003441/// Apply a less-than relation on the node order, which corresponds to the
3442/// instruction order prior to scheduling. IsReverse implements greater-than.
3443template<bool IsReverse>
3444struct SUnitOrder {
Andrew Trick7ccdc5c2012-01-17 06:55:07 +00003445 bool operator()(SUnit *A, SUnit *B) const {
Andrew Trick8823dec2012-03-14 04:00:41 +00003446 if (IsReverse)
3447 return A->NodeNum > B->NodeNum;
3448 else
3449 return A->NodeNum < B->NodeNum;
Andrew Trick7ccdc5c2012-01-17 06:55:07 +00003450 }
3451};
3452
Andrew Tricke77e84e2012-01-13 06:30:30 +00003453/// Reorder instructions as much as possible.
Andrew Trick8823dec2012-03-14 04:00:41 +00003454class InstructionShuffler : public MachineSchedStrategy {
3455 bool IsAlternating;
3456 bool IsTopDown;
3457
3458 // Using a less-than relation (SUnitOrder<false>) for the TopQ priority
3459 // gives nodes with a higher number higher priority causing the latest
3460 // instructions to be scheduled first.
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003461 PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false>>
Andrew Trick8823dec2012-03-14 04:00:41 +00003462 TopQ;
3463 // When scheduling bottom-up, use greater-than as the queue priority.
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003464 PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true>>
Andrew Trick8823dec2012-03-14 04:00:41 +00003465 BottomQ;
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003466
Andrew Tricke77e84e2012-01-13 06:30:30 +00003467public:
Andrew Trick8823dec2012-03-14 04:00:41 +00003468 InstructionShuffler(bool alternate, bool topdown)
3469 : IsAlternating(alternate), IsTopDown(topdown) {}
Andrew Tricke77e84e2012-01-13 06:30:30 +00003470
Craig Topper9d74a5a2014-04-29 07:58:41 +00003471 void initialize(ScheduleDAGMI*) override {
Andrew Trick8823dec2012-03-14 04:00:41 +00003472 TopQ.clear();
3473 BottomQ.clear();
3474 }
Andrew Trick7ccdc5c2012-01-17 06:55:07 +00003475
Andrew Trick8823dec2012-03-14 04:00:41 +00003476 /// Implement MachineSchedStrategy interface.
3477 /// -----------------------------------------
3478
Craig Topper9d74a5a2014-04-29 07:58:41 +00003479 SUnit *pickNode(bool &IsTopNode) override {
Andrew Trick8823dec2012-03-14 04:00:41 +00003480 SUnit *SU;
3481 if (IsTopDown) {
3482 do {
Craig Topperc0196b12014-04-14 00:51:57 +00003483 if (TopQ.empty()) return nullptr;
Andrew Trick8823dec2012-03-14 04:00:41 +00003484 SU = TopQ.top();
3485 TopQ.pop();
3486 } while (SU->isScheduled);
3487 IsTopNode = true;
Matthias Braunb550b762016-04-21 01:54:13 +00003488 } else {
Andrew Trick8823dec2012-03-14 04:00:41 +00003489 do {
Craig Topperc0196b12014-04-14 00:51:57 +00003490 if (BottomQ.empty()) return nullptr;
Andrew Trick8823dec2012-03-14 04:00:41 +00003491 SU = BottomQ.top();
3492 BottomQ.pop();
3493 } while (SU->isScheduled);
3494 IsTopNode = false;
3495 }
3496 if (IsAlternating)
3497 IsTopDown = !IsTopDown;
Andrew Trick7ccdc5c2012-01-17 06:55:07 +00003498 return SU;
3499 }
3500
Craig Topper9d74a5a2014-04-29 07:58:41 +00003501 void schedNode(SUnit *SU, bool IsTopNode) override {}
Andrew Trick61f1a272012-05-24 22:11:09 +00003502
Craig Topper9d74a5a2014-04-29 07:58:41 +00003503 void releaseTopNode(SUnit *SU) override {
Andrew Trick8823dec2012-03-14 04:00:41 +00003504 TopQ.push(SU);
3505 }
Craig Topper9d74a5a2014-04-29 07:58:41 +00003506 void releaseBottomNode(SUnit *SU) override {
Andrew Trick8823dec2012-03-14 04:00:41 +00003507 BottomQ.push(SU);
Andrew Tricke77e84e2012-01-13 06:30:30 +00003508 }
3509};
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003510
3511} // end anonymous namespace
Andrew Tricke77e84e2012-01-13 06:30:30 +00003512
Andrew Trick02a80da2012-03-08 01:41:12 +00003513static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) {
Andrew Trick8823dec2012-03-14 04:00:41 +00003514 bool Alternate = !ForceTopDown && !ForceBottomUp;
3515 bool TopDown = !ForceBottomUp;
Benjamin Kramer05e7a842012-03-14 11:26:37 +00003516 assert((TopDown || !ForceTopDown) &&
Andrew Trick8823dec2012-03-14 04:00:41 +00003517 "-misched-topdown incompatible with -misched-bottomup");
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003518 return new ScheduleDAGMILive(
3519 C, llvm::make_unique<InstructionShuffler>(Alternate, TopDown));
Andrew Tricke77e84e2012-01-13 06:30:30 +00003520}
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003521
Andrew Trick8823dec2012-03-14 04:00:41 +00003522static MachineSchedRegistry ShufflerRegistry(
3523 "shuffle", "Shuffle machine instructions alternating directions",
3524 createInstructionShuffler);
Andrew Tricke77e84e2012-01-13 06:30:30 +00003525#endif // !NDEBUG
Andrew Trickea9fd952013-01-25 07:45:29 +00003526
3527//===----------------------------------------------------------------------===//
Andrew Trickd7f890e2013-12-28 21:56:47 +00003528// GraphWriter support for ScheduleDAGMILive.
Andrew Trickea9fd952013-01-25 07:45:29 +00003529//===----------------------------------------------------------------------===//
3530
3531#ifndef NDEBUG
3532namespace llvm {
3533
3534template<> struct GraphTraits<
3535 ScheduleDAGMI*> : public GraphTraits<ScheduleDAG*> {};
3536
3537template<>
3538struct DOTGraphTraits<ScheduleDAGMI*> : public DefaultDOTGraphTraits {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003539 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
Andrew Trickea9fd952013-01-25 07:45:29 +00003540
3541 static std::string getGraphName(const ScheduleDAG *G) {
3542 return G->MF.getName();
3543 }
3544
3545 static bool renderGraphFromBottomUp() {
3546 return true;
3547 }
3548
3549 static bool isNodeHidden(const SUnit *Node) {
Matthias Braund78ee542015-09-17 21:09:59 +00003550 if (ViewMISchedCutoff == 0)
3551 return false;
3552 return (Node->Preds.size() > ViewMISchedCutoff
3553 || Node->Succs.size() > ViewMISchedCutoff);
Andrew Trickea9fd952013-01-25 07:45:29 +00003554 }
3555
Andrew Trickea9fd952013-01-25 07:45:29 +00003556 /// If you want to override the dot attributes printed for a particular
3557 /// edge, override this method.
3558 static std::string getEdgeAttributes(const SUnit *Node,
3559 SUnitIterator EI,
3560 const ScheduleDAG *Graph) {
3561 if (EI.isArtificialDep())
3562 return "color=cyan,style=dashed";
3563 if (EI.isCtrlDep())
3564 return "color=blue,style=dashed";
3565 return "";
3566 }
3567
3568 static std::string getNodeLabel(const SUnit *SU, const ScheduleDAG *G) {
Alp Tokere69170a2014-06-26 22:52:05 +00003569 std::string Str;
3570 raw_string_ostream SS(Str);
Andrew Trickd7f890e2013-12-28 21:56:47 +00003571 const ScheduleDAGMI *DAG = static_cast<const ScheduleDAGMI*>(G);
3572 const SchedDFSResult *DFS = DAG->hasVRegLiveness() ?
Craig Topperc0196b12014-04-14 00:51:57 +00003573 static_cast<const ScheduleDAGMILive*>(G)->getDFSResult() : nullptr;
Andrew Trick7609b7d2013-09-06 17:32:42 +00003574 SS << "SU:" << SU->NodeNum;
3575 if (DFS)
3576 SS << " I:" << DFS->getNumInstrs(SU);
Andrew Trickea9fd952013-01-25 07:45:29 +00003577 return SS.str();
3578 }
3579 static std::string getNodeDescription(const SUnit *SU, const ScheduleDAG *G) {
3580 return G->getGraphNodeLabel(SU);
3581 }
3582
Andrew Trickd7f890e2013-12-28 21:56:47 +00003583 static std::string getNodeAttributes(const SUnit *N, const ScheduleDAG *G) {
Andrew Trickea9fd952013-01-25 07:45:29 +00003584 std::string Str("shape=Mrecord");
Andrew Trickd7f890e2013-12-28 21:56:47 +00003585 const ScheduleDAGMI *DAG = static_cast<const ScheduleDAGMI*>(G);
3586 const SchedDFSResult *DFS = DAG->hasVRegLiveness() ?
Craig Topperc0196b12014-04-14 00:51:57 +00003587 static_cast<const ScheduleDAGMILive*>(G)->getDFSResult() : nullptr;
Andrew Trickea9fd952013-01-25 07:45:29 +00003588 if (DFS) {
3589 Str += ",style=filled,fillcolor=\"#";
3590 Str += DOT::getColorString(DFS->getSubtreeID(N));
3591 Str += '"';
3592 }
3593 return Str;
3594 }
3595};
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00003596
3597} // end namespace llvm
Andrew Trickea9fd952013-01-25 07:45:29 +00003598#endif // NDEBUG
3599
3600/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
3601/// rendered using 'dot'.
3602///
3603void ScheduleDAGMI::viewGraph(const Twine &Name, const Twine &Title) {
3604#ifndef NDEBUG
3605 ViewGraph(this, Name, false, Title);
3606#else
3607 errs() << "ScheduleDAGMI::viewGraph is only available in debug builds on "
3608 << "systems with Graphviz or gv!\n";
3609#endif // NDEBUG
3610}
3611
3612/// Out-of-line implementation with no arguments is handy for gdb.
3613void ScheduleDAGMI::viewGraph() {
3614 viewGraph(getDAGName(), "Scheduling-Units Graph for " + getDAGName());
3615}