LLVM 22.0.0git
TargetSchedule.h
Go to the documentation of this file.
1//===- llvm/CodeGen/TargetSchedule.h - Sched Machine Model ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a wrapper around MCSchedModel that allows the interface to
10// benefit from information currently only available in TargetInstrInfo.
11// Ideally, the scheduling interface would be fully defined in the MC layer.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_TARGETSCHEDULE_H
16#define LLVM_CODEGEN_TARGETSCHEDULE_H
17
20#include "llvm/Config/llvm-config.h"
22#include "llvm/MC/MCSchedule.h"
24
25namespace llvm {
26
27class MachineInstr;
28class TargetInstrInfo;
29
30/// Provide an instruction scheduling machine model to CodeGen passes.
32 // For efficiency, hold a copy of the statically defined MCSchedModel for this
33 // processor.
34 MCSchedModel SchedModel;
35 InstrItineraryData InstrItins;
36 const TargetSubtargetInfo *STI = nullptr;
37 const TargetInstrInfo *TII = nullptr;
38
39 SmallVector<unsigned, 16> ResourceFactors;
40
41 // Multiply to normalize microops to resource units.
42 unsigned MicroOpFactor = 0;
43
44 // Resource units per cycle. Latency normalization factor.
45 unsigned ResourceLCM = 0;
46
47 unsigned computeInstrLatency(const MCSchedClassDesc &SCDesc) const;
48
49 // EnableSchedModel and EnableSchedItins are used to control whether or not to
50 // use the Target's {SchedMachineModel, InstrItins} for hardware infor based
51 // Scheduling decisions. If both are enabled, as is the default, preference
52 // will be given to one based on the API implementation. By disabling one, we
53 // can force preference of the other. By disabling both, we will throw away
54 // any target specific hardware details for scheduling decisions, and fall
55 // into things that provide generic info such as defaultDefLatency.
56 bool EnableSchedModel = true;
57 bool EnableSchedItins = true;
58
59public:
61
62 /// Initialize the machine model for instruction scheduling.
63 ///
64 /// The machine model API keeps a copy of the top-level MCSchedModel table
65 /// indices and may query TargetSubtargetInfo and TargetInstrInfo to resolve
66 /// dynamic properties.
67 LLVM_ABI void init(const TargetSubtargetInfo *TSInfo,
68 bool EnableSModel = true, bool EnableSItins = true);
69
70 /// Return the MCSchedClassDesc for this instruction.
72 resolveSchedClass(const MachineInstr *MI) const;
73
74 /// TargetSubtargetInfo getter.
75 const TargetSubtargetInfo *getSubtargetInfo() const { return STI; }
76
77 /// TargetInstrInfo getter.
78 const TargetInstrInfo *getInstrInfo() const { return TII; }
79
80 /// Return true if this machine model includes an instruction-level
81 /// scheduling model.
82 ///
83 /// This is more detailed than the course grain IssueWidth and default
84 /// latency properties, but separate from the per-cycle itinerary data.
85 LLVM_ABI bool hasInstrSchedModel() const;
86
87 const MCSchedModel *getMCSchedModel() const { return &SchedModel; }
88
89 /// Return true if this machine model includes cycle-to-cycle itinerary
90 /// data.
91 ///
92 /// This models scheduling at each stage in the processor pipeline.
93 LLVM_ABI bool hasInstrItineraries() const;
94
97 return &InstrItins;
98 return nullptr;
99 }
100
101 /// Return true if this machine model includes an instruction-level
102 /// scheduling model or cycle-to-cycle itinerary data.
105 }
106 LLVM_ABI bool enableIntervals() const;
107 /// Identify the processor corresponding to the current subtarget.
108 unsigned getProcessorID() const { return SchedModel.getProcessorID(); }
109
110 /// Maximum number of micro-ops that may be scheduled per cycle.
111 unsigned getIssueWidth() const { return SchedModel.IssueWidth; }
112
113 /// Return true if new group must begin.
115 const MCSchedClassDesc *SC = nullptr) const;
116 /// Return true if current group must end.
118 const MCSchedClassDesc *SC = nullptr) const;
119
120 /// Return the number of issue slots required for this MI.
121 LLVM_ABI unsigned getNumMicroOps(const MachineInstr *MI,
122 const MCSchedClassDesc *SC = nullptr) const;
123
124 /// Get the number of kinds of resources for this target.
125 unsigned getNumProcResourceKinds() const {
126 return SchedModel.getNumProcResourceKinds();
127 }
128
129 /// Get a processor resource by ID for convenience.
130 const MCProcResourceDesc *getProcResource(unsigned PIdx) const {
131 return SchedModel.getProcResource(PIdx);
132 }
133
134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135 const char *getResourceName(unsigned PIdx) const {
136 if (!PIdx)
137 return "MOps";
138 return SchedModel.getProcResource(PIdx)->Name;
139 }
140#endif
141
143
144 // Get an iterator into the processor resources consumed by this
145 // scheduling class.
147 // The subtarget holds a single resource table for all processors.
148 return STI->getWriteProcResBegin(SC);
149 }
151 return STI->getWriteProcResEnd(SC);
152 }
153
154 /// Multiply the number of units consumed for a resource by this factor
155 /// to normalize it relative to other resources.
156 unsigned getResourceFactor(unsigned ResIdx) const {
157 return ResourceFactors[ResIdx];
158 }
159
160 /// Multiply number of micro-ops by this factor to normalize it
161 /// relative to other resources.
162 unsigned getMicroOpFactor() const {
163 return MicroOpFactor;
164 }
165
166 /// Multiply cycle count by this factor to normalize it relative to
167 /// other resources. This is the number of resource units per cycle.
168 unsigned getLatencyFactor() const {
169 return ResourceLCM;
170 }
171
172 /// Number of micro-ops that may be buffered for OOO execution.
173 unsigned getMicroOpBufferSize() const { return SchedModel.MicroOpBufferSize; }
174
175 /// Number of resource units that may be buffered for OOO execution.
176 /// \return The buffer size in resource units or -1 for unlimited.
177 int getResourceBufferSize(unsigned PIdx) const {
178 return SchedModel.getProcResource(PIdx)->BufferSize;
179 }
180
181 /// Compute operand latency based on the available machine model.
182 ///
183 /// Compute and return the latency of the given data dependent def and use
184 /// when the operand indices are already known. UseMI may be NULL for an
185 /// unknown user.
187 unsigned DefOperIdx,
188 const MachineInstr *UseMI,
189 unsigned UseOperIdx) const;
190
191 /// Compute the instruction latency based on the available machine
192 /// model.
193 ///
194 /// Compute and return the expected latency of this instruction independent of
195 /// a particular use. computeOperandLatency is the preferred API, but this is
196 /// occasionally useful to help estimate instruction cost.
197 ///
198 /// If UseDefaultDefLatency is false and no new machine sched model is
199 /// present this method falls back to TII->getInstrLatency with an empty
200 /// instruction itinerary (this is so we preserve the previous behavior of the
201 /// if converter after moving it to TargetSchedModel).
202 LLVM_ABI unsigned computeInstrLatency(const MachineInstr *MI,
203 bool UseDefaultDefLatency = true) const;
204 LLVM_ABI unsigned computeInstrLatency(const MCInst &Inst) const;
205 LLVM_ABI unsigned computeInstrLatency(unsigned Opcode) const;
206
207 /// Output dependency latency of a pair of defs of the same register.
208 ///
209 /// This is typically one cycle.
211 unsigned DefOperIdx,
212 const MachineInstr *DepMI) const;
213
214 /// Compute the reciprocal throughput of the given instruction.
216 LLVM_ABI double computeReciprocalThroughput(const MCInst &MI) const;
217 LLVM_ABI double computeReciprocalThroughput(unsigned Opcode) const;
218};
219
220} // end namespace llvm
221
222#endif // LLVM_CODEGEN_TARGETSCHEDULE_H
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
#define LLVM_ABI
Definition: Compiler.h:213
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file defines the SmallVector class.
Itinerary data supplied by a subtarget to be used by a target.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
const MCWriteProcResEntry * getWriteProcResEnd(const MCSchedClassDesc *SC) const
const MCWriteProcResEntry * getWriteProcResBegin(const MCSchedClassDesc *SC) const
Return an iterator at the first process resource consumed by the given scheduling class.
Representation of each machine instruction.
Definition: MachineInstr.h:72
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
TargetInstrInfo - Interface to description of machine instruction set.
Provide an instruction scheduling machine model to CodeGen passes.
const char * getResourceName(unsigned PIdx) const
const MCSchedModel * getMCSchedModel() const
const TargetInstrInfo * getInstrInfo() const
TargetInstrInfo getter.
LLVM_ABI bool mustEndGroup(const MachineInstr *MI, const MCSchedClassDesc *SC=nullptr) const
Return true if current group must end.
unsigned getIssueWidth() const
Maximum number of micro-ops that may be scheduled per cycle.
unsigned getMicroOpFactor() const
Multiply number of micro-ops by this factor to normalize it relative to other resources.
ProcResIter getWriteProcResEnd(const MCSchedClassDesc *SC) const
int getResourceBufferSize(unsigned PIdx) const
Number of resource units that may be buffered for OOO execution.
LLVM_ABI bool hasInstrSchedModel() const
Return true if this machine model includes an instruction-level scheduling model.
LLVM_ABI unsigned computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx, const MachineInstr *DepMI) const
Output dependency latency of a pair of defs of the same register.
LLVM_ABI bool mustBeginGroup(const MachineInstr *MI, const MCSchedClassDesc *SC=nullptr) const
Return true if new group must begin.
unsigned getLatencyFactor() const
Multiply cycle count by this factor to normalize it relative to other resources.
unsigned getResourceFactor(unsigned ResIdx) const
Multiply the number of units consumed for a resource by this factor to normalize it relative to other...
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
LLVM_ABI const MCSchedClassDesc * resolveSchedClass(const MachineInstr *MI) const
Return the MCSchedClassDesc for this instruction.
LLVM_ABI unsigned computeOperandLatency(const MachineInstr *DefMI, unsigned DefOperIdx, const MachineInstr *UseMI, unsigned UseOperIdx) const
Compute operand latency based on the available machine model.
bool hasInstrSchedModelOrItineraries() const
Return true if this machine model includes an instruction-level scheduling model or cycle-to-cycle it...
unsigned getMicroOpBufferSize() const
Number of micro-ops that may be buffered for OOO execution.
LLVM_ABI double computeReciprocalThroughput(const MachineInstr *MI) const
Compute the reciprocal throughput of the given instruction.
LLVM_ABI unsigned getNumMicroOps(const MachineInstr *MI, const MCSchedClassDesc *SC=nullptr) const
Return the number of issue slots required for this MI.
const MCProcResourceDesc * getProcResource(unsigned PIdx) const
Get a processor resource by ID for convenience.
unsigned getNumProcResourceKinds() const
Get the number of kinds of resources for this target.
unsigned getProcessorID() const
Identify the processor corresponding to the current subtarget.
const InstrItineraryData * getInstrItineraries() const
const TargetSubtargetInfo * getSubtargetInfo() const
TargetSubtargetInfo getter.
LLVM_ABI bool enableIntervals() const
ProcResIter getWriteProcResBegin(const MCSchedClassDesc *SC) const
LLVM_ABI bool hasInstrItineraries() const
Return true if this machine model includes cycle-to-cycle itinerary data.
TargetSubtargetInfo - Generic base class for all target subtargets.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Default
The result values are uniform if and only if all operands are uniform.
Define a kind of processor resource that will be modeled by the scheduler.
Definition: MCSchedule.h:36
Summarize the scheduling resources required for an instruction of a particular scheduling class.
Definition: MCSchedule.h:123
Machine model for scheduling, bundling, and heuristics.
Definition: MCSchedule.h:258
unsigned getProcessorID() const
Definition: MCSchedule.h:337
unsigned getNumProcResourceKinds() const
Definition: MCSchedule.h:355
unsigned MicroOpBufferSize
Definition: MCSchedule.h:287
unsigned IssueWidth
Definition: MCSchedule.h:270
const MCProcResourceDesc * getProcResource(unsigned ProcResourceIdx) const
Definition: MCSchedule.h:359
Identify one of the processor resource kinds consumed by a particular scheduling class for the specif...
Definition: MCSchedule.h:68