Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 1 | //===- HexagonMachineScheduler.cpp - MI Scheduler for Hexagon -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // MachineScheduler schedules machine instructions after phi elimination. It |
| 11 | // preserves LiveIntervals so it can be invoked before register allocation. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 15 | #include "HexagonMachineScheduler.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 16 | #include "HexagonInstrInfo.h" |
Krzysztof Parzyszek | 9be6673 | 2016-07-15 17:48:09 +0000 | [diff] [blame] | 17 | #include "HexagonSubtarget.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/CodeGen/DFAPacketizer.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstr.h" |
Jakub Staszak | df17ddd | 2013-03-10 13:11:23 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/RegisterPressure.h" |
| 26 | #include "llvm/CodeGen/ScheduleDAG.h" |
| 27 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 30 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/TargetSchedule.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Jakub Staszak | df17ddd | 2013-03-10 13:11:23 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
| 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 37 | #include <algorithm> |
| 38 | #include <cassert> |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 39 | #include <iomanip> |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 40 | #include <limits> |
| 41 | #include <memory> |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 42 | #include <sstream> |
| 43 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
| 46 | #define DEBUG_TYPE "machine-scheduler" |
| 47 | |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 48 | static cl::opt<bool> IgnoreBBRegPressure("ignore-bb-reg-pressure", |
| 49 | cl::Hidden, cl::ZeroOrMore, cl::init(false)); |
| 50 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 51 | static cl::opt<bool> UseNewerCandidate("use-newer-candidate", |
| 52 | cl::Hidden, cl::ZeroOrMore, cl::init(true)); |
| 53 | |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 54 | static cl::opt<unsigned> SchedDebugVerboseLevel("misched-verbose-level", |
| 55 | cl::Hidden, cl::ZeroOrMore, cl::init(1)); |
| 56 | |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 57 | // Check if the scheduler should penalize instructions that are available to |
| 58 | // early due to a zero-latency dependence. |
| 59 | static cl::opt<bool> CheckEarlyAvail("check-early-avail", cl::Hidden, |
| 60 | cl::ZeroOrMore, cl::init(true)); |
| 61 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 62 | // This value is used to determine if a register class is a high pressure set. |
| 63 | // We compute the maximum number of registers needed and divided by the total |
| 64 | // available. Then, we compare the result to this value. |
| 65 | static cl::opt<float> RPThreshold("hexagon-reg-pressure", cl::Hidden, |
| 66 | cl::init(0.75f), cl::desc("High register pressure threhold.")); |
| 67 | |
| 68 | /// Return true if there is a dependence between SUd and SUu. |
| 69 | static bool hasDependence(const SUnit *SUd, const SUnit *SUu, |
| 70 | const HexagonInstrInfo &QII) { |
| 71 | if (SUd->Succs.size() == 0) |
| 72 | return false; |
| 73 | |
| 74 | // Enable .cur formation. |
| 75 | if (QII.mayBeCurLoad(*SUd->getInstr())) |
| 76 | return false; |
| 77 | |
| 78 | if (QII.canExecuteInBundle(*SUd->getInstr(), *SUu->getInstr())) |
| 79 | return false; |
| 80 | |
| 81 | for (const auto &S : SUd->Succs) { |
| 82 | // Since we do not add pseudos to packets, might as well |
| 83 | // ignore order dependencies. |
| 84 | if (S.isCtrl()) |
| 85 | continue; |
| 86 | |
| 87 | if (S.getSUnit() == SUu && S.getLatency() > 0) |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
Krzysztof Parzyszek | 6c715e1 | 2016-07-15 20:16:03 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 93 | /// Check if scheduling of this SU is possible |
| 94 | /// in the current packet. |
| 95 | /// It is _not_ precise (statefull), it is more like |
| 96 | /// another heuristic. Many corner cases are figured |
| 97 | /// empirically. |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 98 | bool VLIWResourceModel::isResourceAvailable(SUnit *SU, bool IsTop) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 99 | if (!SU || !SU->getInstr()) |
| 100 | return false; |
| 101 | |
| 102 | // First see if the pipeline could receive this instruction |
| 103 | // in the current cycle. |
| 104 | switch (SU->getInstr()->getOpcode()) { |
| 105 | default: |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 106 | if (!ResourcesModel->canReserveResources(*SU->getInstr())) |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 107 | return false; |
| 108 | case TargetOpcode::EXTRACT_SUBREG: |
| 109 | case TargetOpcode::INSERT_SUBREG: |
| 110 | case TargetOpcode::SUBREG_TO_REG: |
| 111 | case TargetOpcode::REG_SEQUENCE: |
| 112 | case TargetOpcode::IMPLICIT_DEF: |
| 113 | case TargetOpcode::COPY: |
| 114 | case TargetOpcode::INLINEASM: |
| 115 | break; |
| 116 | } |
| 117 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 118 | MachineBasicBlock *MBB = SU->getInstr()->getParent(); |
| 119 | auto &QST = MBB->getParent()->getSubtarget<HexagonSubtarget>(); |
| 120 | const auto &QII = *QST.getInstrInfo(); |
Krzysztof Parzyszek | 786333f | 2016-07-18 16:05:27 +0000 | [diff] [blame] | 121 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 122 | // Now see if there are no other dependencies to instructions already |
| 123 | // in the packet. |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 124 | if (IsTop) { |
| 125 | for (unsigned i = 0, e = Packet.size(); i != e; ++i) |
| 126 | if (hasDependence(Packet[i], SU, QII)) |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 127 | return false; |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 128 | } else { |
| 129 | for (unsigned i = 0, e = Packet.size(); i != e; ++i) |
| 130 | if (hasDependence(SU, Packet[i], QII)) |
| 131 | return false; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /// Keep track of available resources. |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 137 | bool VLIWResourceModel::reserveResources(SUnit *SU, bool IsTop) { |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 138 | bool startNewCycle = false; |
Sergei Larin | 2db64a7 | 2012-09-14 15:07:59 +0000 | [diff] [blame] | 139 | // Artificially reset state. |
| 140 | if (!SU) { |
| 141 | ResourcesModel->clearResources(); |
| 142 | Packet.clear(); |
| 143 | TotalPackets++; |
| 144 | return false; |
| 145 | } |
Krzysztof Parzyszek | 4c6b65f | 2018-03-20 17:03:27 +0000 | [diff] [blame] | 146 | // If this SU does not fit in the packet or the packet is now full |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 147 | // start a new one. |
Krzysztof Parzyszek | 4c6b65f | 2018-03-20 17:03:27 +0000 | [diff] [blame] | 148 | if (!isResourceAvailable(SU, IsTop) || |
| 149 | Packet.size() >= SchedModel->getIssueWidth()) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 150 | ResourcesModel->clearResources(); |
| 151 | Packet.clear(); |
| 152 | TotalPackets++; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 153 | startNewCycle = true; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | switch (SU->getInstr()->getOpcode()) { |
| 157 | default: |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 158 | ResourcesModel->reserveResources(*SU->getInstr()); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 159 | break; |
| 160 | case TargetOpcode::EXTRACT_SUBREG: |
| 161 | case TargetOpcode::INSERT_SUBREG: |
| 162 | case TargetOpcode::SUBREG_TO_REG: |
| 163 | case TargetOpcode::REG_SEQUENCE: |
| 164 | case TargetOpcode::IMPLICIT_DEF: |
| 165 | case TargetOpcode::KILL: |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 166 | case TargetOpcode::CFI_INSTRUCTION: |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 167 | case TargetOpcode::EH_LABEL: |
| 168 | case TargetOpcode::COPY: |
| 169 | case TargetOpcode::INLINEASM: |
| 170 | break; |
| 171 | } |
| 172 | Packet.push_back(SU); |
| 173 | |
| 174 | #ifndef NDEBUG |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 175 | LLVM_DEBUG(dbgs() << "Packet[" << TotalPackets << "]:\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 176 | for (unsigned i = 0, e = Packet.size(); i != e; ++i) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 177 | LLVM_DEBUG(dbgs() << "\t[" << i << "] SU("); |
| 178 | LLVM_DEBUG(dbgs() << Packet[i]->NodeNum << ")\t"); |
| 179 | LLVM_DEBUG(Packet[i]->getInstr()->dump()); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 183 | return startNewCycle; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 186 | /// schedule - Called back from MachineScheduler::runOnMachineFunction |
| 187 | /// after setting up the current scheduling region. [RegionBegin, RegionEnd) |
| 188 | /// only includes instructions that have DAG nodes, not scheduling boundaries. |
| 189 | void VLIWMachineScheduler::schedule() { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 190 | LLVM_DEBUG(dbgs() << "********** MI Converging Scheduling VLIW " |
| 191 | << printMBBReference(*BB) << " " << BB->getName() |
| 192 | << " in_func " << BB->getParent()->getName() |
| 193 | << " at loop depth " << MLI->getLoopDepth(BB) << " \n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 194 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 195 | buildDAGWithRegPressure(); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 196 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 197 | Topo.InitDAGTopologicalSorting(); |
| 198 | |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 199 | // Postprocess the DAG to add platform-specific artificial dependencies. |
| 200 | postprocessDAG(); |
| 201 | |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 202 | SmallVector<SUnit*, 8> TopRoots, BotRoots; |
| 203 | findRootsAndBiasEdges(TopRoots, BotRoots); |
| 204 | |
| 205 | // Initialize the strategy before modifying the DAG. |
| 206 | SchedImpl->initialize(this); |
| 207 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 208 | LLVM_DEBUG(unsigned maxH = 0; |
| 209 | for (unsigned su = 0, e = SUnits.size(); su != e; |
| 210 | ++su) if (SUnits[su].getHeight() > maxH) maxH = |
| 211 | SUnits[su].getHeight(); |
| 212 | dbgs() << "Max Height " << maxH << "\n";); |
| 213 | LLVM_DEBUG(unsigned maxD = 0; |
| 214 | for (unsigned su = 0, e = SUnits.size(); su != e; |
| 215 | ++su) if (SUnits[su].getDepth() > maxD) maxD = |
| 216 | SUnits[su].getDepth(); |
| 217 | dbgs() << "Max Depth " << maxD << "\n";); |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame^] | 218 | LLVM_DEBUG(dump()); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 219 | |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 220 | initQueues(TopRoots, BotRoots); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 221 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 222 | bool IsTopNode = false; |
James Y Knight | e72b0db | 2015-09-18 18:52:20 +0000 | [diff] [blame] | 223 | while (true) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 224 | LLVM_DEBUG( |
| 225 | dbgs() << "** VLIWMachineScheduler::schedule picking next node\n"); |
James Y Knight | e72b0db | 2015-09-18 18:52:20 +0000 | [diff] [blame] | 226 | SUnit *SU = SchedImpl->pickNode(IsTopNode); |
| 227 | if (!SU) break; |
| 228 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 229 | if (!checkSchedLimit()) |
| 230 | break; |
| 231 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 232 | scheduleMI(SU, IsTopNode); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 233 | |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 234 | // Notify the scheduling strategy after updating the DAG. |
| 235 | SchedImpl->schedNode(SU, IsTopNode); |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 236 | |
| 237 | updateQueues(SU, IsTopNode); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 238 | } |
| 239 | assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); |
| 240 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 241 | placeDebugValues(); |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 242 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 243 | LLVM_DEBUG({ |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 244 | dbgs() << "*** Final schedule for " |
| 245 | << printMBBReference(*begin()->getParent()) << " ***\n"; |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 246 | dumpSchedule(); |
| 247 | dbgs() << '\n'; |
| 248 | }); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Andrew Trick | 7a8e100 | 2012-09-11 00:39:15 +0000 | [diff] [blame] | 251 | void ConvergingVLIWScheduler::initialize(ScheduleDAGMI *dag) { |
| 252 | DAG = static_cast<VLIWMachineScheduler*>(dag); |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 253 | SchedModel = DAG->getSchedModel(); |
Andrew Trick | 553e0fe | 2013-02-13 19:22:27 +0000 | [diff] [blame] | 254 | |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 255 | Top.init(DAG, SchedModel); |
| 256 | Bot.init(DAG, SchedModel); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 257 | |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 258 | // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or |
| 259 | // are disabled, then these HazardRecs will be disabled. |
| 260 | const InstrItineraryData *Itin = DAG->getSchedModel()->getInstrItineraries(); |
Eric Christopher | f8b8e4a | 2015-02-02 22:11:40 +0000 | [diff] [blame] | 261 | const TargetSubtargetInfo &STI = DAG->MF.getSubtarget(); |
| 262 | const TargetInstrInfo *TII = STI.getInstrInfo(); |
Andrew Trick | 553e0fe | 2013-02-13 19:22:27 +0000 | [diff] [blame] | 263 | delete Top.HazardRec; |
| 264 | delete Bot.HazardRec; |
Eric Christopher | f8b8e4a | 2015-02-02 22:11:40 +0000 | [diff] [blame] | 265 | Top.HazardRec = TII->CreateTargetMIHazardRecognizer(Itin, DAG); |
| 266 | Bot.HazardRec = TII->CreateTargetMIHazardRecognizer(Itin, DAG); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 267 | |
Chandler Carruth | c18e39c | 2013-07-27 10:48:45 +0000 | [diff] [blame] | 268 | delete Top.ResourceModel; |
| 269 | delete Bot.ResourceModel; |
Eric Christopher | f8b8e4a | 2015-02-02 22:11:40 +0000 | [diff] [blame] | 270 | Top.ResourceModel = new VLIWResourceModel(STI, DAG->getSchedModel()); |
| 271 | Bot.ResourceModel = new VLIWResourceModel(STI, DAG->getSchedModel()); |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 272 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 273 | const std::vector<unsigned> &MaxPressure = |
| 274 | DAG->getRegPressure().MaxSetPressure; |
| 275 | HighPressureSets.assign(MaxPressure.size(), 0); |
| 276 | for (unsigned i = 0, e = MaxPressure.size(); i < e; ++i) { |
| 277 | unsigned Limit = DAG->getRegClassInfo()->getRegPressureSetLimit(i); |
| 278 | HighPressureSets[i] = |
Krzysztof Parzyszek | 2c4231d | 2018-03-20 13:28:46 +0000 | [diff] [blame] | 279 | ((float) MaxPressure[i] > ((float) Limit * RPThreshold)); |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 282 | assert((!ForceTopDown || !ForceBottomUp) && |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 283 | "-misched-topdown incompatible with -misched-bottomup"); |
| 284 | } |
| 285 | |
| 286 | void ConvergingVLIWScheduler::releaseTopNode(SUnit *SU) { |
| 287 | if (SU->isScheduled) |
| 288 | return; |
| 289 | |
Krzysztof Parzyszek | 2be7ead | 2016-07-18 16:15:15 +0000 | [diff] [blame] | 290 | for (const SDep &PI : SU->Preds) { |
| 291 | unsigned PredReadyCycle = PI.getSUnit()->TopReadyCycle; |
| 292 | unsigned MinLatency = PI.getLatency(); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 293 | #ifndef NDEBUG |
| 294 | Top.MaxMinLatency = std::max(MinLatency, Top.MaxMinLatency); |
| 295 | #endif |
| 296 | if (SU->TopReadyCycle < PredReadyCycle + MinLatency) |
| 297 | SU->TopReadyCycle = PredReadyCycle + MinLatency; |
| 298 | } |
| 299 | Top.releaseNode(SU, SU->TopReadyCycle); |
| 300 | } |
| 301 | |
| 302 | void ConvergingVLIWScheduler::releaseBottomNode(SUnit *SU) { |
| 303 | if (SU->isScheduled) |
| 304 | return; |
| 305 | |
| 306 | assert(SU->getInstr() && "Scheduled SUnit must have instr"); |
| 307 | |
| 308 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 309 | I != E; ++I) { |
| 310 | unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle; |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 311 | unsigned MinLatency = I->getLatency(); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 312 | #ifndef NDEBUG |
| 313 | Bot.MaxMinLatency = std::max(MinLatency, Bot.MaxMinLatency); |
| 314 | #endif |
| 315 | if (SU->BotReadyCycle < SuccReadyCycle + MinLatency) |
| 316 | SU->BotReadyCycle = SuccReadyCycle + MinLatency; |
| 317 | } |
| 318 | Bot.releaseNode(SU, SU->BotReadyCycle); |
| 319 | } |
| 320 | |
| 321 | /// Does this SU have a hazard within the current instruction group. |
| 322 | /// |
| 323 | /// The scheduler supports two modes of hazard recognition. The first is the |
| 324 | /// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that |
| 325 | /// supports highly complicated in-order reservation tables |
| 326 | /// (ScoreboardHazardRecognizer) and arbitrary target-specific logic. |
| 327 | /// |
| 328 | /// The second is a streamlined mechanism that checks for hazards based on |
| 329 | /// simple counters that the scheduler itself maintains. It explicitly checks |
| 330 | /// for instruction dispatch limitations, including the number of micro-ops that |
| 331 | /// can dispatch per cycle. |
| 332 | /// |
| 333 | /// TODO: Also check whether the SU must start a new group. |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 334 | bool ConvergingVLIWScheduler::VLIWSchedBoundary::checkHazard(SUnit *SU) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 335 | if (HazardRec->isEnabled()) |
| 336 | return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard; |
| 337 | |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 338 | unsigned uops = SchedModel->getNumMicroOps(SU->getInstr()); |
| 339 | if (IssueCount + uops > SchedModel->getIssueWidth()) |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 340 | return true; |
| 341 | |
| 342 | return false; |
| 343 | } |
| 344 | |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 345 | void ConvergingVLIWScheduler::VLIWSchedBoundary::releaseNode(SUnit *SU, |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 346 | unsigned ReadyCycle) { |
| 347 | if (ReadyCycle < MinReadyCycle) |
| 348 | MinReadyCycle = ReadyCycle; |
| 349 | |
| 350 | // Check for interlocks first. For the purpose of other heuristics, an |
| 351 | // instruction that cannot issue appears as if it's not in the ReadyQueue. |
| 352 | if (ReadyCycle > CurrCycle || checkHazard(SU)) |
| 353 | |
| 354 | Pending.push(SU); |
| 355 | else |
| 356 | Available.push(SU); |
| 357 | } |
| 358 | |
| 359 | /// Move the boundary of scheduled code by one cycle. |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 360 | void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpCycle() { |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 361 | unsigned Width = SchedModel->getIssueWidth(); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 362 | IssueCount = (IssueCount <= Width) ? 0 : IssueCount - Width; |
| 363 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 364 | assert(MinReadyCycle < std::numeric_limits<unsigned>::max() && |
| 365 | "MinReadyCycle uninitialized"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 366 | unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle); |
| 367 | |
| 368 | if (!HazardRec->isEnabled()) { |
| 369 | // Bypass HazardRec virtual calls. |
| 370 | CurrCycle = NextCycle; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 371 | } else { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 372 | // Bypass getHazardType calls in case of long latency. |
| 373 | for (; CurrCycle != NextCycle; ++CurrCycle) { |
| 374 | if (isTop()) |
| 375 | HazardRec->AdvanceCycle(); |
| 376 | else |
| 377 | HazardRec->RecedeCycle(); |
| 378 | } |
| 379 | } |
| 380 | CheckPending = true; |
| 381 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 382 | LLVM_DEBUG(dbgs() << "*** Next cycle " << Available.getName() << " cycle " |
| 383 | << CurrCycle << '\n'); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /// Move the boundary of scheduled code by one SUnit. |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 387 | void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpNode(SUnit *SU) { |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 388 | bool startNewCycle = false; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 389 | |
| 390 | // Update the reservation table. |
| 391 | if (HazardRec->isEnabled()) { |
| 392 | if (!isTop() && SU->isCall) { |
| 393 | // Calls are scheduled with their preceding instructions. For bottom-up |
| 394 | // scheduling, clear the pipeline state before emitting. |
| 395 | HazardRec->Reset(); |
| 396 | } |
| 397 | HazardRec->EmitInstruction(SU); |
| 398 | } |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 399 | |
| 400 | // Update DFA model. |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 401 | startNewCycle = ResourceModel->reserveResources(SU, isTop()); |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 402 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 403 | // Check the instruction group dispatch limit. |
| 404 | // TODO: Check if this SU must end a dispatch group. |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 405 | IssueCount += SchedModel->getNumMicroOps(SU->getInstr()); |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 406 | if (startNewCycle) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 407 | LLVM_DEBUG(dbgs() << "*** Max instrs at cycle " << CurrCycle << '\n'); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 408 | bumpCycle(); |
| 409 | } |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 410 | else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 411 | LLVM_DEBUG(dbgs() << "*** IssueCount " << IssueCount << " at cycle " |
| 412 | << CurrCycle << '\n'); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /// Release pending ready nodes in to the available queue. This makes them |
| 416 | /// visible to heuristics. |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 417 | void ConvergingVLIWScheduler::VLIWSchedBoundary::releasePending() { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 418 | // If the available queue is empty, it is safe to reset MinReadyCycle. |
| 419 | if (Available.empty()) |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 420 | MinReadyCycle = std::numeric_limits<unsigned>::max(); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 421 | |
| 422 | // Check to see if any of the pending instructions are ready to issue. If |
| 423 | // so, add them to the available queue. |
| 424 | for (unsigned i = 0, e = Pending.size(); i != e; ++i) { |
| 425 | SUnit *SU = *(Pending.begin()+i); |
| 426 | unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle; |
| 427 | |
| 428 | if (ReadyCycle < MinReadyCycle) |
| 429 | MinReadyCycle = ReadyCycle; |
| 430 | |
| 431 | if (ReadyCycle > CurrCycle) |
| 432 | continue; |
| 433 | |
| 434 | if (checkHazard(SU)) |
| 435 | continue; |
| 436 | |
| 437 | Available.push(SU); |
| 438 | Pending.remove(Pending.begin()+i); |
| 439 | --i; --e; |
| 440 | } |
| 441 | CheckPending = false; |
| 442 | } |
| 443 | |
| 444 | /// Remove SU from the ready set for this boundary. |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 445 | void ConvergingVLIWScheduler::VLIWSchedBoundary::removeReady(SUnit *SU) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 446 | if (Available.isInQueue(SU)) |
| 447 | Available.remove(Available.find(SU)); |
| 448 | else { |
| 449 | assert(Pending.isInQueue(SU) && "bad ready count"); |
| 450 | Pending.remove(Pending.find(SU)); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /// If this queue only has one ready candidate, return it. As a side effect, |
| 455 | /// advance the cycle until at least one node is ready. If multiple instructions |
| 456 | /// are ready, return NULL. |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 457 | SUnit *ConvergingVLIWScheduler::VLIWSchedBoundary::pickOnlyChoice() { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 458 | if (CheckPending) |
| 459 | releasePending(); |
| 460 | |
Krzysztof Parzyszek | 73be83d | 2018-03-20 16:22:06 +0000 | [diff] [blame] | 461 | auto AdvanceCycle = [this]() { |
| 462 | if (Available.empty()) |
| 463 | return true; |
| 464 | if (Available.size() == 1 && Pending.size() > 0) |
| 465 | return !ResourceModel->isResourceAvailable(*Available.begin(), isTop()) || |
| 466 | getWeakLeft(*Available.begin(), isTop()) != 0; |
| 467 | return false; |
| 468 | }; |
| 469 | for (unsigned i = 0; AdvanceCycle(); ++i) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 470 | assert(i <= (HazardRec->getMaxLookAhead() + MaxMinLatency) && |
| 471 | "permanent hazard"); (void)i; |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 472 | ResourceModel->reserveResources(nullptr, isTop()); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 473 | bumpCycle(); |
| 474 | releasePending(); |
| 475 | } |
| 476 | if (Available.size() == 1) |
| 477 | return *Available.begin(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 478 | return nullptr; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | #ifndef NDEBUG |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 482 | void ConvergingVLIWScheduler::traceCandidate(const char *Label, |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 483 | const ReadyQueue &Q, SUnit *SU, int Cost, PressureChange P) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 484 | dbgs() << Label << " " << Q.getName() << " "; |
| 485 | if (P.isValid()) |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 486 | dbgs() << DAG->TRI->getRegPressureSetName(P.getPSet()) << ":" |
| 487 | << P.getUnitInc() << " "; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 488 | else |
| 489 | dbgs() << " "; |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 490 | dbgs() << "cost(" << Cost << ")\t"; |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame^] | 491 | DAG->dumpNode(*SU); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 492 | } |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 493 | |
| 494 | // Very detailed queue dump, to be used with higher verbosity levels. |
| 495 | void ConvergingVLIWScheduler::readyQueueVerboseDump( |
| 496 | const RegPressureTracker &RPTracker, SchedCandidate &Candidate, |
| 497 | ReadyQueue &Q) { |
| 498 | RegPressureTracker &TempTracker = const_cast<RegPressureTracker &>(RPTracker); |
| 499 | |
| 500 | dbgs() << ">>> " << Q.getName() << "\n"; |
| 501 | for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { |
| 502 | RegPressureDelta RPDelta; |
| 503 | TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta, |
| 504 | DAG->getRegionCriticalPSets(), |
| 505 | DAG->getRegPressure().MaxSetPressure); |
| 506 | std::stringstream dbgstr; |
| 507 | dbgstr << "SU(" << std::setw(3) << (*I)->NodeNum << ")"; |
| 508 | dbgs() << dbgstr.str(); |
| 509 | SchedulingCost(Q, *I, Candidate, RPDelta, true); |
| 510 | dbgs() << "\t"; |
| 511 | (*I)->getInstr()->dump(); |
| 512 | } |
| 513 | dbgs() << "\n"; |
| 514 | } |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 515 | #endif |
| 516 | |
Nirav Dave | 6a38cc6 | 2017-06-08 18:49:25 +0000 | [diff] [blame] | 517 | /// isSingleUnscheduledPred - If SU2 is the only unscheduled predecessor |
| 518 | /// of SU, return true (we may have duplicates) |
| 519 | static inline bool isSingleUnscheduledPred(SUnit *SU, SUnit *SU2) { |
| 520 | if (SU->NumPredsLeft == 0) |
| 521 | return false; |
| 522 | |
| 523 | for (auto &Pred : SU->Preds) { |
| 524 | // We found an available, but not scheduled, predecessor. |
| 525 | if (!Pred.getSUnit()->isScheduled && (Pred.getSUnit() != SU2)) |
| 526 | return false; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 527 | } |
Nirav Dave | 6a38cc6 | 2017-06-08 18:49:25 +0000 | [diff] [blame] | 528 | |
| 529 | return true; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Nirav Dave | 6a38cc6 | 2017-06-08 18:49:25 +0000 | [diff] [blame] | 532 | /// isSingleUnscheduledSucc - If SU2 is the only unscheduled successor |
| 533 | /// of SU, return true (we may have duplicates) |
| 534 | static inline bool isSingleUnscheduledSucc(SUnit *SU, SUnit *SU2) { |
| 535 | if (SU->NumSuccsLeft == 0) |
| 536 | return false; |
| 537 | |
| 538 | for (auto &Succ : SU->Succs) { |
| 539 | // We found an available, but not scheduled, successor. |
| 540 | if (!Succ.getSUnit()->isScheduled && (Succ.getSUnit() != SU2)) |
| 541 | return false; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 542 | } |
Nirav Dave | 6a38cc6 | 2017-06-08 18:49:25 +0000 | [diff] [blame] | 543 | return true; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 546 | /// Check if the instruction changes the register pressure of a register in the |
| 547 | /// high pressure set. The function returns a negative value if the pressure |
| 548 | /// decreases and a positive value is the pressure increases. If the instruction |
| 549 | /// doesn't use a high pressure register or doesn't change the register |
| 550 | /// pressure, then return 0. |
| 551 | int ConvergingVLIWScheduler::pressureChange(const SUnit *SU, bool isBotUp) { |
| 552 | PressureDiff &PD = DAG->getPressureDiff(SU); |
| 553 | for (auto &P : PD) { |
| 554 | if (!P.isValid()) |
| 555 | continue; |
| 556 | // The pressure differences are computed bottom-up, so the comparision for |
| 557 | // an increase is positive in the bottom direction, but negative in the |
| 558 | // top-down direction. |
| 559 | if (HighPressureSets[P.getPSet()]) |
| 560 | return (isBotUp ? P.getUnitInc() : -P.getUnitInc()); |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 565 | // Constants used to denote relative importance of |
| 566 | // heuristic components for cost computation. |
| 567 | static const unsigned PriorityOne = 200; |
Eli Friedman | 8f06d55 | 2013-09-11 00:41:02 +0000 | [diff] [blame] | 568 | static const unsigned PriorityTwo = 50; |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 569 | static const unsigned PriorityThree = 75; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 570 | static const unsigned ScaleTwo = 10; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 571 | |
| 572 | /// Single point to compute overall scheduling cost. |
| 573 | /// TODO: More heuristics will be used soon. |
| 574 | int ConvergingVLIWScheduler::SchedulingCost(ReadyQueue &Q, SUnit *SU, |
| 575 | SchedCandidate &Candidate, |
| 576 | RegPressureDelta &Delta, |
| 577 | bool verbose) { |
| 578 | // Initial trivial priority. |
| 579 | int ResCount = 1; |
| 580 | |
| 581 | // Do not waste time on a node that is already scheduled. |
| 582 | if (!SU || SU->isScheduled) |
| 583 | return ResCount; |
| 584 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 585 | LLVM_DEBUG(if (verbose) dbgs() |
| 586 | << ((Q.getID() == TopQID) ? "(top|" : "(bot|")); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 587 | // Forced priority is high. |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 588 | if (SU->isScheduleHigh) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 589 | ResCount += PriorityOne; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 590 | LLVM_DEBUG(dbgs() << "H|"); |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 591 | } |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 592 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 593 | unsigned IsAvailableAmt = 0; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 594 | // Critical path first. |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 595 | if (Q.getID() == TopQID) { |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 596 | if (Top.isLatencyBound(SU)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 597 | LLVM_DEBUG(if (verbose) dbgs() << "LB|"); |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 598 | ResCount += (SU->getHeight() * ScaleTwo); |
| 599 | } |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 600 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 601 | LLVM_DEBUG(if (verbose) { |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 602 | std::stringstream dbgstr; |
| 603 | dbgstr << "h" << std::setw(3) << SU->getHeight() << "|"; |
| 604 | dbgs() << dbgstr.str(); |
| 605 | }); |
| 606 | |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 607 | // If resources are available for it, multiply the |
| 608 | // chance of scheduling. |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 609 | if (Top.ResourceModel->isResourceAvailable(SU, true)) { |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 610 | IsAvailableAmt = (PriorityTwo + PriorityThree); |
| 611 | ResCount += IsAvailableAmt; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 612 | LLVM_DEBUG(if (verbose) dbgs() << "A|"); |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 613 | } else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 614 | LLVM_DEBUG(if (verbose) dbgs() << " |"); |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 615 | } else { |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 616 | if (Bot.isLatencyBound(SU)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 617 | LLVM_DEBUG(if (verbose) dbgs() << "LB|"); |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 618 | ResCount += (SU->getDepth() * ScaleTwo); |
| 619 | } |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 620 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 621 | LLVM_DEBUG(if (verbose) { |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 622 | std::stringstream dbgstr; |
| 623 | dbgstr << "d" << std::setw(3) << SU->getDepth() << "|"; |
| 624 | dbgs() << dbgstr.str(); |
| 625 | }); |
| 626 | |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 627 | // If resources are available for it, multiply the |
| 628 | // chance of scheduling. |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 629 | if (Bot.ResourceModel->isResourceAvailable(SU, false)) { |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 630 | IsAvailableAmt = (PriorityTwo + PriorityThree); |
| 631 | ResCount += IsAvailableAmt; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 632 | LLVM_DEBUG(if (verbose) dbgs() << "A|"); |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 633 | } else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 634 | LLVM_DEBUG(if (verbose) dbgs() << " |"); |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | unsigned NumNodesBlocking = 0; |
| 638 | if (Q.getID() == TopQID) { |
| 639 | // How many SUs does it block from scheduling? |
| 640 | // Look at all of the successors of this node. |
| 641 | // Count the number of nodes that |
| 642 | // this node is the sole unscheduled node for. |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 643 | if (Top.isLatencyBound(SU)) |
| 644 | for (const SDep &SI : SU->Succs) |
| 645 | if (isSingleUnscheduledPred(SI.getSUnit(), SU)) |
| 646 | ++NumNodesBlocking; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 647 | } else { |
| 648 | // How many unscheduled predecessors block this node? |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 649 | if (Bot.isLatencyBound(SU)) |
| 650 | for (const SDep &PI : SU->Preds) |
| 651 | if (isSingleUnscheduledSucc(PI.getSUnit(), SU)) |
| 652 | ++NumNodesBlocking; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 653 | } |
| 654 | ResCount += (NumNodesBlocking * ScaleTwo); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 655 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 656 | LLVM_DEBUG(if (verbose) { |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 657 | std::stringstream dbgstr; |
| 658 | dbgstr << "blk " << std::setw(2) << NumNodesBlocking << ")|"; |
| 659 | dbgs() << dbgstr.str(); |
| 660 | }); |
| 661 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 662 | // Factor in reg pressure as a heuristic. |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 663 | if (!IgnoreBBRegPressure) { |
| 664 | // Decrease priority by the amount that register pressure exceeds the limit. |
| 665 | ResCount -= (Delta.Excess.getUnitInc()*PriorityOne); |
| 666 | // Decrease priority if register pressure exceeds the limit. |
| 667 | ResCount -= (Delta.CriticalMax.getUnitInc()*PriorityOne); |
| 668 | // Decrease priority slightly if register pressure would increase over the |
| 669 | // current maximum. |
| 670 | ResCount -= (Delta.CurrentMax.getUnitInc()*PriorityTwo); |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 671 | // If there are register pressure issues, then we remove the value added for |
| 672 | // the instruction being available. The rationale is that we really don't |
| 673 | // want to schedule an instruction that causes a spill. |
| 674 | if (IsAvailableAmt && pressureChange(SU, Q.getID() != TopQID) > 0 && |
| 675 | (Delta.Excess.getUnitInc() || Delta.CriticalMax.getUnitInc() || |
| 676 | Delta.CurrentMax.getUnitInc())) |
| 677 | ResCount -= IsAvailableAmt; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 678 | LLVM_DEBUG(if (verbose) { |
| 679 | dbgs() << "RP " << Delta.Excess.getUnitInc() << "/" |
| 680 | << Delta.CriticalMax.getUnitInc() << "/" |
| 681 | << Delta.CurrentMax.getUnitInc() << ")|"; |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 682 | }); |
| 683 | } |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 684 | |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 685 | // Give a little extra priority to a .cur instruction if there is a resource |
| 686 | // available for it. |
Krzysztof Parzyszek | 6c715e1 | 2016-07-15 20:16:03 +0000 | [diff] [blame] | 687 | auto &QST = DAG->MF.getSubtarget<HexagonSubtarget>(); |
| 688 | auto &QII = *QST.getInstrInfo(); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 689 | if (SU->isInstr() && QII.mayBeCurLoad(*SU->getInstr())) { |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 690 | if (Q.getID() == TopQID && |
| 691 | Top.ResourceModel->isResourceAvailable(SU, true)) { |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 692 | ResCount += PriorityTwo; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 693 | LLVM_DEBUG(if (verbose) dbgs() << "C|"); |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 694 | } else if (Q.getID() == BotQID && |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 695 | Bot.ResourceModel->isResourceAvailable(SU, false)) { |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 696 | ResCount += PriorityTwo; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 697 | LLVM_DEBUG(if (verbose) dbgs() << "C|"); |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 701 | // Give preference to a zero latency instruction if the dependent |
| 702 | // instruction is in the current packet. |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame] | 703 | if (Q.getID() == TopQID && getWeakLeft(SU, true) == 0) { |
Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 704 | for (const SDep &PI : SU->Preds) { |
| 705 | if (!PI.getSUnit()->getInstr()->isPseudo() && PI.isAssignedRegDep() && |
| 706 | PI.getLatency() == 0 && |
| 707 | Top.ResourceModel->isInPacket(PI.getSUnit())) { |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 708 | ResCount += PriorityThree; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 709 | LLVM_DEBUG(if (verbose) dbgs() << "Z|"); |
Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame] | 712 | } else if (Q.getID() == BotQID && getWeakLeft(SU, false) == 0) { |
Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 713 | for (const SDep &SI : SU->Succs) { |
| 714 | if (!SI.getSUnit()->getInstr()->isPseudo() && SI.isAssignedRegDep() && |
| 715 | SI.getLatency() == 0 && |
| 716 | Bot.ResourceModel->isInPacket(SI.getSUnit())) { |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 717 | ResCount += PriorityThree; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 718 | LLVM_DEBUG(if (verbose) dbgs() << "Z|"); |
Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 723 | // If the instruction has a non-zero latency dependence with an instruction in |
| 724 | // the current packet, then it should not be scheduled yet. The case occurs |
| 725 | // when the dependent instruction is scheduled in a new packet, so the |
| 726 | // scheduler updates the current cycle and pending instructions become |
| 727 | // available. |
| 728 | if (CheckEarlyAvail) { |
| 729 | if (Q.getID() == TopQID) { |
| 730 | for (const auto &PI : SU->Preds) { |
| 731 | if (PI.getLatency() > 0 && |
| 732 | Top.ResourceModel->isInPacket(PI.getSUnit())) { |
| 733 | ResCount -= PriorityOne; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 734 | LLVM_DEBUG(if (verbose) dbgs() << "D|"); |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | } else { |
| 738 | for (const auto &SI : SU->Succs) { |
| 739 | if (SI.getLatency() > 0 && |
| 740 | Bot.ResourceModel->isInPacket(SI.getSUnit())) { |
| 741 | ResCount -= PriorityOne; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 742 | LLVM_DEBUG(if (verbose) dbgs() << "D|"); |
Krzysztof Parzyszek | 3467e9d | 2016-07-18 14:52:13 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 748 | LLVM_DEBUG(if (verbose) { |
Krzysztof Parzyszek | f05dc4d | 2016-07-18 15:47:25 +0000 | [diff] [blame] | 749 | std::stringstream dbgstr; |
| 750 | dbgstr << "Total " << std::setw(4) << ResCount << ")"; |
| 751 | dbgs() << dbgstr.str(); |
| 752 | }); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 753 | |
| 754 | return ResCount; |
| 755 | } |
| 756 | |
| 757 | /// Pick the best candidate from the top queue. |
| 758 | /// |
| 759 | /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during |
| 760 | /// DAG building. To adjust for the current scheduling location we need to |
| 761 | /// maintain the number of vreg uses remaining to be top-scheduled. |
| 762 | ConvergingVLIWScheduler::CandResult ConvergingVLIWScheduler:: |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 763 | pickNodeFromQueue(VLIWSchedBoundary &Zone, const RegPressureTracker &RPTracker, |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 764 | SchedCandidate &Candidate) { |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 765 | ReadyQueue &Q = Zone.Available; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 766 | LLVM_DEBUG(if (SchedDebugVerboseLevel > 1) |
| 767 | readyQueueVerboseDump(RPTracker, Candidate, Q); |
| 768 | else Q.dump();); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 769 | |
| 770 | // getMaxPressureDelta temporarily modifies the tracker. |
| 771 | RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); |
| 772 | |
| 773 | // BestSU remains NULL if no top candidates beat the best existing candidate. |
| 774 | CandResult FoundCandidate = NoCand; |
| 775 | for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { |
| 776 | RegPressureDelta RPDelta; |
| 777 | TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta, |
| 778 | DAG->getRegionCriticalPSets(), |
| 779 | DAG->getRegPressure().MaxSetPressure); |
| 780 | |
| 781 | int CurrentCost = SchedulingCost(Q, *I, Candidate, RPDelta, false); |
| 782 | |
| 783 | // Initialize the candidate if needed. |
| 784 | if (!Candidate.SU) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 785 | LLVM_DEBUG(traceCandidate("DCAND", Q, *I, CurrentCost)); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 786 | Candidate.SU = *I; |
| 787 | Candidate.RPDelta = RPDelta; |
| 788 | Candidate.SCost = CurrentCost; |
| 789 | FoundCandidate = NodeOrder; |
| 790 | continue; |
| 791 | } |
| 792 | |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 793 | // Choose node order for negative cost candidates. There is no good |
| 794 | // candidate in this case. |
| 795 | if (CurrentCost < 0 && Candidate.SCost < 0) { |
| 796 | if ((Q.getID() == TopQID && (*I)->NodeNum < Candidate.SU->NodeNum) |
| 797 | || (Q.getID() == BotQID && (*I)->NodeNum > Candidate.SU->NodeNum)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 798 | LLVM_DEBUG(traceCandidate("NCAND", Q, *I, CurrentCost)); |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 799 | Candidate.SU = *I; |
| 800 | Candidate.RPDelta = RPDelta; |
| 801 | Candidate.SCost = CurrentCost; |
| 802 | FoundCandidate = NodeOrder; |
| 803 | } |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 804 | continue; |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 805 | } |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 806 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 807 | // Best cost. |
| 808 | if (CurrentCost > Candidate.SCost) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 809 | LLVM_DEBUG(traceCandidate("CCAND", Q, *I, CurrentCost)); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 810 | Candidate.SU = *I; |
| 811 | Candidate.RPDelta = RPDelta; |
| 812 | Candidate.SCost = CurrentCost; |
| 813 | FoundCandidate = BestCost; |
| 814 | continue; |
| 815 | } |
| 816 | |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 817 | // Choose an instruction that does not depend on an artificial edge. |
| 818 | unsigned CurrWeak = getWeakLeft(*I, (Q.getID() == TopQID)); |
| 819 | unsigned CandWeak = getWeakLeft(Candidate.SU, (Q.getID() == TopQID)); |
| 820 | if (CurrWeak != CandWeak) { |
| 821 | if (CurrWeak < CandWeak) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 822 | LLVM_DEBUG(traceCandidate("WCAND", Q, *I, CurrentCost)); |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 823 | Candidate.SU = *I; |
| 824 | Candidate.RPDelta = RPDelta; |
| 825 | Candidate.SCost = CurrentCost; |
| 826 | FoundCandidate = Weak; |
Krzysztof Parzyszek | 393b379 | 2016-07-18 15:17:10 +0000 | [diff] [blame] | 827 | } |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 828 | continue; |
Krzysztof Parzyszek | 393b379 | 2016-07-18 15:17:10 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 831 | if (CurrentCost == Candidate.SCost && Zone.isLatencyBound(*I)) { |
| 832 | unsigned CurrSize, CandSize; |
| 833 | if (Q.getID() == TopQID) { |
| 834 | CurrSize = (*I)->Succs.size(); |
| 835 | CandSize = Candidate.SU->Succs.size(); |
| 836 | } else { |
| 837 | CurrSize = (*I)->Preds.size(); |
| 838 | CandSize = Candidate.SU->Preds.size(); |
| 839 | } |
| 840 | if (CurrSize > CandSize) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 841 | LLVM_DEBUG(traceCandidate("SPCAND", Q, *I, CurrentCost)); |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 842 | Candidate.SU = *I; |
| 843 | Candidate.RPDelta = RPDelta; |
| 844 | Candidate.SCost = CurrentCost; |
| 845 | FoundCandidate = BestCost; |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 846 | } |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 847 | // Keep the old candidate if it's a better candidate. That is, don't use |
| 848 | // the subsequent tie breaker. |
| 849 | if (CurrSize != CandSize) |
| 850 | continue; |
Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 853 | // Tie breaker. |
| 854 | // To avoid scheduling indeterminism, we need a tie breaker |
| 855 | // for the case when cost is identical for two nodes. |
| 856 | if (UseNewerCandidate && CurrentCost == Candidate.SCost) { |
| 857 | if ((Q.getID() == TopQID && (*I)->NodeNum < Candidate.SU->NodeNum) |
| 858 | || (Q.getID() == BotQID && (*I)->NodeNum > Candidate.SU->NodeNum)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 859 | LLVM_DEBUG(traceCandidate("TCAND", Q, *I, CurrentCost)); |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 860 | Candidate.SU = *I; |
| 861 | Candidate.RPDelta = RPDelta; |
| 862 | Candidate.SCost = CurrentCost; |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 863 | FoundCandidate = NodeOrder; |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 864 | continue; |
| 865 | } |
| 866 | } |
| 867 | |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 868 | // Fall through to original instruction order. |
| 869 | // Only consider node order if Candidate was chosen from this Q. |
| 870 | if (FoundCandidate == NoCand) |
| 871 | continue; |
| 872 | } |
| 873 | return FoundCandidate; |
| 874 | } |
| 875 | |
| 876 | /// Pick the best candidate node from either the top or bottom queue. |
| 877 | SUnit *ConvergingVLIWScheduler::pickNodeBidrectional(bool &IsTopNode) { |
| 878 | // Schedule as far as possible in the direction of no choice. This is most |
| 879 | // efficient, but also provides the best heuristics for CriticalPSets. |
| 880 | if (SUnit *SU = Bot.pickOnlyChoice()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 881 | LLVM_DEBUG(dbgs() << "Picked only Bottom\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 882 | IsTopNode = false; |
| 883 | return SU; |
| 884 | } |
| 885 | if (SUnit *SU = Top.pickOnlyChoice()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 886 | LLVM_DEBUG(dbgs() << "Picked only Top\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 887 | IsTopNode = true; |
| 888 | return SU; |
| 889 | } |
| 890 | SchedCandidate BotCand; |
| 891 | // Prefer bottom scheduling when heuristics are silent. |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 892 | CandResult BotResult = pickNodeFromQueue(Bot, |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 893 | DAG->getBotRPTracker(), BotCand); |
| 894 | assert(BotResult != NoCand && "failed to find the first candidate"); |
| 895 | |
| 896 | // If either Q has a single candidate that provides the least increase in |
| 897 | // Excess pressure, we can immediately schedule from that Q. |
| 898 | // |
| 899 | // RegionCriticalPSets summarizes the pressure within the scheduled region and |
| 900 | // affects picking from either Q. If scheduling in one direction must |
| 901 | // increase pressure for one of the excess PSets, then schedule in that |
| 902 | // direction first to provide more freedom in the other direction. |
| 903 | if (BotResult == SingleExcess || BotResult == SingleCritical) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 904 | LLVM_DEBUG(dbgs() << "Prefered Bottom Node\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 905 | IsTopNode = false; |
| 906 | return BotCand.SU; |
| 907 | } |
| 908 | // Check if the top Q has a better candidate. |
| 909 | SchedCandidate TopCand; |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 910 | CandResult TopResult = pickNodeFromQueue(Top, |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 911 | DAG->getTopRPTracker(), TopCand); |
| 912 | assert(TopResult != NoCand && "failed to find the first candidate"); |
| 913 | |
| 914 | if (TopResult == SingleExcess || TopResult == SingleCritical) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 915 | LLVM_DEBUG(dbgs() << "Prefered Top Node\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 916 | IsTopNode = true; |
| 917 | return TopCand.SU; |
| 918 | } |
| 919 | // If either Q has a single candidate that minimizes pressure above the |
| 920 | // original region's pressure pick it. |
| 921 | if (BotResult == SingleMax) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 922 | LLVM_DEBUG(dbgs() << "Prefered Bottom Node SingleMax\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 923 | IsTopNode = false; |
| 924 | return BotCand.SU; |
| 925 | } |
| 926 | if (TopResult == SingleMax) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 927 | LLVM_DEBUG(dbgs() << "Prefered Top Node SingleMax\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 928 | IsTopNode = true; |
| 929 | return TopCand.SU; |
| 930 | } |
| 931 | if (TopCand.SCost > BotCand.SCost) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 932 | LLVM_DEBUG(dbgs() << "Prefered Top Node Cost\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 933 | IsTopNode = true; |
| 934 | return TopCand.SU; |
| 935 | } |
| 936 | // Otherwise prefer the bottom candidate in node order. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 937 | LLVM_DEBUG(dbgs() << "Prefered Bottom in Node order\n"); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 938 | IsTopNode = false; |
| 939 | return BotCand.SU; |
| 940 | } |
| 941 | |
| 942 | /// Pick the best node to balance the schedule. Implements MachineSchedStrategy. |
| 943 | SUnit *ConvergingVLIWScheduler::pickNode(bool &IsTopNode) { |
| 944 | if (DAG->top() == DAG->bottom()) { |
| 945 | assert(Top.Available.empty() && Top.Pending.empty() && |
| 946 | Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 947 | return nullptr; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 948 | } |
| 949 | SUnit *SU; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 950 | if (ForceTopDown) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 951 | SU = Top.pickOnlyChoice(); |
| 952 | if (!SU) { |
| 953 | SchedCandidate TopCand; |
| 954 | CandResult TopResult = |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 955 | pickNodeFromQueue(Top, DAG->getTopRPTracker(), TopCand); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 956 | assert(TopResult != NoCand && "failed to find the first candidate"); |
| 957 | (void)TopResult; |
| 958 | SU = TopCand.SU; |
| 959 | } |
| 960 | IsTopNode = true; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 961 | } else if (ForceBottomUp) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 962 | SU = Bot.pickOnlyChoice(); |
| 963 | if (!SU) { |
| 964 | SchedCandidate BotCand; |
| 965 | CandResult BotResult = |
Krzysztof Parzyszek | 65059ee | 2018-03-20 19:26:27 +0000 | [diff] [blame] | 966 | pickNodeFromQueue(Bot, DAG->getBotRPTracker(), BotCand); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 967 | assert(BotResult != NoCand && "failed to find the first candidate"); |
| 968 | (void)BotResult; |
| 969 | SU = BotCand.SU; |
| 970 | } |
| 971 | IsTopNode = false; |
| 972 | } else { |
| 973 | SU = pickNodeBidrectional(IsTopNode); |
| 974 | } |
| 975 | if (SU->isTopReady()) |
| 976 | Top.removeReady(SU); |
| 977 | if (SU->isBottomReady()) |
| 978 | Bot.removeReady(SU); |
| 979 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 980 | LLVM_DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom") |
| 981 | << " Scheduling instruction in cycle " |
| 982 | << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) << " (" |
| 983 | << reportPackets() << ")\n"; |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame^] | 984 | DAG->dumpNode(*SU)); |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 985 | return SU; |
| 986 | } |
| 987 | |
| 988 | /// Update the scheduler's state after scheduling a node. This is the same node |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 989 | /// that was just returned by pickNode(). However, VLIWMachineScheduler needs |
| 990 | /// to update it's state based on the current cycle before MachineSchedStrategy |
| 991 | /// does. |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 992 | void ConvergingVLIWScheduler::schedNode(SUnit *SU, bool IsTopNode) { |
| 993 | if (IsTopNode) { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 994 | Top.bumpNode(SU); |
Krzysztof Parzyszek | 4c6b65f | 2018-03-20 17:03:27 +0000 | [diff] [blame] | 995 | SU->TopReadyCycle = Top.CurrCycle; |
Sergei Larin | ef4cc11 | 2012-09-10 17:31:34 +0000 | [diff] [blame] | 996 | } else { |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 997 | Bot.bumpNode(SU); |
Krzysztof Parzyszek | 4c6b65f | 2018-03-20 17:03:27 +0000 | [diff] [blame] | 998 | SU->BotReadyCycle = Bot.CurrCycle; |
Sergei Larin | 4d8986a | 2012-09-04 14:49:56 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |