blob: f0482f4069df988e2194c8bd89f3bd05a64725ad [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"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000018#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000019#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/SmallSet.h"
21#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000022#include "llvm/Support/Casting.h"
Andrew Trick87255e32012-07-07 04:00:00 +000023#include "llvm/Support/Debug.h"
Andrew Trick9e1deb62012-10-03 23:06:32 +000024#include "llvm/Support/Regex.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000026#include "llvm/TableGen/Error.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000027#include <algorithm>
28#include <iterator>
29#include <utility>
Andrew Trick87255e32012-07-07 04:00:00 +000030
31using namespace llvm;
32
Chandler Carruth97acce22014-04-22 03:06:00 +000033#define DEBUG_TYPE "subtarget-emitter"
34
Andrew Trick76686492012-09-15 00:19:57 +000035#ifndef NDEBUG
Benjamin Kramere1761952015-10-24 12:46:49 +000036static void dumpIdxVec(ArrayRef<unsigned> V) {
37 for (unsigned Idx : V)
38 dbgs() << Idx << ", ";
Andrew Trick33401e82012-09-15 00:19:59 +000039}
Andrew Trick76686492012-09-15 00:19:57 +000040#endif
41
Juergen Ributzka05c5a932013-11-19 03:08:35 +000042namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000043
Andrew Trick9e1deb62012-10-03 23:06:32 +000044// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
45struct InstrsOp : public SetTheory::Operator {
Craig Topper716b0732014-03-05 05:17:42 +000046 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
47 ArrayRef<SMLoc> Loc) override {
Juergen Ributzka05c5a932013-11-19 03:08:35 +000048 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
49 }
50};
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000051
Andrew Trick9e1deb62012-10-03 23:06:32 +000052// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
Andrew Trick9e1deb62012-10-03 23:06:32 +000053struct InstRegexOp : public SetTheory::Operator {
54 const CodeGenTarget &Target;
55 InstRegexOp(const CodeGenTarget &t): Target(t) {}
56
Benjamin Kramercbce2f02018-01-23 23:05:04 +000057 /// Remove any text inside of parentheses from S.
58 static std::string removeParens(llvm::StringRef S) {
59 std::string Result;
60 unsigned Paren = 0;
61 // NB: We don't care about escaped parens here.
62 for (char C : S) {
63 switch (C) {
64 case '(':
65 ++Paren;
66 break;
67 case ')':
68 --Paren;
69 break;
70 default:
71 if (Paren == 0)
72 Result += C;
73 }
74 }
75 return Result;
76 }
77
Juergen Ributzka05c5a932013-11-19 03:08:35 +000078 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
Craig Topper716b0732014-03-05 05:17:42 +000079 ArrayRef<SMLoc> Loc) override {
Benjamin Kramercbce2f02018-01-23 23:05:04 +000080 SmallVector<std::pair<StringRef, Optional<Regex>>, 4> RegexList;
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());
86 // Extract a prefix that we can binary search on.
87 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
88 auto FirstMeta = SI->getValue().find_first_of(RegexMetachars);
89 // Look for top-level | or ?. We cannot optimize them to binary search.
90 if (removeParens(SI->getValue()).find_first_of("|?") != std::string::npos)
91 FirstMeta = 0;
92 StringRef Prefix = SI->getValue().substr(0, FirstMeta);
93 std::string pat = SI->getValue().substr(FirstMeta);
94 if (pat.empty()) {
95 RegexList.push_back(std::make_pair(Prefix, None));
96 continue;
97 }
98 // For the rest use a python-style prefix match.
Juergen Ributzka05c5a932013-11-19 03:08:35 +000099 if (pat[0] != '^') {
100 pat.insert(0, "^(");
101 pat.insert(pat.end(), ')');
102 }
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000103 RegexList.push_back(std::make_pair(Prefix, Regex(pat)));
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000104 }
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000105 for (auto &R : RegexList) {
Benjamin Kramer4890a712018-01-24 22:35:11 +0000106 unsigned NumGeneric = Target.getNumFixedInstructions();
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000107 // The generic opcodes are unsorted, handle them manually.
Benjamin Kramer4890a712018-01-24 22:35:11 +0000108 for (auto *Inst :
109 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1)) {
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000110 if (Inst->TheDef->getName().startswith(R.first) &&
111 (!R.second ||
112 R.second->match(Inst->TheDef->getName().substr(R.first.size()))))
113 Elts.insert(Inst->TheDef);
114 }
115
116 ArrayRef<const CodeGenInstruction *> Instructions =
Benjamin Kramer4890a712018-01-24 22:35:11 +0000117 Target.getInstructionsByEnumValue().slice(NumGeneric + 1);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000118
119 // Target instructions are sorted. Find the range that starts with our
120 // prefix.
121 struct Comp {
122 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
123 return LHS->TheDef->getName() < RHS;
124 }
125 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
126 return LHS < RHS->TheDef->getName() &&
127 !RHS->TheDef->getName().startswith(LHS);
128 }
129 };
130 auto Range = std::equal_range(Instructions.begin(), Instructions.end(),
131 R.first, Comp());
132
133 // For this range we know that it starts with the prefix. Check if there's
134 // a regex that needs to be checked.
135 for (auto *Inst : make_range(Range)) {
136 if (!R.second ||
137 R.second->match(Inst->TheDef->getName().substr(R.first.size())))
Craig Topper8a417c12014-12-09 08:05:51 +0000138 Elts.insert(Inst->TheDef);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000139 }
140 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000141 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000142};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000143
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000144} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000145
Andrew Trick76686492012-09-15 00:19:57 +0000146/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000147CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
148 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000149 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000150
Andrew Trick9e1deb62012-10-03 23:06:32 +0000151 Sets.addFieldExpander("InstRW", "Instrs");
152
153 // Allow Set evaluation to recognize the dags used in InstRW records:
154 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000155 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
156 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000157
Andrew Trick76686492012-09-15 00:19:57 +0000158 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
159 // that are explicitly referenced in tablegen records. Resources associated
160 // with each processor will be derived later. Populate ProcModelMap with the
161 // CodeGenProcModel instances.
162 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000163
Andrew Trick76686492012-09-15 00:19:57 +0000164 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
165 // defined, and populate SchedReads and SchedWrites vectors. Implicit
166 // SchedReadWrites that represent sequences derived from expanded variant will
167 // be inferred later.
168 collectSchedRW();
169
170 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
171 // required by an instruction definition, and populate SchedClassIdxMap. Set
172 // NumItineraryClasses to the number of explicit itinerary classes referenced
173 // by instructions. Set NumInstrSchedClasses to the number of itinerary
174 // classes plus any classes implied by instructions that derive from class
175 // Sched and provide SchedRW list. This does not infer any new classes from
176 // SchedVariant.
177 collectSchedClasses();
178
179 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000180 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000181 // all itinerary classes to be discovered.
182 collectProcItins();
183
184 // Find ItinRW records for each processor and itinerary class.
185 // (For per-operand resources mapped to itinerary classes).
186 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000187
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000188 // Find UnsupportedFeatures records for each processor.
189 // (For per-operand resources mapped to itinerary classes).
190 collectProcUnsupportedFeatures();
191
Andrew Trick33401e82012-09-15 00:19:59 +0000192 // Infer new SchedClasses from SchedVariant.
193 inferSchedClasses();
194
Andrew Trick1e46d482012-09-15 00:20:02 +0000195 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
196 // ProcResourceDefs.
Joel Jones80372332017-06-28 00:06:40 +0000197 DEBUG(dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000198 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000199
200 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000201}
202
Andrew Trick76686492012-09-15 00:19:57 +0000203/// Gather all processor models.
204void CodeGenSchedModels::collectProcModels() {
205 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
206 std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000207
Andrew Trick76686492012-09-15 00:19:57 +0000208 // Reserve space because we can. Reallocation would be ok.
209 ProcModels.reserve(ProcRecords.size()+1);
210
211 // Use idx=0 for NoModel/NoItineraries.
212 Record *NoModelDef = Records.getDef("NoSchedModel");
213 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000214 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000215 ProcModelMap[NoModelDef] = 0;
216
217 // For each processor, find a unique machine model.
Joel Jones80372332017-06-28 00:06:40 +0000218 DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000219 for (Record *ProcRecord : ProcRecords)
220 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000221}
222
223/// Get a unique processor model based on the defined MachineModel and
224/// ProcessorItineraries.
225void CodeGenSchedModels::addProcModel(Record *ProcDef) {
226 Record *ModelKey = getModelOrItinDef(ProcDef);
227 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
228 return;
229
230 std::string Name = ModelKey->getName();
231 if (ModelKey->isSubClassOf("SchedMachineModel")) {
232 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000233 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000234 }
235 else {
236 // An itinerary is defined without a machine model. Infer a new model.
237 if (!ModelKey->getValueAsListOfDefs("IID").empty())
238 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000239 ProcModels.emplace_back(ProcModels.size(), Name,
240 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000241 }
242 DEBUG(ProcModels.back().dump());
243}
244
245// Recursively find all reachable SchedReadWrite records.
246static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
247 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000248 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000249 return;
250 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000251 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000252 if (RWDef->isSubClassOf("WriteSequence")) {
253 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000254 for (Record *WSRec : Seq)
255 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000256 }
257 else if (RWDef->isSubClassOf("SchedVariant")) {
258 // Visit each variant (guarded by a different predicate).
259 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000260 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000261 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000262 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
263 for (Record *SelDef : Selected)
264 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000265 }
266 }
267}
268
269// Collect and sort all SchedReadWrites reachable via tablegen records.
270// More may be inferred later when inferring new SchedClasses from variants.
271void CodeGenSchedModels::collectSchedRW() {
272 // Reserve idx=0 for invalid writes/reads.
273 SchedWrites.resize(1);
274 SchedReads.resize(1);
275
276 SmallPtrSet<Record*, 16> RWSet;
277
278 // Find all SchedReadWrites referenced by instruction defs.
279 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000280 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000281 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000282 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000283 continue;
284 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000285 for (Record *RW : RWs) {
286 if (RW->isSubClassOf("SchedWrite"))
287 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000288 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000289 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
290 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000291 }
292 }
293 }
294 // Find all ReadWrites referenced by InstRW.
295 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000296 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000297 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000298 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
299 for (Record *RWDef : RWDefs) {
300 if (RWDef->isSubClassOf("SchedWrite"))
301 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000302 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000303 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
304 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000305 }
306 }
307 }
308 // Find all ReadWrites referenced by ItinRW.
309 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000310 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000311 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000312 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
313 for (Record *RWDef : RWDefs) {
314 if (RWDef->isSubClassOf("SchedWrite"))
315 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000316 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000317 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
318 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000319 }
320 }
321 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000322 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
323 // for the loop below that initializes Alias vectors.
324 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
325 std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000326 for (Record *ADef : AliasDefs) {
327 Record *MatchDef = ADef->getValueAsDef("MatchRW");
328 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000329 if (MatchDef->isSubClassOf("SchedWrite")) {
330 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000331 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000332 scanSchedRW(AliasDef, SWDefs, RWSet);
333 }
334 else {
335 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
336 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000337 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000338 scanSchedRW(AliasDef, SRDefs, RWSet);
339 }
340 }
Andrew Trick76686492012-09-15 00:19:57 +0000341 // Sort and add the SchedReadWrites directly referenced by instructions or
342 // itinerary resources. Index reads and writes in separate domains.
343 std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000344 for (Record *SWDef : SWDefs) {
345 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
346 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000347 }
348 std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000349 for (Record *SRDef : SRDefs) {
350 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
351 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000352 }
353 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000354 for (CodeGenSchedRW &CGRW : SchedWrites) {
355 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000356 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000357 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000358 /*IsRead=*/false);
359 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000360 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000361 for (Record *ADef : AliasDefs) {
362 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000363 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000364 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000365 CodeGenSchedRW &RW = getSchedRW(MatchDef);
366 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000367 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
368 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000369 }
Andrew Trick76686492012-09-15 00:19:57 +0000370 DEBUG(
Joel Jones80372332017-06-28 00:06:40 +0000371 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
Andrew Trick76686492012-09-15 00:19:57 +0000372 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
373 dbgs() << WIdx << ": ";
374 SchedWrites[WIdx].dump();
375 dbgs() << '\n';
376 }
377 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
378 dbgs() << RIdx << ": ";
379 SchedReads[RIdx].dump();
380 dbgs() << '\n';
381 }
382 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
Javed Absar67b042c2017-09-13 10:31:10 +0000383 for (Record *RWDef : RWDefs) {
384 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
385 const std::string &Name = RWDef->getName();
Andrew Trick76686492012-09-15 00:19:57 +0000386 if (Name != "NoWrite" && Name != "ReadDefault")
Javed Absar67b042c2017-09-13 10:31:10 +0000387 dbgs() << "Unused SchedReadWrite " << RWDef->getName() << '\n';
Andrew Trick76686492012-09-15 00:19:57 +0000388 }
389 });
390}
391
392/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000393std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000394 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000395 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000396 if (I != Seq.begin())
397 Name += '_';
398 Name += getSchedRW(*I, IsRead).Name;
399 }
400 Name += ')';
401 return Name;
402}
403
404unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead,
405 unsigned After) const {
406 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
407 assert(After < RWVec.size() && "start position out of bounds");
408 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin() + After,
409 E = RWVec.end(); I != E; ++I) {
410 if (I->TheDef == Def)
411 return I - RWVec.begin();
412 }
413 return 0;
414}
415
Andrew Trickcfe222c2012-09-19 04:43:19 +0000416bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000417 for (const CodeGenSchedRW &Read : SchedReads) {
418 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000419 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
420 continue;
421
422 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000423 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000424 return true;
425 }
426 }
427 return false;
428}
429
Andrew Trick76686492012-09-15 00:19:57 +0000430namespace llvm {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000431
Andrew Trick76686492012-09-15 00:19:57 +0000432void 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
444} // end namespace llvm
Andrew Trick76686492012-09-15 00:19:57 +0000445
446// Split the SchedReadWrites defs and call findRWs for each list.
447void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
448 IdxVec &Writes, IdxVec &Reads) const {
449 RecVec WriteDefs;
450 RecVec ReadDefs;
451 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
452 findRWs(WriteDefs, Writes, false);
453 findRWs(ReadDefs, Reads, true);
454}
455
456// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
457void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
458 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000459 for (Record *RWDef : RWDefs) {
460 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000461 assert(Idx && "failed to collect SchedReadWrite");
462 RWs.push_back(Idx);
463 }
464}
465
Andrew Trick33401e82012-09-15 00:19:59 +0000466void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
467 bool IsRead) const {
468 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
469 if (!SchedRW.IsSequence) {
470 RWSeq.push_back(RWIdx);
471 return;
472 }
473 int Repeat =
474 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
475 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000476 for (unsigned I : SchedRW.Sequence) {
477 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000478 }
479 }
480}
481
Andrew Trickda984b12012-10-03 23:06:28 +0000482// Expand a SchedWrite as a sequence following any aliases that coincide with
483// the given processor model.
484void CodeGenSchedModels::expandRWSeqForProc(
485 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
486 const CodeGenProcModel &ProcModel) const {
487
488 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000489 Record *AliasDef = nullptr;
Andrew Trickda984b12012-10-03 23:06:28 +0000490 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
491 AI != AE; ++AI) {
492 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
493 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
494 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
495 if (&getProcModel(ModelDef) != &ProcModel)
496 continue;
497 }
498 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000499 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
500 "defined for processor " + ProcModel.ModelName +
501 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000502 AliasDef = AliasRW.TheDef;
503 }
504 if (AliasDef) {
505 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
506 RWSeq, IsRead,ProcModel);
507 return;
508 }
509 if (!SchedWrite.IsSequence) {
510 RWSeq.push_back(RWIdx);
511 return;
512 }
513 int Repeat =
514 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
515 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000516 for (unsigned I : SchedWrite.Sequence) {
517 expandRWSeqForProc(I, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000518 }
519 }
520}
521
Andrew Trick33401e82012-09-15 00:19:59 +0000522// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000523unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000524 bool IsRead) {
525 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
526
527 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
528 I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000529 if (makeArrayRef(I->Sequence) == Seq)
Andrew Trick33401e82012-09-15 00:19:59 +0000530 return I - RWVec.begin();
531 }
532 // Index zero reserved for invalid RW.
533 return 0;
534}
535
536/// Add this ReadWrite if it doesn't already exist.
537unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
538 bool IsRead) {
539 assert(!Seq.empty() && "cannot insert empty sequence");
540 if (Seq.size() == 1)
541 return Seq.back();
542
543 unsigned Idx = findRWForSequence(Seq, IsRead);
544 if (Idx)
545 return Idx;
546
Andrew Trickda984b12012-10-03 23:06:28 +0000547 unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
548 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
549 if (IsRead)
Andrew Trick33401e82012-09-15 00:19:59 +0000550 SchedReads.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000551 else
552 SchedWrites.push_back(SchedRW);
553 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000554}
555
Andrew Trick76686492012-09-15 00:19:57 +0000556/// Visit all the instruction definitions for this target to gather and
557/// enumerate the itinerary classes. These are the explicitly specified
558/// SchedClasses. More SchedClasses may be inferred.
559void CodeGenSchedModels::collectSchedClasses() {
560
561 // NoItinerary is always the first class at Idx=0
Andrew Trick87255e32012-07-07 04:00:00 +0000562 SchedClasses.resize(1);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000563 SchedClasses.back().Index = 0;
564 SchedClasses.back().Name = "NoInstrModel";
565 SchedClasses.back().ItinClassDef = Records.getDef("NoItinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000566 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000567
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000568 // Create a SchedClass for each unique combination of itinerary class and
569 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000570 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000571 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000572 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000573 if (!Inst->TheDef->isValueUnset("SchedRW"))
574 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000575
Andrew Trick76686492012-09-15 00:19:57 +0000576 // ProcIdx == 0 indicates the class applies to all processors.
577 IdxVec ProcIndices(1, 0);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000578
579 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, ProcIndices);
Craig Topper8a417c12014-12-09 08:05:51 +0000580 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000581 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000582 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000583 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
584 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000585 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000586 for (Record *RWDef : InstRWDefs)
587 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000588
Andrew Trick76686492012-09-15 00:19:57 +0000589 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000590
Andrew Trick76686492012-09-15 00:19:57 +0000591 bool EnableDump = false;
592 DEBUG(EnableDump = true);
593 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000594 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000595
Joel Jones80372332017-06-28 00:06:40 +0000596 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000597 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000598 StringRef InstName = Inst->TheDef->getName();
Craig Topper8a417c12014-12-09 08:05:51 +0000599 unsigned SCIdx = InstrClassMap.lookup(Inst->TheDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000600 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000601 if (!Inst->hasNoSchedulingInfo)
602 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000603 continue;
604 }
605 CodeGenSchedClass &SC = getSchedClass(SCIdx);
606 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000607 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000608 "must not be subtarget specific.");
609
610 IdxVec ProcIndices;
611 if (SC.ItinClassDef->getName() != "NoItinerary") {
612 ProcIndices.push_back(0);
613 dbgs() << "Itinerary for " << InstName << ": "
614 << SC.ItinClassDef->getName() << '\n';
615 }
616 if (!SC.Writes.empty()) {
617 ProcIndices.push_back(0);
618 dbgs() << "SchedRW machine model for " << InstName;
619 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
620 dbgs() << " " << SchedWrites[*WI].Name;
621 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
622 dbgs() << " " << SchedReads[*RI].Name;
623 dbgs() << '\n';
624 }
625 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000626 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000627 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000628 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000629 ProcIndices.push_back(ProcModel.Index);
630 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000631 IdxVec Writes;
632 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000633 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000634 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000635 for (unsigned WIdx : Writes)
636 dbgs() << " " << SchedWrites[WIdx].Name;
637 for (unsigned RIdx : Reads)
638 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000639 dbgs() << '\n';
640 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000641 // If ProcIndices contains zero, the class applies to all processors.
642 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
Javed Absar21c75912017-10-09 16:21:25 +0000643 for (const CodeGenProcModel &PM : ProcModels) {
Javed Absarfc500042017-10-05 13:27:43 +0000644 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
Andrew Trickf9df92c92016-10-18 04:17:44 +0000645 dbgs() << "No machine model for " << Inst->TheDef->getName()
Javed Absarfc500042017-10-05 13:27:43 +0000646 << " on processor " << PM.ModelName << '\n';
Andrew Trickf9df92c92016-10-18 04:17:44 +0000647 }
Andrew Trick87255e32012-07-07 04:00:00 +0000648 }
649 }
Andrew Trick76686492012-09-15 00:19:57 +0000650}
651
Andrew Trick76686492012-09-15 00:19:57 +0000652/// Find an SchedClass that has been inferred from a per-operand list of
653/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000654unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000655 ArrayRef<unsigned> Writes,
656 ArrayRef<unsigned> Reads) const {
Andrew Trick76686492012-09-15 00:19:57 +0000657 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000658 if (I->ItinClassDef == ItinClassDef && makeArrayRef(I->Writes) == Writes &&
659 makeArrayRef(I->Reads) == Reads) {
Andrew Trick76686492012-09-15 00:19:57 +0000660 return I - schedClassBegin();
661 }
Andrew Trick87255e32012-07-07 04:00:00 +0000662 }
Andrew Trick76686492012-09-15 00:19:57 +0000663 return 0;
664}
Andrew Trick87255e32012-07-07 04:00:00 +0000665
Andrew Trick76686492012-09-15 00:19:57 +0000666// Get the SchedClass index for an instruction.
667unsigned CodeGenSchedModels::getSchedClassIdx(
668 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000669
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000670 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000671}
672
Benjamin Kramere1761952015-10-24 12:46:49 +0000673std::string
674CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
675 ArrayRef<unsigned> OperWrites,
676 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000677
678 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000679 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
680 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000681 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000682 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000683 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000684 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000685 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000686 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000687 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000688 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000689 }
690 return Name;
691}
692
693std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
694
695 std::string Name;
696 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
697 if (I != InstDefs.begin())
698 Name += '_';
699 Name += (*I)->getName();
700 }
701 return Name;
702}
703
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000704/// Add an inferred sched class from an itinerary class and per-operand list of
705/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
706/// processors that may utilize this class.
707unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000708 ArrayRef<unsigned> OperWrites,
709 ArrayRef<unsigned> OperReads,
710 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000711 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
712
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000713 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
714 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000715 IdxVec PI;
716 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
717 SchedClasses[Idx].ProcIndices.end(),
718 ProcIndices.begin(), ProcIndices.end(),
719 std::back_inserter(PI));
720 SchedClasses[Idx].ProcIndices.swap(PI);
721 return Idx;
722 }
723 Idx = SchedClasses.size();
724 SchedClasses.resize(Idx+1);
725 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000726 SC.Index = Idx;
727 SC.Name = createSchedClassName(ItinClassDef, OperWrites, OperReads);
728 SC.ItinClassDef = ItinClassDef;
Andrew Trick76686492012-09-15 00:19:57 +0000729 SC.Writes = OperWrites;
730 SC.Reads = OperReads;
731 SC.ProcIndices = ProcIndices;
732
733 return Idx;
734}
735
736// Create classes for each set of opcodes that are in the same InstReadWrite
737// definition across all processors.
738void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
739 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
740 // intersects with an existing class via a previous InstRWDef. Instrs that do
741 // not intersect with an existing class refer back to their former class as
742 // determined from ItinDef or SchedRW.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000743 SmallVector<std::pair<unsigned, SmallVector<Record *, 8>>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000744 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000745 const RecVec *InstDefs = Sets.expand(InstRWDef);
746 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000747 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000748
Craig Topper93dd77d2018-03-18 08:38:03 +0000749 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +0000750 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000751 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +0000752 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000753 unsigned SCIdx = Pos->second;
Andrew Trick76686492012-09-15 00:19:57 +0000754 unsigned CIdx = 0, CEnd = ClassInstrs.size();
755 for (; CIdx != CEnd; ++CIdx) {
756 if (ClassInstrs[CIdx].first == SCIdx)
757 break;
758 }
759 if (CIdx == CEnd) {
760 ClassInstrs.resize(CEnd + 1);
761 ClassInstrs[CIdx].first = SCIdx;
762 }
Javed Absarfc500042017-10-05 13:27:43 +0000763 ClassInstrs[CIdx].second.push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +0000764 }
765 // For each set of Instrs, create a new class if necessary, and map or remap
766 // the Instrs to it.
Craig Topper7f31e732018-03-18 08:38:02 +0000767 for (unsigned CIdx = 0, CEnd = ClassInstrs.size(); CIdx != CEnd; ++CIdx) {
Andrew Trick76686492012-09-15 00:19:57 +0000768 unsigned OldSCIdx = ClassInstrs[CIdx].first;
769 ArrayRef<Record*> InstDefs = ClassInstrs[CIdx].second;
770 // If the all instrs in the current class are accounted for, then leave
771 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000772 if (OldSCIdx) {
773 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
774 if (!RWDefs.empty()) {
775 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
776 unsigned OrigNumInstrs = 0;
Craig Topper93dd77d2018-03-18 08:38:03 +0000777 for (Record *OIDef : *OrigInstDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000778 if (InstrClassMap[OIDef] == OldSCIdx)
Andrew Trick78a08512013-06-05 06:55:20 +0000779 ++OrigNumInstrs;
780 }
781 if (OrigNumInstrs == InstDefs.size()) {
782 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
783 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000784 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
785 // Make sure we didn't already have a InstRW containing this
786 // instruction on this model.
787 for (Record *RWD : RWDefs) {
788 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
789 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
790 for (Record *Inst : InstDefs) {
791 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
792 Inst->getName() + " also matches " +
793 RWD->getValue("Instrs")->getValue()->getAsString());
794 }
795 }
796 }
Andrew Trick78a08512013-06-05 06:55:20 +0000797 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
798 << SchedClasses[OldSCIdx].Name << " on "
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000799 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +0000800 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
801 continue;
802 }
803 }
Andrew Trick76686492012-09-15 00:19:57 +0000804 }
805 unsigned SCIdx = SchedClasses.size();
806 SchedClasses.resize(SCIdx+1);
807 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000808 SC.Index = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000809 SC.Name = createSchedClassName(InstDefs);
Andrew Trick78a08512013-06-05 06:55:20 +0000810 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
811 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
812
Andrew Trick76686492012-09-15 00:19:57 +0000813 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
814 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
815 SC.Writes = SchedClasses[OldSCIdx].Writes;
816 SC.Reads = SchedClasses[OldSCIdx].Reads;
817 SC.ProcIndices.push_back(0);
818 // Map each Instr to this new class.
819 // Note that InstDefs may be a smaller list than InstRWDef's "Instrs".
Andrew Trick9e1deb62012-10-03 23:06:32 +0000820 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
821 SmallSet<unsigned, 4> RemappedClassIDs;
Andrew Trick76686492012-09-15 00:19:57 +0000822 for (ArrayRef<Record*>::const_iterator
823 II = InstDefs.begin(), IE = InstDefs.end(); II != IE; ++II) {
824 unsigned OldSCIdx = InstrClassMap[*II];
David Blaikie70573dc2014-11-19 07:49:26 +0000825 if (OldSCIdx && RemappedClassIDs.insert(OldSCIdx).second) {
Andrew Trick9e1deb62012-10-03 23:06:32 +0000826 for (RecIter RI = SchedClasses[OldSCIdx].InstRWs.begin(),
827 RE = SchedClasses[OldSCIdx].InstRWs.end(); RI != RE; ++RI) {
828 if ((*RI)->getValueAsDef("SchedModel") == RWModelDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000829 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
Andrew Trick9e1deb62012-10-03 23:06:32 +0000830 (*II)->getName() + " also matches " +
831 (*RI)->getValue("Instrs")->getValue()->getAsString());
832 }
833 assert(*RI != InstRWDef && "SchedClass has duplicate InstRW def");
834 SC.InstRWs.push_back(*RI);
835 }
Andrew Trick76686492012-09-15 00:19:57 +0000836 }
837 InstrClassMap[*II] = SCIdx;
838 }
839 SC.InstRWs.push_back(InstRWDef);
840 }
Andrew Trick87255e32012-07-07 04:00:00 +0000841}
842
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000843// True if collectProcItins found anything.
844bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000845 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
846 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000847 return true;
848 }
849 return false;
850}
851
Andrew Trick87255e32012-07-07 04:00:00 +0000852// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000853void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000854 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000855 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000856 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000857 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000858
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000859 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
860 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
861
862 // Populate ItinDefList with Itinerary records.
863 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000864
865 // Insert each itinerary data record in the correct position within
866 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +0000867 for (Record *ItinData : ItinRecords) {
Andrew Trick76686492012-09-15 00:19:57 +0000868 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000869 bool FoundClass = false;
870 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
871 SCI != SCE; ++SCI) {
872 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000873 if (SCI->ItinClassDef == ItinDef) {
874 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000875 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000876 }
Andrew Trick76686492012-09-15 00:19:57 +0000877 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000878 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000879 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
880 << " missing class for itinerary " << ItinDef->getName() << '\n');
881 }
Andrew Trick87255e32012-07-07 04:00:00 +0000882 }
Andrew Trick76686492012-09-15 00:19:57 +0000883 // Check for missing itinerary entries.
884 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
885 DEBUG(
886 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
887 if (!ProcModel.ItinDefList[i])
888 dbgs() << ProcModel.ItinsDef->getName()
889 << " missing itinerary for class "
890 << SchedClasses[i].Name << '\n';
891 });
Andrew Trick87255e32012-07-07 04:00:00 +0000892 }
Andrew Trick87255e32012-07-07 04:00:00 +0000893}
Andrew Trick76686492012-09-15 00:19:57 +0000894
895// Gather the read/write types for each itinerary class.
896void CodeGenSchedModels::collectProcItinRW() {
897 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
898 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +0000899 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000900 if (!RWDef->getValueInit("SchedModel")->isComplete())
901 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
902 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +0000903 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
904 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000905 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000906 + ModelDef->getName());
907 }
Javed Absarf45d0b92017-10-08 17:23:30 +0000908 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000909 }
910}
911
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000912// Gather the unsupported features for processor models.
913void CodeGenSchedModels::collectProcUnsupportedFeatures() {
914 for (CodeGenProcModel &ProcModel : ProcModels) {
915 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
916 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
917 }
918 }
919}
920
Andrew Trick33401e82012-09-15 00:19:59 +0000921/// Infer new classes from existing classes. In the process, this may create new
922/// SchedWrites from sequences of existing SchedWrites.
923void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000924 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000925 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
926
Andrew Trick33401e82012-09-15 00:19:59 +0000927 // Visit all existing classes and newly created classes.
928 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000929 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
930
Andrew Trick33401e82012-09-15 00:19:59 +0000931 if (SchedClasses[Idx].ItinClassDef)
932 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000933 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000934 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000935 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000936 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
937 Idx, SchedClasses[Idx].ProcIndices);
938 }
939 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
940 "too many SchedVariants");
941 }
942}
943
944/// Infer classes from per-processor itinerary resources.
945void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
946 unsigned FromClassIdx) {
947 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
948 const CodeGenProcModel &PM = ProcModels[PIdx];
949 // For all ItinRW entries.
950 bool HasMatch = false;
951 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
952 II != IE; ++II) {
953 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
954 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
955 continue;
956 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000957 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000958 + ItinClassDef->getName()
959 + " in ItinResources for " + PM.ModelName);
960 HasMatch = true;
961 IdxVec Writes, Reads;
962 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
963 IdxVec ProcIndices(1, PIdx);
964 inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
965 }
966 }
967}
968
969/// Infer classes from per-processor InstReadWrite definitions.
970void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000971 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000972 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000973 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
974 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000975 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000976 for (; II != IE; ++II) {
977 if (InstrClassMap[*II] == SCIdx)
978 break;
979 }
980 // If this class no longer has any instructions mapped to it, it has become
981 // irrelevant.
982 if (II == IE)
983 continue;
984 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000985 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
986 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Andrew Trick33401e82012-09-15 00:19:59 +0000987 IdxVec ProcIndices(1, PIdx);
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000988 inferFromRW(Writes, Reads, SCIdx, ProcIndices); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +0000989 }
990}
991
992namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000993
Andrew Trick9257b8f2012-09-22 02:24:21 +0000994// Helper for substituteVariantOperand.
995struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +0000996 Record *VarOrSeqDef; // Variant or sequence.
997 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000998 unsigned ProcIdx; // Processor model index or zero for any.
999 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
1000
1001 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +00001002 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +00001003};
1004
Andrew Trick33401e82012-09-15 00:19:59 +00001005// Associate a predicate with the SchedReadWrite that it guards.
1006// RWIdx is the index of the read/write variant.
1007struct PredCheck {
1008 bool IsRead;
1009 unsigned RWIdx;
1010 Record *Predicate;
1011
1012 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
1013};
1014
1015// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1016struct PredTransition {
1017 // A predicate term is a conjunction of PredChecks.
1018 SmallVector<PredCheck, 4> PredTerm;
1019 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1020 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001021 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001022};
1023
1024// Encapsulate a set of partially constructed transitions.
1025// The results are built by repeated calls to substituteVariants.
1026class PredTransitions {
1027 CodeGenSchedModels &SchedModels;
1028
1029public:
1030 std::vector<PredTransition> TransVec;
1031
1032 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1033
1034 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1035 bool IsRead, unsigned StartIdx);
1036
1037 void substituteVariants(const PredTransition &Trans);
1038
1039#ifndef NDEBUG
1040 void dump() const;
1041#endif
1042
1043private:
1044 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001045 void getIntersectingVariants(
1046 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1047 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001048 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001049};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001050
1051} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001052
1053// Return true if this predicate is mutually exclusive with a PredTerm. This
1054// degenerates into checking if the predicate is mutually exclusive with any
1055// predicate in the Term's conjunction.
1056//
1057// All predicates associated with a given SchedRW are considered mutually
1058// exclusive. This should work even if the conditions expressed by the
1059// predicates are not exclusive because the predicates for a given SchedWrite
1060// are always checked in the order they are defined in the .td file. Later
1061// conditions implicitly negate any prior condition.
1062bool PredTransitions::mutuallyExclusive(Record *PredDef,
1063 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001064 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001065 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001066 return false;
1067
Javed Absarfc500042017-10-05 13:27:43 +00001068 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001069 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1070 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1071 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1072 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1073 return true;
1074 }
1075 }
1076 return false;
1077}
1078
Andrew Trickda984b12012-10-03 23:06:28 +00001079static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1080 CodeGenSchedModels &SchedModels) {
1081 if (RW.HasVariants)
1082 return true;
1083
Javed Absar21c75912017-10-09 16:21:25 +00001084 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001085 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001086 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001087 if (AliasRW.HasVariants)
1088 return true;
1089 if (AliasRW.IsSequence) {
1090 IdxVec ExpandedRWs;
1091 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1092 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1093 SI != SE; ++SI) {
1094 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1095 SchedModels)) {
1096 return true;
1097 }
1098 }
1099 }
1100 }
1101 return false;
1102}
1103
1104static bool hasVariant(ArrayRef<PredTransition> Transitions,
1105 CodeGenSchedModels &SchedModels) {
1106 for (ArrayRef<PredTransition>::iterator
1107 PTI = Transitions.begin(), PTE = Transitions.end();
1108 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001109 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001110 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1111 WSI != WSE; ++WSI) {
1112 for (SmallVectorImpl<unsigned>::const_iterator
1113 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1114 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1115 return true;
1116 }
1117 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001118 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001119 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1120 RSI != RSE; ++RSI) {
1121 for (SmallVectorImpl<unsigned>::const_iterator
1122 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1123 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1124 return true;
1125 }
1126 }
1127 }
1128 return false;
1129}
1130
1131// Populate IntersectingVariants with any variants or aliased sequences of the
1132// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001133// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001134void PredTransitions::getIntersectingVariants(
1135 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1136 std::vector<TransVariant> &IntersectingVariants) {
1137
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001138 bool GenericRW = false;
1139
Andrew Trickda984b12012-10-03 23:06:28 +00001140 std::vector<TransVariant> Variants;
1141 if (SchedRW.HasVariants) {
1142 unsigned VarProcIdx = 0;
1143 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1144 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1145 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1146 }
1147 // Push each variant. Assign TransVecIdx later.
1148 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001149 for (Record *VarDef : VarDefs)
1150 Variants.push_back(TransVariant(VarDef, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001151 if (VarProcIdx == 0)
1152 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001153 }
1154 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1155 AI != AE; ++AI) {
1156 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1157 // to is defined within a processor model, constrain all variants to
1158 // that processor.
1159 unsigned AliasProcIdx = 0;
1160 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1161 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1162 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1163 }
1164 const CodeGenSchedRW &AliasRW =
1165 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1166
1167 if (AliasRW.HasVariants) {
1168 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001169 for (Record *VD : VarDefs)
1170 Variants.push_back(TransVariant(VD, AliasRW.Index, AliasProcIdx, 0));
Andrew Trickda984b12012-10-03 23:06:28 +00001171 }
1172 if (AliasRW.IsSequence) {
1173 Variants.push_back(
1174 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1175 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001176 if (AliasProcIdx == 0)
1177 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001178 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001179 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001180 // Don't expand variants if the processor models don't intersect.
1181 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001182 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001183 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001184 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1185 Variant.ProcIdx);
1186 if (!Cnt)
1187 continue;
1188 if (Cnt > 1) {
1189 const CodeGenProcModel &PM =
1190 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001191 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1192 "Multiple variants defined for processor " +
1193 PM.ModelName +
1194 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001195 }
1196 }
1197 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1198 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1199 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1200 continue;
1201 }
1202 if (IntersectingVariants.empty()) {
1203 // The first variant builds on the existing transition.
1204 Variant.TransVecIdx = TransIdx;
1205 IntersectingVariants.push_back(Variant);
1206 }
1207 else {
1208 // Push another copy of the current transition for more variants.
1209 Variant.TransVecIdx = TransVec.size();
1210 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001211 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001212 }
1213 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001214 if (GenericRW && IntersectingVariants.empty()) {
1215 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1216 "a matching predicate on any processor");
1217 }
Andrew Trickda984b12012-10-03 23:06:28 +00001218}
1219
Andrew Trick9257b8f2012-09-22 02:24:21 +00001220// Push the Reads/Writes selected by this variant onto the PredTransition
1221// specified by VInfo.
1222void PredTransitions::
1223pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001224 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1225
Andrew Trick9257b8f2012-09-22 02:24:21 +00001226 // If this operand transition is reached through a processor-specific alias,
1227 // then the whole transition is specific to this processor.
1228 if (VInfo.ProcIdx != 0)
1229 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1230
Andrew Trick33401e82012-09-15 00:19:59 +00001231 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001232 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1233 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1234 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1235 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1236 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1237 }
1238 else {
1239 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1240 "variant must be a SchedVariant or aliased WriteSequence");
1241 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1242 }
Andrew Trick33401e82012-09-15 00:19:59 +00001243
Andrew Trick9257b8f2012-09-22 02:24:21 +00001244 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001245
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001246 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001247 ? Trans.ReadSequences : Trans.WriteSequences;
1248 if (SchedRW.IsVariadic) {
1249 unsigned OperIdx = RWSequences.size()-1;
1250 // Make N-1 copies of this transition's last sequence.
1251 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001252 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001253 RWSequences.reserve(RWSequences.size() + 1);
1254 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001255 }
1256 // Push each of the N elements of the SelectedRWs onto a copy of the last
1257 // sequence (split the current operand into N operands).
1258 // Note that write sequences should be expanded within this loop--the entire
1259 // sequence belongs to a single operand.
1260 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1261 RWI != RWE; ++RWI, ++OperIdx) {
1262 IdxVec ExpandedRWs;
1263 if (IsRead)
1264 ExpandedRWs.push_back(*RWI);
1265 else
1266 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1267 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1268 ExpandedRWs.begin(), ExpandedRWs.end());
1269 }
1270 assert(OperIdx == RWSequences.size() && "missed a sequence");
1271 }
1272 else {
1273 // Push this transition's expanded sequence onto this transition's last
1274 // sequence (add to the current operand's sequence).
1275 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1276 IdxVec ExpandedRWs;
1277 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1278 RWI != RWE; ++RWI) {
1279 if (IsRead)
1280 ExpandedRWs.push_back(*RWI);
1281 else
1282 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1283 }
1284 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1285 }
1286}
1287
1288// RWSeq is a sequence of all Reads or all Writes for the next read or write
1289// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001290// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001291// of TransVec.
1292void PredTransitions::substituteVariantOperand(
1293 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1294
1295 // Visit each original RW within the current sequence.
1296 for (SmallVectorImpl<unsigned>::const_iterator
1297 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1298 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1299 // Push this RW on all partial PredTransitions or distribute variants.
1300 // New PredTransitions may be pushed within this loop which should not be
1301 // revisited (TransEnd must be loop invariant).
1302 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1303 TransIdx != TransEnd; ++TransIdx) {
1304 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001305 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001306 if (IsRead)
1307 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1308 else
1309 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1310 continue;
1311 }
1312 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001313 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001314 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001315 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001316 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001317 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001318 IVI = IntersectingVariants.begin(),
1319 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001320 IVI != IVE; ++IVI) {
1321 pushVariant(*IVI, IsRead);
1322 }
Andrew Trick33401e82012-09-15 00:19:59 +00001323 }
1324 }
1325}
1326
1327// For each variant of a Read/Write in Trans, substitute the sequence of
1328// Read/Writes guarded by the variant. This is exponential in the number of
1329// variant Read/Writes, but in practice detection of mutually exclusive
1330// predicates should result in linear growth in the total number variants.
1331//
1332// This is one step in a breadth-first search of nested variants.
1333void PredTransitions::substituteVariants(const PredTransition &Trans) {
1334 // Build up a set of partial results starting at the back of
1335 // PredTransitions. Remember the first new transition.
1336 unsigned StartIdx = TransVec.size();
1337 TransVec.resize(TransVec.size() + 1);
1338 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001339 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001340
1341 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001342 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001343 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1344 WSI != WSE; ++WSI) {
1345 // Push a new (empty) write sequence onto all partial Transitions.
1346 for (std::vector<PredTransition>::iterator I =
1347 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
1348 I->WriteSequences.resize(I->WriteSequences.size() + 1);
1349 }
1350 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1351 }
1352 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001353 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001354 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1355 RSI != RSE; ++RSI) {
1356 // Push a new (empty) read sequence onto all partial Transitions.
1357 for (std::vector<PredTransition>::iterator I =
1358 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
1359 I->ReadSequences.resize(I->ReadSequences.size() + 1);
1360 }
1361 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1362 }
1363}
1364
Andrew Trick33401e82012-09-15 00:19:59 +00001365// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001366static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001367 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001368 CodeGenSchedModels &SchedModels) {
1369 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1370 // requires creating a new SchedClass.
1371 for (ArrayRef<PredTransition>::iterator
1372 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1373 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001374 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1375 [&SchedModels](ArrayRef<unsigned> WS) {
1376 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1377 });
Andrew Trick33401e82012-09-15 00:19:59 +00001378 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001379 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1380 [&SchedModels](ArrayRef<unsigned> RS) {
1381 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1382 });
Andrew Trick9257b8f2012-09-22 02:24:21 +00001383 IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001384 CodeGenSchedTransition SCTrans;
1385 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001386 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001387 OperReadsVariant, ProcIndices);
Andrew Trick33401e82012-09-15 00:19:59 +00001388 SCTrans.ProcIndices = ProcIndices;
1389 // The final PredTerm is unique set of predicates guarding the transition.
1390 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001391 transform(I->PredTerm, std::back_inserter(Preds),
1392 [](const PredCheck &P) {
1393 return P.Predicate;
1394 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001395 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001396 SCTrans.PredTerm = Preds;
1397 SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
1398 }
1399}
1400
Andrew Trick9257b8f2012-09-22 02:24:21 +00001401// Create new SchedClasses for the given ReadWrite list. If any of the
1402// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1403// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001404void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1405 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001406 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001407 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001408 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001409
1410 // Create a seed transition with an empty PredTerm and the expanded sequences
1411 // of SchedWrites for the current SchedClass.
1412 std::vector<PredTransition> LastTransitions;
1413 LastTransitions.resize(1);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001414 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1415 ProcIndices.end());
1416
Benjamin Kramere1761952015-10-24 12:46:49 +00001417 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001418 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001419 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Andrew Trick33401e82012-09-15 00:19:59 +00001420 unsigned Idx = LastTransitions[0].WriteSequences.size();
1421 LastTransitions[0].WriteSequences.resize(Idx + 1);
1422 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences[Idx];
Craig Topper1f57456c2018-03-20 20:24:14 +00001423 Seq.append(WriteSeq.begin(), WriteSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001424 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1425 }
1426 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001427 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001428 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001429 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Andrew Trick33401e82012-09-15 00:19:59 +00001430 unsigned Idx = LastTransitions[0].ReadSequences.size();
1431 LastTransitions[0].ReadSequences.resize(Idx + 1);
1432 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences[Idx];
Craig Topper1f57456c2018-03-20 20:24:14 +00001433 Seq.append(ReadSeq.begin(), ReadSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001434 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1435 }
1436 DEBUG(dbgs() << '\n');
1437
1438 // Collect all PredTransitions for individual operands.
1439 // Iterate until no variant writes remain.
1440 while (hasVariant(LastTransitions, *this)) {
1441 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001442 for (const PredTransition &Trans : LastTransitions)
1443 Transitions.substituteVariants(Trans);
Andrew Trick33401e82012-09-15 00:19:59 +00001444 DEBUG(Transitions.dump());
1445 LastTransitions.swap(Transitions.TransVec);
1446 }
1447 // If the first transition has no variants, nothing to do.
1448 if (LastTransitions[0].PredTerm.empty())
1449 return;
1450
1451 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1452 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001453 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001454}
1455
Andrew Trickcf398b22013-04-23 23:45:14 +00001456// Check if any processor resource group contains all resource records in
1457// SubUnits.
1458bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1459 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1460 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1461 continue;
1462 RecVec SuperUnits =
1463 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1464 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1465 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001466 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001467 break;
1468 }
1469 }
1470 if (RI == RE)
1471 return true;
1472 }
1473 return false;
1474}
1475
1476// Verify that overlapping groups have a common supergroup.
1477void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1478 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1479 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1480 continue;
1481 RecVec CheckUnits =
1482 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1483 for (unsigned j = i+1; j < e; ++j) {
1484 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1485 continue;
1486 RecVec OtherUnits =
1487 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1488 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1489 OtherUnits.begin(), OtherUnits.end())
1490 != CheckUnits.end()) {
1491 // CheckUnits and OtherUnits overlap
1492 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1493 CheckUnits.end());
1494 if (!hasSuperGroup(OtherUnits, PM)) {
1495 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1496 "proc resource group overlaps with "
1497 + PM.ProcResourceDefs[j]->getName()
1498 + " but no supergroup contains both.");
1499 }
1500 }
1501 }
1502 }
1503}
1504
Andrew Trick1e46d482012-09-15 00:20:02 +00001505// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1506void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001507 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1508 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1509
Andrew Trick1e46d482012-09-15 00:20:02 +00001510 // Add any subtarget-specific SchedReadWrites that are directly associated
1511 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1512 // determine which processors they apply to.
1513 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1514 SCI != SCE; ++SCI) {
1515 if (SCI->ItinClassDef)
1516 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001517 else {
1518 // This class may have a default ReadWrite list which can be overriden by
1519 // InstRW definitions.
1520 if (!SCI->InstRWs.empty()) {
1521 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1522 RWI != RWE; ++RWI) {
1523 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
1524 IdxVec ProcIndices(1, getProcModel(RWModelDef).Index);
1525 IdxVec Writes, Reads;
1526 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1527 Writes, Reads);
1528 collectRWResources(Writes, Reads, ProcIndices);
1529 }
1530 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001531 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001532 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001533 }
1534 // Add resources separately defined by each subtarget.
1535 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001536 for (Record *WR : WRDefs) {
1537 Record *ModelDef = WR->getValueAsDef("SchedModel");
1538 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001539 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001540 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001541 for (Record *SWR : SWRDefs) {
1542 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1543 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001544 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001545 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001546 for (Record *RA : RADefs) {
1547 Record *ModelDef = RA->getValueAsDef("SchedModel");
1548 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001549 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001550 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001551 for (Record *SRA : SRADefs) {
1552 if (SRA->getValueInit("SchedModel")->isComplete()) {
1553 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1554 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001555 }
1556 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001557 // Add ProcResGroups that are defined within this processor model, which may
1558 // not be directly referenced but may directly specify a buffer size.
1559 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001560 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001561 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001562 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001563 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1564 if (!is_contained(PM.ProcResourceDefs, PRG))
1565 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001566 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001567 // Add ProcResourceUnits unconditionally.
1568 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1569 if (!PRU->getValueInit("SchedModel")->isComplete())
1570 continue;
1571 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1572 if (!is_contained(PM.ProcResourceDefs, PRU))
1573 PM.ProcResourceDefs.push_back(PRU);
1574 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001575 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001576 for (CodeGenProcModel &PM : ProcModels) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001577 std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1578 LessRecord());
1579 std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1580 LessRecord());
1581 std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1582 LessRecord());
1583 DEBUG(
1584 PM.dump();
1585 dbgs() << "WriteResDefs: ";
1586 for (RecIter RI = PM.WriteResDefs.begin(),
1587 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1588 if ((*RI)->isSubClassOf("WriteRes"))
1589 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1590 else
1591 dbgs() << (*RI)->getName() << " ";
1592 }
1593 dbgs() << "\nReadAdvanceDefs: ";
1594 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1595 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1596 if ((*RI)->isSubClassOf("ReadAdvance"))
1597 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1598 else
1599 dbgs() << (*RI)->getName() << " ";
1600 }
1601 dbgs() << "\nProcResourceDefs: ";
1602 for (RecIter RI = PM.ProcResourceDefs.begin(),
1603 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1604 dbgs() << (*RI)->getName() << " ";
1605 }
1606 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001607 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001608 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001609
1610 ProcResourceDefs.clear();
1611 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001612}
1613
Matthias Braun17cb5792016-03-01 20:03:21 +00001614void CodeGenSchedModels::checkCompleteness() {
1615 bool Complete = true;
1616 bool HadCompleteModel = false;
1617 for (const CodeGenProcModel &ProcModel : procModels()) {
Matthias Braun17cb5792016-03-01 20:03:21 +00001618 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1619 continue;
1620 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1621 if (Inst->hasNoSchedulingInfo)
1622 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001623 if (ProcModel.isUnsupported(*Inst))
1624 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001625 unsigned SCIdx = getSchedClassIdx(*Inst);
1626 if (!SCIdx) {
1627 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1628 PrintError("No schedule information for instruction '"
1629 + Inst->TheDef->getName() + "'");
1630 Complete = false;
1631 }
1632 continue;
1633 }
1634
1635 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1636 if (!SC.Writes.empty())
1637 continue;
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001638 if (SC.ItinClassDef != nullptr &&
1639 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001640 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001641
1642 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001643 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1644 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1645 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001646 if (I == InstRWs.end()) {
1647 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1648 Inst->TheDef->getName() + "'");
1649 Complete = false;
1650 }
1651 }
1652 HadCompleteModel = true;
1653 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001654 if (!Complete) {
1655 errs() << "\n\nIncomplete schedule models found.\n"
1656 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1657 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1658 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001659 "you may temporarily use an empty list.\n"
1660 << "- Instructions related to unsupported features can be excluded with "
1661 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1662 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001663 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001664 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001665}
1666
Andrew Trick1e46d482012-09-15 00:20:02 +00001667// Collect itinerary class resources for each processor.
1668void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1669 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1670 const CodeGenProcModel &PM = ProcModels[PIdx];
1671 // For all ItinRW entries.
1672 bool HasMatch = false;
1673 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1674 II != IE; ++II) {
1675 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1676 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1677 continue;
1678 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001679 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1680 + ItinClassDef->getName()
1681 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001682 HasMatch = true;
1683 IdxVec Writes, Reads;
1684 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1685 IdxVec ProcIndices(1, PIdx);
1686 collectRWResources(Writes, Reads, ProcIndices);
1687 }
1688 }
1689}
1690
Andrew Trickd0b9c442012-10-10 05:43:13 +00001691void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001692 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001693 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1694 if (SchedRW.TheDef) {
1695 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001696 for (unsigned Idx : ProcIndices)
1697 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001698 }
1699 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001700 for (unsigned Idx : ProcIndices)
1701 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001702 }
1703 }
1704 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1705 AI != AE; ++AI) {
1706 IdxVec AliasProcIndices;
1707 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1708 AliasProcIndices.push_back(
1709 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1710 }
1711 else
1712 AliasProcIndices = ProcIndices;
1713 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1714 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1715
1716 IdxVec ExpandedRWs;
1717 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1718 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1719 SI != SE; ++SI) {
1720 collectRWResources(*SI, IsRead, AliasProcIndices);
1721 }
1722 }
1723}
Andrew Trick1e46d482012-09-15 00:20:02 +00001724
1725// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001726void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1727 ArrayRef<unsigned> Reads,
1728 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001729 for (unsigned Idx : Writes)
1730 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001731
Benjamin Kramere1761952015-10-24 12:46:49 +00001732 for (unsigned Idx : Reads)
1733 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001734}
1735
1736// Find the processor's resource units for this kind of resource.
1737Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001738 const CodeGenProcModel &PM,
1739 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00001740 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1741 return ProcResKind;
1742
Craig Topper24064772014-04-15 07:20:03 +00001743 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001744 assert(!ProcResourceDefs.empty());
1745 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001746
Javed Absar67b042c2017-09-13 10:31:10 +00001747 for (Record *ProcResDef : ProcResourceDefs) {
1748 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1749 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001750 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001751 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001752 "Multiple ProcessorResourceUnits associated with "
1753 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001754 }
Javed Absar67b042c2017-09-13 10:31:10 +00001755 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001756 }
1757 }
Javed Absar67b042c2017-09-13 10:31:10 +00001758 for (Record *ProcResGroup : ProcResGroups) {
1759 if (ProcResGroup == ProcResKind
1760 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001761 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001762 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00001763 "Multiple ProcessorResourceUnits associated with "
1764 + ProcResKind->getName());
1765 }
Javed Absar67b042c2017-09-13 10:31:10 +00001766 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001767 }
1768 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001769 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001770 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001771 "No ProcessorResources associated with "
1772 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001773 }
1774 return ProcUnitDef;
1775}
1776
1777// Iteratively add a resource and its super resources.
1778void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001779 CodeGenProcModel &PM,
1780 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001781 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001782 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00001783
1784 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001785 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001786 return;
1787
1788 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001789 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1790 return;
1791
Andrew Trick1e46d482012-09-15 00:20:02 +00001792 if (!ProcResUnits->getValueInit("Super")->isComplete())
1793 return;
1794
1795 ProcResKind = ProcResUnits->getValueAsDef("Super");
1796 }
1797}
1798
1799// Add resources for a SchedWrite to this processor if they don't exist.
1800void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001801 assert(PIdx && "don't add resources to an invalid Processor model");
1802
Andrew Trick1e46d482012-09-15 00:20:02 +00001803 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001804 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001805 return;
1806 WRDefs.push_back(ProcWriteResDef);
1807
1808 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1809 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1810 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1811 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001812 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00001813 }
1814}
1815
1816// Add resources for a ReadAdvance to this processor if they don't exist.
1817void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1818 unsigned PIdx) {
1819 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001820 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001821 return;
1822 RADefs.push_back(ProcReadAdvanceDef);
1823}
1824
Andrew Trick8fa00f52012-09-17 22:18:43 +00001825unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001826 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001827 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001828 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1829 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001830 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001831 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001832}
1833
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001834bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1835 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1836 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1837 if (TheDef->getName() == PredDef->getName())
1838 return true;
1839 }
1840 }
1841 return false;
1842}
1843
Andrew Trick76686492012-09-15 00:19:57 +00001844#ifndef NDEBUG
1845void CodeGenProcModel::dump() const {
1846 dbgs() << Index << ": " << ModelName << " "
1847 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1848 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1849}
1850
1851void CodeGenSchedRW::dump() const {
1852 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1853 if (IsSequence) {
1854 dbgs() << "(";
1855 dumpIdxVec(Sequence);
1856 dbgs() << ")";
1857 }
1858}
1859
1860void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001861 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001862 << " Writes: ";
1863 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1864 SchedModels->getSchedWrite(Writes[i]).dump();
1865 if (i < N-1) {
1866 dbgs() << '\n';
1867 dbgs().indent(10);
1868 }
1869 }
1870 dbgs() << "\n Reads: ";
1871 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1872 SchedModels->getSchedRead(Reads[i]).dump();
1873 if (i < N-1) {
1874 dbgs() << '\n';
1875 dbgs().indent(10);
1876 }
1877 }
1878 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001879 if (!Transitions.empty()) {
1880 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001881 for (const CodeGenSchedTransition &Transition : Transitions) {
1882 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001883 }
1884 }
Andrew Trick76686492012-09-15 00:19:57 +00001885}
Andrew Trick33401e82012-09-15 00:19:59 +00001886
1887void PredTransitions::dump() const {
1888 dbgs() << "Expanded Variants:\n";
1889 for (std::vector<PredTransition>::const_iterator
1890 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1891 dbgs() << "{";
1892 for (SmallVectorImpl<PredCheck>::const_iterator
1893 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1894 PCI != PCE; ++PCI) {
1895 if (PCI != TI->PredTerm.begin())
1896 dbgs() << ", ";
1897 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1898 << ":" << PCI->Predicate->getName();
1899 }
1900 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001901 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001902 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1903 WSI != WSE; ++WSI) {
1904 dbgs() << "(";
1905 for (SmallVectorImpl<unsigned>::const_iterator
1906 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1907 if (WI != WSI->begin())
1908 dbgs() << ", ";
1909 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1910 }
1911 dbgs() << "),";
1912 }
1913 dbgs() << "}\n";
1914 }
1915}
Andrew Trick76686492012-09-15 00:19:57 +00001916#endif // NDEBUG