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