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