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