blob: 53cb672dc457624a4155edd68b3a28f477ee933a [file] [log] [blame]
Andrew Trick96f678f2012-01-13 06:30:30 +00001//===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// MachineScheduler schedules machine instructions after phi elimination. It
11// preserves LiveIntervals so it can be invoked before register allocation.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "misched"
16
17#include "ScheduleDAGInstrs.h"
18#include "LiveDebugVariables.h"
19#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "llvm/CodeGen/MachinePassRegistry.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Andrew Tricke9ef4ed2012-01-14 02:17:09 +000023#include "llvm/Target/TargetInstrInfo.h"
Andrew Trick96f678f2012-01-13 06:30:30 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/ADT/OwningPtr.h"
29
30using namespace llvm;
31
Andrew Trick5edf2f02012-01-14 02:17:06 +000032//===----------------------------------------------------------------------===//
33// Machine Instruction Scheduling Pass and Registry
34//===----------------------------------------------------------------------===//
35
Andrew Trick96f678f2012-01-13 06:30:30 +000036namespace {
Andrew Trick42b7a712012-01-17 06:55:03 +000037/// MachineScheduler runs after coalescing and before register allocation.
38class MachineScheduler : public MachineFunctionPass {
Andrew Trick96f678f2012-01-13 06:30:30 +000039public:
40 MachineFunction *MF;
Andrew Trick5edf2f02012-01-14 02:17:06 +000041 const TargetInstrInfo *TII;
Andrew Trick96f678f2012-01-13 06:30:30 +000042 const MachineLoopInfo *MLI;
43 const MachineDominatorTree *MDT;
44
Andrew Trick42b7a712012-01-17 06:55:03 +000045 MachineScheduler();
Andrew Trick96f678f2012-01-13 06:30:30 +000046
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
48
49 virtual void releaseMemory() {}
50
51 virtual bool runOnMachineFunction(MachineFunction&);
52
53 virtual void print(raw_ostream &O, const Module* = 0) const;
54
55 static char ID; // Class identification, replacement for typeinfo
56};
57} // namespace
58
Andrew Trick42b7a712012-01-17 06:55:03 +000059char MachineScheduler::ID = 0;
Andrew Trick96f678f2012-01-13 06:30:30 +000060
Andrew Trick42b7a712012-01-17 06:55:03 +000061char &llvm::MachineSchedulerID = MachineScheduler::ID;
Andrew Trick96f678f2012-01-13 06:30:30 +000062
Andrew Trick42b7a712012-01-17 06:55:03 +000063INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000064 "Machine Instruction Scheduler", false, false)
65INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
66INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
67INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
68INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
69INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination)
70INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
Andrew Trick42b7a712012-01-17 06:55:03 +000071INITIALIZE_PASS_END(MachineScheduler, "misched",
Andrew Trick96f678f2012-01-13 06:30:30 +000072 "Machine Instruction Scheduler", false, false)
73
Andrew Trick42b7a712012-01-17 06:55:03 +000074MachineScheduler::MachineScheduler()
Andrew Trick96f678f2012-01-13 06:30:30 +000075: MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) {
Andrew Trick42b7a712012-01-17 06:55:03 +000076 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick96f678f2012-01-13 06:30:30 +000077}
78
Andrew Trick42b7a712012-01-17 06:55:03 +000079void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Trick96f678f2012-01-13 06:30:30 +000080 AU.setPreservesCFG();
81 AU.addRequiredID(MachineDominatorsID);
82 AU.addRequired<MachineLoopInfo>();
83 AU.addRequired<AliasAnalysis>();
84 AU.addPreserved<AliasAnalysis>();
85 AU.addRequired<SlotIndexes>();
86 AU.addPreserved<SlotIndexes>();
87 AU.addRequired<LiveIntervals>();
88 AU.addPreserved<LiveIntervals>();
89 AU.addRequired<LiveDebugVariables>();
90 AU.addPreserved<LiveDebugVariables>();
91 if (StrongPHIElim) {
92 AU.addRequiredID(StrongPHIEliminationID);
93 AU.addPreservedID(StrongPHIEliminationID);
94 }
95 AU.addRequiredID(RegisterCoalescerPassID);
96 AU.addPreservedID(RegisterCoalescerPassID);
97 MachineFunctionPass::getAnalysisUsage(AU);
98}
99
100namespace {
Andrew Trick96f678f2012-01-13 06:30:30 +0000101/// MachineSchedRegistry provides a selection of available machine instruction
102/// schedulers.
103class MachineSchedRegistry : public MachinePassRegistryNode {
104public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000105 typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineScheduler *);
Andrew Trick96f678f2012-01-13 06:30:30 +0000106
107 // RegisterPassParser requires a (misnamed) FunctionPassCtor type.
108 typedef ScheduleDAGCtor FunctionPassCtor;
109
110 static MachinePassRegistry Registry;
111
112 MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C)
113 : MachinePassRegistryNode(N, D, (MachinePassCtor)C) {
114 Registry.Add(this);
115 }
116 ~MachineSchedRegistry() { Registry.Remove(this); }
117
118 // Accessors.
119 //
120 MachineSchedRegistry *getNext() const {
121 return (MachineSchedRegistry *)MachinePassRegistryNode::getNext();
122 }
123 static MachineSchedRegistry *getList() {
124 return (MachineSchedRegistry *)Registry.getList();
125 }
126 static ScheduleDAGCtor getDefault() {
127 return (ScheduleDAGCtor)Registry.getDefault();
128 }
129 static void setDefault(ScheduleDAGCtor C) {
130 Registry.setDefault((MachinePassCtor)C);
131 }
132 static void setListener(MachinePassRegistryListener *L) {
133 Registry.setListener(L);
134 }
135};
136} // namespace
137
138MachinePassRegistry MachineSchedRegistry::Registry;
139
Andrew Trick42b7a712012-01-17 06:55:03 +0000140static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P);
Andrew Trick96f678f2012-01-13 06:30:30 +0000141
142/// MachineSchedOpt allows command line selection of the scheduler.
143static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
144 RegisterPassParser<MachineSchedRegistry> >
145MachineSchedOpt("misched",
146 cl::init(&createDefaultMachineSched), cl::Hidden,
147 cl::desc("Machine instruction scheduler to use"));
148
Andrew Trick5edf2f02012-01-14 02:17:06 +0000149//===----------------------------------------------------------------------===//
Andrew Trick42b7a712012-01-17 06:55:03 +0000150// Machine Instruction Scheduling Common Implementation
Andrew Trick5edf2f02012-01-14 02:17:06 +0000151//===----------------------------------------------------------------------===//
152
153namespace {
154/// MachineScheduler is an implementation of ScheduleDAGInstrs that schedules
155/// machine instructions while updating LiveIntervals.
Andrew Trick42b7a712012-01-17 06:55:03 +0000156class ScheduleTopDownLive : public ScheduleDAGInstrs {
157protected:
158 MachineScheduler *Pass;
Andrew Trick5edf2f02012-01-14 02:17:06 +0000159public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000160 ScheduleTopDownLive(MachineScheduler *P):
Andrew Trick5e920d72012-01-14 02:17:12 +0000161 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
Andrew Trick5edf2f02012-01-14 02:17:06 +0000162};
163} // namespace
164
Andrew Trick42b7a712012-01-17 06:55:03 +0000165bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000166 // Initialize the context of the pass.
167 MF = &mf;
168 MLI = &getAnalysis<MachineLoopInfo>();
169 MDT = &getAnalysis<MachineDominatorTree>();
Andrew Trick5edf2f02012-01-14 02:17:06 +0000170 TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000171
172 // Select the scheduler, or set the default.
173 MachineSchedRegistry::ScheduleDAGCtor Ctor =
174 MachineSchedRegistry::getDefault();
175 if (!Ctor) {
176 Ctor = MachineSchedOpt;
177 MachineSchedRegistry::setDefault(Ctor);
178 }
179 // Instantiate the selected scheduler.
180 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
181
182 // Visit all machine basic blocks.
183 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
184 MBB != MBBEnd; ++MBB) {
185
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000186 // Break the block into scheduling regions [I, RegionEnd), and schedule each
187 // region as soon as it is discovered.
188 unsigned RemainingCount = MBB->size();
189 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
190 RegionEnd != MBB->begin();) {
191 // The next region starts above the previous region. Look backward in the
192 // instruction stream until we find the nearest boundary.
193 MachineBasicBlock::iterator I = RegionEnd;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000194 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000195 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
196 break;
197 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000198 if (I == RegionEnd) {
199 // Skip empty scheduling regions.
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000200 RegionEnd = llvm::prior(RegionEnd);
Andrew Trick3c58ba82012-01-14 02:17:18 +0000201 --RemainingCount;
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000202 continue;
203 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000204 // Schedule regions with more than one instruction.
205 if (I != llvm::prior(RegionEnd)) {
206 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
207 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: "
208 << *RegionEnd << " Remaining: " << RemainingCount << "\n");
Andrew Trick96f678f2012-01-13 06:30:30 +0000209
Andrew Trick3c58ba82012-01-14 02:17:18 +0000210 // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
211 // to our Schedule() method.
212 Scheduler->Run(MBB, I, RegionEnd, MBB->size());
213 }
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000214 RegionEnd = I;
215 }
216 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick96f678f2012-01-13 06:30:30 +0000217 }
218 return true;
219}
220
Andrew Trick42b7a712012-01-17 06:55:03 +0000221void MachineScheduler::print(raw_ostream &O, const Module* m) const {
Andrew Trick96f678f2012-01-13 06:30:30 +0000222 // unimplemented
223}
224
Andrew Trick5edf2f02012-01-14 02:17:06 +0000225//===----------------------------------------------------------------------===//
Andrew Trick42b7a712012-01-17 06:55:03 +0000226// Placeholder for extending the machine instruction scheduler.
227//===----------------------------------------------------------------------===//
228
229namespace {
230class DefaultMachineScheduler : public ScheduleTopDownLive {
231public:
232 DefaultMachineScheduler(MachineScheduler *P):
233 ScheduleTopDownLive(P) {}
234
235 void Schedule();
236};
237} // namespace
238
239static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P) {
240 return new DefaultMachineScheduler(P);
241}
242static MachineSchedRegistry
243SchedDefaultRegistry("default", "Activate the scheduler pass, "
244 "but don't reorder instructions",
245 createDefaultMachineSched);
246
247
248/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
249/// time to do some work.
250void DefaultMachineScheduler::Schedule() {
251 BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
252
253 DEBUG(dbgs() << "********** MI Scheduling **********\n");
254 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
255 SUnits[su].dumpAll(this));
256
257 // TODO: Put interesting things here.
258}
259
260//===----------------------------------------------------------------------===//
Andrew Trick5edf2f02012-01-14 02:17:06 +0000261// Machine Instruction Shuffler for Correctness Testing
262//===----------------------------------------------------------------------===//
263
Andrew Trick96f678f2012-01-13 06:30:30 +0000264#ifndef NDEBUG
265namespace {
266/// Reorder instructions as much as possible.
Andrew Trick42b7a712012-01-17 06:55:03 +0000267class InstructionShuffler : public ScheduleTopDownLive {
268 MachineScheduler *Pass;
Andrew Trick96f678f2012-01-13 06:30:30 +0000269public:
Andrew Trick42b7a712012-01-17 06:55:03 +0000270 InstructionShuffler(MachineScheduler *P):
271 ScheduleTopDownLive(P) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000272
273 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
274 /// time to do some work.
275 virtual void Schedule() {
276 llvm_unreachable("unimplemented");
277 }
278};
279} // namespace
280
Andrew Trick42b7a712012-01-17 06:55:03 +0000281static ScheduleDAGInstrs *createInstructionShuffler(MachineScheduler *P) {
Andrew Trick96f678f2012-01-13 06:30:30 +0000282 return new InstructionShuffler(P);
283}
284static MachineSchedRegistry ShufflerRegistry("shuffle",
285 "Shuffle machine instructions",
286 createInstructionShuffler);
287#endif // !NDEBUG