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 | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 17 | #include "RegisterClassInfo.h" |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 18 | #include "RegisterPressure.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineScheduler.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
Andrew Trick | ed395c8 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/ADT/OwningPtr.h" |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/PriorityQueue.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 31 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 32 | #include <queue> |
| 33 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 36 | static cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden, |
| 37 | cl::desc("Force top-down list scheduling")); |
| 38 | static cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden, |
| 39 | cl::desc("Force bottom-up list scheduling")); |
| 40 | |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 41 | #ifndef NDEBUG |
| 42 | static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden, |
| 43 | cl::desc("Pop up a window to show MISched dags after they are processed")); |
Lang Hames | 23f1cbb | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 44 | |
| 45 | static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden, |
| 46 | cl::desc("Stop scheduling after N instructions"), cl::init(~0U)); |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 47 | #else |
| 48 | static bool ViewMISchedDAGs = false; |
| 49 | #endif // NDEBUG |
| 50 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Machine Instruction Scheduling Pass and Registry |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 55 | MachineSchedContext::MachineSchedContext(): |
| 56 | MF(0), MLI(0), MDT(0), PassConfig(0), AA(0), LIS(0) { |
| 57 | RegClassInfo = new RegisterClassInfo(); |
| 58 | } |
| 59 | |
| 60 | MachineSchedContext::~MachineSchedContext() { |
| 61 | delete RegClassInfo; |
| 62 | } |
| 63 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 64 | namespace { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 65 | /// MachineScheduler runs after coalescing and before register allocation. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 66 | class MachineScheduler : public MachineSchedContext, |
| 67 | public MachineFunctionPass { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 68 | public: |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 69 | MachineScheduler(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 70 | |
| 71 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 72 | |
| 73 | virtual void releaseMemory() {} |
| 74 | |
| 75 | virtual bool runOnMachineFunction(MachineFunction&); |
| 76 | |
| 77 | virtual void print(raw_ostream &O, const Module* = 0) const; |
| 78 | |
| 79 | static char ID; // Class identification, replacement for typeinfo |
| 80 | }; |
| 81 | } // namespace |
| 82 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 83 | char MachineScheduler::ID = 0; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 84 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 85 | char &llvm::MachineSchedulerID = MachineScheduler::ID; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 86 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 87 | INITIALIZE_PASS_BEGIN(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 88 | "Machine Instruction Scheduler", false, false) |
| 89 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 90 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 91 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 92 | INITIALIZE_PASS_END(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 93 | "Machine Instruction Scheduler", false, false) |
| 94 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 95 | MachineScheduler::MachineScheduler() |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 96 | : MachineFunctionPass(ID) { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 97 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 100 | void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 101 | AU.setPreservesCFG(); |
| 102 | AU.addRequiredID(MachineDominatorsID); |
| 103 | AU.addRequired<MachineLoopInfo>(); |
| 104 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 105 | AU.addRequired<TargetPassConfig>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 106 | AU.addRequired<SlotIndexes>(); |
| 107 | AU.addPreserved<SlotIndexes>(); |
| 108 | AU.addRequired<LiveIntervals>(); |
| 109 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 110 | MachineFunctionPass::getAnalysisUsage(AU); |
| 111 | } |
| 112 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 113 | MachinePassRegistry MachineSchedRegistry::Registry; |
| 114 | |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 115 | /// A dummy default scheduler factory indicates whether the scheduler |
| 116 | /// is overridden on the command line. |
| 117 | static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) { |
| 118 | return 0; |
| 119 | } |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 120 | |
| 121 | /// MachineSchedOpt allows command line selection of the scheduler. |
| 122 | static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false, |
| 123 | RegisterPassParser<MachineSchedRegistry> > |
| 124 | MachineSchedOpt("misched", |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 125 | cl::init(&useDefaultMachineSched), cl::Hidden, |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 126 | cl::desc("Machine instruction scheduler to use")); |
| 127 | |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 128 | static MachineSchedRegistry |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 129 | DefaultSchedRegistry("default", "Use the target's default scheduler choice.", |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 130 | useDefaultMachineSched); |
| 131 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 132 | /// Forward declare the standard machine scheduler. This will be used as the |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 133 | /// default scheduler if the target does not set a default. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 134 | static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 135 | |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 136 | |
| 137 | /// Decrement this iterator until reaching the top or a non-debug instr. |
| 138 | static MachineBasicBlock::iterator |
| 139 | priorNonDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator Beg) { |
| 140 | assert(I != Beg && "reached the top of the region, cannot decrement"); |
| 141 | while (--I != Beg) { |
| 142 | if (!I->isDebugValue()) |
| 143 | break; |
| 144 | } |
| 145 | return I; |
| 146 | } |
| 147 | |
| 148 | /// If this iterator is a debug value, increment until reaching the End or a |
| 149 | /// non-debug instruction. |
| 150 | static MachineBasicBlock::iterator |
| 151 | nextIfDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator End) { |
| 152 | while(I != End) { |
| 153 | if (!I->isDebugValue()) |
| 154 | break; |
| 155 | } |
| 156 | return I; |
| 157 | } |
| 158 | |
Andrew Trick | cb058d5 | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 159 | /// Top-level MachineScheduler pass driver. |
| 160 | /// |
| 161 | /// Visit blocks in function order. Divide each block into scheduling regions |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 162 | /// and visit them bottom-up. Visiting regions bottom-up is not required, but is |
| 163 | /// consistent with the DAG builder, which traverses the interior of the |
| 164 | /// scheduling regions bottom-up. |
Andrew Trick | cb058d5 | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 165 | /// |
| 166 | /// This design avoids exposing scheduling boundaries to the DAG builder, |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 167 | /// simplifying the DAG builder's support for "special" target instructions. |
| 168 | /// At the same time the design allows target schedulers to operate across |
Andrew Trick | cb058d5 | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 169 | /// scheduling boundaries, for example to bundle the boudary instructions |
| 170 | /// without reordering them. This creates complexity, because the target |
| 171 | /// scheduler must update the RegionBegin and RegionEnd positions cached by |
| 172 | /// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler |
| 173 | /// design would be to split blocks at scheduling boundaries, but LLVM has a |
| 174 | /// general bias against block splitting purely for implementation simplicity. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 175 | bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 176 | // Initialize the context of the pass. |
| 177 | MF = &mf; |
| 178 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 179 | MDT = &getAnalysis<MachineDominatorTree>(); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 180 | PassConfig = &getAnalysis<TargetPassConfig>(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 181 | AA = &getAnalysis<AliasAnalysis>(); |
| 182 | |
Lang Hames | 907cc8f | 2012-01-27 22:36:19 +0000 | [diff] [blame] | 183 | LIS = &getAnalysis<LiveIntervals>(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 184 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 185 | |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 186 | RegClassInfo->runOnMachineFunction(*MF); |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 187 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 188 | // Select the scheduler, or set the default. |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 189 | MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt; |
| 190 | if (Ctor == useDefaultMachineSched) { |
| 191 | // Get the default scheduler set by the target. |
| 192 | Ctor = MachineSchedRegistry::getDefault(); |
| 193 | if (!Ctor) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 194 | Ctor = createConvergingSched; |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 195 | MachineSchedRegistry::setDefault(Ctor); |
| 196 | } |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 197 | } |
| 198 | // Instantiate the selected scheduler. |
| 199 | OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this)); |
| 200 | |
| 201 | // Visit all machine basic blocks. |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 202 | // |
| 203 | // TODO: Visit blocks in global postorder or postorder within the bottom-up |
| 204 | // loop tree. Then we can optionally compute global RegPressure. |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 205 | for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end(); |
| 206 | MBB != MBBEnd; ++MBB) { |
| 207 | |
Andrew Trick | 1fabd9f | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 208 | Scheduler->startBlock(MBB); |
| 209 | |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 210 | // Break the block into scheduling regions [I, RegionEnd), and schedule each |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 211 | // region as soon as it is discovered. RegionEnd points the the scheduling |
| 212 | // boundary at the bottom of the region. The DAG does not include RegionEnd, |
| 213 | // but the region does (i.e. the next RegionEnd is above the previous |
| 214 | // RegionBegin). If the current block has no terminator then RegionEnd == |
| 215 | // MBB->end() for the bottom region. |
| 216 | // |
| 217 | // The Scheduler may insert instructions during either schedule() or |
| 218 | // exitRegion(), even for empty regions. So the local iterators 'I' and |
| 219 | // 'RegionEnd' are invalid across these calls. |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 220 | unsigned RemainingCount = MBB->size(); |
Andrew Trick | 7799eb4 | 2012-03-09 03:46:39 +0000 | [diff] [blame] | 221 | for(MachineBasicBlock::iterator RegionEnd = MBB->end(); |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 222 | RegionEnd != MBB->begin(); RegionEnd = Scheduler->begin()) { |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 223 | |
Andrew Trick | 1fabd9f | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 224 | // Avoid decrementing RegionEnd for blocks with no terminator. |
| 225 | if (RegionEnd != MBB->end() |
| 226 | || TII->isSchedulingBoundary(llvm::prior(RegionEnd), MBB, *MF)) { |
| 227 | --RegionEnd; |
| 228 | // Count the boundary instruction. |
| 229 | --RemainingCount; |
| 230 | } |
| 231 | |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 232 | // The next region starts above the previous region. Look backward in the |
| 233 | // instruction stream until we find the nearest boundary. |
| 234 | MachineBasicBlock::iterator I = RegionEnd; |
Andrew Trick | 7799eb4 | 2012-03-09 03:46:39 +0000 | [diff] [blame] | 235 | for(;I != MBB->begin(); --I, --RemainingCount) { |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 236 | if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF)) |
| 237 | break; |
| 238 | } |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 239 | // Notify the scheduler of the region, even if we may skip scheduling |
| 240 | // it. Perhaps it still needs to be bundled. |
| 241 | Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount); |
| 242 | |
| 243 | // Skip empty scheduling regions (0 or 1 schedulable instructions). |
| 244 | if (I == RegionEnd || I == llvm::prior(RegionEnd)) { |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 245 | // Close the current region. Bundle the terminator if needed. |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 246 | // This invalidates 'RegionEnd' and 'I'. |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 247 | Scheduler->exitRegion(); |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 248 | continue; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 249 | } |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 250 | DEBUG(dbgs() << "MachineScheduling " << MF->getFunction()->getName() |
Andrew Trick | 291411c | 2012-02-08 02:17:21 +0000 | [diff] [blame] | 251 | << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: "; |
| 252 | if (RegionEnd != MBB->end()) dbgs() << *RegionEnd; |
| 253 | else dbgs() << "End"; |
| 254 | dbgs() << " Remaining: " << RemainingCount << "\n"); |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 255 | |
Andrew Trick | d24da97 | 2012-03-09 03:46:42 +0000 | [diff] [blame] | 256 | // Schedule a region: possibly reorder instructions. |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 257 | // This invalidates 'RegionEnd' and 'I'. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 258 | Scheduler->schedule(); |
Andrew Trick | d24da97 | 2012-03-09 03:46:42 +0000 | [diff] [blame] | 259 | |
| 260 | // Close the current region. |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 261 | Scheduler->exitRegion(); |
| 262 | |
| 263 | // Scheduling has invalidated the current iterator 'I'. Ask the |
| 264 | // scheduler for the top of it's scheduled region. |
| 265 | RegionEnd = Scheduler->begin(); |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 266 | } |
| 267 | assert(RemainingCount == 0 && "Instruction count mismatch!"); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 268 | Scheduler->finishBlock(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 269 | } |
Andrew Trick | 830da40 | 2012-04-01 07:24:23 +0000 | [diff] [blame] | 270 | Scheduler->finalizeSchedule(); |
Andrew Trick | aad37f1 | 2012-03-21 04:12:12 +0000 | [diff] [blame] | 271 | DEBUG(LIS->print(dbgs())); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 275 | void MachineScheduler::print(raw_ostream &O, const Module* m) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 276 | // unimplemented |
| 277 | } |
| 278 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 279 | //===----------------------------------------------------------------------===// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 280 | // MachineSchedStrategy - Interface to a machine scheduling algorithm. |
| 281 | //===----------------------------------------------------------------------===// |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 282 | |
| 283 | namespace { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 284 | class ScheduleDAGMI; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 285 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 286 | /// MachineSchedStrategy - Interface used by ScheduleDAGMI to drive the selected |
| 287 | /// scheduling algorithm. |
| 288 | /// |
| 289 | /// If this works well and targets wish to reuse ScheduleDAGMI, we may expose it |
| 290 | /// in ScheduleDAGInstrs.h |
| 291 | class MachineSchedStrategy { |
| 292 | public: |
| 293 | virtual ~MachineSchedStrategy() {} |
| 294 | |
| 295 | /// Initialize the strategy after building the DAG for a new region. |
| 296 | virtual void initialize(ScheduleDAGMI *DAG) = 0; |
| 297 | |
| 298 | /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to |
| 299 | /// schedule the node at the top of the unscheduled region. Otherwise it will |
| 300 | /// be scheduled at the bottom. |
| 301 | virtual SUnit *pickNode(bool &IsTopNode) = 0; |
| 302 | |
| 303 | /// When all predecessor dependencies have been resolved, free this node for |
| 304 | /// top-down scheduling. |
| 305 | virtual void releaseTopNode(SUnit *SU) = 0; |
| 306 | /// When all successor dependencies have been resolved, free this node for |
| 307 | /// bottom-up scheduling. |
| 308 | virtual void releaseBottomNode(SUnit *SU) = 0; |
| 309 | }; |
| 310 | } // namespace |
| 311 | |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | // ScheduleDAGMI - Base class for MachineInstr scheduling with LiveIntervals |
| 314 | // preservation. |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | |
| 317 | namespace { |
| 318 | /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that schedules |
| 319 | /// machine instructions while updating LiveIntervals. |
| 320 | class ScheduleDAGMI : public ScheduleDAGInstrs { |
| 321 | AliasAnalysis *AA; |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 322 | RegisterClassInfo *RegClassInfo; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 323 | MachineSchedStrategy *SchedImpl; |
| 324 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 325 | // Register pressure in this region computed by buildSchedGraph. |
| 326 | IntervalPressure RegPressure; |
| 327 | RegPressureTracker RPTracker; |
| 328 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 329 | /// The top of the unscheduled zone. |
| 330 | MachineBasicBlock::iterator CurrentTop; |
| 331 | |
| 332 | /// The bottom of the unscheduled zone. |
| 333 | MachineBasicBlock::iterator CurrentBottom; |
Lang Hames | 23f1cbb | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 334 | |
| 335 | /// The number of instructions scheduled so far. Used to cut off the |
| 336 | /// scheduler at the point determined by misched-cutoff. |
| 337 | unsigned NumInstrsScheduled; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 338 | public: |
| 339 | ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S): |
| 340 | ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS), |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 341 | AA(C->AA), RegClassInfo(C->RegClassInfo), SchedImpl(S), |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 342 | RPTracker(RegPressure), CurrentTop(), CurrentBottom(), |
Lang Hames | 23f1cbb | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 343 | NumInstrsScheduled(0) {} |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 344 | |
| 345 | ~ScheduleDAGMI() { |
| 346 | delete SchedImpl; |
| 347 | } |
| 348 | |
| 349 | MachineBasicBlock::iterator top() const { return CurrentTop; } |
| 350 | MachineBasicBlock::iterator bottom() const { return CurrentBottom; } |
| 351 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 352 | /// Implement the ScheduleDAGInstrs interface for handling the next scheduling |
| 353 | /// region. This covers all instructions in a block, while schedule() may only |
| 354 | /// cover a subset. |
| 355 | void enterRegion(MachineBasicBlock *bb, |
| 356 | MachineBasicBlock::iterator begin, |
| 357 | MachineBasicBlock::iterator end, |
| 358 | unsigned endcount); |
| 359 | |
| 360 | /// Implement ScheduleDAGInstrs interface for scheduling a sequence of |
| 361 | /// reorderable instructions. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 362 | void schedule(); |
| 363 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 364 | protected: |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 365 | void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos); |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 366 | bool checkSchedLimit(); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 367 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 368 | void releaseSucc(SUnit *SU, SDep *SuccEdge); |
| 369 | void releaseSuccessors(SUnit *SU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 370 | void releasePred(SUnit *SU, SDep *PredEdge); |
| 371 | void releasePredecessors(SUnit *SU); |
Andrew Trick | 000b250 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 372 | |
| 373 | void placeDebugValues(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 374 | }; |
| 375 | } // namespace |
| 376 | |
| 377 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When |
| 378 | /// NumPredsLeft reaches zero, release the successor node. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 379 | void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) { |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 380 | SUnit *SuccSU = SuccEdge->getSUnit(); |
| 381 | |
| 382 | #ifndef NDEBUG |
| 383 | if (SuccSU->NumPredsLeft == 0) { |
| 384 | dbgs() << "*** Scheduling failed! ***\n"; |
| 385 | SuccSU->dump(this); |
| 386 | dbgs() << " has been released too many times!\n"; |
| 387 | llvm_unreachable(0); |
| 388 | } |
| 389 | #endif |
| 390 | --SuccSU->NumPredsLeft; |
| 391 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 392 | SchedImpl->releaseTopNode(SuccSU); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /// releaseSuccessors - Call releaseSucc on each of SU's successors. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 396 | void ScheduleDAGMI::releaseSuccessors(SUnit *SU) { |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 397 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 398 | I != E; ++I) { |
| 399 | releaseSucc(SU, &*I); |
| 400 | } |
| 401 | } |
| 402 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 403 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When |
| 404 | /// NumSuccsLeft reaches zero, release the predecessor node. |
| 405 | void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) { |
| 406 | SUnit *PredSU = PredEdge->getSUnit(); |
| 407 | |
| 408 | #ifndef NDEBUG |
| 409 | if (PredSU->NumSuccsLeft == 0) { |
| 410 | dbgs() << "*** Scheduling failed! ***\n"; |
| 411 | PredSU->dump(this); |
| 412 | dbgs() << " has been released too many times!\n"; |
| 413 | llvm_unreachable(0); |
| 414 | } |
| 415 | #endif |
| 416 | --PredSU->NumSuccsLeft; |
| 417 | if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) |
| 418 | SchedImpl->releaseBottomNode(PredSU); |
| 419 | } |
| 420 | |
| 421 | /// releasePredecessors - Call releasePred on each of SU's predecessors. |
| 422 | void ScheduleDAGMI::releasePredecessors(SUnit *SU) { |
| 423 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 424 | I != E; ++I) { |
| 425 | releasePred(SU, &*I); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void ScheduleDAGMI::moveInstruction(MachineInstr *MI, |
| 430 | MachineBasicBlock::iterator InsertPos) { |
Andrew Trick | 1ce062f | 2012-03-21 04:12:10 +0000 | [diff] [blame] | 431 | // Fix RegionBegin if the first instruction moves down. |
| 432 | if (&*RegionBegin == MI) |
| 433 | RegionBegin = llvm::next(RegionBegin); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 434 | BB->splice(InsertPos, BB, MI); |
| 435 | LIS->handleMove(MI); |
Andrew Trick | 1ce062f | 2012-03-21 04:12:10 +0000 | [diff] [blame] | 436 | // Fix RegionBegin if another instruction moves above the first instruction. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 437 | if (RegionBegin == InsertPos) |
| 438 | RegionBegin = MI; |
| 439 | } |
| 440 | |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 441 | bool ScheduleDAGMI::checkSchedLimit() { |
| 442 | #ifndef NDEBUG |
| 443 | if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) { |
| 444 | CurrentTop = CurrentBottom; |
| 445 | return false; |
| 446 | } |
| 447 | ++NumInstrsScheduled; |
| 448 | #endif |
| 449 | return true; |
| 450 | } |
| 451 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 452 | /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after |
| 453 | /// crossing a scheduling boundary. [begin, end) includes all instructions in |
| 454 | /// the region, including the boundary itself and single-instruction regions |
| 455 | /// that don't get scheduled. |
| 456 | void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb, |
| 457 | MachineBasicBlock::iterator begin, |
| 458 | MachineBasicBlock::iterator end, |
| 459 | unsigned endcount) |
| 460 | { |
| 461 | ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount); |
| 462 | // Setup the register pressure tracker to begin tracking at the end of this |
| 463 | // region. |
| 464 | RPTracker.init(&MF, RegClassInfo, LIS, BB, end); |
| 465 | } |
| 466 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 467 | /// schedule - Called back from MachineScheduler::runOnMachineFunction |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 468 | /// after setting up the current scheduling region. [RegionBegin, RegionEnd) |
| 469 | /// only includes instructions that have DAG nodes, not scheduling boundaries. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 470 | void ScheduleDAGMI::schedule() { |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 471 | while(RPTracker.getPos() != RegionEnd) { |
| 472 | bool Moved = RPTracker.recede(); |
| 473 | assert(Moved && "Regpressure tracker cannot find RegionEnd"); (void)Moved; |
| 474 | } |
| 475 | |
| 476 | // Build the DAG. |
| 477 | buildSchedGraph(AA, &RPTracker); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 478 | |
| 479 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
| 480 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 481 | SUnits[su].dumpAll(this)); |
| 482 | |
| 483 | if (ViewMISchedDAGs) viewGraph(); |
| 484 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 485 | SchedImpl->initialize(this); |
| 486 | |
| 487 | // Release edges from the special Entry node or to the special Exit node. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 488 | releaseSuccessors(&EntrySU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 489 | releasePredecessors(&ExitSU); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 490 | |
| 491 | // Release all DAG roots for scheduling. |
| 492 | for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end(); |
| 493 | I != E; ++I) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 494 | // A SUnit is ready to top schedule if it has no predecessors. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 495 | if (I->Preds.empty()) |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 496 | SchedImpl->releaseTopNode(&(*I)); |
| 497 | // A SUnit is ready to bottom schedule if it has no successors. |
| 498 | if (I->Succs.empty()) |
| 499 | SchedImpl->releaseBottomNode(&(*I)); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 502 | CurrentTop = nextIfDebug(RegionBegin, RegionEnd); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 503 | CurrentBottom = RegionEnd; |
| 504 | bool IsTopNode = false; |
| 505 | while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) { |
| 506 | DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom") |
| 507 | << " Scheduling Instruction:\n"; SU->dump(this)); |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 508 | if (!checkSchedLimit()) |
| 509 | break; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 510 | |
| 511 | // Move the instruction to its new location in the instruction stream. |
| 512 | MachineInstr *MI = SU->getInstr(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 513 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 514 | if (IsTopNode) { |
| 515 | assert(SU->isTopReady() && "node still has unscheduled dependencies"); |
| 516 | if (&*CurrentTop == MI) |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 517 | CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 518 | else |
| 519 | moveInstruction(MI, CurrentTop); |
| 520 | // Release dependent instructions for scheduling. |
| 521 | releaseSuccessors(SU); |
| 522 | } |
| 523 | else { |
| 524 | assert(SU->isBottomReady() && "node still has unscheduled dependencies"); |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 525 | MachineBasicBlock::iterator priorII = |
| 526 | priorNonDebug(CurrentBottom, CurrentTop); |
| 527 | if (&*priorII == MI) |
| 528 | CurrentBottom = priorII; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 529 | else { |
Andrew Trick | 1ce062f | 2012-03-21 04:12:10 +0000 | [diff] [blame] | 530 | if (&*CurrentTop == MI) |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 531 | CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 532 | moveInstruction(MI, CurrentBottom); |
| 533 | CurrentBottom = MI; |
| 534 | } |
| 535 | // Release dependent instructions for scheduling. |
| 536 | releasePredecessors(SU); |
| 537 | } |
| 538 | SU->isScheduled = true; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 539 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 540 | assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); |
Andrew Trick | 000b250 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 541 | |
| 542 | placeDebugValues(); |
| 543 | } |
| 544 | |
| 545 | /// Reinsert any remaining debug_values, just like the PostRA scheduler. |
| 546 | void ScheduleDAGMI::placeDebugValues() { |
| 547 | // If first instruction was a DBG_VALUE then put it back. |
| 548 | if (FirstDbgValue) { |
| 549 | BB->splice(RegionBegin, BB, FirstDbgValue); |
| 550 | RegionBegin = FirstDbgValue; |
| 551 | } |
| 552 | |
| 553 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 554 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 555 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 556 | MachineInstr *DbgValue = P.first; |
| 557 | MachineBasicBlock::iterator OrigPrevMI = P.second; |
| 558 | BB->splice(++OrigPrevMI, BB, DbgValue); |
| 559 | if (OrigPrevMI == llvm::prior(RegionEnd)) |
| 560 | RegionEnd = DbgValue; |
| 561 | } |
| 562 | DbgValues.clear(); |
| 563 | FirstDbgValue = NULL; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | //===----------------------------------------------------------------------===// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 567 | // ConvergingScheduler - Implementation of the standard MachineSchedStrategy. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 568 | //===----------------------------------------------------------------------===// |
| 569 | |
| 570 | namespace { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 571 | /// ConvergingScheduler shrinks the unscheduled zone using heuristics to balance |
| 572 | /// the schedule. |
| 573 | class ConvergingScheduler : public MachineSchedStrategy { |
| 574 | ScheduleDAGMI *DAG; |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 575 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 576 | unsigned NumTopReady; |
| 577 | unsigned NumBottomReady; |
| 578 | |
| 579 | public: |
| 580 | virtual void initialize(ScheduleDAGMI *dag) { |
| 581 | DAG = dag; |
| 582 | |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 583 | assert((!ForceTopDown || !ForceBottomUp) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 584 | "-misched-topdown incompatible with -misched-bottomup"); |
| 585 | } |
| 586 | |
| 587 | virtual SUnit *pickNode(bool &IsTopNode) { |
| 588 | if (DAG->top() == DAG->bottom()) |
| 589 | return NULL; |
| 590 | |
| 591 | // As an initial placeholder heuristic, schedule in the direction that has |
| 592 | // the fewest choices. |
| 593 | SUnit *SU; |
| 594 | if (ForceTopDown || (!ForceBottomUp && NumTopReady <= NumBottomReady)) { |
| 595 | SU = DAG->getSUnit(DAG->top()); |
| 596 | IsTopNode = true; |
| 597 | } |
| 598 | else { |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 599 | SU = DAG->getSUnit(priorNonDebug(DAG->bottom(), DAG->top())); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 600 | IsTopNode = false; |
| 601 | } |
| 602 | if (SU->isTopReady()) { |
| 603 | assert(NumTopReady > 0 && "bad ready count"); |
| 604 | --NumTopReady; |
| 605 | } |
| 606 | if (SU->isBottomReady()) { |
| 607 | assert(NumBottomReady > 0 && "bad ready count"); |
| 608 | --NumBottomReady; |
| 609 | } |
| 610 | return SU; |
| 611 | } |
| 612 | |
| 613 | virtual void releaseTopNode(SUnit *SU) { |
| 614 | ++NumTopReady; |
| 615 | } |
| 616 | virtual void releaseBottomNode(SUnit *SU) { |
| 617 | ++NumBottomReady; |
| 618 | } |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 619 | }; |
| 620 | } // namespace |
| 621 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 622 | /// Create the standard converging machine scheduler. This will be used as the |
| 623 | /// default scheduler if the target does not set a default. |
| 624 | static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C) { |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 625 | assert((!ForceTopDown || !ForceBottomUp) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 626 | "-misched-topdown incompatible with -misched-bottomup"); |
| 627 | return new ScheduleDAGMI(C, new ConvergingScheduler()); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 628 | } |
| 629 | static MachineSchedRegistry |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 630 | ConvergingSchedRegistry("converge", "Standard converging scheduler.", |
| 631 | createConvergingSched); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 632 | |
| 633 | //===----------------------------------------------------------------------===// |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 634 | // Machine Instruction Shuffler for Correctness Testing |
| 635 | //===----------------------------------------------------------------------===// |
| 636 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 637 | #ifndef NDEBUG |
| 638 | namespace { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 639 | /// Apply a less-than relation on the node order, which corresponds to the |
| 640 | /// instruction order prior to scheduling. IsReverse implements greater-than. |
| 641 | template<bool IsReverse> |
| 642 | struct SUnitOrder { |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 643 | bool operator()(SUnit *A, SUnit *B) const { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 644 | if (IsReverse) |
| 645 | return A->NodeNum > B->NodeNum; |
| 646 | else |
| 647 | return A->NodeNum < B->NodeNum; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 648 | } |
| 649 | }; |
| 650 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 651 | /// Reorder instructions as much as possible. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 652 | class InstructionShuffler : public MachineSchedStrategy { |
| 653 | bool IsAlternating; |
| 654 | bool IsTopDown; |
| 655 | |
| 656 | // Using a less-than relation (SUnitOrder<false>) for the TopQ priority |
| 657 | // gives nodes with a higher number higher priority causing the latest |
| 658 | // instructions to be scheduled first. |
| 659 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false> > |
| 660 | TopQ; |
| 661 | // When scheduling bottom-up, use greater-than as the queue priority. |
| 662 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true> > |
| 663 | BottomQ; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 664 | public: |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 665 | InstructionShuffler(bool alternate, bool topdown) |
| 666 | : IsAlternating(alternate), IsTopDown(topdown) {} |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 667 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 668 | virtual void initialize(ScheduleDAGMI *) { |
| 669 | TopQ.clear(); |
| 670 | BottomQ.clear(); |
| 671 | } |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 672 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 673 | /// Implement MachineSchedStrategy interface. |
| 674 | /// ----------------------------------------- |
| 675 | |
| 676 | virtual SUnit *pickNode(bool &IsTopNode) { |
| 677 | SUnit *SU; |
| 678 | if (IsTopDown) { |
| 679 | do { |
| 680 | if (TopQ.empty()) return NULL; |
| 681 | SU = TopQ.top(); |
| 682 | TopQ.pop(); |
| 683 | } while (SU->isScheduled); |
| 684 | IsTopNode = true; |
| 685 | } |
| 686 | else { |
| 687 | do { |
| 688 | if (BottomQ.empty()) return NULL; |
| 689 | SU = BottomQ.top(); |
| 690 | BottomQ.pop(); |
| 691 | } while (SU->isScheduled); |
| 692 | IsTopNode = false; |
| 693 | } |
| 694 | if (IsAlternating) |
| 695 | IsTopDown = !IsTopDown; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 696 | return SU; |
| 697 | } |
| 698 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 699 | virtual void releaseTopNode(SUnit *SU) { |
| 700 | TopQ.push(SU); |
| 701 | } |
| 702 | virtual void releaseBottomNode(SUnit *SU) { |
| 703 | BottomQ.push(SU); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 704 | } |
| 705 | }; |
| 706 | } // namespace |
| 707 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 708 | static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 709 | bool Alternate = !ForceTopDown && !ForceBottomUp; |
| 710 | bool TopDown = !ForceBottomUp; |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 711 | assert((TopDown || !ForceTopDown) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 712 | "-misched-topdown incompatible with -misched-bottomup"); |
| 713 | return new ScheduleDAGMI(C, new InstructionShuffler(Alternate, TopDown)); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 714 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 715 | static MachineSchedRegistry ShufflerRegistry( |
| 716 | "shuffle", "Shuffle machine instructions alternating directions", |
| 717 | createInstructionShuffler); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 718 | #endif // !NDEBUG |