blob: b8747b9330be35486c850597724fc2bdd44bfcf7 [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"
22#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-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 Gohman343f0c02008-11-19 23:18:57 +000029#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohman3f237442008-12-16 03:25:46 +000030#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman3f237442008-12-16 03:25:46 +000034#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick15252602012-06-06 20:29:31 +000036#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Tricked395c82012-03-07 23:01:06 +000037#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohman2836c282009-01-16 01:33:36 +000038#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000039#include "llvm/CodeGen/SchedulerRegistry.h"
David Goodwine10deca2009-10-26 22:31:16 +000040#include "llvm/Support/CommandLine.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000041#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000042#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000043#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-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 Johannesene7e7d0d2007-07-13 17:13:54 +000049using namespace llvm;
50
Dan Gohman2836c282009-01-16 01:33:36 +000051STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000052STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin2e7be612009-10-26 16:59:04 +000053STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman343f0c02008-11-19 23:18:57 +000054
David Goodwin471850a2009-10-01 21:46:35 +000055// Post-RA scheduling is enabled with
Evan Cheng5b1b44892011-07-01 21:01:15 +000056// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
David Goodwin471850a2009-10-01 21:46:35 +000057// override the target.
58static cl::opt<bool>
59EnablePostRAScheduler("post-RA-scheduler",
60 cl::desc("Enable scheduling after register allocation"),
David Goodwin9843a932009-10-01 22:19:57 +000061 cl::init(false), cl::Hidden);
David Goodwin2e7be612009-10-26 16:59:04 +000062static cl::opt<std::string>
Dan Gohman21d90032008-11-25 00:52:40 +000063EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin2e7be612009-10-26 16:59:04 +000064 cl::desc("Break post-RA scheduling anti-dependencies: "
65 "\"critical\", \"all\", or \"none\""),
66 cl::init("none"), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000067
David Goodwin1f152282009-09-01 18:34:03 +000068// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
69static cl::opt<int>
70DebugDiv("postra-sched-debugdiv",
71 cl::desc("Debug control MBBs that are scheduled"),
72 cl::init(0), cl::Hidden);
73static cl::opt<int>
74DebugMod("postra-sched-debugmod",
75 cl::desc("Debug control MBBs that are scheduled"),
76 cl::init(0), cl::Hidden);
77
David Goodwinada0ef82009-10-26 19:41:00 +000078AntiDepBreaker::~AntiDepBreaker() { }
79
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000080namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000081 class PostRAScheduler : public MachineFunctionPass {
Evan Cheng86050dc2010-06-18 23:09:54 +000082 const TargetInstrInfo *TII;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000083 RegisterClassInfo RegClassInfo;
Dan Gohmana70dca12009-10-09 23:27:56 +000084
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000085 public:
86 static char ID;
Andrew Trickc7d081b2012-02-08 21:22:53 +000087 PostRAScheduler() : MachineFunctionPass(ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000088
Dan Gohman3f237442008-12-16 03:25:46 +000089 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000090 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000091 AU.addRequired<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +000092 AU.addRequired<TargetPassConfig>();
Dan Gohman3f237442008-12-16 03:25:46 +000093 AU.addRequired<MachineDominatorTree>();
94 AU.addPreserved<MachineDominatorTree>();
95 AU.addRequired<MachineLoopInfo>();
96 AU.addPreserved<MachineLoopInfo>();
97 MachineFunctionPass::getAnalysisUsage(AU);
98 }
99
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000100 bool runOnMachineFunction(MachineFunction &Fn);
101 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000102 char PostRAScheduler::ID = 0;
103
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000104 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000105 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000106 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000107 LatencyPriorityQueue AvailableQueue;
Jim Grosbach90013032010-05-14 21:19:48 +0000108
Dan Gohman343f0c02008-11-19 23:18:57 +0000109 /// PendingQueue - This contains all of the instructions whose operands have
110 /// been issued, but their results are not ready yet (due to the latency of
111 /// the operation). Once the operands becomes available, the instruction is
112 /// added to the AvailableQueue.
113 std::vector<SUnit*> PendingQueue;
114
Dan Gohman2836c282009-01-16 01:33:36 +0000115 /// HazardRec - The hazard recognizer to use.
116 ScheduleHazardRecognizer *HazardRec;
117
David Goodwin2e7be612009-10-26 16:59:04 +0000118 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
119 AntiDepBreaker *AntiDepBreak;
120
Dan Gohmana70dca12009-10-09 23:27:56 +0000121 /// AA - AliasAnalysis for making memory reference queries.
122 AliasAnalysis *AA;
123
Benjamin Kramer46252d82012-02-23 19:15:40 +0000124 /// LiveRegs - true if the register is live.
125 BitVector LiveRegs;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000126
Andrew Trick47c14452012-03-07 05:21:52 +0000127 /// The schedule. Null SUnit*'s represent noop instructions.
128 std::vector<SUnit*> Sequence;
129
Andrew Trickd2763f62013-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 Gohman21d90032008-11-25 00:52:40 +0000136 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000137 SchedulePostRATDList(
138 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000139 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000140 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000141 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000142
Andrew Trick2da8bc82010-12-24 05:03:26 +0000143 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000144
Andrew Trick953be892012-03-07 23:00:49 +0000145 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146 /// this block.
147 ///
Andrew Trick953be892012-03-07 23:00:49 +0000148 void startBlock(MachineBasicBlock *BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000149
Andrew Trickd2763f62013-08-23 17:48:33 +0000150 // Set the index of RegionEnd within the current BB.
151 void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; }
152
Andrew Trick47c14452012-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 Trickd2763f62013-08-23 17:48:33 +0000157 unsigned regioninstrs);
Andrew Trick47c14452012-03-07 05:21:52 +0000158
159 /// Notify that the scheduler has finished scheduling the current region.
160 virtual void exitRegion();
161
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000162 /// Schedule - Schedule the instruction range using list scheduling.
163 ///
Andrew Trick953be892012-03-07 23:00:49 +0000164 void schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000165
Andrew Trick84b454d2012-03-07 05:21:44 +0000166 void EmitSchedule();
167
Dan Gohmanc1ae8c92009-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 Trick953be892012-03-07 23:00:49 +0000173 /// finishBlock - Clean up register live-range state.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000174 ///
Andrew Trick953be892012-03-07 23:00:49 +0000175 void finishBlock();
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000176
David Goodwin2e7be612009-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 Gohman343f0c02008-11-19 23:18:57 +0000182 private:
David Goodwin557bbe62009-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 Goodwin5e411782009-09-03 22:15:25 +0000187 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000188
David Goodwin8f909342009-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 Trick73ba69b2012-03-07 05:21:40 +0000193
194 void dumpSchedule() const;
Dan Gohman343f0c02008-11-19 23:18:57 +0000195 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000196}
197
Andrew Trick1dd8c852012-02-08 21:23:13 +0000198char &llvm::PostRASchedulerID = PostRAScheduler::ID;
199
200INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
201 "Post RA top-down list latency scheduler", false, false)
202
Andrew Trick2da8bc82010-12-24 05:03:26 +0000203SchedulePostRATDList::SchedulePostRATDList(
204 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000205 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000206 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000207 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trickae692f22012-11-12 19:28:57 +0000208 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA),
Andrew Trickd2763f62013-08-23 17:48:33 +0000209 LiveRegs(TRI->getNumRegs()), EndIndex(0)
Andrew Trick2da8bc82010-12-24 05:03:26 +0000210{
211 const TargetMachine &TM = MF.getTarget();
212 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
213 HazardRec =
214 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000215
216 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
217 MRI.tracksLiveness()) &&
218 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick2da8bc82010-12-24 05:03:26 +0000219 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000220 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000221 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000222 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000223 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000224}
225
226SchedulePostRATDList::~SchedulePostRATDList() {
227 delete HazardRec;
228 delete AntiDepBreak;
229}
230
Andrew Trick47c14452012-03-07 05:21:52 +0000231/// Initialize state associated with the next scheduling region.
232void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
233 MachineBasicBlock::iterator begin,
234 MachineBasicBlock::iterator end,
Andrew Trickd2763f62013-08-23 17:48:33 +0000235 unsigned regioninstrs) {
236 ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs);
Andrew Trick47c14452012-03-07 05:21:52 +0000237 Sequence.clear();
238}
239
240/// Print the schedule before exiting the region.
241void SchedulePostRATDList::exitRegion() {
242 DEBUG({
243 dbgs() << "*** Final schedule ***\n";
244 dumpSchedule();
245 dbgs() << '\n';
246 });
247 ScheduleDAGInstrs::exitRegion();
248}
249
Manman Renb720be62012-09-11 22:23:19 +0000250#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick73ba69b2012-03-07 05:21:40 +0000251/// dumpSchedule - dump the scheduled Sequence.
252void SchedulePostRATDList::dumpSchedule() const {
253 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
254 if (SUnit *SU = Sequence[i])
255 SU->dump(this);
256 else
257 dbgs() << "**** NOOP ****\n";
258 }
259}
Manman Ren77e300e2012-09-06 19:06:06 +0000260#endif
Andrew Trick73ba69b2012-03-07 05:21:40 +0000261
Dan Gohman343f0c02008-11-19 23:18:57 +0000262bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000263 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000264 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
265 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
266 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000267 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
268
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000269 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000270
David Goodwin471850a2009-10-01 21:46:35 +0000271 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000272 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
273 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000274 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000275 if (EnablePostRAScheduler.getPosition() > 0) {
276 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000277 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000278 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000279 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000280 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000281 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000282 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
283 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000284 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000285 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000286
David Goodwin4c3715c2009-10-22 23:19:17 +0000287 // Check for antidep breaking override...
288 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000289 AntiDepMode = (EnableAntiDepBreaking == "all")
290 ? TargetSubtargetInfo::ANTIDEP_ALL
291 : ((EnableAntiDepBreaking == "critical")
292 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
293 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000294 }
295
David Greenee1b21292010-01-05 01:26:01 +0000296 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000297
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000298 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000299 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000300
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000301 // Loop over all of the basic blocks
302 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000303 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000304#ifndef NDEBUG
305 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
306 if (DebugDiv > 0) {
307 static int bbcnt = 0;
308 if (bbcnt++ % DebugDiv != DebugMod)
309 continue;
Craig Topper96601ca2012-08-22 06:07:19 +0000310 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000311 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000312 }
313#endif
314
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000315 // Initialize register live-range state for scheduling in this block.
Andrew Trick953be892012-03-07 23:00:49 +0000316 Scheduler.startBlock(MBB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000317
Dan Gohmanf7119392009-01-16 22:10:20 +0000318 // Schedule each sequence of instructions not interrupted by a label
319 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000320 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000321 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000322 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000323 MachineInstr *MI = llvm::prior(I);
Andrew Trickd2763f62013-08-23 17:48:33 +0000324 --Count;
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000325 // Calls are not scheduling boundaries before register allocation, but
326 // post-ra we don't gain anything by scheduling across calls since we
327 // don't need to worry about register pressure.
328 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Trickd2763f62013-08-23 17:48:33 +0000329 Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count);
330 Scheduler.setEndIndex(CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000331 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000332 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000333 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000334 Current = MI;
Andrew Trickd2763f62013-08-23 17:48:33 +0000335 CurrentCount = Count;
Dan Gohman1274ced2009-03-10 18:10:43 +0000336 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000337 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000338 I = MI;
Evan Chengddfd1372011-12-14 02:11:42 +0000339 if (MI->isBundle())
340 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000341 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000342 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000343 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000344 "Instruction count mismatch!");
Andrew Trick47c14452012-03-07 05:21:52 +0000345 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Trickd2763f62013-08-23 17:48:33 +0000346 Scheduler.setEndIndex(CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000347 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000348 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000349 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000350
351 // Clean up register live-range state.
Andrew Trick953be892012-03-07 23:00:49 +0000352 Scheduler.finishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000353
David Goodwin5e411782009-09-03 22:15:25 +0000354 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000355 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000356 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000357
358 return true;
359}
Jim Grosbach90013032010-05-14 21:19:48 +0000360
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000361/// StartBlock - Initialize register live-range state for scheduling in
362/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000363///
Andrew Trick953be892012-03-07 23:00:49 +0000364void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000365 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000366 ScheduleDAGInstrs::startBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000367
David Goodwin2e7be612009-10-26 16:59:04 +0000368 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000369 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000370 if (AntiDepBreak != NULL)
371 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000372}
373
374/// Schedule - Schedule the instruction range using list scheduling.
375///
Andrew Trick953be892012-03-07 23:00:49 +0000376void SchedulePostRATDList::schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000377 // Build the scheduling graph.
Andrew Trick953be892012-03-07 23:00:49 +0000378 buildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000379
David Goodwin2e7be612009-10-26 16:59:04 +0000380 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000381 unsigned Broken =
Andrew Trick68675c62012-03-09 04:29:02 +0000382 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
383 EndIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000384
David Goodwin557bbe62009-11-20 19:32:48 +0000385 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000386 // We made changes. Update the dependency graph.
387 // Theoretically we could update the graph in place:
388 // When a live range is changed to use a different register, remove
389 // the def's anti-dependence *and* output-dependence edges due to
390 // that register, and add new anti-dependence and output-dependence
391 // edges based on the next live range of the register.
Andrew Trick47c14452012-03-07 05:21:52 +0000392 ScheduleDAG::clearDAG();
Andrew Trick953be892012-03-07 23:00:49 +0000393 buildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000394
David Goodwin2e7be612009-10-26 16:59:04 +0000395 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000396 }
397 }
398
David Greenee1b21292010-01-05 01:26:01 +0000399 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000400 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
401 SUnits[su].dumpAll(this));
402
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000403 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000404 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000405 AvailableQueue.releaseState();
406}
407
408/// Observe - Update liveness information to account for the current
409/// instruction, which will not be scheduled.
410///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000411void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000412 if (AntiDepBreak != NULL)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000413 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000414}
415
416/// FinishBlock - Clean up register live-range state.
417///
Andrew Trick953be892012-03-07 23:00:49 +0000418void SchedulePostRATDList::finishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000419 if (AntiDepBreak != NULL)
420 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000421
422 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000423 ScheduleDAGInstrs::finishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000424}
425
David Goodwin5e411782009-09-03 22:15:25 +0000426/// StartBlockForKills - Initialize register live-range state for updating kills
427///
428void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000429 // Start with no live registers.
430 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000431
Jakob Stoklund Olesenb45e4de2013-02-05 18:21:52 +0000432 // Examine the live-in regs of all successors.
433 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
434 SE = BB->succ_end(); SI != SE; ++SI) {
435 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
436 E = (*SI)->livein_end(); I != E; ++I) {
David Goodwin5e411782009-09-03 22:15:25 +0000437 unsigned Reg = *I;
Chad Rosier62c320a2013-05-22 23:17:36 +0000438 // Repeat, for reg and all subregs.
439 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
440 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000441 LiveRegs.set(*SubRegs);
David Goodwin5e411782009-09-03 22:15:25 +0000442 }
443 }
David Goodwin5e411782009-09-03 22:15:25 +0000444}
445
David Goodwin8f909342009-09-23 16:35:25 +0000446bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
447 MachineOperand &MO) {
448 // Setting kill flag...
449 if (!MO.isKill()) {
450 MO.setIsKill(true);
451 return false;
452 }
Jim Grosbach90013032010-05-14 21:19:48 +0000453
David Goodwin8f909342009-09-23 16:35:25 +0000454 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000455 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000456 MO.setIsKill(false);
457 return false;
458 }
459
460 // If any subreg of MO is live, then create an imp-def for that
461 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000462 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000463 bool AllDead = true;
464 const unsigned SuperReg = MO.getReg();
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +0000465 MachineInstrBuilder MIB(MF, MI);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000466 for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
467 if (LiveRegs.test(*SubRegs)) {
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +0000468 MIB.addReg(*SubRegs, RegState::ImplicitDefine);
David Goodwin8f909342009-09-23 16:35:25 +0000469 AllDead = false;
470 }
471 }
472
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000473 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000474 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000475 return false;
476}
477
David Goodwin88a589c2009-08-25 17:03:05 +0000478/// FixupKills - Fix the register kill flags, they may have been made
479/// incorrect by instruction reordering.
480///
481void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000482 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000483
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000484 BitVector killedRegs(TRI->getNumRegs());
David Goodwin5e411782009-09-03 22:15:25 +0000485
486 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000487
David Goodwin7886cd82009-08-29 00:11:13 +0000488 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000489 unsigned Count = MBB->size();
490 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
491 I != E; --Count) {
492 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000493 if (MI->isDebugValue())
494 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000495
David Goodwin7886cd82009-08-29 00:11:13 +0000496 // Update liveness. Registers that are defed but not used in this
497 // instruction are now dead. Mark register and all subregs as they
498 // are completely defined.
499 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
500 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000501 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000502 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000503 if (!MO.isReg()) continue;
504 unsigned Reg = MO.getReg();
505 if (Reg == 0) continue;
506 if (!MO.isDef()) continue;
507 // Ignore two-addr defs.
508 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000509
Chad Rosier62c320a2013-05-22 23:17:36 +0000510 // Repeat for reg and all subregs.
511 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
512 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000513 LiveRegs.reset(*SubRegs);
David Goodwin7886cd82009-08-29 00:11:13 +0000514 }
David Goodwin88a589c2009-08-25 17:03:05 +0000515
David Goodwin8f909342009-09-23 16:35:25 +0000516 // Examine all used registers and set/clear kill flag. When a
517 // register is used multiple times we only set the kill flag on
518 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000519 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000520 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
521 MachineOperand &MO = MI->getOperand(i);
522 if (!MO.isReg() || !MO.isUse()) continue;
523 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000524 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000525
David Goodwin7886cd82009-08-29 00:11:13 +0000526 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000527 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000528 kill = true;
529 // A register is not killed if any subregs are live...
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000530 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
531 if (LiveRegs.test(*SubRegs)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000532 kill = false;
533 break;
534 }
535 }
536
537 // If subreg is not live, then register is killed if it became
538 // live in this instruction
539 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000540 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000541 }
Jim Grosbach90013032010-05-14 21:19:48 +0000542
David Goodwin88a589c2009-08-25 17:03:05 +0000543 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000544 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000545 // Warning: ToggleKillFlag may invalidate MO.
546 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000547 DEBUG(MI->dump());
548 }
Jim Grosbach90013032010-05-14 21:19:48 +0000549
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000550 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000551 }
Jim Grosbach90013032010-05-14 21:19:48 +0000552
David Goodwina3251db2009-08-31 20:47:02 +0000553 // Mark any used register (that is not using undef) and subregs as
554 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000555 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
556 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000557 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000558 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000559 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000560
Chad Rosier62c320a2013-05-22 23:17:36 +0000561 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
562 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000563 LiveRegs.set(*SubRegs);
David Goodwin7886cd82009-08-29 00:11:13 +0000564 }
David Goodwin88a589c2009-08-25 17:03:05 +0000565 }
566}
567
Dan Gohman343f0c02008-11-19 23:18:57 +0000568//===----------------------------------------------------------------------===//
569// Top-Down Scheduling
570//===----------------------------------------------------------------------===//
571
572/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickae692f22012-11-12 19:28:57 +0000573/// the PendingQueue if the count reaches zero.
David Goodwin557bbe62009-11-20 19:32:48 +0000574void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000575 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000576
Andrew Trickcf6b6132012-11-13 02:35:06 +0000577 if (SuccEdge->isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000578 --SuccSU->WeakPredsLeft;
579 return;
580 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000581#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000582 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000583 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000584 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000585 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000586 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000587 }
588#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000589 --SuccSU->NumPredsLeft;
590
Andrew Trick89fd4372011-05-06 18:14:32 +0000591 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000592 // here as such:
593 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
594 //
595 // However, we lazily compute node depth instead. Note that
596 // ScheduleNodeTopDown has already updated the depth of this node which causes
597 // all descendents to be marked dirty. Setting the successor depth explicitly
598 // here would cause depth to be recomputed for all its ancestors. If the
599 // successor is not yet ready (because of a transitively redundant edge) then
600 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000601
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000602 // If all the node's predecessors are scheduled, this node is ready
603 // to be scheduled. Ignore the special ExitSU node.
604 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000605 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000606}
607
608/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000609void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000610 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000611 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000612 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000613 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000614}
615
616/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
617/// count of its successors. If a successor pending count is zero, add it to
618/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000619void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000620 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000621 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000622
Dan Gohman343f0c02008-11-19 23:18:57 +0000623 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000624 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000625 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000626 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000627
David Goodwin557bbe62009-11-20 19:32:48 +0000628 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000629 SU->isScheduled = true;
Andrew Trick953be892012-03-07 23:00:49 +0000630 AvailableQueue.scheduledNode(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000631}
632
633/// ListScheduleTopDown - The main loop of list scheduling for top-down
634/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000635void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000636 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000637
David Goodwin4de099d2009-11-03 20:57:50 +0000638 // We're scheduling top-down but we're visiting the regions in
639 // bottom-up order, so we don't know the hazards at the start of a
640 // region. So assume no hazards (this should usually be ok as most
641 // blocks are a single region).
642 HazardRec->Reset();
643
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000644 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000645 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000646
David Goodwin557bbe62009-11-20 19:32:48 +0000647 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000648 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
649 // It is available if it has no predecessors.
Andrew Trickae692f22012-11-12 19:28:57 +0000650 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000651 AvailableQueue.push(&SUnits[i]);
652 SUnits[i].isAvailable = true;
653 }
654 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000655
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000656 // In any cycle where we can't schedule any instructions, we must
657 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000658 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000659
Dan Gohman343f0c02008-11-19 23:18:57 +0000660 // While Available queue is not empty, grab the node with the highest
661 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000662 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000663 Sequence.reserve(SUnits.size());
664 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
665 // Check to see if any of the pending instructions are ready to issue. If
666 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000667 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000668 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000669 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000670 AvailableQueue.push(PendingQueue[i]);
671 PendingQueue[i]->isAvailable = true;
672 PendingQueue[i] = PendingQueue.back();
673 PendingQueue.pop_back();
674 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000675 } else if (PendingQueue[i]->getDepth() < MinDepth)
676 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000677 }
David Goodwinc93d8372009-08-11 17:35:23 +0000678
Andrew Trick2da8bc82010-12-24 05:03:26 +0000679 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000680
Dan Gohman2836c282009-01-16 01:33:36 +0000681 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000682 bool HasNoopHazards = false;
683 while (!AvailableQueue.empty()) {
684 SUnit *CurSUnit = AvailableQueue.pop();
685
686 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000687 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000688 if (HT == ScheduleHazardRecognizer::NoHazard) {
689 FoundSUnit = CurSUnit;
690 break;
691 }
692
693 // Remember if this is a noop hazard.
694 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
695
696 NotReady.push_back(CurSUnit);
697 }
698
699 // Add the nodes that aren't ready back onto the available list.
700 if (!NotReady.empty()) {
701 AvailableQueue.push_all(NotReady);
702 NotReady.clear();
703 }
704
David Goodwin4de099d2009-11-03 20:57:50 +0000705 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000706 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000707 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000708 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000709 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000710 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000711 if (HazardRec->atIssueLimit()) {
712 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
713 HazardRec->AdvanceCycle();
714 ++CurCycle;
715 CycleHasInsts = false;
716 }
Dan Gohman2836c282009-01-16 01:33:36 +0000717 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000718 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000719 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000720 HazardRec->AdvanceCycle();
721 } else if (!HasNoopHazards) {
722 // Otherwise, we have a pipeline stall, but no other problem,
723 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000724 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000725 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000726 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000727 } else {
728 // Otherwise, we have no instructions to issue and we have instructions
729 // that will fault if we don't do this right. This is the case for
730 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000731 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000732 HazardRec->EmitNoop();
733 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000734 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000735 }
736
Dan Gohman2836c282009-01-16 01:33:36 +0000737 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000738 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000739 }
740 }
741
742#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000743 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
744 unsigned Noops = 0;
745 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
746 if (!Sequence[i])
747 ++Noops;
748 assert(Sequence.size() - Noops == ScheduledNodes &&
749 "The number of nodes scheduled doesn't match the expected number!");
750#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000751}
Andrew Trick84b454d2012-03-07 05:21:44 +0000752
753// EmitSchedule - Emit the machine code in scheduled order.
754void SchedulePostRATDList::EmitSchedule() {
Andrew Trick68675c62012-03-09 04:29:02 +0000755 RegionBegin = RegionEnd;
Andrew Trick84b454d2012-03-07 05:21:44 +0000756
757 // If first instruction was a DBG_VALUE then put it back.
758 if (FirstDbgValue)
Andrew Trick68675c62012-03-09 04:29:02 +0000759 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Trick84b454d2012-03-07 05:21:44 +0000760
761 // Then re-insert them according to the given schedule.
762 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
763 if (SUnit *SU = Sequence[i])
Andrew Trick68675c62012-03-09 04:29:02 +0000764 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Trick84b454d2012-03-07 05:21:44 +0000765 else
766 // Null SUnit* is a noop.
Andrew Trick68675c62012-03-09 04:29:02 +0000767 TII->insertNoop(*BB, RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000768
769 // Update the Begin iterator, as the first instruction in the block
770 // may have been scheduled later.
771 if (i == 0)
Andrew Trick68675c62012-03-09 04:29:02 +0000772 RegionBegin = prior(RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000773 }
774
775 // Reinsert any remaining debug_values.
776 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
777 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
778 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
779 MachineInstr *DbgValue = P.first;
780 MachineBasicBlock::iterator OrigPrivMI = P.second;
781 BB->splice(++OrigPrivMI, BB, DbgValue);
782 }
783 DbgValues.clear();
784 FirstDbgValue = NULL;
785}