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