blob: 488f1d45643ff658e3d314f0a20283597a5f7f85 [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
Dan Gohman21d90032008-11-25 00:52:40 +0000130 public:
Andrew Trick2da8bc82010-12-24 05:03:26 +0000131 SchedulePostRATDList(
132 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000133 AliasAnalysis *AA, const RegisterClassInfo&,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000134 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000135 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
Dan Gohman2836c282009-01-16 01:33:36 +0000136
Andrew Trick2da8bc82010-12-24 05:03:26 +0000137 ~SchedulePostRATDList();
Dan Gohman343f0c02008-11-19 23:18:57 +0000138
Andrew Trick953be892012-03-07 23:00:49 +0000139 /// startBlock - Initialize register live-range state for scheduling in
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000140 /// this block.
141 ///
Andrew Trick953be892012-03-07 23:00:49 +0000142 void startBlock(MachineBasicBlock *BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000143
Andrew Trick47c14452012-03-07 05:21:52 +0000144 /// Initialize the scheduler state for the next scheduling region.
145 virtual void enterRegion(MachineBasicBlock *bb,
146 MachineBasicBlock::iterator begin,
147 MachineBasicBlock::iterator end,
148 unsigned endcount);
149
150 /// Notify that the scheduler has finished scheduling the current region.
151 virtual void exitRegion();
152
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000153 /// Schedule - Schedule the instruction range using list scheduling.
154 ///
Andrew Trick953be892012-03-07 23:00:49 +0000155 void schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000156
Andrew Trick84b454d2012-03-07 05:21:44 +0000157 void EmitSchedule();
158
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000159 /// Observe - Update liveness information to account for the current
160 /// instruction, which will not be scheduled.
161 ///
162 void Observe(MachineInstr *MI, unsigned Count);
163
Andrew Trick953be892012-03-07 23:00:49 +0000164 /// finishBlock - Clean up register live-range state.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000165 ///
Andrew Trick953be892012-03-07 23:00:49 +0000166 void finishBlock();
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000167
David Goodwin2e7be612009-10-26 16:59:04 +0000168 /// FixupKills - Fix register kill flags that have been made
169 /// invalid due to scheduling
170 ///
171 void FixupKills(MachineBasicBlock *MBB);
172
Dan Gohman343f0c02008-11-19 23:18:57 +0000173 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000174 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
175 void ReleaseSuccessors(SUnit *SU);
176 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
177 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000178 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000179
David Goodwin8f909342009-09-23 16:35:25 +0000180 // ToggleKillFlag - Toggle a register operand kill flag. Other
181 // adjustments may be made to the instruction if necessary. Return
182 // true if the operand has been deleted, false if not.
183 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Andrew Trick73ba69b2012-03-07 05:21:40 +0000184
185 void dumpSchedule() const;
Dan Gohman343f0c02008-11-19 23:18:57 +0000186 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000187}
188
Andrew Trick1dd8c852012-02-08 21:23:13 +0000189char &llvm::PostRASchedulerID = PostRAScheduler::ID;
190
191INITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
192 "Post RA top-down list latency scheduler", false, false)
193
Andrew Trick2da8bc82010-12-24 05:03:26 +0000194SchedulePostRATDList::SchedulePostRATDList(
195 MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000196 AliasAnalysis *AA, const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000197 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
Craig Topper44d23822012-02-22 05:59:10 +0000198 SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
Andrew Trickae692f22012-11-12 19:28:57 +0000199 : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA),
Benjamin Kramer46252d82012-02-23 19:15:40 +0000200 LiveRegs(TRI->getNumRegs())
Andrew Trick2da8bc82010-12-24 05:03:26 +0000201{
202 const TargetMachine &TM = MF.getTarget();
203 const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
204 HazardRec =
205 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000206
207 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
208 MRI.tracksLiveness()) &&
209 "Live-ins must be accurate for anti-dependency breaking");
Andrew Trick2da8bc82010-12-24 05:03:26 +0000210 AntiDepBreak =
Evan Cheng5b1b44892011-07-01 21:01:15 +0000211 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000212 (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
Evan Cheng5b1b44892011-07-01 21:01:15 +0000213 ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000214 (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
Andrew Trick2da8bc82010-12-24 05:03:26 +0000215}
216
217SchedulePostRATDList::~SchedulePostRATDList() {
218 delete HazardRec;
219 delete AntiDepBreak;
220}
221
Andrew Trick47c14452012-03-07 05:21:52 +0000222/// Initialize state associated with the next scheduling region.
223void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
224 MachineBasicBlock::iterator begin,
225 MachineBasicBlock::iterator end,
226 unsigned endcount) {
227 ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount);
228 Sequence.clear();
229}
230
231/// Print the schedule before exiting the region.
232void SchedulePostRATDList::exitRegion() {
233 DEBUG({
234 dbgs() << "*** Final schedule ***\n";
235 dumpSchedule();
236 dbgs() << '\n';
237 });
238 ScheduleDAGInstrs::exitRegion();
239}
240
Manman Renb720be62012-09-11 22:23:19 +0000241#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick73ba69b2012-03-07 05:21:40 +0000242/// dumpSchedule - dump the scheduled Sequence.
243void SchedulePostRATDList::dumpSchedule() const {
244 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
245 if (SUnit *SU = Sequence[i])
246 SU->dump(this);
247 else
248 dbgs() << "**** NOOP ****\n";
249 }
250}
Manman Ren77e300e2012-09-06 19:06:06 +0000251#endif
Andrew Trick73ba69b2012-03-07 05:21:40 +0000252
Dan Gohman343f0c02008-11-19 23:18:57 +0000253bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000254 TII = Fn.getTarget().getInstrInfo();
Andrew Trick2da8bc82010-12-24 05:03:26 +0000255 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
256 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
257 AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000258 TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
259
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000260 RegClassInfo.runOnMachineFunction(Fn);
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000261
David Goodwin471850a2009-10-01 21:46:35 +0000262 // Check for explicit enable/disable of post-ra scheduling.
Evan Chengddfd1372011-12-14 02:11:42 +0000263 TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
264 TargetSubtargetInfo::ANTIDEP_NONE;
Craig Topper44d23822012-02-22 05:59:10 +0000265 SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000266 if (EnablePostRAScheduler.getPosition() > 0) {
267 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000268 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000269 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000270 // Check that post-RA scheduling is enabled for this target.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000271 // This may upgrade the AntiDepMode.
Evan Cheng5b1b44892011-07-01 21:01:15 +0000272 const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
Andrew Trickc7d081b2012-02-08 21:22:53 +0000273 if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
274 CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000275 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000276 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000277
David Goodwin4c3715c2009-10-22 23:19:17 +0000278 // Check for antidep breaking override...
279 if (EnableAntiDepBreaking.getPosition() > 0) {
Evan Cheng5b1b44892011-07-01 21:01:15 +0000280 AntiDepMode = (EnableAntiDepBreaking == "all")
281 ? TargetSubtargetInfo::ANTIDEP_ALL
282 : ((EnableAntiDepBreaking == "critical")
283 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
284 : TargetSubtargetInfo::ANTIDEP_NONE);
David Goodwin4c3715c2009-10-22 23:19:17 +0000285 }
286
David Greenee1b21292010-01-05 01:26:01 +0000287 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000288
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000289 SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
Andrew Trick2da8bc82010-12-24 05:03:26 +0000290 CriticalPathRCs);
Dan Gohman79ce2762009-01-15 19:20:50 +0000291
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000292 // Loop over all of the basic blocks
293 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000294 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000295#ifndef NDEBUG
296 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
297 if (DebugDiv > 0) {
298 static int bbcnt = 0;
299 if (bbcnt++ % DebugDiv != DebugMod)
300 continue;
Craig Topper96601ca2012-08-22 06:07:19 +0000301 dbgs() << "*** DEBUG scheduling " << Fn.getName()
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000302 << ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000303 }
304#endif
305
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000306 // Initialize register live-range state for scheduling in this block.
Andrew Trick953be892012-03-07 23:00:49 +0000307 Scheduler.startBlock(MBB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000308
Dan Gohmanf7119392009-01-16 22:10:20 +0000309 // Schedule each sequence of instructions not interrupted by a label
310 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000311 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000312 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000313 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000314 MachineInstr *MI = llvm::prior(I);
Jakob Stoklund Olesen976647d2012-02-23 17:54:21 +0000315 // Calls are not scheduling boundaries before register allocation, but
316 // post-ra we don't gain anything by scheduling across calls since we
317 // don't need to worry about register pressure.
318 if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
Andrew Trick47c14452012-03-07 05:21:52 +0000319 Scheduler.enterRegion(MBB, I, Current, CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000320 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000321 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000322 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000324 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000325 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000326 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000327 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000328 --Count;
Evan Chengddfd1372011-12-14 02:11:42 +0000329 if (MI->isBundle())
330 Count -= MI->getBundleSize();
Dan Gohman43f07fb2009-02-03 18:57:45 +0000331 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000332 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000333 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000334 "Instruction count mismatch!");
Andrew Trick47c14452012-03-07 05:21:52 +0000335 Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
Andrew Trick953be892012-03-07 23:00:49 +0000336 Scheduler.schedule();
Andrew Trick47c14452012-03-07 05:21:52 +0000337 Scheduler.exitRegion();
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000338 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000339
340 // Clean up register live-range state.
Andrew Trick953be892012-03-07 23:00:49 +0000341 Scheduler.finishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000342
David Goodwin5e411782009-09-03 22:15:25 +0000343 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000344 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000345 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000346
347 return true;
348}
Jim Grosbach90013032010-05-14 21:19:48 +0000349
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000350/// StartBlock - Initialize register live-range state for scheduling in
351/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000352///
Andrew Trick953be892012-03-07 23:00:49 +0000353void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000354 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000355 ScheduleDAGInstrs::startBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000356
David Goodwin2e7be612009-10-26 16:59:04 +0000357 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000358 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000359 if (AntiDepBreak != NULL)
360 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000361}
362
363/// Schedule - Schedule the instruction range using list scheduling.
364///
Andrew Trick953be892012-03-07 23:00:49 +0000365void SchedulePostRATDList::schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000366 // Build the scheduling graph.
Andrew Trick953be892012-03-07 23:00:49 +0000367 buildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000368
David Goodwin2e7be612009-10-26 16:59:04 +0000369 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000370 unsigned Broken =
Andrew Trick68675c62012-03-09 04:29:02 +0000371 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
372 EndIndex, DbgValues);
Jim Grosbach90013032010-05-14 21:19:48 +0000373
David Goodwin557bbe62009-11-20 19:32:48 +0000374 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000375 // We made changes. Update the dependency graph.
376 // Theoretically we could update the graph in place:
377 // When a live range is changed to use a different register, remove
378 // the def's anti-dependence *and* output-dependence edges due to
379 // that register, and add new anti-dependence and output-dependence
380 // edges based on the next live range of the register.
Andrew Trick47c14452012-03-07 05:21:52 +0000381 ScheduleDAG::clearDAG();
Andrew Trick953be892012-03-07 23:00:49 +0000382 buildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000383
David Goodwin2e7be612009-10-26 16:59:04 +0000384 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000385 }
386 }
387
David Greenee1b21292010-01-05 01:26:01 +0000388 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000389 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
390 SUnits[su].dumpAll(this));
391
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000392 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000393 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000394 AvailableQueue.releaseState();
395}
396
397/// Observe - Update liveness information to account for the current
398/// instruction, which will not be scheduled.
399///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000400void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000401 if (AntiDepBreak != NULL)
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000402 AntiDepBreak->Observe(MI, Count, EndIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000403}
404
405/// FinishBlock - Clean up register live-range state.
406///
Andrew Trick953be892012-03-07 23:00:49 +0000407void SchedulePostRATDList::finishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000408 if (AntiDepBreak != NULL)
409 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000410
411 // Call the superclass.
Andrew Trick953be892012-03-07 23:00:49 +0000412 ScheduleDAGInstrs::finishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000413}
414
David Goodwin5e411782009-09-03 22:15:25 +0000415/// StartBlockForKills - Initialize register live-range state for updating kills
416///
417void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
Benjamin Kramer46252d82012-02-23 19:15:40 +0000418 // Start with no live registers.
419 LiveRegs.reset();
David Goodwin5e411782009-09-03 22:15:25 +0000420
421 // Determine the live-out physregs for this block.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000422 if (!BB->empty() && BB->back().isReturn()) {
David Goodwin5e411782009-09-03 22:15:25 +0000423 // In a return block, examine the function live-out regs.
424 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
425 E = MRI.liveout_end(); I != E; ++I) {
426 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000427 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000428 // Repeat, for all subregs.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000429 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
430 LiveRegs.set(*SubRegs);
David Goodwin5e411782009-09-03 22:15:25 +0000431 }
432 }
433 else {
434 // In a non-return block, examine the live-in regs of all successors.
435 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
436 SE = BB->succ_end(); SI != SE; ++SI) {
437 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
438 E = (*SI)->livein_end(); I != E; ++I) {
439 unsigned Reg = *I;
Benjamin Kramer46252d82012-02-23 19:15:40 +0000440 LiveRegs.set(Reg);
David Goodwin5e411782009-09-03 22:15:25 +0000441 // Repeat, for all subregs.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000442 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
443 LiveRegs.set(*SubRegs);
David Goodwin5e411782009-09-03 22:15:25 +0000444 }
445 }
446 }
447}
448
David Goodwin8f909342009-09-23 16:35:25 +0000449bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
450 MachineOperand &MO) {
451 // Setting kill flag...
452 if (!MO.isKill()) {
453 MO.setIsKill(true);
454 return false;
455 }
Jim Grosbach90013032010-05-14 21:19:48 +0000456
David Goodwin8f909342009-09-23 16:35:25 +0000457 // If MO itself is live, clear the kill flag...
Benjamin Kramer46252d82012-02-23 19:15:40 +0000458 if (LiveRegs.test(MO.getReg())) {
David Goodwin8f909342009-09-23 16:35:25 +0000459 MO.setIsKill(false);
460 return false;
461 }
462
463 // If any subreg of MO is live, then create an imp-def for that
464 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000465 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000466 bool AllDead = true;
467 const unsigned SuperReg = MO.getReg();
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +0000468 MachineInstrBuilder MIB(MF, MI);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000469 for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
470 if (LiveRegs.test(*SubRegs)) {
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +0000471 MIB.addReg(*SubRegs, RegState::ImplicitDefine);
David Goodwin8f909342009-09-23 16:35:25 +0000472 AllDead = false;
473 }
474 }
475
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000476 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000477 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000478 return false;
479}
480
David Goodwin88a589c2009-08-25 17:03:05 +0000481/// FixupKills - Fix the register kill flags, they may have been made
482/// incorrect by instruction reordering.
483///
484void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000485 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000486
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000487 BitVector killedRegs(TRI->getNumRegs());
David Goodwin5e411782009-09-03 22:15:25 +0000488
489 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000490
David Goodwin7886cd82009-08-29 00:11:13 +0000491 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000492 unsigned Count = MBB->size();
493 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
494 I != E; --Count) {
495 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000496 if (MI->isDebugValue())
497 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000498
David Goodwin7886cd82009-08-29 00:11:13 +0000499 // Update liveness. Registers that are defed but not used in this
500 // instruction are now dead. Mark register and all subregs as they
501 // are completely defined.
502 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
503 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenf19a5922012-02-23 01:22:15 +0000504 if (MO.isRegMask())
Benjamin Kramerb6bd8cc2012-02-23 19:29:25 +0000505 LiveRegs.clearBitsNotInMask(MO.getRegMask());
David Goodwin7886cd82009-08-29 00:11:13 +0000506 if (!MO.isReg()) continue;
507 unsigned Reg = MO.getReg();
508 if (Reg == 0) continue;
509 if (!MO.isDef()) continue;
510 // Ignore two-addr defs.
511 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000512
Benjamin Kramer46252d82012-02-23 19:15:40 +0000513 LiveRegs.reset(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000514
David Goodwin7886cd82009-08-29 00:11:13 +0000515 // Repeat for all subregs.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000516 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
517 LiveRegs.reset(*SubRegs);
David Goodwin7886cd82009-08-29 00:11:13 +0000518 }
David Goodwin88a589c2009-08-25 17:03:05 +0000519
David Goodwin8f909342009-09-23 16:35:25 +0000520 // Examine all used registers and set/clear kill flag. When a
521 // register is used multiple times we only set the kill flag on
522 // the first use.
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000523 killedRegs.reset();
David Goodwin88a589c2009-08-25 17:03:05 +0000524 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
525 MachineOperand &MO = MI->getOperand(i);
526 if (!MO.isReg() || !MO.isUse()) continue;
527 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000528 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000529
David Goodwin7886cd82009-08-29 00:11:13 +0000530 bool kill = false;
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000531 if (!killedRegs.test(Reg)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000532 kill = true;
533 // A register is not killed if any subregs are live...
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000534 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
535 if (LiveRegs.test(*SubRegs)) {
David Goodwin7886cd82009-08-29 00:11:13 +0000536 kill = false;
537 break;
538 }
539 }
540
541 // If subreg is not live, then register is killed if it became
542 // live in this instruction
543 if (kill)
Benjamin Kramer46252d82012-02-23 19:15:40 +0000544 kill = !LiveRegs.test(Reg);
David Goodwin7886cd82009-08-29 00:11:13 +0000545 }
Jim Grosbach90013032010-05-14 21:19:48 +0000546
David Goodwin88a589c2009-08-25 17:03:05 +0000547 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000548 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000549 // Warning: ToggleKillFlag may invalidate MO.
550 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000551 DEBUG(MI->dump());
552 }
Jim Grosbach90013032010-05-14 21:19:48 +0000553
Benjamin Kramer49b726c2012-02-23 18:28:32 +0000554 killedRegs.set(Reg);
David Goodwin88a589c2009-08-25 17:03:05 +0000555 }
Jim Grosbach90013032010-05-14 21:19:48 +0000556
David Goodwina3251db2009-08-31 20:47:02 +0000557 // Mark any used register (that is not using undef) and subregs as
558 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000559 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
560 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000561 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000562 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000563 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000564
Benjamin Kramer46252d82012-02-23 19:15:40 +0000565 LiveRegs.set(Reg);
Jim Grosbach90013032010-05-14 21:19:48 +0000566
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000567 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
568 LiveRegs.set(*SubRegs);
David Goodwin7886cd82009-08-29 00:11:13 +0000569 }
David Goodwin88a589c2009-08-25 17:03:05 +0000570 }
571}
572
Dan Gohman343f0c02008-11-19 23:18:57 +0000573//===----------------------------------------------------------------------===//
574// Top-Down Scheduling
575//===----------------------------------------------------------------------===//
576
577/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Andrew Trickae692f22012-11-12 19:28:57 +0000578/// the PendingQueue if the count reaches zero.
David Goodwin557bbe62009-11-20 19:32:48 +0000579void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000580 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000581
Andrew Trickcf6b6132012-11-13 02:35:06 +0000582 if (SuccEdge->isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000583 --SuccSU->WeakPredsLeft;
584 return;
585 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000586#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000587 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000588 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000589 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000590 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000591 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000592 }
593#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000594 --SuccSU->NumPredsLeft;
595
Andrew Trick89fd4372011-05-06 18:14:32 +0000596 // Standard scheduler algorithms will recompute the depth of the successor
Andrew Trick15ab3592011-05-06 17:09:08 +0000597 // here as such:
598 // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
599 //
600 // However, we lazily compute node depth instead. Note that
601 // ScheduleNodeTopDown has already updated the depth of this node which causes
602 // all descendents to be marked dirty. Setting the successor depth explicitly
603 // here would cause depth to be recomputed for all its ancestors. If the
604 // successor is not yet ready (because of a transitively redundant edge) then
605 // this causes depth computation to be quadratic in the size of the DAG.
Jim Grosbach90013032010-05-14 21:19:48 +0000606
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000607 // If all the node's predecessors are scheduled, this node is ready
608 // to be scheduled. Ignore the special ExitSU node.
609 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000610 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000611}
612
613/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000614void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000615 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000616 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000617 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000618 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000619}
620
621/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
622/// count of its successors. If a successor pending count is zero, add it to
623/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000624void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000625 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000626 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000627
Dan Gohman343f0c02008-11-19 23:18:57 +0000628 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000629 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000630 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000631 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000632
David Goodwin557bbe62009-11-20 19:32:48 +0000633 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000634 SU->isScheduled = true;
Andrew Trick953be892012-03-07 23:00:49 +0000635 AvailableQueue.scheduledNode(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000636}
637
638/// ListScheduleTopDown - The main loop of list scheduling for top-down
639/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000640void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000641 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000642
David Goodwin4de099d2009-11-03 20:57:50 +0000643 // We're scheduling top-down but we're visiting the regions in
644 // bottom-up order, so we don't know the hazards at the start of a
645 // region. So assume no hazards (this should usually be ok as most
646 // blocks are a single region).
647 HazardRec->Reset();
648
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000649 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000650 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000651
David Goodwin557bbe62009-11-20 19:32:48 +0000652 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000653 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
654 // It is available if it has no predecessors.
Andrew Trickae692f22012-11-12 19:28:57 +0000655 if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000656 AvailableQueue.push(&SUnits[i]);
657 SUnits[i].isAvailable = true;
658 }
659 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000660
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000661 // In any cycle where we can't schedule any instructions, we must
662 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000663 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000664
Dan Gohman343f0c02008-11-19 23:18:57 +0000665 // While Available queue is not empty, grab the node with the highest
666 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000667 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000668 Sequence.reserve(SUnits.size());
669 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
670 // Check to see if any of the pending instructions are ready to issue. If
671 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000672 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000673 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000674 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000675 AvailableQueue.push(PendingQueue[i]);
676 PendingQueue[i]->isAvailable = true;
677 PendingQueue[i] = PendingQueue.back();
678 PendingQueue.pop_back();
679 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000680 } else if (PendingQueue[i]->getDepth() < MinDepth)
681 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000682 }
David Goodwinc93d8372009-08-11 17:35:23 +0000683
Andrew Trick2da8bc82010-12-24 05:03:26 +0000684 DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
David Goodwinc93d8372009-08-11 17:35:23 +0000685
Dan Gohman2836c282009-01-16 01:33:36 +0000686 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000687 bool HasNoopHazards = false;
688 while (!AvailableQueue.empty()) {
689 SUnit *CurSUnit = AvailableQueue.pop();
690
691 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick2da8bc82010-12-24 05:03:26 +0000692 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman2836c282009-01-16 01:33:36 +0000693 if (HT == ScheduleHazardRecognizer::NoHazard) {
694 FoundSUnit = CurSUnit;
695 break;
696 }
697
698 // Remember if this is a noop hazard.
699 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
700
701 NotReady.push_back(CurSUnit);
702 }
703
704 // Add the nodes that aren't ready back onto the available list.
705 if (!NotReady.empty()) {
706 AvailableQueue.push_all(NotReady);
707 NotReady.clear();
708 }
709
David Goodwin4de099d2009-11-03 20:57:50 +0000710 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000711 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000712 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000713 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000714 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000715 CycleHasInsts = true;
Andrew Trickcf9aa282011-06-01 03:27:56 +0000716 if (HazardRec->atIssueLimit()) {
717 DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
718 HazardRec->AdvanceCycle();
719 ++CurCycle;
720 CycleHasInsts = false;
721 }
Dan Gohman2836c282009-01-16 01:33:36 +0000722 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000723 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000724 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000725 HazardRec->AdvanceCycle();
726 } else if (!HasNoopHazards) {
727 // Otherwise, we have a pipeline stall, but no other problem,
728 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000729 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000730 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000731 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000732 } else {
733 // Otherwise, we have no instructions to issue and we have instructions
734 // that will fault if we don't do this right. This is the case for
735 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000736 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000737 HazardRec->EmitNoop();
738 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000739 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000740 }
741
Dan Gohman2836c282009-01-16 01:33:36 +0000742 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000743 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000744 }
745 }
746
747#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000748 unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
749 unsigned Noops = 0;
750 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
751 if (!Sequence[i])
752 ++Noops;
753 assert(Sequence.size() - Noops == ScheduledNodes &&
754 "The number of nodes scheduled doesn't match the expected number!");
755#endif // NDEBUG
Dan Gohman343f0c02008-11-19 23:18:57 +0000756}
Andrew Trick84b454d2012-03-07 05:21:44 +0000757
758// EmitSchedule - Emit the machine code in scheduled order.
759void SchedulePostRATDList::EmitSchedule() {
Andrew Trick68675c62012-03-09 04:29:02 +0000760 RegionBegin = RegionEnd;
Andrew Trick84b454d2012-03-07 05:21:44 +0000761
762 // If first instruction was a DBG_VALUE then put it back.
763 if (FirstDbgValue)
Andrew Trick68675c62012-03-09 04:29:02 +0000764 BB->splice(RegionEnd, BB, FirstDbgValue);
Andrew Trick84b454d2012-03-07 05:21:44 +0000765
766 // Then re-insert them according to the given schedule.
767 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
768 if (SUnit *SU = Sequence[i])
Andrew Trick68675c62012-03-09 04:29:02 +0000769 BB->splice(RegionEnd, BB, SU->getInstr());
Andrew Trick84b454d2012-03-07 05:21:44 +0000770 else
771 // Null SUnit* is a noop.
Andrew Trick68675c62012-03-09 04:29:02 +0000772 TII->insertNoop(*BB, RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000773
774 // Update the Begin iterator, as the first instruction in the block
775 // may have been scheduled later.
776 if (i == 0)
Andrew Trick68675c62012-03-09 04:29:02 +0000777 RegionBegin = prior(RegionEnd);
Andrew Trick84b454d2012-03-07 05:21:44 +0000778 }
779
780 // Reinsert any remaining debug_values.
781 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
782 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
783 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
784 MachineInstr *DbgValue = P.first;
785 MachineBasicBlock::iterator OrigPrivMI = P.second;
786 BB->splice(++OrigPrivMI, BB, DbgValue);
787 }
788 DbgValues.clear();
789 FirstDbgValue = NULL;
790}