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