LLVM API Documentation
00001 //===-- llvm/Target/TargetSchedule.cpp - Sched Machine Model ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a wrapper around MCSchedModel that allows the interface 00011 // to benefit from information currently only available in TargetInstrInfo. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/CodeGen/TargetSchedule.h" 00016 #include "llvm/Support/CommandLine.h" 00017 #include "llvm/Support/raw_ostream.h" 00018 #include "llvm/Target/TargetInstrInfo.h" 00019 #include "llvm/Target/TargetMachine.h" 00020 #include "llvm/Target/TargetRegisterInfo.h" 00021 #include "llvm/Target/TargetSubtargetInfo.h" 00022 00023 using namespace llvm; 00024 00025 static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true), 00026 cl::desc("Use TargetSchedModel for latency lookup")); 00027 00028 static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true), 00029 cl::desc("Use InstrItineraryData for latency lookup")); 00030 00031 bool TargetSchedModel::hasInstrSchedModel() const { 00032 return EnableSchedModel && SchedModel.hasInstrSchedModel(); 00033 } 00034 00035 bool TargetSchedModel::hasInstrItineraries() const { 00036 return EnableSchedItins && !InstrItins.isEmpty(); 00037 } 00038 00039 static unsigned gcd(unsigned Dividend, unsigned Divisor) { 00040 // Dividend and Divisor will be naturally swapped as needed. 00041 while(Divisor) { 00042 unsigned Rem = Dividend % Divisor; 00043 Dividend = Divisor; 00044 Divisor = Rem; 00045 }; 00046 return Dividend; 00047 } 00048 static unsigned lcm(unsigned A, unsigned B) { 00049 unsigned LCM = (uint64_t(A) * B) / gcd(A, B); 00050 assert((LCM >= A && LCM >= B) && "LCM overflow"); 00051 return LCM; 00052 } 00053 00054 void TargetSchedModel::init(const MCSchedModel &sm, 00055 const TargetSubtargetInfo *sti, 00056 const TargetInstrInfo *tii) { 00057 SchedModel = sm; 00058 STI = sti; 00059 TII = tii; 00060 STI->initInstrItins(InstrItins); 00061 00062 unsigned NumRes = SchedModel.getNumProcResourceKinds(); 00063 ResourceFactors.resize(NumRes); 00064 ResourceLCM = SchedModel.IssueWidth; 00065 for (unsigned Idx = 0; Idx < NumRes; ++Idx) { 00066 unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits; 00067 if (NumUnits > 0) 00068 ResourceLCM = lcm(ResourceLCM, NumUnits); 00069 } 00070 MicroOpFactor = ResourceLCM / SchedModel.IssueWidth; 00071 for (unsigned Idx = 0; Idx < NumRes; ++Idx) { 00072 unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits; 00073 ResourceFactors[Idx] = NumUnits ? (ResourceLCM / NumUnits) : 0; 00074 } 00075 } 00076 00077 unsigned TargetSchedModel::getNumMicroOps(const MachineInstr *MI, 00078 const MCSchedClassDesc *SC) const { 00079 if (hasInstrItineraries()) { 00080 int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass()); 00081 return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, MI); 00082 } 00083 if (hasInstrSchedModel()) { 00084 if (!SC) 00085 SC = resolveSchedClass(MI); 00086 if (SC->isValid()) 00087 return SC->NumMicroOps; 00088 } 00089 return MI->isTransient() ? 0 : 1; 00090 } 00091 00092 // The machine model may explicitly specify an invalid latency, which 00093 // effectively means infinite latency. Since users of the TargetSchedule API 00094 // don't know how to handle this, we convert it to a very large latency that is 00095 // easy to distinguish when debugging the DAG but won't induce overflow. 00096 static unsigned capLatency(int Cycles) { 00097 return Cycles >= 0 ? Cycles : 1000; 00098 } 00099 00100 /// Return the MCSchedClassDesc for this instruction. Some SchedClasses require 00101 /// evaluation of predicates that depend on instruction operands or flags. 00102 const MCSchedClassDesc *TargetSchedModel:: 00103 resolveSchedClass(const MachineInstr *MI) const { 00104 00105 // Get the definition's scheduling class descriptor from this machine model. 00106 unsigned SchedClass = MI->getDesc().getSchedClass(); 00107 const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass); 00108 if (!SCDesc->isValid()) 00109 return SCDesc; 00110 00111 #ifndef NDEBUG 00112 unsigned NIter = 0; 00113 #endif 00114 while (SCDesc->isVariant()) { 00115 assert(++NIter < 6 && "Variants are nested deeper than the magic number"); 00116 00117 SchedClass = STI->resolveSchedClass(SchedClass, MI, this); 00118 SCDesc = SchedModel.getSchedClassDesc(SchedClass); 00119 } 00120 return SCDesc; 00121 } 00122 00123 /// Find the def index of this operand. This index maps to the machine model and 00124 /// is independent of use operands. Def operands may be reordered with uses or 00125 /// merged with uses without affecting the def index (e.g. before/after 00126 /// regalloc). However, an instruction's def operands must never be reordered 00127 /// with respect to each other. 00128 static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) { 00129 unsigned DefIdx = 0; 00130 for (unsigned i = 0; i != DefOperIdx; ++i) { 00131 const MachineOperand &MO = MI->getOperand(i); 00132 if (MO.isReg() && MO.isDef()) 00133 ++DefIdx; 00134 } 00135 return DefIdx; 00136 } 00137 00138 /// Find the use index of this operand. This is independent of the instruction's 00139 /// def operands. 00140 /// 00141 /// Note that uses are not determined by the operand's isUse property, which 00142 /// is simply the inverse of isDef. Here we consider any readsReg operand to be 00143 /// a "use". The machine model allows an operand to be both a Def and Use. 00144 static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) { 00145 unsigned UseIdx = 0; 00146 for (unsigned i = 0; i != UseOperIdx; ++i) { 00147 const MachineOperand &MO = MI->getOperand(i); 00148 if (MO.isReg() && MO.readsReg()) 00149 ++UseIdx; 00150 } 00151 return UseIdx; 00152 } 00153 00154 // Top-level API for clients that know the operand indices. 00155 unsigned TargetSchedModel::computeOperandLatency( 00156 const MachineInstr *DefMI, unsigned DefOperIdx, 00157 const MachineInstr *UseMI, unsigned UseOperIdx) const { 00158 00159 if (!hasInstrSchedModel() && !hasInstrItineraries()) 00160 return TII->defaultDefLatency(SchedModel, DefMI); 00161 00162 if (hasInstrItineraries()) { 00163 int OperLatency = 0; 00164 if (UseMI) { 00165 OperLatency = TII->getOperandLatency(&InstrItins, DefMI, DefOperIdx, 00166 UseMI, UseOperIdx); 00167 } 00168 else { 00169 unsigned DefClass = DefMI->getDesc().getSchedClass(); 00170 OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx); 00171 } 00172 if (OperLatency >= 0) 00173 return OperLatency; 00174 00175 // No operand latency was found. 00176 unsigned InstrLatency = TII->getInstrLatency(&InstrItins, DefMI); 00177 00178 // Expected latency is the max of the stage latency and itinerary props. 00179 // Rather than directly querying InstrItins stage latency, we call a TII 00180 // hook to allow subtargets to specialize latency. This hook is only 00181 // applicable to the InstrItins model. InstrSchedModel should model all 00182 // special cases without TII hooks. 00183 InstrLatency = std::max(InstrLatency, 00184 TII->defaultDefLatency(SchedModel, DefMI)); 00185 return InstrLatency; 00186 } 00187 // hasInstrSchedModel() 00188 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); 00189 unsigned DefIdx = findDefIdx(DefMI, DefOperIdx); 00190 if (DefIdx < SCDesc->NumWriteLatencyEntries) { 00191 // Lookup the definition's write latency in SubtargetInfo. 00192 const MCWriteLatencyEntry *WLEntry = 00193 STI->getWriteLatencyEntry(SCDesc, DefIdx); 00194 unsigned WriteID = WLEntry->WriteResourceID; 00195 unsigned Latency = capLatency(WLEntry->Cycles); 00196 if (!UseMI) 00197 return Latency; 00198 00199 // Lookup the use's latency adjustment in SubtargetInfo. 00200 const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI); 00201 if (UseDesc->NumReadAdvanceEntries == 0) 00202 return Latency; 00203 unsigned UseIdx = findUseIdx(UseMI, UseOperIdx); 00204 int Advance = STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID); 00205 if (Advance > 0 && (unsigned)Advance > Latency) // unsigned wrap 00206 return 0; 00207 return Latency - Advance; 00208 } 00209 // If DefIdx does not exist in the model (e.g. implicit defs), then return 00210 // unit latency (defaultDefLatency may be too conservative). 00211 #ifndef NDEBUG 00212 if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit() 00213 && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef() 00214 && SchedModel.isComplete()) { 00215 std::string Err; 00216 raw_string_ostream ss(Err); 00217 ss << "DefIdx " << DefIdx << " exceeds machine model writes for " 00218 << *DefMI; 00219 report_fatal_error(ss.str()); 00220 } 00221 #endif 00222 // FIXME: Automatically giving all implicit defs defaultDefLatency is 00223 // undesirable. We should only do it for defs that are known to the MC 00224 // desc like flags. Truly implicit defs should get 1 cycle latency. 00225 return DefMI->isTransient() ? 0 : TII->defaultDefLatency(SchedModel, DefMI); 00226 } 00227 00228 unsigned TargetSchedModel::computeInstrLatency(unsigned Opcode) const { 00229 assert(hasInstrSchedModel() && "Only call this function with a SchedModel"); 00230 00231 unsigned SCIdx = TII->get(Opcode).getSchedClass(); 00232 const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SCIdx); 00233 unsigned Latency = 0; 00234 00235 if (SCDesc->isValid() && !SCDesc->isVariant()) { 00236 for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries; 00237 DefIdx != DefEnd; ++DefIdx) { 00238 // Lookup the definition's write latency in SubtargetInfo. 00239 const MCWriteLatencyEntry *WLEntry = 00240 STI->getWriteLatencyEntry(SCDesc, DefIdx); 00241 Latency = std::max(Latency, capLatency(WLEntry->Cycles)); 00242 } 00243 return Latency; 00244 } 00245 00246 assert(Latency && "No MI sched latency"); 00247 return 0; 00248 } 00249 00250 unsigned 00251 TargetSchedModel::computeInstrLatency(const MachineInstr *MI, 00252 bool UseDefaultDefLatency) const { 00253 // For the itinerary model, fall back to the old subtarget hook. 00254 // Allow subtargets to compute Bundle latencies outside the machine model. 00255 if (hasInstrItineraries() || MI->isBundle() || 00256 (!hasInstrSchedModel() && !UseDefaultDefLatency)) 00257 return TII->getInstrLatency(&InstrItins, MI); 00258 00259 if (hasInstrSchedModel()) { 00260 const MCSchedClassDesc *SCDesc = resolveSchedClass(MI); 00261 if (SCDesc->isValid()) { 00262 unsigned Latency = 0; 00263 for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries; 00264 DefIdx != DefEnd; ++DefIdx) { 00265 // Lookup the definition's write latency in SubtargetInfo. 00266 const MCWriteLatencyEntry *WLEntry = 00267 STI->getWriteLatencyEntry(SCDesc, DefIdx); 00268 Latency = std::max(Latency, capLatency(WLEntry->Cycles)); 00269 } 00270 return Latency; 00271 } 00272 } 00273 return TII->defaultDefLatency(SchedModel, MI); 00274 } 00275 00276 unsigned TargetSchedModel:: 00277 computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx, 00278 const MachineInstr *DepMI) const { 00279 if (SchedModel.MicroOpBufferSize <= 1) 00280 return 1; 00281 00282 // MicroOpBufferSize > 1 indicates an out-of-order processor that can dispatch 00283 // WAW dependencies in the same cycle. 00284 00285 // Treat predication as a data dependency for out-of-order cpus. In-order 00286 // cpus do not need to treat predicated writes specially. 00287 // 00288 // TODO: The following hack exists because predication passes do not 00289 // correctly append imp-use operands, and readsReg() strangely returns false 00290 // for predicated defs. 00291 unsigned Reg = DefMI->getOperand(DefOperIdx).getReg(); 00292 const MachineFunction &MF = *DefMI->getParent()->getParent(); 00293 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 00294 if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(DepMI)) 00295 return computeInstrLatency(DefMI); 00296 00297 // If we have a per operand scheduling model, check if this def is writing 00298 // an unbuffered resource. If so, it treated like an in-order cpu. 00299 if (hasInstrSchedModel()) { 00300 const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI); 00301 if (SCDesc->isValid()) { 00302 for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc), 00303 *PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) { 00304 if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->BufferSize) 00305 return 1; 00306 } 00307 } 00308 } 00309 return 0; 00310 }