MCSchedule.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //===-- llvm/MC/MCSchedule.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 defines the classes used to describe a subtarget's machine model
  11. // for scheduling and other instruction cost heuristics.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_MC_MCSCHEDULE_H
  15. #define LLVM_MC_MCSCHEDULE_H
  16. #include "llvm/Support/DataTypes.h"
  17. #include <cassert>
  18. namespace llvm {
  19. struct InstrItinerary;
  20. /// Define a kind of processor resource that will be modeled by the scheduler.
  21. struct MCProcResourceDesc {
  22. #ifndef NDEBUG
  23. const char *Name;
  24. #endif
  25. unsigned NumUnits; // Number of resource of this kind
  26. unsigned SuperIdx; // Index of the resources kind that contains this kind.
  27. // Number of resources that may be buffered.
  28. //
  29. // Buffered resources (BufferSize != 0) may be consumed at some indeterminate
  30. // cycle after dispatch. This should be used for out-of-order cpus when
  31. // instructions that use this resource can be buffered in a reservaton
  32. // station.
  33. //
  34. // Unbuffered resources (BufferSize == 0) always consume their resource some
  35. // fixed number of cycles after dispatch. If a resource is unbuffered, then
  36. // the scheduler will avoid scheduling instructions with conflicting resources
  37. // in the same cycle. This is for in-order cpus, or the in-order portion of
  38. // an out-of-order cpus.
  39. int BufferSize;
  40. bool operator==(const MCProcResourceDesc &Other) const {
  41. return NumUnits == Other.NumUnits && SuperIdx == Other.SuperIdx
  42. && BufferSize == Other.BufferSize;
  43. }
  44. };
  45. /// Identify one of the processor resource kinds consumed by a particular
  46. /// scheduling class for the specified number of cycles.
  47. struct MCWriteProcResEntry {
  48. unsigned ProcResourceIdx;
  49. unsigned Cycles;
  50. bool operator==(const MCWriteProcResEntry &Other) const {
  51. return ProcResourceIdx == Other.ProcResourceIdx && Cycles == Other.Cycles;
  52. }
  53. };
  54. /// Specify the latency in cpu cycles for a particular scheduling class and def
  55. /// index. -1 indicates an invalid latency. Heuristics would typically consider
  56. /// an instruction with invalid latency to have infinite latency. Also identify
  57. /// the WriteResources of this def. When the operand expands to a sequence of
  58. /// writes, this ID is the last write in the sequence.
  59. struct MCWriteLatencyEntry {
  60. int Cycles;
  61. unsigned WriteResourceID;
  62. bool operator==(const MCWriteLatencyEntry &Other) const {
  63. return Cycles == Other.Cycles && WriteResourceID == Other.WriteResourceID;
  64. }
  65. };
  66. /// Specify the number of cycles allowed after instruction issue before a
  67. /// particular use operand reads its registers. This effectively reduces the
  68. /// write's latency. Here we allow negative cycles for corner cases where
  69. /// latency increases. This rule only applies when the entry's WriteResource
  70. /// matches the write's WriteResource.
  71. ///
  72. /// MCReadAdvanceEntries are sorted first by operand index (UseIdx), then by
  73. /// WriteResourceIdx.
  74. struct MCReadAdvanceEntry {
  75. unsigned UseIdx;
  76. unsigned WriteResourceID;
  77. int Cycles;
  78. bool operator==(const MCReadAdvanceEntry &Other) const {
  79. return UseIdx == Other.UseIdx && WriteResourceID == Other.WriteResourceID
  80. && Cycles == Other.Cycles;
  81. }
  82. };
  83. /// Summarize the scheduling resources required for an instruction of a
  84. /// particular scheduling class.
  85. ///
  86. /// Defined as an aggregate struct for creating tables with initializer lists.
  87. struct MCSchedClassDesc {
  88. static const unsigned short InvalidNumMicroOps = UINT16_MAX;
  89. static const unsigned short VariantNumMicroOps = UINT16_MAX - 1;
  90. #ifndef NDEBUG
  91. const char* Name;
  92. #endif
  93. unsigned short NumMicroOps;
  94. bool BeginGroup;
  95. bool EndGroup;
  96. unsigned WriteProcResIdx; // First index into WriteProcResTable.
  97. unsigned NumWriteProcResEntries;
  98. unsigned WriteLatencyIdx; // First index into WriteLatencyTable.
  99. unsigned NumWriteLatencyEntries;
  100. unsigned ReadAdvanceIdx; // First index into ReadAdvanceTable.
  101. unsigned NumReadAdvanceEntries;
  102. bool isValid() const {
  103. return NumMicroOps != InvalidNumMicroOps;
  104. }
  105. bool isVariant() const {
  106. return NumMicroOps == VariantNumMicroOps;
  107. }
  108. };
  109. /// Machine model for scheduling, bundling, and heuristics.
  110. ///
  111. /// The machine model directly provides basic information about the
  112. /// microarchitecture to the scheduler in the form of properties. It also
  113. /// optionally refers to scheduler resource tables and itinerary
  114. /// tables. Scheduler resource tables model the latency and cost for each
  115. /// instruction type. Itinerary tables are an independent mechanism that
  116. /// provides a detailed reservation table describing each cycle of instruction
  117. /// execution. Subtargets may define any or all of the above categories of data
  118. /// depending on the type of CPU and selected scheduler.
  119. struct MCSchedModel {
  120. // IssueWidth is the maximum number of instructions that may be scheduled in
  121. // the same per-cycle group.
  122. unsigned IssueWidth;
  123. static const unsigned DefaultIssueWidth = 1;
  124. // MicroOpBufferSize is the number of micro-ops that the processor may buffer
  125. // for out-of-order execution.
  126. //
  127. // "0" means operations that are not ready in this cycle are not considered
  128. // for scheduling (they go in the pending queue). Latency is paramount. This
  129. // may be more efficient if many instructions are pending in a schedule.
  130. //
  131. // "1" means all instructions are considered for scheduling regardless of
  132. // whether they are ready in this cycle. Latency still causes issue stalls,
  133. // but we balance those stalls against other heuristics.
  134. //
  135. // "> 1" means the processor is out-of-order. This is a machine independent
  136. // estimate of highly machine specific characteristics such as the register
  137. // renaming pool and reorder buffer.
  138. unsigned MicroOpBufferSize;
  139. static const unsigned DefaultMicroOpBufferSize = 0;
  140. // LoopMicroOpBufferSize is the number of micro-ops that the processor may
  141. // buffer for optimized loop execution. More generally, this represents the
  142. // optimal number of micro-ops in a loop body. A loop may be partially
  143. // unrolled to bring the count of micro-ops in the loop body closer to this
  144. // number.
  145. unsigned LoopMicroOpBufferSize;
  146. static const unsigned DefaultLoopMicroOpBufferSize = 0;
  147. // LoadLatency is the expected latency of load instructions.
  148. //
  149. // If MinLatency >= 0, this may be overriden for individual load opcodes by
  150. // InstrItinerary OperandCycles.
  151. unsigned LoadLatency;
  152. static const unsigned DefaultLoadLatency = 4;
  153. // HighLatency is the expected latency of "very high latency" operations.
  154. // See TargetInstrInfo::isHighLatencyDef().
  155. // By default, this is set to an arbitrarily high number of cycles
  156. // likely to have some impact on scheduling heuristics.
  157. // If MinLatency >= 0, this may be overriden by InstrItinData OperandCycles.
  158. unsigned HighLatency;
  159. static const unsigned DefaultHighLatency = 10;
  160. // MispredictPenalty is the typical number of extra cycles the processor
  161. // takes to recover from a branch misprediction.
  162. unsigned MispredictPenalty;
  163. static const unsigned DefaultMispredictPenalty = 10;
  164. bool PostRAScheduler; // default value is false
  165. bool CompleteModel;
  166. unsigned ProcID;
  167. const MCProcResourceDesc *ProcResourceTable;
  168. const MCSchedClassDesc *SchedClassTable;
  169. unsigned NumProcResourceKinds;
  170. unsigned NumSchedClasses;
  171. // Instruction itinerary tables used by InstrItineraryData.
  172. friend class InstrItineraryData;
  173. const InstrItinerary *InstrItineraries;
  174. unsigned getProcessorID() const { return ProcID; }
  175. /// Does this machine model include instruction-level scheduling.
  176. bool hasInstrSchedModel() const { return SchedClassTable; }
  177. /// Return true if this machine model data for all instructions with a
  178. /// scheduling class (itinerary class or SchedRW list).
  179. bool isComplete() const { return CompleteModel; }
  180. unsigned getNumProcResourceKinds() const {
  181. return NumProcResourceKinds;
  182. }
  183. const MCProcResourceDesc *getProcResource(unsigned ProcResourceIdx) const {
  184. assert(hasInstrSchedModel() && "No scheduling machine model");
  185. assert(ProcResourceIdx < NumProcResourceKinds && "bad proc resource idx");
  186. return &ProcResourceTable[ProcResourceIdx];
  187. }
  188. const MCSchedClassDesc *getSchedClassDesc(unsigned SchedClassIdx) const {
  189. assert(hasInstrSchedModel() && "No scheduling machine model");
  190. assert(SchedClassIdx < NumSchedClasses && "bad scheduling class idx");
  191. return &SchedClassTable[SchedClassIdx];
  192. }
  193. /// Returns the default initialized model.
  194. static const MCSchedModel &GetDefaultSchedModel() { return Default; }
  195. static const MCSchedModel Default;
  196. };
  197. } // End llvm namespace
  198. #endif