blob: 488dab721764bd8a88e009bc31f6efdca5f9fff2 [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
Andrew Trick84b454d2012-03-07 05:21:44 +0000148 void EmitSchedule();
149
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);
Andrew Trick73ba69b2012-03-07 05:21:40 +0000175
176 void dumpSchedule() const;
Dan Gohman343f0c02008-11-19 23:18:57 +0000177 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000178}
179
Andrew Trick1dd8c852012-02-08 21:23:13 +0000180char &llvm::PostRASchedulerID = PostRAScheduler::ID;
181
182INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
183 "Post RA top-down list latency scheduler", false, false)
184
Andrew Trick2da8bc82010-12-24 05:03:26 +0000185SchedulePostRATDList::SchedulePostRATDList(
186 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000187 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000188 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000189 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trick5e920d72012-01-14 02:17:12 +0000190 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
Benjamin Kramer46252d82012-02-23 19:15:40 +0000191 LiveRegs(TRI->getNumRegs())
Andrew Trick2da8bc82010-12-24 05:03:26 +0000192{
193 const TargetMachine &TM = MF.getTarget();
194 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
195 HazardRec =
196 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
197 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000198 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000199 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000200 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000201 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000202}
203
204SchedulePostRATDList::~SchedulePostRATDList() {
205 delete HazardRec;
206 delete AntiDepBreak;
207}
208
Andrew Trick73ba69b2012-03-07 05:21:40 +0000209/// dumpSchedule - dump the scheduled Sequence.
210void SchedulePostRATDList::dumpSchedule() const {
211 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
212 if (SUnit *SU = Sequence[i])
213 SU->dump(this);
214 else
215 dbgs() << "**** NOOP ****\n";
216 }
217}
218
Dan Gohman343f0c02008-11-19 23:18:57 +0000219bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000220 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000221 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
222 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
223 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000224 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
225
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000226 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000227
David Goodwin471850a2009-10-01 21:46:35 +0000228 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000229 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
230 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000231 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000232 if (EnablePostRAScheduler.getPosition() > 0) {
233 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000234 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000235 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000236 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000237 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000238 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000239 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
240 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000241 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000242 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000243
David Goodwin4c3715c2009-10-22 23:19:17 +0000244 // Check for antidep breaking override...
245 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000246 AntiDepMode = (EnableAntiDepBreaking == "all")
247 ? TargetSubtargetInfo::ANTIDEP_ALL
248 : ((EnableAntiDepBreaking == "critical")
249 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
250 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000251 }
252
David Greenee1b21292010-01-05 01:26:01 +0000253 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000254
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000255 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000256 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000257
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000258 // Loop over all of the basic blocks
259 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000260 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000261#ifndef NDEBUG
262 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
263 if (DebugDiv > 0) {
264 static int bbcnt = 0;
265 if (bbcnt++ % DebugDiv != DebugMod)
266 continue;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000267 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getName()
268 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000269 }
270#endif
271
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000272 // Initialize register live-range state for scheduling in this block.
273 Scheduler.StartBlock(MBB);
274
Dan Gohmanf7119392009-01-16 22:10:20 +0000275 // Schedule each sequence of instructions not interrupted by a label
276 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000277 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000278 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000279 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000280 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000281 // Calls are not scheduling boundaries before register allocation, but
282 // post-ra we don't gain anything by scheduling across calls since we
283 // don't need to worry about register pressure.
284 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000285 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000286 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000287 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000288 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000289 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000290 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000291 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000292 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000293 if (MI->isBundle())
294 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000295 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000296 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000297 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000298 "Instruction count mismatch!");
299 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000300 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000301
302 // Clean up register live-range state.
303 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000304
David Goodwin5e411782009-09-03 22:15:25 +0000305 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000306 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000307 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000308
309 return true;
310}
Jim Grosbach90013032010-05-14 21:19:48 +0000311
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000312/// StartBlock - Initialize register live-range state for scheduling in
313/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000314///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000315void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
316 // Call the superclass.
317 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000318
David Goodwin2e7be612009-10-26 16:59:04 +0000319 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000320 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000321 if (AntiDepBreak != NULL)
322 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323}
324
325/// Schedule - Schedule the instruction range using list scheduling.
326///
327void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000328 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000329 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000330
David Goodwin2e7be612009-10-26 16:59:04 +0000331 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000332 unsigned Broken =
David Goodwin557bbe62009-11-20 19:32:48 +0000333 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
Devang Patele29e8e12011-06-02 21:26:52 +0000334 InsertPosIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000335
David Goodwin557bbe62009-11-20 19:32:48 +0000336 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000337 // We made changes. Update the dependency graph.
338 // Theoretically we could update the graph in place:
339 // When a live range is changed to use a different register, remove
340 // the def's anti-dependence *and* output-dependence edges due to
341 // that register, and add new anti-dependence and output-dependence
342 // edges based on the next live range of the register.
David Goodwin557bbe62009-11-20 19:32:48 +0000343 SUnits.clear();
344 Sequence.clear();
345 EntrySU = SUnit();
346 ExitSU = SUnit();
347 BuildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000348
David Goodwin2e7be612009-10-26 16:59:04 +0000349 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000350 }
351 }
352
David Greenee1b21292010-01-05 01:26:01 +0000353 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000354 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
355 SUnits[su].dumpAll(this));
356
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000357 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000358 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000359 AvailableQueue.releaseState();
Andrew Trick73ba69b2012-03-07 05:21:40 +0000360
361 DEBUG({
362 dbgs() << "*** Final schedule ***\n";
363 dumpSchedule();
364 dbgs() << '\n';
365 });
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000366}
367
368/// Observe - Update liveness information to account for the current
369/// instruction, which will not be scheduled.
370///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000371void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000372 if (AntiDepBreak != NULL)
373 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000374}
375
376/// FinishBlock - Clean up register live-range state.
377///
378void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000379 if (AntiDepBreak != NULL)
380 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000381
382 // Call the superclass.
383 ScheduleDAGInstrs::FinishBlock();
384}
385
David Goodwin5e411782009-09-03 22:15:25 +0000386/// StartBlockForKills - Initialize register live-range state for updating kills
387///
388void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000389 // Start with no live registers.
390 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000391
392 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000393 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000394 // In a return block, examine the function live-out regs.
395 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
396 E = MRI.liveout_end(); I != E; ++I) {
397 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000398 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000399 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000400 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000401 *Subreg; ++Subreg)
402 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000403 }
404 }
405 else {
406 // In a non-return block, examine the live-in regs of all successors.
407 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
408 SE = BB->succ_end(); SI != SE; ++SI) {
409 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
410 E = (*SI)->livein_end(); I != E; ++I) {
411 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000412 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000413 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000414 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000415 *Subreg; ++Subreg)
416 LiveRegs.set(*Subreg);
David Goodwin5e411782009-09-03 22:15:25 +0000417 }
418 }
419 }
420}
421
David Goodwin8f909342009-09-23 16:35:25 +0000422bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
423 MachineOperand &MO) {
424 // Setting kill flag...
425 if (!MO.isKill()) {
426 MO.setIsKill(true);
427 return false;
428 }
Jim Grosbach90013032010-05-14 21:19:48 +0000429
David Goodwin8f909342009-09-23 16:35:25 +0000430 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000431 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000432 MO.setIsKill(false);
433 return false;
434 }
435
436 // If any subreg of MO is live, then create an imp-def for that
437 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000438 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000439 bool AllDead = true;
440 const unsigned SuperReg = MO.getReg();
Craig Topper9ebfbf82012-03-05 05:37:41 +0000441 for (const uint16_t *Subreg = TRI->getSubRegisters(SuperReg);
David Goodwin8f909342009-09-23 16:35:25 +0000442 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000443 if (LiveRegs.test(*Subreg)) {
David Goodwin8f909342009-09-23 16:35:25 +0000444 MI->addOperand(MachineOperand::CreateReg(*Subreg,
445 true /*IsDef*/,
446 true /*IsImp*/,
447 false /*IsKill*/,
448 false /*IsDead*/));
449 AllDead = false;
450 }
451 }
452
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000453 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000454 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000455 return false;
456}
457
David Goodwin88a589c2009-08-25 17:03:05 +0000458/// FixupKills - Fix the register kill flags, they may have been made
459/// incorrect by instruction reordering.
460///
461void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000462 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000463
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000464 BitVector killedRegs(TRI->getNumRegs());
David Goodwin88a589c2009-08-25 17:03:05 +0000465 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000466
467 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000468
David Goodwin7886cd82009-08-29 00:11:13 +0000469 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000470 unsigned Count = MBB->size();
471 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
472 I != E; --Count) {
473 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000474 if (MI->isDebugValue())
475 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000476
David Goodwin7886cd82009-08-29 00:11:13 +0000477 // Update liveness. Registers that are defed but not used in this
478 // instruction are now dead. Mark register and all subregs as they
479 // are completely defined.
480 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
481 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000482 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000483 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000484 if (!MO.isReg()) continue;
485 unsigned Reg = MO.getReg();
486 if (Reg == 0) continue;
487 if (!MO.isDef()) continue;
488 // Ignore two-addr defs.
489 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000490
Benjamin Kramer46252d82012-02-23 19:15:40 +0000491 LiveRegs.reset(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000492
David Goodwin7886cd82009-08-29 00:11:13 +0000493 // Repeat for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000494 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000495 *Subreg; ++Subreg)
496 LiveRegs.reset(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000497 }
David Goodwin88a589c2009-08-25 17:03:05 +0000498
David Goodwin8f909342009-09-23 16:35:25 +0000499 // Examine all used registers and set/clear kill flag. When a
500 // register is used multiple times we only set the kill flag on
501 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000502 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000503 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
504 MachineOperand &MO = MI->getOperand(i);
505 if (!MO.isReg() || !MO.isUse()) continue;
506 unsigned Reg = MO.getReg();
507 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
508
David Goodwin7886cd82009-08-29 00:11:13 +0000509 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000510 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000511 kill = true;
512 // A register is not killed if any subregs are live...
Craig Topper9ebfbf82012-03-05 05:37:41 +0000513 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000514 *Subreg; ++Subreg) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000515 if (LiveRegs.test(*Subreg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000516 kill = false;
517 break;
518 }
519 }
520
521 // If subreg is not live, then register is killed if it became
522 // live in this instruction
523 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000524 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000525 }
Jim Grosbach90013032010-05-14 21:19:48 +0000526
David Goodwin88a589c2009-08-25 17:03:05 +0000527 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000528 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000529 // Warning: ToggleKillFlag may invalidate MO.
530 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000531 DEBUG(MI->dump());
532 }
Jim Grosbach90013032010-05-14 21:19:48 +0000533
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000534 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000535 }
Jim Grosbach90013032010-05-14 21:19:48 +0000536
David Goodwina3251db2009-08-31 20:47:02 +0000537 // Mark any used register (that is not using undef) and subregs as
538 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000539 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
540 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000541 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000542 unsigned Reg = MO.getReg();
543 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
544
Benjamin Kramer46252d82012-02-23 19:15:40 +0000545 LiveRegs.set(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000546
Craig Topper9ebfbf82012-03-05 05:37:41 +0000547 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Benjamin Kramer46252d82012-02-23 19:15:40 +0000548 *Subreg; ++Subreg)
549 LiveRegs.set(*Subreg);
David Goodwin7886cd82009-08-29 00:11:13 +0000550 }
David Goodwin88a589c2009-08-25 17:03:05 +0000551 }
552}
553
Dan Gohman343f0c02008-11-19 23:18:57 +0000554//===----------------------------------------------------------------------===//
555// Top-Down Scheduling
556//===----------------------------------------------------------------------===//
557
558/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
559/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000560void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000561 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000562
Dan Gohman343f0c02008-11-19 23:18:57 +0000563#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000564 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000565 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000566 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000567 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000568 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000569 }
570#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000571 --SuccSU->NumPredsLeft;
572
Andrew Trick89fd4372011-05-06 18:14:32 +0000573 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000574 // here as such:
575 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
576 //
577 // However, we lazily compute node depth instead. Note that
578 // ScheduleNodeTopDown has already updated the depth of this node which causes
579 // all descendents to be marked dirty. Setting the successor depth explicitly
580 // here would cause depth to be recomputed for all its ancestors. If the
581 // successor is not yet ready (because of a transitively redundant edge) then
582 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000583
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000584 // If all the node's predecessors are scheduled, this node is ready
585 // to be scheduled. Ignore the special ExitSU node.
586 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000587 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000588}
589
590/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000591void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000592 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000593 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000594 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000595 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000596}
597
598/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
599/// count of its successors. If a successor pending count is zero, add it to
600/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000601void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000602 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000603 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000604
Dan Gohman343f0c02008-11-19 23:18:57 +0000605 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000606 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000607 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000608 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000609
David Goodwin557bbe62009-11-20 19:32:48 +0000610 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000611 SU->isScheduled = true;
612 AvailableQueue.ScheduledNode(SU);
613}
614
615/// ListScheduleTopDown - The main loop of list scheduling for top-down
616/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000617void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000618 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000619
David Goodwin4de099d2009-11-03 20:57:50 +0000620 // We're scheduling top-down but we're visiting the regions in
621 // bottom-up order, so we don't know the hazards at the start of a
622 // region. So assume no hazards (this should usually be ok as most
623 // blocks are a single region).
624 HazardRec->Reset();
625
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000626 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000627 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000628
David Goodwin557bbe62009-11-20 19:32:48 +0000629 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000630 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
631 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000632 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000633 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000634 AvailableQueue.push(&SUnits[i]);
635 SUnits[i].isAvailable = true;
636 }
637 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000638
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000639 // In any cycle where we can't schedule any instructions, we must
640 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000641 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000642
Dan Gohman343f0c02008-11-19 23:18:57 +0000643 // While Available queue is not empty, grab the node with the highest
644 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000645 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000646 Sequence.reserve(SUnits.size());
647 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
648 // Check to see if any of the pending instructions are ready to issue. If
649 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000650 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000651 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000652 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000653 AvailableQueue.push(PendingQueue[i]);
654 PendingQueue[i]->isAvailable = true;
655 PendingQueue[i] = PendingQueue.back();
656 PendingQueue.pop_back();
657 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000658 } else if (PendingQueue[i]->getDepth() < MinDepth)
659 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000660 }
David Goodwinc93d8372009-08-11 17:35:23 +0000661
Andrew Trick2da8bc82010-12-24 05:03:26 +0000662 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000663
Dan Gohman2836c282009-01-16 01:33:36 +0000664 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000665 bool HasNoopHazards = false;
666 while (!AvailableQueue.empty()) {
667 SUnit *CurSUnit = AvailableQueue.pop();
668
669 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000670 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000671 if (HT == ScheduleHazardRecognizer::NoHazard) {
672 FoundSUnit = CurSUnit;
673 break;
674 }
675
676 // Remember if this is a noop hazard.
677 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
678
679 NotReady.push_back(CurSUnit);
680 }
681
682 // Add the nodes that aren't ready back onto the available list.
683 if (!NotReady.empty()) {
684 AvailableQueue.push_all(NotReady);
685 NotReady.clear();
686 }
687
David Goodwin4de099d2009-11-03 20:57:50 +0000688 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000689 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000690 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000691 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000692 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000693 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000694 if (HazardRec->atIssueLimit()) {
695 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
696 HazardRec->AdvanceCycle();
697 ++CurCycle;
698 CycleHasInsts = false;
699 }
Dan Gohman2836c282009-01-16 01:33:36 +0000700 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000701 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000702 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000703 HazardRec->AdvanceCycle();
704 } else if (!HasNoopHazards) {
705 // Otherwise, we have a pipeline stall, but no other problem,
706 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000707 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000708 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000709 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000710 } else {
711 // Otherwise, we have no instructions to issue and we have instructions
712 // that will fault if we don't do this right. This is the case for
713 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000714 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000715 HazardRec->EmitNoop();
716 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000717 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000718 }
719
Dan Gohman2836c282009-01-16 01:33:36 +0000720 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000721 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000722 }
723 }
724
725#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000726 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
727 unsigned Noops = 0;
728 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
729 if (!Sequence[i])
730 ++Noops;
731 assert(Sequence.size() - Noops == ScheduledNodes &&
732 "The number of nodes scheduled doesn't match the expected number!");
733#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000734}
Andrew Trick84b454d2012-03-07 05:21:44 +0000735
736// EmitSchedule - Emit the machine code in scheduled order.
737void SchedulePostRATDList::EmitSchedule() {
738 Begin = InsertPos;
739
740 // If first instruction was a DBG_VALUE then put it back.
741 if (FirstDbgValue)
742 BB->splice(InsertPos, BB, FirstDbgValue);
743
744 // Then re-insert them according to the given schedule.
745 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
746 if (SUnit *SU = Sequence[i])
747 BB->splice(InsertPos, BB, SU->getInstr());
748 else
749 // Null SUnit* is a noop.
750 TII->insertNoop(*BB, InsertPos);
751
752 // Update the Begin iterator, as the first instruction in the block
753 // may have been scheduled later.
754 if (i == 0)
755 Begin = prior(InsertPos);
756 }
757
758 // Reinsert any remaining debug_values.
759 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
760 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
761 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
762 MachineInstr *DbgValue = P.first;
763 MachineBasicBlock::iterator OrigPrivMI = P.second;
764 BB->splice(++OrigPrivMI, BB, DbgValue);
765 }
766 DbgValues.clear();
767 FirstDbgValue = NULL;
768}