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; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 29 | class CodeGenRegisterClass; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 30 | |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 31 | using RecVec = std::vector<Record*>; |
| 32 | using RecIter = std::vector<Record*>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 33 | |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 34 | using IdxVec = std::vector<unsigned>; |
| 35 | using IdxIter = std::vector<unsigned>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 36 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 37 | /// We have two kinds of SchedReadWrites. Explicitly defined and inferred |
| 38 | /// sequences. TheDef is nonnull for explicit SchedWrites, but Sequence may or |
| 39 | /// may not be empty. TheDef is null for inferred sequences, and Sequence must |
| 40 | /// be nonempty. |
| 41 | /// |
| 42 | /// IsVariadic controls whether the variants are expanded into multiple operands |
| 43 | /// or a sequence of writes on one operand. |
| 44 | struct CodeGenSchedRW { |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 45 | unsigned Index; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 46 | std::string Name; |
| 47 | Record *TheDef; |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 48 | bool IsRead; |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 49 | bool IsAlias; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 50 | bool HasVariants; |
| 51 | bool IsVariadic; |
| 52 | bool IsSequence; |
| 53 | IdxVec Sequence; |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 54 | RecVec Aliases; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 55 | |
Richard Smith | a7bb16a | 2012-12-20 01:05:39 +0000 | [diff] [blame] | 56 | CodeGenSchedRW() |
Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 57 | : Index(0), TheDef(nullptr), IsRead(false), IsAlias(false), |
Richard Smith | a7bb16a | 2012-12-20 01:05:39 +0000 | [diff] [blame] | 58 | HasVariants(false), IsVariadic(false), IsSequence(false) {} |
| 59 | CodeGenSchedRW(unsigned Idx, Record *Def) |
| 60 | : Index(Idx), TheDef(Def), IsAlias(false), IsVariadic(false) { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 61 | Name = Def->getName(); |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 62 | IsRead = Def->isSubClassOf("SchedRead"); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 63 | HasVariants = Def->isSubClassOf("SchedVariant"); |
| 64 | if (HasVariants) |
| 65 | IsVariadic = Def->getValueAsBit("Variadic"); |
| 66 | |
| 67 | // Read records don't currently have sequences, but it can be easily |
| 68 | // added. Note that implicit Reads (from ReadVariant) may have a Sequence |
| 69 | // (but no record). |
| 70 | IsSequence = Def->isSubClassOf("WriteSequence"); |
| 71 | } |
| 72 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 73 | CodeGenSchedRW(unsigned Idx, bool Read, ArrayRef<unsigned> Seq, |
Richard Smith | a7bb16a | 2012-12-20 01:05:39 +0000 | [diff] [blame] | 74 | const std::string &Name) |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 75 | : Index(Idx), Name(Name), TheDef(nullptr), IsRead(Read), IsAlias(false), |
| 76 | HasVariants(false), IsVariadic(false), IsSequence(true), Sequence(Seq) { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 77 | assert(Sequence.size() > 1 && "implied sequence needs >1 RWs"); |
| 78 | } |
| 79 | |
| 80 | bool isValid() const { |
| 81 | assert((!HasVariants || TheDef) && "Variant write needs record def"); |
| 82 | assert((!IsVariadic || HasVariants) && "Variadic write needs variants"); |
| 83 | assert((!IsSequence || !HasVariants) && "Sequence can't have variant"); |
| 84 | assert((!IsSequence || !Sequence.empty()) && "Sequence should be nonempty"); |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 85 | assert((!IsAlias || Aliases.empty()) && "Alias cannot have aliases"); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 86 | return TheDef || !Sequence.empty(); |
| 87 | } |
| 88 | |
| 89 | #ifndef NDEBUG |
| 90 | void dump() const; |
| 91 | #endif |
| 92 | }; |
| 93 | |
Andrew Trick | ea28dbd | 2012-09-18 04:03:30 +0000 | [diff] [blame] | 94 | /// Represent a transition between SchedClasses induced by SchedVariant. |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 95 | struct CodeGenSchedTransition { |
| 96 | unsigned ToClassIdx; |
| 97 | IdxVec ProcIndices; |
| 98 | RecVec PredTerm; |
| 99 | }; |
| 100 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 101 | /// Scheduling class. |
| 102 | /// |
| 103 | /// Each instruction description will be mapped to a scheduling class. There are |
| 104 | /// four types of classes: |
| 105 | /// |
| 106 | /// 1) An explicitly defined itinerary class with ItinClassDef set. |
| 107 | /// Writes and ReadDefs are empty. ProcIndices contains 0 for any processor. |
| 108 | /// |
| 109 | /// 2) An implied class with a list of SchedWrites and SchedReads that are |
| 110 | /// defined in an instruction definition and which are common across all |
| 111 | /// subtargets. ProcIndices contains 0 for any processor. |
| 112 | /// |
| 113 | /// 3) An implied class with a list of InstRW records that map instructions to |
| 114 | /// SchedWrites and SchedReads per-processor. InstrClassMap should map the same |
| 115 | /// instructions to this class. ProcIndices contains all the processors that |
| 116 | /// provided InstrRW records for this class. ItinClassDef or Writes/Reads may |
| 117 | /// still be defined for processors with no InstRW entry. |
| 118 | /// |
| 119 | /// 4) An inferred class represents a variant of another class that may be |
| 120 | /// resolved at runtime. ProcIndices contains the set of processors that may |
| 121 | /// require the class. ProcIndices are propagated through SchedClasses as |
| 122 | /// variants are expanded. Multiple SchedClasses may be inferred from an |
| 123 | /// itinerary class. Each inherits the processor index from the ItinRW record |
| 124 | /// that mapped the itinerary class to the variant Writes or Reads. |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 125 | struct CodeGenSchedClass { |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 126 | unsigned Index; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 127 | std::string Name; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 128 | Record *ItinClassDef; |
| 129 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 130 | IdxVec Writes; |
| 131 | IdxVec Reads; |
| 132 | // Sorted list of ProcIdx, where ProcIdx==0 implies any processor. |
| 133 | IdxVec ProcIndices; |
| 134 | |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 135 | std::vector<CodeGenSchedTransition> Transitions; |
| 136 | |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 137 | // InstRW records associated with this class. These records may refer to an |
| 138 | // Instruction no longer mapped to this class by InstrClassMap. These |
| 139 | // Instructions should be ignored by this class because they have been split |
| 140 | // off to join another inferred class. |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 141 | RecVec InstRWs; |
| 142 | |
Craig Topper | 281a19c | 2018-03-22 06:15:08 +0000 | [diff] [blame] | 143 | CodeGenSchedClass(unsigned Index, std::string Name, Record *ItinClassDef) |
| 144 | : Index(Index), Name(std::move(Name)), ItinClassDef(ItinClassDef) {} |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 145 | |
Simon Pilgrim | 4cca3b1 | 2018-03-21 17:57:21 +0000 | [diff] [blame] | 146 | bool isKeyEqual(Record *IC, ArrayRef<unsigned> W, |
| 147 | ArrayRef<unsigned> R) const { |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 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 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 161 | /// Represent the cost of allocating a register of register class RCDef. |
| 162 | /// |
| 163 | /// The cost of allocating a register is equivalent to the number of physical |
| 164 | /// registers used by the register renamer. Register costs are defined at |
| 165 | /// register class granularity. |
| 166 | struct CodeGenRegisterCost { |
| 167 | Record *RCDef; |
| 168 | unsigned Cost; |
| 169 | CodeGenRegisterCost(Record *RC, unsigned RegisterCost) |
| 170 | : RCDef(RC), Cost(RegisterCost) {} |
| 171 | CodeGenRegisterCost(const CodeGenRegisterCost &) = default; |
| 172 | CodeGenRegisterCost &operator=(const CodeGenRegisterCost &) = delete; |
| 173 | }; |
| 174 | |
| 175 | /// A processor register file. |
| 176 | /// |
| 177 | /// This class describes a processor register file. Register file information is |
| 178 | /// currently consumed by external tools like llvm-mca to predict dispatch |
| 179 | /// stalls due to register pressure. |
| 180 | struct CodeGenRegisterFile { |
| 181 | std::string Name; |
| 182 | Record *RegisterFileDef; |
| 183 | |
| 184 | unsigned NumPhysRegs; |
| 185 | std::vector<CodeGenRegisterCost> Costs; |
| 186 | |
| 187 | CodeGenRegisterFile(StringRef name, Record *def) |
| 188 | : Name(name), RegisterFileDef(def), NumPhysRegs(0) {} |
| 189 | |
| 190 | bool hasDefaultCosts() const { return Costs.empty(); } |
| 191 | }; |
| 192 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 193 | // Processor model. |
| 194 | // |
| 195 | // ModelName is a unique name used to name an instantiation of MCSchedModel. |
| 196 | // |
| 197 | // ModelDef is NULL for inferred Models. This happens when a processor defines |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 198 | // 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] | 199 | // model nor itinerary, then ModelDef remains pointing to NoModel. NoModel has |
| 200 | // the special "NoModel" field set to true. |
| 201 | // |
| 202 | // ItinsDef always points to a valid record definition, but may point to the |
| 203 | // default NoItineraries. NoItineraries has an empty list of InstrItinData |
| 204 | // records. |
| 205 | // |
| 206 | // ItinDefList orders this processor's InstrItinData records by SchedClass idx. |
| 207 | struct CodeGenProcModel { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 208 | unsigned Index; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 209 | std::string ModelName; |
| 210 | Record *ModelDef; |
| 211 | Record *ItinsDef; |
| 212 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 213 | // Derived members... |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 214 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 215 | // Array of InstrItinData records indexed by a CodeGenSchedClass index. |
| 216 | // This list is empty if the Processor has no value for Itineraries. |
| 217 | // Initialized by collectProcItins(). |
| 218 | RecVec ItinDefList; |
| 219 | |
| 220 | // Map itinerary classes to per-operand resources. |
| 221 | // This list is empty if no ItinRW refers to this Processor. |
| 222 | RecVec ItinRWDefs; |
| 223 | |
Simon Dardis | 5f95c9a | 2016-06-24 08:43:27 +0000 | [diff] [blame] | 224 | // List of unsupported feature. |
| 225 | // This list is empty if the Processor has no UnsupportedFeatures. |
| 226 | RecVec UnsupportedFeaturesDefs; |
| 227 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 228 | // All read/write resources associated with this processor. |
| 229 | RecVec WriteResDefs; |
| 230 | RecVec ReadAdvanceDefs; |
| 231 | |
| 232 | // Per-operand machine model resources associated with this processor. |
| 233 | RecVec ProcResourceDefs; |
| 234 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 235 | // List of Register Files. |
| 236 | std::vector<CodeGenRegisterFile> RegisterFiles; |
| 237 | |
Andrea Di Biagio | c74ad50 | 2018-04-05 15:41:41 +0000 | [diff] [blame] | 238 | // Optional Retire Control Unit definition. |
| 239 | Record *RetireControlUnit; |
| 240 | |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 241 | // List of PfmCounters. |
| 242 | RecVec PfmIssueCounterDefs; |
| 243 | Record *PfmCycleCounterDef = nullptr; |
| 244 | |
Craig Topper | 281a19c | 2018-03-22 06:15:08 +0000 | [diff] [blame] | 245 | CodeGenProcModel(unsigned Idx, std::string Name, Record *MDef, |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 246 | Record *IDef) : |
Andrea Di Biagio | c74ad50 | 2018-04-05 15:41:41 +0000 | [diff] [blame] | 247 | Index(Idx), ModelName(std::move(Name)), ModelDef(MDef), ItinsDef(IDef), |
| 248 | RetireControlUnit(nullptr) {} |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 249 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 250 | bool hasItineraries() const { |
| 251 | return !ItinsDef->getValueAsListOfDefs("IID").empty(); |
| 252 | } |
| 253 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 254 | bool hasInstrSchedModel() const { |
| 255 | return !WriteResDefs.empty() || !ItinRWDefs.empty(); |
| 256 | } |
| 257 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 258 | bool hasExtraProcessorInfo() const { |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 259 | return RetireControlUnit || !RegisterFiles.empty() || |
| 260 | !PfmIssueCounterDefs.empty() || |
| 261 | PfmCycleCounterDef != nullptr; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 264 | unsigned getProcResourceIdx(Record *PRDef) const; |
| 265 | |
Simon Dardis | 5f95c9a | 2016-06-24 08:43:27 +0000 | [diff] [blame] | 266 | bool isUnsupported(const CodeGenInstruction &Inst) const; |
| 267 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 268 | #ifndef NDEBUG |
| 269 | void dump() const; |
| 270 | #endif |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 273 | /// Top level container for machine model data. |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 274 | class CodeGenSchedModels { |
| 275 | RecordKeeper &Records; |
| 276 | const CodeGenTarget &Target; |
| 277 | |
Andrew Trick | 9e1deb6 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 278 | // Map dag expressions to Instruction lists. |
| 279 | SetTheory Sets; |
| 280 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 281 | // List of unique processor models. |
| 282 | std::vector<CodeGenProcModel> ProcModels; |
| 283 | |
| 284 | // Map Processor's MachineModel or ProcItin to a CodeGenProcModel index. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 285 | using ProcModelMapTy = DenseMap<Record*, unsigned>; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 286 | ProcModelMapTy ProcModelMap; |
| 287 | |
| 288 | // Per-operand SchedReadWrite types. |
| 289 | std::vector<CodeGenSchedRW> SchedWrites; |
| 290 | std::vector<CodeGenSchedRW> SchedReads; |
| 291 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 292 | // List of unique SchedClasses. |
| 293 | std::vector<CodeGenSchedClass> SchedClasses; |
| 294 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 295 | // Any inferred SchedClass has an index greater than NumInstrSchedClassses. |
| 296 | unsigned NumInstrSchedClasses; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 297 | |
Matthias Braun | 6b1fd9a | 2016-06-21 03:24:03 +0000 | [diff] [blame] | 298 | RecVec ProcResourceDefs; |
| 299 | RecVec ProcResGroups; |
| 300 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 301 | // Map each instruction to its unique SchedClass index considering the |
| 302 | // combination of it's itinerary class, SchedRW list, and InstRW records. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 303 | using InstClassMapTy = DenseMap<Record*, unsigned>; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 304 | InstClassMapTy InstrClassMap; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 305 | |
| 306 | public: |
| 307 | CodeGenSchedModels(RecordKeeper& RK, const CodeGenTarget &TGT); |
| 308 | |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 309 | // iterator access to the scheduling classes. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 310 | using class_iterator = std::vector<CodeGenSchedClass>::iterator; |
| 311 | using const_class_iterator = std::vector<CodeGenSchedClass>::const_iterator; |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 312 | class_iterator classes_begin() { return SchedClasses.begin(); } |
| 313 | const_class_iterator classes_begin() const { return SchedClasses.begin(); } |
| 314 | class_iterator classes_end() { return SchedClasses.end(); } |
| 315 | const_class_iterator classes_end() const { return SchedClasses.end(); } |
| 316 | iterator_range<class_iterator> classes() { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 317 | return make_range(classes_begin(), classes_end()); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 318 | } |
| 319 | iterator_range<const_class_iterator> classes() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 320 | return make_range(classes_begin(), classes_end()); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 321 | } |
| 322 | iterator_range<class_iterator> explicit_classes() { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 323 | return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 324 | } |
| 325 | iterator_range<const_class_iterator> explicit_classes() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 326 | return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses); |
Jim Grosbach | af81445 | 2014-04-18 02:09:04 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 329 | Record *getModelOrItinDef(Record *ProcDef) const { |
| 330 | Record *ModelDef = ProcDef->getValueAsDef("SchedModel"); |
| 331 | Record *ItinsDef = ProcDef->getValueAsDef("ProcItin"); |
| 332 | if (!ItinsDef->getValueAsListOfDefs("IID").empty()) { |
| 333 | assert(ModelDef->getValueAsBit("NoModel") |
| 334 | && "Itineraries must be defined within SchedMachineModel"); |
| 335 | return ItinsDef; |
| 336 | } |
| 337 | return ModelDef; |
| 338 | } |
| 339 | |
| 340 | const CodeGenProcModel &getModelForProc(Record *ProcDef) const { |
| 341 | Record *ModelDef = getModelOrItinDef(ProcDef); |
| 342 | ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); |
| 343 | assert(I != ProcModelMap.end() && "missing machine model"); |
| 344 | return ProcModels[I->second]; |
| 345 | } |
| 346 | |
Andrew Trick | 40c4f38 | 2013-06-15 04:50:06 +0000 | [diff] [blame] | 347 | CodeGenProcModel &getProcModel(Record *ModelDef) { |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 348 | ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); |
| 349 | assert(I != ProcModelMap.end() && "missing machine model"); |
| 350 | return ProcModels[I->second]; |
| 351 | } |
Andrew Trick | 40c4f38 | 2013-06-15 04:50:06 +0000 | [diff] [blame] | 352 | const CodeGenProcModel &getProcModel(Record *ModelDef) const { |
| 353 | return const_cast<CodeGenSchedModels*>(this)->getProcModel(ModelDef); |
| 354 | } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 355 | |
| 356 | // Iterate over the unique processor models. |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 357 | using ProcIter = std::vector<CodeGenProcModel>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 358 | ProcIter procModelBegin() const { return ProcModels.begin(); } |
| 359 | ProcIter procModelEnd() const { return ProcModels.end(); } |
Craig Topper | 29c55dcb | 2016-02-13 06:03:32 +0000 | [diff] [blame] | 360 | ArrayRef<CodeGenProcModel> procModels() const { return ProcModels; } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 361 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 362 | // Return true if any processors have itineraries. |
| 363 | bool hasItineraries() const; |
| 364 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 365 | // Get a SchedWrite from its index. |
| 366 | const CodeGenSchedRW &getSchedWrite(unsigned Idx) const { |
| 367 | assert(Idx < SchedWrites.size() && "bad SchedWrite index"); |
| 368 | assert(SchedWrites[Idx].isValid() && "invalid SchedWrite"); |
| 369 | return SchedWrites[Idx]; |
| 370 | } |
| 371 | // Get a SchedWrite from its index. |
| 372 | const CodeGenSchedRW &getSchedRead(unsigned Idx) const { |
| 373 | assert(Idx < SchedReads.size() && "bad SchedRead index"); |
| 374 | assert(SchedReads[Idx].isValid() && "invalid SchedRead"); |
| 375 | return SchedReads[Idx]; |
| 376 | } |
| 377 | |
| 378 | const CodeGenSchedRW &getSchedRW(unsigned Idx, bool IsRead) const { |
| 379 | return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx); |
| 380 | } |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 381 | CodeGenSchedRW &getSchedRW(Record *Def) { |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 382 | bool IsRead = Def->isSubClassOf("SchedRead"); |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 383 | unsigned Idx = getSchedRWIdx(Def, IsRead); |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 384 | return const_cast<CodeGenSchedRW&>( |
| 385 | IsRead ? getSchedRead(Idx) : getSchedWrite(Idx)); |
| 386 | } |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 387 | const CodeGenSchedRW &getSchedRW(Record*Def) const { |
| 388 | return const_cast<CodeGenSchedModels&>(*this).getSchedRW(Def); |
Andrew Trick | 9257b8f | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 389 | } |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 390 | |
Craig Topper | e261184 | 2018-03-21 05:13:04 +0000 | [diff] [blame] | 391 | unsigned getSchedRWIdx(Record *Def, bool IsRead) const; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 392 | |
Andrew Trick | cfe222c | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 393 | // Return true if the given write record is referenced by a ReadAdvance. |
| 394 | bool hasReadOfWrite(Record *WriteDef) const; |
| 395 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 396 | // Get a SchedClass from its index. |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 397 | CodeGenSchedClass &getSchedClass(unsigned Idx) { |
| 398 | assert(Idx < SchedClasses.size() && "bad SchedClass index"); |
| 399 | return SchedClasses[Idx]; |
| 400 | } |
| 401 | const CodeGenSchedClass &getSchedClass(unsigned Idx) const { |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 402 | assert(Idx < SchedClasses.size() && "bad SchedClass index"); |
| 403 | return SchedClasses[Idx]; |
| 404 | } |
| 405 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 406 | // Get the SchedClass index for an instruction. Instructions with no |
| 407 | // itinerary, no SchedReadWrites, and no InstrReadWrites references return 0 |
| 408 | // for NoItinerary. |
| 409 | unsigned getSchedClassIdx(const CodeGenInstruction &Inst) const; |
| 410 | |
Javed Absar | 67b042c | 2017-09-13 10:31:10 +0000 | [diff] [blame] | 411 | using SchedClassIter = std::vector<CodeGenSchedClass>::const_iterator; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 412 | SchedClassIter schedClassBegin() const { return SchedClasses.begin(); } |
| 413 | SchedClassIter schedClassEnd() const { return SchedClasses.end(); } |
Craig Topper | 29c55dcb | 2016-02-13 06:03:32 +0000 | [diff] [blame] | 414 | ArrayRef<CodeGenSchedClass> schedClasses() const { return SchedClasses; } |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 415 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 416 | unsigned numInstrSchedClasses() const { return NumInstrSchedClasses; } |
| 417 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 418 | void findRWs(const RecVec &RWDefs, IdxVec &Writes, IdxVec &Reads) const; |
| 419 | void findRWs(const RecVec &RWDefs, IdxVec &RWs, bool IsRead) const; |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 420 | void expandRWSequence(unsigned RWIdx, IdxVec &RWSeq, bool IsRead) const; |
Andrew Trick | da984b1 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 421 | void expandRWSeqForProc(unsigned RWIdx, IdxVec &RWSeq, bool IsRead, |
| 422 | const CodeGenProcModel &ProcModel) const; |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 423 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 424 | unsigned addSchedClass(Record *ItinDef, ArrayRef<unsigned> OperWrites, |
| 425 | ArrayRef<unsigned> OperReads, |
| 426 | ArrayRef<unsigned> ProcIndices); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 427 | |
| 428 | unsigned findOrInsertRW(ArrayRef<unsigned> Seq, bool IsRead); |
| 429 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 430 | unsigned findSchedClassIdx(Record *ItinClassDef, ArrayRef<unsigned> Writes, |
| 431 | ArrayRef<unsigned> Reads) const; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 432 | |
Evandro Menezes | 9dc54e2 | 2017-11-21 21:33:52 +0000 | [diff] [blame] | 433 | Record *findProcResUnits(Record *ProcResKind, const CodeGenProcModel &PM, |
| 434 | ArrayRef<SMLoc> Loc) const; |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 435 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 436 | private: |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 437 | void collectProcModels(); |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 438 | |
| 439 | // Initialize a new processor model if it is unique. |
| 440 | void addProcModel(Record *ProcDef); |
| 441 | |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 442 | void collectSchedRW(); |
| 443 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 444 | std::string genRWName(ArrayRef<unsigned> Seq, bool IsRead); |
| 445 | unsigned findRWForSequence(ArrayRef<unsigned> Seq, bool IsRead); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 446 | |
| 447 | void collectSchedClasses(); |
| 448 | |
Andrea Di Biagio | c74ad50 | 2018-04-05 15:41:41 +0000 | [diff] [blame] | 449 | void collectRetireControlUnits(); |
| 450 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 451 | void collectRegisterFiles(); |
| 452 | |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 453 | void collectPfmCounters(); |
| 454 | |
Andrea Di Biagio | c74ad50 | 2018-04-05 15:41:41 +0000 | [diff] [blame] | 455 | void collectOptionalProcessorInfo(); |
| 456 | |
Andrew Trick | bf8a28d | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 457 | std::string createSchedClassName(Record *ItinClassDef, |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 458 | ArrayRef<unsigned> OperWrites, |
| 459 | ArrayRef<unsigned> OperReads); |
Andrew Trick | 7668649 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 460 | std::string createSchedClassName(const RecVec &InstDefs); |
| 461 | void createInstRWClass(Record *InstRWDef); |
| 462 | |
| 463 | void collectProcItins(); |
| 464 | |
| 465 | void collectProcItinRW(); |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 466 | |
Simon Dardis | 5f95c9a | 2016-06-24 08:43:27 +0000 | [diff] [blame] | 467 | void collectProcUnsupportedFeatures(); |
| 468 | |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 469 | void inferSchedClasses(); |
| 470 | |
Matthias Braun | 17cb579 | 2016-03-01 20:03:21 +0000 | [diff] [blame] | 471 | void checkCompleteness(); |
| 472 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 473 | void inferFromRW(ArrayRef<unsigned> OperWrites, ArrayRef<unsigned> OperReads, |
| 474 | unsigned FromClassIdx, ArrayRef<unsigned> ProcIndices); |
Andrew Trick | 33401e8 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 475 | void inferFromItinClass(Record *ItinClassDef, unsigned FromClassIdx); |
| 476 | void inferFromInstRWs(unsigned SCIdx); |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 477 | |
Andrew Trick | cf398b2 | 2013-04-23 23:45:14 +0000 | [diff] [blame] | 478 | bool hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM); |
| 479 | void verifyProcResourceGroups(CodeGenProcModel &PM); |
| 480 | |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 481 | void collectProcResources(); |
| 482 | |
| 483 | void collectItinProcResources(Record *ItinClassDef); |
| 484 | |
Andrew Trick | d0b9c44 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 485 | void collectRWResources(unsigned RWIdx, bool IsRead, |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 486 | ArrayRef<unsigned> ProcIndices); |
Andrew Trick | d0b9c44 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 487 | |
Benjamin Kramer | e176195 | 2015-10-24 12:46:49 +0000 | [diff] [blame] | 488 | void collectRWResources(ArrayRef<unsigned> Writes, ArrayRef<unsigned> Reads, |
| 489 | ArrayRef<unsigned> ProcIndices); |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 490 | |
Evandro Menezes | 9dc54e2 | 2017-11-21 21:33:52 +0000 | [diff] [blame] | 491 | void addProcResource(Record *ProcResourceKind, CodeGenProcModel &PM, |
| 492 | ArrayRef<SMLoc> Loc); |
Andrew Trick | 1e46d48 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 493 | |
| 494 | void addWriteRes(Record *ProcWriteResDef, unsigned PIdx); |
| 495 | |
| 496 | void addReadAdvance(Record *ProcReadAdvanceDef, unsigned PIdx); |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | } // namespace llvm |
| 500 | |
| 501 | #endif |