blob: 89e1d113ff70e9ca000547d4087b4170faa4f9fe [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetSubtargetInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000046using namespace llvm;
47
Stephen Hinesdce4a402014-05-29 02:49:00 -070048#define DEBUG_TYPE "post-RA-sched"
49
Dan Gohman2836c282009-01-16 01:33:36 +000050STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000051STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin2e7be612009-10-26 16:59:04 +000052STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman343f0c02008-11-19 23:18:57 +000053
David Goodwin471850a2009-10-01 21:46:35 +000054// Post-RA scheduling is enabled with
Evan Cheng5b1b44892011-07-01 21:01:15 +000055// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin471850a2009-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 Goodwin9843a932009-10-01 22:19:57 +000060 cl::init(false), cl::Hidden);
David Goodwin2e7be612009-10-26 16:59:04 +000061static cl::opt<std::string>
Dan Gohman21d90032008-11-25 00:52:40 +000062EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin2e7be612009-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 Gohman2836c282009-01-16 01:33:36 +000066
David Goodwin1f152282009-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 Goodwinada0ef82009-10-26 19:41:00 +000077AntiDepBreaker::~AntiDepBreaker() { }
78
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000079namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000080 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng86050dc2010-06-18 23:09:54 +000081 const TargetInstrInfo *TII;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000082 RegisterClassInfo RegClassInfo;
Dan Gohmana70dca12009-10-09 23:27:56 +000083
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000084 public:
85 static char ID;
Andrew Trickc7d081b2012-02-08 21:22:53 +000086 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000087
Stephen Hines36b56882014-04-23 16:57:46 -070088 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman845012e2009-07-31 23:37:33 +000089 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000090 AU.addRequired<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +000091 AU.addRequired<TargetPassConfig>();
Dan Gohman3f237442008-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
Stephen Hines36b56882014-04-23 16:57:46 -070099 bool runOnMachineFunction(MachineFunction &Fn) override;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800100
101 bool enablePostRAScheduler(
102 const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel,
103 TargetSubtargetInfo::AntiDepBreakMode &Mode,
104 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const;
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000105 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000106 char PostRAScheduler::ID = 0;
107
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000108 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000109 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000110 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000111 LatencyPriorityQueue AvailableQueue;
Jim Grosbach90013032010-05-14 21:19:48 +0000112
Dan Gohman343f0c02008-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 Gohman2836c282009-01-16 01:33:36 +0000119 /// HazardRec - The hazard recognizer to use.
120 ScheduleHazardRecognizer *HazardRec;
121
David Goodwin2e7be612009-10-26 16:59:04 +0000122 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
123 AntiDepBreaker *AntiDepBreak;
124
Dan Gohmana70dca12009-10-09 23:27:56 +0000125 /// AA - AliasAnalysis for making memory reference queries.
126 AliasAnalysis *AA;
127
Andrew Trick47c14452012-03-07 05:21:52 +0000128 /// The schedule. Null SUnit*'s represent noop instructions.
129 std::vector<SUnit*> Sequence;
130
Andrew Trickd2763f62013-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 Gohman21d90032008-11-25 00:52:40 +0000137 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000138 SchedulePostRATDList(
Stephen Hines37ed9c12014-12-01 14:51:49 -0800139 MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA,
140 const RegisterClassInfo &,
141 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
142 SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000143
Andrew Trick2da8bc82010-12-24 05:03:26 +0000144 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000145
Andrew Trick953be892012-03-07 23:00:49 +0000146 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000147 /// this block.
148 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700149 void startBlock(MachineBasicBlock *BB) override;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000150
Andrew Trickd2763f62013-08-23 17:48:33 +0000151 // Set the index of RegionEnd within the current BB.
152 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
153
Andrew Trick47c14452012-03-07 05:21:52 +0000154 /// Initialize the scheduler state for the next scheduling region.
Stephen Hines36b56882014-04-23 16:57:46 -0700155 void enterRegion(MachineBasicBlock *bb,
156 MachineBasicBlock::iterator begin,
157 MachineBasicBlock::iterator end,
158 unsigned regioninstrs) override;
Andrew Trick47c14452012-03-07 05:21:52 +0000159
160 /// Notify that the scheduler has finished scheduling the current region.
Stephen Hines36b56882014-04-23 16:57:46 -0700161 void exitRegion() override;
Andrew Trick47c14452012-03-07 05:21:52 +0000162
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000163 /// Schedule - Schedule the instruction range using list scheduling.
164 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700165 void schedule() override;
Jim Grosbach90013032010-05-14 21:19:48 +0000166
Andrew Trick84b454d2012-03-07 05:21:44 +0000167 void EmitSchedule();
168
Dan Gohmanc1ae8c92009-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 Trick953be892012-03-07 23:00:49 +0000174 /// finishBlock - Clean up register live-range state.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000175 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700176 void finishBlock() override;
David Goodwin2e7be612009-10-26 16:59:04 +0000177
Dan Gohman343f0c02008-11-19 23:18:57 +0000178 private:
David Goodwin557bbe62009-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();
Andrew Trick73ba69b2012-03-07 05:21:40 +0000183
184 void dumpSchedule() const;
Stephen Hines36b56882014-04-23 16:57:46 -0700185 void emitNoop(unsigned CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000186 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000187}
188
Andrew Trick1dd8c852012-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 Trick2da8bc82010-12-24 05:03:26 +0000194SchedulePostRATDList::SchedulePostRATDList(
Stephen Hines37ed9c12014-12-01 14:51:49 -0800195 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) {
Stephen Hines36b56882014-04-23 16:57:46 -0700200
Stephen Hines37ed9c12014-12-01 14:51:49 -0800201 const InstrItineraryData *InstrItins =
202 MF.getSubtarget().getInstrItineraryData();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000203 HazardRec =
Stephen Hines37ed9c12014-12-01 14:51:49 -0800204 MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer(
205 InstrItins, this);
Preston Gurd6a8c7bf2012-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 Trick2da8bc82010-12-24 05:03:26 +0000210 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000211 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000212 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000213 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Stephen Hinesdce4a402014-05-29 02:49:00 -0700214 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000215}
216
217SchedulePostRATDList::~SchedulePostRATDList() {
218 delete HazardRec;
219 delete AntiDepBreak;
220}
221
Andrew Trick47c14452012-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 Trickd2763f62013-08-23 17:48:33 +0000226 unsigned regioninstrs) {
227 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick47c14452012-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 Renb720be62012-09-11 22:23:19 +0000241#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick73ba69b2012-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 Ren77e300e2012-09-06 19:06:06 +0000251#endif
Andrew Trick73ba69b2012-03-07 05:21:40 +0000252
Stephen Hines37ed9c12014-12-01 14:51:49 -0800253bool 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);
260 return ST.enablePostMachineScheduler() &&
261 OptLevel >= ST.getOptLevelToEnablePostRAScheduler();
262}
263
Dan Gohman343f0c02008-11-19 23:18:57 +0000264bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Stephen Hines36b56882014-04-23 16:57:46 -0700265 if (skipOptnoneFunction(*Fn.getFunction()))
266 return false;
267
Stephen Hines37ed9c12014-12-01 14:51:49 -0800268 TII = Fn.getSubtarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000269 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000270 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000271 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
272
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000273 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000274
David Goodwin471850a2009-10-01 21:46:35 +0000275 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000276 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
277 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000278 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000279 if (EnablePostRAScheduler.getPosition() > 0) {
280 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000281 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000282 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000283 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000284 // This may upgrade the AntiDepMode.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800285 const TargetSubtargetInfo &ST =
286 Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
287 if (!enablePostRAScheduler(ST, PassConfig->getOptLevel(),
288 AntiDepMode, CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000289 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000290 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000291
David Goodwin4c3715c2009-10-22 23:19:17 +0000292 // Check for antidep breaking override...
293 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000294 AntiDepMode = (EnableAntiDepBreaking == "all")
295 ? TargetSubtargetInfo::ANTIDEP_ALL
296 : ((EnableAntiDepBreaking == "critical")
297 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
298 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000299 }
300
David Greenee1b21292010-01-05 01:26:01 +0000301 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000302
Stephen Hines37ed9c12014-12-01 14:51:49 -0800303 SchedulePostRATDList Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000304 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000305
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000306 // Loop over all of the basic blocks
307 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000308 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000309#ifndef NDEBUG
310 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
311 if (DebugDiv > 0) {
312 static int bbcnt = 0;
313 if (bbcnt++ % DebugDiv != DebugMod)
314 continue;
Craig Topper96601ca2012-08-22 06:07:19 +0000315 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000316 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000317 }
318#endif
319
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000320 // Initialize register live-range state for scheduling in this block.
Andrew Trick953be892012-03-07 23:00:49 +0000321 Scheduler.startBlock(MBB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000322
Dan Gohmanf7119392009-01-16 22:10:20 +0000323 // Schedule each sequence of instructions not interrupted by a label
324 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000325 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000326 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000327 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Stephen Hines36b56882014-04-23 16:57:46 -0700328 MachineInstr *MI = std::prev(I);
Andrew Trickd2763f62013-08-23 17:48:33 +0000329 --Count;
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000330 // Calls are not scheduling boundaries before register allocation, but
331 // post-ra we don't gain anything by scheduling across calls since we
332 // don't need to worry about register pressure.
333 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Trickd2763f62013-08-23 17:48:33 +0000334 Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count);
335 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 Current = MI;
Andrew Trickd2763f62013-08-23 17:48:33 +0000340 CurrentCount = Count;
Dan Gohman1274ced2009-03-10 18:10:43 +0000341 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000342 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000343 I = MI;
Evan Chengddfd1372011-12-14 02:11:42 +0000344 if (MI->isBundle())
345 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000346 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000347 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000348 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000349 "Instruction count mismatch!");
Andrew Trick47c14452012-03-07 05:21:52 +0000350 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Trickd2763f62013-08-23 17:48:33 +0000351 Scheduler.setEndIndex(CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000352 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000353 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000354 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000355
356 // Clean up register live-range state.
Andrew Trick953be892012-03-07 23:00:49 +0000357 Scheduler.finishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000358
David Goodwin5e411782009-09-03 22:15:25 +0000359 // Update register kills
Stephen Hines36b56882014-04-23 16:57:46 -0700360 Scheduler.fixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000361 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000362
363 return true;
364}
Jim Grosbach90013032010-05-14 21:19:48 +0000365
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000366/// StartBlock - Initialize register live-range state for scheduling in
367/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000368///
Andrew Trick953be892012-03-07 23:00:49 +0000369void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000370 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000371 ScheduleDAGInstrs::startBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000372
David Goodwin2e7be612009-10-26 16:59:04 +0000373 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000374 HazardRec->Reset();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700375 if (AntiDepBreak)
David Goodwin2e7be612009-10-26 16:59:04 +0000376 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000377}
378
379/// Schedule - Schedule the instruction range using list scheduling.
380///
Andrew Trick953be892012-03-07 23:00:49 +0000381void SchedulePostRATDList::schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000382 // Build the scheduling graph.
Andrew Trick953be892012-03-07 23:00:49 +0000383 buildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000384
Stephen Hinesdce4a402014-05-29 02:49:00 -0700385 if (AntiDepBreak) {
Jim Grosbach90013032010-05-14 21:19:48 +0000386 unsigned Broken =
Andrew Trick68675c62012-03-09 04:29:02 +0000387 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
388 EndIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000389
David Goodwin557bbe62009-11-20 19:32:48 +0000390 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000391 // We made changes. Update the dependency graph.
392 // Theoretically we could update the graph in place:
393 // When a live range is changed to use a different register, remove
394 // the def's anti-dependence *and* output-dependence edges due to
395 // that register, and add new anti-dependence and output-dependence
396 // edges based on the next live range of the register.
Andrew Trick47c14452012-03-07 05:21:52 +0000397 ScheduleDAG::clearDAG();
Andrew Trick953be892012-03-07 23:00:49 +0000398 buildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000399
David Goodwin2e7be612009-10-26 16:59:04 +0000400 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000401 }
402 }
403
David Greenee1b21292010-01-05 01:26:01 +0000404 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000405 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
406 SUnits[su].dumpAll(this));
407
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000408 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000409 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000410 AvailableQueue.releaseState();
411}
412
413/// Observe - Update liveness information to account for the current
414/// instruction, which will not be scheduled.
415///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000416void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700417 if (AntiDepBreak)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000418 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000419}
420
421/// FinishBlock - Clean up register live-range state.
422///
Andrew Trick953be892012-03-07 23:00:49 +0000423void SchedulePostRATDList::finishBlock() {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700424 if (AntiDepBreak)
David Goodwin2e7be612009-10-26 16:59:04 +0000425 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000426
427 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000428 ScheduleDAGInstrs::finishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000429}
430
Dan Gohman343f0c02008-11-19 23:18:57 +0000431//===----------------------------------------------------------------------===//
432// Top-Down Scheduling
433//===----------------------------------------------------------------------===//
434
435/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickae692f22012-11-12 19:28:57 +0000436/// the PendingQueue if the count reaches zero.
David Goodwin557bbe62009-11-20 19:32:48 +0000437void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000438 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000439
Andrew Trickcf6b6132012-11-13 02:35:06 +0000440 if (SuccEdge->isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000441 --SuccSU->WeakPredsLeft;
442 return;
443 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000444#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000445 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000446 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000447 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000448 dbgs() << " has been released too many times!\n";
Stephen Hinesdce4a402014-05-29 02:49:00 -0700449 llvm_unreachable(nullptr);
Dan Gohman343f0c02008-11-19 23:18:57 +0000450 }
451#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000452 --SuccSU->NumPredsLeft;
453
Andrew Trick89fd4372011-05-06 18:14:32 +0000454 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000455 // here as such:
456 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
457 //
458 // However, we lazily compute node depth instead. Note that
459 // ScheduleNodeTopDown has already updated the depth of this node which causes
460 // all descendents to be marked dirty. Setting the successor depth explicitly
461 // here would cause depth to be recomputed for all its ancestors. If the
462 // successor is not yet ready (because of a transitively redundant edge) then
463 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000464
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000465 // If all the node's predecessors are scheduled, this node is ready
466 // to be scheduled. Ignore the special ExitSU node.
467 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000468 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000469}
470
471/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000472void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000473 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000474 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000475 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000476 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000477}
478
479/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
480/// count of its successors. If a successor pending count is zero, add it to
481/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000482void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000483 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000484 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000485
Dan Gohman343f0c02008-11-19 23:18:57 +0000486 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000487 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000488 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000489 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000490
David Goodwin557bbe62009-11-20 19:32:48 +0000491 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000492 SU->isScheduled = true;
Andrew Trick953be892012-03-07 23:00:49 +0000493 AvailableQueue.scheduledNode(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000494}
495
Stephen Hines36b56882014-04-23 16:57:46 -0700496/// emitNoop - Add a noop to the current instruction sequence.
497void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
498 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
499 HazardRec->EmitNoop();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700500 Sequence.push_back(nullptr); // NULL here means noop
Stephen Hines36b56882014-04-23 16:57:46 -0700501 ++NumNoops;
502}
503
Dan Gohman343f0c02008-11-19 23:18:57 +0000504/// ListScheduleTopDown - The main loop of list scheduling for top-down
505/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000506void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000507 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000508
David Goodwin4de099d2009-11-03 20:57:50 +0000509 // We're scheduling top-down but we're visiting the regions in
510 // bottom-up order, so we don't know the hazards at the start of a
511 // region. So assume no hazards (this should usually be ok as most
512 // blocks are a single region).
513 HazardRec->Reset();
514
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000515 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000516 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000517
David Goodwin557bbe62009-11-20 19:32:48 +0000518 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000519 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
520 // It is available if it has no predecessors.
Andrew Trickae692f22012-11-12 19:28:57 +0000521 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000522 AvailableQueue.push(&SUnits[i]);
523 SUnits[i].isAvailable = true;
524 }
525 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000526
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000527 // In any cycle where we can't schedule any instructions, we must
528 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000529 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000530
Dan Gohman343f0c02008-11-19 23:18:57 +0000531 // While Available queue is not empty, grab the node with the highest
532 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000533 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000534 Sequence.reserve(SUnits.size());
535 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
536 // Check to see if any of the pending instructions are ready to issue. If
537 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000538 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000539 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000540 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000541 AvailableQueue.push(PendingQueue[i]);
542 PendingQueue[i]->isAvailable = true;
543 PendingQueue[i] = PendingQueue.back();
544 PendingQueue.pop_back();
545 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000546 } else if (PendingQueue[i]->getDepth() < MinDepth)
547 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000548 }
David Goodwinc93d8372009-08-11 17:35:23 +0000549
Andrew Trick2da8bc82010-12-24 05:03:26 +0000550 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000551
Stephen Hinesdce4a402014-05-29 02:49:00 -0700552 SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr;
Dan Gohman2836c282009-01-16 01:33:36 +0000553 bool HasNoopHazards = false;
554 while (!AvailableQueue.empty()) {
555 SUnit *CurSUnit = AvailableQueue.pop();
556
557 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000558 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000559 if (HT == ScheduleHazardRecognizer::NoHazard) {
Stephen Hines36b56882014-04-23 16:57:46 -0700560 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
561 if (!NotPreferredSUnit) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800562 // If this is the first non-preferred node for this cycle, then
563 // record it and continue searching for a preferred node. If this
564 // is not the first non-preferred node, then treat it as though
565 // there had been a hazard.
Stephen Hines36b56882014-04-23 16:57:46 -0700566 NotPreferredSUnit = CurSUnit;
567 continue;
568 }
569 } else {
570 FoundSUnit = CurSUnit;
571 break;
572 }
Dan Gohman2836c282009-01-16 01:33:36 +0000573 }
574
575 // Remember if this is a noop hazard.
576 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
577
578 NotReady.push_back(CurSUnit);
579 }
580
Stephen Hines36b56882014-04-23 16:57:46 -0700581 // If we have a non-preferred node, push it back onto the available list.
582 // If we did not find a preferred node, then schedule this first
583 // non-preferred node.
584 if (NotPreferredSUnit) {
585 if (!FoundSUnit) {
586 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
587 FoundSUnit = NotPreferredSUnit;
588 } else {
589 AvailableQueue.push(NotPreferredSUnit);
590 }
591
Stephen Hinesdce4a402014-05-29 02:49:00 -0700592 NotPreferredSUnit = nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -0700593 }
594
Dan Gohman2836c282009-01-16 01:33:36 +0000595 // Add the nodes that aren't ready back onto the available list.
596 if (!NotReady.empty()) {
597 AvailableQueue.push_all(NotReady);
598 NotReady.clear();
599 }
600
David Goodwin4de099d2009-11-03 20:57:50 +0000601 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000602 if (FoundSUnit) {
Stephen Hines36b56882014-04-23 16:57:46 -0700603 // If we need to emit noops prior to this instruction, then do so.
604 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
605 for (unsigned i = 0; i != NumPreNoops; ++i)
606 emitNoop(CurCycle);
607
David Goodwin4de099d2009-11-03 20:57:50 +0000608 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000609 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000610 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000611 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000612 if (HazardRec->atIssueLimit()) {
613 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
614 HazardRec->AdvanceCycle();
615 ++CurCycle;
616 CycleHasInsts = false;
617 }
Dan Gohman2836c282009-01-16 01:33:36 +0000618 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000619 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000620 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000621 HazardRec->AdvanceCycle();
622 } else if (!HasNoopHazards) {
623 // Otherwise, we have a pipeline stall, but no other problem,
624 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000625 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000626 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000627 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000628 } else {
629 // Otherwise, we have no instructions to issue and we have instructions
630 // that will fault if we don't do this right. This is the case for
631 // processors without pipeline interlocks and other cases.
Stephen Hines36b56882014-04-23 16:57:46 -0700632 emitNoop(CurCycle);
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000633 }
634
Dan Gohman2836c282009-01-16 01:33:36 +0000635 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000636 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000637 }
638 }
639
640#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000641 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
642 unsigned Noops = 0;
643 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
644 if (!Sequence[i])
645 ++Noops;
646 assert(Sequence.size() - Noops == ScheduledNodes &&
647 "The number of nodes scheduled doesn't match the expected number!");
648#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000649}
Andrew Trick84b454d2012-03-07 05:21:44 +0000650
651// EmitSchedule - Emit the machine code in scheduled order.
652void SchedulePostRATDList::EmitSchedule() {
Andrew Trick68675c62012-03-09 04:29:02 +0000653 RegionBegin = RegionEnd;
Andrew Trick84b454d2012-03-07 05:21:44 +0000654
655 // If first instruction was a DBG_VALUE then put it back.
656 if (FirstDbgValue)
Andrew Trick68675c62012-03-09 04:29:02 +0000657 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Trick84b454d2012-03-07 05:21:44 +0000658
659 // Then re-insert them according to the given schedule.
660 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
661 if (SUnit *SU = Sequence[i])
Andrew Trick68675c62012-03-09 04:29:02 +0000662 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Trick84b454d2012-03-07 05:21:44 +0000663 else
664 // Null SUnit* is a noop.
Andrew Trick68675c62012-03-09 04:29:02 +0000665 TII->insertNoop(*BB, RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000666
667 // Update the Begin iterator, as the first instruction in the block
668 // may have been scheduled later.
669 if (i == 0)
Stephen Hines36b56882014-04-23 16:57:46 -0700670 RegionBegin = std::prev(RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000671 }
672
673 // Reinsert any remaining debug_values.
674 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
675 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
Stephen Hines36b56882014-04-23 16:57:46 -0700676 std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI);
Andrew Trick84b454d2012-03-07 05:21:44 +0000677 MachineInstr *DbgValue = P.first;
678 MachineBasicBlock::iterator OrigPrivMI = P.second;
679 BB->splice(++OrigPrivMI, BB, DbgValue);
680 }
681 DbgValues.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700682 FirstDbgValue = nullptr;
Andrew Trick84b454d2012-03-07 05:21:44 +0000683}