blob: d8afbbbbe7d5556f675cb2e40e15ba08a6469b34 [file] [log] [blame]
Dale Johannesen4dc35db2007-07-13 17:31:29 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
Dale Johannesen2182f062007-07-13 17:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Johannesen2182f062007-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"
22#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "AggressiveAntiDepBreaker.h"
24#include "AntiDepBreaker.h"
25#include "CriticalAntiDepBreaker.h"
26#include "llvm/ADT/BitVector.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000029#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000030#include "llvm/CodeGen/MachineDominators.h"
David Goodwinbe3039e2009-10-01 19:45:32 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesenf623e982012-12-20 18:08:06 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000034#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanad2134d2008-11-25 00:52:40 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000036#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Trick9a0c5832012-03-07 23:01:06 +000037#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohmanceac7c32009-01-16 01:33:36 +000038#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/CodeGen/SchedulerRegistry.h"
David Goodwine056d102009-10-26 22:31:16 +000040#include "llvm/Support/CommandLine.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000041#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000042#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000043#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSubtargetInfo.h"
Dale Johannesen2182f062007-07-13 17:13:54 +000049using namespace llvm;
50
Dan Gohmanceac7c32009-01-16 01:33:36 +000051STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman60cb69e2008-11-19 23:18:57 +000052STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin83704852009-10-26 16:59:04 +000053STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman60cb69e2008-11-19 23:18:57 +000054
David Goodwin9a051a52009-10-01 21:46:35 +000055// Post-RA scheduling is enabled with
Evan Cheng0d639a22011-07-01 21:01:15 +000056// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin9a051a52009-10-01 21:46:35 +000057// override the target.
58static cl::opt<bool>
59EnablePostRAScheduler("post-RA-scheduler",
60 cl::desc("Enable scheduling after register allocation"),
David Goodwin1cc6dd92009-10-01 22:19:57 +000061 cl::init(false), cl::Hidden);
David Goodwin83704852009-10-26 16:59:04 +000062static cl::opt<std::string>
Dan Gohmanad2134d2008-11-25 00:52:40 +000063EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin83704852009-10-26 16:59:04 +000064 cl::desc("Break post-RA scheduling anti-dependencies: "
65 "\"critical\", \"all\", or \"none\""),
66 cl::init("none"), cl::Hidden);
Dan Gohmanceac7c32009-01-16 01:33:36 +000067
David Goodwin7f651692009-09-01 18:34:03 +000068// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
69static cl::opt<int>
70DebugDiv("postra-sched-debugdiv",
71 cl::desc("Debug control MBBs that are scheduled"),
72 cl::init(0), cl::Hidden);
73static cl::opt<int>
74DebugMod("postra-sched-debugmod",
75 cl::desc("Debug control MBBs that are scheduled"),
76 cl::init(0), cl::Hidden);
77
David Goodwin661ea982009-10-26 19:41:00 +000078AntiDepBreaker::~AntiDepBreaker() { }
79
Dale Johannesen2182f062007-07-13 17:13:54 +000080namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000081 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng2d51c7c2010-06-18 23:09:54 +000082 const TargetInstrInfo *TII;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000083 RegisterClassInfo RegClassInfo;
Dan Gohman87b02d52009-10-09 23:27:56 +000084
Dale Johannesen2182f062007-07-13 17:13:54 +000085 public:
86 static char ID;
Andrew Trickdf7e3762012-02-08 21:22:53 +000087 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohmanad2134d2008-11-25 00:52:40 +000088
Dan Gohmandddc1ac2008-12-16 03:25:46 +000089 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman04023152009-07-31 23:37:33 +000090 AU.setPreservesCFG();
Dan Gohman87b02d52009-10-09 23:27:56 +000091 AU.addRequired<AliasAnalysis>();
Andrew Trickdf7e3762012-02-08 21:22:53 +000092 AU.addRequired<TargetPassConfig>();
Dan Gohmandddc1ac2008-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 Johannesen2182f062007-07-13 17:13:54 +0000100 bool runOnMachineFunction(MachineFunction &Fn);
101 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000102 char PostRAScheduler::ID = 0;
103
Nick Lewycky02d5f772009-10-25 06:33:48 +0000104 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000105 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohman682a2d12009-10-21 01:44:44 +0000106 ///
Dan Gohman60cb69e2008-11-19 23:18:57 +0000107 LatencyPriorityQueue AvailableQueue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000108
Dan Gohman60cb69e2008-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 Gohmanceac7c32009-01-16 01:33:36 +0000115 /// HazardRec - The hazard recognizer to use.
116 ScheduleHazardRecognizer *HazardRec;
117
David Goodwin83704852009-10-26 16:59:04 +0000118 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
119 AntiDepBreaker *AntiDepBreak;
120
Dan Gohman87b02d52009-10-09 23:27:56 +0000121 /// AA - AliasAnalysis for making memory reference queries.
122 AliasAnalysis *AA;
123
Benjamin Kramer796fd462012-02-23 19:15:40 +0000124 /// LiveRegs - true if the register is live.
125 BitVector LiveRegs;
Dan Gohmanb9543432009-02-10 23:27:53 +0000126
Andrew Trick60cf03e2012-03-07 05:21:52 +0000127 /// The schedule. Null SUnit*'s represent noop instructions.
128 std::vector<SUnit*> Sequence;
129
Andrew Tricka53e1012013-08-23 17:48:33 +0000130 /// The index in BB of RegionEnd.
131 ///
132 /// This is the instruction number from the top of the current block, not
133 /// the SlotIndex. It is only used by the AntiDepBreaker.
134 unsigned EndIndex;
135
Dan Gohmanad2134d2008-11-25 00:52:40 +0000136 public:
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000137 SchedulePostRATDList(
138 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000139 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng0d639a22011-07-01 21:01:15 +0000140 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper760b1342012-02-22 05:59:10 +0000141 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000142
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000143 ~SchedulePostRATDList();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000144
Andrew Trick52226d42012-03-07 23:00:49 +0000145 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohmanb9543432009-02-10 23:27:53 +0000146 /// this block.
147 ///
Andrew Trick52226d42012-03-07 23:00:49 +0000148 void startBlock(MachineBasicBlock *BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000149
Andrew Tricka53e1012013-08-23 17:48:33 +0000150 // Set the index of RegionEnd within the current BB.
151 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
152
Andrew Trick60cf03e2012-03-07 05:21:52 +0000153 /// Initialize the scheduler state for the next scheduling region.
154 virtual void enterRegion(MachineBasicBlock *bb,
155 MachineBasicBlock::iterator begin,
156 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000157 unsigned regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000158
159 /// Notify that the scheduler has finished scheduling the current region.
160 virtual void exitRegion();
161
Dan Gohmanb9543432009-02-10 23:27:53 +0000162 /// Schedule - Schedule the instruction range using list scheduling.
163 ///
Andrew Trick52226d42012-03-07 23:00:49 +0000164 void schedule();
Jim Grosbachd772bde2010-05-14 21:19:48 +0000165
Andrew Tricke932bb72012-03-07 05:21:44 +0000166 void EmitSchedule();
167
Dan Gohman682a2d12009-10-21 01:44:44 +0000168 /// Observe - Update liveness information to account for the current
169 /// instruction, which will not be scheduled.
170 ///
171 void Observe(MachineInstr *MI, unsigned Count);
172
Andrew Trick52226d42012-03-07 23:00:49 +0000173 /// finishBlock - Clean up register live-range state.
Dan Gohman682a2d12009-10-21 01:44:44 +0000174 ///
Andrew Trick52226d42012-03-07 23:00:49 +0000175 void finishBlock();
Dan Gohman682a2d12009-10-21 01:44:44 +0000176
David Goodwin83704852009-10-26 16:59:04 +0000177 /// FixupKills - Fix register kill flags that have been made
178 /// invalid due to scheduling
179 ///
180 void FixupKills(MachineBasicBlock *MBB);
181
Dan Gohman60cb69e2008-11-19 23:18:57 +0000182 private:
David Goodwin80a03cc2009-11-20 19:32:48 +0000183 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
184 void ReleaseSuccessors(SUnit *SU);
185 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
186 void ListScheduleTopDown();
David Goodwin6c08cfc2009-09-03 22:15:25 +0000187 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000188
David Goodwina4c98a32009-09-23 16:35:25 +0000189 // ToggleKillFlag - Toggle a register operand kill flag. Other
190 // adjustments may be made to the instruction if necessary. Return
191 // true if the operand has been deleted, false if not.
192 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Andrew Trickedee68c2012-03-07 05:21:40 +0000193
194 void dumpSchedule() const;
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000195 void emitNoop(unsigned CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000196 };
Dale Johannesen2182f062007-07-13 17:13:54 +0000197}
198
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000199char &llvm::PostRASchedulerID = PostRAScheduler::ID;
200
201INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
202 "Post RA top-down list latency scheduler", false, false)
203
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000204SchedulePostRATDList::SchedulePostRATDList(
205 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000206 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng0d639a22011-07-01 21:01:15 +0000207 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper760b1342012-02-22 05:59:10 +0000208 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000209 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA),
Andrew Tricka53e1012013-08-23 17:48:33 +0000210 LiveRegs(TRI->getNumRegs()), EndIndex(0)
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000211{
212 const TargetMachine &TM = MF.getTarget();
213 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
214 HazardRec =
215 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
Preston Gurd9a091472012-04-23 21:39:35 +0000216
217 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
218 MRI.tracksLiveness()) &&
219 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000220 AntiDepBreak =
Evan Cheng0d639a22011-07-01 21:01:15 +0000221 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000222 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng0d639a22011-07-01 21:01:15 +0000223 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000224 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000225}
226
227SchedulePostRATDList::~SchedulePostRATDList() {
228 delete HazardRec;
229 delete AntiDepBreak;
230}
231
Andrew Trick60cf03e2012-03-07 05:21:52 +0000232/// Initialize state associated with the next scheduling region.
233void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
234 MachineBasicBlock::iterator begin,
235 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000236 unsigned regioninstrs) {
237 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick60cf03e2012-03-07 05:21:52 +0000238 Sequence.clear();
239}
240
241/// Print the schedule before exiting the region.
242void SchedulePostRATDList::exitRegion() {
243 DEBUG({
244 dbgs() << "*** Final schedule ***\n";
245 dumpSchedule();
246 dbgs() << '\n';
247 });
248 ScheduleDAGInstrs::exitRegion();
249}
250
Manman Ren19f49ac2012-09-11 22:23:19 +0000251#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trickedee68c2012-03-07 05:21:40 +0000252/// dumpSchedule - dump the scheduled Sequence.
253void SchedulePostRATDList::dumpSchedule() const {
254 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
255 if (SUnit *SU = Sequence[i])
256 SU->dump(this);
257 else
258 dbgs() << "**** NOOP ****\n";
259 }
260}
Manman Ren742534c2012-09-06 19:06:06 +0000261#endif
Andrew Trickedee68c2012-03-07 05:21:40 +0000262
Dan Gohman60cb69e2008-11-19 23:18:57 +0000263bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng2d51c7c2010-06-18 23:09:54 +0000264 TII = Fn.getTarget().getInstrInfo();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000265 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
266 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
267 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000268 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
269
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000270 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman26e9b892009-10-10 00:15:38 +0000271
David Goodwin9a051a52009-10-01 21:46:35 +0000272 // Check for explicit enable/disable of post-ra scheduling.
Evan Cheng7fae11b2011-12-14 02:11:42 +0000273 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
274 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper760b1342012-02-22 05:59:10 +0000275 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin9a051a52009-10-01 21:46:35 +0000276 if (EnablePostRAScheduler.getPosition() > 0) {
277 if (!EnablePostRAScheduler)
Evan Cheng8b614762009-10-16 06:10:34 +0000278 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000279 } else {
Evan Cheng8b614762009-10-16 06:10:34 +0000280 // Check that post-RA scheduling is enabled for this target.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000281 // This may upgrade the AntiDepMode.
Evan Cheng0d639a22011-07-01 21:01:15 +0000282 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickdf7e3762012-02-08 21:22:53 +0000283 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
284 CriticalPathRCs))
Evan Cheng8b614762009-10-16 06:10:34 +0000285 return false;
David Goodwin9a051a52009-10-01 21:46:35 +0000286 }
David Goodwin17199b52009-09-30 00:10:16 +0000287
David Goodwin02ad4cb2009-10-22 23:19:17 +0000288 // Check for antidep breaking override...
289 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng0d639a22011-07-01 21:01:15 +0000290 AntiDepMode = (EnableAntiDepBreaking == "all")
291 ? TargetSubtargetInfo::ANTIDEP_ALL
292 : ((EnableAntiDepBreaking == "critical")
293 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
294 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin02ad4cb2009-10-22 23:19:17 +0000295 }
296
David Greeneaa8ce382010-01-05 01:26:01 +0000297 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesen2182f062007-07-13 17:13:54 +0000298
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000299 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000300 CriticalPathRCs);
Dan Gohman619ef482009-01-15 19:20:50 +0000301
Dale Johannesen2182f062007-07-13 17:13:54 +0000302 // Loop over all of the basic blocks
303 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000304 MBB != MBBe; ++MBB) {
David Goodwin7f651692009-09-01 18:34:03 +0000305#ifndef NDEBUG
306 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
307 if (DebugDiv > 0) {
308 static int bbcnt = 0;
309 if (bbcnt++ % DebugDiv != DebugMod)
310 continue;
Craig Toppera538d832012-08-22 06:07:19 +0000311 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000312 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin7f651692009-09-01 18:34:03 +0000313 }
314#endif
315
Dan Gohmanb9543432009-02-10 23:27:53 +0000316 // Initialize register live-range state for scheduling in this block.
Andrew Trick52226d42012-03-07 23:00:49 +0000317 Scheduler.startBlock(MBB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000318
Dan Gohman5f8a2592009-01-16 22:10:20 +0000319 // Schedule each sequence of instructions not interrupted by a label
320 // or anything else that effectively needs to shut down scheduling.
Dan Gohmanb9543432009-02-10 23:27:53 +0000321 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohmandfaf6462009-02-11 04:27:20 +0000322 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohmanb9543432009-02-10 23:27:53 +0000323 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng2d51c7c2010-06-18 23:09:54 +0000324 MachineInstr *MI = llvm::prior(I);
Andrew Tricka53e1012013-08-23 17:48:33 +0000325 --Count;
Jakob Stoklund Olesena793a592012-02-23 17:54:21 +0000326 // Calls are not scheduling boundaries before register allocation, but
327 // post-ra we don't gain anything by scheduling across calls since we
328 // don't need to worry about register pressure.
329 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Tricka53e1012013-08-23 17:48:33 +0000330 Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count);
331 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000332 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000333 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000334 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000335 Current = MI;
Andrew Tricka53e1012013-08-23 17:48:33 +0000336 CurrentCount = Count;
Dan Gohman64613ac2009-03-10 18:10:43 +0000337 Scheduler.Observe(MI, CurrentCount);
Dan Gohman5f8a2592009-01-16 22:10:20 +0000338 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000339 I = MI;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000340 if (MI->isBundle())
341 Count -= MI->getBundleSize();
Dan Gohmand5643532009-02-03 18:57:45 +0000342 }
Dan Gohmandfaf6462009-02-11 04:27:20 +0000343 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sandsbe69d602009-03-11 09:04:34 +0000344 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman64613ac2009-03-10 18:10:43 +0000345 "Instruction count mismatch!");
Andrew Trick60cf03e2012-03-07 05:21:52 +0000346 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Tricka53e1012013-08-23 17:48:33 +0000347 Scheduler.setEndIndex(CurrentCount);
Andrew Trick52226d42012-03-07 23:00:49 +0000348 Scheduler.schedule();
Andrew Trick60cf03e2012-03-07 05:21:52 +0000349 Scheduler.exitRegion();
Dan Gohman25c16532010-05-01 00:01:06 +0000350 Scheduler.EmitSchedule();
Dan Gohmanb9543432009-02-10 23:27:53 +0000351
352 // Clean up register live-range state.
Andrew Trick52226d42012-03-07 23:00:49 +0000353 Scheduler.finishBlock();
David Goodwinae6bc822009-08-25 17:03:05 +0000354
David Goodwin6c08cfc2009-09-03 22:15:25 +0000355 // Update register kills
David Goodwinae6bc822009-08-25 17:03:05 +0000356 Scheduler.FixupKills(MBB);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000357 }
Dale Johannesen2182f062007-07-13 17:13:54 +0000358
359 return true;
360}
Jim Grosbachd772bde2010-05-14 21:19:48 +0000361
Dan Gohmanb9543432009-02-10 23:27:53 +0000362/// StartBlock - Initialize register live-range state for scheduling in
363/// this block.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000364///
Andrew Trick52226d42012-03-07 23:00:49 +0000365void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000366 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000367 ScheduleDAGInstrs::startBlock(BB);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000368
David Goodwin83704852009-10-26 16:59:04 +0000369 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin6021b4d2009-08-10 15:55:25 +0000370 HazardRec->Reset();
David Goodwin83704852009-10-26 16:59:04 +0000371 if (AntiDepBreak != NULL)
372 AntiDepBreak->StartBlock(BB);
Dan Gohmanb9543432009-02-10 23:27:53 +0000373}
374
375/// Schedule - Schedule the instruction range using list scheduling.
376///
Andrew Trick52226d42012-03-07 23:00:49 +0000377void SchedulePostRATDList::schedule() {
Dan Gohmanb9543432009-02-10 23:27:53 +0000378 // Build the scheduling graph.
Andrew Trick52226d42012-03-07 23:00:49 +0000379 buildSchedGraph(AA);
Dan Gohmanb9543432009-02-10 23:27:53 +0000380
David Goodwin83704852009-10-26 16:59:04 +0000381 if (AntiDepBreak != NULL) {
Jim Grosbachd772bde2010-05-14 21:19:48 +0000382 unsigned Broken =
Andrew Trick8c207e42012-03-09 04:29:02 +0000383 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
384 EndIndex, DbgValues);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000385
David Goodwin80a03cc2009-11-20 19:32:48 +0000386 if (Broken != 0) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000387 // We made changes. Update the dependency graph.
388 // Theoretically we could update the graph in place:
389 // When a live range is changed to use a different register, remove
390 // the def's anti-dependence *and* output-dependence edges due to
391 // that register, and add new anti-dependence and output-dependence
392 // edges based on the next live range of the register.
Andrew Trick60cf03e2012-03-07 05:21:52 +0000393 ScheduleDAG::clearDAG();
Andrew Trick52226d42012-03-07 23:00:49 +0000394 buildSchedGraph(AA);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000395
David Goodwin83704852009-10-26 16:59:04 +0000396 NumFixedAnti += Broken;
Dan Gohmanb9543432009-02-10 23:27:53 +0000397 }
398 }
399
David Greeneaa8ce382010-01-05 01:26:01 +0000400 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwin6021b4d2009-08-10 15:55:25 +0000401 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
402 SUnits[su].dumpAll(this));
403
Dan Gohmanb9543432009-02-10 23:27:53 +0000404 AvailableQueue.initNodes(SUnits);
David Goodwin80a03cc2009-11-20 19:32:48 +0000405 ListScheduleTopDown();
Dan Gohmanb9543432009-02-10 23:27:53 +0000406 AvailableQueue.releaseState();
407}
408
409/// Observe - Update liveness information to account for the current
410/// instruction, which will not be scheduled.
411///
Dan Gohmandfaf6462009-02-11 04:27:20 +0000412void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin83704852009-10-26 16:59:04 +0000413 if (AntiDepBreak != NULL)
Andrew Tricka316faa2012-03-07 23:00:52 +0000414 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohmanb9543432009-02-10 23:27:53 +0000415}
416
417/// FinishBlock - Clean up register live-range state.
418///
Andrew Trick52226d42012-03-07 23:00:49 +0000419void SchedulePostRATDList::finishBlock() {
David Goodwin83704852009-10-26 16:59:04 +0000420 if (AntiDepBreak != NULL)
421 AntiDepBreak->FinishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000422
423 // Call the superclass.
Andrew Trick52226d42012-03-07 23:00:49 +0000424 ScheduleDAGInstrs::finishBlock();
Dan Gohmanb9543432009-02-10 23:27:53 +0000425}
426
David Goodwin6c08cfc2009-09-03 22:15:25 +0000427/// StartBlockForKills - Initialize register live-range state for updating kills
428///
429void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer796fd462012-02-23 19:15:40 +0000430 // Start with no live registers.
431 LiveRegs.reset();
David Goodwin6c08cfc2009-09-03 22:15:25 +0000432
Jakob Stoklund Olesenc3386792013-02-05 18:21:52 +0000433 // 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) {
David Goodwin6c08cfc2009-09-03 22:15:25 +0000438 unsigned Reg = *I;
Chad Rosierabdb1d62013-05-22 23:17:36 +0000439 // Repeat, for reg and all subregs.
440 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
441 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000442 LiveRegs.set(*SubRegs);
David Goodwin6c08cfc2009-09-03 22:15:25 +0000443 }
444 }
David Goodwin6c08cfc2009-09-03 22:15:25 +0000445}
446
David Goodwina4c98a32009-09-23 16:35:25 +0000447bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
448 MachineOperand &MO) {
449 // Setting kill flag...
450 if (!MO.isKill()) {
451 MO.setIsKill(true);
452 return false;
453 }
Jim Grosbachd772bde2010-05-14 21:19:48 +0000454
David Goodwina4c98a32009-09-23 16:35:25 +0000455 // If MO itself is live, clear the kill flag...
Benjamin Kramer796fd462012-02-23 19:15:40 +0000456 if (LiveRegs.test(MO.getReg())) {
David Goodwina4c98a32009-09-23 16:35:25 +0000457 MO.setIsKill(false);
458 return false;
459 }
460
461 // If any subreg of MO is live, then create an imp-def for that
462 // subreg and keep MO marked as killed.
Benjamin Kramer3b008a32009-10-02 15:59:52 +0000463 MO.setIsKill(false);
David Goodwina4c98a32009-09-23 16:35:25 +0000464 bool AllDead = true;
465 const unsigned SuperReg = MO.getReg();
Jakob Stoklund Olesenf623e982012-12-20 18:08:06 +0000466 MachineInstrBuilder MIB(MF, MI);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000467 for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
468 if (LiveRegs.test(*SubRegs)) {
Jakob Stoklund Olesenf623e982012-12-20 18:08:06 +0000469 MIB.addReg(*SubRegs, RegState::ImplicitDefine);
David Goodwina4c98a32009-09-23 16:35:25 +0000470 AllDead = false;
471 }
472 }
473
Dan Gohman682a2d12009-10-21 01:44:44 +0000474 if(AllDead)
Benjamin Kramer3b008a32009-10-02 15:59:52 +0000475 MO.setIsKill(true);
David Goodwina4c98a32009-09-23 16:35:25 +0000476 return false;
477}
478
David Goodwinae6bc822009-08-25 17:03:05 +0000479/// FixupKills - Fix the register kill flags, they may have been made
480/// incorrect by instruction reordering.
481///
482void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greeneaa8ce382010-01-05 01:26:01 +0000483 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwinae6bc822009-08-25 17:03:05 +0000484
Benjamin Kramer21974b12012-02-23 18:28:32 +0000485 BitVector killedRegs(TRI->getNumRegs());
David Goodwin6c08cfc2009-09-03 22:15:25 +0000486
487 StartBlockForKills(MBB);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000488
David Goodwin7cb103d2009-08-29 00:11:13 +0000489 // Examine block from end to start...
David Goodwinae6bc822009-08-25 17:03:05 +0000490 unsigned Count = MBB->size();
491 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
492 I != E; --Count) {
493 MachineInstr *MI = --I;
Dale Johannesen2061c842010-03-05 00:02:59 +0000494 if (MI->isDebugValue())
495 continue;
David Goodwinae6bc822009-08-25 17:03:05 +0000496
David Goodwin7cb103d2009-08-29 00:11:13 +0000497 // Update liveness. Registers that are defed but not used in this
498 // instruction are now dead. Mark register and all subregs as they
499 // are completely defined.
500 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
501 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen28d48032012-02-23 01:22:15 +0000502 if (MO.isRegMask())
Benjamin Krameref8bf392012-02-23 19:29:25 +0000503 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7cb103d2009-08-29 00:11:13 +0000504 if (!MO.isReg()) continue;
505 unsigned Reg = MO.getReg();
506 if (Reg == 0) continue;
507 if (!MO.isDef()) continue;
508 // Ignore two-addr defs.
509 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000510
Chad Rosierabdb1d62013-05-22 23:17:36 +0000511 // Repeat for reg and all subregs.
512 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
513 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000514 LiveRegs.reset(*SubRegs);
David Goodwin7cb103d2009-08-29 00:11:13 +0000515 }
David Goodwinae6bc822009-08-25 17:03:05 +0000516
David Goodwina4c98a32009-09-23 16:35:25 +0000517 // Examine all used registers and set/clear kill flag. When a
518 // register is used multiple times we only set the kill flag on
Andrew Trick811a2ef2013-10-16 18:30:23 +0000519 // the first use. Don't set kill flags on undef operands.
Benjamin Kramer21974b12012-02-23 18:28:32 +0000520 killedRegs.reset();
David Goodwinae6bc822009-08-25 17:03:05 +0000521 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
522 MachineOperand &MO = MI->getOperand(i);
Andrew Trick811a2ef2013-10-16 18:30:23 +0000523 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwinae6bc822009-08-25 17:03:05 +0000524 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000525 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwinae6bc822009-08-25 17:03:05 +0000526
David Goodwin7cb103d2009-08-29 00:11:13 +0000527 bool kill = false;
Benjamin Kramer21974b12012-02-23 18:28:32 +0000528 if (!killedRegs.test(Reg)) {
David Goodwin7cb103d2009-08-29 00:11:13 +0000529 kill = true;
530 // A register is not killed if any subregs are live...
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000531 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
532 if (LiveRegs.test(*SubRegs)) {
David Goodwin7cb103d2009-08-29 00:11:13 +0000533 kill = false;
534 break;
535 }
536 }
537
538 // If subreg is not live, then register is killed if it became
539 // live in this instruction
540 if (kill)
Benjamin Kramer796fd462012-02-23 19:15:40 +0000541 kill = !LiveRegs.test(Reg);
David Goodwin7cb103d2009-08-29 00:11:13 +0000542 }
Jim Grosbachd772bde2010-05-14 21:19:48 +0000543
David Goodwinae6bc822009-08-25 17:03:05 +0000544 if (MO.isKill() != kill) {
David Greeneaa8ce382010-01-05 01:26:01 +0000545 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen83924562009-12-03 01:49:56 +0000546 // Warning: ToggleKillFlag may invalidate MO.
547 ToggleKillFlag(MI, MO);
David Goodwinae6bc822009-08-25 17:03:05 +0000548 DEBUG(MI->dump());
549 }
Jim Grosbachd772bde2010-05-14 21:19:48 +0000550
Benjamin Kramer21974b12012-02-23 18:28:32 +0000551 killedRegs.set(Reg);
David Goodwinae6bc822009-08-25 17:03:05 +0000552 }
Jim Grosbachd772bde2010-05-14 21:19:48 +0000553
David Goodwinc8985202009-08-31 20:47:02 +0000554 // Mark any used register (that is not using undef) and subregs as
555 // now live...
David Goodwin7cb103d2009-08-29 00:11:13 +0000556 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
557 MachineOperand &MO = MI->getOperand(i);
David Goodwinc8985202009-08-31 20:47:02 +0000558 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7cb103d2009-08-29 00:11:13 +0000559 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000560 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin7cb103d2009-08-29 00:11:13 +0000561
Chad Rosierabdb1d62013-05-22 23:17:36 +0000562 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
563 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000564 LiveRegs.set(*SubRegs);
David Goodwin7cb103d2009-08-29 00:11:13 +0000565 }
David Goodwinae6bc822009-08-25 17:03:05 +0000566 }
567}
568
Dan Gohman60cb69e2008-11-19 23:18:57 +0000569//===----------------------------------------------------------------------===//
570// Top-Down Scheduling
571//===----------------------------------------------------------------------===//
572
573/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000574/// the PendingQueue if the count reaches zero.
David Goodwin80a03cc2009-11-20 19:32:48 +0000575void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman2d170892008-12-09 22:54:47 +0000576 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000577
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000578 if (SuccEdge->isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000579 --SuccSU->WeakPredsLeft;
580 return;
581 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000582#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000583 if (SuccSU->NumPredsLeft == 0) {
David Greeneaa8ce382010-01-05 01:26:01 +0000584 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000585 SuccSU->dump(this);
David Greeneaa8ce382010-01-05 01:26:01 +0000586 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000587 llvm_unreachable(0);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000588 }
589#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000590 --SuccSU->NumPredsLeft;
591
Andrew Trick84f9ad92011-05-06 18:14:32 +0000592 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trickaab77fe2011-05-06 17:09:08 +0000593 // here as such:
594 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
595 //
596 // However, we lazily compute node depth instead. Note that
597 // ScheduleNodeTopDown has already updated the depth of this node which causes
598 // all descendents to be marked dirty. Setting the successor depth explicitly
599 // here would cause depth to be recomputed for all its ancestors. If the
600 // successor is not yet ready (because of a transitively redundant edge) then
601 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbachd772bde2010-05-14 21:19:48 +0000602
Dan Gohmanb9543432009-02-10 23:27:53 +0000603 // If all the node's predecessors are scheduled, this node is ready
604 // to be scheduled. Ignore the special ExitSU node.
605 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman60cb69e2008-11-19 23:18:57 +0000606 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000607}
608
609/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin80a03cc2009-11-20 19:32:48 +0000610void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmanb9543432009-02-10 23:27:53 +0000611 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +0000612 I != E; ++I) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000613 ReleaseSucc(SU, &*I);
David Goodwin8501dbbe2009-11-03 20:57:50 +0000614 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000615}
616
617/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
618/// count of its successors. If a successor pending count is zero, add it to
619/// the Available queue.
David Goodwin80a03cc2009-11-20 19:32:48 +0000620void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greeneaa8ce382010-01-05 01:26:01 +0000621 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000622 DEBUG(SU->dump(this));
Jim Grosbachd772bde2010-05-14 21:19:48 +0000623
Dan Gohman60cb69e2008-11-19 23:18:57 +0000624 Sequence.push_back(SU);
Jim Grosbachd772bde2010-05-14 21:19:48 +0000625 assert(CurCycle >= SU->getDepth() &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000626 "Node scheduled above its depth!");
David Goodwin80a03cc2009-11-20 19:32:48 +0000627 SU->setDepthToAtLeast(CurCycle);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000628
David Goodwin80a03cc2009-11-20 19:32:48 +0000629 ReleaseSuccessors(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000630 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000631 AvailableQueue.scheduledNode(SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000632}
633
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000634/// emitNoop - Add a noop to the current instruction sequence.
635void SchedulePostRATDList::emitNoop(unsigned CurCycle) {
636 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
637 HazardRec->EmitNoop();
638 Sequence.push_back(0); // NULL here means noop
639 ++NumNoops;
640}
641
Dan Gohman60cb69e2008-11-19 23:18:57 +0000642/// ListScheduleTopDown - The main loop of list scheduling for top-down
643/// schedulers.
David Goodwin80a03cc2009-11-20 19:32:48 +0000644void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000645 unsigned CurCycle = 0;
Jim Grosbachd772bde2010-05-14 21:19:48 +0000646
David Goodwin8501dbbe2009-11-03 20:57:50 +0000647 // We're scheduling top-down but we're visiting the regions in
648 // bottom-up order, so we don't know the hazards at the start of a
649 // region. So assume no hazards (this should usually be ok as most
650 // blocks are a single region).
651 HazardRec->Reset();
652
Dan Gohmanb9543432009-02-10 23:27:53 +0000653 // Release any successors of the special Entry node.
David Goodwin80a03cc2009-11-20 19:32:48 +0000654 ReleaseSuccessors(&EntrySU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000655
David Goodwin80a03cc2009-11-20 19:32:48 +0000656 // Add all leaves to Available queue.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000657 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
658 // It is available if it has no predecessors.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000659 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000660 AvailableQueue.push(&SUnits[i]);
661 SUnits[i].isAvailable = true;
662 }
663 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000664
David Goodwin1f8c7a72009-08-12 21:47:46 +0000665 // In any cycle where we can't schedule any instructions, we must
666 // stall or emit a noop, depending on the target.
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000667 bool CycleHasInsts = false;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000668
Dan Gohman60cb69e2008-11-19 23:18:57 +0000669 // While Available queue is not empty, grab the node with the highest
670 // priority. If it is not ready put it back. Schedule the node.
Dan Gohmanceac7c32009-01-16 01:33:36 +0000671 std::vector<SUnit*> NotReady;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000672 Sequence.reserve(SUnits.size());
673 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
674 // Check to see if any of the pending instructions are ready to issue. If
675 // so, add them to the available queue.
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000676 unsigned MinDepth = ~0u;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000677 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000678 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000679 AvailableQueue.push(PendingQueue[i]);
680 PendingQueue[i]->isAvailable = true;
681 PendingQueue[i] = PendingQueue.back();
682 PendingQueue.pop_back();
683 --i; --e;
David Goodwin80a03cc2009-11-20 19:32:48 +0000684 } else if (PendingQueue[i]->getDepth() < MinDepth)
685 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000686 }
David Goodwinebd694b2009-08-11 17:35:23 +0000687
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000688 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinebd694b2009-08-11 17:35:23 +0000689
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000690 SUnit *FoundSUnit = 0, *NotPreferredSUnit = 0;
Dan Gohmanceac7c32009-01-16 01:33:36 +0000691 bool HasNoopHazards = false;
692 while (!AvailableQueue.empty()) {
693 SUnit *CurSUnit = AvailableQueue.pop();
694
695 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000696 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000697 if (HT == ScheduleHazardRecognizer::NoHazard) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000698 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
699 if (!NotPreferredSUnit) {
700 // If this is the first non-preferred node for this cycle, then
701 // record it and continue searching for a preferred node. If this
702 // is not the first non-preferred node, then treat it as though
703 // there had been a hazard.
704 NotPreferredSUnit = CurSUnit;
705 continue;
706 }
707 } else {
708 FoundSUnit = CurSUnit;
709 break;
710 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000711 }
712
713 // Remember if this is a noop hazard.
714 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
715
716 NotReady.push_back(CurSUnit);
717 }
718
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000719 // If we have a non-preferred node, push it back onto the available list.
720 // If we did not find a preferred node, then schedule this first
721 // non-preferred node.
722 if (NotPreferredSUnit) {
723 if (!FoundSUnit) {
724 DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n");
725 FoundSUnit = NotPreferredSUnit;
726 } else {
727 AvailableQueue.push(NotPreferredSUnit);
728 }
729
730 NotPreferredSUnit = 0;
731 }
732
Dan Gohmanceac7c32009-01-16 01:33:36 +0000733 // Add the nodes that aren't ready back onto the available list.
734 if (!NotReady.empty()) {
735 AvailableQueue.push_all(NotReady);
736 NotReady.clear();
737 }
738
David Goodwin8501dbbe2009-11-03 20:57:50 +0000739 // If we found a node to schedule...
Dan Gohman60cb69e2008-11-19 23:18:57 +0000740 if (FoundSUnit) {
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000741 // If we need to emit noops prior to this instruction, then do so.
742 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
743 for (unsigned i = 0; i != NumPreNoops; ++i)
744 emitNoop(CurCycle);
745
David Goodwin8501dbbe2009-11-03 20:57:50 +0000746 // ... schedule the node...
David Goodwin80a03cc2009-11-20 19:32:48 +0000747 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohmanceac7c32009-01-16 01:33:36 +0000748 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000749 CycleHasInsts = true;
Andrew Trick18c9b372011-06-01 03:27:56 +0000750 if (HazardRec->atIssueLimit()) {
751 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
752 HazardRec->AdvanceCycle();
753 ++CurCycle;
754 CycleHasInsts = false;
755 }
Dan Gohmanceac7c32009-01-16 01:33:36 +0000756 } else {
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000757 if (CycleHasInsts) {
David Greeneaa8ce382010-01-05 01:26:01 +0000758 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000759 HazardRec->AdvanceCycle();
760 } else if (!HasNoopHazards) {
761 // Otherwise, we have a pipeline stall, but no other problem,
762 // just advance the current cycle and try again.
David Greeneaa8ce382010-01-05 01:26:01 +0000763 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin1f8c7a72009-08-12 21:47:46 +0000764 HazardRec->AdvanceCycle();
David Goodwin80a03cc2009-11-20 19:32:48 +0000765 ++NumStalls;
David Goodwin1f8c7a72009-08-12 21:47:46 +0000766 } else {
767 // Otherwise, we have no instructions to issue and we have instructions
768 // that will fault if we don't do this right. This is the case for
769 // processors without pipeline interlocks and other cases.
Hal Finkel4fd3b1d2013-12-11 22:33:43 +0000770 emitNoop(CurCycle);
David Goodwin1f8c7a72009-08-12 21:47:46 +0000771 }
772
Dan Gohmanceac7c32009-01-16 01:33:36 +0000773 ++CurCycle;
Benjamin Kramere3c9d232009-09-06 12:10:17 +0000774 CycleHasInsts = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000775 }
776 }
777
778#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000779 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
780 unsigned Noops = 0;
781 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
782 if (!Sequence[i])
783 ++Noops;
784 assert(Sequence.size() - Noops == ScheduledNodes &&
785 "The number of nodes scheduled doesn't match the expected number!");
786#endif // NDEBUG
Dan Gohman60cb69e2008-11-19 23:18:57 +0000787}
Andrew Tricke932bb72012-03-07 05:21:44 +0000788
789// EmitSchedule - Emit the machine code in scheduled order.
790void SchedulePostRATDList::EmitSchedule() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000791 RegionBegin = RegionEnd;
Andrew Tricke932bb72012-03-07 05:21:44 +0000792
793 // If first instruction was a DBG_VALUE then put it back.
794 if (FirstDbgValue)
Andrew Trick8c207e42012-03-09 04:29:02 +0000795 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Tricke932bb72012-03-07 05:21:44 +0000796
797 // Then re-insert them according to the given schedule.
798 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
799 if (SUnit *SU = Sequence[i])
Andrew Trick8c207e42012-03-09 04:29:02 +0000800 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Tricke932bb72012-03-07 05:21:44 +0000801 else
802 // Null SUnit* is a noop.
Andrew Trick8c207e42012-03-09 04:29:02 +0000803 TII->insertNoop(*BB, RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000804
805 // Update the Begin iterator, as the first instruction in the block
806 // may have been scheduled later.
807 if (i == 0)
Andrew Trick8c207e42012-03-09 04:29:02 +0000808 RegionBegin = prior(RegionEnd);
Andrew Tricke932bb72012-03-07 05:21:44 +0000809 }
810
811 // Reinsert any remaining debug_values.
812 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
813 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
814 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
815 MachineInstr *DbgValue = P.first;
816 MachineBasicBlock::iterator OrigPrivMI = P.second;
817 BB->splice(++OrigPrivMI, BB, DbgValue);
818 }
819 DbgValues.clear();
820 FirstDbgValue = NULL;
821}