blob: df706cbec6db4507c6c6764e65f80d3ce3dc3c4d [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 {
37/// MachineSchedulerPass runs after coalescing and before register allocation.
38class MachineSchedulerPass : public MachineFunctionPass {
39public:
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
45 MachineSchedulerPass();
46
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
59char MachineSchedulerPass::ID = 0;
60
61char &llvm::MachineSchedulerPassID = MachineSchedulerPass::ID;
62
63INITIALIZE_PASS_BEGIN(MachineSchedulerPass, "misched",
64 "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)
71INITIALIZE_PASS_END(MachineSchedulerPass, "misched",
72 "Machine Instruction Scheduler", false, false)
73
74MachineSchedulerPass::MachineSchedulerPass()
75: MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) {
76 initializeMachineSchedulerPassPass(*PassRegistry::getPassRegistry());
77}
78
79void MachineSchedulerPass::getAnalysisUsage(AnalysisUsage &AU) const {
80 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:
105 typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineSchedulerPass *);
106
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
140static ScheduleDAGInstrs *createDefaultMachineSched(MachineSchedulerPass *P);
141
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//===----------------------------------------------------------------------===//
150// Machine Instruction Scheduling Implementation
151//===----------------------------------------------------------------------===//
152
153namespace {
154/// MachineScheduler is an implementation of ScheduleDAGInstrs that schedules
155/// machine instructions while updating LiveIntervals.
156class MachineScheduler : public ScheduleDAGInstrs {
157 MachineSchedulerPass *Pass;
158public:
159 MachineScheduler(MachineSchedulerPass *P):
Andrew Trick5e920d72012-01-14 02:17:12 +0000160 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
Andrew Trick5edf2f02012-01-14 02:17:06 +0000161
162 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
163 /// time to do some work.
164 virtual void Schedule();
165};
166} // namespace
167
Andrew Trick96f678f2012-01-13 06:30:30 +0000168static ScheduleDAGInstrs *createDefaultMachineSched(MachineSchedulerPass *P) {
169 return new MachineScheduler(P);
170}
171static MachineSchedRegistry
172SchedDefaultRegistry("default", "Activate the scheduler pass, "
173 "but don't reorder instructions",
174 createDefaultMachineSched);
175
176/// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
177/// time to do some work.
178void MachineScheduler::Schedule() {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000179 BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>());
180
Andrew Trick96f678f2012-01-13 06:30:30 +0000181 DEBUG(dbgs() << "********** MI Scheduling **********\n");
182 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
183 SUnits[su].dumpAll(this));
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000184
Andrew Trick96f678f2012-01-13 06:30:30 +0000185 // TODO: Put interesting things here.
186}
187
188bool MachineSchedulerPass::runOnMachineFunction(MachineFunction &mf) {
189 // Initialize the context of the pass.
190 MF = &mf;
191 MLI = &getAnalysis<MachineLoopInfo>();
192 MDT = &getAnalysis<MachineDominatorTree>();
Andrew Trick5edf2f02012-01-14 02:17:06 +0000193 TII = MF->getTarget().getInstrInfo();
Andrew Trick96f678f2012-01-13 06:30:30 +0000194
195 // Select the scheduler, or set the default.
196 MachineSchedRegistry::ScheduleDAGCtor Ctor =
197 MachineSchedRegistry::getDefault();
198 if (!Ctor) {
199 Ctor = MachineSchedOpt;
200 MachineSchedRegistry::setDefault(Ctor);
201 }
202 // Instantiate the selected scheduler.
203 OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
204
205 // Visit all machine basic blocks.
206 for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
207 MBB != MBBEnd; ++MBB) {
208
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000209 // Break the block into scheduling regions [I, RegionEnd), and schedule each
210 // region as soon as it is discovered.
211 unsigned RemainingCount = MBB->size();
212 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
213 RegionEnd != MBB->begin();) {
214 // The next region starts above the previous region. Look backward in the
215 // instruction stream until we find the nearest boundary.
216 MachineBasicBlock::iterator I = RegionEnd;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000217 for(;I != MBB->begin(); --I, --RemainingCount) {
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000218 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
219 break;
220 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000221 if (I == RegionEnd) {
222 // Skip empty scheduling regions.
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000223 RegionEnd = llvm::prior(RegionEnd);
Andrew Trick3c58ba82012-01-14 02:17:18 +0000224 --RemainingCount;
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000225 continue;
226 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000227 // Schedule regions with more than one instruction.
228 if (I != llvm::prior(RegionEnd)) {
229 DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName()
230 << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: "
231 << *RegionEnd << " Remaining: " << RemainingCount << "\n");
Andrew Trick96f678f2012-01-13 06:30:30 +0000232
Andrew Trick3c58ba82012-01-14 02:17:18 +0000233 // Inform ScheduleDAGInstrs of the region being scheduled. It calls back
234 // to our Schedule() method.
235 Scheduler->Run(MBB, I, RegionEnd, MBB->size());
236 }
Andrew Tricke9ef4ed2012-01-14 02:17:09 +0000237 RegionEnd = I;
238 }
239 assert(RemainingCount == 0 && "Instruction count mismatch!");
Andrew Trick96f678f2012-01-13 06:30:30 +0000240 }
241 return true;
242}
243
244void MachineSchedulerPass::print(raw_ostream &O, const Module* m) const {
245 // unimplemented
246}
247
Andrew Trick5edf2f02012-01-14 02:17:06 +0000248//===----------------------------------------------------------------------===//
249// Machine Instruction Shuffler for Correctness Testing
250//===----------------------------------------------------------------------===//
251
Andrew Trick96f678f2012-01-13 06:30:30 +0000252#ifndef NDEBUG
253namespace {
254/// Reorder instructions as much as possible.
255class InstructionShuffler : public ScheduleDAGInstrs {
Andrew Trick5edf2f02012-01-14 02:17:06 +0000256 MachineSchedulerPass *Pass;
Andrew Trick96f678f2012-01-13 06:30:30 +0000257public:
Andrew Trick5edf2f02012-01-14 02:17:06 +0000258 InstructionShuffler(MachineSchedulerPass *P):
Andrew Trick5e920d72012-01-14 02:17:12 +0000259 ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false), Pass(P) {}
Andrew Trick96f678f2012-01-13 06:30:30 +0000260
261 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
262 /// time to do some work.
263 virtual void Schedule() {
264 llvm_unreachable("unimplemented");
265 }
266};
267} // namespace
268
269static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedulerPass *P) {
270 return new InstructionShuffler(P);
271}
272static MachineSchedRegistry ShufflerRegistry("shuffle",
273 "Shuffle machine instructions",
274 createInstructionShuffler);
275#endif // !NDEBUG