blob: 79958c9a8b38fd34fd7c7b23ed6a179c7ba2db6f [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"
David Goodwinbe3039e2009-10-01 19:45:32 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanad2134d2008-11-25 00:52:40 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000032#include "llvm/CodeGen/Passes.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000033#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Trick9a0c5832012-03-07 23:01:06 +000034#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohmanceac7c32009-01-16 01:33:36 +000035#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/SchedulerRegistry.h"
David Goodwine056d102009-10-26 22:31:16 +000037#include "llvm/Support/CommandLine.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000040#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/Target/TargetRegisterInfo.h"
44#include "llvm/Target/TargetSubtargetInfo.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(
100 MachineFunctionProperties::Property::AllVRegsAllocated);
101 }
102
Craig Topper4584cd52014-03-07 09:26:03 +0000103 bool runOnMachineFunction(MachineFunction &Fn) override;
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000104
Sanjay Patela2f658d2014-07-15 22:39:58 +0000105 bool enablePostRAScheduler(
106 const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel,
107 TargetSubtargetInfo::AntiDepBreakMode &Mode,
108 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const;
Dale Johannesen2182f062007-07-13 17:13:54 +0000109 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000110 char PostRAScheduler::ID = 0;
111
Nick Lewycky02d5f772009-10-25 06:33:48 +0000112 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000113 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohman682a2d12009-10-21 01:44:44 +0000114 ///
Dan Gohman60cb69e2008-11-19 23:18:57 +0000115 LatencyPriorityQueue AvailableQueue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000116
Dan Gohman60cb69e2008-11-19 23:18:57 +0000117 /// PendingQueue - This contains all of the instructions whose operands have
118 /// been issued, but their results are not ready yet (due to the latency of
119 /// the operation). Once the operands becomes available, the instruction is
120 /// added to the AvailableQueue.
121 std::vector<SUnit*> PendingQueue;
122
Dan Gohmanceac7c32009-01-16 01:33:36 +0000123 /// HazardRec - The hazard recognizer to use.
124 ScheduleHazardRecognizer *HazardRec;
125
David Goodwin83704852009-10-26 16:59:04 +0000126 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
127 AntiDepBreaker *AntiDepBreak;
128
Dan Gohman87b02d52009-10-09 23:27:56 +0000129 /// AA - AliasAnalysis for making memory reference queries.
130 AliasAnalysis *AA;
131
Andrew Trick60cf03e2012-03-07 05:21:52 +0000132 /// The schedule. Null SUnit*'s represent noop instructions.
133 std::vector<SUnit*> Sequence;
134
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000135 /// Ordered list of DAG postprocessing steps.
136 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
137
Andrew Tricka53e1012013-08-23 17:48:33 +0000138 /// The index in BB of RegionEnd.
139 ///
140 /// This is the instruction number from the top of the current block, not
141 /// the SlotIndex. It is only used by the AntiDepBreaker.
142 unsigned EndIndex;
143
Dan Gohmanad2134d2008-11-25 00:52:40 +0000144 public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000145 SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000146 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
147 const RegisterClassInfo &,
148 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
149 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000150
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000151 ~SchedulePostRATDList() override;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000152
Andrew Trick52226d42012-03-07 23:00:49 +0000153 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohmanb9543432009-02-10 23:27:53 +0000154 /// this block.
155 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000156 void startBlock(MachineBasicBlock *BB) override;
Dan Gohmanb9543432009-02-10 23:27:53 +0000157
Andrew Tricka53e1012013-08-23 17:48:33 +0000158 // Set the index of RegionEnd within the current BB.
159 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
160
Andrew Trick60cf03e2012-03-07 05:21:52 +0000161 /// Initialize the scheduler state for the next scheduling region.
Craig Topper4584cd52014-03-07 09:26:03 +0000162 void enterRegion(MachineBasicBlock *bb,
163 MachineBasicBlock::iterator begin,
164 MachineBasicBlock::iterator end,
165 unsigned regioninstrs) override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000166
167 /// Notify that the scheduler has finished scheduling the current region.
Craig Topper4584cd52014-03-07 09:26:03 +0000168 void exitRegion() override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000169
Dan Gohmanb9543432009-02-10 23:27:53 +0000170 /// Schedule - Schedule the instruction range using list scheduling.
171 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000172 void schedule() override;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000173
Andrew Tricke932bb72012-03-07 05:21:44 +0000174 void EmitSchedule();
175
Dan Gohman682a2d12009-10-21 01:44:44 +0000176 /// Observe - Update liveness information to account for the current
177 /// instruction, which will not be scheduled.
178 ///
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000179 void Observe(MachineInstr &MI, unsigned Count);
Dan Gohman682a2d12009-10-21 01:44:44 +0000180
Andrew Trick52226d42012-03-07 23:00:49 +0000181 /// finishBlock - Clean up register live-range state.
Dan Gohman682a2d12009-10-21 01:44:44 +0000182 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000183 void finishBlock() override;
Dan Gohman682a2d12009-10-21 01:44:44 +0000184
Dan Gohman60cb69e2008-11-19 23:18:57 +0000185 private:
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000186 /// Apply each ScheduleDAGMutation step in order.
187 void postprocessDAG();
188
David Goodwin80a03cc2009-11-20 19:32:48 +0000189 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
190 void ReleaseSuccessors(SUnit *SU);
191 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
192 void ListScheduleTopDown();
Jim Grosbachd772bde2010-05-14 21:19:48 +0000193
Andrew Trickedee68c2012-03-07 05:21:40 +0000194 void dumpSchedule() const;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000195 void emitNoop(unsigned CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000196 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000197}
Dale Johannesen2182f062007-07-13 17:13:54 +0000198
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000199char &llvm::PostRASchedulerID = PostRAScheduler::ID;
200
201INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
202 "Post RA top-down list latency scheduler", false, false)
203
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000204SchedulePostRATDList::SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000205 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
206 const RegisterClassInfo &RCI,
207 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
208 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs)
Matthias Braun93563e72015-11-03 01:53:29 +0000209 : ScheduleDAGInstrs(MF, &MLI), AA(AA), EndIndex(0) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000210
Eric Christopherd9134482014-08-04 21:25:23 +0000211 const InstrItineraryData *InstrItins =
Eric Christopherb66367a2014-10-14 07:17:23 +0000212 MF.getSubtarget().getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000213 HazardRec =
Eric Christopherb66367a2014-10-14 07:17:23 +0000214 MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +0000215 InstrItins, this);
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000216 MF.getSubtarget().getPostRAMutations(Mutations);
Preston Gurd9a091472012-04-23 21:39:35 +0000217
218 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
219 MRI.tracksLiveness()) &&
220 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000221 AntiDepBreak =
Evan Cheng0d639a22011-07-01 21:01:15 +0000222 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000223 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng0d639a22011-07-01 21:01:15 +0000224 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Craig Topperc0196b12014-04-14 00:51:57 +0000225 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000226}
227
228SchedulePostRATDList::~SchedulePostRATDList() {
229 delete HazardRec;
230 delete AntiDepBreak;
231}
232
Andrew Trick60cf03e2012-03-07 05:21:52 +0000233/// Initialize state associated with the next scheduling region.
234void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
235 MachineBasicBlock::iterator begin,
236 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000237 unsigned regioninstrs) {
238 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000239 Sequence.clear();
240}
241
242/// Print the schedule before exiting the region.
243void SchedulePostRATDList::exitRegion() {
244 DEBUG({
245 dbgs() << "*** Final schedule ***\n";
246 dumpSchedule();
247 dbgs() << '\n';
248 });
249 ScheduleDAGInstrs::exitRegion();
250}
251
Manman Ren19f49ac2012-09-11 22:23:19 +0000252#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickedee68c2012-03-07 05:21:40 +0000253/// dumpSchedule - dump the scheduled Sequence.
254void SchedulePostRATDList::dumpSchedule() const {
255 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
256 if (SUnit *SU = Sequence[i])
257 SU->dump(this);
258 else
259 dbgs() << "**** NOOP ****\n";
260 }
261}
Manman Ren742534c2012-09-06 19:06:06 +0000262#endif
Andrew Trickedee68c2012-03-07 05:21:40 +0000263
Sanjay Patela2f658d2014-07-15 22:39:58 +0000264bool PostRAScheduler::enablePostRAScheduler(
265 const TargetSubtargetInfo &ST,
266 CodeGenOpt::Level OptLevel,
267 TargetSubtargetInfo::AntiDepBreakMode &Mode,
268 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const {
269 Mode = ST.getAntiDepBreakMode();
270 ST.getCriticalPathRCs(CriticalPathRCs);
Matthias Braun39a2afc2015-06-13 03:42:16 +0000271 return ST.enablePostRAScheduler() &&
Sanjay Patela2f658d2014-07-15 22:39:58 +0000272 OptLevel >= ST.getOptLevelToEnablePostRAScheduler();
273}
274
Dan Gohman60cb69e2008-11-19 23:18:57 +0000275bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000276 if (skipFunction(*Fn.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000277 return false;
278
Eric Christopherfc6de422014-08-05 02:39:49 +0000279 TII = Fn.getSubtarget().getInstrInfo();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000280 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000281 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000282 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
283
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000284 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman26e9b892009-10-10 00:15:38 +0000285
David Goodwin9a051a52009-10-01 21:46:35 +0000286 // Check for explicit enable/disable of post-ra scheduling.
Evan Cheng7fae11b2011-12-14 02:11:42 +0000287 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
288 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper760b1342012-02-22 05:59:10 +0000289 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin9a051a52009-10-01 21:46:35 +0000290 if (EnablePostRAScheduler.getPosition() > 0) {
291 if (!EnablePostRAScheduler)
Evan Cheng8b614762009-10-16 06:10:34 +0000292 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000293 } else {
Evan Cheng8b614762009-10-16 06:10:34 +0000294 // Check that post-RA scheduling is enabled for this target.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000295 // This may upgrade the AntiDepMode.
Eric Christopher3d4276f2015-01-27 07:31:29 +0000296 if (!enablePostRAScheduler(Fn.getSubtarget(), PassConfig->getOptLevel(),
Sanjay Patela2f658d2014-07-15 22:39:58 +0000297 AntiDepMode, CriticalPathRCs))
Evan Cheng8b614762009-10-16 06:10:34 +0000298 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000299 }
David Goodwin17199b52009-09-30 00:10:16 +0000300
David Goodwin02ad4cb2009-10-22 23:19:17 +0000301 // Check for antidep breaking override...
302 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng0d639a22011-07-01 21:01:15 +0000303 AntiDepMode = (EnableAntiDepBreaking == "all")
304 ? TargetSubtargetInfo::ANTIDEP_ALL
305 : ((EnableAntiDepBreaking == "critical")
306 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
307 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin02ad4cb2009-10-22 23:19:17 +0000308 }
309
David Greeneaa8ce382010-01-05 01:26:01 +0000310 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesen2182f062007-07-13 17:13:54 +0000311
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000312 SchedulePostRATDList Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode,
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000313 CriticalPathRCs);
Dan Gohman619ef482009-01-15 19:20:50 +0000314
Dale Johannesen2182f062007-07-13 17:13:54 +0000315 // Loop over all of the basic blocks
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000316 for (auto &MBB : Fn) {
David Goodwin7f651692009-09-01 18:34:03 +0000317#ifndef NDEBUG
318 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
319 if (DebugDiv > 0) {
320 static int bbcnt = 0;
321 if (bbcnt++ % DebugDiv != DebugMod)
322 continue;
Craig Toppera538d832012-08-22 06:07:19 +0000323 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000324 << ":BB#" << MBB.getNumber() << " ***\n";
David Goodwin7f651692009-09-01 18:34:03 +0000325 }
326#endif
327
Dan Gohmanb9543432009-02-10 23:27:53 +0000328 // Initialize register live-range state for scheduling in this block.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000329 Scheduler.startBlock(&MBB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000330
Dan Gohman5f8a2592009-01-16 22:10:20 +0000331 // Schedule each sequence of instructions not interrupted by a label
332 // or anything else that effectively needs to shut down scheduling.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000333 MachineBasicBlock::iterator Current = MBB.end();
334 unsigned Count = MBB.size(), CurrentCount = Count;
335 for (MachineBasicBlock::iterator I = Current; I != MBB.begin();) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000336 MachineInstr *MI = std::prev(I);
Andrew Tricka53e1012013-08-23 17:48:33 +0000337 --Count;
Jakob Stoklund Olesena793a592012-02-23 17:54:21 +0000338 // Calls are not scheduling boundaries before register allocation, but
339 // post-ra we don't gain anything by scheduling across calls since we
340 // don't need to worry about register pressure.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000341 if (MI->isCall() || TII->isSchedulingBoundary(MI, &MBB, Fn)) {
342 Scheduler.enterRegion(&MBB, I, Current, CurrentCount - Count);
Andrew Tricka53e1012013-08-23 17:48:33 +0000343 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000344 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000345 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000346 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000347 Current = MI;
Andrew Tricka53e1012013-08-23 17:48:33 +0000348 CurrentCount = Count;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000349 Scheduler.Observe(*MI, CurrentCount);
Dan Gohman5f8a2592009-01-16 22:10:20 +0000350 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000351 I = MI;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000352 if (MI->isBundle())
353 Count -= MI->getBundleSize();
Dan Gohmand5643532009-02-03 18:57:45 +0000354 }
Dan Gohmandfaf6462009-02-11 04:27:20 +0000355 assert(Count == 0 && "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000356 assert((MBB.begin() == Current || CurrentCount != 0) &&
Dan Gohman64613ac2009-03-10 18:10:43 +0000357 "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000358 Scheduler.enterRegion(&MBB, MBB.begin(), Current, CurrentCount);
Andrew Tricka53e1012013-08-23 17:48:33 +0000359 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000360 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000361 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000362 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000363
364 // Clean up register live-range state.
Andrew Trick52226d42012-03-07 23:00:49 +0000365 Scheduler.finishBlock();
David Goodwinae6bc822009-08-25 17:03:05 +0000366
David Goodwin6c08cfc2009-09-03 22:15:25 +0000367 // Update register kills
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000368 Scheduler.fixupKills(&MBB);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000369 }
Dale Johannesen2182f062007-07-13 17:13:54 +0000370
371 return true;
372}
Jim Grosbachd772bde2010-05-14 21:19:48 +0000373
Dan Gohmanb9543432009-02-10 23:27:53 +0000374/// StartBlock - Initialize register live-range state for scheduling in
375/// this block.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000376///
Andrew Trick52226d42012-03-07 23:00:49 +0000377void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000378 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000379 ScheduleDAGInstrs::startBlock(BB);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000380
David Goodwin83704852009-10-26 16:59:04 +0000381 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin6021b4d2009-08-10 15:55:25 +0000382 HazardRec->Reset();
Craig Topperc0196b12014-04-14 00:51:57 +0000383 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000384 AntiDepBreak->StartBlock(BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000385}
386
387/// Schedule - Schedule the instruction range using list scheduling.
388///
Andrew Trick52226d42012-03-07 23:00:49 +0000389void SchedulePostRATDList::schedule() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000390 // Build the scheduling graph.
Andrew Trick52226d42012-03-07 23:00:49 +0000391 buildSchedGraph(AA);
Dan Gohmanb9543432009-02-10 23:27:53 +0000392
Craig Topperc0196b12014-04-14 00:51:57 +0000393 if (AntiDepBreak) {
Jim Grosbachd772bde2010-05-14 21:19:48 +0000394 unsigned Broken =
Andrew Trick8c207e42012-03-09 04:29:02 +0000395 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
396 EndIndex, DbgValues);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000397
David Goodwin80a03cc2009-11-20 19:32:48 +0000398 if (Broken != 0) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000399 // We made changes. Update the dependency graph.
400 // Theoretically we could update the graph in place:
401 // When a live range is changed to use a different register, remove
402 // the def's anti-dependence *and* output-dependence edges due to
403 // that register, and add new anti-dependence and output-dependence
404 // edges based on the next live range of the register.
Andrew Trick60cf03e2012-03-07 05:21:52 +0000405 ScheduleDAG::clearDAG();
Andrew Trick52226d42012-03-07 23:00:49 +0000406 buildSchedGraph(AA);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000407
David Goodwin83704852009-10-26 16:59:04 +0000408 NumFixedAnti += Broken;
Dan Gohmanb9543432009-02-10 23:27:53 +0000409 }
410 }
411
Krzysztof Parzyszekcd99e362016-03-08 16:54:20 +0000412 postprocessDAG();
413
David Greeneaa8ce382010-01-05 01:26:01 +0000414 DEBUG(dbgs() << "********** List Scheduling **********\n");
Matthias Braun9198c672015-11-06 20:59:02 +0000415 DEBUG(
416 for (const SUnit &SU : SUnits) {
417 SU.dumpAll(this);
418 dbgs() << '\n';
419 }
420 );
David Goodwin6021b4d2009-08-10 15:55:25 +0000421
Dan Gohmanb9543432009-02-10 23:27:53 +0000422 AvailableQueue.initNodes(SUnits);
David Goodwin80a03cc2009-11-20 19:32:48 +0000423 ListScheduleTopDown();
Dan Gohmanb9543432009-02-10 23:27:53 +0000424 AvailableQueue.releaseState();
425}
426
427/// Observe - Update liveness information to account for the current
428/// instruction, which will not be scheduled.
429///
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000430void SchedulePostRATDList::Observe(MachineInstr &MI, unsigned Count) {
Craig Topperc0196b12014-04-14 00:51:57 +0000431 if (AntiDepBreak)
Andrew Tricka316faa2012-03-07 23:00:52 +0000432 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohmanb9543432009-02-10 23:27:53 +0000433}
434
435/// FinishBlock - Clean up register live-range state.
436///
Andrew Trick52226d42012-03-07 23:00:49 +0000437void SchedulePostRATDList::finishBlock() {
Craig Topperc0196b12014-04-14 00:51:57 +0000438 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000439 AntiDepBreak->FinishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000440
441 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000442 ScheduleDAGInstrs::finishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000443}
444
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000445/// Apply each ScheduleDAGMutation step in order.
446void SchedulePostRATDList::postprocessDAG() {
447 for (auto &M : Mutations)
448 M->apply(this);
449}
450
Dan Gohman60cb69e2008-11-19 23:18:57 +0000451//===----------------------------------------------------------------------===//
452// Top-Down Scheduling
453//===----------------------------------------------------------------------===//
454
455/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000456/// the PendingQueue if the count reaches zero.
David Goodwin80a03cc2009-11-20 19:32:48 +0000457void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000458 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000459
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000460 if (SuccEdge->isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000461 --SuccSU->WeakPredsLeft;
462 return;
463 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000464#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000465 if (SuccSU->NumPredsLeft == 0) {
David Greeneaa8ce382010-01-05 01:26:01 +0000466 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000467 SuccSU->dump(this);
David Greeneaa8ce382010-01-05 01:26:01 +0000468 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000469 llvm_unreachable(nullptr);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000470 }
471#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000472 --SuccSU->NumPredsLeft;
473
Andrew Trick84f9ad92011-05-06 18:14:32 +0000474 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trickaab77fe2011-05-06 17:09:08 +0000475 // here as such:
476 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
477 //
478 // However, we lazily compute node depth instead. Note that
479 // ScheduleNodeTopDown has already updated the depth of this node which causes
480 // all descendents to be marked dirty. Setting the successor depth explicitly
481 // here would cause depth to be recomputed for all its ancestors. If the
482 // successor is not yet ready (because of a transitively redundant edge) then
483 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbachd772bde2010-05-14 21:19:48 +0000484
Dan Gohmanb9543432009-02-10 23:27:53 +0000485 // If all the node's predecessors are scheduled, this node is ready
486 // to be scheduled. Ignore the special ExitSU node.
487 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman60cb69e2008-11-19 23:18:57 +0000488 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000489}
490
491/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin80a03cc2009-11-20 19:32:48 +0000492void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000493 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +0000494 I != E; ++I) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000495 ReleaseSucc(SU, &*I);
David Goodwin8501dbbe2009-11-03 20:57:50 +0000496 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000497}
498
499/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
500/// count of its successors. If a successor pending count is zero, add it to
501/// the Available queue.
David Goodwin80a03cc2009-11-20 19:32:48 +0000502void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greeneaa8ce382010-01-05 01:26:01 +0000503 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000504 DEBUG(SU->dump(this));
Jim Grosbachd772bde2010-05-14 21:19:48 +0000505
Dan Gohman60cb69e2008-11-19 23:18:57 +0000506 Sequence.push_back(SU);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000507 assert(CurCycle >= SU->getDepth() &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000508 "Node scheduled above its depth!");
David Goodwin80a03cc2009-11-20 19:32:48 +0000509 SU->setDepthToAtLeast(CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000510
David Goodwin80a03cc2009-11-20 19:32:48 +0000511 ReleaseSuccessors(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000512 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000513 AvailableQueue.scheduledNode(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000514}
515
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000516/// emitNoop - Add a noop to the current instruction sequence.
517void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
518 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
519 HazardRec->EmitNoop();
Craig Topperc0196b12014-04-14 00:51:57 +0000520 Sequence.push_back(nullptr); // NULL here means noop
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000521 ++NumNoops;
522}
523
Dan Gohman60cb69e2008-11-19 23:18:57 +0000524/// ListScheduleTopDown - The main loop of list scheduling for top-down
525/// schedulers.
David Goodwin80a03cc2009-11-20 19:32:48 +0000526void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000527 unsigned CurCycle = 0;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000528
David Goodwin8501dbbe2009-11-03 20:57:50 +0000529 // We're scheduling top-down but we're visiting the regions in
530 // bottom-up order, so we don't know the hazards at the start of a
531 // region. So assume no hazards (this should usually be ok as most
532 // blocks are a single region).
533 HazardRec->Reset();
534
Dan Gohmanb9543432009-02-10 23:27:53 +0000535 // Release any successors of the special Entry node.
David Goodwin80a03cc2009-11-20 19:32:48 +0000536 ReleaseSuccessors(&EntrySU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000537
David Goodwin80a03cc2009-11-20 19:32:48 +0000538 // Add all leaves to Available queue.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000539 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
540 // It is available if it has no predecessors.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000541 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000542 AvailableQueue.push(&SUnits[i]);
543 SUnits[i].isAvailable = true;
544 }
545 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000546
David Goodwin1f8c7a72009-08-12 21:47:46 +0000547 // In any cycle where we can't schedule any instructions, we must
548 // stall or emit a noop, depending on the target.
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000549 bool CycleHasInsts = false;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000550
Dan Gohman60cb69e2008-11-19 23:18:57 +0000551 // While Available queue is not empty, grab the node with the highest
552 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmanceac7c32009-01-16 01:33:36 +0000553 std::vector<SUnit*> NotReady;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000554 Sequence.reserve(SUnits.size());
555 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
556 // Check to see if any of the pending instructions are ready to issue. If
557 // so, add them to the available queue.
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000558 unsigned MinDepth = ~0u;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000559 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000560 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000561 AvailableQueue.push(PendingQueue[i]);
562 PendingQueue[i]->isAvailable = true;
563 PendingQueue[i] = PendingQueue.back();
564 PendingQueue.pop_back();
565 --i; --e;
David Goodwin80a03cc2009-11-20 19:32:48 +0000566 } else if (PendingQueue[i]->getDepth() < MinDepth)
567 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000568 }
David Goodwinebd694b2009-08-11 17:35:23 +0000569
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000570 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinebd694b2009-08-11 17:35:23 +0000571
Craig Topperc0196b12014-04-14 00:51:57 +0000572 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohmanceac7c32009-01-16 01:33:36 +0000573 bool HasNoopHazards = false;
574 while (!AvailableQueue.empty()) {
575 SUnit *CurSUnit = AvailableQueue.pop();
576
577 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000578 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000579 if (HT == ScheduleHazardRecognizer::NoHazard) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000580 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
581 if (!NotPreferredSUnit) {
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000582 // If this is the first non-preferred node for this cycle, then
583 // record it and continue searching for a preferred node. If this
584 // is not the first non-preferred node, then treat it as though
585 // there had been a hazard.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000586 NotPreferredSUnit = CurSUnit;
587 continue;
588 }
589 } else {
590 FoundSUnit = CurSUnit;
591 break;
592 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000593 }
594
595 // Remember if this is a noop hazard.
596 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
597
598 NotReady.push_back(CurSUnit);
599 }
600
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000601 // If we have a non-preferred node, push it back onto the available list.
602 // If we did not find a preferred node, then schedule this first
603 // non-preferred node.
604 if (NotPreferredSUnit) {
605 if (!FoundSUnit) {
606 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
607 FoundSUnit = NotPreferredSUnit;
608 } else {
609 AvailableQueue.push(NotPreferredSUnit);
610 }
611
Craig Topperc0196b12014-04-14 00:51:57 +0000612 NotPreferredSUnit = nullptr;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000613 }
614
Dan Gohmanceac7c32009-01-16 01:33:36 +0000615 // Add the nodes that aren't ready back onto the available list.
616 if (!NotReady.empty()) {
617 AvailableQueue.push_all(NotReady);
618 NotReady.clear();
619 }
620
David Goodwin8501dbbe2009-11-03 20:57:50 +0000621 // If we found a node to schedule...
Dan Gohman60cb69e2008-11-19 23:18:57 +0000622 if (FoundSUnit) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000623 // If we need to emit noops prior to this instruction, then do so.
624 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
625 for (unsigned i = 0; i != NumPreNoops; ++i)
626 emitNoop(CurCycle);
627
David Goodwin8501dbbe2009-11-03 20:57:50 +0000628 // ... schedule the node...
David Goodwin80a03cc2009-11-20 19:32:48 +0000629 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000630 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000631 CycleHasInsts = true;
Andrew Trick18c9b372011-06-01 03:27:56 +0000632 if (HazardRec->atIssueLimit()) {
633 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
634 HazardRec->AdvanceCycle();
635 ++CurCycle;
636 CycleHasInsts = false;
637 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000638 } else {
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000639 if (CycleHasInsts) {
David Greeneaa8ce382010-01-05 01:26:01 +0000640 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000641 HazardRec->AdvanceCycle();
642 } else if (!HasNoopHazards) {
643 // Otherwise, we have a pipeline stall, but no other problem,
644 // just advance the current cycle and try again.
David Greeneaa8ce382010-01-05 01:26:01 +0000645 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000646 HazardRec->AdvanceCycle();
David Goodwin80a03cc2009-11-20 19:32:48 +0000647 ++NumStalls;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000648 } else {
649 // Otherwise, we have no instructions to issue and we have instructions
650 // that will fault if we don't do this right. This is the case for
651 // processors without pipeline interlocks and other cases.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000652 emitNoop(CurCycle);
David Goodwin1f8c7a72009-08-12 21:47:46 +0000653 }
654
Dan Gohmanceac7c32009-01-16 01:33:36 +0000655 ++CurCycle;
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000656 CycleHasInsts = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000657 }
658 }
659
660#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000661 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
662 unsigned Noops = 0;
663 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
664 if (!Sequence[i])
665 ++Noops;
666 assert(Sequence.size() - Noops == ScheduledNodes &&
667 "The number of nodes scheduled doesn't match the expected number!");
668#endif // NDEBUG
Dan Gohman60cb69e2008-11-19 23:18:57 +0000669}
Andrew Tricke932bb72012-03-07 05:21:44 +0000670
671// EmitSchedule - Emit the machine code in scheduled order.
672void SchedulePostRATDList::EmitSchedule() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000673 RegionBegin = RegionEnd;
Andrew Tricke932bb72012-03-07 05:21:44 +0000674
675 // If first instruction was a DBG_VALUE then put it back.
676 if (FirstDbgValue)
Andrew Trick8c207e42012-03-09 04:29:02 +0000677 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Tricke932bb72012-03-07 05:21:44 +0000678
679 // Then re-insert them according to the given schedule.
680 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
681 if (SUnit *SU = Sequence[i])
Andrew Trick8c207e42012-03-09 04:29:02 +0000682 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Tricke932bb72012-03-07 05:21:44 +0000683 else
684 // Null SUnit* is a noop.
Andrew Trick8c207e42012-03-09 04:29:02 +0000685 TII->insertNoop(*BB, RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000686
687 // Update the Begin iterator, as the first instruction in the block
688 // may have been scheduled later.
689 if (i == 0)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000690 RegionBegin = std::prev(RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000691 }
692
693 // Reinsert any remaining debug_values.
694 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
695 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000696 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Tricke932bb72012-03-07 05:21:44 +0000697 MachineInstr *DbgValue = P.first;
698 MachineBasicBlock::iterator OrigPrivMI = P.second;
699 BB->splice(++OrigPrivMI, BB, DbgValue);
700 }
701 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000702 FirstDbgValue = nullptr;
Andrew Tricke932bb72012-03-07 05:21:44 +0000703}