Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1 | //===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===// |
| 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 decribed in |
| 11 | // the target description. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "subtarget-emitter" |
| 16 | |
| 17 | #include "CodeGenSchedule.h" |
| 18 | #include "CodeGenTarget.h" |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 19 | #include "llvm/TableGen/Error.h" |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Regex.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 26 | #ifndef NDEBUG |
| 27 | static void dumpIdxVec(const IdxVec &V) { |
| 28 | for (unsigned i = 0, e = V.size(); i < e; ++i) { |
| 29 | dbgs() << V[i] << ", "; |
| 30 | } |
| 31 | } |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 32 | static void dumpIdxVec(const SmallVectorImpl<unsigned> &V) { |
| 33 | for (unsigned i = 0, e = V.size(); i < e; ++i) { |
| 34 | dbgs() << V[i] << ", "; |
| 35 | } |
| 36 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 39 | // (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp. |
| 40 | struct InstrsOp : public SetTheory::Operator { |
Joerg Sonnenberger | 2c6d713 | 2012-10-24 22:03:59 +0000 | [diff] [blame] | 41 | void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 42 | ArrayRef<SMLoc> Loc) { |
| 43 | ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc); |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 44 | } |
| 45 | }; |
| 46 | |
| 47 | // (instregex "OpcPat",...) Find all instructions matching an opcode pattern. |
| 48 | // |
| 49 | // TODO: Since this is a prefix match, perform a binary search over the |
| 50 | // instruction names using lower_bound. Note that the predefined instrs must be |
| 51 | // scanned linearly first. However, this is only safe if the regex pattern has |
| 52 | // no top-level bars. The DAG already has a list of patterns, so there's no |
| 53 | // reason to use top-level bars, but we need a way to verify they don't exist |
| 54 | // before implementing the optimization. |
| 55 | struct InstRegexOp : public SetTheory::Operator { |
| 56 | const CodeGenTarget &Target; |
| 57 | InstRegexOp(const CodeGenTarget &t): Target(t) {} |
| 58 | |
Joerg Sonnenberger | 2c6d713 | 2012-10-24 22:03:59 +0000 | [diff] [blame] | 59 | void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 60 | ArrayRef<SMLoc> Loc) { |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 61 | SmallVector<Regex*, 4> RegexList; |
| 62 | for (DagInit::const_arg_iterator |
| 63 | AI = Expr->arg_begin(), AE = Expr->arg_end(); AI != AE; ++AI) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 64 | StringInit *SI = dyn_cast<StringInit>(*AI); |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 65 | if (!SI) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 66 | PrintFatalError(Loc, "instregex requires pattern string: " |
Joerg Sonnenberger | 2c6d713 | 2012-10-24 22:03:59 +0000 | [diff] [blame] | 67 | + Expr->getAsString()); |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 68 | std::string pat = SI->getValue(); |
| 69 | // Implement a python-style prefix match. |
| 70 | if (pat[0] != '^') { |
| 71 | pat.insert(0, "^("); |
| 72 | pat.insert(pat.end(), ')'); |
| 73 | } |
| 74 | RegexList.push_back(new Regex(pat)); |
| 75 | } |
| 76 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 77 | E = Target.inst_end(); I != E; ++I) { |
| 78 | for (SmallVectorImpl<Regex*>::iterator |
| 79 | RI = RegexList.begin(), RE = RegexList.end(); RI != RE; ++RI) { |
| 80 | if ((*RI)->match((*I)->TheDef->getName())) |
| 81 | Elts.insert((*I)->TheDef); |
| 82 | } |
| 83 | } |
| 84 | DeleteContainerPointers(RegexList); |
| 85 | } |
| 86 | }; |
| 87 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 88 | /// CodeGenModels ctor interprets machine model records and populates maps. |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 89 | CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK, |
| 90 | const CodeGenTarget &TGT): |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 91 | Records(RK), Target(TGT), NumItineraryClasses(0) { |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 92 | |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 93 | Sets.addFieldExpander("InstRW", "Instrs"); |
| 94 | |
| 95 | // Allow Set evaluation to recognize the dags used in InstRW records: |
| 96 | // (instrs Op1, Op1...) |
| 97 | Sets.addOperator("instrs", new InstrsOp); |
| 98 | Sets.addOperator("instregex", new InstRegexOp(Target)); |
| 99 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 100 | // Instantiate a CodeGenProcModel for each SchedMachineModel with the values |
| 101 | // that are explicitly referenced in tablegen records. Resources associated |
| 102 | // with each processor will be derived later. Populate ProcModelMap with the |
| 103 | // CodeGenProcModel instances. |
| 104 | collectProcModels(); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 105 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 106 | // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly |
| 107 | // defined, and populate SchedReads and SchedWrites vectors. Implicit |
| 108 | // SchedReadWrites that represent sequences derived from expanded variant will |
| 109 | // be inferred later. |
| 110 | collectSchedRW(); |
| 111 | |
| 112 | // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly |
| 113 | // required by an instruction definition, and populate SchedClassIdxMap. Set |
| 114 | // NumItineraryClasses to the number of explicit itinerary classes referenced |
| 115 | // by instructions. Set NumInstrSchedClasses to the number of itinerary |
| 116 | // classes plus any classes implied by instructions that derive from class |
| 117 | // Sched and provide SchedRW list. This does not infer any new classes from |
| 118 | // SchedVariant. |
| 119 | collectSchedClasses(); |
| 120 | |
| 121 | // Find instruction itineraries for each processor. Sort and populate |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 122 | // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 123 | // all itinerary classes to be discovered. |
| 124 | collectProcItins(); |
| 125 | |
| 126 | // Find ItinRW records for each processor and itinerary class. |
| 127 | // (For per-operand resources mapped to itinerary classes). |
| 128 | collectProcItinRW(); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 129 | |
| 130 | // Infer new SchedClasses from SchedVariant. |
| 131 | inferSchedClasses(); |
| 132 | |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 133 | // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and |
| 134 | // ProcResourceDefs. |
| 135 | collectProcResources(); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 138 | /// Gather all processor models. |
| 139 | void CodeGenSchedModels::collectProcModels() { |
| 140 | RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor"); |
| 141 | std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName()); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 142 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 143 | // Reserve space because we can. Reallocation would be ok. |
| 144 | ProcModels.reserve(ProcRecords.size()+1); |
| 145 | |
| 146 | // Use idx=0 for NoModel/NoItineraries. |
| 147 | Record *NoModelDef = Records.getDef("NoSchedModel"); |
| 148 | Record *NoItinsDef = Records.getDef("NoItineraries"); |
| 149 | ProcModels.push_back(CodeGenProcModel(0, "NoSchedModel", |
| 150 | NoModelDef, NoItinsDef)); |
| 151 | ProcModelMap[NoModelDef] = 0; |
| 152 | |
| 153 | // For each processor, find a unique machine model. |
| 154 | for (unsigned i = 0, N = ProcRecords.size(); i < N; ++i) |
| 155 | addProcModel(ProcRecords[i]); |
| 156 | } |
| 157 | |
| 158 | /// Get a unique processor model based on the defined MachineModel and |
| 159 | /// ProcessorItineraries. |
| 160 | void CodeGenSchedModels::addProcModel(Record *ProcDef) { |
| 161 | Record *ModelKey = getModelOrItinDef(ProcDef); |
| 162 | if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second) |
| 163 | return; |
| 164 | |
| 165 | std::string Name = ModelKey->getName(); |
| 166 | if (ModelKey->isSubClassOf("SchedMachineModel")) { |
| 167 | Record *ItinsDef = ModelKey->getValueAsDef("Itineraries"); |
| 168 | ProcModels.push_back( |
| 169 | CodeGenProcModel(ProcModels.size(), Name, ModelKey, ItinsDef)); |
| 170 | } |
| 171 | else { |
| 172 | // An itinerary is defined without a machine model. Infer a new model. |
| 173 | if (!ModelKey->getValueAsListOfDefs("IID").empty()) |
| 174 | Name = Name + "Model"; |
| 175 | ProcModels.push_back( |
| 176 | CodeGenProcModel(ProcModels.size(), Name, |
| 177 | ProcDef->getValueAsDef("SchedModel"), ModelKey)); |
| 178 | } |
| 179 | DEBUG(ProcModels.back().dump()); |
| 180 | } |
| 181 | |
| 182 | // Recursively find all reachable SchedReadWrite records. |
| 183 | static void scanSchedRW(Record *RWDef, RecVec &RWDefs, |
| 184 | SmallPtrSet<Record*, 16> &RWSet) { |
| 185 | if (!RWSet.insert(RWDef)) |
| 186 | return; |
| 187 | RWDefs.push_back(RWDef); |
| 188 | // Reads don't current have sequence records, but it can be added later. |
| 189 | if (RWDef->isSubClassOf("WriteSequence")) { |
| 190 | RecVec Seq = RWDef->getValueAsListOfDefs("Writes"); |
| 191 | for (RecIter I = Seq.begin(), E = Seq.end(); I != E; ++I) |
| 192 | scanSchedRW(*I, RWDefs, RWSet); |
| 193 | } |
| 194 | else if (RWDef->isSubClassOf("SchedVariant")) { |
| 195 | // Visit each variant (guarded by a different predicate). |
| 196 | RecVec Vars = RWDef->getValueAsListOfDefs("Variants"); |
| 197 | for (RecIter VI = Vars.begin(), VE = Vars.end(); VI != VE; ++VI) { |
| 198 | // Visit each RW in the sequence selected by the current variant. |
| 199 | RecVec Selected = (*VI)->getValueAsListOfDefs("Selected"); |
| 200 | for (RecIter I = Selected.begin(), E = Selected.end(); I != E; ++I) |
| 201 | scanSchedRW(*I, RWDefs, RWSet); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Collect and sort all SchedReadWrites reachable via tablegen records. |
| 207 | // More may be inferred later when inferring new SchedClasses from variants. |
| 208 | void CodeGenSchedModels::collectSchedRW() { |
| 209 | // Reserve idx=0 for invalid writes/reads. |
| 210 | SchedWrites.resize(1); |
| 211 | SchedReads.resize(1); |
| 212 | |
| 213 | SmallPtrSet<Record*, 16> RWSet; |
| 214 | |
| 215 | // Find all SchedReadWrites referenced by instruction defs. |
| 216 | RecVec SWDefs, SRDefs; |
| 217 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 218 | E = Target.inst_end(); I != E; ++I) { |
| 219 | Record *SchedDef = (*I)->TheDef; |
| 220 | if (!SchedDef->isSubClassOf("Sched")) |
| 221 | continue; |
| 222 | RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW"); |
| 223 | for (RecIter RWI = RWs.begin(), RWE = RWs.end(); RWI != RWE; ++RWI) { |
| 224 | if ((*RWI)->isSubClassOf("SchedWrite")) |
| 225 | scanSchedRW(*RWI, SWDefs, RWSet); |
| 226 | else { |
| 227 | assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite"); |
| 228 | scanSchedRW(*RWI, SRDefs, RWSet); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | // Find all ReadWrites referenced by InstRW. |
| 233 | RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW"); |
| 234 | for (RecIter OI = InstRWDefs.begin(), OE = InstRWDefs.end(); OI != OE; ++OI) { |
| 235 | // For all OperandReadWrites. |
| 236 | RecVec RWDefs = (*OI)->getValueAsListOfDefs("OperandReadWrites"); |
| 237 | for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); |
| 238 | RWI != RWE; ++RWI) { |
| 239 | if ((*RWI)->isSubClassOf("SchedWrite")) |
| 240 | scanSchedRW(*RWI, SWDefs, RWSet); |
| 241 | else { |
| 242 | assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite"); |
| 243 | scanSchedRW(*RWI, SRDefs, RWSet); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | // Find all ReadWrites referenced by ItinRW. |
| 248 | RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW"); |
| 249 | for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) { |
| 250 | // For all OperandReadWrites. |
| 251 | RecVec RWDefs = (*II)->getValueAsListOfDefs("OperandReadWrites"); |
| 252 | for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); |
| 253 | RWI != RWE; ++RWI) { |
| 254 | if ((*RWI)->isSubClassOf("SchedWrite")) |
| 255 | scanSchedRW(*RWI, SWDefs, RWSet); |
| 256 | else { |
| 257 | assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite"); |
| 258 | scanSchedRW(*RWI, SRDefs, RWSet); |
| 259 | } |
| 260 | } |
| 261 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 262 | // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted |
| 263 | // for the loop below that initializes Alias vectors. |
| 264 | RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias"); |
| 265 | std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord()); |
| 266 | for (RecIter AI = AliasDefs.begin(), AE = AliasDefs.end(); AI != AE; ++AI) { |
| 267 | Record *MatchDef = (*AI)->getValueAsDef("MatchRW"); |
| 268 | Record *AliasDef = (*AI)->getValueAsDef("AliasRW"); |
| 269 | if (MatchDef->isSubClassOf("SchedWrite")) { |
| 270 | if (!AliasDef->isSubClassOf("SchedWrite")) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 271 | PrintFatalError((*AI)->getLoc(), "SchedWrite Alias must be SchedWrite"); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 272 | scanSchedRW(AliasDef, SWDefs, RWSet); |
| 273 | } |
| 274 | else { |
| 275 | assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite"); |
| 276 | if (!AliasDef->isSubClassOf("SchedRead")) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 277 | PrintFatalError((*AI)->getLoc(), "SchedRead Alias must be SchedRead"); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 278 | scanSchedRW(AliasDef, SRDefs, RWSet); |
| 279 | } |
| 280 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 281 | // Sort and add the SchedReadWrites directly referenced by instructions or |
| 282 | // itinerary resources. Index reads and writes in separate domains. |
| 283 | std::sort(SWDefs.begin(), SWDefs.end(), LessRecord()); |
| 284 | for (RecIter SWI = SWDefs.begin(), SWE = SWDefs.end(); SWI != SWE; ++SWI) { |
| 285 | assert(!getSchedRWIdx(*SWI, /*IsRead=*/false) && "duplicate SchedWrite"); |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 286 | SchedWrites.push_back(CodeGenSchedRW(SchedWrites.size(), *SWI)); |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 287 | } |
| 288 | std::sort(SRDefs.begin(), SRDefs.end(), LessRecord()); |
| 289 | for (RecIter SRI = SRDefs.begin(), SRE = SRDefs.end(); SRI != SRE; ++SRI) { |
| 290 | assert(!getSchedRWIdx(*SRI, /*IsRead-*/true) && "duplicate SchedWrite"); |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 291 | SchedReads.push_back(CodeGenSchedRW(SchedReads.size(), *SRI)); |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 292 | } |
| 293 | // Initialize WriteSequence vectors. |
| 294 | for (std::vector<CodeGenSchedRW>::iterator WI = SchedWrites.begin(), |
| 295 | WE = SchedWrites.end(); WI != WE; ++WI) { |
| 296 | if (!WI->IsSequence) |
| 297 | continue; |
| 298 | findRWs(WI->TheDef->getValueAsListOfDefs("Writes"), WI->Sequence, |
| 299 | /*IsRead=*/false); |
| 300 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 301 | // Initialize Aliases vectors. |
| 302 | for (RecIter AI = AliasDefs.begin(), AE = AliasDefs.end(); AI != AE; ++AI) { |
| 303 | Record *AliasDef = (*AI)->getValueAsDef("AliasRW"); |
| 304 | getSchedRW(AliasDef).IsAlias = true; |
| 305 | Record *MatchDef = (*AI)->getValueAsDef("MatchRW"); |
| 306 | CodeGenSchedRW &RW = getSchedRW(MatchDef); |
| 307 | if (RW.IsAlias) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 308 | PrintFatalError((*AI)->getLoc(), "Cannot Alias an Alias"); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 309 | RW.Aliases.push_back(*AI); |
| 310 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 311 | DEBUG( |
| 312 | for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) { |
| 313 | dbgs() << WIdx << ": "; |
| 314 | SchedWrites[WIdx].dump(); |
| 315 | dbgs() << '\n'; |
| 316 | } |
| 317 | for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) { |
| 318 | dbgs() << RIdx << ": "; |
| 319 | SchedReads[RIdx].dump(); |
| 320 | dbgs() << '\n'; |
| 321 | } |
| 322 | RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite"); |
| 323 | for (RecIter RI = RWDefs.begin(), RE = RWDefs.end(); |
| 324 | RI != RE; ++RI) { |
| 325 | if (!getSchedRWIdx(*RI, (*RI)->isSubClassOf("SchedRead"))) { |
| 326 | const std::string &Name = (*RI)->getName(); |
| 327 | if (Name != "NoWrite" && Name != "ReadDefault") |
| 328 | dbgs() << "Unused SchedReadWrite " << (*RI)->getName() << '\n'; |
| 329 | } |
| 330 | }); |
| 331 | } |
| 332 | |
| 333 | /// Compute a SchedWrite name from a sequence of writes. |
| 334 | std::string CodeGenSchedModels::genRWName(const IdxVec& Seq, bool IsRead) { |
| 335 | std::string Name("("); |
| 336 | for (IdxIter I = Seq.begin(), E = Seq.end(); I != E; ++I) { |
| 337 | if (I != Seq.begin()) |
| 338 | Name += '_'; |
| 339 | Name += getSchedRW(*I, IsRead).Name; |
| 340 | } |
| 341 | Name += ')'; |
| 342 | return Name; |
| 343 | } |
| 344 | |
| 345 | unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead, |
| 346 | unsigned After) const { |
| 347 | const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites; |
| 348 | assert(After < RWVec.size() && "start position out of bounds"); |
| 349 | for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin() + After, |
| 350 | E = RWVec.end(); I != E; ++I) { |
| 351 | if (I->TheDef == Def) |
| 352 | return I - RWVec.begin(); |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 357 | bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const { |
| 358 | for (unsigned i = 0, e = SchedReads.size(); i < e; ++i) { |
| 359 | Record *ReadDef = SchedReads[i].TheDef; |
| 360 | if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance")) |
| 361 | continue; |
| 362 | |
| 363 | RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites"); |
| 364 | if (std::find(ValidWrites.begin(), ValidWrites.end(), WriteDef) |
| 365 | != ValidWrites.end()) { |
| 366 | return true; |
| 367 | } |
| 368 | } |
| 369 | return false; |
| 370 | } |
| 371 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 372 | namespace llvm { |
| 373 | void splitSchedReadWrites(const RecVec &RWDefs, |
| 374 | RecVec &WriteDefs, RecVec &ReadDefs) { |
| 375 | for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); RWI != RWE; ++RWI) { |
| 376 | if ((*RWI)->isSubClassOf("SchedWrite")) |
| 377 | WriteDefs.push_back(*RWI); |
| 378 | else { |
| 379 | assert((*RWI)->isSubClassOf("SchedRead") && "unknown SchedReadWrite"); |
| 380 | ReadDefs.push_back(*RWI); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } // namespace llvm |
| 385 | |
| 386 | // Split the SchedReadWrites defs and call findRWs for each list. |
| 387 | void CodeGenSchedModels::findRWs(const RecVec &RWDefs, |
| 388 | IdxVec &Writes, IdxVec &Reads) const { |
| 389 | RecVec WriteDefs; |
| 390 | RecVec ReadDefs; |
| 391 | splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs); |
| 392 | findRWs(WriteDefs, Writes, false); |
| 393 | findRWs(ReadDefs, Reads, true); |
| 394 | } |
| 395 | |
| 396 | // Call getSchedRWIdx for all elements in a sequence of SchedRW defs. |
| 397 | void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs, |
| 398 | bool IsRead) const { |
| 399 | for (RecIter RI = RWDefs.begin(), RE = RWDefs.end(); RI != RE; ++RI) { |
| 400 | unsigned Idx = getSchedRWIdx(*RI, IsRead); |
| 401 | assert(Idx && "failed to collect SchedReadWrite"); |
| 402 | RWs.push_back(Idx); |
| 403 | } |
| 404 | } |
| 405 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 406 | void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq, |
| 407 | bool IsRead) const { |
| 408 | const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead); |
| 409 | if (!SchedRW.IsSequence) { |
| 410 | RWSeq.push_back(RWIdx); |
| 411 | return; |
| 412 | } |
| 413 | int Repeat = |
| 414 | SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1; |
| 415 | for (int i = 0; i < Repeat; ++i) { |
| 416 | for (IdxIter I = SchedRW.Sequence.begin(), E = SchedRW.Sequence.end(); |
| 417 | I != E; ++I) { |
| 418 | expandRWSequence(*I, RWSeq, IsRead); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 423 | // Expand a SchedWrite as a sequence following any aliases that coincide with |
| 424 | // the given processor model. |
| 425 | void CodeGenSchedModels::expandRWSeqForProc( |
| 426 | unsigned RWIdx, IdxVec &RWSeq, bool IsRead, |
| 427 | const CodeGenProcModel &ProcModel) const { |
| 428 | |
| 429 | const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead); |
| 430 | Record *AliasDef = 0; |
| 431 | for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end(); |
| 432 | AI != AE; ++AI) { |
| 433 | const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW")); |
| 434 | if ((*AI)->getValueInit("SchedModel")->isComplete()) { |
| 435 | Record *ModelDef = (*AI)->getValueAsDef("SchedModel"); |
| 436 | if (&getProcModel(ModelDef) != &ProcModel) |
| 437 | continue; |
| 438 | } |
| 439 | if (AliasDef) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 440 | PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases " |
| 441 | "defined for processor " + ProcModel.ModelName + |
| 442 | " Ensure only one SchedAlias exists per RW."); |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 443 | AliasDef = AliasRW.TheDef; |
| 444 | } |
| 445 | if (AliasDef) { |
| 446 | expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead), |
| 447 | RWSeq, IsRead,ProcModel); |
| 448 | return; |
| 449 | } |
| 450 | if (!SchedWrite.IsSequence) { |
| 451 | RWSeq.push_back(RWIdx); |
| 452 | return; |
| 453 | } |
| 454 | int Repeat = |
| 455 | SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1; |
| 456 | for (int i = 0; i < Repeat; ++i) { |
| 457 | for (IdxIter I = SchedWrite.Sequence.begin(), E = SchedWrite.Sequence.end(); |
| 458 | I != E; ++I) { |
| 459 | expandRWSeqForProc(*I, RWSeq, IsRead, ProcModel); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 464 | // Find the existing SchedWrite that models this sequence of writes. |
| 465 | unsigned CodeGenSchedModels::findRWForSequence(const IdxVec &Seq, |
| 466 | bool IsRead) { |
| 467 | std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites; |
| 468 | |
| 469 | for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end(); |
| 470 | I != E; ++I) { |
| 471 | if (I->Sequence == Seq) |
| 472 | return I - RWVec.begin(); |
| 473 | } |
| 474 | // Index zero reserved for invalid RW. |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /// Add this ReadWrite if it doesn't already exist. |
| 479 | unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq, |
| 480 | bool IsRead) { |
| 481 | assert(!Seq.empty() && "cannot insert empty sequence"); |
| 482 | if (Seq.size() == 1) |
| 483 | return Seq.back(); |
| 484 | |
| 485 | unsigned Idx = findRWForSequence(Seq, IsRead); |
| 486 | if (Idx) |
| 487 | return Idx; |
| 488 | |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 489 | unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size(); |
| 490 | CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead)); |
| 491 | if (IsRead) |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 492 | SchedReads.push_back(SchedRW); |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 493 | else |
| 494 | SchedWrites.push_back(SchedRW); |
| 495 | return RWIdx; |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 498 | /// Visit all the instruction definitions for this target to gather and |
| 499 | /// enumerate the itinerary classes. These are the explicitly specified |
| 500 | /// SchedClasses. More SchedClasses may be inferred. |
| 501 | void CodeGenSchedModels::collectSchedClasses() { |
| 502 | |
| 503 | // NoItinerary is always the first class at Idx=0 |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 504 | SchedClasses.resize(1); |
| 505 | SchedClasses.back().Name = "NoItinerary"; |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 506 | SchedClasses.back().ProcIndices.push_back(0); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 507 | SchedClassIdxMap[SchedClasses.back().Name] = 0; |
| 508 | |
| 509 | // Gather and sort all itinerary classes used by instruction descriptions. |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 510 | RecVec ItinClassList; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 511 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 512 | E = Target.inst_end(); I != E; ++I) { |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 513 | Record *ItinDef = (*I)->TheDef->getValueAsDef("Itinerary"); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 514 | // Map a new SchedClass with no index. |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 515 | if (!SchedClassIdxMap.count(ItinDef->getName())) { |
| 516 | SchedClassIdxMap[ItinDef->getName()] = 0; |
| 517 | ItinClassList.push_back(ItinDef); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | // Assign each itinerary class unique number, skipping NoItinerary==0 |
| 521 | NumItineraryClasses = ItinClassList.size(); |
| 522 | std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord()); |
| 523 | for (unsigned i = 0, N = NumItineraryClasses; i < N; i++) { |
| 524 | Record *ItinDef = ItinClassList[i]; |
| 525 | SchedClassIdxMap[ItinDef->getName()] = SchedClasses.size(); |
| 526 | SchedClasses.push_back(CodeGenSchedClass(ItinDef)); |
| 527 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 528 | // Infer classes from SchedReadWrite resources listed for each |
| 529 | // instruction definition that inherits from class Sched. |
| 530 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 531 | E = Target.inst_end(); I != E; ++I) { |
| 532 | if (!(*I)->TheDef->isSubClassOf("Sched")) |
| 533 | continue; |
| 534 | IdxVec Writes, Reads; |
| 535 | findRWs((*I)->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads); |
| 536 | // ProcIdx == 0 indicates the class applies to all processors. |
| 537 | IdxVec ProcIndices(1, 0); |
| 538 | addSchedClass(Writes, Reads, ProcIndices); |
| 539 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 540 | // Create classes for InstRW defs. |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 541 | RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW"); |
| 542 | std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord()); |
| 543 | for (RecIter OI = InstRWDefs.begin(), OE = InstRWDefs.end(); OI != OE; ++OI) |
| 544 | createInstRWClass(*OI); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 545 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 546 | NumInstrSchedClasses = SchedClasses.size(); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 547 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 548 | bool EnableDump = false; |
| 549 | DEBUG(EnableDump = true); |
| 550 | if (!EnableDump) |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 551 | return; |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 552 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 553 | E = Target.inst_end(); I != E; ++I) { |
| 554 | Record *SchedDef = (*I)->TheDef; |
| 555 | std::string InstName = (*I)->TheDef->getName(); |
| 556 | if (SchedDef->isSubClassOf("Sched")) { |
| 557 | IdxVec Writes; |
| 558 | IdxVec Reads; |
| 559 | findRWs((*I)->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads); |
| 560 | dbgs() << "SchedRW machine model for " << InstName; |
| 561 | for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) |
| 562 | dbgs() << " " << SchedWrites[*WI].Name; |
| 563 | for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI) |
| 564 | dbgs() << " " << SchedReads[*RI].Name; |
| 565 | dbgs() << '\n'; |
| 566 | } |
| 567 | unsigned SCIdx = InstrClassMap.lookup((*I)->TheDef); |
| 568 | if (SCIdx) { |
| 569 | const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs; |
| 570 | for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); |
| 571 | RWI != RWE; ++RWI) { |
| 572 | const CodeGenProcModel &ProcModel = |
| 573 | getProcModel((*RWI)->getValueAsDef("SchedModel")); |
Andrew Trick | fe05d98 | 2012-10-03 23:06:25 +0000 | [diff] [blame] | 574 | dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName; |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 575 | IdxVec Writes; |
| 576 | IdxVec Reads; |
| 577 | findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"), |
| 578 | Writes, Reads); |
| 579 | for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) |
| 580 | dbgs() << " " << SchedWrites[*WI].Name; |
| 581 | for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI) |
| 582 | dbgs() << " " << SchedReads[*RI].Name; |
| 583 | dbgs() << '\n'; |
| 584 | } |
| 585 | continue; |
| 586 | } |
| 587 | if (!SchedDef->isSubClassOf("Sched") |
| 588 | && (SchedDef->getValueAsDef("Itinerary")->getName() == "NoItinerary")) { |
| 589 | dbgs() << "No machine model for " << (*I)->TheDef->getName() << '\n'; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | unsigned CodeGenSchedModels::getSchedClassIdx( |
| 595 | const RecVec &RWDefs) const { |
| 596 | |
| 597 | IdxVec Writes, Reads; |
| 598 | findRWs(RWDefs, Writes, Reads); |
| 599 | return findSchedClassIdx(Writes, Reads); |
| 600 | } |
| 601 | |
| 602 | /// Find an SchedClass that has been inferred from a per-operand list of |
| 603 | /// SchedWrites and SchedReads. |
| 604 | unsigned CodeGenSchedModels::findSchedClassIdx(const IdxVec &Writes, |
| 605 | const IdxVec &Reads) const { |
| 606 | for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I) { |
| 607 | // Classes with InstRWs may have the same Writes/Reads as a class originally |
| 608 | // produced by a SchedRW definition. We need to be able to recover the |
| 609 | // original class index for processors that don't match any InstRWs. |
| 610 | if (I->ItinClassDef || !I->InstRWs.empty()) |
| 611 | continue; |
| 612 | |
| 613 | if (I->Writes == Writes && I->Reads == Reads) { |
| 614 | return I - schedClassBegin(); |
| 615 | } |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 616 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 617 | return 0; |
| 618 | } |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 619 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 620 | // Get the SchedClass index for an instruction. |
| 621 | unsigned CodeGenSchedModels::getSchedClassIdx( |
| 622 | const CodeGenInstruction &Inst) const { |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 623 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 624 | unsigned SCIdx = InstrClassMap.lookup(Inst.TheDef); |
| 625 | if (SCIdx) |
| 626 | return SCIdx; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 627 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 628 | // If this opcode isn't mapped by the subtarget fallback to the instruction |
| 629 | // definition's SchedRW or ItinDef values. |
| 630 | if (Inst.TheDef->isSubClassOf("Sched")) { |
| 631 | RecVec RWs = Inst.TheDef->getValueAsListOfDefs("SchedRW"); |
| 632 | return getSchedClassIdx(RWs); |
| 633 | } |
| 634 | Record *ItinDef = Inst.TheDef->getValueAsDef("Itinerary"); |
| 635 | assert(SchedClassIdxMap.count(ItinDef->getName()) && "missing ItinClass"); |
| 636 | unsigned Idx = SchedClassIdxMap.lookup(ItinDef->getName()); |
| 637 | assert(Idx <= NumItineraryClasses && "bad ItinClass index"); |
| 638 | return Idx; |
| 639 | } |
| 640 | |
| 641 | std::string CodeGenSchedModels::createSchedClassName( |
| 642 | const IdxVec &OperWrites, const IdxVec &OperReads) { |
| 643 | |
| 644 | std::string Name; |
| 645 | for (IdxIter WI = OperWrites.begin(), WE = OperWrites.end(); WI != WE; ++WI) { |
| 646 | if (WI != OperWrites.begin()) |
| 647 | Name += '_'; |
| 648 | Name += SchedWrites[*WI].Name; |
| 649 | } |
| 650 | for (IdxIter RI = OperReads.begin(), RE = OperReads.end(); RI != RE; ++RI) { |
| 651 | Name += '_'; |
| 652 | Name += SchedReads[*RI].Name; |
| 653 | } |
| 654 | return Name; |
| 655 | } |
| 656 | |
| 657 | std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) { |
| 658 | |
| 659 | std::string Name; |
| 660 | for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) { |
| 661 | if (I != InstDefs.begin()) |
| 662 | Name += '_'; |
| 663 | Name += (*I)->getName(); |
| 664 | } |
| 665 | return Name; |
| 666 | } |
| 667 | |
| 668 | /// Add an inferred sched class from a per-operand list of SchedWrites and |
| 669 | /// SchedReads. ProcIndices contains the set of IDs of processors that may |
| 670 | /// utilize this class. |
| 671 | unsigned CodeGenSchedModels::addSchedClass(const IdxVec &OperWrites, |
| 672 | const IdxVec &OperReads, |
| 673 | const IdxVec &ProcIndices) |
| 674 | { |
| 675 | assert(!ProcIndices.empty() && "expect at least one ProcIdx"); |
| 676 | |
| 677 | unsigned Idx = findSchedClassIdx(OperWrites, OperReads); |
| 678 | if (Idx) { |
| 679 | IdxVec PI; |
| 680 | std::set_union(SchedClasses[Idx].ProcIndices.begin(), |
| 681 | SchedClasses[Idx].ProcIndices.end(), |
| 682 | ProcIndices.begin(), ProcIndices.end(), |
| 683 | std::back_inserter(PI)); |
| 684 | SchedClasses[Idx].ProcIndices.swap(PI); |
| 685 | return Idx; |
| 686 | } |
| 687 | Idx = SchedClasses.size(); |
| 688 | SchedClasses.resize(Idx+1); |
| 689 | CodeGenSchedClass &SC = SchedClasses.back(); |
| 690 | SC.Name = createSchedClassName(OperWrites, OperReads); |
| 691 | SC.Writes = OperWrites; |
| 692 | SC.Reads = OperReads; |
| 693 | SC.ProcIndices = ProcIndices; |
| 694 | |
| 695 | return Idx; |
| 696 | } |
| 697 | |
| 698 | // Create classes for each set of opcodes that are in the same InstReadWrite |
| 699 | // definition across all processors. |
| 700 | void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) { |
| 701 | // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that |
| 702 | // intersects with an existing class via a previous InstRWDef. Instrs that do |
| 703 | // not intersect with an existing class refer back to their former class as |
| 704 | // determined from ItinDef or SchedRW. |
| 705 | SmallVector<std::pair<unsigned, SmallVector<Record *, 8> >, 4> ClassInstrs; |
| 706 | // Sort Instrs into sets. |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 707 | const RecVec *InstDefs = Sets.expand(InstRWDef); |
| 708 | if (InstDefs->empty()) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 709 | PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes"); |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 710 | |
| 711 | for (RecIter I = InstDefs->begin(), E = InstDefs->end(); I != E; ++I) { |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 712 | unsigned SCIdx = 0; |
| 713 | InstClassMapTy::const_iterator Pos = InstrClassMap.find(*I); |
| 714 | if (Pos != InstrClassMap.end()) |
| 715 | SCIdx = Pos->second; |
| 716 | else { |
| 717 | // This instruction has not been mapped yet. Get the original class. All |
| 718 | // instructions in the same InstrRW class must be from the same original |
| 719 | // class because that is the fall-back class for other processors. |
| 720 | Record *ItinDef = (*I)->getValueAsDef("Itinerary"); |
| 721 | SCIdx = SchedClassIdxMap.lookup(ItinDef->getName()); |
| 722 | if (!SCIdx && (*I)->isSubClassOf("Sched")) |
| 723 | SCIdx = getSchedClassIdx((*I)->getValueAsListOfDefs("SchedRW")); |
| 724 | } |
| 725 | unsigned CIdx = 0, CEnd = ClassInstrs.size(); |
| 726 | for (; CIdx != CEnd; ++CIdx) { |
| 727 | if (ClassInstrs[CIdx].first == SCIdx) |
| 728 | break; |
| 729 | } |
| 730 | if (CIdx == CEnd) { |
| 731 | ClassInstrs.resize(CEnd + 1); |
| 732 | ClassInstrs[CIdx].first = SCIdx; |
| 733 | } |
| 734 | ClassInstrs[CIdx].second.push_back(*I); |
| 735 | } |
| 736 | // For each set of Instrs, create a new class if necessary, and map or remap |
| 737 | // the Instrs to it. |
| 738 | unsigned CIdx = 0, CEnd = ClassInstrs.size(); |
| 739 | for (; CIdx != CEnd; ++CIdx) { |
| 740 | unsigned OldSCIdx = ClassInstrs[CIdx].first; |
| 741 | ArrayRef<Record*> InstDefs = ClassInstrs[CIdx].second; |
| 742 | // If the all instrs in the current class are accounted for, then leave |
| 743 | // them mapped to their old class. |
| 744 | if (SchedClasses[OldSCIdx].InstRWs.size() == InstDefs.size()) { |
| 745 | assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 && |
| 746 | "expected a generic SchedClass"); |
| 747 | continue; |
| 748 | } |
| 749 | unsigned SCIdx = SchedClasses.size(); |
| 750 | SchedClasses.resize(SCIdx+1); |
| 751 | CodeGenSchedClass &SC = SchedClasses.back(); |
| 752 | SC.Name = createSchedClassName(InstDefs); |
| 753 | // Preserve ItinDef and Writes/Reads for processors without an InstRW entry. |
| 754 | SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef; |
| 755 | SC.Writes = SchedClasses[OldSCIdx].Writes; |
| 756 | SC.Reads = SchedClasses[OldSCIdx].Reads; |
| 757 | SC.ProcIndices.push_back(0); |
| 758 | // Map each Instr to this new class. |
| 759 | // Note that InstDefs may be a smaller list than InstRWDef's "Instrs". |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 760 | Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel"); |
| 761 | SmallSet<unsigned, 4> RemappedClassIDs; |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 762 | for (ArrayRef<Record*>::const_iterator |
| 763 | II = InstDefs.begin(), IE = InstDefs.end(); II != IE; ++II) { |
| 764 | unsigned OldSCIdx = InstrClassMap[*II]; |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 765 | if (OldSCIdx && RemappedClassIDs.insert(OldSCIdx)) { |
| 766 | for (RecIter RI = SchedClasses[OldSCIdx].InstRWs.begin(), |
| 767 | RE = SchedClasses[OldSCIdx].InstRWs.end(); RI != RE; ++RI) { |
| 768 | if ((*RI)->getValueAsDef("SchedModel") == RWModelDef) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 769 | PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " + |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 770 | (*II)->getName() + " also matches " + |
| 771 | (*RI)->getValue("Instrs")->getValue()->getAsString()); |
| 772 | } |
| 773 | assert(*RI != InstRWDef && "SchedClass has duplicate InstRW def"); |
| 774 | SC.InstRWs.push_back(*RI); |
| 775 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 776 | } |
| 777 | InstrClassMap[*II] = SCIdx; |
| 778 | } |
| 779 | SC.InstRWs.push_back(InstRWDef); |
| 780 | } |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Gather the processor itineraries. |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 784 | void CodeGenSchedModels::collectProcItins() { |
| 785 | for (std::vector<CodeGenProcModel>::iterator PI = ProcModels.begin(), |
| 786 | PE = ProcModels.end(); PI != PE; ++PI) { |
| 787 | CodeGenProcModel &ProcModel = *PI; |
| 788 | RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID"); |
| 789 | // Skip empty itinerary. |
| 790 | if (ItinRecords.empty()) |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 791 | continue; |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 792 | |
| 793 | ProcModel.ItinDefList.resize(NumItineraryClasses+1); |
| 794 | |
| 795 | // Insert each itinerary data record in the correct position within |
| 796 | // the processor model's ItinDefList. |
| 797 | for (unsigned i = 0, N = ItinRecords.size(); i < N; i++) { |
| 798 | Record *ItinData = ItinRecords[i]; |
| 799 | Record *ItinDef = ItinData->getValueAsDef("TheClass"); |
| 800 | if (!SchedClassIdxMap.count(ItinDef->getName())) { |
| 801 | DEBUG(dbgs() << ProcModel.ItinsDef->getName() |
| 802 | << " has unused itinerary class " << ItinDef->getName() << '\n'); |
| 803 | continue; |
| 804 | } |
| 805 | assert(SchedClassIdxMap.count(ItinDef->getName()) && "missing ItinClass"); |
| 806 | unsigned Idx = SchedClassIdxMap.lookup(ItinDef->getName()); |
| 807 | assert(Idx <= NumItineraryClasses && "bad ItinClass index"); |
| 808 | ProcModel.ItinDefList[Idx] = ItinData; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 809 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 810 | // Check for missing itinerary entries. |
| 811 | assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec"); |
| 812 | DEBUG( |
| 813 | for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) { |
| 814 | if (!ProcModel.ItinDefList[i]) |
| 815 | dbgs() << ProcModel.ItinsDef->getName() |
| 816 | << " missing itinerary for class " |
| 817 | << SchedClasses[i].Name << '\n'; |
| 818 | }); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 819 | } |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 820 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 821 | |
| 822 | // Gather the read/write types for each itinerary class. |
| 823 | void CodeGenSchedModels::collectProcItinRW() { |
| 824 | RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW"); |
| 825 | std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord()); |
| 826 | for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) { |
| 827 | if (!(*II)->getValueInit("SchedModel")->isComplete()) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 828 | PrintFatalError((*II)->getLoc(), "SchedModel is undefined"); |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 829 | Record *ModelDef = (*II)->getValueAsDef("SchedModel"); |
| 830 | ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef); |
| 831 | if (I == ProcModelMap.end()) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 832 | PrintFatalError((*II)->getLoc(), "Undefined SchedMachineModel " |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 833 | + ModelDef->getName()); |
| 834 | } |
| 835 | ProcModels[I->second].ItinRWDefs.push_back(*II); |
| 836 | } |
| 837 | } |
| 838 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 839 | /// Infer new classes from existing classes. In the process, this may create new |
| 840 | /// SchedWrites from sequences of existing SchedWrites. |
| 841 | void CodeGenSchedModels::inferSchedClasses() { |
| 842 | // Visit all existing classes and newly created classes. |
| 843 | for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) { |
| 844 | if (SchedClasses[Idx].ItinClassDef) |
| 845 | inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx); |
| 846 | else if (!SchedClasses[Idx].InstRWs.empty()) |
| 847 | inferFromInstRWs(Idx); |
| 848 | else { |
| 849 | inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads, |
| 850 | Idx, SchedClasses[Idx].ProcIndices); |
| 851 | } |
| 852 | assert(SchedClasses.size() < (NumInstrSchedClasses*6) && |
| 853 | "too many SchedVariants"); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /// Infer classes from per-processor itinerary resources. |
| 858 | void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef, |
| 859 | unsigned FromClassIdx) { |
| 860 | for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) { |
| 861 | const CodeGenProcModel &PM = ProcModels[PIdx]; |
| 862 | // For all ItinRW entries. |
| 863 | bool HasMatch = false; |
| 864 | for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end(); |
| 865 | II != IE; ++II) { |
| 866 | RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses"); |
| 867 | if (!std::count(Matched.begin(), Matched.end(), ItinClassDef)) |
| 868 | continue; |
| 869 | if (HasMatch) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 870 | PrintFatalError((*II)->getLoc(), "Duplicate itinerary class " |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 871 | + ItinClassDef->getName() |
| 872 | + " in ItinResources for " + PM.ModelName); |
| 873 | HasMatch = true; |
| 874 | IdxVec Writes, Reads; |
| 875 | findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads); |
| 876 | IdxVec ProcIndices(1, PIdx); |
| 877 | inferFromRW(Writes, Reads, FromClassIdx, ProcIndices); |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /// Infer classes from per-processor InstReadWrite definitions. |
| 883 | void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) { |
| 884 | const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs; |
| 885 | for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); RWI != RWE; ++RWI) { |
Andrew Trick | 1374526 | 2012-10-03 23:06:32 +0000 | [diff] [blame] | 886 | const RecVec *InstDefs = Sets.expand(*RWI); |
| 887 | RecIter II = InstDefs->begin(), IE = InstDefs->end(); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 888 | for (; II != IE; ++II) { |
| 889 | if (InstrClassMap[*II] == SCIdx) |
| 890 | break; |
| 891 | } |
| 892 | // If this class no longer has any instructions mapped to it, it has become |
| 893 | // irrelevant. |
| 894 | if (II == IE) |
| 895 | continue; |
| 896 | IdxVec Writes, Reads; |
| 897 | findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads); |
| 898 | unsigned PIdx = getProcModel((*RWI)->getValueAsDef("SchedModel")).Index; |
| 899 | IdxVec ProcIndices(1, PIdx); |
| 900 | inferFromRW(Writes, Reads, SCIdx, ProcIndices); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | namespace { |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 905 | // Helper for substituteVariantOperand. |
| 906 | struct TransVariant { |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 907 | Record *VarOrSeqDef; // Variant or sequence. |
| 908 | unsigned RWIdx; // Index of this variant or sequence's matched type. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 909 | unsigned ProcIdx; // Processor model index or zero for any. |
| 910 | unsigned TransVecIdx; // Index into PredTransitions::TransVec. |
| 911 | |
| 912 | TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti): |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 913 | VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {} |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 914 | }; |
| 915 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 916 | // Associate a predicate with the SchedReadWrite that it guards. |
| 917 | // RWIdx is the index of the read/write variant. |
| 918 | struct PredCheck { |
| 919 | bool IsRead; |
| 920 | unsigned RWIdx; |
| 921 | Record *Predicate; |
| 922 | |
| 923 | PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {} |
| 924 | }; |
| 925 | |
| 926 | // A Predicate transition is a list of RW sequences guarded by a PredTerm. |
| 927 | struct PredTransition { |
| 928 | // A predicate term is a conjunction of PredChecks. |
| 929 | SmallVector<PredCheck, 4> PredTerm; |
| 930 | SmallVector<SmallVector<unsigned,4>, 16> WriteSequences; |
| 931 | SmallVector<SmallVector<unsigned,4>, 16> ReadSequences; |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 932 | SmallVector<unsigned, 4> ProcIndices; |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 933 | }; |
| 934 | |
| 935 | // Encapsulate a set of partially constructed transitions. |
| 936 | // The results are built by repeated calls to substituteVariants. |
| 937 | class PredTransitions { |
| 938 | CodeGenSchedModels &SchedModels; |
| 939 | |
| 940 | public: |
| 941 | std::vector<PredTransition> TransVec; |
| 942 | |
| 943 | PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {} |
| 944 | |
| 945 | void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq, |
| 946 | bool IsRead, unsigned StartIdx); |
| 947 | |
| 948 | void substituteVariants(const PredTransition &Trans); |
| 949 | |
| 950 | #ifndef NDEBUG |
| 951 | void dump() const; |
| 952 | #endif |
| 953 | |
| 954 | private: |
| 955 | bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term); |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 956 | void getIntersectingVariants( |
| 957 | const CodeGenSchedRW &SchedRW, unsigned TransIdx, |
| 958 | std::vector<TransVariant> &IntersectingVariants); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 959 | void pushVariant(const TransVariant &VInfo, bool IsRead); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 960 | }; |
| 961 | } // anonymous |
| 962 | |
| 963 | // Return true if this predicate is mutually exclusive with a PredTerm. This |
| 964 | // degenerates into checking if the predicate is mutually exclusive with any |
| 965 | // predicate in the Term's conjunction. |
| 966 | // |
| 967 | // All predicates associated with a given SchedRW are considered mutually |
| 968 | // exclusive. This should work even if the conditions expressed by the |
| 969 | // predicates are not exclusive because the predicates for a given SchedWrite |
| 970 | // are always checked in the order they are defined in the .td file. Later |
| 971 | // conditions implicitly negate any prior condition. |
| 972 | bool PredTransitions::mutuallyExclusive(Record *PredDef, |
| 973 | ArrayRef<PredCheck> Term) { |
| 974 | |
| 975 | for (ArrayRef<PredCheck>::iterator I = Term.begin(), E = Term.end(); |
| 976 | I != E; ++I) { |
| 977 | if (I->Predicate == PredDef) |
| 978 | return false; |
| 979 | |
| 980 | const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(I->RWIdx, I->IsRead); |
| 981 | assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant"); |
| 982 | RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants"); |
| 983 | for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) { |
| 984 | if ((*VI)->getValueAsDef("Predicate") == PredDef) |
| 985 | return true; |
| 986 | } |
| 987 | } |
| 988 | return false; |
| 989 | } |
| 990 | |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 991 | static bool hasAliasedVariants(const CodeGenSchedRW &RW, |
| 992 | CodeGenSchedModels &SchedModels) { |
| 993 | if (RW.HasVariants) |
| 994 | return true; |
| 995 | |
| 996 | for (RecIter I = RW.Aliases.begin(), E = RW.Aliases.end(); I != E; ++I) { |
| 997 | const CodeGenSchedRW &AliasRW = |
| 998 | SchedModels.getSchedRW((*I)->getValueAsDef("AliasRW")); |
| 999 | if (AliasRW.HasVariants) |
| 1000 | return true; |
| 1001 | if (AliasRW.IsSequence) { |
| 1002 | IdxVec ExpandedRWs; |
| 1003 | SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead); |
| 1004 | for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end(); |
| 1005 | SI != SE; ++SI) { |
| 1006 | if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead), |
| 1007 | SchedModels)) { |
| 1008 | return true; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
| 1016 | static bool hasVariant(ArrayRef<PredTransition> Transitions, |
| 1017 | CodeGenSchedModels &SchedModels) { |
| 1018 | for (ArrayRef<PredTransition>::iterator |
| 1019 | PTI = Transitions.begin(), PTE = Transitions.end(); |
| 1020 | PTI != PTE; ++PTI) { |
| 1021 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1022 | WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end(); |
| 1023 | WSI != WSE; ++WSI) { |
| 1024 | for (SmallVectorImpl<unsigned>::const_iterator |
| 1025 | WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) { |
| 1026 | if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels)) |
| 1027 | return true; |
| 1028 | } |
| 1029 | } |
| 1030 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1031 | RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end(); |
| 1032 | RSI != RSE; ++RSI) { |
| 1033 | for (SmallVectorImpl<unsigned>::const_iterator |
| 1034 | RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) { |
| 1035 | if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels)) |
| 1036 | return true; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | // Populate IntersectingVariants with any variants or aliased sequences of the |
| 1044 | // given SchedRW whose processor indices and predicates are not mutually |
| 1045 | // exclusive with the given transition, |
| 1046 | void PredTransitions::getIntersectingVariants( |
| 1047 | const CodeGenSchedRW &SchedRW, unsigned TransIdx, |
| 1048 | std::vector<TransVariant> &IntersectingVariants) { |
| 1049 | |
| 1050 | std::vector<TransVariant> Variants; |
| 1051 | if (SchedRW.HasVariants) { |
| 1052 | unsigned VarProcIdx = 0; |
| 1053 | if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) { |
| 1054 | Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel"); |
| 1055 | VarProcIdx = SchedModels.getProcModel(ModelDef).Index; |
| 1056 | } |
| 1057 | // Push each variant. Assign TransVecIdx later. |
| 1058 | const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants"); |
| 1059 | for (RecIter RI = VarDefs.begin(), RE = VarDefs.end(); RI != RE; ++RI) |
| 1060 | Variants.push_back(TransVariant(*RI, SchedRW.Index, VarProcIdx, 0)); |
| 1061 | } |
| 1062 | for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end(); |
| 1063 | AI != AE; ++AI) { |
| 1064 | // If either the SchedAlias itself or the SchedReadWrite that it aliases |
| 1065 | // to is defined within a processor model, constrain all variants to |
| 1066 | // that processor. |
| 1067 | unsigned AliasProcIdx = 0; |
| 1068 | if ((*AI)->getValueInit("SchedModel")->isComplete()) { |
| 1069 | Record *ModelDef = (*AI)->getValueAsDef("SchedModel"); |
| 1070 | AliasProcIdx = SchedModels.getProcModel(ModelDef).Index; |
| 1071 | } |
| 1072 | const CodeGenSchedRW &AliasRW = |
| 1073 | SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW")); |
| 1074 | |
| 1075 | if (AliasRW.HasVariants) { |
| 1076 | const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants"); |
| 1077 | for (RecIter RI = VarDefs.begin(), RE = VarDefs.end(); RI != RE; ++RI) |
| 1078 | Variants.push_back(TransVariant(*RI, AliasRW.Index, AliasProcIdx, 0)); |
| 1079 | } |
| 1080 | if (AliasRW.IsSequence) { |
| 1081 | Variants.push_back( |
| 1082 | TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0)); |
| 1083 | } |
| 1084 | } |
| 1085 | for (unsigned VIdx = 0, VEnd = Variants.size(); VIdx != VEnd; ++VIdx) { |
| 1086 | TransVariant &Variant = Variants[VIdx]; |
| 1087 | // Don't expand variants if the processor models don't intersect. |
| 1088 | // A zero processor index means any processor. |
| 1089 | SmallVector<unsigned, 4> &ProcIndices = TransVec[TransIdx].ProcIndices; |
| 1090 | if (ProcIndices[0] && Variants[VIdx].ProcIdx) { |
| 1091 | unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(), |
| 1092 | Variant.ProcIdx); |
| 1093 | if (!Cnt) |
| 1094 | continue; |
| 1095 | if (Cnt > 1) { |
| 1096 | const CodeGenProcModel &PM = |
| 1097 | *(SchedModels.procModelBegin() + Variant.ProcIdx); |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1098 | PrintFatalError(Variant.VarOrSeqDef->getLoc(), |
| 1099 | "Multiple variants defined for processor " + |
| 1100 | PM.ModelName + |
| 1101 | " Ensure only one SchedAlias exists per RW."); |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) { |
| 1105 | Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate"); |
| 1106 | if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm)) |
| 1107 | continue; |
| 1108 | } |
| 1109 | if (IntersectingVariants.empty()) { |
| 1110 | // The first variant builds on the existing transition. |
| 1111 | Variant.TransVecIdx = TransIdx; |
| 1112 | IntersectingVariants.push_back(Variant); |
| 1113 | } |
| 1114 | else { |
| 1115 | // Push another copy of the current transition for more variants. |
| 1116 | Variant.TransVecIdx = TransVec.size(); |
| 1117 | IntersectingVariants.push_back(Variant); |
| 1118 | TransVec.push_back(TransVec[TransIdx]); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1123 | // Push the Reads/Writes selected by this variant onto the PredTransition |
| 1124 | // specified by VInfo. |
| 1125 | void PredTransitions:: |
| 1126 | pushVariant(const TransVariant &VInfo, bool IsRead) { |
| 1127 | |
| 1128 | PredTransition &Trans = TransVec[VInfo.TransVecIdx]; |
| 1129 | |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1130 | // If this operand transition is reached through a processor-specific alias, |
| 1131 | // then the whole transition is specific to this processor. |
| 1132 | if (VInfo.ProcIdx != 0) |
| 1133 | Trans.ProcIndices.assign(1, VInfo.ProcIdx); |
| 1134 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1135 | IdxVec SelectedRWs; |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 1136 | if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) { |
| 1137 | Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate"); |
| 1138 | Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef)); |
| 1139 | RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected"); |
| 1140 | SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead); |
| 1141 | } |
| 1142 | else { |
| 1143 | assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") && |
| 1144 | "variant must be a SchedVariant or aliased WriteSequence"); |
| 1145 | SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead)); |
| 1146 | } |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1147 | |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1148 | const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1149 | |
| 1150 | SmallVectorImpl<SmallVector<unsigned,4> > &RWSequences = IsRead |
| 1151 | ? Trans.ReadSequences : Trans.WriteSequences; |
| 1152 | if (SchedRW.IsVariadic) { |
| 1153 | unsigned OperIdx = RWSequences.size()-1; |
| 1154 | // Make N-1 copies of this transition's last sequence. |
| 1155 | for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) { |
| 1156 | RWSequences.push_back(RWSequences[OperIdx]); |
| 1157 | } |
| 1158 | // Push each of the N elements of the SelectedRWs onto a copy of the last |
| 1159 | // sequence (split the current operand into N operands). |
| 1160 | // Note that write sequences should be expanded within this loop--the entire |
| 1161 | // sequence belongs to a single operand. |
| 1162 | for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end(); |
| 1163 | RWI != RWE; ++RWI, ++OperIdx) { |
| 1164 | IdxVec ExpandedRWs; |
| 1165 | if (IsRead) |
| 1166 | ExpandedRWs.push_back(*RWI); |
| 1167 | else |
| 1168 | SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead); |
| 1169 | RWSequences[OperIdx].insert(RWSequences[OperIdx].end(), |
| 1170 | ExpandedRWs.begin(), ExpandedRWs.end()); |
| 1171 | } |
| 1172 | assert(OperIdx == RWSequences.size() && "missed a sequence"); |
| 1173 | } |
| 1174 | else { |
| 1175 | // Push this transition's expanded sequence onto this transition's last |
| 1176 | // sequence (add to the current operand's sequence). |
| 1177 | SmallVectorImpl<unsigned> &Seq = RWSequences.back(); |
| 1178 | IdxVec ExpandedRWs; |
| 1179 | for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end(); |
| 1180 | RWI != RWE; ++RWI) { |
| 1181 | if (IsRead) |
| 1182 | ExpandedRWs.push_back(*RWI); |
| 1183 | else |
| 1184 | SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead); |
| 1185 | } |
| 1186 | Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end()); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | // RWSeq is a sequence of all Reads or all Writes for the next read or write |
| 1191 | // operand. StartIdx is an index into TransVec where partial results |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1192 | // starts. RWSeq must be applied to all transitions between StartIdx and the end |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1193 | // of TransVec. |
| 1194 | void PredTransitions::substituteVariantOperand( |
| 1195 | const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) { |
| 1196 | |
| 1197 | // Visit each original RW within the current sequence. |
| 1198 | for (SmallVectorImpl<unsigned>::const_iterator |
| 1199 | RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) { |
| 1200 | const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead); |
| 1201 | // Push this RW on all partial PredTransitions or distribute variants. |
| 1202 | // New PredTransitions may be pushed within this loop which should not be |
| 1203 | // revisited (TransEnd must be loop invariant). |
| 1204 | for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size(); |
| 1205 | TransIdx != TransEnd; ++TransIdx) { |
| 1206 | // In the common case, push RW onto the current operand's sequence. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1207 | if (!hasAliasedVariants(SchedRW, SchedModels)) { |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1208 | if (IsRead) |
| 1209 | TransVec[TransIdx].ReadSequences.back().push_back(*RWI); |
| 1210 | else |
| 1211 | TransVec[TransIdx].WriteSequences.back().push_back(*RWI); |
| 1212 | continue; |
| 1213 | } |
| 1214 | // Distribute this partial PredTransition across intersecting variants. |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 1215 | // This will push a copies of TransVec[TransIdx] on the back of TransVec. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1216 | std::vector<TransVariant> IntersectingVariants; |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 1217 | getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1218 | if (IntersectingVariants.empty()) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1219 | PrintFatalError(SchedRW.TheDef->getLoc(), |
| 1220 | "No variant of this type has " |
| 1221 | "a matching predicate on any processor"); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1222 | // Now expand each variant on top of its copy of the transition. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1223 | for (std::vector<TransVariant>::const_iterator |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1224 | IVI = IntersectingVariants.begin(), |
| 1225 | IVE = IntersectingVariants.end(); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1226 | IVI != IVE; ++IVI) { |
| 1227 | pushVariant(*IVI, IsRead); |
| 1228 | } |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | // For each variant of a Read/Write in Trans, substitute the sequence of |
| 1234 | // Read/Writes guarded by the variant. This is exponential in the number of |
| 1235 | // variant Read/Writes, but in practice detection of mutually exclusive |
| 1236 | // predicates should result in linear growth in the total number variants. |
| 1237 | // |
| 1238 | // This is one step in a breadth-first search of nested variants. |
| 1239 | void PredTransitions::substituteVariants(const PredTransition &Trans) { |
| 1240 | // Build up a set of partial results starting at the back of |
| 1241 | // PredTransitions. Remember the first new transition. |
| 1242 | unsigned StartIdx = TransVec.size(); |
| 1243 | TransVec.resize(TransVec.size() + 1); |
| 1244 | TransVec.back().PredTerm = Trans.PredTerm; |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1245 | TransVec.back().ProcIndices = Trans.ProcIndices; |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1246 | |
| 1247 | // Visit each original write sequence. |
| 1248 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1249 | WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end(); |
| 1250 | WSI != WSE; ++WSI) { |
| 1251 | // Push a new (empty) write sequence onto all partial Transitions. |
| 1252 | for (std::vector<PredTransition>::iterator I = |
| 1253 | TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) { |
| 1254 | I->WriteSequences.resize(I->WriteSequences.size() + 1); |
| 1255 | } |
| 1256 | substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx); |
| 1257 | } |
| 1258 | // Visit each original read sequence. |
| 1259 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1260 | RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end(); |
| 1261 | RSI != RSE; ++RSI) { |
| 1262 | // Push a new (empty) read sequence onto all partial Transitions. |
| 1263 | for (std::vector<PredTransition>::iterator I = |
| 1264 | TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) { |
| 1265 | I->ReadSequences.resize(I->ReadSequences.size() + 1); |
| 1266 | } |
| 1267 | substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx); |
| 1268 | } |
| 1269 | } |
| 1270 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1271 | // Create a new SchedClass for each variant found by inferFromRW. Pass |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1272 | static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions, |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1273 | unsigned FromClassIdx, |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1274 | CodeGenSchedModels &SchedModels) { |
| 1275 | // For each PredTransition, create a new CodeGenSchedTransition, which usually |
| 1276 | // requires creating a new SchedClass. |
| 1277 | for (ArrayRef<PredTransition>::iterator |
| 1278 | I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) { |
| 1279 | IdxVec OperWritesVariant; |
| 1280 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1281 | WSI = I->WriteSequences.begin(), WSE = I->WriteSequences.end(); |
| 1282 | WSI != WSE; ++WSI) { |
| 1283 | // Create a new write representing the expanded sequence. |
| 1284 | OperWritesVariant.push_back( |
| 1285 | SchedModels.findOrInsertRW(*WSI, /*IsRead=*/false)); |
| 1286 | } |
| 1287 | IdxVec OperReadsVariant; |
| 1288 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1289 | RSI = I->ReadSequences.begin(), RSE = I->ReadSequences.end(); |
| 1290 | RSI != RSE; ++RSI) { |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1291 | // Create a new read representing the expanded sequence. |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1292 | OperReadsVariant.push_back( |
| 1293 | SchedModels.findOrInsertRW(*RSI, /*IsRead=*/true)); |
| 1294 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1295 | IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end()); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1296 | CodeGenSchedTransition SCTrans; |
| 1297 | SCTrans.ToClassIdx = |
| 1298 | SchedModels.addSchedClass(OperWritesVariant, OperReadsVariant, |
| 1299 | ProcIndices); |
| 1300 | SCTrans.ProcIndices = ProcIndices; |
| 1301 | // The final PredTerm is unique set of predicates guarding the transition. |
| 1302 | RecVec Preds; |
| 1303 | for (SmallVectorImpl<PredCheck>::const_iterator |
| 1304 | PI = I->PredTerm.begin(), PE = I->PredTerm.end(); PI != PE; ++PI) { |
| 1305 | Preds.push_back(PI->Predicate); |
| 1306 | } |
| 1307 | RecIter PredsEnd = std::unique(Preds.begin(), Preds.end()); |
| 1308 | Preds.resize(PredsEnd - Preds.begin()); |
| 1309 | SCTrans.PredTerm = Preds; |
| 1310 | SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1314 | // Create new SchedClasses for the given ReadWrite list. If any of the |
| 1315 | // ReadWrites refers to a SchedVariant, create a new SchedClass for each variant |
| 1316 | // of the ReadWrite list, following Aliases if necessary. |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1317 | void CodeGenSchedModels::inferFromRW(const IdxVec &OperWrites, |
| 1318 | const IdxVec &OperReads, |
| 1319 | unsigned FromClassIdx, |
| 1320 | const IdxVec &ProcIndices) { |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1321 | DEBUG(dbgs() << "INFER RW: "); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1322 | |
| 1323 | // Create a seed transition with an empty PredTerm and the expanded sequences |
| 1324 | // of SchedWrites for the current SchedClass. |
| 1325 | std::vector<PredTransition> LastTransitions; |
| 1326 | LastTransitions.resize(1); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1327 | LastTransitions.back().ProcIndices.append(ProcIndices.begin(), |
| 1328 | ProcIndices.end()); |
| 1329 | |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1330 | for (IdxIter I = OperWrites.begin(), E = OperWrites.end(); I != E; ++I) { |
| 1331 | IdxVec WriteSeq; |
| 1332 | expandRWSequence(*I, WriteSeq, /*IsRead=*/false); |
| 1333 | unsigned Idx = LastTransitions[0].WriteSequences.size(); |
| 1334 | LastTransitions[0].WriteSequences.resize(Idx + 1); |
| 1335 | SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences[Idx]; |
| 1336 | for (IdxIter WI = WriteSeq.begin(), WE = WriteSeq.end(); WI != WE; ++WI) |
| 1337 | Seq.push_back(*WI); |
| 1338 | DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") "); |
| 1339 | } |
| 1340 | DEBUG(dbgs() << " Reads: "); |
| 1341 | for (IdxIter I = OperReads.begin(), E = OperReads.end(); I != E; ++I) { |
| 1342 | IdxVec ReadSeq; |
| 1343 | expandRWSequence(*I, ReadSeq, /*IsRead=*/true); |
| 1344 | unsigned Idx = LastTransitions[0].ReadSequences.size(); |
| 1345 | LastTransitions[0].ReadSequences.resize(Idx + 1); |
| 1346 | SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences[Idx]; |
| 1347 | for (IdxIter RI = ReadSeq.begin(), RE = ReadSeq.end(); RI != RE; ++RI) |
| 1348 | Seq.push_back(*RI); |
| 1349 | DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") "); |
| 1350 | } |
| 1351 | DEBUG(dbgs() << '\n'); |
| 1352 | |
| 1353 | // Collect all PredTransitions for individual operands. |
| 1354 | // Iterate until no variant writes remain. |
| 1355 | while (hasVariant(LastTransitions, *this)) { |
| 1356 | PredTransitions Transitions(*this); |
| 1357 | for (std::vector<PredTransition>::const_iterator |
| 1358 | I = LastTransitions.begin(), E = LastTransitions.end(); |
| 1359 | I != E; ++I) { |
| 1360 | Transitions.substituteVariants(*I); |
| 1361 | } |
| 1362 | DEBUG(Transitions.dump()); |
| 1363 | LastTransitions.swap(Transitions.TransVec); |
| 1364 | } |
| 1365 | // If the first transition has no variants, nothing to do. |
| 1366 | if (LastTransitions[0].PredTerm.empty()) |
| 1367 | return; |
| 1368 | |
| 1369 | // WARNING: We are about to mutate the SchedClasses vector. Do not refer to |
| 1370 | // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1371 | inferFromTransitions(LastTransitions, FromClassIdx, *this); |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1374 | // Collect and sort WriteRes, ReadAdvance, and ProcResources. |
| 1375 | void CodeGenSchedModels::collectProcResources() { |
| 1376 | // Add any subtarget-specific SchedReadWrites that are directly associated |
| 1377 | // with processor resources. Refer to the parent SchedClass's ProcIndices to |
| 1378 | // determine which processors they apply to. |
| 1379 | for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd(); |
| 1380 | SCI != SCE; ++SCI) { |
| 1381 | if (SCI->ItinClassDef) |
| 1382 | collectItinProcResources(SCI->ItinClassDef); |
| 1383 | else |
| 1384 | collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices); |
| 1385 | } |
| 1386 | // Add resources separately defined by each subtarget. |
| 1387 | RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes"); |
| 1388 | for (RecIter WRI = WRDefs.begin(), WRE = WRDefs.end(); WRI != WRE; ++WRI) { |
| 1389 | Record *ModelDef = (*WRI)->getValueAsDef("SchedModel"); |
| 1390 | addWriteRes(*WRI, getProcModel(ModelDef).Index); |
| 1391 | } |
| 1392 | RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance"); |
| 1393 | for (RecIter RAI = RADefs.begin(), RAE = RADefs.end(); RAI != RAE; ++RAI) { |
| 1394 | Record *ModelDef = (*RAI)->getValueAsDef("SchedModel"); |
| 1395 | addReadAdvance(*RAI, getProcModel(ModelDef).Index); |
| 1396 | } |
| 1397 | // Finalize each ProcModel by sorting the record arrays. |
| 1398 | for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) { |
| 1399 | CodeGenProcModel &PM = ProcModels[PIdx]; |
| 1400 | std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(), |
| 1401 | LessRecord()); |
| 1402 | std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(), |
| 1403 | LessRecord()); |
| 1404 | std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(), |
| 1405 | LessRecord()); |
| 1406 | DEBUG( |
| 1407 | PM.dump(); |
| 1408 | dbgs() << "WriteResDefs: "; |
| 1409 | for (RecIter RI = PM.WriteResDefs.begin(), |
| 1410 | RE = PM.WriteResDefs.end(); RI != RE; ++RI) { |
| 1411 | if ((*RI)->isSubClassOf("WriteRes")) |
| 1412 | dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " "; |
| 1413 | else |
| 1414 | dbgs() << (*RI)->getName() << " "; |
| 1415 | } |
| 1416 | dbgs() << "\nReadAdvanceDefs: "; |
| 1417 | for (RecIter RI = PM.ReadAdvanceDefs.begin(), |
| 1418 | RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) { |
| 1419 | if ((*RI)->isSubClassOf("ReadAdvance")) |
| 1420 | dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " "; |
| 1421 | else |
| 1422 | dbgs() << (*RI)->getName() << " "; |
| 1423 | } |
| 1424 | dbgs() << "\nProcResourceDefs: "; |
| 1425 | for (RecIter RI = PM.ProcResourceDefs.begin(), |
| 1426 | RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) { |
| 1427 | dbgs() << (*RI)->getName() << " "; |
| 1428 | } |
| 1429 | dbgs() << '\n'); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | // Collect itinerary class resources for each processor. |
| 1434 | void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) { |
| 1435 | for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) { |
| 1436 | const CodeGenProcModel &PM = ProcModels[PIdx]; |
| 1437 | // For all ItinRW entries. |
| 1438 | bool HasMatch = false; |
| 1439 | for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end(); |
| 1440 | II != IE; ++II) { |
| 1441 | RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses"); |
| 1442 | if (!std::count(Matched.begin(), Matched.end(), ItinClassDef)) |
| 1443 | continue; |
| 1444 | if (HasMatch) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1445 | PrintFatalError((*II)->getLoc(), "Duplicate itinerary class " |
| 1446 | + ItinClassDef->getName() |
| 1447 | + " in ItinResources for " + PM.ModelName); |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1448 | HasMatch = true; |
| 1449 | IdxVec Writes, Reads; |
| 1450 | findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads); |
| 1451 | IdxVec ProcIndices(1, PIdx); |
| 1452 | collectRWResources(Writes, Reads, ProcIndices); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
Andrew Trick | dbe6d43 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 1457 | void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead, |
| 1458 | const IdxVec &ProcIndices) { |
| 1459 | const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead); |
| 1460 | if (SchedRW.TheDef) { |
| 1461 | if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) { |
| 1462 | for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end(); |
| 1463 | PI != PE; ++PI) { |
| 1464 | addWriteRes(SchedRW.TheDef, *PI); |
| 1465 | } |
| 1466 | } |
| 1467 | else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) { |
| 1468 | for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end(); |
| 1469 | PI != PE; ++PI) { |
| 1470 | addReadAdvance(SchedRW.TheDef, *PI); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end(); |
| 1475 | AI != AE; ++AI) { |
| 1476 | IdxVec AliasProcIndices; |
| 1477 | if ((*AI)->getValueInit("SchedModel")->isComplete()) { |
| 1478 | AliasProcIndices.push_back( |
| 1479 | getProcModel((*AI)->getValueAsDef("SchedModel")).Index); |
| 1480 | } |
| 1481 | else |
| 1482 | AliasProcIndices = ProcIndices; |
| 1483 | const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW")); |
| 1484 | assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes"); |
| 1485 | |
| 1486 | IdxVec ExpandedRWs; |
| 1487 | expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead); |
| 1488 | for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end(); |
| 1489 | SI != SE; ++SI) { |
| 1490 | collectRWResources(*SI, IsRead, AliasProcIndices); |
| 1491 | } |
| 1492 | } |
| 1493 | } |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1494 | |
| 1495 | // Collect resources for a set of read/write types and processor indices. |
| 1496 | void CodeGenSchedModels::collectRWResources(const IdxVec &Writes, |
| 1497 | const IdxVec &Reads, |
| 1498 | const IdxVec &ProcIndices) { |
| 1499 | |
Andrew Trick | dbe6d43 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 1500 | for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) |
| 1501 | collectRWResources(*WI, /*IsRead=*/false, ProcIndices); |
| 1502 | |
| 1503 | for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI) |
| 1504 | collectRWResources(*RI, /*IsRead=*/true, ProcIndices); |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Andrew Trick | dbe6d43 | 2012-10-10 05:43:13 +0000 | [diff] [blame] | 1507 | |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1508 | // Find the processor's resource units for this kind of resource. |
| 1509 | Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind, |
| 1510 | const CodeGenProcModel &PM) const { |
| 1511 | if (ProcResKind->isSubClassOf("ProcResourceUnits")) |
| 1512 | return ProcResKind; |
| 1513 | |
| 1514 | Record *ProcUnitDef = 0; |
| 1515 | RecVec ProcResourceDefs = |
| 1516 | Records.getAllDerivedDefinitions("ProcResourceUnits"); |
| 1517 | |
| 1518 | for (RecIter RI = ProcResourceDefs.begin(), RE = ProcResourceDefs.end(); |
| 1519 | RI != RE; ++RI) { |
| 1520 | |
| 1521 | if ((*RI)->getValueAsDef("Kind") == ProcResKind |
| 1522 | && (*RI)->getValueAsDef("SchedModel") == PM.ModelDef) { |
| 1523 | if (ProcUnitDef) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1524 | PrintFatalError((*RI)->getLoc(), |
| 1525 | "Multiple ProcessorResourceUnits associated with " |
| 1526 | + ProcResKind->getName()); |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1527 | } |
| 1528 | ProcUnitDef = *RI; |
| 1529 | } |
| 1530 | } |
| 1531 | if (!ProcUnitDef) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1532 | PrintFatalError(ProcResKind->getLoc(), |
| 1533 | "No ProcessorResources associated with " |
| 1534 | + ProcResKind->getName()); |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1535 | } |
| 1536 | return ProcUnitDef; |
| 1537 | } |
| 1538 | |
| 1539 | // Iteratively add a resource and its super resources. |
| 1540 | void CodeGenSchedModels::addProcResource(Record *ProcResKind, |
| 1541 | CodeGenProcModel &PM) { |
| 1542 | for (;;) { |
| 1543 | Record *ProcResUnits = findProcResUnits(ProcResKind, PM); |
| 1544 | |
| 1545 | // See if this ProcResource is already associated with this processor. |
| 1546 | RecIter I = std::find(PM.ProcResourceDefs.begin(), |
| 1547 | PM.ProcResourceDefs.end(), ProcResUnits); |
| 1548 | if (I != PM.ProcResourceDefs.end()) |
| 1549 | return; |
| 1550 | |
| 1551 | PM.ProcResourceDefs.push_back(ProcResUnits); |
| 1552 | if (!ProcResUnits->getValueInit("Super")->isComplete()) |
| 1553 | return; |
| 1554 | |
| 1555 | ProcResKind = ProcResUnits->getValueAsDef("Super"); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | // Add resources for a SchedWrite to this processor if they don't exist. |
| 1560 | void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) { |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 1561 | assert(PIdx && "don't add resources to an invalid Processor model"); |
| 1562 | |
Andrew Trick | 3cbd178 | 2012-09-15 00:20:02 +0000 | [diff] [blame] | 1563 | RecVec &WRDefs = ProcModels[PIdx].WriteResDefs; |
| 1564 | RecIter WRI = std::find(WRDefs.begin(), WRDefs.end(), ProcWriteResDef); |
| 1565 | if (WRI != WRDefs.end()) |
| 1566 | return; |
| 1567 | WRDefs.push_back(ProcWriteResDef); |
| 1568 | |
| 1569 | // Visit ProcResourceKinds referenced by the newly discovered WriteRes. |
| 1570 | RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources"); |
| 1571 | for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end(); |
| 1572 | WritePRI != WritePRE; ++WritePRI) { |
| 1573 | addProcResource(*WritePRI, ProcModels[PIdx]); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | // Add resources for a ReadAdvance to this processor if they don't exist. |
| 1578 | void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef, |
| 1579 | unsigned PIdx) { |
| 1580 | RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs; |
| 1581 | RecIter I = std::find(RADefs.begin(), RADefs.end(), ProcReadAdvanceDef); |
| 1582 | if (I != RADefs.end()) |
| 1583 | return; |
| 1584 | RADefs.push_back(ProcReadAdvanceDef); |
| 1585 | } |
| 1586 | |
Andrew Trick | bc4ff6e | 2012-09-17 22:18:43 +0000 | [diff] [blame] | 1587 | unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const { |
| 1588 | RecIter PRPos = std::find(ProcResourceDefs.begin(), ProcResourceDefs.end(), |
| 1589 | PRDef); |
| 1590 | if (PRPos == ProcResourceDefs.end()) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1591 | PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in " |
| 1592 | "the ProcResources list for " + ModelName); |
Andrew Trick | bc4ff6e | 2012-09-17 22:18:43 +0000 | [diff] [blame] | 1593 | // Idx=0 is reserved for invalid. |
Rafael Espindola | 322ff88 | 2012-11-02 20:57:36 +0000 | [diff] [blame] | 1594 | return 1 + (PRPos - ProcResourceDefs.begin()); |
Andrew Trick | bc4ff6e | 2012-09-17 22:18:43 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 1597 | #ifndef NDEBUG |
| 1598 | void CodeGenProcModel::dump() const { |
| 1599 | dbgs() << Index << ": " << ModelName << " " |
| 1600 | << (ModelDef ? ModelDef->getName() : "inferred") << " " |
| 1601 | << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n'; |
| 1602 | } |
| 1603 | |
| 1604 | void CodeGenSchedRW::dump() const { |
| 1605 | dbgs() << Name << (IsVariadic ? " (V) " : " "); |
| 1606 | if (IsSequence) { |
| 1607 | dbgs() << "("; |
| 1608 | dumpIdxVec(Sequence); |
| 1609 | dbgs() << ")"; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const { |
| 1614 | dbgs() << "SCHEDCLASS " << Name << '\n' |
| 1615 | << " Writes: "; |
| 1616 | for (unsigned i = 0, N = Writes.size(); i < N; ++i) { |
| 1617 | SchedModels->getSchedWrite(Writes[i]).dump(); |
| 1618 | if (i < N-1) { |
| 1619 | dbgs() << '\n'; |
| 1620 | dbgs().indent(10); |
| 1621 | } |
| 1622 | } |
| 1623 | dbgs() << "\n Reads: "; |
| 1624 | for (unsigned i = 0, N = Reads.size(); i < N; ++i) { |
| 1625 | SchedModels->getSchedRead(Reads[i]).dump(); |
| 1626 | if (i < N-1) { |
| 1627 | dbgs() << '\n'; |
| 1628 | dbgs().indent(10); |
| 1629 | } |
| 1630 | } |
| 1631 | dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n'; |
| 1632 | } |
Andrew Trick | 5e613c2 | 2012-09-15 00:19:59 +0000 | [diff] [blame] | 1633 | |
| 1634 | void PredTransitions::dump() const { |
| 1635 | dbgs() << "Expanded Variants:\n"; |
| 1636 | for (std::vector<PredTransition>::const_iterator |
| 1637 | TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) { |
| 1638 | dbgs() << "{"; |
| 1639 | for (SmallVectorImpl<PredCheck>::const_iterator |
| 1640 | PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end(); |
| 1641 | PCI != PCE; ++PCI) { |
| 1642 | if (PCI != TI->PredTerm.begin()) |
| 1643 | dbgs() << ", "; |
| 1644 | dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name |
| 1645 | << ":" << PCI->Predicate->getName(); |
| 1646 | } |
| 1647 | dbgs() << "},\n => {"; |
| 1648 | for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator |
| 1649 | WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end(); |
| 1650 | WSI != WSE; ++WSI) { |
| 1651 | dbgs() << "("; |
| 1652 | for (SmallVectorImpl<unsigned>::const_iterator |
| 1653 | WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) { |
| 1654 | if (WI != WSI->begin()) |
| 1655 | dbgs() << ", "; |
| 1656 | dbgs() << SchedModels.getSchedWrite(*WI).Name; |
| 1657 | } |
| 1658 | dbgs() << "),"; |
| 1659 | } |
| 1660 | dbgs() << "}\n"; |
| 1661 | } |
| 1662 | } |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 1663 | #endif // NDEBUG |