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