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