Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1 | //===- llvm/Target/TargetSchedule.cpp - Sched Machine Model ---------------===// |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a wrapper around MCSchedModel that allows the interface |
| 11 | // to benefit from information currently only available in TargetInstrInfo. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
| 16 | #include "llvm/CodeGen/MachineInstr.h" |
| 17 | #include "llvm/CodeGen/MachineOperand.h" |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetSchedule.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInstrDesc.h" |
| 20 | #include "llvm/MC/MCInstrItineraries.h" |
| 21 | #include "llvm/MC/MCSchedule.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegisterInfo.h" |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetSubtargetInfo.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <cassert> |
| 30 | #include <cstdint> |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 34 | static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true), |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 35 | cl::desc("Use TargetSchedModel for latency lookup")); |
| 36 | |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 37 | static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true), |
| 38 | cl::desc("Use InstrItineraryData for latency lookup")); |
| 39 | |
Andrew Trick | cfcf520 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 40 | bool TargetSchedModel::hasInstrSchedModel() const { |
| 41 | return EnableSchedModel && SchedModel.hasInstrSchedModel(); |
| 42 | } |
| 43 | |
| 44 | bool TargetSchedModel::hasInstrItineraries() const { |
| 45 | return EnableSchedItins && !InstrItins.isEmpty(); |
| 46 | } |
| 47 | |
Andrew Trick | e96390e | 2012-11-06 07:10:38 +0000 | [diff] [blame] | 48 | static unsigned gcd(unsigned Dividend, unsigned Divisor) { |
| 49 | // Dividend and Divisor will be naturally swapped as needed. |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 50 | while (Divisor) { |
Andrew Trick | e96390e | 2012-11-06 07:10:38 +0000 | [diff] [blame] | 51 | unsigned Rem = Dividend % Divisor; |
| 52 | Dividend = Divisor; |
| 53 | Divisor = Rem; |
| 54 | }; |
| 55 | return Dividend; |
| 56 | } |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 57 | |
Andrew Trick | e96390e | 2012-11-06 07:10:38 +0000 | [diff] [blame] | 58 | static unsigned lcm(unsigned A, unsigned B) { |
| 59 | unsigned LCM = (uint64_t(A) * B) / gcd(A, B); |
| 60 | assert((LCM >= A && LCM >= B) && "LCM overflow"); |
| 61 | return LCM; |
| 62 | } |
| 63 | |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 64 | void TargetSchedModel::init(const MCSchedModel &sm, |
| 65 | const TargetSubtargetInfo *sti, |
| 66 | const TargetInstrInfo *tii) { |
| 67 | SchedModel = sm; |
| 68 | STI = sti; |
| 69 | TII = tii; |
| 70 | STI->initInstrItins(InstrItins); |
Andrew Trick | e96390e | 2012-11-06 07:10:38 +0000 | [diff] [blame] | 71 | |
| 72 | unsigned NumRes = SchedModel.getNumProcResourceKinds(); |
| 73 | ResourceFactors.resize(NumRes); |
| 74 | ResourceLCM = SchedModel.IssueWidth; |
| 75 | for (unsigned Idx = 0; Idx < NumRes; ++Idx) { |
| 76 | unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits; |
| 77 | if (NumUnits > 0) |
| 78 | ResourceLCM = lcm(ResourceLCM, NumUnits); |
| 79 | } |
| 80 | MicroOpFactor = ResourceLCM / SchedModel.IssueWidth; |
| 81 | for (unsigned Idx = 0; Idx < NumRes; ++Idx) { |
| 82 | unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits; |
| 83 | ResourceFactors[Idx] = NumUnits ? (ResourceLCM / NumUnits) : 0; |
| 84 | } |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 85 | } |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 86 | |
Javed Absar | 3d59437 | 2017-03-27 20:46:37 +0000 | [diff] [blame] | 87 | /// Returns true only if instruction is specified as single issue. |
| 88 | bool TargetSchedModel::mustBeginGroup(const MachineInstr *MI, |
| 89 | const MCSchedClassDesc *SC) const { |
| 90 | if (hasInstrSchedModel()) { |
| 91 | if (!SC) |
| 92 | SC = resolveSchedClass(MI); |
| 93 | if (SC->isValid()) |
| 94 | return SC->BeginGroup; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | bool TargetSchedModel::mustEndGroup(const MachineInstr *MI, |
| 100 | const MCSchedClassDesc *SC) const { |
| 101 | if (hasInstrSchedModel()) { |
| 102 | if (!SC) |
| 103 | SC = resolveSchedClass(MI); |
| 104 | if (SC->isValid()) |
| 105 | return SC->EndGroup; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
Andrew Trick | e96390e | 2012-11-06 07:10:38 +0000 | [diff] [blame] | 110 | unsigned TargetSchedModel::getNumMicroOps(const MachineInstr *MI, |
| 111 | const MCSchedClassDesc *SC) const { |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 112 | if (hasInstrItineraries()) { |
| 113 | int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass()); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 114 | return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, *MI); |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 115 | } |
Andrew Trick | 5f35afb | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 116 | if (hasInstrSchedModel()) { |
Andrew Trick | e96390e | 2012-11-06 07:10:38 +0000 | [diff] [blame] | 117 | if (!SC) |
| 118 | SC = resolveSchedClass(MI); |
| 119 | if (SC->isValid()) |
| 120 | return SC->NumMicroOps; |
Andrew Trick | 5f35afb | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 121 | } |
| 122 | return MI->isTransient() ? 0 : 1; |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Andrew Trick | 0b1d8d0 | 2012-10-17 17:27:10 +0000 | [diff] [blame] | 125 | // The machine model may explicitly specify an invalid latency, which |
| 126 | // effectively means infinite latency. Since users of the TargetSchedule API |
| 127 | // don't know how to handle this, we convert it to a very large latency that is |
| 128 | // easy to distinguish when debugging the DAG but won't induce overflow. |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 129 | static unsigned capLatency(int Cycles) { |
Andrew Trick | 0b1d8d0 | 2012-10-17 17:27:10 +0000 | [diff] [blame] | 130 | return Cycles >= 0 ? Cycles : 1000; |
| 131 | } |
| 132 | |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 133 | /// Return the MCSchedClassDesc for this instruction. Some SchedClasses require |
| 134 | /// evaluation of predicates that depend on instruction operands or flags. |
| 135 | const MCSchedClassDesc *TargetSchedModel:: |
| 136 | resolveSchedClass(const MachineInstr *MI) const { |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 137 | // Get the definition's scheduling class descriptor from this machine model. |
| 138 | unsigned SchedClass = MI->getDesc().getSchedClass(); |
| 139 | const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
Andrew Trick | be2bccb | 2013-04-13 06:07:45 +0000 | [diff] [blame] | 140 | if (!SCDesc->isValid()) |
| 141 | return SCDesc; |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 142 | |
| 143 | #ifndef NDEBUG |
| 144 | unsigned NIter = 0; |
| 145 | #endif |
| 146 | while (SCDesc->isVariant()) { |
| 147 | assert(++NIter < 6 && "Variants are nested deeper than the magic number"); |
| 148 | |
| 149 | SchedClass = STI->resolveSchedClass(SchedClass, MI, this); |
| 150 | SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
| 151 | } |
| 152 | return SCDesc; |
| 153 | } |
| 154 | |
| 155 | /// Find the def index of this operand. This index maps to the machine model and |
| 156 | /// is independent of use operands. Def operands may be reordered with uses or |
| 157 | /// merged with uses without affecting the def index (e.g. before/after |
| 158 | /// regalloc). However, an instruction's def operands must never be reordered |
| 159 | /// with respect to each other. |
| 160 | static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) { |
| 161 | unsigned DefIdx = 0; |
| 162 | for (unsigned i = 0; i != DefOperIdx; ++i) { |
| 163 | const MachineOperand &MO = MI->getOperand(i); |
| 164 | if (MO.isReg() && MO.isDef()) |
| 165 | ++DefIdx; |
| 166 | } |
| 167 | return DefIdx; |
| 168 | } |
| 169 | |
| 170 | /// Find the use index of this operand. This is independent of the instruction's |
| 171 | /// def operands. |
Andrew Trick | f2b70d9 | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 172 | /// |
| 173 | /// Note that uses are not determined by the operand's isUse property, which |
| 174 | /// is simply the inverse of isDef. Here we consider any readsReg operand to be |
| 175 | /// a "use". The machine model allows an operand to be both a Def and Use. |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 176 | static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) { |
| 177 | unsigned UseIdx = 0; |
| 178 | for (unsigned i = 0; i != UseOperIdx; ++i) { |
| 179 | const MachineOperand &MO = MI->getOperand(i); |
Matthias Braun | 3a13315 | 2016-08-24 02:32:29 +0000 | [diff] [blame] | 180 | if (MO.isReg() && MO.readsReg() && !MO.isDef()) |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 181 | ++UseIdx; |
| 182 | } |
| 183 | return UseIdx; |
| 184 | } |
| 185 | |
| 186 | // Top-level API for clients that know the operand indices. |
| 187 | unsigned TargetSchedModel::computeOperandLatency( |
| 188 | const MachineInstr *DefMI, unsigned DefOperIdx, |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 189 | const MachineInstr *UseMI, unsigned UseOperIdx) const { |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 190 | |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 191 | if (!hasInstrSchedModel() && !hasInstrItineraries()) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 192 | return TII->defaultDefLatency(SchedModel, *DefMI); |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 193 | |
Andrew Trick | cfcf520 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 194 | if (hasInstrItineraries()) { |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 195 | int OperLatency = 0; |
| 196 | if (UseMI) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 197 | OperLatency = TII->getOperandLatency(&InstrItins, *DefMI, DefOperIdx, |
| 198 | *UseMI, UseOperIdx); |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 199 | } |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 200 | else { |
| 201 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 202 | OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx); |
| 203 | } |
| 204 | if (OperLatency >= 0) |
| 205 | return OperLatency; |
| 206 | |
| 207 | // No operand latency was found. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 208 | unsigned InstrLatency = TII->getInstrLatency(&InstrItins, *DefMI); |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 209 | |
| 210 | // Expected latency is the max of the stage latency and itinerary props. |
Andrew Trick | 780fae8 | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 211 | // Rather than directly querying InstrItins stage latency, we call a TII |
| 212 | // hook to allow subtargets to specialize latency. This hook is only |
| 213 | // applicable to the InstrItins model. InstrSchedModel should model all |
| 214 | // special cases without TII hooks. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 215 | InstrLatency = |
| 216 | std::max(InstrLatency, TII->defaultDefLatency(SchedModel, *DefMI)); |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 217 | return InstrLatency; |
| 218 | } |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 219 | // hasInstrSchedModel() |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 220 | const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); |
| 221 | unsigned DefIdx = findDefIdx(DefMI, DefOperIdx); |
| 222 | if (DefIdx < SCDesc->NumWriteLatencyEntries) { |
| 223 | // Lookup the definition's write latency in SubtargetInfo. |
| 224 | const MCWriteLatencyEntry *WLEntry = |
| 225 | STI->getWriteLatencyEntry(SCDesc, DefIdx); |
| 226 | unsigned WriteID = WLEntry->WriteResourceID; |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 227 | unsigned Latency = capLatency(WLEntry->Cycles); |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 228 | if (!UseMI) |
| 229 | return Latency; |
| 230 | |
| 231 | // Lookup the use's latency adjustment in SubtargetInfo. |
| 232 | const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI); |
| 233 | if (UseDesc->NumReadAdvanceEntries == 0) |
| 234 | return Latency; |
| 235 | unsigned UseIdx = findUseIdx(UseMI, UseOperIdx); |
Andrew Trick | 5d486186 | 2013-06-17 21:45:18 +0000 | [diff] [blame] | 236 | int Advance = STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID); |
| 237 | if (Advance > 0 && (unsigned)Advance > Latency) // unsigned wrap |
| 238 | return 0; |
| 239 | return Latency - Advance; |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 240 | } |
| 241 | // If DefIdx does not exist in the model (e.g. implicit defs), then return |
| 242 | // unit latency (defaultDefLatency may be too conservative). |
Andrew Trick | f2b70d9 | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 243 | #ifndef NDEBUG |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 244 | if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit() |
Andrew Trick | b6854d8 | 2013-09-25 18:14:12 +0000 | [diff] [blame] | 245 | && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef() |
| 246 | && SchedModel.isComplete()) { |
Matthias Braun | 244a677 | 2015-07-17 17:50:11 +0000 | [diff] [blame] | 247 | errs() << "DefIdx " << DefIdx << " exceeds machine model writes for " |
MinSeong Kim | 4a9a4e1 | 2016-01-05 14:50:15 +0000 | [diff] [blame] | 248 | << *DefMI << " (Try with MCSchedModel.CompleteModel set to false)"; |
Matthias Braun | 244a677 | 2015-07-17 17:50:11 +0000 | [diff] [blame] | 249 | llvm_unreachable("incomplete machine model"); |
Andrew Trick | 8abcf4d | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 250 | } |
Andrew Trick | f2b70d9 | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 251 | #endif |
Andrew Trick | 6057017 | 2013-03-16 18:58:57 +0000 | [diff] [blame] | 252 | // FIXME: Automatically giving all implicit defs defaultDefLatency is |
| 253 | // undesirable. We should only do it for defs that are known to the MC |
| 254 | // desc like flags. Truly implicit defs should get 1 cycle latency. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 255 | return DefMI->isTransient() ? 0 : TII->defaultDefLatency(SchedModel, *DefMI); |
Andrew Trick | 6e6d597 | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 256 | } |
Andrew Trick | 780fae8 | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 257 | |
Matthias Braun | 42e1e66 | 2015-05-14 18:01:13 +0000 | [diff] [blame] | 258 | unsigned |
| 259 | TargetSchedModel::computeInstrLatency(const MCSchedClassDesc &SCDesc) const { |
| 260 | unsigned Latency = 0; |
| 261 | for (unsigned DefIdx = 0, DefEnd = SCDesc.NumWriteLatencyEntries; |
| 262 | DefIdx != DefEnd; ++DefIdx) { |
| 263 | // Lookup the definition's write latency in SubtargetInfo. |
| 264 | const MCWriteLatencyEntry *WLEntry = |
| 265 | STI->getWriteLatencyEntry(&SCDesc, DefIdx); |
| 266 | Latency = std::max(Latency, capLatency(WLEntry->Cycles)); |
| 267 | } |
| 268 | return Latency; |
| 269 | } |
| 270 | |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 271 | unsigned TargetSchedModel::computeInstrLatency(unsigned Opcode) const { |
| 272 | assert(hasInstrSchedModel() && "Only call this function with a SchedModel"); |
| 273 | |
| 274 | unsigned SCIdx = TII->get(Opcode).getSchedClass(); |
| 275 | const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SCIdx); |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 276 | |
Matthias Braun | 42e1e66 | 2015-05-14 18:01:13 +0000 | [diff] [blame] | 277 | if (SCDesc->isValid() && !SCDesc->isVariant()) |
| 278 | return computeInstrLatency(*SCDesc); |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 279 | |
Andrew V. Tischenko | 75745d0 | 2017-04-14 07:44:23 +0000 | [diff] [blame^] | 280 | if (SCDesc->isValid()) { |
| 281 | assert (!SCDesc->isVariant() && "No MI sched latency: SCDesc->isVariant()"); |
| 282 | return computeInstrLatency(*SCDesc); |
| 283 | } |
| 284 | return 0; |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 287 | unsigned |
| 288 | TargetSchedModel::computeInstrLatency(const MachineInstr *MI, |
| 289 | bool UseDefaultDefLatency) const { |
Andrew Trick | c334bd4 | 2012-10-10 05:43:18 +0000 | [diff] [blame] | 290 | // For the itinerary model, fall back to the old subtarget hook. |
| 291 | // Allow subtargets to compute Bundle latencies outside the machine model. |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 292 | if (hasInstrItineraries() || MI->isBundle() || |
| 293 | (!hasInstrSchedModel() && !UseDefaultDefLatency)) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 294 | return TII->getInstrLatency(&InstrItins, *MI); |
Andrew Trick | c334bd4 | 2012-10-10 05:43:18 +0000 | [diff] [blame] | 295 | |
Andrew Trick | 780fae8 | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 296 | if (hasInstrSchedModel()) { |
Andrew Trick | 780fae8 | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 297 | const MCSchedClassDesc *SCDesc = resolveSchedClass(MI); |
Matthias Braun | 42e1e66 | 2015-05-14 18:01:13 +0000 | [diff] [blame] | 298 | if (SCDesc->isValid()) |
| 299 | return computeInstrLatency(*SCDesc); |
Andrew Trick | 780fae8 | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 300 | } |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 301 | return TII->defaultDefLatency(SchedModel, *MI); |
Andrew Trick | 780fae8 | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 302 | } |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 303 | |
| 304 | unsigned TargetSchedModel:: |
| 305 | computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx, |
| 306 | const MachineInstr *DepMI) const { |
Junmo Park | 1181192 | 2016-06-21 08:09:58 +0000 | [diff] [blame] | 307 | if (!SchedModel.isOutOfOrder()) |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 308 | return 1; |
| 309 | |
Junmo Park | 1181192 | 2016-06-21 08:09:58 +0000 | [diff] [blame] | 310 | // Out-of-order processor can dispatch WAW dependencies in the same cycle. |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 311 | |
| 312 | // Treat predication as a data dependency for out-of-order cpus. In-order |
| 313 | // cpus do not need to treat predicated writes specially. |
| 314 | // |
| 315 | // TODO: The following hack exists because predication passes do not |
| 316 | // correctly append imp-use operands, and readsReg() strangely returns false |
| 317 | // for predicated defs. |
| 318 | unsigned Reg = DefMI->getOperand(DefOperIdx).getReg(); |
| 319 | const MachineFunction &MF = *DefMI->getParent()->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 320 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 321 | if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(*DepMI)) |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 322 | return computeInstrLatency(DefMI); |
| 323 | |
| 324 | // If we have a per operand scheduling model, check if this def is writing |
| 325 | // an unbuffered resource. If so, it treated like an in-order cpu. |
| 326 | if (hasInstrSchedModel()) { |
| 327 | const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); |
Andrew Trick | 5f35afb | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 328 | if (SCDesc->isValid()) { |
| 329 | for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc), |
| 330 | *PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) { |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 331 | if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->BufferSize) |
Andrew Trick | 5f35afb | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 332 | return 1; |
| 333 | } |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | return 0; |
| 337 | } |
Andrew V. Tischenko | 75745d0 | 2017-04-14 07:44:23 +0000 | [diff] [blame^] | 338 | |
| 339 | static Optional<double> |
| 340 | getRTroughputFromItineraries(unsigned schedClass, |
| 341 | const InstrItineraryData *IID){ |
| 342 | double Unknown = std::numeric_limits<double>::infinity(); |
| 343 | double Throughput = Unknown; |
| 344 | |
| 345 | for (const InstrStage *IS = IID->beginStage(schedClass), |
| 346 | *E = IID->endStage(schedClass); |
| 347 | IS != E; ++IS) { |
| 348 | unsigned Cycles = IS->getCycles(); |
| 349 | if (!Cycles) |
| 350 | continue; |
| 351 | Throughput = |
| 352 | std::min(Throughput, countPopulation(IS->getUnits()) * 1.0 / Cycles); |
| 353 | } |
| 354 | // We need reciprocal throughput that's why we return such value. |
| 355 | return 1 / Throughput; |
| 356 | } |
| 357 | |
| 358 | static Optional<double> |
| 359 | getRTroughputFromInstrSchedModel(const MCSchedClassDesc *SCDesc, |
| 360 | const TargetSubtargetInfo *STI, |
| 361 | const MCSchedModel &SchedModel) { |
| 362 | double Unknown = std::numeric_limits<double>::infinity(); |
| 363 | double Throughput = Unknown; |
| 364 | |
| 365 | for (const MCWriteProcResEntry *WPR = STI->getWriteProcResBegin(SCDesc), |
| 366 | *WEnd = STI->getWriteProcResEnd(SCDesc); |
| 367 | WPR != WEnd; ++WPR) { |
| 368 | unsigned Cycles = WPR->Cycles; |
| 369 | if (!Cycles) |
| 370 | return Optional<double>(); |
| 371 | |
| 372 | unsigned NumUnits = |
| 373 | SchedModel.getProcResource(WPR->ProcResourceIdx)->NumUnits; |
| 374 | Throughput = std::min(Throughput, NumUnits * 1.0 / Cycles); |
| 375 | } |
| 376 | // We need reciprocal throughput that's why we return such value. |
| 377 | return 1 / Throughput; |
| 378 | } |
| 379 | |
| 380 | Optional<double> |
| 381 | TargetSchedModel::computeInstrRThroughput(const MachineInstr *MI) const { |
| 382 | if (hasInstrItineraries()) |
| 383 | return getRTroughputFromItineraries(MI->getDesc().getSchedClass(), |
| 384 | getInstrItineraries()); |
| 385 | if (hasInstrSchedModel()) |
| 386 | return getRTroughputFromInstrSchedModel(resolveSchedClass(MI), STI, |
| 387 | SchedModel); |
| 388 | return Optional<double>(); |
| 389 | } |
| 390 | |
| 391 | Optional<double> |
| 392 | TargetSchedModel::computeInstrRThroughput(unsigned Opcode) const { |
| 393 | unsigned SchedClass = TII->get(Opcode).getSchedClass(); |
| 394 | if (hasInstrItineraries()) |
| 395 | return getRTroughputFromItineraries(SchedClass, getInstrItineraries()); |
| 396 | if (hasInstrSchedModel()) { |
| 397 | const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
| 398 | if (SCDesc->isValid() && !SCDesc->isVariant()) |
| 399 | return getRTroughputFromInstrSchedModel(SCDesc, STI, SchedModel); |
| 400 | } |
| 401 | return Optional<double>(); |
| 402 | } |