2
0

TargetSchedule.cpp 11 KB

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