blob: b3e616e939002f364f0a9848a6a0803ed14a8a08 [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
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000127 /// KillIndices - The index of the most recent kill (proceding bottom-up),
128 /// or ~0u if the register is not live.
Bill Wendling24173da2010-07-15 20:01:02 +0000129 std::vector<unsigned> KillIndices;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000130
Dan Gohman21d90032008-11-25 00:52:40 +0000131 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000132 SchedulePostRATDList(
133 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000134 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000135 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000136 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000137
Andrew Trick2da8bc82010-12-24 05:03:26 +0000138 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000139
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000140 /// StartBlock - Initialize register live-range state for scheduling in
141 /// this block.
142 ///
143 void StartBlock(MachineBasicBlock *BB);
144
145 /// Schedule - Schedule the instruction range using list scheduling.
146 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000147 void Schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000148
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000149 /// Observe - Update liveness information to account for the current
150 /// instruction, which will not be scheduled.
151 ///
152 void Observe(MachineInstr *MI, unsigned Count);
153
154 /// FinishBlock - Clean up register live-range state.
155 ///
156 void FinishBlock();
157
David Goodwin2e7be612009-10-26 16:59:04 +0000158 /// FixupKills - Fix register kill flags that have been made
159 /// invalid due to scheduling
160 ///
161 void FixupKills(MachineBasicBlock *MBB);
162
Dan Gohman343f0c02008-11-19 23:18:57 +0000163 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000164 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
165 void ReleaseSuccessors(SUnit *SU);
166 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
167 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000168 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000169
David Goodwin8f909342009-09-23 16:35:25 +0000170 // ToggleKillFlag - Toggle a register operand kill flag. Other
171 // adjustments may be made to the instruction if necessary. Return
172 // true if the operand has been deleted, false if not.
173 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohman343f0c02008-11-19 23:18:57 +0000174 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000175}
176
Andrew Trick1dd8c852012-02-08 21:23:13 +0000177char &llvm::PostRASchedulerID = PostRAScheduler::ID;
178
179INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
180 "Post RA top-down list latency scheduler", false, false)
181
Andrew Trick2da8bc82010-12-24 05:03:26 +0000182SchedulePostRATDList::SchedulePostRATDList(
183 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000184 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000185 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000186 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trick5e920d72012-01-14 02:17:12 +0000187 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
Andrew Trick2da8bc82010-12-24 05:03:26 +0000188 KillIndices(TRI->getNumRegs())
189{
190 const TargetMachine &TM = MF.getTarget();
191 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
192 HazardRec =
193 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
194 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000195 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000196 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000197 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000198 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000199}
200
201SchedulePostRATDList::~SchedulePostRATDList() {
202 delete HazardRec;
203 delete AntiDepBreak;
204}
205
Dan Gohman343f0c02008-11-19 23:18:57 +0000206bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000207 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000208 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
209 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
210 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000211 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
212
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000213 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000214
David Goodwin471850a2009-10-01 21:46:35 +0000215 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000216 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
217 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000218 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000219 if (EnablePostRAScheduler.getPosition() > 0) {
220 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000221 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000222 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000223 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000224 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000225 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000226 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
227 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000228 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000229 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000230
David Goodwin4c3715c2009-10-22 23:19:17 +0000231 // Check for antidep breaking override...
232 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000233 AntiDepMode = (EnableAntiDepBreaking == "all")
234 ? TargetSubtargetInfo::ANTIDEP_ALL
235 : ((EnableAntiDepBreaking == "critical")
236 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
237 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000238 }
239
David Greenee1b21292010-01-05 01:26:01 +0000240 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000241
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000242 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000243 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000244
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000245 // Loop over all of the basic blocks
246 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000247 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000248#ifndef NDEBUG
249 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
250 if (DebugDiv > 0) {
251 static int bbcnt = 0;
252 if (bbcnt++ % DebugDiv != DebugMod)
253 continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000254 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
255 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000256 }
257#endif
258
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000259 // Initialize register live-range state for scheduling in this block.
260 Scheduler.StartBlock(MBB);
261
Dan Gohmanf7119392009-01-16 22:10:20 +0000262 // Schedule each sequence of instructions not interrupted by a label
263 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000264 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000265 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000266 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000267 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000268 // Calls are not scheduling boundaries before register allocation, but
269 // post-ra we don't gain anything by scheduling across calls since we
270 // don't need to worry about register pressure.
271 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000272 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000273 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000274 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000275 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000276 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000277 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000278 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000279 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000280 if (MI->isBundle())
281 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000282 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000283 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000284 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000285 "Instruction count mismatch!");
286 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000287 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000288
289 // Clean up register live-range state.
290 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000291
David Goodwin5e411782009-09-03 22:15:25 +0000292 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000293 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000294 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000295
296 return true;
297}
Jim Grosbach90013032010-05-14 21:19:48 +0000298
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000299/// StartBlock - Initialize register live-range state for scheduling in
300/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000301///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000302void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
303 // Call the superclass.
304 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000305
David Goodwin2e7be612009-10-26 16:59:04 +0000306 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000307 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000308 if (AntiDepBreak != NULL)
309 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000310}
311
312/// Schedule - Schedule the instruction range using list scheduling.
313///
314void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000315 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000316 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000317
David Goodwin2e7be612009-10-26 16:59:04 +0000318 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000319 unsigned Broken =
David Goodwin557bbe62009-11-20 19:32:48 +0000320 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
Devang Patele29e8e12011-06-02 21:26:52 +0000321 InsertPosIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000322
David Goodwin557bbe62009-11-20 19:32:48 +0000323 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000324 // We made changes. Update the dependency graph.
325 // Theoretically we could update the graph in place:
326 // When a live range is changed to use a different register, remove
327 // the def's anti-dependence *and* output-dependence edges due to
328 // that register, and add new anti-dependence and output-dependence
329 // edges based on the next live range of the register.
David Goodwin557bbe62009-11-20 19:32:48 +0000330 SUnits.clear();
331 Sequence.clear();
332 EntrySU = SUnit();
333 ExitSU = SUnit();
334 BuildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000335
David Goodwin2e7be612009-10-26 16:59:04 +0000336 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000337 }
338 }
339
David Greenee1b21292010-01-05 01:26:01 +0000340 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000341 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
342 SUnits[su].dumpAll(this));
343
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000344 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000345 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000346 AvailableQueue.releaseState();
347}
348
349/// Observe - Update liveness information to account for the current
350/// instruction, which will not be scheduled.
351///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000352void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000353 if (AntiDepBreak != NULL)
354 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000355}
356
357/// FinishBlock - Clean up register live-range state.
358///
359void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000360 if (AntiDepBreak != NULL)
361 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000362
363 // Call the superclass.
364 ScheduleDAGInstrs::FinishBlock();
365}
366
David Goodwin5e411782009-09-03 22:15:25 +0000367/// StartBlockForKills - Initialize register live-range state for updating kills
368///
369void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
370 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +0000371 for (unsigned i = 0; i < TRI->getNumRegs(); ++i)
372 KillIndices[i] = ~0u;
David Goodwin5e411782009-09-03 22:15:25 +0000373
374 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000375 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000376 // In a return block, examine the function live-out regs.
377 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
378 E = MRI.liveout_end(); I != E; ++I) {
379 unsigned Reg = *I;
380 KillIndices[Reg] = BB->size();
381 // Repeat, for all subregs.
382 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
383 *Subreg; ++Subreg) {
384 KillIndices[*Subreg] = BB->size();
385 }
386 }
387 }
388 else {
389 // In a non-return block, examine the live-in regs of all successors.
390 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
391 SE = BB->succ_end(); SI != SE; ++SI) {
392 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
393 E = (*SI)->livein_end(); I != E; ++I) {
394 unsigned Reg = *I;
395 KillIndices[Reg] = BB->size();
396 // Repeat, for all subregs.
397 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
398 *Subreg; ++Subreg) {
399 KillIndices[*Subreg] = BB->size();
400 }
401 }
402 }
403 }
404}
405
David Goodwin8f909342009-09-23 16:35:25 +0000406bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
407 MachineOperand &MO) {
408 // Setting kill flag...
409 if (!MO.isKill()) {
410 MO.setIsKill(true);
411 return false;
412 }
Jim Grosbach90013032010-05-14 21:19:48 +0000413
David Goodwin8f909342009-09-23 16:35:25 +0000414 // If MO itself is live, clear the kill flag...
415 if (KillIndices[MO.getReg()] != ~0u) {
416 MO.setIsKill(false);
417 return false;
418 }
419
420 // If any subreg of MO is live, then create an imp-def for that
421 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000422 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000423 bool AllDead = true;
424 const unsigned SuperReg = MO.getReg();
425 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
426 *Subreg; ++Subreg) {
427 if (KillIndices[*Subreg] != ~0u) {
428 MI->addOperand(MachineOperand::CreateReg(*Subreg,
429 true /*IsDef*/,
430 true /*IsImp*/,
431 false /*IsKill*/,
432 false /*IsDead*/));
433 AllDead = false;
434 }
435 }
436
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000437 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000438 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000439 return false;
440}
441
David Goodwin88a589c2009-08-25 17:03:05 +0000442/// FixupKills - Fix the register kill flags, they may have been made
443/// incorrect by instruction reordering.
444///
445void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000446 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000447
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000448 BitVector killedRegs(TRI->getNumRegs());
David Goodwin88a589c2009-08-25 17:03:05 +0000449 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000450
451 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000452
David Goodwin7886cd82009-08-29 00:11:13 +0000453 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000454 unsigned Count = MBB->size();
455 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
456 I != E; --Count) {
457 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000458 if (MI->isDebugValue())
459 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000460
David Goodwin7886cd82009-08-29 00:11:13 +0000461 // Update liveness. Registers that are defed but not used in this
462 // instruction are now dead. Mark register and all subregs as they
463 // are completely defined.
464 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
465 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000466 if (MO.isRegMask())
467 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
468 if (MO.clobbersPhysReg(i))
469 KillIndices[i] = ~0u;
David Goodwin7886cd82009-08-29 00:11:13 +0000470 if (!MO.isReg()) continue;
471 unsigned Reg = MO.getReg();
472 if (Reg == 0) continue;
473 if (!MO.isDef()) continue;
474 // Ignore two-addr defs.
475 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000476
David Goodwin7886cd82009-08-29 00:11:13 +0000477 KillIndices[Reg] = ~0u;
Jim Grosbach90013032010-05-14 21:19:48 +0000478
David Goodwin7886cd82009-08-29 00:11:13 +0000479 // Repeat for all subregs.
480 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
481 *Subreg; ++Subreg) {
482 KillIndices[*Subreg] = ~0u;
483 }
484 }
David Goodwin88a589c2009-08-25 17:03:05 +0000485
David Goodwin8f909342009-09-23 16:35:25 +0000486 // Examine all used registers and set/clear kill flag. When a
487 // register is used multiple times we only set the kill flag on
488 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000489 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000490 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
491 MachineOperand &MO = MI->getOperand(i);
492 if (!MO.isReg() || !MO.isUse()) continue;
493 unsigned Reg = MO.getReg();
494 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
495
David Goodwin7886cd82009-08-29 00:11:13 +0000496 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000497 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000498 kill = true;
499 // A register is not killed if any subregs are live...
500 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
501 *Subreg; ++Subreg) {
502 if (KillIndices[*Subreg] != ~0u) {
503 kill = false;
504 break;
505 }
506 }
507
508 // If subreg is not live, then register is killed if it became
509 // live in this instruction
510 if (kill)
511 kill = (KillIndices[Reg] == ~0u);
512 }
Jim Grosbach90013032010-05-14 21:19:48 +0000513
David Goodwin88a589c2009-08-25 17:03:05 +0000514 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000515 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000516 // Warning: ToggleKillFlag may invalidate MO.
517 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000518 DEBUG(MI->dump());
519 }
Jim Grosbach90013032010-05-14 21:19:48 +0000520
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000521 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000522 }
Jim Grosbach90013032010-05-14 21:19:48 +0000523
David Goodwina3251db2009-08-31 20:47:02 +0000524 // Mark any used register (that is not using undef) and subregs as
525 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000526 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
527 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000528 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000529 unsigned Reg = MO.getReg();
530 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
531
David Goodwin7886cd82009-08-29 00:11:13 +0000532 KillIndices[Reg] = Count;
Jim Grosbach90013032010-05-14 21:19:48 +0000533
David Goodwin7886cd82009-08-29 00:11:13 +0000534 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
535 *Subreg; ++Subreg) {
536 KillIndices[*Subreg] = Count;
537 }
538 }
David Goodwin88a589c2009-08-25 17:03:05 +0000539 }
540}
541
Dan Gohman343f0c02008-11-19 23:18:57 +0000542//===----------------------------------------------------------------------===//
543// Top-Down Scheduling
544//===----------------------------------------------------------------------===//
545
546/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
547/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000548void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000549 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000550
Dan Gohman343f0c02008-11-19 23:18:57 +0000551#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000552 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000553 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000554 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000555 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000556 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000557 }
558#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000559 --SuccSU->NumPredsLeft;
560
Andrew Trick89fd4372011-05-06 18:14:32 +0000561 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000562 // here as such:
563 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
564 //
565 // However, we lazily compute node depth instead. Note that
566 // ScheduleNodeTopDown has already updated the depth of this node which causes
567 // all descendents to be marked dirty. Setting the successor depth explicitly
568 // here would cause depth to be recomputed for all its ancestors. If the
569 // successor is not yet ready (because of a transitively redundant edge) then
570 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000571
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000572 // If all the node's predecessors are scheduled, this node is ready
573 // to be scheduled. Ignore the special ExitSU node.
574 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000575 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000576}
577
578/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000579void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000580 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000581 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000582 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000583 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000584}
585
586/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
587/// count of its successors. If a successor pending count is zero, add it to
588/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000589void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000590 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000591 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000592
Dan Gohman343f0c02008-11-19 23:18:57 +0000593 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000594 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000595 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000596 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000597
David Goodwin557bbe62009-11-20 19:32:48 +0000598 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000599 SU->isScheduled = true;
600 AvailableQueue.ScheduledNode(SU);
601}
602
603/// ListScheduleTopDown - The main loop of list scheduling for top-down
604/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000605void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000606 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000607
David Goodwin4de099d2009-11-03 20:57:50 +0000608 // We're scheduling top-down but we're visiting the regions in
609 // bottom-up order, so we don't know the hazards at the start of a
610 // region. So assume no hazards (this should usually be ok as most
611 // blocks are a single region).
612 HazardRec->Reset();
613
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000614 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000615 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000616
David Goodwin557bbe62009-11-20 19:32:48 +0000617 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000618 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
619 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000620 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000621 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000622 AvailableQueue.push(&SUnits[i]);
623 SUnits[i].isAvailable = true;
624 }
625 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000626
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000627 // In any cycle where we can't schedule any instructions, we must
628 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000629 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000630
Dan Gohman343f0c02008-11-19 23:18:57 +0000631 // While Available queue is not empty, grab the node with the highest
632 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000633 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000634 Sequence.reserve(SUnits.size());
635 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
636 // Check to see if any of the pending instructions are ready to issue. If
637 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000638 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000639 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000640 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000641 AvailableQueue.push(PendingQueue[i]);
642 PendingQueue[i]->isAvailable = true;
643 PendingQueue[i] = PendingQueue.back();
644 PendingQueue.pop_back();
645 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000646 } else if (PendingQueue[i]->getDepth() < MinDepth)
647 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000648 }
David Goodwinc93d8372009-08-11 17:35:23 +0000649
Andrew Trick2da8bc82010-12-24 05:03:26 +0000650 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000651
Dan Gohman2836c282009-01-16 01:33:36 +0000652 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000653 bool HasNoopHazards = false;
654 while (!AvailableQueue.empty()) {
655 SUnit *CurSUnit = AvailableQueue.pop();
656
657 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000658 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000659 if (HT == ScheduleHazardRecognizer::NoHazard) {
660 FoundSUnit = CurSUnit;
661 break;
662 }
663
664 // Remember if this is a noop hazard.
665 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
666
667 NotReady.push_back(CurSUnit);
668 }
669
670 // Add the nodes that aren't ready back onto the available list.
671 if (!NotReady.empty()) {
672 AvailableQueue.push_all(NotReady);
673 NotReady.clear();
674 }
675
David Goodwin4de099d2009-11-03 20:57:50 +0000676 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000677 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000678 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000679 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000680 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000681 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000682 if (HazardRec->atIssueLimit()) {
683 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
684 HazardRec->AdvanceCycle();
685 ++CurCycle;
686 CycleHasInsts = false;
687 }
Dan Gohman2836c282009-01-16 01:33:36 +0000688 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000689 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000690 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000691 HazardRec->AdvanceCycle();
692 } else if (!HasNoopHazards) {
693 // Otherwise, we have a pipeline stall, but no other problem,
694 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000695 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000696 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000697 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000698 } else {
699 // Otherwise, we have no instructions to issue and we have instructions
700 // that will fault if we don't do this right. This is the case for
701 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000702 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000703 HazardRec->EmitNoop();
704 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000705 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000706 }
707
Dan Gohman2836c282009-01-16 01:33:36 +0000708 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000709 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000710 }
711 }
712
713#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000714 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000715#endif
716}