Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 1 | //===-- llvm/Target/TargetSchedule.cpp - Sched Machine Model ----*- 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 a wrapper around MCSchedModel that allows the interface |
| 11 | // to benefit from information currently only available in TargetInstrInfo. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/TargetSchedule.h" |
| 16 | #include "llvm/Target/TargetInstrInfo.h" |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetMachine.h" |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetRegisterInfo.h" |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 25 | static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true), |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 26 | cl::desc("Use TargetSchedModel for latency lookup")); |
| 27 | |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 28 | static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true), |
| 29 | cl::desc("Use InstrItineraryData for latency lookup")); |
| 30 | |
Andrew Trick | 42bb106 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 31 | bool TargetSchedModel::hasInstrSchedModel() const { |
| 32 | return EnableSchedModel && SchedModel.hasInstrSchedModel(); |
| 33 | } |
| 34 | |
| 35 | bool TargetSchedModel::hasInstrItineraries() const { |
| 36 | return EnableSchedItins && !InstrItins.isEmpty(); |
| 37 | } |
| 38 | |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 39 | void TargetSchedModel::init(const MCSchedModel &sm, |
| 40 | const TargetSubtargetInfo *sti, |
| 41 | const TargetInstrInfo *tii) { |
| 42 | SchedModel = sm; |
| 43 | STI = sti; |
| 44 | TII = tii; |
| 45 | STI->initInstrItins(InstrItins); |
| 46 | } |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 47 | |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 48 | unsigned TargetSchedModel::getNumMicroOps(MachineInstr *MI) const { |
| 49 | if (hasInstrItineraries()) { |
| 50 | int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass()); |
| 51 | return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, MI); |
| 52 | } |
Andrew Trick | 4903c15 | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 53 | if (hasInstrSchedModel()) { |
| 54 | const MCSchedClassDesc *SCDesc = resolveSchedClass(MI); |
| 55 | if (SCDesc->isValid()) |
| 56 | return SCDesc->NumMicroOps; |
| 57 | } |
| 58 | return MI->isTransient() ? 0 : 1; |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Andrew Trick | fdd6fa8 | 2012-10-17 17:27:10 +0000 | [diff] [blame^] | 61 | // The machine model may explicitly specify an invalid latency, which |
| 62 | // effectively means infinite latency. Since users of the TargetSchedule API |
| 63 | // don't know how to handle this, we convert it to a very large latency that is |
| 64 | // easy to distinguish when debugging the DAG but won't induce overflow. |
| 65 | static unsigned convertLatency(int Cycles) { |
| 66 | return Cycles >= 0 ? Cycles : 1000; |
| 67 | } |
| 68 | |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 69 | /// If we can determine the operand latency from the def only, without machine |
| 70 | /// model or itinerary lookup, do so. Otherwise return -1. |
| 71 | int TargetSchedModel::getDefLatency(const MachineInstr *DefMI, |
| 72 | bool FindMin) const { |
| 73 | |
| 74 | // Return a latency based on the itinerary properties and defining instruction |
| 75 | // if possible. Some common subtargets don't require per-operand latency, |
| 76 | // especially for minimum latencies. |
| 77 | if (FindMin) { |
| 78 | // If MinLatency is invalid, then use the itinerary for MinLatency. If no |
| 79 | // itinerary exists either, then use single cycle latency. |
Andrew Trick | 42bb106 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 80 | if (SchedModel.MinLatency < 0 && !hasInstrItineraries()) { |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | return SchedModel.MinLatency; |
| 84 | } |
Andrew Trick | 42bb106 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 85 | else if (!hasInstrSchedModel() && !hasInstrItineraries()) { |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 86 | return TII->defaultDefLatency(&SchedModel, DefMI); |
| 87 | } |
| 88 | // ...operand lookup required |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | /// Return the MCSchedClassDesc for this instruction. Some SchedClasses require |
| 93 | /// evaluation of predicates that depend on instruction operands or flags. |
| 94 | const MCSchedClassDesc *TargetSchedModel:: |
| 95 | resolveSchedClass(const MachineInstr *MI) const { |
| 96 | |
| 97 | // Get the definition's scheduling class descriptor from this machine model. |
| 98 | unsigned SchedClass = MI->getDesc().getSchedClass(); |
| 99 | const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
| 100 | |
| 101 | #ifndef NDEBUG |
| 102 | unsigned NIter = 0; |
| 103 | #endif |
| 104 | while (SCDesc->isVariant()) { |
| 105 | assert(++NIter < 6 && "Variants are nested deeper than the magic number"); |
| 106 | |
| 107 | SchedClass = STI->resolveSchedClass(SchedClass, MI, this); |
| 108 | SCDesc = SchedModel.getSchedClassDesc(SchedClass); |
| 109 | } |
| 110 | return SCDesc; |
| 111 | } |
| 112 | |
| 113 | /// Find the def index of this operand. This index maps to the machine model and |
| 114 | /// is independent of use operands. Def operands may be reordered with uses or |
| 115 | /// merged with uses without affecting the def index (e.g. before/after |
| 116 | /// regalloc). However, an instruction's def operands must never be reordered |
| 117 | /// with respect to each other. |
| 118 | static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) { |
| 119 | unsigned DefIdx = 0; |
| 120 | for (unsigned i = 0; i != DefOperIdx; ++i) { |
| 121 | const MachineOperand &MO = MI->getOperand(i); |
| 122 | if (MO.isReg() && MO.isDef()) |
| 123 | ++DefIdx; |
| 124 | } |
| 125 | return DefIdx; |
| 126 | } |
| 127 | |
| 128 | /// Find the use index of this operand. This is independent of the instruction's |
| 129 | /// def operands. |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 130 | /// |
| 131 | /// Note that uses are not determined by the operand's isUse property, which |
| 132 | /// is simply the inverse of isDef. Here we consider any readsReg operand to be |
| 133 | /// 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] | 134 | static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) { |
| 135 | unsigned UseIdx = 0; |
| 136 | for (unsigned i = 0; i != UseOperIdx; ++i) { |
| 137 | const MachineOperand &MO = MI->getOperand(i); |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 138 | if (MO.isReg() && MO.readsReg()) |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 139 | ++UseIdx; |
| 140 | } |
| 141 | return UseIdx; |
| 142 | } |
| 143 | |
| 144 | // Top-level API for clients that know the operand indices. |
| 145 | unsigned TargetSchedModel::computeOperandLatency( |
| 146 | const MachineInstr *DefMI, unsigned DefOperIdx, |
| 147 | const MachineInstr *UseMI, unsigned UseOperIdx, |
| 148 | bool FindMin) const { |
| 149 | |
| 150 | int DefLatency = getDefLatency(DefMI, FindMin); |
| 151 | if (DefLatency >= 0) |
| 152 | return DefLatency; |
| 153 | |
Andrew Trick | 42bb106 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 154 | if (hasInstrItineraries()) { |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 155 | int OperLatency = 0; |
| 156 | if (UseMI) { |
| 157 | OperLatency = |
| 158 | TII->getOperandLatency(&InstrItins, DefMI, DefOperIdx, UseMI, UseOperIdx); |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 159 | } |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 160 | else { |
| 161 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 162 | OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx); |
| 163 | } |
| 164 | if (OperLatency >= 0) |
| 165 | return OperLatency; |
| 166 | |
| 167 | // No operand latency was found. |
| 168 | unsigned InstrLatency = TII->getInstrLatency(&InstrItins, DefMI); |
| 169 | |
| 170 | // Expected latency is the max of the stage latency and itinerary props. |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 171 | // Rather than directly querying InstrItins stage latency, we call a TII |
| 172 | // hook to allow subtargets to specialize latency. This hook is only |
| 173 | // applicable to the InstrItins model. InstrSchedModel should model all |
| 174 | // special cases without TII hooks. |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 175 | if (!FindMin) |
| 176 | InstrLatency = std::max(InstrLatency, |
| 177 | TII->defaultDefLatency(&SchedModel, DefMI)); |
| 178 | return InstrLatency; |
| 179 | } |
Andrew Trick | 42bb106 | 2012-10-09 23:44:26 +0000 | [diff] [blame] | 180 | assert(!FindMin && hasInstrSchedModel() && |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 181 | "Expected a SchedModel for this cpu"); |
| 182 | const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); |
| 183 | unsigned DefIdx = findDefIdx(DefMI, DefOperIdx); |
| 184 | if (DefIdx < SCDesc->NumWriteLatencyEntries) { |
| 185 | // Lookup the definition's write latency in SubtargetInfo. |
| 186 | const MCWriteLatencyEntry *WLEntry = |
| 187 | STI->getWriteLatencyEntry(SCDesc, DefIdx); |
| 188 | unsigned WriteID = WLEntry->WriteResourceID; |
Andrew Trick | fdd6fa8 | 2012-10-17 17:27:10 +0000 | [diff] [blame^] | 189 | unsigned Latency = convertLatency(WLEntry->Cycles); |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 190 | if (!UseMI) |
| 191 | return Latency; |
| 192 | |
| 193 | // Lookup the use's latency adjustment in SubtargetInfo. |
| 194 | const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI); |
| 195 | if (UseDesc->NumReadAdvanceEntries == 0) |
| 196 | return Latency; |
| 197 | unsigned UseIdx = findUseIdx(UseMI, UseOperIdx); |
| 198 | return Latency - STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID); |
| 199 | } |
| 200 | // If DefIdx does not exist in the model (e.g. implicit defs), then return |
| 201 | // unit latency (defaultDefLatency may be too conservative). |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 202 | #ifndef NDEBUG |
Andrew Trick | 72fd0a9 | 2012-10-04 00:24:34 +0000 | [diff] [blame] | 203 | if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit() |
| 204 | && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef()) { |
| 205 | std::string Err; |
| 206 | raw_string_ostream ss(Err); |
| 207 | ss << "DefIdx " << DefIdx << " exceeds machine model writes for " |
| 208 | << *DefMI; |
| 209 | report_fatal_error(ss.str()); |
| 210 | } |
Andrew Trick | 3918cad | 2012-09-18 18:20:02 +0000 | [diff] [blame] | 211 | #endif |
Andrew Trick | 4903c15 | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 212 | return DefMI->isTransient() ? 0 : 1; |
Andrew Trick | 34301ce | 2012-09-18 04:03:34 +0000 | [diff] [blame] | 213 | } |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 214 | |
| 215 | unsigned TargetSchedModel::computeInstrLatency(const MachineInstr *MI) const { |
Andrew Trick | 82d46ae | 2012-10-10 05:43:18 +0000 | [diff] [blame] | 216 | // For the itinerary model, fall back to the old subtarget hook. |
| 217 | // Allow subtargets to compute Bundle latencies outside the machine model. |
| 218 | if (hasInstrItineraries() || MI->isBundle()) |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 219 | return TII->getInstrLatency(&InstrItins, MI); |
Andrew Trick | 82d46ae | 2012-10-10 05:43:18 +0000 | [diff] [blame] | 220 | |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 221 | if (hasInstrSchedModel()) { |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 222 | const MCSchedClassDesc *SCDesc = resolveSchedClass(MI); |
Andrew Trick | 4903c15 | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 223 | if (SCDesc->isValid()) { |
| 224 | unsigned Latency = 0; |
| 225 | for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries; |
| 226 | DefIdx != DefEnd; ++DefIdx) { |
| 227 | // Lookup the definition's write latency in SubtargetInfo. |
| 228 | const MCWriteLatencyEntry *WLEntry = |
| 229 | STI->getWriteLatencyEntry(SCDesc, DefIdx); |
Andrew Trick | fdd6fa8 | 2012-10-17 17:27:10 +0000 | [diff] [blame^] | 230 | Latency = std::max(Latency, convertLatency(WLEntry->Cycles)); |
Andrew Trick | 4903c15 | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 231 | } |
| 232 | return Latency; |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 233 | } |
Andrew Trick | c0dfffa | 2012-10-09 23:44:32 +0000 | [diff] [blame] | 234 | } |
| 235 | return TII->defaultDefLatency(&SchedModel, MI); |
| 236 | } |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 237 | |
| 238 | unsigned TargetSchedModel:: |
| 239 | computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx, |
| 240 | const MachineInstr *DepMI) const { |
| 241 | // MinLatency == -1 is for in-order processors that always have unit |
| 242 | // MinLatency. MinLatency > 0 is for in-order processors with varying min |
| 243 | // latencies, but since this is not a RAW dep, we always use unit latency. |
| 244 | if (SchedModel.MinLatency != 0) |
| 245 | return 1; |
| 246 | |
| 247 | // MinLatency == 0 indicates an out-of-order processor that can dispatch |
| 248 | // WAW dependencies in the same cycle. |
| 249 | |
| 250 | // Treat predication as a data dependency for out-of-order cpus. In-order |
| 251 | // cpus do not need to treat predicated writes specially. |
| 252 | // |
| 253 | // TODO: The following hack exists because predication passes do not |
| 254 | // correctly append imp-use operands, and readsReg() strangely returns false |
| 255 | // for predicated defs. |
| 256 | unsigned Reg = DefMI->getOperand(DefOperIdx).getReg(); |
| 257 | const MachineFunction &MF = *DefMI->getParent()->getParent(); |
| 258 | const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); |
| 259 | if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(DepMI)) |
| 260 | return computeInstrLatency(DefMI); |
| 261 | |
| 262 | // If we have a per operand scheduling model, check if this def is writing |
| 263 | // an unbuffered resource. If so, it treated like an in-order cpu. |
| 264 | if (hasInstrSchedModel()) { |
| 265 | const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); |
Andrew Trick | 4903c15 | 2012-10-11 05:37:06 +0000 | [diff] [blame] | 266 | if (SCDesc->isValid()) { |
| 267 | for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc), |
| 268 | *PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) { |
| 269 | if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->IsBuffered) |
| 270 | return 1; |
| 271 | } |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | return 0; |
| 275 | } |