Andrew Trick | 6a50baa | 2012-05-17 22:37:09 +0000 | [diff] [blame] | 1 | //===- MachineScheduler.cpp - Machine Instruction Scheduler ---------------===// |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // MachineScheduler schedules machine instructions after phi elimination. It |
| 11 | // preserves LiveIntervals so it can be invoked before register allocation. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "misched" |
| 16 | |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineScheduler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/OwningPtr.h" |
| 19 | #include "llvm/ADT/PriorityQueue.h" |
| 20 | #include "llvm/Analysis/AliasAnalysis.h" |
| 21 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakub Staszak | df17ddd | 2013-03-10 13:11:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineDominators.h" |
| 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Andrew Trick | 736dd9a | 2013-06-21 18:32:58 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/Passes.h" |
Andrew Trick | 05ff466 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Andrew Trick | cd1c2f9 | 2012-11-28 05:13:24 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/ScheduleDFS.h" |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
Andrew Trick | ea9fd95 | 2013-01-25 07:45:29 +0000 | [diff] [blame] | 32 | #include "llvm/Support/GraphWriter.h" |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Jakub Staszak | 80df8b8 | 2013-06-14 00:00:13 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 35 | #include <queue> |
| 36 | |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 39 | namespace llvm { |
| 40 | cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden, |
| 41 | cl::desc("Force top-down list scheduling")); |
| 42 | cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden, |
| 43 | cl::desc("Force bottom-up list scheduling")); |
| 44 | } |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 45 | |
Andrew Trick | a5f1956 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 46 | #ifndef NDEBUG |
| 47 | static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden, |
| 48 | cl::desc("Pop up a window to show MISched dags after they are processed")); |
Lang Hames | dd98c49 | 2012-03-19 18:38:38 +0000 | [diff] [blame] | 49 | |
| 50 | static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden, |
| 51 | cl::desc("Stop scheduling after N instructions"), cl::init(~0U)); |
Andrew Trick | a5f1956 | 2012-03-07 00:18:25 +0000 | [diff] [blame] | 52 | #else |
| 53 | static bool ViewMISchedDAGs = false; |
| 54 | #endif // NDEBUG |
| 55 | |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 56 | static cl::opt<bool> EnableRegPressure("misched-regpressure", cl::Hidden, |
| 57 | cl::desc("Enable register pressure scheduling."), cl::init(true)); |
| 58 | |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 59 | static cl::opt<bool> EnableCyclicPath("misched-cyclicpath", cl::Hidden, |
Andrew Trick | 6c88b35 | 2013-09-09 23:31:14 +0000 | [diff] [blame] | 60 | cl::desc("Enable cyclic critical path analysis."), cl::init(true)); |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 61 | |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 62 | static cl::opt<bool> EnableLoadCluster("misched-cluster", cl::Hidden, |
Andrew Trick | 108c88c | 2012-11-13 08:47:29 +0000 | [diff] [blame] | 63 | cl::desc("Enable load clustering."), cl::init(true)); |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 64 | |
Andrew Trick | 26328024 | 2012-11-12 19:52:20 +0000 | [diff] [blame] | 65 | // Experimental heuristics |
| 66 | static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden, |
Andrew Trick | 108c88c | 2012-11-13 08:47:29 +0000 | [diff] [blame] | 67 | cl::desc("Enable scheduling for macro fusion."), cl::init(true)); |
Andrew Trick | 26328024 | 2012-11-12 19:52:20 +0000 | [diff] [blame] | 68 | |
Andrew Trick | 48f2a72 | 2013-03-08 05:40:34 +0000 | [diff] [blame] | 69 | static cl::opt<bool> VerifyScheduling("verify-misched", cl::Hidden, |
| 70 | cl::desc("Verify machine instrs before and after machine scheduling")); |
| 71 | |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 72 | // DAG subtrees must have at least this many nodes. |
| 73 | static const unsigned MinSubtreeSize = 8; |
| 74 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 75 | // Pin the vtables to this file. |
| 76 | void MachineSchedStrategy::anchor() {} |
| 77 | void ScheduleDAGMutation::anchor() {} |
| 78 | |
Andrew Trick | 6344087 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
| 80 | // Machine Instruction Scheduling Pass and Registry |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
Andrew Trick | 4d4b546 | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 83 | MachineSchedContext::MachineSchedContext(): |
| 84 | MF(0), MLI(0), MDT(0), PassConfig(0), AA(0), LIS(0) { |
| 85 | RegClassInfo = new RegisterClassInfo(); |
| 86 | } |
| 87 | |
| 88 | MachineSchedContext::~MachineSchedContext() { |
| 89 | delete RegClassInfo; |
| 90 | } |
| 91 | |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 92 | namespace { |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 93 | /// MachineScheduler runs after coalescing and before register allocation. |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 94 | class MachineScheduler : public MachineSchedContext, |
| 95 | public MachineFunctionPass { |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 96 | public: |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 97 | MachineScheduler(); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 98 | |
| 99 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 100 | |
| 101 | virtual void releaseMemory() {} |
| 102 | |
| 103 | virtual bool runOnMachineFunction(MachineFunction&); |
| 104 | |
| 105 | virtual void print(raw_ostream &O, const Module* = 0) const; |
| 106 | |
| 107 | static char ID; // Class identification, replacement for typeinfo |
Andrew Trick | 978674b | 2013-09-20 05:14:41 +0000 | [diff] [blame] | 108 | |
| 109 | protected: |
| 110 | ScheduleDAGInstrs *createMachineScheduler(); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 111 | }; |
| 112 | } // namespace |
| 113 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 114 | char MachineScheduler::ID = 0; |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 115 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 116 | char &llvm::MachineSchedulerID = MachineScheduler::ID; |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 117 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 118 | INITIALIZE_PASS_BEGIN(MachineScheduler, "misched", |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 119 | "Machine Instruction Scheduler", false, false) |
| 120 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 121 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 122 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 123 | INITIALIZE_PASS_END(MachineScheduler, "misched", |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 124 | "Machine Instruction Scheduler", false, false) |
| 125 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 126 | MachineScheduler::MachineScheduler() |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 127 | : MachineFunctionPass(ID) { |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 128 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 131 | void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const { |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 132 | AU.setPreservesCFG(); |
| 133 | AU.addRequiredID(MachineDominatorsID); |
| 134 | AU.addRequired<MachineLoopInfo>(); |
| 135 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 136 | AU.addRequired<TargetPassConfig>(); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 137 | AU.addRequired<SlotIndexes>(); |
| 138 | AU.addPreserved<SlotIndexes>(); |
| 139 | AU.addRequired<LiveIntervals>(); |
| 140 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 141 | MachineFunctionPass::getAnalysisUsage(AU); |
| 142 | } |
| 143 | |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 144 | MachinePassRegistry MachineSchedRegistry::Registry; |
| 145 | |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 146 | /// A dummy default scheduler factory indicates whether the scheduler |
| 147 | /// is overridden on the command line. |
| 148 | static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) { |
| 149 | return 0; |
| 150 | } |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 151 | |
| 152 | /// MachineSchedOpt allows command line selection of the scheduler. |
| 153 | static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false, |
| 154 | RegisterPassParser<MachineSchedRegistry> > |
| 155 | MachineSchedOpt("misched", |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 156 | cl::init(&useDefaultMachineSched), cl::Hidden, |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 157 | cl::desc("Machine instruction scheduler to use")); |
| 158 | |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 159 | static MachineSchedRegistry |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 160 | DefaultSchedRegistry("default", "Use the target's default scheduler choice.", |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 161 | useDefaultMachineSched); |
| 162 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 163 | /// Forward declare the standard machine scheduler. This will be used as the |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 164 | /// default scheduler if the target does not set a default. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 165 | static ScheduleDAGInstrs *createGenericSched(MachineSchedContext *C); |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 166 | |
Andrew Trick | cc45a28 | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 167 | |
| 168 | /// Decrement this iterator until reaching the top or a non-debug instr. |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 169 | static MachineBasicBlock::const_iterator |
| 170 | priorNonDebug(MachineBasicBlock::const_iterator I, |
| 171 | MachineBasicBlock::const_iterator Beg) { |
Andrew Trick | cc45a28 | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 172 | assert(I != Beg && "reached the top of the region, cannot decrement"); |
| 173 | while (--I != Beg) { |
| 174 | if (!I->isDebugValue()) |
| 175 | break; |
| 176 | } |
| 177 | return I; |
| 178 | } |
| 179 | |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 180 | /// Non-const version. |
| 181 | static MachineBasicBlock::iterator |
| 182 | priorNonDebug(MachineBasicBlock::iterator I, |
| 183 | MachineBasicBlock::const_iterator Beg) { |
| 184 | return const_cast<MachineInstr*>( |
| 185 | &*priorNonDebug(MachineBasicBlock::const_iterator(I), Beg)); |
| 186 | } |
| 187 | |
Andrew Trick | cc45a28 | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 188 | /// If this iterator is a debug value, increment until reaching the End or a |
| 189 | /// non-debug instruction. |
Andrew Trick | 2c4f8b7 | 2013-08-31 05:17:58 +0000 | [diff] [blame] | 190 | static MachineBasicBlock::const_iterator |
| 191 | nextIfDebug(MachineBasicBlock::const_iterator I, |
| 192 | MachineBasicBlock::const_iterator End) { |
Andrew Trick | 463b2f1 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 193 | for(; I != End; ++I) { |
Andrew Trick | cc45a28 | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 194 | if (!I->isDebugValue()) |
| 195 | break; |
| 196 | } |
| 197 | return I; |
| 198 | } |
| 199 | |
Andrew Trick | 2c4f8b7 | 2013-08-31 05:17:58 +0000 | [diff] [blame] | 200 | /// Non-const version. |
| 201 | static MachineBasicBlock::iterator |
| 202 | nextIfDebug(MachineBasicBlock::iterator I, |
| 203 | MachineBasicBlock::const_iterator End) { |
| 204 | // Cast the return value to nonconst MachineInstr, then cast to an |
| 205 | // instr_iterator, which does not check for null, finally return a |
| 206 | // bundle_iterator. |
| 207 | return MachineBasicBlock::instr_iterator( |
| 208 | const_cast<MachineInstr*>( |
| 209 | &*nextIfDebug(MachineBasicBlock::const_iterator(I), End))); |
| 210 | } |
| 211 | |
Andrew Trick | dc4c1ad | 2013-09-24 17:11:19 +0000 | [diff] [blame] | 212 | /// Instantiate a ScheduleDAGInstrs that will be owned by the caller. |
Andrew Trick | 978674b | 2013-09-20 05:14:41 +0000 | [diff] [blame] | 213 | ScheduleDAGInstrs *MachineScheduler::createMachineScheduler() { |
| 214 | // Select the scheduler, or set the default. |
| 215 | MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt; |
| 216 | if (Ctor != useDefaultMachineSched) |
| 217 | return Ctor(this); |
| 218 | |
| 219 | // Get the default scheduler set by the target for this function. |
| 220 | ScheduleDAGInstrs *Scheduler = PassConfig->createMachineScheduler(this); |
| 221 | if (Scheduler) |
| 222 | return Scheduler; |
| 223 | |
| 224 | // Default to GenericScheduler. |
| 225 | return createGenericSched(this); |
| 226 | } |
| 227 | |
Andrew Trick | 72515be | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 228 | /// Top-level MachineScheduler pass driver. |
| 229 | /// |
| 230 | /// Visit blocks in function order. Divide each block into scheduling regions |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 231 | /// and visit them bottom-up. Visiting regions bottom-up is not required, but is |
| 232 | /// consistent with the DAG builder, which traverses the interior of the |
| 233 | /// scheduling regions bottom-up. |
Andrew Trick | 72515be | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 234 | /// |
| 235 | /// This design avoids exposing scheduling boundaries to the DAG builder, |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 236 | /// simplifying the DAG builder's support for "special" target instructions. |
| 237 | /// At the same time the design allows target schedulers to operate across |
Andrew Trick | 72515be | 2012-03-14 04:00:38 +0000 | [diff] [blame] | 238 | /// scheduling boundaries, for example to bundle the boudary instructions |
| 239 | /// without reordering them. This creates complexity, because the target |
| 240 | /// scheduler must update the RegionBegin and RegionEnd positions cached by |
| 241 | /// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler |
| 242 | /// design would be to split blocks at scheduling boundaries, but LLVM has a |
| 243 | /// general bias against block splitting purely for implementation simplicity. |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 244 | bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) { |
Andrew Trick | c5d7008 | 2012-05-10 21:06:21 +0000 | [diff] [blame] | 245 | DEBUG(dbgs() << "Before MISsched:\n"; mf.print(dbgs())); |
| 246 | |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 247 | // Initialize the context of the pass. |
| 248 | MF = &mf; |
| 249 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 250 | MDT = &getAnalysis<MachineDominatorTree>(); |
Andrew Trick | 4530068 | 2012-03-09 00:52:20 +0000 | [diff] [blame] | 251 | PassConfig = &getAnalysis<TargetPassConfig>(); |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 252 | AA = &getAnalysis<AliasAnalysis>(); |
| 253 | |
Lang Hames | ad33d5a | 2012-01-27 22:36:19 +0000 | [diff] [blame] | 254 | LIS = &getAnalysis<LiveIntervals>(); |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 255 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 256 | |
Andrew Trick | 48f2a72 | 2013-03-08 05:40:34 +0000 | [diff] [blame] | 257 | if (VerifyScheduling) { |
Andrew Trick | 9706496 | 2013-07-25 07:26:26 +0000 | [diff] [blame] | 258 | DEBUG(LIS->dump()); |
Andrew Trick | 48f2a72 | 2013-03-08 05:40:34 +0000 | [diff] [blame] | 259 | MF->verify(this, "Before machine scheduling."); |
| 260 | } |
Andrew Trick | 4d4b546 | 2012-04-24 20:36:19 +0000 | [diff] [blame] | 261 | RegClassInfo->runOnMachineFunction(*MF); |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 262 | |
Andrew Trick | 978674b | 2013-09-20 05:14:41 +0000 | [diff] [blame] | 263 | // Instantiate the selected scheduler for this target, function, and |
| 264 | // optimization level. |
| 265 | OwningPtr<ScheduleDAGInstrs> Scheduler(createMachineScheduler()); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 266 | |
| 267 | // Visit all machine basic blocks. |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 268 | // |
| 269 | // TODO: Visit blocks in global postorder or postorder within the bottom-up |
| 270 | // loop tree. Then we can optionally compute global RegPressure. |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 271 | for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end(); |
| 272 | MBB != MBBEnd; ++MBB) { |
| 273 | |
Andrew Trick | edfe2ec | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 274 | Scheduler->startBlock(MBB); |
| 275 | |
Andrew Trick | 7e120f4 | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 276 | // Break the block into scheduling regions [I, RegionEnd), and schedule each |
Sylvestre Ledru | 35521e2 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 277 | // region as soon as it is discovered. RegionEnd points the scheduling |
Andrew Trick | af1bee7 | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 278 | // boundary at the bottom of the region. The DAG does not include RegionEnd, |
| 279 | // but the region does (i.e. the next RegionEnd is above the previous |
| 280 | // RegionBegin). If the current block has no terminator then RegionEnd == |
| 281 | // MBB->end() for the bottom region. |
| 282 | // |
| 283 | // The Scheduler may insert instructions during either schedule() or |
| 284 | // exitRegion(), even for empty regions. So the local iterators 'I' and |
| 285 | // 'RegionEnd' are invalid across these calls. |
Andrew Trick | 4d1fa71 | 2012-11-06 07:10:34 +0000 | [diff] [blame] | 286 | unsigned RemainingInstrs = MBB->size(); |
Andrew Trick | a21daf7 | 2012-03-09 03:46:39 +0000 | [diff] [blame] | 287 | for(MachineBasicBlock::iterator RegionEnd = MBB->end(); |
Andrew Trick | af1bee7 | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 288 | RegionEnd != MBB->begin(); RegionEnd = Scheduler->begin()) { |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 289 | |
Andrew Trick | edfe2ec | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 290 | // Avoid decrementing RegionEnd for blocks with no terminator. |
| 291 | if (RegionEnd != MBB->end() |
| 292 | || TII->isSchedulingBoundary(llvm::prior(RegionEnd), MBB, *MF)) { |
| 293 | --RegionEnd; |
| 294 | // Count the boundary instruction. |
Andrew Trick | 4d1fa71 | 2012-11-06 07:10:34 +0000 | [diff] [blame] | 295 | --RemainingInstrs; |
Andrew Trick | edfe2ec | 2012-03-09 08:02:51 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Andrew Trick | 7e120f4 | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 298 | // The next region starts above the previous region. Look backward in the |
| 299 | // instruction stream until we find the nearest boundary. |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 300 | unsigned NumRegionInstrs = 0; |
Andrew Trick | 7e120f4 | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 301 | MachineBasicBlock::iterator I = RegionEnd; |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 302 | for(;I != MBB->begin(); --I, --RemainingInstrs, ++NumRegionInstrs) { |
Andrew Trick | 7e120f4 | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 303 | if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF)) |
| 304 | break; |
| 305 | } |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 306 | // Notify the scheduler of the region, even if we may skip scheduling |
| 307 | // it. Perhaps it still needs to be bundled. |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 308 | Scheduler->enterRegion(MBB, I, RegionEnd, NumRegionInstrs); |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 309 | |
| 310 | // Skip empty scheduling regions (0 or 1 schedulable instructions). |
| 311 | if (I == RegionEnd || I == llvm::prior(RegionEnd)) { |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 312 | // Close the current region. Bundle the terminator if needed. |
Andrew Trick | af1bee7 | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 313 | // This invalidates 'RegionEnd' and 'I'. |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 314 | Scheduler->exitRegion(); |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 315 | continue; |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 316 | } |
Andrew Trick | 79d3eec | 2012-05-24 22:11:14 +0000 | [diff] [blame] | 317 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 318 | DEBUG(dbgs() << MF->getName() |
Andrew Trick | 54b2ce3 | 2013-01-25 07:45:31 +0000 | [diff] [blame] | 319 | << ":BB#" << MBB->getNumber() << " " << MBB->getName() |
| 320 | << "\n From: " << *I << " To: "; |
Andrew Trick | e57583a | 2012-02-08 02:17:21 +0000 | [diff] [blame] | 321 | if (RegionEnd != MBB->end()) dbgs() << *RegionEnd; |
| 322 | else dbgs() << "End"; |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 323 | dbgs() << " RegionInstrs: " << NumRegionInstrs |
| 324 | << " Remaining: " << RemainingInstrs << "\n"); |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 325 | |
Andrew Trick | 1c0ec45 | 2012-03-09 03:46:42 +0000 | [diff] [blame] | 326 | // Schedule a region: possibly reorder instructions. |
Andrew Trick | af1bee7 | 2012-03-09 22:34:56 +0000 | [diff] [blame] | 327 | // This invalidates 'RegionEnd' and 'I'. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 328 | Scheduler->schedule(); |
Andrew Trick | 1c0ec45 | 2012-03-09 03:46:42 +0000 | [diff] [blame] | 329 | |
| 330 | // Close the current region. |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 331 | Scheduler->exitRegion(); |
| 332 | |
| 333 | // Scheduling has invalidated the current iterator 'I'. Ask the |
| 334 | // scheduler for the top of it's scheduled region. |
| 335 | RegionEnd = Scheduler->begin(); |
Andrew Trick | 7e120f4 | 2012-01-14 02:17:09 +0000 | [diff] [blame] | 336 | } |
Andrew Trick | 4d1fa71 | 2012-11-06 07:10:34 +0000 | [diff] [blame] | 337 | assert(RemainingInstrs == 0 && "Instruction count mismatch!"); |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 338 | Scheduler->finishBlock(); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 339 | } |
Andrew Trick | 779b32a | 2012-04-01 07:24:23 +0000 | [diff] [blame] | 340 | Scheduler->finalizeSchedule(); |
Andrew Trick | 9706496 | 2013-07-25 07:26:26 +0000 | [diff] [blame] | 341 | DEBUG(LIS->dump()); |
Andrew Trick | 48f2a72 | 2013-03-08 05:40:34 +0000 | [diff] [blame] | 342 | if (VerifyScheduling) |
| 343 | MF->verify(this, "After machine scheduling."); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 347 | void MachineScheduler::print(raw_ostream &O, const Module* m) const { |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 348 | // unimplemented |
| 349 | } |
| 350 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 351 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 352 | void ReadyQueue::dump() { |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 353 | dbgs() << Name << ": "; |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 354 | for (unsigned i = 0, e = Queue.size(); i < e; ++i) |
| 355 | dbgs() << Queue[i]->NodeNum << " "; |
| 356 | dbgs() << "\n"; |
| 357 | } |
| 358 | #endif |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 359 | |
| 360 | //===----------------------------------------------------------------------===// |
| 361 | // ScheduleDAGMI - Base class for MachineInstr scheduling with LiveIntervals |
| 362 | // preservation. |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 365 | ScheduleDAGMI::~ScheduleDAGMI() { |
| 366 | delete DFSResult; |
| 367 | DeleteContainerPointers(Mutations); |
| 368 | delete SchedImpl; |
| 369 | } |
| 370 | |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 371 | bool ScheduleDAGMI::canAddEdge(SUnit *SuccSU, SUnit *PredSU) { |
| 372 | return SuccSU == &ExitSU || !Topo.IsReachable(PredSU, SuccSU); |
| 373 | } |
| 374 | |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 375 | bool ScheduleDAGMI::addEdge(SUnit *SuccSU, const SDep &PredDep) { |
Andrew Trick | 26328024 | 2012-11-12 19:52:20 +0000 | [diff] [blame] | 376 | if (SuccSU != &ExitSU) { |
| 377 | // Do not use WillCreateCycle, it assumes SD scheduling. |
| 378 | // If Pred is reachable from Succ, then the edge creates a cycle. |
| 379 | if (Topo.IsReachable(PredDep.getSUnit(), SuccSU)) |
| 380 | return false; |
| 381 | Topo.AddPred(SuccSU, PredDep.getSUnit()); |
| 382 | } |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 383 | SuccSU->addPred(PredDep, /*Required=*/!PredDep.isArtificial()); |
| 384 | // Return true regardless of whether a new edge needed to be inserted. |
| 385 | return true; |
| 386 | } |
| 387 | |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 388 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When |
| 389 | /// NumPredsLeft reaches zero, release the successor node. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 390 | /// |
| 391 | /// FIXME: Adjust SuccSU height based on MinLatency. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 392 | void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) { |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 393 | SUnit *SuccSU = SuccEdge->getSUnit(); |
| 394 | |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 395 | if (SuccEdge->isWeak()) { |
| 396 | --SuccSU->WeakPredsLeft; |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 397 | if (SuccEdge->isCluster()) |
| 398 | NextClusterSucc = SuccSU; |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 399 | return; |
| 400 | } |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 401 | #ifndef NDEBUG |
| 402 | if (SuccSU->NumPredsLeft == 0) { |
| 403 | dbgs() << "*** Scheduling failed! ***\n"; |
| 404 | SuccSU->dump(this); |
| 405 | dbgs() << " has been released too many times!\n"; |
| 406 | llvm_unreachable(0); |
| 407 | } |
| 408 | #endif |
| 409 | --SuccSU->NumPredsLeft; |
| 410 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 411 | SchedImpl->releaseTopNode(SuccSU); |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /// releaseSuccessors - Call releaseSucc on each of SU's successors. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 415 | void ScheduleDAGMI::releaseSuccessors(SUnit *SU) { |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 416 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 417 | I != E; ++I) { |
| 418 | releaseSucc(SU, &*I); |
| 419 | } |
| 420 | } |
| 421 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 422 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When |
| 423 | /// NumSuccsLeft reaches zero, release the predecessor node. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 424 | /// |
| 425 | /// FIXME: Adjust PredSU height based on MinLatency. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 426 | void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) { |
| 427 | SUnit *PredSU = PredEdge->getSUnit(); |
| 428 | |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 429 | if (PredEdge->isWeak()) { |
| 430 | --PredSU->WeakSuccsLeft; |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 431 | if (PredEdge->isCluster()) |
| 432 | NextClusterPred = PredSU; |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 435 | #ifndef NDEBUG |
| 436 | if (PredSU->NumSuccsLeft == 0) { |
| 437 | dbgs() << "*** Scheduling failed! ***\n"; |
| 438 | PredSU->dump(this); |
| 439 | dbgs() << " has been released too many times!\n"; |
| 440 | llvm_unreachable(0); |
| 441 | } |
| 442 | #endif |
| 443 | --PredSU->NumSuccsLeft; |
| 444 | if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) |
| 445 | SchedImpl->releaseBottomNode(PredSU); |
| 446 | } |
| 447 | |
| 448 | /// releasePredecessors - Call releasePred on each of SU's predecessors. |
| 449 | void ScheduleDAGMI::releasePredecessors(SUnit *SU) { |
| 450 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 451 | I != E; ++I) { |
| 452 | releasePred(SU, &*I); |
| 453 | } |
| 454 | } |
| 455 | |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 456 | /// This is normally called from the main scheduler loop but may also be invoked |
| 457 | /// by the scheduling strategy to perform additional code motion. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 458 | void ScheduleDAGMI::moveInstruction(MachineInstr *MI, |
| 459 | MachineBasicBlock::iterator InsertPos) { |
Andrew Trick | 463b2f1 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 460 | // Advance RegionBegin if the first instruction moves down. |
Andrew Trick | 54f7def | 2012-03-21 04:12:10 +0000 | [diff] [blame] | 461 | if (&*RegionBegin == MI) |
Andrew Trick | 463b2f1 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 462 | ++RegionBegin; |
| 463 | |
| 464 | // Update the instruction stream. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 465 | BB->splice(InsertPos, BB, MI); |
Andrew Trick | 463b2f1 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 466 | |
| 467 | // Update LiveIntervals |
Andrew Trick | d9d4be0 | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 468 | LIS->handleMove(MI, /*UpdateFlags=*/true); |
Andrew Trick | 463b2f1 | 2012-05-17 18:35:03 +0000 | [diff] [blame] | 469 | |
| 470 | // Recede RegionBegin if an instruction moves above the first. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 471 | if (RegionBegin == InsertPos) |
| 472 | RegionBegin = MI; |
| 473 | } |
| 474 | |
Andrew Trick | de670c0 | 2012-03-21 04:12:07 +0000 | [diff] [blame] | 475 | bool ScheduleDAGMI::checkSchedLimit() { |
| 476 | #ifndef NDEBUG |
| 477 | if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) { |
| 478 | CurrentTop = CurrentBottom; |
| 479 | return false; |
| 480 | } |
| 481 | ++NumInstrsScheduled; |
| 482 | #endif |
| 483 | return true; |
| 484 | } |
| 485 | |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 486 | /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after |
| 487 | /// crossing a scheduling boundary. [begin, end) includes all instructions in |
| 488 | /// the region, including the boundary itself and single-instruction regions |
| 489 | /// that don't get scheduled. |
| 490 | void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb, |
| 491 | MachineBasicBlock::iterator begin, |
| 492 | MachineBasicBlock::iterator end, |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 493 | unsigned regioninstrs) |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 494 | { |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 495 | ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs); |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 496 | |
| 497 | // For convenience remember the end of the liveness region. |
| 498 | LiveRegionEnd = |
| 499 | (RegionEnd == bb->end()) ? RegionEnd : llvm::next(RegionEnd); |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 500 | |
Andrew Trick | b248b4a | 2013-09-06 17:32:47 +0000 | [diff] [blame] | 501 | SUPressureDiffs.clear(); |
| 502 | |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 503 | SchedImpl->initPolicy(begin, end, regioninstrs); |
| 504 | |
| 505 | ShouldTrackPressure = SchedImpl->shouldTrackPressure(); |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Setup the register pressure trackers for the top scheduled top and bottom |
| 509 | // scheduled regions. |
| 510 | void ScheduleDAGMI::initRegPressure() { |
| 511 | TopRPTracker.init(&MF, RegClassInfo, LIS, BB, RegionBegin); |
| 512 | BotRPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd); |
| 513 | |
| 514 | // Close the RPTracker to finalize live ins. |
| 515 | RPTracker.closeRegion(); |
| 516 | |
Andrew Trick | 9c17eab | 2013-07-30 19:59:12 +0000 | [diff] [blame] | 517 | DEBUG(RPTracker.dump()); |
Andrew Trick | 79d3eec | 2012-05-24 22:11:14 +0000 | [diff] [blame] | 518 | |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 519 | // Initialize the live ins and live outs. |
| 520 | TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs); |
| 521 | BotRPTracker.addLiveRegs(RPTracker.getPressure().LiveOutRegs); |
| 522 | |
| 523 | // Close one end of the tracker so we can call |
| 524 | // getMaxUpward/DownwardPressureDelta before advancing across any |
| 525 | // instructions. This converts currently live regs into live ins/outs. |
| 526 | TopRPTracker.closeTop(); |
| 527 | BotRPTracker.closeBottom(); |
| 528 | |
Andrew Trick | 9c17eab | 2013-07-30 19:59:12 +0000 | [diff] [blame] | 529 | BotRPTracker.initLiveThru(RPTracker); |
| 530 | if (!BotRPTracker.getLiveThru().empty()) { |
| 531 | TopRPTracker.initLiveThru(BotRPTracker.getLiveThru()); |
| 532 | DEBUG(dbgs() << "Live Thru: "; |
| 533 | dumpRegSetPressure(BotRPTracker.getLiveThru(), TRI)); |
| 534 | }; |
| 535 | |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 536 | // For each live out vreg reduce the pressure change associated with other |
| 537 | // uses of the same vreg below the live-out reaching def. |
| 538 | updatePressureDiffs(RPTracker.getPressure().LiveOutRegs); |
| 539 | |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 540 | // Account for liveness generated by the region boundary. |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 541 | if (LiveRegionEnd != RegionEnd) { |
| 542 | SmallVector<unsigned, 8> LiveUses; |
| 543 | BotRPTracker.recede(&LiveUses); |
| 544 | updatePressureDiffs(LiveUses); |
| 545 | } |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 546 | |
| 547 | assert(BotRPTracker.getPos() == RegionEnd && "Can't find the region bottom"); |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 548 | |
| 549 | // Cache the list of excess pressure sets in this region. This will also track |
| 550 | // the max pressure in the scheduled code for these sets. |
| 551 | RegionCriticalPSets.clear(); |
Jakub Staszak | c641ada | 2013-01-25 21:44:27 +0000 | [diff] [blame] | 552 | const std::vector<unsigned> &RegionPressure = |
| 553 | RPTracker.getPressure().MaxSetPressure; |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 554 | for (unsigned i = 0, e = RegionPressure.size(); i < e; ++i) { |
Andrew Trick | 736dd9a | 2013-06-21 18:32:58 +0000 | [diff] [blame] | 555 | unsigned Limit = RegClassInfo->getRegPressureSetLimit(i); |
Andrew Trick | b55db58 | 2013-06-21 18:33:01 +0000 | [diff] [blame] | 556 | if (RegionPressure[i] > Limit) { |
| 557 | DEBUG(dbgs() << TRI->getRegPressureSetName(i) |
| 558 | << " Limit " << Limit |
| 559 | << " Actual " << RegionPressure[i] << "\n"); |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 560 | RegionCriticalPSets.push_back(PressureChange(i)); |
Andrew Trick | b55db58 | 2013-06-21 18:33:01 +0000 | [diff] [blame] | 561 | } |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 562 | } |
| 563 | DEBUG(dbgs() << "Excess PSets: "; |
| 564 | for (unsigned i = 0, e = RegionCriticalPSets.size(); i != e; ++i) |
| 565 | dbgs() << TRI->getRegPressureSetName( |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 566 | RegionCriticalPSets[i].getPSet()) << " "; |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 567 | dbgs() << "\n"); |
| 568 | } |
| 569 | |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 570 | void ScheduleDAGMI:: |
Andrew Trick | b248b4a | 2013-09-06 17:32:47 +0000 | [diff] [blame] | 571 | updateScheduledPressure(const SUnit *SU, |
| 572 | const std::vector<unsigned> &NewMaxPressure) { |
| 573 | const PressureDiff &PDiff = getPressureDiff(SU); |
| 574 | unsigned CritIdx = 0, CritEnd = RegionCriticalPSets.size(); |
| 575 | for (PressureDiff::const_iterator I = PDiff.begin(), E = PDiff.end(); |
| 576 | I != E; ++I) { |
| 577 | if (!I->isValid()) |
| 578 | break; |
| 579 | unsigned ID = I->getPSet(); |
| 580 | while (CritIdx != CritEnd && RegionCriticalPSets[CritIdx].getPSet() < ID) |
| 581 | ++CritIdx; |
| 582 | if (CritIdx != CritEnd && RegionCriticalPSets[CritIdx].getPSet() == ID) { |
| 583 | if ((int)NewMaxPressure[ID] > RegionCriticalPSets[CritIdx].getUnitInc() |
| 584 | && NewMaxPressure[ID] <= INT16_MAX) |
| 585 | RegionCriticalPSets[CritIdx].setUnitInc(NewMaxPressure[ID]); |
| 586 | } |
| 587 | unsigned Limit = RegClassInfo->getRegPressureSetLimit(ID); |
| 588 | if (NewMaxPressure[ID] >= Limit - 2) { |
| 589 | DEBUG(dbgs() << " " << TRI->getRegPressureSetName(ID) << ": " |
| 590 | << NewMaxPressure[ID] << " > " << Limit << "(+ " |
| 591 | << BotRPTracker.getLiveThru()[ID] << " livethru)\n"); |
| 592 | } |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 593 | } |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 596 | /// Update the PressureDiff array for liveness after scheduling this |
| 597 | /// instruction. |
| 598 | void ScheduleDAGMI::updatePressureDiffs(ArrayRef<unsigned> LiveUses) { |
| 599 | for (unsigned LUIdx = 0, LUEnd = LiveUses.size(); LUIdx != LUEnd; ++LUIdx) { |
| 600 | /// FIXME: Currently assuming single-use physregs. |
| 601 | unsigned Reg = LiveUses[LUIdx]; |
Andrew Trick | ffdbefb | 2013-09-06 17:32:39 +0000 | [diff] [blame] | 602 | DEBUG(dbgs() << " LiveReg: " << PrintVRegOrUnit(Reg, TRI) << "\n"); |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 603 | if (!TRI->isVirtualRegister(Reg)) |
| 604 | continue; |
Andrew Trick | ffdbefb | 2013-09-06 17:32:39 +0000 | [diff] [blame] | 605 | |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 606 | // This may be called before CurrentBottom has been initialized. However, |
| 607 | // BotRPTracker must have a valid position. We want the value live into the |
| 608 | // instruction or live out of the block, so ask for the previous |
| 609 | // instruction's live-out. |
| 610 | const LiveInterval &LI = LIS->getInterval(Reg); |
| 611 | VNInfo *VNI; |
Andrew Trick | 2c4f8b7 | 2013-08-31 05:17:58 +0000 | [diff] [blame] | 612 | MachineBasicBlock::const_iterator I = |
| 613 | nextIfDebug(BotRPTracker.getPos(), BB->end()); |
| 614 | if (I == BB->end()) |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 615 | VNI = LI.getVNInfoBefore(LIS->getMBBEndIdx(BB)); |
| 616 | else { |
Matthias Braun | 88dd0ab | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 617 | LiveQueryResult LRQ = LI.Query(LIS->getInstructionIndex(I)); |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 618 | VNI = LRQ.valueIn(); |
| 619 | } |
| 620 | // RegisterPressureTracker guarantees that readsReg is true for LiveUses. |
| 621 | assert(VNI && "No live value at use."); |
| 622 | for (VReg2UseMap::iterator |
| 623 | UI = VRegUses.find(Reg); UI != VRegUses.end(); ++UI) { |
| 624 | SUnit *SU = UI->SU; |
Andrew Trick | ffdbefb | 2013-09-06 17:32:39 +0000 | [diff] [blame] | 625 | DEBUG(dbgs() << " UpdateRegP: SU(" << SU->NodeNum << ") " |
| 626 | << *SU->getInstr()); |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 627 | // If this use comes before the reaching def, it cannot be a last use, so |
| 628 | // descrease its pressure change. |
| 629 | if (!SU->isScheduled && SU != &ExitSU) { |
Matthias Braun | 88dd0ab | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 630 | LiveQueryResult LRQ |
| 631 | = LI.Query(LIS->getInstructionIndex(SU->getInstr())); |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 632 | if (LRQ.valueIn() == VNI) |
| 633 | getPressureDiff(SU).addPressureChange(Reg, true, &MRI); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 639 | /// schedule - Called back from MachineScheduler::runOnMachineFunction |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 640 | /// after setting up the current scheduling region. [RegionBegin, RegionEnd) |
| 641 | /// only includes instructions that have DAG nodes, not scheduling boundaries. |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 642 | /// |
| 643 | /// This is a skeletal driver, with all the functionality pushed into helpers, |
| 644 | /// so that it can be easilly extended by experimental schedulers. Generally, |
| 645 | /// implementing MachineSchedStrategy should be sufficient to implement a new |
| 646 | /// scheduling algorithm. However, if a scheduler further subclasses |
| 647 | /// ScheduleDAGMI then it will want to override this virtual method in order to |
| 648 | /// update any specialized state. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 649 | void ScheduleDAGMI::schedule() { |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 650 | buildDAGWithRegPressure(); |
| 651 | |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 652 | Topo.InitDAGTopologicalSorting(); |
| 653 | |
Andrew Trick | a2733e9 | 2012-09-14 17:22:42 +0000 | [diff] [blame] | 654 | postprocessDAG(); |
| 655 | |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 656 | SmallVector<SUnit*, 8> TopRoots, BotRoots; |
| 657 | findRootsAndBiasEdges(TopRoots, BotRoots); |
| 658 | |
| 659 | // Initialize the strategy before modifying the DAG. |
| 660 | // This may initialize a DFSResult to be used for queue priority. |
| 661 | SchedImpl->initialize(this); |
| 662 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 663 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 664 | SUnits[su].dumpAll(this)); |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 665 | if (ViewMISchedDAGs) viewGraph(); |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 666 | |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 667 | // Initialize ready queues now that the DAG and priority data are finalized. |
| 668 | initQueues(TopRoots, BotRoots); |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 669 | |
| 670 | bool IsTopNode = false; |
| 671 | while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) { |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 672 | assert(!SU->isScheduled && "Node already scheduled"); |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 673 | if (!checkSchedLimit()) |
| 674 | break; |
| 675 | |
| 676 | scheduleMI(SU, IsTopNode); |
| 677 | |
| 678 | updateQueues(SU, IsTopNode); |
| 679 | } |
| 680 | assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); |
| 681 | |
| 682 | placeDebugValues(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 683 | |
| 684 | DEBUG({ |
Andrew Trick | cf7e697 | 2012-11-28 03:42:47 +0000 | [diff] [blame] | 685 | unsigned BBNum = begin()->getParent()->getNumber(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 686 | dbgs() << "*** Final schedule for BB#" << BBNum << " ***\n"; |
| 687 | dumpSchedule(); |
| 688 | dbgs() << '\n'; |
| 689 | }); |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /// Build the DAG and setup three register pressure trackers. |
| 693 | void ScheduleDAGMI::buildDAGWithRegPressure() { |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 694 | if (!ShouldTrackPressure) { |
| 695 | RPTracker.reset(); |
| 696 | RegionCriticalPSets.clear(); |
| 697 | buildSchedGraph(AA); |
| 698 | return; |
| 699 | } |
| 700 | |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 701 | // Initialize the register pressure tracker used by buildSchedGraph. |
Andrew Trick | 9c17eab | 2013-07-30 19:59:12 +0000 | [diff] [blame] | 702 | RPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd, |
| 703 | /*TrackUntiedDefs=*/true); |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 704 | |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 705 | // Account for liveness generate by the region boundary. |
| 706 | if (LiveRegionEnd != RegionEnd) |
| 707 | RPTracker.recede(); |
| 708 | |
| 709 | // Build the DAG, and compute current register pressure. |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 710 | buildSchedGraph(AA, &RPTracker, &SUPressureDiffs); |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 711 | |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 712 | // Initialize top/bottom trackers after computing region pressure. |
| 713 | initRegPressure(); |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 714 | } |
Andrew Trick | 4add42f | 2012-05-10 21:06:10 +0000 | [diff] [blame] | 715 | |
Andrew Trick | a2733e9 | 2012-09-14 17:22:42 +0000 | [diff] [blame] | 716 | /// Apply each ScheduleDAGMutation step in order. |
| 717 | void ScheduleDAGMI::postprocessDAG() { |
| 718 | for (unsigned i = 0, e = Mutations.size(); i < e; ++i) { |
| 719 | Mutations[i]->apply(this); |
| 720 | } |
| 721 | } |
| 722 | |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 723 | void ScheduleDAGMI::computeDFSResult() { |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 724 | if (!DFSResult) |
| 725 | DFSResult = new SchedDFSResult(/*BottomU*/true, MinSubtreeSize); |
| 726 | DFSResult->clear(); |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 727 | ScheduledTrees.clear(); |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 728 | DFSResult->resize(SUnits.size()); |
| 729 | DFSResult->compute(SUnits); |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 730 | ScheduledTrees.resize(DFSResult->getNumSubtrees()); |
| 731 | } |
| 732 | |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 733 | void ScheduleDAGMI::findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots, |
| 734 | SmallVectorImpl<SUnit*> &BotRoots) { |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 735 | for (std::vector<SUnit>::iterator |
| 736 | I = SUnits.begin(), E = SUnits.end(); I != E; ++I) { |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 737 | SUnit *SU = &(*I); |
Andrew Trick | 399c9bf | 2013-01-29 06:26:35 +0000 | [diff] [blame] | 738 | assert(!SU->isBoundaryNode() && "Boundary node should not be in SUnits"); |
Andrew Trick | 92da424 | 2013-01-24 02:09:57 +0000 | [diff] [blame] | 739 | |
| 740 | // Order predecessors so DFSResult follows the critical path. |
| 741 | SU->biasCriticalPath(); |
| 742 | |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 743 | // A SUnit is ready to top schedule if it has no predecessors. |
Andrew Trick | 399c9bf | 2013-01-29 06:26:35 +0000 | [diff] [blame] | 744 | if (!I->NumPredsLeft) |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 745 | TopRoots.push_back(SU); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 746 | // A SUnit is ready to bottom schedule if it has no successors. |
Andrew Trick | 399c9bf | 2013-01-29 06:26:35 +0000 | [diff] [blame] | 747 | if (!I->NumSuccsLeft) |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 748 | BotRoots.push_back(SU); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 749 | } |
Andrew Trick | 399c9bf | 2013-01-29 06:26:35 +0000 | [diff] [blame] | 750 | ExitSU.biasCriticalPath(); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 753 | /// Compute the max cyclic critical path through the DAG. The scheduling DAG |
| 754 | /// only provides the critical path for single block loops. To handle loops that |
| 755 | /// span blocks, we could use the vreg path latencies provided by |
| 756 | /// MachineTraceMetrics instead. However, MachineTraceMetrics is not currently |
| 757 | /// available for use in the scheduler. |
| 758 | /// |
| 759 | /// The cyclic path estimation identifies a def-use pair that crosses the back |
Andrew Trick | ef80f50 | 2013-08-30 02:02:12 +0000 | [diff] [blame] | 760 | /// edge and considers the depth and height of the nodes. For example, consider |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 761 | /// the following instruction sequence where each instruction has unit latency |
| 762 | /// and defines an epomymous virtual register: |
| 763 | /// |
| 764 | /// a->b(a,c)->c(b)->d(c)->exit |
| 765 | /// |
| 766 | /// The cyclic critical path is a two cycles: b->c->b |
| 767 | /// The acyclic critical path is four cycles: a->b->c->d->exit |
| 768 | /// LiveOutHeight = height(c) = len(c->d->exit) = 2 |
| 769 | /// LiveOutDepth = depth(c) + 1 = len(a->b->c) + 1 = 3 |
| 770 | /// LiveInHeight = height(b) + 1 = len(b->c->d->exit) + 1 = 4 |
| 771 | /// LiveInDepth = depth(b) = len(a->b) = 1 |
| 772 | /// |
| 773 | /// LiveOutDepth - LiveInDepth = 3 - 1 = 2 |
| 774 | /// LiveInHeight - LiveOutHeight = 4 - 2 = 2 |
| 775 | /// CyclicCriticalPath = min(2, 2) = 2 |
| 776 | unsigned ScheduleDAGMI::computeCyclicCriticalPath() { |
| 777 | // This only applies to single block loop. |
| 778 | if (!BB->isSuccessor(BB)) |
| 779 | return 0; |
| 780 | |
| 781 | unsigned MaxCyclicLatency = 0; |
| 782 | // Visit each live out vreg def to find def/use pairs that cross iterations. |
| 783 | ArrayRef<unsigned> LiveOuts = RPTracker.getPressure().LiveOutRegs; |
| 784 | for (ArrayRef<unsigned>::iterator RI = LiveOuts.begin(), RE = LiveOuts.end(); |
| 785 | RI != RE; ++RI) { |
| 786 | unsigned Reg = *RI; |
| 787 | if (!TRI->isVirtualRegister(Reg)) |
| 788 | continue; |
| 789 | const LiveInterval &LI = LIS->getInterval(Reg); |
| 790 | const VNInfo *DefVNI = LI.getVNInfoBefore(LIS->getMBBEndIdx(BB)); |
| 791 | if (!DefVNI) |
| 792 | continue; |
| 793 | |
| 794 | MachineInstr *DefMI = LIS->getInstructionFromIndex(DefVNI->def); |
| 795 | const SUnit *DefSU = getSUnit(DefMI); |
| 796 | if (!DefSU) |
| 797 | continue; |
| 798 | |
| 799 | unsigned LiveOutHeight = DefSU->getHeight(); |
| 800 | unsigned LiveOutDepth = DefSU->getDepth() + DefSU->Latency; |
| 801 | // Visit all local users of the vreg def. |
| 802 | for (VReg2UseMap::iterator |
| 803 | UI = VRegUses.find(Reg); UI != VRegUses.end(); ++UI) { |
| 804 | if (UI->SU == &ExitSU) |
| 805 | continue; |
| 806 | |
| 807 | // Only consider uses of the phi. |
Matthias Braun | 88dd0ab | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 808 | LiveQueryResult LRQ = |
| 809 | LI.Query(LIS->getInstructionIndex(UI->SU->getInstr())); |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 810 | if (!LRQ.valueIn()->isPHIDef()) |
| 811 | continue; |
| 812 | |
| 813 | // Assume that a path spanning two iterations is a cycle, which could |
| 814 | // overestimate in strange cases. This allows cyclic latency to be |
| 815 | // estimated as the minimum slack of the vreg's depth or height. |
| 816 | unsigned CyclicLatency = 0; |
| 817 | if (LiveOutDepth > UI->SU->getDepth()) |
| 818 | CyclicLatency = LiveOutDepth - UI->SU->getDepth(); |
| 819 | |
| 820 | unsigned LiveInHeight = UI->SU->getHeight() + DefSU->Latency; |
| 821 | if (LiveInHeight > LiveOutHeight) { |
| 822 | if (LiveInHeight - LiveOutHeight < CyclicLatency) |
| 823 | CyclicLatency = LiveInHeight - LiveOutHeight; |
| 824 | } |
| 825 | else |
| 826 | CyclicLatency = 0; |
| 827 | |
| 828 | DEBUG(dbgs() << "Cyclic Path: SU(" << DefSU->NodeNum << ") -> SU(" |
| 829 | << UI->SU->NodeNum << ") = " << CyclicLatency << "c\n"); |
| 830 | if (CyclicLatency > MaxCyclicLatency) |
| 831 | MaxCyclicLatency = CyclicLatency; |
| 832 | } |
| 833 | } |
| 834 | DEBUG(dbgs() << "Cyclic Critical Path: " << MaxCyclicLatency << "c\n"); |
| 835 | return MaxCyclicLatency; |
| 836 | } |
| 837 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 838 | /// Identify DAG roots and setup scheduler queues. |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 839 | void ScheduleDAGMI::initQueues(ArrayRef<SUnit*> TopRoots, |
| 840 | ArrayRef<SUnit*> BotRoots) { |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 841 | NextClusterSucc = NULL; |
| 842 | NextClusterPred = NULL; |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 843 | |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 844 | // Release all DAG roots for scheduling, not including EntrySU/ExitSU. |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 845 | // |
| 846 | // Nodes with unreleased weak edges can still be roots. |
| 847 | // Release top roots in forward order. |
| 848 | for (SmallVectorImpl<SUnit*>::const_iterator |
| 849 | I = TopRoots.begin(), E = TopRoots.end(); I != E; ++I) { |
| 850 | SchedImpl->releaseTopNode(*I); |
| 851 | } |
| 852 | // Release bottom roots in reverse order so the higher priority nodes appear |
| 853 | // first. This is more natural and slightly more efficient. |
| 854 | for (SmallVectorImpl<SUnit*>::const_reverse_iterator |
| 855 | I = BotRoots.rbegin(), E = BotRoots.rend(); I != E; ++I) { |
| 856 | SchedImpl->releaseBottomNode(*I); |
| 857 | } |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 858 | |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 859 | releaseSuccessors(&EntrySU); |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 860 | releasePredecessors(&ExitSU); |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 861 | |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 862 | SchedImpl->registerRoots(); |
| 863 | |
Andrew Trick | b767d1e | 2012-12-01 01:22:49 +0000 | [diff] [blame] | 864 | // Advance past initial DebugValues. |
Andrew Trick | cc45a28 | 2012-04-24 18:04:34 +0000 | [diff] [blame] | 865 | CurrentTop = nextIfDebug(RegionBegin, RegionEnd); |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 866 | CurrentBottom = RegionEnd; |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 867 | |
| 868 | if (ShouldTrackPressure) { |
| 869 | assert(TopRPTracker.getPos() == RegionBegin && "bad initial Top tracker"); |
| 870 | TopRPTracker.setPos(CurrentTop); |
| 871 | } |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 872 | } |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 873 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 874 | /// Move an instruction and update register pressure. |
| 875 | void ScheduleDAGMI::scheduleMI(SUnit *SU, bool IsTopNode) { |
| 876 | // Move the instruction to its new location in the instruction stream. |
| 877 | MachineInstr *MI = SU->getInstr(); |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 878 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 879 | if (IsTopNode) { |
| 880 | assert(SU->isTopReady() && "node still has unscheduled dependencies"); |
| 881 | if (&*CurrentTop == MI) |
| 882 | CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom); |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 883 | else { |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 884 | moveInstruction(MI, CurrentTop); |
| 885 | TopRPTracker.setPos(MI); |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 886 | } |
Andrew Trick | c3ea005 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 887 | |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 888 | if (ShouldTrackPressure) { |
| 889 | // Update top scheduled pressure. |
| 890 | TopRPTracker.advance(); |
| 891 | assert(TopRPTracker.getPos() == CurrentTop && "out of sync"); |
Andrew Trick | b248b4a | 2013-09-06 17:32:47 +0000 | [diff] [blame] | 892 | updateScheduledPressure(SU, TopRPTracker.getPressure().MaxSetPressure); |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 893 | } |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 894 | } |
| 895 | else { |
| 896 | assert(SU->isBottomReady() && "node still has unscheduled dependencies"); |
| 897 | MachineBasicBlock::iterator priorII = |
| 898 | priorNonDebug(CurrentBottom, CurrentTop); |
| 899 | if (&*priorII == MI) |
| 900 | CurrentBottom = priorII; |
| 901 | else { |
| 902 | if (&*CurrentTop == MI) { |
| 903 | CurrentTop = nextIfDebug(++CurrentTop, priorII); |
| 904 | TopRPTracker.setPos(CurrentTop); |
| 905 | } |
| 906 | moveInstruction(MI, CurrentBottom); |
| 907 | CurrentBottom = MI; |
| 908 | } |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 909 | if (ShouldTrackPressure) { |
| 910 | // Update bottom scheduled pressure. |
| 911 | SmallVector<unsigned, 8> LiveUses; |
| 912 | BotRPTracker.recede(&LiveUses); |
| 913 | assert(BotRPTracker.getPos() == CurrentBottom && "out of sync"); |
Andrew Trick | b248b4a | 2013-09-06 17:32:47 +0000 | [diff] [blame] | 914 | updateScheduledPressure(SU, BotRPTracker.getPressure().MaxSetPressure); |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 915 | updatePressureDiffs(LiveUses); |
Andrew Trick | b6e7471 | 2013-09-04 20:59:59 +0000 | [diff] [blame] | 916 | } |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
| 920 | /// Update scheduler queues after scheduling an instruction. |
| 921 | void ScheduleDAGMI::updateQueues(SUnit *SU, bool IsTopNode) { |
| 922 | // Release dependent instructions for scheduling. |
| 923 | if (IsTopNode) |
| 924 | releaseSuccessors(SU); |
| 925 | else |
| 926 | releasePredecessors(SU); |
| 927 | |
| 928 | SU->isScheduled = true; |
| 929 | |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 930 | if (DFSResult) { |
| 931 | unsigned SubtreeID = DFSResult->getSubtreeID(SU); |
| 932 | if (!ScheduledTrees.test(SubtreeID)) { |
| 933 | ScheduledTrees.set(SubtreeID); |
| 934 | DFSResult->scheduleTree(SubtreeID); |
| 935 | SchedImpl->scheduleTree(SubtreeID); |
| 936 | } |
| 937 | } |
| 938 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 939 | // Notify the scheduling strategy after updating the DAG. |
| 940 | SchedImpl->schedNode(SU, IsTopNode); |
Andrew Trick | c3ea005 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /// Reinsert any remaining debug_values, just like the PostRA scheduler. |
| 944 | void ScheduleDAGMI::placeDebugValues() { |
| 945 | // If first instruction was a DBG_VALUE then put it back. |
| 946 | if (FirstDbgValue) { |
| 947 | BB->splice(RegionBegin, BB, FirstDbgValue); |
| 948 | RegionBegin = FirstDbgValue; |
| 949 | } |
| 950 | |
| 951 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 952 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 953 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 954 | MachineInstr *DbgValue = P.first; |
| 955 | MachineBasicBlock::iterator OrigPrevMI = P.second; |
Andrew Trick | e7ea8aa | 2012-12-01 01:22:38 +0000 | [diff] [blame] | 956 | if (&*RegionBegin == DbgValue) |
| 957 | ++RegionBegin; |
Andrew Trick | c3ea005 | 2012-04-24 18:04:37 +0000 | [diff] [blame] | 958 | BB->splice(++OrigPrevMI, BB, DbgValue); |
| 959 | if (OrigPrevMI == llvm::prior(RegionEnd)) |
| 960 | RegionEnd = DbgValue; |
| 961 | } |
| 962 | DbgValues.clear(); |
| 963 | FirstDbgValue = NULL; |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 966 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 967 | void ScheduleDAGMI::dumpSchedule() const { |
| 968 | for (MachineBasicBlock::iterator MI = begin(), ME = end(); MI != ME; ++MI) { |
| 969 | if (SUnit *SU = getSUnit(&(*MI))) |
| 970 | SU->dump(this); |
| 971 | else |
| 972 | dbgs() << "Missing SUnit\n"; |
| 973 | } |
| 974 | } |
| 975 | #endif |
| 976 | |
Andrew Trick | 26328024 | 2012-11-12 19:52:20 +0000 | [diff] [blame] | 977 | //===----------------------------------------------------------------------===// |
| 978 | // LoadClusterMutation - DAG post-processing to cluster loads. |
| 979 | //===----------------------------------------------------------------------===// |
| 980 | |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 981 | namespace { |
| 982 | /// \brief Post-process the DAG to create cluster edges between neighboring |
| 983 | /// loads. |
| 984 | class LoadClusterMutation : public ScheduleDAGMutation { |
| 985 | struct LoadInfo { |
| 986 | SUnit *SU; |
| 987 | unsigned BaseReg; |
| 988 | unsigned Offset; |
| 989 | LoadInfo(SUnit *su, unsigned reg, unsigned ofs) |
| 990 | : SU(su), BaseReg(reg), Offset(ofs) {} |
| 991 | }; |
| 992 | static bool LoadInfoLess(const LoadClusterMutation::LoadInfo &LHS, |
| 993 | const LoadClusterMutation::LoadInfo &RHS); |
| 994 | |
| 995 | const TargetInstrInfo *TII; |
| 996 | const TargetRegisterInfo *TRI; |
| 997 | public: |
| 998 | LoadClusterMutation(const TargetInstrInfo *tii, |
| 999 | const TargetRegisterInfo *tri) |
| 1000 | : TII(tii), TRI(tri) {} |
| 1001 | |
| 1002 | virtual void apply(ScheduleDAGMI *DAG); |
| 1003 | protected: |
| 1004 | void clusterNeighboringLoads(ArrayRef<SUnit*> Loads, ScheduleDAGMI *DAG); |
| 1005 | }; |
| 1006 | } // anonymous |
| 1007 | |
| 1008 | bool LoadClusterMutation::LoadInfoLess( |
| 1009 | const LoadClusterMutation::LoadInfo &LHS, |
| 1010 | const LoadClusterMutation::LoadInfo &RHS) { |
| 1011 | if (LHS.BaseReg != RHS.BaseReg) |
| 1012 | return LHS.BaseReg < RHS.BaseReg; |
| 1013 | return LHS.Offset < RHS.Offset; |
| 1014 | } |
| 1015 | |
| 1016 | void LoadClusterMutation::clusterNeighboringLoads(ArrayRef<SUnit*> Loads, |
| 1017 | ScheduleDAGMI *DAG) { |
| 1018 | SmallVector<LoadClusterMutation::LoadInfo,32> LoadRecords; |
| 1019 | for (unsigned Idx = 0, End = Loads.size(); Idx != End; ++Idx) { |
| 1020 | SUnit *SU = Loads[Idx]; |
| 1021 | unsigned BaseReg; |
| 1022 | unsigned Offset; |
| 1023 | if (TII->getLdStBaseRegImmOfs(SU->getInstr(), BaseReg, Offset, TRI)) |
| 1024 | LoadRecords.push_back(LoadInfo(SU, BaseReg, Offset)); |
| 1025 | } |
| 1026 | if (LoadRecords.size() < 2) |
| 1027 | return; |
| 1028 | std::sort(LoadRecords.begin(), LoadRecords.end(), LoadInfoLess); |
| 1029 | unsigned ClusterLength = 1; |
| 1030 | for (unsigned Idx = 0, End = LoadRecords.size(); Idx < (End - 1); ++Idx) { |
| 1031 | if (LoadRecords[Idx].BaseReg != LoadRecords[Idx+1].BaseReg) { |
| 1032 | ClusterLength = 1; |
| 1033 | continue; |
| 1034 | } |
| 1035 | |
| 1036 | SUnit *SUa = LoadRecords[Idx].SU; |
| 1037 | SUnit *SUb = LoadRecords[Idx+1].SU; |
Andrew Trick | ec369d5 | 2012-11-12 21:28:10 +0000 | [diff] [blame] | 1038 | if (TII->shouldClusterLoads(SUa->getInstr(), SUb->getInstr(), ClusterLength) |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 1039 | && DAG->addEdge(SUb, SDep(SUa, SDep::Cluster))) { |
| 1040 | |
| 1041 | DEBUG(dbgs() << "Cluster loads SU(" << SUa->NodeNum << ") - SU(" |
| 1042 | << SUb->NodeNum << ")\n"); |
| 1043 | // Copy successor edges from SUa to SUb. Interleaving computation |
| 1044 | // dependent on SUa can prevent load combining due to register reuse. |
| 1045 | // Predecessor edges do not need to be copied from SUb to SUa since nearby |
| 1046 | // loads should have effectively the same inputs. |
| 1047 | for (SUnit::const_succ_iterator |
| 1048 | SI = SUa->Succs.begin(), SE = SUa->Succs.end(); SI != SE; ++SI) { |
| 1049 | if (SI->getSUnit() == SUb) |
| 1050 | continue; |
| 1051 | DEBUG(dbgs() << " Copy Succ SU(" << SI->getSUnit()->NodeNum << ")\n"); |
| 1052 | DAG->addEdge(SI->getSUnit(), SDep(SUb, SDep::Artificial)); |
| 1053 | } |
| 1054 | ++ClusterLength; |
| 1055 | } |
| 1056 | else |
| 1057 | ClusterLength = 1; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /// \brief Callback from DAG postProcessing to create cluster edges for loads. |
| 1062 | void LoadClusterMutation::apply(ScheduleDAGMI *DAG) { |
| 1063 | // Map DAG NodeNum to store chain ID. |
| 1064 | DenseMap<unsigned, unsigned> StoreChainIDs; |
| 1065 | // Map each store chain to a set of dependent loads. |
| 1066 | SmallVector<SmallVector<SUnit*,4>, 32> StoreChainDependents; |
| 1067 | for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) { |
| 1068 | SUnit *SU = &DAG->SUnits[Idx]; |
| 1069 | if (!SU->getInstr()->mayLoad()) |
| 1070 | continue; |
| 1071 | unsigned ChainPredID = DAG->SUnits.size(); |
| 1072 | for (SUnit::const_pred_iterator |
| 1073 | PI = SU->Preds.begin(), PE = SU->Preds.end(); PI != PE; ++PI) { |
| 1074 | if (PI->isCtrl()) { |
| 1075 | ChainPredID = PI->getSUnit()->NodeNum; |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
| 1079 | // Check if this chain-like pred has been seen |
| 1080 | // before. ChainPredID==MaxNodeID for loads at the top of the schedule. |
| 1081 | unsigned NumChains = StoreChainDependents.size(); |
| 1082 | std::pair<DenseMap<unsigned, unsigned>::iterator, bool> Result = |
| 1083 | StoreChainIDs.insert(std::make_pair(ChainPredID, NumChains)); |
| 1084 | if (Result.second) |
| 1085 | StoreChainDependents.resize(NumChains + 1); |
| 1086 | StoreChainDependents[Result.first->second].push_back(SU); |
| 1087 | } |
| 1088 | // Iterate over the store chains. |
| 1089 | for (unsigned Idx = 0, End = StoreChainDependents.size(); Idx != End; ++Idx) |
| 1090 | clusterNeighboringLoads(StoreChainDependents[Idx], DAG); |
| 1091 | } |
| 1092 | |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 1093 | //===----------------------------------------------------------------------===// |
Andrew Trick | 26328024 | 2012-11-12 19:52:20 +0000 | [diff] [blame] | 1094 | // MacroFusion - DAG post-processing to encourage fusion of macro ops. |
| 1095 | //===----------------------------------------------------------------------===// |
| 1096 | |
| 1097 | namespace { |
| 1098 | /// \brief Post-process the DAG to create cluster edges between instructions |
| 1099 | /// that may be fused by the processor into a single operation. |
| 1100 | class MacroFusion : public ScheduleDAGMutation { |
| 1101 | const TargetInstrInfo *TII; |
| 1102 | public: |
| 1103 | MacroFusion(const TargetInstrInfo *tii): TII(tii) {} |
| 1104 | |
| 1105 | virtual void apply(ScheduleDAGMI *DAG); |
| 1106 | }; |
| 1107 | } // anonymous |
| 1108 | |
| 1109 | /// \brief Callback from DAG postProcessing to create cluster edges to encourage |
| 1110 | /// fused operations. |
| 1111 | void MacroFusion::apply(ScheduleDAGMI *DAG) { |
| 1112 | // For now, assume targets can only fuse with the branch. |
| 1113 | MachineInstr *Branch = DAG->ExitSU.getInstr(); |
| 1114 | if (!Branch) |
| 1115 | return; |
| 1116 | |
| 1117 | for (unsigned Idx = DAG->SUnits.size(); Idx > 0;) { |
| 1118 | SUnit *SU = &DAG->SUnits[--Idx]; |
| 1119 | if (!TII->shouldScheduleAdjacent(SU->getInstr(), Branch)) |
| 1120 | continue; |
| 1121 | |
| 1122 | // Create a single weak edge from SU to ExitSU. The only effect is to cause |
| 1123 | // bottom-up scheduling to heavily prioritize the clustered SU. There is no |
| 1124 | // need to copy predecessor edges from ExitSU to SU, since top-down |
| 1125 | // scheduling cannot prioritize ExitSU anyway. To defer top-down scheduling |
| 1126 | // of SU, we could create an artificial edge from the deepest root, but it |
| 1127 | // hasn't been needed yet. |
| 1128 | bool Success = DAG->addEdge(&DAG->ExitSU, SDep(SU, SDep::Cluster)); |
| 1129 | (void)Success; |
| 1130 | assert(Success && "No DAG nodes should be reachable from ExitSU"); |
| 1131 | |
| 1132 | DEBUG(dbgs() << "Macro Fuse SU(" << SU->NodeNum << ")\n"); |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | //===----------------------------------------------------------------------===// |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 1138 | // CopyConstrain - DAG post-processing to encourage copy elimination. |
| 1139 | //===----------------------------------------------------------------------===// |
| 1140 | |
| 1141 | namespace { |
| 1142 | /// \brief Post-process the DAG to create weak edges from all uses of a copy to |
| 1143 | /// the one use that defines the copy's source vreg, most likely an induction |
| 1144 | /// variable increment. |
| 1145 | class CopyConstrain : public ScheduleDAGMutation { |
| 1146 | // Transient state. |
| 1147 | SlotIndex RegionBeginIdx; |
Andrew Trick | 2e87517 | 2013-04-24 23:19:56 +0000 | [diff] [blame] | 1148 | // RegionEndIdx is the slot index of the last non-debug instruction in the |
| 1149 | // scheduling region. So we may have RegionBeginIdx == RegionEndIdx. |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 1150 | SlotIndex RegionEndIdx; |
| 1151 | public: |
| 1152 | CopyConstrain(const TargetInstrInfo *, const TargetRegisterInfo *) {} |
| 1153 | |
| 1154 | virtual void apply(ScheduleDAGMI *DAG); |
| 1155 | |
| 1156 | protected: |
| 1157 | void constrainLocalCopy(SUnit *CopySU, ScheduleDAGMI *DAG); |
| 1158 | }; |
| 1159 | } // anonymous |
| 1160 | |
| 1161 | /// constrainLocalCopy handles two possibilities: |
| 1162 | /// 1) Local src: |
| 1163 | /// I0: = dst |
| 1164 | /// I1: src = ... |
| 1165 | /// I2: = dst |
| 1166 | /// I3: dst = src (copy) |
| 1167 | /// (create pred->succ edges I0->I1, I2->I1) |
| 1168 | /// |
| 1169 | /// 2) Local copy: |
| 1170 | /// I0: dst = src (copy) |
| 1171 | /// I1: = dst |
| 1172 | /// I2: src = ... |
| 1173 | /// I3: = dst |
| 1174 | /// (create pred->succ edges I1->I2, I3->I2) |
| 1175 | /// |
| 1176 | /// Although the MachineScheduler is currently constrained to single blocks, |
| 1177 | /// this algorithm should handle extended blocks. An EBB is a set of |
| 1178 | /// contiguously numbered blocks such that the previous block in the EBB is |
| 1179 | /// always the single predecessor. |
| 1180 | void CopyConstrain::constrainLocalCopy(SUnit *CopySU, ScheduleDAGMI *DAG) { |
| 1181 | LiveIntervals *LIS = DAG->getLIS(); |
| 1182 | MachineInstr *Copy = CopySU->getInstr(); |
| 1183 | |
| 1184 | // Check for pure vreg copies. |
| 1185 | unsigned SrcReg = Copy->getOperand(1).getReg(); |
| 1186 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 1187 | return; |
| 1188 | |
| 1189 | unsigned DstReg = Copy->getOperand(0).getReg(); |
| 1190 | if (!TargetRegisterInfo::isVirtualRegister(DstReg)) |
| 1191 | return; |
| 1192 | |
| 1193 | // Check if either the dest or source is local. If it's live across a back |
| 1194 | // edge, it's not local. Note that if both vregs are live across the back |
| 1195 | // edge, we cannot successfully contrain the copy without cyclic scheduling. |
| 1196 | unsigned LocalReg = DstReg; |
| 1197 | unsigned GlobalReg = SrcReg; |
| 1198 | LiveInterval *LocalLI = &LIS->getInterval(LocalReg); |
| 1199 | if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx)) { |
| 1200 | LocalReg = SrcReg; |
| 1201 | GlobalReg = DstReg; |
| 1202 | LocalLI = &LIS->getInterval(LocalReg); |
| 1203 | if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx)) |
| 1204 | return; |
| 1205 | } |
| 1206 | LiveInterval *GlobalLI = &LIS->getInterval(GlobalReg); |
| 1207 | |
| 1208 | // Find the global segment after the start of the local LI. |
| 1209 | LiveInterval::iterator GlobalSegment = GlobalLI->find(LocalLI->beginIndex()); |
| 1210 | // If GlobalLI does not overlap LocalLI->start, then a copy directly feeds a |
| 1211 | // local live range. We could create edges from other global uses to the local |
| 1212 | // start, but the coalescer should have already eliminated these cases, so |
| 1213 | // don't bother dealing with it. |
| 1214 | if (GlobalSegment == GlobalLI->end()) |
| 1215 | return; |
| 1216 | |
| 1217 | // If GlobalSegment is killed at the LocalLI->start, the call to find() |
| 1218 | // returned the next global segment. But if GlobalSegment overlaps with |
| 1219 | // LocalLI->start, then advance to the next segement. If a hole in GlobalLI |
| 1220 | // exists in LocalLI's vicinity, GlobalSegment will be the end of the hole. |
| 1221 | if (GlobalSegment->contains(LocalLI->beginIndex())) |
| 1222 | ++GlobalSegment; |
| 1223 | |
| 1224 | if (GlobalSegment == GlobalLI->end()) |
| 1225 | return; |
| 1226 | |
| 1227 | // Check if GlobalLI contains a hole in the vicinity of LocalLI. |
| 1228 | if (GlobalSegment != GlobalLI->begin()) { |
| 1229 | // Two address defs have no hole. |
| 1230 | if (SlotIndex::isSameInstr(llvm::prior(GlobalSegment)->end, |
| 1231 | GlobalSegment->start)) { |
| 1232 | return; |
| 1233 | } |
Andrew Trick | d976177 | 2013-07-30 19:59:08 +0000 | [diff] [blame] | 1234 | // If the prior global segment may be defined by the same two-address |
| 1235 | // instruction that also defines LocalLI, then can't make a hole here. |
| 1236 | if (SlotIndex::isSameInstr(llvm::prior(GlobalSegment)->start, |
| 1237 | LocalLI->beginIndex())) { |
| 1238 | return; |
| 1239 | } |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 1240 | // If GlobalLI has a prior segment, it must be live into the EBB. Otherwise |
| 1241 | // it would be a disconnected component in the live range. |
| 1242 | assert(llvm::prior(GlobalSegment)->start < LocalLI->beginIndex() && |
| 1243 | "Disconnected LRG within the scheduling region."); |
| 1244 | } |
| 1245 | MachineInstr *GlobalDef = LIS->getInstructionFromIndex(GlobalSegment->start); |
| 1246 | if (!GlobalDef) |
| 1247 | return; |
| 1248 | |
| 1249 | SUnit *GlobalSU = DAG->getSUnit(GlobalDef); |
| 1250 | if (!GlobalSU) |
| 1251 | return; |
| 1252 | |
| 1253 | // GlobalDef is the bottom of the GlobalLI hole. Open the hole by |
| 1254 | // constraining the uses of the last local def to precede GlobalDef. |
| 1255 | SmallVector<SUnit*,8> LocalUses; |
| 1256 | const VNInfo *LastLocalVN = LocalLI->getVNInfoBefore(LocalLI->endIndex()); |
| 1257 | MachineInstr *LastLocalDef = LIS->getInstructionFromIndex(LastLocalVN->def); |
| 1258 | SUnit *LastLocalSU = DAG->getSUnit(LastLocalDef); |
| 1259 | for (SUnit::const_succ_iterator |
| 1260 | I = LastLocalSU->Succs.begin(), E = LastLocalSU->Succs.end(); |
| 1261 | I != E; ++I) { |
| 1262 | if (I->getKind() != SDep::Data || I->getReg() != LocalReg) |
| 1263 | continue; |
| 1264 | if (I->getSUnit() == GlobalSU) |
| 1265 | continue; |
| 1266 | if (!DAG->canAddEdge(GlobalSU, I->getSUnit())) |
| 1267 | return; |
| 1268 | LocalUses.push_back(I->getSUnit()); |
| 1269 | } |
| 1270 | // Open the top of the GlobalLI hole by constraining any earlier global uses |
| 1271 | // to precede the start of LocalLI. |
| 1272 | SmallVector<SUnit*,8> GlobalUses; |
| 1273 | MachineInstr *FirstLocalDef = |
| 1274 | LIS->getInstructionFromIndex(LocalLI->beginIndex()); |
| 1275 | SUnit *FirstLocalSU = DAG->getSUnit(FirstLocalDef); |
| 1276 | for (SUnit::const_pred_iterator |
| 1277 | I = GlobalSU->Preds.begin(), E = GlobalSU->Preds.end(); I != E; ++I) { |
| 1278 | if (I->getKind() != SDep::Anti || I->getReg() != GlobalReg) |
| 1279 | continue; |
| 1280 | if (I->getSUnit() == FirstLocalSU) |
| 1281 | continue; |
| 1282 | if (!DAG->canAddEdge(FirstLocalSU, I->getSUnit())) |
| 1283 | return; |
| 1284 | GlobalUses.push_back(I->getSUnit()); |
| 1285 | } |
| 1286 | DEBUG(dbgs() << "Constraining copy SU(" << CopySU->NodeNum << ")\n"); |
| 1287 | // Add the weak edges. |
| 1288 | for (SmallVectorImpl<SUnit*>::const_iterator |
| 1289 | I = LocalUses.begin(), E = LocalUses.end(); I != E; ++I) { |
| 1290 | DEBUG(dbgs() << " Local use SU(" << (*I)->NodeNum << ") -> SU(" |
| 1291 | << GlobalSU->NodeNum << ")\n"); |
| 1292 | DAG->addEdge(GlobalSU, SDep(*I, SDep::Weak)); |
| 1293 | } |
| 1294 | for (SmallVectorImpl<SUnit*>::const_iterator |
| 1295 | I = GlobalUses.begin(), E = GlobalUses.end(); I != E; ++I) { |
| 1296 | DEBUG(dbgs() << " Global use SU(" << (*I)->NodeNum << ") -> SU(" |
| 1297 | << FirstLocalSU->NodeNum << ")\n"); |
| 1298 | DAG->addEdge(FirstLocalSU, SDep(*I, SDep::Weak)); |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | /// \brief Callback from DAG postProcessing to create weak edges to encourage |
| 1303 | /// copy elimination. |
| 1304 | void CopyConstrain::apply(ScheduleDAGMI *DAG) { |
Andrew Trick | 2e87517 | 2013-04-24 23:19:56 +0000 | [diff] [blame] | 1305 | MachineBasicBlock::iterator FirstPos = nextIfDebug(DAG->begin(), DAG->end()); |
| 1306 | if (FirstPos == DAG->end()) |
| 1307 | return; |
| 1308 | RegionBeginIdx = DAG->getLIS()->getInstructionIndex(&*FirstPos); |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 1309 | RegionEndIdx = DAG->getLIS()->getInstructionIndex( |
| 1310 | &*priorNonDebug(DAG->end(), DAG->begin())); |
| 1311 | |
| 1312 | for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) { |
| 1313 | SUnit *SU = &DAG->SUnits[Idx]; |
| 1314 | if (!SU->getInstr()->isCopy()) |
| 1315 | continue; |
| 1316 | |
| 1317 | constrainLocalCopy(SU, DAG); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | //===----------------------------------------------------------------------===// |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1322 | // GenericScheduler - Implementation of the generic MachineSchedStrategy. |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 1323 | //===----------------------------------------------------------------------===// |
| 1324 | |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1325 | static const unsigned InvalidCycle = ~0U; |
| 1326 | |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 1327 | namespace { |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1328 | /// GenericScheduler shrinks the unscheduled zone using heuristics to balance |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1329 | /// the schedule. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1330 | class GenericScheduler : public MachineSchedStrategy { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1331 | public: |
| 1332 | /// Represent the type of SchedCandidate found within a single queue. |
| 1333 | /// pickNodeBidirectional depends on these listed by decreasing priority. |
| 1334 | enum CandReason { |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 1335 | NoCand, PhysRegCopy, RegExcess, RegCritical, Stall, Cluster, Weak, RegMax, |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 1336 | ResourceReduce, ResourceDemand, BotHeightReduce, BotPathReduce, |
Andrew Trick | 71f08a3 | 2013-06-17 21:45:13 +0000 | [diff] [blame] | 1337 | TopDepthReduce, TopPathReduce, NextDefUse, NodeOrder}; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1338 | |
| 1339 | #ifndef NDEBUG |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1340 | static const char *getReasonStr(GenericScheduler::CandReason Reason); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1341 | #endif |
| 1342 | |
| 1343 | /// Policy for scheduling the next instruction in the candidate's zone. |
| 1344 | struct CandPolicy { |
| 1345 | bool ReduceLatency; |
| 1346 | unsigned ReduceResIdx; |
| 1347 | unsigned DemandResIdx; |
| 1348 | |
| 1349 | CandPolicy(): ReduceLatency(false), ReduceResIdx(0), DemandResIdx(0) {} |
| 1350 | }; |
| 1351 | |
| 1352 | /// Status of an instruction's critical resource consumption. |
| 1353 | struct SchedResourceDelta { |
| 1354 | // Count critical resources in the scheduled region required by SU. |
| 1355 | unsigned CritResources; |
| 1356 | |
| 1357 | // Count critical resources from another region consumed by SU. |
| 1358 | unsigned DemandedResources; |
| 1359 | |
| 1360 | SchedResourceDelta(): CritResources(0), DemandedResources(0) {} |
| 1361 | |
| 1362 | bool operator==(const SchedResourceDelta &RHS) const { |
| 1363 | return CritResources == RHS.CritResources |
| 1364 | && DemandedResources == RHS.DemandedResources; |
| 1365 | } |
| 1366 | bool operator!=(const SchedResourceDelta &RHS) const { |
| 1367 | return !operator==(RHS); |
| 1368 | } |
| 1369 | }; |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1370 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1371 | /// Store the state used by GenericScheduler heuristics, required for the |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1372 | /// lifetime of one invocation of pickNode(). |
| 1373 | struct SchedCandidate { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1374 | CandPolicy Policy; |
| 1375 | |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1376 | // The best SUnit candidate. |
| 1377 | SUnit *SU; |
| 1378 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1379 | // The reason for this candidate. |
| 1380 | CandReason Reason; |
| 1381 | |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 1382 | // Set of reasons that apply to multiple candidates. |
| 1383 | uint32_t RepeatReasonSet; |
| 1384 | |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1385 | // Register pressure values for the best candidate. |
| 1386 | RegPressureDelta RPDelta; |
| 1387 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1388 | // Critical resource consumption of the best candidate. |
| 1389 | SchedResourceDelta ResDelta; |
| 1390 | |
| 1391 | SchedCandidate(const CandPolicy &policy) |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 1392 | : Policy(policy), SU(NULL), Reason(NoCand), RepeatReasonSet(0) {} |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1393 | |
| 1394 | bool isValid() const { return SU; } |
| 1395 | |
| 1396 | // Copy the status of another candidate without changing policy. |
| 1397 | void setBest(SchedCandidate &Best) { |
| 1398 | assert(Best.Reason != NoCand && "uninitialized Sched candidate"); |
| 1399 | SU = Best.SU; |
| 1400 | Reason = Best.Reason; |
| 1401 | RPDelta = Best.RPDelta; |
| 1402 | ResDelta = Best.ResDelta; |
| 1403 | } |
| 1404 | |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 1405 | bool isRepeat(CandReason R) { return RepeatReasonSet & (1 << R); } |
| 1406 | void setRepeat(CandReason R) { RepeatReasonSet |= (1 << R); } |
| 1407 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1408 | void initResourceDelta(const ScheduleDAGMI *DAG, |
| 1409 | const TargetSchedModel *SchedModel); |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1410 | }; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1411 | |
| 1412 | /// Summarize the unscheduled region. |
| 1413 | struct SchedRemainder { |
| 1414 | // Critical path through the DAG in expected latency. |
| 1415 | unsigned CriticalPath; |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1416 | unsigned CyclicCritPath; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1417 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1418 | // Scaled count of micro-ops left to schedule. |
| 1419 | unsigned RemIssueCount; |
| 1420 | |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1421 | bool IsAcyclicLatencyLimited; |
| 1422 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1423 | // Unscheduled resources |
| 1424 | SmallVector<unsigned, 16> RemainingCounts; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1425 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1426 | void reset() { |
| 1427 | CriticalPath = 0; |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1428 | CyclicCritPath = 0; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1429 | RemIssueCount = 0; |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1430 | IsAcyclicLatencyLimited = false; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1431 | RemainingCounts.clear(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | SchedRemainder() { reset(); } |
| 1435 | |
| 1436 | void init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel); |
| 1437 | }; |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1438 | |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1439 | /// Each Scheduling boundary is associated with ready queues. It tracks the |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1440 | /// current cycle in the direction of movement, and maintains the state |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1441 | /// of "hazards" and other interlocks at the current cycle. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1442 | struct SchedBoundary { |
Andrew Trick | ce27bb9 | 2012-06-29 03:23:22 +0000 | [diff] [blame] | 1443 | ScheduleDAGMI *DAG; |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 1444 | const TargetSchedModel *SchedModel; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1445 | SchedRemainder *Rem; |
Andrew Trick | ce27bb9 | 2012-06-29 03:23:22 +0000 | [diff] [blame] | 1446 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1447 | ReadyQueue Available; |
| 1448 | ReadyQueue Pending; |
| 1449 | bool CheckPending; |
| 1450 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1451 | // For heuristics, keep a list of the nodes that immediately depend on the |
| 1452 | // most recently scheduled node. |
| 1453 | SmallPtrSet<const SUnit*, 8> NextSUs; |
| 1454 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1455 | ScheduleHazardRecognizer *HazardRec; |
| 1456 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1457 | /// Number of cycles it takes to issue the instructions scheduled in this |
| 1458 | /// zone. It is defined as: scheduled-micro-ops / issue-width + stalls. |
| 1459 | /// See getStalls(). |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1460 | unsigned CurrCycle; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1461 | |
| 1462 | /// Micro-ops issued in the current cycle |
Andrew Trick | e2ff575 | 2013-06-15 04:49:49 +0000 | [diff] [blame] | 1463 | unsigned CurrMOps; |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1464 | |
| 1465 | /// MinReadyCycle - Cycle of the soonest available instruction. |
| 1466 | unsigned MinReadyCycle; |
| 1467 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1468 | // The expected latency of the critical path in this scheduled zone. |
| 1469 | unsigned ExpectedLatency; |
| 1470 | |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 1471 | // The latency of dependence chains leading into this zone. |
Andrew Trick | 2f7667e | 2013-08-07 17:20:32 +0000 | [diff] [blame] | 1472 | // For each node scheduled bottom-up: DLat = max DLat, N.Depth. |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 1473 | // For each cycle scheduled: DLat -= 1. |
| 1474 | unsigned DependentLatency; |
| 1475 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1476 | /// Count the scheduled (issued) micro-ops that can be retired by |
| 1477 | /// time=CurrCycle assuming the first scheduled instr is retired at time=0. |
| 1478 | unsigned RetiredMOps; |
| 1479 | |
| 1480 | // Count scheduled resources that have been executed. Resources are |
| 1481 | // considered executed if they become ready in the time that it takes to |
| 1482 | // saturate any resource including the one in question. Counts are scaled |
Andrew Trick | b13ef17 | 2013-07-19 00:20:07 +0000 | [diff] [blame] | 1483 | // for direct comparison with other resources. Counts can be compared with |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1484 | // MOps * getMicroOpFactor and Latency * getLatencyFactor. |
| 1485 | SmallVector<unsigned, 16> ExecutedResCounts; |
| 1486 | |
| 1487 | /// Cache the max count for a single resource. |
| 1488 | unsigned MaxExecutedResCount; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1489 | |
| 1490 | // Cache the critical resources ID in this scheduled zone. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1491 | unsigned ZoneCritResIdx; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Is the scheduled region resource limited vs. latency limited. |
| 1494 | bool IsResourceLimited; |
| 1495 | |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1496 | // Record the highest cycle at which each resource has been reserved by a |
| 1497 | // scheduled instruction. |
| 1498 | SmallVector<unsigned, 16> ReservedCycles; |
| 1499 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1500 | #ifndef NDEBUG |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1501 | // Remember the greatest operand latency as an upper bound on the number of |
| 1502 | // times we should retry the pending queue because of a hazard. |
| 1503 | unsigned MaxObservedLatency; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1504 | #endif |
| 1505 | |
| 1506 | void reset() { |
Andrew Trick | 553e0fe | 2013-02-13 19:22:27 +0000 | [diff] [blame] | 1507 | // A new HazardRec is created for each DAG and owned by SchedBoundary. |
Andrew Trick | b05db8e | 2013-09-04 21:12:05 +0000 | [diff] [blame] | 1508 | // Destroying and reconstructing it is very expensive though. So keep |
Andrew Trick | 8c699c9 | 2013-09-04 21:00:05 +0000 | [diff] [blame] | 1509 | // invalid, placeholder HazardRecs. |
| 1510 | if (HazardRec && HazardRec->isEnabled()) { |
| 1511 | delete HazardRec; |
| 1512 | HazardRec = 0; |
| 1513 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1514 | Available.clear(); |
| 1515 | Pending.clear(); |
| 1516 | CheckPending = false; |
| 1517 | NextSUs.clear(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1518 | CurrCycle = 0; |
Andrew Trick | e2ff575 | 2013-06-15 04:49:49 +0000 | [diff] [blame] | 1519 | CurrMOps = 0; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1520 | MinReadyCycle = UINT_MAX; |
| 1521 | ExpectedLatency = 0; |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 1522 | DependentLatency = 0; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1523 | RetiredMOps = 0; |
| 1524 | MaxExecutedResCount = 0; |
| 1525 | ZoneCritResIdx = 0; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1526 | IsResourceLimited = false; |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1527 | ReservedCycles.clear(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1528 | #ifndef NDEBUG |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1529 | MaxObservedLatency = 0; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1530 | #endif |
| 1531 | // Reserve a zero-count for invalid CritResIdx. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1532 | ExecutedResCounts.resize(1); |
| 1533 | assert(!ExecutedResCounts[0] && "nonzero count for bad resource"); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1534 | } |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1535 | |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1536 | /// Pending queues extend the ready queues with the same ID and the |
| 1537 | /// PendingFlag set. |
| 1538 | SchedBoundary(unsigned ID, const Twine &Name): |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1539 | DAG(0), SchedModel(0), Rem(0), Available(ID, Name+".A"), |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1540 | Pending(ID << GenericScheduler::LogMaxQID, Name+".P"), |
Andrew Trick | 553e0fe | 2013-02-13 19:22:27 +0000 | [diff] [blame] | 1541 | HazardRec(0) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1542 | reset(); |
| 1543 | } |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1544 | |
| 1545 | ~SchedBoundary() { delete HazardRec; } |
| 1546 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1547 | void init(ScheduleDAGMI *dag, const TargetSchedModel *smodel, |
| 1548 | SchedRemainder *rem); |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 1549 | |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1550 | bool isTop() const { |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1551 | return Available.getID() == GenericScheduler::TopQID; |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1552 | } |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1553 | |
Andrew Trick | 8e8415f | 2013-06-15 05:46:47 +0000 | [diff] [blame] | 1554 | #ifndef NDEBUG |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1555 | const char *getResourceName(unsigned PIdx) { |
| 1556 | if (!PIdx) |
| 1557 | return "MOps"; |
| 1558 | return SchedModel->getProcResource(PIdx)->Name; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1559 | } |
Andrew Trick | 8e8415f | 2013-06-15 05:46:47 +0000 | [diff] [blame] | 1560 | #endif |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1561 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1562 | /// Get the number of latency cycles "covered" by the scheduled |
| 1563 | /// instructions. This is the larger of the critical path within the zone |
| 1564 | /// and the number of cycles required to issue the instructions. |
| 1565 | unsigned getScheduledLatency() const { |
| 1566 | return std::max(ExpectedLatency, CurrCycle); |
| 1567 | } |
| 1568 | |
| 1569 | unsigned getUnscheduledLatency(SUnit *SU) const { |
| 1570 | return isTop() ? SU->getHeight() : SU->getDepth(); |
| 1571 | } |
| 1572 | |
| 1573 | unsigned getResourceCount(unsigned ResIdx) const { |
| 1574 | return ExecutedResCounts[ResIdx]; |
| 1575 | } |
| 1576 | |
| 1577 | /// Get the scaled count of scheduled micro-ops and resources, including |
| 1578 | /// executed resources. |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1579 | unsigned getCriticalCount() const { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1580 | if (!ZoneCritResIdx) |
| 1581 | return RetiredMOps * SchedModel->getMicroOpFactor(); |
| 1582 | return getResourceCount(ZoneCritResIdx); |
| 1583 | } |
| 1584 | |
| 1585 | /// Get a scaled count for the minimum execution time of the scheduled |
| 1586 | /// micro-ops that are ready to execute by getExecutedCount. Notice the |
| 1587 | /// feedback loop. |
| 1588 | unsigned getExecutedCount() const { |
| 1589 | return std::max(CurrCycle * SchedModel->getLatencyFactor(), |
| 1590 | MaxExecutedResCount); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 1593 | /// Get the difference between the given SUnit's ready time and the current |
| 1594 | /// cycle. |
| 1595 | unsigned getLatencyStallCycles(SUnit *SU); |
| 1596 | |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1597 | unsigned getNextResourceCycle(unsigned PIdx, unsigned Cycles); |
| 1598 | |
Andrew Trick | 8c9e672 | 2012-06-29 03:23:24 +0000 | [diff] [blame] | 1599 | bool checkHazard(SUnit *SU); |
| 1600 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1601 | unsigned findMaxLatency(ArrayRef<SUnit*> ReadySUs); |
| 1602 | |
| 1603 | unsigned getOtherResourceCount(unsigned &OtherCritIdx); |
| 1604 | |
| 1605 | void setPolicy(CandPolicy &Policy, SchedBoundary &OtherZone); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1606 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1607 | void releaseNode(SUnit *SU, unsigned ReadyCycle); |
| 1608 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1609 | void bumpCycle(unsigned NextCycle); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1610 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1611 | void incExecutedResources(unsigned PIdx, unsigned Count); |
| 1612 | |
| 1613 | unsigned countResource(unsigned PIdx, unsigned Cycles, unsigned ReadyCycle); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1614 | |
Andrew Trick | ce27bb9 | 2012-06-29 03:23:22 +0000 | [diff] [blame] | 1615 | void bumpNode(SUnit *SU); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1616 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1617 | void releasePending(); |
| 1618 | |
| 1619 | void removeReady(SUnit *SU); |
| 1620 | |
| 1621 | SUnit *pickOnlyChoice(); |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1622 | |
Andrew Trick | 8e8415f | 2013-06-15 05:46:47 +0000 | [diff] [blame] | 1623 | #ifndef NDEBUG |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1624 | void dumpScheduledState(); |
Andrew Trick | 8e8415f | 2013-06-15 05:46:47 +0000 | [diff] [blame] | 1625 | #endif |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1626 | }; |
| 1627 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1628 | private: |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 1629 | const MachineSchedContext *Context; |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1630 | ScheduleDAGMI *DAG; |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 1631 | const TargetSchedModel *SchedModel; |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1632 | const TargetRegisterInfo *TRI; |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 1633 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1634 | // State of the top and bottom scheduled instruction boundaries. |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1635 | SchedRemainder Rem; |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1636 | SchedBoundary Top; |
| 1637 | SchedBoundary Bot; |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1638 | |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 1639 | MachineSchedPolicy RegionPolicy; |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1640 | public: |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1641 | /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both) |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1642 | enum { |
| 1643 | TopQID = 1, |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 1644 | BotQID = 2, |
| 1645 | LogMaxQID = 2 |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1646 | }; |
| 1647 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1648 | GenericScheduler(const MachineSchedContext *C): |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 1649 | Context(C), DAG(0), SchedModel(0), TRI(0), |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 1650 | Top(TopQID, "TopQ"), Bot(BotQID, "BotQ") {} |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 1651 | |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 1652 | virtual void initPolicy(MachineBasicBlock::iterator Begin, |
| 1653 | MachineBasicBlock::iterator End, |
| 1654 | unsigned NumRegionInstrs); |
| 1655 | |
| 1656 | bool shouldTrackPressure() const { return RegionPolicy.ShouldTrackPressure; } |
Andrew Trick | 95dafd8 | 2012-05-10 21:06:12 +0000 | [diff] [blame] | 1657 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1658 | virtual void initialize(ScheduleDAGMI *dag); |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1659 | |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 1660 | virtual SUnit *pickNode(bool &IsTopNode); |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 1661 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1662 | virtual void schedNode(SUnit *SU, bool IsTopNode); |
| 1663 | |
| 1664 | virtual void releaseTopNode(SUnit *SU); |
| 1665 | |
| 1666 | virtual void releaseBottomNode(SUnit *SU); |
| 1667 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1668 | virtual void registerRoots(); |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 1669 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1670 | protected: |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1671 | void checkAcyclicLatency(); |
| 1672 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1673 | void tryCandidate(SchedCandidate &Cand, |
| 1674 | SchedCandidate &TryCand, |
| 1675 | SchedBoundary &Zone, |
| 1676 | const RegPressureTracker &RPTracker, |
| 1677 | RegPressureTracker &TempTracker); |
| 1678 | |
| 1679 | SUnit *pickNodeBidirectional(bool &IsTopNode); |
| 1680 | |
| 1681 | void pickNodeFromQueue(SchedBoundary &Zone, |
| 1682 | const RegPressureTracker &RPTracker, |
| 1683 | SchedCandidate &Candidate); |
| 1684 | |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 1685 | void reschedulePhysRegCopies(SUnit *SU, bool isTop); |
| 1686 | |
Andrew Trick | 419eae2 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 1687 | #ifndef NDEBUG |
Andrew Trick | 419d491 | 2013-04-05 00:31:29 +0000 | [diff] [blame] | 1688 | void traceCandidate(const SchedCandidate &Cand); |
Andrew Trick | 419eae2 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 1689 | #endif |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 1690 | }; |
| 1691 | } // namespace |
| 1692 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1693 | void GenericScheduler::SchedRemainder:: |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1694 | init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel) { |
| 1695 | reset(); |
| 1696 | if (!SchedModel->hasInstrSchedModel()) |
| 1697 | return; |
| 1698 | RemainingCounts.resize(SchedModel->getNumProcResourceKinds()); |
| 1699 | for (std::vector<SUnit>::iterator |
| 1700 | I = DAG->SUnits.begin(), E = DAG->SUnits.end(); I != E; ++I) { |
| 1701 | const MCSchedClassDesc *SC = DAG->getSchedClass(&*I); |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1702 | RemIssueCount += SchedModel->getNumMicroOps(I->getInstr(), SC) |
| 1703 | * SchedModel->getMicroOpFactor(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1704 | for (TargetSchedModel::ProcResIter |
| 1705 | PI = SchedModel->getWriteProcResBegin(SC), |
| 1706 | PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) { |
| 1707 | unsigned PIdx = PI->ProcResourceIdx; |
| 1708 | unsigned Factor = SchedModel->getResourceFactor(PIdx); |
| 1709 | RemainingCounts[PIdx] += (Factor * PI->Cycles); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1714 | void GenericScheduler::SchedBoundary:: |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1715 | init(ScheduleDAGMI *dag, const TargetSchedModel *smodel, SchedRemainder *rem) { |
| 1716 | reset(); |
| 1717 | DAG = dag; |
| 1718 | SchedModel = smodel; |
| 1719 | Rem = rem; |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1720 | if (SchedModel->hasInstrSchedModel()) { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1721 | ExecutedResCounts.resize(SchedModel->getNumProcResourceKinds()); |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1722 | ReservedCycles.resize(SchedModel->getNumProcResourceKinds(), InvalidCycle); |
| 1723 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 1726 | /// Initialize the per-region scheduling policy. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1727 | void GenericScheduler::initPolicy(MachineBasicBlock::iterator Begin, |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 1728 | MachineBasicBlock::iterator End, |
| 1729 | unsigned NumRegionInstrs) { |
| 1730 | const TargetMachine &TM = Context->MF->getTarget(); |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 1731 | |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 1732 | // Avoid setting up the register pressure tracker for small regions to save |
| 1733 | // compile time. As a rough heuristic, only track pressure when the number of |
| 1734 | // schedulable instructions exceeds half the integer register file. |
| 1735 | unsigned NIntRegs = Context->RegClassInfo->getNumAllocatableRegs( |
| 1736 | TM.getTargetLowering()->getRegClassFor(MVT::i32)); |
| 1737 | |
| 1738 | RegionPolicy.ShouldTrackPressure = NumRegionInstrs > (NIntRegs / 2); |
| 1739 | |
| 1740 | // For generic targets, we default to bottom-up, because it's simpler and more |
| 1741 | // compile-time optimizations have been implemented in that direction. |
| 1742 | RegionPolicy.OnlyBottomUp = true; |
| 1743 | |
| 1744 | // Allow the subtarget to override default policy. |
| 1745 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 1746 | ST.overrideSchedPolicy(RegionPolicy, Begin, End, NumRegionInstrs); |
| 1747 | |
| 1748 | // After subtarget overrides, apply command line options. |
| 1749 | if (!EnableRegPressure) |
| 1750 | RegionPolicy.ShouldTrackPressure = false; |
| 1751 | |
| 1752 | // Check -misched-topdown/bottomup can force or unforce scheduling direction. |
| 1753 | // e.g. -misched-bottomup=false allows scheduling in both directions. |
| 1754 | assert((!ForceTopDown || !ForceBottomUp) && |
| 1755 | "-misched-topdown incompatible with -misched-bottomup"); |
| 1756 | if (ForceBottomUp.getNumOccurrences() > 0) { |
| 1757 | RegionPolicy.OnlyBottomUp = ForceBottomUp; |
| 1758 | if (RegionPolicy.OnlyBottomUp) |
| 1759 | RegionPolicy.OnlyTopDown = false; |
| 1760 | } |
| 1761 | if (ForceTopDown.getNumOccurrences() > 0) { |
| 1762 | RegionPolicy.OnlyTopDown = ForceTopDown; |
| 1763 | if (RegionPolicy.OnlyTopDown) |
| 1764 | RegionPolicy.OnlyBottomUp = false; |
| 1765 | } |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1768 | void GenericScheduler::initialize(ScheduleDAGMI *dag) { |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1769 | DAG = dag; |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 1770 | SchedModel = DAG->getSchedModel(); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1771 | TRI = DAG->TRI; |
Andrew Trick | 553e0fe | 2013-02-13 19:22:27 +0000 | [diff] [blame] | 1772 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1773 | Rem.init(DAG, SchedModel); |
| 1774 | Top.init(DAG, SchedModel, &Rem); |
| 1775 | Bot.init(DAG, SchedModel, &Rem); |
| 1776 | |
| 1777 | // Initialize resource counts. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1778 | |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 1779 | // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or |
| 1780 | // are disabled, then these HazardRecs will be disabled. |
| 1781 | const InstrItineraryData *Itin = SchedModel->getInstrItineraries(); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1782 | const TargetMachine &TM = DAG->MF.getTarget(); |
Andrew Trick | 8c699c9 | 2013-09-04 21:00:05 +0000 | [diff] [blame] | 1783 | if (!Top.HazardRec) { |
| 1784 | Top.HazardRec = |
| 1785 | TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG); |
| 1786 | } |
| 1787 | if (!Bot.HazardRec) { |
| 1788 | Bot.HazardRec = |
| 1789 | TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG); |
| 1790 | } |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1793 | void GenericScheduler::releaseTopNode(SUnit *SU) { |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1794 | if (SU->isScheduled) |
| 1795 | return; |
| 1796 | |
Andrew Trick | 493b867 | 2012-12-18 20:52:52 +0000 | [diff] [blame] | 1797 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1798 | I != E; ++I) { |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1799 | if (I->isWeak()) |
| 1800 | continue; |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1801 | unsigned PredReadyCycle = I->getSUnit()->TopReadyCycle; |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1802 | unsigned Latency = I->getLatency(); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1803 | #ifndef NDEBUG |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1804 | Top.MaxObservedLatency = std::max(Latency, Top.MaxObservedLatency); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1805 | #endif |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1806 | if (SU->TopReadyCycle < PredReadyCycle + Latency) |
| 1807 | SU->TopReadyCycle = PredReadyCycle + Latency; |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1808 | } |
| 1809 | Top.releaseNode(SU, SU->TopReadyCycle); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1812 | void GenericScheduler::releaseBottomNode(SUnit *SU) { |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1813 | if (SU->isScheduled) |
| 1814 | return; |
| 1815 | |
| 1816 | assert(SU->getInstr() && "Scheduled SUnit must have instr"); |
| 1817 | |
| 1818 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1819 | I != E; ++I) { |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 1820 | if (I->isWeak()) |
| 1821 | continue; |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1822 | unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle; |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1823 | unsigned Latency = I->getLatency(); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1824 | #ifndef NDEBUG |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1825 | Bot.MaxObservedLatency = std::max(Latency, Bot.MaxObservedLatency); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1826 | #endif |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1827 | if (SU->BotReadyCycle < SuccReadyCycle + Latency) |
| 1828 | SU->BotReadyCycle = SuccReadyCycle + Latency; |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 1829 | } |
| 1830 | Bot.releaseNode(SU, SU->BotReadyCycle); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 1833 | /// Set IsAcyclicLatencyLimited if the acyclic path is longer than the cyclic |
| 1834 | /// critical path by more cycles than it takes to drain the instruction buffer. |
| 1835 | /// We estimate an upper bounds on in-flight instructions as: |
| 1836 | /// |
| 1837 | /// CyclesPerIteration = max( CyclicPath, Loop-Resource-Height ) |
| 1838 | /// InFlightIterations = AcyclicPath / CyclesPerIteration |
| 1839 | /// InFlightResources = InFlightIterations * LoopResources |
| 1840 | /// |
| 1841 | /// TODO: Check execution resources in addition to IssueCount. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1842 | void GenericScheduler::checkAcyclicLatency() { |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1843 | if (Rem.CyclicCritPath == 0 || Rem.CyclicCritPath >= Rem.CriticalPath) |
| 1844 | return; |
| 1845 | |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 1846 | // Scaled number of cycles per loop iteration. |
| 1847 | unsigned IterCount = |
| 1848 | std::max(Rem.CyclicCritPath * SchedModel->getLatencyFactor(), |
| 1849 | Rem.RemIssueCount); |
| 1850 | // Scaled acyclic critical path. |
| 1851 | unsigned AcyclicCount = Rem.CriticalPath * SchedModel->getLatencyFactor(); |
| 1852 | // InFlightCount = (AcyclicPath / IterCycles) * InstrPerLoop |
| 1853 | unsigned InFlightCount = |
| 1854 | (AcyclicCount * Rem.RemIssueCount + IterCount-1) / IterCount; |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1855 | unsigned BufferLimit = |
| 1856 | SchedModel->getMicroOpBufferSize() * SchedModel->getMicroOpFactor(); |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1857 | |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 1858 | Rem.IsAcyclicLatencyLimited = InFlightCount > BufferLimit; |
| 1859 | |
| 1860 | DEBUG(dbgs() << "IssueCycles=" |
| 1861 | << Rem.RemIssueCount / SchedModel->getLatencyFactor() << "c " |
| 1862 | << "IterCycles=" << IterCount / SchedModel->getLatencyFactor() |
| 1863 | << "c NumIters=" << (AcyclicCount + IterCount-1) / IterCount |
| 1864 | << " InFlight=" << InFlightCount / SchedModel->getMicroOpFactor() |
| 1865 | << "m BufferLim=" << SchedModel->getMicroOpBufferSize() << "m\n"; |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1866 | if (Rem.IsAcyclicLatencyLimited) |
| 1867 | dbgs() << " ACYCLIC LATENCY LIMIT\n"); |
| 1868 | } |
| 1869 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1870 | void GenericScheduler::registerRoots() { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1871 | Rem.CriticalPath = DAG->ExitSU.getDepth(); |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 1872 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1873 | // Some roots may not feed into ExitSU. Check all of them in case. |
| 1874 | for (std::vector<SUnit*>::const_iterator |
| 1875 | I = Bot.Available.begin(), E = Bot.Available.end(); I != E; ++I) { |
| 1876 | if ((*I)->getDepth() > Rem.CriticalPath) |
| 1877 | Rem.CriticalPath = (*I)->getDepth(); |
| 1878 | } |
| 1879 | DEBUG(dbgs() << "Critical Path: " << Rem.CriticalPath << '\n'); |
Andrew Trick | 483f419 | 2013-08-29 18:04:49 +0000 | [diff] [blame] | 1880 | |
| 1881 | if (EnableCyclicPath) { |
| 1882 | Rem.CyclicCritPath = DAG->computeCyclicCriticalPath(); |
| 1883 | checkAcyclicLatency(); |
| 1884 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 1887 | /// Compute the stall cycles based on this SUnit's ready time. Heuristics treat |
| 1888 | /// these "soft stalls" differently than the hard stall cycles based on CPU |
| 1889 | /// resources and computed by checkHazard(). A fully in-order model |
| 1890 | /// (MicroOpBufferSize==0) will not make use of this since instructions are not |
| 1891 | /// available for scheduling until they are ready. However, a weaker in-order |
| 1892 | /// model may use this for heuristics. For example, if a processor has in-order |
| 1893 | /// behavior when reading certain resources, this may come into play. |
| 1894 | unsigned GenericScheduler::SchedBoundary::getLatencyStallCycles(SUnit *SU) { |
| 1895 | if (!SU->isUnbuffered) |
| 1896 | return 0; |
| 1897 | |
| 1898 | unsigned ReadyCycle = (isTop() ? SU->TopReadyCycle : SU->BotReadyCycle); |
| 1899 | if (ReadyCycle > CurrCycle) |
| 1900 | return ReadyCycle - CurrCycle; |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1904 | /// Compute the next cycle at which the given processor resource can be |
| 1905 | /// scheduled. |
| 1906 | unsigned GenericScheduler::SchedBoundary:: |
| 1907 | getNextResourceCycle(unsigned PIdx, unsigned Cycles) { |
| 1908 | unsigned NextUnreserved = ReservedCycles[PIdx]; |
| 1909 | // If this resource has never been used, always return cycle zero. |
| 1910 | if (NextUnreserved == InvalidCycle) |
| 1911 | return 0; |
| 1912 | // For bottom-up scheduling add the cycles needed for the current operation. |
| 1913 | if (!isTop()) |
| 1914 | NextUnreserved += Cycles; |
| 1915 | return NextUnreserved; |
| 1916 | } |
| 1917 | |
Andrew Trick | 8c9e672 | 2012-06-29 03:23:24 +0000 | [diff] [blame] | 1918 | /// Does this SU have a hazard within the current instruction group. |
| 1919 | /// |
| 1920 | /// The scheduler supports two modes of hazard recognition. The first is the |
| 1921 | /// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that |
| 1922 | /// supports highly complicated in-order reservation tables |
| 1923 | /// (ScoreboardHazardRecognizer) and arbitraty target-specific logic. |
| 1924 | /// |
| 1925 | /// The second is a streamlined mechanism that checks for hazards based on |
| 1926 | /// simple counters that the scheduler itself maintains. It explicitly checks |
| 1927 | /// for instruction dispatch limitations, including the number of micro-ops that |
| 1928 | /// can dispatch per cycle. |
| 1929 | /// |
| 1930 | /// TODO: Also check whether the SU must start a new group. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1931 | bool GenericScheduler::SchedBoundary::checkHazard(SUnit *SU) { |
Andrew Trick | 8c9e672 | 2012-06-29 03:23:24 +0000 | [diff] [blame] | 1932 | if (HazardRec->isEnabled()) |
| 1933 | return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard; |
| 1934 | |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 1935 | unsigned uops = SchedModel->getNumMicroOps(SU->getInstr()); |
Andrew Trick | e2ff575 | 2013-06-15 04:49:49 +0000 | [diff] [blame] | 1936 | if ((CurrMOps > 0) && (CurrMOps + uops > SchedModel->getIssueWidth())) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1937 | DEBUG(dbgs() << " SU(" << SU->NodeNum << ") uops=" |
| 1938 | << SchedModel->getNumMicroOps(SU->getInstr()) << '\n'); |
Andrew Trick | 8c9e672 | 2012-06-29 03:23:24 +0000 | [diff] [blame] | 1939 | return true; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1940 | } |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 1941 | if (SchedModel->hasInstrSchedModel() && SU->hasReservedResource) { |
| 1942 | const MCSchedClassDesc *SC = DAG->getSchedClass(SU); |
| 1943 | for (TargetSchedModel::ProcResIter |
| 1944 | PI = SchedModel->getWriteProcResBegin(SC), |
| 1945 | PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) { |
| 1946 | if (getNextResourceCycle(PI->ProcResourceIdx, PI->Cycles) > CurrCycle) |
| 1947 | return true; |
| 1948 | } |
| 1949 | } |
Andrew Trick | 8c9e672 | 2012-06-29 03:23:24 +0000 | [diff] [blame] | 1950 | return false; |
| 1951 | } |
| 1952 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1953 | // Find the unscheduled node in ReadySUs with the highest latency. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1954 | unsigned GenericScheduler::SchedBoundary:: |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1955 | findMaxLatency(ArrayRef<SUnit*> ReadySUs) { |
| 1956 | SUnit *LateSU = 0; |
| 1957 | unsigned RemLatency = 0; |
| 1958 | for (ArrayRef<SUnit*>::iterator I = ReadySUs.begin(), E = ReadySUs.end(); |
Andrew Trick | d6d5ad3 | 2012-12-18 20:52:56 +0000 | [diff] [blame] | 1959 | I != E; ++I) { |
| 1960 | unsigned L = getUnscheduledLatency(*I); |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 1961 | if (L > RemLatency) { |
Andrew Trick | d6d5ad3 | 2012-12-18 20:52:56 +0000 | [diff] [blame] | 1962 | RemLatency = L; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1963 | LateSU = *I; |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 1964 | } |
Andrew Trick | d6d5ad3 | 2012-12-18 20:52:56 +0000 | [diff] [blame] | 1965 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1966 | if (LateSU) { |
| 1967 | DEBUG(dbgs() << Available.getName() << " RemLatency SU(" |
| 1968 | << LateSU->NodeNum << ") " << RemLatency << "c\n"); |
Andrew Trick | d6d5ad3 | 2012-12-18 20:52:56 +0000 | [diff] [blame] | 1969 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1970 | return RemLatency; |
| 1971 | } |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 1972 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1973 | // Count resources in this zone and the remaining unscheduled |
| 1974 | // instruction. Return the max count, scaled. Set OtherCritIdx to the critical |
| 1975 | // resource index, or zero if the zone is issue limited. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 1976 | unsigned GenericScheduler::SchedBoundary:: |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1977 | getOtherResourceCount(unsigned &OtherCritIdx) { |
Alexey Samsonov | 64c391d | 2013-07-19 08:55:18 +0000 | [diff] [blame] | 1978 | OtherCritIdx = 0; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1979 | if (!SchedModel->hasInstrSchedModel()) |
| 1980 | return 0; |
| 1981 | |
| 1982 | unsigned OtherCritCount = Rem->RemIssueCount |
| 1983 | + (RetiredMOps * SchedModel->getMicroOpFactor()); |
| 1984 | DEBUG(dbgs() << " " << Available.getName() << " + Remain MOps: " |
| 1985 | << OtherCritCount / SchedModel->getMicroOpFactor() << '\n'); |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1986 | for (unsigned PIdx = 1, PEnd = SchedModel->getNumProcResourceKinds(); |
| 1987 | PIdx != PEnd; ++PIdx) { |
| 1988 | unsigned OtherCount = getResourceCount(PIdx) + Rem->RemainingCounts[PIdx]; |
| 1989 | if (OtherCount > OtherCritCount) { |
| 1990 | OtherCritCount = OtherCount; |
| 1991 | OtherCritIdx = PIdx; |
| 1992 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 1993 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 1994 | if (OtherCritIdx) { |
| 1995 | DEBUG(dbgs() << " " << Available.getName() << " + Remain CritRes: " |
| 1996 | << OtherCritCount / SchedModel->getResourceFactor(OtherCritIdx) |
| 1997 | << " " << getResourceName(OtherCritIdx) << "\n"); |
| 1998 | } |
| 1999 | return OtherCritCount; |
| 2000 | } |
| 2001 | |
| 2002 | /// Set the CandPolicy for this zone given the current resources and latencies |
| 2003 | /// inside and outside the zone. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2004 | void GenericScheduler::SchedBoundary::setPolicy(CandPolicy &Policy, |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2005 | SchedBoundary &OtherZone) { |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 2006 | // Apply preemptive heuristics based on the the total latency and resources |
| 2007 | // inside and outside this zone. Potential stalls should be considered before |
| 2008 | // following this policy. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Compute remaining latency. We need this both to determine whether the |
| 2011 | // overall schedule has become latency-limited and whether the instructions |
| 2012 | // outside this zone are resource or latency limited. |
| 2013 | // |
| 2014 | // The "dependent" latency is updated incrementally during scheduling as the |
| 2015 | // max height/depth of scheduled nodes minus the cycles since it was |
| 2016 | // scheduled: |
| 2017 | // DLat = max (N.depth - (CurrCycle - N.ReadyCycle) for N in Zone |
| 2018 | // |
| 2019 | // The "independent" latency is the max ready queue depth: |
| 2020 | // ILat = max N.depth for N in Available|Pending |
| 2021 | // |
| 2022 | // RemainingLatency is the greater of independent and dependent latency. |
| 2023 | unsigned RemLatency = DependentLatency; |
| 2024 | RemLatency = std::max(RemLatency, findMaxLatency(Available.elements())); |
| 2025 | RemLatency = std::max(RemLatency, findMaxLatency(Pending.elements())); |
| 2026 | |
| 2027 | // Compute the critical resource outside the zone. |
| 2028 | unsigned OtherCritIdx; |
| 2029 | unsigned OtherCount = OtherZone.getOtherResourceCount(OtherCritIdx); |
| 2030 | |
| 2031 | bool OtherResLimited = false; |
| 2032 | if (SchedModel->hasInstrSchedModel()) { |
| 2033 | unsigned LFactor = SchedModel->getLatencyFactor(); |
| 2034 | OtherResLimited = (int)(OtherCount - (RemLatency * LFactor)) > (int)LFactor; |
| 2035 | } |
| 2036 | if (!OtherResLimited && (RemLatency + CurrCycle > Rem->CriticalPath)) { |
| 2037 | Policy.ReduceLatency |= true; |
| 2038 | DEBUG(dbgs() << " " << Available.getName() << " RemainingLatency " |
| 2039 | << RemLatency << " + " << CurrCycle << "c > CritPath " |
| 2040 | << Rem->CriticalPath << "\n"); |
| 2041 | } |
| 2042 | // If the same resource is limiting inside and outside the zone, do nothing. |
Andrew Trick | b13ef17 | 2013-07-19 00:20:07 +0000 | [diff] [blame] | 2043 | if (ZoneCritResIdx == OtherCritIdx) |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2044 | return; |
| 2045 | |
| 2046 | DEBUG( |
| 2047 | if (IsResourceLimited) { |
| 2048 | dbgs() << " " << Available.getName() << " ResourceLimited: " |
| 2049 | << getResourceName(ZoneCritResIdx) << "\n"; |
| 2050 | } |
| 2051 | if (OtherResLimited) |
Andrew Trick | b55db58 | 2013-06-21 18:33:01 +0000 | [diff] [blame] | 2052 | dbgs() << " RemainingLimit: " << getResourceName(OtherCritIdx) << "\n"; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2053 | if (!IsResourceLimited && !OtherResLimited) |
| 2054 | dbgs() << " Latency limited both directions.\n"); |
| 2055 | |
| 2056 | if (IsResourceLimited && !Policy.ReduceResIdx) |
| 2057 | Policy.ReduceResIdx = ZoneCritResIdx; |
| 2058 | |
| 2059 | if (OtherResLimited) |
| 2060 | Policy.DemandResIdx = OtherCritIdx; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2063 | void GenericScheduler::SchedBoundary::releaseNode(SUnit *SU, |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2064 | unsigned ReadyCycle) { |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2065 | if (ReadyCycle < MinReadyCycle) |
| 2066 | MinReadyCycle = ReadyCycle; |
| 2067 | |
| 2068 | // Check for interlocks first. For the purpose of other heuristics, an |
| 2069 | // instruction that cannot issue appears as if it's not in the ReadyQueue. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2070 | bool IsBuffered = SchedModel->getMicroOpBufferSize() != 0; |
| 2071 | if ((!IsBuffered && ReadyCycle > CurrCycle) || checkHazard(SU)) |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2072 | Pending.push(SU); |
| 2073 | else |
| 2074 | Available.push(SU); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2075 | |
| 2076 | // Record this node as an immediate dependent of the scheduled node. |
| 2077 | NextSUs.insert(SU); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /// Move the boundary of scheduled code by one cycle. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2081 | void GenericScheduler::SchedBoundary::bumpCycle(unsigned NextCycle) { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2082 | if (SchedModel->getMicroOpBufferSize() == 0) { |
| 2083 | assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized"); |
| 2084 | if (MinReadyCycle > NextCycle) |
| 2085 | NextCycle = MinReadyCycle; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2086 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2087 | // Update the current micro-ops, which will issue in the next cycle. |
| 2088 | unsigned DecMOps = SchedModel->getIssueWidth() * (NextCycle - CurrCycle); |
| 2089 | CurrMOps = (CurrMOps <= DecMOps) ? 0 : CurrMOps - DecMOps; |
| 2090 | |
| 2091 | // Decrement DependentLatency based on the next cycle. |
Andrew Trick | f5b8ef2 | 2013-06-15 04:49:44 +0000 | [diff] [blame] | 2092 | if ((NextCycle - CurrCycle) > DependentLatency) |
| 2093 | DependentLatency = 0; |
| 2094 | else |
| 2095 | DependentLatency -= (NextCycle - CurrCycle); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2096 | |
| 2097 | if (!HazardRec->isEnabled()) { |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2098 | // Bypass HazardRec virtual calls. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2099 | CurrCycle = NextCycle; |
| 2100 | } |
| 2101 | else { |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2102 | // Bypass getHazardType calls in case of long latency. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2103 | for (; CurrCycle != NextCycle; ++CurrCycle) { |
| 2104 | if (isTop()) |
| 2105 | HazardRec->AdvanceCycle(); |
| 2106 | else |
| 2107 | HazardRec->RecedeCycle(); |
| 2108 | } |
| 2109 | } |
| 2110 | CheckPending = true; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2111 | unsigned LFactor = SchedModel->getLatencyFactor(); |
| 2112 | IsResourceLimited = |
| 2113 | (int)(getCriticalCount() - (getScheduledLatency() * LFactor)) |
| 2114 | > (int)LFactor; |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2115 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2116 | DEBUG(dbgs() << "Cycle: " << CurrCycle << ' ' << Available.getName() << '\n'); |
| 2117 | } |
| 2118 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2119 | void GenericScheduler::SchedBoundary::incExecutedResources(unsigned PIdx, |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2120 | unsigned Count) { |
| 2121 | ExecutedResCounts[PIdx] += Count; |
| 2122 | if (ExecutedResCounts[PIdx] > MaxExecutedResCount) |
| 2123 | MaxExecutedResCount = ExecutedResCounts[PIdx]; |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2126 | /// Add the given processor resource to this scheduled zone. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2127 | /// |
| 2128 | /// \param Cycles indicates the number of consecutive (non-pipelined) cycles |
| 2129 | /// during which this resource is consumed. |
| 2130 | /// |
| 2131 | /// \return the next cycle at which the instruction may execute without |
| 2132 | /// oversubscribing resources. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2133 | unsigned GenericScheduler::SchedBoundary:: |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2134 | countResource(unsigned PIdx, unsigned Cycles, unsigned NextCycle) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2135 | unsigned Factor = SchedModel->getResourceFactor(PIdx); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2136 | unsigned Count = Factor * Cycles; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2137 | DEBUG(dbgs() << " " << getResourceName(PIdx) |
| 2138 | << " +" << Cycles << "x" << Factor << "u\n"); |
| 2139 | |
| 2140 | // Update Executed resources counts. |
| 2141 | incExecutedResources(PIdx, Count); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2142 | assert(Rem->RemainingCounts[PIdx] >= Count && "resource double counted"); |
| 2143 | Rem->RemainingCounts[PIdx] -= Count; |
| 2144 | |
Andrew Trick | b13ef17 | 2013-07-19 00:20:07 +0000 | [diff] [blame] | 2145 | // Check if this resource exceeds the current critical resource. If so, it |
| 2146 | // becomes the critical resource. |
| 2147 | if (ZoneCritResIdx != PIdx && (getResourceCount(PIdx) > getCriticalCount())) { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2148 | ZoneCritResIdx = PIdx; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2149 | DEBUG(dbgs() << " *** Critical resource " |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2150 | << getResourceName(PIdx) << ": " |
| 2151 | << getResourceCount(PIdx) / SchedModel->getLatencyFactor() << "c\n"); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2152 | } |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2153 | // For reserved resources, record the highest cycle using the resource. |
| 2154 | unsigned NextAvailable = getNextResourceCycle(PIdx, Cycles); |
| 2155 | if (NextAvailable > CurrCycle) { |
| 2156 | DEBUG(dbgs() << " Resource conflict: " |
| 2157 | << SchedModel->getProcResource(PIdx)->Name << " reserved until @" |
| 2158 | << NextAvailable << "\n"); |
| 2159 | } |
| 2160 | return NextAvailable; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2163 | /// Move the boundary of scheduled code by one SUnit. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2164 | void GenericScheduler::SchedBoundary::bumpNode(SUnit *SU) { |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2165 | // Update the reservation table. |
| 2166 | if (HazardRec->isEnabled()) { |
| 2167 | if (!isTop() && SU->isCall) { |
| 2168 | // Calls are scheduled with their preceding instructions. For bottom-up |
| 2169 | // scheduling, clear the pipeline state before emitting. |
| 2170 | HazardRec->Reset(); |
| 2171 | } |
| 2172 | HazardRec->EmitInstruction(SU); |
| 2173 | } |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2174 | // checkHazard should prevent scheduling multiple instructions per cycle that |
| 2175 | // exceed the issue width. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2176 | const MCSchedClassDesc *SC = DAG->getSchedClass(SU); |
| 2177 | unsigned IncMOps = SchedModel->getNumMicroOps(SU->getInstr()); |
Daniel Jasper | 0d92abd | 2013-12-06 08:58:22 +0000 | [diff] [blame] | 2178 | assert( |
| 2179 | (CurrMOps == 0 || (CurrMOps + IncMOps) <= SchedModel->getIssueWidth()) && |
Andrew Trick | f7760a2 | 2013-12-06 17:19:20 +0000 | [diff] [blame^] | 2180 | "Cannot schedule this instruction's MicroOps in the current cycle."); |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2181 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2182 | unsigned ReadyCycle = (isTop() ? SU->TopReadyCycle : SU->BotReadyCycle); |
| 2183 | DEBUG(dbgs() << " Ready @" << ReadyCycle << "c\n"); |
| 2184 | |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2185 | unsigned NextCycle = CurrCycle; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2186 | switch (SchedModel->getMicroOpBufferSize()) { |
| 2187 | case 0: |
| 2188 | assert(ReadyCycle <= CurrCycle && "Broken PendingQueue"); |
| 2189 | break; |
| 2190 | case 1: |
| 2191 | if (ReadyCycle > NextCycle) { |
| 2192 | NextCycle = ReadyCycle; |
| 2193 | DEBUG(dbgs() << " *** Stall until: " << ReadyCycle << "\n"); |
| 2194 | } |
| 2195 | break; |
| 2196 | default: |
| 2197 | // We don't currently model the OOO reorder buffer, so consider all |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 2198 | // scheduled MOps to be "retired". We do loosely model in-order resource |
| 2199 | // latency. If this instruction uses an in-order resource, account for any |
| 2200 | // likely stall cycles. |
| 2201 | if (SU->isUnbuffered && ReadyCycle > NextCycle) |
| 2202 | NextCycle = ReadyCycle; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2203 | break; |
| 2204 | } |
| 2205 | RetiredMOps += IncMOps; |
| 2206 | |
| 2207 | // Update resource counts and critical resource. |
| 2208 | if (SchedModel->hasInstrSchedModel()) { |
| 2209 | unsigned DecRemIssue = IncMOps * SchedModel->getMicroOpFactor(); |
| 2210 | assert(Rem->RemIssueCount >= DecRemIssue && "MOps double counted"); |
| 2211 | Rem->RemIssueCount -= DecRemIssue; |
| 2212 | if (ZoneCritResIdx) { |
| 2213 | // Scale scheduled micro-ops for comparing with the critical resource. |
| 2214 | unsigned ScaledMOps = |
| 2215 | RetiredMOps * SchedModel->getMicroOpFactor(); |
| 2216 | |
| 2217 | // If scaled micro-ops are now more than the previous critical resource by |
| 2218 | // a full cycle, then micro-ops issue becomes critical. |
| 2219 | if ((int)(ScaledMOps - getResourceCount(ZoneCritResIdx)) |
| 2220 | >= (int)SchedModel->getLatencyFactor()) { |
| 2221 | ZoneCritResIdx = 0; |
| 2222 | DEBUG(dbgs() << " *** Critical resource NumMicroOps: " |
| 2223 | << ScaledMOps / SchedModel->getLatencyFactor() << "c\n"); |
| 2224 | } |
| 2225 | } |
| 2226 | for (TargetSchedModel::ProcResIter |
| 2227 | PI = SchedModel->getWriteProcResBegin(SC), |
| 2228 | PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) { |
| 2229 | unsigned RCycle = |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2230 | countResource(PI->ProcResourceIdx, PI->Cycles, NextCycle); |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2231 | if (RCycle > NextCycle) |
| 2232 | NextCycle = RCycle; |
| 2233 | } |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2234 | if (SU->hasReservedResource) { |
| 2235 | // For reserved resources, record the highest cycle using the resource. |
| 2236 | // For top-down scheduling, this is the cycle in which we schedule this |
| 2237 | // instruction plus the number of cycles the operations reserves the |
| 2238 | // resource. For bottom-up is it simply the instruction's cycle. |
| 2239 | for (TargetSchedModel::ProcResIter |
| 2240 | PI = SchedModel->getWriteProcResBegin(SC), |
| 2241 | PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) { |
| 2242 | unsigned PIdx = PI->ProcResourceIdx; |
| 2243 | if (SchedModel->getProcResource(PIdx)->BufferSize == 0) |
| 2244 | ReservedCycles[PIdx] = isTop() ? NextCycle + PI->Cycles : NextCycle; |
| 2245 | } |
| 2246 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2247 | } |
| 2248 | // Update ExpectedLatency and DependentLatency. |
| 2249 | unsigned &TopLatency = isTop() ? ExpectedLatency : DependentLatency; |
| 2250 | unsigned &BotLatency = isTop() ? DependentLatency : ExpectedLatency; |
| 2251 | if (SU->getDepth() > TopLatency) { |
| 2252 | TopLatency = SU->getDepth(); |
| 2253 | DEBUG(dbgs() << " " << Available.getName() |
| 2254 | << " TopLatency SU(" << SU->NodeNum << ") " << TopLatency << "c\n"); |
| 2255 | } |
| 2256 | if (SU->getHeight() > BotLatency) { |
| 2257 | BotLatency = SU->getHeight(); |
| 2258 | DEBUG(dbgs() << " " << Available.getName() |
| 2259 | << " BotLatency SU(" << SU->NodeNum << ") " << BotLatency << "c\n"); |
| 2260 | } |
| 2261 | // If we stall for any reason, bump the cycle. |
| 2262 | if (NextCycle > CurrCycle) { |
| 2263 | bumpCycle(NextCycle); |
| 2264 | } |
| 2265 | else { |
| 2266 | // After updating ZoneCritResIdx and ExpectedLatency, check if we're |
| 2267 | // resource limited. If a stall occured, bumpCycle does this. |
| 2268 | unsigned LFactor = SchedModel->getLatencyFactor(); |
| 2269 | IsResourceLimited = |
| 2270 | (int)(getCriticalCount() - (getScheduledLatency() * LFactor)) |
| 2271 | > (int)LFactor; |
| 2272 | } |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 2273 | // Update CurrMOps after calling bumpCycle to handle stalls, since bumpCycle |
| 2274 | // resets CurrMOps. Loop to handle instructions with more MOps than issue in |
| 2275 | // one cycle. Since we commonly reach the max MOps here, opportunistically |
| 2276 | // bump the cycle to avoid uselessly checking everything in the readyQ. |
| 2277 | CurrMOps += IncMOps; |
| 2278 | while (CurrMOps >= SchedModel->getIssueWidth()) { |
| 2279 | bumpCycle(++NextCycle); |
| 2280 | DEBUG(dbgs() << " *** Max MOps " << CurrMOps |
| 2281 | << " at cycle " << CurrCycle << '\n'); |
| 2282 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2283 | DEBUG(dumpScheduledState()); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2286 | /// Release pending ready nodes in to the available queue. This makes them |
| 2287 | /// visible to heuristics. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2288 | void GenericScheduler::SchedBoundary::releasePending() { |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2289 | // If the available queue is empty, it is safe to reset MinReadyCycle. |
| 2290 | if (Available.empty()) |
| 2291 | MinReadyCycle = UINT_MAX; |
| 2292 | |
| 2293 | // Check to see if any of the pending instructions are ready to issue. If |
| 2294 | // so, add them to the available queue. |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2295 | bool IsBuffered = SchedModel->getMicroOpBufferSize() != 0; |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2296 | for (unsigned i = 0, e = Pending.size(); i != e; ++i) { |
| 2297 | SUnit *SU = *(Pending.begin()+i); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2298 | unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle; |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2299 | |
| 2300 | if (ReadyCycle < MinReadyCycle) |
| 2301 | MinReadyCycle = ReadyCycle; |
| 2302 | |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2303 | if (!IsBuffered && ReadyCycle > CurrCycle) |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2304 | continue; |
| 2305 | |
Andrew Trick | 8c9e672 | 2012-06-29 03:23:24 +0000 | [diff] [blame] | 2306 | if (checkHazard(SU)) |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2307 | continue; |
| 2308 | |
| 2309 | Available.push(SU); |
| 2310 | Pending.remove(Pending.begin()+i); |
| 2311 | --i; --e; |
| 2312 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2313 | DEBUG(if (!Pending.empty()) Pending.dump()); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2314 | CheckPending = false; |
| 2315 | } |
| 2316 | |
| 2317 | /// Remove SU from the ready set for this boundary. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2318 | void GenericScheduler::SchedBoundary::removeReady(SUnit *SU) { |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2319 | if (Available.isInQueue(SU)) |
| 2320 | Available.remove(Available.find(SU)); |
| 2321 | else { |
| 2322 | assert(Pending.isInQueue(SU) && "bad ready count"); |
| 2323 | Pending.remove(Pending.find(SU)); |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | /// If this queue only has one ready candidate, return it. As a side effect, |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2328 | /// defer any nodes that now hit a hazard, and advance the cycle until at least |
| 2329 | /// one node is ready. If multiple instructions are ready, return NULL. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2330 | SUnit *GenericScheduler::SchedBoundary::pickOnlyChoice() { |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2331 | if (CheckPending) |
| 2332 | releasePending(); |
| 2333 | |
Andrew Trick | e2ff575 | 2013-06-15 04:49:49 +0000 | [diff] [blame] | 2334 | if (CurrMOps > 0) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2335 | // Defer any ready instrs that now have a hazard. |
| 2336 | for (ReadyQueue::iterator I = Available.begin(); I != Available.end();) { |
| 2337 | if (checkHazard(*I)) { |
| 2338 | Pending.push(*I); |
| 2339 | I = Available.remove(I); |
| 2340 | continue; |
| 2341 | } |
| 2342 | ++I; |
| 2343 | } |
| 2344 | } |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2345 | for (unsigned i = 0; Available.empty(); ++i) { |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 2346 | assert(i <= (HazardRec->getMaxLookAhead() + MaxObservedLatency) && |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2347 | "permanent hazard"); (void)i; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2348 | bumpCycle(CurrCycle + 1); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2349 | releasePending(); |
| 2350 | } |
| 2351 | if (Available.size() == 1) |
| 2352 | return *Available.begin(); |
| 2353 | return NULL; |
| 2354 | } |
| 2355 | |
Andrew Trick | 8e8415f | 2013-06-15 05:46:47 +0000 | [diff] [blame] | 2356 | #ifndef NDEBUG |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2357 | // This is useful information to dump after bumpNode. |
| 2358 | // Note that the Queue contents are more useful before pickNodeFromQueue. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2359 | void GenericScheduler::SchedBoundary::dumpScheduledState() { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2360 | unsigned ResFactor; |
| 2361 | unsigned ResCount; |
| 2362 | if (ZoneCritResIdx) { |
| 2363 | ResFactor = SchedModel->getResourceFactor(ZoneCritResIdx); |
| 2364 | ResCount = getResourceCount(ZoneCritResIdx); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2365 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2366 | else { |
| 2367 | ResFactor = SchedModel->getMicroOpFactor(); |
| 2368 | ResCount = RetiredMOps * SchedModel->getMicroOpFactor(); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2369 | } |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2370 | unsigned LFactor = SchedModel->getLatencyFactor(); |
| 2371 | dbgs() << Available.getName() << " @" << CurrCycle << "c\n" |
| 2372 | << " Retired: " << RetiredMOps; |
| 2373 | dbgs() << "\n Executed: " << getExecutedCount() / LFactor << "c"; |
| 2374 | dbgs() << "\n Critical: " << ResCount / LFactor << "c, " |
| 2375 | << ResCount / ResFactor << " " << getResourceName(ZoneCritResIdx) |
| 2376 | << "\n ExpectedLatency: " << ExpectedLatency << "c\n" |
| 2377 | << (IsResourceLimited ? " - Resource" : " - Latency") |
| 2378 | << " limited.\n"; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2379 | } |
Andrew Trick | 8e8415f | 2013-06-15 05:46:47 +0000 | [diff] [blame] | 2380 | #endif |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2381 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2382 | void GenericScheduler::SchedCandidate:: |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2383 | initResourceDelta(const ScheduleDAGMI *DAG, |
| 2384 | const TargetSchedModel *SchedModel) { |
| 2385 | if (!Policy.ReduceResIdx && !Policy.DemandResIdx) |
| 2386 | return; |
| 2387 | |
| 2388 | const MCSchedClassDesc *SC = DAG->getSchedClass(SU); |
| 2389 | for (TargetSchedModel::ProcResIter |
| 2390 | PI = SchedModel->getWriteProcResBegin(SC), |
| 2391 | PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) { |
| 2392 | if (PI->ProcResourceIdx == Policy.ReduceResIdx) |
| 2393 | ResDelta.CritResources += PI->Cycles; |
| 2394 | if (PI->ProcResourceIdx == Policy.DemandResIdx) |
| 2395 | ResDelta.DemandedResources += PI->Cycles; |
| 2396 | } |
| 2397 | } |
| 2398 | |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2399 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2400 | /// Return true if this heuristic determines order. |
Andrew Trick | 80e66ce | 2013-04-05 00:31:34 +0000 | [diff] [blame] | 2401 | static bool tryLess(int TryVal, int CandVal, |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2402 | GenericScheduler::SchedCandidate &TryCand, |
| 2403 | GenericScheduler::SchedCandidate &Cand, |
| 2404 | GenericScheduler::CandReason Reason) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2405 | if (TryVal < CandVal) { |
| 2406 | TryCand.Reason = Reason; |
| 2407 | return true; |
| 2408 | } |
| 2409 | if (TryVal > CandVal) { |
| 2410 | if (Cand.Reason > Reason) |
| 2411 | Cand.Reason = Reason; |
| 2412 | return true; |
| 2413 | } |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2414 | Cand.setRepeat(Reason); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2415 | return false; |
| 2416 | } |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2417 | |
Andrew Trick | 80e66ce | 2013-04-05 00:31:34 +0000 | [diff] [blame] | 2418 | static bool tryGreater(int TryVal, int CandVal, |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2419 | GenericScheduler::SchedCandidate &TryCand, |
| 2420 | GenericScheduler::SchedCandidate &Cand, |
| 2421 | GenericScheduler::CandReason Reason) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2422 | if (TryVal > CandVal) { |
| 2423 | TryCand.Reason = Reason; |
| 2424 | return true; |
| 2425 | } |
| 2426 | if (TryVal < CandVal) { |
| 2427 | if (Cand.Reason > Reason) |
| 2428 | Cand.Reason = Reason; |
| 2429 | return true; |
| 2430 | } |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2431 | Cand.setRepeat(Reason); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2432 | return false; |
| 2433 | } |
| 2434 | |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2435 | static bool tryPressure(const PressureChange &TryP, |
| 2436 | const PressureChange &CandP, |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2437 | GenericScheduler::SchedCandidate &TryCand, |
| 2438 | GenericScheduler::SchedCandidate &Cand, |
| 2439 | GenericScheduler::CandReason Reason) { |
Andrew Trick | b1a45b6 | 2013-08-30 04:27:29 +0000 | [diff] [blame] | 2440 | int TryRank = TryP.getPSetOrMax(); |
| 2441 | int CandRank = CandP.getPSetOrMax(); |
| 2442 | // If both candidates affect the same set, go with the smallest increase. |
| 2443 | if (TryRank == CandRank) { |
| 2444 | return tryLess(TryP.getUnitInc(), CandP.getUnitInc(), TryCand, Cand, |
| 2445 | Reason); |
Andrew Trick | 401b695 | 2013-07-25 07:26:35 +0000 | [diff] [blame] | 2446 | } |
Andrew Trick | b1a45b6 | 2013-08-30 04:27:29 +0000 | [diff] [blame] | 2447 | // If one candidate decreases and the other increases, go with it. |
| 2448 | // Invalid candidates have UnitInc==0. |
| 2449 | if (tryLess(TryP.getUnitInc() < 0, CandP.getUnitInc() < 0, TryCand, Cand, |
| 2450 | Reason)) { |
| 2451 | return true; |
| 2452 | } |
Andrew Trick | 401b695 | 2013-07-25 07:26:35 +0000 | [diff] [blame] | 2453 | // If the candidates are decreasing pressure, reverse priority. |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2454 | if (TryP.getUnitInc() < 0) |
Andrew Trick | 401b695 | 2013-07-25 07:26:35 +0000 | [diff] [blame] | 2455 | std::swap(TryRank, CandRank); |
| 2456 | return tryGreater(TryRank, CandRank, TryCand, Cand, Reason); |
| 2457 | } |
| 2458 | |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2459 | static unsigned getWeakLeft(const SUnit *SU, bool isTop) { |
| 2460 | return (isTop) ? SU->WeakPredsLeft : SU->WeakSuccsLeft; |
| 2461 | } |
| 2462 | |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2463 | /// Minimize physical register live ranges. Regalloc wants them adjacent to |
| 2464 | /// their physreg def/use. |
| 2465 | /// |
| 2466 | /// FIXME: This is an unnecessary check on the critical path. Most are root/leaf |
| 2467 | /// copies which can be prescheduled. The rest (e.g. x86 MUL) could be bundled |
| 2468 | /// with the operation that produces or consumes the physreg. We'll do this when |
| 2469 | /// regalloc has support for parallel copies. |
| 2470 | static int biasPhysRegCopy(const SUnit *SU, bool isTop) { |
| 2471 | const MachineInstr *MI = SU->getInstr(); |
| 2472 | if (!MI->isCopy()) |
| 2473 | return 0; |
| 2474 | |
| 2475 | unsigned ScheduledOper = isTop ? 1 : 0; |
| 2476 | unsigned UnscheduledOper = isTop ? 0 : 1; |
| 2477 | // If we have already scheduled the physreg produce/consumer, immediately |
| 2478 | // schedule the copy. |
| 2479 | if (TargetRegisterInfo::isPhysicalRegister( |
| 2480 | MI->getOperand(ScheduledOper).getReg())) |
| 2481 | return 1; |
| 2482 | // If the physreg is at the boundary, defer it. Otherwise schedule it |
| 2483 | // immediately to free the dependent. We can hoist the copy later. |
| 2484 | bool AtBoundary = isTop ? !SU->NumSuccsLeft : !SU->NumPredsLeft; |
| 2485 | if (TargetRegisterInfo::isPhysicalRegister( |
| 2486 | MI->getOperand(UnscheduledOper).getReg())) |
| 2487 | return AtBoundary ? -1 : 1; |
| 2488 | return 0; |
| 2489 | } |
| 2490 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2491 | static bool tryLatency(GenericScheduler::SchedCandidate &TryCand, |
| 2492 | GenericScheduler::SchedCandidate &Cand, |
| 2493 | GenericScheduler::SchedBoundary &Zone) { |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 2494 | if (Zone.isTop()) { |
| 2495 | if (Cand.SU->getDepth() > Zone.getScheduledLatency()) { |
| 2496 | if (tryLess(TryCand.SU->getDepth(), Cand.SU->getDepth(), |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2497 | TryCand, Cand, GenericScheduler::TopDepthReduce)) |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 2498 | return true; |
| 2499 | } |
| 2500 | if (tryGreater(TryCand.SU->getHeight(), Cand.SU->getHeight(), |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2501 | TryCand, Cand, GenericScheduler::TopPathReduce)) |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 2502 | return true; |
| 2503 | } |
| 2504 | else { |
| 2505 | if (Cand.SU->getHeight() > Zone.getScheduledLatency()) { |
| 2506 | if (tryLess(TryCand.SU->getHeight(), Cand.SU->getHeight(), |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2507 | TryCand, Cand, GenericScheduler::BotHeightReduce)) |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 2508 | return true; |
| 2509 | } |
| 2510 | if (tryGreater(TryCand.SU->getDepth(), Cand.SU->getDepth(), |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2511 | TryCand, Cand, GenericScheduler::BotPathReduce)) |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 2512 | return true; |
| 2513 | } |
| 2514 | return false; |
| 2515 | } |
| 2516 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2517 | /// Apply a set of heursitics to a new candidate. Heuristics are currently |
| 2518 | /// hierarchical. This may be more efficient than a graduated cost model because |
| 2519 | /// we don't need to evaluate all aspects of the model for each node in the |
| 2520 | /// queue. But it's really done to make the heuristics easier to debug and |
| 2521 | /// statistically analyze. |
| 2522 | /// |
| 2523 | /// \param Cand provides the policy and current best candidate. |
| 2524 | /// \param TryCand refers to the next SUnit candidate, otherwise uninitialized. |
| 2525 | /// \param Zone describes the scheduled zone that we are extending. |
| 2526 | /// \param RPTracker describes reg pressure within the scheduled zone. |
| 2527 | /// \param TempTracker is a scratch pressure tracker to reuse in queries. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2528 | void GenericScheduler::tryCandidate(SchedCandidate &Cand, |
Andrew Trick | bb1247b | 2013-12-05 17:55:47 +0000 | [diff] [blame] | 2529 | SchedCandidate &TryCand, |
| 2530 | SchedBoundary &Zone, |
| 2531 | const RegPressureTracker &RPTracker, |
| 2532 | RegPressureTracker &TempTracker) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2533 | |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 2534 | if (DAG->isTrackingPressure()) { |
Andrew Trick | 310190e | 2013-09-04 21:00:02 +0000 | [diff] [blame] | 2535 | // Always initialize TryCand's RPDelta. |
| 2536 | if (Zone.isTop()) { |
| 2537 | TempTracker.getMaxDownwardPressureDelta( |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2538 | TryCand.SU->getInstr(), |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2539 | TryCand.RPDelta, |
| 2540 | DAG->getRegionCriticalPSets(), |
| 2541 | DAG->getRegPressure().MaxSetPressure); |
| 2542 | } |
| 2543 | else { |
Andrew Trick | 310190e | 2013-09-04 21:00:02 +0000 | [diff] [blame] | 2544 | if (VerifyScheduling) { |
| 2545 | TempTracker.getMaxUpwardPressureDelta( |
| 2546 | TryCand.SU->getInstr(), |
| 2547 | &DAG->getPressureDiff(TryCand.SU), |
| 2548 | TryCand.RPDelta, |
| 2549 | DAG->getRegionCriticalPSets(), |
| 2550 | DAG->getRegPressure().MaxSetPressure); |
| 2551 | } |
| 2552 | else { |
| 2553 | RPTracker.getUpwardPressureDelta( |
| 2554 | TryCand.SU->getInstr(), |
| 2555 | DAG->getPressureDiff(TryCand.SU), |
| 2556 | TryCand.RPDelta, |
| 2557 | DAG->getRegionCriticalPSets(), |
| 2558 | DAG->getRegPressure().MaxSetPressure); |
| 2559 | } |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
Andrew Trick | c573cd9 | 2013-09-06 17:32:44 +0000 | [diff] [blame] | 2562 | DEBUG(if (TryCand.RPDelta.Excess.isValid()) |
| 2563 | dbgs() << " SU(" << TryCand.SU->NodeNum << ") " |
| 2564 | << TRI->getRegPressureSetName(TryCand.RPDelta.Excess.getPSet()) |
| 2565 | << ":" << TryCand.RPDelta.Excess.getUnitInc() << "\n"); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2566 | |
| 2567 | // Initialize the candidate if needed. |
| 2568 | if (!Cand.isValid()) { |
| 2569 | TryCand.Reason = NodeOrder; |
| 2570 | return; |
| 2571 | } |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2572 | |
| 2573 | if (tryGreater(biasPhysRegCopy(TryCand.SU, Zone.isTop()), |
| 2574 | biasPhysRegCopy(Cand.SU, Zone.isTop()), |
| 2575 | TryCand, Cand, PhysRegCopy)) |
| 2576 | return; |
| 2577 | |
Andrew Trick | 401b695 | 2013-07-25 07:26:35 +0000 | [diff] [blame] | 2578 | // Avoid exceeding the target's limit. If signed PSetID is negative, it is |
| 2579 | // invalid; convert it to INT_MAX to give it lowest priority. |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 2580 | if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.Excess, |
| 2581 | Cand.RPDelta.Excess, |
| 2582 | TryCand, Cand, RegExcess)) |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2583 | return; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2584 | |
| 2585 | // Avoid increasing the max critical pressure in the scheduled region. |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 2586 | if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CriticalMax, |
| 2587 | Cand.RPDelta.CriticalMax, |
| 2588 | TryCand, Cand, RegCritical)) |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2589 | return; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2590 | |
Andrew Trick | ddffae9 | 2013-09-06 17:32:36 +0000 | [diff] [blame] | 2591 | // For loops that are acyclic path limited, aggressively schedule for latency. |
Andrew Trick | e1f7bf2 | 2013-09-09 22:28:08 +0000 | [diff] [blame] | 2592 | // This can result in very long dependence chains scheduled in sequence, so |
| 2593 | // once every cycle (when CurrMOps == 0), switch to normal heuristics. |
| 2594 | if (Rem.IsAcyclicLatencyLimited && !Zone.CurrMOps |
| 2595 | && tryLatency(TryCand, Cand, Zone)) |
Andrew Trick | ddffae9 | 2013-09-06 17:32:36 +0000 | [diff] [blame] | 2596 | return; |
| 2597 | |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 2598 | // Prioritize instructions that read unbuffered resources by stall cycles. |
| 2599 | if (tryLess(Zone.getLatencyStallCycles(TryCand.SU), |
| 2600 | Zone.getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall)) |
| 2601 | return; |
| 2602 | |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2603 | // Keep clustered nodes together to encourage downstream peephole |
| 2604 | // optimizations which may reduce resource requirements. |
| 2605 | // |
| 2606 | // This is a best effort to set things up for a post-RA pass. Optimizations |
| 2607 | // like generating loads of multiple registers should ideally be done within |
| 2608 | // the scheduler pass by combining the loads during DAG postprocessing. |
| 2609 | const SUnit *NextClusterSU = |
| 2610 | Zone.isTop() ? DAG->getNextClusterSucc() : DAG->getNextClusterPred(); |
| 2611 | if (tryGreater(TryCand.SU == NextClusterSU, Cand.SU == NextClusterSU, |
| 2612 | TryCand, Cand, Cluster)) |
| 2613 | return; |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 2614 | |
| 2615 | // Weak edges are for clustering and other constraints. |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2616 | if (tryLess(getWeakLeft(TryCand.SU, Zone.isTop()), |
| 2617 | getWeakLeft(Cand.SU, Zone.isTop()), |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 2618 | TryCand, Cand, Weak)) { |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2619 | return; |
| 2620 | } |
Andrew Trick | 71f08a3 | 2013-06-17 21:45:13 +0000 | [diff] [blame] | 2621 | // Avoid increasing the max pressure of the entire region. |
Andrew Trick | 66c3dfb | 2013-09-04 21:00:11 +0000 | [diff] [blame] | 2622 | if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CurrentMax, |
| 2623 | Cand.RPDelta.CurrentMax, |
| 2624 | TryCand, Cand, RegMax)) |
Andrew Trick | 71f08a3 | 2013-06-17 21:45:13 +0000 | [diff] [blame] | 2625 | return; |
| 2626 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2627 | // Avoid critical resource consumption and balance the schedule. |
| 2628 | TryCand.initResourceDelta(DAG, SchedModel); |
| 2629 | if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources, |
| 2630 | TryCand, Cand, ResourceReduce)) |
| 2631 | return; |
| 2632 | if (tryGreater(TryCand.ResDelta.DemandedResources, |
| 2633 | Cand.ResDelta.DemandedResources, |
| 2634 | TryCand, Cand, ResourceDemand)) |
| 2635 | return; |
| 2636 | |
| 2637 | // Avoid serializing long latency dependence chains. |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 2638 | // For acyclic path limited loops, latency was already checked above. |
| 2639 | if (Cand.Policy.ReduceLatency && !Rem.IsAcyclicLatencyLimited |
| 2640 | && tryLatency(TryCand, Cand, Zone)) { |
| 2641 | return; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2644 | // Prefer immediate defs/users of the last scheduled instruction. This is a |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2645 | // local pressure avoidance strategy that also makes the machine code |
| 2646 | // readable. |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2647 | if (tryGreater(Zone.NextSUs.count(TryCand.SU), Zone.NextSUs.count(Cand.SU), |
| 2648 | TryCand, Cand, NextDefUse)) |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2649 | return; |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2650 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2651 | // Fall through to original instruction order. |
| 2652 | if ((Zone.isTop() && TryCand.SU->NodeNum < Cand.SU->NodeNum) |
| 2653 | || (!Zone.isTop() && TryCand.SU->NodeNum > Cand.SU->NodeNum)) { |
| 2654 | TryCand.Reason = NodeOrder; |
| 2655 | } |
| 2656 | } |
Andrew Trick | 419eae2 | 2012-05-10 21:06:19 +0000 | [diff] [blame] | 2657 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2658 | #ifndef NDEBUG |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2659 | const char *GenericScheduler::getReasonStr( |
| 2660 | GenericScheduler::CandReason Reason) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2661 | switch (Reason) { |
| 2662 | case NoCand: return "NOCAND "; |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2663 | case PhysRegCopy: return "PREG-COPY"; |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2664 | case RegExcess: return "REG-EXCESS"; |
| 2665 | case RegCritical: return "REG-CRIT "; |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 2666 | case Stall: return "STALL "; |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2667 | case Cluster: return "CLUSTER "; |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 2668 | case Weak: return "WEAK "; |
Andrew Trick | 71f08a3 | 2013-06-17 21:45:13 +0000 | [diff] [blame] | 2669 | case RegMax: return "REG-MAX "; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2670 | case ResourceReduce: return "RES-REDUCE"; |
| 2671 | case ResourceDemand: return "RES-DEMAND"; |
| 2672 | case TopDepthReduce: return "TOP-DEPTH "; |
| 2673 | case TopPathReduce: return "TOP-PATH "; |
| 2674 | case BotHeightReduce:return "BOT-HEIGHT"; |
| 2675 | case BotPathReduce: return "BOT-PATH "; |
| 2676 | case NextDefUse: return "DEF-USE "; |
| 2677 | case NodeOrder: return "ORDER "; |
| 2678 | }; |
Benjamin Kramer | c280f41 | 2012-11-09 15:45:22 +0000 | [diff] [blame] | 2679 | llvm_unreachable("Unknown reason!"); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2682 | void GenericScheduler::traceCandidate(const SchedCandidate &Cand) { |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2683 | PressureChange P; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2684 | unsigned ResIdx = 0; |
| 2685 | unsigned Latency = 0; |
| 2686 | switch (Cand.Reason) { |
| 2687 | default: |
| 2688 | break; |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2689 | case RegExcess: |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2690 | P = Cand.RPDelta.Excess; |
| 2691 | break; |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2692 | case RegCritical: |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2693 | P = Cand.RPDelta.CriticalMax; |
| 2694 | break; |
Andrew Trick | 71f08a3 | 2013-06-17 21:45:13 +0000 | [diff] [blame] | 2695 | case RegMax: |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2696 | P = Cand.RPDelta.CurrentMax; |
| 2697 | break; |
| 2698 | case ResourceReduce: |
| 2699 | ResIdx = Cand.Policy.ReduceResIdx; |
| 2700 | break; |
| 2701 | case ResourceDemand: |
| 2702 | ResIdx = Cand.Policy.DemandResIdx; |
| 2703 | break; |
| 2704 | case TopDepthReduce: |
| 2705 | Latency = Cand.SU->getDepth(); |
| 2706 | break; |
| 2707 | case TopPathReduce: |
| 2708 | Latency = Cand.SU->getHeight(); |
| 2709 | break; |
| 2710 | case BotHeightReduce: |
| 2711 | Latency = Cand.SU->getHeight(); |
| 2712 | break; |
| 2713 | case BotPathReduce: |
| 2714 | Latency = Cand.SU->getDepth(); |
| 2715 | break; |
| 2716 | } |
Andrew Trick | 419d491 | 2013-04-05 00:31:29 +0000 | [diff] [blame] | 2717 | dbgs() << " SU(" << Cand.SU->NodeNum << ") " << getReasonStr(Cand.Reason); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2718 | if (P.isValid()) |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 2719 | dbgs() << " " << TRI->getRegPressureSetName(P.getPSet()) |
| 2720 | << ":" << P.getUnitInc() << " "; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2721 | else |
Andrew Trick | 419d491 | 2013-04-05 00:31:29 +0000 | [diff] [blame] | 2722 | dbgs() << " "; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2723 | if (ResIdx) |
Andrew Trick | 419d491 | 2013-04-05 00:31:29 +0000 | [diff] [blame] | 2724 | dbgs() << " " << SchedModel->getProcResource(ResIdx)->Name << " "; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2725 | else |
| 2726 | dbgs() << " "; |
Andrew Trick | 419d491 | 2013-04-05 00:31:29 +0000 | [diff] [blame] | 2727 | if (Latency) |
| 2728 | dbgs() << " " << Latency << " cycles "; |
| 2729 | else |
| 2730 | dbgs() << " "; |
| 2731 | dbgs() << '\n'; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2732 | } |
| 2733 | #endif |
| 2734 | |
Andrew Trick | c573cd9 | 2013-09-06 17:32:44 +0000 | [diff] [blame] | 2735 | /// Pick the best candidate from the queue. |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2736 | /// |
| 2737 | /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during |
| 2738 | /// DAG building. To adjust for the current scheduling location we need to |
| 2739 | /// maintain the number of vreg uses remaining to be top-scheduled. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2740 | void GenericScheduler::pickNodeFromQueue(SchedBoundary &Zone, |
Andrew Trick | bb1247b | 2013-12-05 17:55:47 +0000 | [diff] [blame] | 2741 | const RegPressureTracker &RPTracker, |
| 2742 | SchedCandidate &Cand) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2743 | ReadyQueue &Q = Zone.Available; |
| 2744 | |
Andrew Trick | a8ad5f7 | 2012-05-24 22:11:12 +0000 | [diff] [blame] | 2745 | DEBUG(Q.dump()); |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2746 | |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2747 | // getMaxPressureDelta temporarily modifies the tracker. |
| 2748 | RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); |
| 2749 | |
Andrew Trick | dd375dd | 2012-05-24 22:11:03 +0000 | [diff] [blame] | 2750 | for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2751 | |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2752 | SchedCandidate TryCand(Cand.Policy); |
| 2753 | TryCand.SU = *I; |
| 2754 | tryCandidate(Cand, TryCand, Zone, RPTracker, TempTracker); |
| 2755 | if (TryCand.Reason != NoCand) { |
| 2756 | // Initialize resource delta if needed in case future heuristics query it. |
| 2757 | if (TryCand.ResDelta == SchedResourceDelta()) |
| 2758 | TryCand.initResourceDelta(DAG, SchedModel); |
| 2759 | Cand.setBest(TryCand); |
Andrew Trick | 419d491 | 2013-04-05 00:31:29 +0000 | [diff] [blame] | 2760 | DEBUG(traceCandidate(Cand)); |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2761 | } |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2762 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2765 | static void tracePick(const GenericScheduler::SchedCandidate &Cand, |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2766 | bool IsTop) { |
Andrew Trick | 1f0bb69 | 2013-04-13 06:07:49 +0000 | [diff] [blame] | 2767 | DEBUG(dbgs() << "Pick " << (IsTop ? "Top " : "Bot ") |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2768 | << GenericScheduler::getReasonStr(Cand.Reason) << '\n'); |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2771 | /// Pick the best candidate node from either the top or bottom queue. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2772 | SUnit *GenericScheduler::pickNodeBidirectional(bool &IsTopNode) { |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2773 | // Schedule as far as possible in the direction of no choice. This is most |
| 2774 | // efficient, but also provides the best heuristics for CriticalPSets. |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2775 | if (SUnit *SU = Bot.pickOnlyChoice()) { |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2776 | IsTopNode = false; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2777 | DEBUG(dbgs() << "Pick Bot NOCAND\n"); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2778 | return SU; |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2779 | } |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2780 | if (SUnit *SU = Top.pickOnlyChoice()) { |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2781 | IsTopNode = true; |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2782 | DEBUG(dbgs() << "Pick Top NOCAND\n"); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2783 | return SU; |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2784 | } |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2785 | CandPolicy NoPolicy; |
| 2786 | SchedCandidate BotCand(NoPolicy); |
| 2787 | SchedCandidate TopCand(NoPolicy); |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2788 | Bot.setPolicy(BotCand.Policy, Top); |
| 2789 | Top.setPolicy(TopCand.Policy, Bot); |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2790 | |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2791 | // Prefer bottom scheduling when heuristics are silent. |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2792 | pickNodeFromQueue(Bot, DAG->getBotRPTracker(), BotCand); |
| 2793 | assert(BotCand.Reason != NoCand && "failed to find the first candidate"); |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2794 | |
| 2795 | // If either Q has a single candidate that provides the least increase in |
| 2796 | // Excess pressure, we can immediately schedule from that Q. |
| 2797 | // |
| 2798 | // RegionCriticalPSets summarizes the pressure within the scheduled region and |
| 2799 | // affects picking from either Q. If scheduling in one direction must |
| 2800 | // increase pressure for one of the excess PSets, then schedule in that |
| 2801 | // direction first to provide more freedom in the other direction. |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2802 | if ((BotCand.Reason == RegExcess && !BotCand.isRepeat(RegExcess)) |
| 2803 | || (BotCand.Reason == RegCritical |
| 2804 | && !BotCand.isRepeat(RegCritical))) |
| 2805 | { |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2806 | IsTopNode = false; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2807 | tracePick(BotCand, IsTopNode); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2808 | return BotCand.SU; |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2809 | } |
| 2810 | // Check if the top Q has a better candidate. |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2811 | pickNodeFromQueue(Top, DAG->getTopRPTracker(), TopCand); |
| 2812 | assert(TopCand.Reason != NoCand && "failed to find the first candidate"); |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2813 | |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2814 | // Choose the queue with the most important (lowest enum) reason. |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2815 | if (TopCand.Reason < BotCand.Reason) { |
| 2816 | IsTopNode = true; |
| 2817 | tracePick(TopCand, IsTopNode); |
| 2818 | return TopCand.SU; |
| 2819 | } |
Andrew Trick | d40d0f2 | 2013-06-17 21:45:05 +0000 | [diff] [blame] | 2820 | // Otherwise prefer the bottom candidate, in node order if all else failed. |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2821 | IsTopNode = false; |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2822 | tracePick(BotCand, IsTopNode); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2823 | return BotCand.SU; |
Andrew Trick | 2202577 | 2012-05-17 18:35:10 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | /// Pick the best node to balance the schedule. Implements MachineSchedStrategy. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2827 | SUnit *GenericScheduler::pickNode(bool &IsTopNode) { |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2828 | if (DAG->top() == DAG->bottom()) { |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2829 | assert(Top.Available.empty() && Top.Pending.empty() && |
| 2830 | Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2831 | return NULL; |
| 2832 | } |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2833 | SUnit *SU; |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2834 | do { |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 2835 | if (RegionPolicy.OnlyTopDown) { |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2836 | SU = Top.pickOnlyChoice(); |
| 2837 | if (!SU) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2838 | CandPolicy NoPolicy; |
| 2839 | SchedCandidate TopCand(NoPolicy); |
| 2840 | pickNodeFromQueue(Top, DAG->getTopRPTracker(), TopCand); |
Andrew Trick | 1ab16d9 | 2013-09-04 21:00:13 +0000 | [diff] [blame] | 2841 | assert(TopCand.Reason != NoCand && "failed to find a candidate"); |
Andrew Trick | ef54c59 | 2013-09-04 21:00:16 +0000 | [diff] [blame] | 2842 | tracePick(TopCand, true); |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2843 | SU = TopCand.SU; |
| 2844 | } |
| 2845 | IsTopNode = true; |
Andrew Trick | a306a8a | 2012-05-24 23:11:17 +0000 | [diff] [blame] | 2846 | } |
Andrew Trick | 75e411c | 2013-09-06 17:32:34 +0000 | [diff] [blame] | 2847 | else if (RegionPolicy.OnlyBottomUp) { |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2848 | SU = Bot.pickOnlyChoice(); |
| 2849 | if (!SU) { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2850 | CandPolicy NoPolicy; |
| 2851 | SchedCandidate BotCand(NoPolicy); |
| 2852 | pickNodeFromQueue(Bot, DAG->getBotRPTracker(), BotCand); |
Andrew Trick | 1ab16d9 | 2013-09-04 21:00:13 +0000 | [diff] [blame] | 2853 | assert(BotCand.Reason != NoCand && "failed to find a candidate"); |
Andrew Trick | ef54c59 | 2013-09-04 21:00:16 +0000 | [diff] [blame] | 2854 | tracePick(BotCand, false); |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2855 | SU = BotCand.SU; |
| 2856 | } |
| 2857 | IsTopNode = false; |
Andrew Trick | a306a8a | 2012-05-24 23:11:17 +0000 | [diff] [blame] | 2858 | } |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2859 | else { |
Andrew Trick | 3ca33ac | 2012-11-07 07:05:09 +0000 | [diff] [blame] | 2860 | SU = pickNodeBidirectional(IsTopNode); |
Andrew Trick | 984d98b | 2012-10-08 18:53:53 +0000 | [diff] [blame] | 2861 | } |
| 2862 | } while (SU->isScheduled); |
| 2863 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2864 | if (SU->isTopReady()) |
| 2865 | Top.removeReady(SU); |
| 2866 | if (SU->isBottomReady()) |
| 2867 | Bot.removeReady(SU); |
Andrew Trick | 4e7f6a7 | 2012-05-25 02:02:39 +0000 | [diff] [blame] | 2868 | |
Andrew Trick | 1f0bb69 | 2013-04-13 06:07:49 +0000 | [diff] [blame] | 2869 | DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr()); |
Andrew Trick | 7ee9de5 | 2012-05-10 21:06:16 +0000 | [diff] [blame] | 2870 | return SU; |
| 2871 | } |
| 2872 | |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2873 | void GenericScheduler::reschedulePhysRegCopies(SUnit *SU, bool isTop) { |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2874 | |
| 2875 | MachineBasicBlock::iterator InsertPos = SU->getInstr(); |
| 2876 | if (!isTop) |
| 2877 | ++InsertPos; |
| 2878 | SmallVectorImpl<SDep> &Deps = isTop ? SU->Preds : SU->Succs; |
| 2879 | |
| 2880 | // Find already scheduled copies with a single physreg dependence and move |
| 2881 | // them just above the scheduled instruction. |
| 2882 | for (SmallVectorImpl<SDep>::iterator I = Deps.begin(), E = Deps.end(); |
| 2883 | I != E; ++I) { |
| 2884 | if (I->getKind() != SDep::Data || !TRI->isPhysicalRegister(I->getReg())) |
| 2885 | continue; |
| 2886 | SUnit *DepSU = I->getSUnit(); |
| 2887 | if (isTop ? DepSU->Succs.size() > 1 : DepSU->Preds.size() > 1) |
| 2888 | continue; |
| 2889 | MachineInstr *Copy = DepSU->getInstr(); |
| 2890 | if (!Copy->isCopy()) |
| 2891 | continue; |
| 2892 | DEBUG(dbgs() << " Rescheduling physreg copy "; |
| 2893 | I->getSUnit()->dump(DAG)); |
| 2894 | DAG->moveInstruction(Copy, InsertPos); |
| 2895 | } |
| 2896 | } |
| 2897 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2898 | /// Update the scheduler's state after scheduling a node. This is the same node |
| 2899 | /// that was just returned by pickNode(). However, ScheduleDAGMI needs to update |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2900 | /// it's state based on the current cycle before MachineSchedStrategy does. |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2901 | /// |
| 2902 | /// FIXME: Eventually, we may bundle physreg copies rather than rescheduling |
| 2903 | /// them here. See comments in biasPhysRegCopy. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2904 | void GenericScheduler::schedNode(SUnit *SU, bool IsTopNode) { |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2905 | if (IsTopNode) { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2906 | SU->TopReadyCycle = std::max(SU->TopReadyCycle, Top.CurrCycle); |
Andrew Trick | ce27bb9 | 2012-06-29 03:23:22 +0000 | [diff] [blame] | 2907 | Top.bumpNode(SU); |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2908 | if (SU->hasPhysRegUses) |
| 2909 | reschedulePhysRegCopies(SU, true); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2910 | } |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 2911 | else { |
Andrew Trick | f78e7fa | 2013-06-15 05:39:19 +0000 | [diff] [blame] | 2912 | SU->BotReadyCycle = std::max(SU->BotReadyCycle, Bot.CurrCycle); |
Andrew Trick | ce27bb9 | 2012-06-29 03:23:22 +0000 | [diff] [blame] | 2913 | Bot.bumpNode(SU); |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 2914 | if (SU->hasPhysRegDefs) |
| 2915 | reschedulePhysRegCopies(SU, false); |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 2919 | /// Create the standard converging machine scheduler. This will be used as the |
| 2920 | /// default scheduler if the target does not set a default. |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2921 | static ScheduleDAGInstrs *createGenericSched(MachineSchedContext *C) { |
| 2922 | ScheduleDAGMI *DAG = new ScheduleDAGMI(C, new GenericScheduler(C)); |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2923 | // Register DAG post-processors. |
Andrew Trick | 85a1d4c | 2013-04-24 15:54:43 +0000 | [diff] [blame] | 2924 | // |
| 2925 | // FIXME: extend the mutation API to allow earlier mutations to instantiate |
| 2926 | // data and pass it to later mutations. Have a single mutation that gathers |
| 2927 | // the interesting nodes in one pass. |
Andrew Trick | 0cd8afc | 2013-06-15 04:49:46 +0000 | [diff] [blame] | 2928 | DAG->addMutation(new CopyConstrain(DAG->TII, DAG->TRI)); |
Andrew Trick | a6e8777 | 2013-09-04 21:00:08 +0000 | [diff] [blame] | 2929 | if (EnableLoadCluster && DAG->TII->enableClusterLoads()) |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2930 | DAG->addMutation(new LoadClusterMutation(DAG->TII, DAG->TRI)); |
Andrew Trick | 26328024 | 2012-11-12 19:52:20 +0000 | [diff] [blame] | 2931 | if (EnableMacroFusion) |
| 2932 | DAG->addMutation(new MacroFusion(DAG->TII)); |
Andrew Trick | a7714a0 | 2012-11-12 19:40:10 +0000 | [diff] [blame] | 2933 | return DAG; |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 2934 | } |
| 2935 | static MachineSchedRegistry |
Andrew Trick | 665d3ec | 2013-09-19 23:10:59 +0000 | [diff] [blame] | 2936 | GenericSchedRegistry("converge", "Standard converging scheduler.", |
| 2937 | createGenericSched); |
Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 2938 | |
| 2939 | //===----------------------------------------------------------------------===// |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2940 | // ILP Scheduler. Currently for experimental analysis of heuristics. |
| 2941 | //===----------------------------------------------------------------------===// |
| 2942 | |
| 2943 | namespace { |
| 2944 | /// \brief Order nodes by the ILP metric. |
| 2945 | struct ILPOrder { |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 2946 | const SchedDFSResult *DFSResult; |
| 2947 | const BitVector *ScheduledTrees; |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2948 | bool MaximizeILP; |
| 2949 | |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 2950 | ILPOrder(bool MaxILP): DFSResult(0), ScheduledTrees(0), MaximizeILP(MaxILP) {} |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2951 | |
| 2952 | /// \brief Apply a less-than relation on node priority. |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 2953 | /// |
| 2954 | /// (Return true if A comes after B in the Q.) |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2955 | bool operator()(const SUnit *A, const SUnit *B) const { |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 2956 | unsigned SchedTreeA = DFSResult->getSubtreeID(A); |
| 2957 | unsigned SchedTreeB = DFSResult->getSubtreeID(B); |
| 2958 | if (SchedTreeA != SchedTreeB) { |
| 2959 | // Unscheduled trees have lower priority. |
| 2960 | if (ScheduledTrees->test(SchedTreeA) != ScheduledTrees->test(SchedTreeB)) |
| 2961 | return ScheduledTrees->test(SchedTreeB); |
| 2962 | |
| 2963 | // Trees with shallower connections have have lower priority. |
| 2964 | if (DFSResult->getSubtreeLevel(SchedTreeA) |
| 2965 | != DFSResult->getSubtreeLevel(SchedTreeB)) { |
| 2966 | return DFSResult->getSubtreeLevel(SchedTreeA) |
| 2967 | < DFSResult->getSubtreeLevel(SchedTreeB); |
| 2968 | } |
| 2969 | } |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2970 | if (MaximizeILP) |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 2971 | return DFSResult->getILP(A) < DFSResult->getILP(B); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2972 | else |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 2973 | return DFSResult->getILP(A) > DFSResult->getILP(B); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2974 | } |
| 2975 | }; |
| 2976 | |
| 2977 | /// \brief Schedule based on the ILP metric. |
| 2978 | class ILPScheduler : public MachineSchedStrategy { |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 2979 | ScheduleDAGMI *DAG; |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2980 | ILPOrder Cmp; |
| 2981 | |
| 2982 | std::vector<SUnit*> ReadyQ; |
| 2983 | public: |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 2984 | ILPScheduler(bool MaximizeILP): DAG(0), Cmp(MaximizeILP) {} |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2985 | |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 2986 | virtual void initialize(ScheduleDAGMI *dag) { |
| 2987 | DAG = dag; |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 2988 | DAG->computeDFSResult(); |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 2989 | Cmp.DFSResult = DAG->getDFSResult(); |
| 2990 | Cmp.ScheduledTrees = &DAG->getScheduledTrees(); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2991 | ReadyQ.clear(); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | virtual void registerRoots() { |
Benjamin Kramer | aa598b3 | 2012-11-29 14:36:26 +0000 | [diff] [blame] | 2995 | // Restore the heap in ReadyQ with the updated DFS results. |
| 2996 | std::make_heap(ReadyQ.begin(), ReadyQ.end(), Cmp); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | /// Implement MachineSchedStrategy interface. |
| 3000 | /// ----------------------------------------- |
| 3001 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 3002 | /// Callback to select the highest priority node from the ready Q. |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 3003 | virtual SUnit *pickNode(bool &IsTopNode) { |
| 3004 | if (ReadyQ.empty()) return NULL; |
Matt Arsenault | 4ab769f | 2013-03-21 00:57:21 +0000 | [diff] [blame] | 3005 | std::pop_heap(ReadyQ.begin(), ReadyQ.end(), Cmp); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 3006 | SUnit *SU = ReadyQ.back(); |
| 3007 | ReadyQ.pop_back(); |
| 3008 | IsTopNode = false; |
Andrew Trick | 1f0bb69 | 2013-04-13 06:07:49 +0000 | [diff] [blame] | 3009 | DEBUG(dbgs() << "Pick node " << "SU(" << SU->NodeNum << ") " |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 3010 | << " ILP: " << DAG->getDFSResult()->getILP(SU) |
| 3011 | << " Tree: " << DAG->getDFSResult()->getSubtreeID(SU) << " @" |
| 3012 | << DAG->getDFSResult()->getSubtreeLevel( |
Andrew Trick | 1f0bb69 | 2013-04-13 06:07:49 +0000 | [diff] [blame] | 3013 | DAG->getDFSResult()->getSubtreeID(SU)) << '\n' |
| 3014 | << "Scheduling " << *SU->getInstr()); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 3015 | return SU; |
| 3016 | } |
| 3017 | |
Andrew Trick | 44f750a | 2013-01-25 04:01:04 +0000 | [diff] [blame] | 3018 | /// \brief Scheduler callback to notify that a new subtree is scheduled. |
| 3019 | virtual void scheduleTree(unsigned SubtreeID) { |
| 3020 | std::make_heap(ReadyQ.begin(), ReadyQ.end(), Cmp); |
| 3021 | } |
| 3022 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 3023 | /// Callback after a node is scheduled. Mark a newly scheduled tree, notify |
| 3024 | /// DFSResults, and resort the priority Q. |
| 3025 | virtual void schedNode(SUnit *SU, bool IsTopNode) { |
| 3026 | assert(!IsTopNode && "SchedDFSResult needs bottom-up"); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 3027 | } |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 3028 | |
| 3029 | virtual void releaseTopNode(SUnit *) { /*only called for top roots*/ } |
| 3030 | |
| 3031 | virtual void releaseBottomNode(SUnit *SU) { |
| 3032 | ReadyQ.push_back(SU); |
| 3033 | std::push_heap(ReadyQ.begin(), ReadyQ.end(), Cmp); |
| 3034 | } |
| 3035 | }; |
| 3036 | } // namespace |
| 3037 | |
| 3038 | static ScheduleDAGInstrs *createILPMaxScheduler(MachineSchedContext *C) { |
| 3039 | return new ScheduleDAGMI(C, new ILPScheduler(true)); |
| 3040 | } |
| 3041 | static ScheduleDAGInstrs *createILPMinScheduler(MachineSchedContext *C) { |
| 3042 | return new ScheduleDAGMI(C, new ILPScheduler(false)); |
| 3043 | } |
| 3044 | static MachineSchedRegistry ILPMaxRegistry( |
| 3045 | "ilpmax", "Schedule bottom-up for max ILP", createILPMaxScheduler); |
| 3046 | static MachineSchedRegistry ILPMinRegistry( |
| 3047 | "ilpmin", "Schedule bottom-up for min ILP", createILPMinScheduler); |
| 3048 | |
| 3049 | //===----------------------------------------------------------------------===// |
Andrew Trick | 6344087 | 2012-01-14 02:17:06 +0000 | [diff] [blame] | 3050 | // Machine Instruction Shuffler for Correctness Testing |
| 3051 | //===----------------------------------------------------------------------===// |
| 3052 | |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3053 | #ifndef NDEBUG |
| 3054 | namespace { |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3055 | /// Apply a less-than relation on the node order, which corresponds to the |
| 3056 | /// instruction order prior to scheduling. IsReverse implements greater-than. |
| 3057 | template<bool IsReverse> |
| 3058 | struct SUnitOrder { |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 3059 | bool operator()(SUnit *A, SUnit *B) const { |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3060 | if (IsReverse) |
| 3061 | return A->NodeNum > B->NodeNum; |
| 3062 | else |
| 3063 | return A->NodeNum < B->NodeNum; |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 3064 | } |
| 3065 | }; |
| 3066 | |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3067 | /// Reorder instructions as much as possible. |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3068 | class InstructionShuffler : public MachineSchedStrategy { |
| 3069 | bool IsAlternating; |
| 3070 | bool IsTopDown; |
| 3071 | |
| 3072 | // Using a less-than relation (SUnitOrder<false>) for the TopQ priority |
| 3073 | // gives nodes with a higher number higher priority causing the latest |
| 3074 | // instructions to be scheduled first. |
| 3075 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false> > |
| 3076 | TopQ; |
| 3077 | // When scheduling bottom-up, use greater-than as the queue priority. |
| 3078 | PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true> > |
| 3079 | BottomQ; |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3080 | public: |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3081 | InstructionShuffler(bool alternate, bool topdown) |
| 3082 | : IsAlternating(alternate), IsTopDown(topdown) {} |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3083 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3084 | virtual void initialize(ScheduleDAGMI *) { |
| 3085 | TopQ.clear(); |
| 3086 | BottomQ.clear(); |
| 3087 | } |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 3088 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3089 | /// Implement MachineSchedStrategy interface. |
| 3090 | /// ----------------------------------------- |
| 3091 | |
| 3092 | virtual SUnit *pickNode(bool &IsTopNode) { |
| 3093 | SUnit *SU; |
| 3094 | if (IsTopDown) { |
| 3095 | do { |
| 3096 | if (TopQ.empty()) return NULL; |
| 3097 | SU = TopQ.top(); |
| 3098 | TopQ.pop(); |
| 3099 | } while (SU->isScheduled); |
| 3100 | IsTopNode = true; |
| 3101 | } |
| 3102 | else { |
| 3103 | do { |
| 3104 | if (BottomQ.empty()) return NULL; |
| 3105 | SU = BottomQ.top(); |
| 3106 | BottomQ.pop(); |
| 3107 | } while (SU->isScheduled); |
| 3108 | IsTopNode = false; |
| 3109 | } |
| 3110 | if (IsAlternating) |
| 3111 | IsTopDown = !IsTopDown; |
Andrew Trick | 7ccdc5c | 2012-01-17 06:55:07 +0000 | [diff] [blame] | 3112 | return SU; |
| 3113 | } |
| 3114 | |
Andrew Trick | 61f1a27 | 2012-05-24 22:11:09 +0000 | [diff] [blame] | 3115 | virtual void schedNode(SUnit *SU, bool IsTopNode) {} |
| 3116 | |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3117 | virtual void releaseTopNode(SUnit *SU) { |
| 3118 | TopQ.push(SU); |
| 3119 | } |
| 3120 | virtual void releaseBottomNode(SUnit *SU) { |
| 3121 | BottomQ.push(SU); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3122 | } |
| 3123 | }; |
| 3124 | } // namespace |
| 3125 | |
Andrew Trick | 02a80da | 2012-03-08 01:41:12 +0000 | [diff] [blame] | 3126 | static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) { |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3127 | bool Alternate = !ForceTopDown && !ForceBottomUp; |
| 3128 | bool TopDown = !ForceBottomUp; |
Benjamin Kramer | 05e7a84 | 2012-03-14 11:26:37 +0000 | [diff] [blame] | 3129 | assert((TopDown || !ForceTopDown) && |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3130 | "-misched-topdown incompatible with -misched-bottomup"); |
| 3131 | return new ScheduleDAGMI(C, new InstructionShuffler(Alternate, TopDown)); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3132 | } |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 3133 | static MachineSchedRegistry ShufflerRegistry( |
| 3134 | "shuffle", "Shuffle machine instructions alternating directions", |
| 3135 | createInstructionShuffler); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 3136 | #endif // !NDEBUG |
Andrew Trick | ea9fd95 | 2013-01-25 07:45:29 +0000 | [diff] [blame] | 3137 | |
| 3138 | //===----------------------------------------------------------------------===// |
| 3139 | // GraphWriter support for ScheduleDAGMI. |
| 3140 | //===----------------------------------------------------------------------===// |
| 3141 | |
| 3142 | #ifndef NDEBUG |
| 3143 | namespace llvm { |
| 3144 | |
| 3145 | template<> struct GraphTraits< |
| 3146 | ScheduleDAGMI*> : public GraphTraits<ScheduleDAG*> {}; |
| 3147 | |
| 3148 | template<> |
| 3149 | struct DOTGraphTraits<ScheduleDAGMI*> : public DefaultDOTGraphTraits { |
| 3150 | |
| 3151 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 3152 | |
| 3153 | static std::string getGraphName(const ScheduleDAG *G) { |
| 3154 | return G->MF.getName(); |
| 3155 | } |
| 3156 | |
| 3157 | static bool renderGraphFromBottomUp() { |
| 3158 | return true; |
| 3159 | } |
| 3160 | |
| 3161 | static bool isNodeHidden(const SUnit *Node) { |
Andrew Trick | 856ecd9 | 2013-09-04 21:00:18 +0000 | [diff] [blame] | 3162 | return (Node->Preds.size() > 10 || Node->Succs.size() > 10); |
Andrew Trick | ea9fd95 | 2013-01-25 07:45:29 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | static bool hasNodeAddressLabel(const SUnit *Node, |
| 3166 | const ScheduleDAG *Graph) { |
| 3167 | return false; |
| 3168 | } |
| 3169 | |
| 3170 | /// If you want to override the dot attributes printed for a particular |
| 3171 | /// edge, override this method. |
| 3172 | static std::string getEdgeAttributes(const SUnit *Node, |
| 3173 | SUnitIterator EI, |
| 3174 | const ScheduleDAG *Graph) { |
| 3175 | if (EI.isArtificialDep()) |
| 3176 | return "color=cyan,style=dashed"; |
| 3177 | if (EI.isCtrlDep()) |
| 3178 | return "color=blue,style=dashed"; |
| 3179 | return ""; |
| 3180 | } |
| 3181 | |
| 3182 | static std::string getNodeLabel(const SUnit *SU, const ScheduleDAG *G) { |
| 3183 | std::string Str; |
| 3184 | raw_string_ostream SS(Str); |
Andrew Trick | 7609b7d | 2013-09-06 17:32:42 +0000 | [diff] [blame] | 3185 | const SchedDFSResult *DFS = |
| 3186 | static_cast<const ScheduleDAGMI*>(G)->getDFSResult(); |
| 3187 | SS << "SU:" << SU->NodeNum; |
| 3188 | if (DFS) |
| 3189 | SS << " I:" << DFS->getNumInstrs(SU); |
Andrew Trick | ea9fd95 | 2013-01-25 07:45:29 +0000 | [diff] [blame] | 3190 | return SS.str(); |
| 3191 | } |
| 3192 | static std::string getNodeDescription(const SUnit *SU, const ScheduleDAG *G) { |
| 3193 | return G->getGraphNodeLabel(SU); |
| 3194 | } |
| 3195 | |
| 3196 | static std::string getNodeAttributes(const SUnit *N, |
| 3197 | const ScheduleDAG *Graph) { |
| 3198 | std::string Str("shape=Mrecord"); |
| 3199 | const SchedDFSResult *DFS = |
| 3200 | static_cast<const ScheduleDAGMI*>(Graph)->getDFSResult(); |
| 3201 | if (DFS) { |
| 3202 | Str += ",style=filled,fillcolor=\"#"; |
| 3203 | Str += DOT::getColorString(DFS->getSubtreeID(N)); |
| 3204 | Str += '"'; |
| 3205 | } |
| 3206 | return Str; |
| 3207 | } |
| 3208 | }; |
| 3209 | } // namespace llvm |
| 3210 | #endif // NDEBUG |
| 3211 | |
| 3212 | /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG |
| 3213 | /// rendered using 'dot'. |
| 3214 | /// |
| 3215 | void ScheduleDAGMI::viewGraph(const Twine &Name, const Twine &Title) { |
| 3216 | #ifndef NDEBUG |
| 3217 | ViewGraph(this, Name, false, Title); |
| 3218 | #else |
| 3219 | errs() << "ScheduleDAGMI::viewGraph is only available in debug builds on " |
| 3220 | << "systems with Graphviz or gv!\n"; |
| 3221 | #endif // NDEBUG |
| 3222 | } |
| 3223 | |
| 3224 | /// Out-of-line implementation with no arguments is handy for gdb. |
| 3225 | void ScheduleDAGMI::viewGraph() { |
| 3226 | viewGraph(getDAGName(), "Scheduling-Units Graph for " + getDAGName()); |
| 3227 | } |