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