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