blob: c00cf0e0439c146ebd63f6d0e6312fab4339569c [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
21#define DEBUG_TYPE "post-RA-sched"
David Goodwin82c72482009-10-28 18:29:54 +000022#include "AntiDepBreaker.h"
David Goodwin34877712009-10-26 19:32:42 +000023#include "AggressiveAntiDepBreaker.h"
David Goodwin2e7be612009-10-26 16:59:04 +000024#include "CriticalAntiDepBreaker.h"
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000025#include "RegisterClassInfo.h"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000026#include "ScheduleDAGInstrs.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000028#include "llvm/CodeGen/LatencyPriorityQueue.h"
29#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3f237442008-12-16 03:25:46 +000030#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000033#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2836c282009-01-16 01:33:36 +000035#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000036#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohmanbed353d2009-02-10 23:29:38 +000037#include "llvm/Target/TargetLowering.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000038#include "llvm/Target/TargetMachine.h"
Dan Gohman21d90032008-11-25 00:52:40 +000039#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000041#include "llvm/Target/TargetSubtargetInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000042#include "llvm/Support/CommandLine.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000043#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000044#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000045#include "llvm/Support/raw_ostream.h"
David Goodwin2e7be612009-10-26 16:59:04 +000046#include "llvm/ADT/BitVector.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000047#include "llvm/ADT/Statistic.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000048using namespace llvm;
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 {
Dan Gohmana70dca12009-10-09 23:27:56 +000081 AliasAnalysis *AA;
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
Dan Gohman3f237442008-12-16 03:25:46 +000089 void getAnalysisUsage(AnalysisUsage &AU) const {
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
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000100 bool runOnMachineFunction(MachineFunction &Fn);
101 };
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 Gohman21d90032008-11-25 00:52:40 +0000115 /// Topo - A topological ordering for SUnits.
116 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000117
Dan Gohman2836c282009-01-16 01:33:36 +0000118 /// HazardRec - The hazard recognizer to use.
119 ScheduleHazardRecognizer *HazardRec;
120
David Goodwin2e7be612009-10-26 16:59:04 +0000121 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
122 AntiDepBreaker *AntiDepBreak;
123
Dan Gohmana70dca12009-10-09 23:27:56 +0000124 /// AA - AliasAnalysis for making memory reference queries.
125 AliasAnalysis *AA;
126
Benjamin Kramer46252d82012-02-23 19:15:40 +0000127 /// LiveRegs - true if the register is live.
128 BitVector LiveRegs;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000129
Andrew Trick47c14452012-03-07 05:21:52 +0000130 /// The schedule. Null SUnit*'s represent noop instructions.
131 std::vector<SUnit*> Sequence;
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 ///
Andrew Trick953be892012-03-07 23:00:49 +0000145 void startBlock(MachineBasicBlock *BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146
Andrew Trick47c14452012-03-07 05:21:52 +0000147 /// Initialize the scheduler state for the next scheduling region.
148 virtual void enterRegion(MachineBasicBlock *bb,
149 MachineBasicBlock::iterator begin,
150 MachineBasicBlock::iterator end,
151 unsigned endcount);
152
153 /// Notify that the scheduler has finished scheduling the current region.
154 virtual void exitRegion();
155
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000156 /// Schedule - Schedule the instruction range using list scheduling.
157 ///
Andrew Trick953be892012-03-07 23:00:49 +0000158 void schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000159
Andrew Trick84b454d2012-03-07 05:21:44 +0000160 void EmitSchedule();
161
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000162 /// Observe - Update liveness information to account for the current
163 /// instruction, which will not be scheduled.
164 ///
165 void Observe(MachineInstr *MI, unsigned Count);
166
Andrew Trick953be892012-03-07 23:00:49 +0000167 /// finishBlock - Clean up register live-range state.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000168 ///
Andrew Trick953be892012-03-07 23:00:49 +0000169 void finishBlock();
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000170
David Goodwin2e7be612009-10-26 16:59:04 +0000171 /// FixupKills - Fix register kill flags that have been made
172 /// invalid due to scheduling
173 ///
174 void FixupKills(MachineBasicBlock *MBB);
175
Dan Gohman343f0c02008-11-19 23:18:57 +0000176 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000177 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
178 void ReleaseSuccessors(SUnit *SU);
179 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
180 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000181 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000182
David Goodwin8f909342009-09-23 16:35:25 +0000183 // ToggleKillFlag - Toggle a register operand kill flag. Other
184 // adjustments may be made to the instruction if necessary. Return
185 // true if the operand has been deleted, false if not.
186 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Andrew Trick73ba69b2012-03-07 05:21:40 +0000187
188 void dumpSchedule() const;
Dan Gohman343f0c02008-11-19 23:18:57 +0000189 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000190}
191
Andrew Trick1dd8c852012-02-08 21:23:13 +0000192char &llvm::PostRASchedulerID = PostRAScheduler::ID;
193
194INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
195 "Post RA top-down list latency scheduler", false, false)
196
Andrew Trick2da8bc82010-12-24 05:03:26 +0000197SchedulePostRATDList::SchedulePostRATDList(
198 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000199 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000200 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000201 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trick5e920d72012-01-14 02:17:12 +0000202 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
Benjamin Kramer46252d82012-02-23 19:15:40 +0000203 LiveRegs(TRI->getNumRegs())
Andrew Trick2da8bc82010-12-24 05:03:26 +0000204{
205 const TargetMachine &TM = MF.getTarget();
206 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
207 HazardRec =
208 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
209 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000210 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000211 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000212 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000213 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000214}
215
216SchedulePostRATDList::~SchedulePostRATDList() {
217 delete HazardRec;
218 delete AntiDepBreak;
219}
220
Andrew Trick47c14452012-03-07 05:21:52 +0000221/// Initialize state associated with the next scheduling region.
222void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
223 MachineBasicBlock::iterator begin,
224 MachineBasicBlock::iterator end,
225 unsigned endcount) {
226 ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount);
227 Sequence.clear();
228}
229
230/// Print the schedule before exiting the region.
231void SchedulePostRATDList::exitRegion() {
232 DEBUG({
233 dbgs() << "*** Final schedule ***\n";
234 dumpSchedule();
235 dbgs() << '\n';
236 });
237 ScheduleDAGInstrs::exitRegion();
238}
239
Andrew Trick73ba69b2012-03-07 05:21:40 +0000240/// dumpSchedule - dump the scheduled Sequence.
241void SchedulePostRATDList::dumpSchedule() const {
242 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
243 if (SUnit *SU = Sequence[i])
244 SU->dump(this);
245 else
246 dbgs() << "**** NOOP ****\n";
247 }
248}
249
Dan Gohman343f0c02008-11-19 23:18:57 +0000250bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000251 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000252 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
253 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
254 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000255 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
256
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000257 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000258
David Goodwin471850a2009-10-01 21:46:35 +0000259 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000260 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
261 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000262 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000263 if (EnablePostRAScheduler.getPosition() > 0) {
264 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000265 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000266 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000267 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000268 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000269 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000270 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
271 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000272 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000273 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000274
David Goodwin4c3715c2009-10-22 23:19:17 +0000275 // Check for antidep breaking override...
276 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000277 AntiDepMode = (EnableAntiDepBreaking == "all")
278 ? TargetSubtargetInfo::ANTIDEP_ALL
279 : ((EnableAntiDepBreaking == "critical")
280 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
281 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000282 }
283
David Greenee1b21292010-01-05 01:26:01 +0000284 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000285
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000286 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000287 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000288
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000289 // Loop over all of the basic blocks
290 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000291 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000292#ifndef NDEBUG
293 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
294 if (DebugDiv > 0) {
295 static int bbcnt = 0;
296 if (bbcnt++ % DebugDiv != DebugMod)
297 continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000298 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
299 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000300 }
301#endif
302
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000303 // Initialize register live-range state for scheduling in this block.
Andrew Trick953be892012-03-07 23:00:49 +0000304 Scheduler.startBlock(MBB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000305
Dan Gohmanf7119392009-01-16 22:10:20 +0000306 // Schedule each sequence of instructions not interrupted by a label
307 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000308 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000309 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000310 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000311 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000312 // Calls are not scheduling boundaries before register allocation, but
313 // post-ra we don't gain anything by scheduling across calls since we
314 // don't need to worry about register pressure.
315 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Trick47c14452012-03-07 05:21:52 +0000316 Scheduler.enterRegion(MBB, I, Current, CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000317 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000318 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000319 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000320 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000321 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000322 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000323 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000324 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000325 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000326 if (MI->isBundle())
327 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000328 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000329 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000330 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000331 "Instruction count mismatch!");
Andrew Trick47c14452012-03-07 05:21:52 +0000332 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000333 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000334 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000335 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000336
337 // Clean up register live-range state.
Andrew Trick953be892012-03-07 23:00:49 +0000338 Scheduler.finishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000339
David Goodwin5e411782009-09-03 22:15:25 +0000340 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000341 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000342 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000343
344 return true;
345}
Jim Grosbach90013032010-05-14 21:19:48 +0000346
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000347/// StartBlock - Initialize register live-range state for scheduling in
348/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000349///
Andrew Trick953be892012-03-07 23:00:49 +0000350void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000351 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000352 ScheduleDAGInstrs::startBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000353
David Goodwin2e7be612009-10-26 16:59:04 +0000354 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000355 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000356 if (AntiDepBreak != NULL)
357 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000358}
359
360/// Schedule - Schedule the instruction range using list scheduling.
361///
Andrew Trick953be892012-03-07 23:00:49 +0000362void SchedulePostRATDList::schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000363 // Build the scheduling graph.
Andrew Trick953be892012-03-07 23:00:49 +0000364 buildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000365
David Goodwin2e7be612009-10-26 16:59:04 +0000366 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000367 unsigned Broken =
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000368 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, End, EndIndex,
369 DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000370
David Goodwin557bbe62009-11-20 19:32:48 +0000371 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000372 // We made changes. Update the dependency graph.
373 // Theoretically we could update the graph in place:
374 // When a live range is changed to use a different register, remove
375 // the def's anti-dependence *and* output-dependence edges due to
376 // that register, and add new anti-dependence and output-dependence
377 // edges based on the next live range of the register.
Andrew Trick47c14452012-03-07 05:21:52 +0000378 ScheduleDAG::clearDAG();
Andrew Trick953be892012-03-07 23:00:49 +0000379 buildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000380
David Goodwin2e7be612009-10-26 16:59:04 +0000381 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000382 }
383 }
384
David Greenee1b21292010-01-05 01:26:01 +0000385 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000386 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
387 SUnits[su].dumpAll(this));
388
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000389 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000390 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000391 AvailableQueue.releaseState();
392}
393
394/// Observe - Update liveness information to account for the current
395/// instruction, which will not be scheduled.
396///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000397void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000398 if (AntiDepBreak != NULL)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000399 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000400}
401
402/// FinishBlock - Clean up register live-range state.
403///
Andrew Trick953be892012-03-07 23:00:49 +0000404void SchedulePostRATDList::finishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000405 if (AntiDepBreak != NULL)
406 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000407
408 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000409 ScheduleDAGInstrs::finishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000410}
411
David Goodwin5e411782009-09-03 22:15:25 +0000412/// StartBlockForKills - Initialize register live-range state for updating kills
413///
414void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000415 // Start with no live registers.
416 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000417
418 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000419 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000420 // In a return block, examine the function live-out regs.
421 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
422 E = MRI.liveout_end(); I != E; ++I) {
423 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000424 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000425 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000426 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000427 *Subreg; ++Subreg)
428 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000429 }
430 }
431 else {
432 // In a non-return block, examine the live-in regs of all successors.
433 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
434 SE = BB->succ_end(); SI != SE; ++SI) {
435 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
436 E = (*SI)->livein_end(); I != E; ++I) {
437 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000438 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000439 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000440 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000441 *Subreg; ++Subreg)
442 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000443 }
444 }
445 }
446}
447
David Goodwin8f909342009-09-23 16:35:25 +0000448bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
449 MachineOperand &MO) {
450 // Setting kill flag...
451 if (!MO.isKill()) {
452 MO.setIsKill(true);
453 return false;
454 }
Jim Grosbach90013032010-05-14 21:19:48 +0000455
David Goodwin8f909342009-09-23 16:35:25 +0000456 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000457 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000458 MO.setIsKill(false);
459 return false;
460 }
461
462 // If any subreg of MO is live, then create an imp-def for that
463 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000464 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000465 bool AllDead = true;
466 const unsigned SuperReg = MO.getReg();
Craig Topper9ebfbf82012-03-05 05:37:41 +0000467 for (const uint16_t *Subreg = TRI->getSubRegisters(SuperReg);
David Goodwin8f909342009-09-23 16:35:25 +0000468 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000469 if (LiveRegs.test(*Subreg)) {
David Goodwin8f909342009-09-23 16:35:25 +0000470 MI->addOperand(MachineOperand::CreateReg(*Subreg,
471 true /*IsDef*/,
472 true /*IsImp*/,
473 false /*IsKill*/,
474 false /*IsDead*/));
475 AllDead = false;
476 }
477 }
478
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000479 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000480 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000481 return false;
482}
483
David Goodwin88a589c2009-08-25 17:03:05 +0000484/// FixupKills - Fix the register kill flags, they may have been made
485/// incorrect by instruction reordering.
486///
487void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000488 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000489
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000490 BitVector killedRegs(TRI->getNumRegs());
David Goodwin88a589c2009-08-25 17:03:05 +0000491 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000492
493 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000494
David Goodwin7886cd82009-08-29 00:11:13 +0000495 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000496 unsigned Count = MBB->size();
497 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
498 I != E; --Count) {
499 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000500 if (MI->isDebugValue())
501 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000502
David Goodwin7886cd82009-08-29 00:11:13 +0000503 // Update liveness. Registers that are defed but not used in this
504 // instruction are now dead. Mark register and all subregs as they
505 // are completely defined.
506 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
507 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000508 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000509 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000510 if (!MO.isReg()) continue;
511 unsigned Reg = MO.getReg();
512 if (Reg == 0) continue;
513 if (!MO.isDef()) continue;
514 // Ignore two-addr defs.
515 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000516
Benjamin Kramer46252d82012-02-23 19:15:40 +0000517 LiveRegs.reset(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000518
David Goodwin7886cd82009-08-29 00:11:13 +0000519 // Repeat for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000520 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000521 *Subreg; ++Subreg)
522 LiveRegs.reset(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000523 }
David Goodwin88a589c2009-08-25 17:03:05 +0000524
David Goodwin8f909342009-09-23 16:35:25 +0000525 // Examine all used registers and set/clear kill flag. When a
526 // register is used multiple times we only set the kill flag on
527 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000528 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000529 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
530 MachineOperand &MO = MI->getOperand(i);
531 if (!MO.isReg() || !MO.isUse()) continue;
532 unsigned Reg = MO.getReg();
533 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
534
David Goodwin7886cd82009-08-29 00:11:13 +0000535 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000536 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000537 kill = true;
538 // A register is not killed if any subregs are live...
Craig Topper9ebfbf82012-03-05 05:37:41 +0000539 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000540 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000541 if (LiveRegs.test(*Subreg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000542 kill = false;
543 break;
544 }
545 }
546
547 // If subreg is not live, then register is killed if it became
548 // live in this instruction
549 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000550 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000551 }
Jim Grosbach90013032010-05-14 21:19:48 +0000552
David Goodwin88a589c2009-08-25 17:03:05 +0000553 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000554 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000555 // Warning: ToggleKillFlag may invalidate MO.
556 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000557 DEBUG(MI->dump());
558 }
Jim Grosbach90013032010-05-14 21:19:48 +0000559
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000560 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000561 }
Jim Grosbach90013032010-05-14 21:19:48 +0000562
David Goodwina3251db2009-08-31 20:47:02 +0000563 // Mark any used register (that is not using undef) and subregs as
564 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000565 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
566 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000567 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000568 unsigned Reg = MO.getReg();
569 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
570
Benjamin Kramer46252d82012-02-23 19:15:40 +0000571 LiveRegs.set(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000572
Craig Topper9ebfbf82012-03-05 05:37:41 +0000573 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000574 *Subreg; ++Subreg)
575 LiveRegs.set(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000576 }
David Goodwin88a589c2009-08-25 17:03:05 +0000577 }
578}
579
Dan Gohman343f0c02008-11-19 23:18:57 +0000580//===----------------------------------------------------------------------===//
581// Top-Down Scheduling
582//===----------------------------------------------------------------------===//
583
584/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
585/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000586void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000587 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000588
Dan Gohman343f0c02008-11-19 23:18:57 +0000589#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000590 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000591 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000592 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000593 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000594 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000595 }
596#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000597 --SuccSU->NumPredsLeft;
598
Andrew Trick89fd4372011-05-06 18:14:32 +0000599 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000600 // here as such:
601 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
602 //
603 // However, we lazily compute node depth instead. Note that
604 // ScheduleNodeTopDown has already updated the depth of this node which causes
605 // all descendents to be marked dirty. Setting the successor depth explicitly
606 // here would cause depth to be recomputed for all its ancestors. If the
607 // successor is not yet ready (because of a transitively redundant edge) then
608 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000609
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000610 // If all the node's predecessors are scheduled, this node is ready
611 // to be scheduled. Ignore the special ExitSU node.
612 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000613 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000614}
615
616/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000617void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000618 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000619 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000620 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000621 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000622}
623
624/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
625/// count of its successors. If a successor pending count is zero, add it to
626/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000627void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000628 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000629 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000630
Dan Gohman343f0c02008-11-19 23:18:57 +0000631 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000632 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000633 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000634 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000635
David Goodwin557bbe62009-11-20 19:32:48 +0000636 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000637 SU->isScheduled = true;
Andrew Trick953be892012-03-07 23:00:49 +0000638 AvailableQueue.scheduledNode(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000639}
640
641/// ListScheduleTopDown - The main loop of list scheduling for top-down
642/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000643void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000644 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000645
David Goodwin4de099d2009-11-03 20:57:50 +0000646 // We're scheduling top-down but we're visiting the regions in
647 // bottom-up order, so we don't know the hazards at the start of a
648 // region. So assume no hazards (this should usually be ok as most
649 // blocks are a single region).
650 HazardRec->Reset();
651
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000652 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000653 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000654
David Goodwin557bbe62009-11-20 19:32:48 +0000655 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000656 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
657 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000658 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000659 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000660 AvailableQueue.push(&SUnits[i]);
661 SUnits[i].isAvailable = true;
662 }
663 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000664
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000665 // In any cycle where we can't schedule any instructions, we must
666 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000667 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000668
Dan Gohman343f0c02008-11-19 23:18:57 +0000669 // While Available queue is not empty, grab the node with the highest
670 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000671 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000672 Sequence.reserve(SUnits.size());
673 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
674 // Check to see if any of the pending instructions are ready to issue. If
675 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000676 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000677 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000678 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000679 AvailableQueue.push(PendingQueue[i]);
680 PendingQueue[i]->isAvailable = true;
681 PendingQueue[i] = PendingQueue.back();
682 PendingQueue.pop_back();
683 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000684 } else if (PendingQueue[i]->getDepth() < MinDepth)
685 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000686 }
David Goodwinc93d8372009-08-11 17:35:23 +0000687
Andrew Trick2da8bc82010-12-24 05:03:26 +0000688 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000689
Dan Gohman2836c282009-01-16 01:33:36 +0000690 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000691 bool HasNoopHazards = false;
692 while (!AvailableQueue.empty()) {
693 SUnit *CurSUnit = AvailableQueue.pop();
694
695 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000696 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000697 if (HT == ScheduleHazardRecognizer::NoHazard) {
698 FoundSUnit = CurSUnit;
699 break;
700 }
701
702 // Remember if this is a noop hazard.
703 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
704
705 NotReady.push_back(CurSUnit);
706 }
707
708 // Add the nodes that aren't ready back onto the available list.
709 if (!NotReady.empty()) {
710 AvailableQueue.push_all(NotReady);
711 NotReady.clear();
712 }
713
David Goodwin4de099d2009-11-03 20:57:50 +0000714 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000715 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000716 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000717 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000718 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000719 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000720 if (HazardRec->atIssueLimit()) {
721 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
722 HazardRec->AdvanceCycle();
723 ++CurCycle;
724 CycleHasInsts = false;
725 }
Dan Gohman2836c282009-01-16 01:33:36 +0000726 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000727 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000728 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000729 HazardRec->AdvanceCycle();
730 } else if (!HasNoopHazards) {
731 // Otherwise, we have a pipeline stall, but no other problem,
732 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000733 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000734 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000735 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000736 } else {
737 // Otherwise, we have no instructions to issue and we have instructions
738 // that will fault if we don't do this right. This is the case for
739 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000740 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000741 HazardRec->EmitNoop();
742 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000743 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000744 }
745
Dan Gohman2836c282009-01-16 01:33:36 +0000746 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000747 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000748 }
749 }
750
751#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000752 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
753 unsigned Noops = 0;
754 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
755 if (!Sequence[i])
756 ++Noops;
757 assert(Sequence.size() - Noops == ScheduledNodes &&
758 "The number of nodes scheduled doesn't match the expected number!");
759#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000760}
Andrew Trick84b454d2012-03-07 05:21:44 +0000761
762// EmitSchedule - Emit the machine code in scheduled order.
763void SchedulePostRATDList::EmitSchedule() {
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000764 Begin = End;
Andrew Trick84b454d2012-03-07 05:21:44 +0000765
766 // If first instruction was a DBG_VALUE then put it back.
767 if (FirstDbgValue)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000768 BB->splice(End, BB, FirstDbgValue);
Andrew Trick84b454d2012-03-07 05:21:44 +0000769
770 // Then re-insert them according to the given schedule.
771 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
772 if (SUnit *SU = Sequence[i])
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000773 BB->splice(End, BB, SU->getInstr());
Andrew Trick84b454d2012-03-07 05:21:44 +0000774 else
775 // Null SUnit* is a noop.
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000776 TII->insertNoop(*BB, End);
Andrew Trick84b454d2012-03-07 05:21:44 +0000777
778 // Update the Begin iterator, as the first instruction in the block
779 // may have been scheduled later.
780 if (i == 0)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000781 Begin = prior(End);
Andrew Trick84b454d2012-03-07 05:21:44 +0000782 }
783
784 // Reinsert any remaining debug_values.
785 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
786 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
787 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
788 MachineInstr *DbgValue = P.first;
789 MachineBasicBlock::iterator OrigPrivMI = P.second;
790 BB->splice(++OrigPrivMI, BB, DbgValue);
791 }
792 DbgValues.clear();
793 FirstDbgValue = NULL;
794}