blob: e94ed760fc467efb8bf444b3b2671a8ce79a25e0 [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 {
Roman Tereshind760c202018-05-23 20:45:43 +000081 ArrayRef<const CodeGenInstruction *> Instructions =
82 Target.getInstructionsByEnumValue();
83
84 unsigned NumGeneric = Target.getNumFixedInstructions();
Roman Tereshin9e493182018-05-23 22:10:21 +000085 unsigned NumPseudos = Target.getNumPseudoInstructions();
Roman Tereshind760c202018-05-23 20:45:43 +000086 auto Generics = Instructions.slice(0, NumGeneric);
Roman Tereshin9e493182018-05-23 22:10:21 +000087 auto Pseudos = Instructions.slice(NumGeneric, NumPseudos);
88 auto NonPseudos = Instructions.slice(NumGeneric + NumPseudos);
Roman Tereshind760c202018-05-23 20:45:43 +000089
Javed Absarfc500042017-10-05 13:27:43 +000090 for (Init *Arg : make_range(Expr->arg_begin(), Expr->arg_end())) {
91 StringInit *SI = dyn_cast<StringInit>(Arg);
Juergen Ributzka05c5a932013-11-19 03:08:35 +000092 if (!SI)
Benjamin Kramercbce2f02018-01-23 23:05:04 +000093 PrintFatalError(Loc, "instregex requires pattern string: " +
94 Expr->getAsString());
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000095 StringRef Original = SI->getValue();
96
Benjamin Kramercbce2f02018-01-23 23:05:04 +000097 // Extract a prefix that we can binary search on.
98 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000099 auto FirstMeta = Original.find_first_of(RegexMetachars);
100
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000101 // Look for top-level | or ?. We cannot optimize them to binary search.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000102 if (removeParens(Original).find_first_of("|?") != std::string::npos)
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000103 FirstMeta = 0;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000104
105 Optional<Regex> Regexpr = None;
106 StringRef Prefix = Original.substr(0, FirstMeta);
Simon Pilgrim34d512e2018-03-24 21:04:20 +0000107 StringRef PatStr = Original.substr(FirstMeta);
108 if (!PatStr.empty()) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000109 // For the rest use a python-style prefix match.
Simon Pilgrim34d512e2018-03-24 21:04:20 +0000110 std::string pat = PatStr;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000111 if (pat[0] != '^') {
112 pat.insert(0, "^(");
113 pat.insert(pat.end(), ')');
114 }
115 Regexpr = Regex(pat);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000116 }
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000117
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000118 int NumMatches = 0;
119
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000120 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000121 for (auto *Inst : Generics) {
122 StringRef InstName = Inst->TheDef->getName();
123 if (InstName.startswith(Prefix) &&
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000124 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))) {
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000125 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000126 NumMatches++;
127 }
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000128 }
129
Roman Tereshin9e493182018-05-23 22:10:21 +0000130 // Target instructions are split into two ranges: pseudo instructions
131 // first, than non-pseudos. Each range is in lexicographical order
132 // sorted by name. Find the sub-ranges that start with our prefix.
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000133 struct Comp {
134 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
135 return LHS->TheDef->getName() < RHS;
136 }
137 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
138 return LHS < RHS->TheDef->getName() &&
139 !RHS->TheDef->getName().startswith(LHS);
140 }
141 };
Roman Tereshin9e493182018-05-23 22:10:21 +0000142 auto Range1 =
143 std::equal_range(Pseudos.begin(), Pseudos.end(), Prefix, Comp());
144 auto Range2 = std::equal_range(NonPseudos.begin(), NonPseudos.end(),
145 Prefix, Comp());
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000146
Roman Tereshin9e493182018-05-23 22:10:21 +0000147 // For these ranges we know that instruction names start with the prefix.
148 // Check if there's a regex that needs to be checked.
Roman Tereshind760c202018-05-23 20:45:43 +0000149 const auto HandleNonGeneric = [&](const CodeGenInstruction *Inst) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000150 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000151 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))) {
Craig Topper8a417c12014-12-09 08:05:51 +0000152 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000153 NumMatches++;
154 }
Roman Tereshind760c202018-05-23 20:45:43 +0000155 };
Roman Tereshin9e493182018-05-23 22:10:21 +0000156 std::for_each(Range1.first, Range1.second, HandleNonGeneric);
157 std::for_each(Range2.first, Range2.second, HandleNonGeneric);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000158
159 if (0 == NumMatches)
160 PrintFatalError(Loc, "instregex has no matches: " + Original);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000161 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000162 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000163};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000164
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000165} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000166
Andrew Trick76686492012-09-15 00:19:57 +0000167/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000168CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
169 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000170 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000171
Andrew Trick9e1deb62012-10-03 23:06:32 +0000172 Sets.addFieldExpander("InstRW", "Instrs");
173
174 // Allow Set evaluation to recognize the dags used in InstRW records:
175 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000176 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
177 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000178
Andrew Trick76686492012-09-15 00:19:57 +0000179 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
180 // that are explicitly referenced in tablegen records. Resources associated
181 // with each processor will be derived later. Populate ProcModelMap with the
182 // CodeGenProcModel instances.
183 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000184
Andrew Trick76686492012-09-15 00:19:57 +0000185 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
186 // defined, and populate SchedReads and SchedWrites vectors. Implicit
187 // SchedReadWrites that represent sequences derived from expanded variant will
188 // be inferred later.
189 collectSchedRW();
190
191 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
192 // required by an instruction definition, and populate SchedClassIdxMap. Set
193 // NumItineraryClasses to the number of explicit itinerary classes referenced
194 // by instructions. Set NumInstrSchedClasses to the number of itinerary
195 // classes plus any classes implied by instructions that derive from class
196 // Sched and provide SchedRW list. This does not infer any new classes from
197 // SchedVariant.
198 collectSchedClasses();
199
200 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000201 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000202 // all itinerary classes to be discovered.
203 collectProcItins();
204
205 // Find ItinRW records for each processor and itinerary class.
206 // (For per-operand resources mapped to itinerary classes).
207 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000208
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000209 // Find UnsupportedFeatures records for each processor.
210 // (For per-operand resources mapped to itinerary classes).
211 collectProcUnsupportedFeatures();
212
Andrew Trick33401e82012-09-15 00:19:59 +0000213 // Infer new SchedClasses from SchedVariant.
214 inferSchedClasses();
215
Andrew Trick1e46d482012-09-15 00:20:02 +0000216 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
217 // ProcResourceDefs.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000218 LLVM_DEBUG(
219 dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000220 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000221
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000222 // Collect optional processor description.
223 collectOptionalProcessorInfo();
224
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +0000225 // Check MCInstPredicate definitions.
226 checkMCInstPredicates();
227
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000228 // Check STIPredicate definitions.
229 checkSTIPredicates();
230
231 // Find STIPredicate definitions for each processor model, and construct
232 // STIPredicateFunction objects.
233 collectSTIPredicates();
234
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000235 checkCompleteness();
236}
237
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000238void CodeGenSchedModels::checkSTIPredicates() const {
239 DenseMap<StringRef, const Record *> Declarations;
240
241 // There cannot be multiple declarations with the same name.
242 const RecVec Decls = Records.getAllDerivedDefinitions("STIPredicateDecl");
243 for (const Record *R : Decls) {
244 StringRef Name = R->getValueAsString("Name");
245 const auto It = Declarations.find(Name);
246 if (It == Declarations.end()) {
247 Declarations[Name] = R;
248 continue;
249 }
250
251 PrintError(R->getLoc(), "STIPredicate " + Name + " multiply declared.");
252 PrintNote(It->second->getLoc(), "Previous declaration was here.");
253 PrintFatalError(R->getLoc(), "Invalid STIPredicateDecl found.");
254 }
255
256 // Disallow InstructionEquivalenceClasses with an empty instruction list.
257 const RecVec Defs =
258 Records.getAllDerivedDefinitions("InstructionEquivalenceClass");
259 for (const Record *R : Defs) {
260 RecVec Opcodes = R->getValueAsListOfDefs("Opcodes");
261 if (Opcodes.empty()) {
262 PrintFatalError(R->getLoc(), "Invalid InstructionEquivalenceClass "
263 "defined with an empty opcode list.");
264 }
265 }
266}
267
268// Used by function `processSTIPredicate` to construct a mask of machine
269// instruction operands.
270static APInt constructOperandMask(ArrayRef<int64_t> Indices) {
271 APInt OperandMask;
272 if (Indices.empty())
273 return OperandMask;
274
275 int64_t MaxIndex = *std::max_element(Indices.begin(), Indices.end());
276 assert(MaxIndex >= 0 && "Invalid negative indices in input!");
277 OperandMask = OperandMask.zext(MaxIndex + 1);
278 for (const int64_t Index : Indices) {
279 assert(Index >= 0 && "Invalid negative indices!");
280 OperandMask.setBit(Index);
281 }
282
283 return OperandMask;
284}
285
286static void
287processSTIPredicate(STIPredicateFunction &Fn,
288 const DenseMap<Record *, unsigned> &ProcModelMap) {
289 DenseMap<const Record *, unsigned> Opcode2Index;
290 using OpcodeMapPair = std::pair<const Record *, OpcodeInfo>;
291 std::vector<OpcodeMapPair> OpcodeMappings;
292 std::vector<std::pair<APInt, APInt>> OpcodeMasks;
293
294 DenseMap<const Record *, unsigned> Predicate2Index;
295 unsigned NumUniquePredicates = 0;
296
297 // Number unique predicates and opcodes used by InstructionEquivalenceClass
298 // definitions. Each unique opcode will be associated with an OpcodeInfo
299 // object.
300 for (const Record *Def : Fn.getDefinitions()) {
301 RecVec Classes = Def->getValueAsListOfDefs("Classes");
302 for (const Record *EC : Classes) {
303 const Record *Pred = EC->getValueAsDef("Predicate");
304 if (Predicate2Index.find(Pred) == Predicate2Index.end())
305 Predicate2Index[Pred] = NumUniquePredicates++;
306
307 RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
308 for (const Record *Opcode : Opcodes) {
309 if (Opcode2Index.find(Opcode) == Opcode2Index.end()) {
310 Opcode2Index[Opcode] = OpcodeMappings.size();
311 OpcodeMappings.emplace_back(Opcode, OpcodeInfo());
312 }
313 }
314 }
315 }
316
317 // Initialize vector `OpcodeMasks` with default values. We want to keep track
318 // of which processors "use" which opcodes. We also want to be able to
319 // identify predicates that are used by different processors for a same
320 // opcode.
321 // This information is used later on by this algorithm to sort OpcodeMapping
322 // elements based on their processor and predicate sets.
323 OpcodeMasks.resize(OpcodeMappings.size());
324 APInt DefaultProcMask(ProcModelMap.size(), 0);
325 APInt DefaultPredMask(NumUniquePredicates, 0);
326 for (std::pair<APInt, APInt> &MaskPair : OpcodeMasks)
327 MaskPair = std::make_pair(DefaultProcMask, DefaultPredMask);
328
329 // Construct a OpcodeInfo object for every unique opcode declared by an
330 // InstructionEquivalenceClass definition.
331 for (const Record *Def : Fn.getDefinitions()) {
332 RecVec Classes = Def->getValueAsListOfDefs("Classes");
333 const Record *SchedModel = Def->getValueAsDef("SchedModel");
334 unsigned ProcIndex = ProcModelMap.find(SchedModel)->second;
335 APInt ProcMask(ProcModelMap.size(), 0);
336 ProcMask.setBit(ProcIndex);
337
338 for (const Record *EC : Classes) {
339 RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
340
341 std::vector<int64_t> OpIndices =
342 EC->getValueAsListOfInts("OperandIndices");
343 APInt OperandMask = constructOperandMask(OpIndices);
344
345 const Record *Pred = EC->getValueAsDef("Predicate");
346 APInt PredMask(NumUniquePredicates, 0);
347 PredMask.setBit(Predicate2Index[Pred]);
348
349 for (const Record *Opcode : Opcodes) {
350 unsigned OpcodeIdx = Opcode2Index[Opcode];
351 if (OpcodeMasks[OpcodeIdx].first[ProcIndex]) {
352 std::string Message =
353 "Opcode " + Opcode->getName().str() +
354 " used by multiple InstructionEquivalenceClass definitions.";
355 PrintFatalError(EC->getLoc(), Message);
356 }
357 OpcodeMasks[OpcodeIdx].first |= ProcMask;
358 OpcodeMasks[OpcodeIdx].second |= PredMask;
359 OpcodeInfo &OI = OpcodeMappings[OpcodeIdx].second;
360
361 OI.addPredicateForProcModel(ProcMask, OperandMask, Pred);
362 }
363 }
364 }
365
366 // Sort OpcodeMappings elements based on their CPU and predicate masks.
367 // As a last resort, order elements by opcode identifier.
Fangrui Song0cac7262018-09-27 02:13:45 +0000368 llvm::sort(OpcodeMappings,
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000369 [&](const OpcodeMapPair &Lhs, const OpcodeMapPair &Rhs) {
370 unsigned LhsIdx = Opcode2Index[Lhs.first];
371 unsigned RhsIdx = Opcode2Index[Rhs.first];
372 std::pair<APInt, APInt> &LhsMasks = OpcodeMasks[LhsIdx];
373 std::pair<APInt, APInt> &RhsMasks = OpcodeMasks[RhsIdx];
374
375 if (LhsMasks.first != RhsMasks.first) {
376 if (LhsMasks.first.countPopulation() <
377 RhsMasks.first.countPopulation())
378 return true;
379 return LhsMasks.first.countLeadingZeros() >
380 RhsMasks.first.countLeadingZeros();
381 }
382
383 if (LhsMasks.second != RhsMasks.second) {
384 if (LhsMasks.second.countPopulation() <
385 RhsMasks.second.countPopulation())
386 return true;
387 return LhsMasks.second.countLeadingZeros() >
388 RhsMasks.second.countLeadingZeros();
389 }
390
391 return LhsIdx < RhsIdx;
392 });
393
394 // Now construct opcode groups. Groups are used by the SubtargetEmitter when
395 // expanding the body of a STIPredicate function. In particular, each opcode
396 // group is expanded into a sequence of labels in a switch statement.
397 // It identifies opcodes for which different processors define same predicates
398 // and same opcode masks.
399 for (OpcodeMapPair &Info : OpcodeMappings)
400 Fn.addOpcode(Info.first, std::move(Info.second));
401}
402
403void CodeGenSchedModels::collectSTIPredicates() {
404 // Map STIPredicateDecl records to elements of vector
405 // CodeGenSchedModels::STIPredicates.
406 DenseMap<const Record *, unsigned> Decl2Index;
407
408 RecVec RV = Records.getAllDerivedDefinitions("STIPredicate");
409 for (const Record *R : RV) {
410 const Record *Decl = R->getValueAsDef("Declaration");
411
412 const auto It = Decl2Index.find(Decl);
413 if (It == Decl2Index.end()) {
414 Decl2Index[Decl] = STIPredicates.size();
415 STIPredicateFunction Predicate(Decl);
416 Predicate.addDefinition(R);
417 STIPredicates.emplace_back(std::move(Predicate));
418 continue;
419 }
420
421 STIPredicateFunction &PreviousDef = STIPredicates[It->second];
422 PreviousDef.addDefinition(R);
423 }
424
425 for (STIPredicateFunction &Fn : STIPredicates)
426 processSTIPredicate(Fn, ProcModelMap);
427}
428
429void OpcodeInfo::addPredicateForProcModel(const llvm::APInt &CpuMask,
430 const llvm::APInt &OperandMask,
431 const Record *Predicate) {
432 auto It = llvm::find_if(
433 Predicates, [&OperandMask, &Predicate](const PredicateInfo &P) {
434 return P.Predicate == Predicate && P.OperandMask == OperandMask;
435 });
436 if (It == Predicates.end()) {
437 Predicates.emplace_back(CpuMask, OperandMask, Predicate);
438 return;
439 }
440 It->ProcModelMask |= CpuMask;
441}
442
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +0000443void CodeGenSchedModels::checkMCInstPredicates() const {
444 RecVec MCPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
445 if (MCPredicates.empty())
446 return;
447
448 // A target cannot have multiple TIIPredicate definitions with a same name.
449 llvm::StringMap<const Record *> TIIPredicates(MCPredicates.size());
450 for (const Record *TIIPred : MCPredicates) {
451 StringRef Name = TIIPred->getValueAsString("FunctionName");
452 StringMap<const Record *>::const_iterator It = TIIPredicates.find(Name);
453 if (It == TIIPredicates.end()) {
454 TIIPredicates[Name] = TIIPred;
455 continue;
456 }
457
458 PrintError(TIIPred->getLoc(),
459 "TIIPredicate " + Name + " is multiply defined.");
460 PrintNote(It->second->getLoc(),
461 " Previous definition of " + Name + " was here.");
462 PrintFatalError(TIIPred->getLoc(),
463 "Found conflicting definitions of TIIPredicate.");
464 }
465}
466
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000467void CodeGenSchedModels::collectRetireControlUnits() {
468 RecVec Units = Records.getAllDerivedDefinitions("RetireControlUnit");
469
470 for (Record *RCU : Units) {
471 CodeGenProcModel &PM = getProcModel(RCU->getValueAsDef("SchedModel"));
472 if (PM.RetireControlUnit) {
473 PrintError(RCU->getLoc(),
474 "Expected a single RetireControlUnit definition");
475 PrintNote(PM.RetireControlUnit->getLoc(),
476 "Previous definition of RetireControlUnit was here");
477 }
478 PM.RetireControlUnit = RCU;
479 }
480}
481
482/// Collect optional processor information.
483void CodeGenSchedModels::collectOptionalProcessorInfo() {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000484 // Find register file definitions for each processor.
485 collectRegisterFiles();
486
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000487 // Collect processor RetireControlUnit descriptors if available.
488 collectRetireControlUnits();
Clement Courbetb4493792018-04-10 08:16:37 +0000489
490 // Find pfm counter definitions for each processor.
491 collectPfmCounters();
492
493 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000494}
495
Andrew Trick76686492012-09-15 00:19:57 +0000496/// Gather all processor models.
497void CodeGenSchedModels::collectProcModels() {
498 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +0000499 llvm::sort(ProcRecords, LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000500
Andrew Trick76686492012-09-15 00:19:57 +0000501 // Reserve space because we can. Reallocation would be ok.
502 ProcModels.reserve(ProcRecords.size()+1);
503
504 // Use idx=0 for NoModel/NoItineraries.
505 Record *NoModelDef = Records.getDef("NoSchedModel");
506 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000507 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000508 ProcModelMap[NoModelDef] = 0;
509
510 // For each processor, find a unique machine model.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000511 LLVM_DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000512 for (Record *ProcRecord : ProcRecords)
513 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000514}
515
516/// Get a unique processor model based on the defined MachineModel and
517/// ProcessorItineraries.
518void CodeGenSchedModels::addProcModel(Record *ProcDef) {
519 Record *ModelKey = getModelOrItinDef(ProcDef);
520 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
521 return;
522
523 std::string Name = ModelKey->getName();
524 if (ModelKey->isSubClassOf("SchedMachineModel")) {
525 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000526 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000527 }
528 else {
529 // An itinerary is defined without a machine model. Infer a new model.
530 if (!ModelKey->getValueAsListOfDefs("IID").empty())
531 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000532 ProcModels.emplace_back(ProcModels.size(), Name,
533 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000534 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000535 LLVM_DEBUG(ProcModels.back().dump());
Andrew Trick76686492012-09-15 00:19:57 +0000536}
537
538// Recursively find all reachable SchedReadWrite records.
539static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
540 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000541 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000542 return;
543 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000544 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000545 if (RWDef->isSubClassOf("WriteSequence")) {
546 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000547 for (Record *WSRec : Seq)
548 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000549 }
550 else if (RWDef->isSubClassOf("SchedVariant")) {
551 // Visit each variant (guarded by a different predicate).
552 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000553 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000554 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000555 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
556 for (Record *SelDef : Selected)
557 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000558 }
559 }
560}
561
562// Collect and sort all SchedReadWrites reachable via tablegen records.
563// More may be inferred later when inferring new SchedClasses from variants.
564void CodeGenSchedModels::collectSchedRW() {
565 // Reserve idx=0 for invalid writes/reads.
566 SchedWrites.resize(1);
567 SchedReads.resize(1);
568
569 SmallPtrSet<Record*, 16> RWSet;
570
571 // Find all SchedReadWrites referenced by instruction defs.
572 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000573 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000574 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000575 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000576 continue;
577 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000578 for (Record *RW : RWs) {
579 if (RW->isSubClassOf("SchedWrite"))
580 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000581 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000582 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
583 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000584 }
585 }
586 }
587 // Find all ReadWrites referenced by InstRW.
588 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000589 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000590 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000591 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
592 for (Record *RWDef : RWDefs) {
593 if (RWDef->isSubClassOf("SchedWrite"))
594 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000595 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000596 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
597 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000598 }
599 }
600 }
601 // Find all ReadWrites referenced by ItinRW.
602 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000603 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000604 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000605 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
606 for (Record *RWDef : RWDefs) {
607 if (RWDef->isSubClassOf("SchedWrite"))
608 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000609 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000610 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
611 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000612 }
613 }
614 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000615 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
616 // for the loop below that initializes Alias vectors.
617 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
Fangrui Song0cac7262018-09-27 02:13:45 +0000618 llvm::sort(AliasDefs, LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000619 for (Record *ADef : AliasDefs) {
620 Record *MatchDef = ADef->getValueAsDef("MatchRW");
621 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000622 if (MatchDef->isSubClassOf("SchedWrite")) {
623 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000624 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000625 scanSchedRW(AliasDef, SWDefs, RWSet);
626 }
627 else {
628 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
629 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000630 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000631 scanSchedRW(AliasDef, SRDefs, RWSet);
632 }
633 }
Andrew Trick76686492012-09-15 00:19:57 +0000634 // Sort and add the SchedReadWrites directly referenced by instructions or
635 // itinerary resources. Index reads and writes in separate domains.
Fangrui Song0cac7262018-09-27 02:13:45 +0000636 llvm::sort(SWDefs, LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000637 for (Record *SWDef : SWDefs) {
638 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
639 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000640 }
Fangrui Song0cac7262018-09-27 02:13:45 +0000641 llvm::sort(SRDefs, LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000642 for (Record *SRDef : SRDefs) {
643 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
644 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000645 }
646 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000647 for (CodeGenSchedRW &CGRW : SchedWrites) {
648 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000649 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000650 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000651 /*IsRead=*/false);
652 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000653 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000654 for (Record *ADef : AliasDefs) {
655 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000656 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000657 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000658 CodeGenSchedRW &RW = getSchedRW(MatchDef);
659 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000660 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
661 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000662 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000663 LLVM_DEBUG(
664 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
665 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
666 dbgs() << WIdx << ": ";
667 SchedWrites[WIdx].dump();
668 dbgs() << '\n';
669 } for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd;
670 ++RIdx) {
671 dbgs() << RIdx << ": ";
672 SchedReads[RIdx].dump();
673 dbgs() << '\n';
674 } RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
675 for (Record *RWDef
676 : RWDefs) {
677 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
678 StringRef Name = RWDef->getName();
679 if (Name != "NoWrite" && Name != "ReadDefault")
680 dbgs() << "Unused SchedReadWrite " << Name << '\n';
681 }
682 });
Andrew Trick76686492012-09-15 00:19:57 +0000683}
684
685/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000686std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000687 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000688 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000689 if (I != Seq.begin())
690 Name += '_';
691 Name += getSchedRW(*I, IsRead).Name;
692 }
693 Name += ')';
694 return Name;
695}
696
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000697unsigned CodeGenSchedModels::getSchedRWIdx(const Record *Def,
698 bool IsRead) const {
Andrew Trick76686492012-09-15 00:19:57 +0000699 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000700 const auto I = find_if(
701 RWVec, [Def](const CodeGenSchedRW &RW) { return RW.TheDef == Def; });
702 return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
Andrew Trick76686492012-09-15 00:19:57 +0000703}
704
Andrew Trickcfe222c2012-09-19 04:43:19 +0000705bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000706 for (const CodeGenSchedRW &Read : SchedReads) {
707 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000708 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
709 continue;
710
711 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000712 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000713 return true;
714 }
715 }
716 return false;
717}
718
Craig Topper6f2cc9b2018-03-21 05:13:01 +0000719static void splitSchedReadWrites(const RecVec &RWDefs,
720 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000721 for (Record *RWDef : RWDefs) {
722 if (RWDef->isSubClassOf("SchedWrite"))
723 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000724 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000725 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
726 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000727 }
728 }
729}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000730
Andrew Trick76686492012-09-15 00:19:57 +0000731// Split the SchedReadWrites defs and call findRWs for each list.
732void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
733 IdxVec &Writes, IdxVec &Reads) const {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000734 RecVec WriteDefs;
735 RecVec ReadDefs;
736 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
737 findRWs(WriteDefs, Writes, false);
738 findRWs(ReadDefs, Reads, true);
Andrew Trick76686492012-09-15 00:19:57 +0000739}
740
741// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
742void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
743 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000744 for (Record *RWDef : RWDefs) {
745 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000746 assert(Idx && "failed to collect SchedReadWrite");
747 RWs.push_back(Idx);
748 }
749}
750
Andrew Trick33401e82012-09-15 00:19:59 +0000751void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
752 bool IsRead) const {
753 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
754 if (!SchedRW.IsSequence) {
755 RWSeq.push_back(RWIdx);
756 return;
757 }
758 int Repeat =
759 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
760 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000761 for (unsigned I : SchedRW.Sequence) {
762 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000763 }
764 }
765}
766
Andrew Trickda984b12012-10-03 23:06:28 +0000767// Expand a SchedWrite as a sequence following any aliases that coincide with
768// the given processor model.
769void CodeGenSchedModels::expandRWSeqForProc(
770 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
771 const CodeGenProcModel &ProcModel) const {
772
773 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000774 Record *AliasDef = nullptr;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000775 for (const Record *Rec : SchedWrite.Aliases) {
776 const CodeGenSchedRW &AliasRW = getSchedRW(Rec->getValueAsDef("AliasRW"));
777 if (Rec->getValueInit("SchedModel")->isComplete()) {
778 Record *ModelDef = Rec->getValueAsDef("SchedModel");
Andrew Trickda984b12012-10-03 23:06:28 +0000779 if (&getProcModel(ModelDef) != &ProcModel)
780 continue;
781 }
782 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000783 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
784 "defined for processor " + ProcModel.ModelName +
785 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000786 AliasDef = AliasRW.TheDef;
787 }
788 if (AliasDef) {
789 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
790 RWSeq, IsRead,ProcModel);
791 return;
792 }
793 if (!SchedWrite.IsSequence) {
794 RWSeq.push_back(RWIdx);
795 return;
796 }
797 int Repeat =
798 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000799 for (int I = 0, E = Repeat; I < E; ++I) {
800 for (unsigned Idx : SchedWrite.Sequence) {
801 expandRWSeqForProc(Idx, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000802 }
803 }
804}
805
Andrew Trick33401e82012-09-15 00:19:59 +0000806// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000807unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000808 bool IsRead) {
809 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
810
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000811 auto I = find_if(RWVec, [Seq](CodeGenSchedRW &RW) {
812 return makeArrayRef(RW.Sequence) == Seq;
813 });
Andrew Trick33401e82012-09-15 00:19:59 +0000814 // Index zero reserved for invalid RW.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000815 return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
Andrew Trick33401e82012-09-15 00:19:59 +0000816}
817
818/// Add this ReadWrite if it doesn't already exist.
819unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
820 bool IsRead) {
821 assert(!Seq.empty() && "cannot insert empty sequence");
822 if (Seq.size() == 1)
823 return Seq.back();
824
825 unsigned Idx = findRWForSequence(Seq, IsRead);
826 if (Idx)
827 return Idx;
828
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000829 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
830 unsigned RWIdx = RWVec.size();
Andrew Trickda984b12012-10-03 23:06:28 +0000831 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000832 RWVec.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000833 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000834}
835
Andrew Trick76686492012-09-15 00:19:57 +0000836/// Visit all the instruction definitions for this target to gather and
837/// enumerate the itinerary classes. These are the explicitly specified
838/// SchedClasses. More SchedClasses may be inferred.
839void CodeGenSchedModels::collectSchedClasses() {
840
841 // NoItinerary is always the first class at Idx=0
Craig Topper281a19c2018-03-22 06:15:08 +0000842 assert(SchedClasses.empty() && "Expected empty sched class");
843 SchedClasses.emplace_back(0, "NoInstrModel",
844 Records.getDef("NoItinerary"));
Andrew Trick76686492012-09-15 00:19:57 +0000845 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000846
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000847 // Create a SchedClass for each unique combination of itinerary class and
848 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000849 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000850 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000851 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000852 if (!Inst->TheDef->isValueUnset("SchedRW"))
853 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000854
Andrew Trick76686492012-09-15 00:19:57 +0000855 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topper281a19c2018-03-22 06:15:08 +0000856 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topper8a417c12014-12-09 08:05:51 +0000857 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000858 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000859 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000860 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Fangrui Song0cac7262018-09-27 02:13:45 +0000861 llvm::sort(InstRWDefs, LessRecord());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000862 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000863 for (Record *RWDef : InstRWDefs)
864 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000865
Andrew Trick76686492012-09-15 00:19:57 +0000866 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000867
Andrew Trick76686492012-09-15 00:19:57 +0000868 bool EnableDump = false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000869 LLVM_DEBUG(EnableDump = true);
Andrew Trick76686492012-09-15 00:19:57 +0000870 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000871 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000872
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000873 LLVM_DEBUG(
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000874 dbgs()
875 << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n");
Craig Topper8cc904d2016-01-17 20:38:18 +0000876 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000877 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim949437e2018-03-21 18:09:34 +0000878 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000879 if (!SCIdx) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000880 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000881 if (!Inst->hasNoSchedulingInfo)
882 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
883 });
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000884 continue;
885 }
886 CodeGenSchedClass &SC = getSchedClass(SCIdx);
887 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000888 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000889 "must not be subtarget specific.");
890
891 IdxVec ProcIndices;
892 if (SC.ItinClassDef->getName() != "NoItinerary") {
893 ProcIndices.push_back(0);
894 dbgs() << "Itinerary for " << InstName << ": "
895 << SC.ItinClassDef->getName() << '\n';
896 }
897 if (!SC.Writes.empty()) {
898 ProcIndices.push_back(0);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000899 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000900 dbgs() << "SchedRW machine model for " << InstName;
901 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE;
902 ++WI)
903 dbgs() << " " << SchedWrites[*WI].Name;
904 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
905 dbgs() << " " << SchedReads[*RI].Name;
906 dbgs() << '\n';
907 });
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000908 }
909 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000910 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000911 const CodeGenProcModel &ProcModel =
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000912 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000913 ProcIndices.push_back(ProcModel.Index);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000914 LLVM_DEBUG(dbgs() << "InstRW on " << ProcModel.ModelName << " for "
915 << InstName);
Andrew Trick76686492012-09-15 00:19:57 +0000916 IdxVec Writes;
917 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000918 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000919 Writes, Reads);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000920 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000921 for (unsigned WIdx : Writes)
922 dbgs() << " " << SchedWrites[WIdx].Name;
923 for (unsigned RIdx : Reads)
924 dbgs() << " " << SchedReads[RIdx].Name;
925 dbgs() << '\n';
926 });
Andrew Trick76686492012-09-15 00:19:57 +0000927 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000928 // If ProcIndices contains zero, the class applies to all processors.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000929 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000930 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
931 for (const CodeGenProcModel &PM : ProcModels) {
932 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
933 dbgs() << "No machine model for " << Inst->TheDef->getName()
934 << " on processor " << PM.ModelName << '\n';
935 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000936 }
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000937 });
Andrew Trick87255e32012-07-07 04:00:00 +0000938 }
Andrew Trick76686492012-09-15 00:19:57 +0000939}
940
Andrew Trick76686492012-09-15 00:19:57 +0000941// Get the SchedClass index for an instruction.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000942unsigned
943CodeGenSchedModels::getSchedClassIdx(const CodeGenInstruction &Inst) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000944 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000945}
946
Benjamin Kramere1761952015-10-24 12:46:49 +0000947std::string
948CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
949 ArrayRef<unsigned> OperWrites,
950 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000951
952 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000953 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
954 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000955 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000956 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000957 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000958 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000959 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000960 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000961 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000962 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000963 }
964 return Name;
965}
966
967std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
968
969 std::string Name;
970 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
971 if (I != InstDefs.begin())
972 Name += '_';
973 Name += (*I)->getName();
974 }
975 return Name;
976}
977
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000978/// Add an inferred sched class from an itinerary class and per-operand list of
979/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
980/// processors that may utilize this class.
981unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000982 ArrayRef<unsigned> OperWrites,
983 ArrayRef<unsigned> OperReads,
984 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000985 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
986
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000987 auto IsKeyEqual = [=](const CodeGenSchedClass &SC) {
988 return SC.isKeyEqual(ItinClassDef, OperWrites, OperReads);
989 };
990
991 auto I = find_if(make_range(schedClassBegin(), schedClassEnd()), IsKeyEqual);
992 unsigned Idx = I == schedClassEnd() ? 0 : std::distance(schedClassBegin(), I);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000993 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000994 IdxVec PI;
995 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
996 SchedClasses[Idx].ProcIndices.end(),
997 ProcIndices.begin(), ProcIndices.end(),
998 std::back_inserter(PI));
Craig Topper59d13772018-03-24 22:58:00 +0000999 SchedClasses[Idx].ProcIndices = std::move(PI);
Andrew Trick76686492012-09-15 00:19:57 +00001000 return Idx;
1001 }
1002 Idx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +00001003 SchedClasses.emplace_back(Idx,
1004 createSchedClassName(ItinClassDef, OperWrites,
1005 OperReads),
1006 ItinClassDef);
Andrew Trick76686492012-09-15 00:19:57 +00001007 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick76686492012-09-15 00:19:57 +00001008 SC.Writes = OperWrites;
1009 SC.Reads = OperReads;
1010 SC.ProcIndices = ProcIndices;
1011
1012 return Idx;
1013}
1014
1015// Create classes for each set of opcodes that are in the same InstReadWrite
1016// definition across all processors.
1017void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
1018 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
1019 // intersects with an existing class via a previous InstRWDef. Instrs that do
1020 // not intersect with an existing class refer back to their former class as
1021 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +00001022 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +00001023 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +00001024 const RecVec *InstDefs = Sets.expand(InstRWDef);
1025 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001026 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +00001027
Craig Topper93dd77d2018-03-18 08:38:03 +00001028 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +00001029 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001030 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +00001031 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001032 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +00001033 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +00001034 }
1035 // For each set of Instrs, create a new class if necessary, and map or remap
1036 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +00001037 for (auto &Entry : ClassInstrs) {
1038 unsigned OldSCIdx = Entry.first;
1039 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +00001040 // If the all instrs in the current class are accounted for, then leave
1041 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +00001042 if (OldSCIdx) {
1043 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
1044 if (!RWDefs.empty()) {
1045 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper06d78372018-03-21 19:30:30 +00001046 unsigned OrigNumInstrs =
1047 count_if(*OrigInstDefs, [&](Record *OIDef) {
1048 return InstrClassMap[OIDef] == OldSCIdx;
1049 });
Andrew Trick78a08512013-06-05 06:55:20 +00001050 if (OrigNumInstrs == InstDefs.size()) {
1051 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
1052 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +00001053 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1054 // Make sure we didn't already have a InstRW containing this
1055 // instruction on this model.
1056 for (Record *RWD : RWDefs) {
1057 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
1058 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
1059 for (Record *Inst : InstDefs) {
1060 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
1061 Inst->getName() + " also matches " +
1062 RWD->getValue("Instrs")->getValue()->getAsString());
1063 }
1064 }
1065 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001066 LLVM_DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
1067 << SchedClasses[OldSCIdx].Name << " on "
1068 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +00001069 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
1070 continue;
1071 }
1072 }
Andrew Trick76686492012-09-15 00:19:57 +00001073 }
1074 unsigned SCIdx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +00001075 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick76686492012-09-15 00:19:57 +00001076 CodeGenSchedClass &SC = SchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001077 LLVM_DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
1078 << InstRWDef->getValueAsDef("SchedModel")->getName()
1079 << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +00001080
Andrew Trick76686492012-09-15 00:19:57 +00001081 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
1082 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
1083 SC.Writes = SchedClasses[OldSCIdx].Writes;
1084 SC.Reads = SchedClasses[OldSCIdx].Reads;
1085 SC.ProcIndices.push_back(0);
Craig Topper989d94d2018-03-21 19:52:13 +00001086 // If we had an old class, copy it's InstRWs to this new class.
1087 if (OldSCIdx) {
1088 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1089 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
1090 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
1091 for (Record *InstDef : InstDefs) {
Craig Topper9fbbe5d2018-03-21 19:30:31 +00001092 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
1093 InstDef->getName() + " also matches " +
1094 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick9e1deb62012-10-03 23:06:32 +00001095 }
Andrew Trick9e1deb62012-10-03 23:06:32 +00001096 }
Craig Topper989d94d2018-03-21 19:52:13 +00001097 assert(OldRWDef != InstRWDef &&
1098 "SchedClass has duplicate InstRW def");
1099 SC.InstRWs.push_back(OldRWDef);
Andrew Trick76686492012-09-15 00:19:57 +00001100 }
Andrew Trick76686492012-09-15 00:19:57 +00001101 }
Craig Topper989d94d2018-03-21 19:52:13 +00001102 // Map each Instr to this new class.
1103 for (Record *InstDef : InstDefs)
1104 InstrClassMap[InstDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +00001105 SC.InstRWs.push_back(InstRWDef);
1106 }
Andrew Trick87255e32012-07-07 04:00:00 +00001107}
1108
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001109// True if collectProcItins found anything.
1110bool CodeGenSchedModels::hasItineraries() const {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001111 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd()))
Javed Absar67b042c2017-09-13 10:31:10 +00001112 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001113 return true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001114 return false;
1115}
1116
Andrew Trick87255e32012-07-07 04:00:00 +00001117// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +00001118void CodeGenSchedModels::collectProcItins() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001119 LLVM_DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +00001120 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001121 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +00001122 continue;
Andrew Trick76686492012-09-15 00:19:57 +00001123
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001124 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
1125 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
1126
1127 // Populate ItinDefList with Itinerary records.
1128 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +00001129
1130 // Insert each itinerary data record in the correct position within
1131 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +00001132 for (Record *ItinData : ItinRecords) {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001133 const Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001134 bool FoundClass = false;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001135
1136 for (const CodeGenSchedClass &SC :
1137 make_range(schedClassBegin(), schedClassEnd())) {
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001138 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001139 if (SC.ItinClassDef == ItinDef) {
1140 ProcModel.ItinDefList[SC.Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001141 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001142 }
Andrew Trick76686492012-09-15 00:19:57 +00001143 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001144 if (!FoundClass) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001145 LLVM_DEBUG(dbgs() << ProcModel.ItinsDef->getName()
1146 << " missing class for itinerary "
1147 << ItinDef->getName() << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001148 }
Andrew Trick87255e32012-07-07 04:00:00 +00001149 }
Andrew Trick76686492012-09-15 00:19:57 +00001150 // Check for missing itinerary entries.
1151 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001152 LLVM_DEBUG(
1153 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
1154 if (!ProcModel.ItinDefList[i])
1155 dbgs() << ProcModel.ItinsDef->getName()
1156 << " missing itinerary for class " << SchedClasses[i].Name
1157 << '\n';
1158 });
Andrew Trick87255e32012-07-07 04:00:00 +00001159 }
Andrew Trick87255e32012-07-07 04:00:00 +00001160}
Andrew Trick76686492012-09-15 00:19:57 +00001161
1162// Gather the read/write types for each itinerary class.
1163void CodeGenSchedModels::collectProcItinRW() {
1164 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Fangrui Song0cac7262018-09-27 02:13:45 +00001165 llvm::sort(ItinRWDefs, LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +00001166 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +00001167 if (!RWDef->getValueInit("SchedModel")->isComplete())
1168 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
1169 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +00001170 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
1171 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +00001172 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +00001173 + ModelDef->getName());
1174 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001175 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +00001176 }
1177}
1178
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001179// Gather the unsupported features for processor models.
1180void CodeGenSchedModels::collectProcUnsupportedFeatures() {
1181 for (CodeGenProcModel &ProcModel : ProcModels) {
1182 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
1183 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
1184 }
1185 }
1186}
1187
Andrew Trick33401e82012-09-15 00:19:59 +00001188/// Infer new classes from existing classes. In the process, this may create new
1189/// SchedWrites from sequences of existing SchedWrites.
1190void CodeGenSchedModels::inferSchedClasses() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001191 LLVM_DEBUG(
1192 dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
1193 LLVM_DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001194
Andrew Trick33401e82012-09-15 00:19:59 +00001195 // Visit all existing classes and newly created classes.
1196 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001197 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
1198
Andrew Trick33401e82012-09-15 00:19:59 +00001199 if (SchedClasses[Idx].ItinClassDef)
1200 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001201 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +00001202 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001203 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +00001204 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
1205 Idx, SchedClasses[Idx].ProcIndices);
1206 }
1207 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
1208 "too many SchedVariants");
1209 }
1210}
1211
1212/// Infer classes from per-processor itinerary resources.
1213void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
1214 unsigned FromClassIdx) {
1215 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1216 const CodeGenProcModel &PM = ProcModels[PIdx];
1217 // For all ItinRW entries.
1218 bool HasMatch = false;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001219 for (const Record *Rec : PM.ItinRWDefs) {
1220 RecVec Matched = Rec->getValueAsListOfDefs("MatchedItinClasses");
Andrew Trick33401e82012-09-15 00:19:59 +00001221 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1222 continue;
1223 if (HasMatch)
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001224 PrintFatalError(Rec->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +00001225 + ItinClassDef->getName()
1226 + " in ItinResources for " + PM.ModelName);
1227 HasMatch = true;
1228 IdxVec Writes, Reads;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001229 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001230 inferFromRW(Writes, Reads, FromClassIdx, PIdx);
Andrew Trick33401e82012-09-15 00:19:59 +00001231 }
1232 }
1233}
1234
1235/// Infer classes from per-processor InstReadWrite definitions.
1236void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +00001237 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +00001238 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +00001239 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
1240 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +00001241 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +00001242 for (; II != IE; ++II) {
1243 if (InstrClassMap[*II] == SCIdx)
1244 break;
1245 }
1246 // If this class no longer has any instructions mapped to it, it has become
1247 // irrelevant.
1248 if (II == IE)
1249 continue;
1250 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +00001251 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1252 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Craig Topper9f3293a2018-03-24 21:57:35 +00001253 inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +00001254 }
1255}
1256
1257namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001258
Andrew Trick9257b8f2012-09-22 02:24:21 +00001259// Helper for substituteVariantOperand.
1260struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +00001261 Record *VarOrSeqDef; // Variant or sequence.
1262 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001263 unsigned ProcIdx; // Processor model index or zero for any.
1264 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
1265
1266 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +00001267 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +00001268};
1269
Andrew Trick33401e82012-09-15 00:19:59 +00001270// Associate a predicate with the SchedReadWrite that it guards.
1271// RWIdx is the index of the read/write variant.
1272struct PredCheck {
1273 bool IsRead;
1274 unsigned RWIdx;
1275 Record *Predicate;
1276
1277 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
1278};
1279
1280// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1281struct PredTransition {
1282 // A predicate term is a conjunction of PredChecks.
1283 SmallVector<PredCheck, 4> PredTerm;
1284 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1285 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001286 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001287};
1288
1289// Encapsulate a set of partially constructed transitions.
1290// The results are built by repeated calls to substituteVariants.
1291class PredTransitions {
1292 CodeGenSchedModels &SchedModels;
1293
1294public:
1295 std::vector<PredTransition> TransVec;
1296
1297 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1298
1299 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1300 bool IsRead, unsigned StartIdx);
1301
1302 void substituteVariants(const PredTransition &Trans);
1303
1304#ifndef NDEBUG
1305 void dump() const;
1306#endif
1307
1308private:
1309 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001310 void getIntersectingVariants(
1311 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1312 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001313 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001314};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001315
1316} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001317
1318// Return true if this predicate is mutually exclusive with a PredTerm. This
1319// degenerates into checking if the predicate is mutually exclusive with any
1320// predicate in the Term's conjunction.
1321//
1322// All predicates associated with a given SchedRW are considered mutually
1323// exclusive. This should work even if the conditions expressed by the
1324// predicates are not exclusive because the predicates for a given SchedWrite
1325// are always checked in the order they are defined in the .td file. Later
1326// conditions implicitly negate any prior condition.
1327bool PredTransitions::mutuallyExclusive(Record *PredDef,
1328 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001329 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001330 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001331 return false;
1332
Javed Absarfc500042017-10-05 13:27:43 +00001333 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001334 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1335 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001336 if (any_of(Variants, [PredDef](const Record *R) {
1337 return R->getValueAsDef("Predicate") == PredDef;
1338 }))
1339 return true;
Andrew Trick33401e82012-09-15 00:19:59 +00001340 }
1341 return false;
1342}
1343
Andrew Trickda984b12012-10-03 23:06:28 +00001344static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1345 CodeGenSchedModels &SchedModels) {
1346 if (RW.HasVariants)
1347 return true;
1348
Javed Absar21c75912017-10-09 16:21:25 +00001349 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001350 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001351 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001352 if (AliasRW.HasVariants)
1353 return true;
1354 if (AliasRW.IsSequence) {
1355 IdxVec ExpandedRWs;
1356 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001357 for (unsigned SI : ExpandedRWs) {
1358 if (hasAliasedVariants(SchedModels.getSchedRW(SI, AliasRW.IsRead),
1359 SchedModels))
Andrew Trickda984b12012-10-03 23:06:28 +00001360 return true;
Andrew Trickda984b12012-10-03 23:06:28 +00001361 }
1362 }
1363 }
1364 return false;
1365}
1366
1367static bool hasVariant(ArrayRef<PredTransition> Transitions,
1368 CodeGenSchedModels &SchedModels) {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001369 for (const PredTransition &PTI : Transitions) {
1370 for (const SmallVectorImpl<unsigned> &WSI : PTI.WriteSequences)
1371 for (unsigned WI : WSI)
1372 if (hasAliasedVariants(SchedModels.getSchedWrite(WI), SchedModels))
Andrew Trickda984b12012-10-03 23:06:28 +00001373 return true;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001374
1375 for (const SmallVectorImpl<unsigned> &RSI : PTI.ReadSequences)
1376 for (unsigned RI : RSI)
1377 if (hasAliasedVariants(SchedModels.getSchedRead(RI), SchedModels))
Andrew Trickda984b12012-10-03 23:06:28 +00001378 return true;
Andrew Trickda984b12012-10-03 23:06:28 +00001379 }
1380 return false;
1381}
1382
1383// Populate IntersectingVariants with any variants or aliased sequences of the
1384// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001385// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001386void PredTransitions::getIntersectingVariants(
1387 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1388 std::vector<TransVariant> &IntersectingVariants) {
1389
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001390 bool GenericRW = false;
1391
Andrew Trickda984b12012-10-03 23:06:28 +00001392 std::vector<TransVariant> Variants;
1393 if (SchedRW.HasVariants) {
1394 unsigned VarProcIdx = 0;
1395 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1396 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1397 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1398 }
1399 // Push each variant. Assign TransVecIdx later.
1400 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001401 for (Record *VarDef : VarDefs)
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001402 Variants.emplace_back(VarDef, SchedRW.Index, VarProcIdx, 0);
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001403 if (VarProcIdx == 0)
1404 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001405 }
1406 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1407 AI != AE; ++AI) {
1408 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1409 // to is defined within a processor model, constrain all variants to
1410 // that processor.
1411 unsigned AliasProcIdx = 0;
1412 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1413 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1414 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1415 }
1416 const CodeGenSchedRW &AliasRW =
1417 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1418
1419 if (AliasRW.HasVariants) {
1420 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001421 for (Record *VD : VarDefs)
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001422 Variants.emplace_back(VD, AliasRW.Index, AliasProcIdx, 0);
Andrew Trickda984b12012-10-03 23:06:28 +00001423 }
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001424 if (AliasRW.IsSequence)
1425 Variants.emplace_back(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0);
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001426 if (AliasProcIdx == 0)
1427 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001428 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001429 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001430 // Don't expand variants if the processor models don't intersect.
1431 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001432 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001433 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001434 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1435 Variant.ProcIdx);
1436 if (!Cnt)
1437 continue;
1438 if (Cnt > 1) {
1439 const CodeGenProcModel &PM =
1440 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001441 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1442 "Multiple variants defined for processor " +
1443 PM.ModelName +
1444 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001445 }
1446 }
1447 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1448 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1449 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1450 continue;
1451 }
1452 if (IntersectingVariants.empty()) {
1453 // The first variant builds on the existing transition.
1454 Variant.TransVecIdx = TransIdx;
1455 IntersectingVariants.push_back(Variant);
1456 }
1457 else {
1458 // Push another copy of the current transition for more variants.
1459 Variant.TransVecIdx = TransVec.size();
1460 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001461 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001462 }
1463 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001464 if (GenericRW && IntersectingVariants.empty()) {
1465 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1466 "a matching predicate on any processor");
1467 }
Andrew Trickda984b12012-10-03 23:06:28 +00001468}
1469
Andrew Trick9257b8f2012-09-22 02:24:21 +00001470// Push the Reads/Writes selected by this variant onto the PredTransition
1471// specified by VInfo.
1472void PredTransitions::
1473pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001474 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1475
Andrew Trick9257b8f2012-09-22 02:24:21 +00001476 // If this operand transition is reached through a processor-specific alias,
1477 // then the whole transition is specific to this processor.
1478 if (VInfo.ProcIdx != 0)
1479 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1480
Andrew Trick33401e82012-09-15 00:19:59 +00001481 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001482 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1483 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001484 Trans.PredTerm.emplace_back(IsRead, VInfo.RWIdx,PredDef);
Andrew Trickda984b12012-10-03 23:06:28 +00001485 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1486 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1487 }
1488 else {
1489 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1490 "variant must be a SchedVariant or aliased WriteSequence");
1491 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1492 }
Andrew Trick33401e82012-09-15 00:19:59 +00001493
Andrew Trick9257b8f2012-09-22 02:24:21 +00001494 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001495
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001496 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001497 ? Trans.ReadSequences : Trans.WriteSequences;
1498 if (SchedRW.IsVariadic) {
1499 unsigned OperIdx = RWSequences.size()-1;
1500 // Make N-1 copies of this transition's last sequence.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001501 RWSequences.insert(RWSequences.end(), SelectedRWs.size() - 1,
1502 RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001503 // Push each of the N elements of the SelectedRWs onto a copy of the last
1504 // sequence (split the current operand into N operands).
1505 // Note that write sequences should be expanded within this loop--the entire
1506 // sequence belongs to a single operand.
1507 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1508 RWI != RWE; ++RWI, ++OperIdx) {
1509 IdxVec ExpandedRWs;
1510 if (IsRead)
1511 ExpandedRWs.push_back(*RWI);
1512 else
1513 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1514 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1515 ExpandedRWs.begin(), ExpandedRWs.end());
1516 }
1517 assert(OperIdx == RWSequences.size() && "missed a sequence");
1518 }
1519 else {
1520 // Push this transition's expanded sequence onto this transition's last
1521 // sequence (add to the current operand's sequence).
1522 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1523 IdxVec ExpandedRWs;
1524 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1525 RWI != RWE; ++RWI) {
1526 if (IsRead)
1527 ExpandedRWs.push_back(*RWI);
1528 else
1529 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1530 }
1531 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1532 }
1533}
1534
1535// RWSeq is a sequence of all Reads or all Writes for the next read or write
1536// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001537// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001538// of TransVec.
1539void PredTransitions::substituteVariantOperand(
1540 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1541
1542 // Visit each original RW within the current sequence.
1543 for (SmallVectorImpl<unsigned>::const_iterator
1544 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1545 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1546 // Push this RW on all partial PredTransitions or distribute variants.
1547 // New PredTransitions may be pushed within this loop which should not be
1548 // revisited (TransEnd must be loop invariant).
1549 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1550 TransIdx != TransEnd; ++TransIdx) {
1551 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001552 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001553 if (IsRead)
1554 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1555 else
1556 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1557 continue;
1558 }
1559 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001560 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001561 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001562 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001563 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001564 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001565 IVI = IntersectingVariants.begin(),
1566 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001567 IVI != IVE; ++IVI) {
1568 pushVariant(*IVI, IsRead);
1569 }
Andrew Trick33401e82012-09-15 00:19:59 +00001570 }
1571 }
1572}
1573
1574// For each variant of a Read/Write in Trans, substitute the sequence of
1575// Read/Writes guarded by the variant. This is exponential in the number of
1576// variant Read/Writes, but in practice detection of mutually exclusive
1577// predicates should result in linear growth in the total number variants.
1578//
1579// This is one step in a breadth-first search of nested variants.
1580void PredTransitions::substituteVariants(const PredTransition &Trans) {
1581 // Build up a set of partial results starting at the back of
1582 // PredTransitions. Remember the first new transition.
1583 unsigned StartIdx = TransVec.size();
Craig Topper195aaaf2018-03-22 06:15:10 +00001584 TransVec.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001585 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001586 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001587
1588 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001589 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001590 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1591 WSI != WSE; ++WSI) {
1592 // Push a new (empty) write sequence onto all partial Transitions.
1593 for (std::vector<PredTransition>::iterator I =
1594 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001595 I->WriteSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001596 }
1597 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1598 }
1599 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001600 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001601 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1602 RSI != RSE; ++RSI) {
1603 // Push a new (empty) read sequence onto all partial Transitions.
1604 for (std::vector<PredTransition>::iterator I =
1605 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001606 I->ReadSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001607 }
1608 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1609 }
1610}
1611
Andrew Trick33401e82012-09-15 00:19:59 +00001612// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001613static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001614 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001615 CodeGenSchedModels &SchedModels) {
1616 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1617 // requires creating a new SchedClass.
1618 for (ArrayRef<PredTransition>::iterator
1619 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1620 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001621 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1622 [&SchedModels](ArrayRef<unsigned> WS) {
1623 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1624 });
Andrew Trick33401e82012-09-15 00:19:59 +00001625 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001626 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1627 [&SchedModels](ArrayRef<unsigned> RS) {
1628 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1629 });
Andrew Trick33401e82012-09-15 00:19:59 +00001630 CodeGenSchedTransition SCTrans;
1631 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001632 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Craig Topper2ed54072018-03-24 22:58:03 +00001633 OperReadsVariant, I->ProcIndices);
1634 SCTrans.ProcIndices.assign(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001635 // The final PredTerm is unique set of predicates guarding the transition.
1636 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001637 transform(I->PredTerm, std::back_inserter(Preds),
1638 [](const PredCheck &P) {
1639 return P.Predicate;
1640 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001641 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Craig Topper18cfa2c2018-03-24 22:58:02 +00001642 SCTrans.PredTerm = std::move(Preds);
1643 SchedModels.getSchedClass(FromClassIdx)
1644 .Transitions.push_back(std::move(SCTrans));
Andrew Trick33401e82012-09-15 00:19:59 +00001645 }
1646}
1647
Andrew Trick9257b8f2012-09-22 02:24:21 +00001648// Create new SchedClasses for the given ReadWrite list. If any of the
1649// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1650// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001651void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1652 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001653 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001654 ArrayRef<unsigned> ProcIndices) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001655 LLVM_DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices);
1656 dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001657
1658 // Create a seed transition with an empty PredTerm and the expanded sequences
1659 // of SchedWrites for the current SchedClass.
1660 std::vector<PredTransition> LastTransitions;
Craig Topper195aaaf2018-03-22 06:15:10 +00001661 LastTransitions.emplace_back();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001662 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1663 ProcIndices.end());
1664
Benjamin Kramere1761952015-10-24 12:46:49 +00001665 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001666 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001667 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topper195aaaf2018-03-22 06:15:10 +00001668 LastTransitions[0].WriteSequences.emplace_back();
1669 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001670 Seq.append(WriteSeq.begin(), WriteSeq.end());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001671 LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001672 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001673 LLVM_DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001674 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001675 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001676 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topper195aaaf2018-03-22 06:15:10 +00001677 LastTransitions[0].ReadSequences.emplace_back();
1678 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001679 Seq.append(ReadSeq.begin(), ReadSeq.end());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001680 LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001681 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001682 LLVM_DEBUG(dbgs() << '\n');
Andrew Trick33401e82012-09-15 00:19:59 +00001683
1684 // Collect all PredTransitions for individual operands.
1685 // Iterate until no variant writes remain.
1686 while (hasVariant(LastTransitions, *this)) {
1687 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001688 for (const PredTransition &Trans : LastTransitions)
1689 Transitions.substituteVariants(Trans);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001690 LLVM_DEBUG(Transitions.dump());
Andrew Trick33401e82012-09-15 00:19:59 +00001691 LastTransitions.swap(Transitions.TransVec);
1692 }
1693 // If the first transition has no variants, nothing to do.
1694 if (LastTransitions[0].PredTerm.empty())
1695 return;
1696
1697 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1698 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001699 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001700}
1701
Andrew Trickcf398b22013-04-23 23:45:14 +00001702// Check if any processor resource group contains all resource records in
1703// SubUnits.
1704bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1705 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1706 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1707 continue;
1708 RecVec SuperUnits =
1709 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1710 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1711 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001712 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001713 break;
1714 }
1715 }
1716 if (RI == RE)
1717 return true;
1718 }
1719 return false;
1720}
1721
1722// Verify that overlapping groups have a common supergroup.
1723void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1724 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1725 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1726 continue;
1727 RecVec CheckUnits =
1728 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1729 for (unsigned j = i+1; j < e; ++j) {
1730 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1731 continue;
1732 RecVec OtherUnits =
1733 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1734 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1735 OtherUnits.begin(), OtherUnits.end())
1736 != CheckUnits.end()) {
1737 // CheckUnits and OtherUnits overlap
1738 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1739 CheckUnits.end());
1740 if (!hasSuperGroup(OtherUnits, PM)) {
1741 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1742 "proc resource group overlaps with "
1743 + PM.ProcResourceDefs[j]->getName()
1744 + " but no supergroup contains both.");
1745 }
1746 }
1747 }
1748 }
1749}
1750
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001751// Collect all the RegisterFile definitions available in this target.
1752void CodeGenSchedModels::collectRegisterFiles() {
1753 RecVec RegisterFileDefs = Records.getAllDerivedDefinitions("RegisterFile");
1754
1755 // RegisterFiles is the vector of CodeGenRegisterFile.
1756 for (Record *RF : RegisterFileDefs) {
1757 // For each register file definition, construct a CodeGenRegisterFile object
1758 // and add it to the appropriate scheduling model.
1759 CodeGenProcModel &PM = getProcModel(RF->getValueAsDef("SchedModel"));
1760 PM.RegisterFiles.emplace_back(CodeGenRegisterFile(RF->getName(),RF));
1761 CodeGenRegisterFile &CGRF = PM.RegisterFiles.back();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +00001762 CGRF.MaxMovesEliminatedPerCycle =
1763 RF->getValueAsInt("MaxMovesEliminatedPerCycle");
1764 CGRF.AllowZeroMoveEliminationOnly =
1765 RF->getValueAsBit("AllowZeroMoveEliminationOnly");
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001766
1767 // Now set the number of physical registers as well as the cost of registers
1768 // in each register class.
1769 CGRF.NumPhysRegs = RF->getValueAsInt("NumPhysRegs");
Andrea Di Biagiof455e352018-10-11 10:39:03 +00001770 if (!CGRF.NumPhysRegs) {
1771 PrintFatalError(RF->getLoc(),
1772 "Invalid RegisterFile with zero physical registers");
1773 }
1774
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001775 RecVec RegisterClasses = RF->getValueAsListOfDefs("RegClasses");
1776 std::vector<int64_t> RegisterCosts = RF->getValueAsListOfInts("RegCosts");
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +00001777 ListInit *MoveElimInfo = RF->getValueAsListInit("AllowMoveElimination");
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001778 for (unsigned I = 0, E = RegisterClasses.size(); I < E; ++I) {
1779 int Cost = RegisterCosts.size() > I ? RegisterCosts[I] : 1;
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +00001780
1781 bool AllowMoveElim = false;
1782 if (MoveElimInfo->size() > I) {
1783 BitInit *Val = cast<BitInit>(MoveElimInfo->getElement(I));
1784 AllowMoveElim = Val->getValue();
1785 }
1786
1787 CGRF.Costs.emplace_back(RegisterClasses[I], Cost, AllowMoveElim);
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001788 }
1789 }
1790}
1791
Clement Courbetb4493792018-04-10 08:16:37 +00001792// Collect all the RegisterFile definitions available in this target.
1793void CodeGenSchedModels::collectPfmCounters() {
1794 for (Record *Def : Records.getAllDerivedDefinitions("PfmIssueCounter")) {
1795 CodeGenProcModel &PM = getProcModel(Def->getValueAsDef("SchedModel"));
1796 PM.PfmIssueCounterDefs.emplace_back(Def);
1797 }
1798 for (Record *Def : Records.getAllDerivedDefinitions("PfmCycleCounter")) {
1799 CodeGenProcModel &PM = getProcModel(Def->getValueAsDef("SchedModel"));
1800 if (PM.PfmCycleCounterDef) {
1801 PrintFatalError(Def->getLoc(),
1802 "multiple cycle counters for " +
1803 Def->getValueAsDef("SchedModel")->getName());
1804 }
1805 PM.PfmCycleCounterDef = Def;
1806 }
Clement Courbet596c56f2018-09-26 11:22:56 +00001807 for (Record *Def : Records.getAllDerivedDefinitions("PfmUopsCounter")) {
1808 CodeGenProcModel &PM = getProcModel(Def->getValueAsDef("SchedModel"));
1809 if (PM.PfmUopsCounterDef) {
1810 PrintFatalError(Def->getLoc(),
1811 "multiple uops counters for " +
1812 Def->getValueAsDef("SchedModel")->getName());
1813 }
1814 PM.PfmUopsCounterDef = Def;
1815 }
Clement Courbetb4493792018-04-10 08:16:37 +00001816}
1817
Andrew Trick1e46d482012-09-15 00:20:02 +00001818// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1819void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001820 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1821 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1822
Andrew Trick1e46d482012-09-15 00:20:02 +00001823 // Add any subtarget-specific SchedReadWrites that are directly associated
1824 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1825 // determine which processors they apply to.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001826 for (const CodeGenSchedClass &SC :
1827 make_range(schedClassBegin(), schedClassEnd())) {
1828 if (SC.ItinClassDef) {
1829 collectItinProcResources(SC.ItinClassDef);
1830 continue;
Andrew Trick4fe440d2013-02-01 03:19:54 +00001831 }
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001832
1833 // This class may have a default ReadWrite list which can be overriden by
1834 // InstRW definitions.
1835 for (Record *RW : SC.InstRWs) {
1836 Record *RWModelDef = RW->getValueAsDef("SchedModel");
1837 unsigned PIdx = getProcModel(RWModelDef).Index;
1838 IdxVec Writes, Reads;
1839 findRWs(RW->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1840 collectRWResources(Writes, Reads, PIdx);
1841 }
1842
1843 collectRWResources(SC.Writes, SC.Reads, SC.ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001844 }
1845 // Add resources separately defined by each subtarget.
1846 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001847 for (Record *WR : WRDefs) {
1848 Record *ModelDef = WR->getValueAsDef("SchedModel");
1849 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001850 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001851 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001852 for (Record *SWR : SWRDefs) {
1853 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1854 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001855 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001856 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001857 for (Record *RA : RADefs) {
1858 Record *ModelDef = RA->getValueAsDef("SchedModel");
1859 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001860 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001861 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001862 for (Record *SRA : SRADefs) {
1863 if (SRA->getValueInit("SchedModel")->isComplete()) {
1864 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1865 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001866 }
1867 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001868 // Add ProcResGroups that are defined within this processor model, which may
1869 // not be directly referenced but may directly specify a buffer size.
1870 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001871 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001872 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001873 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001874 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1875 if (!is_contained(PM.ProcResourceDefs, PRG))
1876 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001877 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001878 // Add ProcResourceUnits unconditionally.
1879 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1880 if (!PRU->getValueInit("SchedModel")->isComplete())
1881 continue;
1882 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1883 if (!is_contained(PM.ProcResourceDefs, PRU))
1884 PM.ProcResourceDefs.push_back(PRU);
1885 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001886 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001887 for (CodeGenProcModel &PM : ProcModels) {
Fangrui Song3507c6e2018-09-30 22:31:29 +00001888 llvm::sort(PM.WriteResDefs, LessRecord());
1889 llvm::sort(PM.ReadAdvanceDefs, LessRecord());
1890 llvm::sort(PM.ProcResourceDefs, LessRecord());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001891 LLVM_DEBUG(
1892 PM.dump();
1893 dbgs() << "WriteResDefs: "; for (RecIter RI = PM.WriteResDefs.begin(),
1894 RE = PM.WriteResDefs.end();
1895 RI != RE; ++RI) {
1896 if ((*RI)->isSubClassOf("WriteRes"))
1897 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1898 else
1899 dbgs() << (*RI)->getName() << " ";
1900 } dbgs() << "\nReadAdvanceDefs: ";
1901 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1902 RE = PM.ReadAdvanceDefs.end();
1903 RI != RE; ++RI) {
1904 if ((*RI)->isSubClassOf("ReadAdvance"))
1905 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1906 else
1907 dbgs() << (*RI)->getName() << " ";
1908 } dbgs()
1909 << "\nProcResourceDefs: ";
1910 for (RecIter RI = PM.ProcResourceDefs.begin(),
1911 RE = PM.ProcResourceDefs.end();
1912 RI != RE; ++RI) { dbgs() << (*RI)->getName() << " "; } dbgs()
1913 << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001914 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001915 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001916
1917 ProcResourceDefs.clear();
1918 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001919}
1920
Matthias Braun17cb5792016-03-01 20:03:21 +00001921void CodeGenSchedModels::checkCompleteness() {
1922 bool Complete = true;
1923 bool HadCompleteModel = false;
1924 for (const CodeGenProcModel &ProcModel : procModels()) {
Simon Pilgrim1d793b82018-04-05 13:11:36 +00001925 const bool HasItineraries = ProcModel.hasItineraries();
Matthias Braun17cb5792016-03-01 20:03:21 +00001926 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1927 continue;
1928 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1929 if (Inst->hasNoSchedulingInfo)
1930 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001931 if (ProcModel.isUnsupported(*Inst))
1932 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001933 unsigned SCIdx = getSchedClassIdx(*Inst);
1934 if (!SCIdx) {
1935 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1936 PrintError("No schedule information for instruction '"
1937 + Inst->TheDef->getName() + "'");
1938 Complete = false;
1939 }
1940 continue;
1941 }
1942
1943 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1944 if (!SC.Writes.empty())
1945 continue;
Simon Pilgrim1d793b82018-04-05 13:11:36 +00001946 if (HasItineraries && SC.ItinClassDef != nullptr &&
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001947 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001948 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001949
1950 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001951 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1952 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1953 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001954 if (I == InstRWs.end()) {
1955 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1956 Inst->TheDef->getName() + "'");
1957 Complete = false;
1958 }
1959 }
1960 HadCompleteModel = true;
1961 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001962 if (!Complete) {
1963 errs() << "\n\nIncomplete schedule models found.\n"
1964 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1965 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1966 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001967 "you may temporarily use an empty list.\n"
1968 << "- Instructions related to unsupported features can be excluded with "
1969 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1970 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001971 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001972 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001973}
1974
Andrew Trick1e46d482012-09-15 00:20:02 +00001975// Collect itinerary class resources for each processor.
1976void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1977 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1978 const CodeGenProcModel &PM = ProcModels[PIdx];
1979 // For all ItinRW entries.
1980 bool HasMatch = false;
1981 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1982 II != IE; ++II) {
1983 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1984 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1985 continue;
1986 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001987 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1988 + ItinClassDef->getName()
1989 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001990 HasMatch = true;
1991 IdxVec Writes, Reads;
1992 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001993 collectRWResources(Writes, Reads, PIdx);
Andrew Trick1e46d482012-09-15 00:20:02 +00001994 }
1995 }
1996}
1997
Andrew Trickd0b9c442012-10-10 05:43:13 +00001998void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001999 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00002000 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
2001 if (SchedRW.TheDef) {
2002 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00002003 for (unsigned Idx : ProcIndices)
2004 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00002005 }
2006 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00002007 for (unsigned Idx : ProcIndices)
2008 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00002009 }
2010 }
2011 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
2012 AI != AE; ++AI) {
2013 IdxVec AliasProcIndices;
2014 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
2015 AliasProcIndices.push_back(
2016 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
2017 }
2018 else
2019 AliasProcIndices = ProcIndices;
2020 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
2021 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
2022
2023 IdxVec ExpandedRWs;
2024 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
2025 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
2026 SI != SE; ++SI) {
2027 collectRWResources(*SI, IsRead, AliasProcIndices);
2028 }
2029 }
2030}
Andrew Trick1e46d482012-09-15 00:20:02 +00002031
2032// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00002033void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
2034 ArrayRef<unsigned> Reads,
2035 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00002036 for (unsigned Idx : Writes)
2037 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00002038
Benjamin Kramere1761952015-10-24 12:46:49 +00002039 for (unsigned Idx : Reads)
2040 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00002041}
2042
2043// Find the processor's resource units for this kind of resource.
2044Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002045 const CodeGenProcModel &PM,
2046 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00002047 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
2048 return ProcResKind;
2049
Craig Topper24064772014-04-15 07:20:03 +00002050 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00002051 assert(!ProcResourceDefs.empty());
2052 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00002053
Javed Absar67b042c2017-09-13 10:31:10 +00002054 for (Record *ProcResDef : ProcResourceDefs) {
2055 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
2056 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00002057 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002058 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00002059 "Multiple ProcessorResourceUnits associated with "
2060 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00002061 }
Javed Absar67b042c2017-09-13 10:31:10 +00002062 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00002063 }
2064 }
Javed Absar67b042c2017-09-13 10:31:10 +00002065 for (Record *ProcResGroup : ProcResGroups) {
2066 if (ProcResGroup == ProcResKind
2067 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00002068 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002069 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00002070 "Multiple ProcessorResourceUnits associated with "
2071 + ProcResKind->getName());
2072 }
Javed Absar67b042c2017-09-13 10:31:10 +00002073 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00002074 }
2075 }
Andrew Trick1e46d482012-09-15 00:20:02 +00002076 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002077 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00002078 "No ProcessorResources associated with "
2079 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00002080 }
2081 return ProcUnitDef;
2082}
2083
2084// Iteratively add a resource and its super resources.
2085void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002086 CodeGenProcModel &PM,
2087 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002088 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002089 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00002090
2091 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00002092 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00002093 return;
2094
2095 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00002096 if (ProcResUnits->isSubClassOf("ProcResGroup"))
2097 return;
2098
Andrew Trick1e46d482012-09-15 00:20:02 +00002099 if (!ProcResUnits->getValueInit("Super")->isComplete())
2100 return;
2101
2102 ProcResKind = ProcResUnits->getValueAsDef("Super");
2103 }
2104}
2105
2106// Add resources for a SchedWrite to this processor if they don't exist.
2107void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00002108 assert(PIdx && "don't add resources to an invalid Processor model");
2109
Andrew Trick1e46d482012-09-15 00:20:02 +00002110 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00002111 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00002112 return;
2113 WRDefs.push_back(ProcWriteResDef);
2114
2115 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
2116 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
2117 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
2118 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002119 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00002120 }
2121}
2122
2123// Add resources for a ReadAdvance to this processor if they don't exist.
2124void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
2125 unsigned PIdx) {
2126 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00002127 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00002128 return;
2129 RADefs.push_back(ProcReadAdvanceDef);
2130}
2131
Andrew Trick8fa00f52012-09-17 22:18:43 +00002132unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00002133 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00002134 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00002135 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
2136 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00002137 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00002138 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00002139}
2140
Simon Dardis5f95c9a2016-06-24 08:43:27 +00002141bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
2142 for (const Record *TheDef : UnsupportedFeaturesDefs) {
2143 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
2144 if (TheDef->getName() == PredDef->getName())
2145 return true;
2146 }
2147 }
2148 return false;
2149}
2150
Andrew Trick76686492012-09-15 00:19:57 +00002151#ifndef NDEBUG
2152void CodeGenProcModel::dump() const {
2153 dbgs() << Index << ": " << ModelName << " "
2154 << (ModelDef ? ModelDef->getName() : "inferred") << " "
2155 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
2156}
2157
2158void CodeGenSchedRW::dump() const {
2159 dbgs() << Name << (IsVariadic ? " (V) " : " ");
2160 if (IsSequence) {
2161 dbgs() << "(";
2162 dumpIdxVec(Sequence);
2163 dbgs() << ")";
2164 }
2165}
2166
2167void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00002168 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00002169 << " Writes: ";
2170 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
2171 SchedModels->getSchedWrite(Writes[i]).dump();
2172 if (i < N-1) {
2173 dbgs() << '\n';
2174 dbgs().indent(10);
2175 }
2176 }
2177 dbgs() << "\n Reads: ";
2178 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
2179 SchedModels->getSchedRead(Reads[i]).dump();
2180 if (i < N-1) {
2181 dbgs() << '\n';
2182 dbgs().indent(10);
2183 }
2184 }
2185 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00002186 if (!Transitions.empty()) {
2187 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00002188 for (const CodeGenSchedTransition &Transition : Transitions) {
2189 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00002190 }
2191 }
Andrew Trick76686492012-09-15 00:19:57 +00002192}
Andrew Trick33401e82012-09-15 00:19:59 +00002193
2194void PredTransitions::dump() const {
2195 dbgs() << "Expanded Variants:\n";
2196 for (std::vector<PredTransition>::const_iterator
2197 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
2198 dbgs() << "{";
2199 for (SmallVectorImpl<PredCheck>::const_iterator
2200 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
2201 PCI != PCE; ++PCI) {
2202 if (PCI != TI->PredTerm.begin())
2203 dbgs() << ", ";
2204 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
2205 << ":" << PCI->Predicate->getName();
2206 }
2207 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002208 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00002209 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
2210 WSI != WSE; ++WSI) {
2211 dbgs() << "(";
2212 for (SmallVectorImpl<unsigned>::const_iterator
2213 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
2214 if (WI != WSI->begin())
2215 dbgs() << ", ";
2216 dbgs() << SchedModels.getSchedWrite(*WI).Name;
2217 }
2218 dbgs() << "),";
2219 }
2220 dbgs() << "}\n";
2221 }
2222}
Andrew Trick76686492012-09-15 00:19:57 +00002223#endif // NDEBUG