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