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