blob: 1b516c17530c19a65b58054843918c25816b26ab [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);
98 std::string pat = Original.substr(FirstMeta);
99 if (!pat.empty()) {
100 // For the rest use a python-style prefix match.
101 if (pat[0] != '^') {
102 pat.insert(0, "^(");
103 pat.insert(pat.end(), ')');
104 }
105 Regexpr = Regex(pat);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000106 }
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000107
Benjamin Kramer4890a712018-01-24 22:35:11 +0000108 unsigned NumGeneric = Target.getNumFixedInstructions();
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000109 ArrayRef<const CodeGenInstruction *> Generics =
110 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1);
111
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000112 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000113 for (auto *Inst : Generics) {
114 StringRef InstName = Inst->TheDef->getName();
115 if (InstName.startswith(Prefix) &&
116 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))))
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000117 Elts.insert(Inst->TheDef);
118 }
119
120 ArrayRef<const CodeGenInstruction *> Instructions =
Benjamin Kramer4890a712018-01-24 22:35:11 +0000121 Target.getInstructionsByEnumValue().slice(NumGeneric + 1);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000122
123 // Target instructions are sorted. Find the range that starts with our
124 // prefix.
125 struct Comp {
126 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
127 return LHS->TheDef->getName() < RHS;
128 }
129 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
130 return LHS < RHS->TheDef->getName() &&
131 !RHS->TheDef->getName().startswith(LHS);
132 }
133 };
134 auto Range = std::equal_range(Instructions.begin(), Instructions.end(),
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000135 Prefix, Comp());
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000136
137 // For this range we know that it starts with the prefix. Check if there's
138 // a regex that needs to be checked.
139 for (auto *Inst : make_range(Range)) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000140 StringRef InstName = Inst->TheDef->getName();
141 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))
Craig Topper8a417c12014-12-09 08:05:51 +0000142 Elts.insert(Inst->TheDef);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000143 }
144 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000145 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000146};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000147
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000148} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000149
Andrew Trick76686492012-09-15 00:19:57 +0000150/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000151CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
152 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000153 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000154
Andrew Trick9e1deb62012-10-03 23:06:32 +0000155 Sets.addFieldExpander("InstRW", "Instrs");
156
157 // Allow Set evaluation to recognize the dags used in InstRW records:
158 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000159 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
160 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000161
Andrew Trick76686492012-09-15 00:19:57 +0000162 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
163 // that are explicitly referenced in tablegen records. Resources associated
164 // with each processor will be derived later. Populate ProcModelMap with the
165 // CodeGenProcModel instances.
166 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000167
Andrew Trick76686492012-09-15 00:19:57 +0000168 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
169 // defined, and populate SchedReads and SchedWrites vectors. Implicit
170 // SchedReadWrites that represent sequences derived from expanded variant will
171 // be inferred later.
172 collectSchedRW();
173
174 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
175 // required by an instruction definition, and populate SchedClassIdxMap. Set
176 // NumItineraryClasses to the number of explicit itinerary classes referenced
177 // by instructions. Set NumInstrSchedClasses to the number of itinerary
178 // classes plus any classes implied by instructions that derive from class
179 // Sched and provide SchedRW list. This does not infer any new classes from
180 // SchedVariant.
181 collectSchedClasses();
182
183 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000184 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000185 // all itinerary classes to be discovered.
186 collectProcItins();
187
188 // Find ItinRW records for each processor and itinerary class.
189 // (For per-operand resources mapped to itinerary classes).
190 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000191
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000192 // Find UnsupportedFeatures records for each processor.
193 // (For per-operand resources mapped to itinerary classes).
194 collectProcUnsupportedFeatures();
195
Andrew Trick33401e82012-09-15 00:19:59 +0000196 // Infer new SchedClasses from SchedVariant.
197 inferSchedClasses();
198
Andrew Trick1e46d482012-09-15 00:20:02 +0000199 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
200 // ProcResourceDefs.
Joel Jones80372332017-06-28 00:06:40 +0000201 DEBUG(dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000202 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000203
204 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000205}
206
Andrew Trick76686492012-09-15 00:19:57 +0000207/// Gather all processor models.
208void CodeGenSchedModels::collectProcModels() {
209 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
210 std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000211
Andrew Trick76686492012-09-15 00:19:57 +0000212 // Reserve space because we can. Reallocation would be ok.
213 ProcModels.reserve(ProcRecords.size()+1);
214
215 // Use idx=0 for NoModel/NoItineraries.
216 Record *NoModelDef = Records.getDef("NoSchedModel");
217 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000218 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000219 ProcModelMap[NoModelDef] = 0;
220
221 // For each processor, find a unique machine model.
Joel Jones80372332017-06-28 00:06:40 +0000222 DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000223 for (Record *ProcRecord : ProcRecords)
224 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000225}
226
227/// Get a unique processor model based on the defined MachineModel and
228/// ProcessorItineraries.
229void CodeGenSchedModels::addProcModel(Record *ProcDef) {
230 Record *ModelKey = getModelOrItinDef(ProcDef);
231 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
232 return;
233
234 std::string Name = ModelKey->getName();
235 if (ModelKey->isSubClassOf("SchedMachineModel")) {
236 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000237 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000238 }
239 else {
240 // An itinerary is defined without a machine model. Infer a new model.
241 if (!ModelKey->getValueAsListOfDefs("IID").empty())
242 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000243 ProcModels.emplace_back(ProcModels.size(), Name,
244 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000245 }
246 DEBUG(ProcModels.back().dump());
247}
248
249// Recursively find all reachable SchedReadWrite records.
250static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
251 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000252 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000253 return;
254 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000255 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000256 if (RWDef->isSubClassOf("WriteSequence")) {
257 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000258 for (Record *WSRec : Seq)
259 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000260 }
261 else if (RWDef->isSubClassOf("SchedVariant")) {
262 // Visit each variant (guarded by a different predicate).
263 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000264 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000265 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000266 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
267 for (Record *SelDef : Selected)
268 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000269 }
270 }
271}
272
273// Collect and sort all SchedReadWrites reachable via tablegen records.
274// More may be inferred later when inferring new SchedClasses from variants.
275void CodeGenSchedModels::collectSchedRW() {
276 // Reserve idx=0 for invalid writes/reads.
277 SchedWrites.resize(1);
278 SchedReads.resize(1);
279
280 SmallPtrSet<Record*, 16> RWSet;
281
282 // Find all SchedReadWrites referenced by instruction defs.
283 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000284 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000285 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000286 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000287 continue;
288 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000289 for (Record *RW : RWs) {
290 if (RW->isSubClassOf("SchedWrite"))
291 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000292 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000293 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
294 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000295 }
296 }
297 }
298 // Find all ReadWrites referenced by InstRW.
299 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000300 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000301 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000302 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
303 for (Record *RWDef : RWDefs) {
304 if (RWDef->isSubClassOf("SchedWrite"))
305 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000306 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000307 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
308 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000309 }
310 }
311 }
312 // Find all ReadWrites referenced by ItinRW.
313 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000314 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000315 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000316 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
317 for (Record *RWDef : RWDefs) {
318 if (RWDef->isSubClassOf("SchedWrite"))
319 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000320 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000321 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
322 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000323 }
324 }
325 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000326 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
327 // for the loop below that initializes Alias vectors.
328 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
329 std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000330 for (Record *ADef : AliasDefs) {
331 Record *MatchDef = ADef->getValueAsDef("MatchRW");
332 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000333 if (MatchDef->isSubClassOf("SchedWrite")) {
334 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000335 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000336 scanSchedRW(AliasDef, SWDefs, RWSet);
337 }
338 else {
339 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
340 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000341 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000342 scanSchedRW(AliasDef, SRDefs, RWSet);
343 }
344 }
Andrew Trick76686492012-09-15 00:19:57 +0000345 // Sort and add the SchedReadWrites directly referenced by instructions or
346 // itinerary resources. Index reads and writes in separate domains.
347 std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000348 for (Record *SWDef : SWDefs) {
349 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
350 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000351 }
352 std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000353 for (Record *SRDef : SRDefs) {
354 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
355 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000356 }
357 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000358 for (CodeGenSchedRW &CGRW : SchedWrites) {
359 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000360 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000361 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000362 /*IsRead=*/false);
363 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000364 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000365 for (Record *ADef : AliasDefs) {
366 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000367 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000368 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000369 CodeGenSchedRW &RW = getSchedRW(MatchDef);
370 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000371 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
372 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000373 }
Andrew Trick76686492012-09-15 00:19:57 +0000374 DEBUG(
Joel Jones80372332017-06-28 00:06:40 +0000375 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
Andrew Trick76686492012-09-15 00:19:57 +0000376 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
377 dbgs() << WIdx << ": ";
378 SchedWrites[WIdx].dump();
379 dbgs() << '\n';
380 }
381 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
382 dbgs() << RIdx << ": ";
383 SchedReads[RIdx].dump();
384 dbgs() << '\n';
385 }
386 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
Javed Absar67b042c2017-09-13 10:31:10 +0000387 for (Record *RWDef : RWDefs) {
388 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
389 const std::string &Name = RWDef->getName();
Andrew Trick76686492012-09-15 00:19:57 +0000390 if (Name != "NoWrite" && Name != "ReadDefault")
Javed Absar67b042c2017-09-13 10:31:10 +0000391 dbgs() << "Unused SchedReadWrite " << RWDef->getName() << '\n';
Andrew Trick76686492012-09-15 00:19:57 +0000392 }
393 });
394}
395
396/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000397std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000398 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000399 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000400 if (I != Seq.begin())
401 Name += '_';
402 Name += getSchedRW(*I, IsRead).Name;
403 }
404 Name += ')';
405 return Name;
406}
407
Craig Toppere2611842018-03-21 05:13:04 +0000408unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead) const {
Andrew Trick76686492012-09-15 00:19:57 +0000409 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Craig Toppere2611842018-03-21 05:13:04 +0000410 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin(),
Andrew Trick76686492012-09-15 00:19:57 +0000411 E = RWVec.end(); I != E; ++I) {
412 if (I->TheDef == Def)
413 return I - RWVec.begin();
414 }
415 return 0;
416}
417
Andrew Trickcfe222c2012-09-19 04:43:19 +0000418bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000419 for (const CodeGenSchedRW &Read : SchedReads) {
420 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000421 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
422 continue;
423
424 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000425 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000426 return true;
427 }
428 }
429 return false;
430}
431
Craig Topper6f2cc9b2018-03-21 05:13:01 +0000432static void splitSchedReadWrites(const RecVec &RWDefs,
433 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000434 for (Record *RWDef : RWDefs) {
435 if (RWDef->isSubClassOf("SchedWrite"))
436 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000437 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000438 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
439 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000440 }
441 }
442}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000443
Andrew Trick76686492012-09-15 00:19:57 +0000444// Split the SchedReadWrites defs and call findRWs for each list.
445void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
446 IdxVec &Writes, IdxVec &Reads) const {
447 RecVec WriteDefs;
448 RecVec ReadDefs;
449 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
450 findRWs(WriteDefs, Writes, false);
451 findRWs(ReadDefs, Reads, true);
452}
453
454// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
455void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
456 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000457 for (Record *RWDef : RWDefs) {
458 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000459 assert(Idx && "failed to collect SchedReadWrite");
460 RWs.push_back(Idx);
461 }
462}
463
Andrew Trick33401e82012-09-15 00:19:59 +0000464void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
465 bool IsRead) const {
466 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
467 if (!SchedRW.IsSequence) {
468 RWSeq.push_back(RWIdx);
469 return;
470 }
471 int Repeat =
472 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
473 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000474 for (unsigned I : SchedRW.Sequence) {
475 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000476 }
477 }
478}
479
Andrew Trickda984b12012-10-03 23:06:28 +0000480// Expand a SchedWrite as a sequence following any aliases that coincide with
481// the given processor model.
482void CodeGenSchedModels::expandRWSeqForProc(
483 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
484 const CodeGenProcModel &ProcModel) const {
485
486 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000487 Record *AliasDef = nullptr;
Andrew Trickda984b12012-10-03 23:06:28 +0000488 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
489 AI != AE; ++AI) {
490 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
491 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
492 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
493 if (&getProcModel(ModelDef) != &ProcModel)
494 continue;
495 }
496 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000497 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
498 "defined for processor " + ProcModel.ModelName +
499 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000500 AliasDef = AliasRW.TheDef;
501 }
502 if (AliasDef) {
503 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
504 RWSeq, IsRead,ProcModel);
505 return;
506 }
507 if (!SchedWrite.IsSequence) {
508 RWSeq.push_back(RWIdx);
509 return;
510 }
511 int Repeat =
512 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
513 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000514 for (unsigned I : SchedWrite.Sequence) {
515 expandRWSeqForProc(I, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000516 }
517 }
518}
519
Andrew Trick33401e82012-09-15 00:19:59 +0000520// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000521unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000522 bool IsRead) {
523 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
524
525 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
526 I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000527 if (makeArrayRef(I->Sequence) == Seq)
Andrew Trick33401e82012-09-15 00:19:59 +0000528 return I - RWVec.begin();
529 }
530 // Index zero reserved for invalid RW.
531 return 0;
532}
533
534/// Add this ReadWrite if it doesn't already exist.
535unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
536 bool IsRead) {
537 assert(!Seq.empty() && "cannot insert empty sequence");
538 if (Seq.size() == 1)
539 return Seq.back();
540
541 unsigned Idx = findRWForSequence(Seq, IsRead);
542 if (Idx)
543 return Idx;
544
Andrew Trickda984b12012-10-03 23:06:28 +0000545 unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
546 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
547 if (IsRead)
Andrew Trick33401e82012-09-15 00:19:59 +0000548 SchedReads.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000549 else
550 SchedWrites.push_back(SchedRW);
551 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000552}
553
Andrew Trick76686492012-09-15 00:19:57 +0000554/// Visit all the instruction definitions for this target to gather and
555/// enumerate the itinerary classes. These are the explicitly specified
556/// SchedClasses. More SchedClasses may be inferred.
557void CodeGenSchedModels::collectSchedClasses() {
558
559 // NoItinerary is always the first class at Idx=0
Craig Topper281a19c2018-03-22 06:15:08 +0000560 assert(SchedClasses.empty() && "Expected empty sched class");
561 SchedClasses.emplace_back(0, "NoInstrModel",
562 Records.getDef("NoItinerary"));
Andrew Trick76686492012-09-15 00:19:57 +0000563 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000564
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000565 // Create a SchedClass for each unique combination of itinerary class and
566 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000567 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000568 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000569 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000570 if (!Inst->TheDef->isValueUnset("SchedRW"))
571 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000572
Andrew Trick76686492012-09-15 00:19:57 +0000573 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topper281a19c2018-03-22 06:15:08 +0000574 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topper8a417c12014-12-09 08:05:51 +0000575 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000576 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000577 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000578 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
579 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000580 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000581 for (Record *RWDef : InstRWDefs)
582 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000583
Andrew Trick76686492012-09-15 00:19:57 +0000584 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000585
Andrew Trick76686492012-09-15 00:19:57 +0000586 bool EnableDump = false;
587 DEBUG(EnableDump = true);
588 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000589 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000590
Joel Jones80372332017-06-28 00:06:40 +0000591 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000592 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000593 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim949437e2018-03-21 18:09:34 +0000594 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000595 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000596 if (!Inst->hasNoSchedulingInfo)
597 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000598 continue;
599 }
600 CodeGenSchedClass &SC = getSchedClass(SCIdx);
601 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000602 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000603 "must not be subtarget specific.");
604
605 IdxVec ProcIndices;
606 if (SC.ItinClassDef->getName() != "NoItinerary") {
607 ProcIndices.push_back(0);
608 dbgs() << "Itinerary for " << InstName << ": "
609 << SC.ItinClassDef->getName() << '\n';
610 }
611 if (!SC.Writes.empty()) {
612 ProcIndices.push_back(0);
613 dbgs() << "SchedRW machine model for " << InstName;
614 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
615 dbgs() << " " << SchedWrites[*WI].Name;
616 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
617 dbgs() << " " << SchedReads[*RI].Name;
618 dbgs() << '\n';
619 }
620 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000621 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000622 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000623 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000624 ProcIndices.push_back(ProcModel.Index);
625 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000626 IdxVec Writes;
627 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000628 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000629 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000630 for (unsigned WIdx : Writes)
631 dbgs() << " " << SchedWrites[WIdx].Name;
632 for (unsigned RIdx : Reads)
633 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000634 dbgs() << '\n';
635 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000636 // If ProcIndices contains zero, the class applies to all processors.
637 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
Javed Absar21c75912017-10-09 16:21:25 +0000638 for (const CodeGenProcModel &PM : ProcModels) {
Javed Absarfc500042017-10-05 13:27:43 +0000639 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
Andrew Trickf9df92c92016-10-18 04:17:44 +0000640 dbgs() << "No machine model for " << Inst->TheDef->getName()
Javed Absarfc500042017-10-05 13:27:43 +0000641 << " on processor " << PM.ModelName << '\n';
Andrew Trickf9df92c92016-10-18 04:17:44 +0000642 }
Andrew Trick87255e32012-07-07 04:00:00 +0000643 }
644 }
Andrew Trick76686492012-09-15 00:19:57 +0000645}
646
Andrew Trick76686492012-09-15 00:19:57 +0000647/// Find an SchedClass that has been inferred from a per-operand list of
648/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000649unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000650 ArrayRef<unsigned> Writes,
651 ArrayRef<unsigned> Reads) const {
Simon Pilgrim4cca3b12018-03-21 17:57:21 +0000652 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I)
653 if (I->isKeyEqual(ItinClassDef, Writes, Reads))
Andrew Trick76686492012-09-15 00:19:57 +0000654 return I - schedClassBegin();
Andrew Trick76686492012-09-15 00:19:57 +0000655 return 0;
656}
Andrew Trick87255e32012-07-07 04:00:00 +0000657
Andrew Trick76686492012-09-15 00:19:57 +0000658// Get the SchedClass index for an instruction.
659unsigned CodeGenSchedModels::getSchedClassIdx(
660 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000661
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000662 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000663}
664
Benjamin Kramere1761952015-10-24 12:46:49 +0000665std::string
666CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
667 ArrayRef<unsigned> OperWrites,
668 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000669
670 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000671 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
672 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000673 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000674 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000675 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000676 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000677 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000678 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000679 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000680 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000681 }
682 return Name;
683}
684
685std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
686
687 std::string Name;
688 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
689 if (I != InstDefs.begin())
690 Name += '_';
691 Name += (*I)->getName();
692 }
693 return Name;
694}
695
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000696/// Add an inferred sched class from an itinerary class and per-operand list of
697/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
698/// processors that may utilize this class.
699unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000700 ArrayRef<unsigned> OperWrites,
701 ArrayRef<unsigned> OperReads,
702 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000703 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
704
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000705 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
706 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000707 IdxVec PI;
708 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
709 SchedClasses[Idx].ProcIndices.end(),
710 ProcIndices.begin(), ProcIndices.end(),
711 std::back_inserter(PI));
712 SchedClasses[Idx].ProcIndices.swap(PI);
713 return Idx;
714 }
715 Idx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000716 SchedClasses.emplace_back(Idx,
717 createSchedClassName(ItinClassDef, OperWrites,
718 OperReads),
719 ItinClassDef);
Andrew Trick76686492012-09-15 00:19:57 +0000720 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick76686492012-09-15 00:19:57 +0000721 SC.Writes = OperWrites;
722 SC.Reads = OperReads;
723 SC.ProcIndices = ProcIndices;
724
725 return Idx;
726}
727
728// Create classes for each set of opcodes that are in the same InstReadWrite
729// definition across all processors.
730void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
731 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
732 // intersects with an existing class via a previous InstRWDef. Instrs that do
733 // not intersect with an existing class refer back to their former class as
734 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +0000735 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000736 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000737 const RecVec *InstDefs = Sets.expand(InstRWDef);
738 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000739 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000740
Craig Topper93dd77d2018-03-18 08:38:03 +0000741 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +0000742 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000743 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +0000744 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000745 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +0000746 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +0000747 }
748 // For each set of Instrs, create a new class if necessary, and map or remap
749 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +0000750 for (auto &Entry : ClassInstrs) {
751 unsigned OldSCIdx = Entry.first;
752 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +0000753 // If the all instrs in the current class are accounted for, then leave
754 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000755 if (OldSCIdx) {
756 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
757 if (!RWDefs.empty()) {
758 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper06d78372018-03-21 19:30:30 +0000759 unsigned OrigNumInstrs =
760 count_if(*OrigInstDefs, [&](Record *OIDef) {
761 return InstrClassMap[OIDef] == OldSCIdx;
762 });
Andrew Trick78a08512013-06-05 06:55:20 +0000763 if (OrigNumInstrs == InstDefs.size()) {
764 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
765 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000766 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
767 // Make sure we didn't already have a InstRW containing this
768 // instruction on this model.
769 for (Record *RWD : RWDefs) {
770 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
771 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
772 for (Record *Inst : InstDefs) {
773 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
774 Inst->getName() + " also matches " +
775 RWD->getValue("Instrs")->getValue()->getAsString());
776 }
777 }
778 }
Andrew Trick78a08512013-06-05 06:55:20 +0000779 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
780 << SchedClasses[OldSCIdx].Name << " on "
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000781 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +0000782 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
783 continue;
784 }
785 }
Andrew Trick76686492012-09-15 00:19:57 +0000786 }
787 unsigned SCIdx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000788 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick76686492012-09-15 00:19:57 +0000789 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick78a08512013-06-05 06:55:20 +0000790 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
791 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
792
Andrew Trick76686492012-09-15 00:19:57 +0000793 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
794 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
795 SC.Writes = SchedClasses[OldSCIdx].Writes;
796 SC.Reads = SchedClasses[OldSCIdx].Reads;
797 SC.ProcIndices.push_back(0);
Craig Topper989d94d2018-03-21 19:52:13 +0000798 // If we had an old class, copy it's InstRWs to this new class.
799 if (OldSCIdx) {
800 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
801 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
802 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
803 for (Record *InstDef : InstDefs) {
Craig Topper9fbbe5d2018-03-21 19:30:31 +0000804 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
805 InstDef->getName() + " also matches " +
806 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick9e1deb62012-10-03 23:06:32 +0000807 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000808 }
Craig Topper989d94d2018-03-21 19:52:13 +0000809 assert(OldRWDef != InstRWDef &&
810 "SchedClass has duplicate InstRW def");
811 SC.InstRWs.push_back(OldRWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000812 }
Andrew Trick76686492012-09-15 00:19:57 +0000813 }
Craig Topper989d94d2018-03-21 19:52:13 +0000814 // Map each Instr to this new class.
815 for (Record *InstDef : InstDefs)
816 InstrClassMap[InstDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000817 SC.InstRWs.push_back(InstRWDef);
818 }
Andrew Trick87255e32012-07-07 04:00:00 +0000819}
820
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000821// True if collectProcItins found anything.
822bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000823 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
824 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000825 return true;
826 }
827 return false;
828}
829
Andrew Trick87255e32012-07-07 04:00:00 +0000830// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000831void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000832 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000833 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000834 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000835 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000836
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000837 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
838 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
839
840 // Populate ItinDefList with Itinerary records.
841 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000842
843 // Insert each itinerary data record in the correct position within
844 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +0000845 for (Record *ItinData : ItinRecords) {
Andrew Trick76686492012-09-15 00:19:57 +0000846 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000847 bool FoundClass = false;
848 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
849 SCI != SCE; ++SCI) {
850 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000851 if (SCI->ItinClassDef == ItinDef) {
852 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000853 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000854 }
Andrew Trick76686492012-09-15 00:19:57 +0000855 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000856 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000857 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
858 << " missing class for itinerary " << ItinDef->getName() << '\n');
859 }
Andrew Trick87255e32012-07-07 04:00:00 +0000860 }
Andrew Trick76686492012-09-15 00:19:57 +0000861 // Check for missing itinerary entries.
862 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
863 DEBUG(
864 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
865 if (!ProcModel.ItinDefList[i])
866 dbgs() << ProcModel.ItinsDef->getName()
867 << " missing itinerary for class "
868 << SchedClasses[i].Name << '\n';
869 });
Andrew Trick87255e32012-07-07 04:00:00 +0000870 }
Andrew Trick87255e32012-07-07 04:00:00 +0000871}
Andrew Trick76686492012-09-15 00:19:57 +0000872
873// Gather the read/write types for each itinerary class.
874void CodeGenSchedModels::collectProcItinRW() {
875 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
876 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +0000877 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000878 if (!RWDef->getValueInit("SchedModel")->isComplete())
879 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
880 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +0000881 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
882 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000883 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000884 + ModelDef->getName());
885 }
Javed Absarf45d0b92017-10-08 17:23:30 +0000886 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000887 }
888}
889
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000890// Gather the unsupported features for processor models.
891void CodeGenSchedModels::collectProcUnsupportedFeatures() {
892 for (CodeGenProcModel &ProcModel : ProcModels) {
893 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
894 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
895 }
896 }
897}
898
Andrew Trick33401e82012-09-15 00:19:59 +0000899/// Infer new classes from existing classes. In the process, this may create new
900/// SchedWrites from sequences of existing SchedWrites.
901void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000902 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000903 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
904
Andrew Trick33401e82012-09-15 00:19:59 +0000905 // Visit all existing classes and newly created classes.
906 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000907 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
908
Andrew Trick33401e82012-09-15 00:19:59 +0000909 if (SchedClasses[Idx].ItinClassDef)
910 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000911 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000912 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000913 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000914 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
915 Idx, SchedClasses[Idx].ProcIndices);
916 }
917 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
918 "too many SchedVariants");
919 }
920}
921
922/// Infer classes from per-processor itinerary resources.
923void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
924 unsigned FromClassIdx) {
925 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
926 const CodeGenProcModel &PM = ProcModels[PIdx];
927 // For all ItinRW entries.
928 bool HasMatch = false;
929 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
930 II != IE; ++II) {
931 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
932 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
933 continue;
934 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000935 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000936 + ItinClassDef->getName()
937 + " in ItinResources for " + PM.ModelName);
938 HasMatch = true;
939 IdxVec Writes, Reads;
940 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
941 IdxVec ProcIndices(1, PIdx);
942 inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
943 }
944 }
945}
946
947/// Infer classes from per-processor InstReadWrite definitions.
948void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000949 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000950 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000951 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
952 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000953 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000954 for (; II != IE; ++II) {
955 if (InstrClassMap[*II] == SCIdx)
956 break;
957 }
958 // If this class no longer has any instructions mapped to it, it has become
959 // irrelevant.
960 if (II == IE)
961 continue;
962 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000963 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
964 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Andrew Trick33401e82012-09-15 00:19:59 +0000965 IdxVec ProcIndices(1, PIdx);
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000966 inferFromRW(Writes, Reads, SCIdx, ProcIndices); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +0000967 }
968}
969
970namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000971
Andrew Trick9257b8f2012-09-22 02:24:21 +0000972// Helper for substituteVariantOperand.
973struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +0000974 Record *VarOrSeqDef; // Variant or sequence.
975 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000976 unsigned ProcIdx; // Processor model index or zero for any.
977 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
978
979 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +0000980 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +0000981};
982
Andrew Trick33401e82012-09-15 00:19:59 +0000983// Associate a predicate with the SchedReadWrite that it guards.
984// RWIdx is the index of the read/write variant.
985struct PredCheck {
986 bool IsRead;
987 unsigned RWIdx;
988 Record *Predicate;
989
990 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
991};
992
993// A Predicate transition is a list of RW sequences guarded by a PredTerm.
994struct PredTransition {
995 // A predicate term is a conjunction of PredChecks.
996 SmallVector<PredCheck, 4> PredTerm;
997 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
998 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000999 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001000};
1001
1002// Encapsulate a set of partially constructed transitions.
1003// The results are built by repeated calls to substituteVariants.
1004class PredTransitions {
1005 CodeGenSchedModels &SchedModels;
1006
1007public:
1008 std::vector<PredTransition> TransVec;
1009
1010 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1011
1012 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1013 bool IsRead, unsigned StartIdx);
1014
1015 void substituteVariants(const PredTransition &Trans);
1016
1017#ifndef NDEBUG
1018 void dump() const;
1019#endif
1020
1021private:
1022 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001023 void getIntersectingVariants(
1024 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1025 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001026 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001027};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001028
1029} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001030
1031// Return true if this predicate is mutually exclusive with a PredTerm. This
1032// degenerates into checking if the predicate is mutually exclusive with any
1033// predicate in the Term's conjunction.
1034//
1035// All predicates associated with a given SchedRW are considered mutually
1036// exclusive. This should work even if the conditions expressed by the
1037// predicates are not exclusive because the predicates for a given SchedWrite
1038// are always checked in the order they are defined in the .td file. Later
1039// conditions implicitly negate any prior condition.
1040bool PredTransitions::mutuallyExclusive(Record *PredDef,
1041 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001042 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001043 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001044 return false;
1045
Javed Absarfc500042017-10-05 13:27:43 +00001046 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001047 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1048 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1049 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1050 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1051 return true;
1052 }
1053 }
1054 return false;
1055}
1056
Andrew Trickda984b12012-10-03 23:06:28 +00001057static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1058 CodeGenSchedModels &SchedModels) {
1059 if (RW.HasVariants)
1060 return true;
1061
Javed Absar21c75912017-10-09 16:21:25 +00001062 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001063 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001064 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001065 if (AliasRW.HasVariants)
1066 return true;
1067 if (AliasRW.IsSequence) {
1068 IdxVec ExpandedRWs;
1069 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1070 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1071 SI != SE; ++SI) {
1072 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1073 SchedModels)) {
1074 return true;
1075 }
1076 }
1077 }
1078 }
1079 return false;
1080}
1081
1082static bool hasVariant(ArrayRef<PredTransition> Transitions,
1083 CodeGenSchedModels &SchedModels) {
1084 for (ArrayRef<PredTransition>::iterator
1085 PTI = Transitions.begin(), PTE = Transitions.end();
1086 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001087 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001088 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1089 WSI != WSE; ++WSI) {
1090 for (SmallVectorImpl<unsigned>::const_iterator
1091 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1092 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1093 return true;
1094 }
1095 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001096 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001097 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1098 RSI != RSE; ++RSI) {
1099 for (SmallVectorImpl<unsigned>::const_iterator
1100 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1101 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1102 return true;
1103 }
1104 }
1105 }
1106 return false;
1107}
1108
1109// Populate IntersectingVariants with any variants or aliased sequences of the
1110// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001111// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001112void PredTransitions::getIntersectingVariants(
1113 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1114 std::vector<TransVariant> &IntersectingVariants) {
1115
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001116 bool GenericRW = false;
1117
Andrew Trickda984b12012-10-03 23:06:28 +00001118 std::vector<TransVariant> Variants;
1119 if (SchedRW.HasVariants) {
1120 unsigned VarProcIdx = 0;
1121 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1122 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1123 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1124 }
1125 // Push each variant. Assign TransVecIdx later.
1126 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001127 for (Record *VarDef : VarDefs)
1128 Variants.push_back(TransVariant(VarDef, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001129 if (VarProcIdx == 0)
1130 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001131 }
1132 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1133 AI != AE; ++AI) {
1134 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1135 // to is defined within a processor model, constrain all variants to
1136 // that processor.
1137 unsigned AliasProcIdx = 0;
1138 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1139 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1140 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1141 }
1142 const CodeGenSchedRW &AliasRW =
1143 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1144
1145 if (AliasRW.HasVariants) {
1146 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001147 for (Record *VD : VarDefs)
1148 Variants.push_back(TransVariant(VD, AliasRW.Index, AliasProcIdx, 0));
Andrew Trickda984b12012-10-03 23:06:28 +00001149 }
1150 if (AliasRW.IsSequence) {
1151 Variants.push_back(
1152 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1153 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001154 if (AliasProcIdx == 0)
1155 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001156 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001157 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001158 // Don't expand variants if the processor models don't intersect.
1159 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001160 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001161 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001162 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1163 Variant.ProcIdx);
1164 if (!Cnt)
1165 continue;
1166 if (Cnt > 1) {
1167 const CodeGenProcModel &PM =
1168 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001169 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1170 "Multiple variants defined for processor " +
1171 PM.ModelName +
1172 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001173 }
1174 }
1175 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1176 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1177 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1178 continue;
1179 }
1180 if (IntersectingVariants.empty()) {
1181 // The first variant builds on the existing transition.
1182 Variant.TransVecIdx = TransIdx;
1183 IntersectingVariants.push_back(Variant);
1184 }
1185 else {
1186 // Push another copy of the current transition for more variants.
1187 Variant.TransVecIdx = TransVec.size();
1188 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001189 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001190 }
1191 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001192 if (GenericRW && IntersectingVariants.empty()) {
1193 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1194 "a matching predicate on any processor");
1195 }
Andrew Trickda984b12012-10-03 23:06:28 +00001196}
1197
Andrew Trick9257b8f2012-09-22 02:24:21 +00001198// Push the Reads/Writes selected by this variant onto the PredTransition
1199// specified by VInfo.
1200void PredTransitions::
1201pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001202 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1203
Andrew Trick9257b8f2012-09-22 02:24:21 +00001204 // If this operand transition is reached through a processor-specific alias,
1205 // then the whole transition is specific to this processor.
1206 if (VInfo.ProcIdx != 0)
1207 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1208
Andrew Trick33401e82012-09-15 00:19:59 +00001209 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001210 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1211 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1212 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1213 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1214 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1215 }
1216 else {
1217 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1218 "variant must be a SchedVariant or aliased WriteSequence");
1219 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1220 }
Andrew Trick33401e82012-09-15 00:19:59 +00001221
Andrew Trick9257b8f2012-09-22 02:24:21 +00001222 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001223
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001224 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001225 ? Trans.ReadSequences : Trans.WriteSequences;
1226 if (SchedRW.IsVariadic) {
1227 unsigned OperIdx = RWSequences.size()-1;
1228 // Make N-1 copies of this transition's last sequence.
1229 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001230 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001231 RWSequences.reserve(RWSequences.size() + 1);
1232 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001233 }
1234 // Push each of the N elements of the SelectedRWs onto a copy of the last
1235 // sequence (split the current operand into N operands).
1236 // Note that write sequences should be expanded within this loop--the entire
1237 // sequence belongs to a single operand.
1238 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1239 RWI != RWE; ++RWI, ++OperIdx) {
1240 IdxVec ExpandedRWs;
1241 if (IsRead)
1242 ExpandedRWs.push_back(*RWI);
1243 else
1244 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1245 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1246 ExpandedRWs.begin(), ExpandedRWs.end());
1247 }
1248 assert(OperIdx == RWSequences.size() && "missed a sequence");
1249 }
1250 else {
1251 // Push this transition's expanded sequence onto this transition's last
1252 // sequence (add to the current operand's sequence).
1253 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1254 IdxVec ExpandedRWs;
1255 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1256 RWI != RWE; ++RWI) {
1257 if (IsRead)
1258 ExpandedRWs.push_back(*RWI);
1259 else
1260 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1261 }
1262 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1263 }
1264}
1265
1266// RWSeq is a sequence of all Reads or all Writes for the next read or write
1267// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001268// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001269// of TransVec.
1270void PredTransitions::substituteVariantOperand(
1271 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1272
1273 // Visit each original RW within the current sequence.
1274 for (SmallVectorImpl<unsigned>::const_iterator
1275 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1276 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1277 // Push this RW on all partial PredTransitions or distribute variants.
1278 // New PredTransitions may be pushed within this loop which should not be
1279 // revisited (TransEnd must be loop invariant).
1280 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1281 TransIdx != TransEnd; ++TransIdx) {
1282 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001283 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001284 if (IsRead)
1285 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1286 else
1287 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1288 continue;
1289 }
1290 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001291 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001292 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001293 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001294 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001295 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001296 IVI = IntersectingVariants.begin(),
1297 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001298 IVI != IVE; ++IVI) {
1299 pushVariant(*IVI, IsRead);
1300 }
Andrew Trick33401e82012-09-15 00:19:59 +00001301 }
1302 }
1303}
1304
1305// For each variant of a Read/Write in Trans, substitute the sequence of
1306// Read/Writes guarded by the variant. This is exponential in the number of
1307// variant Read/Writes, but in practice detection of mutually exclusive
1308// predicates should result in linear growth in the total number variants.
1309//
1310// This is one step in a breadth-first search of nested variants.
1311void PredTransitions::substituteVariants(const PredTransition &Trans) {
1312 // Build up a set of partial results starting at the back of
1313 // PredTransitions. Remember the first new transition.
1314 unsigned StartIdx = TransVec.size();
Craig Topper195aaaf2018-03-22 06:15:10 +00001315 TransVec.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001316 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001317 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001318
1319 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001320 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001321 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1322 WSI != WSE; ++WSI) {
1323 // Push a new (empty) write sequence onto all partial Transitions.
1324 for (std::vector<PredTransition>::iterator I =
1325 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001326 I->WriteSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001327 }
1328 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1329 }
1330 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001331 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001332 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1333 RSI != RSE; ++RSI) {
1334 // Push a new (empty) read sequence onto all partial Transitions.
1335 for (std::vector<PredTransition>::iterator I =
1336 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001337 I->ReadSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001338 }
1339 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1340 }
1341}
1342
Andrew Trick33401e82012-09-15 00:19:59 +00001343// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001344static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001345 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001346 CodeGenSchedModels &SchedModels) {
1347 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1348 // requires creating a new SchedClass.
1349 for (ArrayRef<PredTransition>::iterator
1350 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1351 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001352 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1353 [&SchedModels](ArrayRef<unsigned> WS) {
1354 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1355 });
Andrew Trick33401e82012-09-15 00:19:59 +00001356 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001357 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1358 [&SchedModels](ArrayRef<unsigned> RS) {
1359 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1360 });
Andrew Trick9257b8f2012-09-22 02:24:21 +00001361 IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001362 CodeGenSchedTransition SCTrans;
1363 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001364 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001365 OperReadsVariant, ProcIndices);
Andrew Trick33401e82012-09-15 00:19:59 +00001366 SCTrans.ProcIndices = ProcIndices;
1367 // The final PredTerm is unique set of predicates guarding the transition.
1368 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001369 transform(I->PredTerm, std::back_inserter(Preds),
1370 [](const PredCheck &P) {
1371 return P.Predicate;
1372 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001373 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001374 SCTrans.PredTerm = Preds;
1375 SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
1376 }
1377}
1378
Andrew Trick9257b8f2012-09-22 02:24:21 +00001379// Create new SchedClasses for the given ReadWrite list. If any of the
1380// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1381// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001382void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1383 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001384 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001385 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001386 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001387
1388 // Create a seed transition with an empty PredTerm and the expanded sequences
1389 // of SchedWrites for the current SchedClass.
1390 std::vector<PredTransition> LastTransitions;
Craig Topper195aaaf2018-03-22 06:15:10 +00001391 LastTransitions.emplace_back();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001392 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1393 ProcIndices.end());
1394
Benjamin Kramere1761952015-10-24 12:46:49 +00001395 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001396 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001397 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topper195aaaf2018-03-22 06:15:10 +00001398 LastTransitions[0].WriteSequences.emplace_back();
1399 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001400 Seq.append(WriteSeq.begin(), WriteSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001401 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1402 }
1403 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001404 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001405 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001406 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topper195aaaf2018-03-22 06:15:10 +00001407 LastTransitions[0].ReadSequences.emplace_back();
1408 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001409 Seq.append(ReadSeq.begin(), ReadSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001410 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1411 }
1412 DEBUG(dbgs() << '\n');
1413
1414 // Collect all PredTransitions for individual operands.
1415 // Iterate until no variant writes remain.
1416 while (hasVariant(LastTransitions, *this)) {
1417 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001418 for (const PredTransition &Trans : LastTransitions)
1419 Transitions.substituteVariants(Trans);
Andrew Trick33401e82012-09-15 00:19:59 +00001420 DEBUG(Transitions.dump());
1421 LastTransitions.swap(Transitions.TransVec);
1422 }
1423 // If the first transition has no variants, nothing to do.
1424 if (LastTransitions[0].PredTerm.empty())
1425 return;
1426
1427 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1428 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001429 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001430}
1431
Andrew Trickcf398b22013-04-23 23:45:14 +00001432// Check if any processor resource group contains all resource records in
1433// SubUnits.
1434bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1435 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1436 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1437 continue;
1438 RecVec SuperUnits =
1439 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1440 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1441 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001442 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001443 break;
1444 }
1445 }
1446 if (RI == RE)
1447 return true;
1448 }
1449 return false;
1450}
1451
1452// Verify that overlapping groups have a common supergroup.
1453void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1454 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1455 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1456 continue;
1457 RecVec CheckUnits =
1458 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1459 for (unsigned j = i+1; j < e; ++j) {
1460 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1461 continue;
1462 RecVec OtherUnits =
1463 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1464 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1465 OtherUnits.begin(), OtherUnits.end())
1466 != CheckUnits.end()) {
1467 // CheckUnits and OtherUnits overlap
1468 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1469 CheckUnits.end());
1470 if (!hasSuperGroup(OtherUnits, PM)) {
1471 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1472 "proc resource group overlaps with "
1473 + PM.ProcResourceDefs[j]->getName()
1474 + " but no supergroup contains both.");
1475 }
1476 }
1477 }
1478 }
1479}
1480
Andrew Trick1e46d482012-09-15 00:20:02 +00001481// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1482void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001483 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1484 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1485
Andrew Trick1e46d482012-09-15 00:20:02 +00001486 // Add any subtarget-specific SchedReadWrites that are directly associated
1487 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1488 // determine which processors they apply to.
1489 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1490 SCI != SCE; ++SCI) {
1491 if (SCI->ItinClassDef)
1492 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001493 else {
1494 // This class may have a default ReadWrite list which can be overriden by
1495 // InstRW definitions.
1496 if (!SCI->InstRWs.empty()) {
1497 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1498 RWI != RWE; ++RWI) {
1499 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
1500 IdxVec ProcIndices(1, getProcModel(RWModelDef).Index);
1501 IdxVec Writes, Reads;
1502 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1503 Writes, Reads);
1504 collectRWResources(Writes, Reads, ProcIndices);
1505 }
1506 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001507 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001508 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001509 }
1510 // Add resources separately defined by each subtarget.
1511 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001512 for (Record *WR : WRDefs) {
1513 Record *ModelDef = WR->getValueAsDef("SchedModel");
1514 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001515 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001516 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001517 for (Record *SWR : SWRDefs) {
1518 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1519 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001520 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001521 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001522 for (Record *RA : RADefs) {
1523 Record *ModelDef = RA->getValueAsDef("SchedModel");
1524 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001525 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001526 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001527 for (Record *SRA : SRADefs) {
1528 if (SRA->getValueInit("SchedModel")->isComplete()) {
1529 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1530 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001531 }
1532 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001533 // Add ProcResGroups that are defined within this processor model, which may
1534 // not be directly referenced but may directly specify a buffer size.
1535 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001536 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001537 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001538 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001539 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1540 if (!is_contained(PM.ProcResourceDefs, PRG))
1541 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001542 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001543 // Add ProcResourceUnits unconditionally.
1544 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1545 if (!PRU->getValueInit("SchedModel")->isComplete())
1546 continue;
1547 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1548 if (!is_contained(PM.ProcResourceDefs, PRU))
1549 PM.ProcResourceDefs.push_back(PRU);
1550 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001551 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001552 for (CodeGenProcModel &PM : ProcModels) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001553 std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1554 LessRecord());
1555 std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1556 LessRecord());
1557 std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1558 LessRecord());
1559 DEBUG(
1560 PM.dump();
1561 dbgs() << "WriteResDefs: ";
1562 for (RecIter RI = PM.WriteResDefs.begin(),
1563 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1564 if ((*RI)->isSubClassOf("WriteRes"))
1565 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1566 else
1567 dbgs() << (*RI)->getName() << " ";
1568 }
1569 dbgs() << "\nReadAdvanceDefs: ";
1570 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1571 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1572 if ((*RI)->isSubClassOf("ReadAdvance"))
1573 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1574 else
1575 dbgs() << (*RI)->getName() << " ";
1576 }
1577 dbgs() << "\nProcResourceDefs: ";
1578 for (RecIter RI = PM.ProcResourceDefs.begin(),
1579 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1580 dbgs() << (*RI)->getName() << " ";
1581 }
1582 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001583 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001584 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001585
1586 ProcResourceDefs.clear();
1587 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001588}
1589
Matthias Braun17cb5792016-03-01 20:03:21 +00001590void CodeGenSchedModels::checkCompleteness() {
1591 bool Complete = true;
1592 bool HadCompleteModel = false;
1593 for (const CodeGenProcModel &ProcModel : procModels()) {
Matthias Braun17cb5792016-03-01 20:03:21 +00001594 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1595 continue;
1596 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1597 if (Inst->hasNoSchedulingInfo)
1598 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001599 if (ProcModel.isUnsupported(*Inst))
1600 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001601 unsigned SCIdx = getSchedClassIdx(*Inst);
1602 if (!SCIdx) {
1603 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1604 PrintError("No schedule information for instruction '"
1605 + Inst->TheDef->getName() + "'");
1606 Complete = false;
1607 }
1608 continue;
1609 }
1610
1611 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1612 if (!SC.Writes.empty())
1613 continue;
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001614 if (SC.ItinClassDef != nullptr &&
1615 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001616 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001617
1618 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001619 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1620 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1621 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001622 if (I == InstRWs.end()) {
1623 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1624 Inst->TheDef->getName() + "'");
1625 Complete = false;
1626 }
1627 }
1628 HadCompleteModel = true;
1629 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001630 if (!Complete) {
1631 errs() << "\n\nIncomplete schedule models found.\n"
1632 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1633 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1634 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001635 "you may temporarily use an empty list.\n"
1636 << "- Instructions related to unsupported features can be excluded with "
1637 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1638 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001639 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001640 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001641}
1642
Andrew Trick1e46d482012-09-15 00:20:02 +00001643// Collect itinerary class resources for each processor.
1644void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1645 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1646 const CodeGenProcModel &PM = ProcModels[PIdx];
1647 // For all ItinRW entries.
1648 bool HasMatch = false;
1649 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1650 II != IE; ++II) {
1651 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1652 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1653 continue;
1654 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001655 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1656 + ItinClassDef->getName()
1657 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001658 HasMatch = true;
1659 IdxVec Writes, Reads;
1660 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1661 IdxVec ProcIndices(1, PIdx);
1662 collectRWResources(Writes, Reads, ProcIndices);
1663 }
1664 }
1665}
1666
Andrew Trickd0b9c442012-10-10 05:43:13 +00001667void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001668 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001669 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1670 if (SchedRW.TheDef) {
1671 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001672 for (unsigned Idx : ProcIndices)
1673 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001674 }
1675 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001676 for (unsigned Idx : ProcIndices)
1677 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001678 }
1679 }
1680 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1681 AI != AE; ++AI) {
1682 IdxVec AliasProcIndices;
1683 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1684 AliasProcIndices.push_back(
1685 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1686 }
1687 else
1688 AliasProcIndices = ProcIndices;
1689 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1690 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1691
1692 IdxVec ExpandedRWs;
1693 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1694 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1695 SI != SE; ++SI) {
1696 collectRWResources(*SI, IsRead, AliasProcIndices);
1697 }
1698 }
1699}
Andrew Trick1e46d482012-09-15 00:20:02 +00001700
1701// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001702void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1703 ArrayRef<unsigned> Reads,
1704 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001705 for (unsigned Idx : Writes)
1706 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001707
Benjamin Kramere1761952015-10-24 12:46:49 +00001708 for (unsigned Idx : Reads)
1709 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001710}
1711
1712// Find the processor's resource units for this kind of resource.
1713Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001714 const CodeGenProcModel &PM,
1715 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00001716 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1717 return ProcResKind;
1718
Craig Topper24064772014-04-15 07:20:03 +00001719 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001720 assert(!ProcResourceDefs.empty());
1721 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001722
Javed Absar67b042c2017-09-13 10:31:10 +00001723 for (Record *ProcResDef : ProcResourceDefs) {
1724 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1725 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001726 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001727 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001728 "Multiple ProcessorResourceUnits associated with "
1729 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001730 }
Javed Absar67b042c2017-09-13 10:31:10 +00001731 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001732 }
1733 }
Javed Absar67b042c2017-09-13 10:31:10 +00001734 for (Record *ProcResGroup : ProcResGroups) {
1735 if (ProcResGroup == ProcResKind
1736 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001737 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001738 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00001739 "Multiple ProcessorResourceUnits associated with "
1740 + ProcResKind->getName());
1741 }
Javed Absar67b042c2017-09-13 10:31:10 +00001742 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001743 }
1744 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001745 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001746 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001747 "No ProcessorResources associated with "
1748 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001749 }
1750 return ProcUnitDef;
1751}
1752
1753// Iteratively add a resource and its super resources.
1754void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001755 CodeGenProcModel &PM,
1756 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001757 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001758 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00001759
1760 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001761 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001762 return;
1763
1764 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001765 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1766 return;
1767
Andrew Trick1e46d482012-09-15 00:20:02 +00001768 if (!ProcResUnits->getValueInit("Super")->isComplete())
1769 return;
1770
1771 ProcResKind = ProcResUnits->getValueAsDef("Super");
1772 }
1773}
1774
1775// Add resources for a SchedWrite to this processor if they don't exist.
1776void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001777 assert(PIdx && "don't add resources to an invalid Processor model");
1778
Andrew Trick1e46d482012-09-15 00:20:02 +00001779 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001780 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001781 return;
1782 WRDefs.push_back(ProcWriteResDef);
1783
1784 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1785 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1786 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1787 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001788 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00001789 }
1790}
1791
1792// Add resources for a ReadAdvance to this processor if they don't exist.
1793void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1794 unsigned PIdx) {
1795 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001796 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001797 return;
1798 RADefs.push_back(ProcReadAdvanceDef);
1799}
1800
Andrew Trick8fa00f52012-09-17 22:18:43 +00001801unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001802 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001803 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001804 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1805 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001806 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001807 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001808}
1809
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001810bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1811 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1812 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1813 if (TheDef->getName() == PredDef->getName())
1814 return true;
1815 }
1816 }
1817 return false;
1818}
1819
Andrew Trick76686492012-09-15 00:19:57 +00001820#ifndef NDEBUG
1821void CodeGenProcModel::dump() const {
1822 dbgs() << Index << ": " << ModelName << " "
1823 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1824 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1825}
1826
1827void CodeGenSchedRW::dump() const {
1828 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1829 if (IsSequence) {
1830 dbgs() << "(";
1831 dumpIdxVec(Sequence);
1832 dbgs() << ")";
1833 }
1834}
1835
1836void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001837 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001838 << " Writes: ";
1839 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1840 SchedModels->getSchedWrite(Writes[i]).dump();
1841 if (i < N-1) {
1842 dbgs() << '\n';
1843 dbgs().indent(10);
1844 }
1845 }
1846 dbgs() << "\n Reads: ";
1847 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1848 SchedModels->getSchedRead(Reads[i]).dump();
1849 if (i < N-1) {
1850 dbgs() << '\n';
1851 dbgs().indent(10);
1852 }
1853 }
1854 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001855 if (!Transitions.empty()) {
1856 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001857 for (const CodeGenSchedTransition &Transition : Transitions) {
1858 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001859 }
1860 }
Andrew Trick76686492012-09-15 00:19:57 +00001861}
Andrew Trick33401e82012-09-15 00:19:59 +00001862
1863void PredTransitions::dump() const {
1864 dbgs() << "Expanded Variants:\n";
1865 for (std::vector<PredTransition>::const_iterator
1866 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1867 dbgs() << "{";
1868 for (SmallVectorImpl<PredCheck>::const_iterator
1869 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1870 PCI != PCE; ++PCI) {
1871 if (PCI != TI->PredTerm.begin())
1872 dbgs() << ", ";
1873 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1874 << ":" << PCI->Predicate->getName();
1875 }
1876 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001877 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001878 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1879 WSI != WSE; ++WSI) {
1880 dbgs() << "(";
1881 for (SmallVectorImpl<unsigned>::const_iterator
1882 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1883 if (WI != WSI->begin())
1884 dbgs() << ", ";
1885 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1886 }
1887 dbgs() << "),";
1888 }
1889 dbgs() << "}\n";
1890 }
1891}
Andrew Trick76686492012-09-15 00:19:57 +00001892#endif // NDEBUG