blob: db3933e5eedf5b0742febd8bf5d1d803cf18105b [file] [log] [blame]
Dale Johannesen72f15962007-07-13 17:31:29 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Johannesene7e7d0d2007-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 Johannesene7e7d0d2007-07-13 17:13:54 +000021#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-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 Gohman343f0c02008-11-19 23:18:57 +000028#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohman3f237442008-12-16 03:25:46 +000029#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000032#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick15252602012-06-06 20:29:31 +000034#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Tricked395c82012-03-07 23:01:06 +000035#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohman2836c282009-01-16 01:33:36 +000036#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000037#include "llvm/CodeGen/SchedulerRegistry.h"
David Goodwine10deca2009-10-26 22:31:16 +000038#include "llvm/Support/CommandLine.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000039#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000041#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-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 Johannesene7e7d0d2007-07-13 17:13:54 +000047using namespace llvm;
48
Stephen Hinesdce4a402014-05-29 02:49:00 -070049#define DEBUG_TYPE "post-RA-sched"
50
Dan Gohman2836c282009-01-16 01:33:36 +000051STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000052STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin2e7be612009-10-26 16:59:04 +000053STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman343f0c02008-11-19 23:18:57 +000054
David Goodwin471850a2009-10-01 21:46:35 +000055// Post-RA scheduling is enabled with
Evan Cheng5b1b44892011-07-01 21:01:15 +000056// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin471850a2009-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 Goodwin9843a932009-10-01 22:19:57 +000061 cl::init(false), cl::Hidden);
David Goodwin2e7be612009-10-26 16:59:04 +000062static cl::opt<std::string>
Dan Gohman21d90032008-11-25 00:52:40 +000063EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin2e7be612009-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 Gohman2836c282009-01-16 01:33:36 +000067
David Goodwin1f152282009-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 Goodwinada0ef82009-10-26 19:41:00 +000078AntiDepBreaker::~AntiDepBreaker() { }
79
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000080namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000081 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng86050dc2010-06-18 23:09:54 +000082 const TargetInstrInfo *TII;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000083 RegisterClassInfo RegClassInfo;
Dan Gohmana70dca12009-10-09 23:27:56 +000084
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000085 public:
86 static char ID;
Andrew Trickc7d081b2012-02-08 21:22:53 +000087 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000088
Stephen Hines36b56882014-04-23 16:57:46 -070089 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman845012e2009-07-31 23:37:33 +000090 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000091 AU.addRequired<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +000092 AU.addRequired<TargetPassConfig>();
Dan Gohman3f237442008-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
Stephen Hines36b56882014-04-23 16:57:46 -0700100 bool runOnMachineFunction(MachineFunction &Fn) override;
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000101 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000102 char PostRAScheduler::ID = 0;
103
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000104 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000105 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000106 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000107 LatencyPriorityQueue AvailableQueue;
Jim Grosbach90013032010-05-14 21:19:48 +0000108
Dan Gohman343f0c02008-11-19 23:18:57 +0000109 /// PendingQueue - This contains all of the instructions whose operands have
110 /// been issued, but their results are not ready yet (due to the latency of
111 /// the operation). Once the operands becomes available, the instruction is
112 /// added to the AvailableQueue.
113 std::vector<SUnit*> PendingQueue;
114
Dan Gohman2836c282009-01-16 01:33:36 +0000115 /// HazardRec - The hazard recognizer to use.
116 ScheduleHazardRecognizer *HazardRec;
117
David Goodwin2e7be612009-10-26 16:59:04 +0000118 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
119 AntiDepBreaker *AntiDepBreak;
120
Dan Gohmana70dca12009-10-09 23:27:56 +0000121 /// AA - AliasAnalysis for making memory reference queries.
122 AliasAnalysis *AA;
123
Andrew Trick47c14452012-03-07 05:21:52 +0000124 /// The schedule. Null SUnit*'s represent noop instructions.
125 std::vector<SUnit*> Sequence;
126
Andrew Trickd2763f62013-08-23 17:48:33 +0000127 /// The index in BB of RegionEnd.
128 ///
129 /// This is the instruction number from the top of the current block, not
130 /// the SlotIndex. It is only used by the AntiDepBreaker.
131 unsigned EndIndex;
132
Dan Gohman21d90032008-11-25 00:52:40 +0000133 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000134 SchedulePostRATDList(
135 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000136 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000137 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000138 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000139
Andrew Trick2da8bc82010-12-24 05:03:26 +0000140 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000141
Andrew Trick953be892012-03-07 23:00:49 +0000142 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000143 /// this block.
144 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700145 void startBlock(MachineBasicBlock *BB) override;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146
Andrew Trickd2763f62013-08-23 17:48:33 +0000147 // Set the index of RegionEnd within the current BB.
148 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
149
Andrew Trick47c14452012-03-07 05:21:52 +0000150 /// Initialize the scheduler state for the next scheduling region.
Stephen Hines36b56882014-04-23 16:57:46 -0700151 void enterRegion(MachineBasicBlock *bb,
152 MachineBasicBlock::iterator begin,
153 MachineBasicBlock::iterator end,
154 unsigned regioninstrs) override;
Andrew Trick47c14452012-03-07 05:21:52 +0000155
156 /// Notify that the scheduler has finished scheduling the current region.
Stephen Hines36b56882014-04-23 16:57:46 -0700157 void exitRegion() override;
Andrew Trick47c14452012-03-07 05:21:52 +0000158
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000159 /// Schedule - Schedule the instruction range using list scheduling.
160 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700161 void schedule() override;
Jim Grosbach90013032010-05-14 21:19:48 +0000162
Andrew Trick84b454d2012-03-07 05:21:44 +0000163 void EmitSchedule();
164
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000165 /// Observe - Update liveness information to account for the current
166 /// instruction, which will not be scheduled.
167 ///
168 void Observe(MachineInstr *MI, unsigned Count);
169
Andrew Trick953be892012-03-07 23:00:49 +0000170 /// finishBlock - Clean up register live-range state.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000171 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700172 void finishBlock() override;
David Goodwin2e7be612009-10-26 16:59:04 +0000173
Dan Gohman343f0c02008-11-19 23:18:57 +0000174 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000175 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
176 void ReleaseSuccessors(SUnit *SU);
177 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
178 void ListScheduleTopDown();
Andrew Trick73ba69b2012-03-07 05:21:40 +0000179
180 void dumpSchedule() const;
Stephen Hines36b56882014-04-23 16:57:46 -0700181 void emitNoop(unsigned CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000182 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000183}
184
Andrew Trick1dd8c852012-02-08 21:23:13 +0000185char &llvm::PostRASchedulerID = PostRAScheduler::ID;
186
187INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
188 "Post RA top-down list latency scheduler", false, false)
189
Andrew Trick2da8bc82010-12-24 05:03:26 +0000190SchedulePostRATDList::SchedulePostRATDList(
191 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000192 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000193 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000194 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Stephen Hines36b56882014-04-23 16:57:46 -0700195 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA), EndIndex(0) {
196
Andrew Trick2da8bc82010-12-24 05:03:26 +0000197 const TargetMachine &TM = MF.getTarget();
198 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
199 HazardRec =
200 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000201
202 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
203 MRI.tracksLiveness()) &&
204 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick2da8bc82010-12-24 05:03:26 +0000205 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000206 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000207 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000208 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Stephen Hinesdce4a402014-05-29 02:49:00 -0700209 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000210}
211
212SchedulePostRATDList::~SchedulePostRATDList() {
213 delete HazardRec;
214 delete AntiDepBreak;
215}
216
Andrew Trick47c14452012-03-07 05:21:52 +0000217/// Initialize state associated with the next scheduling region.
218void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
219 MachineBasicBlock::iterator begin,
220 MachineBasicBlock::iterator end,
Andrew Trickd2763f62013-08-23 17:48:33 +0000221 unsigned regioninstrs) {
222 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick47c14452012-03-07 05:21:52 +0000223 Sequence.clear();
224}
225
226/// Print the schedule before exiting the region.
227void SchedulePostRATDList::exitRegion() {
228 DEBUG({
229 dbgs() << "*** Final schedule ***\n";
230 dumpSchedule();
231 dbgs() << '\n';
232 });
233 ScheduleDAGInstrs::exitRegion();
234}
235
Manman Renb720be62012-09-11 22:23:19 +0000236#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick73ba69b2012-03-07 05:21:40 +0000237/// dumpSchedule - dump the scheduled Sequence.
238void SchedulePostRATDList::dumpSchedule() const {
239 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
240 if (SUnit *SU = Sequence[i])
241 SU->dump(this);
242 else
243 dbgs() << "**** NOOP ****\n";
244 }
245}
Manman Ren77e300e2012-09-06 19:06:06 +0000246#endif
Andrew Trick73ba69b2012-03-07 05:21:40 +0000247
Dan Gohman343f0c02008-11-19 23:18:57 +0000248bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Stephen Hines36b56882014-04-23 16:57:46 -0700249 if (skipOptnoneFunction(*Fn.getFunction()))
250 return false;
251
Evan Cheng86050dc2010-06-18 23:09:54 +0000252 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000253 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
254 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
255 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000256 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
257
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000258 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000259
David Goodwin471850a2009-10-01 21:46:35 +0000260 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000261 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
262 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000263 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000264 if (EnablePostRAScheduler.getPosition() > 0) {
265 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000266 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000267 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000268 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000269 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000270 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000271 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
272 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000273 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000274 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000275
David Goodwin4c3715c2009-10-22 23:19:17 +0000276 // Check for antidep breaking override...
277 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000278 AntiDepMode = (EnableAntiDepBreaking == "all")
279 ? TargetSubtargetInfo::ANTIDEP_ALL
280 : ((EnableAntiDepBreaking == "critical")
281 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
282 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000283 }
284
David Greenee1b21292010-01-05 01:26:01 +0000285 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000286
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000287 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000288 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000289
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000290 // Loop over all of the basic blocks
291 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000292 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000293#ifndef NDEBUG
294 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
295 if (DebugDiv > 0) {
296 static int bbcnt = 0;
297 if (bbcnt++ % DebugDiv != DebugMod)
298 continue;
Craig Topper96601ca2012-08-22 06:07:19 +0000299 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000300 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000301 }
302#endif
303
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000304 // Initialize register live-range state for scheduling in this block.
Andrew Trick953be892012-03-07 23:00:49 +0000305 Scheduler.startBlock(MBB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000306
Dan Gohmanf7119392009-01-16 22:10:20 +0000307 // Schedule each sequence of instructions not interrupted by a label
308 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000309 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000310 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000311 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Stephen Hines36b56882014-04-23 16:57:46 -0700312 MachineInstr *MI = std::prev(I);
Andrew Trickd2763f62013-08-23 17:48:33 +0000313 --Count;
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000314 // Calls are not scheduling boundaries before register allocation, but
315 // post-ra we don't gain anything by scheduling across calls since we
316 // don't need to worry about register pressure.
317 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Trickd2763f62013-08-23 17:48:33 +0000318 Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count);
319 Scheduler.setEndIndex(CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000320 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000321 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000322 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323 Current = MI;
Andrew Trickd2763f62013-08-23 17:48:33 +0000324 CurrentCount = Count;
Dan Gohman1274ced2009-03-10 18:10:43 +0000325 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000326 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000327 I = MI;
Evan Chengddfd1372011-12-14 02:11:42 +0000328 if (MI->isBundle())
329 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000330 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000331 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000332 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000333 "Instruction count mismatch!");
Andrew Trick47c14452012-03-07 05:21:52 +0000334 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Trickd2763f62013-08-23 17:48:33 +0000335 Scheduler.setEndIndex(CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000336 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000337 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000338 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000339
340 // Clean up register live-range state.
Andrew Trick953be892012-03-07 23:00:49 +0000341 Scheduler.finishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000342
David Goodwin5e411782009-09-03 22:15:25 +0000343 // Update register kills
Stephen Hines36b56882014-04-23 16:57:46 -0700344 Scheduler.fixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000345 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000346
347 return true;
348}
Jim Grosbach90013032010-05-14 21:19:48 +0000349
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000350/// StartBlock - Initialize register live-range state for scheduling in
351/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000352///
Andrew Trick953be892012-03-07 23:00:49 +0000353void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000354 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000355 ScheduleDAGInstrs::startBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000356
David Goodwin2e7be612009-10-26 16:59:04 +0000357 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000358 HazardRec->Reset();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700359 if (AntiDepBreak)
David Goodwin2e7be612009-10-26 16:59:04 +0000360 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000361}
362
363/// Schedule - Schedule the instruction range using list scheduling.
364///
Andrew Trick953be892012-03-07 23:00:49 +0000365void SchedulePostRATDList::schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000366 // Build the scheduling graph.
Andrew Trick953be892012-03-07 23:00:49 +0000367 buildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000368
Stephen Hinesdce4a402014-05-29 02:49:00 -0700369 if (AntiDepBreak) {
Jim Grosbach90013032010-05-14 21:19:48 +0000370 unsigned Broken =
Andrew Trick68675c62012-03-09 04:29:02 +0000371 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
372 EndIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000373
David Goodwin557bbe62009-11-20 19:32:48 +0000374 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000375 // We made changes. Update the dependency graph.
376 // Theoretically we could update the graph in place:
377 // When a live range is changed to use a different register, remove
378 // the def's anti-dependence *and* output-dependence edges due to
379 // that register, and add new anti-dependence and output-dependence
380 // edges based on the next live range of the register.
Andrew Trick47c14452012-03-07 05:21:52 +0000381 ScheduleDAG::clearDAG();
Andrew Trick953be892012-03-07 23:00:49 +0000382 buildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000383
David Goodwin2e7be612009-10-26 16:59:04 +0000384 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000385 }
386 }
387
David Greenee1b21292010-01-05 01:26:01 +0000388 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000389 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
390 SUnits[su].dumpAll(this));
391
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000392 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000393 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000394 AvailableQueue.releaseState();
395}
396
397/// Observe - Update liveness information to account for the current
398/// instruction, which will not be scheduled.
399///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000400void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700401 if (AntiDepBreak)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000402 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000403}
404
405/// FinishBlock - Clean up register live-range state.
406///
Andrew Trick953be892012-03-07 23:00:49 +0000407void SchedulePostRATDList::finishBlock() {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700408 if (AntiDepBreak)
David Goodwin2e7be612009-10-26 16:59:04 +0000409 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000410
411 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000412 ScheduleDAGInstrs::finishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000413}
414
Dan Gohman343f0c02008-11-19 23:18:57 +0000415//===----------------------------------------------------------------------===//
416// Top-Down Scheduling
417//===----------------------------------------------------------------------===//
418
419/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickae692f22012-11-12 19:28:57 +0000420/// the PendingQueue if the count reaches zero.
David Goodwin557bbe62009-11-20 19:32:48 +0000421void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000422 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000423
Andrew Trickcf6b6132012-11-13 02:35:06 +0000424 if (SuccEdge->isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000425 --SuccSU->WeakPredsLeft;
426 return;
427 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000428#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000429 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000430 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000431 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000432 dbgs() << " has been released too many times!\n";
Stephen Hinesdce4a402014-05-29 02:49:00 -0700433 llvm_unreachable(nullptr);
Dan Gohman343f0c02008-11-19 23:18:57 +0000434 }
435#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000436 --SuccSU->NumPredsLeft;
437
Andrew Trick89fd4372011-05-06 18:14:32 +0000438 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000439 // here as such:
440 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
441 //
442 // However, we lazily compute node depth instead. Note that
443 // ScheduleNodeTopDown has already updated the depth of this node which causes
444 // all descendents to be marked dirty. Setting the successor depth explicitly
445 // here would cause depth to be recomputed for all its ancestors. If the
446 // successor is not yet ready (because of a transitively redundant edge) then
447 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000448
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000449 // If all the node's predecessors are scheduled, this node is ready
450 // to be scheduled. Ignore the special ExitSU node.
451 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000452 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000453}
454
455/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000456void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000457 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000458 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000459 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000460 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000461}
462
463/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
464/// count of its successors. If a successor pending count is zero, add it to
465/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000466void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000467 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000468 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000469
Dan Gohman343f0c02008-11-19 23:18:57 +0000470 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000471 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000472 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000473 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000474
David Goodwin557bbe62009-11-20 19:32:48 +0000475 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000476 SU->isScheduled = true;
Andrew Trick953be892012-03-07 23:00:49 +0000477 AvailableQueue.scheduledNode(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000478}
479
Stephen Hines36b56882014-04-23 16:57:46 -0700480/// emitNoop - Add a noop to the current instruction sequence.
481void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
482 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
483 HazardRec->EmitNoop();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700484 Sequence.push_back(nullptr); // NULL here means noop
Stephen Hines36b56882014-04-23 16:57:46 -0700485 ++NumNoops;
486}
487
Dan Gohman343f0c02008-11-19 23:18:57 +0000488/// ListScheduleTopDown - The main loop of list scheduling for top-down
489/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000490void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000491 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000492
David Goodwin4de099d2009-11-03 20:57:50 +0000493 // We're scheduling top-down but we're visiting the regions in
494 // bottom-up order, so we don't know the hazards at the start of a
495 // region. So assume no hazards (this should usually be ok as most
496 // blocks are a single region).
497 HazardRec->Reset();
498
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000499 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000500 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000501
David Goodwin557bbe62009-11-20 19:32:48 +0000502 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000503 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
504 // It is available if it has no predecessors.
Andrew Trickae692f22012-11-12 19:28:57 +0000505 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000506 AvailableQueue.push(&SUnits[i]);
507 SUnits[i].isAvailable = true;
508 }
509 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000510
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000511 // In any cycle where we can't schedule any instructions, we must
512 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000513 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000514
Dan Gohman343f0c02008-11-19 23:18:57 +0000515 // While Available queue is not empty, grab the node with the highest
516 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000517 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000518 Sequence.reserve(SUnits.size());
519 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
520 // Check to see if any of the pending instructions are ready to issue. If
521 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000522 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000523 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000524 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000525 AvailableQueue.push(PendingQueue[i]);
526 PendingQueue[i]->isAvailable = true;
527 PendingQueue[i] = PendingQueue.back();
528 PendingQueue.pop_back();
529 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000530 } else if (PendingQueue[i]->getDepth() < MinDepth)
531 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000532 }
David Goodwinc93d8372009-08-11 17:35:23 +0000533
Andrew Trick2da8bc82010-12-24 05:03:26 +0000534 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000535
Stephen Hinesdce4a402014-05-29 02:49:00 -0700536 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohman2836c282009-01-16 01:33:36 +0000537 bool HasNoopHazards = false;
538 while (!AvailableQueue.empty()) {
539 SUnit *CurSUnit = AvailableQueue.pop();
540
541 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000542 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000543 if (HT == ScheduleHazardRecognizer::NoHazard) {
Stephen Hines36b56882014-04-23 16:57:46 -0700544 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
545 if (!NotPreferredSUnit) {
546 // If this is the first non-preferred node for this cycle, then
547 // record it and continue searching for a preferred node. If this
548 // is not the first non-preferred node, then treat it as though
549 // there had been a hazard.
550 NotPreferredSUnit = CurSUnit;
551 continue;
552 }
553 } else {
554 FoundSUnit = CurSUnit;
555 break;
556 }
Dan Gohman2836c282009-01-16 01:33:36 +0000557 }
558
559 // Remember if this is a noop hazard.
560 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
561
562 NotReady.push_back(CurSUnit);
563 }
564
Stephen Hines36b56882014-04-23 16:57:46 -0700565 // If we have a non-preferred node, push it back onto the available list.
566 // If we did not find a preferred node, then schedule this first
567 // non-preferred node.
568 if (NotPreferredSUnit) {
569 if (!FoundSUnit) {
570 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
571 FoundSUnit = NotPreferredSUnit;
572 } else {
573 AvailableQueue.push(NotPreferredSUnit);
574 }
575
Stephen Hinesdce4a402014-05-29 02:49:00 -0700576 NotPreferredSUnit = nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -0700577 }
578
Dan Gohman2836c282009-01-16 01:33:36 +0000579 // Add the nodes that aren't ready back onto the available list.
580 if (!NotReady.empty()) {
581 AvailableQueue.push_all(NotReady);
582 NotReady.clear();
583 }
584
David Goodwin4de099d2009-11-03 20:57:50 +0000585 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000586 if (FoundSUnit) {
Stephen Hines36b56882014-04-23 16:57:46 -0700587 // If we need to emit noops prior to this instruction, then do so.
588 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
589 for (unsigned i = 0; i != NumPreNoops; ++i)
590 emitNoop(CurCycle);
591
David Goodwin4de099d2009-11-03 20:57:50 +0000592 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000593 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000594 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000595 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000596 if (HazardRec->atIssueLimit()) {
597 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
598 HazardRec->AdvanceCycle();
599 ++CurCycle;
600 CycleHasInsts = false;
601 }
Dan Gohman2836c282009-01-16 01:33:36 +0000602 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000603 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000604 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000605 HazardRec->AdvanceCycle();
606 } else if (!HasNoopHazards) {
607 // Otherwise, we have a pipeline stall, but no other problem,
608 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000609 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000610 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000611 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000612 } else {
613 // Otherwise, we have no instructions to issue and we have instructions
614 // that will fault if we don't do this right. This is the case for
615 // processors without pipeline interlocks and other cases.
Stephen Hines36b56882014-04-23 16:57:46 -0700616 emitNoop(CurCycle);
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000617 }
618
Dan Gohman2836c282009-01-16 01:33:36 +0000619 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000620 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000621 }
622 }
623
624#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000625 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
626 unsigned Noops = 0;
627 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
628 if (!Sequence[i])
629 ++Noops;
630 assert(Sequence.size() - Noops == ScheduledNodes &&
631 "The number of nodes scheduled doesn't match the expected number!");
632#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000633}
Andrew Trick84b454d2012-03-07 05:21:44 +0000634
635// EmitSchedule - Emit the machine code in scheduled order.
636void SchedulePostRATDList::EmitSchedule() {
Andrew Trick68675c62012-03-09 04:29:02 +0000637 RegionBegin = RegionEnd;
Andrew Trick84b454d2012-03-07 05:21:44 +0000638
639 // If first instruction was a DBG_VALUE then put it back.
640 if (FirstDbgValue)
Andrew Trick68675c62012-03-09 04:29:02 +0000641 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Trick84b454d2012-03-07 05:21:44 +0000642
643 // Then re-insert them according to the given schedule.
644 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
645 if (SUnit *SU = Sequence[i])
Andrew Trick68675c62012-03-09 04:29:02 +0000646 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Trick84b454d2012-03-07 05:21:44 +0000647 else
648 // Null SUnit* is a noop.
Andrew Trick68675c62012-03-09 04:29:02 +0000649 TII->insertNoop(*BB, RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000650
651 // Update the Begin iterator, as the first instruction in the block
652 // may have been scheduled later.
653 if (i == 0)
Stephen Hines36b56882014-04-23 16:57:46 -0700654 RegionBegin = std::prev(RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000655 }
656
657 // Reinsert any remaining debug_values.
658 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
659 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Stephen Hines36b56882014-04-23 16:57:46 -0700660 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Trick84b454d2012-03-07 05:21:44 +0000661 MachineInstr *DbgValue = P.first;
662 MachineBasicBlock::iterator OrigPrivMI = P.second;
663 BB->splice(++OrigPrivMI, BB, DbgValue);
664 }
665 DbgValues.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700666 FirstDbgValue = nullptr;
Andrew Trick84b454d2012-03-07 05:21:44 +0000667}