Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 1 | //===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // MachineScheduler schedules machine instructions after phi elimination. It |
| 11 | // preserves LiveIntervals so it can be invoked before register allocation. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "misched" |
| 16 | |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 17 | #include "RegisterClassInfo.h" |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 18 | #include "RegisterPressure.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineScheduler.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
Andrew Trick | ed395c8 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | e9ef4ed | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/ADT/OwningPtr.h" |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/PriorityQueue.h" |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 31 | |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 32 | #include <queue> |
| 33 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 36 | static cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden, |
| 37 | cl::desc("Force top-down list scheduling")); |
| 38 | static cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden, |
| 39 | cl::desc("Force bottom-up list scheduling")); |
| 40 | |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 41 | #ifndef NDEBUG |
| 42 | static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden, |
| 43 | cl::desc("Pop up a window to show MISched dags after they are processed")); |
Lang Hames | 23f1cbb | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 44 | |
| 45 | static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden, |
| 46 | cl::desc("Stop scheduling after N instructions"), cl::init(~0U)); |
Andrew Trick | 0df7f88 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 47 | #else |
| 48 | static bool ViewMISchedDAGs = false; |
| 49 | #endif // NDEBUG |
| 50 | |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Machine Instruction Scheduling Pass and Registry |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 55 | MachineSchedContext::MachineSchedContext(): |
| 56 | MF(0), MLI(0), MDT(0), PassConfig(0), AA(0), LIS(0) { |
| 57 | RegClassInfo = new RegisterClassInfo(); |
| 58 | } |
| 59 | |
| 60 | MachineSchedContext::~MachineSchedContext() { |
| 61 | delete RegClassInfo; |
| 62 | } |
| 63 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 64 | namespace { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 65 | /// MachineScheduler runs after coalescing and before register allocation. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 66 | class MachineScheduler : public MachineSchedContext, |
| 67 | public MachineFunctionPass { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 68 | public: |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 69 | MachineScheduler(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 70 | |
| 71 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 72 | |
| 73 | virtual void releaseMemory() {} |
| 74 | |
| 75 | virtual bool runOnMachineFunction(MachineFunction&); |
| 76 | |
| 77 | virtual void print(raw_ostream &O, const Module* = 0) const; |
| 78 | |
| 79 | static char ID; // Class identification, replacement for typeinfo |
| 80 | }; |
| 81 | } // namespace |
| 82 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 83 | char MachineScheduler::ID = 0; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 84 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 85 | char &llvm::MachineSchedulerID = MachineScheduler::ID; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 86 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 87 | INITIALIZE_PASS_BEGIN(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 88 | "Machine Instruction Scheduler", false, false) |
| 89 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 90 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 91 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 92 | INITIALIZE_PASS_END(MachineScheduler, "misched", |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 93 | "Machine Instruction Scheduler", false, false) |
| 94 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 95 | MachineScheduler::MachineScheduler() |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 96 | : MachineFunctionPass(ID) { |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 97 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 100 | void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const { |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 101 | AU.setPreservesCFG(); |
| 102 | AU.addRequiredID(MachineDominatorsID); |
| 103 | AU.addRequired<MachineLoopInfo>(); |
| 104 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 105 | AU.addRequired<TargetPassConfig>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 106 | AU.addRequired<SlotIndexes>(); |
| 107 | AU.addPreserved<SlotIndexes>(); |
| 108 | AU.addRequired<LiveIntervals>(); |
| 109 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 110 | MachineFunctionPass::getAnalysisUsage(AU); |
| 111 | } |
| 112 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 113 | MachinePassRegistry MachineSchedRegistry::Registry; |
| 114 | |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 115 | /// A dummy default scheduler factory indicates whether the scheduler |
| 116 | /// is overridden on the command line. |
| 117 | static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) { |
| 118 | return 0; |
| 119 | } |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 120 | |
| 121 | /// MachineSchedOpt allows command line selection of the scheduler. |
| 122 | static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false, |
| 123 | RegisterPassParser<MachineSchedRegistry> > |
| 124 | MachineSchedOpt("misched", |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 125 | cl::init(&useDefaultMachineSched), cl::Hidden, |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 126 | cl::desc("Machine instruction scheduler to use")); |
| 127 | |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 128 | static MachineSchedRegistry |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 129 | DefaultSchedRegistry("default", "Use the target's default scheduler choice.", |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 130 | useDefaultMachineSched); |
| 131 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 132 | /// Forward declare the standard machine scheduler. This will be used as the |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 133 | /// default scheduler if the target does not set a default. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 134 | static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C); |
Andrew Trick | d04ec0c | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 135 | |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 136 | |
| 137 | /// Decrement this iterator until reaching the top or a non-debug instr. |
| 138 | static MachineBasicBlock::iterator |
| 139 | priorNonDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator Beg) { |
| 140 | assert(I != Beg && "reached the top of the region, cannot decrement"); |
| 141 | while (--I != Beg) { |
| 142 | if (!I->isDebugValue()) |
| 143 | break; |
| 144 | } |
| 145 | return I; |
| 146 | } |
| 147 | |
| 148 | /// If this iterator is a debug value, increment until reaching the End or a |
| 149 | /// non-debug instruction. |
| 150 | static MachineBasicBlock::iterator |
| 151 | nextIfDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator End) { |
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 | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 329 | // Register pressure in this region computed by buildSchedGraph. |
| 330 | IntervalPressure RegPressure; |
| 331 | RegPressureTracker RPTracker; |
| 332 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 333 | /// The top of the unscheduled zone. |
| 334 | MachineBasicBlock::iterator CurrentTop; |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 335 | IntervalPressure TopPressure; |
| 336 | RegPressureTracker TopRPTracker; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 337 | |
| 338 | /// The bottom of the unscheduled zone. |
| 339 | MachineBasicBlock::iterator CurrentBottom; |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 340 | IntervalPressure BotPressure; |
| 341 | RegPressureTracker BotRPTracker; |
Lang Hames | 23f1cbb | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 342 | |
| 343 | /// The number of instructions scheduled so far. Used to cut off the |
| 344 | /// scheduler at the point determined by misched-cutoff. |
| 345 | unsigned NumInstrsScheduled; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 346 | public: |
| 347 | ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S): |
| 348 | ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS), |
Andrew Trick | 86b7e2a | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 349 | AA(C->AA), RegClassInfo(C->RegClassInfo), SchedImpl(S), |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 350 | RPTracker(RegPressure), CurrentTop(), TopRPTracker(TopPressure), |
| 351 | CurrentBottom(), BotRPTracker(BotPressure), NumInstrsScheduled(0) {} |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 352 | |
| 353 | ~ScheduleDAGMI() { |
| 354 | delete SchedImpl; |
| 355 | } |
| 356 | |
| 357 | MachineBasicBlock::iterator top() const { return CurrentTop; } |
| 358 | MachineBasicBlock::iterator bottom() const { return CurrentBottom; } |
| 359 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 360 | /// Implement the ScheduleDAGInstrs interface for handling the next scheduling |
| 361 | /// region. This covers all instructions in a block, while schedule() may only |
| 362 | /// cover a subset. |
| 363 | void enterRegion(MachineBasicBlock *bb, |
| 364 | MachineBasicBlock::iterator begin, |
| 365 | MachineBasicBlock::iterator end, |
| 366 | unsigned endcount); |
| 367 | |
| 368 | /// Implement ScheduleDAGInstrs interface for scheduling a sequence of |
| 369 | /// reorderable instructions. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 370 | void schedule(); |
| 371 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 372 | /// Get current register pressure for the top scheduled instructions. |
| 373 | const IntervalPressure &getTopPressure() const { return TopPressure; } |
| 374 | const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; } |
| 375 | |
| 376 | /// Get current register pressure for the bottom scheduled instructions. |
| 377 | const IntervalPressure &getBotPressure() const { return BotPressure; } |
| 378 | const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; } |
| 379 | |
| 380 | /// Get register pressure for the entire scheduling region before scheduling. |
| 381 | const IntervalPressure &getRegPressure() const { return RegPressure; } |
| 382 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 383 | protected: |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 384 | void initRegPressure(); |
| 385 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 386 | void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos); |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 387 | bool checkSchedLimit(); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 388 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 389 | void releaseSucc(SUnit *SU, SDep *SuccEdge); |
| 390 | void releaseSuccessors(SUnit *SU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 391 | void releasePred(SUnit *SU, SDep *PredEdge); |
| 392 | void releasePredecessors(SUnit *SU); |
Andrew Trick | 000b250 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 393 | |
| 394 | void placeDebugValues(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 395 | }; |
| 396 | } // namespace |
| 397 | |
| 398 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When |
| 399 | /// NumPredsLeft reaches zero, release the successor node. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 400 | void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) { |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 401 | SUnit *SuccSU = SuccEdge->getSUnit(); |
| 402 | |
| 403 | #ifndef NDEBUG |
| 404 | if (SuccSU->NumPredsLeft == 0) { |
| 405 | dbgs() << "*** Scheduling failed! ***\n"; |
| 406 | SuccSU->dump(this); |
| 407 | dbgs() << " has been released too many times!\n"; |
| 408 | llvm_unreachable(0); |
| 409 | } |
| 410 | #endif |
| 411 | --SuccSU->NumPredsLeft; |
| 412 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 413 | SchedImpl->releaseTopNode(SuccSU); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /// releaseSuccessors - Call releaseSucc on each of SU's successors. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 417 | void ScheduleDAGMI::releaseSuccessors(SUnit *SU) { |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 418 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 419 | I != E; ++I) { |
| 420 | releaseSucc(SU, &*I); |
| 421 | } |
| 422 | } |
| 423 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 424 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When |
| 425 | /// NumSuccsLeft reaches zero, release the predecessor node. |
| 426 | void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) { |
| 427 | SUnit *PredSU = PredEdge->getSUnit(); |
| 428 | |
| 429 | #ifndef NDEBUG |
| 430 | if (PredSU->NumSuccsLeft == 0) { |
| 431 | dbgs() << "*** Scheduling failed! ***\n"; |
| 432 | PredSU->dump(this); |
| 433 | dbgs() << " has been released too many times!\n"; |
| 434 | llvm_unreachable(0); |
| 435 | } |
| 436 | #endif |
| 437 | --PredSU->NumSuccsLeft; |
| 438 | if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) |
| 439 | SchedImpl->releaseBottomNode(PredSU); |
| 440 | } |
| 441 | |
| 442 | /// releasePredecessors - Call releasePred on each of SU's predecessors. |
| 443 | void ScheduleDAGMI::releasePredecessors(SUnit *SU) { |
| 444 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 445 | I != E; ++I) { |
| 446 | releasePred(SU, &*I); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void ScheduleDAGMI::moveInstruction(MachineInstr *MI, |
| 451 | MachineBasicBlock::iterator InsertPos) { |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 452 | // Advance RegionBegin if the first instruction moves down. |
Andrew Trick | 1ce062f | 2012-03-21 04:12:10 +0000 | [diff] [blame] | 453 | if (&*RegionBegin == MI) |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 454 | ++RegionBegin; |
| 455 | |
| 456 | // Update the instruction stream. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 457 | BB->splice(InsertPos, BB, MI); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 458 | |
| 459 | // Update LiveIntervals |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 460 | LIS->handleMove(MI); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 461 | |
| 462 | // Recede RegionBegin if an instruction moves above the first. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 463 | if (RegionBegin == InsertPos) |
| 464 | RegionBegin = MI; |
| 465 | } |
| 466 | |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 467 | bool ScheduleDAGMI::checkSchedLimit() { |
| 468 | #ifndef NDEBUG |
| 469 | if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) { |
| 470 | CurrentTop = CurrentBottom; |
| 471 | return false; |
| 472 | } |
| 473 | ++NumInstrsScheduled; |
| 474 | #endif |
| 475 | return true; |
| 476 | } |
| 477 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 478 | /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after |
| 479 | /// crossing a scheduling boundary. [begin, end) includes all instructions in |
| 480 | /// the region, including the boundary itself and single-instruction regions |
| 481 | /// that don't get scheduled. |
| 482 | void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb, |
| 483 | MachineBasicBlock::iterator begin, |
| 484 | MachineBasicBlock::iterator end, |
| 485 | unsigned endcount) |
| 486 | { |
| 487 | ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount); |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 488 | |
| 489 | // For convenience remember the end of the liveness region. |
| 490 | LiveRegionEnd = |
| 491 | (RegionEnd == bb->end()) ? RegionEnd : llvm::next(RegionEnd); |
| 492 | } |
| 493 | |
| 494 | // Setup the register pressure trackers for the top scheduled top and bottom |
| 495 | // scheduled regions. |
| 496 | void ScheduleDAGMI::initRegPressure() { |
| 497 | TopRPTracker.init(&MF, RegClassInfo, LIS, BB, RegionBegin); |
| 498 | BotRPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd); |
| 499 | |
| 500 | // Close the RPTracker to finalize live ins. |
| 501 | RPTracker.closeRegion(); |
| 502 | |
| 503 | // Initialize the live ins and live outs. |
| 504 | TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs); |
| 505 | BotRPTracker.addLiveRegs(RPTracker.getPressure().LiveOutRegs); |
| 506 | |
| 507 | // Close one end of the tracker so we can call |
| 508 | // getMaxUpward/DownwardPressureDelta before advancing across any |
| 509 | // instructions. This converts currently live regs into live ins/outs. |
| 510 | TopRPTracker.closeTop(); |
| 511 | BotRPTracker.closeBottom(); |
| 512 | |
| 513 | // Account for liveness generated by the region boundary. |
| 514 | if (LiveRegionEnd != RegionEnd) |
| 515 | BotRPTracker.recede(); |
| 516 | |
| 517 | assert(BotRPTracker.getPos() == RegionEnd && "Can't find the region bottom"); |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 520 | /// schedule - Called back from MachineScheduler::runOnMachineFunction |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 521 | /// after setting up the current scheduling region. [RegionBegin, RegionEnd) |
| 522 | /// only includes instructions that have DAG nodes, not scheduling boundaries. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 523 | void ScheduleDAGMI::schedule() { |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 524 | // Initialize the register pressure tracker used by buildSchedGraph. |
| 525 | RPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd); |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 526 | |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 527 | // Account for liveness generate by the region boundary. |
| 528 | if (LiveRegionEnd != RegionEnd) |
| 529 | RPTracker.recede(); |
| 530 | |
| 531 | // Build the DAG, and compute current register pressure. |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 532 | buildSchedGraph(AA, &RPTracker); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 533 | |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 534 | // Initialize top/bottom trackers after computing region pressure. |
| 535 | initRegPressure(); |
| 536 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 537 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
| 538 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 539 | SUnits[su].dumpAll(this)); |
| 540 | |
| 541 | if (ViewMISchedDAGs) viewGraph(); |
| 542 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 543 | SchedImpl->initialize(this); |
| 544 | |
| 545 | // 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] | 546 | releaseSuccessors(&EntrySU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 547 | releasePredecessors(&ExitSU); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 548 | |
| 549 | // Release all DAG roots for scheduling. |
| 550 | for (std::vector<SUnit>::iterator I = SUnits.begin(), E = SUnits.end(); |
| 551 | I != E; ++I) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 552 | // A SUnit is ready to top schedule if it has no predecessors. |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 553 | if (I->Preds.empty()) |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 554 | SchedImpl->releaseTopNode(&(*I)); |
| 555 | // A SUnit is ready to bottom schedule if it has no successors. |
| 556 | if (I->Succs.empty()) |
| 557 | SchedImpl->releaseBottomNode(&(*I)); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 560 | CurrentTop = nextIfDebug(RegionBegin, RegionEnd); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 561 | CurrentBottom = RegionEnd; |
| 562 | bool IsTopNode = false; |
| 563 | while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) { |
| 564 | DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom") |
| 565 | << " Scheduling Instruction:\n"; SU->dump(this)); |
Andrew Trick | 0b0d899 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 566 | if (!checkSchedLimit()) |
| 567 | break; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 568 | |
| 569 | // Move the instruction to its new location in the instruction stream. |
| 570 | MachineInstr *MI = SU->getInstr(); |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 571 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 572 | if (IsTopNode) { |
| 573 | assert(SU->isTopReady() && "node still has unscheduled dependencies"); |
| 574 | if (&*CurrentTop == MI) |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 575 | CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 576 | else { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 577 | moveInstruction(MI, CurrentTop); |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 578 | TopRPTracker.setPos(MI); |
| 579 | } |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 580 | |
| 581 | // Update top scheduled pressure. |
| 582 | TopRPTracker.advance(); |
| 583 | assert(TopRPTracker.getPos() == CurrentTop && "out of sync"); |
| 584 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 585 | // Release dependent instructions for scheduling. |
| 586 | releaseSuccessors(SU); |
| 587 | } |
| 588 | else { |
| 589 | assert(SU->isBottomReady() && "node still has unscheduled dependencies"); |
Andrew Trick | eb45ebb | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 590 | MachineBasicBlock::iterator priorII = |
| 591 | priorNonDebug(CurrentBottom, CurrentTop); |
| 592 | if (&*priorII == MI) |
| 593 | CurrentBottom = priorII; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 594 | else { |
Andrew Trick | 811d9268 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 595 | if (&*CurrentTop == MI) { |
| 596 | CurrentTop = nextIfDebug(++CurrentTop, priorII); |
| 597 | TopRPTracker.setPos(CurrentTop); |
| 598 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 599 | moveInstruction(MI, CurrentBottom); |
| 600 | CurrentBottom = MI; |
| 601 | } |
Andrew Trick | 7f8ab78 | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 602 | // Update bottom scheduled pressure. |
| 603 | BotRPTracker.recede(); |
| 604 | assert(BotRPTracker.getPos() == CurrentBottom && "out of sync"); |
| 605 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 606 | // Release dependent instructions for scheduling. |
| 607 | releasePredecessors(SU); |
| 608 | } |
| 609 | SU->isScheduled = true; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 610 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 611 | assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); |
Andrew Trick | 000b250 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 612 | |
| 613 | placeDebugValues(); |
| 614 | } |
| 615 | |
| 616 | /// Reinsert any remaining debug_values, just like the PostRA scheduler. |
| 617 | void ScheduleDAGMI::placeDebugValues() { |
| 618 | // If first instruction was a DBG_VALUE then put it back. |
| 619 | if (FirstDbgValue) { |
| 620 | BB->splice(RegionBegin, BB, FirstDbgValue); |
| 621 | RegionBegin = FirstDbgValue; |
| 622 | } |
| 623 | |
| 624 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 625 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 626 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 627 | MachineInstr *DbgValue = P.first; |
| 628 | MachineBasicBlock::iterator OrigPrevMI = P.second; |
| 629 | BB->splice(++OrigPrevMI, BB, DbgValue); |
| 630 | if (OrigPrevMI == llvm::prior(RegionEnd)) |
| 631 | RegionEnd = DbgValue; |
| 632 | } |
| 633 | DbgValues.clear(); |
| 634 | FirstDbgValue = NULL; |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | //===----------------------------------------------------------------------===// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 638 | // ConvergingScheduler - Implementation of the standard MachineSchedStrategy. |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 639 | //===----------------------------------------------------------------------===// |
| 640 | |
| 641 | namespace { |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 642 | /// Wrapper around a vector of SUnits with some basic convenience methods. |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 643 | struct ReadyQ { |
| 644 | typedef std::vector<SUnit*>::iterator iterator; |
| 645 | |
| 646 | unsigned ID; |
| 647 | std::vector<SUnit*> Queue; |
| 648 | |
| 649 | ReadyQ(unsigned id): ID(id) {} |
| 650 | |
| 651 | bool isInQueue(SUnit *SU) const { |
| 652 | return SU->NodeQueueId & ID; |
| 653 | } |
| 654 | |
| 655 | bool empty() const { return Queue.empty(); } |
| 656 | |
Andrew Trick | 16716c7 | 2012-05-10 21:06:14 +0000 | [diff] [blame] | 657 | iterator begin() { return Queue.begin(); } |
| 658 | |
| 659 | iterator end() { return Queue.end(); } |
| 660 | |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 661 | iterator find(SUnit *SU) { |
| 662 | return std::find(Queue.begin(), Queue.end(), SU); |
| 663 | } |
| 664 | |
| 665 | void push(SUnit *SU) { |
| 666 | Queue.push_back(SU); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 667 | SU->NodeQueueId |= ID; |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void remove(iterator I) { |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 671 | (*I)->NodeQueueId &= ~ID; |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 672 | *I = Queue.back(); |
| 673 | Queue.pop_back(); |
| 674 | } |
| 675 | }; |
| 676 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 677 | /// ConvergingScheduler shrinks the unscheduled zone using heuristics to balance |
| 678 | /// the schedule. |
| 679 | class ConvergingScheduler : public MachineSchedStrategy { |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 680 | |
| 681 | /// Store the state used by ConvergingScheduler heuristics, required for the |
| 682 | /// lifetime of one invocation of pickNode(). |
| 683 | struct SchedCandidate { |
| 684 | // The best SUnit candidate. |
| 685 | SUnit *SU; |
| 686 | |
| 687 | // Register pressure values for the best candidate. |
| 688 | RegPressureDelta RPDelta; |
| 689 | |
| 690 | SchedCandidate(): SU(NULL) {} |
| 691 | }; |
| 692 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 693 | ScheduleDAGMI *DAG; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 694 | const TargetRegisterInfo *TRI; |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 695 | |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 696 | ReadyQ TopQueue; |
| 697 | ReadyQ BotQueue; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 698 | |
| 699 | public: |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 700 | /// SUnit::NodeQueueId = 0 (none), = 1 (top), = 2 (bottom), = 3 (both) |
| 701 | enum { |
| 702 | TopQID = 1, |
| 703 | BotQID = 2 |
| 704 | }; |
| 705 | |
| 706 | ConvergingScheduler(): DAG(0), TRI(0), TopQueue(TopQID), BotQueue(BotQID) {} |
| 707 | |
| 708 | static const char *getQName(unsigned ID) { |
| 709 | switch(ID) { |
| 710 | default: return "NoQ"; |
| 711 | case TopQID: return "TopQ"; |
| 712 | case BotQID: return "BotQ"; |
| 713 | }; |
| 714 | } |
Andrew Trick | d38f87e | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 715 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 716 | virtual void initialize(ScheduleDAGMI *dag) { |
| 717 | DAG = dag; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 718 | TRI = DAG->TRI; |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 719 | |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 720 | assert((!ForceTopDown || !ForceBottomUp) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 721 | "-misched-topdown incompatible with -misched-bottomup"); |
| 722 | } |
| 723 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 724 | virtual SUnit *pickNode(bool &IsTopNode); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 725 | |
| 726 | virtual void releaseTopNode(SUnit *SU) { |
Andrew Trick | 16716c7 | 2012-05-10 21:06:14 +0000 | [diff] [blame] | 727 | if (!SU->isScheduled) |
| 728 | TopQueue.push(SU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 729 | } |
| 730 | virtual void releaseBottomNode(SUnit *SU) { |
Andrew Trick | 16716c7 | 2012-05-10 21:06:14 +0000 | [diff] [blame] | 731 | if (!SU->isScheduled) |
| 732 | BotQueue.push(SU); |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 733 | } |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 734 | protected: |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 735 | #ifndef NDEBUG |
| 736 | void traceCandidate(const char *Label, unsigned QID, SUnit *SU, |
| 737 | int RPDiff, unsigned PSetID); |
| 738 | #endif |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 739 | bool pickNodeFromQueue(ReadyQ &Q, const RegPressureTracker &RPTracker, |
| 740 | SchedCandidate &Candidate); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 741 | }; |
| 742 | } // namespace |
| 743 | |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 744 | #ifndef NDEBUG |
| 745 | void ConvergingScheduler:: |
| 746 | traceCandidate(const char *Label, unsigned QID, SUnit *SU, |
| 747 | int RPDiff, unsigned PSetID) { |
| 748 | dbgs() << Label << getQName(QID) << " "; |
| 749 | if (RPDiff) |
| 750 | dbgs() << TRI->getRegPressureSetName(PSetID) << ":" << RPDiff << " "; |
| 751 | else |
| 752 | dbgs() << " "; |
| 753 | SU->dump(DAG); |
| 754 | } |
| 755 | #endif |
| 756 | |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 757 | /// Pick the best candidate from the top queue. |
| 758 | /// |
| 759 | /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during |
| 760 | /// DAG building. To adjust for the current scheduling location we need to |
| 761 | /// maintain the number of vreg uses remaining to be top-scheduled. |
| 762 | bool ConvergingScheduler::pickNodeFromQueue(ReadyQ &Q, |
| 763 | const RegPressureTracker &RPTracker, |
| 764 | SchedCandidate &Candidate) { |
| 765 | // getMaxPressureDelta temporarily modifies the tracker. |
| 766 | RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); |
| 767 | |
| 768 | // BestSU remains NULL if no top candidates beat the best existing candidate. |
| 769 | bool FoundCandidate = false; |
| 770 | for (ReadyQ::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { |
| 771 | |
| 772 | RegPressureDelta RPDelta; |
| 773 | TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta); |
| 774 | |
| 775 | // Avoid exceeding the target's limit. |
| 776 | if (!Candidate.SU || RPDelta.ExcessUnits < Candidate.RPDelta.ExcessUnits) { |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 777 | DEBUG(traceCandidate(Candidate.SU ? "PCAND" : "ACAND", Q.ID, *I, |
| 778 | RPDelta.ExcessUnits, RPDelta.ExcessSetID)); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 779 | Candidate.SU = *I; |
| 780 | Candidate.RPDelta = RPDelta; |
| 781 | FoundCandidate = true; |
| 782 | continue; |
| 783 | } |
| 784 | if (RPDelta.ExcessUnits > Candidate.RPDelta.ExcessUnits) |
| 785 | continue; |
| 786 | |
| 787 | // Avoid increasing the max pressure. |
| 788 | if (RPDelta.MaxUnitIncrease < Candidate.RPDelta.MaxUnitIncrease) { |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 789 | DEBUG(traceCandidate("MCAND", Q.ID, *I, |
| 790 | RPDelta.ExcessUnits, RPDelta.ExcessSetID)); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 791 | Candidate.SU = *I; |
| 792 | Candidate.RPDelta = RPDelta; |
| 793 | FoundCandidate = true; |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 794 | continue; |
| 795 | } |
| 796 | if (RPDelta.MaxUnitIncrease > Candidate.RPDelta.MaxUnitIncrease) |
| 797 | continue; |
| 798 | |
| 799 | // Fall through to original instruction order. |
| 800 | // Only consider node order if BestSU was chosen from this Q. |
| 801 | if (!FoundCandidate) |
| 802 | continue; |
| 803 | |
| 804 | if ((Q.ID == TopQID && (*I)->NodeNum < Candidate.SU->NodeNum) |
| 805 | || (Q.ID == BotQID && (*I)->NodeNum > Candidate.SU->NodeNum)) { |
Andrew Trick | 28ebc89 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 806 | DEBUG(traceCandidate("NCAND", Q.ID, *I, 0, 0)); |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 807 | Candidate.SU = *I; |
| 808 | Candidate.RPDelta = RPDelta; |
| 809 | FoundCandidate = true; |
| 810 | } |
| 811 | } |
| 812 | return FoundCandidate; |
| 813 | } |
| 814 | |
| 815 | /// Pick the best node from either the top or bottom queue to balance the |
| 816 | /// schedule. |
| 817 | SUnit *ConvergingScheduler::pickNode(bool &IsTopNode) { |
| 818 | if (DAG->top() == DAG->bottom()) { |
| 819 | assert(TopQueue.empty() && BotQueue.empty() && "ReadyQ garbage"); |
| 820 | return NULL; |
| 821 | } |
Andrew Trick | 7196a8f | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 822 | SUnit *SU; |
| 823 | if (ForceTopDown) { |
| 824 | SU = DAG->getSUnit(DAG->top()); |
| 825 | IsTopNode = true; |
| 826 | } |
| 827 | else if (ForceBottomUp) { |
| 828 | SU = DAG->getSUnit(priorNonDebug(DAG->bottom(), DAG->top())); |
| 829 | IsTopNode = false; |
| 830 | } |
| 831 | else { |
| 832 | SchedCandidate Candidate; |
| 833 | // Prefer picking from the bottom. |
| 834 | pickNodeFromQueue(BotQueue, DAG->getBotRPTracker(), Candidate); |
| 835 | IsTopNode = |
| 836 | pickNodeFromQueue(TopQueue, DAG->getTopRPTracker(), Candidate); |
| 837 | SU = Candidate.SU; |
| 838 | } |
| 839 | if (SU->isTopReady()) { |
| 840 | assert(!TopQueue.empty() && "bad ready count"); |
| 841 | TopQueue.remove(TopQueue.find(SU)); |
| 842 | } |
| 843 | if (SU->isBottomReady()) { |
| 844 | assert(!BotQueue.empty() && "bad ready count"); |
| 845 | BotQueue.remove(BotQueue.find(SU)); |
| 846 | } |
| 847 | return SU; |
| 848 | } |
| 849 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 850 | /// Create the standard converging machine scheduler. This will be used as the |
| 851 | /// default scheduler if the target does not set a default. |
| 852 | static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C) { |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 853 | assert((!ForceTopDown || !ForceBottomUp) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 854 | "-misched-topdown incompatible with -misched-bottomup"); |
| 855 | return new ScheduleDAGMI(C, new ConvergingScheduler()); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 856 | } |
| 857 | static MachineSchedRegistry |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 858 | ConvergingSchedRegistry("converge", "Standard converging scheduler.", |
| 859 | createConvergingSched); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 860 | |
| 861 | //===----------------------------------------------------------------------===// |
Andrew Trick | 5edf2f0 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 862 | // Machine Instruction Shuffler for Correctness Testing |
| 863 | //===----------------------------------------------------------------------===// |
| 864 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 865 | #ifndef NDEBUG |
| 866 | namespace { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 867 | /// Apply a less-than relation on the node order, which corresponds to the |
| 868 | /// instruction order prior to scheduling. IsReverse implements greater-than. |
| 869 | template<bool IsReverse> |
| 870 | struct SUnitOrder { |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 871 | bool operator()(SUnit *A, SUnit *B) const { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 872 | if (IsReverse) |
| 873 | return A->NodeNum > B->NodeNum; |
| 874 | else |
| 875 | return A->NodeNum < B->NodeNum; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 876 | } |
| 877 | }; |
| 878 | |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 879 | /// Reorder instructions as much as possible. |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 880 | class InstructionShuffler : public MachineSchedStrategy { |
| 881 | bool IsAlternating; |
| 882 | bool IsTopDown; |
| 883 | |
| 884 | // Using a less-than relation (SUnitOrder<false>) for the TopQ priority |
| 885 | // gives nodes with a higher number higher priority causing the latest |
| 886 | // instructions to be scheduled first. |
| 887 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false> > |
| 888 | TopQ; |
| 889 | // When scheduling bottom-up, use greater-than as the queue priority. |
| 890 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true> > |
| 891 | BottomQ; |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 892 | public: |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 893 | InstructionShuffler(bool alternate, bool topdown) |
| 894 | : IsAlternating(alternate), IsTopDown(topdown) {} |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 895 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 896 | virtual void initialize(ScheduleDAGMI *) { |
| 897 | TopQ.clear(); |
| 898 | BottomQ.clear(); |
| 899 | } |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 900 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 901 | /// Implement MachineSchedStrategy interface. |
| 902 | /// ----------------------------------------- |
| 903 | |
| 904 | virtual SUnit *pickNode(bool &IsTopNode) { |
| 905 | SUnit *SU; |
| 906 | if (IsTopDown) { |
| 907 | do { |
| 908 | if (TopQ.empty()) return NULL; |
| 909 | SU = TopQ.top(); |
| 910 | TopQ.pop(); |
| 911 | } while (SU->isScheduled); |
| 912 | IsTopNode = true; |
| 913 | } |
| 914 | else { |
| 915 | do { |
| 916 | if (BottomQ.empty()) return NULL; |
| 917 | SU = BottomQ.top(); |
| 918 | BottomQ.pop(); |
| 919 | } while (SU->isScheduled); |
| 920 | IsTopNode = false; |
| 921 | } |
| 922 | if (IsAlternating) |
| 923 | IsTopDown = !IsTopDown; |
Andrew Trick | c6cf11b | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 924 | return SU; |
| 925 | } |
| 926 | |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 927 | virtual void releaseTopNode(SUnit *SU) { |
| 928 | TopQ.push(SU); |
| 929 | } |
| 930 | virtual void releaseBottomNode(SUnit *SU) { |
| 931 | BottomQ.push(SU); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 932 | } |
| 933 | }; |
| 934 | } // namespace |
| 935 | |
Andrew Trick | c174eaf | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 936 | static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) { |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 937 | bool Alternate = !ForceTopDown && !ForceBottomUp; |
| 938 | bool TopDown = !ForceBottomUp; |
Benjamin Kramer | 689e0b4 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 939 | assert((TopDown || !ForceTopDown) && |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 940 | "-misched-topdown incompatible with -misched-bottomup"); |
| 941 | return new ScheduleDAGMI(C, new InstructionShuffler(Alternate, TopDown)); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 942 | } |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 943 | static MachineSchedRegistry ShufflerRegistry( |
| 944 | "shuffle", "Shuffle machine instructions alternating directions", |
| 945 | createInstructionShuffler); |
Andrew Trick | 96f678f | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 946 | #endif // !NDEBUG |