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