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