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