blob: 72ae6febb12bcc9e115c6744fb49a83f661fa065 [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
Dan Gohman21d90032008-11-25 00:52:40 +0000130 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000131 SchedulePostRATDList(
132 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000133 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000134 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000135 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000136
Andrew Trick2da8bc82010-12-24 05:03:26 +0000137 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000138
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000139 /// StartBlock - Initialize register live-range state for scheduling in
140 /// this block.
141 ///
142 void StartBlock(MachineBasicBlock *BB);
143
144 /// Schedule - Schedule the instruction range using list scheduling.
145 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000146 void Schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000147
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000148 /// Observe - Update liveness information to account for the current
149 /// instruction, which will not be scheduled.
150 ///
151 void Observe(MachineInstr *MI, unsigned Count);
152
153 /// FinishBlock - Clean up register live-range state.
154 ///
155 void FinishBlock();
156
David Goodwin2e7be612009-10-26 16:59:04 +0000157 /// FixupKills - Fix register kill flags that have been made
158 /// invalid due to scheduling
159 ///
160 void FixupKills(MachineBasicBlock *MBB);
161
Dan Gohman343f0c02008-11-19 23:18:57 +0000162 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000163 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
164 void ReleaseSuccessors(SUnit *SU);
165 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
166 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000167 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000168
David Goodwin8f909342009-09-23 16:35:25 +0000169 // ToggleKillFlag - Toggle a register operand kill flag. Other
170 // adjustments may be made to the instruction if necessary. Return
171 // true if the operand has been deleted, false if not.
172 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Andrew Trick73ba69b2012-03-07 05:21:40 +0000173
174 void dumpSchedule() const;
Dan Gohman343f0c02008-11-19 23:18:57 +0000175 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000176}
177
Andrew Trick1dd8c852012-02-08 21:23:13 +0000178char &llvm::PostRASchedulerID = PostRAScheduler::ID;
179
180INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
181 "Post RA top-down list latency scheduler", false, false)
182
Andrew Trick2da8bc82010-12-24 05:03:26 +0000183SchedulePostRATDList::SchedulePostRATDList(
184 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000185 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000186 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000187 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trick5e920d72012-01-14 02:17:12 +0000188 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
Benjamin Kramer46252d82012-02-23 19:15:40 +0000189 LiveRegs(TRI->getNumRegs())
Andrew Trick2da8bc82010-12-24 05:03:26 +0000190{
191 const TargetMachine &TM = MF.getTarget();
192 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
193 HazardRec =
194 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
195 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000196 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000197 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000198 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000199 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000200}
201
202SchedulePostRATDList::~SchedulePostRATDList() {
203 delete HazardRec;
204 delete AntiDepBreak;
205}
206
Andrew Trick73ba69b2012-03-07 05:21:40 +0000207/// dumpSchedule - dump the scheduled Sequence.
208void SchedulePostRATDList::dumpSchedule() const {
209 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
210 if (SUnit *SU = Sequence[i])
211 SU->dump(this);
212 else
213 dbgs() << "**** NOOP ****\n";
214 }
215}
216
Dan Gohman343f0c02008-11-19 23:18:57 +0000217bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000218 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000219 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
220 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
221 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000222 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
223
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000224 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000225
David Goodwin471850a2009-10-01 21:46:35 +0000226 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000227 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
228 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000229 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000230 if (EnablePostRAScheduler.getPosition() > 0) {
231 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000232 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000233 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000234 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000235 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000236 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000237 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
238 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000239 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000240 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000241
David Goodwin4c3715c2009-10-22 23:19:17 +0000242 // Check for antidep breaking override...
243 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000244 AntiDepMode = (EnableAntiDepBreaking == "all")
245 ? TargetSubtargetInfo::ANTIDEP_ALL
246 : ((EnableAntiDepBreaking == "critical")
247 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
248 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000249 }
250
David Greenee1b21292010-01-05 01:26:01 +0000251 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000252
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000253 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000254 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000255
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000256 // Loop over all of the basic blocks
257 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000258 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000259#ifndef NDEBUG
260 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
261 if (DebugDiv > 0) {
262 static int bbcnt = 0;
263 if (bbcnt++ % DebugDiv != DebugMod)
264 continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000265 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
266 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000267 }
268#endif
269
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000270 // Initialize register live-range state for scheduling in this block.
271 Scheduler.StartBlock(MBB);
272
Dan Gohmanf7119392009-01-16 22:10:20 +0000273 // Schedule each sequence of instructions not interrupted by a label
274 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000275 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000276 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000277 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000278 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000279 // Calls are not scheduling boundaries before register allocation, but
280 // post-ra we don't gain anything by scheduling across calls since we
281 // don't need to worry about register pressure.
282 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000283 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000284 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000285 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000286 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000287 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000288 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000289 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000290 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000291 if (MI->isBundle())
292 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000293 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000294 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000295 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000296 "Instruction count mismatch!");
297 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000298 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000299
300 // Clean up register live-range state.
301 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000302
David Goodwin5e411782009-09-03 22:15:25 +0000303 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000304 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000305 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000306
307 return true;
308}
Jim Grosbach90013032010-05-14 21:19:48 +0000309
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000310/// StartBlock - Initialize register live-range state for scheduling in
311/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000312///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000313void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
314 // Call the superclass.
315 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000316
David Goodwin2e7be612009-10-26 16:59:04 +0000317 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000318 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000319 if (AntiDepBreak != NULL)
320 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000321}
322
323/// Schedule - Schedule the instruction range using list scheduling.
324///
325void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000326 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000327 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000328
David Goodwin2e7be612009-10-26 16:59:04 +0000329 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000330 unsigned Broken =
David Goodwin557bbe62009-11-20 19:32:48 +0000331 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
Devang Patele29e8e12011-06-02 21:26:52 +0000332 InsertPosIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000333
David Goodwin557bbe62009-11-20 19:32:48 +0000334 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000335 // We made changes. Update the dependency graph.
336 // Theoretically we could update the graph in place:
337 // When a live range is changed to use a different register, remove
338 // the def's anti-dependence *and* output-dependence edges due to
339 // that register, and add new anti-dependence and output-dependence
340 // edges based on the next live range of the register.
David Goodwin557bbe62009-11-20 19:32:48 +0000341 SUnits.clear();
342 Sequence.clear();
343 EntrySU = SUnit();
344 ExitSU = SUnit();
345 BuildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000346
David Goodwin2e7be612009-10-26 16:59:04 +0000347 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000348 }
349 }
350
David Greenee1b21292010-01-05 01:26:01 +0000351 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000352 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
353 SUnits[su].dumpAll(this));
354
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000355 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000356 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000357 AvailableQueue.releaseState();
Andrew Trick73ba69b2012-03-07 05:21:40 +0000358
359 DEBUG({
360 dbgs() << "*** Final schedule ***\n";
361 dumpSchedule();
362 dbgs() << '\n';
363 });
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000364}
365
366/// Observe - Update liveness information to account for the current
367/// instruction, which will not be scheduled.
368///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000369void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000370 if (AntiDepBreak != NULL)
371 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000372}
373
374/// FinishBlock - Clean up register live-range state.
375///
376void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000377 if (AntiDepBreak != NULL)
378 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000379
380 // Call the superclass.
381 ScheduleDAGInstrs::FinishBlock();
382}
383
David Goodwin5e411782009-09-03 22:15:25 +0000384/// StartBlockForKills - Initialize register live-range state for updating kills
385///
386void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000387 // Start with no live registers.
388 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000389
390 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000391 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000392 // In a return block, examine the function live-out regs.
393 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
394 E = MRI.liveout_end(); I != E; ++I) {
395 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000396 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000397 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000398 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000399 *Subreg; ++Subreg)
400 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000401 }
402 }
403 else {
404 // In a non-return block, examine the live-in regs of all successors.
405 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
406 SE = BB->succ_end(); SI != SE; ++SI) {
407 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
408 E = (*SI)->livein_end(); I != E; ++I) {
409 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000410 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000411 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000412 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000413 *Subreg; ++Subreg)
414 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000415 }
416 }
417 }
418}
419
David Goodwin8f909342009-09-23 16:35:25 +0000420bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
421 MachineOperand &MO) {
422 // Setting kill flag...
423 if (!MO.isKill()) {
424 MO.setIsKill(true);
425 return false;
426 }
Jim Grosbach90013032010-05-14 21:19:48 +0000427
David Goodwin8f909342009-09-23 16:35:25 +0000428 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000429 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000430 MO.setIsKill(false);
431 return false;
432 }
433
434 // If any subreg of MO is live, then create an imp-def for that
435 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000436 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000437 bool AllDead = true;
438 const unsigned SuperReg = MO.getReg();
Craig Topper9ebfbf82012-03-05 05:37:41 +0000439 for (const uint16_t *Subreg = TRI->getSubRegisters(SuperReg);
David Goodwin8f909342009-09-23 16:35:25 +0000440 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000441 if (LiveRegs.test(*Subreg)) {
David Goodwin8f909342009-09-23 16:35:25 +0000442 MI->addOperand(MachineOperand::CreateReg(*Subreg,
443 true /*IsDef*/,
444 true /*IsImp*/,
445 false /*IsKill*/,
446 false /*IsDead*/));
447 AllDead = false;
448 }
449 }
450
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000451 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000452 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000453 return false;
454}
455
David Goodwin88a589c2009-08-25 17:03:05 +0000456/// FixupKills - Fix the register kill flags, they may have been made
457/// incorrect by instruction reordering.
458///
459void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000460 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000461
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000462 BitVector killedRegs(TRI->getNumRegs());
David Goodwin88a589c2009-08-25 17:03:05 +0000463 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000464
465 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000466
David Goodwin7886cd82009-08-29 00:11:13 +0000467 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000468 unsigned Count = MBB->size();
469 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
470 I != E; --Count) {
471 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000472 if (MI->isDebugValue())
473 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000474
David Goodwin7886cd82009-08-29 00:11:13 +0000475 // Update liveness. Registers that are defed but not used in this
476 // instruction are now dead. Mark register and all subregs as they
477 // are completely defined.
478 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
479 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000480 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000481 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000482 if (!MO.isReg()) continue;
483 unsigned Reg = MO.getReg();
484 if (Reg == 0) continue;
485 if (!MO.isDef()) continue;
486 // Ignore two-addr defs.
487 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000488
Benjamin Kramer46252d82012-02-23 19:15:40 +0000489 LiveRegs.reset(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000490
David Goodwin7886cd82009-08-29 00:11:13 +0000491 // Repeat for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000492 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000493 *Subreg; ++Subreg)
494 LiveRegs.reset(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000495 }
David Goodwin88a589c2009-08-25 17:03:05 +0000496
David Goodwin8f909342009-09-23 16:35:25 +0000497 // Examine all used registers and set/clear kill flag. When a
498 // register is used multiple times we only set the kill flag on
499 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000500 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000501 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
502 MachineOperand &MO = MI->getOperand(i);
503 if (!MO.isReg() || !MO.isUse()) continue;
504 unsigned Reg = MO.getReg();
505 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
506
David Goodwin7886cd82009-08-29 00:11:13 +0000507 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000508 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000509 kill = true;
510 // A register is not killed if any subregs are live...
Craig Topper9ebfbf82012-03-05 05:37:41 +0000511 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000512 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000513 if (LiveRegs.test(*Subreg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000514 kill = false;
515 break;
516 }
517 }
518
519 // If subreg is not live, then register is killed if it became
520 // live in this instruction
521 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000522 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000523 }
Jim Grosbach90013032010-05-14 21:19:48 +0000524
David Goodwin88a589c2009-08-25 17:03:05 +0000525 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000526 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000527 // Warning: ToggleKillFlag may invalidate MO.
528 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000529 DEBUG(MI->dump());
530 }
Jim Grosbach90013032010-05-14 21:19:48 +0000531
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000532 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000533 }
Jim Grosbach90013032010-05-14 21:19:48 +0000534
David Goodwina3251db2009-08-31 20:47:02 +0000535 // Mark any used register (that is not using undef) and subregs as
536 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000537 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
538 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000539 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000540 unsigned Reg = MO.getReg();
541 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
542
Benjamin Kramer46252d82012-02-23 19:15:40 +0000543 LiveRegs.set(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000544
Craig Topper9ebfbf82012-03-05 05:37:41 +0000545 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000546 *Subreg; ++Subreg)
547 LiveRegs.set(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000548 }
David Goodwin88a589c2009-08-25 17:03:05 +0000549 }
550}
551
Dan Gohman343f0c02008-11-19 23:18:57 +0000552//===----------------------------------------------------------------------===//
553// Top-Down Scheduling
554//===----------------------------------------------------------------------===//
555
556/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
557/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000558void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000559 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000560
Dan Gohman343f0c02008-11-19 23:18:57 +0000561#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000562 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000563 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000564 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000565 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000566 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000567 }
568#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000569 --SuccSU->NumPredsLeft;
570
Andrew Trick89fd4372011-05-06 18:14:32 +0000571 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000572 // here as such:
573 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
574 //
575 // However, we lazily compute node depth instead. Note that
576 // ScheduleNodeTopDown has already updated the depth of this node which causes
577 // all descendents to be marked dirty. Setting the successor depth explicitly
578 // here would cause depth to be recomputed for all its ancestors. If the
579 // successor is not yet ready (because of a transitively redundant edge) then
580 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000581
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000582 // If all the node's predecessors are scheduled, this node is ready
583 // to be scheduled. Ignore the special ExitSU node.
584 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000585 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000586}
587
588/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000589void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000590 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000591 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000592 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000593 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000594}
595
596/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
597/// count of its successors. If a successor pending count is zero, add it to
598/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000599void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000600 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000601 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000602
Dan Gohman343f0c02008-11-19 23:18:57 +0000603 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000604 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000605 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000606 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000607
David Goodwin557bbe62009-11-20 19:32:48 +0000608 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000609 SU->isScheduled = true;
610 AvailableQueue.ScheduledNode(SU);
611}
612
613/// ListScheduleTopDown - The main loop of list scheduling for top-down
614/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000615void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000616 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000617
David Goodwin4de099d2009-11-03 20:57:50 +0000618 // We're scheduling top-down but we're visiting the regions in
619 // bottom-up order, so we don't know the hazards at the start of a
620 // region. So assume no hazards (this should usually be ok as most
621 // blocks are a single region).
622 HazardRec->Reset();
623
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000624 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000625 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000626
David Goodwin557bbe62009-11-20 19:32:48 +0000627 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000628 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
629 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000630 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000631 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000632 AvailableQueue.push(&SUnits[i]);
633 SUnits[i].isAvailable = true;
634 }
635 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000636
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000637 // In any cycle where we can't schedule any instructions, we must
638 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000639 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000640
Dan Gohman343f0c02008-11-19 23:18:57 +0000641 // While Available queue is not empty, grab the node with the highest
642 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000643 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000644 Sequence.reserve(SUnits.size());
645 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
646 // Check to see if any of the pending instructions are ready to issue. If
647 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000648 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000649 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000650 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000651 AvailableQueue.push(PendingQueue[i]);
652 PendingQueue[i]->isAvailable = true;
653 PendingQueue[i] = PendingQueue.back();
654 PendingQueue.pop_back();
655 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000656 } else if (PendingQueue[i]->getDepth() < MinDepth)
657 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000658 }
David Goodwinc93d8372009-08-11 17:35:23 +0000659
Andrew Trick2da8bc82010-12-24 05:03:26 +0000660 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000661
Dan Gohman2836c282009-01-16 01:33:36 +0000662 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000663 bool HasNoopHazards = false;
664 while (!AvailableQueue.empty()) {
665 SUnit *CurSUnit = AvailableQueue.pop();
666
667 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000668 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000669 if (HT == ScheduleHazardRecognizer::NoHazard) {
670 FoundSUnit = CurSUnit;
671 break;
672 }
673
674 // Remember if this is a noop hazard.
675 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
676
677 NotReady.push_back(CurSUnit);
678 }
679
680 // Add the nodes that aren't ready back onto the available list.
681 if (!NotReady.empty()) {
682 AvailableQueue.push_all(NotReady);
683 NotReady.clear();
684 }
685
David Goodwin4de099d2009-11-03 20:57:50 +0000686 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000687 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000688 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000689 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000690 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000691 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000692 if (HazardRec->atIssueLimit()) {
693 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
694 HazardRec->AdvanceCycle();
695 ++CurCycle;
696 CycleHasInsts = false;
697 }
Dan Gohman2836c282009-01-16 01:33:36 +0000698 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000699 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000700 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000701 HazardRec->AdvanceCycle();
702 } else if (!HasNoopHazards) {
703 // Otherwise, we have a pipeline stall, but no other problem,
704 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000705 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000706 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000707 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000708 } else {
709 // Otherwise, we have no instructions to issue and we have instructions
710 // that will fault if we don't do this right. This is the case for
711 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000712 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000713 HazardRec->EmitNoop();
714 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000715 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000716 }
717
Dan Gohman2836c282009-01-16 01:33:36 +0000718 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000719 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000720 }
721 }
722
723#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000724 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
725 unsigned Noops = 0;
726 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
727 if (!Sequence[i])
728 ++Noops;
729 assert(Sequence.size() - Noops == ScheduledNodes &&
730 "The number of nodes scheduled doesn't match the expected number!");
731#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000732}