blob: f1172828fab169d0658bf387fb7f84f31ee51de3 [file] [log] [blame]
Andrew Trick87255e32012-07-07 04:00:00 +00001//===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alp Tokercb402912014-01-24 17:20:08 +000010// This file defines structures to encapsulate the machine model as described in
Andrew Trick87255e32012-07-07 04:00:00 +000011// the target description.
12//
13//===----------------------------------------------------------------------===//
14
Andrew Trick87255e32012-07-07 04:00:00 +000015#include "CodeGenSchedule.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000016#include "CodeGenInstruction.h"
Andrew Trick87255e32012-07-07 04:00:00 +000017#include "CodeGenTarget.h"
Craig Topperf19eacf2018-03-21 02:48:34 +000018#include "llvm/ADT/MapVector.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000019#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000023#include "llvm/Support/Casting.h"
Andrew Trick87255e32012-07-07 04:00:00 +000024#include "llvm/Support/Debug.h"
Andrew Trick9e1deb62012-10-03 23:06:32 +000025#include "llvm/Support/Regex.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000026#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000027#include "llvm/TableGen/Error.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000028#include <algorithm>
29#include <iterator>
30#include <utility>
Andrew Trick87255e32012-07-07 04:00:00 +000031
32using namespace llvm;
33
Chandler Carruth97acce22014-04-22 03:06:00 +000034#define DEBUG_TYPE "subtarget-emitter"
35
Andrew Trick76686492012-09-15 00:19:57 +000036#ifndef NDEBUG
Benjamin Kramere1761952015-10-24 12:46:49 +000037static void dumpIdxVec(ArrayRef<unsigned> V) {
38 for (unsigned Idx : V)
39 dbgs() << Idx << ", ";
Andrew Trick33401e82012-09-15 00:19:59 +000040}
Andrew Trick76686492012-09-15 00:19:57 +000041#endif
42
Juergen Ributzka05c5a932013-11-19 03:08:35 +000043namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000044
Andrew Trick9e1deb62012-10-03 23:06:32 +000045// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
46struct InstrsOp : public SetTheory::Operator {
Craig Topper716b0732014-03-05 05:17:42 +000047 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
48 ArrayRef<SMLoc> Loc) override {
Juergen Ributzka05c5a932013-11-19 03:08:35 +000049 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
50 }
51};
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000052
Andrew Trick9e1deb62012-10-03 23:06:32 +000053// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
Andrew Trick9e1deb62012-10-03 23:06:32 +000054struct InstRegexOp : public SetTheory::Operator {
55 const CodeGenTarget &Target;
56 InstRegexOp(const CodeGenTarget &t): Target(t) {}
57
Benjamin Kramercbce2f02018-01-23 23:05:04 +000058 /// Remove any text inside of parentheses from S.
59 static std::string removeParens(llvm::StringRef S) {
60 std::string Result;
61 unsigned Paren = 0;
62 // NB: We don't care about escaped parens here.
63 for (char C : S) {
64 switch (C) {
65 case '(':
66 ++Paren;
67 break;
68 case ')':
69 --Paren;
70 break;
71 default:
72 if (Paren == 0)
73 Result += C;
74 }
75 }
76 return Result;
77 }
78
Juergen Ributzka05c5a932013-11-19 03:08:35 +000079 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
Craig Topper716b0732014-03-05 05:17:42 +000080 ArrayRef<SMLoc> Loc) override {
Javed Absarfc500042017-10-05 13:27:43 +000081 for (Init *Arg : make_range(Expr->arg_begin(), Expr->arg_end())) {
82 StringInit *SI = dyn_cast<StringInit>(Arg);
Juergen Ributzka05c5a932013-11-19 03:08:35 +000083 if (!SI)
Benjamin Kramercbce2f02018-01-23 23:05:04 +000084 PrintFatalError(Loc, "instregex requires pattern string: " +
85 Expr->getAsString());
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000086 StringRef Original = SI->getValue();
87
Benjamin Kramercbce2f02018-01-23 23:05:04 +000088 // Extract a prefix that we can binary search on.
89 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000090 auto FirstMeta = Original.find_first_of(RegexMetachars);
91
Benjamin Kramercbce2f02018-01-23 23:05:04 +000092 // Look for top-level | or ?. We cannot optimize them to binary search.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000093 if (removeParens(Original).find_first_of("|?") != std::string::npos)
Benjamin Kramercbce2f02018-01-23 23:05:04 +000094 FirstMeta = 0;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000095
96 Optional<Regex> Regexpr = None;
97 StringRef Prefix = Original.substr(0, FirstMeta);
Simon Pilgrim34d512e2018-03-24 21:04:20 +000098 StringRef PatStr = Original.substr(FirstMeta);
99 if (!PatStr.empty()) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000100 // For the rest use a python-style prefix match.
Simon Pilgrim34d512e2018-03-24 21:04:20 +0000101 std::string pat = PatStr;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000102 if (pat[0] != '^') {
103 pat.insert(0, "^(");
104 pat.insert(pat.end(), ')');
105 }
106 Regexpr = Regex(pat);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000107 }
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000108
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000109 int NumMatches = 0;
110
Benjamin Kramer4890a712018-01-24 22:35:11 +0000111 unsigned NumGeneric = Target.getNumFixedInstructions();
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000112 ArrayRef<const CodeGenInstruction *> Generics =
113 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1);
114
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000115 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000116 for (auto *Inst : Generics) {
117 StringRef InstName = Inst->TheDef->getName();
118 if (InstName.startswith(Prefix) &&
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000119 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))) {
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000120 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000121 NumMatches++;
122 }
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000123 }
124
125 ArrayRef<const CodeGenInstruction *> Instructions =
Benjamin Kramer4890a712018-01-24 22:35:11 +0000126 Target.getInstructionsByEnumValue().slice(NumGeneric + 1);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000127
128 // Target instructions are sorted. Find the range that starts with our
129 // prefix.
130 struct Comp {
131 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
132 return LHS->TheDef->getName() < RHS;
133 }
134 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
135 return LHS < RHS->TheDef->getName() &&
136 !RHS->TheDef->getName().startswith(LHS);
137 }
138 };
139 auto Range = std::equal_range(Instructions.begin(), Instructions.end(),
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000140 Prefix, Comp());
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000141
142 // For this range we know that it starts with the prefix. Check if there's
143 // a regex that needs to be checked.
144 for (auto *Inst : make_range(Range)) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000145 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000146 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))) {
Craig Topper8a417c12014-12-09 08:05:51 +0000147 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000148 NumMatches++;
149 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000150 }
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000151
152 if (0 == NumMatches)
153 PrintFatalError(Loc, "instregex has no matches: " + Original);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000154 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000155 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000156};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000157
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000158} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000159
Andrew Trick76686492012-09-15 00:19:57 +0000160/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000161CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
162 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000163 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000164
Andrew Trick9e1deb62012-10-03 23:06:32 +0000165 Sets.addFieldExpander("InstRW", "Instrs");
166
167 // Allow Set evaluation to recognize the dags used in InstRW records:
168 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000169 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
170 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000171
Andrew Trick76686492012-09-15 00:19:57 +0000172 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
173 // that are explicitly referenced in tablegen records. Resources associated
174 // with each processor will be derived later. Populate ProcModelMap with the
175 // CodeGenProcModel instances.
176 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000177
Andrew Trick76686492012-09-15 00:19:57 +0000178 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
179 // defined, and populate SchedReads and SchedWrites vectors. Implicit
180 // SchedReadWrites that represent sequences derived from expanded variant will
181 // be inferred later.
182 collectSchedRW();
183
184 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
185 // required by an instruction definition, and populate SchedClassIdxMap. Set
186 // NumItineraryClasses to the number of explicit itinerary classes referenced
187 // by instructions. Set NumInstrSchedClasses to the number of itinerary
188 // classes plus any classes implied by instructions that derive from class
189 // Sched and provide SchedRW list. This does not infer any new classes from
190 // SchedVariant.
191 collectSchedClasses();
192
193 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000194 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000195 // all itinerary classes to be discovered.
196 collectProcItins();
197
198 // Find ItinRW records for each processor and itinerary class.
199 // (For per-operand resources mapped to itinerary classes).
200 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000201
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000202 // Find UnsupportedFeatures records for each processor.
203 // (For per-operand resources mapped to itinerary classes).
204 collectProcUnsupportedFeatures();
205
Andrew Trick33401e82012-09-15 00:19:59 +0000206 // Infer new SchedClasses from SchedVariant.
207 inferSchedClasses();
208
Andrew Trick1e46d482012-09-15 00:20:02 +0000209 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
210 // ProcResourceDefs.
Joel Jones80372332017-06-28 00:06:40 +0000211 DEBUG(dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000212 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000213
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000214 // Collect optional processor description.
215 collectOptionalProcessorInfo();
216
217 checkCompleteness();
218}
219
220void CodeGenSchedModels::collectRetireControlUnits() {
221 RecVec Units = Records.getAllDerivedDefinitions("RetireControlUnit");
222
223 for (Record *RCU : Units) {
224 CodeGenProcModel &PM = getProcModel(RCU->getValueAsDef("SchedModel"));
225 if (PM.RetireControlUnit) {
226 PrintError(RCU->getLoc(),
227 "Expected a single RetireControlUnit definition");
228 PrintNote(PM.RetireControlUnit->getLoc(),
229 "Previous definition of RetireControlUnit was here");
230 }
231 PM.RetireControlUnit = RCU;
232 }
233}
234
235/// Collect optional processor information.
236void CodeGenSchedModels::collectOptionalProcessorInfo() {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000237 // Find register file definitions for each processor.
238 collectRegisterFiles();
239
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000240 // Collect processor RetireControlUnit descriptors if available.
241 collectRetireControlUnits();
Andrew Trick87255e32012-07-07 04:00:00 +0000242}
243
Andrew Trick76686492012-09-15 00:19:57 +0000244/// Gather all processor models.
245void CodeGenSchedModels::collectProcModels() {
246 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000247 llvm::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000248
Andrew Trick76686492012-09-15 00:19:57 +0000249 // Reserve space because we can. Reallocation would be ok.
250 ProcModels.reserve(ProcRecords.size()+1);
251
252 // Use idx=0 for NoModel/NoItineraries.
253 Record *NoModelDef = Records.getDef("NoSchedModel");
254 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000255 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000256 ProcModelMap[NoModelDef] = 0;
257
258 // For each processor, find a unique machine model.
Joel Jones80372332017-06-28 00:06:40 +0000259 DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000260 for (Record *ProcRecord : ProcRecords)
261 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000262}
263
264/// Get a unique processor model based on the defined MachineModel and
265/// ProcessorItineraries.
266void CodeGenSchedModels::addProcModel(Record *ProcDef) {
267 Record *ModelKey = getModelOrItinDef(ProcDef);
268 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
269 return;
270
271 std::string Name = ModelKey->getName();
272 if (ModelKey->isSubClassOf("SchedMachineModel")) {
273 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000274 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000275 }
276 else {
277 // An itinerary is defined without a machine model. Infer a new model.
278 if (!ModelKey->getValueAsListOfDefs("IID").empty())
279 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000280 ProcModels.emplace_back(ProcModels.size(), Name,
281 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000282 }
283 DEBUG(ProcModels.back().dump());
284}
285
286// Recursively find all reachable SchedReadWrite records.
287static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
288 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000289 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000290 return;
291 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000292 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000293 if (RWDef->isSubClassOf("WriteSequence")) {
294 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000295 for (Record *WSRec : Seq)
296 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000297 }
298 else if (RWDef->isSubClassOf("SchedVariant")) {
299 // Visit each variant (guarded by a different predicate).
300 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000301 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000302 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000303 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
304 for (Record *SelDef : Selected)
305 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000306 }
307 }
308}
309
310// Collect and sort all SchedReadWrites reachable via tablegen records.
311// More may be inferred later when inferring new SchedClasses from variants.
312void CodeGenSchedModels::collectSchedRW() {
313 // Reserve idx=0 for invalid writes/reads.
314 SchedWrites.resize(1);
315 SchedReads.resize(1);
316
317 SmallPtrSet<Record*, 16> RWSet;
318
319 // Find all SchedReadWrites referenced by instruction defs.
320 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000321 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000322 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000323 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000324 continue;
325 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000326 for (Record *RW : RWs) {
327 if (RW->isSubClassOf("SchedWrite"))
328 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000329 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000330 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
331 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000332 }
333 }
334 }
335 // Find all ReadWrites referenced by InstRW.
336 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000337 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000338 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000339 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
340 for (Record *RWDef : RWDefs) {
341 if (RWDef->isSubClassOf("SchedWrite"))
342 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000343 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000344 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
345 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000346 }
347 }
348 }
349 // Find all ReadWrites referenced by ItinRW.
350 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000351 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000352 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000353 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
354 for (Record *RWDef : RWDefs) {
355 if (RWDef->isSubClassOf("SchedWrite"))
356 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000357 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000358 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
359 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000360 }
361 }
362 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000363 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
364 // for the loop below that initializes Alias vectors.
365 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000366 llvm::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000367 for (Record *ADef : AliasDefs) {
368 Record *MatchDef = ADef->getValueAsDef("MatchRW");
369 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000370 if (MatchDef->isSubClassOf("SchedWrite")) {
371 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000372 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000373 scanSchedRW(AliasDef, SWDefs, RWSet);
374 }
375 else {
376 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
377 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000378 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000379 scanSchedRW(AliasDef, SRDefs, RWSet);
380 }
381 }
Andrew Trick76686492012-09-15 00:19:57 +0000382 // Sort and add the SchedReadWrites directly referenced by instructions or
383 // itinerary resources. Index reads and writes in separate domains.
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000384 llvm::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000385 for (Record *SWDef : SWDefs) {
386 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
387 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000388 }
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000389 llvm::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000390 for (Record *SRDef : SRDefs) {
391 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
392 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000393 }
394 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000395 for (CodeGenSchedRW &CGRW : SchedWrites) {
396 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000397 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000398 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000399 /*IsRead=*/false);
400 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000401 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000402 for (Record *ADef : AliasDefs) {
403 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000404 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000405 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000406 CodeGenSchedRW &RW = getSchedRW(MatchDef);
407 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000408 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
409 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000410 }
Andrew Trick76686492012-09-15 00:19:57 +0000411 DEBUG(
Joel Jones80372332017-06-28 00:06:40 +0000412 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
Andrew Trick76686492012-09-15 00:19:57 +0000413 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
414 dbgs() << WIdx << ": ";
415 SchedWrites[WIdx].dump();
416 dbgs() << '\n';
417 }
418 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
419 dbgs() << RIdx << ": ";
420 SchedReads[RIdx].dump();
421 dbgs() << '\n';
422 }
423 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
Javed Absar67b042c2017-09-13 10:31:10 +0000424 for (Record *RWDef : RWDefs) {
425 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
Simon Pilgrim494d0752018-03-24 21:22:32 +0000426 StringRef Name = RWDef->getName();
Andrew Trick76686492012-09-15 00:19:57 +0000427 if (Name != "NoWrite" && Name != "ReadDefault")
Simon Pilgrim494d0752018-03-24 21:22:32 +0000428 dbgs() << "Unused SchedReadWrite " << Name << '\n';
Andrew Trick76686492012-09-15 00:19:57 +0000429 }
430 });
431}
432
433/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000434std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000435 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000436 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000437 if (I != Seq.begin())
438 Name += '_';
439 Name += getSchedRW(*I, IsRead).Name;
440 }
441 Name += ')';
442 return Name;
443}
444
Craig Toppere2611842018-03-21 05:13:04 +0000445unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead) const {
Andrew Trick76686492012-09-15 00:19:57 +0000446 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Craig Toppere2611842018-03-21 05:13:04 +0000447 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin(),
Andrew Trick76686492012-09-15 00:19:57 +0000448 E = RWVec.end(); I != E; ++I) {
449 if (I->TheDef == Def)
450 return I - RWVec.begin();
451 }
452 return 0;
453}
454
Andrew Trickcfe222c2012-09-19 04:43:19 +0000455bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000456 for (const CodeGenSchedRW &Read : SchedReads) {
457 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000458 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
459 continue;
460
461 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000462 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000463 return true;
464 }
465 }
466 return false;
467}
468
Craig Topper6f2cc9b2018-03-21 05:13:01 +0000469static void splitSchedReadWrites(const RecVec &RWDefs,
470 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000471 for (Record *RWDef : RWDefs) {
472 if (RWDef->isSubClassOf("SchedWrite"))
473 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000474 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000475 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
476 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000477 }
478 }
479}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000480
Andrew Trick76686492012-09-15 00:19:57 +0000481// Split the SchedReadWrites defs and call findRWs for each list.
482void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
483 IdxVec &Writes, IdxVec &Reads) const {
484 RecVec WriteDefs;
485 RecVec ReadDefs;
486 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
487 findRWs(WriteDefs, Writes, false);
488 findRWs(ReadDefs, Reads, true);
489}
490
491// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
492void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
493 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000494 for (Record *RWDef : RWDefs) {
495 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000496 assert(Idx && "failed to collect SchedReadWrite");
497 RWs.push_back(Idx);
498 }
499}
500
Andrew Trick33401e82012-09-15 00:19:59 +0000501void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
502 bool IsRead) const {
503 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
504 if (!SchedRW.IsSequence) {
505 RWSeq.push_back(RWIdx);
506 return;
507 }
508 int Repeat =
509 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
510 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000511 for (unsigned I : SchedRW.Sequence) {
512 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000513 }
514 }
515}
516
Andrew Trickda984b12012-10-03 23:06:28 +0000517// Expand a SchedWrite as a sequence following any aliases that coincide with
518// the given processor model.
519void CodeGenSchedModels::expandRWSeqForProc(
520 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
521 const CodeGenProcModel &ProcModel) const {
522
523 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000524 Record *AliasDef = nullptr;
Andrew Trickda984b12012-10-03 23:06:28 +0000525 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
526 AI != AE; ++AI) {
527 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
528 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
529 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
530 if (&getProcModel(ModelDef) != &ProcModel)
531 continue;
532 }
533 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000534 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
535 "defined for processor " + ProcModel.ModelName +
536 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000537 AliasDef = AliasRW.TheDef;
538 }
539 if (AliasDef) {
540 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
541 RWSeq, IsRead,ProcModel);
542 return;
543 }
544 if (!SchedWrite.IsSequence) {
545 RWSeq.push_back(RWIdx);
546 return;
547 }
548 int Repeat =
549 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
550 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000551 for (unsigned I : SchedWrite.Sequence) {
552 expandRWSeqForProc(I, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000553 }
554 }
555}
556
Andrew Trick33401e82012-09-15 00:19:59 +0000557// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000558unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000559 bool IsRead) {
560 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
561
562 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
563 I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000564 if (makeArrayRef(I->Sequence) == Seq)
Andrew Trick33401e82012-09-15 00:19:59 +0000565 return I - RWVec.begin();
566 }
567 // Index zero reserved for invalid RW.
568 return 0;
569}
570
571/// Add this ReadWrite if it doesn't already exist.
572unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
573 bool IsRead) {
574 assert(!Seq.empty() && "cannot insert empty sequence");
575 if (Seq.size() == 1)
576 return Seq.back();
577
578 unsigned Idx = findRWForSequence(Seq, IsRead);
579 if (Idx)
580 return Idx;
581
Andrew Trickda984b12012-10-03 23:06:28 +0000582 unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
583 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
584 if (IsRead)
Andrew Trick33401e82012-09-15 00:19:59 +0000585 SchedReads.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000586 else
587 SchedWrites.push_back(SchedRW);
588 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000589}
590
Andrew Trick76686492012-09-15 00:19:57 +0000591/// Visit all the instruction definitions for this target to gather and
592/// enumerate the itinerary classes. These are the explicitly specified
593/// SchedClasses. More SchedClasses may be inferred.
594void CodeGenSchedModels::collectSchedClasses() {
595
596 // NoItinerary is always the first class at Idx=0
Craig Topper281a19c2018-03-22 06:15:08 +0000597 assert(SchedClasses.empty() && "Expected empty sched class");
598 SchedClasses.emplace_back(0, "NoInstrModel",
599 Records.getDef("NoItinerary"));
Andrew Trick76686492012-09-15 00:19:57 +0000600 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000601
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000602 // Create a SchedClass for each unique combination of itinerary class and
603 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000604 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000605 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000606 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000607 if (!Inst->TheDef->isValueUnset("SchedRW"))
608 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000609
Andrew Trick76686492012-09-15 00:19:57 +0000610 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topper281a19c2018-03-22 06:15:08 +0000611 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topper8a417c12014-12-09 08:05:51 +0000612 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000613 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000614 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000615 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000616 llvm::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000617 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000618 for (Record *RWDef : InstRWDefs)
619 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000620
Andrew Trick76686492012-09-15 00:19:57 +0000621 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000622
Andrew Trick76686492012-09-15 00:19:57 +0000623 bool EnableDump = false;
624 DEBUG(EnableDump = true);
625 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000626 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000627
Joel Jones80372332017-06-28 00:06:40 +0000628 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000629 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000630 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim949437e2018-03-21 18:09:34 +0000631 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000632 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000633 if (!Inst->hasNoSchedulingInfo)
634 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000635 continue;
636 }
637 CodeGenSchedClass &SC = getSchedClass(SCIdx);
638 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000639 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000640 "must not be subtarget specific.");
641
642 IdxVec ProcIndices;
643 if (SC.ItinClassDef->getName() != "NoItinerary") {
644 ProcIndices.push_back(0);
645 dbgs() << "Itinerary for " << InstName << ": "
646 << SC.ItinClassDef->getName() << '\n';
647 }
648 if (!SC.Writes.empty()) {
649 ProcIndices.push_back(0);
650 dbgs() << "SchedRW machine model for " << InstName;
651 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
652 dbgs() << " " << SchedWrites[*WI].Name;
653 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
654 dbgs() << " " << SchedReads[*RI].Name;
655 dbgs() << '\n';
656 }
657 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000658 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000659 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000660 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000661 ProcIndices.push_back(ProcModel.Index);
662 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000663 IdxVec Writes;
664 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000665 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000666 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000667 for (unsigned WIdx : Writes)
668 dbgs() << " " << SchedWrites[WIdx].Name;
669 for (unsigned RIdx : Reads)
670 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000671 dbgs() << '\n';
672 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000673 // If ProcIndices contains zero, the class applies to all processors.
674 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
Javed Absar21c75912017-10-09 16:21:25 +0000675 for (const CodeGenProcModel &PM : ProcModels) {
Javed Absarfc500042017-10-05 13:27:43 +0000676 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
Andrew Trickf9df92c92016-10-18 04:17:44 +0000677 dbgs() << "No machine model for " << Inst->TheDef->getName()
Javed Absarfc500042017-10-05 13:27:43 +0000678 << " on processor " << PM.ModelName << '\n';
Andrew Trickf9df92c92016-10-18 04:17:44 +0000679 }
Andrew Trick87255e32012-07-07 04:00:00 +0000680 }
681 }
Andrew Trick76686492012-09-15 00:19:57 +0000682}
683
Andrew Trick76686492012-09-15 00:19:57 +0000684/// Find an SchedClass that has been inferred from a per-operand list of
685/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000686unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000687 ArrayRef<unsigned> Writes,
688 ArrayRef<unsigned> Reads) const {
Simon Pilgrim4cca3b12018-03-21 17:57:21 +0000689 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I)
690 if (I->isKeyEqual(ItinClassDef, Writes, Reads))
Andrew Trick76686492012-09-15 00:19:57 +0000691 return I - schedClassBegin();
Andrew Trick76686492012-09-15 00:19:57 +0000692 return 0;
693}
Andrew Trick87255e32012-07-07 04:00:00 +0000694
Andrew Trick76686492012-09-15 00:19:57 +0000695// Get the SchedClass index for an instruction.
696unsigned CodeGenSchedModels::getSchedClassIdx(
697 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000698
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000699 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000700}
701
Benjamin Kramere1761952015-10-24 12:46:49 +0000702std::string
703CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
704 ArrayRef<unsigned> OperWrites,
705 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000706
707 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000708 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
709 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000710 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000711 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000712 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000713 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000714 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000715 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000716 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000717 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000718 }
719 return Name;
720}
721
722std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
723
724 std::string Name;
725 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
726 if (I != InstDefs.begin())
727 Name += '_';
728 Name += (*I)->getName();
729 }
730 return Name;
731}
732
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000733/// Add an inferred sched class from an itinerary class and per-operand list of
734/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
735/// processors that may utilize this class.
736unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000737 ArrayRef<unsigned> OperWrites,
738 ArrayRef<unsigned> OperReads,
739 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000740 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
741
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000742 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
743 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000744 IdxVec PI;
745 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
746 SchedClasses[Idx].ProcIndices.end(),
747 ProcIndices.begin(), ProcIndices.end(),
748 std::back_inserter(PI));
Craig Topper59d13772018-03-24 22:58:00 +0000749 SchedClasses[Idx].ProcIndices = std::move(PI);
Andrew Trick76686492012-09-15 00:19:57 +0000750 return Idx;
751 }
752 Idx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000753 SchedClasses.emplace_back(Idx,
754 createSchedClassName(ItinClassDef, OperWrites,
755 OperReads),
756 ItinClassDef);
Andrew Trick76686492012-09-15 00:19:57 +0000757 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick76686492012-09-15 00:19:57 +0000758 SC.Writes = OperWrites;
759 SC.Reads = OperReads;
760 SC.ProcIndices = ProcIndices;
761
762 return Idx;
763}
764
765// Create classes for each set of opcodes that are in the same InstReadWrite
766// definition across all processors.
767void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
768 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
769 // intersects with an existing class via a previous InstRWDef. Instrs that do
770 // not intersect with an existing class refer back to their former class as
771 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +0000772 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000773 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000774 const RecVec *InstDefs = Sets.expand(InstRWDef);
775 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000776 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000777
Craig Topper93dd77d2018-03-18 08:38:03 +0000778 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +0000779 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000780 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +0000781 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000782 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +0000783 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +0000784 }
785 // For each set of Instrs, create a new class if necessary, and map or remap
786 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +0000787 for (auto &Entry : ClassInstrs) {
788 unsigned OldSCIdx = Entry.first;
789 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +0000790 // If the all instrs in the current class are accounted for, then leave
791 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000792 if (OldSCIdx) {
793 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
794 if (!RWDefs.empty()) {
795 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper06d78372018-03-21 19:30:30 +0000796 unsigned OrigNumInstrs =
797 count_if(*OrigInstDefs, [&](Record *OIDef) {
798 return InstrClassMap[OIDef] == OldSCIdx;
799 });
Andrew Trick78a08512013-06-05 06:55:20 +0000800 if (OrigNumInstrs == InstDefs.size()) {
801 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
802 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000803 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
804 // Make sure we didn't already have a InstRW containing this
805 // instruction on this model.
806 for (Record *RWD : RWDefs) {
807 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
808 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
809 for (Record *Inst : InstDefs) {
810 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
811 Inst->getName() + " also matches " +
812 RWD->getValue("Instrs")->getValue()->getAsString());
813 }
814 }
815 }
Andrew Trick78a08512013-06-05 06:55:20 +0000816 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
817 << SchedClasses[OldSCIdx].Name << " on "
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000818 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +0000819 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
820 continue;
821 }
822 }
Andrew Trick76686492012-09-15 00:19:57 +0000823 }
824 unsigned SCIdx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000825 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick76686492012-09-15 00:19:57 +0000826 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick78a08512013-06-05 06:55:20 +0000827 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
828 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
829
Andrew Trick76686492012-09-15 00:19:57 +0000830 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
831 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
832 SC.Writes = SchedClasses[OldSCIdx].Writes;
833 SC.Reads = SchedClasses[OldSCIdx].Reads;
834 SC.ProcIndices.push_back(0);
Craig Topper989d94d2018-03-21 19:52:13 +0000835 // If we had an old class, copy it's InstRWs to this new class.
836 if (OldSCIdx) {
837 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
838 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
839 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
840 for (Record *InstDef : InstDefs) {
Craig Topper9fbbe5d2018-03-21 19:30:31 +0000841 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
842 InstDef->getName() + " also matches " +
843 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick9e1deb62012-10-03 23:06:32 +0000844 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000845 }
Craig Topper989d94d2018-03-21 19:52:13 +0000846 assert(OldRWDef != InstRWDef &&
847 "SchedClass has duplicate InstRW def");
848 SC.InstRWs.push_back(OldRWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000849 }
Andrew Trick76686492012-09-15 00:19:57 +0000850 }
Craig Topper989d94d2018-03-21 19:52:13 +0000851 // Map each Instr to this new class.
852 for (Record *InstDef : InstDefs)
853 InstrClassMap[InstDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000854 SC.InstRWs.push_back(InstRWDef);
855 }
Andrew Trick87255e32012-07-07 04:00:00 +0000856}
857
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000858// True if collectProcItins found anything.
859bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000860 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
861 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000862 return true;
863 }
864 return false;
865}
866
Andrew Trick87255e32012-07-07 04:00:00 +0000867// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000868void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000869 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000870 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000871 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000872 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000873
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000874 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
875 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
876
877 // Populate ItinDefList with Itinerary records.
878 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000879
880 // Insert each itinerary data record in the correct position within
881 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +0000882 for (Record *ItinData : ItinRecords) {
Andrew Trick76686492012-09-15 00:19:57 +0000883 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000884 bool FoundClass = false;
885 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
886 SCI != SCE; ++SCI) {
887 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000888 if (SCI->ItinClassDef == ItinDef) {
889 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000890 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000891 }
Andrew Trick76686492012-09-15 00:19:57 +0000892 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000893 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000894 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
895 << " missing class for itinerary " << ItinDef->getName() << '\n');
896 }
Andrew Trick87255e32012-07-07 04:00:00 +0000897 }
Andrew Trick76686492012-09-15 00:19:57 +0000898 // Check for missing itinerary entries.
899 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
900 DEBUG(
901 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
902 if (!ProcModel.ItinDefList[i])
903 dbgs() << ProcModel.ItinsDef->getName()
904 << " missing itinerary for class "
905 << SchedClasses[i].Name << '\n';
906 });
Andrew Trick87255e32012-07-07 04:00:00 +0000907 }
Andrew Trick87255e32012-07-07 04:00:00 +0000908}
Andrew Trick76686492012-09-15 00:19:57 +0000909
910// Gather the read/write types for each itinerary class.
911void CodeGenSchedModels::collectProcItinRW() {
912 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000913 llvm::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +0000914 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000915 if (!RWDef->getValueInit("SchedModel")->isComplete())
916 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
917 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +0000918 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
919 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000920 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000921 + ModelDef->getName());
922 }
Javed Absarf45d0b92017-10-08 17:23:30 +0000923 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000924 }
925}
926
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000927// Gather the unsupported features for processor models.
928void CodeGenSchedModels::collectProcUnsupportedFeatures() {
929 for (CodeGenProcModel &ProcModel : ProcModels) {
930 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
931 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
932 }
933 }
934}
935
Andrew Trick33401e82012-09-15 00:19:59 +0000936/// Infer new classes from existing classes. In the process, this may create new
937/// SchedWrites from sequences of existing SchedWrites.
938void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000939 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000940 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
941
Andrew Trick33401e82012-09-15 00:19:59 +0000942 // Visit all existing classes and newly created classes.
943 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000944 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
945
Andrew Trick33401e82012-09-15 00:19:59 +0000946 if (SchedClasses[Idx].ItinClassDef)
947 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000948 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000949 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000950 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000951 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
952 Idx, SchedClasses[Idx].ProcIndices);
953 }
954 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
955 "too many SchedVariants");
956 }
957}
958
959/// Infer classes from per-processor itinerary resources.
960void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
961 unsigned FromClassIdx) {
962 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
963 const CodeGenProcModel &PM = ProcModels[PIdx];
964 // For all ItinRW entries.
965 bool HasMatch = false;
966 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
967 II != IE; ++II) {
968 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
969 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
970 continue;
971 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000972 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000973 + ItinClassDef->getName()
974 + " in ItinResources for " + PM.ModelName);
975 HasMatch = true;
976 IdxVec Writes, Reads;
977 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +0000978 inferFromRW(Writes, Reads, FromClassIdx, PIdx);
Andrew Trick33401e82012-09-15 00:19:59 +0000979 }
980 }
981}
982
983/// Infer classes from per-processor InstReadWrite definitions.
984void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000985 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000986 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000987 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
988 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000989 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000990 for (; II != IE; ++II) {
991 if (InstrClassMap[*II] == SCIdx)
992 break;
993 }
994 // If this class no longer has any instructions mapped to it, it has become
995 // irrelevant.
996 if (II == IE)
997 continue;
998 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000999 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1000 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Craig Topper9f3293a2018-03-24 21:57:35 +00001001 inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +00001002 }
1003}
1004
1005namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001006
Andrew Trick9257b8f2012-09-22 02:24:21 +00001007// Helper for substituteVariantOperand.
1008struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +00001009 Record *VarOrSeqDef; // Variant or sequence.
1010 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001011 unsigned ProcIdx; // Processor model index or zero for any.
1012 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
1013
1014 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +00001015 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +00001016};
1017
Andrew Trick33401e82012-09-15 00:19:59 +00001018// Associate a predicate with the SchedReadWrite that it guards.
1019// RWIdx is the index of the read/write variant.
1020struct PredCheck {
1021 bool IsRead;
1022 unsigned RWIdx;
1023 Record *Predicate;
1024
1025 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
1026};
1027
1028// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1029struct PredTransition {
1030 // A predicate term is a conjunction of PredChecks.
1031 SmallVector<PredCheck, 4> PredTerm;
1032 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1033 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001034 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001035};
1036
1037// Encapsulate a set of partially constructed transitions.
1038// The results are built by repeated calls to substituteVariants.
1039class PredTransitions {
1040 CodeGenSchedModels &SchedModels;
1041
1042public:
1043 std::vector<PredTransition> TransVec;
1044
1045 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1046
1047 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1048 bool IsRead, unsigned StartIdx);
1049
1050 void substituteVariants(const PredTransition &Trans);
1051
1052#ifndef NDEBUG
1053 void dump() const;
1054#endif
1055
1056private:
1057 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001058 void getIntersectingVariants(
1059 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1060 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001061 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001062};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001063
1064} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001065
1066// Return true if this predicate is mutually exclusive with a PredTerm. This
1067// degenerates into checking if the predicate is mutually exclusive with any
1068// predicate in the Term's conjunction.
1069//
1070// All predicates associated with a given SchedRW are considered mutually
1071// exclusive. This should work even if the conditions expressed by the
1072// predicates are not exclusive because the predicates for a given SchedWrite
1073// are always checked in the order they are defined in the .td file. Later
1074// conditions implicitly negate any prior condition.
1075bool PredTransitions::mutuallyExclusive(Record *PredDef,
1076 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001077 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001078 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001079 return false;
1080
Javed Absarfc500042017-10-05 13:27:43 +00001081 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001082 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1083 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1084 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1085 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1086 return true;
1087 }
1088 }
1089 return false;
1090}
1091
Andrew Trickda984b12012-10-03 23:06:28 +00001092static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1093 CodeGenSchedModels &SchedModels) {
1094 if (RW.HasVariants)
1095 return true;
1096
Javed Absar21c75912017-10-09 16:21:25 +00001097 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001098 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001099 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001100 if (AliasRW.HasVariants)
1101 return true;
1102 if (AliasRW.IsSequence) {
1103 IdxVec ExpandedRWs;
1104 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1105 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1106 SI != SE; ++SI) {
1107 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1108 SchedModels)) {
1109 return true;
1110 }
1111 }
1112 }
1113 }
1114 return false;
1115}
1116
1117static bool hasVariant(ArrayRef<PredTransition> Transitions,
1118 CodeGenSchedModels &SchedModels) {
1119 for (ArrayRef<PredTransition>::iterator
1120 PTI = Transitions.begin(), PTE = Transitions.end();
1121 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001122 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001123 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1124 WSI != WSE; ++WSI) {
1125 for (SmallVectorImpl<unsigned>::const_iterator
1126 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1127 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1128 return true;
1129 }
1130 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001131 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001132 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1133 RSI != RSE; ++RSI) {
1134 for (SmallVectorImpl<unsigned>::const_iterator
1135 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1136 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1137 return true;
1138 }
1139 }
1140 }
1141 return false;
1142}
1143
1144// Populate IntersectingVariants with any variants or aliased sequences of the
1145// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001146// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001147void PredTransitions::getIntersectingVariants(
1148 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1149 std::vector<TransVariant> &IntersectingVariants) {
1150
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001151 bool GenericRW = false;
1152
Andrew Trickda984b12012-10-03 23:06:28 +00001153 std::vector<TransVariant> Variants;
1154 if (SchedRW.HasVariants) {
1155 unsigned VarProcIdx = 0;
1156 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1157 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1158 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1159 }
1160 // Push each variant. Assign TransVecIdx later.
1161 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001162 for (Record *VarDef : VarDefs)
1163 Variants.push_back(TransVariant(VarDef, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001164 if (VarProcIdx == 0)
1165 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001166 }
1167 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1168 AI != AE; ++AI) {
1169 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1170 // to is defined within a processor model, constrain all variants to
1171 // that processor.
1172 unsigned AliasProcIdx = 0;
1173 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1174 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1175 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1176 }
1177 const CodeGenSchedRW &AliasRW =
1178 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1179
1180 if (AliasRW.HasVariants) {
1181 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001182 for (Record *VD : VarDefs)
1183 Variants.push_back(TransVariant(VD, AliasRW.Index, AliasProcIdx, 0));
Andrew Trickda984b12012-10-03 23:06:28 +00001184 }
1185 if (AliasRW.IsSequence) {
1186 Variants.push_back(
1187 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1188 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001189 if (AliasProcIdx == 0)
1190 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001191 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001192 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001193 // Don't expand variants if the processor models don't intersect.
1194 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001195 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001196 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001197 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1198 Variant.ProcIdx);
1199 if (!Cnt)
1200 continue;
1201 if (Cnt > 1) {
1202 const CodeGenProcModel &PM =
1203 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001204 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1205 "Multiple variants defined for processor " +
1206 PM.ModelName +
1207 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001208 }
1209 }
1210 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1211 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1212 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1213 continue;
1214 }
1215 if (IntersectingVariants.empty()) {
1216 // The first variant builds on the existing transition.
1217 Variant.TransVecIdx = TransIdx;
1218 IntersectingVariants.push_back(Variant);
1219 }
1220 else {
1221 // Push another copy of the current transition for more variants.
1222 Variant.TransVecIdx = TransVec.size();
1223 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001224 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001225 }
1226 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001227 if (GenericRW && IntersectingVariants.empty()) {
1228 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1229 "a matching predicate on any processor");
1230 }
Andrew Trickda984b12012-10-03 23:06:28 +00001231}
1232
Andrew Trick9257b8f2012-09-22 02:24:21 +00001233// Push the Reads/Writes selected by this variant onto the PredTransition
1234// specified by VInfo.
1235void PredTransitions::
1236pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001237 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1238
Andrew Trick9257b8f2012-09-22 02:24:21 +00001239 // If this operand transition is reached through a processor-specific alias,
1240 // then the whole transition is specific to this processor.
1241 if (VInfo.ProcIdx != 0)
1242 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1243
Andrew Trick33401e82012-09-15 00:19:59 +00001244 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001245 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1246 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1247 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1248 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1249 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1250 }
1251 else {
1252 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1253 "variant must be a SchedVariant or aliased WriteSequence");
1254 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1255 }
Andrew Trick33401e82012-09-15 00:19:59 +00001256
Andrew Trick9257b8f2012-09-22 02:24:21 +00001257 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001258
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001259 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001260 ? Trans.ReadSequences : Trans.WriteSequences;
1261 if (SchedRW.IsVariadic) {
1262 unsigned OperIdx = RWSequences.size()-1;
1263 // Make N-1 copies of this transition's last sequence.
1264 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001265 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001266 RWSequences.reserve(RWSequences.size() + 1);
1267 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001268 }
1269 // Push each of the N elements of the SelectedRWs onto a copy of the last
1270 // sequence (split the current operand into N operands).
1271 // Note that write sequences should be expanded within this loop--the entire
1272 // sequence belongs to a single operand.
1273 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1274 RWI != RWE; ++RWI, ++OperIdx) {
1275 IdxVec ExpandedRWs;
1276 if (IsRead)
1277 ExpandedRWs.push_back(*RWI);
1278 else
1279 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1280 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1281 ExpandedRWs.begin(), ExpandedRWs.end());
1282 }
1283 assert(OperIdx == RWSequences.size() && "missed a sequence");
1284 }
1285 else {
1286 // Push this transition's expanded sequence onto this transition's last
1287 // sequence (add to the current operand's sequence).
1288 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1289 IdxVec ExpandedRWs;
1290 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1291 RWI != RWE; ++RWI) {
1292 if (IsRead)
1293 ExpandedRWs.push_back(*RWI);
1294 else
1295 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1296 }
1297 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1298 }
1299}
1300
1301// RWSeq is a sequence of all Reads or all Writes for the next read or write
1302// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001303// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001304// of TransVec.
1305void PredTransitions::substituteVariantOperand(
1306 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1307
1308 // Visit each original RW within the current sequence.
1309 for (SmallVectorImpl<unsigned>::const_iterator
1310 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1311 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1312 // Push this RW on all partial PredTransitions or distribute variants.
1313 // New PredTransitions may be pushed within this loop which should not be
1314 // revisited (TransEnd must be loop invariant).
1315 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1316 TransIdx != TransEnd; ++TransIdx) {
1317 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001318 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001319 if (IsRead)
1320 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1321 else
1322 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1323 continue;
1324 }
1325 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001326 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001327 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001328 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001329 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001330 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001331 IVI = IntersectingVariants.begin(),
1332 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001333 IVI != IVE; ++IVI) {
1334 pushVariant(*IVI, IsRead);
1335 }
Andrew Trick33401e82012-09-15 00:19:59 +00001336 }
1337 }
1338}
1339
1340// For each variant of a Read/Write in Trans, substitute the sequence of
1341// Read/Writes guarded by the variant. This is exponential in the number of
1342// variant Read/Writes, but in practice detection of mutually exclusive
1343// predicates should result in linear growth in the total number variants.
1344//
1345// This is one step in a breadth-first search of nested variants.
1346void PredTransitions::substituteVariants(const PredTransition &Trans) {
1347 // Build up a set of partial results starting at the back of
1348 // PredTransitions. Remember the first new transition.
1349 unsigned StartIdx = TransVec.size();
Craig Topper195aaaf2018-03-22 06:15:10 +00001350 TransVec.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001351 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001352 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001353
1354 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001355 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001356 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1357 WSI != WSE; ++WSI) {
1358 // Push a new (empty) write sequence onto all partial Transitions.
1359 for (std::vector<PredTransition>::iterator I =
1360 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001361 I->WriteSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001362 }
1363 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1364 }
1365 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001366 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001367 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1368 RSI != RSE; ++RSI) {
1369 // Push a new (empty) read sequence onto all partial Transitions.
1370 for (std::vector<PredTransition>::iterator I =
1371 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001372 I->ReadSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001373 }
1374 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1375 }
1376}
1377
Andrew Trick33401e82012-09-15 00:19:59 +00001378// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001379static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001380 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001381 CodeGenSchedModels &SchedModels) {
1382 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1383 // requires creating a new SchedClass.
1384 for (ArrayRef<PredTransition>::iterator
1385 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1386 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001387 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1388 [&SchedModels](ArrayRef<unsigned> WS) {
1389 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1390 });
Andrew Trick33401e82012-09-15 00:19:59 +00001391 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001392 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1393 [&SchedModels](ArrayRef<unsigned> RS) {
1394 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1395 });
Andrew Trick33401e82012-09-15 00:19:59 +00001396 CodeGenSchedTransition SCTrans;
1397 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001398 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Craig Topper2ed54072018-03-24 22:58:03 +00001399 OperReadsVariant, I->ProcIndices);
1400 SCTrans.ProcIndices.assign(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001401 // The final PredTerm is unique set of predicates guarding the transition.
1402 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001403 transform(I->PredTerm, std::back_inserter(Preds),
1404 [](const PredCheck &P) {
1405 return P.Predicate;
1406 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001407 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Craig Topper18cfa2c2018-03-24 22:58:02 +00001408 SCTrans.PredTerm = std::move(Preds);
1409 SchedModels.getSchedClass(FromClassIdx)
1410 .Transitions.push_back(std::move(SCTrans));
Andrew Trick33401e82012-09-15 00:19:59 +00001411 }
1412}
1413
Andrew Trick9257b8f2012-09-22 02:24:21 +00001414// Create new SchedClasses for the given ReadWrite list. If any of the
1415// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1416// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001417void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1418 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001419 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001420 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001421 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001422
1423 // Create a seed transition with an empty PredTerm and the expanded sequences
1424 // of SchedWrites for the current SchedClass.
1425 std::vector<PredTransition> LastTransitions;
Craig Topper195aaaf2018-03-22 06:15:10 +00001426 LastTransitions.emplace_back();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001427 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1428 ProcIndices.end());
1429
Benjamin Kramere1761952015-10-24 12:46:49 +00001430 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001431 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001432 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topper195aaaf2018-03-22 06:15:10 +00001433 LastTransitions[0].WriteSequences.emplace_back();
1434 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001435 Seq.append(WriteSeq.begin(), WriteSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001436 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1437 }
1438 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001439 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001440 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001441 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topper195aaaf2018-03-22 06:15:10 +00001442 LastTransitions[0].ReadSequences.emplace_back();
1443 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001444 Seq.append(ReadSeq.begin(), ReadSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001445 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1446 }
1447 DEBUG(dbgs() << '\n');
1448
1449 // Collect all PredTransitions for individual operands.
1450 // Iterate until no variant writes remain.
1451 while (hasVariant(LastTransitions, *this)) {
1452 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001453 for (const PredTransition &Trans : LastTransitions)
1454 Transitions.substituteVariants(Trans);
Andrew Trick33401e82012-09-15 00:19:59 +00001455 DEBUG(Transitions.dump());
1456 LastTransitions.swap(Transitions.TransVec);
1457 }
1458 // If the first transition has no variants, nothing to do.
1459 if (LastTransitions[0].PredTerm.empty())
1460 return;
1461
1462 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1463 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001464 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001465}
1466
Andrew Trickcf398b22013-04-23 23:45:14 +00001467// Check if any processor resource group contains all resource records in
1468// SubUnits.
1469bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1470 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1471 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1472 continue;
1473 RecVec SuperUnits =
1474 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1475 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1476 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001477 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001478 break;
1479 }
1480 }
1481 if (RI == RE)
1482 return true;
1483 }
1484 return false;
1485}
1486
1487// Verify that overlapping groups have a common supergroup.
1488void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1489 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1490 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1491 continue;
1492 RecVec CheckUnits =
1493 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1494 for (unsigned j = i+1; j < e; ++j) {
1495 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1496 continue;
1497 RecVec OtherUnits =
1498 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1499 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1500 OtherUnits.begin(), OtherUnits.end())
1501 != CheckUnits.end()) {
1502 // CheckUnits and OtherUnits overlap
1503 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1504 CheckUnits.end());
1505 if (!hasSuperGroup(OtherUnits, PM)) {
1506 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1507 "proc resource group overlaps with "
1508 + PM.ProcResourceDefs[j]->getName()
1509 + " but no supergroup contains both.");
1510 }
1511 }
1512 }
1513 }
1514}
1515
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001516// Collect all the RegisterFile definitions available in this target.
1517void CodeGenSchedModels::collectRegisterFiles() {
1518 RecVec RegisterFileDefs = Records.getAllDerivedDefinitions("RegisterFile");
1519
1520 // RegisterFiles is the vector of CodeGenRegisterFile.
1521 for (Record *RF : RegisterFileDefs) {
1522 // For each register file definition, construct a CodeGenRegisterFile object
1523 // and add it to the appropriate scheduling model.
1524 CodeGenProcModel &PM = getProcModel(RF->getValueAsDef("SchedModel"));
1525 PM.RegisterFiles.emplace_back(CodeGenRegisterFile(RF->getName(),RF));
1526 CodeGenRegisterFile &CGRF = PM.RegisterFiles.back();
1527
1528 // Now set the number of physical registers as well as the cost of registers
1529 // in each register class.
1530 CGRF.NumPhysRegs = RF->getValueAsInt("NumPhysRegs");
1531 RecVec RegisterClasses = RF->getValueAsListOfDefs("RegClasses");
1532 std::vector<int64_t> RegisterCosts = RF->getValueAsListOfInts("RegCosts");
1533 for (unsigned I = 0, E = RegisterClasses.size(); I < E; ++I) {
1534 int Cost = RegisterCosts.size() > I ? RegisterCosts[I] : 1;
1535 CGRF.Costs.emplace_back(RegisterClasses[I], Cost);
1536 }
1537 }
1538}
1539
Andrew Trick1e46d482012-09-15 00:20:02 +00001540// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1541void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001542 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1543 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1544
Andrew Trick1e46d482012-09-15 00:20:02 +00001545 // Add any subtarget-specific SchedReadWrites that are directly associated
1546 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1547 // determine which processors they apply to.
1548 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1549 SCI != SCE; ++SCI) {
1550 if (SCI->ItinClassDef)
1551 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001552 else {
1553 // This class may have a default ReadWrite list which can be overriden by
1554 // InstRW definitions.
1555 if (!SCI->InstRWs.empty()) {
1556 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1557 RWI != RWE; ++RWI) {
1558 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
Craig Topper9f3293a2018-03-24 21:57:35 +00001559 unsigned PIdx = getProcModel(RWModelDef).Index;
Andrew Trick4fe440d2013-02-01 03:19:54 +00001560 IdxVec Writes, Reads;
1561 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1562 Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001563 collectRWResources(Writes, Reads, PIdx);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001564 }
1565 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001566 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001567 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001568 }
1569 // Add resources separately defined by each subtarget.
1570 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001571 for (Record *WR : WRDefs) {
1572 Record *ModelDef = WR->getValueAsDef("SchedModel");
1573 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001574 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001575 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001576 for (Record *SWR : SWRDefs) {
1577 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1578 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001579 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001580 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001581 for (Record *RA : RADefs) {
1582 Record *ModelDef = RA->getValueAsDef("SchedModel");
1583 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001584 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001585 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001586 for (Record *SRA : SRADefs) {
1587 if (SRA->getValueInit("SchedModel")->isComplete()) {
1588 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1589 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001590 }
1591 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001592 // Add ProcResGroups that are defined within this processor model, which may
1593 // not be directly referenced but may directly specify a buffer size.
1594 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001595 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001596 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001597 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001598 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1599 if (!is_contained(PM.ProcResourceDefs, PRG))
1600 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001601 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001602 // Add ProcResourceUnits unconditionally.
1603 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1604 if (!PRU->getValueInit("SchedModel")->isComplete())
1605 continue;
1606 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1607 if (!is_contained(PM.ProcResourceDefs, PRU))
1608 PM.ProcResourceDefs.push_back(PRU);
1609 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001610 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001611 for (CodeGenProcModel &PM : ProcModels) {
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001612 llvm::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1613 LessRecord());
1614 llvm::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1615 LessRecord());
1616 llvm::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1617 LessRecord());
Andrew Trick1e46d482012-09-15 00:20:02 +00001618 DEBUG(
1619 PM.dump();
1620 dbgs() << "WriteResDefs: ";
1621 for (RecIter RI = PM.WriteResDefs.begin(),
1622 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1623 if ((*RI)->isSubClassOf("WriteRes"))
1624 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1625 else
1626 dbgs() << (*RI)->getName() << " ";
1627 }
1628 dbgs() << "\nReadAdvanceDefs: ";
1629 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1630 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1631 if ((*RI)->isSubClassOf("ReadAdvance"))
1632 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1633 else
1634 dbgs() << (*RI)->getName() << " ";
1635 }
1636 dbgs() << "\nProcResourceDefs: ";
1637 for (RecIter RI = PM.ProcResourceDefs.begin(),
1638 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1639 dbgs() << (*RI)->getName() << " ";
1640 }
1641 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001642 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001643 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001644
1645 ProcResourceDefs.clear();
1646 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001647}
1648
Matthias Braun17cb5792016-03-01 20:03:21 +00001649void CodeGenSchedModels::checkCompleteness() {
1650 bool Complete = true;
1651 bool HadCompleteModel = false;
1652 for (const CodeGenProcModel &ProcModel : procModels()) {
Simon Pilgrim1d793b82018-04-05 13:11:36 +00001653 const bool HasItineraries = ProcModel.hasItineraries();
Matthias Braun17cb5792016-03-01 20:03:21 +00001654 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1655 continue;
1656 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1657 if (Inst->hasNoSchedulingInfo)
1658 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001659 if (ProcModel.isUnsupported(*Inst))
1660 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001661 unsigned SCIdx = getSchedClassIdx(*Inst);
1662 if (!SCIdx) {
1663 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1664 PrintError("No schedule information for instruction '"
1665 + Inst->TheDef->getName() + "'");
1666 Complete = false;
1667 }
1668 continue;
1669 }
1670
1671 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1672 if (!SC.Writes.empty())
1673 continue;
Simon Pilgrim1d793b82018-04-05 13:11:36 +00001674 if (HasItineraries && SC.ItinClassDef != nullptr &&
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001675 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001676 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001677
1678 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001679 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1680 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1681 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001682 if (I == InstRWs.end()) {
1683 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1684 Inst->TheDef->getName() + "'");
1685 Complete = false;
1686 }
1687 }
1688 HadCompleteModel = true;
1689 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001690 if (!Complete) {
1691 errs() << "\n\nIncomplete schedule models found.\n"
1692 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1693 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1694 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001695 "you may temporarily use an empty list.\n"
1696 << "- Instructions related to unsupported features can be excluded with "
1697 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1698 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001699 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001700 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001701}
1702
Andrew Trick1e46d482012-09-15 00:20:02 +00001703// Collect itinerary class resources for each processor.
1704void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1705 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1706 const CodeGenProcModel &PM = ProcModels[PIdx];
1707 // For all ItinRW entries.
1708 bool HasMatch = false;
1709 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1710 II != IE; ++II) {
1711 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1712 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1713 continue;
1714 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001715 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1716 + ItinClassDef->getName()
1717 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001718 HasMatch = true;
1719 IdxVec Writes, Reads;
1720 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001721 collectRWResources(Writes, Reads, PIdx);
Andrew Trick1e46d482012-09-15 00:20:02 +00001722 }
1723 }
1724}
1725
Andrew Trickd0b9c442012-10-10 05:43:13 +00001726void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001727 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001728 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1729 if (SchedRW.TheDef) {
1730 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001731 for (unsigned Idx : ProcIndices)
1732 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001733 }
1734 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001735 for (unsigned Idx : ProcIndices)
1736 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001737 }
1738 }
1739 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1740 AI != AE; ++AI) {
1741 IdxVec AliasProcIndices;
1742 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1743 AliasProcIndices.push_back(
1744 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1745 }
1746 else
1747 AliasProcIndices = ProcIndices;
1748 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1749 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1750
1751 IdxVec ExpandedRWs;
1752 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1753 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1754 SI != SE; ++SI) {
1755 collectRWResources(*SI, IsRead, AliasProcIndices);
1756 }
1757 }
1758}
Andrew Trick1e46d482012-09-15 00:20:02 +00001759
1760// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001761void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1762 ArrayRef<unsigned> Reads,
1763 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001764 for (unsigned Idx : Writes)
1765 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001766
Benjamin Kramere1761952015-10-24 12:46:49 +00001767 for (unsigned Idx : Reads)
1768 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001769}
1770
1771// Find the processor's resource units for this kind of resource.
1772Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001773 const CodeGenProcModel &PM,
1774 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00001775 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1776 return ProcResKind;
1777
Craig Topper24064772014-04-15 07:20:03 +00001778 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001779 assert(!ProcResourceDefs.empty());
1780 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001781
Javed Absar67b042c2017-09-13 10:31:10 +00001782 for (Record *ProcResDef : ProcResourceDefs) {
1783 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1784 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001785 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001786 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001787 "Multiple ProcessorResourceUnits associated with "
1788 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001789 }
Javed Absar67b042c2017-09-13 10:31:10 +00001790 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001791 }
1792 }
Javed Absar67b042c2017-09-13 10:31:10 +00001793 for (Record *ProcResGroup : ProcResGroups) {
1794 if (ProcResGroup == ProcResKind
1795 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001796 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001797 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00001798 "Multiple ProcessorResourceUnits associated with "
1799 + ProcResKind->getName());
1800 }
Javed Absar67b042c2017-09-13 10:31:10 +00001801 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001802 }
1803 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001804 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001805 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001806 "No ProcessorResources associated with "
1807 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001808 }
1809 return ProcUnitDef;
1810}
1811
1812// Iteratively add a resource and its super resources.
1813void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001814 CodeGenProcModel &PM,
1815 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001816 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001817 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00001818
1819 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001820 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001821 return;
1822
1823 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001824 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1825 return;
1826
Andrew Trick1e46d482012-09-15 00:20:02 +00001827 if (!ProcResUnits->getValueInit("Super")->isComplete())
1828 return;
1829
1830 ProcResKind = ProcResUnits->getValueAsDef("Super");
1831 }
1832}
1833
1834// Add resources for a SchedWrite to this processor if they don't exist.
1835void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001836 assert(PIdx && "don't add resources to an invalid Processor model");
1837
Andrew Trick1e46d482012-09-15 00:20:02 +00001838 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001839 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001840 return;
1841 WRDefs.push_back(ProcWriteResDef);
1842
1843 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1844 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1845 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1846 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001847 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00001848 }
1849}
1850
1851// Add resources for a ReadAdvance to this processor if they don't exist.
1852void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1853 unsigned PIdx) {
1854 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001855 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001856 return;
1857 RADefs.push_back(ProcReadAdvanceDef);
1858}
1859
Andrew Trick8fa00f52012-09-17 22:18:43 +00001860unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001861 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001862 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001863 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1864 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001865 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001866 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001867}
1868
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001869bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1870 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1871 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1872 if (TheDef->getName() == PredDef->getName())
1873 return true;
1874 }
1875 }
1876 return false;
1877}
1878
Andrew Trick76686492012-09-15 00:19:57 +00001879#ifndef NDEBUG
1880void CodeGenProcModel::dump() const {
1881 dbgs() << Index << ": " << ModelName << " "
1882 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1883 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1884}
1885
1886void CodeGenSchedRW::dump() const {
1887 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1888 if (IsSequence) {
1889 dbgs() << "(";
1890 dumpIdxVec(Sequence);
1891 dbgs() << ")";
1892 }
1893}
1894
1895void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001896 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001897 << " Writes: ";
1898 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1899 SchedModels->getSchedWrite(Writes[i]).dump();
1900 if (i < N-1) {
1901 dbgs() << '\n';
1902 dbgs().indent(10);
1903 }
1904 }
1905 dbgs() << "\n Reads: ";
1906 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1907 SchedModels->getSchedRead(Reads[i]).dump();
1908 if (i < N-1) {
1909 dbgs() << '\n';
1910 dbgs().indent(10);
1911 }
1912 }
1913 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001914 if (!Transitions.empty()) {
1915 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001916 for (const CodeGenSchedTransition &Transition : Transitions) {
1917 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001918 }
1919 }
Andrew Trick76686492012-09-15 00:19:57 +00001920}
Andrew Trick33401e82012-09-15 00:19:59 +00001921
1922void PredTransitions::dump() const {
1923 dbgs() << "Expanded Variants:\n";
1924 for (std::vector<PredTransition>::const_iterator
1925 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1926 dbgs() << "{";
1927 for (SmallVectorImpl<PredCheck>::const_iterator
1928 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1929 PCI != PCE; ++PCI) {
1930 if (PCI != TI->PredTerm.begin())
1931 dbgs() << ", ";
1932 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1933 << ":" << PCI->Predicate->getName();
1934 }
1935 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001936 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001937 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1938 WSI != WSE; ++WSI) {
1939 dbgs() << "(";
1940 for (SmallVectorImpl<unsigned>::const_iterator
1941 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1942 if (WI != WSI->begin())
1943 dbgs() << ", ";
1944 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1945 }
1946 dbgs() << "),";
1947 }
1948 dbgs() << "}\n";
1949 }
1950}
Andrew Trick76686492012-09-15 00:19:57 +00001951#endif // NDEBUG