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