blob: 4af8e07f3480031f87329b109e708cc4c7f1403e [file] [log] [blame]
Dale Johannesen72f15962007-07-13 17:31:29 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements a top-down list scheduler, using standard algorithms.
11// The basic approach uses a priority queue of available nodes to schedule.
12// One at a time, nodes are taken from the priority queue (thus in priority
13// order), checked for legality to schedule, and emitted if legal.
14//
15// Nodes may not be legal to schedule either due to structural hazards (e.g.
16// pipeline or resource constraints) or because an input to the instruction has
17// not completed execution.
18//
19//===----------------------------------------------------------------------===//
20
21#define DEBUG_TYPE "post-RA-sched"
David Goodwin82c72482009-10-28 18:29:54 +000022#include "AntiDepBreaker.h"
David Goodwin34877712009-10-26 19:32:42 +000023#include "AggressiveAntiDepBreaker.h"
David Goodwin2e7be612009-10-26 16:59:04 +000024#include "CriticalAntiDepBreaker.h"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000025#include "ScheduleDAGInstrs.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000026#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000027#include "llvm/CodeGen/LatencyPriorityQueue.h"
28#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3f237442008-12-16 03:25:46 +000029#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000032#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2836c282009-01-16 01:33:36 +000034#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000035#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohmanbed353d2009-02-10 23:29:38 +000036#include "llvm/Target/TargetLowering.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000037#include "llvm/Target/TargetMachine.h"
Dan Gohman21d90032008-11-25 00:52:40 +000038#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin0dad89f2009-09-30 00:10:16 +000040#include "llvm/Target/TargetSubtarget.h"
David Goodwine10deca2009-10-26 22:31:16 +000041#include "llvm/Support/CommandLine.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000042#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000043#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000044#include "llvm/Support/raw_ostream.h"
David Goodwin2e7be612009-10-26 16:59:04 +000045#include "llvm/ADT/BitVector.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000046#include "llvm/ADT/Statistic.h"
David Goodwin88a589c2009-08-25 17:03:05 +000047#include <set>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000048using namespace llvm;
49
Dan Gohman2836c282009-01-16 01:33:36 +000050STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000051STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin2e7be612009-10-26 16:59:04 +000052STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman343f0c02008-11-19 23:18:57 +000053
David Goodwin471850a2009-10-01 21:46:35 +000054// Post-RA scheduling is enabled with
55// TargetSubtarget.enablePostRAScheduler(). This flag can be used to
56// override the target.
57static cl::opt<bool>
58EnablePostRAScheduler("post-RA-scheduler",
59 cl::desc("Enable scheduling after register allocation"),
David Goodwin9843a932009-10-01 22:19:57 +000060 cl::init(false), cl::Hidden);
David Goodwin2e7be612009-10-26 16:59:04 +000061static cl::opt<std::string>
Dan Gohman21d90032008-11-25 00:52:40 +000062EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin2e7be612009-10-26 16:59:04 +000063 cl::desc("Break post-RA scheduling anti-dependencies: "
64 "\"critical\", \"all\", or \"none\""),
65 cl::init("none"), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000066
David Goodwin1f152282009-09-01 18:34:03 +000067// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
68static cl::opt<int>
69DebugDiv("postra-sched-debugdiv",
70 cl::desc("Debug control MBBs that are scheduled"),
71 cl::init(0), cl::Hidden);
72static cl::opt<int>
73DebugMod("postra-sched-debugmod",
74 cl::desc("Debug control MBBs that are scheduled"),
75 cl::init(0), cl::Hidden);
76
David Goodwinada0ef82009-10-26 19:41:00 +000077AntiDepBreaker::~AntiDepBreaker() { }
78
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000079namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000080 class PostRAScheduler : public MachineFunctionPass {
Dan Gohmana70dca12009-10-09 23:27:56 +000081 AliasAnalysis *AA;
Evan Cheng86050dc2010-06-18 23:09:54 +000082 const TargetInstrInfo *TII;
Evan Chengfa163542009-10-16 21:06:15 +000083 CodeGenOpt::Level OptLevel;
Dan Gohmana70dca12009-10-09 23:27:56 +000084
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000085 public:
86 static char ID;
Evan Chengfa163542009-10-16 21:06:15 +000087 PostRAScheduler(CodeGenOpt::Level ol) :
88 MachineFunctionPass(&ID), OptLevel(ol) {}
Dan Gohman21d90032008-11-25 00:52:40 +000089
Dan Gohman3f237442008-12-16 03:25:46 +000090 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000091 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000092 AU.addRequired<AliasAnalysis>();
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 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +0000101 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000102 }
103
104 bool runOnMachineFunction(MachineFunction &Fn);
105 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000106 char PostRAScheduler::ID = 0;
107
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000108 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000109 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000110 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000111 LatencyPriorityQueue AvailableQueue;
Jim Grosbach90013032010-05-14 21:19:48 +0000112
Dan Gohman343f0c02008-11-19 23:18:57 +0000113 /// PendingQueue - This contains all of the instructions whose operands have
114 /// been issued, but their results are not ready yet (due to the latency of
115 /// the operation). Once the operands becomes available, the instruction is
116 /// added to the AvailableQueue.
117 std::vector<SUnit*> PendingQueue;
118
Dan Gohman21d90032008-11-25 00:52:40 +0000119 /// Topo - A topological ordering for SUnits.
120 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000121
Dan Gohman2836c282009-01-16 01:33:36 +0000122 /// HazardRec - The hazard recognizer to use.
123 ScheduleHazardRecognizer *HazardRec;
124
David Goodwin2e7be612009-10-26 16:59:04 +0000125 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
126 AntiDepBreaker *AntiDepBreak;
127
Dan Gohmana70dca12009-10-09 23:27:56 +0000128 /// AA - AliasAnalysis for making memory reference queries.
129 AliasAnalysis *AA;
130
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000131 /// KillIndices - The index of the most recent kill (proceding bottom-up),
132 /// or ~0u if the register is not live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000133 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
134
Dan Gohman21d90032008-11-25 00:52:40 +0000135 public:
Dan Gohman79ce2762009-01-15 19:20:50 +0000136 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman3f237442008-12-16 03:25:46 +0000137 const MachineLoopInfo &MLI,
Dan Gohman2836c282009-01-16 01:33:36 +0000138 const MachineDominatorTree &MDT,
Dan Gohmana70dca12009-10-09 23:27:56 +0000139 ScheduleHazardRecognizer *HR,
David Goodwin2e7be612009-10-26 16:59:04 +0000140 AntiDepBreaker *ADB,
141 AliasAnalysis *aa)
Dan Gohman79ce2762009-01-15 19:20:50 +0000142 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
David Goodwin2e7be612009-10-26 16:59:04 +0000143 HazardRec(HR), AntiDepBreak(ADB), AA(aa) {}
Dan Gohman2836c282009-01-16 01:33:36 +0000144
145 ~SchedulePostRATDList() {
Dan Gohman2836c282009-01-16 01:33:36 +0000146 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000147
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000148 /// StartBlock - Initialize register live-range state for scheduling in
149 /// this block.
150 ///
151 void StartBlock(MachineBasicBlock *BB);
152
153 /// Schedule - Schedule the instruction range using list scheduling.
154 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000155 void Schedule();
Jim Grosbach90013032010-05-14 21:19:48 +0000156
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000157 /// Observe - Update liveness information to account for the current
158 /// instruction, which will not be scheduled.
159 ///
160 void Observe(MachineInstr *MI, unsigned Count);
161
162 /// FinishBlock - Clean up register live-range state.
163 ///
164 void FinishBlock();
165
David Goodwin2e7be612009-10-26 16:59:04 +0000166 /// FixupKills - Fix register kill flags that have been made
167 /// invalid due to scheduling
168 ///
169 void FixupKills(MachineBasicBlock *MBB);
170
Dan Gohman343f0c02008-11-19 23:18:57 +0000171 private:
David Goodwin557bbe62009-11-20 19:32:48 +0000172 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
173 void ReleaseSuccessors(SUnit *SU);
174 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
175 void ListScheduleTopDown();
David Goodwin5e411782009-09-03 22:15:25 +0000176 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbach90013032010-05-14 21:19:48 +0000177
David Goodwin8f909342009-09-23 16:35:25 +0000178 // ToggleKillFlag - Toggle a register operand kill flag. Other
179 // adjustments may be made to the instruction if necessary. Return
180 // true if the operand has been deleted, false if not.
181 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohman343f0c02008-11-19 23:18:57 +0000182 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000183}
184
Dan Gohman343f0c02008-11-19 23:18:57 +0000185bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000186 AA = &getAnalysis<AliasAnalysis>();
Evan Cheng86050dc2010-06-18 23:09:54 +0000187 TII = Fn.getTarget().getInstrInfo();
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000188
David Goodwin471850a2009-10-01 21:46:35 +0000189 // Check for explicit enable/disable of post-ra scheduling.
David Goodwin4c3715c2009-10-22 23:19:17 +0000190 TargetSubtarget::AntiDepBreakMode AntiDepMode = TargetSubtarget::ANTIDEP_NONE;
David Goodwin87d21b92009-11-13 19:52:48 +0000191 SmallVector<TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000192 if (EnablePostRAScheduler.getPosition() > 0) {
193 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000194 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000195 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000196 // Check that post-RA scheduling is enabled for this target.
David Goodwin471850a2009-10-01 21:46:35 +0000197 const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
David Goodwin87d21b92009-11-13 19:52:48 +0000198 if (!ST.enablePostRAScheduler(OptLevel, AntiDepMode, CriticalPathRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000199 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000200 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000201
David Goodwin4c3715c2009-10-22 23:19:17 +0000202 // Check for antidep breaking override...
203 if (EnableAntiDepBreaking.getPosition() > 0) {
Jim Grosbach90013032010-05-14 21:19:48 +0000204 AntiDepMode = (EnableAntiDepBreaking == "all") ?
205 TargetSubtarget::ANTIDEP_ALL :
206 (EnableAntiDepBreaking == "critical")
207 ? TargetSubtarget::ANTIDEP_CRITICAL : TargetSubtarget::ANTIDEP_NONE;
David Goodwin4c3715c2009-10-22 23:19:17 +0000208 }
209
David Greenee1b21292010-01-05 01:26:01 +0000210 DEBUG(dbgs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000211
Dan Gohman3f237442008-12-16 03:25:46 +0000212 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
213 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
Evan Cheng729aab32010-06-12 00:12:18 +0000214 const TargetMachine &TM = Fn.getTarget();
215 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
216 ScheduleHazardRecognizer *HR =
217 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins);
Jim Grosbach90013032010-05-14 21:19:48 +0000218 AntiDepBreaker *ADB =
David Goodwin34877712009-10-26 19:32:42 +0000219 ((AntiDepMode == TargetSubtarget::ANTIDEP_ALL) ?
David Goodwin87d21b92009-11-13 19:52:48 +0000220 (AntiDepBreaker *)new AggressiveAntiDepBreaker(Fn, CriticalPathRCs) :
Jim Grosbach90013032010-05-14 21:19:48 +0000221 ((AntiDepMode == TargetSubtarget::ANTIDEP_CRITICAL) ?
David Goodwin34877712009-10-26 19:32:42 +0000222 (AntiDepBreaker *)new CriticalAntiDepBreaker(Fn) : NULL));
Dan Gohman3f237442008-12-16 03:25:46 +0000223
David Goodwin2e7be612009-10-26 16:59:04 +0000224 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, ADB, AA);
Dan Gohman79ce2762009-01-15 19:20:50 +0000225
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000226 // Loop over all of the basic blocks
227 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000228 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000229#ifndef NDEBUG
230 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
231 if (DebugDiv > 0) {
232 static int bbcnt = 0;
233 if (bbcnt++ % DebugDiv != DebugMod)
234 continue;
David Greenee1b21292010-01-05 01:26:01 +0000235 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
Dan Gohman0ba90f32009-10-31 20:19:03 +0000236 ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000237 }
238#endif
239
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000240 // Initialize register live-range state for scheduling in this block.
241 Scheduler.StartBlock(MBB);
242
Dan Gohmanf7119392009-01-16 22:10:20 +0000243 // Schedule each sequence of instructions not interrupted by a label
244 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000245 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000246 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000247 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng86050dc2010-06-18 23:09:54 +0000248 MachineInstr *MI = llvm::prior(I);
249 if (TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000250 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000251 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000252 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000253 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000254 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000255 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000256 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000257 --Count;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000258 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000259 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000260 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000261 "Instruction count mismatch!");
262 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000263 Scheduler.EmitSchedule();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000264
265 // Clean up register live-range state.
266 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000267
David Goodwin5e411782009-09-03 22:15:25 +0000268 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000269 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000270 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000271
David Goodwin2e7be612009-10-26 16:59:04 +0000272 delete HR;
273 delete ADB;
274
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000275 return true;
276}
Jim Grosbach90013032010-05-14 21:19:48 +0000277
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000278/// StartBlock - Initialize register live-range state for scheduling in
279/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000280///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000281void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
282 // Call the superclass.
283 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000284
David Goodwin2e7be612009-10-26 16:59:04 +0000285 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000286 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000287 if (AntiDepBreak != NULL)
288 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000289}
290
291/// Schedule - Schedule the instruction range using list scheduling.
292///
293void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000294 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000295 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000296
David Goodwin2e7be612009-10-26 16:59:04 +0000297 if (AntiDepBreak != NULL) {
Jim Grosbach90013032010-05-14 21:19:48 +0000298 unsigned Broken =
David Goodwin557bbe62009-11-20 19:32:48 +0000299 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
300 InsertPosIndex);
Jim Grosbach90013032010-05-14 21:19:48 +0000301
David Goodwin557bbe62009-11-20 19:32:48 +0000302 if (Broken != 0) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000303 // We made changes. Update the dependency graph.
304 // Theoretically we could update the graph in place:
305 // When a live range is changed to use a different register, remove
306 // the def's anti-dependence *and* output-dependence edges due to
307 // that register, and add new anti-dependence and output-dependence
308 // edges based on the next live range of the register.
David Goodwin557bbe62009-11-20 19:32:48 +0000309 SUnits.clear();
310 Sequence.clear();
311 EntrySU = SUnit();
312 ExitSU = SUnit();
313 BuildSchedGraph(AA);
Jim Grosbach90013032010-05-14 21:19:48 +0000314
David Goodwin2e7be612009-10-26 16:59:04 +0000315 NumFixedAnti += Broken;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000316 }
317 }
318
David Greenee1b21292010-01-05 01:26:01 +0000319 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000320 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
321 SUnits[su].dumpAll(this));
322
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323 AvailableQueue.initNodes(SUnits);
David Goodwin557bbe62009-11-20 19:32:48 +0000324 ListScheduleTopDown();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000325 AvailableQueue.releaseState();
326}
327
328/// Observe - Update liveness information to account for the current
329/// instruction, which will not be scheduled.
330///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000331void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000332 if (AntiDepBreak != NULL)
333 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000334}
335
336/// FinishBlock - Clean up register live-range state.
337///
338void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000339 if (AntiDepBreak != NULL)
340 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000341
342 // Call the superclass.
343 ScheduleDAGInstrs::FinishBlock();
344}
345
David Goodwin5e411782009-09-03 22:15:25 +0000346/// StartBlockForKills - Initialize register live-range state for updating kills
347///
348void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
349 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +0000350 for (unsigned i = 0; i < TRI->getNumRegs(); ++i)
351 KillIndices[i] = ~0u;
David Goodwin5e411782009-09-03 22:15:25 +0000352
353 // Determine the live-out physregs for this block.
354 if (!BB->empty() && BB->back().getDesc().isReturn()) {
355 // In a return block, examine the function live-out regs.
356 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
357 E = MRI.liveout_end(); I != E; ++I) {
358 unsigned Reg = *I;
359 KillIndices[Reg] = BB->size();
360 // Repeat, for all subregs.
361 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
362 *Subreg; ++Subreg) {
363 KillIndices[*Subreg] = BB->size();
364 }
365 }
366 }
367 else {
368 // In a non-return block, examine the live-in regs of all successors.
369 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
370 SE = BB->succ_end(); SI != SE; ++SI) {
371 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
372 E = (*SI)->livein_end(); I != E; ++I) {
373 unsigned Reg = *I;
374 KillIndices[Reg] = BB->size();
375 // Repeat, for all subregs.
376 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
377 *Subreg; ++Subreg) {
378 KillIndices[*Subreg] = BB->size();
379 }
380 }
381 }
382 }
383}
384
David Goodwin8f909342009-09-23 16:35:25 +0000385bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
386 MachineOperand &MO) {
387 // Setting kill flag...
388 if (!MO.isKill()) {
389 MO.setIsKill(true);
390 return false;
391 }
Jim Grosbach90013032010-05-14 21:19:48 +0000392
David Goodwin8f909342009-09-23 16:35:25 +0000393 // If MO itself is live, clear the kill flag...
394 if (KillIndices[MO.getReg()] != ~0u) {
395 MO.setIsKill(false);
396 return false;
397 }
398
399 // If any subreg of MO is live, then create an imp-def for that
400 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000401 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000402 bool AllDead = true;
403 const unsigned SuperReg = MO.getReg();
404 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
405 *Subreg; ++Subreg) {
406 if (KillIndices[*Subreg] != ~0u) {
407 MI->addOperand(MachineOperand::CreateReg(*Subreg,
408 true /*IsDef*/,
409 true /*IsImp*/,
410 false /*IsKill*/,
411 false /*IsDead*/));
412 AllDead = false;
413 }
414 }
415
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000416 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000417 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000418 return false;
419}
420
David Goodwin88a589c2009-08-25 17:03:05 +0000421/// FixupKills - Fix the register kill flags, they may have been made
422/// incorrect by instruction reordering.
423///
424void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greenee1b21292010-01-05 01:26:01 +0000425 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000426
427 std::set<unsigned> killedRegs;
428 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000429
430 StartBlockForKills(MBB);
Jim Grosbach90013032010-05-14 21:19:48 +0000431
David Goodwin7886cd82009-08-29 00:11:13 +0000432 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000433 unsigned Count = MBB->size();
434 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
435 I != E; --Count) {
436 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000437 if (MI->isDebugValue())
438 continue;
David Goodwin88a589c2009-08-25 17:03:05 +0000439
David Goodwin7886cd82009-08-29 00:11:13 +0000440 // Update liveness. Registers that are defed but not used in this
441 // instruction are now dead. Mark register and all subregs as they
442 // are completely defined.
443 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
444 MachineOperand &MO = MI->getOperand(i);
445 if (!MO.isReg()) continue;
446 unsigned Reg = MO.getReg();
447 if (Reg == 0) continue;
448 if (!MO.isDef()) continue;
449 // Ignore two-addr defs.
450 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbach90013032010-05-14 21:19:48 +0000451
David Goodwin7886cd82009-08-29 00:11:13 +0000452 KillIndices[Reg] = ~0u;
Jim Grosbach90013032010-05-14 21:19:48 +0000453
David Goodwin7886cd82009-08-29 00:11:13 +0000454 // Repeat for all subregs.
455 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
456 *Subreg; ++Subreg) {
457 KillIndices[*Subreg] = ~0u;
458 }
459 }
David Goodwin88a589c2009-08-25 17:03:05 +0000460
David Goodwin8f909342009-09-23 16:35:25 +0000461 // Examine all used registers and set/clear kill flag. When a
462 // register is used multiple times we only set the kill flag on
463 // the first use.
David Goodwin88a589c2009-08-25 17:03:05 +0000464 killedRegs.clear();
465 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
466 MachineOperand &MO = MI->getOperand(i);
467 if (!MO.isReg() || !MO.isUse()) continue;
468 unsigned Reg = MO.getReg();
469 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
470
David Goodwin7886cd82009-08-29 00:11:13 +0000471 bool kill = false;
472 if (killedRegs.find(Reg) == killedRegs.end()) {
473 kill = true;
474 // A register is not killed if any subregs are live...
475 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
476 *Subreg; ++Subreg) {
477 if (KillIndices[*Subreg] != ~0u) {
478 kill = false;
479 break;
480 }
481 }
482
483 // If subreg is not live, then register is killed if it became
484 // live in this instruction
485 if (kill)
486 kill = (KillIndices[Reg] == ~0u);
487 }
Jim Grosbach90013032010-05-14 21:19:48 +0000488
David Goodwin88a589c2009-08-25 17:03:05 +0000489 if (MO.isKill() != kill) {
David Greenee1b21292010-01-05 01:26:01 +0000490 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen15d75d92009-12-03 01:49:56 +0000491 // Warning: ToggleKillFlag may invalidate MO.
492 ToggleKillFlag(MI, MO);
David Goodwin88a589c2009-08-25 17:03:05 +0000493 DEBUG(MI->dump());
494 }
Jim Grosbach90013032010-05-14 21:19:48 +0000495
David Goodwin88a589c2009-08-25 17:03:05 +0000496 killedRegs.insert(Reg);
497 }
Jim Grosbach90013032010-05-14 21:19:48 +0000498
David Goodwina3251db2009-08-31 20:47:02 +0000499 // Mark any used register (that is not using undef) and subregs as
500 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000501 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
502 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000503 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000504 unsigned Reg = MO.getReg();
505 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
506
David Goodwin7886cd82009-08-29 00:11:13 +0000507 KillIndices[Reg] = Count;
Jim Grosbach90013032010-05-14 21:19:48 +0000508
David Goodwin7886cd82009-08-29 00:11:13 +0000509 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
510 *Subreg; ++Subreg) {
511 KillIndices[*Subreg] = Count;
512 }
513 }
David Goodwin88a589c2009-08-25 17:03:05 +0000514 }
515}
516
Dan Gohman343f0c02008-11-19 23:18:57 +0000517//===----------------------------------------------------------------------===//
518// Top-Down Scheduling
519//===----------------------------------------------------------------------===//
520
521/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
522/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin557bbe62009-11-20 19:32:48 +0000523void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000524 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000525
Dan Gohman343f0c02008-11-19 23:18:57 +0000526#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000527 if (SuccSU->NumPredsLeft == 0) {
David Greenee1b21292010-01-05 01:26:01 +0000528 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000529 SuccSU->dump(this);
David Greenee1b21292010-01-05 01:26:01 +0000530 dbgs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000531 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000532 }
533#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000534 --SuccSU->NumPredsLeft;
535
Dan Gohman343f0c02008-11-19 23:18:57 +0000536 // Compute how many cycles it will be before this actually becomes
537 // available. This is the max of the start time of all predecessors plus
538 // their latencies.
David Goodwin557bbe62009-11-20 19:32:48 +0000539 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Jim Grosbach90013032010-05-14 21:19:48 +0000540
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000541 // If all the node's predecessors are scheduled, this node is ready
542 // to be scheduled. Ignore the special ExitSU node.
543 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000544 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000545}
546
547/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin557bbe62009-11-20 19:32:48 +0000548void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000549 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000550 I != E; ++I) {
David Goodwin557bbe62009-11-20 19:32:48 +0000551 ReleaseSucc(SU, &*I);
David Goodwin4de099d2009-11-03 20:57:50 +0000552 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000553}
554
555/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
556/// count of its successors. If a successor pending count is zero, add it to
557/// the Available queue.
David Goodwin557bbe62009-11-20 19:32:48 +0000558void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greenee1b21292010-01-05 01:26:01 +0000559 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000560 DEBUG(SU->dump(this));
Jim Grosbach90013032010-05-14 21:19:48 +0000561
Dan Gohman343f0c02008-11-19 23:18:57 +0000562 Sequence.push_back(SU);
Jim Grosbach90013032010-05-14 21:19:48 +0000563 assert(CurCycle >= SU->getDepth() &&
David Goodwin4de099d2009-11-03 20:57:50 +0000564 "Node scheduled above its depth!");
David Goodwin557bbe62009-11-20 19:32:48 +0000565 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +0000566
David Goodwin557bbe62009-11-20 19:32:48 +0000567 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000568 SU->isScheduled = true;
569 AvailableQueue.ScheduledNode(SU);
570}
571
572/// ListScheduleTopDown - The main loop of list scheduling for top-down
573/// schedulers.
David Goodwin557bbe62009-11-20 19:32:48 +0000574void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000575 unsigned CurCycle = 0;
Jim Grosbach90013032010-05-14 21:19:48 +0000576
David Goodwin4de099d2009-11-03 20:57:50 +0000577 // We're scheduling top-down but we're visiting the regions in
578 // bottom-up order, so we don't know the hazards at the start of a
579 // region. So assume no hazards (this should usually be ok as most
580 // blocks are a single region).
581 HazardRec->Reset();
582
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000583 // Release any successors of the special Entry node.
David Goodwin557bbe62009-11-20 19:32:48 +0000584 ReleaseSuccessors(&EntrySU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000585
David Goodwin557bbe62009-11-20 19:32:48 +0000586 // Add all leaves to Available queue.
Dan Gohman343f0c02008-11-19 23:18:57 +0000587 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
588 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000589 bool available = SUnits[i].Preds.empty();
David Goodwin4de099d2009-11-03 20:57:50 +0000590 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000591 AvailableQueue.push(&SUnits[i]);
592 SUnits[i].isAvailable = true;
593 }
594 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000595
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000596 // In any cycle where we can't schedule any instructions, we must
597 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000598 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000599
Dan Gohman343f0c02008-11-19 23:18:57 +0000600 // While Available queue is not empty, grab the node with the highest
601 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000602 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000603 Sequence.reserve(SUnits.size());
604 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
605 // Check to see if any of the pending instructions are ready to issue. If
606 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000607 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000608 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin557bbe62009-11-20 19:32:48 +0000609 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000610 AvailableQueue.push(PendingQueue[i]);
611 PendingQueue[i]->isAvailable = true;
612 PendingQueue[i] = PendingQueue.back();
613 PendingQueue.pop_back();
614 --i; --e;
David Goodwin557bbe62009-11-20 19:32:48 +0000615 } else if (PendingQueue[i]->getDepth() < MinDepth)
616 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +0000617 }
David Goodwinc93d8372009-08-11 17:35:23 +0000618
David Greenee1b21292010-01-05 01:26:01 +0000619 DEBUG(dbgs() << "\n*** Examining Available\n";
David Goodwin7cd01182009-08-11 17:56:42 +0000620 LatencyPriorityQueue q = AvailableQueue;
621 while (!q.empty()) {
622 SUnit *su = q.pop();
David Greenee1b21292010-01-05 01:26:01 +0000623 dbgs() << "Height " << su->getHeight() << ": ";
David Goodwin7cd01182009-08-11 17:56:42 +0000624 su->dump(this);
625 });
David Goodwinc93d8372009-08-11 17:35:23 +0000626
Dan Gohman2836c282009-01-16 01:33:36 +0000627 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000628 bool HasNoopHazards = false;
629 while (!AvailableQueue.empty()) {
630 SUnit *CurSUnit = AvailableQueue.pop();
631
632 ScheduleHazardRecognizer::HazardType HT =
633 HazardRec->getHazardType(CurSUnit);
634 if (HT == ScheduleHazardRecognizer::NoHazard) {
635 FoundSUnit = CurSUnit;
636 break;
637 }
638
639 // Remember if this is a noop hazard.
640 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
641
642 NotReady.push_back(CurSUnit);
643 }
644
645 // Add the nodes that aren't ready back onto the available list.
646 if (!NotReady.empty()) {
647 AvailableQueue.push_all(NotReady);
648 NotReady.clear();
649 }
650
David Goodwin4de099d2009-11-03 20:57:50 +0000651 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000652 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000653 // ... schedule the node...
David Goodwin557bbe62009-11-20 19:32:48 +0000654 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +0000655 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000656 CycleHasInsts = true;
Dan Gohman2836c282009-01-16 01:33:36 +0000657 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000658 if (CycleHasInsts) {
David Greenee1b21292010-01-05 01:26:01 +0000659 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000660 HazardRec->AdvanceCycle();
661 } else if (!HasNoopHazards) {
662 // Otherwise, we have a pipeline stall, but no other problem,
663 // just advance the current cycle and try again.
David Greenee1b21292010-01-05 01:26:01 +0000664 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000665 HazardRec->AdvanceCycle();
David Goodwin557bbe62009-11-20 19:32:48 +0000666 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000667 } else {
668 // Otherwise, we have no instructions to issue and we have instructions
669 // that will fault if we don't do this right. This is the case for
670 // processors without pipeline interlocks and other cases.
David Greenee1b21292010-01-05 01:26:01 +0000671 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000672 HazardRec->EmitNoop();
673 Sequence.push_back(0); // NULL here means noop
David Goodwin557bbe62009-11-20 19:32:48 +0000674 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000675 }
676
Dan Gohman2836c282009-01-16 01:33:36 +0000677 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000678 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000679 }
680 }
681
682#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000683 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000684#endif
685}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000686
687//===----------------------------------------------------------------------===//
688// Public Constructor Functions
689//===----------------------------------------------------------------------===//
690
Evan Chengfa163542009-10-16 21:06:15 +0000691FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) {
692 return new PostRAScheduler(OptLevel);
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000693}