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