MCInstrItineraries.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCInstrItineraries.h - Scheduling ----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file describes the structures used for instruction
  15. // itineraries, stages, and operand reads/writes. This is used by
  16. // schedulers to determine instruction stages and latencies.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_MC_MCINSTRITINERARIES_H
  20. #define LLVM_MC_MCINSTRITINERARIES_H
  21. #include "llvm/MC/MCSchedule.h"
  22. #include <algorithm>
  23. namespace llvm {
  24. //===----------------------------------------------------------------------===//
  25. /// These values represent a non-pipelined step in
  26. /// the execution of an instruction. Cycles represents the number of
  27. /// discrete time slots needed to complete the stage. Units represent
  28. /// the choice of functional units that can be used to complete the
  29. /// stage. Eg. IntUnit1, IntUnit2. NextCycles indicates how many
  30. /// cycles should elapse from the start of this stage to the start of
  31. /// the next stage in the itinerary. A value of -1 indicates that the
  32. /// next stage should start immediately after the current one.
  33. /// For example:
  34. ///
  35. /// { 1, x, -1 }
  36. /// indicates that the stage occupies FU x for 1 cycle and that
  37. /// the next stage starts immediately after this one.
  38. ///
  39. /// { 2, x|y, 1 }
  40. /// indicates that the stage occupies either FU x or FU y for 2
  41. /// consecutive cycles and that the next stage starts one cycle
  42. /// after this stage starts. That is, the stage requirements
  43. /// overlap in time.
  44. ///
  45. /// { 1, x, 0 }
  46. /// indicates that the stage occupies FU x for 1 cycle and that
  47. /// the next stage starts in this same cycle. This can be used to
  48. /// indicate that the instruction requires multiple stages at the
  49. /// same time.
  50. ///
  51. /// FU reservation can be of two different kinds:
  52. /// - FUs which instruction actually requires
  53. /// - FUs which instruction just reserves. Reserved unit is not available for
  54. /// execution of other instruction. However, several instructions can reserve
  55. /// the same unit several times.
  56. /// Such two types of units reservation is used to model instruction domain
  57. /// change stalls, FUs using the same resource (e.g. same register file), etc.
  58. struct InstrStage {
  59. enum ReservationKinds {
  60. Required = 0,
  61. Reserved = 1
  62. };
  63. /// Bitmask representing a set of functional units.
  64. typedef uint64_t FuncUnits;
  65. unsigned Cycles_; ///< Length of stage in machine cycles
  66. FuncUnits Units_; ///< Choice of functional units
  67. int NextCycles_; ///< Number of machine cycles to next stage
  68. ReservationKinds Kind_; ///< Kind of the FU reservation
  69. /// Returns the number of cycles the stage is occupied.
  70. unsigned getCycles() const {
  71. return Cycles_;
  72. }
  73. /// Returns the choice of FUs.
  74. FuncUnits getUnits() const {
  75. return Units_;
  76. }
  77. ReservationKinds getReservationKind() const {
  78. return Kind_;
  79. }
  80. /// Returns the number of cycles from the start of this stage to the
  81. /// start of the next stage in the itinerary
  82. unsigned getNextCycles() const {
  83. return (NextCycles_ >= 0) ? (unsigned)NextCycles_ : Cycles_;
  84. }
  85. };
  86. //===----------------------------------------------------------------------===//
  87. /// An itinerary represents the scheduling information for an instruction.
  88. /// This includes a set of stages occupied by the instruction and the pipeline
  89. /// cycle in which operands are read and written.
  90. ///
  91. struct InstrItinerary {
  92. int16_t NumMicroOps; ///< # of micro-ops, -1 means it's variable
  93. uint16_t FirstStage; ///< Index of first stage in itinerary
  94. uint16_t LastStage; ///< Index of last + 1 stage in itinerary
  95. uint16_t FirstOperandCycle; ///< Index of first operand rd/wr
  96. uint16_t LastOperandCycle; ///< Index of last + 1 operand rd/wr
  97. };
  98. //===----------------------------------------------------------------------===//
  99. /// Itinerary data supplied by a subtarget to be used by a target.
  100. ///
  101. class InstrItineraryData {
  102. public:
  103. MCSchedModel SchedModel =
  104. MCSchedModel::GetDefaultSchedModel(); ///< Basic machine properties.
  105. const InstrStage *Stages = nullptr; ///< Array of stages selected
  106. const unsigned *OperandCycles = nullptr; ///< Array of operand cycles selected
  107. const unsigned *Forwardings = nullptr; ///< Array of pipeline forwarding paths
  108. const InstrItinerary *Itineraries =
  109. nullptr; ///< Array of itineraries selected
  110. InstrItineraryData() = default;
  111. InstrItineraryData(const MCSchedModel &SM, const InstrStage *S,
  112. const unsigned *OS, const unsigned *F)
  113. : SchedModel(SM), Stages(S), OperandCycles(OS), Forwardings(F),
  114. Itineraries(SchedModel.InstrItineraries) {}
  115. /// Returns true if there are no itineraries.
  116. bool isEmpty() const { return Itineraries == nullptr; }
  117. /// Returns true if the index is for the end marker itinerary.
  118. bool isEndMarker(unsigned ItinClassIndx) const {
  119. return ((Itineraries[ItinClassIndx].FirstStage == UINT16_MAX) &&
  120. (Itineraries[ItinClassIndx].LastStage == UINT16_MAX));
  121. }
  122. /// Return the first stage of the itinerary.
  123. const InstrStage *beginStage(unsigned ItinClassIndx) const {
  124. unsigned StageIdx = Itineraries[ItinClassIndx].FirstStage;
  125. return Stages + StageIdx;
  126. }
  127. /// Return the last+1 stage of the itinerary.
  128. const InstrStage *endStage(unsigned ItinClassIndx) const {
  129. unsigned StageIdx = Itineraries[ItinClassIndx].LastStage;
  130. return Stages + StageIdx;
  131. }
  132. /// Return the total stage latency of the given class. The latency is
  133. /// the maximum completion time for any stage in the itinerary. If no stages
  134. /// exist, it defaults to one cycle.
  135. unsigned getStageLatency(unsigned ItinClassIndx) const {
  136. // If the target doesn't provide itinerary information, use a simple
  137. // non-zero default value for all instructions.
  138. if (isEmpty())
  139. return 1;
  140. // Calculate the maximum completion time for any stage.
  141. unsigned Latency = 0, StartCycle = 0;
  142. for (const InstrStage *IS = beginStage(ItinClassIndx),
  143. *E = endStage(ItinClassIndx); IS != E; ++IS) {
  144. Latency = std::max(Latency, StartCycle + IS->getCycles());
  145. StartCycle += IS->getNextCycles();
  146. }
  147. return Latency;
  148. }
  149. /// Return the cycle for the given class and operand. Return -1 if no
  150. /// cycle is specified for the operand.
  151. int getOperandCycle(unsigned ItinClassIndx, unsigned OperandIdx) const {
  152. if (isEmpty())
  153. return -1;
  154. unsigned FirstIdx = Itineraries[ItinClassIndx].FirstOperandCycle;
  155. unsigned LastIdx = Itineraries[ItinClassIndx].LastOperandCycle;
  156. if ((FirstIdx + OperandIdx) >= LastIdx)
  157. return -1;
  158. return (int)OperandCycles[FirstIdx + OperandIdx];
  159. }
  160. /// Return true if there is a pipeline forwarding between instructions
  161. /// of itinerary classes DefClass and UseClasses so that value produced by an
  162. /// instruction of itinerary class DefClass, operand index DefIdx can be
  163. /// bypassed when it's read by an instruction of itinerary class UseClass,
  164. /// operand index UseIdx.
  165. bool hasPipelineForwarding(unsigned DefClass, unsigned DefIdx,
  166. unsigned UseClass, unsigned UseIdx) const {
  167. unsigned FirstDefIdx = Itineraries[DefClass].FirstOperandCycle;
  168. unsigned LastDefIdx = Itineraries[DefClass].LastOperandCycle;
  169. if ((FirstDefIdx + DefIdx) >= LastDefIdx)
  170. return false;
  171. if (Forwardings[FirstDefIdx + DefIdx] == 0)
  172. return false;
  173. unsigned FirstUseIdx = Itineraries[UseClass].FirstOperandCycle;
  174. unsigned LastUseIdx = Itineraries[UseClass].LastOperandCycle;
  175. if ((FirstUseIdx + UseIdx) >= LastUseIdx)
  176. return false;
  177. return Forwardings[FirstDefIdx + DefIdx] ==
  178. Forwardings[FirstUseIdx + UseIdx];
  179. }
  180. /// Compute and return the use operand latency of a given itinerary
  181. /// class and operand index if the value is produced by an instruction of the
  182. /// specified itinerary class and def operand index.
  183. int getOperandLatency(unsigned DefClass, unsigned DefIdx,
  184. unsigned UseClass, unsigned UseIdx) const {
  185. if (isEmpty())
  186. return -1;
  187. int DefCycle = getOperandCycle(DefClass, DefIdx);
  188. if (DefCycle == -1)
  189. return -1;
  190. int UseCycle = getOperandCycle(UseClass, UseIdx);
  191. if (UseCycle == -1)
  192. return -1;
  193. UseCycle = DefCycle - UseCycle + 1;
  194. if (UseCycle > 0 &&
  195. hasPipelineForwarding(DefClass, DefIdx, UseClass, UseIdx))
  196. // FIXME: This assumes one cycle benefit for every pipeline forwarding.
  197. --UseCycle;
  198. return UseCycle;
  199. }
  200. /// Return the number of micro-ops that the given class decodes to.
  201. /// Return -1 for classes that require dynamic lookup via TargetInstrInfo.
  202. int getNumMicroOps(unsigned ItinClassIndx) const {
  203. if (isEmpty())
  204. return 1;
  205. return Itineraries[ItinClassIndx].NumMicroOps;
  206. }
  207. };
  208. } // end namespace llvm
  209. #endif // LLVM_MC_MCINSTRITINERARIES_H
  210. #ifdef __GNUC__
  211. #pragma GCC diagnostic pop
  212. #endif