Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 1 | //===- ResourcePriorityQueue.cpp - A DFA-oriented priority queue -*- C++ -*-==// |
| 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 | // This file implements the ResourcePriorityQueue class, which is a |
| 11 | // SchedulingPriorityQueue that prioritizes instructions using DFA state to |
| 12 | // reduce the length of the critical path through the basic block |
| 13 | // on VLIW platforms. |
| 14 | // The scheduler is basically a top-down adaptable list scheduler with DFA |
| 15 | // resource tracking added to the cost function. |
| 16 | // DFA is queried as a state machine to model "packets/bundles" during |
| 17 | // schedule. Currently packets/bundles are discarded at the end of |
| 18 | // scheduling, affecting only order of instructions. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/ResourcePriorityQueue.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetLowering.h" |
| 26 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "scheduler" |
| 35 | |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 36 | static cl::opt<bool> DisableDFASched("disable-dfa-sched", cl::Hidden, |
| 37 | cl::ZeroOrMore, cl::init(false), |
| 38 | cl::desc("Disable use of DFA during scheduling")); |
| 39 | |
David Majnemer | e61e4bf | 2016-06-21 05:10:24 +0000 | [diff] [blame] | 40 | static cl::opt<int> RegPressureThreshold( |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 41 | "dfa-sched-reg-pressure-threshold", cl::Hidden, cl::ZeroOrMore, cl::init(5), |
| 42 | cl::desc("Track reg pressure and switch priority to in-depth")); |
| 43 | |
Eric Christopher | 6d0e40b | 2014-07-23 22:27:10 +0000 | [diff] [blame] | 44 | ResourcePriorityQueue::ResourcePriorityQueue(SelectionDAGISel *IS) |
Eric Christopher | caf2751 | 2014-10-09 01:59:31 +0000 | [diff] [blame] | 45 | : Picker(this), InstrItins(IS->MF->getSubtarget().getInstrItineraryData()) { |
| 46 | const TargetSubtargetInfo &STI = IS->MF->getSubtarget(); |
| 47 | TRI = STI.getRegisterInfo(); |
Eric Christopher | b17140d | 2014-10-08 07:32:17 +0000 | [diff] [blame] | 48 | TLI = IS->TLI; |
Eric Christopher | caf2751 | 2014-10-09 01:59:31 +0000 | [diff] [blame] | 49 | TII = STI.getInstrInfo(); |
David Blaikie | 0a756e6 | 2015-03-03 20:49:08 +0000 | [diff] [blame] | 50 | ResourcesModel.reset(TII->CreateTargetScheduleState(STI)); |
Eric Christopher | 6d0e40b | 2014-07-23 22:27:10 +0000 | [diff] [blame] | 51 | // This hard requirement could be relaxed, but for now |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 52 | // do not let it proceed. |
Eric Christopher | f19d12b | 2014-07-23 22:34:13 +0000 | [diff] [blame] | 53 | assert(ResourcesModel && "Unimplemented CreateTargetScheduleState."); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 54 | |
Eric Christopher | f19d12b | 2014-07-23 22:34:13 +0000 | [diff] [blame] | 55 | unsigned NumRC = TRI->getNumRegClasses(); |
| 56 | RegLimit.resize(NumRC); |
| 57 | RegPressure.resize(NumRC); |
| 58 | std::fill(RegLimit.begin(), RegLimit.end(), 0); |
| 59 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Krzysztof Parzyszek | ee9aa3f | 2017-01-25 19:29:04 +0000 | [diff] [blame] | 60 | for (const TargetRegisterClass *RC : TRI->regclasses()) |
| 61 | RegLimit[RC->getID()] = TRI->getRegPressureLimit(RC, *IS->MF); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 62 | |
Eric Christopher | f19d12b | 2014-07-23 22:34:13 +0000 | [diff] [blame] | 63 | ParallelLiveRanges = 0; |
| 64 | HorizontalVerticalBalance = 0; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | unsigned |
| 68 | ResourcePriorityQueue::numberRCValPredInSU(SUnit *SU, unsigned RCId) { |
| 69 | unsigned NumberDeps = 0; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 70 | for (SDep &Pred : SU->Preds) { |
| 71 | if (Pred.isCtrl()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 72 | continue; |
| 73 | |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 74 | SUnit *PredSU = Pred.getSUnit(); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 75 | const SDNode *ScegN = PredSU->getNode(); |
| 76 | |
| 77 | if (!ScegN) |
| 78 | continue; |
| 79 | |
| 80 | // If value is passed to CopyToReg, it is probably |
| 81 | // live outside BB. |
| 82 | switch (ScegN->getOpcode()) { |
| 83 | default: break; |
| 84 | case ISD::TokenFactor: break; |
| 85 | case ISD::CopyFromReg: NumberDeps++; break; |
| 86 | case ISD::CopyToReg: break; |
| 87 | case ISD::INLINEASM: break; |
| 88 | } |
| 89 | if (!ScegN->isMachineOpcode()) |
| 90 | continue; |
| 91 | |
| 92 | for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) { |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 93 | MVT VT = ScegN->getSimpleValueType(i); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 94 | if (TLI->isTypeLegal(VT) |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 95 | && (TLI->getRegClassFor(VT)->getID() == RCId)) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 96 | NumberDeps++; |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return NumberDeps; |
| 102 | } |
| 103 | |
| 104 | unsigned ResourcePriorityQueue::numberRCValSuccInSU(SUnit *SU, |
| 105 | unsigned RCId) { |
| 106 | unsigned NumberDeps = 0; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 107 | for (const SDep &Succ : SU->Succs) { |
| 108 | if (Succ.isCtrl()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 109 | continue; |
| 110 | |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 111 | SUnit *SuccSU = Succ.getSUnit(); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 112 | const SDNode *ScegN = SuccSU->getNode(); |
| 113 | if (!ScegN) |
| 114 | continue; |
| 115 | |
| 116 | // If value is passed to CopyToReg, it is probably |
| 117 | // live outside BB. |
| 118 | switch (ScegN->getOpcode()) { |
| 119 | default: break; |
| 120 | case ISD::TokenFactor: break; |
| 121 | case ISD::CopyFromReg: break; |
| 122 | case ISD::CopyToReg: NumberDeps++; break; |
| 123 | case ISD::INLINEASM: break; |
| 124 | } |
| 125 | if (!ScegN->isMachineOpcode()) |
| 126 | continue; |
| 127 | |
| 128 | for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) { |
| 129 | const SDValue &Op = ScegN->getOperand(i); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 130 | MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo()); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 131 | if (TLI->isTypeLegal(VT) |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 132 | && (TLI->getRegClassFor(VT)->getID() == RCId)) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 133 | NumberDeps++; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return NumberDeps; |
| 139 | } |
| 140 | |
| 141 | static unsigned numberCtrlDepsInSU(SUnit *SU) { |
| 142 | unsigned NumberDeps = 0; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 143 | for (const SDep &Succ : SU->Succs) |
| 144 | if (Succ.isCtrl()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 145 | NumberDeps++; |
| 146 | |
| 147 | return NumberDeps; |
| 148 | } |
| 149 | |
| 150 | static unsigned numberCtrlPredInSU(SUnit *SU) { |
| 151 | unsigned NumberDeps = 0; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 152 | for (SDep &Pred : SU->Preds) |
| 153 | if (Pred.isCtrl()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 154 | NumberDeps++; |
| 155 | |
| 156 | return NumberDeps; |
| 157 | } |
| 158 | |
| 159 | /// |
| 160 | /// Initialize nodes. |
| 161 | /// |
| 162 | void ResourcePriorityQueue::initNodes(std::vector<SUnit> &sunits) { |
| 163 | SUnits = &sunits; |
| 164 | NumNodesSolelyBlocking.resize(SUnits->size(), 0); |
| 165 | |
| 166 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { |
| 167 | SUnit *SU = &(*SUnits)[i]; |
| 168 | initNumRegDefsLeft(SU); |
| 169 | SU->NodeQueueId = 0; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// This heuristic is used if DFA scheduling is not desired |
| 174 | /// for some VLIW platform. |
| 175 | bool resource_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { |
| 176 | // The isScheduleHigh flag allows nodes with wraparound dependencies that |
| 177 | // cannot easily be modeled as edges with latencies to be scheduled as |
| 178 | // soon as possible in a top-down schedule. |
| 179 | if (LHS->isScheduleHigh && !RHS->isScheduleHigh) |
| 180 | return false; |
| 181 | |
| 182 | if (!LHS->isScheduleHigh && RHS->isScheduleHigh) |
| 183 | return true; |
| 184 | |
| 185 | unsigned LHSNum = LHS->NodeNum; |
| 186 | unsigned RHSNum = RHS->NodeNum; |
| 187 | |
| 188 | // The most important heuristic is scheduling the critical path. |
| 189 | unsigned LHSLatency = PQ->getLatency(LHSNum); |
| 190 | unsigned RHSLatency = PQ->getLatency(RHSNum); |
| 191 | if (LHSLatency < RHSLatency) return true; |
| 192 | if (LHSLatency > RHSLatency) return false; |
| 193 | |
| 194 | // After that, if two nodes have identical latencies, look to see if one will |
| 195 | // unblock more other nodes than the other. |
| 196 | unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum); |
| 197 | unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum); |
| 198 | if (LHSBlocked < RHSBlocked) return true; |
| 199 | if (LHSBlocked > RHSBlocked) return false; |
| 200 | |
| 201 | // Finally, just to provide a stable ordering, use the node number as a |
| 202 | // deciding factor. |
| 203 | return LHSNum < RHSNum; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor |
| 208 | /// of SU, return it, otherwise return null. |
| 209 | SUnit *ResourcePriorityQueue::getSingleUnscheduledPred(SUnit *SU) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 210 | SUnit *OnlyAvailablePred = nullptr; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 211 | for (const SDep &Pred : SU->Preds) { |
| 212 | SUnit &PredSU = *Pred.getSUnit(); |
| 213 | if (!PredSU.isScheduled) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 214 | // We found an available, but not scheduled, predecessor. If it's the |
| 215 | // only one we have found, keep track of it... otherwise give up. |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 216 | if (OnlyAvailablePred && OnlyAvailablePred != &PredSU) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 217 | return nullptr; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 218 | OnlyAvailablePred = &PredSU; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | return OnlyAvailablePred; |
| 222 | } |
| 223 | |
| 224 | void ResourcePriorityQueue::push(SUnit *SU) { |
| 225 | // Look at all of the successors of this node. Count the number of nodes that |
| 226 | // this node is the sole unscheduled node for. |
| 227 | unsigned NumNodesBlocking = 0; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 228 | for (const SDep &Succ : SU->Succs) |
| 229 | if (getSingleUnscheduledPred(Succ.getSUnit()) == SU) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 230 | ++NumNodesBlocking; |
| 231 | |
| 232 | NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; |
| 233 | Queue.push_back(SU); |
| 234 | } |
| 235 | |
| 236 | /// Check if scheduling of this SU is possible |
| 237 | /// in the current packet. |
| 238 | bool ResourcePriorityQueue::isResourceAvailable(SUnit *SU) { |
| 239 | if (!SU || !SU->getNode()) |
| 240 | return false; |
| 241 | |
| 242 | // If this is a compound instruction, |
| 243 | // it is likely to be a call. Do not delay it. |
| 244 | if (SU->getNode()->getGluedNode()) |
| 245 | return true; |
| 246 | |
| 247 | // First see if the pipeline could receive this instruction |
| 248 | // in the current cycle. |
| 249 | if (SU->getNode()->isMachineOpcode()) |
| 250 | switch (SU->getNode()->getMachineOpcode()) { |
| 251 | default: |
| 252 | if (!ResourcesModel->canReserveResources(&TII->get( |
| 253 | SU->getNode()->getMachineOpcode()))) |
| 254 | return false; |
Adrian Prantl | 0e6694d | 2017-12-19 22:05:25 +0000 | [diff] [blame] | 255 | break; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 256 | case TargetOpcode::EXTRACT_SUBREG: |
| 257 | case TargetOpcode::INSERT_SUBREG: |
| 258 | case TargetOpcode::SUBREG_TO_REG: |
| 259 | case TargetOpcode::REG_SEQUENCE: |
| 260 | case TargetOpcode::IMPLICIT_DEF: |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | // Now see if there are no other dependencies |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 265 | // to instructions already in the packet. |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 266 | for (unsigned i = 0, e = Packet.size(); i != e; ++i) |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 267 | for (const SDep &Succ : Packet[i]->Succs) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 268 | // Since we do not add pseudos to packets, might as well |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 269 | // ignore order deps. |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 270 | if (Succ.isCtrl()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 271 | continue; |
| 272 | |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 273 | if (Succ.getSUnit() == SU) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | /// Keep track of available resources. |
| 281 | void ResourcePriorityQueue::reserveResources(SUnit *SU) { |
| 282 | // If this SU does not fit in the packet |
| 283 | // start a new one. |
| 284 | if (!isResourceAvailable(SU) || SU->getNode()->getGluedNode()) { |
| 285 | ResourcesModel->clearResources(); |
| 286 | Packet.clear(); |
| 287 | } |
| 288 | |
| 289 | if (SU->getNode() && SU->getNode()->isMachineOpcode()) { |
| 290 | switch (SU->getNode()->getMachineOpcode()) { |
| 291 | default: |
| 292 | ResourcesModel->reserveResources(&TII->get( |
| 293 | SU->getNode()->getMachineOpcode())); |
| 294 | break; |
| 295 | case TargetOpcode::EXTRACT_SUBREG: |
| 296 | case TargetOpcode::INSERT_SUBREG: |
| 297 | case TargetOpcode::SUBREG_TO_REG: |
| 298 | case TargetOpcode::REG_SEQUENCE: |
| 299 | case TargetOpcode::IMPLICIT_DEF: |
| 300 | break; |
| 301 | } |
| 302 | Packet.push_back(SU); |
| 303 | } |
| 304 | // Forcefully end packet for PseudoOps. |
| 305 | else { |
| 306 | ResourcesModel->clearResources(); |
| 307 | Packet.clear(); |
| 308 | } |
| 309 | |
| 310 | // If packet is now full, reset the state so in the next cycle |
| 311 | // we start fresh. |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 312 | if (Packet.size() >= InstrItins->SchedModel.IssueWidth) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 313 | ResourcesModel->clearResources(); |
| 314 | Packet.clear(); |
| 315 | } |
| 316 | } |
| 317 | |
David Majnemer | e61e4bf | 2016-06-21 05:10:24 +0000 | [diff] [blame] | 318 | int ResourcePriorityQueue::rawRegPressureDelta(SUnit *SU, unsigned RCId) { |
| 319 | int RegBalance = 0; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 320 | |
| 321 | if (!SU || !SU->getNode() || !SU->getNode()->isMachineOpcode()) |
| 322 | return RegBalance; |
| 323 | |
| 324 | // Gen estimate. |
| 325 | for (unsigned i = 0, e = SU->getNode()->getNumValues(); i != e; ++i) { |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 326 | MVT VT = SU->getNode()->getSimpleValueType(i); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 327 | if (TLI->isTypeLegal(VT) |
| 328 | && TLI->getRegClassFor(VT) |
| 329 | && TLI->getRegClassFor(VT)->getID() == RCId) |
| 330 | RegBalance += numberRCValSuccInSU(SU, RCId); |
| 331 | } |
| 332 | // Kill estimate. |
| 333 | for (unsigned i = 0, e = SU->getNode()->getNumOperands(); i != e; ++i) { |
| 334 | const SDValue &Op = SU->getNode()->getOperand(i); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 335 | MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo()); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 336 | if (isa<ConstantSDNode>(Op.getNode())) |
| 337 | continue; |
| 338 | |
| 339 | if (TLI->isTypeLegal(VT) && TLI->getRegClassFor(VT) |
| 340 | && TLI->getRegClassFor(VT)->getID() == RCId) |
| 341 | RegBalance -= numberRCValPredInSU(SU, RCId); |
| 342 | } |
| 343 | return RegBalance; |
| 344 | } |
| 345 | |
| 346 | /// Estimates change in reg pressure from this SU. |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 347 | /// It is achieved by trivial tracking of defined |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 348 | /// and used vregs in dependent instructions. |
| 349 | /// The RawPressure flag makes this function to ignore |
| 350 | /// existing reg file sizes, and report raw def/use |
| 351 | /// balance. |
David Majnemer | e61e4bf | 2016-06-21 05:10:24 +0000 | [diff] [blame] | 352 | int ResourcePriorityQueue::regPressureDelta(SUnit *SU, bool RawPressure) { |
| 353 | int RegBalance = 0; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 354 | |
| 355 | if (!SU || !SU->getNode() || !SU->getNode()->isMachineOpcode()) |
| 356 | return RegBalance; |
| 357 | |
| 358 | if (RawPressure) { |
Krzysztof Parzyszek | ee9aa3f | 2017-01-25 19:29:04 +0000 | [diff] [blame] | 359 | for (const TargetRegisterClass *RC : TRI->regclasses()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 360 | RegBalance += rawRegPressureDelta(SU, RC->getID()); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 361 | } |
| 362 | else { |
Krzysztof Parzyszek | ee9aa3f | 2017-01-25 19:29:04 +0000 | [diff] [blame] | 363 | for (const TargetRegisterClass *RC : TRI->regclasses()) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 364 | if ((RegPressure[RC->getID()] + |
| 365 | rawRegPressureDelta(SU, RC->getID()) > 0) && |
| 366 | (RegPressure[RC->getID()] + |
| 367 | rawRegPressureDelta(SU, RC->getID()) >= RegLimit[RC->getID()])) |
| 368 | RegBalance += rawRegPressureDelta(SU, RC->getID()); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return RegBalance; |
| 373 | } |
| 374 | |
| 375 | // Constants used to denote relative importance of |
| 376 | // heuristic components for cost computation. |
| 377 | static const unsigned PriorityOne = 200; |
Eli Friedman | 8f06d55 | 2013-09-11 00:41:02 +0000 | [diff] [blame] | 378 | static const unsigned PriorityTwo = 50; |
| 379 | static const unsigned PriorityThree = 15; |
| 380 | static const unsigned PriorityFour = 5; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 381 | static const unsigned ScaleOne = 20; |
| 382 | static const unsigned ScaleTwo = 10; |
| 383 | static const unsigned ScaleThree = 5; |
| 384 | static const unsigned FactorOne = 2; |
| 385 | |
| 386 | /// Returns single number reflecting benefit of scheduling SU |
| 387 | /// in the current cycle. |
David Majnemer | e61e4bf | 2016-06-21 05:10:24 +0000 | [diff] [blame] | 388 | int ResourcePriorityQueue::SUSchedulingCost(SUnit *SU) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 389 | // Initial trivial priority. |
David Majnemer | e61e4bf | 2016-06-21 05:10:24 +0000 | [diff] [blame] | 390 | int ResCount = 1; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 391 | |
| 392 | // Do not waste time on a node that is already scheduled. |
| 393 | if (SU->isScheduled) |
| 394 | return ResCount; |
| 395 | |
| 396 | // Forced priority is high. |
| 397 | if (SU->isScheduleHigh) |
| 398 | ResCount += PriorityOne; |
| 399 | |
| 400 | // Adaptable scheduling |
| 401 | // A small, but very parallel |
| 402 | // region, where reg pressure is an issue. |
| 403 | if (HorizontalVerticalBalance > RegPressureThreshold) { |
| 404 | // Critical path first |
| 405 | ResCount += (SU->getHeight() * ScaleTwo); |
| 406 | // If resources are available for it, multiply the |
| 407 | // chance of scheduling. |
| 408 | if (isResourceAvailable(SU)) |
| 409 | ResCount <<= FactorOne; |
| 410 | |
| 411 | // Consider change to reg pressure from scheduling |
| 412 | // this SU. |
| 413 | ResCount -= (regPressureDelta(SU,true) * ScaleOne); |
| 414 | } |
| 415 | // Default heuristic, greeady and |
| 416 | // critical path driven. |
| 417 | else { |
| 418 | // Critical path first. |
| 419 | ResCount += (SU->getHeight() * ScaleTwo); |
| 420 | // Now see how many instructions is blocked by this SU. |
| 421 | ResCount += (NumNodesSolelyBlocking[SU->NodeNum] * ScaleTwo); |
| 422 | // If resources are available for it, multiply the |
| 423 | // chance of scheduling. |
| 424 | if (isResourceAvailable(SU)) |
| 425 | ResCount <<= FactorOne; |
| 426 | |
| 427 | ResCount -= (regPressureDelta(SU) * ScaleTwo); |
| 428 | } |
| 429 | |
Alp Toker | cf21875 | 2014-06-30 18:57:16 +0000 | [diff] [blame] | 430 | // These are platform-specific things. |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 431 | // Will need to go into the back end |
| 432 | // and accessed from here via a hook. |
| 433 | for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) { |
| 434 | if (N->isMachineOpcode()) { |
| 435 | const MCInstrDesc &TID = TII->get(N->getMachineOpcode()); |
| 436 | if (TID.isCall()) |
Eli Friedman | 8f06d55 | 2013-09-11 00:41:02 +0000 | [diff] [blame] | 437 | ResCount += (PriorityTwo + (ScaleThree*N->getNumValues())); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 438 | } |
| 439 | else |
| 440 | switch (N->getOpcode()) { |
| 441 | default: break; |
| 442 | case ISD::TokenFactor: |
| 443 | case ISD::CopyFromReg: |
| 444 | case ISD::CopyToReg: |
Eli Friedman | 8f06d55 | 2013-09-11 00:41:02 +0000 | [diff] [blame] | 445 | ResCount += PriorityFour; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 446 | break; |
| 447 | |
| 448 | case ISD::INLINEASM: |
Eli Friedman | 8f06d55 | 2013-09-11 00:41:02 +0000 | [diff] [blame] | 449 | ResCount += PriorityThree; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | } |
| 453 | return ResCount; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /// Main resource tracking point. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 458 | void ResourcePriorityQueue::scheduledNode(SUnit *SU) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 459 | // Use NULL entry as an event marker to reset |
| 460 | // the DFA state. |
| 461 | if (!SU) { |
| 462 | ResourcesModel->clearResources(); |
| 463 | Packet.clear(); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | const SDNode *ScegN = SU->getNode(); |
| 468 | // Update reg pressure tracking. |
| 469 | // First update current node. |
| 470 | if (ScegN->isMachineOpcode()) { |
| 471 | // Estimate generated regs. |
| 472 | for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) { |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 473 | MVT VT = ScegN->getSimpleValueType(i); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 474 | |
| 475 | if (TLI->isTypeLegal(VT)) { |
| 476 | const TargetRegisterClass *RC = TLI->getRegClassFor(VT); |
| 477 | if (RC) |
| 478 | RegPressure[RC->getID()] += numberRCValSuccInSU(SU, RC->getID()); |
| 479 | } |
| 480 | } |
| 481 | // Estimate killed regs. |
| 482 | for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) { |
| 483 | const SDValue &Op = ScegN->getOperand(i); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 484 | MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo()); |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 485 | |
| 486 | if (TLI->isTypeLegal(VT)) { |
| 487 | const TargetRegisterClass *RC = TLI->getRegClassFor(VT); |
| 488 | if (RC) { |
| 489 | if (RegPressure[RC->getID()] > |
| 490 | (numberRCValPredInSU(SU, RC->getID()))) |
| 491 | RegPressure[RC->getID()] -= numberRCValPredInSU(SU, RC->getID()); |
| 492 | else RegPressure[RC->getID()] = 0; |
| 493 | } |
| 494 | } |
| 495 | } |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 496 | for (SDep &Pred : SU->Preds) { |
| 497 | if (Pred.isCtrl() || (Pred.getSUnit()->NumRegDefsLeft == 0)) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 498 | continue; |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 499 | --Pred.getSUnit()->NumRegDefsLeft; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | // Reserve resources for this SU. |
| 504 | reserveResources(SU); |
| 505 | |
| 506 | // Adjust number of parallel live ranges. |
| 507 | // Heuristic is simple - node with no data successors reduces |
| 508 | // number of live ranges. All others, increase it. |
| 509 | unsigned NumberNonControlDeps = 0; |
| 510 | |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 511 | for (const SDep &Succ : SU->Succs) { |
| 512 | adjustPriorityOfUnscheduledPreds(Succ.getSUnit()); |
| 513 | if (!Succ.isCtrl()) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 514 | NumberNonControlDeps++; |
| 515 | } |
| 516 | |
| 517 | if (!NumberNonControlDeps) { |
| 518 | if (ParallelLiveRanges >= SU->NumPreds) |
| 519 | ParallelLiveRanges -= SU->NumPreds; |
| 520 | else |
| 521 | ParallelLiveRanges = 0; |
| 522 | |
| 523 | } |
| 524 | else |
| 525 | ParallelLiveRanges += SU->NumRegDefsLeft; |
| 526 | |
| 527 | // Track parallel live chains. |
| 528 | HorizontalVerticalBalance += (SU->Succs.size() - numberCtrlDepsInSU(SU)); |
| 529 | HorizontalVerticalBalance -= (SU->Preds.size() - numberCtrlPredInSU(SU)); |
| 530 | } |
| 531 | |
| 532 | void ResourcePriorityQueue::initNumRegDefsLeft(SUnit *SU) { |
| 533 | unsigned NodeNumDefs = 0; |
| 534 | for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) |
| 535 | if (N->isMachineOpcode()) { |
| 536 | const MCInstrDesc &TID = TII->get(N->getMachineOpcode()); |
| 537 | // No register need be allocated for this. |
| 538 | if (N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) { |
| 539 | NodeNumDefs = 0; |
| 540 | break; |
| 541 | } |
| 542 | NodeNumDefs = std::min(N->getNumValues(), TID.getNumDefs()); |
| 543 | } |
| 544 | else |
| 545 | switch(N->getOpcode()) { |
| 546 | default: break; |
| 547 | case ISD::CopyFromReg: |
| 548 | NodeNumDefs++; |
| 549 | break; |
| 550 | case ISD::INLINEASM: |
| 551 | NodeNumDefs++; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | SU->NumRegDefsLeft = NodeNumDefs; |
| 556 | } |
| 557 | |
| 558 | /// adjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just |
| 559 | /// scheduled. If SU is not itself available, then there is at least one |
| 560 | /// predecessor node that has not been scheduled yet. If SU has exactly ONE |
| 561 | /// unscheduled predecessor, we want to increase its priority: it getting |
| 562 | /// scheduled will make this node available, so it is better than some other |
| 563 | /// node of the same priority that will not make a node available. |
| 564 | void ResourcePriorityQueue::adjustPriorityOfUnscheduledPreds(SUnit *SU) { |
| 565 | if (SU->isAvailable) return; // All preds scheduled. |
| 566 | |
| 567 | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 568 | if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 569 | return; |
| 570 | |
| 571 | // Okay, we found a single predecessor that is available, but not scheduled. |
| 572 | // Since it is available, it must be in the priority queue. First remove it. |
| 573 | remove(OnlyAvailablePred); |
| 574 | |
| 575 | // Reinsert the node into the priority queue, which recomputes its |
| 576 | // NumNodesSolelyBlocking value. |
| 577 | push(OnlyAvailablePred); |
| 578 | } |
| 579 | |
| 580 | |
| 581 | /// Main access point - returns next instructions |
| 582 | /// to be placed in scheduling sequence. |
| 583 | SUnit *ResourcePriorityQueue::pop() { |
| 584 | if (empty()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 585 | return nullptr; |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 586 | |
| 587 | std::vector<SUnit *>::iterator Best = Queue.begin(); |
| 588 | if (!DisableDFASched) { |
David Majnemer | e61e4bf | 2016-06-21 05:10:24 +0000 | [diff] [blame] | 589 | int BestCost = SUSchedulingCost(*Best); |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 590 | for (auto I = std::next(Queue.begin()), E = Queue.end(); I != E; ++I) { |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 591 | |
| 592 | if (SUSchedulingCost(*I) > BestCost) { |
| 593 | BestCost = SUSchedulingCost(*I); |
| 594 | Best = I; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | // Use default TD scheduling mechanism. |
| 599 | else { |
Krzysztof Parzyszek | 41b6e14 | 2017-05-04 13:35:17 +0000 | [diff] [blame] | 600 | for (auto I = std::next(Queue.begin()), E = Queue.end(); I != E; ++I) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 601 | if (Picker(*Best, *I)) |
| 602 | Best = I; |
| 603 | } |
| 604 | |
| 605 | SUnit *V = *Best; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 606 | if (Best != std::prev(Queue.end())) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 607 | std::swap(*Best, Queue.back()); |
| 608 | |
| 609 | Queue.pop_back(); |
| 610 | |
| 611 | return V; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | void ResourcePriorityQueue::remove(SUnit *SU) { |
| 616 | assert(!Queue.empty() && "Queue is empty!"); |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 617 | std::vector<SUnit *>::iterator I = find(Queue, SU); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 618 | if (I != std::prev(Queue.end())) |
Andrew Trick | d06df96 | 2012-02-01 22:13:57 +0000 | [diff] [blame] | 619 | std::swap(*I, Queue.back()); |
| 620 | |
| 621 | Queue.pop_back(); |
| 622 | } |