LLVM API Documentation

MCInstrItineraries.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCInstrItineraries.h - Scheduling ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file describes the structures used for instruction
00011 // itineraries, stages, and operand reads/writes.  This is used by
00012 // schedulers to determine instruction stages and latencies.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_MC_MCINSTRITINERARIES_H
00017 #define LLVM_MC_MCINSTRITINERARIES_H
00018 
00019 #include "llvm/MC/MCSchedule.h"
00020 #include <algorithm>
00021 
00022 namespace llvm {
00023 
00024 //===----------------------------------------------------------------------===//
00025 /// Instruction stage - These values represent a non-pipelined step in
00026 /// the execution of an instruction.  Cycles represents the number of
00027 /// discrete time slots needed to complete the stage.  Units represent
00028 /// the choice of functional units that can be used to complete the
00029 /// stage.  Eg. IntUnit1, IntUnit2. NextCycles indicates how many
00030 /// cycles should elapse from the start of this stage to the start of
00031 /// the next stage in the itinerary. A value of -1 indicates that the
00032 /// next stage should start immediately after the current one.
00033 /// For example:
00034 ///
00035 ///   { 1, x, -1 }
00036 ///      indicates that the stage occupies FU x for 1 cycle and that
00037 ///      the next stage starts immediately after this one.
00038 ///
00039 ///   { 2, x|y, 1 }
00040 ///      indicates that the stage occupies either FU x or FU y for 2
00041 ///      consecuative cycles and that the next stage starts one cycle
00042 ///      after this stage starts. That is, the stage requirements
00043 ///      overlap in time.
00044 ///
00045 ///   { 1, x, 0 }
00046 ///      indicates that the stage occupies FU x for 1 cycle and that
00047 ///      the next stage starts in this same cycle. This can be used to
00048 ///      indicate that the instruction requires multiple stages at the
00049 ///      same time.
00050 ///
00051 /// FU reservation can be of two different kinds:
00052 ///  - FUs which instruction actually requires
00053 ///  - FUs which instruction just reserves. Reserved unit is not available for
00054 ///    execution of other instruction. However, several instructions can reserve
00055 ///    the same unit several times.
00056 /// Such two types of units reservation is used to model instruction domain
00057 /// change stalls, FUs using the same resource (e.g. same register file), etc.
00058 
00059 struct InstrStage {
00060   enum ReservationKinds {
00061     Required = 0,
00062     Reserved = 1
00063   };
00064 
00065   unsigned Cycles_;  ///< Length of stage in machine cycles
00066   unsigned Units_;   ///< Choice of functional units
00067   int NextCycles_;   ///< Number of machine cycles to next stage
00068   ReservationKinds Kind_; ///< Kind of the FU reservation
00069 
00070   /// getCycles - returns the number of cycles the stage is occupied
00071   unsigned getCycles() const {
00072     return Cycles_;
00073   }
00074 
00075   /// getUnits - returns the choice of FUs
00076   unsigned getUnits() const {
00077     return Units_;
00078   }
00079 
00080   ReservationKinds getReservationKind() const {
00081     return Kind_;
00082   }
00083 
00084   /// getNextCycles - returns the number of cycles from the start of
00085   /// this stage to the start of the next stage in the itinerary
00086   unsigned getNextCycles() const {
00087     return (NextCycles_ >= 0) ? (unsigned)NextCycles_ : Cycles_;
00088   }
00089 };
00090 
00091 
00092 //===----------------------------------------------------------------------===//
00093 /// Instruction itinerary - An itinerary represents the scheduling
00094 /// information for an instruction. This includes a set of stages
00095 /// occupies by the instruction, and the pipeline cycle in which
00096 /// operands are read and written.
00097 ///
00098 struct InstrItinerary {
00099   int      NumMicroOps;        ///< # of micro-ops, -1 means it's variable
00100   unsigned FirstStage;         ///< Index of first stage in itinerary
00101   unsigned LastStage;          ///< Index of last + 1 stage in itinerary
00102   unsigned FirstOperandCycle;  ///< Index of first operand rd/wr
00103   unsigned LastOperandCycle;   ///< Index of last + 1 operand rd/wr
00104 };
00105 
00106 
00107 //===----------------------------------------------------------------------===//
00108 /// Instruction itinerary Data - Itinerary data supplied by a subtarget to be
00109 /// used by a target.
00110 ///
00111 class InstrItineraryData {
00112 public:
00113   MCSchedModel          SchedModel;     ///< Basic machine properties.
00114   const InstrStage     *Stages;         ///< Array of stages selected
00115   const unsigned       *OperandCycles;  ///< Array of operand cycles selected
00116   const unsigned       *Forwardings;    ///< Array of pipeline forwarding pathes
00117   const InstrItinerary *Itineraries;    ///< Array of itineraries selected
00118 
00119   /// Ctors.
00120   ///
00121   InstrItineraryData() : SchedModel(MCSchedModel::GetDefaultSchedModel()),
00122                          Stages(nullptr), OperandCycles(nullptr),
00123                          Forwardings(nullptr), Itineraries(nullptr) {}
00124 
00125   InstrItineraryData(const MCSchedModel &SM, const InstrStage *S,
00126                      const unsigned *OS, const unsigned *F)
00127     : SchedModel(SM), Stages(S), OperandCycles(OS), Forwardings(F),
00128       Itineraries(SchedModel.InstrItineraries) {}
00129 
00130   /// isEmpty - Returns true if there are no itineraries.
00131   ///
00132   bool isEmpty() const { return Itineraries == nullptr; }
00133 
00134   /// isEndMarker - Returns true if the index is for the end marker
00135   /// itinerary.
00136   ///
00137   bool isEndMarker(unsigned ItinClassIndx) const {
00138     return ((Itineraries[ItinClassIndx].FirstStage == ~0U) &&
00139             (Itineraries[ItinClassIndx].LastStage == ~0U));
00140   }
00141 
00142   /// beginStage - Return the first stage of the itinerary.
00143   ///
00144   const InstrStage *beginStage(unsigned ItinClassIndx) const {
00145     unsigned StageIdx = Itineraries[ItinClassIndx].FirstStage;
00146     return Stages + StageIdx;
00147   }
00148 
00149   /// endStage - Return the last+1 stage of the itinerary.
00150   ///
00151   const InstrStage *endStage(unsigned ItinClassIndx) const {
00152     unsigned StageIdx = Itineraries[ItinClassIndx].LastStage;
00153     return Stages + StageIdx;
00154   }
00155 
00156   /// getStageLatency - Return the total stage latency of the given
00157   /// class.  The latency is the maximum completion time for any stage
00158   /// in the itinerary.
00159   ///
00160   /// If no stages exist, it defaults to one cycle.
00161   unsigned getStageLatency(unsigned ItinClassIndx) const {
00162     // If the target doesn't provide itinerary information, use a simple
00163     // non-zero default value for all instructions.
00164     if (isEmpty())
00165       return 1;
00166 
00167     // Calculate the maximum completion time for any stage.
00168     unsigned Latency = 0, StartCycle = 0;
00169     for (const InstrStage *IS = beginStage(ItinClassIndx),
00170            *E = endStage(ItinClassIndx); IS != E; ++IS) {
00171       Latency = std::max(Latency, StartCycle + IS->getCycles());
00172       StartCycle += IS->getNextCycles();
00173     }
00174     return Latency;
00175   }
00176 
00177   /// getOperandCycle - Return the cycle for the given class and
00178   /// operand. Return -1 if no cycle is specified for the operand.
00179   ///
00180   int getOperandCycle(unsigned ItinClassIndx, unsigned OperandIdx) const {
00181     if (isEmpty())
00182       return -1;
00183 
00184     unsigned FirstIdx = Itineraries[ItinClassIndx].FirstOperandCycle;
00185     unsigned LastIdx = Itineraries[ItinClassIndx].LastOperandCycle;
00186     if ((FirstIdx + OperandIdx) >= LastIdx)
00187       return -1;
00188 
00189     return (int)OperandCycles[FirstIdx + OperandIdx];
00190   }
00191 
00192   /// hasPipelineForwarding - Return true if there is a pipeline forwarding
00193   /// between instructions of itinerary classes DefClass and UseClasses so that
00194   /// value produced by an instruction of itinerary class DefClass, operand
00195   /// index DefIdx can be bypassed when it's read by an instruction of
00196   /// itinerary class UseClass, operand index UseIdx.
00197   bool hasPipelineForwarding(unsigned DefClass, unsigned DefIdx,
00198                              unsigned UseClass, unsigned UseIdx) const {
00199     unsigned FirstDefIdx = Itineraries[DefClass].FirstOperandCycle;
00200     unsigned LastDefIdx = Itineraries[DefClass].LastOperandCycle;
00201     if ((FirstDefIdx + DefIdx) >= LastDefIdx)
00202       return false;
00203     if (Forwardings[FirstDefIdx + DefIdx] == 0)
00204       return false;
00205 
00206     unsigned FirstUseIdx = Itineraries[UseClass].FirstOperandCycle;
00207     unsigned LastUseIdx = Itineraries[UseClass].LastOperandCycle;
00208     if ((FirstUseIdx + UseIdx) >= LastUseIdx)
00209       return false;
00210 
00211     return Forwardings[FirstDefIdx + DefIdx] ==
00212       Forwardings[FirstUseIdx + UseIdx];
00213   }
00214 
00215   /// getOperandLatency - Compute and return the use operand latency of a given
00216   /// itinerary class and operand index if the value is produced by an
00217   /// instruction of the specified itinerary class and def operand index.
00218   int getOperandLatency(unsigned DefClass, unsigned DefIdx,
00219                         unsigned UseClass, unsigned UseIdx) const {
00220     if (isEmpty())
00221       return -1;
00222 
00223     int DefCycle = getOperandCycle(DefClass, DefIdx);
00224     if (DefCycle == -1)
00225       return -1;
00226 
00227     int UseCycle = getOperandCycle(UseClass, UseIdx);
00228     if (UseCycle == -1)
00229       return -1;
00230 
00231     UseCycle = DefCycle - UseCycle + 1;
00232     if (UseCycle > 0 &&
00233         hasPipelineForwarding(DefClass, DefIdx, UseClass, UseIdx))
00234       // FIXME: This assumes one cycle benefit for every pipeline forwarding.
00235       --UseCycle;
00236     return UseCycle;
00237   }
00238 
00239   /// getNumMicroOps - Return the number of micro-ops that the given class
00240   /// decodes to. Return -1 for classes that require dynamic lookup via
00241   /// TargetInstrInfo.
00242   int getNumMicroOps(unsigned ItinClassIndx) const {
00243     if (isEmpty())
00244       return 1;
00245     return Itineraries[ItinClassIndx].NumMicroOps;
00246   }
00247 };
00248 
00249 } // End llvm namespace
00250 
00251 #endif