blob: 7791f4ac9650e28449656ba59701f0782b53656f [file] [log] [blame]
Andrew Trick87255e32012-07-07 04:00:00 +00001//===- 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 Tokercb402912014-01-24 17:20:08 +000010// This file defines structures to encapsulate the machine model as described in
Andrew Trick87255e32012-07-07 04:00:00 +000011// the target description.
12//
13//===----------------------------------------------------------------------===//
14
Andrew Trick87255e32012-07-07 04:00:00 +000015#include "CodeGenSchedule.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000016#include "CodeGenInstruction.h"
Andrew Trick87255e32012-07-07 04:00:00 +000017#include "CodeGenTarget.h"
Craig Topperf19eacf2018-03-21 02:48:34 +000018#include "llvm/ADT/MapVector.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000019#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000023#include "llvm/Support/Casting.h"
Andrew Trick87255e32012-07-07 04:00:00 +000024#include "llvm/Support/Debug.h"
Andrew Trick9e1deb62012-10-03 23:06:32 +000025#include "llvm/Support/Regex.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000026#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000027#include "llvm/TableGen/Error.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000028#include <algorithm>
29#include <iterator>
30#include <utility>
Andrew Trick87255e32012-07-07 04:00:00 +000031
32using namespace llvm;
33
Chandler Carruth97acce22014-04-22 03:06:00 +000034#define DEBUG_TYPE "subtarget-emitter"
35
Andrew Trick76686492012-09-15 00:19:57 +000036#ifndef NDEBUG
Benjamin Kramere1761952015-10-24 12:46:49 +000037static void dumpIdxVec(ArrayRef<unsigned> V) {
38 for (unsigned Idx : V)
39 dbgs() << Idx << ", ";
Andrew Trick33401e82012-09-15 00:19:59 +000040}
Andrew Trick76686492012-09-15 00:19:57 +000041#endif
42
Juergen Ributzka05c5a932013-11-19 03:08:35 +000043namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000044
Andrew Trick9e1deb62012-10-03 23:06:32 +000045// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
46struct InstrsOp : public SetTheory::Operator {
Craig Topper716b0732014-03-05 05:17:42 +000047 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
48 ArrayRef<SMLoc> Loc) override {
Juergen Ributzka05c5a932013-11-19 03:08:35 +000049 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
50 }
51};
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000052
Andrew Trick9e1deb62012-10-03 23:06:32 +000053// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
Andrew Trick9e1deb62012-10-03 23:06:32 +000054struct InstRegexOp : public SetTheory::Operator {
55 const CodeGenTarget &Target;
56 InstRegexOp(const CodeGenTarget &t): Target(t) {}
57
Benjamin Kramercbce2f02018-01-23 23:05:04 +000058 /// 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 Ributzka05c5a932013-11-19 03:08:35 +000079 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
Craig Topper716b0732014-03-05 05:17:42 +000080 ArrayRef<SMLoc> Loc) override {
Javed Absarfc500042017-10-05 13:27:43 +000081 for (Init *Arg : make_range(Expr->arg_begin(), Expr->arg_end())) {
82 StringInit *SI = dyn_cast<StringInit>(Arg);
Juergen Ributzka05c5a932013-11-19 03:08:35 +000083 if (!SI)
Benjamin Kramercbce2f02018-01-23 23:05:04 +000084 PrintFatalError(Loc, "instregex requires pattern string: " +
85 Expr->getAsString());
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000086 StringRef Original = SI->getValue();
87
Benjamin Kramercbce2f02018-01-23 23:05:04 +000088 // Extract a prefix that we can binary search on.
89 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000090 auto FirstMeta = Original.find_first_of(RegexMetachars);
91
Benjamin Kramercbce2f02018-01-23 23:05:04 +000092 // Look for top-level | or ?. We cannot optimize them to binary search.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000093 if (removeParens(Original).find_first_of("|?") != std::string::npos)
Benjamin Kramercbce2f02018-01-23 23:05:04 +000094 FirstMeta = 0;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000095
96 Optional<Regex> Regexpr = None;
97 StringRef Prefix = Original.substr(0, FirstMeta);
Simon Pilgrim34d512e2018-03-24 21:04:20 +000098 StringRef PatStr = Original.substr(FirstMeta);
99 if (!PatStr.empty()) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000100 // For the rest use a python-style prefix match.
Simon Pilgrim34d512e2018-03-24 21:04:20 +0000101 std::string pat = PatStr;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000102 if (pat[0] != '^') {
103 pat.insert(0, "^(");
104 pat.insert(pat.end(), ')');
105 }
106 Regexpr = Regex(pat);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000107 }
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000108
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000109 int NumMatches = 0;
110
Benjamin Kramer4890a712018-01-24 22:35:11 +0000111 unsigned NumGeneric = Target.getNumFixedInstructions();
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000112 ArrayRef<const CodeGenInstruction *> Generics =
113 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1);
114
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000115 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000116 for (auto *Inst : Generics) {
117 StringRef InstName = Inst->TheDef->getName();
118 if (InstName.startswith(Prefix) &&
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000119 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))) {
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000120 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000121 NumMatches++;
122 }
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000123 }
124
125 ArrayRef<const CodeGenInstruction *> Instructions =
Benjamin Kramer4890a712018-01-24 22:35:11 +0000126 Target.getInstructionsByEnumValue().slice(NumGeneric + 1);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000127
128 // Target instructions are sorted. Find the range that starts with our
129 // prefix.
130 struct Comp {
131 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
132 return LHS->TheDef->getName() < RHS;
133 }
134 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
135 return LHS < RHS->TheDef->getName() &&
136 !RHS->TheDef->getName().startswith(LHS);
137 }
138 };
139 auto Range = std::equal_range(Instructions.begin(), Instructions.end(),
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000140 Prefix, Comp());
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000141
142 // For this range we know that it starts with the prefix. Check if there's
143 // a regex that needs to be checked.
144 for (auto *Inst : make_range(Range)) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000145 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000146 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))) {
Craig Topper8a417c12014-12-09 08:05:51 +0000147 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000148 NumMatches++;
149 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000150 }
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000151
152 if (0 == NumMatches)
153 PrintFatalError(Loc, "instregex has no matches: " + Original);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000154 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000155 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000156};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000157
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000158} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000159
Andrew Trick76686492012-09-15 00:19:57 +0000160/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000161CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
162 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000163 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000164
Andrew Trick9e1deb62012-10-03 23:06:32 +0000165 Sets.addFieldExpander("InstRW", "Instrs");
166
167 // Allow Set evaluation to recognize the dags used in InstRW records:
168 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000169 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
170 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000171
Andrew Trick76686492012-09-15 00:19:57 +0000172 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
173 // that are explicitly referenced in tablegen records. Resources associated
174 // with each processor will be derived later. Populate ProcModelMap with the
175 // CodeGenProcModel instances.
176 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000177
Andrew Trick76686492012-09-15 00:19:57 +0000178 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
179 // defined, and populate SchedReads and SchedWrites vectors. Implicit
180 // SchedReadWrites that represent sequences derived from expanded variant will
181 // be inferred later.
182 collectSchedRW();
183
184 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
185 // required by an instruction definition, and populate SchedClassIdxMap. Set
186 // NumItineraryClasses to the number of explicit itinerary classes referenced
187 // by instructions. Set NumInstrSchedClasses to the number of itinerary
188 // classes plus any classes implied by instructions that derive from class
189 // Sched and provide SchedRW list. This does not infer any new classes from
190 // SchedVariant.
191 collectSchedClasses();
192
193 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000194 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000195 // all itinerary classes to be discovered.
196 collectProcItins();
197
198 // Find ItinRW records for each processor and itinerary class.
199 // (For per-operand resources mapped to itinerary classes).
200 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000201
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000202 // Find UnsupportedFeatures records for each processor.
203 // (For per-operand resources mapped to itinerary classes).
204 collectProcUnsupportedFeatures();
205
Andrew Trick33401e82012-09-15 00:19:59 +0000206 // Infer new SchedClasses from SchedVariant.
207 inferSchedClasses();
208
Andrew Trick1e46d482012-09-15 00:20:02 +0000209 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
210 // ProcResourceDefs.
Joel Jones80372332017-06-28 00:06:40 +0000211 DEBUG(dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000212 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000213
214 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000215}
216
Andrew Trick76686492012-09-15 00:19:57 +0000217/// Gather all processor models.
218void CodeGenSchedModels::collectProcModels() {
219 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
220 std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000221
Andrew Trick76686492012-09-15 00:19:57 +0000222 // Reserve space because we can. Reallocation would be ok.
223 ProcModels.reserve(ProcRecords.size()+1);
224
225 // Use idx=0 for NoModel/NoItineraries.
226 Record *NoModelDef = Records.getDef("NoSchedModel");
227 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000228 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000229 ProcModelMap[NoModelDef] = 0;
230
231 // For each processor, find a unique machine model.
Joel Jones80372332017-06-28 00:06:40 +0000232 DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000233 for (Record *ProcRecord : ProcRecords)
234 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000235}
236
237/// Get a unique processor model based on the defined MachineModel and
238/// ProcessorItineraries.
239void CodeGenSchedModels::addProcModel(Record *ProcDef) {
240 Record *ModelKey = getModelOrItinDef(ProcDef);
241 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
242 return;
243
244 std::string Name = ModelKey->getName();
245 if (ModelKey->isSubClassOf("SchedMachineModel")) {
246 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000247 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000248 }
249 else {
250 // An itinerary is defined without a machine model. Infer a new model.
251 if (!ModelKey->getValueAsListOfDefs("IID").empty())
252 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000253 ProcModels.emplace_back(ProcModels.size(), Name,
254 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000255 }
256 DEBUG(ProcModels.back().dump());
257}
258
259// Recursively find all reachable SchedReadWrite records.
260static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
261 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000262 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000263 return;
264 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000265 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000266 if (RWDef->isSubClassOf("WriteSequence")) {
267 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000268 for (Record *WSRec : Seq)
269 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000270 }
271 else if (RWDef->isSubClassOf("SchedVariant")) {
272 // Visit each variant (guarded by a different predicate).
273 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000274 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000275 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000276 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
277 for (Record *SelDef : Selected)
278 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000279 }
280 }
281}
282
283// Collect and sort all SchedReadWrites reachable via tablegen records.
284// More may be inferred later when inferring new SchedClasses from variants.
285void CodeGenSchedModels::collectSchedRW() {
286 // Reserve idx=0 for invalid writes/reads.
287 SchedWrites.resize(1);
288 SchedReads.resize(1);
289
290 SmallPtrSet<Record*, 16> RWSet;
291
292 // Find all SchedReadWrites referenced by instruction defs.
293 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000294 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000295 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000296 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000297 continue;
298 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000299 for (Record *RW : RWs) {
300 if (RW->isSubClassOf("SchedWrite"))
301 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000302 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000303 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
304 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000305 }
306 }
307 }
308 // Find all ReadWrites referenced by InstRW.
309 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000310 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000311 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000312 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
313 for (Record *RWDef : RWDefs) {
314 if (RWDef->isSubClassOf("SchedWrite"))
315 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000316 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000317 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
318 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000319 }
320 }
321 }
322 // Find all ReadWrites referenced by ItinRW.
323 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000324 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000325 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000326 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
327 for (Record *RWDef : RWDefs) {
328 if (RWDef->isSubClassOf("SchedWrite"))
329 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000330 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000331 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
332 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000333 }
334 }
335 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000336 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
337 // for the loop below that initializes Alias vectors.
338 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
339 std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000340 for (Record *ADef : AliasDefs) {
341 Record *MatchDef = ADef->getValueAsDef("MatchRW");
342 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000343 if (MatchDef->isSubClassOf("SchedWrite")) {
344 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000345 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000346 scanSchedRW(AliasDef, SWDefs, RWSet);
347 }
348 else {
349 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
350 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000351 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000352 scanSchedRW(AliasDef, SRDefs, RWSet);
353 }
354 }
Andrew Trick76686492012-09-15 00:19:57 +0000355 // Sort and add the SchedReadWrites directly referenced by instructions or
356 // itinerary resources. Index reads and writes in separate domains.
357 std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000358 for (Record *SWDef : SWDefs) {
359 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
360 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000361 }
362 std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000363 for (Record *SRDef : SRDefs) {
364 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
365 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000366 }
367 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000368 for (CodeGenSchedRW &CGRW : SchedWrites) {
369 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000370 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000371 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000372 /*IsRead=*/false);
373 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000374 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000375 for (Record *ADef : AliasDefs) {
376 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000377 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000378 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000379 CodeGenSchedRW &RW = getSchedRW(MatchDef);
380 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000381 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
382 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000383 }
Andrew Trick76686492012-09-15 00:19:57 +0000384 DEBUG(
Joel Jones80372332017-06-28 00:06:40 +0000385 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
Andrew Trick76686492012-09-15 00:19:57 +0000386 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
387 dbgs() << WIdx << ": ";
388 SchedWrites[WIdx].dump();
389 dbgs() << '\n';
390 }
391 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
392 dbgs() << RIdx << ": ";
393 SchedReads[RIdx].dump();
394 dbgs() << '\n';
395 }
396 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
Javed Absar67b042c2017-09-13 10:31:10 +0000397 for (Record *RWDef : RWDefs) {
398 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
Simon Pilgrim494d0752018-03-24 21:22:32 +0000399 StringRef Name = RWDef->getName();
Andrew Trick76686492012-09-15 00:19:57 +0000400 if (Name != "NoWrite" && Name != "ReadDefault")
Simon Pilgrim494d0752018-03-24 21:22:32 +0000401 dbgs() << "Unused SchedReadWrite " << Name << '\n';
Andrew Trick76686492012-09-15 00:19:57 +0000402 }
403 });
404}
405
406/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000407std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000408 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000409 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000410 if (I != Seq.begin())
411 Name += '_';
412 Name += getSchedRW(*I, IsRead).Name;
413 }
414 Name += ')';
415 return Name;
416}
417
Craig Toppere2611842018-03-21 05:13:04 +0000418unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead) const {
Andrew Trick76686492012-09-15 00:19:57 +0000419 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Craig Toppere2611842018-03-21 05:13:04 +0000420 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin(),
Andrew Trick76686492012-09-15 00:19:57 +0000421 E = RWVec.end(); I != E; ++I) {
422 if (I->TheDef == Def)
423 return I - RWVec.begin();
424 }
425 return 0;
426}
427
Andrew Trickcfe222c2012-09-19 04:43:19 +0000428bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000429 for (const CodeGenSchedRW &Read : SchedReads) {
430 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000431 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
432 continue;
433
434 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000435 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000436 return true;
437 }
438 }
439 return false;
440}
441
Craig Topper6f2cc9b2018-03-21 05:13:01 +0000442static void splitSchedReadWrites(const RecVec &RWDefs,
443 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000444 for (Record *RWDef : RWDefs) {
445 if (RWDef->isSubClassOf("SchedWrite"))
446 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000447 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000448 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
449 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000450 }
451 }
452}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000453
Andrew Trick76686492012-09-15 00:19:57 +0000454// Split the SchedReadWrites defs and call findRWs for each list.
455void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
456 IdxVec &Writes, IdxVec &Reads) const {
457 RecVec WriteDefs;
458 RecVec ReadDefs;
459 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
460 findRWs(WriteDefs, Writes, false);
461 findRWs(ReadDefs, Reads, true);
462}
463
464// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
465void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
466 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000467 for (Record *RWDef : RWDefs) {
468 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000469 assert(Idx && "failed to collect SchedReadWrite");
470 RWs.push_back(Idx);
471 }
472}
473
Andrew Trick33401e82012-09-15 00:19:59 +0000474void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
475 bool IsRead) const {
476 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
477 if (!SchedRW.IsSequence) {
478 RWSeq.push_back(RWIdx);
479 return;
480 }
481 int Repeat =
482 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
483 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000484 for (unsigned I : SchedRW.Sequence) {
485 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000486 }
487 }
488}
489
Andrew Trickda984b12012-10-03 23:06:28 +0000490// Expand a SchedWrite as a sequence following any aliases that coincide with
491// the given processor model.
492void CodeGenSchedModels::expandRWSeqForProc(
493 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
494 const CodeGenProcModel &ProcModel) const {
495
496 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000497 Record *AliasDef = nullptr;
Andrew Trickda984b12012-10-03 23:06:28 +0000498 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
499 AI != AE; ++AI) {
500 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
501 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
502 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
503 if (&getProcModel(ModelDef) != &ProcModel)
504 continue;
505 }
506 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000507 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
508 "defined for processor " + ProcModel.ModelName +
509 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000510 AliasDef = AliasRW.TheDef;
511 }
512 if (AliasDef) {
513 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
514 RWSeq, IsRead,ProcModel);
515 return;
516 }
517 if (!SchedWrite.IsSequence) {
518 RWSeq.push_back(RWIdx);
519 return;
520 }
521 int Repeat =
522 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
523 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000524 for (unsigned I : SchedWrite.Sequence) {
525 expandRWSeqForProc(I, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000526 }
527 }
528}
529
Andrew Trick33401e82012-09-15 00:19:59 +0000530// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000531unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000532 bool IsRead) {
533 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
534
535 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
536 I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000537 if (makeArrayRef(I->Sequence) == Seq)
Andrew Trick33401e82012-09-15 00:19:59 +0000538 return I - RWVec.begin();
539 }
540 // Index zero reserved for invalid RW.
541 return 0;
542}
543
544/// Add this ReadWrite if it doesn't already exist.
545unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
546 bool IsRead) {
547 assert(!Seq.empty() && "cannot insert empty sequence");
548 if (Seq.size() == 1)
549 return Seq.back();
550
551 unsigned Idx = findRWForSequence(Seq, IsRead);
552 if (Idx)
553 return Idx;
554
Andrew Trickda984b12012-10-03 23:06:28 +0000555 unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
556 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
557 if (IsRead)
Andrew Trick33401e82012-09-15 00:19:59 +0000558 SchedReads.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000559 else
560 SchedWrites.push_back(SchedRW);
561 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000562}
563
Andrew Trick76686492012-09-15 00:19:57 +0000564/// Visit all the instruction definitions for this target to gather and
565/// enumerate the itinerary classes. These are the explicitly specified
566/// SchedClasses. More SchedClasses may be inferred.
567void CodeGenSchedModels::collectSchedClasses() {
568
569 // NoItinerary is always the first class at Idx=0
Craig Topper281a19c2018-03-22 06:15:08 +0000570 assert(SchedClasses.empty() && "Expected empty sched class");
571 SchedClasses.emplace_back(0, "NoInstrModel",
572 Records.getDef("NoItinerary"));
Andrew Trick76686492012-09-15 00:19:57 +0000573 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000574
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000575 // Create a SchedClass for each unique combination of itinerary class and
576 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000577 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000578 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000579 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000580 if (!Inst->TheDef->isValueUnset("SchedRW"))
581 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000582
Andrew Trick76686492012-09-15 00:19:57 +0000583 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topper281a19c2018-03-22 06:15:08 +0000584 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topper8a417c12014-12-09 08:05:51 +0000585 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000586 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000587 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000588 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
589 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000590 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000591 for (Record *RWDef : InstRWDefs)
592 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000593
Andrew Trick76686492012-09-15 00:19:57 +0000594 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000595
Andrew Trick76686492012-09-15 00:19:57 +0000596 bool EnableDump = false;
597 DEBUG(EnableDump = true);
598 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000599 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000600
Joel Jones80372332017-06-28 00:06:40 +0000601 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000602 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000603 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim949437e2018-03-21 18:09:34 +0000604 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000605 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000606 if (!Inst->hasNoSchedulingInfo)
607 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000608 continue;
609 }
610 CodeGenSchedClass &SC = getSchedClass(SCIdx);
611 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000612 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000613 "must not be subtarget specific.");
614
615 IdxVec ProcIndices;
616 if (SC.ItinClassDef->getName() != "NoItinerary") {
617 ProcIndices.push_back(0);
618 dbgs() << "Itinerary for " << InstName << ": "
619 << SC.ItinClassDef->getName() << '\n';
620 }
621 if (!SC.Writes.empty()) {
622 ProcIndices.push_back(0);
623 dbgs() << "SchedRW machine model for " << InstName;
624 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
625 dbgs() << " " << SchedWrites[*WI].Name;
626 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
627 dbgs() << " " << SchedReads[*RI].Name;
628 dbgs() << '\n';
629 }
630 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000631 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000632 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000633 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000634 ProcIndices.push_back(ProcModel.Index);
635 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000636 IdxVec Writes;
637 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000638 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000639 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000640 for (unsigned WIdx : Writes)
641 dbgs() << " " << SchedWrites[WIdx].Name;
642 for (unsigned RIdx : Reads)
643 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000644 dbgs() << '\n';
645 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000646 // If ProcIndices contains zero, the class applies to all processors.
647 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
Javed Absar21c75912017-10-09 16:21:25 +0000648 for (const CodeGenProcModel &PM : ProcModels) {
Javed Absarfc500042017-10-05 13:27:43 +0000649 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
Andrew Trickf9df92c92016-10-18 04:17:44 +0000650 dbgs() << "No machine model for " << Inst->TheDef->getName()
Javed Absarfc500042017-10-05 13:27:43 +0000651 << " on processor " << PM.ModelName << '\n';
Andrew Trickf9df92c92016-10-18 04:17:44 +0000652 }
Andrew Trick87255e32012-07-07 04:00:00 +0000653 }
654 }
Andrew Trick76686492012-09-15 00:19:57 +0000655}
656
Andrew Trick76686492012-09-15 00:19:57 +0000657/// Find an SchedClass that has been inferred from a per-operand list of
658/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000659unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000660 ArrayRef<unsigned> Writes,
661 ArrayRef<unsigned> Reads) const {
Simon Pilgrim4cca3b12018-03-21 17:57:21 +0000662 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I)
663 if (I->isKeyEqual(ItinClassDef, Writes, Reads))
Andrew Trick76686492012-09-15 00:19:57 +0000664 return I - schedClassBegin();
Andrew Trick76686492012-09-15 00:19:57 +0000665 return 0;
666}
Andrew Trick87255e32012-07-07 04:00:00 +0000667
Andrew Trick76686492012-09-15 00:19:57 +0000668// Get the SchedClass index for an instruction.
669unsigned CodeGenSchedModels::getSchedClassIdx(
670 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000671
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000672 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000673}
674
Benjamin Kramere1761952015-10-24 12:46:49 +0000675std::string
676CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
677 ArrayRef<unsigned> OperWrites,
678 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000679
680 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000681 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
682 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000683 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000684 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000685 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000686 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000687 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000688 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000689 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000690 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000691 }
692 return Name;
693}
694
695std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
696
697 std::string Name;
698 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
699 if (I != InstDefs.begin())
700 Name += '_';
701 Name += (*I)->getName();
702 }
703 return Name;
704}
705
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000706/// Add an inferred sched class from an itinerary class and per-operand list of
707/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
708/// processors that may utilize this class.
709unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000710 ArrayRef<unsigned> OperWrites,
711 ArrayRef<unsigned> OperReads,
712 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000713 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
714
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000715 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
716 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000717 IdxVec PI;
718 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
719 SchedClasses[Idx].ProcIndices.end(),
720 ProcIndices.begin(), ProcIndices.end(),
721 std::back_inserter(PI));
Craig Topper59d13772018-03-24 22:58:00 +0000722 SchedClasses[Idx].ProcIndices = std::move(PI);
Andrew Trick76686492012-09-15 00:19:57 +0000723 return Idx;
724 }
725 Idx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000726 SchedClasses.emplace_back(Idx,
727 createSchedClassName(ItinClassDef, OperWrites,
728 OperReads),
729 ItinClassDef);
Andrew Trick76686492012-09-15 00:19:57 +0000730 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick76686492012-09-15 00:19:57 +0000731 SC.Writes = OperWrites;
732 SC.Reads = OperReads;
733 SC.ProcIndices = ProcIndices;
734
735 return Idx;
736}
737
738// Create classes for each set of opcodes that are in the same InstReadWrite
739// definition across all processors.
740void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
741 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
742 // intersects with an existing class via a previous InstRWDef. Instrs that do
743 // not intersect with an existing class refer back to their former class as
744 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +0000745 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000746 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000747 const RecVec *InstDefs = Sets.expand(InstRWDef);
748 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000749 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000750
Craig Topper93dd77d2018-03-18 08:38:03 +0000751 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +0000752 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000753 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +0000754 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000755 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +0000756 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +0000757 }
758 // For each set of Instrs, create a new class if necessary, and map or remap
759 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +0000760 for (auto &Entry : ClassInstrs) {
761 unsigned OldSCIdx = Entry.first;
762 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +0000763 // If the all instrs in the current class are accounted for, then leave
764 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000765 if (OldSCIdx) {
766 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
767 if (!RWDefs.empty()) {
768 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper06d78372018-03-21 19:30:30 +0000769 unsigned OrigNumInstrs =
770 count_if(*OrigInstDefs, [&](Record *OIDef) {
771 return InstrClassMap[OIDef] == OldSCIdx;
772 });
Andrew Trick78a08512013-06-05 06:55:20 +0000773 if (OrigNumInstrs == InstDefs.size()) {
774 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
775 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000776 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
777 // Make sure we didn't already have a InstRW containing this
778 // instruction on this model.
779 for (Record *RWD : RWDefs) {
780 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
781 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
782 for (Record *Inst : InstDefs) {
783 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
784 Inst->getName() + " also matches " +
785 RWD->getValue("Instrs")->getValue()->getAsString());
786 }
787 }
788 }
Andrew Trick78a08512013-06-05 06:55:20 +0000789 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
790 << SchedClasses[OldSCIdx].Name << " on "
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000791 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +0000792 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
793 continue;
794 }
795 }
Andrew Trick76686492012-09-15 00:19:57 +0000796 }
797 unsigned SCIdx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000798 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick76686492012-09-15 00:19:57 +0000799 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick78a08512013-06-05 06:55:20 +0000800 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
801 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
802
Andrew Trick76686492012-09-15 00:19:57 +0000803 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
804 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
805 SC.Writes = SchedClasses[OldSCIdx].Writes;
806 SC.Reads = SchedClasses[OldSCIdx].Reads;
807 SC.ProcIndices.push_back(0);
Craig Topper989d94d2018-03-21 19:52:13 +0000808 // If we had an old class, copy it's InstRWs to this new class.
809 if (OldSCIdx) {
810 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
811 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
812 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
813 for (Record *InstDef : InstDefs) {
Craig Topper9fbbe5d2018-03-21 19:30:31 +0000814 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
815 InstDef->getName() + " also matches " +
816 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick9e1deb62012-10-03 23:06:32 +0000817 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000818 }
Craig Topper989d94d2018-03-21 19:52:13 +0000819 assert(OldRWDef != InstRWDef &&
820 "SchedClass has duplicate InstRW def");
821 SC.InstRWs.push_back(OldRWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000822 }
Andrew Trick76686492012-09-15 00:19:57 +0000823 }
Craig Topper989d94d2018-03-21 19:52:13 +0000824 // Map each Instr to this new class.
825 for (Record *InstDef : InstDefs)
826 InstrClassMap[InstDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000827 SC.InstRWs.push_back(InstRWDef);
828 }
Andrew Trick87255e32012-07-07 04:00:00 +0000829}
830
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000831// True if collectProcItins found anything.
832bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000833 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
834 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000835 return true;
836 }
837 return false;
838}
839
Andrew Trick87255e32012-07-07 04:00:00 +0000840// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000841void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000842 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000843 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000844 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000845 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000846
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000847 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
848 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
849
850 // Populate ItinDefList with Itinerary records.
851 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000852
853 // Insert each itinerary data record in the correct position within
854 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +0000855 for (Record *ItinData : ItinRecords) {
Andrew Trick76686492012-09-15 00:19:57 +0000856 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000857 bool FoundClass = false;
858 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
859 SCI != SCE; ++SCI) {
860 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000861 if (SCI->ItinClassDef == ItinDef) {
862 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000863 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000864 }
Andrew Trick76686492012-09-15 00:19:57 +0000865 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000866 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000867 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
868 << " missing class for itinerary " << ItinDef->getName() << '\n');
869 }
Andrew Trick87255e32012-07-07 04:00:00 +0000870 }
Andrew Trick76686492012-09-15 00:19:57 +0000871 // Check for missing itinerary entries.
872 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
873 DEBUG(
874 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
875 if (!ProcModel.ItinDefList[i])
876 dbgs() << ProcModel.ItinsDef->getName()
877 << " missing itinerary for class "
878 << SchedClasses[i].Name << '\n';
879 });
Andrew Trick87255e32012-07-07 04:00:00 +0000880 }
Andrew Trick87255e32012-07-07 04:00:00 +0000881}
Andrew Trick76686492012-09-15 00:19:57 +0000882
883// Gather the read/write types for each itinerary class.
884void CodeGenSchedModels::collectProcItinRW() {
885 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
886 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +0000887 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000888 if (!RWDef->getValueInit("SchedModel")->isComplete())
889 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
890 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +0000891 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
892 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000893 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000894 + ModelDef->getName());
895 }
Javed Absarf45d0b92017-10-08 17:23:30 +0000896 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000897 }
898}
899
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000900// Gather the unsupported features for processor models.
901void CodeGenSchedModels::collectProcUnsupportedFeatures() {
902 for (CodeGenProcModel &ProcModel : ProcModels) {
903 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
904 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
905 }
906 }
907}
908
Andrew Trick33401e82012-09-15 00:19:59 +0000909/// Infer new classes from existing classes. In the process, this may create new
910/// SchedWrites from sequences of existing SchedWrites.
911void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000912 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000913 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
914
Andrew Trick33401e82012-09-15 00:19:59 +0000915 // Visit all existing classes and newly created classes.
916 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000917 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
918
Andrew Trick33401e82012-09-15 00:19:59 +0000919 if (SchedClasses[Idx].ItinClassDef)
920 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000921 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000922 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000923 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000924 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
925 Idx, SchedClasses[Idx].ProcIndices);
926 }
927 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
928 "too many SchedVariants");
929 }
930}
931
932/// Infer classes from per-processor itinerary resources.
933void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
934 unsigned FromClassIdx) {
935 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
936 const CodeGenProcModel &PM = ProcModels[PIdx];
937 // For all ItinRW entries.
938 bool HasMatch = false;
939 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
940 II != IE; ++II) {
941 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
942 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
943 continue;
944 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000945 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000946 + ItinClassDef->getName()
947 + " in ItinResources for " + PM.ModelName);
948 HasMatch = true;
949 IdxVec Writes, Reads;
950 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +0000951 inferFromRW(Writes, Reads, FromClassIdx, PIdx);
Andrew Trick33401e82012-09-15 00:19:59 +0000952 }
953 }
954}
955
956/// Infer classes from per-processor InstReadWrite definitions.
957void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000958 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000959 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000960 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
961 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000962 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000963 for (; II != IE; ++II) {
964 if (InstrClassMap[*II] == SCIdx)
965 break;
966 }
967 // If this class no longer has any instructions mapped to it, it has become
968 // irrelevant.
969 if (II == IE)
970 continue;
971 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000972 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
973 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Craig Topper9f3293a2018-03-24 21:57:35 +0000974 inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +0000975 }
976}
977
978namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000979
Andrew Trick9257b8f2012-09-22 02:24:21 +0000980// Helper for substituteVariantOperand.
981struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +0000982 Record *VarOrSeqDef; // Variant or sequence.
983 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000984 unsigned ProcIdx; // Processor model index or zero for any.
985 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
986
987 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +0000988 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +0000989};
990
Andrew Trick33401e82012-09-15 00:19:59 +0000991// Associate a predicate with the SchedReadWrite that it guards.
992// RWIdx is the index of the read/write variant.
993struct PredCheck {
994 bool IsRead;
995 unsigned RWIdx;
996 Record *Predicate;
997
998 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
999};
1000
1001// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1002struct PredTransition {
1003 // A predicate term is a conjunction of PredChecks.
1004 SmallVector<PredCheck, 4> PredTerm;
1005 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1006 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001007 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001008};
1009
1010// Encapsulate a set of partially constructed transitions.
1011// The results are built by repeated calls to substituteVariants.
1012class PredTransitions {
1013 CodeGenSchedModels &SchedModels;
1014
1015public:
1016 std::vector<PredTransition> TransVec;
1017
1018 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1019
1020 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1021 bool IsRead, unsigned StartIdx);
1022
1023 void substituteVariants(const PredTransition &Trans);
1024
1025#ifndef NDEBUG
1026 void dump() const;
1027#endif
1028
1029private:
1030 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001031 void getIntersectingVariants(
1032 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1033 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001034 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001035};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001036
1037} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001038
1039// Return true if this predicate is mutually exclusive with a PredTerm. This
1040// degenerates into checking if the predicate is mutually exclusive with any
1041// predicate in the Term's conjunction.
1042//
1043// All predicates associated with a given SchedRW are considered mutually
1044// exclusive. This should work even if the conditions expressed by the
1045// predicates are not exclusive because the predicates for a given SchedWrite
1046// are always checked in the order they are defined in the .td file. Later
1047// conditions implicitly negate any prior condition.
1048bool PredTransitions::mutuallyExclusive(Record *PredDef,
1049 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001050 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001051 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001052 return false;
1053
Javed Absarfc500042017-10-05 13:27:43 +00001054 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001055 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1056 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1057 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1058 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1059 return true;
1060 }
1061 }
1062 return false;
1063}
1064
Andrew Trickda984b12012-10-03 23:06:28 +00001065static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1066 CodeGenSchedModels &SchedModels) {
1067 if (RW.HasVariants)
1068 return true;
1069
Javed Absar21c75912017-10-09 16:21:25 +00001070 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001071 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001072 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001073 if (AliasRW.HasVariants)
1074 return true;
1075 if (AliasRW.IsSequence) {
1076 IdxVec ExpandedRWs;
1077 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1078 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1079 SI != SE; ++SI) {
1080 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1081 SchedModels)) {
1082 return true;
1083 }
1084 }
1085 }
1086 }
1087 return false;
1088}
1089
1090static bool hasVariant(ArrayRef<PredTransition> Transitions,
1091 CodeGenSchedModels &SchedModels) {
1092 for (ArrayRef<PredTransition>::iterator
1093 PTI = Transitions.begin(), PTE = Transitions.end();
1094 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001095 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001096 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1097 WSI != WSE; ++WSI) {
1098 for (SmallVectorImpl<unsigned>::const_iterator
1099 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1100 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1101 return true;
1102 }
1103 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001104 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001105 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1106 RSI != RSE; ++RSI) {
1107 for (SmallVectorImpl<unsigned>::const_iterator
1108 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1109 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1110 return true;
1111 }
1112 }
1113 }
1114 return false;
1115}
1116
1117// Populate IntersectingVariants with any variants or aliased sequences of the
1118// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001119// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001120void PredTransitions::getIntersectingVariants(
1121 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1122 std::vector<TransVariant> &IntersectingVariants) {
1123
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001124 bool GenericRW = false;
1125
Andrew Trickda984b12012-10-03 23:06:28 +00001126 std::vector<TransVariant> Variants;
1127 if (SchedRW.HasVariants) {
1128 unsigned VarProcIdx = 0;
1129 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1130 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1131 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1132 }
1133 // Push each variant. Assign TransVecIdx later.
1134 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001135 for (Record *VarDef : VarDefs)
1136 Variants.push_back(TransVariant(VarDef, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001137 if (VarProcIdx == 0)
1138 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001139 }
1140 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1141 AI != AE; ++AI) {
1142 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1143 // to is defined within a processor model, constrain all variants to
1144 // that processor.
1145 unsigned AliasProcIdx = 0;
1146 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1147 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1148 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1149 }
1150 const CodeGenSchedRW &AliasRW =
1151 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1152
1153 if (AliasRW.HasVariants) {
1154 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001155 for (Record *VD : VarDefs)
1156 Variants.push_back(TransVariant(VD, AliasRW.Index, AliasProcIdx, 0));
Andrew Trickda984b12012-10-03 23:06:28 +00001157 }
1158 if (AliasRW.IsSequence) {
1159 Variants.push_back(
1160 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1161 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001162 if (AliasProcIdx == 0)
1163 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001164 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001165 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001166 // Don't expand variants if the processor models don't intersect.
1167 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001168 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001169 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001170 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1171 Variant.ProcIdx);
1172 if (!Cnt)
1173 continue;
1174 if (Cnt > 1) {
1175 const CodeGenProcModel &PM =
1176 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001177 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1178 "Multiple variants defined for processor " +
1179 PM.ModelName +
1180 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001181 }
1182 }
1183 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1184 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1185 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1186 continue;
1187 }
1188 if (IntersectingVariants.empty()) {
1189 // The first variant builds on the existing transition.
1190 Variant.TransVecIdx = TransIdx;
1191 IntersectingVariants.push_back(Variant);
1192 }
1193 else {
1194 // Push another copy of the current transition for more variants.
1195 Variant.TransVecIdx = TransVec.size();
1196 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001197 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001198 }
1199 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001200 if (GenericRW && IntersectingVariants.empty()) {
1201 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1202 "a matching predicate on any processor");
1203 }
Andrew Trickda984b12012-10-03 23:06:28 +00001204}
1205
Andrew Trick9257b8f2012-09-22 02:24:21 +00001206// Push the Reads/Writes selected by this variant onto the PredTransition
1207// specified by VInfo.
1208void PredTransitions::
1209pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001210 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1211
Andrew Trick9257b8f2012-09-22 02:24:21 +00001212 // If this operand transition is reached through a processor-specific alias,
1213 // then the whole transition is specific to this processor.
1214 if (VInfo.ProcIdx != 0)
1215 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1216
Andrew Trick33401e82012-09-15 00:19:59 +00001217 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001218 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1219 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1220 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1221 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1222 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1223 }
1224 else {
1225 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1226 "variant must be a SchedVariant or aliased WriteSequence");
1227 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1228 }
Andrew Trick33401e82012-09-15 00:19:59 +00001229
Andrew Trick9257b8f2012-09-22 02:24:21 +00001230 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001231
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001232 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001233 ? Trans.ReadSequences : Trans.WriteSequences;
1234 if (SchedRW.IsVariadic) {
1235 unsigned OperIdx = RWSequences.size()-1;
1236 // Make N-1 copies of this transition's last sequence.
1237 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001238 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001239 RWSequences.reserve(RWSequences.size() + 1);
1240 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001241 }
1242 // Push each of the N elements of the SelectedRWs onto a copy of the last
1243 // sequence (split the current operand into N operands).
1244 // Note that write sequences should be expanded within this loop--the entire
1245 // sequence belongs to a single operand.
1246 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1247 RWI != RWE; ++RWI, ++OperIdx) {
1248 IdxVec ExpandedRWs;
1249 if (IsRead)
1250 ExpandedRWs.push_back(*RWI);
1251 else
1252 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1253 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1254 ExpandedRWs.begin(), ExpandedRWs.end());
1255 }
1256 assert(OperIdx == RWSequences.size() && "missed a sequence");
1257 }
1258 else {
1259 // Push this transition's expanded sequence onto this transition's last
1260 // sequence (add to the current operand's sequence).
1261 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1262 IdxVec ExpandedRWs;
1263 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1264 RWI != RWE; ++RWI) {
1265 if (IsRead)
1266 ExpandedRWs.push_back(*RWI);
1267 else
1268 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1269 }
1270 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1271 }
1272}
1273
1274// RWSeq is a sequence of all Reads or all Writes for the next read or write
1275// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001276// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001277// of TransVec.
1278void PredTransitions::substituteVariantOperand(
1279 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1280
1281 // Visit each original RW within the current sequence.
1282 for (SmallVectorImpl<unsigned>::const_iterator
1283 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1284 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1285 // Push this RW on all partial PredTransitions or distribute variants.
1286 // New PredTransitions may be pushed within this loop which should not be
1287 // revisited (TransEnd must be loop invariant).
1288 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1289 TransIdx != TransEnd; ++TransIdx) {
1290 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001291 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001292 if (IsRead)
1293 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1294 else
1295 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1296 continue;
1297 }
1298 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001299 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001300 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001301 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001302 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001303 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001304 IVI = IntersectingVariants.begin(),
1305 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001306 IVI != IVE; ++IVI) {
1307 pushVariant(*IVI, IsRead);
1308 }
Andrew Trick33401e82012-09-15 00:19:59 +00001309 }
1310 }
1311}
1312
1313// For each variant of a Read/Write in Trans, substitute the sequence of
1314// Read/Writes guarded by the variant. This is exponential in the number of
1315// variant Read/Writes, but in practice detection of mutually exclusive
1316// predicates should result in linear growth in the total number variants.
1317//
1318// This is one step in a breadth-first search of nested variants.
1319void PredTransitions::substituteVariants(const PredTransition &Trans) {
1320 // Build up a set of partial results starting at the back of
1321 // PredTransitions. Remember the first new transition.
1322 unsigned StartIdx = TransVec.size();
Craig Topper195aaaf2018-03-22 06:15:10 +00001323 TransVec.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001324 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001325 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001326
1327 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001328 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001329 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1330 WSI != WSE; ++WSI) {
1331 // Push a new (empty) write sequence onto all partial Transitions.
1332 for (std::vector<PredTransition>::iterator I =
1333 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001334 I->WriteSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001335 }
1336 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1337 }
1338 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001339 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001340 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1341 RSI != RSE; ++RSI) {
1342 // Push a new (empty) read sequence onto all partial Transitions.
1343 for (std::vector<PredTransition>::iterator I =
1344 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001345 I->ReadSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001346 }
1347 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1348 }
1349}
1350
Andrew Trick33401e82012-09-15 00:19:59 +00001351// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001352static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001353 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001354 CodeGenSchedModels &SchedModels) {
1355 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1356 // requires creating a new SchedClass.
1357 for (ArrayRef<PredTransition>::iterator
1358 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1359 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001360 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1361 [&SchedModels](ArrayRef<unsigned> WS) {
1362 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1363 });
Andrew Trick33401e82012-09-15 00:19:59 +00001364 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001365 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1366 [&SchedModels](ArrayRef<unsigned> RS) {
1367 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1368 });
Andrew Trick33401e82012-09-15 00:19:59 +00001369 CodeGenSchedTransition SCTrans;
1370 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001371 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Craig Topper2ed54072018-03-24 22:58:03 +00001372 OperReadsVariant, I->ProcIndices);
1373 SCTrans.ProcIndices.assign(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001374 // The final PredTerm is unique set of predicates guarding the transition.
1375 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001376 transform(I->PredTerm, std::back_inserter(Preds),
1377 [](const PredCheck &P) {
1378 return P.Predicate;
1379 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001380 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Craig Topper18cfa2c2018-03-24 22:58:02 +00001381 SCTrans.PredTerm = std::move(Preds);
1382 SchedModels.getSchedClass(FromClassIdx)
1383 .Transitions.push_back(std::move(SCTrans));
Andrew Trick33401e82012-09-15 00:19:59 +00001384 }
1385}
1386
Andrew Trick9257b8f2012-09-22 02:24:21 +00001387// Create new SchedClasses for the given ReadWrite list. If any of the
1388// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1389// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001390void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1391 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001392 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001393 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001394 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001395
1396 // Create a seed transition with an empty PredTerm and the expanded sequences
1397 // of SchedWrites for the current SchedClass.
1398 std::vector<PredTransition> LastTransitions;
Craig Topper195aaaf2018-03-22 06:15:10 +00001399 LastTransitions.emplace_back();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001400 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1401 ProcIndices.end());
1402
Benjamin Kramere1761952015-10-24 12:46:49 +00001403 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001404 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001405 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topper195aaaf2018-03-22 06:15:10 +00001406 LastTransitions[0].WriteSequences.emplace_back();
1407 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001408 Seq.append(WriteSeq.begin(), WriteSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001409 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1410 }
1411 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001412 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001413 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001414 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topper195aaaf2018-03-22 06:15:10 +00001415 LastTransitions[0].ReadSequences.emplace_back();
1416 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001417 Seq.append(ReadSeq.begin(), ReadSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001418 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1419 }
1420 DEBUG(dbgs() << '\n');
1421
1422 // Collect all PredTransitions for individual operands.
1423 // Iterate until no variant writes remain.
1424 while (hasVariant(LastTransitions, *this)) {
1425 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001426 for (const PredTransition &Trans : LastTransitions)
1427 Transitions.substituteVariants(Trans);
Andrew Trick33401e82012-09-15 00:19:59 +00001428 DEBUG(Transitions.dump());
1429 LastTransitions.swap(Transitions.TransVec);
1430 }
1431 // If the first transition has no variants, nothing to do.
1432 if (LastTransitions[0].PredTerm.empty())
1433 return;
1434
1435 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1436 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001437 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001438}
1439
Andrew Trickcf398b22013-04-23 23:45:14 +00001440// Check if any processor resource group contains all resource records in
1441// SubUnits.
1442bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1443 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1444 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1445 continue;
1446 RecVec SuperUnits =
1447 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1448 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1449 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001450 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001451 break;
1452 }
1453 }
1454 if (RI == RE)
1455 return true;
1456 }
1457 return false;
1458}
1459
1460// Verify that overlapping groups have a common supergroup.
1461void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1462 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1463 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1464 continue;
1465 RecVec CheckUnits =
1466 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1467 for (unsigned j = i+1; j < e; ++j) {
1468 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1469 continue;
1470 RecVec OtherUnits =
1471 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1472 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1473 OtherUnits.begin(), OtherUnits.end())
1474 != CheckUnits.end()) {
1475 // CheckUnits and OtherUnits overlap
1476 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1477 CheckUnits.end());
1478 if (!hasSuperGroup(OtherUnits, PM)) {
1479 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1480 "proc resource group overlaps with "
1481 + PM.ProcResourceDefs[j]->getName()
1482 + " but no supergroup contains both.");
1483 }
1484 }
1485 }
1486 }
1487}
1488
Andrew Trick1e46d482012-09-15 00:20:02 +00001489// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1490void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001491 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1492 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1493
Andrew Trick1e46d482012-09-15 00:20:02 +00001494 // Add any subtarget-specific SchedReadWrites that are directly associated
1495 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1496 // determine which processors they apply to.
1497 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1498 SCI != SCE; ++SCI) {
1499 if (SCI->ItinClassDef)
1500 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001501 else {
1502 // This class may have a default ReadWrite list which can be overriden by
1503 // InstRW definitions.
1504 if (!SCI->InstRWs.empty()) {
1505 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1506 RWI != RWE; ++RWI) {
1507 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
Craig Topper9f3293a2018-03-24 21:57:35 +00001508 unsigned PIdx = getProcModel(RWModelDef).Index;
Andrew Trick4fe440d2013-02-01 03:19:54 +00001509 IdxVec Writes, Reads;
1510 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1511 Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001512 collectRWResources(Writes, Reads, PIdx);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001513 }
1514 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001515 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001516 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001517 }
1518 // Add resources separately defined by each subtarget.
1519 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001520 for (Record *WR : WRDefs) {
1521 Record *ModelDef = WR->getValueAsDef("SchedModel");
1522 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001523 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001524 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001525 for (Record *SWR : SWRDefs) {
1526 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1527 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001528 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001529 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001530 for (Record *RA : RADefs) {
1531 Record *ModelDef = RA->getValueAsDef("SchedModel");
1532 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001533 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001534 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001535 for (Record *SRA : SRADefs) {
1536 if (SRA->getValueInit("SchedModel")->isComplete()) {
1537 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1538 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001539 }
1540 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001541 // Add ProcResGroups that are defined within this processor model, which may
1542 // not be directly referenced but may directly specify a buffer size.
1543 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001544 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001545 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001546 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001547 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1548 if (!is_contained(PM.ProcResourceDefs, PRG))
1549 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001550 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001551 // Add ProcResourceUnits unconditionally.
1552 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1553 if (!PRU->getValueInit("SchedModel")->isComplete())
1554 continue;
1555 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1556 if (!is_contained(PM.ProcResourceDefs, PRU))
1557 PM.ProcResourceDefs.push_back(PRU);
1558 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001559 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001560 for (CodeGenProcModel &PM : ProcModels) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001561 std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1562 LessRecord());
1563 std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1564 LessRecord());
1565 std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1566 LessRecord());
1567 DEBUG(
1568 PM.dump();
1569 dbgs() << "WriteResDefs: ";
1570 for (RecIter RI = PM.WriteResDefs.begin(),
1571 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1572 if ((*RI)->isSubClassOf("WriteRes"))
1573 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1574 else
1575 dbgs() << (*RI)->getName() << " ";
1576 }
1577 dbgs() << "\nReadAdvanceDefs: ";
1578 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1579 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1580 if ((*RI)->isSubClassOf("ReadAdvance"))
1581 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1582 else
1583 dbgs() << (*RI)->getName() << " ";
1584 }
1585 dbgs() << "\nProcResourceDefs: ";
1586 for (RecIter RI = PM.ProcResourceDefs.begin(),
1587 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1588 dbgs() << (*RI)->getName() << " ";
1589 }
1590 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001591 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001592 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001593
1594 ProcResourceDefs.clear();
1595 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001596}
1597
Matthias Braun17cb5792016-03-01 20:03:21 +00001598void CodeGenSchedModels::checkCompleteness() {
1599 bool Complete = true;
1600 bool HadCompleteModel = false;
1601 for (const CodeGenProcModel &ProcModel : procModels()) {
Matthias Braun17cb5792016-03-01 20:03:21 +00001602 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1603 continue;
1604 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1605 if (Inst->hasNoSchedulingInfo)
1606 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001607 if (ProcModel.isUnsupported(*Inst))
1608 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001609 unsigned SCIdx = getSchedClassIdx(*Inst);
1610 if (!SCIdx) {
1611 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1612 PrintError("No schedule information for instruction '"
1613 + Inst->TheDef->getName() + "'");
1614 Complete = false;
1615 }
1616 continue;
1617 }
1618
1619 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1620 if (!SC.Writes.empty())
1621 continue;
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001622 if (SC.ItinClassDef != nullptr &&
1623 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001624 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001625
1626 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001627 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1628 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1629 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001630 if (I == InstRWs.end()) {
1631 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1632 Inst->TheDef->getName() + "'");
1633 Complete = false;
1634 }
1635 }
1636 HadCompleteModel = true;
1637 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001638 if (!Complete) {
1639 errs() << "\n\nIncomplete schedule models found.\n"
1640 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1641 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1642 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001643 "you may temporarily use an empty list.\n"
1644 << "- Instructions related to unsupported features can be excluded with "
1645 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1646 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001647 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001648 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001649}
1650
Andrew Trick1e46d482012-09-15 00:20:02 +00001651// Collect itinerary class resources for each processor.
1652void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1653 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1654 const CodeGenProcModel &PM = ProcModels[PIdx];
1655 // For all ItinRW entries.
1656 bool HasMatch = false;
1657 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1658 II != IE; ++II) {
1659 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1660 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1661 continue;
1662 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001663 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1664 + ItinClassDef->getName()
1665 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001666 HasMatch = true;
1667 IdxVec Writes, Reads;
1668 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001669 collectRWResources(Writes, Reads, PIdx);
Andrew Trick1e46d482012-09-15 00:20:02 +00001670 }
1671 }
1672}
1673
Andrew Trickd0b9c442012-10-10 05:43:13 +00001674void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001675 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001676 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1677 if (SchedRW.TheDef) {
1678 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001679 for (unsigned Idx : ProcIndices)
1680 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001681 }
1682 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001683 for (unsigned Idx : ProcIndices)
1684 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001685 }
1686 }
1687 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1688 AI != AE; ++AI) {
1689 IdxVec AliasProcIndices;
1690 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1691 AliasProcIndices.push_back(
1692 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1693 }
1694 else
1695 AliasProcIndices = ProcIndices;
1696 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1697 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1698
1699 IdxVec ExpandedRWs;
1700 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1701 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1702 SI != SE; ++SI) {
1703 collectRWResources(*SI, IsRead, AliasProcIndices);
1704 }
1705 }
1706}
Andrew Trick1e46d482012-09-15 00:20:02 +00001707
1708// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001709void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1710 ArrayRef<unsigned> Reads,
1711 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001712 for (unsigned Idx : Writes)
1713 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001714
Benjamin Kramere1761952015-10-24 12:46:49 +00001715 for (unsigned Idx : Reads)
1716 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001717}
1718
1719// Find the processor's resource units for this kind of resource.
1720Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001721 const CodeGenProcModel &PM,
1722 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00001723 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1724 return ProcResKind;
1725
Craig Topper24064772014-04-15 07:20:03 +00001726 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001727 assert(!ProcResourceDefs.empty());
1728 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001729
Javed Absar67b042c2017-09-13 10:31:10 +00001730 for (Record *ProcResDef : ProcResourceDefs) {
1731 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1732 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001733 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001734 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001735 "Multiple ProcessorResourceUnits associated with "
1736 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001737 }
Javed Absar67b042c2017-09-13 10:31:10 +00001738 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001739 }
1740 }
Javed Absar67b042c2017-09-13 10:31:10 +00001741 for (Record *ProcResGroup : ProcResGroups) {
1742 if (ProcResGroup == ProcResKind
1743 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001744 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001745 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00001746 "Multiple ProcessorResourceUnits associated with "
1747 + ProcResKind->getName());
1748 }
Javed Absar67b042c2017-09-13 10:31:10 +00001749 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001750 }
1751 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001752 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001753 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001754 "No ProcessorResources associated with "
1755 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001756 }
1757 return ProcUnitDef;
1758}
1759
1760// Iteratively add a resource and its super resources.
1761void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001762 CodeGenProcModel &PM,
1763 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001764 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001765 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00001766
1767 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001768 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001769 return;
1770
1771 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001772 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1773 return;
1774
Andrew Trick1e46d482012-09-15 00:20:02 +00001775 if (!ProcResUnits->getValueInit("Super")->isComplete())
1776 return;
1777
1778 ProcResKind = ProcResUnits->getValueAsDef("Super");
1779 }
1780}
1781
1782// Add resources for a SchedWrite to this processor if they don't exist.
1783void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001784 assert(PIdx && "don't add resources to an invalid Processor model");
1785
Andrew Trick1e46d482012-09-15 00:20:02 +00001786 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001787 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001788 return;
1789 WRDefs.push_back(ProcWriteResDef);
1790
1791 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1792 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1793 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1794 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001795 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00001796 }
1797}
1798
1799// Add resources for a ReadAdvance to this processor if they don't exist.
1800void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1801 unsigned PIdx) {
1802 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001803 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001804 return;
1805 RADefs.push_back(ProcReadAdvanceDef);
1806}
1807
Andrew Trick8fa00f52012-09-17 22:18:43 +00001808unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001809 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001810 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001811 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1812 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001813 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001814 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001815}
1816
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001817bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1818 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1819 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1820 if (TheDef->getName() == PredDef->getName())
1821 return true;
1822 }
1823 }
1824 return false;
1825}
1826
Andrew Trick76686492012-09-15 00:19:57 +00001827#ifndef NDEBUG
1828void CodeGenProcModel::dump() const {
1829 dbgs() << Index << ": " << ModelName << " "
1830 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1831 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1832}
1833
1834void CodeGenSchedRW::dump() const {
1835 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1836 if (IsSequence) {
1837 dbgs() << "(";
1838 dumpIdxVec(Sequence);
1839 dbgs() << ")";
1840 }
1841}
1842
1843void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001844 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001845 << " Writes: ";
1846 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1847 SchedModels->getSchedWrite(Writes[i]).dump();
1848 if (i < N-1) {
1849 dbgs() << '\n';
1850 dbgs().indent(10);
1851 }
1852 }
1853 dbgs() << "\n Reads: ";
1854 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1855 SchedModels->getSchedRead(Reads[i]).dump();
1856 if (i < N-1) {
1857 dbgs() << '\n';
1858 dbgs().indent(10);
1859 }
1860 }
1861 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001862 if (!Transitions.empty()) {
1863 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001864 for (const CodeGenSchedTransition &Transition : Transitions) {
1865 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001866 }
1867 }
Andrew Trick76686492012-09-15 00:19:57 +00001868}
Andrew Trick33401e82012-09-15 00:19:59 +00001869
1870void PredTransitions::dump() const {
1871 dbgs() << "Expanded Variants:\n";
1872 for (std::vector<PredTransition>::const_iterator
1873 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1874 dbgs() << "{";
1875 for (SmallVectorImpl<PredCheck>::const_iterator
1876 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1877 PCI != PCE; ++PCI) {
1878 if (PCI != TI->PredTerm.begin())
1879 dbgs() << ", ";
1880 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1881 << ":" << PCI->Predicate->getName();
1882 }
1883 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001884 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001885 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1886 WSI != WSE; ++WSI) {
1887 dbgs() << "(";
1888 for (SmallVectorImpl<unsigned>::const_iterator
1889 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1890 if (WI != WSI->begin())
1891 dbgs() << ", ";
1892 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1893 }
1894 dbgs() << "),";
1895 }
1896 dbgs() << "}\n";
1897 }
1898}
Andrew Trick76686492012-09-15 00:19:57 +00001899#endif // NDEBUG