blob: 506ee01788473ed43c813452b3b9325a65cce0e9 [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
Dale Johannesen2182f062007-07-13 17:13:54 +000021#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "AggressiveAntiDepBreaker.h"
23#include "AntiDepBreaker.h"
24#include "CriticalAntiDepBreaker.h"
25#include "llvm/ADT/BitVector.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000028#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000029#include "llvm/CodeGen/MachineDominators.h"
David Goodwinbe3039e2009-10-01 19:45:32 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000032#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanad2134d2008-11-25 00:52:40 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000034#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Trick9a0c5832012-03-07 23:01:06 +000035#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohmanceac7c32009-01-16 01:33:36 +000036#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/CodeGen/SchedulerRegistry.h"
David Goodwine056d102009-10-26 22:31:16 +000038#include "llvm/Support/CommandLine.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000041#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetLowering.h"
44#include "llvm/Target/TargetMachine.h"
45#include "llvm/Target/TargetRegisterInfo.h"
46#include "llvm/Target/TargetSubtargetInfo.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000047using namespace llvm;
48
Chandler Carruth1b9dde02014-04-22 02:02:50 +000049#define DEBUG_TYPE "post-RA-sched"
50
Dan Gohmanceac7c32009-01-16 01:33:36 +000051STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman60cb69e2008-11-19 23:18:57 +000052STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin83704852009-10-26 16:59:04 +000053STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman60cb69e2008-11-19 23:18:57 +000054
David Goodwin9a051a52009-10-01 21:46:35 +000055// Post-RA scheduling is enabled with
Evan Cheng0d639a22011-07-01 21:01:15 +000056// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin9a051a52009-10-01 21:46:35 +000057// override the target.
58static cl::opt<bool>
59EnablePostRAScheduler("post-RA-scheduler",
60 cl::desc("Enable scheduling after register allocation"),
David Goodwin1cc6dd92009-10-01 22:19:57 +000061 cl::init(false), cl::Hidden);
David Goodwin83704852009-10-26 16:59:04 +000062static cl::opt<std::string>
Dan Gohmanad2134d2008-11-25 00:52:40 +000063EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin83704852009-10-26 16:59:04 +000064 cl::desc("Break post-RA scheduling anti-dependencies: "
65 "\"critical\", \"all\", or \"none\""),
66 cl::init("none"), cl::Hidden);
Dan Gohmanceac7c32009-01-16 01:33:36 +000067
David Goodwin7f651692009-09-01 18:34:03 +000068// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
69static cl::opt<int>
70DebugDiv("postra-sched-debugdiv",
71 cl::desc("Debug control MBBs that are scheduled"),
72 cl::init(0), cl::Hidden);
73static cl::opt<int>
74DebugMod("postra-sched-debugmod",
75 cl::desc("Debug control MBBs that are scheduled"),
76 cl::init(0), cl::Hidden);
77
David Goodwin661ea982009-10-26 19:41:00 +000078AntiDepBreaker::~AntiDepBreaker() { }
79
Dale Johannesen2182f062007-07-13 17:13:54 +000080namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000081 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng2d51c7c2010-06-18 23:09:54 +000082 const TargetInstrInfo *TII;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000083 RegisterClassInfo RegClassInfo;
Dan Gohman87b02d52009-10-09 23:27:56 +000084
Dale Johannesen2182f062007-07-13 17:13:54 +000085 public:
86 static char ID;
Andrew Trickdf7e3762012-02-08 21:22:53 +000087 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohmanad2134d2008-11-25 00:52:40 +000088
Craig Topper4584cd52014-03-07 09:26:03 +000089 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000090 AU.setPreservesCFG();
Dan Gohman87b02d52009-10-09 23:27:56 +000091 AU.addRequired<AliasAnalysis>();
Andrew Trickdf7e3762012-02-08 21:22:53 +000092 AU.addRequired<TargetPassConfig>();
Dan Gohmandddc1ac2008-12-16 03:25:46 +000093 AU.addRequired<MachineDominatorTree>();
94 AU.addPreserved<MachineDominatorTree>();
95 AU.addRequired<MachineLoopInfo>();
96 AU.addPreserved<MachineLoopInfo>();
97 MachineFunctionPass::getAnalysisUsage(AU);
98 }
99
Craig Topper4584cd52014-03-07 09:26:03 +0000100 bool runOnMachineFunction(MachineFunction &Fn) override;
Sanjay Patela2f658d2014-07-15 22:39:58 +0000101
102 bool enablePostRAScheduler(
103 const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel,
104 TargetSubtargetInfo::AntiDepBreakMode &Mode,
105 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const;
Dale Johannesen2182f062007-07-13 17:13:54 +0000106 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000107 char PostRAScheduler::ID = 0;
108
Nick Lewycky02d5f772009-10-25 06:33:48 +0000109 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000110 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohman682a2d12009-10-21 01:44:44 +0000111 ///
Dan Gohman60cb69e2008-11-19 23:18:57 +0000112 LatencyPriorityQueue AvailableQueue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000113
Dan Gohman60cb69e2008-11-19 23:18:57 +0000114 /// PendingQueue - This contains all of the instructions whose operands have
115 /// been issued, but their results are not ready yet (due to the latency of
116 /// the operation). Once the operands becomes available, the instruction is
117 /// added to the AvailableQueue.
118 std::vector<SUnit*> PendingQueue;
119
Dan Gohmanceac7c32009-01-16 01:33:36 +0000120 /// HazardRec - The hazard recognizer to use.
121 ScheduleHazardRecognizer *HazardRec;
122
David Goodwin83704852009-10-26 16:59:04 +0000123 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
124 AntiDepBreaker *AntiDepBreak;
125
Dan Gohman87b02d52009-10-09 23:27:56 +0000126 /// AA - AliasAnalysis for making memory reference queries.
127 AliasAnalysis *AA;
128
Andrew Trick60cf03e2012-03-07 05:21:52 +0000129 /// The schedule. Null SUnit*'s represent noop instructions.
130 std::vector<SUnit*> Sequence;
131
Andrew Tricka53e1012013-08-23 17:48:33 +0000132 /// The index in BB of RegionEnd.
133 ///
134 /// This is the instruction number from the top of the current block, not
135 /// the SlotIndex. It is only used by the AntiDepBreaker.
136 unsigned EndIndex;
137
Dan Gohmanad2134d2008-11-25 00:52:40 +0000138 public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000139 SchedulePostRATDList(
140 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000141 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng0d639a22011-07-01 21:01:15 +0000142 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper760b1342012-02-22 05:59:10 +0000143 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000144
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000145 ~SchedulePostRATDList();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000146
Andrew Trick52226d42012-03-07 23:00:49 +0000147 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohmanb9543432009-02-10 23:27:53 +0000148 /// this block.
149 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000150 void startBlock(MachineBasicBlock *BB) override;
Dan Gohmanb9543432009-02-10 23:27:53 +0000151
Andrew Tricka53e1012013-08-23 17:48:33 +0000152 // Set the index of RegionEnd within the current BB.
153 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
154
Andrew Trick60cf03e2012-03-07 05:21:52 +0000155 /// Initialize the scheduler state for the next scheduling region.
Craig Topper4584cd52014-03-07 09:26:03 +0000156 void enterRegion(MachineBasicBlock *bb,
157 MachineBasicBlock::iterator begin,
158 MachineBasicBlock::iterator end,
159 unsigned regioninstrs) override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000160
161 /// Notify that the scheduler has finished scheduling the current region.
Craig Topper4584cd52014-03-07 09:26:03 +0000162 void exitRegion() override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000163
Dan Gohmanb9543432009-02-10 23:27:53 +0000164 /// Schedule - Schedule the instruction range using list scheduling.
165 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000166 void schedule() override;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000167
Andrew Tricke932bb72012-03-07 05:21:44 +0000168 void EmitSchedule();
169
Dan Gohman682a2d12009-10-21 01:44:44 +0000170 /// Observe - Update liveness information to account for the current
171 /// instruction, which will not be scheduled.
172 ///
173 void Observe(MachineInstr *MI, unsigned Count);
174
Andrew Trick52226d42012-03-07 23:00:49 +0000175 /// finishBlock - Clean up register live-range state.
Dan Gohman682a2d12009-10-21 01:44:44 +0000176 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000177 void finishBlock() override;
Dan Gohman682a2d12009-10-21 01:44:44 +0000178
Dan Gohman60cb69e2008-11-19 23:18:57 +0000179 private:
David Goodwin80a03cc2009-11-20 19:32:48 +0000180 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
181 void ReleaseSuccessors(SUnit *SU);
182 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
183 void ListScheduleTopDown();
Jim Grosbachd772bde2010-05-14 21:19:48 +0000184
Andrew Trickedee68c2012-03-07 05:21:40 +0000185 void dumpSchedule() const;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000186 void emitNoop(unsigned CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000187 };
Dale Johannesen2182f062007-07-13 17:13:54 +0000188}
189
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000190char &llvm::PostRASchedulerID = PostRAScheduler::ID;
191
192INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
193 "Post RA top-down list latency scheduler", false, false)
194
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000195SchedulePostRATDList::SchedulePostRATDList(
196 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000197 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng0d639a22011-07-01 21:01:15 +0000198 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper760b1342012-02-22 05:59:10 +0000199 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trick6b104f82013-12-28 21:56:55 +0000200 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA), EndIndex(0) {
201
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000202 const TargetMachine &TM = MF.getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +0000203 const InstrItineraryData *InstrItins =
204 TM.getSubtargetImpl()->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000205 HazardRec =
Eric Christopherd9134482014-08-04 21:25:23 +0000206 TM.getSubtargetImpl()->getInstrInfo()->CreateTargetPostRAHazardRecognizer(
207 InstrItins, this);
Preston Gurd9a091472012-04-23 21:39:35 +0000208
209 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
210 MRI.tracksLiveness()) &&
211 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000212 AntiDepBreak =
Evan Cheng0d639a22011-07-01 21:01:15 +0000213 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000214 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng0d639a22011-07-01 21:01:15 +0000215 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Craig Topperc0196b12014-04-14 00:51:57 +0000216 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000217}
218
219SchedulePostRATDList::~SchedulePostRATDList() {
220 delete HazardRec;
221 delete AntiDepBreak;
222}
223
Andrew Trick60cf03e2012-03-07 05:21:52 +0000224/// Initialize state associated with the next scheduling region.
225void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
226 MachineBasicBlock::iterator begin,
227 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000228 unsigned regioninstrs) {
229 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000230 Sequence.clear();
231}
232
233/// Print the schedule before exiting the region.
234void SchedulePostRATDList::exitRegion() {
235 DEBUG({
236 dbgs() << "*** Final schedule ***\n";
237 dumpSchedule();
238 dbgs() << '\n';
239 });
240 ScheduleDAGInstrs::exitRegion();
241}
242
Manman Ren19f49ac2012-09-11 22:23:19 +0000243#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickedee68c2012-03-07 05:21:40 +0000244/// dumpSchedule - dump the scheduled Sequence.
245void SchedulePostRATDList::dumpSchedule() const {
246 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
247 if (SUnit *SU = Sequence[i])
248 SU->dump(this);
249 else
250 dbgs() << "**** NOOP ****\n";
251 }
252}
Manman Ren742534c2012-09-06 19:06:06 +0000253#endif
Andrew Trickedee68c2012-03-07 05:21:40 +0000254
Sanjay Patela2f658d2014-07-15 22:39:58 +0000255bool PostRAScheduler::enablePostRAScheduler(
256 const TargetSubtargetInfo &ST,
257 CodeGenOpt::Level OptLevel,
258 TargetSubtargetInfo::AntiDepBreakMode &Mode,
259 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const {
260 Mode = ST.getAntiDepBreakMode();
261 ST.getCriticalPathRCs(CriticalPathRCs);
262 return ST.enablePostMachineScheduler() &&
263 OptLevel >= ST.getOptLevelToEnablePostRAScheduler();
264}
265
Dan Gohman60cb69e2008-11-19 23:18:57 +0000266bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000267 if (skipOptnoneFunction(*Fn.getFunction()))
268 return false;
269
Eric Christopherd9134482014-08-04 21:25:23 +0000270 TII = Fn.getTarget().getSubtargetImpl()->getInstrInfo();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000271 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
272 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
273 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000274 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
275
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000276 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman26e9b892009-10-10 00:15:38 +0000277
David Goodwin9a051a52009-10-01 21:46:35 +0000278 // Check for explicit enable/disable of post-ra scheduling.
Evan Cheng7fae11b2011-12-14 02:11:42 +0000279 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
280 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper760b1342012-02-22 05:59:10 +0000281 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin9a051a52009-10-01 21:46:35 +0000282 if (EnablePostRAScheduler.getPosition() > 0) {
283 if (!EnablePostRAScheduler)
Evan Cheng8b614762009-10-16 06:10:34 +0000284 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000285 } else {
Evan Cheng8b614762009-10-16 06:10:34 +0000286 // Check that post-RA scheduling is enabled for this target.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000287 // This may upgrade the AntiDepMode.
Sanjay Patela2f658d2014-07-15 22:39:58 +0000288 const TargetSubtargetInfo &ST =
289 Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
290 if (!enablePostRAScheduler(ST, PassConfig->getOptLevel(),
291 AntiDepMode, CriticalPathRCs))
Evan Cheng8b614762009-10-16 06:10:34 +0000292 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000293 }
David Goodwin17199b52009-09-30 00:10:16 +0000294
David Goodwin02ad4cb2009-10-22 23:19:17 +0000295 // Check for antidep breaking override...
296 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng0d639a22011-07-01 21:01:15 +0000297 AntiDepMode = (EnableAntiDepBreaking == "all")
298 ? TargetSubtargetInfo::ANTIDEP_ALL
299 : ((EnableAntiDepBreaking == "critical")
300 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
301 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin02ad4cb2009-10-22 23:19:17 +0000302 }
303
David Greeneaa8ce382010-01-05 01:26:01 +0000304 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesen2182f062007-07-13 17:13:54 +0000305
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000306 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000307 CriticalPathRCs);
Dan Gohman619ef482009-01-15 19:20:50 +0000308
Dale Johannesen2182f062007-07-13 17:13:54 +0000309 // Loop over all of the basic blocks
310 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000311 MBB != MBBe; ++MBB) {
David Goodwin7f651692009-09-01 18:34:03 +0000312#ifndef NDEBUG
313 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
314 if (DebugDiv > 0) {
315 static int bbcnt = 0;
316 if (bbcnt++ % DebugDiv != DebugMod)
317 continue;
Craig Toppera538d832012-08-22 06:07:19 +0000318 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000319 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin7f651692009-09-01 18:34:03 +0000320 }
321#endif
322
Dan Gohmanb9543432009-02-10 23:27:53 +0000323 // Initialize register live-range state for scheduling in this block.
Andrew Trick52226d42012-03-07 23:00:49 +0000324 Scheduler.startBlock(MBB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000325
Dan Gohman5f8a2592009-01-16 22:10:20 +0000326 // Schedule each sequence of instructions not interrupted by a label
327 // or anything else that effectively needs to shut down scheduling.
Dan Gohmanb9543432009-02-10 23:27:53 +0000328 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohmandfaf6462009-02-11 04:27:20 +0000329 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohmanb9543432009-02-10 23:27:53 +0000330 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000331 MachineInstr *MI = std::prev(I);
Andrew Tricka53e1012013-08-23 17:48:33 +0000332 --Count;
Jakob Stoklund Olesena793a592012-02-23 17:54:21 +0000333 // Calls are not scheduling boundaries before register allocation, but
334 // post-ra we don't gain anything by scheduling across calls since we
335 // don't need to worry about register pressure.
336 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Tricka53e1012013-08-23 17:48:33 +0000337 Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count);
338 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000339 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000340 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000341 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000342 Current = MI;
Andrew Tricka53e1012013-08-23 17:48:33 +0000343 CurrentCount = Count;
Dan Gohman64613ac2009-03-10 18:10:43 +0000344 Scheduler.Observe(MI, CurrentCount);
Dan Gohman5f8a2592009-01-16 22:10:20 +0000345 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000346 I = MI;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000347 if (MI->isBundle())
348 Count -= MI->getBundleSize();
Dan Gohmand5643532009-02-03 18:57:45 +0000349 }
Dan Gohmandfaf6462009-02-11 04:27:20 +0000350 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sandsbe69d602009-03-11 09:04:34 +0000351 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman64613ac2009-03-10 18:10:43 +0000352 "Instruction count mismatch!");
Andrew Trick60cf03e2012-03-07 05:21:52 +0000353 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Tricka53e1012013-08-23 17:48:33 +0000354 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000355 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000356 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000357 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000358
359 // Clean up register live-range state.
Andrew Trick52226d42012-03-07 23:00:49 +0000360 Scheduler.finishBlock();
David Goodwinae6bc822009-08-25 17:03:05 +0000361
David Goodwin6c08cfc2009-09-03 22:15:25 +0000362 // Update register kills
Andrew Trick6b104f82013-12-28 21:56:55 +0000363 Scheduler.fixupKills(MBB);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000364 }
Dale Johannesen2182f062007-07-13 17:13:54 +0000365
366 return true;
367}
Jim Grosbachd772bde2010-05-14 21:19:48 +0000368
Dan Gohmanb9543432009-02-10 23:27:53 +0000369/// StartBlock - Initialize register live-range state for scheduling in
370/// this block.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000371///
Andrew Trick52226d42012-03-07 23:00:49 +0000372void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000373 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000374 ScheduleDAGInstrs::startBlock(BB);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000375
David Goodwin83704852009-10-26 16:59:04 +0000376 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin6021b4d2009-08-10 15:55:25 +0000377 HazardRec->Reset();
Craig Topperc0196b12014-04-14 00:51:57 +0000378 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000379 AntiDepBreak->StartBlock(BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000380}
381
382/// Schedule - Schedule the instruction range using list scheduling.
383///
Andrew Trick52226d42012-03-07 23:00:49 +0000384void SchedulePostRATDList::schedule() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000385 // Build the scheduling graph.
Andrew Trick52226d42012-03-07 23:00:49 +0000386 buildSchedGraph(AA);
Dan Gohmanb9543432009-02-10 23:27:53 +0000387
Craig Topperc0196b12014-04-14 00:51:57 +0000388 if (AntiDepBreak) {
Jim Grosbachd772bde2010-05-14 21:19:48 +0000389 unsigned Broken =
Andrew Trick8c207e42012-03-09 04:29:02 +0000390 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
391 EndIndex, DbgValues);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000392
David Goodwin80a03cc2009-11-20 19:32:48 +0000393 if (Broken != 0) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000394 // We made changes. Update the dependency graph.
395 // Theoretically we could update the graph in place:
396 // When a live range is changed to use a different register, remove
397 // the def's anti-dependence *and* output-dependence edges due to
398 // that register, and add new anti-dependence and output-dependence
399 // edges based on the next live range of the register.
Andrew Trick60cf03e2012-03-07 05:21:52 +0000400 ScheduleDAG::clearDAG();
Andrew Trick52226d42012-03-07 23:00:49 +0000401 buildSchedGraph(AA);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000402
David Goodwin83704852009-10-26 16:59:04 +0000403 NumFixedAnti += Broken;
Dan Gohmanb9543432009-02-10 23:27:53 +0000404 }
405 }
406
David Greeneaa8ce382010-01-05 01:26:01 +0000407 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwin6021b4d2009-08-10 15:55:25 +0000408 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
409 SUnits[su].dumpAll(this));
410
Dan Gohmanb9543432009-02-10 23:27:53 +0000411 AvailableQueue.initNodes(SUnits);
David Goodwin80a03cc2009-11-20 19:32:48 +0000412 ListScheduleTopDown();
Dan Gohmanb9543432009-02-10 23:27:53 +0000413 AvailableQueue.releaseState();
414}
415
416/// Observe - Update liveness information to account for the current
417/// instruction, which will not be scheduled.
418///
Dan Gohmandfaf6462009-02-11 04:27:20 +0000419void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Craig Topperc0196b12014-04-14 00:51:57 +0000420 if (AntiDepBreak)
Andrew Tricka316faa2012-03-07 23:00:52 +0000421 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohmanb9543432009-02-10 23:27:53 +0000422}
423
424/// FinishBlock - Clean up register live-range state.
425///
Andrew Trick52226d42012-03-07 23:00:49 +0000426void SchedulePostRATDList::finishBlock() {
Craig Topperc0196b12014-04-14 00:51:57 +0000427 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000428 AntiDepBreak->FinishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000429
430 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000431 ScheduleDAGInstrs::finishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000432}
433
Dan Gohman60cb69e2008-11-19 23:18:57 +0000434//===----------------------------------------------------------------------===//
435// Top-Down Scheduling
436//===----------------------------------------------------------------------===//
437
438/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000439/// the PendingQueue if the count reaches zero.
David Goodwin80a03cc2009-11-20 19:32:48 +0000440void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000441 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000442
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000443 if (SuccEdge->isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000444 --SuccSU->WeakPredsLeft;
445 return;
446 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000447#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000448 if (SuccSU->NumPredsLeft == 0) {
David Greeneaa8ce382010-01-05 01:26:01 +0000449 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000450 SuccSU->dump(this);
David Greeneaa8ce382010-01-05 01:26:01 +0000451 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000452 llvm_unreachable(nullptr);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000453 }
454#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000455 --SuccSU->NumPredsLeft;
456
Andrew Trick84f9ad92011-05-06 18:14:32 +0000457 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trickaab77fe2011-05-06 17:09:08 +0000458 // here as such:
459 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
460 //
461 // However, we lazily compute node depth instead. Note that
462 // ScheduleNodeTopDown has already updated the depth of this node which causes
463 // all descendents to be marked dirty. Setting the successor depth explicitly
464 // here would cause depth to be recomputed for all its ancestors. If the
465 // successor is not yet ready (because of a transitively redundant edge) then
466 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbachd772bde2010-05-14 21:19:48 +0000467
Dan Gohmanb9543432009-02-10 23:27:53 +0000468 // If all the node's predecessors are scheduled, this node is ready
469 // to be scheduled. Ignore the special ExitSU node.
470 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman60cb69e2008-11-19 23:18:57 +0000471 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000472}
473
474/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin80a03cc2009-11-20 19:32:48 +0000475void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000476 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +0000477 I != E; ++I) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000478 ReleaseSucc(SU, &*I);
David Goodwin8501dbbe2009-11-03 20:57:50 +0000479 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000480}
481
482/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
483/// count of its successors. If a successor pending count is zero, add it to
484/// the Available queue.
David Goodwin80a03cc2009-11-20 19:32:48 +0000485void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greeneaa8ce382010-01-05 01:26:01 +0000486 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000487 DEBUG(SU->dump(this));
Jim Grosbachd772bde2010-05-14 21:19:48 +0000488
Dan Gohman60cb69e2008-11-19 23:18:57 +0000489 Sequence.push_back(SU);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000490 assert(CurCycle >= SU->getDepth() &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000491 "Node scheduled above its depth!");
David Goodwin80a03cc2009-11-20 19:32:48 +0000492 SU->setDepthToAtLeast(CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000493
David Goodwin80a03cc2009-11-20 19:32:48 +0000494 ReleaseSuccessors(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000495 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000496 AvailableQueue.scheduledNode(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000497}
498
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000499/// emitNoop - Add a noop to the current instruction sequence.
500void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
501 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
502 HazardRec->EmitNoop();
Craig Topperc0196b12014-04-14 00:51:57 +0000503 Sequence.push_back(nullptr); // NULL here means noop
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000504 ++NumNoops;
505}
506
Dan Gohman60cb69e2008-11-19 23:18:57 +0000507/// ListScheduleTopDown - The main loop of list scheduling for top-down
508/// schedulers.
David Goodwin80a03cc2009-11-20 19:32:48 +0000509void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000510 unsigned CurCycle = 0;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000511
David Goodwin8501dbbe2009-11-03 20:57:50 +0000512 // We're scheduling top-down but we're visiting the regions in
513 // bottom-up order, so we don't know the hazards at the start of a
514 // region. So assume no hazards (this should usually be ok as most
515 // blocks are a single region).
516 HazardRec->Reset();
517
Dan Gohmanb9543432009-02-10 23:27:53 +0000518 // Release any successors of the special Entry node.
David Goodwin80a03cc2009-11-20 19:32:48 +0000519 ReleaseSuccessors(&EntrySU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000520
David Goodwin80a03cc2009-11-20 19:32:48 +0000521 // Add all leaves to Available queue.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000522 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
523 // It is available if it has no predecessors.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000524 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000525 AvailableQueue.push(&SUnits[i]);
526 SUnits[i].isAvailable = true;
527 }
528 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000529
David Goodwin1f8c7a72009-08-12 21:47:46 +0000530 // In any cycle where we can't schedule any instructions, we must
531 // stall or emit a noop, depending on the target.
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000532 bool CycleHasInsts = false;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000533
Dan Gohman60cb69e2008-11-19 23:18:57 +0000534 // While Available queue is not empty, grab the node with the highest
535 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmanceac7c32009-01-16 01:33:36 +0000536 std::vector<SUnit*> NotReady;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000537 Sequence.reserve(SUnits.size());
538 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
539 // Check to see if any of the pending instructions are ready to issue. If
540 // so, add them to the available queue.
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000541 unsigned MinDepth = ~0u;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000542 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000543 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000544 AvailableQueue.push(PendingQueue[i]);
545 PendingQueue[i]->isAvailable = true;
546 PendingQueue[i] = PendingQueue.back();
547 PendingQueue.pop_back();
548 --i; --e;
David Goodwin80a03cc2009-11-20 19:32:48 +0000549 } else if (PendingQueue[i]->getDepth() < MinDepth)
550 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000551 }
David Goodwinebd694b2009-08-11 17:35:23 +0000552
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000553 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinebd694b2009-08-11 17:35:23 +0000554
Craig Topperc0196b12014-04-14 00:51:57 +0000555 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohmanceac7c32009-01-16 01:33:36 +0000556 bool HasNoopHazards = false;
557 while (!AvailableQueue.empty()) {
558 SUnit *CurSUnit = AvailableQueue.pop();
559
560 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000561 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000562 if (HT == ScheduleHazardRecognizer::NoHazard) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000563 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
564 if (!NotPreferredSUnit) {
565 // If this is the first non-preferred node for this cycle, then
566 // record it and continue searching for a preferred node. If this
567 // is not the first non-preferred node, then treat it as though
568 // there had been a hazard.
569 NotPreferredSUnit = CurSUnit;
570 continue;
571 }
572 } else {
573 FoundSUnit = CurSUnit;
574 break;
575 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000576 }
577
578 // Remember if this is a noop hazard.
579 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
580
581 NotReady.push_back(CurSUnit);
582 }
583
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000584 // If we have a non-preferred node, push it back onto the available list.
585 // If we did not find a preferred node, then schedule this first
586 // non-preferred node.
587 if (NotPreferredSUnit) {
588 if (!FoundSUnit) {
589 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
590 FoundSUnit = NotPreferredSUnit;
591 } else {
592 AvailableQueue.push(NotPreferredSUnit);
593 }
594
Craig Topperc0196b12014-04-14 00:51:57 +0000595 NotPreferredSUnit = nullptr;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000596 }
597
Dan Gohmanceac7c32009-01-16 01:33:36 +0000598 // Add the nodes that aren't ready back onto the available list.
599 if (!NotReady.empty()) {
600 AvailableQueue.push_all(NotReady);
601 NotReady.clear();
602 }
603
David Goodwin8501dbbe2009-11-03 20:57:50 +0000604 // If we found a node to schedule...
Dan Gohman60cb69e2008-11-19 23:18:57 +0000605 if (FoundSUnit) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000606 // If we need to emit noops prior to this instruction, then do so.
607 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
608 for (unsigned i = 0; i != NumPreNoops; ++i)
609 emitNoop(CurCycle);
610
David Goodwin8501dbbe2009-11-03 20:57:50 +0000611 // ... schedule the node...
David Goodwin80a03cc2009-11-20 19:32:48 +0000612 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000613 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000614 CycleHasInsts = true;
Andrew Trick18c9b372011-06-01 03:27:56 +0000615 if (HazardRec->atIssueLimit()) {
616 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
617 HazardRec->AdvanceCycle();
618 ++CurCycle;
619 CycleHasInsts = false;
620 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000621 } else {
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000622 if (CycleHasInsts) {
David Greeneaa8ce382010-01-05 01:26:01 +0000623 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000624 HazardRec->AdvanceCycle();
625 } else if (!HasNoopHazards) {
626 // Otherwise, we have a pipeline stall, but no other problem,
627 // just advance the current cycle and try again.
David Greeneaa8ce382010-01-05 01:26:01 +0000628 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000629 HazardRec->AdvanceCycle();
David Goodwin80a03cc2009-11-20 19:32:48 +0000630 ++NumStalls;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000631 } else {
632 // Otherwise, we have no instructions to issue and we have instructions
633 // that will fault if we don't do this right. This is the case for
634 // processors without pipeline interlocks and other cases.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000635 emitNoop(CurCycle);
David Goodwin1f8c7a72009-08-12 21:47:46 +0000636 }
637
Dan Gohmanceac7c32009-01-16 01:33:36 +0000638 ++CurCycle;
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000639 CycleHasInsts = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000640 }
641 }
642
643#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000644 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
645 unsigned Noops = 0;
646 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
647 if (!Sequence[i])
648 ++Noops;
649 assert(Sequence.size() - Noops == ScheduledNodes &&
650 "The number of nodes scheduled doesn't match the expected number!");
651#endif // NDEBUG
Dan Gohman60cb69e2008-11-19 23:18:57 +0000652}
Andrew Tricke932bb72012-03-07 05:21:44 +0000653
654// EmitSchedule - Emit the machine code in scheduled order.
655void SchedulePostRATDList::EmitSchedule() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000656 RegionBegin = RegionEnd;
Andrew Tricke932bb72012-03-07 05:21:44 +0000657
658 // If first instruction was a DBG_VALUE then put it back.
659 if (FirstDbgValue)
Andrew Trick8c207e42012-03-09 04:29:02 +0000660 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Tricke932bb72012-03-07 05:21:44 +0000661
662 // Then re-insert them according to the given schedule.
663 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
664 if (SUnit *SU = Sequence[i])
Andrew Trick8c207e42012-03-09 04:29:02 +0000665 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Tricke932bb72012-03-07 05:21:44 +0000666 else
667 // Null SUnit* is a noop.
Andrew Trick8c207e42012-03-09 04:29:02 +0000668 TII->insertNoop(*BB, RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000669
670 // Update the Begin iterator, as the first instruction in the block
671 // may have been scheduled later.
672 if (i == 0)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000673 RegionBegin = std::prev(RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000674 }
675
676 // Reinsert any remaining debug_values.
677 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
678 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000679 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Tricke932bb72012-03-07 05:21:44 +0000680 MachineInstr *DbgValue = P.first;
681 MachineBasicBlock::iterator OrigPrivMI = P.second;
682 BB->splice(++OrigPrivMI, BB, DbgValue);
683 }
684 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000685 FirstDbgValue = nullptr;
Andrew Tricke932bb72012-03-07 05:21:44 +0000686}