Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 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 | // |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 10 | // This file defines structures to encapsulate the machine model as described in |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 11 | // the target description. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_UTILS_TABLEGEN_CODEGENSCHEDULE_H |
| 16 | #define LLVM_UTILS_TABLEGEN_CODEGENSCHEDULE_H |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 17 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | #include "llvm/TableGen/Record.h" |
James Molloy | f1653b5 | 2014-06-17 13:10:38 +0000 | [diff] [blame] | 22 | #include "llvm/TableGen/SetTheory.h" |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class CodeGenTarget; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 27 | class CodeGenSchedModels; |
| 28 | class CodeGenInstruction; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 29 | |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 30 | using RecVec = std::vector<Record*>; |
| 31 | using RecIter = std::vector<Record*>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 32 | |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 33 | using IdxVec = std::vector<unsigned>; |
| 34 | using IdxIter = std::vector<unsigned>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 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 { |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 47 | unsigned Index; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 48 | std::string Name; |
| 49 | Record *TheDef; |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 50 | bool IsRead; |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 51 | bool IsAlias; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 52 | bool HasVariants; |
| 53 | bool IsVariadic; |
| 54 | bool IsSequence; |
| 55 | IdxVec Sequence; |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 56 | RecVec Aliases; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 57 | |
Richard Smith | a7bb16a | 2012-12-20 01:05:39 +0000 | [diff] [blame] | 58 | CodeGenSchedRW() |
Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 59 | : Index(0), TheDef(nullptr), IsRead(false), IsAlias(false), |
Richard Smith | a7bb16a | 2012-12-20 01:05:39 +0000 | [diff] [blame] | 60 | HasVariants(false), IsVariadic(false), IsSequence(false) {} |
| 61 | CodeGenSchedRW(unsigned Idx, Record *Def) |
| 62 | : Index(Idx), TheDef(Def), IsAlias(false), IsVariadic(false) { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 63 | Name = Def->getName(); |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 64 | IsRead = Def->isSubClassOf("SchedRead"); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 75 | CodeGenSchedRW(unsigned Idx, bool Read, ArrayRef<unsigned> Seq, |
Richard Smith | a7bb16a | 2012-12-20 01:05:39 +0000 | [diff] [blame] | 76 | const std::string &Name) |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 77 | : Index(Idx), Name(Name), TheDef(nullptr), IsRead(Read), IsAlias(false), |
| 78 | HasVariants(false), IsVariadic(false), IsSequence(true), Sequence(Seq) { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 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"); |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 87 | assert((!IsAlias || Aliases.empty()) && "Alias cannot have aliases"); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 88 | return TheDef || !Sequence.empty(); |
| 89 | } |
| 90 | |
| 91 | #ifndef NDEBUG |
| 92 | void dump() const; |
| 93 | #endif |
| 94 | }; |
| 95 | |
Andrew Trick | ea28dbd | 2012-09-18 04:03:30 +0000 | [diff] [blame] | 96 | /// Represent a transition between SchedClasses induced by SchedVariant. |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 97 | struct CodeGenSchedTransition { |
| 98 | unsigned ToClassIdx; |
| 99 | IdxVec ProcIndices; |
| 100 | RecVec PredTerm; |
| 101 | }; |
| 102 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 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. |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 127 | struct CodeGenSchedClass { |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 128 | unsigned Index; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 129 | std::string Name; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 130 | Record *ItinClassDef; |
| 131 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 132 | IdxVec Writes; |
| 133 | IdxVec Reads; |
| 134 | // Sorted list of ProcIdx, where ProcIdx==0 implies any processor. |
| 135 | IdxVec ProcIndices; |
| 136 | |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 137 | std::vector<CodeGenSchedTransition> Transitions; |
| 138 | |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 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. |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 143 | RecVec InstRWs; |
| 144 | |
Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 145 | CodeGenSchedClass(): Index(0), ItinClassDef(nullptr) {} |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 146 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 147 | bool isKeyEqual(Record *IC, ArrayRef<unsigned> W, ArrayRef<unsigned> R) { |
| 148 | return ItinClassDef == IC && makeArrayRef(Writes) == W && |
| 149 | makeArrayRef(Reads) == R; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 150 | } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 151 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 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 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 156 | #ifndef NDEBUG |
| 157 | void dump(const CodeGenSchedModels *SchedModels) const; |
| 158 | #endif |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 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 |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 166 | // an itinerary but no machine model. If the processor defines neither a machine |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 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 { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 176 | unsigned Index; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 177 | std::string ModelName; |
| 178 | Record *ModelDef; |
| 179 | Record *ItinsDef; |
| 180 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 181 | // Derived members... |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 182 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 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 | |
Simon Dardis | 5f95c9a | 2016-06-24 08:43:27 +0000 | [diff] [blame] | 192 | // List of unsupported feature. |
| 193 | // This list is empty if the Processor has no UnsupportedFeatures. |
| 194 | RecVec UnsupportedFeaturesDefs; |
| 195 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 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; |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 202 | RecVec ProcResGroupDefs; |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 203 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 204 | CodeGenProcModel(unsigned Idx, const std::string &Name, Record *MDef, |
| 205 | Record *IDef) : |
| 206 | Index(Idx), ModelName(Name), ModelDef(MDef), ItinsDef(IDef) {} |
| 207 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 208 | bool hasItineraries() const { |
| 209 | return !ItinsDef->getValueAsListOfDefs("IID").empty(); |
| 210 | } |
| 211 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 212 | bool hasInstrSchedModel() const { |
| 213 | return !WriteResDefs.empty() || !ItinRWDefs.empty(); |
| 214 | } |
| 215 | |
| 216 | unsigned getProcResourceIdx(Record *PRDef) const; |
| 217 | |
Simon Dardis | 5f95c9a | 2016-06-24 08:43:27 +0000 | [diff] [blame] | 218 | bool isUnsupported(const CodeGenInstruction &Inst) const; |
| 219 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 220 | #ifndef NDEBUG |
| 221 | void dump() const; |
| 222 | #endif |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 225 | /// Top level container for machine model data. |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 226 | class CodeGenSchedModels { |
| 227 | RecordKeeper &Records; |
| 228 | const CodeGenTarget &Target; |
| 229 | |
Andrew Trick | 9e1deb6 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 230 | // Map dag expressions to Instruction lists. |
| 231 | SetTheory Sets; |
| 232 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 233 | // List of unique processor models. |
| 234 | std::vector<CodeGenProcModel> ProcModels; |
| 235 | |
| 236 | // Map Processor's MachineModel or ProcItin to a CodeGenProcModel index. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 237 | using ProcModelMapTy = DenseMap<Record*, unsigned>; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 238 | ProcModelMapTy ProcModelMap; |
| 239 | |
| 240 | // Per-operand SchedReadWrite types. |
| 241 | std::vector<CodeGenSchedRW> SchedWrites; |
| 242 | std::vector<CodeGenSchedRW> SchedReads; |
| 243 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 244 | // List of unique SchedClasses. |
| 245 | std::vector<CodeGenSchedClass> SchedClasses; |
| 246 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 247 | // Any inferred SchedClass has an index greater than NumInstrSchedClassses. |
| 248 | unsigned NumInstrSchedClasses; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 249 | |
Matthias Braun | 6b1fd9a | 2016-06-21 03:24:03 +0000 | [diff] [blame] | 250 | RecVec ProcResourceDefs; |
| 251 | RecVec ProcResGroups; |
| 252 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 253 | // Map each instruction to its unique SchedClass index considering the |
| 254 | // combination of it's itinerary class, SchedRW list, and InstRW records. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 255 | using InstClassMapTy = DenseMap<Record*, unsigned>; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 256 | InstClassMapTy InstrClassMap; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 257 | |
| 258 | public: |
| 259 | CodeGenSchedModels(RecordKeeper& RK, const CodeGenTarget &TGT); |
| 260 | |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 261 | // iterator access to the scheduling classes. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 262 | using class_iterator = std::vector<CodeGenSchedClass>::iterator; |
| 263 | using const_class_iterator = std::vector<CodeGenSchedClass>::const_iterator; |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 264 | class_iterator classes_begin() { return SchedClasses.begin(); } |
| 265 | const_class_iterator classes_begin() const { return SchedClasses.begin(); } |
| 266 | class_iterator classes_end() { return SchedClasses.end(); } |
| 267 | const_class_iterator classes_end() const { return SchedClasses.end(); } |
| 268 | iterator_range<class_iterator> classes() { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 269 | return make_range(classes_begin(), classes_end()); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 270 | } |
| 271 | iterator_range<const_class_iterator> classes() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 272 | return make_range(classes_begin(), classes_end()); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 273 | } |
| 274 | iterator_range<class_iterator> explicit_classes() { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 275 | return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 276 | } |
| 277 | iterator_range<const_class_iterator> explicit_classes() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 278 | return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 281 | Record *getModelOrItinDef(Record *ProcDef) const { |
| 282 | Record *ModelDef = ProcDef->getValueAsDef("SchedModel"); |
| 283 | Record *ItinsDef = ProcDef->getValueAsDef("ProcItin"); |
| 284 | if (!ItinsDef->getValueAsListOfDefs("IID").empty()) { |
| 285 | assert(ModelDef->getValueAsBit("NoModel") |
| 286 | && "Itineraries must be defined within SchedMachineModel"); |
| 287 | return ItinsDef; |
| 288 | } |
| 289 | return ModelDef; |
| 290 | } |
| 291 | |
| 292 | const CodeGenProcModel &getModelForProc(Record *ProcDef) const { |
| 293 | Record *ModelDef = getModelOrItinDef(ProcDef); |
| 294 | ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); |
| 295 | assert(I != ProcModelMap.end() && "missing machine model"); |
| 296 | return ProcModels[I->second]; |
| 297 | } |
| 298 | |
Andrew Trick | 40c4f38 | 2013-06-15 04:50:06 +0000 | [diff] [blame] | 299 | CodeGenProcModel &getProcModel(Record *ModelDef) { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 300 | ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); |
| 301 | assert(I != ProcModelMap.end() && "missing machine model"); |
| 302 | return ProcModels[I->second]; |
| 303 | } |
Andrew Trick | 40c4f38 | 2013-06-15 04:50:06 +0000 | [diff] [blame] | 304 | const CodeGenProcModel &getProcModel(Record *ModelDef) const { |
| 305 | return const_cast<CodeGenSchedModels*>(this)->getProcModel(ModelDef); |
| 306 | } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 307 | |
| 308 | // Iterate over the unique processor models. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 309 | using ProcIter = std::vector<CodeGenProcModel>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 310 | ProcIter procModelBegin() const { return ProcModels.begin(); } |
| 311 | ProcIter procModelEnd() const { return ProcModels.end(); } |
Craig Topper | 29c55dcb | 2016-02-13 06:03:32 +0000 | [diff] [blame] | 312 | ArrayRef<CodeGenProcModel> procModels() const { return ProcModels; } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 313 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 314 | // Return true if any processors have itineraries. |
| 315 | bool hasItineraries() const; |
| 316 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 317 | // Get a SchedWrite from its index. |
| 318 | const CodeGenSchedRW &getSchedWrite(unsigned Idx) const { |
| 319 | assert(Idx < SchedWrites.size() && "bad SchedWrite index"); |
| 320 | assert(SchedWrites[Idx].isValid() && "invalid SchedWrite"); |
| 321 | return SchedWrites[Idx]; |
| 322 | } |
| 323 | // Get a SchedWrite from its index. |
| 324 | const CodeGenSchedRW &getSchedRead(unsigned Idx) const { |
| 325 | assert(Idx < SchedReads.size() && "bad SchedRead index"); |
| 326 | assert(SchedReads[Idx].isValid() && "invalid SchedRead"); |
| 327 | return SchedReads[Idx]; |
| 328 | } |
| 329 | |
| 330 | const CodeGenSchedRW &getSchedRW(unsigned Idx, bool IsRead) const { |
| 331 | return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx); |
| 332 | } |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 333 | CodeGenSchedRW &getSchedRW(Record *Def) { |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 334 | bool IsRead = Def->isSubClassOf("SchedRead"); |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 335 | unsigned Idx = getSchedRWIdx(Def, IsRead); |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 336 | return const_cast<CodeGenSchedRW&>( |
| 337 | IsRead ? getSchedRead(Idx) : getSchedWrite(Idx)); |
| 338 | } |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 339 | const CodeGenSchedRW &getSchedRW(Record*Def) const { |
| 340 | return const_cast<CodeGenSchedModels&>(*this).getSchedRW(Def); |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 341 | } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 342 | |
| 343 | unsigned getSchedRWIdx(Record *Def, bool IsRead, unsigned After = 0) const; |
| 344 | |
Andrew Trick | cfe222c | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 345 | // Return true if the given write record is referenced by a ReadAdvance. |
| 346 | bool hasReadOfWrite(Record *WriteDef) const; |
| 347 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 348 | // Get a SchedClass from its index. |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 349 | CodeGenSchedClass &getSchedClass(unsigned Idx) { |
| 350 | assert(Idx < SchedClasses.size() && "bad SchedClass index"); |
| 351 | return SchedClasses[Idx]; |
| 352 | } |
| 353 | const CodeGenSchedClass &getSchedClass(unsigned Idx) const { |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 354 | assert(Idx < SchedClasses.size() && "bad SchedClass index"); |
| 355 | return SchedClasses[Idx]; |
| 356 | } |
| 357 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 358 | // Get the SchedClass index for an instruction. Instructions with no |
| 359 | // itinerary, no SchedReadWrites, and no InstrReadWrites references return 0 |
| 360 | // for NoItinerary. |
| 361 | unsigned getSchedClassIdx(const CodeGenInstruction &Inst) const; |
| 362 | |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 363 | using SchedClassIter = std::vector<CodeGenSchedClass>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 364 | SchedClassIter schedClassBegin() const { return SchedClasses.begin(); } |
| 365 | SchedClassIter schedClassEnd() const { return SchedClasses.end(); } |
Craig Topper | 29c55dcb | 2016-02-13 06:03:32 +0000 | [diff] [blame] | 366 | ArrayRef<CodeGenSchedClass> schedClasses() const { return SchedClasses; } |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 367 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 368 | unsigned numInstrSchedClasses() const { return NumInstrSchedClasses; } |
| 369 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 370 | void findRWs(const RecVec &RWDefs, IdxVec &Writes, IdxVec &Reads) const; |
| 371 | void findRWs(const RecVec &RWDefs, IdxVec &RWs, bool IsRead) const; |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 372 | void expandRWSequence(unsigned RWIdx, IdxVec &RWSeq, bool IsRead) const; |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 373 | void expandRWSeqForProc(unsigned RWIdx, IdxVec &RWSeq, bool IsRead, |
| 374 | const CodeGenProcModel &ProcModel) const; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 375 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 376 | unsigned addSchedClass(Record *ItinDef, ArrayRef<unsigned> OperWrites, |
| 377 | ArrayRef<unsigned> OperReads, |
| 378 | ArrayRef<unsigned> ProcIndices); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 379 | |
| 380 | unsigned findOrInsertRW(ArrayRef<unsigned> Seq, bool IsRead); |
| 381 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 382 | unsigned findSchedClassIdx(Record *ItinClassDef, ArrayRef<unsigned> Writes, |
| 383 | ArrayRef<unsigned> Reads) const; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 384 | |
Evandro Menezes | 9dc54e2 | 2017-11-21 21:33:52 +0000 | [diff] [blame] | 385 | Record *findProcResUnits(Record *ProcResKind, const CodeGenProcModel &PM, |
| 386 | ArrayRef<SMLoc> Loc) const; |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 387 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 388 | private: |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 389 | void collectProcModels(); |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 390 | |
| 391 | // Initialize a new processor model if it is unique. |
| 392 | void addProcModel(Record *ProcDef); |
| 393 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 394 | void collectSchedRW(); |
| 395 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 396 | std::string genRWName(ArrayRef<unsigned> Seq, bool IsRead); |
| 397 | unsigned findRWForSequence(ArrayRef<unsigned> Seq, bool IsRead); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 398 | |
| 399 | void collectSchedClasses(); |
| 400 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 401 | std::string createSchedClassName(Record *ItinClassDef, |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 402 | ArrayRef<unsigned> OperWrites, |
| 403 | ArrayRef<unsigned> OperReads); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 404 | std::string createSchedClassName(const RecVec &InstDefs); |
| 405 | void createInstRWClass(Record *InstRWDef); |
| 406 | |
| 407 | void collectProcItins(); |
| 408 | |
| 409 | void collectProcItinRW(); |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 410 | |
Simon Dardis | 5f95c9a | 2016-06-24 08:43:27 +0000 | [diff] [blame] | 411 | void collectProcUnsupportedFeatures(); |
| 412 | |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 413 | void inferSchedClasses(); |
| 414 | |
Matthias Braun | 17cb579 | 2016-03-01 20:03:21 +0000 | [diff] [blame] | 415 | void checkCompleteness(); |
| 416 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 417 | void inferFromRW(ArrayRef<unsigned> OperWrites, ArrayRef<unsigned> OperReads, |
| 418 | unsigned FromClassIdx, ArrayRef<unsigned> ProcIndices); |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 419 | void inferFromItinClass(Record *ItinClassDef, unsigned FromClassIdx); |
| 420 | void inferFromInstRWs(unsigned SCIdx); |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 421 | |
Andrew Trick | cf398b2 | 2013-04-23 23:45:14 +0000 | [diff] [blame] | 422 | bool hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM); |
| 423 | void verifyProcResourceGroups(CodeGenProcModel &PM); |
| 424 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 425 | void collectProcResources(); |
| 426 | |
| 427 | void collectItinProcResources(Record *ItinClassDef); |
| 428 | |
Andrew Trick | d0b9c44 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 429 | void collectRWResources(unsigned RWIdx, bool IsRead, |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 430 | ArrayRef<unsigned> ProcIndices); |
Andrew Trick | d0b9c44 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 431 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 432 | void collectRWResources(ArrayRef<unsigned> Writes, ArrayRef<unsigned> Reads, |
| 433 | ArrayRef<unsigned> ProcIndices); |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 434 | |
Evandro Menezes | 9dc54e2 | 2017-11-21 21:33:52 +0000 | [diff] [blame] | 435 | void addProcResource(Record *ProcResourceKind, CodeGenProcModel &PM, |
| 436 | ArrayRef<SMLoc> Loc); |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 437 | |
| 438 | void addWriteRes(Record *ProcWriteResDef, unsigned PIdx); |
| 439 | |
| 440 | void addReadAdvance(Record *ProcReadAdvanceDef, unsigned PIdx); |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | } // namespace llvm |
| 444 | |
| 445 | #endif |