blob: 20ed8c56594bb3ccc426fd5fbbfc94e58c4e969b [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);
Dan Gohman343f0c02008-11-19 23:18:57 +0000173 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000174}
175
Andrew Trick1dd8c852012-02-08 21:23:13 +0000176char &llvm::PostRASchedulerID = PostRAScheduler::ID;
177
178INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
179 "Post RA top-down list latency scheduler", false, false)
180
Andrew Trick2da8bc82010-12-24 05:03:26 +0000181SchedulePostRATDList::SchedulePostRATDList(
182 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000183 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000184 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000185 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trick5e920d72012-01-14 02:17:12 +0000186 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
Benjamin Kramer46252d82012-02-23 19:15:40 +0000187 LiveRegs(TRI->getNumRegs())
Andrew Trick2da8bc82010-12-24 05:03:26 +0000188{
189 const TargetMachine &TM = MF.getTarget();
190 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
191 HazardRec =
192 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
193 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000194 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000195 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000196 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000197 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000198}
199
200SchedulePostRATDList::~SchedulePostRATDList() {
201 delete HazardRec;
202 delete AntiDepBreak;
203}
204
Dan Gohman343f0c02008-11-19 23:18:57 +0000205bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000206 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000207 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
208 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
209 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000210 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
211
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000212 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000213
David Goodwin471850a2009-10-01 21:46:35 +0000214 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000215 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
216 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000217 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000218 if (EnablePostRAScheduler.getPosition() > 0) {
219 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000220 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000221 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000222 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000223 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000224 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000225 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
226 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000227 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000228 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000229
David Goodwin4c3715c2009-10-22 23:19:17 +0000230 // Check for antidep breaking override...
231 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000232 AntiDepMode = (EnableAntiDepBreaking == "all")
233 ? TargetSubtargetInfo::ANTIDEP_ALL
234 : ((EnableAntiDepBreaking == "critical")
235 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
236 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000237 }
238
David Greenee1b21292010-01-05 01:26:01 +0000239 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000240
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000241 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000242 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000243
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000244 // Loop over all of the basic blocks
245 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000246 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000247#ifndef NDEBUG
248 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
249 if (DebugDiv > 0) {
250 static int bbcnt = 0;
251 if (bbcnt++ % DebugDiv != DebugMod)
252 continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000253 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
254 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000255 }
256#endif
257
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000258 // Initialize register live-range state for scheduling in this block.
259 Scheduler.StartBlock(MBB);
260
Dan Gohmanf7119392009-01-16 22:10:20 +0000261 // Schedule each sequence of instructions not interrupted by a label
262 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000263 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000264 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000265 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000266 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000267 // Calls are not scheduling boundaries before register allocation, but
268 // post-ra we don't gain anything by scheduling across calls since we
269 // don't need to worry about register pressure.
270 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000271 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000272 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000273 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000274 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000275 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000276 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000277 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000278 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000279 if (MI->isBundle())
280 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000281 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000282 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000283 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000284 "Instruction count mismatch!");
285 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000286 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000287
288 // Clean up register live-range state.
289 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000290
David Goodwin5e411782009-09-03 22:15:25 +0000291 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000292 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000293 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000294
295 return true;
296}
Jim Grosbach90013032010-05-14 21:19:48 +0000297
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000298/// StartBlock - Initialize register live-range state for scheduling in
299/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000300///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000301void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
302 // Call the superclass.
303 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000304
David Goodwin2e7be612009-10-26 16:59:04 +0000305 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000306 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000307 if (AntiDepBreak != NULL)
308 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000309}
310
311/// Schedule - Schedule the instruction range using list scheduling.
312///
313void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000314 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000315 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000316
David Goodwin2e7be612009-10-26 16:59:04 +0000317 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000318 unsigned Broken =
David Goodwin557bbe62009-11-20 19:32:48 +0000319 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
Devang Patele29e8e12011-06-02 21:26:52 +0000320 InsertPosIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000321
David Goodwin557bbe62009-11-20 19:32:48 +0000322 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323 // We made changes. Update the dependency graph.
324 // Theoretically we could update the graph in place:
325 // When a live range is changed to use a different register, remove
326 // the def's anti-dependence *and* output-dependence edges due to
327 // that register, and add new anti-dependence and output-dependence
328 // edges based on the next live range of the register.
David Goodwin557bbe62009-11-20 19:32:48 +0000329 SUnits.clear();
330 Sequence.clear();
331 EntrySU = SUnit();
332 ExitSU = SUnit();
333 BuildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000334
David Goodwin2e7be612009-10-26 16:59:04 +0000335 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000336 }
337 }
338
David Greenee1b21292010-01-05 01:26:01 +0000339 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000340 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
341 SUnits[su].dumpAll(this));
342
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000343 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000344 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000345 AvailableQueue.releaseState();
346}
347
348/// Observe - Update liveness information to account for the current
349/// instruction, which will not be scheduled.
350///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000351void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000352 if (AntiDepBreak != NULL)
353 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000354}
355
356/// FinishBlock - Clean up register live-range state.
357///
358void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000359 if (AntiDepBreak != NULL)
360 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000361
362 // Call the superclass.
363 ScheduleDAGInstrs::FinishBlock();
364}
365
David Goodwin5e411782009-09-03 22:15:25 +0000366/// StartBlockForKills - Initialize register live-range state for updating kills
367///
368void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000369 // Start with no live registers.
370 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000371
372 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000373 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000374 // In a return block, examine the function live-out regs.
375 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
376 E = MRI.liveout_end(); I != E; ++I) {
377 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000378 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000379 // Repeat, for all subregs.
380 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000381 *Subreg; ++Subreg)
382 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000383 }
384 }
385 else {
386 // In a non-return block, examine the live-in regs of all successors.
387 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
388 SE = BB->succ_end(); SI != SE; ++SI) {
389 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
390 E = (*SI)->livein_end(); I != E; ++I) {
391 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000392 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000393 // Repeat, for all subregs.
394 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000395 *Subreg; ++Subreg)
396 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000397 }
398 }
399 }
400}
401
David Goodwin8f909342009-09-23 16:35:25 +0000402bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
403 MachineOperand &MO) {
404 // Setting kill flag...
405 if (!MO.isKill()) {
406 MO.setIsKill(true);
407 return false;
408 }
Jim Grosbach90013032010-05-14 21:19:48 +0000409
David Goodwin8f909342009-09-23 16:35:25 +0000410 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000411 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000412 MO.setIsKill(false);
413 return false;
414 }
415
416 // If any subreg of MO is live, then create an imp-def for that
417 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000418 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000419 bool AllDead = true;
420 const unsigned SuperReg = MO.getReg();
421 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
422 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000423 if (LiveRegs.test(*Subreg)) {
David Goodwin8f909342009-09-23 16:35:25 +0000424 MI->addOperand(MachineOperand::CreateReg(*Subreg,
425 true /*IsDef*/,
426 true /*IsImp*/,
427 false /*IsKill*/,
428 false /*IsDead*/));
429 AllDead = false;
430 }
431 }
432
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000433 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000434 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000435 return false;
436}
437
David Goodwin88a589c2009-08-25 17:03:05 +0000438/// FixupKills - Fix the register kill flags, they may have been made
439/// incorrect by instruction reordering.
440///
441void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000442 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000443
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000444 BitVector killedRegs(TRI->getNumRegs());
David Goodwin88a589c2009-08-25 17:03:05 +0000445 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000446
447 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000448
David Goodwin7886cd82009-08-29 00:11:13 +0000449 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000450 unsigned Count = MBB->size();
451 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
452 I != E; --Count) {
453 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000454 if (MI->isDebugValue())
455 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000456
David Goodwin7886cd82009-08-29 00:11:13 +0000457 // Update liveness. Registers that are defed but not used in this
458 // instruction are now dead. Mark register and all subregs as they
459 // are completely defined.
460 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
461 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000462 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000463 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000464 if (!MO.isReg()) continue;
465 unsigned Reg = MO.getReg();
466 if (Reg == 0) continue;
467 if (!MO.isDef()) continue;
468 // Ignore two-addr defs.
469 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000470
Benjamin Kramer46252d82012-02-23 19:15:40 +0000471 LiveRegs.reset(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000472
David Goodwin7886cd82009-08-29 00:11:13 +0000473 // Repeat for all subregs.
474 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000475 *Subreg; ++Subreg)
476 LiveRegs.reset(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000477 }
David Goodwin88a589c2009-08-25 17:03:05 +0000478
David Goodwin8f909342009-09-23 16:35:25 +0000479 // Examine all used registers and set/clear kill flag. When a
480 // register is used multiple times we only set the kill flag on
481 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000482 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000483 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
484 MachineOperand &MO = MI->getOperand(i);
485 if (!MO.isReg() || !MO.isUse()) continue;
486 unsigned Reg = MO.getReg();
487 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
488
David Goodwin7886cd82009-08-29 00:11:13 +0000489 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000490 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000491 kill = true;
492 // A register is not killed if any subregs are live...
493 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
494 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000495 if (LiveRegs.test(*Subreg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000496 kill = false;
497 break;
498 }
499 }
500
501 // If subreg is not live, then register is killed if it became
502 // live in this instruction
503 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000504 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000505 }
Jim Grosbach90013032010-05-14 21:19:48 +0000506
David Goodwin88a589c2009-08-25 17:03:05 +0000507 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000508 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000509 // Warning: ToggleKillFlag may invalidate MO.
510 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000511 DEBUG(MI->dump());
512 }
Jim Grosbach90013032010-05-14 21:19:48 +0000513
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000514 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000515 }
Jim Grosbach90013032010-05-14 21:19:48 +0000516
David Goodwina3251db2009-08-31 20:47:02 +0000517 // Mark any used register (that is not using undef) and subregs as
518 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000519 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
520 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000521 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000522 unsigned Reg = MO.getReg();
523 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
524
Benjamin Kramer46252d82012-02-23 19:15:40 +0000525 LiveRegs.set(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000526
David Goodwin7886cd82009-08-29 00:11:13 +0000527 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000528 *Subreg; ++Subreg)
529 LiveRegs.set(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000530 }
David Goodwin88a589c2009-08-25 17:03:05 +0000531 }
532}
533
Dan Gohman343f0c02008-11-19 23:18:57 +0000534//===----------------------------------------------------------------------===//
535// Top-Down Scheduling
536//===----------------------------------------------------------------------===//
537
538/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
539/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000540void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000541 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000542
Dan Gohman343f0c02008-11-19 23:18:57 +0000543#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000544 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000545 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000546 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000547 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000548 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000549 }
550#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000551 --SuccSU->NumPredsLeft;
552
Andrew Trick89fd4372011-05-06 18:14:32 +0000553 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000554 // here as such:
555 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
556 //
557 // However, we lazily compute node depth instead. Note that
558 // ScheduleNodeTopDown has already updated the depth of this node which causes
559 // all descendents to be marked dirty. Setting the successor depth explicitly
560 // here would cause depth to be recomputed for all its ancestors. If the
561 // successor is not yet ready (because of a transitively redundant edge) then
562 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000563
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000564 // If all the node's predecessors are scheduled, this node is ready
565 // to be scheduled. Ignore the special ExitSU node.
566 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000567 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000568}
569
570/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000571void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000572 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000573 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000574 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000575 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000576}
577
578/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
579/// count of its successors. If a successor pending count is zero, add it to
580/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000581void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000582 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000583 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000584
Dan Gohman343f0c02008-11-19 23:18:57 +0000585 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000586 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000587 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000588 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000589
David Goodwin557bbe62009-11-20 19:32:48 +0000590 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000591 SU->isScheduled = true;
592 AvailableQueue.ScheduledNode(SU);
593}
594
595/// ListScheduleTopDown - The main loop of list scheduling for top-down
596/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000597void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000598 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000599
David Goodwin4de099d2009-11-03 20:57:50 +0000600 // We're scheduling top-down but we're visiting the regions in
601 // bottom-up order, so we don't know the hazards at the start of a
602 // region. So assume no hazards (this should usually be ok as most
603 // blocks are a single region).
604 HazardRec->Reset();
605
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000606 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000607 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000608
David Goodwin557bbe62009-11-20 19:32:48 +0000609 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000610 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
611 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000612 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000613 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000614 AvailableQueue.push(&SUnits[i]);
615 SUnits[i].isAvailable = true;
616 }
617 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000618
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000619 // In any cycle where we can't schedule any instructions, we must
620 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000621 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000622
Dan Gohman343f0c02008-11-19 23:18:57 +0000623 // While Available queue is not empty, grab the node with the highest
624 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000625 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000626 Sequence.reserve(SUnits.size());
627 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
628 // Check to see if any of the pending instructions are ready to issue. If
629 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000630 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000631 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000632 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000633 AvailableQueue.push(PendingQueue[i]);
634 PendingQueue[i]->isAvailable = true;
635 PendingQueue[i] = PendingQueue.back();
636 PendingQueue.pop_back();
637 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000638 } else if (PendingQueue[i]->getDepth() < MinDepth)
639 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000640 }
David Goodwinc93d8372009-08-11 17:35:23 +0000641
Andrew Trick2da8bc82010-12-24 05:03:26 +0000642 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000643
Dan Gohman2836c282009-01-16 01:33:36 +0000644 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000645 bool HasNoopHazards = false;
646 while (!AvailableQueue.empty()) {
647 SUnit *CurSUnit = AvailableQueue.pop();
648
649 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000650 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000651 if (HT == ScheduleHazardRecognizer::NoHazard) {
652 FoundSUnit = CurSUnit;
653 break;
654 }
655
656 // Remember if this is a noop hazard.
657 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
658
659 NotReady.push_back(CurSUnit);
660 }
661
662 // Add the nodes that aren't ready back onto the available list.
663 if (!NotReady.empty()) {
664 AvailableQueue.push_all(NotReady);
665 NotReady.clear();
666 }
667
David Goodwin4de099d2009-11-03 20:57:50 +0000668 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000669 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000670 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000671 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000672 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000673 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000674 if (HazardRec->atIssueLimit()) {
675 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
676 HazardRec->AdvanceCycle();
677 ++CurCycle;
678 CycleHasInsts = false;
679 }
Dan Gohman2836c282009-01-16 01:33:36 +0000680 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000681 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000682 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000683 HazardRec->AdvanceCycle();
684 } else if (!HasNoopHazards) {
685 // Otherwise, we have a pipeline stall, but no other problem,
686 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000687 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000688 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000689 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000690 } else {
691 // Otherwise, we have no instructions to issue and we have instructions
692 // that will fault if we don't do this right. This is the case for
693 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000694 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000695 HazardRec->EmitNoop();
696 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000697 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000698 }
699
Dan Gohman2836c282009-01-16 01:33:36 +0000700 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000701 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000702 }
703 }
704
705#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000706 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000707#endif
708}