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