1 //===- CodeGenSchedule.h - Scheduling Machine Models ------------*- 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 defines structures to encapsulate the machine model as described in 11 // the target description. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_UTILS_TABLEGEN_CODEGENSCHEDULE_H 16 #define LLVM_UTILS_TABLEGEN_CODEGENSCHEDULE_H 17 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/TableGen/Record.h" 22 #include "llvm/TableGen/SetTheory.h" 23 24 namespace llvm { 25 26 class CodeGenTarget; 27 class CodeGenSchedModels; 28 class CodeGenInstruction; 29 30 using RecVec = std::vector<Record*>; 31 using RecIter = std::vector<Record*>::const_iterator; 32 33 using IdxVec = std::vector<unsigned>; 34 using IdxIter = std::vector<unsigned>::const_iterator; 35 36 void splitSchedReadWrites(const RecVec &RWDefs, 37 RecVec &WriteDefs, RecVec &ReadDefs); 38 39 /// We have two kinds of SchedReadWrites. Explicitly defined and inferred 40 /// sequences. TheDef is nonnull for explicit SchedWrites, but Sequence may or 41 /// may not be empty. TheDef is null for inferred sequences, and Sequence must 42 /// be nonempty. 43 /// 44 /// IsVariadic controls whether the variants are expanded into multiple operands 45 /// or a sequence of writes on one operand. 46 struct CodeGenSchedRW { 47 unsigned Index; 48 std::string Name; 49 Record *TheDef; 50 bool IsRead; 51 bool IsAlias; 52 bool HasVariants; 53 bool IsVariadic; 54 bool IsSequence; 55 IdxVec Sequence; 56 RecVec Aliases; 57 58 CodeGenSchedRW() 59 : Index(0), TheDef(nullptr), IsRead(false), IsAlias(false), 60 HasVariants(false), IsVariadic(false), IsSequence(false) {} 61 CodeGenSchedRW(unsigned Idx, Record *Def) 62 : Index(Idx), TheDef(Def), IsAlias(false), IsVariadic(false) { 63 Name = Def->getName(); 64 IsRead = Def->isSubClassOf("SchedRead"); 65 HasVariants = Def->isSubClassOf("SchedVariant"); 66 if (HasVariants) 67 IsVariadic = Def->getValueAsBit("Variadic"); 68 69 // Read records don't currently have sequences, but it can be easily 70 // added. Note that implicit Reads (from ReadVariant) may have a Sequence 71 // (but no record). 72 IsSequence = Def->isSubClassOf("WriteSequence"); 73 } 74 75 CodeGenSchedRW(unsigned Idx, bool Read, ArrayRef<unsigned> Seq, 76 const std::string &Name) 77 : Index(Idx), Name(Name), TheDef(nullptr), IsRead(Read), IsAlias(false), 78 HasVariants(false), IsVariadic(false), IsSequence(true), Sequence(Seq) { 79 assert(Sequence.size() > 1 && "implied sequence needs >1 RWs"); 80 } 81 82 bool isValid() const { 83 assert((!HasVariants || TheDef) && "Variant write needs record def"); 84 assert((!IsVariadic || HasVariants) && "Variadic write needs variants"); 85 assert((!IsSequence || !HasVariants) && "Sequence can't have variant"); 86 assert((!IsSequence || !Sequence.empty()) && "Sequence should be nonempty"); 87 assert((!IsAlias || Aliases.empty()) && "Alias cannot have aliases"); 88 return TheDef || !Sequence.empty(); 89 } 90 91 #ifndef NDEBUG 92 void dump() const; 93 #endif 94 }; 95 96 /// Represent a transition between SchedClasses induced by SchedVariant. 97 struct CodeGenSchedTransition { 98 unsigned ToClassIdx; 99 IdxVec ProcIndices; 100 RecVec PredTerm; 101 }; 102 103 /// Scheduling class. 104 /// 105 /// Each instruction description will be mapped to a scheduling class. There are 106 /// four types of classes: 107 /// 108 /// 1) An explicitly defined itinerary class with ItinClassDef set. 109 /// Writes and ReadDefs are empty. ProcIndices contains 0 for any processor. 110 /// 111 /// 2) An implied class with a list of SchedWrites and SchedReads that are 112 /// defined in an instruction definition and which are common across all 113 /// subtargets. ProcIndices contains 0 for any processor. 114 /// 115 /// 3) An implied class with a list of InstRW records that map instructions to 116 /// SchedWrites and SchedReads per-processor. InstrClassMap should map the same 117 /// instructions to this class. ProcIndices contains all the processors that 118 /// provided InstrRW records for this class. ItinClassDef or Writes/Reads may 119 /// still be defined for processors with no InstRW entry. 120 /// 121 /// 4) An inferred class represents a variant of another class that may be 122 /// resolved at runtime. ProcIndices contains the set of processors that may 123 /// require the class. ProcIndices are propagated through SchedClasses as 124 /// variants are expanded. Multiple SchedClasses may be inferred from an 125 /// itinerary class. Each inherits the processor index from the ItinRW record 126 /// that mapped the itinerary class to the variant Writes or Reads. 127 struct CodeGenSchedClass { 128 unsigned Index; 129 std::string Name; 130 Record *ItinClassDef; 131 132 IdxVec Writes; 133 IdxVec Reads; 134 // Sorted list of ProcIdx, where ProcIdx==0 implies any processor. 135 IdxVec ProcIndices; 136 137 std::vector<CodeGenSchedTransition> Transitions; 138 139 // InstRW records associated with this class. These records may refer to an 140 // Instruction no longer mapped to this class by InstrClassMap. These 141 // Instructions should be ignored by this class because they have been split 142 // off to join another inferred class. 143 RecVec InstRWs; 144 145 CodeGenSchedClass(): Index(0), ItinClassDef(nullptr) {} 146 147 bool isKeyEqual(Record *IC, ArrayRef<unsigned> W, ArrayRef<unsigned> R) { 148 return ItinClassDef == IC && makeArrayRef(Writes) == W && 149 makeArrayRef(Reads) == R; 150 } 151 152 // Is this class generated from a variants if existing classes? Instructions 153 // are never mapped directly to inferred scheduling classes. 154 bool isInferred() const { return !ItinClassDef; } 155 156 #ifndef NDEBUG 157 void dump(const CodeGenSchedModels *SchedModels) const; 158 #endif 159 }; 160 161 // Processor model. 162 // 163 // ModelName is a unique name used to name an instantiation of MCSchedModel. 164 // 165 // ModelDef is NULL for inferred Models. This happens when a processor defines 166 // an itinerary but no machine model. If the processor defines neither a machine 167 // model nor itinerary, then ModelDef remains pointing to NoModel. NoModel has 168 // the special "NoModel" field set to true. 169 // 170 // ItinsDef always points to a valid record definition, but may point to the 171 // default NoItineraries. NoItineraries has an empty list of InstrItinData 172 // records. 173 // 174 // ItinDefList orders this processor's InstrItinData records by SchedClass idx. 175 struct CodeGenProcModel { 176 unsigned Index; 177 std::string ModelName; 178 Record *ModelDef; 179 Record *ItinsDef; 180 181 // Derived members... 182 183 // Array of InstrItinData records indexed by a CodeGenSchedClass index. 184 // This list is empty if the Processor has no value for Itineraries. 185 // Initialized by collectProcItins(). 186 RecVec ItinDefList; 187 188 // Map itinerary classes to per-operand resources. 189 // This list is empty if no ItinRW refers to this Processor. 190 RecVec ItinRWDefs; 191 192 // List of unsupported feature. 193 // This list is empty if the Processor has no UnsupportedFeatures. 194 RecVec UnsupportedFeaturesDefs; 195 196 // All read/write resources associated with this processor. 197 RecVec WriteResDefs; 198 RecVec ReadAdvanceDefs; 199 200 // Per-operand machine model resources associated with this processor. 201 RecVec ProcResourceDefs; 202 203 CodeGenProcModel(unsigned Idx, const std::string &Name, Record *MDef, 204 Record *IDef) : 205 Index(Idx), ModelName(Name), ModelDef(MDef), ItinsDef(IDef) {} 206 207 bool hasItineraries() const { 208 return !ItinsDef->getValueAsListOfDefs("IID").empty(); 209 } 210 211 bool hasInstrSchedModel() const { 212 return !WriteResDefs.empty() || !ItinRWDefs.empty(); 213 } 214 215 unsigned getProcResourceIdx(Record *PRDef) const; 216 217 bool isUnsupported(const CodeGenInstruction &Inst) const; 218 219 #ifndef NDEBUG 220 void dump() const; 221 #endif 222 }; 223 224 /// Top level container for machine model data. 225 class CodeGenSchedModels { 226 RecordKeeper &Records; 227 const CodeGenTarget &Target; 228 229 // Map dag expressions to Instruction lists. 230 SetTheory Sets; 231 232 // List of unique processor models. 233 std::vector<CodeGenProcModel> ProcModels; 234 235 // Map Processor's MachineModel or ProcItin to a CodeGenProcModel index. 236 using ProcModelMapTy = DenseMap<Record*, unsigned>; 237 ProcModelMapTy ProcModelMap; 238 239 // Per-operand SchedReadWrite types. 240 std::vector<CodeGenSchedRW> SchedWrites; 241 std::vector<CodeGenSchedRW> SchedReads; 242 243 // List of unique SchedClasses. 244 std::vector<CodeGenSchedClass> SchedClasses; 245 246 // Any inferred SchedClass has an index greater than NumInstrSchedClassses. 247 unsigned NumInstrSchedClasses; 248 249 RecVec ProcResourceDefs; 250 RecVec ProcResGroups; 251 252 // Map each instruction to its unique SchedClass index considering the 253 // combination of it's itinerary class, SchedRW list, and InstRW records. 254 using InstClassMapTy = DenseMap<Record*, unsigned>; 255 InstClassMapTy InstrClassMap; 256 257 public: 258 CodeGenSchedModels(RecordKeeper& RK, const CodeGenTarget &TGT); 259 260 // iterator access to the scheduling classes. 261 using class_iterator = std::vector<CodeGenSchedClass>::iterator; 262 using const_class_iterator = std::vector<CodeGenSchedClass>::const_iterator; 263 class_iterator classes_begin() { return SchedClasses.begin(); } 264 const_class_iterator classes_begin() const { return SchedClasses.begin(); } 265 class_iterator classes_end() { return SchedClasses.end(); } 266 const_class_iterator classes_end() const { return SchedClasses.end(); } 267 iterator_range<class_iterator> classes() { 268 return make_range(classes_begin(), classes_end()); 269 } 270 iterator_range<const_class_iterator> classes() const { 271 return make_range(classes_begin(), classes_end()); 272 } 273 iterator_range<class_iterator> explicit_classes() { 274 return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses); 275 } 276 iterator_range<const_class_iterator> explicit_classes() const { 277 return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses); 278 } 279 280 Record *getModelOrItinDef(Record *ProcDef) const { 281 Record *ModelDef = ProcDef->getValueAsDef("SchedModel"); 282 Record *ItinsDef = ProcDef->getValueAsDef("ProcItin"); 283 if (!ItinsDef->getValueAsListOfDefs("IID").empty()) { 284 assert(ModelDef->getValueAsBit("NoModel") 285 && "Itineraries must be defined within SchedMachineModel"); 286 return ItinsDef; 287 } 288 return ModelDef; 289 } 290 291 const CodeGenProcModel &getModelForProc(Record *ProcDef) const { 292 Record *ModelDef = getModelOrItinDef(ProcDef); 293 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); 294 assert(I != ProcModelMap.end() && "missing machine model"); 295 return ProcModels[I->second]; 296 } 297 298 CodeGenProcModel &getProcModel(Record *ModelDef) { 299 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); 300 assert(I != ProcModelMap.end() && "missing machine model"); 301 return ProcModels[I->second]; 302 } 303 const CodeGenProcModel &getProcModel(Record *ModelDef) const { 304 return const_cast<CodeGenSchedModels*>(this)->getProcModel(ModelDef); 305 } 306 307 // Iterate over the unique processor models. 308 using ProcIter = std::vector<CodeGenProcModel>::const_iterator; 309 ProcIter procModelBegin() const { return ProcModels.begin(); } 310 ProcIter procModelEnd() const { return ProcModels.end(); } 311 ArrayRef<CodeGenProcModel> procModels() const { return ProcModels; } 312 313 // Return true if any processors have itineraries. 314 bool hasItineraries() const; 315 316 // Get a SchedWrite from its index. 317 const CodeGenSchedRW &getSchedWrite(unsigned Idx) const { 318 assert(Idx < SchedWrites.size() && "bad SchedWrite index"); 319 assert(SchedWrites[Idx].isValid() && "invalid SchedWrite"); 320 return SchedWrites[Idx]; 321 } 322 // Get a SchedWrite from its index. 323 const CodeGenSchedRW &getSchedRead(unsigned Idx) const { 324 assert(Idx < SchedReads.size() && "bad SchedRead index"); 325 assert(SchedReads[Idx].isValid() && "invalid SchedRead"); 326 return SchedReads[Idx]; 327 } 328 329 const CodeGenSchedRW &getSchedRW(unsigned Idx, bool IsRead) const { 330 return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx); 331 } 332 CodeGenSchedRW &getSchedRW(Record *Def) { 333 bool IsRead = Def->isSubClassOf("SchedRead"); 334 unsigned Idx = getSchedRWIdx(Def, IsRead); 335 return const_cast<CodeGenSchedRW&>( 336 IsRead ? getSchedRead(Idx) : getSchedWrite(Idx)); 337 } 338 const CodeGenSchedRW &getSchedRW(Record*Def) const { 339 return const_cast<CodeGenSchedModels&>(*this).getSchedRW(Def); 340 } 341 342 unsigned getSchedRWIdx(Record *Def, bool IsRead, unsigned After = 0) const; 343 344 // Return true if the given write record is referenced by a ReadAdvance. 345 bool hasReadOfWrite(Record *WriteDef) const; 346 347 // Get a SchedClass from its index. 348 CodeGenSchedClass &getSchedClass(unsigned Idx) { 349 assert(Idx < SchedClasses.size() && "bad SchedClass index"); 350 return SchedClasses[Idx]; 351 } 352 const CodeGenSchedClass &getSchedClass(unsigned Idx) const { 353 assert(Idx < SchedClasses.size() && "bad SchedClass index"); 354 return SchedClasses[Idx]; 355 } 356 357 // Get the SchedClass index for an instruction. Instructions with no 358 // itinerary, no SchedReadWrites, and no InstrReadWrites references return 0 359 // for NoItinerary. 360 unsigned getSchedClassIdx(const CodeGenInstruction &Inst) const; 361 362 using SchedClassIter = std::vector<CodeGenSchedClass>::const_iterator; 363 SchedClassIter schedClassBegin() const { return SchedClasses.begin(); } 364 SchedClassIter schedClassEnd() const { return SchedClasses.end(); } 365 ArrayRef<CodeGenSchedClass> schedClasses() const { return SchedClasses; } 366 367 unsigned numInstrSchedClasses() const { return NumInstrSchedClasses; } 368 369 void findRWs(const RecVec &RWDefs, IdxVec &Writes, IdxVec &Reads) const; 370 void findRWs(const RecVec &RWDefs, IdxVec &RWs, bool IsRead) const; 371 void expandRWSequence(unsigned RWIdx, IdxVec &RWSeq, bool IsRead) const; 372 void expandRWSeqForProc(unsigned RWIdx, IdxVec &RWSeq, bool IsRead, 373 const CodeGenProcModel &ProcModel) const; 374 375 unsigned addSchedClass(Record *ItinDef, ArrayRef<unsigned> OperWrites, 376 ArrayRef<unsigned> OperReads, 377 ArrayRef<unsigned> ProcIndices); 378 379 unsigned findOrInsertRW(ArrayRef<unsigned> Seq, bool IsRead); 380 381 unsigned findSchedClassIdx(Record *ItinClassDef, ArrayRef<unsigned> Writes, 382 ArrayRef<unsigned> Reads) const; 383 384 Record *findProcResUnits(Record *ProcResKind, const CodeGenProcModel &PM, 385 ArrayRef<SMLoc> Loc) const; 386 387 private: 388 void collectProcModels(); 389 390 // Initialize a new processor model if it is unique. 391 void addProcModel(Record *ProcDef); 392 393 void collectSchedRW(); 394 395 std::string genRWName(ArrayRef<unsigned> Seq, bool IsRead); 396 unsigned findRWForSequence(ArrayRef<unsigned> Seq, bool IsRead); 397 398 void collectSchedClasses(); 399 400 std::string createSchedClassName(Record *ItinClassDef, 401 ArrayRef<unsigned> OperWrites, 402 ArrayRef<unsigned> OperReads); 403 std::string createSchedClassName(const RecVec &InstDefs); 404 void createInstRWClass(Record *InstRWDef); 405 406 void collectProcItins(); 407 408 void collectProcItinRW(); 409 410 void collectProcUnsupportedFeatures(); 411 412 void inferSchedClasses(); 413 414 void checkCompleteness(); 415 416 void inferFromRW(ArrayRef<unsigned> OperWrites, ArrayRef<unsigned> OperReads, 417 unsigned FromClassIdx, ArrayRef<unsigned> ProcIndices); 418 void inferFromItinClass(Record *ItinClassDef, unsigned FromClassIdx); 419 void inferFromInstRWs(unsigned SCIdx); 420 421 bool hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM); 422 void verifyProcResourceGroups(CodeGenProcModel &PM); 423 424 void collectProcResources(); 425 426 void collectItinProcResources(Record *ItinClassDef); 427 428 void collectRWResources(unsigned RWIdx, bool IsRead, 429 ArrayRef<unsigned> ProcIndices); 430 431 void collectRWResources(ArrayRef<unsigned> Writes, ArrayRef<unsigned> Reads, 432 ArrayRef<unsigned> ProcIndices); 433 434 void addProcResource(Record *ProcResourceKind, CodeGenProcModel &PM, 435 ArrayRef<SMLoc> Loc); 436 437 void addWriteRes(Record *ProcWriteResDef, unsigned PIdx); 438 439 void addReadAdvance(Record *ProcReadAdvanceDef, unsigned PIdx); 440 }; 441 442 } // namespace llvm 443 444 #endif 445