Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1 | //===- 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" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 19 | #include "llvm/CodeGen/MachinePassRegistry.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/ADT/OwningPtr.h" |
| 28 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 29 | #include <queue> |
| 30 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 33 | #ifndef NDEBUG |
| 34 | static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden, |
| 35 | cl::desc("Pop up a window to show MISched dags after they are processed")); |
| 36 | #else |
| 37 | static bool ViewMISchedDAGs = false; |
| 38 | #endif // NDEBUG |
| 39 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Machine Instruction Scheduling Pass and Registry |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 44 | namespace { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 45 | /// MachineScheduler runs after coalescing and before register allocation. |
| 46 | class MachineScheduler : public MachineFunctionPass { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 47 | public: |
| 48 | MachineFunction *MF; |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 49 | const TargetInstrInfo *TII; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 50 | const MachineLoopInfo *MLI; |
| 51 | const MachineDominatorTree *MDT; |
Lang Hames | 907cc8f | 2012-01-27 22:36:19 +0000 | [diff] [blame] | 52 | LiveIntervals *LIS; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 53 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 54 | MachineScheduler(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 55 | |
| 56 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 57 | |
| 58 | virtual void releaseMemory() {} |
| 59 | |
| 60 | virtual bool runOnMachineFunction(MachineFunction&); |
| 61 | |
| 62 | virtual void print(raw_ostream &O, const Module* = 0) const; |
| 63 | |
| 64 | static char ID; // Class identification, replacement for typeinfo |
| 65 | }; |
| 66 | } // namespace |
| 67 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 68 | char MachineScheduler::ID = 0; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 69 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 70 | char &llvm::MachineSchedulerID = MachineScheduler::ID; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 71 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 72 | INITIALIZE_PASS_BEGIN(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 73 | "Machine Instruction Scheduler", false, false) |
| 74 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 75 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 76 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 77 | INITIALIZE_PASS_END(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 78 | "Machine Instruction Scheduler", false, false) |
| 79 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 80 | MachineScheduler::MachineScheduler() |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 81 | : MachineFunctionPass(ID), MF(0), MLI(0), MDT(0) { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 82 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 85 | void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 86 | AU.setPreservesCFG(); |
| 87 | AU.addRequiredID(MachineDominatorsID); |
| 88 | AU.addRequired<MachineLoopInfo>(); |
| 89 | AU.addRequired<AliasAnalysis>(); |
| 90 | AU.addPreserved<AliasAnalysis>(); |
| 91 | AU.addRequired<SlotIndexes>(); |
| 92 | AU.addPreserved<SlotIndexes>(); |
| 93 | AU.addRequired<LiveIntervals>(); |
| 94 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 95 | MachineFunctionPass::getAnalysisUsage(AU); |
| 96 | } |
| 97 | |
| 98 | namespace { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 99 | /// MachineSchedRegistry provides a selection of available machine instruction |
| 100 | /// schedulers. |
| 101 | class MachineSchedRegistry : public MachinePassRegistryNode { |
| 102 | public: |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 103 | typedef ScheduleDAGInstrs *(*ScheduleDAGCtor)(MachineScheduler *); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 104 | |
| 105 | // RegisterPassParser requires a (misnamed) FunctionPassCtor type. |
| 106 | typedef ScheduleDAGCtor FunctionPassCtor; |
| 107 | |
| 108 | static MachinePassRegistry Registry; |
| 109 | |
| 110 | MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C) |
| 111 | : MachinePassRegistryNode(N, D, (MachinePassCtor)C) { |
| 112 | Registry.Add(this); |
| 113 | } |
| 114 | ~MachineSchedRegistry() { Registry.Remove(this); } |
| 115 | |
| 116 | // Accessors. |
| 117 | // |
| 118 | MachineSchedRegistry *getNext() const { |
| 119 | return (MachineSchedRegistry *)MachinePassRegistryNode::getNext(); |
| 120 | } |
| 121 | static MachineSchedRegistry *getList() { |
| 122 | return (MachineSchedRegistry *)Registry.getList(); |
| 123 | } |
| 124 | static ScheduleDAGCtor getDefault() { |
| 125 | return (ScheduleDAGCtor)Registry.getDefault(); |
| 126 | } |
| 127 | static void setDefault(ScheduleDAGCtor C) { |
| 128 | Registry.setDefault((MachinePassCtor)C); |
| 129 | } |
| 130 | static void setListener(MachinePassRegistryListener *L) { |
| 131 | Registry.setListener(L); |
| 132 | } |
| 133 | }; |
| 134 | } // namespace |
| 135 | |
| 136 | MachinePassRegistry MachineSchedRegistry::Registry; |
| 137 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 138 | static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 139 | |
| 140 | /// MachineSchedOpt allows command line selection of the scheduler. |
| 141 | static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false, |
| 142 | RegisterPassParser<MachineSchedRegistry> > |
| 143 | MachineSchedOpt("misched", |
| 144 | cl::init(&createDefaultMachineSched), cl::Hidden, |
| 145 | cl::desc("Machine instruction scheduler to use")); |
| 146 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 148 | // Machine Instruction Scheduling Common Implementation |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
| 150 | |
| 151 | namespace { |
Andrew Trick | 78b2961 | 2012-02-09 00:40:52 +0000 | [diff] [blame] | 152 | /// ScheduleTopDownLive is an implementation of ScheduleDAGInstrs that schedules |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 153 | /// machine instructions while updating LiveIntervals. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 154 | class ScheduleTopDownLive : public ScheduleDAGInstrs { |
| 155 | protected: |
| 156 | MachineScheduler *Pass; |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 157 | public: |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 158 | ScheduleTopDownLive(MachineScheduler *P): |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 159 | ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false, P->LIS), |
| 160 | Pass(P) {} |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 161 | |
| 162 | /// ScheduleDAGInstrs callback. |
| 163 | void Schedule(); |
| 164 | |
| 165 | /// Interface implemented by the selected top-down liveinterval scheduler. |
| 166 | /// |
| 167 | /// Pick the next node to schedule, or return NULL. |
| 168 | virtual SUnit *pickNode() = 0; |
| 169 | |
| 170 | /// When all preceeding dependencies have been resolved, free this node for |
| 171 | /// scheduling. |
| 172 | virtual void releaseNode(SUnit *SU) = 0; |
| 173 | |
| 174 | protected: |
| 175 | void releaseSucc(SUnit *SU, SDep *SuccEdge); |
| 176 | void releaseSuccessors(SUnit *SU); |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 177 | }; |
| 178 | } // namespace |
| 179 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 180 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When |
| 181 | /// NumPredsLeft reaches zero, release the successor node. |
| 182 | void ScheduleTopDownLive::releaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 183 | SUnit *SuccSU = SuccEdge->getSUnit(); |
| 184 | |
| 185 | #ifndef NDEBUG |
| 186 | if (SuccSU->NumPredsLeft == 0) { |
| 187 | dbgs() << "*** Scheduling failed! ***\n"; |
| 188 | SuccSU->dump(this); |
| 189 | dbgs() << " has been released too many times!\n"; |
| 190 | llvm_unreachable(0); |
| 191 | } |
| 192 | #endif |
| 193 | --SuccSU->NumPredsLeft; |
| 194 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
| 195 | releaseNode(SuccSU); |
| 196 | } |
| 197 | |
| 198 | /// releaseSuccessors - Call releaseSucc on each of SU's successors. |
| 199 | void ScheduleTopDownLive::releaseSuccessors(SUnit *SU) { |
| 200 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 201 | I != E; ++I) { |
| 202 | releaseSucc(SU, &*I); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's |
| 207 | /// time to do some work. |
| 208 | void ScheduleTopDownLive::Schedule() { |
| 209 | BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>()); |
| 210 | |
| 211 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
| 212 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 213 | SUnits[su].dumpAll(this)); |
| 214 | |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 215 | if (ViewMISchedDAGs) viewGraph(); |
| 216 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 217 | // Release any successors of the special Entry node. It is currently unused, |
| 218 | // but we keep up appearances. |
| 219 | releaseSuccessors(&EntrySU); |
| 220 | |
| 221 | // Release all DAG roots for scheduling. |
| 222 | for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end(); |
| 223 | I != E; ++I) { |
| 224 | // A SUnit is ready to schedule if it has no predecessors. |
| 225 | if (I->Preds.empty()) |
| 226 | releaseNode(&(*I)); |
| 227 | } |
| 228 | |
| 229 | InsertPos = Begin; |
| 230 | while (SUnit *SU = pickNode()) { |
| 231 | DEBUG(dbgs() << "*** Scheduling Instruction:\n"; SU->dump(this)); |
| 232 | |
| 233 | // Move the instruction to its new location in the instruction stream. |
| 234 | MachineInstr *MI = SU->getInstr(); |
| 235 | if (&*InsertPos == MI) |
| 236 | ++InsertPos; |
| 237 | else { |
Lang Hames | da7984f | 2012-02-15 01:23:52 +0000 | [diff] [blame] | 238 | BB->splice(InsertPos, BB, MI); |
| 239 | Pass->LIS->handleMove(MI); |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 240 | if (Begin == InsertPos) |
| 241 | Begin = MI; |
| 242 | } |
| 243 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 244 | // Release dependent instructions for scheduling. |
| 245 | releaseSuccessors(SU); |
| 246 | } |
| 247 | } |
| 248 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 249 | bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 250 | // Initialize the context of the pass. |
| 251 | MF = &mf; |
| 252 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 253 | MDT = &getAnalysis<MachineDominatorTree>(); |
Lang Hames | 907cc8f | 2012-01-27 22:36:19 +0000 | [diff] [blame] | 254 | LIS = &getAnalysis<LiveIntervals>(); |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 255 | TII = MF->getTarget().getInstrInfo(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 256 | |
| 257 | // Select the scheduler, or set the default. |
| 258 | MachineSchedRegistry::ScheduleDAGCtor Ctor = |
| 259 | MachineSchedRegistry::getDefault(); |
| 260 | if (!Ctor) { |
| 261 | Ctor = MachineSchedOpt; |
| 262 | MachineSchedRegistry::setDefault(Ctor); |
| 263 | } |
| 264 | // Instantiate the selected scheduler. |
| 265 | OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this)); |
| 266 | |
| 267 | // Visit all machine basic blocks. |
| 268 | for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end(); |
| 269 | MBB != MBBEnd; ++MBB) { |
| 270 | |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 271 | // Break the block into scheduling regions [I, RegionEnd), and schedule each |
| 272 | // region as soon as it is discovered. |
| 273 | unsigned RemainingCount = MBB->size(); |
| 274 | for(MachineBasicBlock::iterator RegionEnd = MBB->end(); |
| 275 | RegionEnd != MBB->begin();) { |
Andrew Trick | 6cfb14f | 2012-03-07 00:18:05 +0000 | [diff] [blame] | 276 | Scheduler->StartBlock(MBB); |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 277 | // The next region starts above the previous region. Look backward in the |
| 278 | // instruction stream until we find the nearest boundary. |
| 279 | MachineBasicBlock::iterator I = RegionEnd; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 280 | for(;I != MBB->begin(); --I, --RemainingCount) { |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 281 | if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF)) |
| 282 | break; |
| 283 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 284 | if (I == RegionEnd) { |
| 285 | // Skip empty scheduling regions. |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 286 | RegionEnd = llvm::prior(RegionEnd); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 287 | --RemainingCount; |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 288 | continue; |
| 289 | } |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 290 | // Skip regions with one instruction. |
| 291 | if (I == llvm::prior(RegionEnd)) { |
| 292 | RegionEnd = llvm::prior(RegionEnd); |
| 293 | continue; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 294 | } |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 295 | DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName() |
Andrew Trick | 291411c | 2012-02-08 02:17:21 +0000 | [diff] [blame] | 296 | << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: "; |
| 297 | if (RegionEnd != MBB->end()) dbgs() << *RegionEnd; |
| 298 | else dbgs() << "End"; |
| 299 | dbgs() << " Remaining: " << RemainingCount << "\n"); |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 300 | |
| 301 | // Inform ScheduleDAGInstrs of the region being scheduled. It calls back |
| 302 | // to our Schedule() method. |
| 303 | Scheduler->Run(MBB, I, RegionEnd, MBB->size()); |
| 304 | RegionEnd = Scheduler->Begin; |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 305 | } |
| 306 | assert(RemainingCount == 0 && "Instruction count mismatch!"); |
Andrew Trick | 6cfb14f | 2012-03-07 00:18:05 +0000 | [diff] [blame] | 307 | Scheduler->FinishBlock(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 312 | void MachineScheduler::print(raw_ostream &O, const Module* m) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 313 | // unimplemented |
| 314 | } |
| 315 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 316 | //===----------------------------------------------------------------------===// |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 317 | // Placeholder for extending the machine instruction scheduler. |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | |
| 320 | namespace { |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 321 | class DefaultMachineScheduler : public ScheduleDAGInstrs { |
| 322 | MachineScheduler *Pass; |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 323 | public: |
| 324 | DefaultMachineScheduler(MachineScheduler *P): |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 325 | ScheduleDAGInstrs(*P->MF, *P->MLI, *P->MDT, /*IsPostRA=*/false, P->LIS), |
| 326 | Pass(P) {} |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 327 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 328 | /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's |
| 329 | /// time to do some work. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 330 | void Schedule(); |
| 331 | }; |
| 332 | } // namespace |
| 333 | |
| 334 | static ScheduleDAGInstrs *createDefaultMachineSched(MachineScheduler *P) { |
| 335 | return new DefaultMachineScheduler(P); |
| 336 | } |
| 337 | static MachineSchedRegistry |
| 338 | SchedDefaultRegistry("default", "Activate the scheduler pass, " |
| 339 | "but don't reorder instructions", |
| 340 | createDefaultMachineSched); |
| 341 | |
| 342 | |
| 343 | /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's |
| 344 | /// time to do some work. |
| 345 | void DefaultMachineScheduler::Schedule() { |
| 346 | BuildSchedGraph(&Pass->getAnalysis<AliasAnalysis>()); |
| 347 | |
| 348 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
| 349 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 350 | SUnits[su].dumpAll(this)); |
| 351 | |
| 352 | // TODO: Put interesting things here. |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 353 | // |
| 354 | // When this is fully implemented, it will become a subclass of |
| 355 | // ScheduleTopDownLive. So this driver will disappear. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | //===----------------------------------------------------------------------===// |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 359 | // Machine Instruction Shuffler for Correctness Testing |
| 360 | //===----------------------------------------------------------------------===// |
| 361 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 362 | #ifndef NDEBUG |
| 363 | namespace { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 364 | // Nodes with a higher number have higher priority. This way we attempt to |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 365 | // schedule the latest instructions earliest. |
| 366 | // |
| 367 | // TODO: Relies on the property of the BuildSchedGraph that results in SUnits |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 368 | // being ordered in sequence top-down. |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 369 | struct ShuffleSUnitOrder { |
| 370 | bool operator()(SUnit *A, SUnit *B) const { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 371 | return A->NodeNum < B->NodeNum; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 372 | } |
| 373 | }; |
| 374 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 375 | /// Reorder instructions as much as possible. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 376 | class InstructionShuffler : public ScheduleTopDownLive { |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 377 | std::priority_queue<SUnit*, std::vector<SUnit*>, ShuffleSUnitOrder> Queue; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 378 | public: |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 379 | InstructionShuffler(MachineScheduler *P): |
| 380 | ScheduleTopDownLive(P) {} |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 381 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 382 | /// ScheduleTopDownLive Interface |
| 383 | |
| 384 | virtual SUnit *pickNode() { |
| 385 | if (Queue.empty()) return NULL; |
| 386 | SUnit *SU = Queue.top(); |
| 387 | Queue.pop(); |
| 388 | return SU; |
| 389 | } |
| 390 | |
| 391 | virtual void releaseNode(SUnit *SU) { |
| 392 | Queue.push(SU); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 393 | } |
| 394 | }; |
| 395 | } // namespace |
| 396 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 397 | static ScheduleDAGInstrs *createInstructionShuffler(MachineScheduler *P) { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 398 | return new InstructionShuffler(P); |
| 399 | } |
| 400 | static MachineSchedRegistry ShufflerRegistry("shuffle", |
| 401 | "Shuffle machine instructions", |
| 402 | createInstructionShuffler); |
| 403 | #endif // !NDEBUG |