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