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