blob: 81dd6ef2444ff01bc663ebb8fe8cbd76517701e6 [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 Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetLowering.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000039#include "llvm/CodeGen/TargetPassConfig.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetRegisterInfo.h"
41#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Goodwine056d102009-10-26 22:31:16 +000042#include "llvm/Support/CommandLine.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000043#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000044#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000045#include "llvm/Support/raw_ostream.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000046using namespace llvm;
47
Chandler Carruth1b9dde02014-04-22 02:02:50 +000048#define DEBUG_TYPE "post-RA-sched"
49
Dan Gohmanceac7c32009-01-16 01:33:36 +000050STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman60cb69e2008-11-19 23:18:57 +000051STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin83704852009-10-26 16:59:04 +000052STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman60cb69e2008-11-19 23:18:57 +000053
David Goodwin9a051a52009-10-01 21:46:35 +000054// Post-RA scheduling is enabled with
Evan Cheng0d639a22011-07-01 21:01:15 +000055// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin9a051a52009-10-01 21:46:35 +000056// override the target.
57static cl::opt<bool>
58EnablePostRAScheduler("post-RA-scheduler",
59 cl::desc("Enable scheduling after register allocation"),
David Goodwin1cc6dd92009-10-01 22:19:57 +000060 cl::init(false), cl::Hidden);
David Goodwin83704852009-10-26 16:59:04 +000061static cl::opt<std::string>
Dan Gohmanad2134d2008-11-25 00:52:40 +000062EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin83704852009-10-26 16:59:04 +000063 cl::desc("Break post-RA scheduling anti-dependencies: "
64 "\"critical\", \"all\", or \"none\""),
65 cl::init("none"), cl::Hidden);
Dan Gohmanceac7c32009-01-16 01:33:36 +000066
David Goodwin7f651692009-09-01 18:34:03 +000067// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
68static cl::opt<int>
69DebugDiv("postra-sched-debugdiv",
70 cl::desc("Debug control MBBs that are scheduled"),
71 cl::init(0), cl::Hidden);
72static cl::opt<int>
73DebugMod("postra-sched-debugmod",
74 cl::desc("Debug control MBBs that are scheduled"),
75 cl::init(0), cl::Hidden);
76
David Goodwin661ea982009-10-26 19:41:00 +000077AntiDepBreaker::~AntiDepBreaker() { }
78
Dale Johannesen2182f062007-07-13 17:13:54 +000079namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000080 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng2d51c7c2010-06-18 23:09:54 +000081 const TargetInstrInfo *TII;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000082 RegisterClassInfo RegClassInfo;
Dan Gohman87b02d52009-10-09 23:27:56 +000083
Dale Johannesen2182f062007-07-13 17:13:54 +000084 public:
85 static char ID;
Andrew Trickdf7e3762012-02-08 21:22:53 +000086 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohmanad2134d2008-11-25 00:52:40 +000087
Craig Topper4584cd52014-03-07 09:26:03 +000088 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000089 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +000090 AU.addRequired<AAResultsWrapperPass>();
Andrew Trickdf7e3762012-02-08 21:22:53 +000091 AU.addRequired<TargetPassConfig>();
Dan Gohmandddc1ac2008-12-16 03:25:46 +000092 AU.addRequired<MachineDominatorTree>();
93 AU.addPreserved<MachineDominatorTree>();
94 AU.addRequired<MachineLoopInfo>();
95 AU.addPreserved<MachineLoopInfo>();
96 MachineFunctionPass::getAnalysisUsage(AU);
97 }
98
Derek Schuffad154c82016-03-28 17:05:30 +000099 MachineFunctionProperties getRequiredProperties() const override {
100 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000101 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000102 }
103
Craig Topper4584cd52014-03-07 09:26:03 +0000104 bool runOnMachineFunction(MachineFunction &Fn) override;
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000105
Mitch Bodart64535012016-05-19 16:40:49 +0000106 private:
Sanjay Patela2f658d2014-07-15 22:39:58 +0000107 bool enablePostRAScheduler(
108 const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel,
109 TargetSubtargetInfo::AntiDepBreakMode &Mode,
110 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const;
Dale Johannesen2182f062007-07-13 17:13:54 +0000111 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000112 char PostRAScheduler::ID = 0;
113
Nick Lewycky02d5f772009-10-25 06:33:48 +0000114 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000115 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohman682a2d12009-10-21 01:44:44 +0000116 ///
Dan Gohman60cb69e2008-11-19 23:18:57 +0000117 LatencyPriorityQueue AvailableQueue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000118
Dan Gohman60cb69e2008-11-19 23:18:57 +0000119 /// PendingQueue - This contains all of the instructions whose operands have
120 /// been issued, but their results are not ready yet (due to the latency of
121 /// the operation). Once the operands becomes available, the instruction is
122 /// added to the AvailableQueue.
123 std::vector<SUnit*> PendingQueue;
124
Dan Gohmanceac7c32009-01-16 01:33:36 +0000125 /// HazardRec - The hazard recognizer to use.
126 ScheduleHazardRecognizer *HazardRec;
127
David Goodwin83704852009-10-26 16:59:04 +0000128 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
129 AntiDepBreaker *AntiDepBreak;
130
Dan Gohman87b02d52009-10-09 23:27:56 +0000131 /// AA - AliasAnalysis for making memory reference queries.
132 AliasAnalysis *AA;
133
Andrew Trick60cf03e2012-03-07 05:21:52 +0000134 /// The schedule. Null SUnit*'s represent noop instructions.
135 std::vector<SUnit*> Sequence;
136
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000137 /// Ordered list of DAG postprocessing steps.
138 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
139
Andrew Tricka53e1012013-08-23 17:48:33 +0000140 /// The index in BB of RegionEnd.
141 ///
142 /// This is the instruction number from the top of the current block, not
143 /// the SlotIndex. It is only used by the AntiDepBreaker.
144 unsigned EndIndex;
145
Dan Gohmanad2134d2008-11-25 00:52:40 +0000146 public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000147 SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000148 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
149 const RegisterClassInfo &,
150 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
151 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000152
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000153 ~SchedulePostRATDList() override;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000154
Andrew Trick52226d42012-03-07 23:00:49 +0000155 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohmanb9543432009-02-10 23:27:53 +0000156 /// this block.
157 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000158 void startBlock(MachineBasicBlock *BB) override;
Dan Gohmanb9543432009-02-10 23:27:53 +0000159
Andrew Tricka53e1012013-08-23 17:48:33 +0000160 // Set the index of RegionEnd within the current BB.
161 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
162
Andrew Trick60cf03e2012-03-07 05:21:52 +0000163 /// Initialize the scheduler state for the next scheduling region.
Craig Topper4584cd52014-03-07 09:26:03 +0000164 void enterRegion(MachineBasicBlock *bb,
165 MachineBasicBlock::iterator begin,
166 MachineBasicBlock::iterator end,
167 unsigned regioninstrs) override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000168
169 /// Notify that the scheduler has finished scheduling the current region.
Craig Topper4584cd52014-03-07 09:26:03 +0000170 void exitRegion() override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000171
Dan Gohmanb9543432009-02-10 23:27:53 +0000172 /// Schedule - Schedule the instruction range using list scheduling.
173 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000174 void schedule() override;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000175
Andrew Tricke932bb72012-03-07 05:21:44 +0000176 void EmitSchedule();
177
Dan Gohman682a2d12009-10-21 01:44:44 +0000178 /// Observe - Update liveness information to account for the current
179 /// instruction, which will not be scheduled.
180 ///
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000181 void Observe(MachineInstr &MI, unsigned Count);
Dan Gohman682a2d12009-10-21 01:44:44 +0000182
Andrew Trick52226d42012-03-07 23:00:49 +0000183 /// finishBlock - Clean up register live-range state.
Dan Gohman682a2d12009-10-21 01:44:44 +0000184 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000185 void finishBlock() override;
Dan Gohman682a2d12009-10-21 01:44:44 +0000186
Dan Gohman60cb69e2008-11-19 23:18:57 +0000187 private:
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000188 /// Apply each ScheduleDAGMutation step in order.
189 void postprocessDAG();
190
David Goodwin80a03cc2009-11-20 19:32:48 +0000191 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
192 void ReleaseSuccessors(SUnit *SU);
193 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
194 void ListScheduleTopDown();
Jim Grosbachd772bde2010-05-14 21:19:48 +0000195
Andrew Trickedee68c2012-03-07 05:21:40 +0000196 void dumpSchedule() const;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000197 void emitNoop(unsigned CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000198 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000199}
Dale Johannesen2182f062007-07-13 17:13:54 +0000200
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000201char &llvm::PostRASchedulerID = PostRAScheduler::ID;
202
Matthias Braun1527baa2017-05-25 21:26:32 +0000203INITIALIZE_PASS(PostRAScheduler, DEBUG_TYPE,
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000204 "Post RA top-down list latency scheduler", false, false)
205
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000206SchedulePostRATDList::SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000207 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
208 const RegisterClassInfo &RCI,
209 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
210 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs)
Matthias Braun93563e72015-11-03 01:53:29 +0000211 : ScheduleDAGInstrs(MF, &MLI), AA(AA), EndIndex(0) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000212
Eric Christopherd9134482014-08-04 21:25:23 +0000213 const InstrItineraryData *InstrItins =
Eric Christopherb66367a2014-10-14 07:17:23 +0000214 MF.getSubtarget().getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000215 HazardRec =
Eric Christopherb66367a2014-10-14 07:17:23 +0000216 MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +0000217 InstrItins, this);
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000218 MF.getSubtarget().getPostRAMutations(Mutations);
Preston Gurd9a091472012-04-23 21:39:35 +0000219
220 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
221 MRI.tracksLiveness()) &&
222 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000223 AntiDepBreak =
Evan Cheng0d639a22011-07-01 21:01:15 +0000224 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000225 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng0d639a22011-07-01 21:01:15 +0000226 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Craig Topperc0196b12014-04-14 00:51:57 +0000227 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000228}
229
230SchedulePostRATDList::~SchedulePostRATDList() {
231 delete HazardRec;
232 delete AntiDepBreak;
233}
234
Andrew Trick60cf03e2012-03-07 05:21:52 +0000235/// Initialize state associated with the next scheduling region.
236void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
237 MachineBasicBlock::iterator begin,
238 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000239 unsigned regioninstrs) {
240 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000241 Sequence.clear();
242}
243
244/// Print the schedule before exiting the region.
245void SchedulePostRATDList::exitRegion() {
246 DEBUG({
247 dbgs() << "*** Final schedule ***\n";
248 dumpSchedule();
249 dbgs() << '\n';
250 });
251 ScheduleDAGInstrs::exitRegion();
252}
253
Aaron Ballman615eb472017-10-15 14:32:27 +0000254#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickedee68c2012-03-07 05:21:40 +0000255/// dumpSchedule - dump the scheduled Sequence.
Matthias Braun8c209aa2017-01-28 02:02:38 +0000256LLVM_DUMP_METHOD void SchedulePostRATDList::dumpSchedule() const {
Andrew Trickedee68c2012-03-07 05:21:40 +0000257 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
258 if (SUnit *SU = Sequence[i])
259 SU->dump(this);
260 else
261 dbgs() << "**** NOOP ****\n";
262 }
263}
Manman Ren742534c2012-09-06 19:06:06 +0000264#endif
Andrew Trickedee68c2012-03-07 05:21:40 +0000265
Sanjay Patela2f658d2014-07-15 22:39:58 +0000266bool PostRAScheduler::enablePostRAScheduler(
267 const TargetSubtargetInfo &ST,
268 CodeGenOpt::Level OptLevel,
269 TargetSubtargetInfo::AntiDepBreakMode &Mode,
270 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const {
271 Mode = ST.getAntiDepBreakMode();
272 ST.getCriticalPathRCs(CriticalPathRCs);
Mitch Bodart64535012016-05-19 16:40:49 +0000273
274 // Check for explicit enable/disable of post-ra scheduling.
275 if (EnablePostRAScheduler.getPosition() > 0)
276 return EnablePostRAScheduler;
277
Matthias Braun39a2afc2015-06-13 03:42:16 +0000278 return ST.enablePostRAScheduler() &&
Sanjay Patela2f658d2014-07-15 22:39:58 +0000279 OptLevel >= ST.getOptLevelToEnablePostRAScheduler();
280}
281
Dan Gohman60cb69e2008-11-19 23:18:57 +0000282bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000283 if (skipFunction(*Fn.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000284 return false;
285
Eric Christopherfc6de422014-08-05 02:39:49 +0000286 TII = Fn.getSubtarget().getInstrInfo();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000287 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000288 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000289 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
290
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000291 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman26e9b892009-10-10 00:15:38 +0000292
Evan Cheng7fae11b2011-12-14 02:11:42 +0000293 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
294 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper760b1342012-02-22 05:59:10 +0000295 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
Mitch Bodart64535012016-05-19 16:40:49 +0000296
297 // Check that post-RA scheduling is enabled for this target.
298 // This may upgrade the AntiDepMode.
299 if (!enablePostRAScheduler(Fn.getSubtarget(), PassConfig->getOptLevel(),
300 AntiDepMode, CriticalPathRCs))
301 return false;
David Goodwin17199b52009-09-30 00:10:16 +0000302
David Goodwin02ad4cb2009-10-22 23:19:17 +0000303 // Check for antidep breaking override...
304 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng0d639a22011-07-01 21:01:15 +0000305 AntiDepMode = (EnableAntiDepBreaking == "all")
306 ? TargetSubtargetInfo::ANTIDEP_ALL
307 : ((EnableAntiDepBreaking == "critical")
308 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
309 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin02ad4cb2009-10-22 23:19:17 +0000310 }
311
David Greeneaa8ce382010-01-05 01:26:01 +0000312 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesen2182f062007-07-13 17:13:54 +0000313
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000314 SchedulePostRATDList Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode,
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000315 CriticalPathRCs);
Dan Gohman619ef482009-01-15 19:20:50 +0000316
Dale Johannesen2182f062007-07-13 17:13:54 +0000317 // Loop over all of the basic blocks
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000318 for (auto &MBB : Fn) {
David Goodwin7f651692009-09-01 18:34:03 +0000319#ifndef NDEBUG
320 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
321 if (DebugDiv > 0) {
322 static int bbcnt = 0;
323 if (bbcnt++ % DebugDiv != DebugMod)
324 continue;
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000325 dbgs() << "*** DEBUG scheduling " << Fn.getName() << ":"
326 << printMBBReference(MBB) << " ***\n";
David Goodwin7f651692009-09-01 18:34:03 +0000327 }
328#endif
329
Dan Gohmanb9543432009-02-10 23:27:53 +0000330 // Initialize register live-range state for scheduling in this block.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000331 Scheduler.startBlock(&MBB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000332
Dan Gohman5f8a2592009-01-16 22:10:20 +0000333 // Schedule each sequence of instructions not interrupted by a label
334 // or anything else that effectively needs to shut down scheduling.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000335 MachineBasicBlock::iterator Current = MBB.end();
336 unsigned Count = MBB.size(), CurrentCount = Count;
337 for (MachineBasicBlock::iterator I = Current; I != MBB.begin();) {
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000338 MachineInstr &MI = *std::prev(I);
Andrew Tricka53e1012013-08-23 17:48:33 +0000339 --Count;
Jakob Stoklund Olesena793a592012-02-23 17:54:21 +0000340 // Calls are not scheduling boundaries before register allocation, but
341 // post-ra we don't gain anything by scheduling across calls since we
342 // don't need to worry about register pressure.
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000343 if (MI.isCall() || TII->isSchedulingBoundary(MI, &MBB, Fn)) {
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000344 Scheduler.enterRegion(&MBB, I, Current, CurrentCount - Count);
Andrew Tricka53e1012013-08-23 17:48:33 +0000345 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000346 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000347 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000348 Scheduler.EmitSchedule();
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000349 Current = &MI;
Andrew Tricka53e1012013-08-23 17:48:33 +0000350 CurrentCount = Count;
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000351 Scheduler.Observe(MI, CurrentCount);
Dan Gohman5f8a2592009-01-16 22:10:20 +0000352 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000353 I = MI;
Duncan P. N. Exon Smith762c5ca2016-07-01 01:18:53 +0000354 if (MI.isBundle())
355 Count -= MI.getBundleSize();
Dan Gohmand5643532009-02-03 18:57:45 +0000356 }
Dan Gohmandfaf6462009-02-11 04:27:20 +0000357 assert(Count == 0 && "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000358 assert((MBB.begin() == Current || CurrentCount != 0) &&
Dan Gohman64613ac2009-03-10 18:10:43 +0000359 "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000360 Scheduler.enterRegion(&MBB, MBB.begin(), Current, CurrentCount);
Andrew Tricka53e1012013-08-23 17:48:33 +0000361 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000362 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000363 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000364 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000365
366 // Clean up register live-range state.
Andrew Trick52226d42012-03-07 23:00:49 +0000367 Scheduler.finishBlock();
David Goodwinae6bc822009-08-25 17:03:05 +0000368
David Goodwin6c08cfc2009-09-03 22:15:25 +0000369 // Update register kills
Matthias Braun868bbd42017-05-27 02:50:50 +0000370 Scheduler.fixupKills(MBB);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000371 }
Dale Johannesen2182f062007-07-13 17:13:54 +0000372
373 return true;
374}
Jim Grosbachd772bde2010-05-14 21:19:48 +0000375
Dan Gohmanb9543432009-02-10 23:27:53 +0000376/// StartBlock - Initialize register live-range state for scheduling in
377/// this block.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000378///
Andrew Trick52226d42012-03-07 23:00:49 +0000379void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000380 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000381 ScheduleDAGInstrs::startBlock(BB);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000382
David Goodwin83704852009-10-26 16:59:04 +0000383 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin6021b4d2009-08-10 15:55:25 +0000384 HazardRec->Reset();
Craig Topperc0196b12014-04-14 00:51:57 +0000385 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000386 AntiDepBreak->StartBlock(BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000387}
388
389/// Schedule - Schedule the instruction range using list scheduling.
390///
Andrew Trick52226d42012-03-07 23:00:49 +0000391void SchedulePostRATDList::schedule() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000392 // Build the scheduling graph.
Andrew Trick52226d42012-03-07 23:00:49 +0000393 buildSchedGraph(AA);
Dan Gohmanb9543432009-02-10 23:27:53 +0000394
Craig Topperc0196b12014-04-14 00:51:57 +0000395 if (AntiDepBreak) {
Jim Grosbachd772bde2010-05-14 21:19:48 +0000396 unsigned Broken =
Andrew Trick8c207e42012-03-09 04:29:02 +0000397 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
398 EndIndex, DbgValues);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000399
David Goodwin80a03cc2009-11-20 19:32:48 +0000400 if (Broken != 0) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000401 // We made changes. Update the dependency graph.
402 // Theoretically we could update the graph in place:
403 // When a live range is changed to use a different register, remove
404 // the def's anti-dependence *and* output-dependence edges due to
405 // that register, and add new anti-dependence and output-dependence
406 // edges based on the next live range of the register.
Andrew Trick60cf03e2012-03-07 05:21:52 +0000407 ScheduleDAG::clearDAG();
Andrew Trick52226d42012-03-07 23:00:49 +0000408 buildSchedGraph(AA);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000409
David Goodwin83704852009-10-26 16:59:04 +0000410 NumFixedAnti += Broken;
Dan Gohmanb9543432009-02-10 23:27:53 +0000411 }
412 }
413
Krzysztof Parzyszekcd99e362016-03-08 16:54:20 +0000414 postprocessDAG();
415
David Greeneaa8ce382010-01-05 01:26:01 +0000416 DEBUG(dbgs() << "********** List Scheduling **********\n");
Matthias Braun9198c672015-11-06 20:59:02 +0000417 DEBUG(
418 for (const SUnit &SU : SUnits) {
419 SU.dumpAll(this);
420 dbgs() << '\n';
421 }
422 );
David Goodwin6021b4d2009-08-10 15:55:25 +0000423
Dan Gohmanb9543432009-02-10 23:27:53 +0000424 AvailableQueue.initNodes(SUnits);
David Goodwin80a03cc2009-11-20 19:32:48 +0000425 ListScheduleTopDown();
Dan Gohmanb9543432009-02-10 23:27:53 +0000426 AvailableQueue.releaseState();
427}
428
429/// Observe - Update liveness information to account for the current
430/// instruction, which will not be scheduled.
431///
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000432void SchedulePostRATDList::Observe(MachineInstr &MI, unsigned Count) {
Craig Topperc0196b12014-04-14 00:51:57 +0000433 if (AntiDepBreak)
Andrew Tricka316faa2012-03-07 23:00:52 +0000434 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohmanb9543432009-02-10 23:27:53 +0000435}
436
437/// FinishBlock - Clean up register live-range state.
438///
Andrew Trick52226d42012-03-07 23:00:49 +0000439void SchedulePostRATDList::finishBlock() {
Craig Topperc0196b12014-04-14 00:51:57 +0000440 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000441 AntiDepBreak->FinishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000442
443 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000444 ScheduleDAGInstrs::finishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000445}
446
Krzysztof Parzyszek5c61d112016-03-05 15:45:23 +0000447/// Apply each ScheduleDAGMutation step in order.
448void SchedulePostRATDList::postprocessDAG() {
449 for (auto &M : Mutations)
450 M->apply(this);
451}
452
Dan Gohman60cb69e2008-11-19 23:18:57 +0000453//===----------------------------------------------------------------------===//
454// Top-Down Scheduling
455//===----------------------------------------------------------------------===//
456
457/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000458/// the PendingQueue if the count reaches zero.
David Goodwin80a03cc2009-11-20 19:32:48 +0000459void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000460 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000461
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000462 if (SuccEdge->isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000463 --SuccSU->WeakPredsLeft;
464 return;
465 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000466#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000467 if (SuccSU->NumPredsLeft == 0) {
David Greeneaa8ce382010-01-05 01:26:01 +0000468 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000469 SuccSU->dump(this);
David Greeneaa8ce382010-01-05 01:26:01 +0000470 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000471 llvm_unreachable(nullptr);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000472 }
473#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000474 --SuccSU->NumPredsLeft;
475
Andrew Trick84f9ad92011-05-06 18:14:32 +0000476 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trickaab77fe2011-05-06 17:09:08 +0000477 // here as such:
478 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
479 //
480 // However, we lazily compute node depth instead. Note that
481 // ScheduleNodeTopDown has already updated the depth of this node which causes
482 // all descendents to be marked dirty. Setting the successor depth explicitly
483 // here would cause depth to be recomputed for all its ancestors. If the
484 // successor is not yet ready (because of a transitively redundant edge) then
485 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbachd772bde2010-05-14 21:19:48 +0000486
Dan Gohmanb9543432009-02-10 23:27:53 +0000487 // If all the node's predecessors are scheduled, this node is ready
488 // to be scheduled. Ignore the special ExitSU node.
489 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman60cb69e2008-11-19 23:18:57 +0000490 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000491}
492
493/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin80a03cc2009-11-20 19:32:48 +0000494void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000495 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +0000496 I != E; ++I) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000497 ReleaseSucc(SU, &*I);
David Goodwin8501dbbe2009-11-03 20:57:50 +0000498 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000499}
500
501/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
502/// count of its successors. If a successor pending count is zero, add it to
503/// the Available queue.
David Goodwin80a03cc2009-11-20 19:32:48 +0000504void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greeneaa8ce382010-01-05 01:26:01 +0000505 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000506 DEBUG(SU->dump(this));
Jim Grosbachd772bde2010-05-14 21:19:48 +0000507
Dan Gohman60cb69e2008-11-19 23:18:57 +0000508 Sequence.push_back(SU);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000509 assert(CurCycle >= SU->getDepth() &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000510 "Node scheduled above its depth!");
David Goodwin80a03cc2009-11-20 19:32:48 +0000511 SU->setDepthToAtLeast(CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000512
David Goodwin80a03cc2009-11-20 19:32:48 +0000513 ReleaseSuccessors(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000514 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000515 AvailableQueue.scheduledNode(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000516}
517
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000518/// emitNoop - Add a noop to the current instruction sequence.
519void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
520 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
521 HazardRec->EmitNoop();
Craig Topperc0196b12014-04-14 00:51:57 +0000522 Sequence.push_back(nullptr); // NULL here means noop
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000523 ++NumNoops;
524}
525
Dan Gohman60cb69e2008-11-19 23:18:57 +0000526/// ListScheduleTopDown - The main loop of list scheduling for top-down
527/// schedulers.
David Goodwin80a03cc2009-11-20 19:32:48 +0000528void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000529 unsigned CurCycle = 0;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000530
David Goodwin8501dbbe2009-11-03 20:57:50 +0000531 // We're scheduling top-down but we're visiting the regions in
532 // bottom-up order, so we don't know the hazards at the start of a
533 // region. So assume no hazards (this should usually be ok as most
534 // blocks are a single region).
535 HazardRec->Reset();
536
Dan Gohmanb9543432009-02-10 23:27:53 +0000537 // Release any successors of the special Entry node.
David Goodwin80a03cc2009-11-20 19:32:48 +0000538 ReleaseSuccessors(&EntrySU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000539
David Goodwin80a03cc2009-11-20 19:32:48 +0000540 // Add all leaves to Available queue.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000541 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
542 // It is available if it has no predecessors.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000543 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000544 AvailableQueue.push(&SUnits[i]);
545 SUnits[i].isAvailable = true;
546 }
547 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000548
David Goodwin1f8c7a72009-08-12 21:47:46 +0000549 // In any cycle where we can't schedule any instructions, we must
550 // stall or emit a noop, depending on the target.
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000551 bool CycleHasInsts = false;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000552
Dan Gohman60cb69e2008-11-19 23:18:57 +0000553 // While Available queue is not empty, grab the node with the highest
554 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmanceac7c32009-01-16 01:33:36 +0000555 std::vector<SUnit*> NotReady;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000556 Sequence.reserve(SUnits.size());
557 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
558 // Check to see if any of the pending instructions are ready to issue. If
559 // so, add them to the available queue.
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000560 unsigned MinDepth = ~0u;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000561 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000562 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000563 AvailableQueue.push(PendingQueue[i]);
564 PendingQueue[i]->isAvailable = true;
565 PendingQueue[i] = PendingQueue.back();
566 PendingQueue.pop_back();
567 --i; --e;
David Goodwin80a03cc2009-11-20 19:32:48 +0000568 } else if (PendingQueue[i]->getDepth() < MinDepth)
569 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000570 }
David Goodwinebd694b2009-08-11 17:35:23 +0000571
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000572 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinebd694b2009-08-11 17:35:23 +0000573
Craig Topperc0196b12014-04-14 00:51:57 +0000574 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohmanceac7c32009-01-16 01:33:36 +0000575 bool HasNoopHazards = false;
576 while (!AvailableQueue.empty()) {
577 SUnit *CurSUnit = AvailableQueue.pop();
578
579 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000580 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000581 if (HT == ScheduleHazardRecognizer::NoHazard) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000582 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
583 if (!NotPreferredSUnit) {
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000584 // If this is the first non-preferred node for this cycle, then
585 // record it and continue searching for a preferred node. If this
586 // is not the first non-preferred node, then treat it as though
587 // there had been a hazard.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000588 NotPreferredSUnit = CurSUnit;
589 continue;
590 }
591 } else {
592 FoundSUnit = CurSUnit;
593 break;
594 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000595 }
596
597 // Remember if this is a noop hazard.
598 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
599
600 NotReady.push_back(CurSUnit);
601 }
602
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000603 // If we have a non-preferred node, push it back onto the available list.
604 // If we did not find a preferred node, then schedule this first
605 // non-preferred node.
606 if (NotPreferredSUnit) {
607 if (!FoundSUnit) {
608 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
609 FoundSUnit = NotPreferredSUnit;
610 } else {
611 AvailableQueue.push(NotPreferredSUnit);
612 }
613
Craig Topperc0196b12014-04-14 00:51:57 +0000614 NotPreferredSUnit = nullptr;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000615 }
616
Dan Gohmanceac7c32009-01-16 01:33:36 +0000617 // Add the nodes that aren't ready back onto the available list.
618 if (!NotReady.empty()) {
619 AvailableQueue.push_all(NotReady);
620 NotReady.clear();
621 }
622
David Goodwin8501dbbe2009-11-03 20:57:50 +0000623 // If we found a node to schedule...
Dan Gohman60cb69e2008-11-19 23:18:57 +0000624 if (FoundSUnit) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000625 // If we need to emit noops prior to this instruction, then do so.
626 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
627 for (unsigned i = 0; i != NumPreNoops; ++i)
628 emitNoop(CurCycle);
629
David Goodwin8501dbbe2009-11-03 20:57:50 +0000630 // ... schedule the node...
David Goodwin80a03cc2009-11-20 19:32:48 +0000631 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000632 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000633 CycleHasInsts = true;
Andrew Trick18c9b372011-06-01 03:27:56 +0000634 if (HazardRec->atIssueLimit()) {
635 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
636 HazardRec->AdvanceCycle();
637 ++CurCycle;
638 CycleHasInsts = false;
639 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000640 } else {
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000641 if (CycleHasInsts) {
David Greeneaa8ce382010-01-05 01:26:01 +0000642 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000643 HazardRec->AdvanceCycle();
644 } else if (!HasNoopHazards) {
645 // Otherwise, we have a pipeline stall, but no other problem,
646 // just advance the current cycle and try again.
David Greeneaa8ce382010-01-05 01:26:01 +0000647 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000648 HazardRec->AdvanceCycle();
David Goodwin80a03cc2009-11-20 19:32:48 +0000649 ++NumStalls;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000650 } else {
651 // Otherwise, we have no instructions to issue and we have instructions
652 // that will fault if we don't do this right. This is the case for
653 // processors without pipeline interlocks and other cases.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000654 emitNoop(CurCycle);
David Goodwin1f8c7a72009-08-12 21:47:46 +0000655 }
656
Dan Gohmanceac7c32009-01-16 01:33:36 +0000657 ++CurCycle;
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000658 CycleHasInsts = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000659 }
660 }
661
662#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000663 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
664 unsigned Noops = 0;
665 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
666 if (!Sequence[i])
667 ++Noops;
668 assert(Sequence.size() - Noops == ScheduledNodes &&
669 "The number of nodes scheduled doesn't match the expected number!");
670#endif // NDEBUG
Dan Gohman60cb69e2008-11-19 23:18:57 +0000671}
Andrew Tricke932bb72012-03-07 05:21:44 +0000672
673// EmitSchedule - Emit the machine code in scheduled order.
674void SchedulePostRATDList::EmitSchedule() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000675 RegionBegin = RegionEnd;
Andrew Tricke932bb72012-03-07 05:21:44 +0000676
677 // If first instruction was a DBG_VALUE then put it back.
678 if (FirstDbgValue)
Andrew Trick8c207e42012-03-09 04:29:02 +0000679 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Tricke932bb72012-03-07 05:21:44 +0000680
681 // Then re-insert them according to the given schedule.
682 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
683 if (SUnit *SU = Sequence[i])
Andrew Trick8c207e42012-03-09 04:29:02 +0000684 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Tricke932bb72012-03-07 05:21:44 +0000685 else
686 // Null SUnit* is a noop.
Andrew Trick8c207e42012-03-09 04:29:02 +0000687 TII->insertNoop(*BB, RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000688
689 // Update the Begin iterator, as the first instruction in the block
690 // may have been scheduled later.
691 if (i == 0)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000692 RegionBegin = std::prev(RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000693 }
694
695 // Reinsert any remaining debug_values.
696 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
697 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000698 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Tricke932bb72012-03-07 05:21:44 +0000699 MachineInstr *DbgValue = P.first;
700 MachineBasicBlock::iterator OrigPrivMI = P.second;
701 BB->splice(++OrigPrivMI, BB, DbgValue);
702 }
703 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000704 FirstDbgValue = nullptr;
Andrew Tricke932bb72012-03-07 05:21:44 +0000705}