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