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