blob: b73e0767e3b05a24fa06edc492601e9c9d6c7982 [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
Benjamin Kramer4890a712018-01-24 22:35:11 +0000109 unsigned NumGeneric = Target.getNumFixedInstructions();
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000110 ArrayRef<const CodeGenInstruction *> Generics =
111 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1);
112
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000113 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000114 for (auto *Inst : Generics) {
115 StringRef InstName = Inst->TheDef->getName();
116 if (InstName.startswith(Prefix) &&
117 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))))
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000118 Elts.insert(Inst->TheDef);
119 }
120
121 ArrayRef<const CodeGenInstruction *> Instructions =
Benjamin Kramer4890a712018-01-24 22:35:11 +0000122 Target.getInstructionsByEnumValue().slice(NumGeneric + 1);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000123
124 // Target instructions are sorted. Find the range that starts with our
125 // prefix.
126 struct Comp {
127 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
128 return LHS->TheDef->getName() < RHS;
129 }
130 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
131 return LHS < RHS->TheDef->getName() &&
132 !RHS->TheDef->getName().startswith(LHS);
133 }
134 };
135 auto Range = std::equal_range(Instructions.begin(), Instructions.end(),
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000136 Prefix, Comp());
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000137
138 // For this range we know that it starts with the prefix. Check if there's
139 // a regex that needs to be checked.
140 for (auto *Inst : make_range(Range)) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000141 StringRef InstName = Inst->TheDef->getName();
142 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))
Craig Topper8a417c12014-12-09 08:05:51 +0000143 Elts.insert(Inst->TheDef);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000144 }
145 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000146 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000147};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000148
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000149} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000150
Andrew Trick76686492012-09-15 00:19:57 +0000151/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000152CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
153 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000154 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000155
Andrew Trick9e1deb62012-10-03 23:06:32 +0000156 Sets.addFieldExpander("InstRW", "Instrs");
157
158 // Allow Set evaluation to recognize the dags used in InstRW records:
159 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000160 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
161 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000162
Andrew Trick76686492012-09-15 00:19:57 +0000163 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
164 // that are explicitly referenced in tablegen records. Resources associated
165 // with each processor will be derived later. Populate ProcModelMap with the
166 // CodeGenProcModel instances.
167 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000168
Andrew Trick76686492012-09-15 00:19:57 +0000169 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
170 // defined, and populate SchedReads and SchedWrites vectors. Implicit
171 // SchedReadWrites that represent sequences derived from expanded variant will
172 // be inferred later.
173 collectSchedRW();
174
175 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
176 // required by an instruction definition, and populate SchedClassIdxMap. Set
177 // NumItineraryClasses to the number of explicit itinerary classes referenced
178 // by instructions. Set NumInstrSchedClasses to the number of itinerary
179 // classes plus any classes implied by instructions that derive from class
180 // Sched and provide SchedRW list. This does not infer any new classes from
181 // SchedVariant.
182 collectSchedClasses();
183
184 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000185 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000186 // all itinerary classes to be discovered.
187 collectProcItins();
188
189 // Find ItinRW records for each processor and itinerary class.
190 // (For per-operand resources mapped to itinerary classes).
191 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000192
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000193 // Find UnsupportedFeatures records for each processor.
194 // (For per-operand resources mapped to itinerary classes).
195 collectProcUnsupportedFeatures();
196
Andrew Trick33401e82012-09-15 00:19:59 +0000197 // Infer new SchedClasses from SchedVariant.
198 inferSchedClasses();
199
Andrew Trick1e46d482012-09-15 00:20:02 +0000200 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
201 // ProcResourceDefs.
Joel Jones80372332017-06-28 00:06:40 +0000202 DEBUG(dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000203 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000204
205 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000206}
207
Andrew Trick76686492012-09-15 00:19:57 +0000208/// Gather all processor models.
209void CodeGenSchedModels::collectProcModels() {
210 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
211 std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000212
Andrew Trick76686492012-09-15 00:19:57 +0000213 // Reserve space because we can. Reallocation would be ok.
214 ProcModels.reserve(ProcRecords.size()+1);
215
216 // Use idx=0 for NoModel/NoItineraries.
217 Record *NoModelDef = Records.getDef("NoSchedModel");
218 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000219 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000220 ProcModelMap[NoModelDef] = 0;
221
222 // For each processor, find a unique machine model.
Joel Jones80372332017-06-28 00:06:40 +0000223 DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000224 for (Record *ProcRecord : ProcRecords)
225 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000226}
227
228/// Get a unique processor model based on the defined MachineModel and
229/// ProcessorItineraries.
230void CodeGenSchedModels::addProcModel(Record *ProcDef) {
231 Record *ModelKey = getModelOrItinDef(ProcDef);
232 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
233 return;
234
235 std::string Name = ModelKey->getName();
236 if (ModelKey->isSubClassOf("SchedMachineModel")) {
237 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000238 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000239 }
240 else {
241 // An itinerary is defined without a machine model. Infer a new model.
242 if (!ModelKey->getValueAsListOfDefs("IID").empty())
243 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000244 ProcModels.emplace_back(ProcModels.size(), Name,
245 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000246 }
247 DEBUG(ProcModels.back().dump());
248}
249
250// Recursively find all reachable SchedReadWrite records.
251static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
252 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000253 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000254 return;
255 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000256 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000257 if (RWDef->isSubClassOf("WriteSequence")) {
258 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000259 for (Record *WSRec : Seq)
260 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000261 }
262 else if (RWDef->isSubClassOf("SchedVariant")) {
263 // Visit each variant (guarded by a different predicate).
264 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000265 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000266 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000267 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
268 for (Record *SelDef : Selected)
269 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000270 }
271 }
272}
273
274// Collect and sort all SchedReadWrites reachable via tablegen records.
275// More may be inferred later when inferring new SchedClasses from variants.
276void CodeGenSchedModels::collectSchedRW() {
277 // Reserve idx=0 for invalid writes/reads.
278 SchedWrites.resize(1);
279 SchedReads.resize(1);
280
281 SmallPtrSet<Record*, 16> RWSet;
282
283 // Find all SchedReadWrites referenced by instruction defs.
284 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000285 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000286 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000287 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000288 continue;
289 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000290 for (Record *RW : RWs) {
291 if (RW->isSubClassOf("SchedWrite"))
292 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000293 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000294 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
295 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000296 }
297 }
298 }
299 // Find all ReadWrites referenced by InstRW.
300 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000301 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000302 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000303 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
304 for (Record *RWDef : RWDefs) {
305 if (RWDef->isSubClassOf("SchedWrite"))
306 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000307 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000308 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
309 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000310 }
311 }
312 }
313 // Find all ReadWrites referenced by ItinRW.
314 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000315 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000316 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000317 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
318 for (Record *RWDef : RWDefs) {
319 if (RWDef->isSubClassOf("SchedWrite"))
320 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000321 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000322 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
323 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000324 }
325 }
326 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000327 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
328 // for the loop below that initializes Alias vectors.
329 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
330 std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000331 for (Record *ADef : AliasDefs) {
332 Record *MatchDef = ADef->getValueAsDef("MatchRW");
333 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000334 if (MatchDef->isSubClassOf("SchedWrite")) {
335 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000336 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000337 scanSchedRW(AliasDef, SWDefs, RWSet);
338 }
339 else {
340 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
341 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000342 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000343 scanSchedRW(AliasDef, SRDefs, RWSet);
344 }
345 }
Andrew Trick76686492012-09-15 00:19:57 +0000346 // Sort and add the SchedReadWrites directly referenced by instructions or
347 // itinerary resources. Index reads and writes in separate domains.
348 std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000349 for (Record *SWDef : SWDefs) {
350 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
351 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000352 }
353 std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000354 for (Record *SRDef : SRDefs) {
355 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
356 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000357 }
358 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000359 for (CodeGenSchedRW &CGRW : SchedWrites) {
360 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000361 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000362 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000363 /*IsRead=*/false);
364 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000365 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000366 for (Record *ADef : AliasDefs) {
367 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000368 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000369 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000370 CodeGenSchedRW &RW = getSchedRW(MatchDef);
371 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000372 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
373 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000374 }
Andrew Trick76686492012-09-15 00:19:57 +0000375 DEBUG(
Joel Jones80372332017-06-28 00:06:40 +0000376 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
Andrew Trick76686492012-09-15 00:19:57 +0000377 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
378 dbgs() << WIdx << ": ";
379 SchedWrites[WIdx].dump();
380 dbgs() << '\n';
381 }
382 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
383 dbgs() << RIdx << ": ";
384 SchedReads[RIdx].dump();
385 dbgs() << '\n';
386 }
387 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
Javed Absar67b042c2017-09-13 10:31:10 +0000388 for (Record *RWDef : RWDefs) {
389 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
390 const std::string &Name = RWDef->getName();
Andrew Trick76686492012-09-15 00:19:57 +0000391 if (Name != "NoWrite" && Name != "ReadDefault")
Javed Absar67b042c2017-09-13 10:31:10 +0000392 dbgs() << "Unused SchedReadWrite " << RWDef->getName() << '\n';
Andrew Trick76686492012-09-15 00:19:57 +0000393 }
394 });
395}
396
397/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000398std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000399 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000400 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000401 if (I != Seq.begin())
402 Name += '_';
403 Name += getSchedRW(*I, IsRead).Name;
404 }
405 Name += ')';
406 return Name;
407}
408
Craig Toppere2611842018-03-21 05:13:04 +0000409unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead) const {
Andrew Trick76686492012-09-15 00:19:57 +0000410 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Craig Toppere2611842018-03-21 05:13:04 +0000411 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin(),
Andrew Trick76686492012-09-15 00:19:57 +0000412 E = RWVec.end(); I != E; ++I) {
413 if (I->TheDef == Def)
414 return I - RWVec.begin();
415 }
416 return 0;
417}
418
Andrew Trickcfe222c2012-09-19 04:43:19 +0000419bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000420 for (const CodeGenSchedRW &Read : SchedReads) {
421 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000422 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
423 continue;
424
425 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000426 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000427 return true;
428 }
429 }
430 return false;
431}
432
Craig Topper6f2cc9b2018-03-21 05:13:01 +0000433static void splitSchedReadWrites(const RecVec &RWDefs,
434 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000435 for (Record *RWDef : RWDefs) {
436 if (RWDef->isSubClassOf("SchedWrite"))
437 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000438 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000439 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
440 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000441 }
442 }
443}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000444
Andrew Trick76686492012-09-15 00:19:57 +0000445// Split the SchedReadWrites defs and call findRWs for each list.
446void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
447 IdxVec &Writes, IdxVec &Reads) const {
448 RecVec WriteDefs;
449 RecVec ReadDefs;
450 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
451 findRWs(WriteDefs, Writes, false);
452 findRWs(ReadDefs, Reads, true);
453}
454
455// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
456void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
457 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000458 for (Record *RWDef : RWDefs) {
459 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000460 assert(Idx && "failed to collect SchedReadWrite");
461 RWs.push_back(Idx);
462 }
463}
464
Andrew Trick33401e82012-09-15 00:19:59 +0000465void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
466 bool IsRead) const {
467 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
468 if (!SchedRW.IsSequence) {
469 RWSeq.push_back(RWIdx);
470 return;
471 }
472 int Repeat =
473 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
474 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000475 for (unsigned I : SchedRW.Sequence) {
476 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000477 }
478 }
479}
480
Andrew Trickda984b12012-10-03 23:06:28 +0000481// Expand a SchedWrite as a sequence following any aliases that coincide with
482// the given processor model.
483void CodeGenSchedModels::expandRWSeqForProc(
484 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
485 const CodeGenProcModel &ProcModel) const {
486
487 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000488 Record *AliasDef = nullptr;
Andrew Trickda984b12012-10-03 23:06:28 +0000489 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
490 AI != AE; ++AI) {
491 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
492 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
493 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
494 if (&getProcModel(ModelDef) != &ProcModel)
495 continue;
496 }
497 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000498 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
499 "defined for processor " + ProcModel.ModelName +
500 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000501 AliasDef = AliasRW.TheDef;
502 }
503 if (AliasDef) {
504 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
505 RWSeq, IsRead,ProcModel);
506 return;
507 }
508 if (!SchedWrite.IsSequence) {
509 RWSeq.push_back(RWIdx);
510 return;
511 }
512 int Repeat =
513 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
514 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000515 for (unsigned I : SchedWrite.Sequence) {
516 expandRWSeqForProc(I, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000517 }
518 }
519}
520
Andrew Trick33401e82012-09-15 00:19:59 +0000521// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000522unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000523 bool IsRead) {
524 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
525
526 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
527 I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000528 if (makeArrayRef(I->Sequence) == Seq)
Andrew Trick33401e82012-09-15 00:19:59 +0000529 return I - RWVec.begin();
530 }
531 // Index zero reserved for invalid RW.
532 return 0;
533}
534
535/// Add this ReadWrite if it doesn't already exist.
536unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
537 bool IsRead) {
538 assert(!Seq.empty() && "cannot insert empty sequence");
539 if (Seq.size() == 1)
540 return Seq.back();
541
542 unsigned Idx = findRWForSequence(Seq, IsRead);
543 if (Idx)
544 return Idx;
545
Andrew Trickda984b12012-10-03 23:06:28 +0000546 unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
547 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
548 if (IsRead)
Andrew Trick33401e82012-09-15 00:19:59 +0000549 SchedReads.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000550 else
551 SchedWrites.push_back(SchedRW);
552 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000553}
554
Andrew Trick76686492012-09-15 00:19:57 +0000555/// Visit all the instruction definitions for this target to gather and
556/// enumerate the itinerary classes. These are the explicitly specified
557/// SchedClasses. More SchedClasses may be inferred.
558void CodeGenSchedModels::collectSchedClasses() {
559
560 // NoItinerary is always the first class at Idx=0
Craig Topper281a19c2018-03-22 06:15:08 +0000561 assert(SchedClasses.empty() && "Expected empty sched class");
562 SchedClasses.emplace_back(0, "NoInstrModel",
563 Records.getDef("NoItinerary"));
Andrew Trick76686492012-09-15 00:19:57 +0000564 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000565
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000566 // Create a SchedClass for each unique combination of itinerary class and
567 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000568 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000569 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000570 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000571 if (!Inst->TheDef->isValueUnset("SchedRW"))
572 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000573
Andrew Trick76686492012-09-15 00:19:57 +0000574 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topper281a19c2018-03-22 06:15:08 +0000575 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topper8a417c12014-12-09 08:05:51 +0000576 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000577 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000578 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000579 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
580 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000581 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000582 for (Record *RWDef : InstRWDefs)
583 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000584
Andrew Trick76686492012-09-15 00:19:57 +0000585 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000586
Andrew Trick76686492012-09-15 00:19:57 +0000587 bool EnableDump = false;
588 DEBUG(EnableDump = true);
589 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000590 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000591
Joel Jones80372332017-06-28 00:06:40 +0000592 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000593 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000594 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim949437e2018-03-21 18:09:34 +0000595 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000596 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000597 if (!Inst->hasNoSchedulingInfo)
598 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000599 continue;
600 }
601 CodeGenSchedClass &SC = getSchedClass(SCIdx);
602 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000603 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000604 "must not be subtarget specific.");
605
606 IdxVec ProcIndices;
607 if (SC.ItinClassDef->getName() != "NoItinerary") {
608 ProcIndices.push_back(0);
609 dbgs() << "Itinerary for " << InstName << ": "
610 << SC.ItinClassDef->getName() << '\n';
611 }
612 if (!SC.Writes.empty()) {
613 ProcIndices.push_back(0);
614 dbgs() << "SchedRW machine model for " << InstName;
615 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
616 dbgs() << " " << SchedWrites[*WI].Name;
617 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
618 dbgs() << " " << SchedReads[*RI].Name;
619 dbgs() << '\n';
620 }
621 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000622 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000623 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000624 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000625 ProcIndices.push_back(ProcModel.Index);
626 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000627 IdxVec Writes;
628 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000629 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000630 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000631 for (unsigned WIdx : Writes)
632 dbgs() << " " << SchedWrites[WIdx].Name;
633 for (unsigned RIdx : Reads)
634 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000635 dbgs() << '\n';
636 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000637 // If ProcIndices contains zero, the class applies to all processors.
638 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
Javed Absar21c75912017-10-09 16:21:25 +0000639 for (const CodeGenProcModel &PM : ProcModels) {
Javed Absarfc500042017-10-05 13:27:43 +0000640 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
Andrew Trickf9df92c92016-10-18 04:17:44 +0000641 dbgs() << "No machine model for " << Inst->TheDef->getName()
Javed Absarfc500042017-10-05 13:27:43 +0000642 << " on processor " << PM.ModelName << '\n';
Andrew Trickf9df92c92016-10-18 04:17:44 +0000643 }
Andrew Trick87255e32012-07-07 04:00:00 +0000644 }
645 }
Andrew Trick76686492012-09-15 00:19:57 +0000646}
647
Andrew Trick76686492012-09-15 00:19:57 +0000648/// Find an SchedClass that has been inferred from a per-operand list of
649/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000650unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000651 ArrayRef<unsigned> Writes,
652 ArrayRef<unsigned> Reads) const {
Simon Pilgrim4cca3b12018-03-21 17:57:21 +0000653 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I)
654 if (I->isKeyEqual(ItinClassDef, Writes, Reads))
Andrew Trick76686492012-09-15 00:19:57 +0000655 return I - schedClassBegin();
Andrew Trick76686492012-09-15 00:19:57 +0000656 return 0;
657}
Andrew Trick87255e32012-07-07 04:00:00 +0000658
Andrew Trick76686492012-09-15 00:19:57 +0000659// Get the SchedClass index for an instruction.
660unsigned CodeGenSchedModels::getSchedClassIdx(
661 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000662
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000663 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000664}
665
Benjamin Kramere1761952015-10-24 12:46:49 +0000666std::string
667CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
668 ArrayRef<unsigned> OperWrites,
669 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000670
671 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000672 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
673 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000674 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000675 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000676 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000677 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000678 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000679 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000680 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000681 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000682 }
683 return Name;
684}
685
686std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
687
688 std::string Name;
689 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
690 if (I != InstDefs.begin())
691 Name += '_';
692 Name += (*I)->getName();
693 }
694 return Name;
695}
696
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000697/// Add an inferred sched class from an itinerary class and per-operand list of
698/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
699/// processors that may utilize this class.
700unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000701 ArrayRef<unsigned> OperWrites,
702 ArrayRef<unsigned> OperReads,
703 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000704 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
705
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000706 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
707 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000708 IdxVec PI;
709 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
710 SchedClasses[Idx].ProcIndices.end(),
711 ProcIndices.begin(), ProcIndices.end(),
712 std::back_inserter(PI));
713 SchedClasses[Idx].ProcIndices.swap(PI);
714 return Idx;
715 }
716 Idx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000717 SchedClasses.emplace_back(Idx,
718 createSchedClassName(ItinClassDef, OperWrites,
719 OperReads),
720 ItinClassDef);
Andrew Trick76686492012-09-15 00:19:57 +0000721 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick76686492012-09-15 00:19:57 +0000722 SC.Writes = OperWrites;
723 SC.Reads = OperReads;
724 SC.ProcIndices = ProcIndices;
725
726 return Idx;
727}
728
729// Create classes for each set of opcodes that are in the same InstReadWrite
730// definition across all processors.
731void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
732 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
733 // intersects with an existing class via a previous InstRWDef. Instrs that do
734 // not intersect with an existing class refer back to their former class as
735 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +0000736 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000737 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000738 const RecVec *InstDefs = Sets.expand(InstRWDef);
739 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000740 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000741
Craig Topper93dd77d2018-03-18 08:38:03 +0000742 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +0000743 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000744 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +0000745 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000746 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +0000747 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +0000748 }
749 // For each set of Instrs, create a new class if necessary, and map or remap
750 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +0000751 for (auto &Entry : ClassInstrs) {
752 unsigned OldSCIdx = Entry.first;
753 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +0000754 // If the all instrs in the current class are accounted for, then leave
755 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000756 if (OldSCIdx) {
757 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
758 if (!RWDefs.empty()) {
759 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper06d78372018-03-21 19:30:30 +0000760 unsigned OrigNumInstrs =
761 count_if(*OrigInstDefs, [&](Record *OIDef) {
762 return InstrClassMap[OIDef] == OldSCIdx;
763 });
Andrew Trick78a08512013-06-05 06:55:20 +0000764 if (OrigNumInstrs == InstDefs.size()) {
765 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
766 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000767 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
768 // Make sure we didn't already have a InstRW containing this
769 // instruction on this model.
770 for (Record *RWD : RWDefs) {
771 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
772 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
773 for (Record *Inst : InstDefs) {
774 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
775 Inst->getName() + " also matches " +
776 RWD->getValue("Instrs")->getValue()->getAsString());
777 }
778 }
779 }
Andrew Trick78a08512013-06-05 06:55:20 +0000780 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
781 << SchedClasses[OldSCIdx].Name << " on "
Craig Toppere1d6a4d2018-03-18 19:56:15 +0000782 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +0000783 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
784 continue;
785 }
786 }
Andrew Trick76686492012-09-15 00:19:57 +0000787 }
788 unsigned SCIdx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +0000789 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick76686492012-09-15 00:19:57 +0000790 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick78a08512013-06-05 06:55:20 +0000791 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
792 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
793
Andrew Trick76686492012-09-15 00:19:57 +0000794 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
795 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
796 SC.Writes = SchedClasses[OldSCIdx].Writes;
797 SC.Reads = SchedClasses[OldSCIdx].Reads;
798 SC.ProcIndices.push_back(0);
Craig Topper989d94d2018-03-21 19:52:13 +0000799 // If we had an old class, copy it's InstRWs to this new class.
800 if (OldSCIdx) {
801 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
802 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
803 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
804 for (Record *InstDef : InstDefs) {
Craig Topper9fbbe5d2018-03-21 19:30:31 +0000805 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
806 InstDef->getName() + " also matches " +
807 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick9e1deb62012-10-03 23:06:32 +0000808 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000809 }
Craig Topper989d94d2018-03-21 19:52:13 +0000810 assert(OldRWDef != InstRWDef &&
811 "SchedClass has duplicate InstRW def");
812 SC.InstRWs.push_back(OldRWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000813 }
Andrew Trick76686492012-09-15 00:19:57 +0000814 }
Craig Topper989d94d2018-03-21 19:52:13 +0000815 // Map each Instr to this new class.
816 for (Record *InstDef : InstDefs)
817 InstrClassMap[InstDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000818 SC.InstRWs.push_back(InstRWDef);
819 }
Andrew Trick87255e32012-07-07 04:00:00 +0000820}
821
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000822// True if collectProcItins found anything.
823bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000824 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
825 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000826 return true;
827 }
828 return false;
829}
830
Andrew Trick87255e32012-07-07 04:00:00 +0000831// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000832void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000833 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000834 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000835 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000836 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000837
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000838 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
839 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
840
841 // Populate ItinDefList with Itinerary records.
842 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000843
844 // Insert each itinerary data record in the correct position within
845 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +0000846 for (Record *ItinData : ItinRecords) {
Andrew Trick76686492012-09-15 00:19:57 +0000847 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000848 bool FoundClass = false;
849 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
850 SCI != SCE; ++SCI) {
851 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000852 if (SCI->ItinClassDef == ItinDef) {
853 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000854 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000855 }
Andrew Trick76686492012-09-15 00:19:57 +0000856 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000857 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000858 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
859 << " missing class for itinerary " << ItinDef->getName() << '\n');
860 }
Andrew Trick87255e32012-07-07 04:00:00 +0000861 }
Andrew Trick76686492012-09-15 00:19:57 +0000862 // Check for missing itinerary entries.
863 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
864 DEBUG(
865 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
866 if (!ProcModel.ItinDefList[i])
867 dbgs() << ProcModel.ItinsDef->getName()
868 << " missing itinerary for class "
869 << SchedClasses[i].Name << '\n';
870 });
Andrew Trick87255e32012-07-07 04:00:00 +0000871 }
Andrew Trick87255e32012-07-07 04:00:00 +0000872}
Andrew Trick76686492012-09-15 00:19:57 +0000873
874// Gather the read/write types for each itinerary class.
875void CodeGenSchedModels::collectProcItinRW() {
876 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
877 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +0000878 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000879 if (!RWDef->getValueInit("SchedModel")->isComplete())
880 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
881 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +0000882 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
883 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +0000884 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000885 + ModelDef->getName());
886 }
Javed Absarf45d0b92017-10-08 17:23:30 +0000887 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000888 }
889}
890
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000891// Gather the unsupported features for processor models.
892void CodeGenSchedModels::collectProcUnsupportedFeatures() {
893 for (CodeGenProcModel &ProcModel : ProcModels) {
894 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
895 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
896 }
897 }
898}
899
Andrew Trick33401e82012-09-15 00:19:59 +0000900/// Infer new classes from existing classes. In the process, this may create new
901/// SchedWrites from sequences of existing SchedWrites.
902void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000903 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000904 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
905
Andrew Trick33401e82012-09-15 00:19:59 +0000906 // Visit all existing classes and newly created classes.
907 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000908 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
909
Andrew Trick33401e82012-09-15 00:19:59 +0000910 if (SchedClasses[Idx].ItinClassDef)
911 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000912 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000913 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000914 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000915 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
916 Idx, SchedClasses[Idx].ProcIndices);
917 }
918 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
919 "too many SchedVariants");
920 }
921}
922
923/// Infer classes from per-processor itinerary resources.
924void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
925 unsigned FromClassIdx) {
926 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
927 const CodeGenProcModel &PM = ProcModels[PIdx];
928 // For all ItinRW entries.
929 bool HasMatch = false;
930 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
931 II != IE; ++II) {
932 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
933 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
934 continue;
935 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000936 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000937 + ItinClassDef->getName()
938 + " in ItinResources for " + PM.ModelName);
939 HasMatch = true;
940 IdxVec Writes, Reads;
941 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
942 IdxVec ProcIndices(1, PIdx);
943 inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
944 }
945 }
946}
947
948/// Infer classes from per-processor InstReadWrite definitions.
949void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000950 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000951 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000952 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
953 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000954 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000955 for (; II != IE; ++II) {
956 if (InstrClassMap[*II] == SCIdx)
957 break;
958 }
959 // If this class no longer has any instructions mapped to it, it has become
960 // irrelevant.
961 if (II == IE)
962 continue;
963 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000964 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
965 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Andrew Trick33401e82012-09-15 00:19:59 +0000966 IdxVec ProcIndices(1, PIdx);
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000967 inferFromRW(Writes, Reads, SCIdx, ProcIndices); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +0000968 }
969}
970
971namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000972
Andrew Trick9257b8f2012-09-22 02:24:21 +0000973// Helper for substituteVariantOperand.
974struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +0000975 Record *VarOrSeqDef; // Variant or sequence.
976 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000977 unsigned ProcIdx; // Processor model index or zero for any.
978 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
979
980 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +0000981 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +0000982};
983
Andrew Trick33401e82012-09-15 00:19:59 +0000984// Associate a predicate with the SchedReadWrite that it guards.
985// RWIdx is the index of the read/write variant.
986struct PredCheck {
987 bool IsRead;
988 unsigned RWIdx;
989 Record *Predicate;
990
991 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
992};
993
994// A Predicate transition is a list of RW sequences guarded by a PredTerm.
995struct PredTransition {
996 // A predicate term is a conjunction of PredChecks.
997 SmallVector<PredCheck, 4> PredTerm;
998 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
999 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001000 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001001};
1002
1003// Encapsulate a set of partially constructed transitions.
1004// The results are built by repeated calls to substituteVariants.
1005class PredTransitions {
1006 CodeGenSchedModels &SchedModels;
1007
1008public:
1009 std::vector<PredTransition> TransVec;
1010
1011 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1012
1013 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1014 bool IsRead, unsigned StartIdx);
1015
1016 void substituteVariants(const PredTransition &Trans);
1017
1018#ifndef NDEBUG
1019 void dump() const;
1020#endif
1021
1022private:
1023 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001024 void getIntersectingVariants(
1025 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1026 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001027 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001028};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001029
1030} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001031
1032// Return true if this predicate is mutually exclusive with a PredTerm. This
1033// degenerates into checking if the predicate is mutually exclusive with any
1034// predicate in the Term's conjunction.
1035//
1036// All predicates associated with a given SchedRW are considered mutually
1037// exclusive. This should work even if the conditions expressed by the
1038// predicates are not exclusive because the predicates for a given SchedWrite
1039// are always checked in the order they are defined in the .td file. Later
1040// conditions implicitly negate any prior condition.
1041bool PredTransitions::mutuallyExclusive(Record *PredDef,
1042 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001043 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001044 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001045 return false;
1046
Javed Absarfc500042017-10-05 13:27:43 +00001047 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001048 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1049 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1050 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1051 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1052 return true;
1053 }
1054 }
1055 return false;
1056}
1057
Andrew Trickda984b12012-10-03 23:06:28 +00001058static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1059 CodeGenSchedModels &SchedModels) {
1060 if (RW.HasVariants)
1061 return true;
1062
Javed Absar21c75912017-10-09 16:21:25 +00001063 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001064 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001065 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001066 if (AliasRW.HasVariants)
1067 return true;
1068 if (AliasRW.IsSequence) {
1069 IdxVec ExpandedRWs;
1070 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1071 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1072 SI != SE; ++SI) {
1073 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1074 SchedModels)) {
1075 return true;
1076 }
1077 }
1078 }
1079 }
1080 return false;
1081}
1082
1083static bool hasVariant(ArrayRef<PredTransition> Transitions,
1084 CodeGenSchedModels &SchedModels) {
1085 for (ArrayRef<PredTransition>::iterator
1086 PTI = Transitions.begin(), PTE = Transitions.end();
1087 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001088 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001089 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1090 WSI != WSE; ++WSI) {
1091 for (SmallVectorImpl<unsigned>::const_iterator
1092 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1093 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1094 return true;
1095 }
1096 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001097 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001098 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1099 RSI != RSE; ++RSI) {
1100 for (SmallVectorImpl<unsigned>::const_iterator
1101 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1102 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1103 return true;
1104 }
1105 }
1106 }
1107 return false;
1108}
1109
1110// Populate IntersectingVariants with any variants or aliased sequences of the
1111// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001112// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001113void PredTransitions::getIntersectingVariants(
1114 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1115 std::vector<TransVariant> &IntersectingVariants) {
1116
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001117 bool GenericRW = false;
1118
Andrew Trickda984b12012-10-03 23:06:28 +00001119 std::vector<TransVariant> Variants;
1120 if (SchedRW.HasVariants) {
1121 unsigned VarProcIdx = 0;
1122 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1123 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1124 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1125 }
1126 // Push each variant. Assign TransVecIdx later.
1127 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001128 for (Record *VarDef : VarDefs)
1129 Variants.push_back(TransVariant(VarDef, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001130 if (VarProcIdx == 0)
1131 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001132 }
1133 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1134 AI != AE; ++AI) {
1135 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1136 // to is defined within a processor model, constrain all variants to
1137 // that processor.
1138 unsigned AliasProcIdx = 0;
1139 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1140 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1141 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1142 }
1143 const CodeGenSchedRW &AliasRW =
1144 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1145
1146 if (AliasRW.HasVariants) {
1147 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001148 for (Record *VD : VarDefs)
1149 Variants.push_back(TransVariant(VD, AliasRW.Index, AliasProcIdx, 0));
Andrew Trickda984b12012-10-03 23:06:28 +00001150 }
1151 if (AliasRW.IsSequence) {
1152 Variants.push_back(
1153 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1154 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001155 if (AliasProcIdx == 0)
1156 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001157 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001158 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001159 // Don't expand variants if the processor models don't intersect.
1160 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001161 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001162 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001163 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1164 Variant.ProcIdx);
1165 if (!Cnt)
1166 continue;
1167 if (Cnt > 1) {
1168 const CodeGenProcModel &PM =
1169 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001170 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1171 "Multiple variants defined for processor " +
1172 PM.ModelName +
1173 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001174 }
1175 }
1176 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1177 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1178 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1179 continue;
1180 }
1181 if (IntersectingVariants.empty()) {
1182 // The first variant builds on the existing transition.
1183 Variant.TransVecIdx = TransIdx;
1184 IntersectingVariants.push_back(Variant);
1185 }
1186 else {
1187 // Push another copy of the current transition for more variants.
1188 Variant.TransVecIdx = TransVec.size();
1189 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001190 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001191 }
1192 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001193 if (GenericRW && IntersectingVariants.empty()) {
1194 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1195 "a matching predicate on any processor");
1196 }
Andrew Trickda984b12012-10-03 23:06:28 +00001197}
1198
Andrew Trick9257b8f2012-09-22 02:24:21 +00001199// Push the Reads/Writes selected by this variant onto the PredTransition
1200// specified by VInfo.
1201void PredTransitions::
1202pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001203 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1204
Andrew Trick9257b8f2012-09-22 02:24:21 +00001205 // If this operand transition is reached through a processor-specific alias,
1206 // then the whole transition is specific to this processor.
1207 if (VInfo.ProcIdx != 0)
1208 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1209
Andrew Trick33401e82012-09-15 00:19:59 +00001210 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001211 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1212 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1213 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1214 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1215 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1216 }
1217 else {
1218 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1219 "variant must be a SchedVariant or aliased WriteSequence");
1220 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1221 }
Andrew Trick33401e82012-09-15 00:19:59 +00001222
Andrew Trick9257b8f2012-09-22 02:24:21 +00001223 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001224
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001225 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001226 ? Trans.ReadSequences : Trans.WriteSequences;
1227 if (SchedRW.IsVariadic) {
1228 unsigned OperIdx = RWSequences.size()-1;
1229 // Make N-1 copies of this transition's last sequence.
1230 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001231 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001232 RWSequences.reserve(RWSequences.size() + 1);
1233 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001234 }
1235 // Push each of the N elements of the SelectedRWs onto a copy of the last
1236 // sequence (split the current operand into N operands).
1237 // Note that write sequences should be expanded within this loop--the entire
1238 // sequence belongs to a single operand.
1239 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1240 RWI != RWE; ++RWI, ++OperIdx) {
1241 IdxVec ExpandedRWs;
1242 if (IsRead)
1243 ExpandedRWs.push_back(*RWI);
1244 else
1245 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1246 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1247 ExpandedRWs.begin(), ExpandedRWs.end());
1248 }
1249 assert(OperIdx == RWSequences.size() && "missed a sequence");
1250 }
1251 else {
1252 // Push this transition's expanded sequence onto this transition's last
1253 // sequence (add to the current operand's sequence).
1254 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1255 IdxVec ExpandedRWs;
1256 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1257 RWI != RWE; ++RWI) {
1258 if (IsRead)
1259 ExpandedRWs.push_back(*RWI);
1260 else
1261 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1262 }
1263 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1264 }
1265}
1266
1267// RWSeq is a sequence of all Reads or all Writes for the next read or write
1268// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001269// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001270// of TransVec.
1271void PredTransitions::substituteVariantOperand(
1272 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1273
1274 // Visit each original RW within the current sequence.
1275 for (SmallVectorImpl<unsigned>::const_iterator
1276 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1277 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1278 // Push this RW on all partial PredTransitions or distribute variants.
1279 // New PredTransitions may be pushed within this loop which should not be
1280 // revisited (TransEnd must be loop invariant).
1281 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1282 TransIdx != TransEnd; ++TransIdx) {
1283 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001284 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001285 if (IsRead)
1286 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1287 else
1288 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1289 continue;
1290 }
1291 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001292 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001293 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001294 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001295 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001296 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001297 IVI = IntersectingVariants.begin(),
1298 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001299 IVI != IVE; ++IVI) {
1300 pushVariant(*IVI, IsRead);
1301 }
Andrew Trick33401e82012-09-15 00:19:59 +00001302 }
1303 }
1304}
1305
1306// For each variant of a Read/Write in Trans, substitute the sequence of
1307// Read/Writes guarded by the variant. This is exponential in the number of
1308// variant Read/Writes, but in practice detection of mutually exclusive
1309// predicates should result in linear growth in the total number variants.
1310//
1311// This is one step in a breadth-first search of nested variants.
1312void PredTransitions::substituteVariants(const PredTransition &Trans) {
1313 // Build up a set of partial results starting at the back of
1314 // PredTransitions. Remember the first new transition.
1315 unsigned StartIdx = TransVec.size();
Craig Topper195aaaf2018-03-22 06:15:10 +00001316 TransVec.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001317 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001318 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001319
1320 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001321 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001322 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1323 WSI != WSE; ++WSI) {
1324 // Push a new (empty) write sequence onto all partial Transitions.
1325 for (std::vector<PredTransition>::iterator I =
1326 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001327 I->WriteSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001328 }
1329 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1330 }
1331 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001332 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001333 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1334 RSI != RSE; ++RSI) {
1335 // Push a new (empty) read sequence onto all partial Transitions.
1336 for (std::vector<PredTransition>::iterator I =
1337 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001338 I->ReadSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001339 }
1340 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1341 }
1342}
1343
Andrew Trick33401e82012-09-15 00:19:59 +00001344// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001345static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001346 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001347 CodeGenSchedModels &SchedModels) {
1348 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1349 // requires creating a new SchedClass.
1350 for (ArrayRef<PredTransition>::iterator
1351 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1352 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001353 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1354 [&SchedModels](ArrayRef<unsigned> WS) {
1355 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1356 });
Andrew Trick33401e82012-09-15 00:19:59 +00001357 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001358 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1359 [&SchedModels](ArrayRef<unsigned> RS) {
1360 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1361 });
Andrew Trick9257b8f2012-09-22 02:24:21 +00001362 IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001363 CodeGenSchedTransition SCTrans;
1364 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001365 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001366 OperReadsVariant, ProcIndices);
Andrew Trick33401e82012-09-15 00:19:59 +00001367 SCTrans.ProcIndices = ProcIndices;
1368 // The final PredTerm is unique set of predicates guarding the transition.
1369 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001370 transform(I->PredTerm, std::back_inserter(Preds),
1371 [](const PredCheck &P) {
1372 return P.Predicate;
1373 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001374 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001375 SCTrans.PredTerm = Preds;
1376 SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
1377 }
1378}
1379
Andrew Trick9257b8f2012-09-22 02:24:21 +00001380// Create new SchedClasses for the given ReadWrite list. If any of the
1381// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1382// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001383void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1384 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001385 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001386 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001387 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001388
1389 // Create a seed transition with an empty PredTerm and the expanded sequences
1390 // of SchedWrites for the current SchedClass.
1391 std::vector<PredTransition> LastTransitions;
Craig Topper195aaaf2018-03-22 06:15:10 +00001392 LastTransitions.emplace_back();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001393 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1394 ProcIndices.end());
1395
Benjamin Kramere1761952015-10-24 12:46:49 +00001396 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001397 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001398 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topper195aaaf2018-03-22 06:15:10 +00001399 LastTransitions[0].WriteSequences.emplace_back();
1400 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001401 Seq.append(WriteSeq.begin(), WriteSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001402 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1403 }
1404 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001405 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001406 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001407 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topper195aaaf2018-03-22 06:15:10 +00001408 LastTransitions[0].ReadSequences.emplace_back();
1409 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001410 Seq.append(ReadSeq.begin(), ReadSeq.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001411 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1412 }
1413 DEBUG(dbgs() << '\n');
1414
1415 // Collect all PredTransitions for individual operands.
1416 // Iterate until no variant writes remain.
1417 while (hasVariant(LastTransitions, *this)) {
1418 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001419 for (const PredTransition &Trans : LastTransitions)
1420 Transitions.substituteVariants(Trans);
Andrew Trick33401e82012-09-15 00:19:59 +00001421 DEBUG(Transitions.dump());
1422 LastTransitions.swap(Transitions.TransVec);
1423 }
1424 // If the first transition has no variants, nothing to do.
1425 if (LastTransitions[0].PredTerm.empty())
1426 return;
1427
1428 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1429 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001430 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001431}
1432
Andrew Trickcf398b22013-04-23 23:45:14 +00001433// Check if any processor resource group contains all resource records in
1434// SubUnits.
1435bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1436 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1437 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1438 continue;
1439 RecVec SuperUnits =
1440 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1441 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1442 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001443 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001444 break;
1445 }
1446 }
1447 if (RI == RE)
1448 return true;
1449 }
1450 return false;
1451}
1452
1453// Verify that overlapping groups have a common supergroup.
1454void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1455 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1456 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1457 continue;
1458 RecVec CheckUnits =
1459 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1460 for (unsigned j = i+1; j < e; ++j) {
1461 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1462 continue;
1463 RecVec OtherUnits =
1464 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1465 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1466 OtherUnits.begin(), OtherUnits.end())
1467 != CheckUnits.end()) {
1468 // CheckUnits and OtherUnits overlap
1469 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1470 CheckUnits.end());
1471 if (!hasSuperGroup(OtherUnits, PM)) {
1472 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1473 "proc resource group overlaps with "
1474 + PM.ProcResourceDefs[j]->getName()
1475 + " but no supergroup contains both.");
1476 }
1477 }
1478 }
1479 }
1480}
1481
Andrew Trick1e46d482012-09-15 00:20:02 +00001482// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1483void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001484 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1485 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1486
Andrew Trick1e46d482012-09-15 00:20:02 +00001487 // Add any subtarget-specific SchedReadWrites that are directly associated
1488 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1489 // determine which processors they apply to.
1490 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1491 SCI != SCE; ++SCI) {
1492 if (SCI->ItinClassDef)
1493 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001494 else {
1495 // This class may have a default ReadWrite list which can be overriden by
1496 // InstRW definitions.
1497 if (!SCI->InstRWs.empty()) {
1498 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1499 RWI != RWE; ++RWI) {
1500 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
1501 IdxVec ProcIndices(1, getProcModel(RWModelDef).Index);
1502 IdxVec Writes, Reads;
1503 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1504 Writes, Reads);
1505 collectRWResources(Writes, Reads, ProcIndices);
1506 }
1507 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001508 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001509 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001510 }
1511 // Add resources separately defined by each subtarget.
1512 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001513 for (Record *WR : WRDefs) {
1514 Record *ModelDef = WR->getValueAsDef("SchedModel");
1515 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001516 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001517 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001518 for (Record *SWR : SWRDefs) {
1519 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1520 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001521 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001522 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001523 for (Record *RA : RADefs) {
1524 Record *ModelDef = RA->getValueAsDef("SchedModel");
1525 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001526 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001527 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001528 for (Record *SRA : SRADefs) {
1529 if (SRA->getValueInit("SchedModel")->isComplete()) {
1530 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1531 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001532 }
1533 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001534 // Add ProcResGroups that are defined within this processor model, which may
1535 // not be directly referenced but may directly specify a buffer size.
1536 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001537 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001538 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001539 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001540 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1541 if (!is_contained(PM.ProcResourceDefs, PRG))
1542 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001543 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001544 // Add ProcResourceUnits unconditionally.
1545 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1546 if (!PRU->getValueInit("SchedModel")->isComplete())
1547 continue;
1548 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1549 if (!is_contained(PM.ProcResourceDefs, PRU))
1550 PM.ProcResourceDefs.push_back(PRU);
1551 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001552 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001553 for (CodeGenProcModel &PM : ProcModels) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001554 std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1555 LessRecord());
1556 std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1557 LessRecord());
1558 std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1559 LessRecord());
1560 DEBUG(
1561 PM.dump();
1562 dbgs() << "WriteResDefs: ";
1563 for (RecIter RI = PM.WriteResDefs.begin(),
1564 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1565 if ((*RI)->isSubClassOf("WriteRes"))
1566 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1567 else
1568 dbgs() << (*RI)->getName() << " ";
1569 }
1570 dbgs() << "\nReadAdvanceDefs: ";
1571 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1572 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1573 if ((*RI)->isSubClassOf("ReadAdvance"))
1574 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1575 else
1576 dbgs() << (*RI)->getName() << " ";
1577 }
1578 dbgs() << "\nProcResourceDefs: ";
1579 for (RecIter RI = PM.ProcResourceDefs.begin(),
1580 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1581 dbgs() << (*RI)->getName() << " ";
1582 }
1583 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001584 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001585 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001586
1587 ProcResourceDefs.clear();
1588 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001589}
1590
Matthias Braun17cb5792016-03-01 20:03:21 +00001591void CodeGenSchedModels::checkCompleteness() {
1592 bool Complete = true;
1593 bool HadCompleteModel = false;
1594 for (const CodeGenProcModel &ProcModel : procModels()) {
Matthias Braun17cb5792016-03-01 20:03:21 +00001595 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1596 continue;
1597 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1598 if (Inst->hasNoSchedulingInfo)
1599 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001600 if (ProcModel.isUnsupported(*Inst))
1601 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001602 unsigned SCIdx = getSchedClassIdx(*Inst);
1603 if (!SCIdx) {
1604 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1605 PrintError("No schedule information for instruction '"
1606 + Inst->TheDef->getName() + "'");
1607 Complete = false;
1608 }
1609 continue;
1610 }
1611
1612 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1613 if (!SC.Writes.empty())
1614 continue;
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001615 if (SC.ItinClassDef != nullptr &&
1616 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001617 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001618
1619 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001620 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1621 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1622 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001623 if (I == InstRWs.end()) {
1624 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1625 Inst->TheDef->getName() + "'");
1626 Complete = false;
1627 }
1628 }
1629 HadCompleteModel = true;
1630 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001631 if (!Complete) {
1632 errs() << "\n\nIncomplete schedule models found.\n"
1633 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1634 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1635 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001636 "you may temporarily use an empty list.\n"
1637 << "- Instructions related to unsupported features can be excluded with "
1638 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1639 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001640 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001641 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001642}
1643
Andrew Trick1e46d482012-09-15 00:20:02 +00001644// Collect itinerary class resources for each processor.
1645void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1646 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1647 const CodeGenProcModel &PM = ProcModels[PIdx];
1648 // For all ItinRW entries.
1649 bool HasMatch = false;
1650 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1651 II != IE; ++II) {
1652 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1653 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1654 continue;
1655 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001656 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1657 + ItinClassDef->getName()
1658 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001659 HasMatch = true;
1660 IdxVec Writes, Reads;
1661 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1662 IdxVec ProcIndices(1, PIdx);
1663 collectRWResources(Writes, Reads, ProcIndices);
1664 }
1665 }
1666}
1667
Andrew Trickd0b9c442012-10-10 05:43:13 +00001668void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001669 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001670 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1671 if (SchedRW.TheDef) {
1672 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001673 for (unsigned Idx : ProcIndices)
1674 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001675 }
1676 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001677 for (unsigned Idx : ProcIndices)
1678 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001679 }
1680 }
1681 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1682 AI != AE; ++AI) {
1683 IdxVec AliasProcIndices;
1684 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1685 AliasProcIndices.push_back(
1686 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1687 }
1688 else
1689 AliasProcIndices = ProcIndices;
1690 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1691 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1692
1693 IdxVec ExpandedRWs;
1694 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1695 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1696 SI != SE; ++SI) {
1697 collectRWResources(*SI, IsRead, AliasProcIndices);
1698 }
1699 }
1700}
Andrew Trick1e46d482012-09-15 00:20:02 +00001701
1702// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001703void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1704 ArrayRef<unsigned> Reads,
1705 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001706 for (unsigned Idx : Writes)
1707 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001708
Benjamin Kramere1761952015-10-24 12:46:49 +00001709 for (unsigned Idx : Reads)
1710 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001711}
1712
1713// Find the processor's resource units for this kind of resource.
1714Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001715 const CodeGenProcModel &PM,
1716 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00001717 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1718 return ProcResKind;
1719
Craig Topper24064772014-04-15 07:20:03 +00001720 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001721 assert(!ProcResourceDefs.empty());
1722 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001723
Javed Absar67b042c2017-09-13 10:31:10 +00001724 for (Record *ProcResDef : ProcResourceDefs) {
1725 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1726 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001727 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001728 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001729 "Multiple ProcessorResourceUnits associated with "
1730 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001731 }
Javed Absar67b042c2017-09-13 10:31:10 +00001732 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001733 }
1734 }
Javed Absar67b042c2017-09-13 10:31:10 +00001735 for (Record *ProcResGroup : ProcResGroups) {
1736 if (ProcResGroup == ProcResKind
1737 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001738 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001739 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00001740 "Multiple ProcessorResourceUnits associated with "
1741 + ProcResKind->getName());
1742 }
Javed Absar67b042c2017-09-13 10:31:10 +00001743 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001744 }
1745 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001746 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001747 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001748 "No ProcessorResources associated with "
1749 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001750 }
1751 return ProcUnitDef;
1752}
1753
1754// Iteratively add a resource and its super resources.
1755void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001756 CodeGenProcModel &PM,
1757 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001758 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001759 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00001760
1761 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001762 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001763 return;
1764
1765 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001766 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1767 return;
1768
Andrew Trick1e46d482012-09-15 00:20:02 +00001769 if (!ProcResUnits->getValueInit("Super")->isComplete())
1770 return;
1771
1772 ProcResKind = ProcResUnits->getValueAsDef("Super");
1773 }
1774}
1775
1776// Add resources for a SchedWrite to this processor if they don't exist.
1777void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001778 assert(PIdx && "don't add resources to an invalid Processor model");
1779
Andrew Trick1e46d482012-09-15 00:20:02 +00001780 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001781 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001782 return;
1783 WRDefs.push_back(ProcWriteResDef);
1784
1785 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1786 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1787 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1788 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00001789 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00001790 }
1791}
1792
1793// Add resources for a ReadAdvance to this processor if they don't exist.
1794void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1795 unsigned PIdx) {
1796 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001797 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001798 return;
1799 RADefs.push_back(ProcReadAdvanceDef);
1800}
1801
Andrew Trick8fa00f52012-09-17 22:18:43 +00001802unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001803 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001804 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001805 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1806 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001807 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001808 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001809}
1810
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001811bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1812 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1813 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1814 if (TheDef->getName() == PredDef->getName())
1815 return true;
1816 }
1817 }
1818 return false;
1819}
1820
Andrew Trick76686492012-09-15 00:19:57 +00001821#ifndef NDEBUG
1822void CodeGenProcModel::dump() const {
1823 dbgs() << Index << ": " << ModelName << " "
1824 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1825 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1826}
1827
1828void CodeGenSchedRW::dump() const {
1829 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1830 if (IsSequence) {
1831 dbgs() << "(";
1832 dumpIdxVec(Sequence);
1833 dbgs() << ")";
1834 }
1835}
1836
1837void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001838 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001839 << " Writes: ";
1840 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1841 SchedModels->getSchedWrite(Writes[i]).dump();
1842 if (i < N-1) {
1843 dbgs() << '\n';
1844 dbgs().indent(10);
1845 }
1846 }
1847 dbgs() << "\n Reads: ";
1848 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1849 SchedModels->getSchedRead(Reads[i]).dump();
1850 if (i < N-1) {
1851 dbgs() << '\n';
1852 dbgs().indent(10);
1853 }
1854 }
1855 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001856 if (!Transitions.empty()) {
1857 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001858 for (const CodeGenSchedTransition &Transition : Transitions) {
1859 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001860 }
1861 }
Andrew Trick76686492012-09-15 00:19:57 +00001862}
Andrew Trick33401e82012-09-15 00:19:59 +00001863
1864void PredTransitions::dump() const {
1865 dbgs() << "Expanded Variants:\n";
1866 for (std::vector<PredTransition>::const_iterator
1867 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1868 dbgs() << "{";
1869 for (SmallVectorImpl<PredCheck>::const_iterator
1870 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1871 PCI != PCE; ++PCI) {
1872 if (PCI != TI->PredTerm.begin())
1873 dbgs() << ", ";
1874 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1875 << ":" << PCI->Predicate->getName();
1876 }
1877 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001878 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001879 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1880 WSI != WSE; ++WSI) {
1881 dbgs() << "(";
1882 for (SmallVectorImpl<unsigned>::const_iterator
1883 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1884 if (WI != WSI->begin())
1885 dbgs() << ", ";
1886 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1887 }
1888 dbgs() << "),";
1889 }
1890 dbgs() << "}\n";
1891 }
1892}
Andrew Trick76686492012-09-15 00:19:57 +00001893#endif // NDEBUG