Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:14

0001 //===- llvm/MC/MCInstrItineraries.h - Scheduling ----------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file describes the structures used for instruction
0010 // itineraries, stages, and operand reads/writes.  This is used by
0011 // schedulers to determine instruction stages and latencies.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_MC_MCINSTRITINERARIES_H
0016 #define LLVM_MC_MCINSTRITINERARIES_H
0017 
0018 #include "llvm/MC/MCSchedule.h"
0019 #include <algorithm>
0020 #include <optional>
0021 
0022 namespace llvm {
0023 
0024 //===----------------------------------------------------------------------===//
0025 /// These values represent a non-pipelined step in
0026 /// the execution of an instruction.  Cycles represents the number of
0027 /// discrete time slots needed to complete the stage.  Units represent
0028 /// the choice of functional units that can be used to complete the
0029 /// stage.  Eg. IntUnit1, IntUnit2. NextCycles indicates how many
0030 /// cycles should elapse from the start of this stage to the start of
0031 /// the next stage in the itinerary. A value of -1 indicates that the
0032 /// next stage should start immediately after the current one.
0033 /// For example:
0034 ///
0035 ///   { 1, x, -1 }
0036 ///      indicates that the stage occupies FU x for 1 cycle and that
0037 ///      the next stage starts immediately after this one.
0038 ///
0039 ///   { 2, x|y, 1 }
0040 ///      indicates that the stage occupies either FU x or FU y for 2
0041 ///      consecutive cycles and that the next stage starts one cycle
0042 ///      after this stage starts. That is, the stage requirements
0043 ///      overlap in time.
0044 ///
0045 ///   { 1, x, 0 }
0046 ///      indicates that the stage occupies FU x for 1 cycle and that
0047 ///      the next stage starts in this same cycle. This can be used to
0048 ///      indicate that the instruction requires multiple stages at the
0049 ///      same time.
0050 ///
0051 /// FU reservation can be of two different kinds:
0052 ///  - FUs which instruction actually requires
0053 ///  - FUs which instruction just reserves. Reserved unit is not available for
0054 ///    execution of other instruction. However, several instructions can reserve
0055 ///    the same unit several times.
0056 /// Such two types of units reservation is used to model instruction domain
0057 /// change stalls, FUs using the same resource (e.g. same register file), etc.
0058 
0059 struct InstrStage {
0060   enum ReservationKinds {
0061     Required = 0,
0062     Reserved = 1
0063   };
0064 
0065   /// Bitmask representing a set of functional units.
0066   typedef uint64_t FuncUnits;
0067 
0068   unsigned Cycles_;  ///< Length of stage in machine cycles
0069   FuncUnits Units_;  ///< Choice of functional units
0070   int NextCycles_;   ///< Number of machine cycles to next stage
0071   ReservationKinds Kind_; ///< Kind of the FU reservation
0072 
0073   /// Returns the number of cycles the stage is occupied.
0074   unsigned getCycles() const {
0075     return Cycles_;
0076   }
0077 
0078   /// Returns the choice of FUs.
0079   FuncUnits getUnits() const {
0080     return Units_;
0081   }
0082 
0083   ReservationKinds getReservationKind() const {
0084     return Kind_;
0085   }
0086 
0087   /// Returns the number of cycles from the start of this stage to the
0088   /// start of the next stage in the itinerary
0089   unsigned getNextCycles() const {
0090     return (NextCycles_ >= 0) ? (unsigned)NextCycles_ : Cycles_;
0091   }
0092 };
0093 
0094 //===----------------------------------------------------------------------===//
0095 /// An itinerary represents the scheduling information for an instruction.
0096 /// This includes a set of stages occupied by the instruction and the pipeline
0097 /// cycle in which operands are read and written.
0098 ///
0099 struct InstrItinerary {
0100   int16_t  NumMicroOps;        ///< # of micro-ops, -1 means it's variable
0101   uint16_t FirstStage;         ///< Index of first stage in itinerary
0102   uint16_t LastStage;          ///< Index of last + 1 stage in itinerary
0103   uint16_t FirstOperandCycle;  ///< Index of first operand rd/wr
0104   uint16_t LastOperandCycle;   ///< Index of last + 1 operand rd/wr
0105 };
0106 
0107 //===----------------------------------------------------------------------===//
0108 /// Itinerary data supplied by a subtarget to be used by a target.
0109 ///
0110 class InstrItineraryData {
0111 public:
0112   MCSchedModel SchedModel =
0113       MCSchedModel::Default;               ///< Basic machine properties.
0114   const InstrStage *Stages = nullptr;      ///< Array of stages selected
0115   const unsigned *OperandCycles = nullptr; ///< Array of operand cycles selected
0116   const unsigned *Forwardings = nullptr; ///< Array of pipeline forwarding paths
0117   const InstrItinerary *Itineraries =
0118       nullptr; ///< Array of itineraries selected
0119 
0120   InstrItineraryData() = default;
0121   InstrItineraryData(const MCSchedModel &SM, const InstrStage *S,
0122                      const unsigned *OS, const unsigned *F)
0123     : SchedModel(SM), Stages(S), OperandCycles(OS), Forwardings(F),
0124       Itineraries(SchedModel.InstrItineraries) {}
0125 
0126   /// Returns true if there are no itineraries.
0127   bool isEmpty() const { return Itineraries == nullptr; }
0128 
0129   /// Returns true if the index is for the end marker itinerary.
0130   bool isEndMarker(unsigned ItinClassIndx) const {
0131     return ((Itineraries[ItinClassIndx].FirstStage == UINT16_MAX) &&
0132             (Itineraries[ItinClassIndx].LastStage == UINT16_MAX));
0133   }
0134 
0135   /// Return the first stage of the itinerary.
0136   const InstrStage *beginStage(unsigned ItinClassIndx) const {
0137     unsigned StageIdx = Itineraries[ItinClassIndx].FirstStage;
0138     return Stages + StageIdx;
0139   }
0140 
0141   /// Return the last+1 stage of the itinerary.
0142   const InstrStage *endStage(unsigned ItinClassIndx) const {
0143     unsigned StageIdx = Itineraries[ItinClassIndx].LastStage;
0144     return Stages + StageIdx;
0145   }
0146 
0147   /// Return the total stage latency of the given class.  The latency is
0148   /// the maximum completion time for any stage in the itinerary.  If no stages
0149   /// exist, it defaults to one cycle.
0150   unsigned getStageLatency(unsigned ItinClassIndx) const {
0151     // If the target doesn't provide itinerary information, use a simple
0152     // non-zero default value for all instructions.
0153     if (isEmpty())
0154       return 1;
0155 
0156     // Calculate the maximum completion time for any stage.
0157     unsigned Latency = 0, StartCycle = 0;
0158     for (const InstrStage *IS = beginStage(ItinClassIndx),
0159            *E = endStage(ItinClassIndx); IS != E; ++IS) {
0160       Latency = std::max(Latency, StartCycle + IS->getCycles());
0161       StartCycle += IS->getNextCycles();
0162     }
0163     return Latency;
0164   }
0165 
0166   /// Return the cycle for the given class and operand. Return std::nullopt if
0167   /// the information is not available for the operand.
0168   std::optional<unsigned> getOperandCycle(unsigned ItinClassIndx,
0169                                           unsigned OperandIdx) const {
0170     if (isEmpty())
0171       return std::nullopt;
0172 
0173     unsigned FirstIdx = Itineraries[ItinClassIndx].FirstOperandCycle;
0174     unsigned LastIdx = Itineraries[ItinClassIndx].LastOperandCycle;
0175     if ((FirstIdx + OperandIdx) >= LastIdx)
0176       return std::nullopt;
0177 
0178     return OperandCycles[FirstIdx + OperandIdx];
0179   }
0180 
0181   /// Return true if there is a pipeline forwarding between instructions
0182   /// of itinerary classes DefClass and UseClasses so that value produced by an
0183   /// instruction of itinerary class DefClass, operand index DefIdx can be
0184   /// bypassed when it's read by an instruction of itinerary class UseClass,
0185   /// operand index UseIdx.
0186   bool hasPipelineForwarding(unsigned DefClass, unsigned DefIdx,
0187                              unsigned UseClass, unsigned UseIdx) const {
0188     unsigned FirstDefIdx = Itineraries[DefClass].FirstOperandCycle;
0189     unsigned LastDefIdx = Itineraries[DefClass].LastOperandCycle;
0190     if ((FirstDefIdx + DefIdx) >= LastDefIdx)
0191       return false;
0192     if (Forwardings[FirstDefIdx + DefIdx] == 0)
0193       return false;
0194 
0195     unsigned FirstUseIdx = Itineraries[UseClass].FirstOperandCycle;
0196     unsigned LastUseIdx = Itineraries[UseClass].LastOperandCycle;
0197     if ((FirstUseIdx + UseIdx) >= LastUseIdx)
0198       return false;
0199 
0200     return Forwardings[FirstDefIdx + DefIdx] ==
0201       Forwardings[FirstUseIdx + UseIdx];
0202   }
0203 
0204   /// Compute and return the use operand latency of a given itinerary
0205   /// class and operand index if the value is produced by an instruction of the
0206   /// specified itinerary class and def operand index. Return std::nullopt if
0207   /// the information is not available for the operand.
0208   std::optional<unsigned> getOperandLatency(unsigned DefClass, unsigned DefIdx,
0209                                             unsigned UseClass,
0210                                             unsigned UseIdx) const {
0211     if (isEmpty())
0212       return std::nullopt;
0213 
0214     std::optional<unsigned> DefCycle = getOperandCycle(DefClass, DefIdx);
0215     std::optional<unsigned> UseCycle = getOperandCycle(UseClass, UseIdx);
0216     if (!DefCycle || !UseCycle)
0217       return std::nullopt;
0218 
0219     if (UseCycle > *DefCycle + 1)
0220       return std::nullopt;
0221 
0222     UseCycle = *DefCycle - *UseCycle + 1;
0223     if (UseCycle > 0u &&
0224         hasPipelineForwarding(DefClass, DefIdx, UseClass, UseIdx))
0225       // FIXME: This assumes one cycle benefit for every pipeline forwarding.
0226       UseCycle = *UseCycle - 1;
0227     return UseCycle;
0228   }
0229 
0230   /// Return the number of micro-ops that the given class decodes to.
0231   /// Return -1 for classes that require dynamic lookup via TargetInstrInfo.
0232   int getNumMicroOps(unsigned ItinClassIndx) const {
0233     if (isEmpty())
0234       return 1;
0235     return Itineraries[ItinClassIndx].NumMicroOps;
0236   }
0237 };
0238 
0239 } // end namespace llvm
0240 
0241 #endif // LLVM_MC_MCINSTRITINERARIES_H