Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
9.6 KiB

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