TargetSchedule.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //===- llvm/Target/TargetSchedule.cpp - Sched Machine Model ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a wrapper around MCSchedModel that allows the interface
  10. // to benefit from information currently only available in TargetInstrInfo.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/TargetSchedule.h"
  14. #include "llvm/CodeGen/MachineFunction.h"
  15. #include "llvm/CodeGen/MachineInstr.h"
  16. #include "llvm/CodeGen/MachineOperand.h"
  17. #include "llvm/CodeGen/TargetInstrInfo.h"
  18. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  19. #include "llvm/MC/MCInstrDesc.h"
  20. #include "llvm/MC/MCInstrItineraries.h"
  21. #include "llvm/MC/MCSchedule.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <algorithm>
  26. #include <cassert>
  27. #include <cstdint>
  28. #include <numeric>
  29. using namespace llvm;
  30. static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true),
  31. cl::desc("Use TargetSchedModel for latency lookup"));
  32. static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true),
  33. cl::desc("Use InstrItineraryData for latency lookup"));
  34. bool TargetSchedModel::hasInstrSchedModel() const {
  35. return EnableSchedModel && SchedModel.hasInstrSchedModel();
  36. }
  37. bool TargetSchedModel::hasInstrItineraries() const {
  38. return EnableSchedItins && !InstrItins.isEmpty();
  39. }
  40. void TargetSchedModel::init(const TargetSubtargetInfo *TSInfo) {
  41. STI = TSInfo;
  42. SchedModel = TSInfo->getSchedModel();
  43. TII = TSInfo->getInstrInfo();
  44. STI->initInstrItins(InstrItins);
  45. unsigned NumRes = SchedModel.getNumProcResourceKinds();
  46. ResourceFactors.resize(NumRes);
  47. ResourceLCM = SchedModel.IssueWidth;
  48. for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
  49. unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
  50. if (NumUnits > 0)
  51. ResourceLCM = std::lcm(ResourceLCM, NumUnits);
  52. }
  53. MicroOpFactor = ResourceLCM / SchedModel.IssueWidth;
  54. for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
  55. unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
  56. ResourceFactors[Idx] = NumUnits ? (ResourceLCM / NumUnits) : 0;
  57. }
  58. }
  59. /// Returns true only if instruction is specified as single issue.
  60. bool TargetSchedModel::mustBeginGroup(const MachineInstr *MI,
  61. const MCSchedClassDesc *SC) const {
  62. if (hasInstrSchedModel()) {
  63. if (!SC)
  64. SC = resolveSchedClass(MI);
  65. if (SC->isValid())
  66. return SC->BeginGroup;
  67. }
  68. return false;
  69. }
  70. bool TargetSchedModel::mustEndGroup(const MachineInstr *MI,
  71. const MCSchedClassDesc *SC) const {
  72. if (hasInstrSchedModel()) {
  73. if (!SC)
  74. SC = resolveSchedClass(MI);
  75. if (SC->isValid())
  76. return SC->EndGroup;
  77. }
  78. return false;
  79. }
  80. unsigned TargetSchedModel::getNumMicroOps(const MachineInstr *MI,
  81. const MCSchedClassDesc *SC) const {
  82. if (hasInstrItineraries()) {
  83. int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass());
  84. return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, *MI);
  85. }
  86. if (hasInstrSchedModel()) {
  87. if (!SC)
  88. SC = resolveSchedClass(MI);
  89. if (SC->isValid())
  90. return SC->NumMicroOps;
  91. }
  92. return MI->isTransient() ? 0 : 1;
  93. }
  94. // The machine model may explicitly specify an invalid latency, which
  95. // effectively means infinite latency. Since users of the TargetSchedule API
  96. // don't know how to handle this, we convert it to a very large latency that is
  97. // easy to distinguish when debugging the DAG but won't induce overflow.
  98. static unsigned capLatency(int Cycles) {
  99. return Cycles >= 0 ? Cycles : 1000;
  100. }
  101. /// Return the MCSchedClassDesc for this instruction. Some SchedClasses require
  102. /// evaluation of predicates that depend on instruction operands or flags.
  103. const MCSchedClassDesc *TargetSchedModel::
  104. resolveSchedClass(const MachineInstr *MI) const {
  105. // Get the definition's scheduling class descriptor from this machine model.
  106. unsigned SchedClass = MI->getDesc().getSchedClass();
  107. const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass);
  108. if (!SCDesc->isValid())
  109. return SCDesc;
  110. #ifndef NDEBUG
  111. unsigned NIter = 0;
  112. #endif
  113. while (SCDesc->isVariant()) {
  114. assert(++NIter < 6 && "Variants are nested deeper than the magic number");
  115. SchedClass = STI->resolveSchedClass(SchedClass, MI, this);
  116. SCDesc = SchedModel.getSchedClassDesc(SchedClass);
  117. }
  118. return SCDesc;
  119. }
  120. /// Find the def index of this operand. This index maps to the machine model and
  121. /// is independent of use operands. Def operands may be reordered with uses or
  122. /// merged with uses without affecting the def index (e.g. before/after
  123. /// regalloc). However, an instruction's def operands must never be reordered
  124. /// with respect to each other.
  125. static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) {
  126. unsigned DefIdx = 0;
  127. for (unsigned i = 0; i != DefOperIdx; ++i) {
  128. const MachineOperand &MO = MI->getOperand(i);
  129. if (MO.isReg() && MO.isDef())
  130. ++DefIdx;
  131. }
  132. return DefIdx;
  133. }
  134. /// Find the use index of this operand. This is independent of the instruction's
  135. /// def operands.
  136. ///
  137. /// Note that uses are not determined by the operand's isUse property, which
  138. /// is simply the inverse of isDef. Here we consider any readsReg operand to be
  139. /// a "use". The machine model allows an operand to be both a Def and Use.
  140. static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) {
  141. unsigned UseIdx = 0;
  142. for (unsigned i = 0; i != UseOperIdx; ++i) {
  143. const MachineOperand &MO = MI->getOperand(i);
  144. if (MO.isReg() && MO.readsReg() && !MO.isDef())
  145. ++UseIdx;
  146. }
  147. return UseIdx;
  148. }
  149. // Top-level API for clients that know the operand indices.
  150. unsigned TargetSchedModel::computeOperandLatency(
  151. const MachineInstr *DefMI, unsigned DefOperIdx,
  152. const MachineInstr *UseMI, unsigned UseOperIdx) const {
  153. if (!hasInstrSchedModel() && !hasInstrItineraries())
  154. return TII->defaultDefLatency(SchedModel, *DefMI);
  155. if (hasInstrItineraries()) {
  156. int OperLatency = 0;
  157. if (UseMI) {
  158. OperLatency = TII->getOperandLatency(&InstrItins, *DefMI, DefOperIdx,
  159. *UseMI, UseOperIdx);
  160. }
  161. else {
  162. unsigned DefClass = DefMI->getDesc().getSchedClass();
  163. OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx);
  164. }
  165. if (OperLatency >= 0)
  166. return OperLatency;
  167. // No operand latency was found.
  168. unsigned InstrLatency = TII->getInstrLatency(&InstrItins, *DefMI);
  169. // Expected latency is the max of the stage latency and itinerary props.
  170. // Rather than directly querying InstrItins stage latency, we call a TII
  171. // hook to allow subtargets to specialize latency. This hook is only
  172. // applicable to the InstrItins model. InstrSchedModel should model all
  173. // special cases without TII hooks.
  174. InstrLatency =
  175. std::max(InstrLatency, TII->defaultDefLatency(SchedModel, *DefMI));
  176. return InstrLatency;
  177. }
  178. // hasInstrSchedModel()
  179. const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
  180. unsigned DefIdx = findDefIdx(DefMI, DefOperIdx);
  181. if (DefIdx < SCDesc->NumWriteLatencyEntries) {
  182. // Lookup the definition's write latency in SubtargetInfo.
  183. const MCWriteLatencyEntry *WLEntry =
  184. STI->getWriteLatencyEntry(SCDesc, DefIdx);
  185. unsigned WriteID = WLEntry->WriteResourceID;
  186. unsigned Latency = capLatency(WLEntry->Cycles);
  187. if (!UseMI)
  188. return Latency;
  189. // Lookup the use's latency adjustment in SubtargetInfo.
  190. const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI);
  191. if (UseDesc->NumReadAdvanceEntries == 0)
  192. return Latency;
  193. unsigned UseIdx = findUseIdx(UseMI, UseOperIdx);
  194. int Advance = STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID);
  195. if (Advance > 0 && (unsigned)Advance > Latency) // unsigned wrap
  196. return 0;
  197. return Latency - Advance;
  198. }
  199. // If DefIdx does not exist in the model (e.g. implicit defs), then return
  200. // unit latency (defaultDefLatency may be too conservative).
  201. #ifndef NDEBUG
  202. if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit() &&
  203. !DefMI->getDesc().operands()[DefOperIdx].isOptionalDef() &&
  204. SchedModel.isComplete()) {
  205. errs() << "DefIdx " << DefIdx << " exceeds machine model writes for "
  206. << *DefMI << " (Try with MCSchedModel.CompleteModel set to false)";
  207. llvm_unreachable("incomplete machine model");
  208. }
  209. #endif
  210. // FIXME: Automatically giving all implicit defs defaultDefLatency is
  211. // undesirable. We should only do it for defs that are known to the MC
  212. // desc like flags. Truly implicit defs should get 1 cycle latency.
  213. return DefMI->isTransient() ? 0 : TII->defaultDefLatency(SchedModel, *DefMI);
  214. }
  215. unsigned
  216. TargetSchedModel::computeInstrLatency(const MCSchedClassDesc &SCDesc) const {
  217. return capLatency(MCSchedModel::computeInstrLatency(*STI, SCDesc));
  218. }
  219. unsigned TargetSchedModel::computeInstrLatency(unsigned Opcode) const {
  220. assert(hasInstrSchedModel() && "Only call this function with a SchedModel");
  221. unsigned SCIdx = TII->get(Opcode).getSchedClass();
  222. return capLatency(SchedModel.computeInstrLatency(*STI, SCIdx));
  223. }
  224. unsigned TargetSchedModel::computeInstrLatency(const MCInst &Inst) const {
  225. if (hasInstrSchedModel())
  226. return capLatency(SchedModel.computeInstrLatency(*STI, *TII, Inst));
  227. return computeInstrLatency(Inst.getOpcode());
  228. }
  229. unsigned
  230. TargetSchedModel::computeInstrLatency(const MachineInstr *MI,
  231. bool UseDefaultDefLatency) const {
  232. // For the itinerary model, fall back to the old subtarget hook.
  233. // Allow subtargets to compute Bundle latencies outside the machine model.
  234. if (hasInstrItineraries() || MI->isBundle() ||
  235. (!hasInstrSchedModel() && !UseDefaultDefLatency))
  236. return TII->getInstrLatency(&InstrItins, *MI);
  237. if (hasInstrSchedModel()) {
  238. const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
  239. if (SCDesc->isValid())
  240. return computeInstrLatency(*SCDesc);
  241. }
  242. return TII->defaultDefLatency(SchedModel, *MI);
  243. }
  244. unsigned TargetSchedModel::
  245. computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx,
  246. const MachineInstr *DepMI) const {
  247. if (!SchedModel.isOutOfOrder())
  248. return 1;
  249. // Out-of-order processor can dispatch WAW dependencies in the same cycle.
  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. Register Reg = DefMI->getOperand(DefOperIdx).getReg();
  257. const MachineFunction &MF = *DefMI->getMF();
  258. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  259. if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(*DepMI))
  260. return computeInstrLatency(DefMI);
  261. // If we have a per operand scheduling model, check if this def is writing
  262. // an unbuffered resource. If so, it treated like an in-order cpu.
  263. if (hasInstrSchedModel()) {
  264. const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
  265. if (SCDesc->isValid()) {
  266. for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc),
  267. *PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) {
  268. if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->BufferSize)
  269. return 1;
  270. }
  271. }
  272. }
  273. return 0;
  274. }
  275. double
  276. TargetSchedModel::computeReciprocalThroughput(const MachineInstr *MI) const {
  277. if (hasInstrItineraries()) {
  278. unsigned SchedClass = MI->getDesc().getSchedClass();
  279. return MCSchedModel::getReciprocalThroughput(SchedClass,
  280. *getInstrItineraries());
  281. }
  282. if (hasInstrSchedModel())
  283. return MCSchedModel::getReciprocalThroughput(*STI, *resolveSchedClass(MI));
  284. return 0.0;
  285. }
  286. double
  287. TargetSchedModel::computeReciprocalThroughput(unsigned Opcode) const {
  288. unsigned SchedClass = TII->get(Opcode).getSchedClass();
  289. if (hasInstrItineraries())
  290. return MCSchedModel::getReciprocalThroughput(SchedClass,
  291. *getInstrItineraries());
  292. if (hasInstrSchedModel()) {
  293. const MCSchedClassDesc &SCDesc = *SchedModel.getSchedClassDesc(SchedClass);
  294. if (SCDesc.isValid() && !SCDesc.isVariant())
  295. return MCSchedModel::getReciprocalThroughput(*STI, SCDesc);
  296. }
  297. return 0.0;
  298. }
  299. double
  300. TargetSchedModel::computeReciprocalThroughput(const MCInst &MI) const {
  301. if (hasInstrSchedModel())
  302. return SchedModel.getReciprocalThroughput(*STI, *TII, MI);
  303. return computeReciprocalThroughput(MI.getOpcode());
  304. }