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