blob: 5d86faafdd85f4d4df1eda5aa3350f8625804d63 [file] [log] [blame]
Dale Johannesen4dc35db2007-07-13 17:31:29 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
Dale Johannesen2182f062007-07-13 17:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dale Johannesen2182f062007-07-13 17:13:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements a top-down list scheduler, using standard algorithms.
11// The basic approach uses a priority queue of available nodes to schedule.
12// One at a time, nodes are taken from the priority queue (thus in priority
13// order), checked for legality to schedule, and emitted if legal.
14//
15// Nodes may not be legal to schedule either due to structural hazards (e.g.
16// pipeline or resource constraints) or because an input to the instruction has
17// not completed execution.
18//
19//===----------------------------------------------------------------------===//
20
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "AggressiveAntiDepBreaker.h"
22#include "AntiDepBreaker.h"
23#include "CriticalAntiDepBreaker.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000026#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000027#include "llvm/CodeGen/MachineDominators.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000029#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanad2134d2008-11-25 00:52:40 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000031#include "llvm/CodeGen/Passes.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000032#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Trick9a0c5832012-03-07 23:01:06 +000033#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohmanceac7c32009-01-16 01:33:36 +000034#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/CodeGen/SchedulerRegistry.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000036#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000037#include "llvm/CodeGen/TargetLowering.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000038#include "llvm/CodeGen/TargetPassConfig.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Goodwine056d102009-10-26 22:31:16 +000041#include "llvm/Support/CommandLine.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000042#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000043#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000044#include "llvm/Support/raw_ostream.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000045using namespace llvm;
46
Chandler Carruth1b9dde02014-04-22 02:02:50 +000047#define DEBUG_TYPE "post-RA-sched"
48
Dan Gohmanceac7c32009-01-16 01:33:36 +000049STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman60cb69e2008-11-19 23:18:57 +000050STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin83704852009-10-26 16:59:04 +000051STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman60cb69e2008-11-19 23:18:57 +000052
David Goodwin9a051a52009-10-01 21:46:35 +000053// Post-RA scheduling is enabled with
Evan Cheng0d639a22011-07-01 21:01:15 +000054// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin9a051a52009-10-01 21:46:35 +000055// override the target.
56static cl::opt<bool>
57EnablePostRAScheduler("post-RA-scheduler",
58 cl::desc("Enable scheduling after register allocation"),
David Goodwin1cc6dd92009-10-01 22:19:57 +000059 cl::init(false), cl::Hidden);
David Goodwin83704852009-10-26 16:59:04 +000060static cl::opt<std::string>
Dan Gohmanad2134d2008-11-25 00:52:40 +000061EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin83704852009-10-26 16:59:04 +000062 cl::desc("Break post-RA scheduling anti-dependencies: "
63 "\"critical\", \"all\", or \"none\""),
64 cl::init("none"), cl::Hidden);
Dan Gohmanceac7c32009-01-16 01:33:36 +000065
David Goodwin7f651692009-09-01 18:34:03 +000066// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
67static cl::opt<int>
68DebugDiv("postra-sched-debugdiv",
69 cl::desc("Debug control MBBs that are scheduled"),
70 cl::init(0), cl::Hidden);
71static cl::opt<int>
72DebugMod("postra-sched-debugmod",
73 cl::desc("Debug control MBBs that are scheduled"),
74 cl::init(0), cl::Hidden);
75
David Goodwin661ea982009-10-26 19:41:00 +000076AntiDepBreaker::~AntiDepBreaker() { }
77
Dale Johannesen2182f062007-07-13 17:13:54 +000078namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000079 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng2d51c7c2010-06-18 23:09:54 +000080 const TargetInstrInfo *TII;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000081 RegisterClassInfo RegClassInfo;
Dan Gohman87b02d52009-10-09 23:27:56 +000082
Dale Johannesen2182f062007-07-13 17:13:54 +000083 public:
84 static char ID;
Andrew Trickdf7e3762012-02-08 21:22:53 +000085 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohmanad2134d2008-11-25 00:52:40 +000086
Craig Topper4584cd52014-03-07 09:26:03 +000087 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000088 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +000089 AU.addRequired<AAResultsWrapperPass>();
Andrew Trickdf7e3762012-02-08 21:22:53 +000090 AU.addRequired<TargetPassConfig>();
Dan Gohmandddc1ac2008-12-16 03:25:46 +000091 AU.addRequired<MachineDominatorTree>();
92 AU.addPreserved<MachineDominatorTree>();
93 AU.addRequired<MachineLoopInfo>();
94 AU.addPreserved<MachineLoopInfo>();
95 MachineFunctionPass::getAnalysisUsage(AU);
96 }
97
Derek Schuffad154c82016-03-28 17:05:30 +000098 MachineFunctionProperties getRequiredProperties() const override {
99 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000100 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000101 }
102
Craig Topper4584cd52014-03-07 09:26:03 +0000103 bool runOnMachineFunction(MachineFunction &Fn) override;
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000104
Mitch Bodart64535012016-05-19 16:40:49 +0000105 private:
Sanjay Patela2f658d2014-07-15 22:39:58 +0000106 bool enablePostRAScheduler(
107 const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel,
108 TargetSubtargetInfo::AntiDepBreakMode &Mode,
109 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const;
Dale Johannesen2182f062007-07-13 17:13:54 +0000110 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000111 char PostRAScheduler::ID = 0;
112
Nick Lewycky02d5f772009-10-25 06:33:48 +0000113 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000114 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohman682a2d12009-10-21 01:44:44 +0000115 ///
Dan Gohman60cb69e2008-11-19 23:18:57 +0000116 LatencyPriorityQueue AvailableQueue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000117
Dan Gohman60cb69e2008-11-19 23:18:57 +0000118 /// PendingQueue - This contains all of the instructions whose operands have
119 /// been issued, but their results are not ready yet (due to the latency of
120 /// the operation). Once the operands becomes available, the instruction is
121 /// added to the AvailableQueue.
122 std::vector<SUnit*> PendingQueue;
123
Dan Gohmanceac7c32009-01-16 01:33:36 +0000124 /// HazardRec - The hazard recognizer to use.
125 ScheduleHazardRecognizer *HazardRec;
126
David Goodwin83704852009-10-26 16:59:04 +0000127 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
128 AntiDepBreaker *AntiDepBreak;
129
Dan Gohman87b02d52009-10-09 23:27:56 +0000130 /// AA - AliasAnalysis for making memory reference queries.
131 AliasAnalysis *AA;
132
Andrew Trick60cf03e2012-03-07 05:21:52 +0000133 /// The schedule. Null SUnit*'s represent noop instructions.
134 std::vector<SUnit*> Sequence;
135
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000136 /// Ordered list of DAG postprocessing steps.
137 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
138
Andrew Tricka53e1012013-08-23 17:48:33 +0000139 /// The index in BB of RegionEnd.
140 ///
141 /// This is the instruction number from the top of the current block, not
142 /// the SlotIndex. It is only used by the AntiDepBreaker.
143 unsigned EndIndex;
144
Dan Gohmanad2134d2008-11-25 00:52:40 +0000145 public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000146 SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000147 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
148 const RegisterClassInfo &,
149 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
150 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000151
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000152 ~SchedulePostRATDList() override;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000153
Andrew Trick52226d42012-03-07 23:00:49 +0000154 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohmanb9543432009-02-10 23:27:53 +0000155 /// this block.
156 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000157 void startBlock(MachineBasicBlock *BB) override;
Dan Gohmanb9543432009-02-10 23:27:53 +0000158
Andrew Tricka53e1012013-08-23 17:48:33 +0000159 // Set the index of RegionEnd within the current BB.
160 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
161
Andrew Trick60cf03e2012-03-07 05:21:52 +0000162 /// Initialize the scheduler state for the next scheduling region.
Craig Topper4584cd52014-03-07 09:26:03 +0000163 void enterRegion(MachineBasicBlock *bb,
164 MachineBasicBlock::iterator begin,
165 MachineBasicBlock::iterator end,
166 unsigned regioninstrs) override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000167
168 /// Notify that the scheduler has finished scheduling the current region.
Craig Topper4584cd52014-03-07 09:26:03 +0000169 void exitRegion() override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000170
Dan Gohmanb9543432009-02-10 23:27:53 +0000171 /// Schedule - Schedule the instruction range using list scheduling.
172 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000173 void schedule() override;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000174
Andrew Tricke932bb72012-03-07 05:21:44 +0000175 void EmitSchedule();
176
Dan Gohman682a2d12009-10-21 01:44:44 +0000177 /// Observe - Update liveness information to account for the current
178 /// instruction, which will not be scheduled.
179 ///
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000180 void Observe(MachineInstr &MI, unsigned Count);
Dan Gohman682a2d12009-10-21 01:44:44 +0000181
Andrew Trick52226d42012-03-07 23:00:49 +0000182 /// finishBlock - Clean up register live-range state.
Dan Gohman682a2d12009-10-21 01:44:44 +0000183 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000184 void finishBlock() override;
Dan Gohman682a2d12009-10-21 01:44:44 +0000185
Dan Gohman60cb69e2008-11-19 23:18:57 +0000186 private:
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000187 /// Apply each ScheduleDAGMutation step in order.
188 void postprocessDAG();
189
David Goodwin80a03cc2009-11-20 19:32:48 +0000190 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
191 void ReleaseSuccessors(SUnit *SU);
192 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
193 void ListScheduleTopDown();
Jim Grosbachd772bde2010-05-14 21:19:48 +0000194
Andrew Trickedee68c2012-03-07 05:21:40 +0000195 void dumpSchedule() const;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000196 void emitNoop(unsigned CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000197 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000198}
Dale Johannesen2182f062007-07-13 17:13:54 +0000199
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000200char &llvm::PostRASchedulerID = PostRAScheduler::ID;
201
Matthias Braun1527baa2017-05-25 21:26:32 +0000202INITIALIZE_PASS(PostRAScheduler, DEBUG_TYPE,
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000203 "Post RA top-down list latency scheduler", false, false)
204
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000205SchedulePostRATDList::SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000206 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
207 const RegisterClassInfo &RCI,
208 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
209 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs)
Matthias Braun93563e72015-11-03 01:53:29 +0000210 : ScheduleDAGInstrs(MF, &MLI), AA(AA), EndIndex(0) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000211
Eric Christopherd9134482014-08-04 21:25:23 +0000212 const InstrItineraryData *InstrItins =
Eric Christopherb66367a2014-10-14 07:17:23 +0000213 MF.getSubtarget().getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000214 HazardRec =
Eric Christopherb66367a2014-10-14 07:17:23 +0000215 MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +0000216 InstrItins, this);
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000217 MF.getSubtarget().getPostRAMutations(Mutations);
Preston Gurd9a091472012-04-23 21:39:35 +0000218
219 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
220 MRI.tracksLiveness()) &&
221 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000222 AntiDepBreak =
Evan Cheng0d639a22011-07-01 21:01:15 +0000223 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000224 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng0d639a22011-07-01 21:01:15 +0000225 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Craig Topperc0196b12014-04-14 00:51:57 +0000226 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000227}
228
229SchedulePostRATDList::~SchedulePostRATDList() {
230 delete HazardRec;
231 delete AntiDepBreak;
232}
233
Andrew Trick60cf03e2012-03-07 05:21:52 +0000234/// Initialize state associated with the next scheduling region.
235void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
236 MachineBasicBlock::iterator begin,
237 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000238 unsigned regioninstrs) {
239 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000240 Sequence.clear();
241}
242
243/// Print the schedule before exiting the region.
244void SchedulePostRATDList::exitRegion() {
245 DEBUG({
246 dbgs() << "*** Final schedule ***\n";
247 dumpSchedule();
248 dbgs() << '\n';
249 });
250 ScheduleDAGInstrs::exitRegion();
251}
252
Aaron Ballman615eb472017-10-15 14:32:27 +0000253#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickedee68c2012-03-07 05:21:40 +0000254/// dumpSchedule - dump the scheduled Sequence.
Matthias Braun8c209aa2017-01-28 02:02:38 +0000255LLVM_DUMP_METHOD void SchedulePostRATDList::dumpSchedule() const {
Andrew Trickedee68c2012-03-07 05:21:40 +0000256 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
257 if (SUnit *SU = Sequence[i])
258 SU->dump(this);
259 else
260 dbgs() << "**** NOOP ****\n";
261 }
262}
Manman Ren742534c2012-09-06 19:06:06 +0000263#endif
Andrew Trickedee68c2012-03-07 05:21:40 +0000264
Sanjay Patela2f658d2014-07-15 22:39:58 +0000265bool PostRAScheduler::enablePostRAScheduler(
266 const TargetSubtargetInfo &ST,
267 CodeGenOpt::Level OptLevel,
268 TargetSubtargetInfo::AntiDepBreakMode &Mode,
269 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const {
270 Mode = ST.getAntiDepBreakMode();
271 ST.getCriticalPathRCs(CriticalPathRCs);
Mitch Bodart64535012016-05-19 16:40:49 +0000272
273 // Check for explicit enable/disable of post-ra scheduling.
274 if (EnablePostRAScheduler.getPosition() > 0)
275 return EnablePostRAScheduler;
276
Matthias Braun39a2afc2015-06-13 03:42:16 +0000277 return ST.enablePostRAScheduler() &&
Sanjay Patela2f658d2014-07-15 22:39:58 +0000278 OptLevel >= ST.getOptLevelToEnablePostRAScheduler();
279}
280
Dan Gohman60cb69e2008-11-19 23:18:57 +0000281bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000282 if (skipFunction(Fn.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000283 return false;
284
Eric Christopherfc6de422014-08-05 02:39:49 +0000285 TII = Fn.getSubtarget().getInstrInfo();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000286 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000287 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000288 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
289
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000290 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman26e9b892009-10-10 00:15:38 +0000291
Evan Cheng7fae11b2011-12-14 02:11:42 +0000292 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
293 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper760b1342012-02-22 05:59:10 +0000294 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
Mitch Bodart64535012016-05-19 16:40:49 +0000295
296 // Check that post-RA scheduling is enabled for this target.
297 // This may upgrade the AntiDepMode.
298 if (!enablePostRAScheduler(Fn.getSubtarget(), PassConfig->getOptLevel(),
299 AntiDepMode, CriticalPathRCs))
300 return false;
David Goodwin17199b52009-09-30 00:10:16 +0000301
David Goodwin02ad4cb2009-10-22 23:19:17 +0000302 // Check for antidep breaking override...
303 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng0d639a22011-07-01 21:01:15 +0000304 AntiDepMode = (EnableAntiDepBreaking == "all")
305 ? TargetSubtargetInfo::ANTIDEP_ALL
306 : ((EnableAntiDepBreaking == "critical")
307 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
308 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin02ad4cb2009-10-22 23:19:17 +0000309 }
310
David Greeneaa8ce382010-01-05 01:26:01 +0000311 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesen2182f062007-07-13 17:13:54 +0000312
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000313 SchedulePostRATDList Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode,
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000314 CriticalPathRCs);
Dan Gohman619ef482009-01-15 19:20:50 +0000315
Dale Johannesen2182f062007-07-13 17:13:54 +0000316 // Loop over all of the basic blocks
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000317 for (auto &MBB : Fn) {
David Goodwin7f651692009-09-01 18:34:03 +0000318#ifndef NDEBUG
319 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
320 if (DebugDiv > 0) {
321 static int bbcnt = 0;
322 if (bbcnt++ % DebugDiv != DebugMod)
323 continue;
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000324 dbgs() << "*** DEBUG scheduling " << Fn.getName() << ":"
325 << printMBBReference(MBB) << " ***\n";
David Goodwin7f651692009-09-01 18:34:03 +0000326 }
327#endif
328
Dan Gohmanb9543432009-02-10 23:27:53 +0000329 // Initialize register live-range state for scheduling in this block.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000330 Scheduler.startBlock(&MBB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000331
Dan Gohman5f8a2592009-01-16 22:10:20 +0000332 // Schedule each sequence of instructions not interrupted by a label
333 // or anything else that effectively needs to shut down scheduling.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000334 MachineBasicBlock::iterator Current = MBB.end();
335 unsigned Count = MBB.size(), CurrentCount = Count;
336 for (MachineBasicBlock::iterator I = Current; I != MBB.begin();) {
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000337 MachineInstr &MI = *std::prev(I);
Andrew Tricka53e1012013-08-23 17:48:33 +0000338 --Count;
Jakob Stoklund Olesena793a592012-02-23 17:54:21 +0000339 // Calls are not scheduling boundaries before register allocation, but
340 // post-ra we don't gain anything by scheduling across calls since we
341 // don't need to worry about register pressure.
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000342 if (MI.isCall() || TII->isSchedulingBoundary(MI, &MBB, Fn)) {
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000343 Scheduler.enterRegion(&MBB, I, Current, CurrentCount - Count);
Andrew Tricka53e1012013-08-23 17:48:33 +0000344 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000345 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000346 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000347 Scheduler.EmitSchedule();
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000348 Current = &MI;
Andrew Tricka53e1012013-08-23 17:48:33 +0000349 CurrentCount = Count;
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000350 Scheduler.Observe(MI, CurrentCount);
Dan Gohman5f8a2592009-01-16 22:10:20 +0000351 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000352 I = MI;
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000353 if (MI.isBundle())
354 Count -= MI.getBundleSize();
Dan Gohmand5643532009-02-03 18:57:45 +0000355 }
Dan Gohmandfaf6462009-02-11 04:27:20 +0000356 assert(Count == 0 && "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000357 assert((MBB.begin() == Current || CurrentCount != 0) &&
Dan Gohman64613ac2009-03-10 18:10:43 +0000358 "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000359 Scheduler.enterRegion(&MBB, MBB.begin(), Current, CurrentCount);
Andrew Tricka53e1012013-08-23 17:48:33 +0000360 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000361 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000362 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000363 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000364
365 // Clean up register live-range state.
Andrew Trick52226d42012-03-07 23:00:49 +0000366 Scheduler.finishBlock();
David Goodwinae6bc822009-08-25 17:03:05 +0000367
David Goodwin6c08cfc2009-09-03 22:15:25 +0000368 // Update register kills
Matthias Braun868bbd42017-05-27 02:50:50 +0000369 Scheduler.fixupKills(MBB);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000370 }
Dale Johannesen2182f062007-07-13 17:13:54 +0000371
372 return true;
373}
Jim Grosbachd772bde2010-05-14 21:19:48 +0000374
Dan Gohmanb9543432009-02-10 23:27:53 +0000375/// StartBlock - Initialize register live-range state for scheduling in
376/// this block.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000377///
Andrew Trick52226d42012-03-07 23:00:49 +0000378void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000379 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000380 ScheduleDAGInstrs::startBlock(BB);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000381
David Goodwin83704852009-10-26 16:59:04 +0000382 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin6021b4d2009-08-10 15:55:25 +0000383 HazardRec->Reset();
Craig Topperc0196b12014-04-14 00:51:57 +0000384 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000385 AntiDepBreak->StartBlock(BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000386}
387
388/// Schedule - Schedule the instruction range using list scheduling.
389///
Andrew Trick52226d42012-03-07 23:00:49 +0000390void SchedulePostRATDList::schedule() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000391 // Build the scheduling graph.
Andrew Trick52226d42012-03-07 23:00:49 +0000392 buildSchedGraph(AA);
Dan Gohmanb9543432009-02-10 23:27:53 +0000393
Craig Topperc0196b12014-04-14 00:51:57 +0000394 if (AntiDepBreak) {
Jim Grosbachd772bde2010-05-14 21:19:48 +0000395 unsigned Broken =
Andrew Trick8c207e42012-03-09 04:29:02 +0000396 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
397 EndIndex, DbgValues);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000398
David Goodwin80a03cc2009-11-20 19:32:48 +0000399 if (Broken != 0) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000400 // We made changes. Update the dependency graph.
401 // Theoretically we could update the graph in place:
402 // When a live range is changed to use a different register, remove
403 // the def's anti-dependence *and* output-dependence edges due to
404 // that register, and add new anti-dependence and output-dependence
405 // edges based on the next live range of the register.
Andrew Trick60cf03e2012-03-07 05:21:52 +0000406 ScheduleDAG::clearDAG();
Andrew Trick52226d42012-03-07 23:00:49 +0000407 buildSchedGraph(AA);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000408
David Goodwin83704852009-10-26 16:59:04 +0000409 NumFixedAnti += Broken;
Dan Gohmanb9543432009-02-10 23:27:53 +0000410 }
411 }
412
Krzysztof Parzyszekcd99e362016-03-08 16:54:20 +0000413 postprocessDAG();
414
David Greeneaa8ce382010-01-05 01:26:01 +0000415 DEBUG(dbgs() << "********** List Scheduling **********\n");
Matthias Braun9198c672015-11-06 20:59:02 +0000416 DEBUG(
417 for (const SUnit &SU : SUnits) {
418 SU.dumpAll(this);
419 dbgs() << '\n';
420 }
421 );
David Goodwin6021b4d2009-08-10 15:55:25 +0000422
Dan Gohmanb9543432009-02-10 23:27:53 +0000423 AvailableQueue.initNodes(SUnits);
David Goodwin80a03cc2009-11-20 19:32:48 +0000424 ListScheduleTopDown();
Dan Gohmanb9543432009-02-10 23:27:53 +0000425 AvailableQueue.releaseState();
426}
427
428/// Observe - Update liveness information to account for the current
429/// instruction, which will not be scheduled.
430///
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000431void SchedulePostRATDList::Observe(MachineInstr &MI, unsigned Count) {
Craig Topperc0196b12014-04-14 00:51:57 +0000432 if (AntiDepBreak)
Andrew Tricka316faa2012-03-07 23:00:52 +0000433 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohmanb9543432009-02-10 23:27:53 +0000434}
435
436/// FinishBlock - Clean up register live-range state.
437///
Andrew Trick52226d42012-03-07 23:00:49 +0000438void SchedulePostRATDList::finishBlock() {
Craig Topperc0196b12014-04-14 00:51:57 +0000439 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000440 AntiDepBreak->FinishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000441
442 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000443 ScheduleDAGInstrs::finishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000444}
445
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000446/// Apply each ScheduleDAGMutation step in order.
447void SchedulePostRATDList::postprocessDAG() {
448 for (auto &M : Mutations)
449 M->apply(this);
450}
451
Dan Gohman60cb69e2008-11-19 23:18:57 +0000452//===----------------------------------------------------------------------===//
453// Top-Down Scheduling
454//===----------------------------------------------------------------------===//
455
456/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000457/// the PendingQueue if the count reaches zero.
David Goodwin80a03cc2009-11-20 19:32:48 +0000458void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000459 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000460
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000461 if (SuccEdge->isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000462 --SuccSU->WeakPredsLeft;
463 return;
464 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000465#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000466 if (SuccSU->NumPredsLeft == 0) {
David Greeneaa8ce382010-01-05 01:26:01 +0000467 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000468 SuccSU->dump(this);
David Greeneaa8ce382010-01-05 01:26:01 +0000469 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000470 llvm_unreachable(nullptr);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000471 }
472#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000473 --SuccSU->NumPredsLeft;
474
Andrew Trick84f9ad92011-05-06 18:14:32 +0000475 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trickaab77fe2011-05-06 17:09:08 +0000476 // here as such:
477 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
478 //
479 // However, we lazily compute node depth instead. Note that
480 // ScheduleNodeTopDown has already updated the depth of this node which causes
481 // all descendents to be marked dirty. Setting the successor depth explicitly
482 // here would cause depth to be recomputed for all its ancestors. If the
483 // successor is not yet ready (because of a transitively redundant edge) then
484 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbachd772bde2010-05-14 21:19:48 +0000485
Dan Gohmanb9543432009-02-10 23:27:53 +0000486 // If all the node's predecessors are scheduled, this node is ready
487 // to be scheduled. Ignore the special ExitSU node.
488 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman60cb69e2008-11-19 23:18:57 +0000489 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000490}
491
492/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin80a03cc2009-11-20 19:32:48 +0000493void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000494 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +0000495 I != E; ++I) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000496 ReleaseSucc(SU, &*I);
David Goodwin8501dbbe2009-11-03 20:57:50 +0000497 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000498}
499
500/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
501/// count of its successors. If a successor pending count is zero, add it to
502/// the Available queue.
David Goodwin80a03cc2009-11-20 19:32:48 +0000503void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greeneaa8ce382010-01-05 01:26:01 +0000504 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000505 DEBUG(SU->dump(this));
Jim Grosbachd772bde2010-05-14 21:19:48 +0000506
Dan Gohman60cb69e2008-11-19 23:18:57 +0000507 Sequence.push_back(SU);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000508 assert(CurCycle >= SU->getDepth() &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000509 "Node scheduled above its depth!");
David Goodwin80a03cc2009-11-20 19:32:48 +0000510 SU->setDepthToAtLeast(CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000511
David Goodwin80a03cc2009-11-20 19:32:48 +0000512 ReleaseSuccessors(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000513 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000514 AvailableQueue.scheduledNode(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000515}
516
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000517/// emitNoop - Add a noop to the current instruction sequence.
518void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
519 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
520 HazardRec->EmitNoop();
Craig Topperc0196b12014-04-14 00:51:57 +0000521 Sequence.push_back(nullptr); // NULL here means noop
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000522 ++NumNoops;
523}
524
Dan Gohman60cb69e2008-11-19 23:18:57 +0000525/// ListScheduleTopDown - The main loop of list scheduling for top-down
526/// schedulers.
David Goodwin80a03cc2009-11-20 19:32:48 +0000527void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000528 unsigned CurCycle = 0;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000529
David Goodwin8501dbbe2009-11-03 20:57:50 +0000530 // We're scheduling top-down but we're visiting the regions in
531 // bottom-up order, so we don't know the hazards at the start of a
532 // region. So assume no hazards (this should usually be ok as most
533 // blocks are a single region).
534 HazardRec->Reset();
535
Dan Gohmanb9543432009-02-10 23:27:53 +0000536 // Release any successors of the special Entry node.
David Goodwin80a03cc2009-11-20 19:32:48 +0000537 ReleaseSuccessors(&EntrySU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000538
David Goodwin80a03cc2009-11-20 19:32:48 +0000539 // Add all leaves to Available queue.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000540 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
541 // It is available if it has no predecessors.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000542 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000543 AvailableQueue.push(&SUnits[i]);
544 SUnits[i].isAvailable = true;
545 }
546 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000547
David Goodwin1f8c7a72009-08-12 21:47:46 +0000548 // In any cycle where we can't schedule any instructions, we must
549 // stall or emit a noop, depending on the target.
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000550 bool CycleHasInsts = false;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000551
Dan Gohman60cb69e2008-11-19 23:18:57 +0000552 // While Available queue is not empty, grab the node with the highest
553 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmanceac7c32009-01-16 01:33:36 +0000554 std::vector<SUnit*> NotReady;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000555 Sequence.reserve(SUnits.size());
556 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
557 // Check to see if any of the pending instructions are ready to issue. If
558 // so, add them to the available queue.
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000559 unsigned MinDepth = ~0u;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000560 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000561 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000562 AvailableQueue.push(PendingQueue[i]);
563 PendingQueue[i]->isAvailable = true;
564 PendingQueue[i] = PendingQueue.back();
565 PendingQueue.pop_back();
566 --i; --e;
David Goodwin80a03cc2009-11-20 19:32:48 +0000567 } else if (PendingQueue[i]->getDepth() < MinDepth)
568 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000569 }
David Goodwinebd694b2009-08-11 17:35:23 +0000570
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000571 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinebd694b2009-08-11 17:35:23 +0000572
Craig Topperc0196b12014-04-14 00:51:57 +0000573 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohmanceac7c32009-01-16 01:33:36 +0000574 bool HasNoopHazards = false;
575 while (!AvailableQueue.empty()) {
576 SUnit *CurSUnit = AvailableQueue.pop();
577
578 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000579 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000580 if (HT == ScheduleHazardRecognizer::NoHazard) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000581 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
582 if (!NotPreferredSUnit) {
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000583 // If this is the first non-preferred node for this cycle, then
584 // record it and continue searching for a preferred node. If this
585 // is not the first non-preferred node, then treat it as though
586 // there had been a hazard.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000587 NotPreferredSUnit = CurSUnit;
588 continue;
589 }
590 } else {
591 FoundSUnit = CurSUnit;
592 break;
593 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000594 }
595
596 // Remember if this is a noop hazard.
597 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
598
599 NotReady.push_back(CurSUnit);
600 }
601
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000602 // If we have a non-preferred node, push it back onto the available list.
603 // If we did not find a preferred node, then schedule this first
604 // non-preferred node.
605 if (NotPreferredSUnit) {
606 if (!FoundSUnit) {
607 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
608 FoundSUnit = NotPreferredSUnit;
609 } else {
610 AvailableQueue.push(NotPreferredSUnit);
611 }
612
Craig Topperc0196b12014-04-14 00:51:57 +0000613 NotPreferredSUnit = nullptr;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000614 }
615
Dan Gohmanceac7c32009-01-16 01:33:36 +0000616 // Add the nodes that aren't ready back onto the available list.
617 if (!NotReady.empty()) {
618 AvailableQueue.push_all(NotReady);
619 NotReady.clear();
620 }
621
David Goodwin8501dbbe2009-11-03 20:57:50 +0000622 // If we found a node to schedule...
Dan Gohman60cb69e2008-11-19 23:18:57 +0000623 if (FoundSUnit) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000624 // If we need to emit noops prior to this instruction, then do so.
625 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
626 for (unsigned i = 0; i != NumPreNoops; ++i)
627 emitNoop(CurCycle);
628
David Goodwin8501dbbe2009-11-03 20:57:50 +0000629 // ... schedule the node...
David Goodwin80a03cc2009-11-20 19:32:48 +0000630 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000631 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000632 CycleHasInsts = true;
Andrew Trick18c9b372011-06-01 03:27:56 +0000633 if (HazardRec->atIssueLimit()) {
634 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
635 HazardRec->AdvanceCycle();
636 ++CurCycle;
637 CycleHasInsts = false;
638 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000639 } else {
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000640 if (CycleHasInsts) {
David Greeneaa8ce382010-01-05 01:26:01 +0000641 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000642 HazardRec->AdvanceCycle();
643 } else if (!HasNoopHazards) {
644 // Otherwise, we have a pipeline stall, but no other problem,
645 // just advance the current cycle and try again.
David Greeneaa8ce382010-01-05 01:26:01 +0000646 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000647 HazardRec->AdvanceCycle();
David Goodwin80a03cc2009-11-20 19:32:48 +0000648 ++NumStalls;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000649 } else {
650 // Otherwise, we have no instructions to issue and we have instructions
651 // that will fault if we don't do this right. This is the case for
652 // processors without pipeline interlocks and other cases.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000653 emitNoop(CurCycle);
David Goodwin1f8c7a72009-08-12 21:47:46 +0000654 }
655
Dan Gohmanceac7c32009-01-16 01:33:36 +0000656 ++CurCycle;
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000657 CycleHasInsts = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000658 }
659 }
660
661#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000662 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
663 unsigned Noops = 0;
664 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
665 if (!Sequence[i])
666 ++Noops;
667 assert(Sequence.size() - Noops == ScheduledNodes &&
668 "The number of nodes scheduled doesn't match the expected number!");
669#endif // NDEBUG
Dan Gohman60cb69e2008-11-19 23:18:57 +0000670}
Andrew Tricke932bb72012-03-07 05:21:44 +0000671
672// EmitSchedule - Emit the machine code in scheduled order.
673void SchedulePostRATDList::EmitSchedule() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000674 RegionBegin = RegionEnd;
Andrew Tricke932bb72012-03-07 05:21:44 +0000675
676 // If first instruction was a DBG_VALUE then put it back.
677 if (FirstDbgValue)
Andrew Trick8c207e42012-03-09 04:29:02 +0000678 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Tricke932bb72012-03-07 05:21:44 +0000679
680 // Then re-insert them according to the given schedule.
681 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
682 if (SUnit *SU = Sequence[i])
Andrew Trick8c207e42012-03-09 04:29:02 +0000683 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Tricke932bb72012-03-07 05:21:44 +0000684 else
685 // Null SUnit* is a noop.
Andrew Trick8c207e42012-03-09 04:29:02 +0000686 TII->insertNoop(*BB, RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000687
688 // Update the Begin iterator, as the first instruction in the block
689 // may have been scheduled later.
690 if (i == 0)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000691 RegionBegin = std::prev(RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000692 }
693
694 // Reinsert any remaining debug_values.
695 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
696 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000697 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Tricke932bb72012-03-07 05:21:44 +0000698 MachineInstr *DbgValue = P.first;
699 MachineBasicBlock::iterator OrigPrivMI = P.second;
700 BB->splice(++OrigPrivMI, BB, DbgValue);
701 }
702 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000703 FirstDbgValue = nullptr;
Andrew Tricke932bb72012-03-07 05:21:44 +0000704}