Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1 | //===- MachinePipeliner.cpp - Machine Software Pipeliner Pass -------------===// |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // An implementation of the Swing Modulo Scheduling (SMS) software pipeliner. |
| 10 | // |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 11 | // This SMS implementation is a target-independent back-end pass. When enabled, |
| 12 | // the pass runs just prior to the register allocation pass, while the machine |
| 13 | // IR is in SSA form. If software pipelining is successful, then the original |
| 14 | // loop is replaced by the optimized loop. The optimized loop contains one or |
| 15 | // more prolog blocks, the pipelined kernel, and one or more epilog blocks. If |
| 16 | // the instructions cannot be scheduled in a given MII, we increase the MII by |
| 17 | // one and try again. |
| 18 | // |
| 19 | // The SMS implementation is an extension of the ScheduleDAGInstrs class. We |
| 20 | // represent loop carried dependences in the DAG as order edges to the Phi |
| 21 | // nodes. We also perform several passes over the DAG to eliminate unnecessary |
| 22 | // edges that inhibit the ability to pipeline. The implementation uses the |
| 23 | // DFAPacketizer class to compute the minimum initiation interval and the check |
| 24 | // where an instruction may be inserted in the pipelined schedule. |
| 25 | // |
| 26 | // In order for the SMS pass to work, several target specific hooks need to be |
| 27 | // implemented to get information about the loop structure and to rewrite |
| 28 | // instructions. |
| 29 | // |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/ArrayRef.h" |
| 33 | #include "llvm/ADT/BitVector.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseMap.h" |
| 35 | #include "llvm/ADT/MapVector.h" |
| 36 | #include "llvm/ADT/PriorityQueue.h" |
| 37 | #include "llvm/ADT/SetVector.h" |
| 38 | #include "llvm/ADT/SmallPtrSet.h" |
| 39 | #include "llvm/ADT/SmallSet.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SmallVector.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/iterator_range.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/MemoryLocation.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ValueTracking.h" |
| 46 | #include "llvm/CodeGen/DFAPacketizer.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/LiveIntervals.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 49 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/MachineFunction.h" |
| 51 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 52 | #include "llvm/CodeGen/MachineInstr.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 54 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 56 | #include "llvm/CodeGen/MachineOperand.h" |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 57 | #include "llvm/CodeGen/MachinePipeliner.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 58 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
James Molloy | 790a779 | 2019-08-30 18:49:50 +0000 | [diff] [blame] | 59 | #include "llvm/CodeGen/ModuloSchedule.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 60 | #include "llvm/CodeGen/RegisterPressure.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 61 | #include "llvm/CodeGen/ScheduleDAG.h" |
Krzysztof Parzyszek | 8839124 | 2016-12-22 19:21:20 +0000 | [diff] [blame] | 62 | #include "llvm/CodeGen/ScheduleDAGMutation.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 63 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 64 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 65 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 66 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Attributes.h" |
| 68 | #include "llvm/IR/DebugLoc.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 69 | #include "llvm/IR/Function.h" |
| 70 | #include "llvm/MC/LaneBitmask.h" |
| 71 | #include "llvm/MC/MCInstrDesc.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 72 | #include "llvm/MC/MCInstrItineraries.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 73 | #include "llvm/MC/MCRegisterInfo.h" |
| 74 | #include "llvm/Pass.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 75 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 76 | #include "llvm/Support/Compiler.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 77 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 78 | #include "llvm/Support/MathExtras.h" |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 79 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 80 | #include <algorithm> |
| 81 | #include <cassert> |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 82 | #include <climits> |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 83 | #include <cstdint> |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 84 | #include <deque> |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 85 | #include <functional> |
| 86 | #include <iterator> |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 87 | #include <map> |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 88 | #include <memory> |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 89 | #include <tuple> |
| 90 | #include <utility> |
| 91 | #include <vector> |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 92 | |
| 93 | using namespace llvm; |
| 94 | |
| 95 | #define DEBUG_TYPE "pipeliner" |
| 96 | |
| 97 | STATISTIC(NumTrytoPipeline, "Number of loops that we attempt to pipeline"); |
| 98 | STATISTIC(NumPipelined, "Number of loops software pipelined"); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 99 | STATISTIC(NumNodeOrderIssues, "Number of node order issues found"); |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 100 | STATISTIC(NumFailBranch, "Pipeliner abort due to unknown branch"); |
| 101 | STATISTIC(NumFailLoop, "Pipeliner abort due to unsupported loop"); |
| 102 | STATISTIC(NumFailPreheader, "Pipeliner abort due to missing preheader"); |
| 103 | STATISTIC(NumFailLargeMaxMII, "Pipeliner abort due to MaxMII too large"); |
| 104 | STATISTIC(NumFailZeroMII, "Pipeliner abort due to zero MII"); |
| 105 | STATISTIC(NumFailNoSchedule, "Pipeliner abort due to no schedule found"); |
| 106 | STATISTIC(NumFailZeroStage, "Pipeliner abort due to zero stage"); |
| 107 | STATISTIC(NumFailLargeMaxStage, "Pipeliner abort due to too many stages"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 108 | |
| 109 | /// A command line option to turn software pipelining on or off. |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 110 | static cl::opt<bool> EnableSWP("enable-pipeliner", cl::Hidden, cl::init(true), |
| 111 | cl::ZeroOrMore, |
| 112 | cl::desc("Enable Software Pipelining")); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 113 | |
| 114 | /// A command line option to enable SWP at -Os. |
| 115 | static cl::opt<bool> EnableSWPOptSize("enable-pipeliner-opt-size", |
| 116 | cl::desc("Enable SWP at Os."), cl::Hidden, |
| 117 | cl::init(false)); |
| 118 | |
| 119 | /// A command line argument to limit minimum initial interval for pipelining. |
| 120 | static cl::opt<int> SwpMaxMii("pipeliner-max-mii", |
Hiroshi Inoue | 8f976ba | 2018-01-17 12:29:38 +0000 | [diff] [blame] | 121 | cl::desc("Size limit for the MII."), |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 122 | cl::Hidden, cl::init(27)); |
| 123 | |
| 124 | /// A command line argument to limit the number of stages in the pipeline. |
| 125 | static cl::opt<int> |
| 126 | SwpMaxStages("pipeliner-max-stages", |
| 127 | cl::desc("Maximum stages allowed in the generated scheduled."), |
| 128 | cl::Hidden, cl::init(3)); |
| 129 | |
| 130 | /// A command line option to disable the pruning of chain dependences due to |
| 131 | /// an unrelated Phi. |
| 132 | static cl::opt<bool> |
| 133 | SwpPruneDeps("pipeliner-prune-deps", |
| 134 | cl::desc("Prune dependences between unrelated Phi nodes."), |
| 135 | cl::Hidden, cl::init(true)); |
| 136 | |
| 137 | /// A command line option to disable the pruning of loop carried order |
| 138 | /// dependences. |
| 139 | static cl::opt<bool> |
| 140 | SwpPruneLoopCarried("pipeliner-prune-loop-carried", |
| 141 | cl::desc("Prune loop carried order dependences."), |
| 142 | cl::Hidden, cl::init(true)); |
| 143 | |
| 144 | #ifndef NDEBUG |
| 145 | static cl::opt<int> SwpLoopLimit("pipeliner-max", cl::Hidden, cl::init(-1)); |
| 146 | #endif |
| 147 | |
| 148 | static cl::opt<bool> SwpIgnoreRecMII("pipeliner-ignore-recmii", |
| 149 | cl::ReallyHidden, cl::init(false), |
| 150 | cl::ZeroOrMore, cl::desc("Ignore RecMII")); |
| 151 | |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 152 | static cl::opt<bool> SwpShowResMask("pipeliner-show-mask", cl::Hidden, |
| 153 | cl::init(false)); |
| 154 | static cl::opt<bool> SwpDebugResource("pipeliner-dbg-res", cl::Hidden, |
| 155 | cl::init(false)); |
| 156 | |
James Molloy | 9354995 | 2019-09-03 08:20:31 +0000 | [diff] [blame] | 157 | static cl::opt<bool> EmitTestAnnotations( |
| 158 | "pipeliner-annotate-for-testing", cl::Hidden, cl::init(false), |
| 159 | cl::desc("Instead of emitting the pipelined code, annotate instructions " |
| 160 | "with the generated schedule for feeding into the " |
| 161 | "-modulo-schedule-test pass")); |
| 162 | |
James Molloy | fef9f59 | 2019-09-04 12:54:24 +0000 | [diff] [blame] | 163 | static cl::opt<bool> ExperimentalCodeGen( |
| 164 | "pipeliner-experimental-cg", cl::Hidden, cl::init(false), |
| 165 | cl::desc( |
| 166 | "Use the experimental peeling code generator for software pipelining")); |
| 167 | |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 168 | namespace llvm { |
| 169 | |
Sumanth Gundapaneni | 62ac69d | 2018-10-18 15:51:16 +0000 | [diff] [blame] | 170 | // A command line option to enable the CopyToPhi DAG mutation. |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 171 | cl::opt<bool> |
Aleksandr Urakov | 00d4c38 | 2018-10-23 14:27:45 +0000 | [diff] [blame] | 172 | SwpEnableCopyToPhi("pipeliner-enable-copytophi", cl::ReallyHidden, |
| 173 | cl::init(true), cl::ZeroOrMore, |
| 174 | cl::desc("Enable CopyToPhi DAG Mutation")); |
Sumanth Gundapaneni | 62ac69d | 2018-10-18 15:51:16 +0000 | [diff] [blame] | 175 | |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 176 | } // end namespace llvm |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 177 | |
| 178 | unsigned SwingSchedulerDAG::Circuits::MaxPaths = 5; |
| 179 | char MachinePipeliner::ID = 0; |
| 180 | #ifndef NDEBUG |
| 181 | int MachinePipeliner::NumTries = 0; |
| 182 | #endif |
| 183 | char &llvm::MachinePipelinerID = MachinePipeliner::ID; |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 184 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 185 | INITIALIZE_PASS_BEGIN(MachinePipeliner, DEBUG_TYPE, |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 186 | "Modulo Software Pipelining", false, false) |
| 187 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 188 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 189 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 190 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 191 | INITIALIZE_PASS_END(MachinePipeliner, DEBUG_TYPE, |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 192 | "Modulo Software Pipelining", false, false) |
| 193 | |
| 194 | /// The "main" function for implementing Swing Modulo Scheduling. |
| 195 | bool MachinePipeliner::runOnMachineFunction(MachineFunction &mf) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 196 | if (skipFunction(mf.getFunction())) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 197 | return false; |
| 198 | |
| 199 | if (!EnableSWP) |
| 200 | return false; |
| 201 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 202 | if (mf.getFunction().getAttributes().hasAttribute( |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 203 | AttributeList::FunctionIndex, Attribute::OptimizeForSize) && |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 204 | !EnableSWPOptSize.getPosition()) |
| 205 | return false; |
| 206 | |
Jinsong Ji | ef2d6d9 | 2019-06-11 17:40:39 +0000 | [diff] [blame] | 207 | if (!mf.getSubtarget().enableMachinePipeliner()) |
| 208 | return false; |
| 209 | |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 210 | // Cannot pipeline loops without instruction itineraries if we are using |
| 211 | // DFA for the pipeliner. |
| 212 | if (mf.getSubtarget().useDFAforSMS() && |
| 213 | (!mf.getSubtarget().getInstrItineraryData() || |
| 214 | mf.getSubtarget().getInstrItineraryData()->isEmpty())) |
| 215 | return false; |
| 216 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 217 | MF = &mf; |
| 218 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 219 | MDT = &getAnalysis<MachineDominatorTree>(); |
| 220 | TII = MF->getSubtarget().getInstrInfo(); |
| 221 | RegClassInfo.runOnMachineFunction(*MF); |
| 222 | |
| 223 | for (auto &L : *MLI) |
| 224 | scheduleLoop(*L); |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | /// Attempt to perform the SMS algorithm on the specified loop. This function is |
| 230 | /// the main entry point for the algorithm. The function identifies candidate |
| 231 | /// loops, calculates the minimum initiation interval, and attempts to schedule |
| 232 | /// the loop. |
| 233 | bool MachinePipeliner::scheduleLoop(MachineLoop &L) { |
| 234 | bool Changed = false; |
| 235 | for (auto &InnerLoop : L) |
| 236 | Changed |= scheduleLoop(*InnerLoop); |
| 237 | |
| 238 | #ifndef NDEBUG |
| 239 | // Stop trying after reaching the limit (if any). |
| 240 | int Limit = SwpLoopLimit; |
| 241 | if (Limit >= 0) { |
| 242 | if (NumTries >= SwpLoopLimit) |
| 243 | return Changed; |
| 244 | NumTries++; |
| 245 | } |
| 246 | #endif |
| 247 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 248 | setPragmaPipelineOptions(L); |
| 249 | if (!canPipelineLoop(L)) { |
| 250 | LLVM_DEBUG(dbgs() << "\n!!! Can not pipeline loop.\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 251 | return Changed; |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 252 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 253 | |
| 254 | ++NumTrytoPipeline; |
| 255 | |
| 256 | Changed = swingModuloScheduler(L); |
| 257 | |
| 258 | return Changed; |
| 259 | } |
| 260 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 261 | void MachinePipeliner::setPragmaPipelineOptions(MachineLoop &L) { |
| 262 | MachineBasicBlock *LBLK = L.getTopBlock(); |
| 263 | |
| 264 | if (LBLK == nullptr) |
| 265 | return; |
| 266 | |
| 267 | const BasicBlock *BBLK = LBLK->getBasicBlock(); |
| 268 | if (BBLK == nullptr) |
| 269 | return; |
| 270 | |
| 271 | const Instruction *TI = BBLK->getTerminator(); |
| 272 | if (TI == nullptr) |
| 273 | return; |
| 274 | |
| 275 | MDNode *LoopID = TI->getMetadata(LLVMContext::MD_loop); |
| 276 | if (LoopID == nullptr) |
| 277 | return; |
| 278 | |
| 279 | assert(LoopID->getNumOperands() > 0 && "requires atleast one operand"); |
| 280 | assert(LoopID->getOperand(0) == LoopID && "invalid loop"); |
| 281 | |
| 282 | for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { |
| 283 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 284 | |
| 285 | if (MD == nullptr) |
| 286 | continue; |
| 287 | |
| 288 | MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 289 | |
| 290 | if (S == nullptr) |
| 291 | continue; |
| 292 | |
| 293 | if (S->getString() == "llvm.loop.pipeline.initiationinterval") { |
| 294 | assert(MD->getNumOperands() == 2 && |
| 295 | "Pipeline initiation interval hint metadata should have two operands."); |
| 296 | II_setByPragma = |
| 297 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue(); |
| 298 | assert(II_setByPragma >= 1 && "Pipeline initiation interval must be positive."); |
| 299 | } else if (S->getString() == "llvm.loop.pipeline.disable") { |
| 300 | disabledByPragma = true; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 305 | /// Return true if the loop can be software pipelined. The algorithm is |
| 306 | /// restricted to loops with a single basic block. Make sure that the |
| 307 | /// branch in the loop can be analyzed. |
| 308 | bool MachinePipeliner::canPipelineLoop(MachineLoop &L) { |
| 309 | if (L.getNumBlocks() != 1) |
| 310 | return false; |
| 311 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 312 | if (disabledByPragma) |
| 313 | return false; |
| 314 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 315 | // Check if the branch can't be understood because we can't do pipelining |
| 316 | // if that's the case. |
| 317 | LI.TBB = nullptr; |
| 318 | LI.FBB = nullptr; |
| 319 | LI.BrCond.clear(); |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 320 | if (TII->analyzeBranch(*L.getHeader(), LI.TBB, LI.FBB, LI.BrCond)) { |
| 321 | LLVM_DEBUG( |
| 322 | dbgs() << "Unable to analyzeBranch, can NOT pipeline current Loop\n"); |
| 323 | NumFailBranch++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 324 | return false; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 325 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 326 | |
| 327 | LI.LoopInductionVar = nullptr; |
| 328 | LI.LoopCompare = nullptr; |
James Molloy | 8a74eca | 2019-09-21 08:19:41 +0000 | [diff] [blame] | 329 | if (!TII->analyzeLoopForPipelining(L.getTopBlock())) { |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 330 | LLVM_DEBUG( |
| 331 | dbgs() << "Unable to analyzeLoop, can NOT pipeline current Loop\n"); |
| 332 | NumFailLoop++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 333 | return false; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 334 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 335 | |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 336 | if (!L.getLoopPreheader()) { |
| 337 | LLVM_DEBUG( |
| 338 | dbgs() << "Preheader not found, can NOT pipeline current Loop\n"); |
| 339 | NumFailPreheader++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 340 | return false; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 341 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 342 | |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 343 | // Remove any subregisters from inputs to phi nodes. |
| 344 | preprocessPhiNodes(*L.getHeader()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 348 | void MachinePipeliner::preprocessPhiNodes(MachineBasicBlock &B) { |
| 349 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 350 | SlotIndexes &Slots = *getAnalysis<LiveIntervals>().getSlotIndexes(); |
| 351 | |
| 352 | for (MachineInstr &PI : make_range(B.begin(), B.getFirstNonPHI())) { |
| 353 | MachineOperand &DefOp = PI.getOperand(0); |
| 354 | assert(DefOp.getSubReg() == 0); |
| 355 | auto *RC = MRI.getRegClass(DefOp.getReg()); |
| 356 | |
| 357 | for (unsigned i = 1, n = PI.getNumOperands(); i != n; i += 2) { |
| 358 | MachineOperand &RegOp = PI.getOperand(i); |
| 359 | if (RegOp.getSubReg() == 0) |
| 360 | continue; |
| 361 | |
| 362 | // If the operand uses a subregister, replace it with a new register |
| 363 | // without subregisters, and generate a copy to the new register. |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 364 | Register NewReg = MRI.createVirtualRegister(RC); |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 365 | MachineBasicBlock &PredB = *PI.getOperand(i+1).getMBB(); |
| 366 | MachineBasicBlock::iterator At = PredB.getFirstTerminator(); |
| 367 | const DebugLoc &DL = PredB.findDebugLoc(At); |
| 368 | auto Copy = BuildMI(PredB, At, DL, TII->get(TargetOpcode::COPY), NewReg) |
| 369 | .addReg(RegOp.getReg(), getRegState(RegOp), |
| 370 | RegOp.getSubReg()); |
| 371 | Slots.insertMachineInstrInMaps(*Copy); |
| 372 | RegOp.setReg(NewReg); |
| 373 | RegOp.setSubReg(0); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 378 | /// The SMS algorithm consists of the following main steps: |
| 379 | /// 1. Computation and analysis of the dependence graph. |
| 380 | /// 2. Ordering of the nodes (instructions). |
| 381 | /// 3. Attempt to Schedule the loop. |
| 382 | bool MachinePipeliner::swingModuloScheduler(MachineLoop &L) { |
| 383 | assert(L.getBlocks().size() == 1 && "SMS works on single blocks only."); |
| 384 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 385 | SwingSchedulerDAG SMS(*this, L, getAnalysis<LiveIntervals>(), RegClassInfo, |
| 386 | II_setByPragma); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 387 | |
| 388 | MachineBasicBlock *MBB = L.getHeader(); |
| 389 | // The kernel should not include any terminator instructions. These |
| 390 | // will be added back later. |
| 391 | SMS.startBlock(MBB); |
| 392 | |
| 393 | // Compute the number of 'real' instructions in the basic block by |
| 394 | // ignoring terminators. |
| 395 | unsigned size = MBB->size(); |
| 396 | for (MachineBasicBlock::iterator I = MBB->getFirstTerminator(), |
| 397 | E = MBB->instr_end(); |
| 398 | I != E; ++I, --size) |
| 399 | ; |
| 400 | |
| 401 | SMS.enterRegion(MBB, MBB->begin(), MBB->getFirstTerminator(), size); |
| 402 | SMS.schedule(); |
| 403 | SMS.exitRegion(); |
| 404 | |
| 405 | SMS.finishBlock(); |
| 406 | return SMS.hasNewSchedule(); |
| 407 | } |
| 408 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 409 | void SwingSchedulerDAG::setMII(unsigned ResMII, unsigned RecMII) { |
| 410 | if (II_setByPragma > 0) |
| 411 | MII = II_setByPragma; |
| 412 | else |
| 413 | MII = std::max(ResMII, RecMII); |
| 414 | } |
| 415 | |
| 416 | void SwingSchedulerDAG::setMAX_II() { |
| 417 | if (II_setByPragma > 0) |
| 418 | MAX_II = II_setByPragma; |
| 419 | else |
| 420 | MAX_II = MII + 10; |
| 421 | } |
| 422 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 423 | /// We override the schedule function in ScheduleDAGInstrs to implement the |
| 424 | /// scheduling part of the Swing Modulo Scheduling algorithm. |
| 425 | void SwingSchedulerDAG::schedule() { |
| 426 | AliasAnalysis *AA = &Pass.getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 427 | buildSchedGraph(AA); |
| 428 | addLoopCarriedDependences(AA); |
| 429 | updatePhiDependences(); |
| 430 | Topo.InitDAGTopologicalSorting(); |
| 431 | changeDependences(); |
Sumanth Gundapaneni | 62ac69d | 2018-10-18 15:51:16 +0000 | [diff] [blame] | 432 | postprocessDAG(); |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 433 | LLVM_DEBUG(dump()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 434 | |
| 435 | NodeSetType NodeSets; |
| 436 | findCircuits(NodeSets); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 437 | NodeSetType Circuits = NodeSets; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 438 | |
| 439 | // Calculate the MII. |
| 440 | unsigned ResMII = calculateResMII(); |
| 441 | unsigned RecMII = calculateRecMII(NodeSets); |
| 442 | |
| 443 | fuseRecs(NodeSets); |
| 444 | |
| 445 | // This flag is used for testing and can cause correctness problems. |
| 446 | if (SwpIgnoreRecMII) |
| 447 | RecMII = 0; |
| 448 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 449 | setMII(ResMII, RecMII); |
| 450 | setMAX_II(); |
| 451 | |
| 452 | LLVM_DEBUG(dbgs() << "MII = " << MII << " MAX_II = " << MAX_II |
| 453 | << " (rec=" << RecMII << ", res=" << ResMII << ")\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 454 | |
| 455 | // Can't schedule a loop without a valid MII. |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 456 | if (MII == 0) { |
| 457 | LLVM_DEBUG( |
| 458 | dbgs() |
| 459 | << "0 is not a valid Minimal Initiation Interval, can NOT schedule\n"); |
| 460 | NumFailZeroMII++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 461 | return; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 462 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 463 | |
| 464 | // Don't pipeline large loops. |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 465 | if (SwpMaxMii != -1 && (int)MII > SwpMaxMii) { |
| 466 | LLVM_DEBUG(dbgs() << "MII > " << SwpMaxMii |
| 467 | << ", we don't pipleline large loops\n"); |
| 468 | NumFailLargeMaxMII++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 469 | return; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 470 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 471 | |
| 472 | computeNodeFunctions(NodeSets); |
| 473 | |
| 474 | registerPressureFilter(NodeSets); |
| 475 | |
| 476 | colocateNodeSets(NodeSets); |
| 477 | |
| 478 | checkNodeSets(NodeSets); |
| 479 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 480 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 481 | for (auto &I : NodeSets) { |
| 482 | dbgs() << " Rec NodeSet "; |
| 483 | I.dump(); |
| 484 | } |
| 485 | }); |
| 486 | |
Fangrui Song | efd94c5 | 2019-04-23 14:51:27 +0000 | [diff] [blame] | 487 | llvm::stable_sort(NodeSets, std::greater<NodeSet>()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 488 | |
| 489 | groupRemainingNodes(NodeSets); |
| 490 | |
| 491 | removeDuplicateNodes(NodeSets); |
| 492 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 493 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 494 | for (auto &I : NodeSets) { |
| 495 | dbgs() << " NodeSet "; |
| 496 | I.dump(); |
| 497 | } |
| 498 | }); |
| 499 | |
| 500 | computeNodeOrder(NodeSets); |
| 501 | |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 502 | // check for node order issues |
| 503 | checkValidNodeOrder(Circuits); |
| 504 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 505 | SMSchedule Schedule(Pass.MF); |
| 506 | Scheduled = schedulePipeline(Schedule); |
| 507 | |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 508 | if (!Scheduled){ |
| 509 | LLVM_DEBUG(dbgs() << "No schedule found, return\n"); |
| 510 | NumFailNoSchedule++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 511 | return; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 512 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 513 | |
| 514 | unsigned numStages = Schedule.getMaxStageCount(); |
| 515 | // No need to generate pipeline if there are no overlapped iterations. |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 516 | if (numStages == 0) { |
| 517 | LLVM_DEBUG( |
| 518 | dbgs() << "No overlapped iterations, no need to generate pipeline\n"); |
| 519 | NumFailZeroStage++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 520 | return; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 521 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 522 | // Check that the maximum stage count is less than user-defined limit. |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 523 | if (SwpMaxStages > -1 && (int)numStages > SwpMaxStages) { |
| 524 | LLVM_DEBUG(dbgs() << "numStages:" << numStages << ">" << SwpMaxStages |
| 525 | << " : too many stages, abort\n"); |
| 526 | NumFailLargeMaxStage++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 527 | return; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 528 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 529 | |
James Molloy | 790a779 | 2019-08-30 18:49:50 +0000 | [diff] [blame] | 530 | // Generate the schedule as a ModuloSchedule. |
| 531 | DenseMap<MachineInstr *, int> Cycles, Stages; |
| 532 | std::vector<MachineInstr *> OrderedInsts; |
| 533 | for (int Cycle = Schedule.getFirstCycle(); Cycle <= Schedule.getFinalCycle(); |
| 534 | ++Cycle) { |
| 535 | for (SUnit *SU : Schedule.getInstructions(Cycle)) { |
| 536 | OrderedInsts.push_back(SU->getInstr()); |
| 537 | Cycles[SU->getInstr()] = Cycle; |
| 538 | Stages[SU->getInstr()] = Schedule.stageScheduled(SU); |
| 539 | } |
| 540 | } |
| 541 | DenseMap<MachineInstr *, std::pair<unsigned, int64_t>> NewInstrChanges; |
| 542 | for (auto &KV : NewMIs) { |
| 543 | Cycles[KV.first] = Cycles[KV.second]; |
| 544 | Stages[KV.first] = Stages[KV.second]; |
| 545 | NewInstrChanges[KV.first] = InstrChanges[getSUnit(KV.first)]; |
| 546 | } |
| 547 | |
| 548 | ModuloSchedule MS(MF, &Loop, std::move(OrderedInsts), std::move(Cycles), |
| 549 | std::move(Stages)); |
James Molloy | 9354995 | 2019-09-03 08:20:31 +0000 | [diff] [blame] | 550 | if (EmitTestAnnotations) { |
| 551 | assert(NewInstrChanges.empty() && |
| 552 | "Cannot serialize a schedule with InstrChanges!"); |
| 553 | ModuloScheduleTestAnnotater MSTI(MF, MS); |
| 554 | MSTI.annotate(); |
| 555 | return; |
| 556 | } |
James Molloy | fef9f59 | 2019-09-04 12:54:24 +0000 | [diff] [blame] | 557 | // The experimental code generator can't work if there are InstChanges. |
| 558 | if (ExperimentalCodeGen && NewInstrChanges.empty()) { |
| 559 | PeelingModuloScheduleExpander MSE(MF, MS, &LIS); |
| 560 | // Experimental code generation isn't complete yet, but it can partially |
| 561 | // validate the code it generates against the original |
| 562 | // ModuloScheduleExpander. |
| 563 | MSE.validateAgainstModuloScheduleExpander(); |
| 564 | } else { |
| 565 | ModuloScheduleExpander MSE(MF, MS, LIS, std::move(NewInstrChanges)); |
| 566 | MSE.expand(); |
| 567 | MSE.cleanup(); |
| 568 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 569 | ++NumPipelined; |
| 570 | } |
| 571 | |
| 572 | /// Clean up after the software pipeliner runs. |
| 573 | void SwingSchedulerDAG::finishBlock() { |
James Molloy | 790a779 | 2019-08-30 18:49:50 +0000 | [diff] [blame] | 574 | for (auto &KV : NewMIs) |
| 575 | MF.DeleteMachineInstr(KV.second); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 576 | NewMIs.clear(); |
| 577 | |
| 578 | // Call the superclass. |
| 579 | ScheduleDAGInstrs::finishBlock(); |
| 580 | } |
| 581 | |
| 582 | /// Return the register values for the operands of a Phi instruction. |
| 583 | /// This function assume the instruction is a Phi. |
| 584 | static void getPhiRegs(MachineInstr &Phi, MachineBasicBlock *Loop, |
| 585 | unsigned &InitVal, unsigned &LoopVal) { |
| 586 | assert(Phi.isPHI() && "Expecting a Phi."); |
| 587 | |
| 588 | InitVal = 0; |
| 589 | LoopVal = 0; |
| 590 | for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2) |
| 591 | if (Phi.getOperand(i + 1).getMBB() != Loop) |
| 592 | InitVal = Phi.getOperand(i).getReg(); |
Simon Pilgrim | fbfb19b | 2017-03-16 19:52:00 +0000 | [diff] [blame] | 593 | else |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 594 | LoopVal = Phi.getOperand(i).getReg(); |
| 595 | |
| 596 | assert(InitVal != 0 && LoopVal != 0 && "Unexpected Phi structure."); |
| 597 | } |
| 598 | |
Hiroshi Inoue | 8f976ba | 2018-01-17 12:29:38 +0000 | [diff] [blame] | 599 | /// Return the Phi register value that comes the loop block. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 600 | static unsigned getLoopPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) { |
| 601 | for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2) |
| 602 | if (Phi.getOperand(i + 1).getMBB() == LoopBB) |
| 603 | return Phi.getOperand(i).getReg(); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /// Return true if SUb can be reached from SUa following the chain edges. |
| 608 | static bool isSuccOrder(SUnit *SUa, SUnit *SUb) { |
| 609 | SmallPtrSet<SUnit *, 8> Visited; |
| 610 | SmallVector<SUnit *, 8> Worklist; |
| 611 | Worklist.push_back(SUa); |
| 612 | while (!Worklist.empty()) { |
| 613 | const SUnit *SU = Worklist.pop_back_val(); |
| 614 | for (auto &SI : SU->Succs) { |
| 615 | SUnit *SuccSU = SI.getSUnit(); |
| 616 | if (SI.getKind() == SDep::Order) { |
| 617 | if (Visited.count(SuccSU)) |
| 618 | continue; |
| 619 | if (SuccSU == SUb) |
| 620 | return true; |
| 621 | Worklist.push_back(SuccSU); |
| 622 | Visited.insert(SuccSU); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | /// Return true if the instruction causes a chain between memory |
| 630 | /// references before and after it. |
| 631 | static bool isDependenceBarrier(MachineInstr &MI, AliasAnalysis *AA) { |
Ulrich Weigand | 6c5d5ce | 2019-06-05 22:33:10 +0000 | [diff] [blame] | 632 | return MI.isCall() || MI.mayRaiseFPException() || |
| 633 | MI.hasUnmodeledSideEffects() || |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 634 | (MI.hasOrderedMemoryRef() && |
Justin Lebar | d98cf00 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 635 | (!MI.mayLoad() || !MI.isDereferenceableInvariantLoad(AA))); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /// Return the underlying objects for the memory references of an instruction. |
| 639 | /// This function calls the code in ValueTracking, but first checks that the |
| 640 | /// instruction has a memory operand. |
Bjorn Pettersson | 71e8c6f | 2019-04-24 06:55:50 +0000 | [diff] [blame] | 641 | static void getUnderlyingObjects(const MachineInstr *MI, |
| 642 | SmallVectorImpl<const Value *> &Objs, |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 643 | const DataLayout &DL) { |
| 644 | if (!MI->hasOneMemOperand()) |
| 645 | return; |
| 646 | MachineMemOperand *MM = *MI->memoperands_begin(); |
| 647 | if (!MM->getValue()) |
| 648 | return; |
Bjorn Pettersson | 71e8c6f | 2019-04-24 06:55:50 +0000 | [diff] [blame] | 649 | GetUnderlyingObjects(MM->getValue(), Objs, DL); |
| 650 | for (const Value *V : Objs) { |
Krzysztof Parzyszek | 9f041b1 | 2018-03-26 16:50:11 +0000 | [diff] [blame] | 651 | if (!isIdentifiedObject(V)) { |
| 652 | Objs.clear(); |
| 653 | return; |
| 654 | } |
| 655 | Objs.push_back(V); |
| 656 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /// Add a chain edge between a load and store if the store can be an |
| 660 | /// alias of the load on a subsequent iteration, i.e., a loop carried |
| 661 | /// dependence. This code is very similar to the code in ScheduleDAGInstrs |
| 662 | /// but that code doesn't create loop carried dependences. |
| 663 | void SwingSchedulerDAG::addLoopCarriedDependences(AliasAnalysis *AA) { |
Bjorn Pettersson | 71e8c6f | 2019-04-24 06:55:50 +0000 | [diff] [blame] | 664 | MapVector<const Value *, SmallVector<SUnit *, 4>> PendingLoads; |
Krzysztof Parzyszek | 9f041b1 | 2018-03-26 16:50:11 +0000 | [diff] [blame] | 665 | Value *UnknownValue = |
| 666 | UndefValue::get(Type::getVoidTy(MF.getFunction().getContext())); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 667 | for (auto &SU : SUnits) { |
| 668 | MachineInstr &MI = *SU.getInstr(); |
| 669 | if (isDependenceBarrier(MI, AA)) |
| 670 | PendingLoads.clear(); |
| 671 | else if (MI.mayLoad()) { |
Bjorn Pettersson | 71e8c6f | 2019-04-24 06:55:50 +0000 | [diff] [blame] | 672 | SmallVector<const Value *, 4> Objs; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 673 | getUnderlyingObjects(&MI, Objs, MF.getDataLayout()); |
Krzysztof Parzyszek | 9f041b1 | 2018-03-26 16:50:11 +0000 | [diff] [blame] | 674 | if (Objs.empty()) |
| 675 | Objs.push_back(UnknownValue); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 676 | for (auto V : Objs) { |
| 677 | SmallVector<SUnit *, 4> &SUs = PendingLoads[V]; |
| 678 | SUs.push_back(&SU); |
| 679 | } |
| 680 | } else if (MI.mayStore()) { |
Bjorn Pettersson | 71e8c6f | 2019-04-24 06:55:50 +0000 | [diff] [blame] | 681 | SmallVector<const Value *, 4> Objs; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 682 | getUnderlyingObjects(&MI, Objs, MF.getDataLayout()); |
Krzysztof Parzyszek | 9f041b1 | 2018-03-26 16:50:11 +0000 | [diff] [blame] | 683 | if (Objs.empty()) |
| 684 | Objs.push_back(UnknownValue); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 685 | for (auto V : Objs) { |
Bjorn Pettersson | 71e8c6f | 2019-04-24 06:55:50 +0000 | [diff] [blame] | 686 | MapVector<const Value *, SmallVector<SUnit *, 4>>::iterator I = |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 687 | PendingLoads.find(V); |
| 688 | if (I == PendingLoads.end()) |
| 689 | continue; |
| 690 | for (auto Load : I->second) { |
| 691 | if (isSuccOrder(Load, &SU)) |
| 692 | continue; |
| 693 | MachineInstr &LdMI = *Load->getInstr(); |
| 694 | // First, perform the cheaper check that compares the base register. |
| 695 | // If they are the same and the load offset is less than the store |
| 696 | // offset, then mark the dependence as loop carried potentially. |
Bjorn Pettersson | 238c9d630 | 2019-04-19 09:08:38 +0000 | [diff] [blame] | 697 | const MachineOperand *BaseOp1, *BaseOp2; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 698 | int64_t Offset1, Offset2; |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 699 | if (TII->getMemOperandWithOffset(LdMI, BaseOp1, Offset1, TRI) && |
| 700 | TII->getMemOperandWithOffset(MI, BaseOp2, Offset2, TRI)) { |
| 701 | if (BaseOp1->isIdenticalTo(*BaseOp2) && |
| 702 | (int)Offset1 < (int)Offset2) { |
Changpeng Fang | f5524f0 | 2019-09-26 22:53:44 +0000 | [diff] [blame] | 703 | assert(TII->areMemAccessesTriviallyDisjoint(LdMI, MI) && |
Krzysztof Parzyszek | 9f041b1 | 2018-03-26 16:50:11 +0000 | [diff] [blame] | 704 | "What happened to the chain edge?"); |
| 705 | SDep Dep(Load, SDep::Barrier); |
| 706 | Dep.setLatency(1); |
| 707 | SU.addPred(Dep); |
| 708 | continue; |
| 709 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 710 | } |
| 711 | // Second, the more expensive check that uses alias analysis on the |
| 712 | // base registers. If they alias, and the load offset is less than |
| 713 | // the store offset, the mark the dependence as loop carried. |
| 714 | if (!AA) { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 715 | SDep Dep(Load, SDep::Barrier); |
| 716 | Dep.setLatency(1); |
| 717 | SU.addPred(Dep); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 718 | continue; |
| 719 | } |
| 720 | MachineMemOperand *MMO1 = *LdMI.memoperands_begin(); |
| 721 | MachineMemOperand *MMO2 = *MI.memoperands_begin(); |
| 722 | if (!MMO1->getValue() || !MMO2->getValue()) { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 723 | SDep Dep(Load, SDep::Barrier); |
| 724 | Dep.setLatency(1); |
| 725 | SU.addPred(Dep); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 726 | continue; |
| 727 | } |
| 728 | if (MMO1->getValue() == MMO2->getValue() && |
| 729 | MMO1->getOffset() <= MMO2->getOffset()) { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 730 | SDep Dep(Load, SDep::Barrier); |
| 731 | Dep.setLatency(1); |
| 732 | SU.addPred(Dep); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 733 | continue; |
| 734 | } |
| 735 | AliasResult AAResult = AA->alias( |
George Burgess IV | 6ef8002 | 2018-10-10 21:28:44 +0000 | [diff] [blame] | 736 | MemoryLocation(MMO1->getValue(), LocationSize::unknown(), |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 737 | MMO1->getAAInfo()), |
George Burgess IV | 6ef8002 | 2018-10-10 21:28:44 +0000 | [diff] [blame] | 738 | MemoryLocation(MMO2->getValue(), LocationSize::unknown(), |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 739 | MMO2->getAAInfo())); |
| 740 | |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 741 | if (AAResult != NoAlias) { |
| 742 | SDep Dep(Load, SDep::Barrier); |
| 743 | Dep.setLatency(1); |
| 744 | SU.addPred(Dep); |
| 745 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /// Update the phi dependences to the DAG because ScheduleDAGInstrs no longer |
| 753 | /// processes dependences for PHIs. This function adds true dependences |
| 754 | /// from a PHI to a use, and a loop carried dependence from the use to the |
| 755 | /// PHI. The loop carried dependence is represented as an anti dependence |
| 756 | /// edge. This function also removes chain dependences between unrelated |
| 757 | /// PHIs. |
| 758 | void SwingSchedulerDAG::updatePhiDependences() { |
| 759 | SmallVector<SDep, 4> RemoveDeps; |
| 760 | const TargetSubtargetInfo &ST = MF.getSubtarget<TargetSubtargetInfo>(); |
| 761 | |
| 762 | // Iterate over each DAG node. |
| 763 | for (SUnit &I : SUnits) { |
| 764 | RemoveDeps.clear(); |
| 765 | // Set to true if the instruction has an operand defined by a Phi. |
| 766 | unsigned HasPhiUse = 0; |
| 767 | unsigned HasPhiDef = 0; |
| 768 | MachineInstr *MI = I.getInstr(); |
| 769 | // Iterate over each operand, and we process the definitions. |
| 770 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), |
| 771 | MOE = MI->operands_end(); |
| 772 | MOI != MOE; ++MOI) { |
| 773 | if (!MOI->isReg()) |
| 774 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 775 | Register Reg = MOI->getReg(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 776 | if (MOI->isDef()) { |
| 777 | // If the register is used by a Phi, then create an anti dependence. |
| 778 | for (MachineRegisterInfo::use_instr_iterator |
| 779 | UI = MRI.use_instr_begin(Reg), |
| 780 | UE = MRI.use_instr_end(); |
| 781 | UI != UE; ++UI) { |
| 782 | MachineInstr *UseMI = &*UI; |
| 783 | SUnit *SU = getSUnit(UseMI); |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 784 | if (SU != nullptr && UseMI->isPHI()) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 785 | if (!MI->isPHI()) { |
| 786 | SDep Dep(SU, SDep::Anti, Reg); |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 787 | Dep.setLatency(1); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 788 | I.addPred(Dep); |
| 789 | } else { |
| 790 | HasPhiDef = Reg; |
| 791 | // Add a chain edge to a dependent Phi that isn't an existing |
| 792 | // predecessor. |
| 793 | if (SU->NodeNum < I.NodeNum && !I.isPred(SU)) |
| 794 | I.addPred(SDep(SU, SDep::Barrier)); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | } else if (MOI->isUse()) { |
| 799 | // If the register is defined by a Phi, then create a true dependence. |
| 800 | MachineInstr *DefMI = MRI.getUniqueVRegDef(Reg); |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 801 | if (DefMI == nullptr) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 802 | continue; |
| 803 | SUnit *SU = getSUnit(DefMI); |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 804 | if (SU != nullptr && DefMI->isPHI()) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 805 | if (!MI->isPHI()) { |
| 806 | SDep Dep(SU, SDep::Data, Reg); |
| 807 | Dep.setLatency(0); |
| 808 | ST.adjustSchedDependency(SU, &I, Dep); |
| 809 | I.addPred(Dep); |
| 810 | } else { |
| 811 | HasPhiUse = Reg; |
| 812 | // Add a chain edge to a dependent Phi that isn't an existing |
| 813 | // predecessor. |
| 814 | if (SU->NodeNum < I.NodeNum && !I.isPred(SU)) |
| 815 | I.addPred(SDep(SU, SDep::Barrier)); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | // Remove order dependences from an unrelated Phi. |
| 821 | if (!SwpPruneDeps) |
| 822 | continue; |
| 823 | for (auto &PI : I.Preds) { |
| 824 | MachineInstr *PMI = PI.getSUnit()->getInstr(); |
| 825 | if (PMI->isPHI() && PI.getKind() == SDep::Order) { |
| 826 | if (I.getInstr()->isPHI()) { |
| 827 | if (PMI->getOperand(0).getReg() == HasPhiUse) |
| 828 | continue; |
| 829 | if (getLoopPhiReg(*PMI, PMI->getParent()) == HasPhiDef) |
| 830 | continue; |
| 831 | } |
| 832 | RemoveDeps.push_back(PI); |
| 833 | } |
| 834 | } |
| 835 | for (int i = 0, e = RemoveDeps.size(); i != e; ++i) |
| 836 | I.removePred(RemoveDeps[i]); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /// Iterate over each DAG node and see if we can change any dependences |
| 841 | /// in order to reduce the recurrence MII. |
| 842 | void SwingSchedulerDAG::changeDependences() { |
| 843 | // See if an instruction can use a value from the previous iteration. |
| 844 | // If so, we update the base and offset of the instruction and change |
| 845 | // the dependences. |
| 846 | for (SUnit &I : SUnits) { |
| 847 | unsigned BasePos = 0, OffsetPos = 0, NewBase = 0; |
| 848 | int64_t NewOffset = 0; |
| 849 | if (!canUseLastOffsetValue(I.getInstr(), BasePos, OffsetPos, NewBase, |
| 850 | NewOffset)) |
| 851 | continue; |
| 852 | |
| 853 | // Get the MI and SUnit for the instruction that defines the original base. |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 854 | Register OrigBase = I.getInstr()->getOperand(BasePos).getReg(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 855 | MachineInstr *DefMI = MRI.getUniqueVRegDef(OrigBase); |
| 856 | if (!DefMI) |
| 857 | continue; |
| 858 | SUnit *DefSU = getSUnit(DefMI); |
| 859 | if (!DefSU) |
| 860 | continue; |
| 861 | // Get the MI and SUnit for the instruction that defins the new base. |
| 862 | MachineInstr *LastMI = MRI.getUniqueVRegDef(NewBase); |
| 863 | if (!LastMI) |
| 864 | continue; |
| 865 | SUnit *LastSU = getSUnit(LastMI); |
| 866 | if (!LastSU) |
| 867 | continue; |
| 868 | |
| 869 | if (Topo.IsReachable(&I, LastSU)) |
| 870 | continue; |
| 871 | |
| 872 | // Remove the dependence. The value now depends on a prior iteration. |
| 873 | SmallVector<SDep, 4> Deps; |
| 874 | for (SUnit::pred_iterator P = I.Preds.begin(), E = I.Preds.end(); P != E; |
| 875 | ++P) |
| 876 | if (P->getSUnit() == DefSU) |
| 877 | Deps.push_back(*P); |
| 878 | for (int i = 0, e = Deps.size(); i != e; i++) { |
| 879 | Topo.RemovePred(&I, Deps[i].getSUnit()); |
| 880 | I.removePred(Deps[i]); |
| 881 | } |
| 882 | // Remove the chain dependence between the instructions. |
| 883 | Deps.clear(); |
| 884 | for (auto &P : LastSU->Preds) |
| 885 | if (P.getSUnit() == &I && P.getKind() == SDep::Order) |
| 886 | Deps.push_back(P); |
| 887 | for (int i = 0, e = Deps.size(); i != e; i++) { |
| 888 | Topo.RemovePred(LastSU, Deps[i].getSUnit()); |
| 889 | LastSU->removePred(Deps[i]); |
| 890 | } |
| 891 | |
| 892 | // Add a dependence between the new instruction and the instruction |
| 893 | // that defines the new base. |
| 894 | SDep Dep(&I, SDep::Anti, NewBase); |
Sumanth Gundapaneni | 8916e43 | 2018-10-11 19:42:46 +0000 | [diff] [blame] | 895 | Topo.AddPred(LastSU, &I); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 896 | LastSU->addPred(Dep); |
| 897 | |
| 898 | // Remember the base and offset information so that we can update the |
| 899 | // instruction during code generation. |
| 900 | InstrChanges[&I] = std::make_pair(NewBase, NewOffset); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | namespace { |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 905 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 906 | // FuncUnitSorter - Comparison operator used to sort instructions by |
| 907 | // the number of functional unit choices. |
| 908 | struct FuncUnitSorter { |
| 909 | const InstrItineraryData *InstrItins; |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 910 | const MCSubtargetInfo *STI; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 911 | DenseMap<unsigned, unsigned> Resources; |
| 912 | |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 913 | FuncUnitSorter(const TargetSubtargetInfo &TSI) |
| 914 | : InstrItins(TSI.getInstrItineraryData()), STI(&TSI) {} |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 915 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 916 | // Compute the number of functional unit alternatives needed |
| 917 | // at each stage, and take the minimum value. We prioritize the |
| 918 | // instructions by the least number of choices first. |
| 919 | unsigned minFuncUnits(const MachineInstr *Inst, unsigned &F) const { |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 920 | unsigned SchedClass = Inst->getDesc().getSchedClass(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 921 | unsigned min = UINT_MAX; |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 922 | if (InstrItins && !InstrItins->isEmpty()) { |
| 923 | for (const InstrStage &IS : |
| 924 | make_range(InstrItins->beginStage(SchedClass), |
| 925 | InstrItins->endStage(SchedClass))) { |
| 926 | unsigned funcUnits = IS.getUnits(); |
| 927 | unsigned numAlternatives = countPopulation(funcUnits); |
| 928 | if (numAlternatives < min) { |
| 929 | min = numAlternatives; |
| 930 | F = funcUnits; |
| 931 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 932 | } |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 933 | return min; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 934 | } |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 935 | if (STI && STI->getSchedModel().hasInstrSchedModel()) { |
| 936 | const MCSchedClassDesc *SCDesc = |
| 937 | STI->getSchedModel().getSchedClassDesc(SchedClass); |
| 938 | if (!SCDesc->isValid()) |
| 939 | // No valid Schedule Class Desc for schedClass, should be |
| 940 | // Pseudo/PostRAPseudo |
| 941 | return min; |
| 942 | |
| 943 | for (const MCWriteProcResEntry &PRE : |
| 944 | make_range(STI->getWriteProcResBegin(SCDesc), |
| 945 | STI->getWriteProcResEnd(SCDesc))) { |
| 946 | if (!PRE.Cycles) |
| 947 | continue; |
| 948 | const MCProcResourceDesc *ProcResource = |
| 949 | STI->getSchedModel().getProcResource(PRE.ProcResourceIdx); |
| 950 | unsigned NumUnits = ProcResource->NumUnits; |
| 951 | if (NumUnits < min) { |
| 952 | min = NumUnits; |
| 953 | F = PRE.ProcResourceIdx; |
| 954 | } |
| 955 | } |
| 956 | return min; |
| 957 | } |
| 958 | llvm_unreachable("Should have non-empty InstrItins or hasInstrSchedModel!"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | // Compute the critical resources needed by the instruction. This |
| 962 | // function records the functional units needed by instructions that |
| 963 | // must use only one functional unit. We use this as a tie breaker |
| 964 | // for computing the resource MII. The instrutions that require |
| 965 | // the same, highly used, functional unit have high priority. |
| 966 | void calcCriticalResources(MachineInstr &MI) { |
| 967 | unsigned SchedClass = MI.getDesc().getSchedClass(); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 968 | if (InstrItins && !InstrItins->isEmpty()) { |
| 969 | for (const InstrStage &IS : |
| 970 | make_range(InstrItins->beginStage(SchedClass), |
| 971 | InstrItins->endStage(SchedClass))) { |
| 972 | unsigned FuncUnits = IS.getUnits(); |
| 973 | if (countPopulation(FuncUnits) == 1) |
| 974 | Resources[FuncUnits]++; |
| 975 | } |
| 976 | return; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 977 | } |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 978 | if (STI && STI->getSchedModel().hasInstrSchedModel()) { |
| 979 | const MCSchedClassDesc *SCDesc = |
| 980 | STI->getSchedModel().getSchedClassDesc(SchedClass); |
| 981 | if (!SCDesc->isValid()) |
| 982 | // No valid Schedule Class Desc for schedClass, should be |
| 983 | // Pseudo/PostRAPseudo |
| 984 | return; |
| 985 | |
| 986 | for (const MCWriteProcResEntry &PRE : |
| 987 | make_range(STI->getWriteProcResBegin(SCDesc), |
| 988 | STI->getWriteProcResEnd(SCDesc))) { |
| 989 | if (!PRE.Cycles) |
| 990 | continue; |
| 991 | Resources[PRE.ProcResourceIdx]++; |
| 992 | } |
| 993 | return; |
| 994 | } |
| 995 | llvm_unreachable("Should have non-empty InstrItins or hasInstrSchedModel!"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 998 | /// Return true if IS1 has less priority than IS2. |
| 999 | bool operator()(const MachineInstr *IS1, const MachineInstr *IS2) const { |
| 1000 | unsigned F1 = 0, F2 = 0; |
| 1001 | unsigned MFUs1 = minFuncUnits(IS1, F1); |
| 1002 | unsigned MFUs2 = minFuncUnits(IS2, F2); |
Jinsong Ji | 6349ce5 | 2019-08-09 14:10:57 +0000 | [diff] [blame] | 1003 | if (MFUs1 == MFUs2) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1004 | return Resources.lookup(F1) < Resources.lookup(F2); |
| 1005 | return MFUs1 > MFUs2; |
| 1006 | } |
| 1007 | }; |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 1008 | |
| 1009 | } // end anonymous namespace |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1010 | |
| 1011 | /// Calculate the resource constrained minimum initiation interval for the |
| 1012 | /// specified loop. We use the DFA to model the resources needed for |
| 1013 | /// each instruction, and we ignore dependences. A different DFA is created |
| 1014 | /// for each cycle that is required. When adding a new instruction, we attempt |
| 1015 | /// to add it to each existing DFA, until a legal space is found. If the |
| 1016 | /// instruction cannot be reserved in an existing DFA, we create a new one. |
| 1017 | unsigned SwingSchedulerDAG::calculateResMII() { |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1018 | |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1019 | LLVM_DEBUG(dbgs() << "calculateResMII:\n"); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1020 | SmallVector<ResourceManager*, 8> Resources; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1021 | MachineBasicBlock *MBB = Loop.getHeader(); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1022 | Resources.push_back(new ResourceManager(&MF.getSubtarget())); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1023 | |
| 1024 | // Sort the instructions by the number of available choices for scheduling, |
| 1025 | // least to most. Use the number of critical resources as the tie breaker. |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1026 | FuncUnitSorter FUS = FuncUnitSorter(MF.getSubtarget()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1027 | for (MachineBasicBlock::iterator I = MBB->getFirstNonPHI(), |
| 1028 | E = MBB->getFirstTerminator(); |
| 1029 | I != E; ++I) |
| 1030 | FUS.calcCriticalResources(*I); |
| 1031 | PriorityQueue<MachineInstr *, std::vector<MachineInstr *>, FuncUnitSorter> |
| 1032 | FuncUnitOrder(FUS); |
| 1033 | |
| 1034 | for (MachineBasicBlock::iterator I = MBB->getFirstNonPHI(), |
| 1035 | E = MBB->getFirstTerminator(); |
| 1036 | I != E; ++I) |
| 1037 | FuncUnitOrder.push(&*I); |
| 1038 | |
| 1039 | while (!FuncUnitOrder.empty()) { |
| 1040 | MachineInstr *MI = FuncUnitOrder.top(); |
| 1041 | FuncUnitOrder.pop(); |
| 1042 | if (TII->isZeroCost(MI->getOpcode())) |
| 1043 | continue; |
| 1044 | // Attempt to reserve the instruction in an existing DFA. At least one |
| 1045 | // DFA is needed for each cycle. |
| 1046 | unsigned NumCycles = getSUnit(MI)->Latency; |
| 1047 | unsigned ReservedCycles = 0; |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1048 | SmallVectorImpl<ResourceManager *>::iterator RI = Resources.begin(); |
| 1049 | SmallVectorImpl<ResourceManager *>::iterator RE = Resources.end(); |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1050 | LLVM_DEBUG({ |
| 1051 | dbgs() << "Trying to reserve resource for " << NumCycles |
| 1052 | << " cycles for \n"; |
| 1053 | MI->dump(); |
| 1054 | }); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1055 | for (unsigned C = 0; C < NumCycles; ++C) |
| 1056 | while (RI != RE) { |
Jinsong Ji | fee855b | 2019-06-25 21:50:56 +0000 | [diff] [blame] | 1057 | if ((*RI)->canReserveResources(*MI)) { |
| 1058 | (*RI)->reserveResources(*MI); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1059 | ++ReservedCycles; |
| 1060 | break; |
| 1061 | } |
Jinsong Ji | fee855b | 2019-06-25 21:50:56 +0000 | [diff] [blame] | 1062 | RI++; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1063 | } |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1064 | LLVM_DEBUG(dbgs() << "ReservedCycles:" << ReservedCycles |
| 1065 | << ", NumCycles:" << NumCycles << "\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1066 | // Add new DFAs, if needed, to reserve resources. |
| 1067 | for (unsigned C = ReservedCycles; C < NumCycles; ++C) { |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 1068 | LLVM_DEBUG(if (SwpDebugResource) dbgs() |
| 1069 | << "NewResource created to reserve resources" |
| 1070 | << "\n"); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1071 | ResourceManager *NewResource = new ResourceManager(&MF.getSubtarget()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1072 | assert(NewResource->canReserveResources(*MI) && "Reserve error."); |
| 1073 | NewResource->reserveResources(*MI); |
| 1074 | Resources.push_back(NewResource); |
| 1075 | } |
| 1076 | } |
| 1077 | int Resmii = Resources.size(); |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1078 | LLVM_DEBUG(dbgs() << "Retrun Res MII:" << Resmii << "\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1079 | // Delete the memory for each of the DFAs that were created earlier. |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 1080 | for (ResourceManager *RI : Resources) { |
| 1081 | ResourceManager *D = RI; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1082 | delete D; |
| 1083 | } |
| 1084 | Resources.clear(); |
| 1085 | return Resmii; |
| 1086 | } |
| 1087 | |
| 1088 | /// Calculate the recurrence-constrainted minimum initiation interval. |
| 1089 | /// Iterate over each circuit. Compute the delay(c) and distance(c) |
| 1090 | /// for each circuit. The II needs to satisfy the inequality |
| 1091 | /// delay(c) - II*distance(c) <= 0. For each circuit, choose the smallest |
Hiroshi Inoue | c73b6d6 | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 1092 | /// II that satisfies the inequality, and the RecMII is the maximum |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1093 | /// of those values. |
| 1094 | unsigned SwingSchedulerDAG::calculateRecMII(NodeSetType &NodeSets) { |
| 1095 | unsigned RecMII = 0; |
| 1096 | |
| 1097 | for (NodeSet &Nodes : NodeSets) { |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1098 | if (Nodes.empty()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1099 | continue; |
| 1100 | |
Krzysztof Parzyszek | a212204 | 2018-03-26 16:33:16 +0000 | [diff] [blame] | 1101 | unsigned Delay = Nodes.getLatency(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1102 | unsigned Distance = 1; |
| 1103 | |
| 1104 | // ii = ceil(delay / distance) |
| 1105 | unsigned CurMII = (Delay + Distance - 1) / Distance; |
| 1106 | Nodes.setRecMII(CurMII); |
| 1107 | if (CurMII > RecMII) |
| 1108 | RecMII = CurMII; |
| 1109 | } |
| 1110 | |
| 1111 | return RecMII; |
| 1112 | } |
| 1113 | |
| 1114 | /// Swap all the anti dependences in the DAG. That means it is no longer a DAG, |
| 1115 | /// but we do this to find the circuits, and then change them back. |
| 1116 | static void swapAntiDependences(std::vector<SUnit> &SUnits) { |
| 1117 | SmallVector<std::pair<SUnit *, SDep>, 8> DepsAdded; |
| 1118 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 1119 | SUnit *SU = &SUnits[i]; |
| 1120 | for (SUnit::pred_iterator IP = SU->Preds.begin(), EP = SU->Preds.end(); |
| 1121 | IP != EP; ++IP) { |
| 1122 | if (IP->getKind() != SDep::Anti) |
| 1123 | continue; |
| 1124 | DepsAdded.push_back(std::make_pair(SU, *IP)); |
| 1125 | } |
| 1126 | } |
| 1127 | for (SmallVector<std::pair<SUnit *, SDep>, 8>::iterator I = DepsAdded.begin(), |
| 1128 | E = DepsAdded.end(); |
| 1129 | I != E; ++I) { |
| 1130 | // Remove this anti dependency and add one in the reverse direction. |
| 1131 | SUnit *SU = I->first; |
| 1132 | SDep &D = I->second; |
| 1133 | SUnit *TargetSU = D.getSUnit(); |
| 1134 | unsigned Reg = D.getReg(); |
| 1135 | unsigned Lat = D.getLatency(); |
| 1136 | SU->removePred(D); |
| 1137 | SDep Dep(SU, SDep::Anti, Reg); |
| 1138 | Dep.setLatency(Lat); |
| 1139 | TargetSU->addPred(Dep); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /// Create the adjacency structure of the nodes in the graph. |
| 1144 | void SwingSchedulerDAG::Circuits::createAdjacencyStructure( |
| 1145 | SwingSchedulerDAG *DAG) { |
| 1146 | BitVector Added(SUnits.size()); |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 1147 | DenseMap<int, int> OutputDeps; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1148 | for (int i = 0, e = SUnits.size(); i != e; ++i) { |
| 1149 | Added.reset(); |
| 1150 | // Add any successor to the adjacency matrix and exclude duplicates. |
| 1151 | for (auto &SI : SUnits[i].Succs) { |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 1152 | // Only create a back-edge on the first and last nodes of a dependence |
| 1153 | // chain. This records any chains and adds them later. |
| 1154 | if (SI.getKind() == SDep::Output) { |
| 1155 | int N = SI.getSUnit()->NodeNum; |
| 1156 | int BackEdge = i; |
| 1157 | auto Dep = OutputDeps.find(BackEdge); |
| 1158 | if (Dep != OutputDeps.end()) { |
| 1159 | BackEdge = Dep->second; |
| 1160 | OutputDeps.erase(Dep); |
| 1161 | } |
| 1162 | OutputDeps[N] = BackEdge; |
| 1163 | } |
Sumanth Gundapaneni | ada0f51 | 2018-10-25 21:27:08 +0000 | [diff] [blame] | 1164 | // Do not process a boundary node, an artificial node. |
| 1165 | // A back-edge is processed only if it goes to a Phi. |
| 1166 | if (SI.getSUnit()->isBoundaryNode() || SI.isArtificial() || |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1167 | (SI.getKind() == SDep::Anti && !SI.getSUnit()->getInstr()->isPHI())) |
| 1168 | continue; |
| 1169 | int N = SI.getSUnit()->NodeNum; |
| 1170 | if (!Added.test(N)) { |
| 1171 | AdjK[i].push_back(N); |
| 1172 | Added.set(N); |
| 1173 | } |
| 1174 | } |
| 1175 | // A chain edge between a store and a load is treated as a back-edge in the |
| 1176 | // adjacency matrix. |
| 1177 | for (auto &PI : SUnits[i].Preds) { |
| 1178 | if (!SUnits[i].getInstr()->mayStore() || |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 1179 | !DAG->isLoopCarriedDep(&SUnits[i], PI, false)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1180 | continue; |
| 1181 | if (PI.getKind() == SDep::Order && PI.getSUnit()->getInstr()->mayLoad()) { |
| 1182 | int N = PI.getSUnit()->NodeNum; |
| 1183 | if (!Added.test(N)) { |
| 1184 | AdjK[i].push_back(N); |
| 1185 | Added.set(N); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | } |
Hiroshi Inoue | dad8c6a | 2019-01-09 05:11:10 +0000 | [diff] [blame] | 1190 | // Add back-edges in the adjacency matrix for the output dependences. |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 1191 | for (auto &OD : OutputDeps) |
| 1192 | if (!Added.test(OD.second)) { |
| 1193 | AdjK[OD.first].push_back(OD.second); |
| 1194 | Added.set(OD.second); |
| 1195 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /// Identify an elementary circuit in the dependence graph starting at the |
| 1199 | /// specified node. |
| 1200 | bool SwingSchedulerDAG::Circuits::circuit(int V, int S, NodeSetType &NodeSets, |
| 1201 | bool HasBackedge) { |
| 1202 | SUnit *SV = &SUnits[V]; |
| 1203 | bool F = false; |
| 1204 | Stack.insert(SV); |
| 1205 | Blocked.set(V); |
| 1206 | |
| 1207 | for (auto W : AdjK[V]) { |
| 1208 | if (NumPaths > MaxPaths) |
| 1209 | break; |
| 1210 | if (W < S) |
| 1211 | continue; |
| 1212 | if (W == S) { |
| 1213 | if (!HasBackedge) |
| 1214 | NodeSets.push_back(NodeSet(Stack.begin(), Stack.end())); |
| 1215 | F = true; |
| 1216 | ++NumPaths; |
| 1217 | break; |
| 1218 | } else if (!Blocked.test(W)) { |
Sumanth Gundapaneni | 77418a3 | 2018-10-11 19:45:07 +0000 | [diff] [blame] | 1219 | if (circuit(W, S, NodeSets, |
| 1220 | Node2Idx->at(W) < Node2Idx->at(V) ? true : HasBackedge)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1221 | F = true; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (F) |
| 1226 | unblock(V); |
| 1227 | else { |
| 1228 | for (auto W : AdjK[V]) { |
| 1229 | if (W < S) |
| 1230 | continue; |
| 1231 | if (B[W].count(SV) == 0) |
| 1232 | B[W].insert(SV); |
| 1233 | } |
| 1234 | } |
| 1235 | Stack.pop_back(); |
| 1236 | return F; |
| 1237 | } |
| 1238 | |
| 1239 | /// Unblock a node in the circuit finding algorithm. |
| 1240 | void SwingSchedulerDAG::Circuits::unblock(int U) { |
| 1241 | Blocked.reset(U); |
| 1242 | SmallPtrSet<SUnit *, 4> &BU = B[U]; |
| 1243 | while (!BU.empty()) { |
| 1244 | SmallPtrSet<SUnit *, 4>::iterator SI = BU.begin(); |
| 1245 | assert(SI != BU.end() && "Invalid B set."); |
| 1246 | SUnit *W = *SI; |
| 1247 | BU.erase(W); |
| 1248 | if (Blocked.test(W->NodeNum)) |
| 1249 | unblock(W->NodeNum); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | /// Identify all the elementary circuits in the dependence graph using |
| 1254 | /// Johnson's circuit algorithm. |
| 1255 | void SwingSchedulerDAG::findCircuits(NodeSetType &NodeSets) { |
| 1256 | // Swap all the anti dependences in the DAG. That means it is no longer a DAG, |
| 1257 | // but we do this to find the circuits, and then change them back. |
| 1258 | swapAntiDependences(SUnits); |
| 1259 | |
Sumanth Gundapaneni | 77418a3 | 2018-10-11 19:45:07 +0000 | [diff] [blame] | 1260 | Circuits Cir(SUnits, Topo); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1261 | // Create the adjacency structure. |
| 1262 | Cir.createAdjacencyStructure(this); |
| 1263 | for (int i = 0, e = SUnits.size(); i != e; ++i) { |
| 1264 | Cir.reset(); |
| 1265 | Cir.circuit(i, i, NodeSets); |
| 1266 | } |
| 1267 | |
| 1268 | // Change the dependences back so that we've created a DAG again. |
| 1269 | swapAntiDependences(SUnits); |
| 1270 | } |
| 1271 | |
Sumanth Gundapaneni | 62ac69d | 2018-10-18 15:51:16 +0000 | [diff] [blame] | 1272 | // Create artificial dependencies between the source of COPY/REG_SEQUENCE that |
| 1273 | // is loop-carried to the USE in next iteration. This will help pipeliner avoid |
| 1274 | // additional copies that are needed across iterations. An artificial dependence |
| 1275 | // edge is added from USE to SOURCE of COPY/REG_SEQUENCE. |
| 1276 | |
| 1277 | // PHI-------Anti-Dep-----> COPY/REG_SEQUENCE (loop-carried) |
| 1278 | // SRCOfCopY------True-Dep---> COPY/REG_SEQUENCE |
| 1279 | // PHI-------True-Dep------> USEOfPhi |
| 1280 | |
| 1281 | // The mutation creates |
| 1282 | // USEOfPHI -------Artificial-Dep---> SRCOfCopy |
| 1283 | |
| 1284 | // This overall will ensure, the USEOfPHI is scheduled before SRCOfCopy |
| 1285 | // (since USE is a predecessor), implies, the COPY/ REG_SEQUENCE is scheduled |
| 1286 | // late to avoid additional copies across iterations. The possible scheduling |
| 1287 | // order would be |
| 1288 | // USEOfPHI --- SRCOfCopy--- COPY/REG_SEQUENCE. |
| 1289 | |
| 1290 | void SwingSchedulerDAG::CopyToPhiMutation::apply(ScheduleDAGInstrs *DAG) { |
| 1291 | for (SUnit &SU : DAG->SUnits) { |
| 1292 | // Find the COPY/REG_SEQUENCE instruction. |
| 1293 | if (!SU.getInstr()->isCopy() && !SU.getInstr()->isRegSequence()) |
| 1294 | continue; |
| 1295 | |
| 1296 | // Record the loop carried PHIs. |
| 1297 | SmallVector<SUnit *, 4> PHISUs; |
| 1298 | // Record the SrcSUs that feed the COPY/REG_SEQUENCE instructions. |
| 1299 | SmallVector<SUnit *, 4> SrcSUs; |
| 1300 | |
| 1301 | for (auto &Dep : SU.Preds) { |
| 1302 | SUnit *TmpSU = Dep.getSUnit(); |
| 1303 | MachineInstr *TmpMI = TmpSU->getInstr(); |
| 1304 | SDep::Kind DepKind = Dep.getKind(); |
| 1305 | // Save the loop carried PHI. |
| 1306 | if (DepKind == SDep::Anti && TmpMI->isPHI()) |
| 1307 | PHISUs.push_back(TmpSU); |
| 1308 | // Save the source of COPY/REG_SEQUENCE. |
| 1309 | // If the source has no pre-decessors, we will end up creating cycles. |
| 1310 | else if (DepKind == SDep::Data && !TmpMI->isPHI() && TmpSU->NumPreds > 0) |
| 1311 | SrcSUs.push_back(TmpSU); |
| 1312 | } |
| 1313 | |
| 1314 | if (PHISUs.size() == 0 || SrcSUs.size() == 0) |
| 1315 | continue; |
| 1316 | |
| 1317 | // Find the USEs of PHI. If the use is a PHI or REG_SEQUENCE, push back this |
| 1318 | // SUnit to the container. |
| 1319 | SmallVector<SUnit *, 8> UseSUs; |
| 1320 | for (auto I = PHISUs.begin(); I != PHISUs.end(); ++I) { |
| 1321 | for (auto &Dep : (*I)->Succs) { |
| 1322 | if (Dep.getKind() != SDep::Data) |
| 1323 | continue; |
| 1324 | |
| 1325 | SUnit *TmpSU = Dep.getSUnit(); |
| 1326 | MachineInstr *TmpMI = TmpSU->getInstr(); |
| 1327 | if (TmpMI->isPHI() || TmpMI->isRegSequence()) { |
| 1328 | PHISUs.push_back(TmpSU); |
| 1329 | continue; |
| 1330 | } |
| 1331 | UseSUs.push_back(TmpSU); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (UseSUs.size() == 0) |
| 1336 | continue; |
| 1337 | |
| 1338 | SwingSchedulerDAG *SDAG = cast<SwingSchedulerDAG>(DAG); |
| 1339 | // Add the artificial dependencies if it does not form a cycle. |
| 1340 | for (auto I : UseSUs) { |
| 1341 | for (auto Src : SrcSUs) { |
| 1342 | if (!SDAG->Topo.IsReachable(I, Src) && Src != I) { |
| 1343 | Src->addPred(SDep(I, SDep::Artificial)); |
| 1344 | SDAG->Topo.AddPred(Src, I); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1351 | /// Return true for DAG nodes that we ignore when computing the cost functions. |
Hiroshi Inoue | c73b6d6 | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 1352 | /// We ignore the back-edge recurrence in order to avoid unbounded recursion |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1353 | /// in the calculation of the ASAP, ALAP, etc functions. |
| 1354 | static bool ignoreDependence(const SDep &D, bool isPred) { |
| 1355 | if (D.isArtificial()) |
| 1356 | return true; |
| 1357 | return D.getKind() == SDep::Anti && isPred; |
| 1358 | } |
| 1359 | |
| 1360 | /// Compute several functions need to order the nodes for scheduling. |
| 1361 | /// ASAP - Earliest time to schedule a node. |
| 1362 | /// ALAP - Latest time to schedule a node. |
| 1363 | /// MOV - Mobility function, difference between ALAP and ASAP. |
| 1364 | /// D - Depth of each node. |
| 1365 | /// H - Height of each node. |
| 1366 | void SwingSchedulerDAG::computeNodeFunctions(NodeSetType &NodeSets) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1367 | ScheduleInfo.resize(SUnits.size()); |
| 1368 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1369 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1370 | for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(), |
| 1371 | E = Topo.end(); |
| 1372 | I != E; ++I) { |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 1373 | const SUnit &SU = SUnits[*I]; |
| 1374 | dumpNode(SU); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1375 | } |
| 1376 | }); |
| 1377 | |
| 1378 | int maxASAP = 0; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1379 | // Compute ASAP and ZeroLatencyDepth. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1380 | for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(), |
| 1381 | E = Topo.end(); |
| 1382 | I != E; ++I) { |
| 1383 | int asap = 0; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1384 | int zeroLatencyDepth = 0; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1385 | SUnit *SU = &SUnits[*I]; |
| 1386 | for (SUnit::const_pred_iterator IP = SU->Preds.begin(), |
| 1387 | EP = SU->Preds.end(); |
| 1388 | IP != EP; ++IP) { |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1389 | SUnit *pred = IP->getSUnit(); |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 1390 | if (IP->getLatency() == 0) |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1391 | zeroLatencyDepth = |
| 1392 | std::max(zeroLatencyDepth, getZeroLatencyDepth(pred) + 1); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1393 | if (ignoreDependence(*IP, true)) |
| 1394 | continue; |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 1395 | asap = std::max(asap, (int)(getASAP(pred) + IP->getLatency() - |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1396 | getDistance(pred, SU, *IP) * MII)); |
| 1397 | } |
| 1398 | maxASAP = std::max(maxASAP, asap); |
| 1399 | ScheduleInfo[*I].ASAP = asap; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1400 | ScheduleInfo[*I].ZeroLatencyDepth = zeroLatencyDepth; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1403 | // Compute ALAP, ZeroLatencyHeight, and MOV. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1404 | for (ScheduleDAGTopologicalSort::const_reverse_iterator I = Topo.rbegin(), |
| 1405 | E = Topo.rend(); |
| 1406 | I != E; ++I) { |
| 1407 | int alap = maxASAP; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1408 | int zeroLatencyHeight = 0; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1409 | SUnit *SU = &SUnits[*I]; |
| 1410 | for (SUnit::const_succ_iterator IS = SU->Succs.begin(), |
| 1411 | ES = SU->Succs.end(); |
| 1412 | IS != ES; ++IS) { |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1413 | SUnit *succ = IS->getSUnit(); |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 1414 | if (IS->getLatency() == 0) |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1415 | zeroLatencyHeight = |
| 1416 | std::max(zeroLatencyHeight, getZeroLatencyHeight(succ) + 1); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1417 | if (ignoreDependence(*IS, true)) |
| 1418 | continue; |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 1419 | alap = std::min(alap, (int)(getALAP(succ) - IS->getLatency() + |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1420 | getDistance(SU, succ, *IS) * MII)); |
| 1421 | } |
| 1422 | |
| 1423 | ScheduleInfo[*I].ALAP = alap; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1424 | ScheduleInfo[*I].ZeroLatencyHeight = zeroLatencyHeight; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | // After computing the node functions, compute the summary for each node set. |
| 1428 | for (NodeSet &I : NodeSets) |
| 1429 | I.computeNodeSetInfo(this); |
| 1430 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1431 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1432 | for (unsigned i = 0; i < SUnits.size(); i++) { |
| 1433 | dbgs() << "\tNode " << i << ":\n"; |
| 1434 | dbgs() << "\t ASAP = " << getASAP(&SUnits[i]) << "\n"; |
| 1435 | dbgs() << "\t ALAP = " << getALAP(&SUnits[i]) << "\n"; |
| 1436 | dbgs() << "\t MOV = " << getMOV(&SUnits[i]) << "\n"; |
| 1437 | dbgs() << "\t D = " << getDepth(&SUnits[i]) << "\n"; |
| 1438 | dbgs() << "\t H = " << getHeight(&SUnits[i]) << "\n"; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1439 | dbgs() << "\t ZLD = " << getZeroLatencyDepth(&SUnits[i]) << "\n"; |
| 1440 | dbgs() << "\t ZLH = " << getZeroLatencyHeight(&SUnits[i]) << "\n"; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1441 | } |
| 1442 | }); |
| 1443 | } |
| 1444 | |
| 1445 | /// Compute the Pred_L(O) set, as defined in the paper. The set is defined |
| 1446 | /// as the predecessors of the elements of NodeOrder that are not also in |
| 1447 | /// NodeOrder. |
| 1448 | static bool pred_L(SetVector<SUnit *> &NodeOrder, |
| 1449 | SmallSetVector<SUnit *, 8> &Preds, |
| 1450 | const NodeSet *S = nullptr) { |
| 1451 | Preds.clear(); |
| 1452 | for (SetVector<SUnit *>::iterator I = NodeOrder.begin(), E = NodeOrder.end(); |
| 1453 | I != E; ++I) { |
| 1454 | for (SUnit::pred_iterator PI = (*I)->Preds.begin(), PE = (*I)->Preds.end(); |
| 1455 | PI != PE; ++PI) { |
| 1456 | if (S && S->count(PI->getSUnit()) == 0) |
| 1457 | continue; |
| 1458 | if (ignoreDependence(*PI, true)) |
| 1459 | continue; |
| 1460 | if (NodeOrder.count(PI->getSUnit()) == 0) |
| 1461 | Preds.insert(PI->getSUnit()); |
| 1462 | } |
| 1463 | // Back-edges are predecessors with an anti-dependence. |
| 1464 | for (SUnit::const_succ_iterator IS = (*I)->Succs.begin(), |
| 1465 | ES = (*I)->Succs.end(); |
| 1466 | IS != ES; ++IS) { |
| 1467 | if (IS->getKind() != SDep::Anti) |
| 1468 | continue; |
| 1469 | if (S && S->count(IS->getSUnit()) == 0) |
| 1470 | continue; |
| 1471 | if (NodeOrder.count(IS->getSUnit()) == 0) |
| 1472 | Preds.insert(IS->getSUnit()); |
| 1473 | } |
| 1474 | } |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1475 | return !Preds.empty(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /// Compute the Succ_L(O) set, as defined in the paper. The set is defined |
| 1479 | /// as the successors of the elements of NodeOrder that are not also in |
| 1480 | /// NodeOrder. |
| 1481 | static bool succ_L(SetVector<SUnit *> &NodeOrder, |
| 1482 | SmallSetVector<SUnit *, 8> &Succs, |
| 1483 | const NodeSet *S = nullptr) { |
| 1484 | Succs.clear(); |
| 1485 | for (SetVector<SUnit *>::iterator I = NodeOrder.begin(), E = NodeOrder.end(); |
| 1486 | I != E; ++I) { |
| 1487 | for (SUnit::succ_iterator SI = (*I)->Succs.begin(), SE = (*I)->Succs.end(); |
| 1488 | SI != SE; ++SI) { |
| 1489 | if (S && S->count(SI->getSUnit()) == 0) |
| 1490 | continue; |
| 1491 | if (ignoreDependence(*SI, false)) |
| 1492 | continue; |
| 1493 | if (NodeOrder.count(SI->getSUnit()) == 0) |
| 1494 | Succs.insert(SI->getSUnit()); |
| 1495 | } |
| 1496 | for (SUnit::const_pred_iterator PI = (*I)->Preds.begin(), |
| 1497 | PE = (*I)->Preds.end(); |
| 1498 | PI != PE; ++PI) { |
| 1499 | if (PI->getKind() != SDep::Anti) |
| 1500 | continue; |
| 1501 | if (S && S->count(PI->getSUnit()) == 0) |
| 1502 | continue; |
| 1503 | if (NodeOrder.count(PI->getSUnit()) == 0) |
| 1504 | Succs.insert(PI->getSUnit()); |
| 1505 | } |
| 1506 | } |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1507 | return !Succs.empty(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /// Return true if there is a path from the specified node to any of the nodes |
| 1511 | /// in DestNodes. Keep track and return the nodes in any path. |
| 1512 | static bool computePath(SUnit *Cur, SetVector<SUnit *> &Path, |
| 1513 | SetVector<SUnit *> &DestNodes, |
| 1514 | SetVector<SUnit *> &Exclude, |
| 1515 | SmallPtrSet<SUnit *, 8> &Visited) { |
| 1516 | if (Cur->isBoundaryNode()) |
| 1517 | return false; |
| 1518 | if (Exclude.count(Cur) != 0) |
| 1519 | return false; |
| 1520 | if (DestNodes.count(Cur) != 0) |
| 1521 | return true; |
| 1522 | if (!Visited.insert(Cur).second) |
| 1523 | return Path.count(Cur) != 0; |
| 1524 | bool FoundPath = false; |
| 1525 | for (auto &SI : Cur->Succs) |
| 1526 | FoundPath |= computePath(SI.getSUnit(), Path, DestNodes, Exclude, Visited); |
| 1527 | for (auto &PI : Cur->Preds) |
| 1528 | if (PI.getKind() == SDep::Anti) |
| 1529 | FoundPath |= |
| 1530 | computePath(PI.getSUnit(), Path, DestNodes, Exclude, Visited); |
| 1531 | if (FoundPath) |
| 1532 | Path.insert(Cur); |
| 1533 | return FoundPath; |
| 1534 | } |
| 1535 | |
| 1536 | /// Return true if Set1 is a subset of Set2. |
| 1537 | template <class S1Ty, class S2Ty> static bool isSubset(S1Ty &Set1, S2Ty &Set2) { |
| 1538 | for (typename S1Ty::iterator I = Set1.begin(), E = Set1.end(); I != E; ++I) |
| 1539 | if (Set2.count(*I) == 0) |
| 1540 | return false; |
| 1541 | return true; |
| 1542 | } |
| 1543 | |
| 1544 | /// Compute the live-out registers for the instructions in a node-set. |
| 1545 | /// The live-out registers are those that are defined in the node-set, |
| 1546 | /// but not used. Except for use operands of Phis. |
| 1547 | static void computeLiveOuts(MachineFunction &MF, RegPressureTracker &RPTracker, |
| 1548 | NodeSet &NS) { |
| 1549 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 1550 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 1551 | SmallVector<RegisterMaskPair, 8> LiveOutRegs; |
| 1552 | SmallSet<unsigned, 4> Uses; |
| 1553 | for (SUnit *SU : NS) { |
| 1554 | const MachineInstr *MI = SU->getInstr(); |
| 1555 | if (MI->isPHI()) |
| 1556 | continue; |
Matthias Braun | fc37155 | 2016-10-24 21:36:43 +0000 | [diff] [blame] | 1557 | for (const MachineOperand &MO : MI->operands()) |
| 1558 | if (MO.isReg() && MO.isUse()) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1559 | Register Reg = MO.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 1560 | if (Register::isVirtualRegister(Reg)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1561 | Uses.insert(Reg); |
| 1562 | else if (MRI.isAllocatable(Reg)) |
| 1563 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) |
| 1564 | Uses.insert(*Units); |
| 1565 | } |
| 1566 | } |
| 1567 | for (SUnit *SU : NS) |
Matthias Braun | fc37155 | 2016-10-24 21:36:43 +0000 | [diff] [blame] | 1568 | for (const MachineOperand &MO : SU->getInstr()->operands()) |
| 1569 | if (MO.isReg() && MO.isDef() && !MO.isDead()) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1570 | Register Reg = MO.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 1571 | if (Register::isVirtualRegister(Reg)) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1572 | if (!Uses.count(Reg)) |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1573 | LiveOutRegs.push_back(RegisterMaskPair(Reg, |
| 1574 | LaneBitmask::getNone())); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1575 | } else if (MRI.isAllocatable(Reg)) { |
| 1576 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) |
| 1577 | if (!Uses.count(*Units)) |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1578 | LiveOutRegs.push_back(RegisterMaskPair(*Units, |
| 1579 | LaneBitmask::getNone())); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | RPTracker.addLiveRegs(LiveOutRegs); |
| 1583 | } |
| 1584 | |
| 1585 | /// A heuristic to filter nodes in recurrent node-sets if the register |
| 1586 | /// pressure of a set is too high. |
| 1587 | void SwingSchedulerDAG::registerPressureFilter(NodeSetType &NodeSets) { |
| 1588 | for (auto &NS : NodeSets) { |
| 1589 | // Skip small node-sets since they won't cause register pressure problems. |
| 1590 | if (NS.size() <= 2) |
| 1591 | continue; |
| 1592 | IntervalPressure RecRegPressure; |
| 1593 | RegPressureTracker RecRPTracker(RecRegPressure); |
| 1594 | RecRPTracker.init(&MF, &RegClassInfo, &LIS, BB, BB->end(), false, true); |
| 1595 | computeLiveOuts(MF, RecRPTracker, NS); |
| 1596 | RecRPTracker.closeBottom(); |
| 1597 | |
| 1598 | std::vector<SUnit *> SUnits(NS.begin(), NS.end()); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1599 | llvm::sort(SUnits, [](const SUnit *A, const SUnit *B) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1600 | return A->NodeNum > B->NodeNum; |
| 1601 | }); |
| 1602 | |
| 1603 | for (auto &SU : SUnits) { |
| 1604 | // Since we're computing the register pressure for a subset of the |
| 1605 | // instructions in a block, we need to set the tracker for each |
| 1606 | // instruction in the node-set. The tracker is set to the instruction |
| 1607 | // just after the one we're interested in. |
| 1608 | MachineBasicBlock::const_iterator CurInstI = SU->getInstr(); |
| 1609 | RecRPTracker.setPos(std::next(CurInstI)); |
| 1610 | |
| 1611 | RegPressureDelta RPDelta; |
| 1612 | ArrayRef<PressureChange> CriticalPSets; |
| 1613 | RecRPTracker.getMaxUpwardPressureDelta(SU->getInstr(), nullptr, RPDelta, |
| 1614 | CriticalPSets, |
| 1615 | RecRegPressure.MaxSetPressure); |
| 1616 | if (RPDelta.Excess.isValid()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1617 | LLVM_DEBUG( |
| 1618 | dbgs() << "Excess register pressure: SU(" << SU->NodeNum << ") " |
| 1619 | << TRI->getRegPressureSetName(RPDelta.Excess.getPSet()) |
| 1620 | << ":" << RPDelta.Excess.getUnitInc()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1621 | NS.setExceedPressure(SU); |
| 1622 | break; |
| 1623 | } |
| 1624 | RecRPTracker.recede(); |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /// A heuristic to colocate node sets that have the same set of |
| 1630 | /// successors. |
| 1631 | void SwingSchedulerDAG::colocateNodeSets(NodeSetType &NodeSets) { |
| 1632 | unsigned Colocate = 0; |
| 1633 | for (int i = 0, e = NodeSets.size(); i < e; ++i) { |
| 1634 | NodeSet &N1 = NodeSets[i]; |
| 1635 | SmallSetVector<SUnit *, 8> S1; |
| 1636 | if (N1.empty() || !succ_L(N1, S1)) |
| 1637 | continue; |
| 1638 | for (int j = i + 1; j < e; ++j) { |
| 1639 | NodeSet &N2 = NodeSets[j]; |
| 1640 | if (N1.compareRecMII(N2) != 0) |
| 1641 | continue; |
| 1642 | SmallSetVector<SUnit *, 8> S2; |
| 1643 | if (N2.empty() || !succ_L(N2, S2)) |
| 1644 | continue; |
| 1645 | if (isSubset(S1, S2) && S1.size() == S2.size()) { |
| 1646 | N1.setColocate(++Colocate); |
| 1647 | N2.setColocate(Colocate); |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | /// Check if the existing node-sets are profitable. If not, then ignore the |
| 1655 | /// recurrent node-sets, and attempt to schedule all nodes together. This is |
Krzysztof Parzyszek | 3ca2334 | 2018-03-26 17:07:41 +0000 | [diff] [blame] | 1656 | /// a heuristic. If the MII is large and all the recurrent node-sets are small, |
| 1657 | /// then it's best to try to schedule all instructions together instead of |
| 1658 | /// starting with the recurrent node-sets. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1659 | void SwingSchedulerDAG::checkNodeSets(NodeSetType &NodeSets) { |
| 1660 | // Look for loops with a large MII. |
Krzysztof Parzyszek | 3ca2334 | 2018-03-26 17:07:41 +0000 | [diff] [blame] | 1661 | if (MII < 17) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1662 | return; |
| 1663 | // Check if the node-set contains only a simple add recurrence. |
Krzysztof Parzyszek | 3ca2334 | 2018-03-26 17:07:41 +0000 | [diff] [blame] | 1664 | for (auto &NS : NodeSets) { |
| 1665 | if (NS.getRecMII() > 2) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1666 | return; |
Krzysztof Parzyszek | 3ca2334 | 2018-03-26 17:07:41 +0000 | [diff] [blame] | 1667 | if (NS.getMaxDepth() > MII) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1668 | return; |
Krzysztof Parzyszek | 3ca2334 | 2018-03-26 17:07:41 +0000 | [diff] [blame] | 1669 | } |
| 1670 | NodeSets.clear(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1671 | LLVM_DEBUG(dbgs() << "Clear recurrence node-sets\n"); |
Krzysztof Parzyszek | 3ca2334 | 2018-03-26 17:07:41 +0000 | [diff] [blame] | 1672 | return; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /// Add the nodes that do not belong to a recurrence set into groups |
| 1676 | /// based upon connected componenets. |
| 1677 | void SwingSchedulerDAG::groupRemainingNodes(NodeSetType &NodeSets) { |
| 1678 | SetVector<SUnit *> NodesAdded; |
| 1679 | SmallPtrSet<SUnit *, 8> Visited; |
| 1680 | // Add the nodes that are on a path between the previous node sets and |
| 1681 | // the current node set. |
| 1682 | for (NodeSet &I : NodeSets) { |
| 1683 | SmallSetVector<SUnit *, 8> N; |
| 1684 | // Add the nodes from the current node set to the previous node set. |
| 1685 | if (succ_L(I, N)) { |
| 1686 | SetVector<SUnit *> Path; |
| 1687 | for (SUnit *NI : N) { |
| 1688 | Visited.clear(); |
| 1689 | computePath(NI, Path, NodesAdded, I, Visited); |
| 1690 | } |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1691 | if (!Path.empty()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1692 | I.insert(Path.begin(), Path.end()); |
| 1693 | } |
| 1694 | // Add the nodes from the previous node set to the current node set. |
| 1695 | N.clear(); |
| 1696 | if (succ_L(NodesAdded, N)) { |
| 1697 | SetVector<SUnit *> Path; |
| 1698 | for (SUnit *NI : N) { |
| 1699 | Visited.clear(); |
| 1700 | computePath(NI, Path, I, NodesAdded, Visited); |
| 1701 | } |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1702 | if (!Path.empty()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1703 | I.insert(Path.begin(), Path.end()); |
| 1704 | } |
| 1705 | NodesAdded.insert(I.begin(), I.end()); |
| 1706 | } |
| 1707 | |
| 1708 | // Create a new node set with the connected nodes of any successor of a node |
| 1709 | // in a recurrent set. |
| 1710 | NodeSet NewSet; |
| 1711 | SmallSetVector<SUnit *, 8> N; |
| 1712 | if (succ_L(NodesAdded, N)) |
| 1713 | for (SUnit *I : N) |
| 1714 | addConnectedNodes(I, NewSet, NodesAdded); |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1715 | if (!NewSet.empty()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1716 | NodeSets.push_back(NewSet); |
| 1717 | |
| 1718 | // Create a new node set with the connected nodes of any predecessor of a node |
| 1719 | // in a recurrent set. |
| 1720 | NewSet.clear(); |
| 1721 | if (pred_L(NodesAdded, N)) |
| 1722 | for (SUnit *I : N) |
| 1723 | addConnectedNodes(I, NewSet, NodesAdded); |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1724 | if (!NewSet.empty()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1725 | NodeSets.push_back(NewSet); |
| 1726 | |
Hiroshi Inoue | 372ffa1 | 2018-04-13 11:37:06 +0000 | [diff] [blame] | 1727 | // Create new nodes sets with the connected nodes any remaining node that |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1728 | // has no predecessor. |
| 1729 | for (unsigned i = 0; i < SUnits.size(); ++i) { |
| 1730 | SUnit *SU = &SUnits[i]; |
| 1731 | if (NodesAdded.count(SU) == 0) { |
| 1732 | NewSet.clear(); |
| 1733 | addConnectedNodes(SU, NewSet, NodesAdded); |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1734 | if (!NewSet.empty()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1735 | NodeSets.push_back(NewSet); |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
Alexey Lapshin | 31f47b8 | 2019-01-25 21:59:53 +0000 | [diff] [blame] | 1740 | /// Add the node to the set, and add all of its connected nodes to the set. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1741 | void SwingSchedulerDAG::addConnectedNodes(SUnit *SU, NodeSet &NewSet, |
| 1742 | SetVector<SUnit *> &NodesAdded) { |
| 1743 | NewSet.insert(SU); |
| 1744 | NodesAdded.insert(SU); |
| 1745 | for (auto &SI : SU->Succs) { |
| 1746 | SUnit *Successor = SI.getSUnit(); |
| 1747 | if (!SI.isArtificial() && NodesAdded.count(Successor) == 0) |
| 1748 | addConnectedNodes(Successor, NewSet, NodesAdded); |
| 1749 | } |
| 1750 | for (auto &PI : SU->Preds) { |
| 1751 | SUnit *Predecessor = PI.getSUnit(); |
| 1752 | if (!PI.isArtificial() && NodesAdded.count(Predecessor) == 0) |
| 1753 | addConnectedNodes(Predecessor, NewSet, NodesAdded); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | /// Return true if Set1 contains elements in Set2. The elements in common |
| 1758 | /// are returned in a different container. |
| 1759 | static bool isIntersect(SmallSetVector<SUnit *, 8> &Set1, const NodeSet &Set2, |
| 1760 | SmallSetVector<SUnit *, 8> &Result) { |
| 1761 | Result.clear(); |
| 1762 | for (unsigned i = 0, e = Set1.size(); i != e; ++i) { |
| 1763 | SUnit *SU = Set1[i]; |
| 1764 | if (Set2.count(SU) != 0) |
| 1765 | Result.insert(SU); |
| 1766 | } |
| 1767 | return !Result.empty(); |
| 1768 | } |
| 1769 | |
| 1770 | /// Merge the recurrence node sets that have the same initial node. |
| 1771 | void SwingSchedulerDAG::fuseRecs(NodeSetType &NodeSets) { |
| 1772 | for (NodeSetType::iterator I = NodeSets.begin(), E = NodeSets.end(); I != E; |
| 1773 | ++I) { |
| 1774 | NodeSet &NI = *I; |
| 1775 | for (NodeSetType::iterator J = I + 1; J != E;) { |
| 1776 | NodeSet &NJ = *J; |
| 1777 | if (NI.getNode(0)->NodeNum == NJ.getNode(0)->NodeNum) { |
| 1778 | if (NJ.compareRecMII(NI) > 0) |
| 1779 | NI.setRecMII(NJ.getRecMII()); |
| 1780 | for (NodeSet::iterator NII = J->begin(), ENI = J->end(); NII != ENI; |
| 1781 | ++NII) |
| 1782 | I->insert(*NII); |
| 1783 | NodeSets.erase(J); |
| 1784 | E = NodeSets.end(); |
| 1785 | } else { |
| 1786 | ++J; |
| 1787 | } |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | /// Remove nodes that have been scheduled in previous NodeSets. |
| 1793 | void SwingSchedulerDAG::removeDuplicateNodes(NodeSetType &NodeSets) { |
| 1794 | for (NodeSetType::iterator I = NodeSets.begin(), E = NodeSets.end(); I != E; |
| 1795 | ++I) |
| 1796 | for (NodeSetType::iterator J = I + 1; J != E;) { |
| 1797 | J->remove_if([&](SUnit *SUJ) { return I->count(SUJ); }); |
| 1798 | |
Eugene Zelenko | 32a4056 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1799 | if (J->empty()) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1800 | NodeSets.erase(J); |
| 1801 | E = NodeSets.end(); |
| 1802 | } else { |
| 1803 | ++J; |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1808 | /// Compute an ordered list of the dependence graph nodes, which |
| 1809 | /// indicates the order that the nodes will be scheduled. This is a |
| 1810 | /// two-level algorithm. First, a partial order is created, which |
| 1811 | /// consists of a list of sets ordered from highest to lowest priority. |
| 1812 | void SwingSchedulerDAG::computeNodeOrder(NodeSetType &NodeSets) { |
| 1813 | SmallSetVector<SUnit *, 8> R; |
| 1814 | NodeOrder.clear(); |
| 1815 | |
| 1816 | for (auto &Nodes : NodeSets) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1817 | LLVM_DEBUG(dbgs() << "NodeSet size " << Nodes.size() << "\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1818 | OrderKind Order; |
| 1819 | SmallSetVector<SUnit *, 8> N; |
| 1820 | if (pred_L(NodeOrder, N) && isSubset(N, Nodes)) { |
| 1821 | R.insert(N.begin(), N.end()); |
| 1822 | Order = BottomUp; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1823 | LLVM_DEBUG(dbgs() << " Bottom up (preds) "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1824 | } else if (succ_L(NodeOrder, N) && isSubset(N, Nodes)) { |
| 1825 | R.insert(N.begin(), N.end()); |
| 1826 | Order = TopDown; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1827 | LLVM_DEBUG(dbgs() << " Top down (succs) "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1828 | } else if (isIntersect(N, Nodes, R)) { |
| 1829 | // If some of the successors are in the existing node-set, then use the |
| 1830 | // top-down ordering. |
| 1831 | Order = TopDown; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1832 | LLVM_DEBUG(dbgs() << " Top down (intersect) "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1833 | } else if (NodeSets.size() == 1) { |
| 1834 | for (auto &N : Nodes) |
| 1835 | if (N->Succs.size() == 0) |
| 1836 | R.insert(N); |
| 1837 | Order = BottomUp; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1838 | LLVM_DEBUG(dbgs() << " Bottom up (all) "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1839 | } else { |
| 1840 | // Find the node with the highest ASAP. |
| 1841 | SUnit *maxASAP = nullptr; |
| 1842 | for (SUnit *SU : Nodes) { |
Krzysztof Parzyszek | a212204 | 2018-03-26 16:33:16 +0000 | [diff] [blame] | 1843 | if (maxASAP == nullptr || getASAP(SU) > getASAP(maxASAP) || |
| 1844 | (getASAP(SU) == getASAP(maxASAP) && SU->NodeNum > maxASAP->NodeNum)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1845 | maxASAP = SU; |
| 1846 | } |
| 1847 | R.insert(maxASAP); |
| 1848 | Order = BottomUp; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1849 | LLVM_DEBUG(dbgs() << " Bottom up (default) "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | while (!R.empty()) { |
| 1853 | if (Order == TopDown) { |
| 1854 | // Choose the node with the maximum height. If more than one, choose |
Krzysztof Parzyszek | a212204 | 2018-03-26 16:33:16 +0000 | [diff] [blame] | 1855 | // the node wiTH the maximum ZeroLatencyHeight. If still more than one, |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1856 | // choose the node with the lowest MOV. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1857 | while (!R.empty()) { |
| 1858 | SUnit *maxHeight = nullptr; |
| 1859 | for (SUnit *I : R) { |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 1860 | if (maxHeight == nullptr || getHeight(I) > getHeight(maxHeight)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1861 | maxHeight = I; |
| 1862 | else if (getHeight(I) == getHeight(maxHeight) && |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1863 | getZeroLatencyHeight(I) > getZeroLatencyHeight(maxHeight)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1864 | maxHeight = I; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1865 | else if (getHeight(I) == getHeight(maxHeight) && |
| 1866 | getZeroLatencyHeight(I) == |
| 1867 | getZeroLatencyHeight(maxHeight) && |
| 1868 | getMOV(I) < getMOV(maxHeight)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1869 | maxHeight = I; |
| 1870 | } |
| 1871 | NodeOrder.insert(maxHeight); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1872 | LLVM_DEBUG(dbgs() << maxHeight->NodeNum << " "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1873 | R.remove(maxHeight); |
| 1874 | for (const auto &I : maxHeight->Succs) { |
| 1875 | if (Nodes.count(I.getSUnit()) == 0) |
| 1876 | continue; |
| 1877 | if (NodeOrder.count(I.getSUnit()) != 0) |
| 1878 | continue; |
| 1879 | if (ignoreDependence(I, false)) |
| 1880 | continue; |
| 1881 | R.insert(I.getSUnit()); |
| 1882 | } |
| 1883 | // Back-edges are predecessors with an anti-dependence. |
| 1884 | for (const auto &I : maxHeight->Preds) { |
| 1885 | if (I.getKind() != SDep::Anti) |
| 1886 | continue; |
| 1887 | if (Nodes.count(I.getSUnit()) == 0) |
| 1888 | continue; |
| 1889 | if (NodeOrder.count(I.getSUnit()) != 0) |
| 1890 | continue; |
| 1891 | R.insert(I.getSUnit()); |
| 1892 | } |
| 1893 | } |
| 1894 | Order = BottomUp; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1895 | LLVM_DEBUG(dbgs() << "\n Switching order to bottom up "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1896 | SmallSetVector<SUnit *, 8> N; |
| 1897 | if (pred_L(NodeOrder, N, &Nodes)) |
| 1898 | R.insert(N.begin(), N.end()); |
| 1899 | } else { |
| 1900 | // Choose the node with the maximum depth. If more than one, choose |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1901 | // the node with the maximum ZeroLatencyDepth. If still more than one, |
| 1902 | // choose the node with the lowest MOV. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1903 | while (!R.empty()) { |
| 1904 | SUnit *maxDepth = nullptr; |
| 1905 | for (SUnit *I : R) { |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 1906 | if (maxDepth == nullptr || getDepth(I) > getDepth(maxDepth)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1907 | maxDepth = I; |
| 1908 | else if (getDepth(I) == getDepth(maxDepth) && |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1909 | getZeroLatencyDepth(I) > getZeroLatencyDepth(maxDepth)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1910 | maxDepth = I; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 1911 | else if (getDepth(I) == getDepth(maxDepth) && |
| 1912 | getZeroLatencyDepth(I) == getZeroLatencyDepth(maxDepth) && |
| 1913 | getMOV(I) < getMOV(maxDepth)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1914 | maxDepth = I; |
| 1915 | } |
| 1916 | NodeOrder.insert(maxDepth); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1917 | LLVM_DEBUG(dbgs() << maxDepth->NodeNum << " "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1918 | R.remove(maxDepth); |
| 1919 | if (Nodes.isExceedSU(maxDepth)) { |
| 1920 | Order = TopDown; |
| 1921 | R.clear(); |
| 1922 | R.insert(Nodes.getNode(0)); |
| 1923 | break; |
| 1924 | } |
| 1925 | for (const auto &I : maxDepth->Preds) { |
| 1926 | if (Nodes.count(I.getSUnit()) == 0) |
| 1927 | continue; |
| 1928 | if (NodeOrder.count(I.getSUnit()) != 0) |
| 1929 | continue; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1930 | R.insert(I.getSUnit()); |
| 1931 | } |
| 1932 | // Back-edges are predecessors with an anti-dependence. |
| 1933 | for (const auto &I : maxDepth->Succs) { |
| 1934 | if (I.getKind() != SDep::Anti) |
| 1935 | continue; |
| 1936 | if (Nodes.count(I.getSUnit()) == 0) |
| 1937 | continue; |
| 1938 | if (NodeOrder.count(I.getSUnit()) != 0) |
| 1939 | continue; |
| 1940 | R.insert(I.getSUnit()); |
| 1941 | } |
| 1942 | } |
| 1943 | Order = TopDown; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1944 | LLVM_DEBUG(dbgs() << "\n Switching order to top down "); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1945 | SmallSetVector<SUnit *, 8> N; |
| 1946 | if (succ_L(NodeOrder, N, &Nodes)) |
| 1947 | R.insert(N.begin(), N.end()); |
| 1948 | } |
| 1949 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1950 | LLVM_DEBUG(dbgs() << "\nDone with Nodeset\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1953 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1954 | dbgs() << "Node order: "; |
| 1955 | for (SUnit *I : NodeOrder) |
| 1956 | dbgs() << " " << I->NodeNum << " "; |
| 1957 | dbgs() << "\n"; |
| 1958 | }); |
| 1959 | } |
| 1960 | |
| 1961 | /// Process the nodes in the computed order and create the pipelined schedule |
| 1962 | /// of the instructions, if possible. Return true if a schedule is found. |
| 1963 | bool SwingSchedulerDAG::schedulePipeline(SMSchedule &Schedule) { |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (NodeOrder.empty()){ |
| 1966 | LLVM_DEBUG(dbgs() << "NodeOrder is empty! abort scheduling\n" ); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1967 | return false; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1968 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1969 | |
| 1970 | bool scheduleFound = false; |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 1971 | unsigned II = 0; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1972 | // Keep increasing II until a valid schedule is found. |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 1973 | for (II = MII; II <= MAX_II && !scheduleFound; ++II) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1974 | Schedule.reset(); |
| 1975 | Schedule.setInitiationInterval(II); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1976 | LLVM_DEBUG(dbgs() << "Try to schedule with " << II << "\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1977 | |
| 1978 | SetVector<SUnit *>::iterator NI = NodeOrder.begin(); |
| 1979 | SetVector<SUnit *>::iterator NE = NodeOrder.end(); |
| 1980 | do { |
| 1981 | SUnit *SU = *NI; |
| 1982 | |
| 1983 | // Compute the schedule time for the instruction, which is based |
| 1984 | // upon the scheduled time for any predecessors/successors. |
| 1985 | int EarlyStart = INT_MIN; |
| 1986 | int LateStart = INT_MAX; |
| 1987 | // These values are set when the size of the schedule window is limited |
| 1988 | // due to chain dependences. |
| 1989 | int SchedEnd = INT_MAX; |
| 1990 | int SchedStart = INT_MIN; |
| 1991 | Schedule.computeStart(SU, &EarlyStart, &LateStart, &SchedEnd, &SchedStart, |
| 1992 | II, this); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1993 | LLVM_DEBUG({ |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 1994 | dbgs() << "\n"; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 1995 | dbgs() << "Inst (" << SU->NodeNum << ") "; |
| 1996 | SU->getInstr()->dump(); |
| 1997 | dbgs() << "\n"; |
| 1998 | }); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1999 | LLVM_DEBUG({ |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 2000 | dbgs() << format("\tes: %8x ls: %8x me: %8x ms: %8x\n", EarlyStart, |
| 2001 | LateStart, SchedEnd, SchedStart); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2002 | }); |
| 2003 | |
| 2004 | if (EarlyStart > LateStart || SchedEnd < EarlyStart || |
| 2005 | SchedStart > LateStart) |
| 2006 | scheduleFound = false; |
| 2007 | else if (EarlyStart != INT_MIN && LateStart == INT_MAX) { |
| 2008 | SchedEnd = std::min(SchedEnd, EarlyStart + (int)II - 1); |
| 2009 | scheduleFound = Schedule.insert(SU, EarlyStart, SchedEnd, II); |
| 2010 | } else if (EarlyStart == INT_MIN && LateStart != INT_MAX) { |
| 2011 | SchedStart = std::max(SchedStart, LateStart - (int)II + 1); |
| 2012 | scheduleFound = Schedule.insert(SU, LateStart, SchedStart, II); |
| 2013 | } else if (EarlyStart != INT_MIN && LateStart != INT_MAX) { |
| 2014 | SchedEnd = |
| 2015 | std::min(SchedEnd, std::min(LateStart, EarlyStart + (int)II - 1)); |
| 2016 | // When scheduling a Phi it is better to start at the late cycle and go |
| 2017 | // backwards. The default order may insert the Phi too far away from |
| 2018 | // its first dependence. |
| 2019 | if (SU->getInstr()->isPHI()) |
| 2020 | scheduleFound = Schedule.insert(SU, SchedEnd, EarlyStart, II); |
| 2021 | else |
| 2022 | scheduleFound = Schedule.insert(SU, EarlyStart, SchedEnd, II); |
| 2023 | } else { |
| 2024 | int FirstCycle = Schedule.getFirstCycle(); |
| 2025 | scheduleFound = Schedule.insert(SU, FirstCycle + getASAP(SU), |
| 2026 | FirstCycle + getASAP(SU) + II - 1, II); |
| 2027 | } |
| 2028 | // Even if we find a schedule, make sure the schedule doesn't exceed the |
| 2029 | // allowable number of stages. We keep trying if this happens. |
| 2030 | if (scheduleFound) |
| 2031 | if (SwpMaxStages > -1 && |
| 2032 | Schedule.getMaxStageCount() > (unsigned)SwpMaxStages) |
| 2033 | scheduleFound = false; |
| 2034 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2035 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2036 | if (!scheduleFound) |
| 2037 | dbgs() << "\tCan't schedule\n"; |
| 2038 | }); |
| 2039 | } while (++NI != NE && scheduleFound); |
| 2040 | |
| 2041 | // If a schedule is found, check if it is a valid schedule too. |
| 2042 | if (scheduleFound) |
| 2043 | scheduleFound = Schedule.isValidSchedule(this); |
| 2044 | } |
| 2045 | |
Brendon Cahoon | 59d9973 | 2019-01-23 03:26:10 +0000 | [diff] [blame] | 2046 | LLVM_DEBUG(dbgs() << "Schedule Found? " << scheduleFound << " (II=" << II |
| 2047 | << ")\n"); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (scheduleFound) |
| 2050 | Schedule.finalizeSchedule(this); |
| 2051 | else |
| 2052 | Schedule.reset(); |
| 2053 | |
| 2054 | return scheduleFound && Schedule.getMaxStageCount() > 0; |
| 2055 | } |
| 2056 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2057 | /// Return true if we can compute the amount the instruction changes |
| 2058 | /// during each iteration. Set Delta to the amount of the change. |
| 2059 | bool SwingSchedulerDAG::computeDelta(MachineInstr &MI, unsigned &Delta) { |
| 2060 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Bjorn Pettersson | 238c9d630 | 2019-04-19 09:08:38 +0000 | [diff] [blame] | 2061 | const MachineOperand *BaseOp; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2062 | int64_t Offset; |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 2063 | if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, TRI)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2064 | return false; |
| 2065 | |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 2066 | if (!BaseOp->isReg()) |
| 2067 | return false; |
| 2068 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2069 | Register BaseReg = BaseOp->getReg(); |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 2070 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2071 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 2072 | // Check if there is a Phi. If so, get the definition in the loop. |
| 2073 | MachineInstr *BaseDef = MRI.getVRegDef(BaseReg); |
| 2074 | if (BaseDef && BaseDef->isPHI()) { |
| 2075 | BaseReg = getLoopPhiReg(*BaseDef, MI.getParent()); |
| 2076 | BaseDef = MRI.getVRegDef(BaseReg); |
| 2077 | } |
| 2078 | if (!BaseDef) |
| 2079 | return false; |
| 2080 | |
| 2081 | int D = 0; |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2082 | if (!TII->getIncrementValue(*BaseDef, D) && D >= 0) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2083 | return false; |
| 2084 | |
| 2085 | Delta = D; |
| 2086 | return true; |
| 2087 | } |
| 2088 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2089 | /// Check if we can change the instruction to use an offset value from the |
| 2090 | /// previous iteration. If so, return true and set the base and offset values |
| 2091 | /// so that we can rewrite the load, if necessary. |
| 2092 | /// v1 = Phi(v0, v3) |
| 2093 | /// v2 = load v1, 0 |
| 2094 | /// v3 = post_store v1, 4, x |
| 2095 | /// This function enables the load to be rewritten as v2 = load v3, 4. |
| 2096 | bool SwingSchedulerDAG::canUseLastOffsetValue(MachineInstr *MI, |
| 2097 | unsigned &BasePos, |
| 2098 | unsigned &OffsetPos, |
| 2099 | unsigned &NewBase, |
| 2100 | int64_t &Offset) { |
| 2101 | // Get the load instruction. |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2102 | if (TII->isPostIncrement(*MI)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2103 | return false; |
| 2104 | unsigned BasePosLd, OffsetPosLd; |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2105 | if (!TII->getBaseAndOffsetPosition(*MI, BasePosLd, OffsetPosLd)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2106 | return false; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2107 | Register BaseReg = MI->getOperand(BasePosLd).getReg(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Look for the Phi instruction. |
Justin Bogner | fdf9bf4 | 2017-10-10 23:50:49 +0000 | [diff] [blame] | 2110 | MachineRegisterInfo &MRI = MI->getMF()->getRegInfo(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2111 | MachineInstr *Phi = MRI.getVRegDef(BaseReg); |
| 2112 | if (!Phi || !Phi->isPHI()) |
| 2113 | return false; |
| 2114 | // Get the register defined in the loop block. |
| 2115 | unsigned PrevReg = getLoopPhiReg(*Phi, MI->getParent()); |
| 2116 | if (!PrevReg) |
| 2117 | return false; |
| 2118 | |
| 2119 | // Check for the post-increment load/store instruction. |
| 2120 | MachineInstr *PrevDef = MRI.getVRegDef(PrevReg); |
| 2121 | if (!PrevDef || PrevDef == MI) |
| 2122 | return false; |
| 2123 | |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2124 | if (!TII->isPostIncrement(*PrevDef)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2125 | return false; |
| 2126 | |
| 2127 | unsigned BasePos1 = 0, OffsetPos1 = 0; |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2128 | if (!TII->getBaseAndOffsetPosition(*PrevDef, BasePos1, OffsetPos1)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2129 | return false; |
| 2130 | |
Krzysztof Parzyszek | 40df8a2 | 2018-03-26 16:17:06 +0000 | [diff] [blame] | 2131 | // Make sure that the instructions do not access the same memory location in |
| 2132 | // the next iteration. |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2133 | int64_t LoadOffset = MI->getOperand(OffsetPosLd).getImm(); |
| 2134 | int64_t StoreOffset = PrevDef->getOperand(OffsetPos1).getImm(); |
Krzysztof Parzyszek | 40df8a2 | 2018-03-26 16:17:06 +0000 | [diff] [blame] | 2135 | MachineInstr *NewMI = MF.CloneMachineInstr(MI); |
| 2136 | NewMI->getOperand(OffsetPosLd).setImm(LoadOffset + StoreOffset); |
| 2137 | bool Disjoint = TII->areMemAccessesTriviallyDisjoint(*NewMI, *PrevDef); |
| 2138 | MF.DeleteMachineInstr(NewMI); |
| 2139 | if (!Disjoint) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2140 | return false; |
| 2141 | |
| 2142 | // Set the return value once we determine that we return true. |
| 2143 | BasePos = BasePosLd; |
| 2144 | OffsetPos = OffsetPosLd; |
| 2145 | NewBase = PrevReg; |
| 2146 | Offset = StoreOffset; |
| 2147 | return true; |
| 2148 | } |
| 2149 | |
| 2150 | /// Apply changes to the instruction if needed. The changes are need |
| 2151 | /// to improve the scheduling and depend up on the final schedule. |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2152 | void SwingSchedulerDAG::applyInstrChange(MachineInstr *MI, |
| 2153 | SMSchedule &Schedule) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2154 | SUnit *SU = getSUnit(MI); |
| 2155 | DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It = |
| 2156 | InstrChanges.find(SU); |
| 2157 | if (It != InstrChanges.end()) { |
| 2158 | std::pair<unsigned, int64_t> RegAndOffset = It->second; |
| 2159 | unsigned BasePos, OffsetPos; |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2160 | if (!TII->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos)) |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2161 | return; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2162 | Register BaseReg = MI->getOperand(BasePos).getReg(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2163 | MachineInstr *LoopDef = findDefInLoop(BaseReg); |
| 2164 | int DefStageNum = Schedule.stageScheduled(getSUnit(LoopDef)); |
| 2165 | int DefCycleNum = Schedule.cycleScheduled(getSUnit(LoopDef)); |
| 2166 | int BaseStageNum = Schedule.stageScheduled(SU); |
| 2167 | int BaseCycleNum = Schedule.cycleScheduled(SU); |
| 2168 | if (BaseStageNum < DefStageNum) { |
| 2169 | MachineInstr *NewMI = MF.CloneMachineInstr(MI); |
| 2170 | int OffsetDiff = DefStageNum - BaseStageNum; |
| 2171 | if (DefCycleNum < BaseCycleNum) { |
| 2172 | NewMI->getOperand(BasePos).setReg(RegAndOffset.first); |
| 2173 | if (OffsetDiff > 0) |
| 2174 | --OffsetDiff; |
| 2175 | } |
| 2176 | int64_t NewOffset = |
| 2177 | MI->getOperand(OffsetPos).getImm() + RegAndOffset.second * OffsetDiff; |
| 2178 | NewMI->getOperand(OffsetPos).setImm(NewOffset); |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2179 | SU->setInstr(NewMI); |
| 2180 | MISUnitMap[NewMI] = SU; |
James Molloy | 790a779 | 2019-08-30 18:49:50 +0000 | [diff] [blame] | 2181 | NewMIs[MI] = NewMI; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2182 | } |
| 2183 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
James Molloy | 790a779 | 2019-08-30 18:49:50 +0000 | [diff] [blame] | 2186 | /// Return the instruction in the loop that defines the register. |
| 2187 | /// If the definition is a Phi, then follow the Phi operand to |
| 2188 | /// the instruction in the loop. |
| 2189 | MachineInstr *SwingSchedulerDAG::findDefInLoop(unsigned Reg) { |
| 2190 | SmallPtrSet<MachineInstr *, 8> Visited; |
| 2191 | MachineInstr *Def = MRI.getVRegDef(Reg); |
| 2192 | while (Def->isPHI()) { |
| 2193 | if (!Visited.insert(Def).second) |
| 2194 | break; |
| 2195 | for (unsigned i = 1, e = Def->getNumOperands(); i < e; i += 2) |
| 2196 | if (Def->getOperand(i + 1).getMBB() == BB) { |
| 2197 | Def = MRI.getVRegDef(Def->getOperand(i).getReg()); |
| 2198 | break; |
| 2199 | } |
| 2200 | } |
| 2201 | return Def; |
| 2202 | } |
| 2203 | |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2204 | /// Return true for an order or output dependence that is loop carried |
| 2205 | /// potentially. A dependence is loop carried if the destination defines a valu |
| 2206 | /// that may be used or defined by the source in a subsequent iteration. |
| 2207 | bool SwingSchedulerDAG::isLoopCarriedDep(SUnit *Source, const SDep &Dep, |
| 2208 | bool isSucc) { |
| 2209 | if ((Dep.getKind() != SDep::Order && Dep.getKind() != SDep::Output) || |
| 2210 | Dep.isArtificial()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2211 | return false; |
| 2212 | |
| 2213 | if (!SwpPruneLoopCarried) |
| 2214 | return true; |
| 2215 | |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2216 | if (Dep.getKind() == SDep::Output) |
| 2217 | return true; |
| 2218 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2219 | MachineInstr *SI = Source->getInstr(); |
| 2220 | MachineInstr *DI = Dep.getSUnit()->getInstr(); |
| 2221 | if (!isSucc) |
| 2222 | std::swap(SI, DI); |
| 2223 | assert(SI != nullptr && DI != nullptr && "Expecting SUnit with an MI."); |
| 2224 | |
| 2225 | // Assume ordered loads and stores may have a loop carried dependence. |
| 2226 | if (SI->hasUnmodeledSideEffects() || DI->hasUnmodeledSideEffects() || |
Ulrich Weigand | 6c5d5ce | 2019-06-05 22:33:10 +0000 | [diff] [blame] | 2227 | SI->mayRaiseFPException() || DI->mayRaiseFPException() || |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2228 | SI->hasOrderedMemoryRef() || DI->hasOrderedMemoryRef()) |
| 2229 | return true; |
| 2230 | |
| 2231 | // Only chain dependences between a load and store can be loop carried. |
| 2232 | if (!DI->mayStore() || !SI->mayLoad()) |
| 2233 | return false; |
| 2234 | |
| 2235 | unsigned DeltaS, DeltaD; |
| 2236 | if (!computeDelta(*SI, DeltaS) || !computeDelta(*DI, DeltaD)) |
| 2237 | return true; |
| 2238 | |
Bjorn Pettersson | 238c9d630 | 2019-04-19 09:08:38 +0000 | [diff] [blame] | 2239 | const MachineOperand *BaseOpS, *BaseOpD; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2240 | int64_t OffsetS, OffsetD; |
| 2241 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 2242 | if (!TII->getMemOperandWithOffset(*SI, BaseOpS, OffsetS, TRI) || |
| 2243 | !TII->getMemOperandWithOffset(*DI, BaseOpD, OffsetD, TRI)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2244 | return true; |
| 2245 | |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 2246 | if (!BaseOpS->isIdenticalTo(*BaseOpD)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2247 | return true; |
| 2248 | |
Krzysztof Parzyszek | 8c07d0c | 2018-03-26 16:58:40 +0000 | [diff] [blame] | 2249 | // Check that the base register is incremented by a constant value for each |
| 2250 | // iteration. |
Francis Visoiu Mistrih | d7eebd6 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 2251 | MachineInstr *Def = MRI.getVRegDef(BaseOpS->getReg()); |
Krzysztof Parzyszek | 8c07d0c | 2018-03-26 16:58:40 +0000 | [diff] [blame] | 2252 | if (!Def || !Def->isPHI()) |
| 2253 | return true; |
| 2254 | unsigned InitVal = 0; |
| 2255 | unsigned LoopVal = 0; |
| 2256 | getPhiRegs(*Def, BB, InitVal, LoopVal); |
| 2257 | MachineInstr *LoopDef = MRI.getVRegDef(LoopVal); |
| 2258 | int D = 0; |
| 2259 | if (!LoopDef || !TII->getIncrementValue(*LoopDef, D)) |
| 2260 | return true; |
| 2261 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2262 | uint64_t AccessSizeS = (*SI->memoperands_begin())->getSize(); |
| 2263 | uint64_t AccessSizeD = (*DI->memoperands_begin())->getSize(); |
| 2264 | |
| 2265 | // This is the main test, which checks the offset values and the loop |
| 2266 | // increment value to determine if the accesses may be loop carried. |
Brendon Cahoon | 57c3d4b | 2019-04-11 21:57:51 +0000 | [diff] [blame] | 2267 | if (AccessSizeS == MemoryLocation::UnknownSize || |
| 2268 | AccessSizeD == MemoryLocation::UnknownSize) |
| 2269 | return true; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2270 | |
Brendon Cahoon | 57c3d4b | 2019-04-11 21:57:51 +0000 | [diff] [blame] | 2271 | if (DeltaS != DeltaD || DeltaS < AccessSizeS || DeltaD < AccessSizeD) |
| 2272 | return true; |
| 2273 | |
| 2274 | return (OffsetS + (int64_t)AccessSizeS < OffsetD + (int64_t)AccessSizeD); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Krzysztof Parzyszek | 8839124 | 2016-12-22 19:21:20 +0000 | [diff] [blame] | 2277 | void SwingSchedulerDAG::postprocessDAG() { |
| 2278 | for (auto &M : Mutations) |
| 2279 | M->apply(this); |
| 2280 | } |
| 2281 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2282 | /// Try to schedule the node at the specified StartCycle and continue |
| 2283 | /// until the node is schedule or the EndCycle is reached. This function |
| 2284 | /// returns true if the node is scheduled. This routine may search either |
| 2285 | /// forward or backward for a place to insert the instruction based upon |
| 2286 | /// the relative values of StartCycle and EndCycle. |
| 2287 | bool SMSchedule::insert(SUnit *SU, int StartCycle, int EndCycle, int II) { |
| 2288 | bool forward = true; |
Jinsong Ji | 18e7bf5 | 2019-05-31 15:35:19 +0000 | [diff] [blame] | 2289 | LLVM_DEBUG({ |
| 2290 | dbgs() << "Trying to insert node between " << StartCycle << " and " |
| 2291 | << EndCycle << " II: " << II << "\n"; |
| 2292 | }); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2293 | if (StartCycle > EndCycle) |
| 2294 | forward = false; |
| 2295 | |
| 2296 | // The terminating condition depends on the direction. |
| 2297 | int termCycle = forward ? EndCycle + 1 : EndCycle - 1; |
| 2298 | for (int curCycle = StartCycle; curCycle != termCycle; |
| 2299 | forward ? ++curCycle : --curCycle) { |
| 2300 | |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2301 | // Add the already scheduled instructions at the specified cycle to the |
| 2302 | // DFA. |
| 2303 | ProcItinResources.clearResources(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2304 | for (int checkCycle = FirstCycle + ((curCycle - FirstCycle) % II); |
| 2305 | checkCycle <= LastCycle; checkCycle += II) { |
| 2306 | std::deque<SUnit *> &cycleInstrs = ScheduledInstrs[checkCycle]; |
| 2307 | |
| 2308 | for (std::deque<SUnit *>::iterator I = cycleInstrs.begin(), |
| 2309 | E = cycleInstrs.end(); |
| 2310 | I != E; ++I) { |
| 2311 | if (ST.getInstrInfo()->isZeroCost((*I)->getInstr()->getOpcode())) |
| 2312 | continue; |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2313 | assert(ProcItinResources.canReserveResources(*(*I)->getInstr()) && |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2314 | "These instructions have already been scheduled."); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2315 | ProcItinResources.reserveResources(*(*I)->getInstr()); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | if (ST.getInstrInfo()->isZeroCost(SU->getInstr()->getOpcode()) || |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2319 | ProcItinResources.canReserveResources(*SU->getInstr())) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2320 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2321 | dbgs() << "\tinsert at cycle " << curCycle << " "; |
| 2322 | SU->getInstr()->dump(); |
| 2323 | }); |
| 2324 | |
| 2325 | ScheduledInstrs[curCycle].push_back(SU); |
| 2326 | InstrToCycle.insert(std::make_pair(SU, curCycle)); |
| 2327 | if (curCycle > LastCycle) |
| 2328 | LastCycle = curCycle; |
| 2329 | if (curCycle < FirstCycle) |
| 2330 | FirstCycle = curCycle; |
| 2331 | return true; |
| 2332 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2333 | LLVM_DEBUG({ |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2334 | dbgs() << "\tfailed to insert at cycle " << curCycle << " "; |
| 2335 | SU->getInstr()->dump(); |
| 2336 | }); |
| 2337 | } |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
| 2341 | // Return the cycle of the earliest scheduled instruction in the chain. |
| 2342 | int SMSchedule::earliestCycleInChain(const SDep &Dep) { |
| 2343 | SmallPtrSet<SUnit *, 8> Visited; |
| 2344 | SmallVector<SDep, 8> Worklist; |
| 2345 | Worklist.push_back(Dep); |
| 2346 | int EarlyCycle = INT_MAX; |
| 2347 | while (!Worklist.empty()) { |
| 2348 | const SDep &Cur = Worklist.pop_back_val(); |
| 2349 | SUnit *PrevSU = Cur.getSUnit(); |
| 2350 | if (Visited.count(PrevSU)) |
| 2351 | continue; |
| 2352 | std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(PrevSU); |
| 2353 | if (it == InstrToCycle.end()) |
| 2354 | continue; |
| 2355 | EarlyCycle = std::min(EarlyCycle, it->second); |
| 2356 | for (const auto &PI : PrevSU->Preds) |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2357 | if (PI.getKind() == SDep::Order || Dep.getKind() == SDep::Output) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2358 | Worklist.push_back(PI); |
| 2359 | Visited.insert(PrevSU); |
| 2360 | } |
| 2361 | return EarlyCycle; |
| 2362 | } |
| 2363 | |
| 2364 | // Return the cycle of the latest scheduled instruction in the chain. |
| 2365 | int SMSchedule::latestCycleInChain(const SDep &Dep) { |
| 2366 | SmallPtrSet<SUnit *, 8> Visited; |
| 2367 | SmallVector<SDep, 8> Worklist; |
| 2368 | Worklist.push_back(Dep); |
| 2369 | int LateCycle = INT_MIN; |
| 2370 | while (!Worklist.empty()) { |
| 2371 | const SDep &Cur = Worklist.pop_back_val(); |
| 2372 | SUnit *SuccSU = Cur.getSUnit(); |
| 2373 | if (Visited.count(SuccSU)) |
| 2374 | continue; |
| 2375 | std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SuccSU); |
| 2376 | if (it == InstrToCycle.end()) |
| 2377 | continue; |
| 2378 | LateCycle = std::max(LateCycle, it->second); |
| 2379 | for (const auto &SI : SuccSU->Succs) |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2380 | if (SI.getKind() == SDep::Order || Dep.getKind() == SDep::Output) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2381 | Worklist.push_back(SI); |
| 2382 | Visited.insert(SuccSU); |
| 2383 | } |
| 2384 | return LateCycle; |
| 2385 | } |
| 2386 | |
| 2387 | /// If an instruction has a use that spans multiple iterations, then |
| 2388 | /// return true. These instructions are characterized by having a back-ege |
| 2389 | /// to a Phi, which contains a reference to another Phi. |
| 2390 | static SUnit *multipleIterations(SUnit *SU, SwingSchedulerDAG *DAG) { |
| 2391 | for (auto &P : SU->Preds) |
| 2392 | if (DAG->isBackedge(SU, P) && P.getSUnit()->getInstr()->isPHI()) |
| 2393 | for (auto &S : P.getSUnit()->Succs) |
Krzysztof Parzyszek | b9b75b8 | 2018-03-26 15:53:23 +0000 | [diff] [blame] | 2394 | if (S.getKind() == SDep::Data && S.getSUnit()->getInstr()->isPHI()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2395 | return P.getSUnit(); |
| 2396 | return nullptr; |
| 2397 | } |
| 2398 | |
| 2399 | /// Compute the scheduling start slot for the instruction. The start slot |
| 2400 | /// depends on any predecessor or successor nodes scheduled already. |
| 2401 | void SMSchedule::computeStart(SUnit *SU, int *MaxEarlyStart, int *MinLateStart, |
| 2402 | int *MinEnd, int *MaxStart, int II, |
| 2403 | SwingSchedulerDAG *DAG) { |
| 2404 | // Iterate over each instruction that has been scheduled already. The start |
Hiroshi Inoue | c73b6d6 | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 2405 | // slot computation depends on whether the previously scheduled instruction |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2406 | // is a predecessor or successor of the specified instruction. |
| 2407 | for (int cycle = getFirstCycle(); cycle <= LastCycle; ++cycle) { |
| 2408 | |
| 2409 | // Iterate over each instruction in the current cycle. |
| 2410 | for (SUnit *I : getInstructions(cycle)) { |
| 2411 | // Because we're processing a DAG for the dependences, we recognize |
| 2412 | // the back-edge in recurrences by anti dependences. |
| 2413 | for (unsigned i = 0, e = (unsigned)SU->Preds.size(); i != e; ++i) { |
| 2414 | const SDep &Dep = SU->Preds[i]; |
| 2415 | if (Dep.getSUnit() == I) { |
| 2416 | if (!DAG->isBackedge(SU, Dep)) { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 2417 | int EarlyStart = cycle + Dep.getLatency() - |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2418 | DAG->getDistance(Dep.getSUnit(), SU, Dep) * II; |
| 2419 | *MaxEarlyStart = std::max(*MaxEarlyStart, EarlyStart); |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2420 | if (DAG->isLoopCarriedDep(SU, Dep, false)) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2421 | int End = earliestCycleInChain(Dep) + (II - 1); |
| 2422 | *MinEnd = std::min(*MinEnd, End); |
| 2423 | } |
| 2424 | } else { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 2425 | int LateStart = cycle - Dep.getLatency() + |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2426 | DAG->getDistance(SU, Dep.getSUnit(), Dep) * II; |
| 2427 | *MinLateStart = std::min(*MinLateStart, LateStart); |
| 2428 | } |
| 2429 | } |
| 2430 | // For instruction that requires multiple iterations, make sure that |
| 2431 | // the dependent instruction is not scheduled past the definition. |
| 2432 | SUnit *BE = multipleIterations(I, DAG); |
| 2433 | if (BE && Dep.getSUnit() == BE && !SU->getInstr()->isPHI() && |
| 2434 | !SU->isPred(I)) |
| 2435 | *MinLateStart = std::min(*MinLateStart, cycle); |
| 2436 | } |
Krzysztof Parzyszek | a212204 | 2018-03-26 16:33:16 +0000 | [diff] [blame] | 2437 | for (unsigned i = 0, e = (unsigned)SU->Succs.size(); i != e; ++i) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2438 | if (SU->Succs[i].getSUnit() == I) { |
| 2439 | const SDep &Dep = SU->Succs[i]; |
| 2440 | if (!DAG->isBackedge(SU, Dep)) { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 2441 | int LateStart = cycle - Dep.getLatency() + |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2442 | DAG->getDistance(SU, Dep.getSUnit(), Dep) * II; |
| 2443 | *MinLateStart = std::min(*MinLateStart, LateStart); |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2444 | if (DAG->isLoopCarriedDep(SU, Dep)) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2445 | int Start = latestCycleInChain(Dep) + 1 - II; |
| 2446 | *MaxStart = std::max(*MaxStart, Start); |
| 2447 | } |
| 2448 | } else { |
Krzysztof Parzyszek | c715a5d | 2018-03-21 16:39:11 +0000 | [diff] [blame] | 2449 | int EarlyStart = cycle + Dep.getLatency() - |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2450 | DAG->getDistance(Dep.getSUnit(), SU, Dep) * II; |
| 2451 | *MaxEarlyStart = std::max(*MaxEarlyStart, EarlyStart); |
| 2452 | } |
| 2453 | } |
Krzysztof Parzyszek | a212204 | 2018-03-26 16:33:16 +0000 | [diff] [blame] | 2454 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | /// Order the instructions within a cycle so that the definitions occur |
| 2460 | /// before the uses. Returns true if the instruction is added to the start |
| 2461 | /// of the list, or false if added to the end. |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2462 | void SMSchedule::orderDependence(SwingSchedulerDAG *SSD, SUnit *SU, |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2463 | std::deque<SUnit *> &Insts) { |
| 2464 | MachineInstr *MI = SU->getInstr(); |
| 2465 | bool OrderBeforeUse = false; |
| 2466 | bool OrderAfterDef = false; |
| 2467 | bool OrderBeforeDef = false; |
| 2468 | unsigned MoveDef = 0; |
| 2469 | unsigned MoveUse = 0; |
| 2470 | int StageInst1 = stageScheduled(SU); |
| 2471 | |
| 2472 | unsigned Pos = 0; |
| 2473 | for (std::deque<SUnit *>::iterator I = Insts.begin(), E = Insts.end(); I != E; |
| 2474 | ++I, ++Pos) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2475 | for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) { |
| 2476 | MachineOperand &MO = MI->getOperand(i); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 2477 | if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg())) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2478 | continue; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2479 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2480 | Register Reg = MO.getReg(); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2481 | unsigned BasePos, OffsetPos; |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 2482 | if (ST.getInstrInfo()->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos)) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2483 | if (MI->getOperand(BasePos).getReg() == Reg) |
| 2484 | if (unsigned NewReg = SSD->getInstrBaseReg(SU)) |
| 2485 | Reg = NewReg; |
| 2486 | bool Reads, Writes; |
| 2487 | std::tie(Reads, Writes) = |
| 2488 | (*I)->getInstr()->readsWritesVirtualRegister(Reg); |
| 2489 | if (MO.isDef() && Reads && stageScheduled(*I) <= StageInst1) { |
| 2490 | OrderBeforeUse = true; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2491 | if (MoveUse == 0) |
| 2492 | MoveUse = Pos; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2493 | } else if (MO.isDef() && Reads && stageScheduled(*I) > StageInst1) { |
| 2494 | // Add the instruction after the scheduled instruction. |
| 2495 | OrderAfterDef = true; |
| 2496 | MoveDef = Pos; |
| 2497 | } else if (MO.isUse() && Writes && stageScheduled(*I) == StageInst1) { |
| 2498 | if (cycleScheduled(*I) == cycleScheduled(SU) && !(*I)->isSucc(SU)) { |
| 2499 | OrderBeforeUse = true; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2500 | if (MoveUse == 0) |
| 2501 | MoveUse = Pos; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2502 | } else { |
| 2503 | OrderAfterDef = true; |
| 2504 | MoveDef = Pos; |
| 2505 | } |
| 2506 | } else if (MO.isUse() && Writes && stageScheduled(*I) > StageInst1) { |
| 2507 | OrderBeforeUse = true; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2508 | if (MoveUse == 0) |
| 2509 | MoveUse = Pos; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2510 | if (MoveUse != 0) { |
| 2511 | OrderAfterDef = true; |
| 2512 | MoveDef = Pos - 1; |
| 2513 | } |
| 2514 | } else if (MO.isUse() && Writes && stageScheduled(*I) < StageInst1) { |
| 2515 | // Add the instruction before the scheduled instruction. |
| 2516 | OrderBeforeUse = true; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2517 | if (MoveUse == 0) |
| 2518 | MoveUse = Pos; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2519 | } else if (MO.isUse() && stageScheduled(*I) == StageInst1 && |
| 2520 | isLoopCarriedDefOfUse(SSD, (*I)->getInstr(), MO)) { |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2521 | if (MoveUse == 0) { |
| 2522 | OrderBeforeDef = true; |
| 2523 | MoveUse = Pos; |
| 2524 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | // Check for order dependences between instructions. Make sure the source |
| 2528 | // is ordered before the destination. |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2529 | for (auto &S : SU->Succs) { |
| 2530 | if (S.getSUnit() != *I) |
| 2531 | continue; |
| 2532 | if (S.getKind() == SDep::Order && stageScheduled(*I) == StageInst1) { |
| 2533 | OrderBeforeUse = true; |
| 2534 | if (Pos < MoveUse) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2535 | MoveUse = Pos; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2536 | } |
Jinsong Ji | 9577086 | 2019-07-12 01:59:42 +0000 | [diff] [blame] | 2537 | // We did not handle HW dependences in previous for loop, |
| 2538 | // and we normally set Latency = 0 for Anti deps, |
| 2539 | // so may have nodes in same cycle with Anti denpendent on HW regs. |
| 2540 | else if (S.getKind() == SDep::Anti && stageScheduled(*I) == StageInst1) { |
| 2541 | OrderBeforeUse = true; |
| 2542 | if ((MoveUse == 0) || (Pos < MoveUse)) |
| 2543 | MoveUse = Pos; |
| 2544 | } |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2545 | } |
| 2546 | for (auto &P : SU->Preds) { |
| 2547 | if (P.getSUnit() != *I) |
| 2548 | continue; |
| 2549 | if (P.getKind() == SDep::Order && stageScheduled(*I) == StageInst1) { |
| 2550 | OrderAfterDef = true; |
| 2551 | MoveDef = Pos; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2552 | } |
Krzysztof Parzyszek | 8e1363d | 2018-03-26 16:05:55 +0000 | [diff] [blame] | 2553 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | // A circular dependence. |
| 2557 | if (OrderAfterDef && OrderBeforeUse && MoveUse == MoveDef) |
| 2558 | OrderBeforeUse = false; |
| 2559 | |
| 2560 | // OrderAfterDef takes precedences over OrderBeforeDef. The latter is due |
| 2561 | // to a loop-carried dependence. |
| 2562 | if (OrderBeforeDef) |
| 2563 | OrderBeforeUse = !OrderAfterDef || (MoveUse > MoveDef); |
| 2564 | |
| 2565 | // The uncommon case when the instruction order needs to be updated because |
| 2566 | // there is both a use and def. |
| 2567 | if (OrderBeforeUse && OrderAfterDef) { |
| 2568 | SUnit *UseSU = Insts.at(MoveUse); |
| 2569 | SUnit *DefSU = Insts.at(MoveDef); |
| 2570 | if (MoveUse > MoveDef) { |
| 2571 | Insts.erase(Insts.begin() + MoveUse); |
| 2572 | Insts.erase(Insts.begin() + MoveDef); |
| 2573 | } else { |
| 2574 | Insts.erase(Insts.begin() + MoveDef); |
| 2575 | Insts.erase(Insts.begin() + MoveUse); |
| 2576 | } |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2577 | orderDependence(SSD, UseSU, Insts); |
| 2578 | orderDependence(SSD, SU, Insts); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2579 | orderDependence(SSD, DefSU, Insts); |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2580 | return; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2581 | } |
| 2582 | // Put the new instruction first if there is a use in the list. Otherwise, |
| 2583 | // put it at the end of the list. |
| 2584 | if (OrderBeforeUse) |
| 2585 | Insts.push_front(SU); |
| 2586 | else |
| 2587 | Insts.push_back(SU); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | /// Return true if the scheduled Phi has a loop carried operand. |
| 2591 | bool SMSchedule::isLoopCarried(SwingSchedulerDAG *SSD, MachineInstr &Phi) { |
| 2592 | if (!Phi.isPHI()) |
| 2593 | return false; |
Hiroshi Inoue | c73b6d6 | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 2594 | assert(Phi.isPHI() && "Expecting a Phi."); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2595 | SUnit *DefSU = SSD->getSUnit(&Phi); |
| 2596 | unsigned DefCycle = cycleScheduled(DefSU); |
| 2597 | int DefStage = stageScheduled(DefSU); |
| 2598 | |
| 2599 | unsigned InitVal = 0; |
| 2600 | unsigned LoopVal = 0; |
| 2601 | getPhiRegs(Phi, Phi.getParent(), InitVal, LoopVal); |
| 2602 | SUnit *UseSU = SSD->getSUnit(MRI.getVRegDef(LoopVal)); |
| 2603 | if (!UseSU) |
| 2604 | return true; |
| 2605 | if (UseSU->getInstr()->isPHI()) |
| 2606 | return true; |
| 2607 | unsigned LoopCycle = cycleScheduled(UseSU); |
| 2608 | int LoopStage = stageScheduled(UseSU); |
Simon Pilgrim | 3d8482a | 2016-11-14 10:40:23 +0000 | [diff] [blame] | 2609 | return (LoopCycle > DefCycle) || (LoopStage <= DefStage); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | /// Return true if the instruction is a definition that is loop carried |
| 2613 | /// and defines the use on the next iteration. |
| 2614 | /// v1 = phi(v2, v3) |
| 2615 | /// (Def) v3 = op v1 |
| 2616 | /// (MO) = v1 |
| 2617 | /// If MO appears before Def, then then v1 and v3 may get assigned to the same |
| 2618 | /// register. |
| 2619 | bool SMSchedule::isLoopCarriedDefOfUse(SwingSchedulerDAG *SSD, |
| 2620 | MachineInstr *Def, MachineOperand &MO) { |
| 2621 | if (!MO.isReg()) |
| 2622 | return false; |
| 2623 | if (Def->isPHI()) |
| 2624 | return false; |
| 2625 | MachineInstr *Phi = MRI.getVRegDef(MO.getReg()); |
| 2626 | if (!Phi || !Phi->isPHI() || Phi->getParent() != Def->getParent()) |
| 2627 | return false; |
| 2628 | if (!isLoopCarried(SSD, *Phi)) |
| 2629 | return false; |
| 2630 | unsigned LoopReg = getLoopPhiReg(*Phi, Phi->getParent()); |
| 2631 | for (unsigned i = 0, e = Def->getNumOperands(); i != e; ++i) { |
| 2632 | MachineOperand &DMO = Def->getOperand(i); |
| 2633 | if (!DMO.isReg() || !DMO.isDef()) |
| 2634 | continue; |
| 2635 | if (DMO.getReg() == LoopReg) |
| 2636 | return true; |
| 2637 | } |
| 2638 | return false; |
| 2639 | } |
| 2640 | |
| 2641 | // Check if the generated schedule is valid. This function checks if |
| 2642 | // an instruction that uses a physical register is scheduled in a |
| 2643 | // different stage than the definition. The pipeliner does not handle |
| 2644 | // physical register values that may cross a basic block boundary. |
| 2645 | bool SMSchedule::isValidSchedule(SwingSchedulerDAG *SSD) { |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2646 | for (int i = 0, e = SSD->SUnits.size(); i < e; ++i) { |
| 2647 | SUnit &SU = SSD->SUnits[i]; |
| 2648 | if (!SU.hasPhysRegDefs) |
| 2649 | continue; |
| 2650 | int StageDef = stageScheduled(&SU); |
| 2651 | assert(StageDef != -1 && "Instruction should have been scheduled."); |
| 2652 | for (auto &SI : SU.Succs) |
| 2653 | if (SI.isAssignedRegDep()) |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 2654 | if (Register::isPhysicalRegister(SI.getReg())) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2655 | if (stageScheduled(SI.getSUnit()) != StageDef) |
| 2656 | return false; |
| 2657 | } |
| 2658 | return true; |
| 2659 | } |
| 2660 | |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2661 | /// A property of the node order in swing-modulo-scheduling is |
| 2662 | /// that for nodes outside circuits the following holds: |
| 2663 | /// none of them is scheduled after both a successor and a |
| 2664 | /// predecessor. |
| 2665 | /// The method below checks whether the property is met. |
| 2666 | /// If not, debug information is printed and statistics information updated. |
| 2667 | /// Note that we do not use an assert statement. |
| 2668 | /// The reason is that although an invalid node oder may prevent |
| 2669 | /// the pipeliner from finding a pipelined schedule for arbitrary II, |
| 2670 | /// it does not lead to the generation of incorrect code. |
| 2671 | void SwingSchedulerDAG::checkValidNodeOrder(const NodeSetType &Circuits) const { |
| 2672 | |
| 2673 | // a sorted vector that maps each SUnit to its index in the NodeOrder |
| 2674 | typedef std::pair<SUnit *, unsigned> UnitIndex; |
| 2675 | std::vector<UnitIndex> Indices(NodeOrder.size(), std::make_pair(nullptr, 0)); |
| 2676 | |
| 2677 | for (unsigned i = 0, s = NodeOrder.size(); i < s; ++i) |
| 2678 | Indices.push_back(std::make_pair(NodeOrder[i], i)); |
| 2679 | |
| 2680 | auto CompareKey = [](UnitIndex i1, UnitIndex i2) { |
| 2681 | return std::get<0>(i1) < std::get<0>(i2); |
| 2682 | }; |
| 2683 | |
| 2684 | // sort, so that we can perform a binary search |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 2685 | llvm::sort(Indices, CompareKey); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2686 | |
| 2687 | bool Valid = true; |
David L Kreitzer | febf70a | 2018-03-16 21:21:23 +0000 | [diff] [blame] | 2688 | (void)Valid; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2689 | // for each SUnit in the NodeOrder, check whether |
| 2690 | // it appears after both a successor and a predecessor |
| 2691 | // of the SUnit. If this is the case, and the SUnit |
| 2692 | // is not part of circuit, then the NodeOrder is not |
| 2693 | // valid. |
| 2694 | for (unsigned i = 0, s = NodeOrder.size(); i < s; ++i) { |
| 2695 | SUnit *SU = NodeOrder[i]; |
| 2696 | unsigned Index = i; |
| 2697 | |
| 2698 | bool PredBefore = false; |
| 2699 | bool SuccBefore = false; |
| 2700 | |
| 2701 | SUnit *Succ; |
| 2702 | SUnit *Pred; |
David L Kreitzer | febf70a | 2018-03-16 21:21:23 +0000 | [diff] [blame] | 2703 | (void)Succ; |
| 2704 | (void)Pred; |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2705 | |
| 2706 | for (SDep &PredEdge : SU->Preds) { |
| 2707 | SUnit *PredSU = PredEdge.getSUnit(); |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 2708 | unsigned PredIndex = std::get<1>( |
| 2709 | *llvm::lower_bound(Indices, std::make_pair(PredSU, 0), CompareKey)); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2710 | if (!PredSU->getInstr()->isPHI() && PredIndex < Index) { |
| 2711 | PredBefore = true; |
| 2712 | Pred = PredSU; |
| 2713 | break; |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | for (SDep &SuccEdge : SU->Succs) { |
| 2718 | SUnit *SuccSU = SuccEdge.getSUnit(); |
Jinsong Ji | 1c88445 | 2019-06-13 21:51:12 +0000 | [diff] [blame] | 2719 | // Do not process a boundary node, it was not included in NodeOrder, |
| 2720 | // hence not in Indices either, call to std::lower_bound() below will |
| 2721 | // return Indices.end(). |
| 2722 | if (SuccSU->isBoundaryNode()) |
| 2723 | continue; |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 2724 | unsigned SuccIndex = std::get<1>( |
| 2725 | *llvm::lower_bound(Indices, std::make_pair(SuccSU, 0), CompareKey)); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2726 | if (!SuccSU->getInstr()->isPHI() && SuccIndex < Index) { |
| 2727 | SuccBefore = true; |
| 2728 | Succ = SuccSU; |
| 2729 | break; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | if (PredBefore && SuccBefore && !SU->getInstr()->isPHI()) { |
| 2734 | // instructions in circuits are allowed to be scheduled |
| 2735 | // after both a successor and predecessor. |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 2736 | bool InCircuit = llvm::any_of( |
| 2737 | Circuits, [SU](const NodeSet &Circuit) { return Circuit.count(SU); }); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2738 | if (InCircuit) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2739 | LLVM_DEBUG(dbgs() << "In a circuit, predecessor ";); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2740 | else { |
| 2741 | Valid = false; |
| 2742 | NumNodeOrderIssues++; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2743 | LLVM_DEBUG(dbgs() << "Predecessor ";); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2744 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2745 | LLVM_DEBUG(dbgs() << Pred->NodeNum << " and successor " << Succ->NodeNum |
| 2746 | << " are scheduled before node " << SU->NodeNum |
| 2747 | << "\n";); |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2751 | LLVM_DEBUG({ |
Roorda, Jan-Willem | 4b8bcf0 | 2018-03-07 18:53:36 +0000 | [diff] [blame] | 2752 | if (!Valid) |
| 2753 | dbgs() << "Invalid node order found!\n"; |
| 2754 | }); |
| 2755 | } |
| 2756 | |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2757 | /// Attempt to fix the degenerate cases when the instruction serialization |
| 2758 | /// causes the register lifetimes to overlap. For example, |
| 2759 | /// p' = store_pi(p, b) |
| 2760 | /// = load p, offset |
| 2761 | /// In this case p and p' overlap, which means that two registers are needed. |
| 2762 | /// Instead, this function changes the load to use p' and updates the offset. |
| 2763 | void SwingSchedulerDAG::fixupRegisterOverlaps(std::deque<SUnit *> &Instrs) { |
| 2764 | unsigned OverlapReg = 0; |
| 2765 | unsigned NewBaseReg = 0; |
| 2766 | for (SUnit *SU : Instrs) { |
| 2767 | MachineInstr *MI = SU->getInstr(); |
| 2768 | for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) { |
| 2769 | const MachineOperand &MO = MI->getOperand(i); |
| 2770 | // Look for an instruction that uses p. The instruction occurs in the |
| 2771 | // same cycle but occurs later in the serialized order. |
| 2772 | if (MO.isReg() && MO.isUse() && MO.getReg() == OverlapReg) { |
| 2773 | // Check that the instruction appears in the InstrChanges structure, |
| 2774 | // which contains instructions that can have the offset updated. |
| 2775 | DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It = |
| 2776 | InstrChanges.find(SU); |
| 2777 | if (It != InstrChanges.end()) { |
| 2778 | unsigned BasePos, OffsetPos; |
| 2779 | // Update the base register and adjust the offset. |
| 2780 | if (TII->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos)) { |
Krzysztof Parzyszek | 12bdcab | 2017-10-11 15:59:51 +0000 | [diff] [blame] | 2781 | MachineInstr *NewMI = MF.CloneMachineInstr(MI); |
| 2782 | NewMI->getOperand(BasePos).setReg(NewBaseReg); |
| 2783 | int64_t NewOffset = |
| 2784 | MI->getOperand(OffsetPos).getImm() - It->second.second; |
| 2785 | NewMI->getOperand(OffsetPos).setImm(NewOffset); |
| 2786 | SU->setInstr(NewMI); |
| 2787 | MISUnitMap[NewMI] = SU; |
James Molloy | 790a779 | 2019-08-30 18:49:50 +0000 | [diff] [blame] | 2788 | NewMIs[MI] = NewMI; |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | OverlapReg = 0; |
| 2792 | NewBaseReg = 0; |
| 2793 | break; |
| 2794 | } |
| 2795 | // Look for an instruction of the form p' = op(p), which uses and defines |
| 2796 | // two virtual registers that get allocated to the same physical register. |
| 2797 | unsigned TiedUseIdx = 0; |
| 2798 | if (MI->isRegTiedToUseOperand(i, &TiedUseIdx)) { |
| 2799 | // OverlapReg is p in the example above. |
| 2800 | OverlapReg = MI->getOperand(TiedUseIdx).getReg(); |
| 2801 | // NewBaseReg is p' in the example above. |
| 2802 | NewBaseReg = MI->getOperand(i).getReg(); |
| 2803 | break; |
| 2804 | } |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2809 | /// After the schedule has been formed, call this function to combine |
| 2810 | /// the instructions from the different stages/cycles. That is, this |
| 2811 | /// function creates a schedule that represents a single iteration. |
| 2812 | void SMSchedule::finalizeSchedule(SwingSchedulerDAG *SSD) { |
| 2813 | // Move all instructions to the first stage from later stages. |
| 2814 | for (int cycle = getFirstCycle(); cycle <= getFinalCycle(); ++cycle) { |
| 2815 | for (int stage = 1, lastStage = getMaxStageCount(); stage <= lastStage; |
| 2816 | ++stage) { |
| 2817 | std::deque<SUnit *> &cycleInstrs = |
| 2818 | ScheduledInstrs[cycle + (stage * InitiationInterval)]; |
| 2819 | for (std::deque<SUnit *>::reverse_iterator I = cycleInstrs.rbegin(), |
| 2820 | E = cycleInstrs.rend(); |
| 2821 | I != E; ++I) |
| 2822 | ScheduledInstrs[cycle].push_front(*I); |
| 2823 | } |
| 2824 | } |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2825 | |
| 2826 | // Erase all the elements in the later stages. Only one iteration should |
| 2827 | // remain in the scheduled list, and it contains all the instructions. |
| 2828 | for (int cycle = getFinalCycle() + 1; cycle <= LastCycle; ++cycle) |
| 2829 | ScheduledInstrs.erase(cycle); |
| 2830 | |
| 2831 | // Change the registers in instruction as specified in the InstrChanges |
| 2832 | // map. We need to use the new registers to create the correct order. |
| 2833 | for (int i = 0, e = SSD->SUnits.size(); i != e; ++i) { |
| 2834 | SUnit *SU = &SSD->SUnits[i]; |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2835 | SSD->applyInstrChange(SU->getInstr(), *this); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | // Reorder the instructions in each cycle to fix and improve the |
| 2839 | // generated code. |
| 2840 | for (int Cycle = getFirstCycle(), E = getFinalCycle(); Cycle <= E; ++Cycle) { |
| 2841 | std::deque<SUnit *> &cycleInstrs = ScheduledInstrs[Cycle]; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2842 | std::deque<SUnit *> newOrderPhi; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2843 | for (unsigned i = 0, e = cycleInstrs.size(); i < e; ++i) { |
| 2844 | SUnit *SU = cycleInstrs[i]; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2845 | if (SU->getInstr()->isPHI()) |
| 2846 | newOrderPhi.push_back(SU); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2847 | } |
| 2848 | std::deque<SUnit *> newOrderI; |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2849 | for (unsigned i = 0, e = cycleInstrs.size(); i < e; ++i) { |
| 2850 | SUnit *SU = cycleInstrs[i]; |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2851 | if (!SU->getInstr()->isPHI()) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2852 | orderDependence(SSD, SU, newOrderI); |
| 2853 | } |
| 2854 | // Replace the old order with the new order. |
Krzysztof Parzyszek | f13bbf1 | 2018-03-26 16:23:29 +0000 | [diff] [blame] | 2855 | cycleInstrs.swap(newOrderPhi); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2856 | cycleInstrs.insert(cycleInstrs.end(), newOrderI.begin(), newOrderI.end()); |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 2857 | SSD->fixupRegisterOverlaps(cycleInstrs); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2860 | LLVM_DEBUG(dump();); |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 2863 | void NodeSet::print(raw_ostream &os) const { |
| 2864 | os << "Num nodes " << size() << " rec " << RecMII << " mov " << MaxMOV |
| 2865 | << " depth " << MaxDepth << " col " << Colocate << "\n"; |
| 2866 | for (const auto &I : Nodes) |
| 2867 | os << " SU(" << I->NodeNum << ") " << *(I->getInstr()); |
| 2868 | os << "\n"; |
| 2869 | } |
| 2870 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 2871 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Brendon Cahoon | 254f889 | 2016-07-29 16:44:44 +0000 | [diff] [blame] | 2872 | /// Print the schedule information to the given output. |
| 2873 | void SMSchedule::print(raw_ostream &os) const { |
| 2874 | // Iterate over each cycle. |
| 2875 | for (int cycle = getFirstCycle(); cycle <= getFinalCycle(); ++cycle) { |
| 2876 | // Iterate over each instruction in the cycle. |
| 2877 | const_sched_iterator cycleInstrs = ScheduledInstrs.find(cycle); |
| 2878 | for (SUnit *CI : cycleInstrs->second) { |
| 2879 | os << "cycle " << cycle << " (" << stageScheduled(CI) << ") "; |
| 2880 | os << "(" << CI->NodeNum << ") "; |
| 2881 | CI->getInstr()->print(os); |
| 2882 | os << "\n"; |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | /// Utility function used for debugging to print the schedule. |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 2888 | LLVM_DUMP_METHOD void SMSchedule::dump() const { print(dbgs()); } |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 2889 | LLVM_DUMP_METHOD void NodeSet::dump() const { print(dbgs()); } |
| 2890 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 2891 | #endif |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 2892 | |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2893 | void ResourceManager::initProcResourceVectors( |
| 2894 | const MCSchedModel &SM, SmallVectorImpl<uint64_t> &Masks) { |
| 2895 | unsigned ProcResourceID = 0; |
Adrian Prantl | fa2e358 | 2019-01-14 17:24:11 +0000 | [diff] [blame] | 2896 | |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2897 | // We currently limit the resource kinds to 64 and below so that we can use |
| 2898 | // uint64_t for Masks |
| 2899 | assert(SM.getNumProcResourceKinds() < 64 && |
| 2900 | "Too many kinds of resources, unsupported"); |
| 2901 | // Create a unique bitmask for every processor resource unit. |
| 2902 | // Skip resource at index 0, since it always references 'InvalidUnit'. |
| 2903 | Masks.resize(SM.getNumProcResourceKinds()); |
| 2904 | for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 2905 | const MCProcResourceDesc &Desc = *SM.getProcResource(I); |
| 2906 | if (Desc.SubUnitsIdxBegin) |
| 2907 | continue; |
| 2908 | Masks[I] = 1ULL << ProcResourceID; |
| 2909 | ProcResourceID++; |
| 2910 | } |
| 2911 | // Create a unique bitmask for every processor resource group. |
| 2912 | for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 2913 | const MCProcResourceDesc &Desc = *SM.getProcResource(I); |
| 2914 | if (!Desc.SubUnitsIdxBegin) |
| 2915 | continue; |
| 2916 | Masks[I] = 1ULL << ProcResourceID; |
| 2917 | for (unsigned U = 0; U < Desc.NumUnits; ++U) |
| 2918 | Masks[I] |= Masks[Desc.SubUnitsIdxBegin[U]]; |
| 2919 | ProcResourceID++; |
| 2920 | } |
| 2921 | LLVM_DEBUG({ |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 2922 | if (SwpShowResMask) { |
| 2923 | dbgs() << "ProcResourceDesc:\n"; |
| 2924 | for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 2925 | const MCProcResourceDesc *ProcResource = SM.getProcResource(I); |
| 2926 | dbgs() << format(" %16s(%2d): Mask: 0x%08x, NumUnits:%2d\n", |
| 2927 | ProcResource->Name, I, Masks[I], |
| 2928 | ProcResource->NumUnits); |
| 2929 | } |
| 2930 | dbgs() << " -----------------\n"; |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2931 | } |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2932 | }); |
| 2933 | } |
| 2934 | |
| 2935 | bool ResourceManager::canReserveResources(const MCInstrDesc *MID) const { |
| 2936 | |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 2937 | LLVM_DEBUG({ |
| 2938 | if (SwpDebugResource) |
| 2939 | dbgs() << "canReserveResources:\n"; |
| 2940 | }); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2941 | if (UseDFA) |
| 2942 | return DFAResources->canReserveResources(MID); |
| 2943 | |
| 2944 | unsigned InsnClass = MID->getSchedClass(); |
| 2945 | const MCSchedClassDesc *SCDesc = SM.getSchedClassDesc(InsnClass); |
| 2946 | if (!SCDesc->isValid()) { |
| 2947 | LLVM_DEBUG({ |
| 2948 | dbgs() << "No valid Schedule Class Desc for schedClass!\n"; |
| 2949 | dbgs() << "isPseduo:" << MID->isPseudo() << "\n"; |
| 2950 | }); |
| 2951 | return true; |
| 2952 | } |
| 2953 | |
| 2954 | const MCWriteProcResEntry *I = STI->getWriteProcResBegin(SCDesc); |
| 2955 | const MCWriteProcResEntry *E = STI->getWriteProcResEnd(SCDesc); |
| 2956 | for (; I != E; ++I) { |
| 2957 | if (!I->Cycles) |
| 2958 | continue; |
| 2959 | const MCProcResourceDesc *ProcResource = |
| 2960 | SM.getProcResource(I->ProcResourceIdx); |
| 2961 | unsigned NumUnits = ProcResource->NumUnits; |
| 2962 | LLVM_DEBUG({ |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 2963 | if (SwpDebugResource) |
| 2964 | dbgs() << format(" %16s(%2d): Count: %2d, NumUnits:%2d, Cycles:%2d\n", |
| 2965 | ProcResource->Name, I->ProcResourceIdx, |
| 2966 | ProcResourceCount[I->ProcResourceIdx], NumUnits, |
| 2967 | I->Cycles); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2968 | }); |
| 2969 | if (ProcResourceCount[I->ProcResourceIdx] >= NumUnits) |
| 2970 | return false; |
| 2971 | } |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 2972 | LLVM_DEBUG(if (SwpDebugResource) dbgs() << "return true\n\n";); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2973 | return true; |
| 2974 | } |
| 2975 | |
| 2976 | void ResourceManager::reserveResources(const MCInstrDesc *MID) { |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 2977 | LLVM_DEBUG({ |
| 2978 | if (SwpDebugResource) |
| 2979 | dbgs() << "reserveResources:\n"; |
| 2980 | }); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2981 | if (UseDFA) |
| 2982 | return DFAResources->reserveResources(MID); |
| 2983 | |
| 2984 | unsigned InsnClass = MID->getSchedClass(); |
| 2985 | const MCSchedClassDesc *SCDesc = SM.getSchedClassDesc(InsnClass); |
| 2986 | if (!SCDesc->isValid()) { |
| 2987 | LLVM_DEBUG({ |
| 2988 | dbgs() << "No valid Schedule Class Desc for schedClass!\n"; |
| 2989 | dbgs() << "isPseduo:" << MID->isPseudo() << "\n"; |
| 2990 | }); |
| 2991 | return; |
| 2992 | } |
| 2993 | for (const MCWriteProcResEntry &PRE : |
| 2994 | make_range(STI->getWriteProcResBegin(SCDesc), |
| 2995 | STI->getWriteProcResEnd(SCDesc))) { |
| 2996 | if (!PRE.Cycles) |
| 2997 | continue; |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 2998 | ++ProcResourceCount[PRE.ProcResourceIdx]; |
| 2999 | LLVM_DEBUG({ |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 3000 | if (SwpDebugResource) { |
| 3001 | const MCProcResourceDesc *ProcResource = |
| 3002 | SM.getProcResource(PRE.ProcResourceIdx); |
| 3003 | dbgs() << format(" %16s(%2d): Count: %2d, NumUnits:%2d, Cycles:%2d\n", |
| 3004 | ProcResource->Name, PRE.ProcResourceIdx, |
| 3005 | ProcResourceCount[PRE.ProcResourceIdx], |
| 3006 | ProcResource->NumUnits, PRE.Cycles); |
| 3007 | } |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 3008 | }); |
| 3009 | } |
Jinsong Ji | ba43840 | 2019-06-18 20:24:49 +0000 | [diff] [blame] | 3010 | LLVM_DEBUG({ |
| 3011 | if (SwpDebugResource) |
| 3012 | dbgs() << "reserveResources: done!\n\n"; |
| 3013 | }); |
Jinsong Ji | f6cb3bc | 2019-05-29 03:02:59 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | bool ResourceManager::canReserveResources(const MachineInstr &MI) const { |
| 3017 | return canReserveResources(&MI.getDesc()); |
| 3018 | } |
| 3019 | |
| 3020 | void ResourceManager::reserveResources(const MachineInstr &MI) { |
| 3021 | return reserveResources(&MI.getDesc()); |
| 3022 | } |
| 3023 | |
| 3024 | void ResourceManager::clearResources() { |
| 3025 | if (UseDFA) |
| 3026 | return DFAResources->clearResources(); |
| 3027 | std::fill(ProcResourceCount.begin(), ProcResourceCount.end(), 0); |
| 3028 | } |