Andrew Trick | 5429a6b | 2012-05-17 22:37:09 +0000 | [diff] [blame] | 1 | //===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===// |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 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 | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include "llvm/ADT/OwningPtr.h" |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/PriorityQueue.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 32 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 33 | #include <queue> |
| 34 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 37 | static cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden, |
| 38 | cl::desc("Force top-down list scheduling")); |
| 39 | static cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden, |
| 40 | cl::desc("Force bottom-up list scheduling")); |
| 41 | |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 42 | #ifndef NDEBUG |
| 43 | static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden, |
| 44 | 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] | 45 | |
| 46 | static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden, |
| 47 | cl::desc("Stop scheduling after N instructions"), cl::init(~0U)); |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 48 | #else |
| 49 | static bool ViewMISchedDAGs = false; |
| 50 | #endif // NDEBUG |
| 51 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | // Machine Instruction Scheduling Pass and Registry |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 56 | MachineSchedContext::MachineSchedContext(): |
| 57 | MF(0), MLI(0), MDT(0), PassConfig(0), AA(0), LIS(0) { |
| 58 | RegClassInfo = new RegisterClassInfo(); |
| 59 | } |
| 60 | |
| 61 | MachineSchedContext::~MachineSchedContext() { |
| 62 | delete RegClassInfo; |
| 63 | } |
| 64 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 65 | namespace { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 66 | /// MachineScheduler runs after coalescing and before register allocation. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 67 | class MachineScheduler : public MachineSchedContext, |
| 68 | public MachineFunctionPass { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 69 | public: |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 70 | MachineScheduler(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 71 | |
| 72 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 73 | |
| 74 | virtual void releaseMemory() {} |
| 75 | |
| 76 | virtual bool runOnMachineFunction(MachineFunction&); |
| 77 | |
| 78 | virtual void print(raw_ostream &O, const Module* = 0) const; |
| 79 | |
| 80 | static char ID; // Class identification, replacement for typeinfo |
| 81 | }; |
| 82 | } // namespace |
| 83 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 84 | char MachineScheduler::ID = 0; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 85 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 86 | char &llvm::MachineSchedulerID = MachineScheduler::ID; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 87 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 88 | INITIALIZE_PASS_BEGIN(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 89 | "Machine Instruction Scheduler", false, false) |
| 90 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 91 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 92 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 93 | INITIALIZE_PASS_END(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 94 | "Machine Instruction Scheduler", false, false) |
| 95 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 96 | MachineScheduler::MachineScheduler() |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 97 | : MachineFunctionPass(ID) { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 98 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 101 | void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 102 | AU.setPreservesCFG(); |
| 103 | AU.addRequiredID(MachineDominatorsID); |
| 104 | AU.addRequired<MachineLoopInfo>(); |
| 105 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 106 | AU.addRequired<TargetPassConfig>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 107 | AU.addRequired<SlotIndexes>(); |
| 108 | AU.addPreserved<SlotIndexes>(); |
| 109 | AU.addRequired<LiveIntervals>(); |
| 110 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 111 | MachineFunctionPass::getAnalysisUsage(AU); |
| 112 | } |
| 113 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 114 | MachinePassRegistry MachineSchedRegistry::Registry; |
| 115 | |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 116 | /// A dummy default scheduler factory indicates whether the scheduler |
| 117 | /// is overridden on the command line. |
| 118 | static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) { |
| 119 | return 0; |
| 120 | } |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 121 | |
| 122 | /// MachineSchedOpt allows command line selection of the scheduler. |
| 123 | static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false, |
| 124 | RegisterPassParser<MachineSchedRegistry> > |
| 125 | MachineSchedOpt("misched", |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 126 | cl::init(&useDefaultMachineSched), cl::Hidden, |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 127 | cl::desc("Machine instruction scheduler to use")); |
| 128 | |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 129 | static MachineSchedRegistry |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 130 | DefaultSchedRegistry("default", "Use the target's default scheduler choice.", |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 131 | useDefaultMachineSched); |
| 132 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 133 | /// Forward declare the standard machine scheduler. This will be used as the |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 134 | /// default scheduler if the target does not set a default. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 135 | static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 136 | |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 137 | |
| 138 | /// Decrement this iterator until reaching the top or a non-debug instr. |
| 139 | static MachineBasicBlock::iterator |
| 140 | priorNonDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator Beg) { |
| 141 | assert(I != Beg && "reached the top of the region, cannot decrement"); |
| 142 | while (--I != Beg) { |
| 143 | if (!I->isDebugValue()) |
| 144 | break; |
| 145 | } |
| 146 | return I; |
| 147 | } |
| 148 | |
| 149 | /// If this iterator is a debug value, increment until reaching the End or a |
| 150 | /// non-debug instruction. |
| 151 | static MachineBasicBlock::iterator |
| 152 | nextIfDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator End) { |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 153 | for(; I != End; ++I) { |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 154 | if (!I->isDebugValue()) |
| 155 | break; |
| 156 | } |
| 157 | return I; |
| 158 | } |
| 159 | |
Andrew Trick | cb058d5 | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 160 | /// Top-level MachineScheduler pass driver. |
| 161 | /// |
| 162 | /// Visit blocks in function order. Divide each block into scheduling regions |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 163 | /// and visit them bottom-up. Visiting regions bottom-up is not required, but is |
| 164 | /// consistent with the DAG builder, which traverses the interior of the |
| 165 | /// scheduling regions bottom-up. |
Andrew Trick | cb058d5 | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 166 | /// |
| 167 | /// This design avoids exposing scheduling boundaries to the DAG builder, |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 168 | /// simplifying the DAG builder's support for "special" target instructions. |
| 169 | /// At the same time the design allows target schedulers to operate across |
Andrew Trick | cb058d5 | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 170 | /// scheduling boundaries, for example to bundle the boudary instructions |
| 171 | /// without reordering them. This creates complexity, because the target |
| 172 | /// scheduler must update the RegionBegin and RegionEnd positions cached by |
| 173 | /// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler |
| 174 | /// design would be to split blocks at scheduling boundaries, but LLVM has a |
| 175 | /// general bias against block splitting purely for implementation simplicity. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 176 | bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) { |
Andrew Trick | 89c324b | 2012-05-10 21:06:21 +0000 | [diff] [blame] | 177 | DEBUG(dbgs() << "Before MISsched:\n"; mf.print(dbgs())); |
| 178 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 179 | // Initialize the context of the pass. |
| 180 | MF = &mf; |
| 181 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 182 | MDT = &getAnalysis<MachineDominatorTree>(); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 183 | PassConfig = &getAnalysis<TargetPassConfig>(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 184 | AA = &getAnalysis<AliasAnalysis>(); |
| 185 | |
Lang Hames | 907cc8f | 2012-01-27 22:36:19 +0000 | [diff] [blame] | 186 | LIS = &getAnalysis<LiveIntervals>(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 187 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 188 | |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 189 | RegClassInfo->runOnMachineFunction(*MF); |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 190 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 191 | // Select the scheduler, or set the default. |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 192 | MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt; |
| 193 | if (Ctor == useDefaultMachineSched) { |
| 194 | // Get the default scheduler set by the target. |
| 195 | Ctor = MachineSchedRegistry::getDefault(); |
| 196 | if (!Ctor) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 197 | Ctor = createConvergingSched; |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 198 | MachineSchedRegistry::setDefault(Ctor); |
| 199 | } |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 200 | } |
| 201 | // Instantiate the selected scheduler. |
| 202 | OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this)); |
| 203 | |
| 204 | // Visit all machine basic blocks. |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 205 | // |
| 206 | // TODO: Visit blocks in global postorder or postorder within the bottom-up |
| 207 | // loop tree. Then we can optionally compute global RegPressure. |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 208 | for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end(); |
| 209 | MBB != MBBEnd; ++MBB) { |
| 210 | |
Andrew Trick | 1fabd9f | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 211 | Scheduler->startBlock(MBB); |
| 212 | |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 213 | // Break the block into scheduling regions [I, RegionEnd), and schedule each |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 214 | // region as soon as it is discovered. RegionEnd points the the scheduling |
| 215 | // boundary at the bottom of the region. The DAG does not include RegionEnd, |
| 216 | // but the region does (i.e. the next RegionEnd is above the previous |
| 217 | // RegionBegin). If the current block has no terminator then RegionEnd == |
| 218 | // MBB->end() for the bottom region. |
| 219 | // |
| 220 | // The Scheduler may insert instructions during either schedule() or |
| 221 | // exitRegion(), even for empty regions. So the local iterators 'I' and |
| 222 | // 'RegionEnd' are invalid across these calls. |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 223 | unsigned RemainingCount = MBB->size(); |
Andrew Trick | 7799eb4 | 2012-03-09 03:46:39 +0000 | [diff] [blame] | 224 | for(MachineBasicBlock::iterator RegionEnd = MBB->end(); |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 225 | RegionEnd != MBB->begin(); RegionEnd = Scheduler->begin()) { |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 226 | |
Andrew Trick | 1fabd9f | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 227 | // Avoid decrementing RegionEnd for blocks with no terminator. |
| 228 | if (RegionEnd != MBB->end() |
| 229 | || TII->isSchedulingBoundary(llvm::prior(RegionEnd), MBB, *MF)) { |
| 230 | --RegionEnd; |
| 231 | // Count the boundary instruction. |
| 232 | --RemainingCount; |
| 233 | } |
| 234 | |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 235 | // The next region starts above the previous region. Look backward in the |
| 236 | // instruction stream until we find the nearest boundary. |
| 237 | MachineBasicBlock::iterator I = RegionEnd; |
Andrew Trick | 7799eb4 | 2012-03-09 03:46:39 +0000 | [diff] [blame] | 238 | for(;I != MBB->begin(); --I, --RemainingCount) { |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 239 | if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF)) |
| 240 | break; |
| 241 | } |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 242 | // Notify the scheduler of the region, even if we may skip scheduling |
| 243 | // it. Perhaps it still needs to be bundled. |
| 244 | Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount); |
| 245 | |
| 246 | // Skip empty scheduling regions (0 or 1 schedulable instructions). |
| 247 | if (I == RegionEnd || I == llvm::prior(RegionEnd)) { |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 248 | // Close the current region. Bundle the terminator if needed. |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 249 | // This invalidates 'RegionEnd' and 'I'. |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 250 | Scheduler->exitRegion(); |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 251 | continue; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 252 | } |
Andrew Trick | bb0a242 | 2012-05-24 22:11:14 +0000 | [diff] [blame^] | 253 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
| 254 | DEBUG(dbgs() << MF->getFunction()->getName() |
Andrew Trick | 291411c | 2012-02-08 02:17:21 +0000 | [diff] [blame] | 255 | << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: "; |
| 256 | if (RegionEnd != MBB->end()) dbgs() << *RegionEnd; |
| 257 | else dbgs() << "End"; |
| 258 | dbgs() << " Remaining: " << RemainingCount << "\n"); |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 259 | |
Andrew Trick | d24da97 | 2012-03-09 03:46:42 +0000 | [diff] [blame] | 260 | // Schedule a region: possibly reorder instructions. |
Andrew Trick | fe4d6df | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 261 | // This invalidates 'RegionEnd' and 'I'. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 262 | Scheduler->schedule(); |
Andrew Trick | d24da97 | 2012-03-09 03:46:42 +0000 | [diff] [blame] | 263 | |
| 264 | // Close the current region. |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 265 | Scheduler->exitRegion(); |
| 266 | |
| 267 | // Scheduling has invalidated the current iterator 'I'. Ask the |
| 268 | // scheduler for the top of it's scheduled region. |
| 269 | RegionEnd = Scheduler->begin(); |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 270 | } |
| 271 | assert(RemainingCount == 0 && "Instruction count mismatch!"); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 272 | Scheduler->finishBlock(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 273 | } |
Andrew Trick | 830da40 | 2012-04-01 07:24:23 +0000 | [diff] [blame] | 274 | Scheduler->finalizeSchedule(); |
Andrew Trick | aad37f1 | 2012-03-21 04:12:12 +0000 | [diff] [blame] | 275 | DEBUG(LIS->print(dbgs())); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 279 | void MachineScheduler::print(raw_ostream &O, const Module* m) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 280 | // unimplemented |
| 281 | } |
| 282 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 283 | //===----------------------------------------------------------------------===// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 284 | // MachineSchedStrategy - Interface to a machine scheduling algorithm. |
| 285 | //===----------------------------------------------------------------------===// |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 286 | |
| 287 | namespace { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 288 | class ScheduleDAGMI; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 289 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 290 | /// MachineSchedStrategy - Interface used by ScheduleDAGMI to drive the selected |
| 291 | /// scheduling algorithm. |
| 292 | /// |
| 293 | /// If this works well and targets wish to reuse ScheduleDAGMI, we may expose it |
| 294 | /// in ScheduleDAGInstrs.h |
| 295 | class MachineSchedStrategy { |
| 296 | public: |
| 297 | virtual ~MachineSchedStrategy() {} |
| 298 | |
| 299 | /// Initialize the strategy after building the DAG for a new region. |
| 300 | virtual void initialize(ScheduleDAGMI *DAG) = 0; |
| 301 | |
| 302 | /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to |
| 303 | /// schedule the node at the top of the unscheduled region. Otherwise it will |
| 304 | /// be scheduled at the bottom. |
| 305 | virtual SUnit *pickNode(bool &IsTopNode) = 0; |
| 306 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 307 | /// Notify MachineSchedStrategy that ScheduleDAGMI has scheduled a node. |
| 308 | virtual void schedNode(SUnit *SU, bool IsTopNode) = 0; |
| 309 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 310 | /// When all predecessor dependencies have been resolved, free this node for |
| 311 | /// top-down scheduling. |
| 312 | virtual void releaseTopNode(SUnit *SU) = 0; |
| 313 | /// When all successor dependencies have been resolved, free this node for |
| 314 | /// bottom-up scheduling. |
| 315 | virtual void releaseBottomNode(SUnit *SU) = 0; |
| 316 | }; |
| 317 | } // namespace |
| 318 | |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | // ScheduleDAGMI - Base class for MachineInstr scheduling with LiveIntervals |
| 321 | // preservation. |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | |
| 324 | namespace { |
| 325 | /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that schedules |
| 326 | /// machine instructions while updating LiveIntervals. |
| 327 | class ScheduleDAGMI : public ScheduleDAGInstrs { |
| 328 | AliasAnalysis *AA; |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 329 | RegisterClassInfo *RegClassInfo; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 330 | MachineSchedStrategy *SchedImpl; |
| 331 | |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 332 | MachineBasicBlock::iterator LiveRegionEnd; |
| 333 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 334 | /// Register pressure in this region computed by buildSchedGraph. |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 335 | IntervalPressure RegPressure; |
| 336 | RegPressureTracker RPTracker; |
| 337 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 338 | /// List of pressure sets that exceed the target's pressure limit before |
| 339 | /// scheduling, listed in increasing set ID order. Each pressure set is paired |
| 340 | /// with its max pressure in the currently scheduled regions. |
| 341 | std::vector<PressureElement> RegionCriticalPSets; |
| 342 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 343 | /// The top of the unscheduled zone. |
| 344 | MachineBasicBlock::iterator CurrentTop; |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 345 | IntervalPressure TopPressure; |
| 346 | RegPressureTracker TopRPTracker; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 347 | |
| 348 | /// The bottom of the unscheduled zone. |
| 349 | MachineBasicBlock::iterator CurrentBottom; |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 350 | IntervalPressure BotPressure; |
| 351 | RegPressureTracker BotRPTracker; |
Lang Hames | 23f1cbb | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 352 | |
| 353 | /// The number of instructions scheduled so far. Used to cut off the |
| 354 | /// scheduler at the point determined by misched-cutoff. |
| 355 | unsigned NumInstrsScheduled; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 356 | public: |
| 357 | ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S): |
| 358 | ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS), |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 359 | AA(C->AA), RegClassInfo(C->RegClassInfo), SchedImpl(S), |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 360 | RPTracker(RegPressure), CurrentTop(), TopRPTracker(TopPressure), |
| 361 | CurrentBottom(), BotRPTracker(BotPressure), NumInstrsScheduled(0) {} |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 362 | |
| 363 | ~ScheduleDAGMI() { |
| 364 | delete SchedImpl; |
| 365 | } |
| 366 | |
| 367 | MachineBasicBlock::iterator top() const { return CurrentTop; } |
| 368 | MachineBasicBlock::iterator bottom() const { return CurrentBottom; } |
| 369 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 370 | /// Implement the ScheduleDAGInstrs interface for handling the next scheduling |
| 371 | /// region. This covers all instructions in a block, while schedule() may only |
| 372 | /// cover a subset. |
| 373 | void enterRegion(MachineBasicBlock *bb, |
| 374 | MachineBasicBlock::iterator begin, |
| 375 | MachineBasicBlock::iterator end, |
| 376 | unsigned endcount); |
| 377 | |
| 378 | /// Implement ScheduleDAGInstrs interface for scheduling a sequence of |
| 379 | /// reorderable instructions. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 380 | void schedule(); |
| 381 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 382 | /// Get current register pressure for the top scheduled instructions. |
| 383 | const IntervalPressure &getTopPressure() const { return TopPressure; } |
| 384 | const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; } |
| 385 | |
| 386 | /// Get current register pressure for the bottom scheduled instructions. |
| 387 | const IntervalPressure &getBotPressure() const { return BotPressure; } |
| 388 | const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; } |
| 389 | |
| 390 | /// Get register pressure for the entire scheduling region before scheduling. |
| 391 | const IntervalPressure &getRegPressure() const { return RegPressure; } |
| 392 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 393 | const std::vector<PressureElement> &getRegionCriticalPSets() const { |
| 394 | return RegionCriticalPSets; |
| 395 | } |
| 396 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 397 | protected: |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 398 | void initRegPressure(); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 399 | void updateScheduledPressure(std::vector<unsigned> NewMaxPressure); |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 400 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 401 | void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos); |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 402 | bool checkSchedLimit(); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 403 | |
Andrew Trick | 2aa689d | 2012-05-24 22:11:05 +0000 | [diff] [blame] | 404 | void releaseRoots(); |
| 405 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 406 | void releaseSucc(SUnit *SU, SDep *SuccEdge); |
| 407 | void releaseSuccessors(SUnit *SU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 408 | void releasePred(SUnit *SU, SDep *PredEdge); |
| 409 | void releasePredecessors(SUnit *SU); |
Andrew Trick | 000b250 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 410 | |
| 411 | void placeDebugValues(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 412 | }; |
| 413 | } // namespace |
| 414 | |
| 415 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When |
| 416 | /// NumPredsLeft reaches zero, release the successor node. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 417 | /// |
| 418 | /// FIXME: Adjust SuccSU height based on MinLatency. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 419 | void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) { |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 420 | SUnit *SuccSU = SuccEdge->getSUnit(); |
| 421 | |
| 422 | #ifndef NDEBUG |
| 423 | if (SuccSU->NumPredsLeft == 0) { |
| 424 | dbgs() << "*** Scheduling failed! ***\n"; |
| 425 | SuccSU->dump(this); |
| 426 | dbgs() << " has been released too many times!\n"; |
| 427 | llvm_unreachable(0); |
| 428 | } |
| 429 | #endif |
| 430 | --SuccSU->NumPredsLeft; |
| 431 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 432 | SchedImpl->releaseTopNode(SuccSU); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /// releaseSuccessors - Call releaseSucc on each of SU's successors. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 436 | void ScheduleDAGMI::releaseSuccessors(SUnit *SU) { |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 437 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 438 | I != E; ++I) { |
| 439 | releaseSucc(SU, &*I); |
| 440 | } |
| 441 | } |
| 442 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 443 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When |
| 444 | /// NumSuccsLeft reaches zero, release the predecessor node. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 445 | /// |
| 446 | /// FIXME: Adjust PredSU height based on MinLatency. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 447 | void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) { |
| 448 | SUnit *PredSU = PredEdge->getSUnit(); |
| 449 | |
| 450 | #ifndef NDEBUG |
| 451 | if (PredSU->NumSuccsLeft == 0) { |
| 452 | dbgs() << "*** Scheduling failed! ***\n"; |
| 453 | PredSU->dump(this); |
| 454 | dbgs() << " has been released too many times!\n"; |
| 455 | llvm_unreachable(0); |
| 456 | } |
| 457 | #endif |
| 458 | --PredSU->NumSuccsLeft; |
| 459 | if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) |
| 460 | SchedImpl->releaseBottomNode(PredSU); |
| 461 | } |
| 462 | |
| 463 | /// releasePredecessors - Call releasePred on each of SU's predecessors. |
| 464 | void ScheduleDAGMI::releasePredecessors(SUnit *SU) { |
| 465 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 466 | I != E; ++I) { |
| 467 | releasePred(SU, &*I); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | void ScheduleDAGMI::moveInstruction(MachineInstr *MI, |
| 472 | MachineBasicBlock::iterator InsertPos) { |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 473 | // Advance RegionBegin if the first instruction moves down. |
Andrew Trick | 1ce062f | 2012-03-21 04:12:10 +0000 | [diff] [blame] | 474 | if (&*RegionBegin == MI) |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 475 | ++RegionBegin; |
| 476 | |
| 477 | // Update the instruction stream. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 478 | BB->splice(InsertPos, BB, MI); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 479 | |
| 480 | // Update LiveIntervals |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 481 | LIS->handleMove(MI); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 482 | |
| 483 | // Recede RegionBegin if an instruction moves above the first. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 484 | if (RegionBegin == InsertPos) |
| 485 | RegionBegin = MI; |
| 486 | } |
| 487 | |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 488 | bool ScheduleDAGMI::checkSchedLimit() { |
| 489 | #ifndef NDEBUG |
| 490 | if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) { |
| 491 | CurrentTop = CurrentBottom; |
| 492 | return false; |
| 493 | } |
| 494 | ++NumInstrsScheduled; |
| 495 | #endif |
| 496 | return true; |
| 497 | } |
| 498 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 499 | /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after |
| 500 | /// crossing a scheduling boundary. [begin, end) includes all instructions in |
| 501 | /// the region, including the boundary itself and single-instruction regions |
| 502 | /// that don't get scheduled. |
| 503 | void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb, |
| 504 | MachineBasicBlock::iterator begin, |
| 505 | MachineBasicBlock::iterator end, |
| 506 | unsigned endcount) |
| 507 | { |
| 508 | ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount); |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 509 | |
| 510 | // For convenience remember the end of the liveness region. |
| 511 | LiveRegionEnd = |
| 512 | (RegionEnd == bb->end()) ? RegionEnd : llvm::next(RegionEnd); |
| 513 | } |
| 514 | |
| 515 | // Setup the register pressure trackers for the top scheduled top and bottom |
| 516 | // scheduled regions. |
| 517 | void ScheduleDAGMI::initRegPressure() { |
| 518 | TopRPTracker.init(&MF, RegClassInfo, LIS, BB, RegionBegin); |
| 519 | BotRPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd); |
| 520 | |
| 521 | // Close the RPTracker to finalize live ins. |
| 522 | RPTracker.closeRegion(); |
| 523 | |
Andrew Trick | bb0a242 | 2012-05-24 22:11:14 +0000 | [diff] [blame^] | 524 | DEBUG(RPTracker.getPressure().dump(TRI)); |
| 525 | |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 526 | // Initialize the live ins and live outs. |
| 527 | TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs); |
| 528 | BotRPTracker.addLiveRegs(RPTracker.getPressure().LiveOutRegs); |
| 529 | |
| 530 | // Close one end of the tracker so we can call |
| 531 | // getMaxUpward/DownwardPressureDelta before advancing across any |
| 532 | // instructions. This converts currently live regs into live ins/outs. |
| 533 | TopRPTracker.closeTop(); |
| 534 | BotRPTracker.closeBottom(); |
| 535 | |
| 536 | // Account for liveness generated by the region boundary. |
| 537 | if (LiveRegionEnd != RegionEnd) |
| 538 | BotRPTracker.recede(); |
| 539 | |
| 540 | assert(BotRPTracker.getPos() == RegionEnd && "Can't find the region bottom"); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 541 | |
| 542 | // Cache the list of excess pressure sets in this region. This will also track |
| 543 | // the max pressure in the scheduled code for these sets. |
| 544 | RegionCriticalPSets.clear(); |
| 545 | std::vector<unsigned> RegionPressure = RPTracker.getPressure().MaxSetPressure; |
| 546 | for (unsigned i = 0, e = RegionPressure.size(); i < e; ++i) { |
| 547 | unsigned Limit = TRI->getRegPressureSetLimit(i); |
| 548 | if (RegionPressure[i] > Limit) |
| 549 | RegionCriticalPSets.push_back(PressureElement(i, 0)); |
| 550 | } |
| 551 | DEBUG(dbgs() << "Excess PSets: "; |
| 552 | for (unsigned i = 0, e = RegionCriticalPSets.size(); i != e; ++i) |
| 553 | dbgs() << TRI->getRegPressureSetName( |
| 554 | RegionCriticalPSets[i].PSetID) << " "; |
| 555 | dbgs() << "\n"); |
| 556 | } |
| 557 | |
| 558 | // FIXME: When the pressure tracker deals in pressure differences then we won't |
| 559 | // iterate over all RegionCriticalPSets[i]. |
| 560 | void ScheduleDAGMI:: |
| 561 | updateScheduledPressure(std::vector<unsigned> NewMaxPressure) { |
| 562 | for (unsigned i = 0, e = RegionCriticalPSets.size(); i < e; ++i) { |
| 563 | unsigned ID = RegionCriticalPSets[i].PSetID; |
| 564 | int &MaxUnits = RegionCriticalPSets[i].UnitIncrease; |
| 565 | if ((int)NewMaxPressure[ID] > MaxUnits) |
| 566 | MaxUnits = NewMaxPressure[ID]; |
| 567 | } |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Andrew Trick | 2aa689d | 2012-05-24 22:11:05 +0000 | [diff] [blame] | 570 | // Release all DAG roots for scheduling. |
| 571 | void ScheduleDAGMI::releaseRoots() { |
| 572 | SmallVector<SUnit*, 16> BotRoots; |
| 573 | |
| 574 | for (std::vector<SUnit>::iterator |
| 575 | I = SUnits.begin(), E = SUnits.end(); I != E; ++I) { |
| 576 | // A SUnit is ready to top schedule if it has no predecessors. |
| 577 | if (I->Preds.empty()) |
| 578 | SchedImpl->releaseTopNode(&(*I)); |
| 579 | // A SUnit is ready to bottom schedule if it has no successors. |
| 580 | if (I->Succs.empty()) |
| 581 | BotRoots.push_back(&(*I)); |
| 582 | } |
| 583 | // Release bottom roots in reverse order so the higher priority nodes appear |
| 584 | // first. This is more natural and slightly more efficient. |
| 585 | for (SmallVectorImpl<SUnit*>::const_reverse_iterator |
| 586 | I = BotRoots.rbegin(), E = BotRoots.rend(); I != E; ++I) |
| 587 | SchedImpl->releaseBottomNode(*I); |
| 588 | } |
| 589 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 590 | /// schedule - Called back from MachineScheduler::runOnMachineFunction |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 591 | /// after setting up the current scheduling region. [RegionBegin, RegionEnd) |
| 592 | /// only includes instructions that have DAG nodes, not scheduling boundaries. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 593 | void ScheduleDAGMI::schedule() { |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 594 | // Initialize the register pressure tracker used by buildSchedGraph. |
| 595 | RPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd); |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 596 | |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 597 | // Account for liveness generate by the region boundary. |
| 598 | if (LiveRegionEnd != RegionEnd) |
| 599 | RPTracker.recede(); |
| 600 | |
| 601 | // Build the DAG, and compute current register pressure. |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 602 | buildSchedGraph(AA, &RPTracker); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 603 | |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 604 | // Initialize top/bottom trackers after computing region pressure. |
| 605 | initRegPressure(); |
| 606 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 607 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 608 | SUnits[su].dumpAll(this)); |
| 609 | |
| 610 | if (ViewMISchedDAGs) viewGraph(); |
| 611 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 612 | SchedImpl->initialize(this); |
| 613 | |
| 614 | // 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] | 615 | releaseSuccessors(&EntrySU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 616 | releasePredecessors(&ExitSU); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 617 | |
| 618 | // Release all DAG roots for scheduling. |
Andrew Trick | 2aa689d | 2012-05-24 22:11:05 +0000 | [diff] [blame] | 619 | releaseRoots(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 620 | |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 621 | CurrentTop = nextIfDebug(RegionBegin, RegionEnd); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 622 | CurrentBottom = RegionEnd; |
| 623 | bool IsTopNode = false; |
| 624 | while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) { |
| 625 | DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom") |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 626 | << " Scheduling Instruction"); |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 627 | if (!checkSchedLimit()) |
| 628 | break; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 629 | |
| 630 | // Move the instruction to its new location in the instruction stream. |
| 631 | MachineInstr *MI = SU->getInstr(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 632 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 633 | if (IsTopNode) { |
| 634 | assert(SU->isTopReady() && "node still has unscheduled dependencies"); |
| 635 | if (&*CurrentTop == MI) |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 636 | CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 637 | else { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 638 | moveInstruction(MI, CurrentTop); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 639 | TopRPTracker.setPos(MI); |
| 640 | } |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 641 | |
| 642 | // Update top scheduled pressure. |
| 643 | TopRPTracker.advance(); |
| 644 | assert(TopRPTracker.getPos() == CurrentTop && "out of sync"); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 645 | updateScheduledPressure(TopRPTracker.getPressure().MaxSetPressure); |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 646 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 647 | // Release dependent instructions for scheduling. |
| 648 | releaseSuccessors(SU); |
| 649 | } |
| 650 | else { |
| 651 | assert(SU->isBottomReady() && "node still has unscheduled dependencies"); |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 652 | MachineBasicBlock::iterator priorII = |
| 653 | priorNonDebug(CurrentBottom, CurrentTop); |
| 654 | if (&*priorII == MI) |
| 655 | CurrentBottom = priorII; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 656 | else { |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 657 | if (&*CurrentTop == MI) { |
| 658 | CurrentTop = nextIfDebug(++CurrentTop, priorII); |
| 659 | TopRPTracker.setPos(CurrentTop); |
| 660 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 661 | moveInstruction(MI, CurrentBottom); |
| 662 | CurrentBottom = MI; |
| 663 | } |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 664 | // Update bottom scheduled pressure. |
| 665 | BotRPTracker.recede(); |
| 666 | assert(BotRPTracker.getPos() == CurrentBottom && "out of sync"); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 667 | updateScheduledPressure(BotRPTracker.getPressure().MaxSetPressure); |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 668 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 669 | // Release dependent instructions for scheduling. |
| 670 | releasePredecessors(SU); |
| 671 | } |
| 672 | SU->isScheduled = true; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 673 | SchedImpl->schedNode(SU, IsTopNode); |
| 674 | DEBUG(SU->dump(this)); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 675 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 676 | assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); |
Andrew Trick | 000b250 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 677 | |
| 678 | placeDebugValues(); |
| 679 | } |
| 680 | |
| 681 | /// Reinsert any remaining debug_values, just like the PostRA scheduler. |
| 682 | void ScheduleDAGMI::placeDebugValues() { |
| 683 | // If first instruction was a DBG_VALUE then put it back. |
| 684 | if (FirstDbgValue) { |
| 685 | BB->splice(RegionBegin, BB, FirstDbgValue); |
| 686 | RegionBegin = FirstDbgValue; |
| 687 | } |
| 688 | |
| 689 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 690 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 691 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 692 | MachineInstr *DbgValue = P.first; |
| 693 | MachineBasicBlock::iterator OrigPrevMI = P.second; |
| 694 | BB->splice(++OrigPrevMI, BB, DbgValue); |
| 695 | if (OrigPrevMI == llvm::prior(RegionEnd)) |
| 696 | RegionEnd = DbgValue; |
| 697 | } |
| 698 | DbgValues.clear(); |
| 699 | FirstDbgValue = NULL; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | //===----------------------------------------------------------------------===// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 703 | // ConvergingScheduler - Implementation of the standard MachineSchedStrategy. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 704 | //===----------------------------------------------------------------------===// |
| 705 | |
| 706 | namespace { |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 707 | /// ReadyQ encapsulates vector of "ready" SUnits with basic convenience methods |
| 708 | /// for pushing and removing nodes. ReadyQ's are uniquely identified by an |
| 709 | /// ID. SUnit::NodeQueueId us a mask of the ReadyQs that the SUnit is in. |
| 710 | class ReadyQueue { |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 711 | unsigned ID; |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 712 | std::string Name; |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 713 | std::vector<SUnit*> Queue; |
| 714 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 715 | public: |
| 716 | ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {} |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 717 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 718 | unsigned getID() const { return ID; } |
| 719 | |
| 720 | StringRef getName() const { return Name; } |
| 721 | |
| 722 | // SU is in this queue if it's NodeQueueID is a superset of this ID. |
| 723 | bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); } |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 724 | |
| 725 | bool empty() const { return Queue.empty(); } |
| 726 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 727 | unsigned size() const { return Queue.size(); } |
| 728 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 729 | typedef std::vector<SUnit*>::iterator iterator; |
| 730 | |
Andrew Trick | 16716c7 | 2012-05-10 21:06:14 +0000 | [diff] [blame] | 731 | iterator begin() { return Queue.begin(); } |
| 732 | |
| 733 | iterator end() { return Queue.end(); } |
| 734 | |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 735 | iterator find(SUnit *SU) { |
| 736 | return std::find(Queue.begin(), Queue.end(), SU); |
| 737 | } |
| 738 | |
| 739 | void push(SUnit *SU) { |
| 740 | Queue.push_back(SU); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 741 | SU->NodeQueueId |= ID; |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void remove(iterator I) { |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 745 | (*I)->NodeQueueId &= ~ID; |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 746 | *I = Queue.back(); |
| 747 | Queue.pop_back(); |
| 748 | } |
Andrew Trick | 81f1be3 | 2012-05-17 18:35:13 +0000 | [diff] [blame] | 749 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 750 | void dump() { |
Andrew Trick | 81f1be3 | 2012-05-17 18:35:13 +0000 | [diff] [blame] | 751 | dbgs() << Name << ": "; |
| 752 | for (unsigned i = 0, e = Queue.size(); i < e; ++i) |
| 753 | dbgs() << Queue[i]->NodeNum << " "; |
| 754 | dbgs() << "\n"; |
| 755 | } |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 756 | }; |
| 757 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 758 | /// ConvergingScheduler shrinks the unscheduled zone using heuristics to balance |
| 759 | /// the schedule. |
| 760 | class ConvergingScheduler : public MachineSchedStrategy { |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 761 | |
| 762 | /// Store the state used by ConvergingScheduler heuristics, required for the |
| 763 | /// lifetime of one invocation of pickNode(). |
| 764 | struct SchedCandidate { |
| 765 | // The best SUnit candidate. |
| 766 | SUnit *SU; |
| 767 | |
| 768 | // Register pressure values for the best candidate. |
| 769 | RegPressureDelta RPDelta; |
| 770 | |
| 771 | SchedCandidate(): SU(NULL) {} |
| 772 | }; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 773 | /// Represent the type of SchedCandidate found within a single queue. |
| 774 | enum CandResult { |
| 775 | NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure }; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 776 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 777 | /// Each Scheduling boundary is associated with ready queues. It tracks the |
| 778 | /// current cycle in whichever direction at has moved, and maintains the state |
| 779 | /// of "hazards" and other interlocks at the current cycle. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 780 | struct SchedBoundary { |
| 781 | ReadyQueue Available; |
| 782 | ReadyQueue Pending; |
| 783 | bool CheckPending; |
| 784 | |
| 785 | ScheduleHazardRecognizer *HazardRec; |
| 786 | |
| 787 | unsigned CurrCycle; |
| 788 | unsigned IssueCount; |
| 789 | |
| 790 | /// MinReadyCycle - Cycle of the soonest available instruction. |
| 791 | unsigned MinReadyCycle; |
| 792 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 793 | /// Pending queues extend the ready queues with the same ID and the |
| 794 | /// PendingFlag set. |
| 795 | SchedBoundary(unsigned ID, const Twine &Name): |
| 796 | Available(ID, Name+".A"), |
| 797 | Pending(ID << ConvergingScheduler::LogMaxQID, Name+".P"), |
| 798 | CheckPending(false), HazardRec(0), CurrCycle(0), IssueCount(0), |
| 799 | MinReadyCycle(UINT_MAX) {} |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 800 | |
| 801 | ~SchedBoundary() { delete HazardRec; } |
| 802 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 803 | bool isTop() const { |
| 804 | return Available.getID() == ConvergingScheduler::TopQID; |
| 805 | } |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 806 | |
| 807 | void releaseNode(SUnit *SU, unsigned ReadyCycle); |
| 808 | |
| 809 | void bumpCycle(); |
| 810 | |
| 811 | void releasePending(); |
| 812 | |
| 813 | void removeReady(SUnit *SU); |
| 814 | |
| 815 | SUnit *pickOnlyChoice(); |
| 816 | }; |
| 817 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 818 | ScheduleDAGMI *DAG; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 819 | const TargetRegisterInfo *TRI; |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 820 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 821 | // State of the top and bottom scheduled instruction boundaries. |
| 822 | SchedBoundary Top; |
| 823 | SchedBoundary Bot; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 824 | |
| 825 | public: |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 826 | /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both) |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 827 | enum { |
| 828 | TopQID = 1, |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 829 | BotQID = 2, |
| 830 | LogMaxQID = 2 |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 831 | }; |
| 832 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 833 | ConvergingScheduler(): |
| 834 | DAG(0), TRI(0), Top(TopQID, "TopQ"), Bot(BotQID, "BotQ") {} |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 835 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 836 | virtual void initialize(ScheduleDAGMI *dag); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 837 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 838 | virtual SUnit *pickNode(bool &IsTopNode); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 839 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 840 | virtual void schedNode(SUnit *SU, bool IsTopNode); |
| 841 | |
| 842 | virtual void releaseTopNode(SUnit *SU); |
| 843 | |
| 844 | virtual void releaseBottomNode(SUnit *SU); |
| 845 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 846 | protected: |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 847 | SUnit *pickNodeBidrectional(bool &IsTopNode); |
| 848 | |
Andrew Trick | 8c2d921 | 2012-05-24 22:11:03 +0000 | [diff] [blame] | 849 | CandResult pickNodeFromQueue(ReadyQueue &Q, |
| 850 | const RegPressureTracker &RPTracker, |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 851 | SchedCandidate &Candidate); |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 852 | #ifndef NDEBUG |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 853 | void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU, |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 854 | PressureElement P = PressureElement()); |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 855 | #endif |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 856 | }; |
| 857 | } // namespace |
| 858 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 859 | void ConvergingScheduler::initialize(ScheduleDAGMI *dag) { |
| 860 | DAG = dag; |
| 861 | TRI = DAG->TRI; |
| 862 | |
| 863 | // Initialize the HazardRecognizers. |
| 864 | const TargetMachine &TM = DAG->MF.getTarget(); |
| 865 | const InstrItineraryData *Itin = TM.getInstrItineraryData(); |
| 866 | Top.HazardRec = TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG); |
| 867 | Bot.HazardRec = TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG); |
| 868 | |
| 869 | assert((!ForceTopDown || !ForceBottomUp) && |
| 870 | "-misched-topdown incompatible with -misched-bottomup"); |
| 871 | } |
| 872 | |
| 873 | void ConvergingScheduler::releaseTopNode(SUnit *SU) { |
| 874 | Top.releaseNode(SU, SU->getDepth()); |
| 875 | } |
| 876 | |
| 877 | void ConvergingScheduler::releaseBottomNode(SUnit *SU) { |
| 878 | Bot.releaseNode(SU, SU->getHeight()); |
| 879 | } |
| 880 | |
| 881 | void ConvergingScheduler::SchedBoundary::releaseNode(SUnit *SU, |
| 882 | unsigned ReadyCycle) { |
| 883 | if (SU->isScheduled) |
| 884 | return; |
| 885 | |
| 886 | if (ReadyCycle < MinReadyCycle) |
| 887 | MinReadyCycle = ReadyCycle; |
| 888 | |
| 889 | // Check for interlocks first. For the purpose of other heuristics, an |
| 890 | // instruction that cannot issue appears as if it's not in the ReadyQueue. |
| 891 | if (HazardRec->isEnabled() |
| 892 | && HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard) |
| 893 | Pending.push(SU); |
| 894 | else |
| 895 | Available.push(SU); |
| 896 | } |
| 897 | |
| 898 | /// Move the boundary of scheduled code by one cycle. |
| 899 | void ConvergingScheduler::SchedBoundary::bumpCycle() { |
| 900 | IssueCount = 0; |
| 901 | |
| 902 | assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized"); |
| 903 | unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle); |
| 904 | |
| 905 | if (!HazardRec->isEnabled()) { |
| 906 | // Bypass lots of virtual calls in case of long latency. |
| 907 | CurrCycle = NextCycle; |
| 908 | } |
| 909 | else { |
| 910 | for (; CurrCycle != NextCycle; ++CurrCycle) { |
| 911 | if (isTop()) |
| 912 | HazardRec->AdvanceCycle(); |
| 913 | else |
| 914 | HazardRec->RecedeCycle(); |
| 915 | } |
| 916 | } |
| 917 | CheckPending = true; |
| 918 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 919 | DEBUG(dbgs() << "*** " << Available.getName() << " cycle " |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 920 | << CurrCycle << '\n'); |
| 921 | } |
| 922 | |
| 923 | /// Release pending ready nodes in to the available queue. This makes them |
| 924 | /// visible to heuristics. |
| 925 | void ConvergingScheduler::SchedBoundary::releasePending() { |
| 926 | // If the available queue is empty, it is safe to reset MinReadyCycle. |
| 927 | if (Available.empty()) |
| 928 | MinReadyCycle = UINT_MAX; |
| 929 | |
| 930 | // Check to see if any of the pending instructions are ready to issue. If |
| 931 | // so, add them to the available queue. |
| 932 | for (unsigned i = 0, e = Pending.size(); i != e; ++i) { |
| 933 | SUnit *SU = *(Pending.begin()+i); |
| 934 | unsigned ReadyCycle = isTop() ? SU->getHeight() : SU->getDepth(); |
| 935 | |
| 936 | if (ReadyCycle < MinReadyCycle) |
| 937 | MinReadyCycle = ReadyCycle; |
| 938 | |
| 939 | if (ReadyCycle > CurrCycle) |
| 940 | continue; |
| 941 | |
| 942 | if (HazardRec->isEnabled() |
| 943 | && HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard) |
| 944 | continue; |
| 945 | |
| 946 | Available.push(SU); |
| 947 | Pending.remove(Pending.begin()+i); |
| 948 | --i; --e; |
| 949 | } |
| 950 | CheckPending = false; |
| 951 | } |
| 952 | |
| 953 | /// Remove SU from the ready set for this boundary. |
| 954 | void ConvergingScheduler::SchedBoundary::removeReady(SUnit *SU) { |
| 955 | if (Available.isInQueue(SU)) |
| 956 | Available.remove(Available.find(SU)); |
| 957 | else { |
| 958 | assert(Pending.isInQueue(SU) && "bad ready count"); |
| 959 | Pending.remove(Pending.find(SU)); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /// If this queue only has one ready candidate, return it. As a side effect, |
| 964 | /// advance the cycle until at least one node is ready. If multiple instructions |
| 965 | /// are ready, return NULL. |
| 966 | SUnit *ConvergingScheduler::SchedBoundary::pickOnlyChoice() { |
| 967 | if (CheckPending) |
| 968 | releasePending(); |
| 969 | |
| 970 | for (unsigned i = 0; Available.empty(); ++i) { |
| 971 | assert(i <= HazardRec->getMaxLookAhead() && "permanent hazard"); (void)i; |
| 972 | bumpCycle(); |
| 973 | releasePending(); |
| 974 | } |
| 975 | if (Available.size() == 1) |
| 976 | return *Available.begin(); |
| 977 | return NULL; |
| 978 | } |
| 979 | |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 980 | #ifndef NDEBUG |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 981 | void ConvergingScheduler::traceCandidate(const char *Label, const ReadyQueue &Q, |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 982 | SUnit *SU, PressureElement P) { |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 983 | dbgs() << Label << " " << Q.getName() << " "; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 984 | if (P.isValid()) |
| 985 | dbgs() << TRI->getRegPressureSetName(P.PSetID) << ":" << P.UnitIncrease |
| 986 | << " "; |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 987 | else |
| 988 | dbgs() << " "; |
| 989 | SU->dump(DAG); |
| 990 | } |
| 991 | #endif |
| 992 | |
Andrew Trick | 5429a6b | 2012-05-17 22:37:09 +0000 | [diff] [blame] | 993 | /// pickNodeFromQueue helper that returns true if the LHS reg pressure effect is |
| 994 | /// more desirable than RHS from scheduling standpoint. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 995 | static bool compareRPDelta(const RegPressureDelta &LHS, |
| 996 | const RegPressureDelta &RHS) { |
| 997 | // Compare each component of pressure in decreasing order of importance |
| 998 | // without checking if any are valid. Invalid PressureElements are assumed to |
| 999 | // have UnitIncrease==0, so are neutral. |
Andrew Trick | c8fe4ec | 2012-05-24 22:11:01 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Avoid increasing the max critical pressure in the scheduled region. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1002 | if (LHS.Excess.UnitIncrease != RHS.Excess.UnitIncrease) |
| 1003 | return LHS.Excess.UnitIncrease < RHS.Excess.UnitIncrease; |
| 1004 | |
Andrew Trick | c8fe4ec | 2012-05-24 22:11:01 +0000 | [diff] [blame] | 1005 | // Avoid increasing the max critical pressure in the scheduled region. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1006 | if (LHS.CriticalMax.UnitIncrease != RHS.CriticalMax.UnitIncrease) |
| 1007 | return LHS.CriticalMax.UnitIncrease < RHS.CriticalMax.UnitIncrease; |
| 1008 | |
Andrew Trick | c8fe4ec | 2012-05-24 22:11:01 +0000 | [diff] [blame] | 1009 | // Avoid increasing the max pressure of the entire region. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1010 | if (LHS.CurrentMax.UnitIncrease != RHS.CurrentMax.UnitIncrease) |
| 1011 | return LHS.CurrentMax.UnitIncrease < RHS.CurrentMax.UnitIncrease; |
| 1012 | |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1016 | /// Pick the best candidate from the top queue. |
| 1017 | /// |
| 1018 | /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during |
| 1019 | /// DAG building. To adjust for the current scheduling location we need to |
| 1020 | /// maintain the number of vreg uses remaining to be top-scheduled. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1021 | ConvergingScheduler::CandResult ConvergingScheduler:: |
Andrew Trick | 8c2d921 | 2012-05-24 22:11:03 +0000 | [diff] [blame] | 1022 | pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker, |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1023 | SchedCandidate &Candidate) { |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1024 | DEBUG(Q.dump()); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1025 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1026 | // getMaxPressureDelta temporarily modifies the tracker. |
| 1027 | RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); |
| 1028 | |
| 1029 | // BestSU remains NULL if no top candidates beat the best existing candidate. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1030 | CandResult FoundCandidate = NoCand; |
Andrew Trick | 8c2d921 | 2012-05-24 22:11:03 +0000 | [diff] [blame] | 1031 | for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1032 | RegPressureDelta RPDelta; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1033 | TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta, |
| 1034 | DAG->getRegionCriticalPSets(), |
| 1035 | DAG->getRegPressure().MaxSetPressure); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1036 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1037 | // Initialize the candidate if needed. |
| 1038 | if (!Candidate.SU) { |
| 1039 | Candidate.SU = *I; |
| 1040 | Candidate.RPDelta = RPDelta; |
| 1041 | FoundCandidate = NodeOrder; |
| 1042 | continue; |
| 1043 | } |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1044 | // Avoid exceeding the target's limit. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1045 | if (RPDelta.Excess.UnitIncrease < Candidate.RPDelta.Excess.UnitIncrease) { |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1046 | DEBUG(traceCandidate("ECAND", Q, *I, RPDelta.Excess)); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1047 | Candidate.SU = *I; |
| 1048 | Candidate.RPDelta = RPDelta; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1049 | FoundCandidate = SingleExcess; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1050 | continue; |
| 1051 | } |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1052 | if (RPDelta.Excess.UnitIncrease > Candidate.RPDelta.Excess.UnitIncrease) |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1053 | continue; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1054 | if (FoundCandidate == SingleExcess) |
| 1055 | FoundCandidate = MultiPressure; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1056 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1057 | // Avoid increasing the max critical pressure in the scheduled region. |
| 1058 | if (RPDelta.CriticalMax.UnitIncrease |
| 1059 | < Candidate.RPDelta.CriticalMax.UnitIncrease) { |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1060 | DEBUG(traceCandidate("PCAND", Q, *I, RPDelta.CriticalMax)); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1061 | Candidate.SU = *I; |
| 1062 | Candidate.RPDelta = RPDelta; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1063 | FoundCandidate = SingleCritical; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1064 | continue; |
| 1065 | } |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1066 | if (RPDelta.CriticalMax.UnitIncrease |
| 1067 | > Candidate.RPDelta.CriticalMax.UnitIncrease) |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1068 | continue; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1069 | if (FoundCandidate == SingleCritical) |
| 1070 | FoundCandidate = MultiPressure; |
| 1071 | |
| 1072 | // Avoid increasing the max pressure of the entire region. |
| 1073 | if (RPDelta.CurrentMax.UnitIncrease |
| 1074 | < Candidate.RPDelta.CurrentMax.UnitIncrease) { |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1075 | DEBUG(traceCandidate("MCAND", Q, *I, RPDelta.CurrentMax)); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1076 | Candidate.SU = *I; |
| 1077 | Candidate.RPDelta = RPDelta; |
| 1078 | FoundCandidate = SingleMax; |
| 1079 | continue; |
| 1080 | } |
| 1081 | if (RPDelta.CurrentMax.UnitIncrease |
| 1082 | > Candidate.RPDelta.CurrentMax.UnitIncrease) |
| 1083 | continue; |
| 1084 | if (FoundCandidate == SingleMax) |
| 1085 | FoundCandidate = MultiPressure; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1086 | |
| 1087 | // Fall through to original instruction order. |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1088 | // Only consider node order if Candidate was chosen from this Q. |
| 1089 | if (FoundCandidate == NoCand) |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1090 | continue; |
| 1091 | |
Andrew Trick | f323424 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1092 | if ((Q.getID() == TopQID && (*I)->NodeNum < Candidate.SU->NodeNum) |
| 1093 | || (Q.getID() == BotQID && (*I)->NodeNum > Candidate.SU->NodeNum)) { |
| 1094 | DEBUG(traceCandidate("NCAND", Q, *I)); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1095 | Candidate.SU = *I; |
| 1096 | Candidate.RPDelta = RPDelta; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1097 | FoundCandidate = NodeOrder; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | return FoundCandidate; |
| 1101 | } |
| 1102 | |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1103 | /// Pick the best candidate node from either the top or bottom queue. |
| 1104 | SUnit *ConvergingScheduler::pickNodeBidrectional(bool &IsTopNode) { |
| 1105 | // Schedule as far as possible in the direction of no choice. This is most |
| 1106 | // efficient, but also provides the best heuristics for CriticalPSets. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1107 | if (SUnit *SU = Bot.pickOnlyChoice()) { |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1108 | IsTopNode = false; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1109 | return SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1110 | } |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1111 | if (SUnit *SU = Top.pickOnlyChoice()) { |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1112 | IsTopNode = true; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1113 | return SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1114 | } |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1115 | SchedCandidate BotCand; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1116 | // Prefer bottom scheduling when heuristics are silent. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1117 | CandResult BotResult = pickNodeFromQueue(Bot.Available, |
| 1118 | DAG->getBotRPTracker(), BotCand); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1119 | assert(BotResult != NoCand && "failed to find the first candidate"); |
| 1120 | |
| 1121 | // If either Q has a single candidate that provides the least increase in |
| 1122 | // Excess pressure, we can immediately schedule from that Q. |
| 1123 | // |
| 1124 | // RegionCriticalPSets summarizes the pressure within the scheduled region and |
| 1125 | // affects picking from either Q. If scheduling in one direction must |
| 1126 | // increase pressure for one of the excess PSets, then schedule in that |
| 1127 | // direction first to provide more freedom in the other direction. |
| 1128 | if (BotResult == SingleExcess || BotResult == SingleCritical) { |
| 1129 | IsTopNode = false; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1130 | return BotCand.SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1131 | } |
| 1132 | // Check if the top Q has a better candidate. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1133 | SchedCandidate TopCand; |
| 1134 | CandResult TopResult = pickNodeFromQueue(Top.Available, |
| 1135 | DAG->getTopRPTracker(), TopCand); |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1136 | assert(TopResult != NoCand && "failed to find the first candidate"); |
| 1137 | |
| 1138 | if (TopResult == SingleExcess || TopResult == SingleCritical) { |
| 1139 | IsTopNode = true; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1140 | return TopCand.SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1141 | } |
| 1142 | // If either Q has a single candidate that minimizes pressure above the |
| 1143 | // original region's pressure pick it. |
| 1144 | if (BotResult == SingleMax) { |
| 1145 | IsTopNode = false; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1146 | return BotCand.SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1147 | } |
| 1148 | if (TopResult == SingleMax) { |
| 1149 | IsTopNode = true; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1150 | return TopCand.SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1151 | } |
| 1152 | // Check for a salient pressure difference and pick the best from either side. |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1153 | if (compareRPDelta(TopCand.RPDelta, BotCand.RPDelta)) { |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1154 | IsTopNode = true; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1155 | return TopCand.SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1156 | } |
| 1157 | // Otherwise prefer the bottom candidate in node order. |
| 1158 | IsTopNode = false; |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1159 | return BotCand.SU; |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /// Pick the best node to balance the schedule. Implements MachineSchedStrategy. |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1163 | SUnit *ConvergingScheduler::pickNode(bool &IsTopNode) { |
| 1164 | if (DAG->top() == DAG->bottom()) { |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1165 | assert(Top.Available.empty() && Top.Pending.empty() && |
| 1166 | Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1167 | return NULL; |
| 1168 | } |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1169 | SUnit *SU; |
| 1170 | if (ForceTopDown) { |
| 1171 | SU = DAG->getSUnit(DAG->top()); |
| 1172 | IsTopNode = true; |
| 1173 | } |
| 1174 | else if (ForceBottomUp) { |
| 1175 | SU = DAG->getSUnit(priorNonDebug(DAG->bottom(), DAG->top())); |
| 1176 | IsTopNode = false; |
| 1177 | } |
| 1178 | else { |
Andrew Trick | 73a0d8e | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1179 | SU = pickNodeBidrectional(IsTopNode); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1180 | } |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1181 | if (SU->isTopReady()) |
| 1182 | Top.removeReady(SU); |
| 1183 | if (SU->isBottomReady()) |
| 1184 | Bot.removeReady(SU); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1185 | return SU; |
| 1186 | } |
| 1187 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1188 | /// Update the scheduler's state after scheduling a node. This is the same node |
| 1189 | /// that was just returned by pickNode(). However, ScheduleDAGMI needs to update |
| 1190 | /// it's state based on the current cycle before MachineSchedStrategy. |
| 1191 | void ConvergingScheduler::schedNode(SUnit *SU, bool IsTopNode) { |
| 1192 | DEBUG(dbgs() << " in cycle " << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) |
| 1193 | << '\n'); |
| 1194 | |
| 1195 | // Update the reservation table. |
| 1196 | if (IsTopNode && Top.HazardRec->isEnabled()) { |
| 1197 | Top.HazardRec->EmitInstruction(SU); |
| 1198 | if (Top.HazardRec->atIssueLimit()) { |
| 1199 | DEBUG(dbgs() << "*** Max instrs at cycle " << Top.CurrCycle << '\n'); |
| 1200 | Top.bumpCycle(); |
| 1201 | } |
| 1202 | } |
| 1203 | else if (Bot.HazardRec->isEnabled()) { |
| 1204 | if (SU->isCall) { |
| 1205 | // Calls are scheduled with their preceding instructions. For bottom-up |
| 1206 | // scheduling, clear the pipeline state before emitting. |
| 1207 | Bot.HazardRec->Reset(); |
| 1208 | } |
| 1209 | Bot.HazardRec->EmitInstruction(SU); |
| 1210 | if (Bot.HazardRec->atIssueLimit()) { |
| 1211 | DEBUG(dbgs() << "*** Max instrs at cycle " << Bot.CurrCycle << '\n'); |
| 1212 | Bot.bumpCycle(); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1217 | /// Create the standard converging machine scheduler. This will be used as the |
| 1218 | /// default scheduler if the target does not set a default. |
| 1219 | static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C) { |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 1220 | assert((!ForceTopDown || !ForceBottomUp) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1221 | "-misched-topdown incompatible with -misched-bottomup"); |
| 1222 | return new ScheduleDAGMI(C, new ConvergingScheduler()); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 1223 | } |
| 1224 | static MachineSchedRegistry |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1225 | ConvergingSchedRegistry("converge", "Standard converging scheduler.", |
| 1226 | createConvergingSched); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 1227 | |
| 1228 | //===----------------------------------------------------------------------===// |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 1229 | // Machine Instruction Shuffler for Correctness Testing |
| 1230 | //===----------------------------------------------------------------------===// |
| 1231 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1232 | #ifndef NDEBUG |
| 1233 | namespace { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1234 | /// Apply a less-than relation on the node order, which corresponds to the |
| 1235 | /// instruction order prior to scheduling. IsReverse implements greater-than. |
| 1236 | template<bool IsReverse> |
| 1237 | struct SUnitOrder { |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 1238 | bool operator()(SUnit *A, SUnit *B) const { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1239 | if (IsReverse) |
| 1240 | return A->NodeNum > B->NodeNum; |
| 1241 | else |
| 1242 | return A->NodeNum < B->NodeNum; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 1243 | } |
| 1244 | }; |
| 1245 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1246 | /// Reorder instructions as much as possible. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1247 | class InstructionShuffler : public MachineSchedStrategy { |
| 1248 | bool IsAlternating; |
| 1249 | bool IsTopDown; |
| 1250 | |
| 1251 | // Using a less-than relation (SUnitOrder<false>) for the TopQ priority |
| 1252 | // gives nodes with a higher number higher priority causing the latest |
| 1253 | // instructions to be scheduled first. |
| 1254 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false> > |
| 1255 | TopQ; |
| 1256 | // When scheduling bottom-up, use greater-than as the queue priority. |
| 1257 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true> > |
| 1258 | BottomQ; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1259 | public: |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1260 | InstructionShuffler(bool alternate, bool topdown) |
| 1261 | : IsAlternating(alternate), IsTopDown(topdown) {} |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1262 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1263 | virtual void initialize(ScheduleDAGMI *) { |
| 1264 | TopQ.clear(); |
| 1265 | BottomQ.clear(); |
| 1266 | } |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 1267 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1268 | /// Implement MachineSchedStrategy interface. |
| 1269 | /// ----------------------------------------- |
| 1270 | |
| 1271 | virtual SUnit *pickNode(bool &IsTopNode) { |
| 1272 | SUnit *SU; |
| 1273 | if (IsTopDown) { |
| 1274 | do { |
| 1275 | if (TopQ.empty()) return NULL; |
| 1276 | SU = TopQ.top(); |
| 1277 | TopQ.pop(); |
| 1278 | } while (SU->isScheduled); |
| 1279 | IsTopNode = true; |
| 1280 | } |
| 1281 | else { |
| 1282 | do { |
| 1283 | if (BottomQ.empty()) return NULL; |
| 1284 | SU = BottomQ.top(); |
| 1285 | BottomQ.pop(); |
| 1286 | } while (SU->isScheduled); |
| 1287 | IsTopNode = false; |
| 1288 | } |
| 1289 | if (IsAlternating) |
| 1290 | IsTopDown = !IsTopDown; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 1291 | return SU; |
| 1292 | } |
| 1293 | |
Andrew Trick | 0a39d4e | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1294 | virtual void schedNode(SUnit *SU, bool IsTopNode) {} |
| 1295 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1296 | virtual void releaseTopNode(SUnit *SU) { |
| 1297 | TopQ.push(SU); |
| 1298 | } |
| 1299 | virtual void releaseBottomNode(SUnit *SU) { |
| 1300 | BottomQ.push(SU); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1301 | } |
| 1302 | }; |
| 1303 | } // namespace |
| 1304 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 1305 | static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1306 | bool Alternate = !ForceTopDown && !ForceBottomUp; |
| 1307 | bool TopDown = !ForceBottomUp; |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 1308 | assert((TopDown || !ForceTopDown) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1309 | "-misched-topdown incompatible with -misched-bottomup"); |
| 1310 | return new ScheduleDAGMI(C, new InstructionShuffler(Alternate, TopDown)); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1311 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1312 | static MachineSchedRegistry ShufflerRegistry( |
| 1313 | "shuffle", "Shuffle machine instructions alternating directions", |
| 1314 | createInstructionShuffler); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1315 | #endif // !NDEBUG |