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