blob: f37fc82b2a9c941d19fe312c46de6760133799bd [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"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000025#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000026#include "llvm/CodeGen/LatencyPriorityQueue.h"
27#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3f237442008-12-16 03:25:46 +000028#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000031#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick15252602012-06-06 20:29:31 +000033#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Tricked395c82012-03-07 23:01:06 +000034#include "llvm/CodeGen/ScheduleDAGInstrs.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 {
Evan Cheng86050dc2010-06-18 23:09:54 +000081 const TargetInstrInfo *TII;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000082 RegisterClassInfo RegClassInfo;
Dan Gohmana70dca12009-10-09 23:27:56 +000083
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000084 public:
85 static char ID;
Andrew Trickc7d081b2012-02-08 21:22:53 +000086 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000087
Dan Gohman3f237442008-12-16 03:25:46 +000088 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000089 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000090 AU.addRequired<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +000091 AU.addRequired<TargetPassConfig>();
Dan Gohman3f237442008-12-16 03:25:46 +000092 AU.addRequired<MachineDominatorTree>();
93 AU.addPreserved<MachineDominatorTree>();
94 AU.addRequired<MachineLoopInfo>();
95 AU.addPreserved<MachineLoopInfo>();
96 MachineFunctionPass::getAnalysisUsage(AU);
97 }
98
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000099 bool runOnMachineFunction(MachineFunction &Fn);
100 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000101 char PostRAScheduler::ID = 0;
102
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000103 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000104 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000105 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000106 LatencyPriorityQueue AvailableQueue;
Jim Grosbach90013032010-05-14 21:19:48 +0000107
Dan Gohman343f0c02008-11-19 23:18:57 +0000108 /// PendingQueue - This contains all of the instructions whose operands have
109 /// been issued, but their results are not ready yet (due to the latency of
110 /// the operation). Once the operands becomes available, the instruction is
111 /// added to the AvailableQueue.
112 std::vector<SUnit*> PendingQueue;
113
Dan Gohman2836c282009-01-16 01:33:36 +0000114 /// HazardRec - The hazard recognizer to use.
115 ScheduleHazardRecognizer *HazardRec;
116
David Goodwin2e7be612009-10-26 16:59:04 +0000117 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
118 AntiDepBreaker *AntiDepBreak;
119
Dan Gohmana70dca12009-10-09 23:27:56 +0000120 /// AA - AliasAnalysis for making memory reference queries.
121 AliasAnalysis *AA;
122
Benjamin Kramer46252d82012-02-23 19:15:40 +0000123 /// LiveRegs - true if the register is live.
124 BitVector LiveRegs;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000125
Andrew Trick47c14452012-03-07 05:21:52 +0000126 /// The schedule. Null SUnit*'s represent noop instructions.
127 std::vector<SUnit*> Sequence;
128
Dan Gohman21d90032008-11-25 00:52:40 +0000129 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000130 SchedulePostRATDList(
131 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000132 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000133 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000134 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000135
Andrew Trick2da8bc82010-12-24 05:03:26 +0000136 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000137
Andrew Trick953be892012-03-07 23:00:49 +0000138 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000139 /// this block.
140 ///
Andrew Trick953be892012-03-07 23:00:49 +0000141 void startBlock(MachineBasicBlock *BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000142
Andrew Trick47c14452012-03-07 05:21:52 +0000143 /// Initialize the scheduler state for the next scheduling region.
144 virtual void enterRegion(MachineBasicBlock *bb,
145 MachineBasicBlock::iterator begin,
146 MachineBasicBlock::iterator end,
147 unsigned endcount);
148
149 /// Notify that the scheduler has finished scheduling the current region.
150 virtual void exitRegion();
151
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000152 /// Schedule - Schedule the instruction range using list scheduling.
153 ///
Andrew Trick953be892012-03-07 23:00:49 +0000154 void schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000155
Andrew Trick84b454d2012-03-07 05:21:44 +0000156 void EmitSchedule();
157
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000158 /// Observe - Update liveness information to account for the current
159 /// instruction, which will not be scheduled.
160 ///
161 void Observe(MachineInstr *MI, unsigned Count);
162
Andrew Trick953be892012-03-07 23:00:49 +0000163 /// finishBlock - Clean up register live-range state.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000164 ///
Andrew Trick953be892012-03-07 23:00:49 +0000165 void finishBlock();
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000166
David Goodwin2e7be612009-10-26 16:59:04 +0000167 /// FixupKills - Fix register kill flags that have been made
168 /// invalid due to scheduling
169 ///
170 void FixupKills(MachineBasicBlock *MBB);
171
Dan Gohman343f0c02008-11-19 23:18:57 +0000172 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000173 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
174 void ReleaseSuccessors(SUnit *SU);
175 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
176 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000177 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000178
David Goodwin8f909342009-09-23 16:35:25 +0000179 // ToggleKillFlag - Toggle a register operand kill flag. Other
180 // adjustments may be made to the instruction if necessary. Return
181 // true if the operand has been deleted, false if not.
182 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Andrew Trick73ba69b2012-03-07 05:21:40 +0000183
184 void dumpSchedule() const;
Dan Gohman343f0c02008-11-19 23:18:57 +0000185 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000186}
187
Andrew Trick1dd8c852012-02-08 21:23:13 +0000188char &llvm::PostRASchedulerID = PostRAScheduler::ID;
189
190INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
191 "Post RA top-down list latency scheduler", false, false)
192
Andrew Trick2da8bc82010-12-24 05:03:26 +0000193SchedulePostRATDList::SchedulePostRATDList(
194 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000195 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000196 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000197 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trickae692f22012-11-12 19:28:57 +0000198 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA),
Benjamin Kramer46252d82012-02-23 19:15:40 +0000199 LiveRegs(TRI->getNumRegs())
Andrew Trick2da8bc82010-12-24 05:03:26 +0000200{
201 const TargetMachine &TM = MF.getTarget();
202 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
203 HazardRec =
204 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000205
206 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
207 MRI.tracksLiveness()) &&
208 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick2da8bc82010-12-24 05:03:26 +0000209 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000210 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000211 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000212 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000213 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000214}
215
216SchedulePostRATDList::~SchedulePostRATDList() {
217 delete HazardRec;
218 delete AntiDepBreak;
219}
220
Andrew Trick47c14452012-03-07 05:21:52 +0000221/// Initialize state associated with the next scheduling region.
222void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
223 MachineBasicBlock::iterator begin,
224 MachineBasicBlock::iterator end,
225 unsigned endcount) {
226 ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount);
227 Sequence.clear();
228}
229
230/// Print the schedule before exiting the region.
231void SchedulePostRATDList::exitRegion() {
232 DEBUG({
233 dbgs() << "*** Final schedule ***\n";
234 dumpSchedule();
235 dbgs() << '\n';
236 });
237 ScheduleDAGInstrs::exitRegion();
238}
239
Manman Renb720be62012-09-11 22:23:19 +0000240#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick73ba69b2012-03-07 05:21:40 +0000241/// dumpSchedule - dump the scheduled Sequence.
242void SchedulePostRATDList::dumpSchedule() const {
243 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
244 if (SUnit *SU = Sequence[i])
245 SU->dump(this);
246 else
247 dbgs() << "**** NOOP ****\n";
248 }
249}
Manman Ren77e300e2012-09-06 19:06:06 +0000250#endif
Andrew Trick73ba69b2012-03-07 05:21:40 +0000251
Dan Gohman343f0c02008-11-19 23:18:57 +0000252bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000253 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000254 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
255 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
256 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000257 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
258
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000259 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000260
David Goodwin471850a2009-10-01 21:46:35 +0000261 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000262 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
263 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000264 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000265 if (EnablePostRAScheduler.getPosition() > 0) {
266 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000267 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000268 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000269 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000270 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000271 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000272 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
273 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000274 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000275 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000276
David Goodwin4c3715c2009-10-22 23:19:17 +0000277 // Check for antidep breaking override...
278 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000279 AntiDepMode = (EnableAntiDepBreaking == "all")
280 ? TargetSubtargetInfo::ANTIDEP_ALL
281 : ((EnableAntiDepBreaking == "critical")
282 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
283 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000284 }
285
David Greenee1b21292010-01-05 01:26:01 +0000286 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000287
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000288 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000289 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000290
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000291 // Loop over all of the basic blocks
292 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000293 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000294#ifndef NDEBUG
295 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
296 if (DebugDiv > 0) {
297 static int bbcnt = 0;
298 if (bbcnt++ % DebugDiv != DebugMod)
299 continue;
Craig Topper96601ca2012-08-22 06:07:19 +0000300 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000301 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000302 }
303#endif
304
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000305 // Initialize register live-range state for scheduling in this block.
Andrew Trick953be892012-03-07 23:00:49 +0000306 Scheduler.startBlock(MBB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000307
Dan Gohmanf7119392009-01-16 22:10:20 +0000308 // Schedule each sequence of instructions not interrupted by a label
309 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000310 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000311 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000312 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000313 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000314 // Calls are not scheduling boundaries before register allocation, but
315 // post-ra we don't gain anything by scheduling across calls since we
316 // don't need to worry about register pressure.
317 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Trick47c14452012-03-07 05:21:52 +0000318 Scheduler.enterRegion(MBB, I, Current, CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000319 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000320 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000321 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000322 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000323 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000324 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000325 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000326 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000327 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000328 if (MI->isBundle())
329 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000330 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000331 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000332 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000333 "Instruction count mismatch!");
Andrew Trick47c14452012-03-07 05:21:52 +0000334 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000335 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000336 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000337 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000338
339 // Clean up register live-range state.
Andrew Trick953be892012-03-07 23:00:49 +0000340 Scheduler.finishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000341
David Goodwin5e411782009-09-03 22:15:25 +0000342 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000343 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000344 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000345
346 return true;
347}
Jim Grosbach90013032010-05-14 21:19:48 +0000348
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000349/// StartBlock - Initialize register live-range state for scheduling in
350/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000351///
Andrew Trick953be892012-03-07 23:00:49 +0000352void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000353 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000354 ScheduleDAGInstrs::startBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000355
David Goodwin2e7be612009-10-26 16:59:04 +0000356 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000357 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000358 if (AntiDepBreak != NULL)
359 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000360}
361
362/// Schedule - Schedule the instruction range using list scheduling.
363///
Andrew Trick953be892012-03-07 23:00:49 +0000364void SchedulePostRATDList::schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000365 // Build the scheduling graph.
Andrew Trick953be892012-03-07 23:00:49 +0000366 buildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000367
David Goodwin2e7be612009-10-26 16:59:04 +0000368 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000369 unsigned Broken =
Andrew Trick68675c62012-03-09 04:29:02 +0000370 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
371 EndIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000372
David Goodwin557bbe62009-11-20 19:32:48 +0000373 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000374 // We made changes. Update the dependency graph.
375 // Theoretically we could update the graph in place:
376 // When a live range is changed to use a different register, remove
377 // the def's anti-dependence *and* output-dependence edges due to
378 // that register, and add new anti-dependence and output-dependence
379 // edges based on the next live range of the register.
Andrew Trick47c14452012-03-07 05:21:52 +0000380 ScheduleDAG::clearDAG();
Andrew Trick953be892012-03-07 23:00:49 +0000381 buildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000382
David Goodwin2e7be612009-10-26 16:59:04 +0000383 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000384 }
385 }
386
David Greenee1b21292010-01-05 01:26:01 +0000387 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000388 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
389 SUnits[su].dumpAll(this));
390
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000391 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000392 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000393 AvailableQueue.releaseState();
394}
395
396/// Observe - Update liveness information to account for the current
397/// instruction, which will not be scheduled.
398///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000399void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000400 if (AntiDepBreak != NULL)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000401 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000402}
403
404/// FinishBlock - Clean up register live-range state.
405///
Andrew Trick953be892012-03-07 23:00:49 +0000406void SchedulePostRATDList::finishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000407 if (AntiDepBreak != NULL)
408 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000409
410 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000411 ScheduleDAGInstrs::finishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000412}
413
David Goodwin5e411782009-09-03 22:15:25 +0000414/// StartBlockForKills - Initialize register live-range state for updating kills
415///
416void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000417 // Start with no live registers.
418 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000419
420 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000421 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000422 // In a return block, examine the function live-out regs.
423 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
424 E = MRI.liveout_end(); I != E; ++I) {
425 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000426 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000427 // Repeat, for all subregs.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000428 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
429 LiveRegs.set(*SubRegs);
David Goodwin5e411782009-09-03 22:15:25 +0000430 }
431 }
432 else {
433 // In a non-return block, examine the live-in regs of all successors.
434 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
435 SE = BB->succ_end(); SI != SE; ++SI) {
436 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
437 E = (*SI)->livein_end(); I != E; ++I) {
438 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000439 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000440 // Repeat, for all subregs.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000441 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
442 LiveRegs.set(*SubRegs);
David Goodwin5e411782009-09-03 22:15:25 +0000443 }
444 }
445 }
446}
447
David Goodwin8f909342009-09-23 16:35:25 +0000448bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
449 MachineOperand &MO) {
450 // Setting kill flag...
451 if (!MO.isKill()) {
452 MO.setIsKill(true);
453 return false;
454 }
Jim Grosbach90013032010-05-14 21:19:48 +0000455
David Goodwin8f909342009-09-23 16:35:25 +0000456 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000457 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000458 MO.setIsKill(false);
459 return false;
460 }
461
462 // If any subreg of MO is live, then create an imp-def for that
463 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000464 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000465 bool AllDead = true;
466 const unsigned SuperReg = MO.getReg();
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000467 for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
468 if (LiveRegs.test(*SubRegs)) {
469 MI->addOperand(MachineOperand::CreateReg(*SubRegs,
David Goodwin8f909342009-09-23 16:35:25 +0000470 true /*IsDef*/,
471 true /*IsImp*/,
472 false /*IsKill*/,
473 false /*IsDead*/));
474 AllDead = false;
475 }
476 }
477
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000478 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000479 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000480 return false;
481}
482
David Goodwin88a589c2009-08-25 17:03:05 +0000483/// FixupKills - Fix the register kill flags, they may have been made
484/// incorrect by instruction reordering.
485///
486void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000487 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000488
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000489 BitVector killedRegs(TRI->getNumRegs());
David Goodwin5e411782009-09-03 22:15:25 +0000490
491 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000492
David Goodwin7886cd82009-08-29 00:11:13 +0000493 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000494 unsigned Count = MBB->size();
495 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
496 I != E; --Count) {
497 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000498 if (MI->isDebugValue())
499 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000500
David Goodwin7886cd82009-08-29 00:11:13 +0000501 // Update liveness. Registers that are defed but not used in this
502 // instruction are now dead. Mark register and all subregs as they
503 // are completely defined.
504 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
505 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000506 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000507 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000508 if (!MO.isReg()) continue;
509 unsigned Reg = MO.getReg();
510 if (Reg == 0) continue;
511 if (!MO.isDef()) continue;
512 // Ignore two-addr defs.
513 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000514
Benjamin Kramer46252d82012-02-23 19:15:40 +0000515 LiveRegs.reset(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000516
David Goodwin7886cd82009-08-29 00:11:13 +0000517 // Repeat for all subregs.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000518 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
519 LiveRegs.reset(*SubRegs);
David Goodwin7886cd82009-08-29 00:11:13 +0000520 }
David Goodwin88a589c2009-08-25 17:03:05 +0000521
David Goodwin8f909342009-09-23 16:35:25 +0000522 // Examine all used registers and set/clear kill flag. When a
523 // register is used multiple times we only set the kill flag on
524 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000525 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000526 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
527 MachineOperand &MO = MI->getOperand(i);
528 if (!MO.isReg() || !MO.isUse()) continue;
529 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000530 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000531
David Goodwin7886cd82009-08-29 00:11:13 +0000532 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000533 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000534 kill = true;
535 // A register is not killed if any subregs are live...
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000536 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
537 if (LiveRegs.test(*SubRegs)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000538 kill = false;
539 break;
540 }
541 }
542
543 // If subreg is not live, then register is killed if it became
544 // live in this instruction
545 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000546 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000547 }
Jim Grosbach90013032010-05-14 21:19:48 +0000548
David Goodwin88a589c2009-08-25 17:03:05 +0000549 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000550 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000551 // Warning: ToggleKillFlag may invalidate MO.
552 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000553 DEBUG(MI->dump());
554 }
Jim Grosbach90013032010-05-14 21:19:48 +0000555
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000556 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000557 }
Jim Grosbach90013032010-05-14 21:19:48 +0000558
David Goodwina3251db2009-08-31 20:47:02 +0000559 // Mark any used register (that is not using undef) and subregs as
560 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000561 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
562 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000563 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000564 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000565 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000566
Benjamin Kramer46252d82012-02-23 19:15:40 +0000567 LiveRegs.set(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000568
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000569 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
570 LiveRegs.set(*SubRegs);
David Goodwin7886cd82009-08-29 00:11:13 +0000571 }
David Goodwin88a589c2009-08-25 17:03:05 +0000572 }
573}
574
Dan Gohman343f0c02008-11-19 23:18:57 +0000575//===----------------------------------------------------------------------===//
576// Top-Down Scheduling
577//===----------------------------------------------------------------------===//
578
579/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickae692f22012-11-12 19:28:57 +0000580/// the PendingQueue if the count reaches zero.
David Goodwin557bbe62009-11-20 19:32:48 +0000581void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000582 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000583
Andrew Trickcf6b6132012-11-13 02:35:06 +0000584 if (SuccEdge->isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000585 --SuccSU->WeakPredsLeft;
586 return;
587 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000588#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000589 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000590 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000591 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000592 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000593 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000594 }
595#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000596 --SuccSU->NumPredsLeft;
597
Andrew Trick89fd4372011-05-06 18:14:32 +0000598 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000599 // here as such:
600 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
601 //
602 // However, we lazily compute node depth instead. Note that
603 // ScheduleNodeTopDown has already updated the depth of this node which causes
604 // all descendents to be marked dirty. Setting the successor depth explicitly
605 // here would cause depth to be recomputed for all its ancestors. If the
606 // successor is not yet ready (because of a transitively redundant edge) then
607 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000608
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000609 // If all the node's predecessors are scheduled, this node is ready
610 // to be scheduled. Ignore the special ExitSU node.
611 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000612 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000613}
614
615/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000616void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000617 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000618 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000619 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000620 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000621}
622
623/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
624/// count of its successors. If a successor pending count is zero, add it to
625/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000626void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000627 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000628 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000629
Dan Gohman343f0c02008-11-19 23:18:57 +0000630 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000631 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000632 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000633 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000634
David Goodwin557bbe62009-11-20 19:32:48 +0000635 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000636 SU->isScheduled = true;
Andrew Trick953be892012-03-07 23:00:49 +0000637 AvailableQueue.scheduledNode(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000638}
639
640/// ListScheduleTopDown - The main loop of list scheduling for top-down
641/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000642void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000643 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000644
David Goodwin4de099d2009-11-03 20:57:50 +0000645 // We're scheduling top-down but we're visiting the regions in
646 // bottom-up order, so we don't know the hazards at the start of a
647 // region. So assume no hazards (this should usually be ok as most
648 // blocks are a single region).
649 HazardRec->Reset();
650
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000651 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000652 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000653
David Goodwin557bbe62009-11-20 19:32:48 +0000654 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000655 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
656 // It is available if it has no predecessors.
Andrew Trickae692f22012-11-12 19:28:57 +0000657 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000658 AvailableQueue.push(&SUnits[i]);
659 SUnits[i].isAvailable = true;
660 }
661 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000662
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000663 // In any cycle where we can't schedule any instructions, we must
664 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000665 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000666
Dan Gohman343f0c02008-11-19 23:18:57 +0000667 // While Available queue is not empty, grab the node with the highest
668 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000669 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000670 Sequence.reserve(SUnits.size());
671 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
672 // Check to see if any of the pending instructions are ready to issue. If
673 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000674 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000675 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000676 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000677 AvailableQueue.push(PendingQueue[i]);
678 PendingQueue[i]->isAvailable = true;
679 PendingQueue[i] = PendingQueue.back();
680 PendingQueue.pop_back();
681 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000682 } else if (PendingQueue[i]->getDepth() < MinDepth)
683 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000684 }
David Goodwinc93d8372009-08-11 17:35:23 +0000685
Andrew Trick2da8bc82010-12-24 05:03:26 +0000686 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000687
Dan Gohman2836c282009-01-16 01:33:36 +0000688 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000689 bool HasNoopHazards = false;
690 while (!AvailableQueue.empty()) {
691 SUnit *CurSUnit = AvailableQueue.pop();
692
693 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000694 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000695 if (HT == ScheduleHazardRecognizer::NoHazard) {
696 FoundSUnit = CurSUnit;
697 break;
698 }
699
700 // Remember if this is a noop hazard.
701 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
702
703 NotReady.push_back(CurSUnit);
704 }
705
706 // Add the nodes that aren't ready back onto the available list.
707 if (!NotReady.empty()) {
708 AvailableQueue.push_all(NotReady);
709 NotReady.clear();
710 }
711
David Goodwin4de099d2009-11-03 20:57:50 +0000712 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000713 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000714 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000715 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000716 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000717 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000718 if (HazardRec->atIssueLimit()) {
719 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
720 HazardRec->AdvanceCycle();
721 ++CurCycle;
722 CycleHasInsts = false;
723 }
Dan Gohman2836c282009-01-16 01:33:36 +0000724 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000725 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000726 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000727 HazardRec->AdvanceCycle();
728 } else if (!HasNoopHazards) {
729 // Otherwise, we have a pipeline stall, but no other problem,
730 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000731 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000732 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000733 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000734 } else {
735 // Otherwise, we have no instructions to issue and we have instructions
736 // that will fault if we don't do this right. This is the case for
737 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000738 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000739 HazardRec->EmitNoop();
740 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000741 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000742 }
743
Dan Gohman2836c282009-01-16 01:33:36 +0000744 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000745 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000746 }
747 }
748
749#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000750 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
751 unsigned Noops = 0;
752 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
753 if (!Sequence[i])
754 ++Noops;
755 assert(Sequence.size() - Noops == ScheduledNodes &&
756 "The number of nodes scheduled doesn't match the expected number!");
757#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000758}
Andrew Trick84b454d2012-03-07 05:21:44 +0000759
760// EmitSchedule - Emit the machine code in scheduled order.
761void SchedulePostRATDList::EmitSchedule() {
Andrew Trick68675c62012-03-09 04:29:02 +0000762 RegionBegin = RegionEnd;
Andrew Trick84b454d2012-03-07 05:21:44 +0000763
764 // If first instruction was a DBG_VALUE then put it back.
765 if (FirstDbgValue)
Andrew Trick68675c62012-03-09 04:29:02 +0000766 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Trick84b454d2012-03-07 05:21:44 +0000767
768 // Then re-insert them according to the given schedule.
769 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
770 if (SUnit *SU = Sequence[i])
Andrew Trick68675c62012-03-09 04:29:02 +0000771 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Trick84b454d2012-03-07 05:21:44 +0000772 else
773 // Null SUnit* is a noop.
Andrew Trick68675c62012-03-09 04:29:02 +0000774 TII->insertNoop(*BB, RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000775
776 // Update the Begin iterator, as the first instruction in the block
777 // may have been scheduled later.
778 if (i == 0)
Andrew Trick68675c62012-03-09 04:29:02 +0000779 RegionBegin = prior(RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000780 }
781
782 // Reinsert any remaining debug_values.
783 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
784 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
785 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
786 MachineInstr *DbgValue = P.first;
787 MachineBasicBlock::iterator OrigPrivMI = P.second;
788 BB->splice(++OrigPrivMI, BB, DbgValue);
789 }
790 DbgValues.clear();
791 FirstDbgValue = NULL;
792}