1 //===-- llvm/MC/MCInstrItineraries.h - Scheduling ---------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file describes the structures used for instruction 11 // itineraries, stages, and operand reads/writes. This is used by 12 // schedulers to determine instruction stages and latencies. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_MC_MCINSTRITINERARIES_H 17 #define LLVM_MC_MCINSTRITINERARIES_H 18 19 #include "llvm/MC/MCSchedule.h" 20 #include <algorithm> 21 22 namespace llvm { 23 24 //===----------------------------------------------------------------------===// 25 /// These values represent a non-pipelined step in 26 /// the execution of an instruction. Cycles represents the number of 27 /// discrete time slots needed to complete the stage. Units represent 28 /// the choice of functional units that can be used to complete the 29 /// stage. Eg. IntUnit1, IntUnit2. NextCycles indicates how many 30 /// cycles should elapse from the start of this stage to the start of 31 /// the next stage in the itinerary. A value of -1 indicates that the 32 /// next stage should start immediately after the current one. 33 /// For example: 34 /// 35 /// { 1, x, -1 } 36 /// indicates that the stage occupies FU x for 1 cycle and that 37 /// the next stage starts immediately after this one. 38 /// 39 /// { 2, x|y, 1 } 40 /// indicates that the stage occupies either FU x or FU y for 2 41 /// consecuative cycles and that the next stage starts one cycle 42 /// after this stage starts. That is, the stage requirements 43 /// overlap in time. 44 /// 45 /// { 1, x, 0 } 46 /// indicates that the stage occupies FU x for 1 cycle and that 47 /// the next stage starts in this same cycle. This can be used to 48 /// indicate that the instruction requires multiple stages at the 49 /// same time. 50 /// 51 /// FU reservation can be of two different kinds: 52 /// - FUs which instruction actually requires 53 /// - FUs which instruction just reserves. Reserved unit is not available for 54 /// execution of other instruction. However, several instructions can reserve 55 /// the same unit several times. 56 /// Such two types of units reservation is used to model instruction domain 57 /// change stalls, FUs using the same resource (e.g. same register file), etc. 58 59 struct InstrStage { 60 enum ReservationKinds { 61 Required = 0, 62 Reserved = 1 63 }; 64 65 unsigned Cycles_; ///< Length of stage in machine cycles 66 unsigned Units_; ///< Choice of functional units 67 int NextCycles_; ///< Number of machine cycles to next stage 68 ReservationKinds Kind_; ///< Kind of the FU reservation 69 70 /// Returns the number of cycles the stage is occupied. getCyclesInstrStage71 unsigned getCycles() const { 72 return Cycles_; 73 } 74 75 /// Returns the choice of FUs. getUnitsInstrStage76 unsigned getUnits() const { 77 return Units_; 78 } 79 getReservationKindInstrStage80 ReservationKinds getReservationKind() const { 81 return Kind_; 82 } 83 84 /// Returns the number of cycles from the start of 85 /// this stage to the start of the next stage in the itinerary getNextCyclesInstrStage86 unsigned getNextCycles() const { 87 return (NextCycles_ >= 0) ? (unsigned)NextCycles_ : Cycles_; 88 } 89 }; 90 91 92 //===----------------------------------------------------------------------===// 93 /// An itinerary represents the scheduling information for an instruction. 94 /// This includes a set of stages occupied by the instruction and the pipeline 95 /// cycle in which operands are read and written. 96 /// 97 struct InstrItinerary { 98 int NumMicroOps; ///< # of micro-ops, -1 means it's variable 99 unsigned FirstStage; ///< Index of first stage in itinerary 100 unsigned LastStage; ///< Index of last + 1 stage in itinerary 101 unsigned FirstOperandCycle; ///< Index of first operand rd/wr 102 unsigned LastOperandCycle; ///< Index of last + 1 operand rd/wr 103 }; 104 105 106 //===----------------------------------------------------------------------===// 107 /// Itinerary data supplied by a subtarget to be used by a target. 108 /// 109 class InstrItineraryData { 110 public: 111 MCSchedModel SchedModel; ///< Basic machine properties. 112 const InstrStage *Stages; ///< Array of stages selected 113 const unsigned *OperandCycles; ///< Array of operand cycles selected 114 const unsigned *Forwardings; ///< Array of pipeline forwarding pathes 115 const InstrItinerary *Itineraries; ///< Array of itineraries selected 116 117 /// Ctors. 118 /// InstrItineraryData()119 InstrItineraryData() : SchedModel(MCSchedModel::GetDefaultSchedModel()), 120 Stages(nullptr), OperandCycles(nullptr), 121 Forwardings(nullptr), Itineraries(nullptr) {} 122 InstrItineraryData(const MCSchedModel & SM,const InstrStage * S,const unsigned * OS,const unsigned * F)123 InstrItineraryData(const MCSchedModel &SM, const InstrStage *S, 124 const unsigned *OS, const unsigned *F) 125 : SchedModel(SM), Stages(S), OperandCycles(OS), Forwardings(F), 126 Itineraries(SchedModel.InstrItineraries) {} 127 128 /// Returns true if there are no itineraries. isEmpty()129 bool isEmpty() const { return Itineraries == nullptr; } 130 131 /// Returns true if the index is for the end marker itinerary. isEndMarker(unsigned ItinClassIndx)132 bool isEndMarker(unsigned ItinClassIndx) const { 133 return ((Itineraries[ItinClassIndx].FirstStage == ~0U) && 134 (Itineraries[ItinClassIndx].LastStage == ~0U)); 135 } 136 137 /// Return the first stage of the itinerary. beginStage(unsigned ItinClassIndx)138 const InstrStage *beginStage(unsigned ItinClassIndx) const { 139 unsigned StageIdx = Itineraries[ItinClassIndx].FirstStage; 140 return Stages + StageIdx; 141 } 142 143 /// Return the last+1 stage of the itinerary. endStage(unsigned ItinClassIndx)144 const InstrStage *endStage(unsigned ItinClassIndx) const { 145 unsigned StageIdx = Itineraries[ItinClassIndx].LastStage; 146 return Stages + StageIdx; 147 } 148 149 /// Return the total stage latency of the given class. 150 /// The latency is the maximum completion time for any stage in the itinerary. 151 /// If no stages exist, it defaults to one cycle. getStageLatency(unsigned ItinClassIndx)152 unsigned getStageLatency(unsigned ItinClassIndx) const { 153 // If the target doesn't provide itinerary information, use a simple 154 // non-zero default value for all instructions. 155 if (isEmpty()) 156 return 1; 157 158 // Calculate the maximum completion time for any stage. 159 unsigned Latency = 0, StartCycle = 0; 160 for (const InstrStage *IS = beginStage(ItinClassIndx), 161 *E = endStage(ItinClassIndx); IS != E; ++IS) { 162 Latency = std::max(Latency, StartCycle + IS->getCycles()); 163 StartCycle += IS->getNextCycles(); 164 } 165 return Latency; 166 } 167 168 /// Return the cycle for the given class and operand. 169 /// Return -1 if no cycle is specified for the operand. getOperandCycle(unsigned ItinClassIndx,unsigned OperandIdx)170 int getOperandCycle(unsigned ItinClassIndx, unsigned OperandIdx) const { 171 if (isEmpty()) 172 return -1; 173 174 unsigned FirstIdx = Itineraries[ItinClassIndx].FirstOperandCycle; 175 unsigned LastIdx = Itineraries[ItinClassIndx].LastOperandCycle; 176 if ((FirstIdx + OperandIdx) >= LastIdx) 177 return -1; 178 179 return (int)OperandCycles[FirstIdx + OperandIdx]; 180 } 181 182 /// Return true if there is a pipeline forwarding 183 /// between instructions of itinerary classes DefClass and UseClasses so that 184 /// value produced by an instruction of itinerary class DefClass, operand 185 /// index DefIdx can be bypassed when it's read by an instruction of 186 /// itinerary class UseClass, operand index UseIdx. hasPipelineForwarding(unsigned DefClass,unsigned DefIdx,unsigned UseClass,unsigned UseIdx)187 bool hasPipelineForwarding(unsigned DefClass, unsigned DefIdx, 188 unsigned UseClass, unsigned UseIdx) const { 189 unsigned FirstDefIdx = Itineraries[DefClass].FirstOperandCycle; 190 unsigned LastDefIdx = Itineraries[DefClass].LastOperandCycle; 191 if ((FirstDefIdx + DefIdx) >= LastDefIdx) 192 return false; 193 if (Forwardings[FirstDefIdx + DefIdx] == 0) 194 return false; 195 196 unsigned FirstUseIdx = Itineraries[UseClass].FirstOperandCycle; 197 unsigned LastUseIdx = Itineraries[UseClass].LastOperandCycle; 198 if ((FirstUseIdx + UseIdx) >= LastUseIdx) 199 return false; 200 201 return Forwardings[FirstDefIdx + DefIdx] == 202 Forwardings[FirstUseIdx + UseIdx]; 203 } 204 205 /// Compute and return the use operand latency of a given 206 /// itinerary class and operand index if the value is produced by an 207 /// instruction of the specified itinerary class and def operand index. getOperandLatency(unsigned DefClass,unsigned DefIdx,unsigned UseClass,unsigned UseIdx)208 int getOperandLatency(unsigned DefClass, unsigned DefIdx, 209 unsigned UseClass, unsigned UseIdx) const { 210 if (isEmpty()) 211 return -1; 212 213 int DefCycle = getOperandCycle(DefClass, DefIdx); 214 if (DefCycle == -1) 215 return -1; 216 217 int UseCycle = getOperandCycle(UseClass, UseIdx); 218 if (UseCycle == -1) 219 return -1; 220 221 UseCycle = DefCycle - UseCycle + 1; 222 if (UseCycle > 0 && 223 hasPipelineForwarding(DefClass, DefIdx, UseClass, UseIdx)) 224 // FIXME: This assumes one cycle benefit for every pipeline forwarding. 225 --UseCycle; 226 return UseCycle; 227 } 228 229 /// Return the number of micro-ops that the given class decodes to. 230 /// Return -1 for classes that require dynamic lookup via TargetInstrInfo. getNumMicroOps(unsigned ItinClassIndx)231 int getNumMicroOps(unsigned ItinClassIndx) const { 232 if (isEmpty()) 233 return 1; 234 return Itineraries[ItinClassIndx].NumMicroOps; 235 } 236 }; 237 238 } // End llvm namespace 239 240 #endif 241