blob: 777f77f0dc7b602bce34153b7b99a1105df46d11 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetSubtargetInfo.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
Craig Topper4584cd52014-03-07 09:26:03 +000099 bool runOnMachineFunction(MachineFunction &Fn) override;
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000100
Sanjay Patela2f658d2014-07-15 22:39:58 +0000101 bool enablePostRAScheduler(
102 const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel,
103 TargetSubtargetInfo::AntiDepBreakMode &Mode,
104 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const;
Dale Johannesen2182f062007-07-13 17:13:54 +0000105 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000106 char PostRAScheduler::ID = 0;
107
Nick Lewycky02d5f772009-10-25 06:33:48 +0000108 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000109 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohman682a2d12009-10-21 01:44:44 +0000110 ///
Dan Gohman60cb69e2008-11-19 23:18:57 +0000111 LatencyPriorityQueue AvailableQueue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000112
Dan Gohman60cb69e2008-11-19 23:18:57 +0000113 /// PendingQueue - This contains all of the instructions whose operands have
114 /// been issued, but their results are not ready yet (due to the latency of
115 /// the operation). Once the operands becomes available, the instruction is
116 /// added to the AvailableQueue.
117 std::vector<SUnit*> PendingQueue;
118
Dan Gohmanceac7c32009-01-16 01:33:36 +0000119 /// HazardRec - The hazard recognizer to use.
120 ScheduleHazardRecognizer *HazardRec;
121
David Goodwin83704852009-10-26 16:59:04 +0000122 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
123 AntiDepBreaker *AntiDepBreak;
124
Dan Gohman87b02d52009-10-09 23:27:56 +0000125 /// AA - AliasAnalysis for making memory reference queries.
126 AliasAnalysis *AA;
127
Andrew Trick60cf03e2012-03-07 05:21:52 +0000128 /// The schedule. Null SUnit*'s represent noop instructions.
129 std::vector<SUnit*> Sequence;
130
Andrew Tricka53e1012013-08-23 17:48:33 +0000131 /// The index in BB of RegionEnd.
132 ///
133 /// This is the instruction number from the top of the current block, not
134 /// the SlotIndex. It is only used by the AntiDepBreaker.
135 unsigned EndIndex;
136
Dan Gohmanad2134d2008-11-25 00:52:40 +0000137 public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000138 SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000139 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
140 const RegisterClassInfo &,
141 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
142 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000143
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000144 ~SchedulePostRATDList() override;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000145
Andrew Trick52226d42012-03-07 23:00:49 +0000146 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohmanb9543432009-02-10 23:27:53 +0000147 /// this block.
148 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000149 void startBlock(MachineBasicBlock *BB) override;
Dan Gohmanb9543432009-02-10 23:27:53 +0000150
Andrew Tricka53e1012013-08-23 17:48:33 +0000151 // Set the index of RegionEnd within the current BB.
152 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
153
Andrew Trick60cf03e2012-03-07 05:21:52 +0000154 /// Initialize the scheduler state for the next scheduling region.
Craig Topper4584cd52014-03-07 09:26:03 +0000155 void enterRegion(MachineBasicBlock *bb,
156 MachineBasicBlock::iterator begin,
157 MachineBasicBlock::iterator end,
158 unsigned regioninstrs) override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000159
160 /// Notify that the scheduler has finished scheduling the current region.
Craig Topper4584cd52014-03-07 09:26:03 +0000161 void exitRegion() override;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000162
Dan Gohmanb9543432009-02-10 23:27:53 +0000163 /// Schedule - Schedule the instruction range using list scheduling.
164 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000165 void schedule() override;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000166
Andrew Tricke932bb72012-03-07 05:21:44 +0000167 void EmitSchedule();
168
Dan Gohman682a2d12009-10-21 01:44:44 +0000169 /// Observe - Update liveness information to account for the current
170 /// instruction, which will not be scheduled.
171 ///
172 void Observe(MachineInstr *MI, unsigned Count);
173
Andrew Trick52226d42012-03-07 23:00:49 +0000174 /// finishBlock - Clean up register live-range state.
Dan Gohman682a2d12009-10-21 01:44:44 +0000175 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000176 void finishBlock() override;
Dan Gohman682a2d12009-10-21 01:44:44 +0000177
Dan Gohman60cb69e2008-11-19 23:18:57 +0000178 private:
David Goodwin80a03cc2009-11-20 19:32:48 +0000179 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
180 void ReleaseSuccessors(SUnit *SU);
181 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
182 void ListScheduleTopDown();
Jim Grosbachd772bde2010-05-14 21:19:48 +0000183
Andrew Trickedee68c2012-03-07 05:21:40 +0000184 void dumpSchedule() const;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000185 void emitNoop(unsigned CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000186 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000187}
Dale Johannesen2182f062007-07-13 17:13:54 +0000188
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000189char &llvm::PostRASchedulerID = PostRAScheduler::ID;
190
191INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
192 "Post RA top-down list latency scheduler", false, false)
193
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000194SchedulePostRATDList::SchedulePostRATDList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000195 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
196 const RegisterClassInfo &RCI,
197 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
198 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs)
199 : ScheduleDAGInstrs(MF, &MLI, /*IsPostRA=*/true), AA(AA), EndIndex(0) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000200
Eric Christopherd9134482014-08-04 21:25:23 +0000201 const InstrItineraryData *InstrItins =
Eric Christopherb66367a2014-10-14 07:17:23 +0000202 MF.getSubtarget().getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000203 HazardRec =
Eric Christopherb66367a2014-10-14 07:17:23 +0000204 MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer(
Eric Christopherd9134482014-08-04 21:25:23 +0000205 InstrItins, this);
Preston Gurd9a091472012-04-23 21:39:35 +0000206
207 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
208 MRI.tracksLiveness()) &&
209 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000210 AntiDepBreak =
Evan Cheng0d639a22011-07-01 21:01:15 +0000211 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000212 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng0d639a22011-07-01 21:01:15 +0000213 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Craig Topperc0196b12014-04-14 00:51:57 +0000214 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000215}
216
217SchedulePostRATDList::~SchedulePostRATDList() {
218 delete HazardRec;
219 delete AntiDepBreak;
220}
221
Andrew Trick60cf03e2012-03-07 05:21:52 +0000222/// Initialize state associated with the next scheduling region.
223void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
224 MachineBasicBlock::iterator begin,
225 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000226 unsigned regioninstrs) {
227 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000228 Sequence.clear();
229}
230
231/// Print the schedule before exiting the region.
232void SchedulePostRATDList::exitRegion() {
233 DEBUG({
234 dbgs() << "*** Final schedule ***\n";
235 dumpSchedule();
236 dbgs() << '\n';
237 });
238 ScheduleDAGInstrs::exitRegion();
239}
240
Manman Ren19f49ac2012-09-11 22:23:19 +0000241#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickedee68c2012-03-07 05:21:40 +0000242/// dumpSchedule - dump the scheduled Sequence.
243void SchedulePostRATDList::dumpSchedule() const {
244 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
245 if (SUnit *SU = Sequence[i])
246 SU->dump(this);
247 else
248 dbgs() << "**** NOOP ****\n";
249 }
250}
Manman Ren742534c2012-09-06 19:06:06 +0000251#endif
Andrew Trickedee68c2012-03-07 05:21:40 +0000252
Sanjay Patela2f658d2014-07-15 22:39:58 +0000253bool PostRAScheduler::enablePostRAScheduler(
254 const TargetSubtargetInfo &ST,
255 CodeGenOpt::Level OptLevel,
256 TargetSubtargetInfo::AntiDepBreakMode &Mode,
257 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const {
258 Mode = ST.getAntiDepBreakMode();
259 ST.getCriticalPathRCs(CriticalPathRCs);
Matthias Braun39a2afc2015-06-13 03:42:16 +0000260 return ST.enablePostRAScheduler() &&
Sanjay Patela2f658d2014-07-15 22:39:58 +0000261 OptLevel >= ST.getOptLevelToEnablePostRAScheduler();
262}
263
Dan Gohman60cb69e2008-11-19 23:18:57 +0000264bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000265 if (skipOptnoneFunction(*Fn.getFunction()))
266 return false;
267
Eric Christopherfc6de422014-08-05 02:39:49 +0000268 TII = Fn.getSubtarget().getInstrInfo();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000269 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000270 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000271 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
272
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000273 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman26e9b892009-10-10 00:15:38 +0000274
David Goodwin9a051a52009-10-01 21:46:35 +0000275 // Check for explicit enable/disable of post-ra scheduling.
Evan Cheng7fae11b2011-12-14 02:11:42 +0000276 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
277 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper760b1342012-02-22 05:59:10 +0000278 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin9a051a52009-10-01 21:46:35 +0000279 if (EnablePostRAScheduler.getPosition() > 0) {
280 if (!EnablePostRAScheduler)
Evan Cheng8b614762009-10-16 06:10:34 +0000281 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000282 } else {
Evan Cheng8b614762009-10-16 06:10:34 +0000283 // Check that post-RA scheduling is enabled for this target.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000284 // This may upgrade the AntiDepMode.
Eric Christopher3d4276f2015-01-27 07:31:29 +0000285 if (!enablePostRAScheduler(Fn.getSubtarget(), PassConfig->getOptLevel(),
Sanjay Patela2f658d2014-07-15 22:39:58 +0000286 AntiDepMode, CriticalPathRCs))
Evan Cheng8b614762009-10-16 06:10:34 +0000287 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000288 }
David Goodwin17199b52009-09-30 00:10:16 +0000289
David Goodwin02ad4cb2009-10-22 23:19:17 +0000290 // Check for antidep breaking override...
291 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng0d639a22011-07-01 21:01:15 +0000292 AntiDepMode = (EnableAntiDepBreaking == "all")
293 ? TargetSubtargetInfo::ANTIDEP_ALL
294 : ((EnableAntiDepBreaking == "critical")
295 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
296 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin02ad4cb2009-10-22 23:19:17 +0000297 }
298
David Greeneaa8ce382010-01-05 01:26:01 +0000299 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesen2182f062007-07-13 17:13:54 +0000300
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000301 SchedulePostRATDList Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode,
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000302 CriticalPathRCs);
Dan Gohman619ef482009-01-15 19:20:50 +0000303
Dale Johannesen2182f062007-07-13 17:13:54 +0000304 // Loop over all of the basic blocks
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000305 for (auto &MBB : Fn) {
David Goodwin7f651692009-09-01 18:34:03 +0000306#ifndef NDEBUG
307 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
308 if (DebugDiv > 0) {
309 static int bbcnt = 0;
310 if (bbcnt++ % DebugDiv != DebugMod)
311 continue;
Craig Toppera538d832012-08-22 06:07:19 +0000312 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000313 << ":BB#" << MBB.getNumber() << " ***\n";
David Goodwin7f651692009-09-01 18:34:03 +0000314 }
315#endif
316
Dan Gohmanb9543432009-02-10 23:27:53 +0000317 // Initialize register live-range state for scheduling in this block.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000318 Scheduler.startBlock(&MBB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000319
Dan Gohman5f8a2592009-01-16 22:10:20 +0000320 // Schedule each sequence of instructions not interrupted by a label
321 // or anything else that effectively needs to shut down scheduling.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000322 MachineBasicBlock::iterator Current = MBB.end();
323 unsigned Count = MBB.size(), CurrentCount = Count;
324 for (MachineBasicBlock::iterator I = Current; I != MBB.begin();) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000325 MachineInstr *MI = std::prev(I);
Andrew Tricka53e1012013-08-23 17:48:33 +0000326 --Count;
Jakob Stoklund Olesena793a592012-02-23 17:54:21 +0000327 // Calls are not scheduling boundaries before register allocation, but
328 // post-ra we don't gain anything by scheduling across calls since we
329 // don't need to worry about register pressure.
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000330 if (MI->isCall() || TII->isSchedulingBoundary(MI, &MBB, Fn)) {
331 Scheduler.enterRegion(&MBB, I, Current, CurrentCount - Count);
Andrew Tricka53e1012013-08-23 17:48:33 +0000332 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000333 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000334 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000335 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000336 Current = MI;
Andrew Tricka53e1012013-08-23 17:48:33 +0000337 CurrentCount = Count;
Dan Gohman64613ac2009-03-10 18:10:43 +0000338 Scheduler.Observe(MI, CurrentCount);
Dan Gohman5f8a2592009-01-16 22:10:20 +0000339 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000340 I = MI;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000341 if (MI->isBundle())
342 Count -= MI->getBundleSize();
Dan Gohmand5643532009-02-03 18:57:45 +0000343 }
Dan Gohmandfaf6462009-02-11 04:27:20 +0000344 assert(Count == 0 && "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000345 assert((MBB.begin() == Current || CurrentCount != 0) &&
Dan Gohman64613ac2009-03-10 18:10:43 +0000346 "Instruction count mismatch!");
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000347 Scheduler.enterRegion(&MBB, MBB.begin(), Current, CurrentCount);
Andrew Tricka53e1012013-08-23 17:48:33 +0000348 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000349 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000350 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000351 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000352
353 // Clean up register live-range state.
Andrew Trick52226d42012-03-07 23:00:49 +0000354 Scheduler.finishBlock();
David Goodwinae6bc822009-08-25 17:03:05 +0000355
David Goodwin6c08cfc2009-09-03 22:15:25 +0000356 // Update register kills
Duncan P. N. Exon Smith1ff40982015-10-09 21:05:00 +0000357 Scheduler.fixupKills(&MBB);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000358 }
Dale Johannesen2182f062007-07-13 17:13:54 +0000359
360 return true;
361}
Jim Grosbachd772bde2010-05-14 21:19:48 +0000362
Dan Gohmanb9543432009-02-10 23:27:53 +0000363/// StartBlock - Initialize register live-range state for scheduling in
364/// this block.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000365///
Andrew Trick52226d42012-03-07 23:00:49 +0000366void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000367 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000368 ScheduleDAGInstrs::startBlock(BB);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000369
David Goodwin83704852009-10-26 16:59:04 +0000370 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin6021b4d2009-08-10 15:55:25 +0000371 HazardRec->Reset();
Craig Topperc0196b12014-04-14 00:51:57 +0000372 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000373 AntiDepBreak->StartBlock(BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000374}
375
376/// Schedule - Schedule the instruction range using list scheduling.
377///
Andrew Trick52226d42012-03-07 23:00:49 +0000378void SchedulePostRATDList::schedule() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000379 // Build the scheduling graph.
Andrew Trick52226d42012-03-07 23:00:49 +0000380 buildSchedGraph(AA);
Dan Gohmanb9543432009-02-10 23:27:53 +0000381
Craig Topperc0196b12014-04-14 00:51:57 +0000382 if (AntiDepBreak) {
Jim Grosbachd772bde2010-05-14 21:19:48 +0000383 unsigned Broken =
Andrew Trick8c207e42012-03-09 04:29:02 +0000384 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
385 EndIndex, DbgValues);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000386
David Goodwin80a03cc2009-11-20 19:32:48 +0000387 if (Broken != 0) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000388 // We made changes. Update the dependency graph.
389 // Theoretically we could update the graph in place:
390 // When a live range is changed to use a different register, remove
391 // the def's anti-dependence *and* output-dependence edges due to
392 // that register, and add new anti-dependence and output-dependence
393 // edges based on the next live range of the register.
Andrew Trick60cf03e2012-03-07 05:21:52 +0000394 ScheduleDAG::clearDAG();
Andrew Trick52226d42012-03-07 23:00:49 +0000395 buildSchedGraph(AA);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000396
David Goodwin83704852009-10-26 16:59:04 +0000397 NumFixedAnti += Broken;
Dan Gohmanb9543432009-02-10 23:27:53 +0000398 }
399 }
400
David Greeneaa8ce382010-01-05 01:26:01 +0000401 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwin6021b4d2009-08-10 15:55:25 +0000402 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
403 SUnits[su].dumpAll(this));
404
Dan Gohmanb9543432009-02-10 23:27:53 +0000405 AvailableQueue.initNodes(SUnits);
David Goodwin80a03cc2009-11-20 19:32:48 +0000406 ListScheduleTopDown();
Dan Gohmanb9543432009-02-10 23:27:53 +0000407 AvailableQueue.releaseState();
408}
409
410/// Observe - Update liveness information to account for the current
411/// instruction, which will not be scheduled.
412///
Dan Gohmandfaf6462009-02-11 04:27:20 +0000413void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Craig Topperc0196b12014-04-14 00:51:57 +0000414 if (AntiDepBreak)
Andrew Tricka316faa2012-03-07 23:00:52 +0000415 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohmanb9543432009-02-10 23:27:53 +0000416}
417
418/// FinishBlock - Clean up register live-range state.
419///
Andrew Trick52226d42012-03-07 23:00:49 +0000420void SchedulePostRATDList::finishBlock() {
Craig Topperc0196b12014-04-14 00:51:57 +0000421 if (AntiDepBreak)
David Goodwin83704852009-10-26 16:59:04 +0000422 AntiDepBreak->FinishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000423
424 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000425 ScheduleDAGInstrs::finishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000426}
427
Dan Gohman60cb69e2008-11-19 23:18:57 +0000428//===----------------------------------------------------------------------===//
429// Top-Down Scheduling
430//===----------------------------------------------------------------------===//
431
432/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000433/// the PendingQueue if the count reaches zero.
David Goodwin80a03cc2009-11-20 19:32:48 +0000434void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000435 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000436
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000437 if (SuccEdge->isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000438 --SuccSU->WeakPredsLeft;
439 return;
440 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000441#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000442 if (SuccSU->NumPredsLeft == 0) {
David Greeneaa8ce382010-01-05 01:26:01 +0000443 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000444 SuccSU->dump(this);
David Greeneaa8ce382010-01-05 01:26:01 +0000445 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000446 llvm_unreachable(nullptr);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000447 }
448#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000449 --SuccSU->NumPredsLeft;
450
Andrew Trick84f9ad92011-05-06 18:14:32 +0000451 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trickaab77fe2011-05-06 17:09:08 +0000452 // here as such:
453 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
454 //
455 // However, we lazily compute node depth instead. Note that
456 // ScheduleNodeTopDown has already updated the depth of this node which causes
457 // all descendents to be marked dirty. Setting the successor depth explicitly
458 // here would cause depth to be recomputed for all its ancestors. If the
459 // successor is not yet ready (because of a transitively redundant edge) then
460 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbachd772bde2010-05-14 21:19:48 +0000461
Dan Gohmanb9543432009-02-10 23:27:53 +0000462 // If all the node's predecessors are scheduled, this node is ready
463 // to be scheduled. Ignore the special ExitSU node.
464 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman60cb69e2008-11-19 23:18:57 +0000465 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000466}
467
468/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin80a03cc2009-11-20 19:32:48 +0000469void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000470 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +0000471 I != E; ++I) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000472 ReleaseSucc(SU, &*I);
David Goodwin8501dbbe2009-11-03 20:57:50 +0000473 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000474}
475
476/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
477/// count of its successors. If a successor pending count is zero, add it to
478/// the Available queue.
David Goodwin80a03cc2009-11-20 19:32:48 +0000479void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greeneaa8ce382010-01-05 01:26:01 +0000480 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000481 DEBUG(SU->dump(this));
Jim Grosbachd772bde2010-05-14 21:19:48 +0000482
Dan Gohman60cb69e2008-11-19 23:18:57 +0000483 Sequence.push_back(SU);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000484 assert(CurCycle >= SU->getDepth() &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000485 "Node scheduled above its depth!");
David Goodwin80a03cc2009-11-20 19:32:48 +0000486 SU->setDepthToAtLeast(CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000487
David Goodwin80a03cc2009-11-20 19:32:48 +0000488 ReleaseSuccessors(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000489 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000490 AvailableQueue.scheduledNode(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000491}
492
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000493/// emitNoop - Add a noop to the current instruction sequence.
494void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
495 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
496 HazardRec->EmitNoop();
Craig Topperc0196b12014-04-14 00:51:57 +0000497 Sequence.push_back(nullptr); // NULL here means noop
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000498 ++NumNoops;
499}
500
Dan Gohman60cb69e2008-11-19 23:18:57 +0000501/// ListScheduleTopDown - The main loop of list scheduling for top-down
502/// schedulers.
David Goodwin80a03cc2009-11-20 19:32:48 +0000503void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000504 unsigned CurCycle = 0;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000505
David Goodwin8501dbbe2009-11-03 20:57:50 +0000506 // We're scheduling top-down but we're visiting the regions in
507 // bottom-up order, so we don't know the hazards at the start of a
508 // region. So assume no hazards (this should usually be ok as most
509 // blocks are a single region).
510 HazardRec->Reset();
511
Dan Gohmanb9543432009-02-10 23:27:53 +0000512 // Release any successors of the special Entry node.
David Goodwin80a03cc2009-11-20 19:32:48 +0000513 ReleaseSuccessors(&EntrySU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000514
David Goodwin80a03cc2009-11-20 19:32:48 +0000515 // Add all leaves to Available queue.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000516 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
517 // It is available if it has no predecessors.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000518 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000519 AvailableQueue.push(&SUnits[i]);
520 SUnits[i].isAvailable = true;
521 }
522 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000523
David Goodwin1f8c7a72009-08-12 21:47:46 +0000524 // In any cycle where we can't schedule any instructions, we must
525 // stall or emit a noop, depending on the target.
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000526 bool CycleHasInsts = false;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000527
Dan Gohman60cb69e2008-11-19 23:18:57 +0000528 // While Available queue is not empty, grab the node with the highest
529 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmanceac7c32009-01-16 01:33:36 +0000530 std::vector<SUnit*> NotReady;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000531 Sequence.reserve(SUnits.size());
532 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
533 // Check to see if any of the pending instructions are ready to issue. If
534 // so, add them to the available queue.
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000535 unsigned MinDepth = ~0u;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000536 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000537 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000538 AvailableQueue.push(PendingQueue[i]);
539 PendingQueue[i]->isAvailable = true;
540 PendingQueue[i] = PendingQueue.back();
541 PendingQueue.pop_back();
542 --i; --e;
David Goodwin80a03cc2009-11-20 19:32:48 +0000543 } else if (PendingQueue[i]->getDepth() < MinDepth)
544 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000545 }
David Goodwinebd694b2009-08-11 17:35:23 +0000546
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000547 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinebd694b2009-08-11 17:35:23 +0000548
Craig Topperc0196b12014-04-14 00:51:57 +0000549 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohmanceac7c32009-01-16 01:33:36 +0000550 bool HasNoopHazards = false;
551 while (!AvailableQueue.empty()) {
552 SUnit *CurSUnit = AvailableQueue.pop();
553
554 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000555 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000556 if (HT == ScheduleHazardRecognizer::NoHazard) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000557 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
558 if (!NotPreferredSUnit) {
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +0000559 // If this is the first non-preferred node for this cycle, then
560 // record it and continue searching for a preferred node. If this
561 // is not the first non-preferred node, then treat it as though
562 // there had been a hazard.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000563 NotPreferredSUnit = CurSUnit;
564 continue;
565 }
566 } else {
567 FoundSUnit = CurSUnit;
568 break;
569 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000570 }
571
572 // Remember if this is a noop hazard.
573 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
574
575 NotReady.push_back(CurSUnit);
576 }
577
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000578 // If we have a non-preferred node, push it back onto the available list.
579 // If we did not find a preferred node, then schedule this first
580 // non-preferred node.
581 if (NotPreferredSUnit) {
582 if (!FoundSUnit) {
583 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
584 FoundSUnit = NotPreferredSUnit;
585 } else {
586 AvailableQueue.push(NotPreferredSUnit);
587 }
588
Craig Topperc0196b12014-04-14 00:51:57 +0000589 NotPreferredSUnit = nullptr;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000590 }
591
Dan Gohmanceac7c32009-01-16 01:33:36 +0000592 // Add the nodes that aren't ready back onto the available list.
593 if (!NotReady.empty()) {
594 AvailableQueue.push_all(NotReady);
595 NotReady.clear();
596 }
597
David Goodwin8501dbbe2009-11-03 20:57:50 +0000598 // If we found a node to schedule...
Dan Gohman60cb69e2008-11-19 23:18:57 +0000599 if (FoundSUnit) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000600 // If we need to emit noops prior to this instruction, then do so.
601 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
602 for (unsigned i = 0; i != NumPreNoops; ++i)
603 emitNoop(CurCycle);
604
David Goodwin8501dbbe2009-11-03 20:57:50 +0000605 // ... schedule the node...
David Goodwin80a03cc2009-11-20 19:32:48 +0000606 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000607 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000608 CycleHasInsts = true;
Andrew Trick18c9b372011-06-01 03:27:56 +0000609 if (HazardRec->atIssueLimit()) {
610 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
611 HazardRec->AdvanceCycle();
612 ++CurCycle;
613 CycleHasInsts = false;
614 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000615 } else {
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000616 if (CycleHasInsts) {
David Greeneaa8ce382010-01-05 01:26:01 +0000617 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000618 HazardRec->AdvanceCycle();
619 } else if (!HasNoopHazards) {
620 // Otherwise, we have a pipeline stall, but no other problem,
621 // just advance the current cycle and try again.
David Greeneaa8ce382010-01-05 01:26:01 +0000622 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000623 HazardRec->AdvanceCycle();
David Goodwin80a03cc2009-11-20 19:32:48 +0000624 ++NumStalls;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000625 } else {
626 // Otherwise, we have no instructions to issue and we have instructions
627 // that will fault if we don't do this right. This is the case for
628 // processors without pipeline interlocks and other cases.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000629 emitNoop(CurCycle);
David Goodwin1f8c7a72009-08-12 21:47:46 +0000630 }
631
Dan Gohmanceac7c32009-01-16 01:33:36 +0000632 ++CurCycle;
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000633 CycleHasInsts = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000634 }
635 }
636
637#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000638 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
639 unsigned Noops = 0;
640 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
641 if (!Sequence[i])
642 ++Noops;
643 assert(Sequence.size() - Noops == ScheduledNodes &&
644 "The number of nodes scheduled doesn't match the expected number!");
645#endif // NDEBUG
Dan Gohman60cb69e2008-11-19 23:18:57 +0000646}
Andrew Tricke932bb72012-03-07 05:21:44 +0000647
648// EmitSchedule - Emit the machine code in scheduled order.
649void SchedulePostRATDList::EmitSchedule() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000650 RegionBegin = RegionEnd;
Andrew Tricke932bb72012-03-07 05:21:44 +0000651
652 // If first instruction was a DBG_VALUE then put it back.
653 if (FirstDbgValue)
Andrew Trick8c207e42012-03-09 04:29:02 +0000654 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Tricke932bb72012-03-07 05:21:44 +0000655
656 // Then re-insert them according to the given schedule.
657 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
658 if (SUnit *SU = Sequence[i])
Andrew Trick8c207e42012-03-09 04:29:02 +0000659 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Tricke932bb72012-03-07 05:21:44 +0000660 else
661 // Null SUnit* is a noop.
Andrew Trick8c207e42012-03-09 04:29:02 +0000662 TII->insertNoop(*BB, RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000663
664 // Update the Begin iterator, as the first instruction in the block
665 // may have been scheduled later.
666 if (i == 0)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000667 RegionBegin = std::prev(RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000668 }
669
670 // Reinsert any remaining debug_values.
671 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
672 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000673 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Tricke932bb72012-03-07 05:21:44 +0000674 MachineInstr *DbgValue = P.first;
675 MachineBasicBlock::iterator OrigPrivMI = P.second;
676 BB->splice(++OrigPrivMI, BB, DbgValue);
677 }
678 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000679 FirstDbgValue = nullptr;
Andrew Tricke932bb72012-03-07 05:21:44 +0000680}