blob: 8f51df8b8c2333567c0de558860ce4e2de9c8f06 [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
Andrew Trick87255e32012-07-07 04:00:00 +0000560 SchedClasses.resize(1);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000561 SchedClasses.back().Index = 0;
562 SchedClasses.back().Name = "NoInstrModel";
563 SchedClasses.back().ItinClassDef = Records.getDef("NoItinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000564 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000565
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000566 // Create a SchedClass for each unique combination of itinerary class and
567 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000568 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000569 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000570 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000571 if (!Inst->TheDef->isValueUnset("SchedRW"))
572 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000573
Andrew Trick76686492012-09-15 00:19:57 +0000574 // ProcIdx == 0 indicates the class applies to all processors.
575 IdxVec ProcIndices(1, 0);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000576
577 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, ProcIndices);
Craig Topper8a417c12014-12-09 08:05:51 +0000578 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000579 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000580 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000581 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
582 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000583 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000584 for (Record *RWDef : InstRWDefs)
585 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000586
Andrew Trick76686492012-09-15 00:19:57 +0000587 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000588
Andrew Trick76686492012-09-15 00:19:57 +0000589 bool EnableDump = false;
590 DEBUG(EnableDump = true);
591 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000592 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000593
Joel Jones80372332017-06-28 00:06:40 +0000594 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000595 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000596 StringRef InstName = Inst->TheDef->getName();
Craig Topper8a417c12014-12-09 08:05:51 +0000597 unsigned SCIdx = InstrClassMap.lookup(Inst->TheDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000598 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000599 if (!Inst->hasNoSchedulingInfo)
600 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000601 continue;
602 }
603 CodeGenSchedClass &SC = getSchedClass(SCIdx);
604 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000605 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000606 "must not be subtarget specific.");
607
608 IdxVec ProcIndices;
609 if (SC.ItinClassDef->getName() != "NoItinerary") {
610 ProcIndices.push_back(0);
611 dbgs() << "Itinerary for " << InstName << ": "
612 << SC.ItinClassDef->getName() << '\n';
613 }
614 if (!SC.Writes.empty()) {
615 ProcIndices.push_back(0);
616 dbgs() << "SchedRW machine model for " << InstName;
617 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
618 dbgs() << " " << SchedWrites[*WI].Name;
619 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
620 dbgs() << " " << SchedReads[*RI].Name;
621 dbgs() << '\n';
622 }
623 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000624 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000625 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000626 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000627 ProcIndices.push_back(ProcModel.Index);
628 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000629 IdxVec Writes;
630 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000631 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000632 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000633 for (unsigned WIdx : Writes)
634 dbgs() << " " << SchedWrites[WIdx].Name;
635 for (unsigned RIdx : Reads)
636 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000637 dbgs() << '\n';
638 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000639 // If ProcIndices contains zero, the class applies to all processors.
640 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
Javed Absar21c75912017-10-09 16:21:25 +0000641 for (const CodeGenProcModel &PM : ProcModels) {
Javed Absarfc500042017-10-05 13:27:43 +0000642 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
Andrew Trickf9df92c92016-10-18 04:17:44 +0000643 dbgs() << "No machine model for " << Inst->TheDef->getName()
Javed Absarfc500042017-10-05 13:27:43 +0000644 << " on processor " << PM.ModelName << '\n';
Andrew Trickf9df92c92016-10-18 04:17:44 +0000645 }
Andrew Trick87255e32012-07-07 04:00:00 +0000646 }
647 }
Andrew Trick76686492012-09-15 00:19:57 +0000648}
649
Andrew Trick76686492012-09-15 00:19:57 +0000650/// Find an SchedClass that has been inferred from a per-operand list of
651/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000652unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000653 ArrayRef<unsigned> Writes,
654 ArrayRef<unsigned> Reads) const {
Andrew Trick76686492012-09-15 00:19:57 +0000655 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000656 if (I->ItinClassDef == ItinClassDef && makeArrayRef(I->Writes) == Writes &&
657 makeArrayRef(I->Reads) == Reads) {
Andrew Trick76686492012-09-15 00:19:57 +0000658 return I - schedClassBegin();
659 }
Andrew Trick87255e32012-07-07 04:00:00 +0000660 }
Andrew Trick76686492012-09-15 00:19:57 +0000661 return 0;
662}
Andrew Trick87255e32012-07-07 04:00:00 +0000663
Andrew Trick76686492012-09-15 00:19:57 +0000664// Get the SchedClass index for an instruction.
665unsigned CodeGenSchedModels::getSchedClassIdx(
666 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000667
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000668 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000669}
670
Benjamin Kramere1761952015-10-24 12:46:49 +0000671std::string
672CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
673 ArrayRef<unsigned> OperWrites,
674 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000675
676 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000677 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
678 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000679 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000680 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000681 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000682 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000683 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000684 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000685 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000686 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000687 }
688 return Name;
689}
690
691std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
692
693 std::string Name;
694 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
695 if (I != InstDefs.begin())
696 Name += '_';
697 Name += (*I)->getName();
698 }
699 return Name;
700}
701
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000702/// Add an inferred sched class from an itinerary class and per-operand list of
703/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
704/// processors that may utilize this class.
705unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000706 ArrayRef<unsigned> OperWrites,
707 ArrayRef<unsigned> OperReads,
708 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000709 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
710
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000711 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
712 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000713 IdxVec PI;
714 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
715 SchedClasses[Idx].ProcIndices.end(),
716 ProcIndices.begin(), ProcIndices.end(),
717 std::back_inserter(PI));
718 SchedClasses[Idx].ProcIndices.swap(PI);
719 return Idx;
720 }
721 Idx = SchedClasses.size();
722 SchedClasses.resize(Idx+1);
723 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000724 SC.Index = Idx;
725 SC.Name = createSchedClassName(ItinClassDef, OperWrites, OperReads);
726 SC.ItinClassDef = ItinClassDef;
Andrew Trick76686492012-09-15 00:19:57 +0000727 SC.Writes = OperWrites;
728 SC.Reads = OperReads;
729 SC.ProcIndices = ProcIndices;
730
731 return Idx;
732}
733
734// Create classes for each set of opcodes that are in the same InstReadWrite
735// definition across all processors.
736void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
737 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
738 // intersects with an existing class via a previous InstRWDef. Instrs that do
739 // not intersect with an existing class refer back to their former class as
740 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +0000741 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000742 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000743 const RecVec *InstDefs = Sets.expand(InstRWDef);
744 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000745 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000746
Craig Topper93dd77d2018-03-18 08:38:03 +0000747 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +0000748 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000749 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +0000750 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000751 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +0000752 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +0000753 }
754 // For each set of Instrs, create a new class if necessary, and map or remap
755 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +0000756 for (auto &Entry : ClassInstrs) {
757 unsigned OldSCIdx = Entry.first;
758 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +0000759 // If the all instrs in the current class are accounted for, then leave
760 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000761 if (OldSCIdx) {
762 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
763 if (!RWDefs.empty()) {
764 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
765 unsigned OrigNumInstrs = 0;
Craig Topper93dd77d2018-03-18 08:38:03 +0000766 for (Record *OIDef : *OrigInstDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000767 if (InstrClassMap[OIDef] == OldSCIdx)
Andrew Trick78a08512013-06-05 06:55:20 +0000768 ++OrigNumInstrs;
769 }
770 if (OrigNumInstrs == InstDefs.size()) {
771 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
772 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000773 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
774 // Make sure we didn't already have a InstRW containing this
775 // instruction on this model.
776 for (Record *RWD : RWDefs) {
777 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
778 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
779 for (Record *Inst : InstDefs) {
780 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
781 Inst->getName() + " also matches " +
782 RWD->getValue("Instrs")->getValue()->getAsString());
783 }
784 }
785 }
Andrew Trick78a08512013-06-05 06:55:20 +0000786 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
787 << SchedClasses[OldSCIdx].Name << " on "
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000788 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +0000789 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
790 continue;
791 }
792 }
Andrew Trick76686492012-09-15 00:19:57 +0000793 }
794 unsigned SCIdx = SchedClasses.size();
795 SchedClasses.resize(SCIdx+1);
796 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000797 SC.Index = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000798 SC.Name = createSchedClassName(InstDefs);
Andrew Trick78a08512013-06-05 06:55:20 +0000799 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
800 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
801
Andrew Trick76686492012-09-15 00:19:57 +0000802 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
803 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
804 SC.Writes = SchedClasses[OldSCIdx].Writes;
805 SC.Reads = SchedClasses[OldSCIdx].Reads;
806 SC.ProcIndices.push_back(0);
807 // Map each Instr to this new class.
808 // Note that InstDefs may be a smaller list than InstRWDef's "Instrs".
Andrew Trick9e1deb62012-10-03 23:06:32 +0000809 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
810 SmallSet<unsigned, 4> RemappedClassIDs;
Andrew Trick76686492012-09-15 00:19:57 +0000811 for (ArrayRef<Record*>::const_iterator
812 II = InstDefs.begin(), IE = InstDefs.end(); II != IE; ++II) {
813 unsigned OldSCIdx = InstrClassMap[*II];
David Blaikie70573dc2014-11-19 07:49:26 +0000814 if (OldSCIdx && RemappedClassIDs.insert(OldSCIdx).second) {
Andrew Trick9e1deb62012-10-03 23:06:32 +0000815 for (RecIter RI = SchedClasses[OldSCIdx].InstRWs.begin(),
816 RE = SchedClasses[OldSCIdx].InstRWs.end(); RI != RE; ++RI) {
817 if ((*RI)->getValueAsDef("SchedModel") == RWModelDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000818 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
Andrew Trick9e1deb62012-10-03 23:06:32 +0000819 (*II)->getName() + " also matches " +
820 (*RI)->getValue("Instrs")->getValue()->getAsString());
821 }
822 assert(*RI != InstRWDef && "SchedClass has duplicate InstRW def");
823 SC.InstRWs.push_back(*RI);
824 }
Andrew Trick76686492012-09-15 00:19:57 +0000825 }
826 InstrClassMap[*II] = SCIdx;
827 }
828 SC.InstRWs.push_back(InstRWDef);
829 }
Andrew Trick87255e32012-07-07 04:00:00 +0000830}
831
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000832// True if collectProcItins found anything.
833bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000834 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
835 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000836 return true;
837 }
838 return false;
839}
840
Andrew Trick87255e32012-07-07 04:00:00 +0000841// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000842void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000843 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000844 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000845 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000846 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000847
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000848 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
849 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
850
851 // Populate ItinDefList with Itinerary records.
852 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000853
854 // Insert each itinerary data record in the correct position within
855 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +0000856 for (Record *ItinData : ItinRecords) {
Andrew Trick76686492012-09-15 00:19:57 +0000857 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000858 bool FoundClass = false;
859 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
860 SCI != SCE; ++SCI) {
861 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000862 if (SCI->ItinClassDef == ItinDef) {
863 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000864 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000865 }
Andrew Trick76686492012-09-15 00:19:57 +0000866 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000867 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000868 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
869 << " missing class for itinerary " << ItinDef->getName() << '\n');
870 }
Andrew Trick87255e32012-07-07 04:00:00 +0000871 }
Andrew Trick76686492012-09-15 00:19:57 +0000872 // Check for missing itinerary entries.
873 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
874 DEBUG(
875 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
876 if (!ProcModel.ItinDefList[i])
877 dbgs() << ProcModel.ItinsDef->getName()
878 << " missing itinerary for class "
879 << SchedClasses[i].Name << '\n';
880 });
Andrew Trick87255e32012-07-07 04:00:00 +0000881 }
Andrew Trick87255e32012-07-07 04:00:00 +0000882}
Andrew Trick76686492012-09-15 00:19:57 +0000883
884// Gather the read/write types for each itinerary class.
885void CodeGenSchedModels::collectProcItinRW() {
886 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
887 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +0000888 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000889 if (!RWDef->getValueInit("SchedModel")->isComplete())
890 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
891 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +0000892 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
893 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000894 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000895 + ModelDef->getName());
896 }
Javed Absarf45d0b92017-10-08 17:23:30 +0000897 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000898 }
899}
900
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000901// Gather the unsupported features for processor models.
902void CodeGenSchedModels::collectProcUnsupportedFeatures() {
903 for (CodeGenProcModel &ProcModel : ProcModels) {
904 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
905 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
906 }
907 }
908}
909
Andrew Trick33401e82012-09-15 00:19:59 +0000910/// Infer new classes from existing classes. In the process, this may create new
911/// SchedWrites from sequences of existing SchedWrites.
912void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000913 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000914 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
915
Andrew Trick33401e82012-09-15 00:19:59 +0000916 // Visit all existing classes and newly created classes.
917 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000918 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
919
Andrew Trick33401e82012-09-15 00:19:59 +0000920 if (SchedClasses[Idx].ItinClassDef)
921 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000922 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000923 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000924 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000925 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
926 Idx, SchedClasses[Idx].ProcIndices);
927 }
928 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
929 "too many SchedVariants");
930 }
931}
932
933/// Infer classes from per-processor itinerary resources.
934void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
935 unsigned FromClassIdx) {
936 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
937 const CodeGenProcModel &PM = ProcModels[PIdx];
938 // For all ItinRW entries.
939 bool HasMatch = false;
940 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
941 II != IE; ++II) {
942 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
943 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
944 continue;
945 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000946 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000947 + ItinClassDef->getName()
948 + " in ItinResources for " + PM.ModelName);
949 HasMatch = true;
950 IdxVec Writes, Reads;
951 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
952 IdxVec ProcIndices(1, PIdx);
953 inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
954 }
955 }
956}
957
958/// Infer classes from per-processor InstReadWrite definitions.
959void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000960 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000961 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000962 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
963 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000964 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000965 for (; II != IE; ++II) {
966 if (InstrClassMap[*II] == SCIdx)
967 break;
968 }
969 // If this class no longer has any instructions mapped to it, it has become
970 // irrelevant.
971 if (II == IE)
972 continue;
973 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000974 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
975 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Andrew Trick33401e82012-09-15 00:19:59 +0000976 IdxVec ProcIndices(1, PIdx);
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000977 inferFromRW(Writes, Reads, SCIdx, ProcIndices); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +0000978 }
979}
980
981namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000982
Andrew Trick9257b8f2012-09-22 02:24:21 +0000983// Helper for substituteVariantOperand.
984struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +0000985 Record *VarOrSeqDef; // Variant or sequence.
986 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000987 unsigned ProcIdx; // Processor model index or zero for any.
988 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
989
990 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +0000991 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +0000992};
993
Andrew Trick33401e82012-09-15 00:19:59 +0000994// Associate a predicate with the SchedReadWrite that it guards.
995// RWIdx is the index of the read/write variant.
996struct PredCheck {
997 bool IsRead;
998 unsigned RWIdx;
999 Record *Predicate;
1000
1001 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
1002};
1003
1004// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1005struct PredTransition {
1006 // A predicate term is a conjunction of PredChecks.
1007 SmallVector<PredCheck, 4> PredTerm;
1008 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1009 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001010 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001011};
1012
1013// Encapsulate a set of partially constructed transitions.
1014// The results are built by repeated calls to substituteVariants.
1015class PredTransitions {
1016 CodeGenSchedModels &SchedModels;
1017
1018public:
1019 std::vector<PredTransition> TransVec;
1020
1021 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1022
1023 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1024 bool IsRead, unsigned StartIdx);
1025
1026 void substituteVariants(const PredTransition &Trans);
1027
1028#ifndef NDEBUG
1029 void dump() const;
1030#endif
1031
1032private:
1033 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001034 void getIntersectingVariants(
1035 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1036 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001037 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001038};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001039
1040} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001041
1042// Return true if this predicate is mutually exclusive with a PredTerm. This
1043// degenerates into checking if the predicate is mutually exclusive with any
1044// predicate in the Term's conjunction.
1045//
1046// All predicates associated with a given SchedRW are considered mutually
1047// exclusive. This should work even if the conditions expressed by the
1048// predicates are not exclusive because the predicates for a given SchedWrite
1049// are always checked in the order they are defined in the .td file. Later
1050// conditions implicitly negate any prior condition.
1051bool PredTransitions::mutuallyExclusive(Record *PredDef,
1052 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001053 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001054 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001055 return false;
1056
Javed Absarfc500042017-10-05 13:27:43 +00001057 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001058 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1059 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1060 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1061 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1062 return true;
1063 }
1064 }
1065 return false;
1066}
1067
Andrew Trickda984b12012-10-03 23:06:28 +00001068static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1069 CodeGenSchedModels &SchedModels) {
1070 if (RW.HasVariants)
1071 return true;
1072
Javed Absar21c75912017-10-09 16:21:25 +00001073 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001074 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001075 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001076 if (AliasRW.HasVariants)
1077 return true;
1078 if (AliasRW.IsSequence) {
1079 IdxVec ExpandedRWs;
1080 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1081 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1082 SI != SE; ++SI) {
1083 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1084 SchedModels)) {
1085 return true;
1086 }
1087 }
1088 }
1089 }
1090 return false;
1091}
1092
1093static bool hasVariant(ArrayRef<PredTransition> Transitions,
1094 CodeGenSchedModels &SchedModels) {
1095 for (ArrayRef<PredTransition>::iterator
1096 PTI = Transitions.begin(), PTE = Transitions.end();
1097 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001098 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001099 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1100 WSI != WSE; ++WSI) {
1101 for (SmallVectorImpl<unsigned>::const_iterator
1102 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1103 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1104 return true;
1105 }
1106 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001107 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001108 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1109 RSI != RSE; ++RSI) {
1110 for (SmallVectorImpl<unsigned>::const_iterator
1111 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1112 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1113 return true;
1114 }
1115 }
1116 }
1117 return false;
1118}
1119
1120// Populate IntersectingVariants with any variants or aliased sequences of the
1121// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001122// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001123void PredTransitions::getIntersectingVariants(
1124 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1125 std::vector<TransVariant> &IntersectingVariants) {
1126
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001127 bool GenericRW = false;
1128
Andrew Trickda984b12012-10-03 23:06:28 +00001129 std::vector<TransVariant> Variants;
1130 if (SchedRW.HasVariants) {
1131 unsigned VarProcIdx = 0;
1132 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1133 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1134 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1135 }
1136 // Push each variant. Assign TransVecIdx later.
1137 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001138 for (Record *VarDef : VarDefs)
1139 Variants.push_back(TransVariant(VarDef, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001140 if (VarProcIdx == 0)
1141 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001142 }
1143 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1144 AI != AE; ++AI) {
1145 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1146 // to is defined within a processor model, constrain all variants to
1147 // that processor.
1148 unsigned AliasProcIdx = 0;
1149 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1150 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1151 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1152 }
1153 const CodeGenSchedRW &AliasRW =
1154 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1155
1156 if (AliasRW.HasVariants) {
1157 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001158 for (Record *VD : VarDefs)
1159 Variants.push_back(TransVariant(VD, AliasRW.Index, AliasProcIdx, 0));
Andrew Trickda984b12012-10-03 23:06:28 +00001160 }
1161 if (AliasRW.IsSequence) {
1162 Variants.push_back(
1163 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1164 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001165 if (AliasProcIdx == 0)
1166 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001167 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001168 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001169 // Don't expand variants if the processor models don't intersect.
1170 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001171 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001172 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001173 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1174 Variant.ProcIdx);
1175 if (!Cnt)
1176 continue;
1177 if (Cnt > 1) {
1178 const CodeGenProcModel &PM =
1179 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001180 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1181 "Multiple variants defined for processor " +
1182 PM.ModelName +
1183 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001184 }
1185 }
1186 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1187 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1188 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1189 continue;
1190 }
1191 if (IntersectingVariants.empty()) {
1192 // The first variant builds on the existing transition.
1193 Variant.TransVecIdx = TransIdx;
1194 IntersectingVariants.push_back(Variant);
1195 }
1196 else {
1197 // Push another copy of the current transition for more variants.
1198 Variant.TransVecIdx = TransVec.size();
1199 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001200 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001201 }
1202 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001203 if (GenericRW && IntersectingVariants.empty()) {
1204 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1205 "a matching predicate on any processor");
1206 }
Andrew Trickda984b12012-10-03 23:06:28 +00001207}
1208
Andrew Trick9257b8f2012-09-22 02:24:21 +00001209// Push the Reads/Writes selected by this variant onto the PredTransition
1210// specified by VInfo.
1211void PredTransitions::
1212pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001213 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1214
Andrew Trick9257b8f2012-09-22 02:24:21 +00001215 // If this operand transition is reached through a processor-specific alias,
1216 // then the whole transition is specific to this processor.
1217 if (VInfo.ProcIdx != 0)
1218 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1219
Andrew Trick33401e82012-09-15 00:19:59 +00001220 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001221 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1222 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1223 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1224 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1225 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1226 }
1227 else {
1228 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1229 "variant must be a SchedVariant or aliased WriteSequence");
1230 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1231 }
Andrew Trick33401e82012-09-15 00:19:59 +00001232
Andrew Trick9257b8f2012-09-22 02:24:21 +00001233 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001234
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001235 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001236 ? Trans.ReadSequences : Trans.WriteSequences;
1237 if (SchedRW.IsVariadic) {
1238 unsigned OperIdx = RWSequences.size()-1;
1239 // Make N-1 copies of this transition's last sequence.
1240 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001241 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001242 RWSequences.reserve(RWSequences.size() + 1);
1243 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001244 }
1245 // Push each of the N elements of the SelectedRWs onto a copy of the last
1246 // sequence (split the current operand into N operands).
1247 // Note that write sequences should be expanded within this loop--the entire
1248 // sequence belongs to a single operand.
1249 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1250 RWI != RWE; ++RWI, ++OperIdx) {
1251 IdxVec ExpandedRWs;
1252 if (IsRead)
1253 ExpandedRWs.push_back(*RWI);
1254 else
1255 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1256 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1257 ExpandedRWs.begin(), ExpandedRWs.end());
1258 }
1259 assert(OperIdx == RWSequences.size() && "missed a sequence");
1260 }
1261 else {
1262 // Push this transition's expanded sequence onto this transition's last
1263 // sequence (add to the current operand's sequence).
1264 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1265 IdxVec ExpandedRWs;
1266 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1267 RWI != RWE; ++RWI) {
1268 if (IsRead)
1269 ExpandedRWs.push_back(*RWI);
1270 else
1271 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1272 }
1273 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1274 }
1275}
1276
1277// RWSeq is a sequence of all Reads or all Writes for the next read or write
1278// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001279// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001280// of TransVec.
1281void PredTransitions::substituteVariantOperand(
1282 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1283
1284 // Visit each original RW within the current sequence.
1285 for (SmallVectorImpl<unsigned>::const_iterator
1286 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1287 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1288 // Push this RW on all partial PredTransitions or distribute variants.
1289 // New PredTransitions may be pushed within this loop which should not be
1290 // revisited (TransEnd must be loop invariant).
1291 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1292 TransIdx != TransEnd; ++TransIdx) {
1293 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001294 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001295 if (IsRead)
1296 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1297 else
1298 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1299 continue;
1300 }
1301 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001302 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001303 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001304 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001305 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001306 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001307 IVI = IntersectingVariants.begin(),
1308 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001309 IVI != IVE; ++IVI) {
1310 pushVariant(*IVI, IsRead);
1311 }
Andrew Trick33401e82012-09-15 00:19:59 +00001312 }
1313 }
1314}
1315
1316// For each variant of a Read/Write in Trans, substitute the sequence of
1317// Read/Writes guarded by the variant. This is exponential in the number of
1318// variant Read/Writes, but in practice detection of mutually exclusive
1319// predicates should result in linear growth in the total number variants.
1320//
1321// This is one step in a breadth-first search of nested variants.
1322void PredTransitions::substituteVariants(const PredTransition &Trans) {
1323 // Build up a set of partial results starting at the back of
1324 // PredTransitions. Remember the first new transition.
1325 unsigned StartIdx = TransVec.size();
1326 TransVec.resize(TransVec.size() + 1);
1327 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001328 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001329
1330 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001331 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001332 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1333 WSI != WSE; ++WSI) {
1334 // Push a new (empty) write sequence onto all partial Transitions.
1335 for (std::vector<PredTransition>::iterator I =
1336 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
1337 I->WriteSequences.resize(I->WriteSequences.size() + 1);
1338 }
1339 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1340 }
1341 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001342 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001343 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1344 RSI != RSE; ++RSI) {
1345 // Push a new (empty) read sequence onto all partial Transitions.
1346 for (std::vector<PredTransition>::iterator I =
1347 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
1348 I->ReadSequences.resize(I->ReadSequences.size() + 1);
1349 }
1350 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1351 }
1352}
1353
Andrew Trick33401e82012-09-15 00:19:59 +00001354// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001355static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001356 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001357 CodeGenSchedModels &SchedModels) {
1358 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1359 // requires creating a new SchedClass.
1360 for (ArrayRef<PredTransition>::iterator
1361 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1362 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001363 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1364 [&SchedModels](ArrayRef<unsigned> WS) {
1365 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1366 });
Andrew Trick33401e82012-09-15 00:19:59 +00001367 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001368 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1369 [&SchedModels](ArrayRef<unsigned> RS) {
1370 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1371 });
Andrew Trick9257b8f2012-09-22 02:24:21 +00001372 IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001373 CodeGenSchedTransition SCTrans;
1374 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001375 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001376 OperReadsVariant, ProcIndices);
Andrew Trick33401e82012-09-15 00:19:59 +00001377 SCTrans.ProcIndices = ProcIndices;
1378 // The final PredTerm is unique set of predicates guarding the transition.
1379 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001380 transform(I->PredTerm, std::back_inserter(Preds),
1381 [](const PredCheck &P) {
1382 return P.Predicate;
1383 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001384 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001385 SCTrans.PredTerm = Preds;
1386 SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
1387 }
1388}
1389
Andrew Trick9257b8f2012-09-22 02:24:21 +00001390// Create new SchedClasses for the given ReadWrite list. If any of the
1391// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1392// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001393void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1394 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001395 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001396 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001397 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001398
1399 // Create a seed transition with an empty PredTerm and the expanded sequences
1400 // of SchedWrites for the current SchedClass.
1401 std::vector<PredTransition> LastTransitions;
1402 LastTransitions.resize(1);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001403 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1404 ProcIndices.end());
1405
Benjamin Kramere1761952015-10-24 12:46:49 +00001406 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001407 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001408 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Andrew Trick33401e82012-09-15 00:19:59 +00001409 unsigned Idx = LastTransitions[0].WriteSequences.size();
1410 LastTransitions[0].WriteSequences.resize(Idx + 1);
1411 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences[Idx];
Craig Topper1f57456c2018-03-20 20:24:14 +00001412 Seq.append(WriteSeq.begin(), WriteSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001413 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1414 }
1415 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001416 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001417 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001418 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Andrew Trick33401e82012-09-15 00:19:59 +00001419 unsigned Idx = LastTransitions[0].ReadSequences.size();
1420 LastTransitions[0].ReadSequences.resize(Idx + 1);
1421 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences[Idx];
Craig Topper1f57456c2018-03-20 20:24:14 +00001422 Seq.append(ReadSeq.begin(), ReadSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001423 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1424 }
1425 DEBUG(dbgs() << '\n');
1426
1427 // Collect all PredTransitions for individual operands.
1428 // Iterate until no variant writes remain.
1429 while (hasVariant(LastTransitions, *this)) {
1430 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001431 for (const PredTransition &Trans : LastTransitions)
1432 Transitions.substituteVariants(Trans);
Andrew Trick33401e82012-09-15 00:19:59 +00001433 DEBUG(Transitions.dump());
1434 LastTransitions.swap(Transitions.TransVec);
1435 }
1436 // If the first transition has no variants, nothing to do.
1437 if (LastTransitions[0].PredTerm.empty())
1438 return;
1439
1440 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1441 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001442 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001443}
1444
Andrew Trickcf398b22013-04-23 23:45:14 +00001445// Check if any processor resource group contains all resource records in
1446// SubUnits.
1447bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1448 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1449 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1450 continue;
1451 RecVec SuperUnits =
1452 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1453 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1454 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001455 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001456 break;
1457 }
1458 }
1459 if (RI == RE)
1460 return true;
1461 }
1462 return false;
1463}
1464
1465// Verify that overlapping groups have a common supergroup.
1466void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1467 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1468 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1469 continue;
1470 RecVec CheckUnits =
1471 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1472 for (unsigned j = i+1; j < e; ++j) {
1473 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1474 continue;
1475 RecVec OtherUnits =
1476 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1477 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1478 OtherUnits.begin(), OtherUnits.end())
1479 != CheckUnits.end()) {
1480 // CheckUnits and OtherUnits overlap
1481 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1482 CheckUnits.end());
1483 if (!hasSuperGroup(OtherUnits, PM)) {
1484 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1485 "proc resource group overlaps with "
1486 + PM.ProcResourceDefs[j]->getName()
1487 + " but no supergroup contains both.");
1488 }
1489 }
1490 }
1491 }
1492}
1493
Andrew Trick1e46d482012-09-15 00:20:02 +00001494// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1495void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001496 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1497 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1498
Andrew Trick1e46d482012-09-15 00:20:02 +00001499 // Add any subtarget-specific SchedReadWrites that are directly associated
1500 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1501 // determine which processors they apply to.
1502 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1503 SCI != SCE; ++SCI) {
1504 if (SCI->ItinClassDef)
1505 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001506 else {
1507 // This class may have a default ReadWrite list which can be overriden by
1508 // InstRW definitions.
1509 if (!SCI->InstRWs.empty()) {
1510 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1511 RWI != RWE; ++RWI) {
1512 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
1513 IdxVec ProcIndices(1, getProcModel(RWModelDef).Index);
1514 IdxVec Writes, Reads;
1515 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1516 Writes, Reads);
1517 collectRWResources(Writes, Reads, ProcIndices);
1518 }
1519 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001520 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001521 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001522 }
1523 // Add resources separately defined by each subtarget.
1524 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001525 for (Record *WR : WRDefs) {
1526 Record *ModelDef = WR->getValueAsDef("SchedModel");
1527 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001528 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001529 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001530 for (Record *SWR : SWRDefs) {
1531 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1532 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001533 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001534 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001535 for (Record *RA : RADefs) {
1536 Record *ModelDef = RA->getValueAsDef("SchedModel");
1537 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001538 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001539 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001540 for (Record *SRA : SRADefs) {
1541 if (SRA->getValueInit("SchedModel")->isComplete()) {
1542 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1543 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001544 }
1545 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001546 // Add ProcResGroups that are defined within this processor model, which may
1547 // not be directly referenced but may directly specify a buffer size.
1548 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001549 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001550 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001551 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001552 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1553 if (!is_contained(PM.ProcResourceDefs, PRG))
1554 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001555 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001556 // Add ProcResourceUnits unconditionally.
1557 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1558 if (!PRU->getValueInit("SchedModel")->isComplete())
1559 continue;
1560 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1561 if (!is_contained(PM.ProcResourceDefs, PRU))
1562 PM.ProcResourceDefs.push_back(PRU);
1563 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001564 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001565 for (CodeGenProcModel &PM : ProcModels) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001566 std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1567 LessRecord());
1568 std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1569 LessRecord());
1570 std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1571 LessRecord());
1572 DEBUG(
1573 PM.dump();
1574 dbgs() << "WriteResDefs: ";
1575 for (RecIter RI = PM.WriteResDefs.begin(),
1576 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1577 if ((*RI)->isSubClassOf("WriteRes"))
1578 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1579 else
1580 dbgs() << (*RI)->getName() << " ";
1581 }
1582 dbgs() << "\nReadAdvanceDefs: ";
1583 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1584 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1585 if ((*RI)->isSubClassOf("ReadAdvance"))
1586 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1587 else
1588 dbgs() << (*RI)->getName() << " ";
1589 }
1590 dbgs() << "\nProcResourceDefs: ";
1591 for (RecIter RI = PM.ProcResourceDefs.begin(),
1592 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1593 dbgs() << (*RI)->getName() << " ";
1594 }
1595 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001596 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001597 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001598
1599 ProcResourceDefs.clear();
1600 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001601}
1602
Matthias Braun17cb5792016-03-01 20:03:21 +00001603void CodeGenSchedModels::checkCompleteness() {
1604 bool Complete = true;
1605 bool HadCompleteModel = false;
1606 for (const CodeGenProcModel &ProcModel : procModels()) {
Matthias Braun17cb5792016-03-01 20:03:21 +00001607 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1608 continue;
1609 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1610 if (Inst->hasNoSchedulingInfo)
1611 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001612 if (ProcModel.isUnsupported(*Inst))
1613 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001614 unsigned SCIdx = getSchedClassIdx(*Inst);
1615 if (!SCIdx) {
1616 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1617 PrintError("No schedule information for instruction '"
1618 + Inst->TheDef->getName() + "'");
1619 Complete = false;
1620 }
1621 continue;
1622 }
1623
1624 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1625 if (!SC.Writes.empty())
1626 continue;
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001627 if (SC.ItinClassDef != nullptr &&
1628 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001629 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001630
1631 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001632 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1633 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1634 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001635 if (I == InstRWs.end()) {
1636 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1637 Inst->TheDef->getName() + "'");
1638 Complete = false;
1639 }
1640 }
1641 HadCompleteModel = true;
1642 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001643 if (!Complete) {
1644 errs() << "\n\nIncomplete schedule models found.\n"
1645 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1646 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1647 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001648 "you may temporarily use an empty list.\n"
1649 << "- Instructions related to unsupported features can be excluded with "
1650 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1651 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001652 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001653 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001654}
1655
Andrew Trick1e46d482012-09-15 00:20:02 +00001656// Collect itinerary class resources for each processor.
1657void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1658 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1659 const CodeGenProcModel &PM = ProcModels[PIdx];
1660 // For all ItinRW entries.
1661 bool HasMatch = false;
1662 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1663 II != IE; ++II) {
1664 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1665 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1666 continue;
1667 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001668 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1669 + ItinClassDef->getName()
1670 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001671 HasMatch = true;
1672 IdxVec Writes, Reads;
1673 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1674 IdxVec ProcIndices(1, PIdx);
1675 collectRWResources(Writes, Reads, ProcIndices);
1676 }
1677 }
1678}
1679
Andrew Trickd0b9c442012-10-10 05:43:13 +00001680void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001681 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001682 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1683 if (SchedRW.TheDef) {
1684 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001685 for (unsigned Idx : ProcIndices)
1686 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001687 }
1688 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001689 for (unsigned Idx : ProcIndices)
1690 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001691 }
1692 }
1693 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1694 AI != AE; ++AI) {
1695 IdxVec AliasProcIndices;
1696 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1697 AliasProcIndices.push_back(
1698 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1699 }
1700 else
1701 AliasProcIndices = ProcIndices;
1702 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1703 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1704
1705 IdxVec ExpandedRWs;
1706 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1707 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1708 SI != SE; ++SI) {
1709 collectRWResources(*SI, IsRead, AliasProcIndices);
1710 }
1711 }
1712}
Andrew Trick1e46d482012-09-15 00:20:02 +00001713
1714// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001715void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1716 ArrayRef<unsigned> Reads,
1717 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001718 for (unsigned Idx : Writes)
1719 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001720
Benjamin Kramere1761952015-10-24 12:46:49 +00001721 for (unsigned Idx : Reads)
1722 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001723}
1724
1725// Find the processor's resource units for this kind of resource.
1726Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001727 const CodeGenProcModel &PM,
1728 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00001729 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1730 return ProcResKind;
1731
Craig Topper24064772014-04-15 07:20:03 +00001732 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001733 assert(!ProcResourceDefs.empty());
1734 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001735
Javed Absar67b042c2017-09-13 10:31:10 +00001736 for (Record *ProcResDef : ProcResourceDefs) {
1737 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1738 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001739 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001740 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001741 "Multiple ProcessorResourceUnits associated with "
1742 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001743 }
Javed Absar67b042c2017-09-13 10:31:10 +00001744 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001745 }
1746 }
Javed Absar67b042c2017-09-13 10:31:10 +00001747 for (Record *ProcResGroup : ProcResGroups) {
1748 if (ProcResGroup == ProcResKind
1749 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001750 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001751 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00001752 "Multiple ProcessorResourceUnits associated with "
1753 + ProcResKind->getName());
1754 }
Javed Absar67b042c2017-09-13 10:31:10 +00001755 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001756 }
1757 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001758 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001759 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001760 "No ProcessorResources associated with "
1761 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001762 }
1763 return ProcUnitDef;
1764}
1765
1766// Iteratively add a resource and its super resources.
1767void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001768 CodeGenProcModel &PM,
1769 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001770 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001771 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00001772
1773 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001774 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001775 return;
1776
1777 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001778 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1779 return;
1780
Andrew Trick1e46d482012-09-15 00:20:02 +00001781 if (!ProcResUnits->getValueInit("Super")->isComplete())
1782 return;
1783
1784 ProcResKind = ProcResUnits->getValueAsDef("Super");
1785 }
1786}
1787
1788// Add resources for a SchedWrite to this processor if they don't exist.
1789void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001790 assert(PIdx && "don't add resources to an invalid Processor model");
1791
Andrew Trick1e46d482012-09-15 00:20:02 +00001792 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001793 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001794 return;
1795 WRDefs.push_back(ProcWriteResDef);
1796
1797 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1798 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1799 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1800 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001801 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00001802 }
1803}
1804
1805// Add resources for a ReadAdvance to this processor if they don't exist.
1806void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1807 unsigned PIdx) {
1808 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001809 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001810 return;
1811 RADefs.push_back(ProcReadAdvanceDef);
1812}
1813
Andrew Trick8fa00f52012-09-17 22:18:43 +00001814unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001815 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001816 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001817 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1818 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001819 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001820 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001821}
1822
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001823bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1824 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1825 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1826 if (TheDef->getName() == PredDef->getName())
1827 return true;
1828 }
1829 }
1830 return false;
1831}
1832
Andrew Trick76686492012-09-15 00:19:57 +00001833#ifndef NDEBUG
1834void CodeGenProcModel::dump() const {
1835 dbgs() << Index << ": " << ModelName << " "
1836 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1837 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1838}
1839
1840void CodeGenSchedRW::dump() const {
1841 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1842 if (IsSequence) {
1843 dbgs() << "(";
1844 dumpIdxVec(Sequence);
1845 dbgs() << ")";
1846 }
1847}
1848
1849void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001850 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001851 << " Writes: ";
1852 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1853 SchedModels->getSchedWrite(Writes[i]).dump();
1854 if (i < N-1) {
1855 dbgs() << '\n';
1856 dbgs().indent(10);
1857 }
1858 }
1859 dbgs() << "\n Reads: ";
1860 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1861 SchedModels->getSchedRead(Reads[i]).dump();
1862 if (i < N-1) {
1863 dbgs() << '\n';
1864 dbgs().indent(10);
1865 }
1866 }
1867 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001868 if (!Transitions.empty()) {
1869 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001870 for (const CodeGenSchedTransition &Transition : Transitions) {
1871 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001872 }
1873 }
Andrew Trick76686492012-09-15 00:19:57 +00001874}
Andrew Trick33401e82012-09-15 00:19:59 +00001875
1876void PredTransitions::dump() const {
1877 dbgs() << "Expanded Variants:\n";
1878 for (std::vector<PredTransition>::const_iterator
1879 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1880 dbgs() << "{";
1881 for (SmallVectorImpl<PredCheck>::const_iterator
1882 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1883 PCI != PCE; ++PCI) {
1884 if (PCI != TI->PredTerm.begin())
1885 dbgs() << ", ";
1886 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1887 << ":" << PCI->Predicate->getName();
1888 }
1889 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001890 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001891 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1892 WSI != WSE; ++WSI) {
1893 dbgs() << "(";
1894 for (SmallVectorImpl<unsigned>::const_iterator
1895 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1896 if (WI != WSI->begin())
1897 dbgs() << ", ";
1898 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1899 }
1900 dbgs() << "),";
1901 }
1902 dbgs() << "}\n";
1903 }
1904}
Andrew Trick76686492012-09-15 00:19:57 +00001905#endif // NDEBUG