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