1 //===-- llvm/MC/MCSchedule.h - Scheduling -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the classes used to describe a subtarget's machine model
11 // for scheduling and other instruction cost heuristics.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MC_MCSCHEDULE_H
16 #define LLVM_MC_MCSCHEDULE_H
18 #include "llvm/Support/DataTypes.h"
23 struct InstrItinerary;
25 /// Define a kind of processor resource that will be modeled by the scheduler.
26 struct MCProcResourceDesc {
30 unsigned NumUnits; // Number of resource of this kind
31 unsigned SuperIdx; // Index of the resources kind that contains this kind.
33 // Number of resources that may be buffered.
35 // Buffered resources (BufferSize > 0 || BufferSize == -1) may be consumed at
36 // some indeterminate cycle after dispatch (e.g. for instructions that may
37 // issue out-of-order). Unbuffered resources (BufferSize == 0) always consume
38 // their resource some fixed number of cycles after dispatch (e.g. for
39 // instruction interlocking that may stall the pipeline). If BufferSize==1,
40 // the latency between producer and consumer is modeled as a stall.
43 bool operator==(const MCProcResourceDesc &Other) const {
44 return NumUnits == Other.NumUnits && SuperIdx == Other.SuperIdx
45 && BufferSize == Other.BufferSize;
49 /// Identify one of the processor resource kinds consumed by a particular
50 /// scheduling class for the specified number of cycles.
51 struct MCWriteProcResEntry {
52 unsigned ProcResourceIdx;
55 bool operator==(const MCWriteProcResEntry &Other) const {
56 return ProcResourceIdx == Other.ProcResourceIdx && Cycles == Other.Cycles;
60 /// Specify the latency in cpu cycles for a particular scheduling class and def
61 /// index. -1 indicates an invalid latency. Heuristics would typically consider
62 /// an instruction with invalid latency to have infinite latency. Also identify
63 /// the WriteResources of this def. When the operand expands to a sequence of
64 /// writes, this ID is the last write in the sequence.
65 struct MCWriteLatencyEntry {
67 unsigned WriteResourceID;
69 bool operator==(const MCWriteLatencyEntry &Other) const {
70 return Cycles == Other.Cycles && WriteResourceID == Other.WriteResourceID;
74 /// Specify the number of cycles allowed after instruction issue before a
75 /// particular use operand reads its registers. This effectively reduces the
76 /// write's latency. Here we allow negative cycles for corner cases where
77 /// latency increases. This rule only applies when the entry's WriteResource
78 /// matches the write's WriteResource.
80 /// MCReadAdvanceEntries are sorted first by operand index (UseIdx), then by
82 struct MCReadAdvanceEntry {
84 unsigned WriteResourceID;
87 bool operator==(const MCReadAdvanceEntry &Other) const {
88 return UseIdx == Other.UseIdx && WriteResourceID == Other.WriteResourceID
89 && Cycles == Other.Cycles;
93 /// Summarize the scheduling resources required for an instruction of a
94 /// particular scheduling class.
96 /// Defined as an aggregate struct for creating tables with initializer lists.
97 struct MCSchedClassDesc {
98 static const unsigned short InvalidNumMicroOps = UINT16_MAX;
99 static const unsigned short VariantNumMicroOps = UINT16_MAX - 1;
104 unsigned short NumMicroOps;
107 unsigned WriteProcResIdx; // First index into WriteProcResTable.
108 unsigned NumWriteProcResEntries;
109 unsigned WriteLatencyIdx; // First index into WriteLatencyTable.
110 unsigned NumWriteLatencyEntries;
111 unsigned ReadAdvanceIdx; // First index into ReadAdvanceTable.
112 unsigned NumReadAdvanceEntries;
114 bool isValid() const {
115 return NumMicroOps != InvalidNumMicroOps;
117 bool isVariant() const {
118 return NumMicroOps == VariantNumMicroOps;
122 /// Machine model for scheduling, bundling, and heuristics.
124 /// The machine model directly provides basic information about the
125 /// microarchitecture to the scheduler in the form of properties. It also
126 /// optionally refers to scheduler resource tables and itinerary
127 /// tables. Scheduler resource tables model the latency and cost for each
128 /// instruction type. Itinerary tables are an independent mechanism that
129 /// provides a detailed reservation table describing each cycle of instruction
130 /// execution. Subtargets may define any or all of the above categories of data
131 /// depending on the type of CPU and selected scheduler.
134 static MCSchedModel DefaultSchedModel; // For unknown processors.
136 // IssueWidth is the maximum number of instructions that may be scheduled in
137 // the same per-cycle group.
139 static const unsigned DefaultIssueWidth = 1;
141 // MicroOpBufferSize is the number of micro-ops that the processor may buffer
142 // for out-of-order execution.
144 // "0" means operations that are not ready in this cycle are not considered
145 // for scheduling (they go in the pending queue). Latency is paramount. This
146 // may be more efficient if many instructions are pending in a schedule.
148 // "1" means all instructions are considered for scheduling regardless of
149 // whether they are ready in this cycle. Latency still causes issue stalls,
150 // but we balance those stalls against other heuristics.
152 // "> 1" means the processor is out-of-order. This is a machine independent
153 // estimate of highly machine specific characteristics such as the register
154 // renaming pool and reorder buffer.
155 unsigned MicroOpBufferSize;
156 static const unsigned DefaultMicroOpBufferSize = 0;
158 // LoadLatency is the expected latency of load instructions.
160 // If MinLatency >= 0, this may be overriden for individual load opcodes by
161 // InstrItinerary OperandCycles.
162 unsigned LoadLatency;
163 static const unsigned DefaultLoadLatency = 4;
165 // HighLatency is the expected latency of "very high latency" operations.
166 // See TargetInstrInfo::isHighLatencyDef().
167 // By default, this is set to an arbitrarily high number of cycles
168 // likely to have some impact on scheduling heuristics.
169 // If MinLatency >= 0, this may be overriden by InstrItinData OperandCycles.
170 unsigned HighLatency;
171 static const unsigned DefaultHighLatency = 10;
173 // MispredictPenalty is the typical number of extra cycles the processor
174 // takes to recover from a branch misprediction.
175 unsigned MispredictPenalty;
176 static const unsigned DefaultMispredictPenalty = 10;
182 const MCProcResourceDesc *ProcResourceTable;
183 const MCSchedClassDesc *SchedClassTable;
184 unsigned NumProcResourceKinds;
185 unsigned NumSchedClasses;
186 // Instruction itinerary tables used by InstrItineraryData.
187 friend class InstrItineraryData;
188 const InstrItinerary *InstrItineraries;
191 // Default's must be specified as static const literals so that tablegenerated
192 // target code can use it in static initializers. The defaults need to be
193 // initialized in this default ctor because some clients directly instantiate
194 // MCSchedModel instead of using a generated itinerary.
195 MCSchedModel(): IssueWidth(DefaultIssueWidth),
196 MicroOpBufferSize(DefaultMicroOpBufferSize),
197 LoadLatency(DefaultLoadLatency),
198 HighLatency(DefaultHighLatency),
199 MispredictPenalty(DefaultMispredictPenalty),
201 ProcID(0), ProcResourceTable(0), SchedClassTable(0),
202 NumProcResourceKinds(0), NumSchedClasses(0),
203 InstrItineraries(0) {
204 (void)NumProcResourceKinds;
205 (void)NumSchedClasses;
208 // Table-gen driven ctor.
209 MCSchedModel(unsigned iw, int mbs, unsigned ll, unsigned hl,
210 unsigned mp, bool cm, unsigned pi, const MCProcResourceDesc *pr,
211 const MCSchedClassDesc *sc, unsigned npr, unsigned nsc,
212 const InstrItinerary *ii):
213 IssueWidth(iw), MicroOpBufferSize(mbs), LoadLatency(ll), HighLatency(hl),
214 MispredictPenalty(mp), CompleteModel(cm), ProcID(pi),
215 ProcResourceTable(pr), SchedClassTable(sc), NumProcResourceKinds(npr),
216 NumSchedClasses(nsc), InstrItineraries(ii) {}
218 unsigned getProcessorID() const { return ProcID; }
220 /// Does this machine model include instruction-level scheduling.
221 bool hasInstrSchedModel() const { return SchedClassTable; }
223 /// Return true if this machine model data for all instructions with a
224 /// scheduling class (itinerary class or SchedRW list).
225 bool isComplete() const { return CompleteModel; }
227 unsigned getNumProcResourceKinds() const {
228 return NumProcResourceKinds;
231 const MCProcResourceDesc *getProcResource(unsigned ProcResourceIdx) const {
232 assert(hasInstrSchedModel() && "No scheduling machine model");
234 assert(ProcResourceIdx < NumProcResourceKinds && "bad proc resource idx");
235 return &ProcResourceTable[ProcResourceIdx];
238 const MCSchedClassDesc *getSchedClassDesc(unsigned SchedClassIdx) const {
239 assert(hasInstrSchedModel() && "No scheduling machine model");
241 assert(SchedClassIdx < NumSchedClasses && "bad scheduling class idx");
242 return &SchedClassTable[SchedClassIdx];
246 } // End llvm namespace