blob: e437be1b69aad18c309050f8e48ddb26b8d7032d [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"
David Goodwin88a589c2009-08-25 17:03:05 +000048#include <set>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000049using namespace llvm;
50
Dan Gohman2836c282009-01-16 01:33:36 +000051STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000052STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin2e7be612009-10-26 16:59:04 +000053STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman343f0c02008-11-19 23:18:57 +000054
David Goodwin471850a2009-10-01 21:46:35 +000055// Post-RA scheduling is enabled with
Evan Cheng5b1b44892011-07-01 21:01:15 +000056// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin471850a2009-10-01 21:46:35 +000057// override the target.
58static cl::opt<bool>
59EnablePostRAScheduler("post-RA-scheduler",
60 cl::desc("Enable scheduling after register allocation"),
David Goodwin9843a932009-10-01 22:19:57 +000061 cl::init(false), cl::Hidden);
David Goodwin2e7be612009-10-26 16:59:04 +000062static cl::opt<std::string>
Dan Gohman21d90032008-11-25 00:52:40 +000063EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin2e7be612009-10-26 16:59:04 +000064 cl::desc("Break post-RA scheduling anti-dependencies: "
65 "\"critical\", \"all\", or \"none\""),
66 cl::init("none"), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000067
David Goodwin1f152282009-09-01 18:34:03 +000068// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
69static cl::opt<int>
70DebugDiv("postra-sched-debugdiv",
71 cl::desc("Debug control MBBs that are scheduled"),
72 cl::init(0), cl::Hidden);
73static cl::opt<int>
74DebugMod("postra-sched-debugmod",
75 cl::desc("Debug control MBBs that are scheduled"),
76 cl::init(0), cl::Hidden);
77
David Goodwinada0ef82009-10-26 19:41:00 +000078AntiDepBreaker::~AntiDepBreaker() { }
79
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000080namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000081 class PostRAScheduler : public MachineFunctionPass {
Dan Gohmana70dca12009-10-09 23:27:56 +000082 AliasAnalysis *AA;
Evan Cheng86050dc2010-06-18 23:09:54 +000083 const TargetInstrInfo *TII;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000084 RegisterClassInfo RegClassInfo;
Dan Gohmana70dca12009-10-09 23:27:56 +000085
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000086 public:
87 static char ID;
Andrew Trickc7d081b2012-02-08 21:22:53 +000088 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000089
Dan Gohman3f237442008-12-16 03:25:46 +000090 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000091 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000092 AU.addRequired<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +000093 AU.addRequired<TargetPassConfig>();
Dan Gohman3f237442008-12-16 03:25:46 +000094 AU.addRequired<MachineDominatorTree>();
95 AU.addPreserved<MachineDominatorTree>();
96 AU.addRequired<MachineLoopInfo>();
97 AU.addPreserved<MachineLoopInfo>();
98 MachineFunctionPass::getAnalysisUsage(AU);
99 }
100
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000101 bool runOnMachineFunction(MachineFunction &Fn);
102 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000103 char PostRAScheduler::ID = 0;
104
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000105 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000106 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000107 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000108 LatencyPriorityQueue AvailableQueue;
Jim Grosbach90013032010-05-14 21:19:48 +0000109
Dan Gohman343f0c02008-11-19 23:18:57 +0000110 /// PendingQueue - This contains all of the instructions whose operands have
111 /// been issued, but their results are not ready yet (due to the latency of
112 /// the operation). Once the operands becomes available, the instruction is
113 /// added to the AvailableQueue.
114 std::vector<SUnit*> PendingQueue;
115
Dan Gohman21d90032008-11-25 00:52:40 +0000116 /// Topo - A topological ordering for SUnits.
117 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000118
Dan Gohman2836c282009-01-16 01:33:36 +0000119 /// HazardRec - The hazard recognizer to use.
120 ScheduleHazardRecognizer *HazardRec;
121
David Goodwin2e7be612009-10-26 16:59:04 +0000122 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
123 AntiDepBreaker *AntiDepBreak;
124
Dan Gohmana70dca12009-10-09 23:27:56 +0000125 /// AA - AliasAnalysis for making memory reference queries.
126 AliasAnalysis *AA;
127
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000128 /// KillIndices - The index of the most recent kill (proceding bottom-up),
129 /// or ~0u if the register is not live.
Bill Wendling24173da2010-07-15 20:01:02 +0000130 std::vector<unsigned> KillIndices;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000131
Dan Gohman21d90032008-11-25 00:52:40 +0000132 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000133 SchedulePostRATDList(
134 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000135 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000136 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000137 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000138
Andrew Trick2da8bc82010-12-24 05:03:26 +0000139 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000140
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000141 /// StartBlock - Initialize register live-range state for scheduling in
142 /// this block.
143 ///
144 void StartBlock(MachineBasicBlock *BB);
145
146 /// Schedule - Schedule the instruction range using list scheduling.
147 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000148 void Schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000149
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000150 /// Observe - Update liveness information to account for the current
151 /// instruction, which will not be scheduled.
152 ///
153 void Observe(MachineInstr *MI, unsigned Count);
154
155 /// FinishBlock - Clean up register live-range state.
156 ///
157 void FinishBlock();
158
David Goodwin2e7be612009-10-26 16:59:04 +0000159 /// FixupKills - Fix register kill flags that have been made
160 /// invalid due to scheduling
161 ///
162 void FixupKills(MachineBasicBlock *MBB);
163
Dan Gohman343f0c02008-11-19 23:18:57 +0000164 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000165 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
166 void ReleaseSuccessors(SUnit *SU);
167 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
168 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000169 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000170
David Goodwin8f909342009-09-23 16:35:25 +0000171 // ToggleKillFlag - Toggle a register operand kill flag. Other
172 // adjustments may be made to the instruction if necessary. Return
173 // true if the operand has been deleted, false if not.
174 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
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),
Andrew Trick2da8bc82010-12-24 05:03:26 +0000189 KillIndices(TRI->getNumRegs())
190{
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
Dan Gohman343f0c02008-11-19 23:18:57 +0000207bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000208 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000209 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
210 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
211 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000212 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
213
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000214 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000215
David Goodwin471850a2009-10-01 21:46:35 +0000216 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000217 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
218 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000219 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000220 if (EnablePostRAScheduler.getPosition() > 0) {
221 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000222 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000223 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000224 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000225 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000226 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000227 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
228 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000229 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000230 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000231
David Goodwin4c3715c2009-10-22 23:19:17 +0000232 // Check for antidep breaking override...
233 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000234 AntiDepMode = (EnableAntiDepBreaking == "all")
235 ? TargetSubtargetInfo::ANTIDEP_ALL
236 : ((EnableAntiDepBreaking == "critical")
237 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
238 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000239 }
240
David Greenee1b21292010-01-05 01:26:01 +0000241 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000242
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000243 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000244 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000245
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000246 // Loop over all of the basic blocks
247 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000248 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000249#ifndef NDEBUG
250 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
251 if (DebugDiv > 0) {
252 static int bbcnt = 0;
253 if (bbcnt++ % DebugDiv != DebugMod)
254 continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000255 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
256 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000257 }
258#endif
259
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000260 // Initialize register live-range state for scheduling in this block.
261 Scheduler.StartBlock(MBB);
262
Dan Gohmanf7119392009-01-16 22:10:20 +0000263 // Schedule each sequence of instructions not interrupted by a label
264 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000265 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000266 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000267 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000268 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000269 // Calls are not scheduling boundaries before register allocation, but
270 // post-ra we don't gain anything by scheduling across calls since we
271 // don't need to worry about register pressure.
272 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000273 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000274 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000275 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000276 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000277 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000278 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000279 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000280 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000281 if (MI->isBundle())
282 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000283 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000284 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000285 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000286 "Instruction count mismatch!");
287 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000288 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000289
290 // Clean up register live-range state.
291 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000292
David Goodwin5e411782009-09-03 22:15:25 +0000293 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000294 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000295 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000296
297 return true;
298}
Jim Grosbach90013032010-05-14 21:19:48 +0000299
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000300/// StartBlock - Initialize register live-range state for scheduling in
301/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000302///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000303void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
304 // Call the superclass.
305 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000306
David Goodwin2e7be612009-10-26 16:59:04 +0000307 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000308 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000309 if (AntiDepBreak != NULL)
310 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000311}
312
313/// Schedule - Schedule the instruction range using list scheduling.
314///
315void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000316 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000317 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000318
David Goodwin2e7be612009-10-26 16:59:04 +0000319 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000320 unsigned Broken =
David Goodwin557bbe62009-11-20 19:32:48 +0000321 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
Devang Patele29e8e12011-06-02 21:26:52 +0000322 InsertPosIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000323
David Goodwin557bbe62009-11-20 19:32:48 +0000324 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000325 // We made changes. Update the dependency graph.
326 // Theoretically we could update the graph in place:
327 // When a live range is changed to use a different register, remove
328 // the def's anti-dependence *and* output-dependence edges due to
329 // that register, and add new anti-dependence and output-dependence
330 // edges based on the next live range of the register.
David Goodwin557bbe62009-11-20 19:32:48 +0000331 SUnits.clear();
332 Sequence.clear();
333 EntrySU = SUnit();
334 ExitSU = SUnit();
335 BuildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000336
David Goodwin2e7be612009-10-26 16:59:04 +0000337 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000338 }
339 }
340
David Greenee1b21292010-01-05 01:26:01 +0000341 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000342 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
343 SUnits[su].dumpAll(this));
344
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000345 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000346 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000347 AvailableQueue.releaseState();
348}
349
350/// Observe - Update liveness information to account for the current
351/// instruction, which will not be scheduled.
352///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000353void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000354 if (AntiDepBreak != NULL)
355 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000356}
357
358/// FinishBlock - Clean up register live-range state.
359///
360void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000361 if (AntiDepBreak != NULL)
362 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000363
364 // Call the superclass.
365 ScheduleDAGInstrs::FinishBlock();
366}
367
David Goodwin5e411782009-09-03 22:15:25 +0000368/// StartBlockForKills - Initialize register live-range state for updating kills
369///
370void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
371 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +0000372 for (unsigned i = 0; i < TRI->getNumRegs(); ++i)
373 KillIndices[i] = ~0u;
David Goodwin5e411782009-09-03 22:15:25 +0000374
375 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000376 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000377 // In a return block, examine the function live-out regs.
378 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
379 E = MRI.liveout_end(); I != E; ++I) {
380 unsigned Reg = *I;
381 KillIndices[Reg] = BB->size();
382 // Repeat, for all subregs.
383 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
384 *Subreg; ++Subreg) {
385 KillIndices[*Subreg] = BB->size();
386 }
387 }
388 }
389 else {
390 // In a non-return block, examine the live-in regs of all successors.
391 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
392 SE = BB->succ_end(); SI != SE; ++SI) {
393 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
394 E = (*SI)->livein_end(); I != E; ++I) {
395 unsigned Reg = *I;
396 KillIndices[Reg] = BB->size();
397 // Repeat, for all subregs.
398 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
399 *Subreg; ++Subreg) {
400 KillIndices[*Subreg] = BB->size();
401 }
402 }
403 }
404 }
405}
406
David Goodwin8f909342009-09-23 16:35:25 +0000407bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
408 MachineOperand &MO) {
409 // Setting kill flag...
410 if (!MO.isKill()) {
411 MO.setIsKill(true);
412 return false;
413 }
Jim Grosbach90013032010-05-14 21:19:48 +0000414
David Goodwin8f909342009-09-23 16:35:25 +0000415 // If MO itself is live, clear the kill flag...
416 if (KillIndices[MO.getReg()] != ~0u) {
417 MO.setIsKill(false);
418 return false;
419 }
420
421 // If any subreg of MO is live, then create an imp-def for that
422 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000423 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000424 bool AllDead = true;
425 const unsigned SuperReg = MO.getReg();
426 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
427 *Subreg; ++Subreg) {
428 if (KillIndices[*Subreg] != ~0u) {
429 MI->addOperand(MachineOperand::CreateReg(*Subreg,
430 true /*IsDef*/,
431 true /*IsImp*/,
432 false /*IsKill*/,
433 false /*IsDead*/));
434 AllDead = false;
435 }
436 }
437
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000438 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000439 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000440 return false;
441}
442
David Goodwin88a589c2009-08-25 17:03:05 +0000443/// FixupKills - Fix the register kill flags, they may have been made
444/// incorrect by instruction reordering.
445///
446void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000447 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000448
449 std::set<unsigned> killedRegs;
450 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000451
452 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000453
David Goodwin7886cd82009-08-29 00:11:13 +0000454 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000455 unsigned Count = MBB->size();
456 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
457 I != E; --Count) {
458 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000459 if (MI->isDebugValue())
460 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000461
David Goodwin7886cd82009-08-29 00:11:13 +0000462 // Update liveness. Registers that are defed but not used in this
463 // instruction are now dead. Mark register and all subregs as they
464 // are completely defined.
465 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
466 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000467 if (MO.isRegMask())
468 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
469 if (MO.clobbersPhysReg(i))
470 KillIndices[i] = ~0u;
David Goodwin7886cd82009-08-29 00:11:13 +0000471 if (!MO.isReg()) continue;
472 unsigned Reg = MO.getReg();
473 if (Reg == 0) continue;
474 if (!MO.isDef()) continue;
475 // Ignore two-addr defs.
476 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000477
David Goodwin7886cd82009-08-29 00:11:13 +0000478 KillIndices[Reg] = ~0u;
Jim Grosbach90013032010-05-14 21:19:48 +0000479
David Goodwin7886cd82009-08-29 00:11:13 +0000480 // Repeat for all subregs.
481 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
482 *Subreg; ++Subreg) {
483 KillIndices[*Subreg] = ~0u;
484 }
485 }
David Goodwin88a589c2009-08-25 17:03:05 +0000486
David Goodwin8f909342009-09-23 16:35:25 +0000487 // Examine all used registers and set/clear kill flag. When a
488 // register is used multiple times we only set the kill flag on
489 // the first use.
David Goodwin88a589c2009-08-25 17:03:05 +0000490 killedRegs.clear();
491 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
492 MachineOperand &MO = MI->getOperand(i);
493 if (!MO.isReg() || !MO.isUse()) continue;
494 unsigned Reg = MO.getReg();
495 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
496
David Goodwin7886cd82009-08-29 00:11:13 +0000497 bool kill = false;
498 if (killedRegs.find(Reg) == killedRegs.end()) {
499 kill = true;
500 // A register is not killed if any subregs are live...
501 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
502 *Subreg; ++Subreg) {
503 if (KillIndices[*Subreg] != ~0u) {
504 kill = false;
505 break;
506 }
507 }
508
509 // If subreg is not live, then register is killed if it became
510 // live in this instruction
511 if (kill)
512 kill = (KillIndices[Reg] == ~0u);
513 }
Jim Grosbach90013032010-05-14 21:19:48 +0000514
David Goodwin88a589c2009-08-25 17:03:05 +0000515 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000516 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000517 // Warning: ToggleKillFlag may invalidate MO.
518 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000519 DEBUG(MI->dump());
520 }
Jim Grosbach90013032010-05-14 21:19:48 +0000521
David Goodwin88a589c2009-08-25 17:03:05 +0000522 killedRegs.insert(Reg);
523 }
Jim Grosbach90013032010-05-14 21:19:48 +0000524
David Goodwina3251db2009-08-31 20:47:02 +0000525 // Mark any used register (that is not using undef) and subregs as
526 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000527 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
528 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000529 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000530 unsigned Reg = MO.getReg();
531 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
532
David Goodwin7886cd82009-08-29 00:11:13 +0000533 KillIndices[Reg] = Count;
Jim Grosbach90013032010-05-14 21:19:48 +0000534
David Goodwin7886cd82009-08-29 00:11:13 +0000535 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
536 *Subreg; ++Subreg) {
537 KillIndices[*Subreg] = Count;
538 }
539 }
David Goodwin88a589c2009-08-25 17:03:05 +0000540 }
541}
542
Dan Gohman343f0c02008-11-19 23:18:57 +0000543//===----------------------------------------------------------------------===//
544// Top-Down Scheduling
545//===----------------------------------------------------------------------===//
546
547/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
548/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000549void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000550 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000551
Dan Gohman343f0c02008-11-19 23:18:57 +0000552#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000553 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000554 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000555 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000556 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000557 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000558 }
559#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000560 --SuccSU->NumPredsLeft;
561
Andrew Trick89fd4372011-05-06 18:14:32 +0000562 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000563 // here as such:
564 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
565 //
566 // However, we lazily compute node depth instead. Note that
567 // ScheduleNodeTopDown has already updated the depth of this node which causes
568 // all descendents to be marked dirty. Setting the successor depth explicitly
569 // here would cause depth to be recomputed for all its ancestors. If the
570 // successor is not yet ready (because of a transitively redundant edge) then
571 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000572
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000573 // If all the node's predecessors are scheduled, this node is ready
574 // to be scheduled. Ignore the special ExitSU node.
575 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000576 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000577}
578
579/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000580void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000581 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000582 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000583 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000584 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000585}
586
587/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
588/// count of its successors. If a successor pending count is zero, add it to
589/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000590void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000591 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000592 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000593
Dan Gohman343f0c02008-11-19 23:18:57 +0000594 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000595 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000596 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000597 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000598
David Goodwin557bbe62009-11-20 19:32:48 +0000599 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000600 SU->isScheduled = true;
601 AvailableQueue.ScheduledNode(SU);
602}
603
604/// ListScheduleTopDown - The main loop of list scheduling for top-down
605/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000606void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000607 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000608
David Goodwin4de099d2009-11-03 20:57:50 +0000609 // We're scheduling top-down but we're visiting the regions in
610 // bottom-up order, so we don't know the hazards at the start of a
611 // region. So assume no hazards (this should usually be ok as most
612 // blocks are a single region).
613 HazardRec->Reset();
614
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000615 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000616 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000617
David Goodwin557bbe62009-11-20 19:32:48 +0000618 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000619 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
620 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000621 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000622 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000623 AvailableQueue.push(&SUnits[i]);
624 SUnits[i].isAvailable = true;
625 }
626 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000627
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000628 // In any cycle where we can't schedule any instructions, we must
629 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000630 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000631
Dan Gohman343f0c02008-11-19 23:18:57 +0000632 // While Available queue is not empty, grab the node with the highest
633 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000634 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000635 Sequence.reserve(SUnits.size());
636 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
637 // Check to see if any of the pending instructions are ready to issue. If
638 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000639 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000640 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000641 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000642 AvailableQueue.push(PendingQueue[i]);
643 PendingQueue[i]->isAvailable = true;
644 PendingQueue[i] = PendingQueue.back();
645 PendingQueue.pop_back();
646 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000647 } else if (PendingQueue[i]->getDepth() < MinDepth)
648 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000649 }
David Goodwinc93d8372009-08-11 17:35:23 +0000650
Andrew Trick2da8bc82010-12-24 05:03:26 +0000651 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000652
Dan Gohman2836c282009-01-16 01:33:36 +0000653 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000654 bool HasNoopHazards = false;
655 while (!AvailableQueue.empty()) {
656 SUnit *CurSUnit = AvailableQueue.pop();
657
658 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000659 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000660 if (HT == ScheduleHazardRecognizer::NoHazard) {
661 FoundSUnit = CurSUnit;
662 break;
663 }
664
665 // Remember if this is a noop hazard.
666 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
667
668 NotReady.push_back(CurSUnit);
669 }
670
671 // Add the nodes that aren't ready back onto the available list.
672 if (!NotReady.empty()) {
673 AvailableQueue.push_all(NotReady);
674 NotReady.clear();
675 }
676
David Goodwin4de099d2009-11-03 20:57:50 +0000677 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000678 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000679 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000680 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000681 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000682 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000683 if (HazardRec->atIssueLimit()) {
684 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
685 HazardRec->AdvanceCycle();
686 ++CurCycle;
687 CycleHasInsts = false;
688 }
Dan Gohman2836c282009-01-16 01:33:36 +0000689 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000690 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000691 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000692 HazardRec->AdvanceCycle();
693 } else if (!HasNoopHazards) {
694 // Otherwise, we have a pipeline stall, but no other problem,
695 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000696 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000697 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000698 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000699 } else {
700 // Otherwise, we have no instructions to issue and we have instructions
701 // that will fault if we don't do this right. This is the case for
702 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000703 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000704 HazardRec->EmitNoop();
705 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000706 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000707 }
708
Dan Gohman2836c282009-01-16 01:33:36 +0000709 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000710 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000711 }
712 }
713
714#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000715 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000716#endif
717}