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