xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGen/TargetSchedule.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/MC/MCInstrItineraries.h"
22 #include "llvm/MC/MCSchedule.h"
23 #include "llvm/Support/Compiler.h"
24 
25 namespace llvm {
26 
27 class MachineInstr;
28 class TargetInstrInfo;
29 
30 /// Provide an instruction scheduling machine model to CodeGen passes.
31 class TargetSchedModel {
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 
59 public:
TargetSchedModel()60   TargetSchedModel() : SchedModel(MCSchedModel::Default) {}
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.
71   LLVM_ABI const MCSchedClassDesc *
72   resolveSchedClass(const MachineInstr *MI) const;
73 
74   /// TargetSubtargetInfo getter.
getSubtargetInfo()75   const TargetSubtargetInfo *getSubtargetInfo() const { return STI; }
76 
77   /// TargetInstrInfo getter.
getInstrInfo()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 
getMCSchedModel()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 
getInstrItineraries()95   const InstrItineraryData *getInstrItineraries() const {
96     if (hasInstrItineraries())
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.
hasInstrSchedModelOrItineraries()103   bool hasInstrSchedModelOrItineraries() const {
104     return hasInstrSchedModel() || hasInstrItineraries();
105   }
106   LLVM_ABI bool enableIntervals() const;
107   /// Identify the processor corresponding to the current subtarget.
getProcessorID()108   unsigned getProcessorID() const { return SchedModel.getProcessorID(); }
109 
110   /// Maximum number of micro-ops that may be scheduled per cycle.
getIssueWidth()111   unsigned getIssueWidth() const { return SchedModel.IssueWidth; }
112 
113   /// Return true if new group must begin.
114   LLVM_ABI bool mustBeginGroup(const MachineInstr *MI,
115                                const MCSchedClassDesc *SC = nullptr) const;
116   /// Return true if current group must end.
117   LLVM_ABI bool mustEndGroup(const MachineInstr *MI,
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.
getNumProcResourceKinds()125   unsigned getNumProcResourceKinds() const {
126     return SchedModel.getNumProcResourceKinds();
127   }
128 
129   /// Get a processor resource by ID for convenience.
getProcResource(unsigned PIdx)130   const MCProcResourceDesc *getProcResource(unsigned PIdx) const {
131     return SchedModel.getProcResource(PIdx);
132   }
133 
134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
getResourceName(unsigned PIdx)135   const char *getResourceName(unsigned PIdx) const {
136     if (!PIdx)
137       return "MOps";
138     return SchedModel.getProcResource(PIdx)->Name;
139   }
140 #endif
141 
142   using ProcResIter = const MCWriteProcResEntry *;
143 
144   // Get an iterator into the processor resources consumed by this
145   // scheduling class.
getWriteProcResBegin(const MCSchedClassDesc * SC)146   ProcResIter getWriteProcResBegin(const MCSchedClassDesc *SC) const {
147     // The subtarget holds a single resource table for all processors.
148     return STI->getWriteProcResBegin(SC);
149   }
getWriteProcResEnd(const MCSchedClassDesc * SC)150   ProcResIter getWriteProcResEnd(const MCSchedClassDesc *SC) const {
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.
getResourceFactor(unsigned ResIdx)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.
getMicroOpFactor()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.
getLatencyFactor()168   unsigned getLatencyFactor() const {
169     return ResourceLCM;
170   }
171 
172   /// Number of micro-ops that may be buffered for OOO execution.
getMicroOpBufferSize()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.
getResourceBufferSize(unsigned PIdx)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.
186   LLVM_ABI unsigned computeOperandLatency(const MachineInstr *DefMI,
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.
210   LLVM_ABI unsigned computeOutputLatency(const MachineInstr *DefMI,
211                                          unsigned DefOperIdx,
212                                          const MachineInstr *DepMI) const;
213 
214   /// Compute the reciprocal throughput of the given instruction.
215   LLVM_ABI double computeReciprocalThroughput(const MachineInstr *MI) const;
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
223