TargetSchedule.td 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //===- TargetSchedule.td - Target Independent Scheduling ---*- tablegen -*-===//
  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 target-independent scheduling interfaces which should
  11. // be implemented by each target which is using TableGen based scheduling.
  12. //
  13. // The SchedMachineModel is defined by subtargets for three categories of data:
  14. // 1. Basic properties for coarse grained instruction cost model.
  15. // 2. Scheduler Read/Write resources for simple per-opcode cost model.
  16. // 3. Instruction itineraties for detailed reservation tables.
  17. //
  18. // (1) Basic properties are defined by the SchedMachineModel
  19. // class. Target hooks allow subtargets to associate opcodes with
  20. // those properties.
  21. //
  22. // (2) A per-operand machine model can be implemented in any
  23. // combination of the following ways:
  24. //
  25. // A. Associate per-operand SchedReadWrite types with Instructions by
  26. // modifying the Instruction definition to inherit from Sched. For
  27. // each subtarget, define WriteRes and ReadAdvance to associate
  28. // processor resources and latency with each SchedReadWrite type.
  29. //
  30. // B. In each instruction definition, name an ItineraryClass. For each
  31. // subtarget, define ItinRW entries to map ItineraryClass to
  32. // per-operand SchedReadWrite types. Unlike method A, these types may
  33. // be subtarget specific and can be directly associated with resources
  34. // by defining SchedWriteRes and SchedReadAdvance.
  35. //
  36. // C. In the subtarget, map SchedReadWrite types to specific
  37. // opcodes. This overrides any SchedReadWrite types or
  38. // ItineraryClasses defined by the Instruction. As in method B, the
  39. // subtarget can directly associate resources with SchedReadWrite
  40. // types by defining SchedWriteRes and SchedReadAdvance.
  41. //
  42. // D. In either the target or subtarget, define SchedWriteVariant or
  43. // SchedReadVariant to map one SchedReadWrite type onto another
  44. // sequence of SchedReadWrite types. This allows dynamic selection of
  45. // an instruction's machine model via custom C++ code. It also allows
  46. // a machine-independent SchedReadWrite type to map to a sequence of
  47. // machine-dependent types.
  48. //
  49. // (3) A per-pipeline-stage machine model can be implemented by providing
  50. // Itineraries in addition to mapping instructions to ItineraryClasses.
  51. //===----------------------------------------------------------------------===//
  52. include "llvm/Target/TargetItinerary.td"
  53. class Instruction; // Forward def
  54. // DAG operator that interprets the DAG args as Instruction defs.
  55. def instrs;
  56. // DAG operator that interprets each DAG arg as a regex pattern for
  57. // matching Instruction opcode names.
  58. // The regex must match the beginning of the opcode (as in Python re.match).
  59. // To avoid matching prefixes, append '$' to the pattern.
  60. def instregex;
  61. // Define the SchedMachineModel and provide basic properties for
  62. // coarse grained instruction cost model. Default values for the
  63. // properties are defined in MCSchedModel. A value of "-1" in the
  64. // target description's SchedMachineModel indicates that the property
  65. // is not overriden by the target.
  66. //
  67. // Target hooks allow subtargets to associate LoadLatency and
  68. // HighLatency with groups of opcodes.
  69. //
  70. // See MCSchedule.h for detailed comments.
  71. class SchedMachineModel {
  72. int IssueWidth = -1; // Max micro-ops that may be scheduled per cycle.
  73. int MinLatency = -1; // Determines which instructions are allowed in a group.
  74. // (-1) inorder (0) ooo, (1): inorder +var latencies.
  75. int MicroOpBufferSize = -1; // Max micro-ops that can be buffered.
  76. int LoopMicroOpBufferSize = -1; // Max micro-ops that can be buffered for
  77. // optimized loop dispatch/execution.
  78. int LoadLatency = -1; // Cycles for loads to access the cache.
  79. int HighLatency = -1; // Approximation of cycles for "high latency" ops.
  80. int MispredictPenalty = -1; // Extra cycles for a mispredicted branch.
  81. // Per-cycle resources tables.
  82. ProcessorItineraries Itineraries = NoItineraries;
  83. bit PostRAScheduler = 0; // Enable Post RegAlloc Scheduler pass.
  84. // Subtargets that define a model for only a subset of instructions
  85. // that have a scheduling class (itinerary class or SchedRW list)
  86. // and may actually be generated for that subtarget must clear this
  87. // bit. Otherwise, the scheduler considers an unmodelled opcode to
  88. // be an error. This should only be set during initial bringup,
  89. // or there will be no way to catch simple errors in the model
  90. // resulting from changes to the instruction definitions.
  91. bit CompleteModel = 1;
  92. bit NoModel = 0; // Special tag to indicate missing machine model.
  93. }
  94. def NoSchedModel : SchedMachineModel {
  95. let NoModel = 1;
  96. }
  97. // Define a kind of processor resource that may be common across
  98. // similar subtargets.
  99. class ProcResourceKind;
  100. // Define a number of interchangeable processor resources. NumUnits
  101. // determines the throughput of instructions that require the resource.
  102. //
  103. // An optional Super resource may be given to model these resources as
  104. // a subset of the more general super resources. Using one of these
  105. // resources implies using one of the super resoruces.
  106. //
  107. // ProcResourceUnits normally model a few buffered resources within an
  108. // out-of-order engine. Buffered resources may be held for multiple
  109. // clock cycles, but the scheduler does not pin them to a particular
  110. // clock cycle relative to instruction dispatch. Setting BufferSize=0
  111. // changes this to an in-order issue/dispatch resource. In this case,
  112. // the scheduler counts down from the cycle that the instruction
  113. // issues in-order, forcing a stall whenever a subsequent instruction
  114. // requires the same resource until the number of ResourceCyles
  115. // specified in WriteRes expire. Setting BufferSize=1 changes this to
  116. // an in-order latency resource. In this case, the scheduler models
  117. // producer/consumer stalls between instructions that use the
  118. // resource.
  119. //
  120. // Examples (all assume an out-of-order engine):
  121. //
  122. // Use BufferSize = -1 for "issue ports" fed by a unified reservation
  123. // station. Here the size of the reservation station is modeled by
  124. // MicroOpBufferSize, which should be the minimum size of either the
  125. // register rename pool, unified reservation station, or reorder
  126. // buffer.
  127. //
  128. // Use BufferSize = 0 for resources that force "dispatch/issue
  129. // groups". (Different processors define dispath/issue
  130. // differently. Here we refer to stage between decoding into micro-ops
  131. // and moving them into a reservation station.) Normally NumMicroOps
  132. // is sufficient to limit dispatch/issue groups. However, some
  133. // processors can form groups of with only certain combinitions of
  134. // instruction types. e.g. POWER7.
  135. //
  136. // Use BufferSize = 1 for in-order execution units. This is used for
  137. // an in-order pipeline within an out-of-order core where scheduling
  138. // dependent operations back-to-back is guaranteed to cause a
  139. // bubble. e.g. Cortex-a9 floating-point.
  140. //
  141. // Use BufferSize > 1 for out-of-order executions units with a
  142. // separate reservation station. This simply models the size of the
  143. // reservation station.
  144. //
  145. // To model both dispatch/issue groups and in-order execution units,
  146. // create two types of units, one with BufferSize=0 and one with
  147. // BufferSize=1.
  148. //
  149. // SchedModel ties these units to a processor for any stand-alone defs
  150. // of this class. Instances of subclass ProcResource will be automatically
  151. // attached to a processor, so SchedModel is not needed.
  152. class ProcResourceUnits<ProcResourceKind kind, int num> {
  153. ProcResourceKind Kind = kind;
  154. int NumUnits = num;
  155. ProcResourceKind Super = ?;
  156. int BufferSize = -1;
  157. SchedMachineModel SchedModel = ?;
  158. }
  159. // EponymousProcResourceKind helps implement ProcResourceUnits by
  160. // allowing a ProcResourceUnits definition to reference itself. It
  161. // should not be referenced anywhere else.
  162. def EponymousProcResourceKind : ProcResourceKind;
  163. // Subtargets typically define processor resource kind and number of
  164. // units in one place.
  165. class ProcResource<int num> : ProcResourceKind,
  166. ProcResourceUnits<EponymousProcResourceKind, num>;
  167. class ProcResGroup<list<ProcResource> resources> : ProcResourceKind {
  168. list<ProcResource> Resources = resources;
  169. SchedMachineModel SchedModel = ?;
  170. int BufferSize = -1;
  171. }
  172. // A target architecture may define SchedReadWrite types and associate
  173. // them with instruction operands.
  174. class SchedReadWrite;
  175. // List the per-operand types that map to the machine model of an
  176. // instruction. One SchedWrite type must be listed for each explicit
  177. // def operand in order. Additional SchedWrite types may optionally be
  178. // listed for implicit def operands. SchedRead types may optionally
  179. // be listed for use operands in order. The order of defs relative to
  180. // uses is insignificant. This way, the same SchedReadWrite list may
  181. // be used for multiple forms of an operation. For example, a
  182. // two-address instruction could have two tied operands or single
  183. // operand that both reads and writes a reg. In both cases we have a
  184. // single SchedWrite and single SchedRead in any order.
  185. class Sched<list<SchedReadWrite> schedrw> {
  186. list<SchedReadWrite> SchedRW = schedrw;
  187. }
  188. // Define a scheduler resource associated with a def operand.
  189. class SchedWrite : SchedReadWrite;
  190. def NoWrite : SchedWrite;
  191. // Define a scheduler resource associated with a use operand.
  192. class SchedRead : SchedReadWrite;
  193. // Define a SchedWrite that is modeled as a sequence of other
  194. // SchedWrites with additive latency. This allows a single operand to
  195. // be mapped the resources composed from a set of previously defined
  196. // SchedWrites.
  197. //
  198. // If the final write in this sequence is a SchedWriteVariant marked
  199. // Variadic, then the list of prior writes are distributed across all
  200. // operands after resolving the predicate for the final write.
  201. //
  202. // SchedModel silences warnings but is ignored.
  203. class WriteSequence<list<SchedWrite> writes, int rep = 1> : SchedWrite {
  204. list<SchedWrite> Writes = writes;
  205. int Repeat = rep;
  206. SchedMachineModel SchedModel = ?;
  207. }
  208. // Define values common to WriteRes and SchedWriteRes.
  209. //
  210. // SchedModel ties these resources to a processor.
  211. class ProcWriteResources<list<ProcResourceKind> resources> {
  212. list<ProcResourceKind> ProcResources = resources;
  213. list<int> ResourceCycles = [];
  214. int Latency = 1;
  215. int NumMicroOps = 1;
  216. bit BeginGroup = 0;
  217. bit EndGroup = 0;
  218. // Allow a processor to mark some scheduling classes as unsupported
  219. // for stronger verification.
  220. bit Unsupported = 0;
  221. SchedMachineModel SchedModel = ?;
  222. }
  223. // Define the resources and latency of a SchedWrite. This will be used
  224. // directly by targets that have no itinerary classes. In this case,
  225. // SchedWrite is defined by the target, while WriteResources is
  226. // defined by the subtarget, and maps the SchedWrite to processor
  227. // resources.
  228. //
  229. // If a target already has itinerary classes, SchedWriteResources can
  230. // be used instead to define subtarget specific SchedWrites and map
  231. // them to processor resources in one place. Then ItinRW can map
  232. // itinerary classes to the subtarget's SchedWrites.
  233. //
  234. // ProcResources indicates the set of resources consumed by the write.
  235. // Optionally, ResourceCycles indicates the number of cycles the
  236. // resource is consumed. Each ResourceCycles item is paired with the
  237. // ProcResource item at the same position in its list. Since
  238. // ResourceCycles are rarely specialized, the list may be
  239. // incomplete. By default, resources are consumed for a single cycle,
  240. // regardless of latency, which models a fully pipelined processing
  241. // unit. A value of 0 for ResourceCycles means that the resource must
  242. // be available but is not consumed, which is only relevant for
  243. // unbuffered resources.
  244. //
  245. // By default, each SchedWrite takes one micro-op, which is counted
  246. // against the processor's IssueWidth limit. If an instruction can
  247. // write multiple registers with a single micro-op, the subtarget
  248. // should define one of the writes to be zero micro-ops. If a
  249. // subtarget requires multiple micro-ops to write a single result, it
  250. // should either override the write's NumMicroOps to be greater than 1
  251. // or require additional writes. Extra writes can be required either
  252. // by defining a WriteSequence, or simply listing extra writes in the
  253. // instruction's list of writers beyond the number of "def"
  254. // operands. The scheduler assumes that all micro-ops must be
  255. // dispatched in the same cycle. These micro-ops may be required to
  256. // begin or end the current dispatch group.
  257. class WriteRes<SchedWrite write, list<ProcResourceKind> resources>
  258. : ProcWriteResources<resources> {
  259. SchedWrite WriteType = write;
  260. }
  261. // Directly name a set of WriteResources defining a new SchedWrite
  262. // type at the same time. This class is unaware of its SchedModel so
  263. // must be referenced by InstRW or ItinRW.
  264. class SchedWriteRes<list<ProcResourceKind> resources> : SchedWrite,
  265. ProcWriteResources<resources>;
  266. // Define values common to ReadAdvance and SchedReadAdvance.
  267. //
  268. // SchedModel ties these resources to a processor.
  269. class ProcReadAdvance<int cycles, list<SchedWrite> writes = []> {
  270. int Cycles = cycles;
  271. list<SchedWrite> ValidWrites = writes;
  272. // Allow a processor to mark some scheduling classes as unsupported
  273. // for stronger verification.
  274. bit Unsupported = 0;
  275. SchedMachineModel SchedModel = ?;
  276. }
  277. // A processor may define a ReadAdvance associated with a SchedRead
  278. // to reduce latency of a prior write by N cycles. A negative advance
  279. // effectively increases latency, which may be used for cross-domain
  280. // stalls.
  281. //
  282. // A ReadAdvance may be associated with a list of SchedWrites
  283. // to implement pipeline bypass. The Writes list may be empty to
  284. // indicate operands that are always read this number of Cycles later
  285. // than a normal register read, allowing the read's parent instruction
  286. // to issue earlier relative to the writer.
  287. class ReadAdvance<SchedRead read, int cycles, list<SchedWrite> writes = []>
  288. : ProcReadAdvance<cycles, writes> {
  289. SchedRead ReadType = read;
  290. }
  291. // Directly associate a new SchedRead type with a delay and optional
  292. // pipeline bypess. For use with InstRW or ItinRW.
  293. class SchedReadAdvance<int cycles, list<SchedWrite> writes = []> : SchedRead,
  294. ProcReadAdvance<cycles, writes>;
  295. // Define SchedRead defaults. Reads seldom need special treatment.
  296. def ReadDefault : SchedRead;
  297. def NoReadAdvance : SchedReadAdvance<0>;
  298. // Define shared code that will be in the same scope as all
  299. // SchedPredicates. Available variables are:
  300. // (const MachineInstr *MI, const TargetSchedModel *SchedModel)
  301. class PredicateProlog<code c> {
  302. code Code = c;
  303. }
  304. // Define a predicate to determine which SchedVariant applies to a
  305. // particular MachineInstr. The code snippet is used as an
  306. // if-statement's expression. Available variables are MI, SchedModel,
  307. // and anything defined in a PredicateProlog.
  308. //
  309. // SchedModel silences warnings but is ignored.
  310. class SchedPredicate<code pred> {
  311. SchedMachineModel SchedModel = ?;
  312. code Predicate = pred;
  313. }
  314. def NoSchedPred : SchedPredicate<[{true}]>;
  315. // Associate a predicate with a list of SchedReadWrites. By default,
  316. // the selected SchedReadWrites are still associated with a single
  317. // operand and assumed to execute sequentially with additive
  318. // latency. However, if the parent SchedWriteVariant or
  319. // SchedReadVariant is marked "Variadic", then each Selected
  320. // SchedReadWrite is mapped in place to the instruction's variadic
  321. // operands. In this case, latency is not additive. If the current Variant
  322. // is already part of a Sequence, then that entire chain leading up to
  323. // the Variant is distributed over the variadic operands.
  324. class SchedVar<SchedPredicate pred, list<SchedReadWrite> selected> {
  325. SchedPredicate Predicate = pred;
  326. list<SchedReadWrite> Selected = selected;
  327. }
  328. // SchedModel silences warnings but is ignored.
  329. class SchedVariant<list<SchedVar> variants> {
  330. list<SchedVar> Variants = variants;
  331. bit Variadic = 0;
  332. SchedMachineModel SchedModel = ?;
  333. }
  334. // A SchedWriteVariant is a single SchedWrite type that maps to a list
  335. // of SchedWrite types under the conditions defined by its predicates.
  336. //
  337. // A Variadic write is expanded to cover multiple "def" operands. The
  338. // SchedVariant's Expansion list is then interpreted as one write
  339. // per-operand instead of the usual sequential writes feeding a single
  340. // operand.
  341. class SchedWriteVariant<list<SchedVar> variants> : SchedWrite,
  342. SchedVariant<variants> {
  343. }
  344. // A SchedReadVariant is a single SchedRead type that maps to a list
  345. // of SchedRead types under the conditions defined by its predicates.
  346. //
  347. // A Variadic write is expanded to cover multiple "readsReg" operands as
  348. // explained above.
  349. class SchedReadVariant<list<SchedVar> variants> : SchedRead,
  350. SchedVariant<variants> {
  351. }
  352. // Map a set of opcodes to a list of SchedReadWrite types. This allows
  353. // the subtarget to easily override specific operations.
  354. //
  355. // SchedModel ties this opcode mapping to a processor.
  356. class InstRW<list<SchedReadWrite> rw, dag instrlist> {
  357. list<SchedReadWrite> OperandReadWrites = rw;
  358. dag Instrs = instrlist;
  359. SchedMachineModel SchedModel = ?;
  360. }
  361. // Map a set of itinerary classes to SchedReadWrite resources. This is
  362. // used to bootstrap a target (e.g. ARM) when itineraries already
  363. // exist and changing InstrInfo is undesirable.
  364. //
  365. // SchedModel ties this ItineraryClass mapping to a processor.
  366. class ItinRW<list<SchedReadWrite> rw, list<InstrItinClass> iic> {
  367. list<InstrItinClass> MatchedItinClasses = iic;
  368. list<SchedReadWrite> OperandReadWrites = rw;
  369. SchedMachineModel SchedModel = ?;
  370. }
  371. // Alias a target-defined SchedReadWrite to a processor specific
  372. // SchedReadWrite. This allows a subtarget to easily map a
  373. // SchedReadWrite type onto a WriteSequence, SchedWriteVariant, or
  374. // SchedReadVariant.
  375. //
  376. // SchedModel will usually be provided by surrounding let statement
  377. // and ties this SchedAlias mapping to a processor.
  378. class SchedAlias<SchedReadWrite match, SchedReadWrite alias> {
  379. SchedReadWrite MatchRW = match;
  380. SchedReadWrite AliasRW = alias;
  381. SchedMachineModel SchedModel = ?;
  382. }