blob: d10825b7803a2ba02a32721b6d32a5e2e495b55b [file] [log] [blame]
Andrew Trick87255e32012-07-07 04:00:00 +00001//===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Trick87255e32012-07-07 04:00:00 +00006//
7//===----------------------------------------------------------------------===//
8//
Alp Tokercb402912014-01-24 17:20:08 +00009// This file defines structures to encapsulate the machine model as described in
Andrew Trick87255e32012-07-07 04:00:00 +000010// the target description.
11//
12//===----------------------------------------------------------------------===//
13
Andrew Trick87255e32012-07-07 04:00:00 +000014#include "CodeGenSchedule.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000015#include "CodeGenInstruction.h"
Andrew Trick87255e32012-07-07 04:00:00 +000016#include "CodeGenTarget.h"
Craig Topperf19eacf2018-03-21 02:48:34 +000017#include "llvm/ADT/MapVector.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000018#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000019#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/SmallSet.h"
21#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000022#include "llvm/Support/Casting.h"
Andrew Trick87255e32012-07-07 04:00:00 +000023#include "llvm/Support/Debug.h"
Andrew Trick9e1deb62012-10-03 23:06:32 +000024#include "llvm/Support/Regex.h"
Benjamin Kramercbce2f02018-01-23 23:05:04 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000026#include "llvm/TableGen/Error.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000027#include <algorithm>
28#include <iterator>
29#include <utility>
Andrew Trick87255e32012-07-07 04:00:00 +000030
31using namespace llvm;
32
Chandler Carruth97acce22014-04-22 03:06:00 +000033#define DEBUG_TYPE "subtarget-emitter"
34
Andrew Trick76686492012-09-15 00:19:57 +000035#ifndef NDEBUG
Benjamin Kramere1761952015-10-24 12:46:49 +000036static void dumpIdxVec(ArrayRef<unsigned> V) {
37 for (unsigned Idx : V)
38 dbgs() << Idx << ", ";
Andrew Trick33401e82012-09-15 00:19:59 +000039}
Andrew Trick76686492012-09-15 00:19:57 +000040#endif
41
Juergen Ributzka05c5a932013-11-19 03:08:35 +000042namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000043
Andrew Trick9e1deb62012-10-03 23:06:32 +000044// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
45struct InstrsOp : public SetTheory::Operator {
Craig Topper716b0732014-03-05 05:17:42 +000046 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
47 ArrayRef<SMLoc> Loc) override {
Juergen Ributzka05c5a932013-11-19 03:08:35 +000048 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
49 }
50};
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000051
Andrew Trick9e1deb62012-10-03 23:06:32 +000052// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
Andrew Trick9e1deb62012-10-03 23:06:32 +000053struct InstRegexOp : public SetTheory::Operator {
54 const CodeGenTarget &Target;
55 InstRegexOp(const CodeGenTarget &t): Target(t) {}
56
Benjamin Kramercbce2f02018-01-23 23:05:04 +000057 /// Remove any text inside of parentheses from S.
58 static std::string removeParens(llvm::StringRef S) {
59 std::string Result;
60 unsigned Paren = 0;
61 // NB: We don't care about escaped parens here.
62 for (char C : S) {
63 switch (C) {
64 case '(':
65 ++Paren;
66 break;
67 case ')':
68 --Paren;
69 break;
70 default:
71 if (Paren == 0)
72 Result += C;
73 }
74 }
75 return Result;
76 }
77
Juergen Ributzka05c5a932013-11-19 03:08:35 +000078 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
Craig Topper716b0732014-03-05 05:17:42 +000079 ArrayRef<SMLoc> Loc) override {
Roman Tereshind760c202018-05-23 20:45:43 +000080 ArrayRef<const CodeGenInstruction *> Instructions =
81 Target.getInstructionsByEnumValue();
82
83 unsigned NumGeneric = Target.getNumFixedInstructions();
Roman Tereshin9e493182018-05-23 22:10:21 +000084 unsigned NumPseudos = Target.getNumPseudoInstructions();
Roman Tereshind760c202018-05-23 20:45:43 +000085 auto Generics = Instructions.slice(0, NumGeneric);
Roman Tereshin9e493182018-05-23 22:10:21 +000086 auto Pseudos = Instructions.slice(NumGeneric, NumPseudos);
87 auto NonPseudos = Instructions.slice(NumGeneric + NumPseudos);
Roman Tereshind760c202018-05-23 20:45:43 +000088
Javed Absarfc500042017-10-05 13:27:43 +000089 for (Init *Arg : make_range(Expr->arg_begin(), Expr->arg_end())) {
90 StringInit *SI = dyn_cast<StringInit>(Arg);
Juergen Ributzka05c5a932013-11-19 03:08:35 +000091 if (!SI)
Benjamin Kramercbce2f02018-01-23 23:05:04 +000092 PrintFatalError(Loc, "instregex requires pattern string: " +
93 Expr->getAsString());
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000094 StringRef Original = SI->getValue();
95
Benjamin Kramercbce2f02018-01-23 23:05:04 +000096 // Extract a prefix that we can binary search on.
97 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
Simon Pilgrim75cc2f92018-03-20 22:20:28 +000098 auto FirstMeta = Original.find_first_of(RegexMetachars);
99
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000100 // Look for top-level | or ?. We cannot optimize them to binary search.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000101 if (removeParens(Original).find_first_of("|?") != std::string::npos)
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000102 FirstMeta = 0;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000103
104 Optional<Regex> Regexpr = None;
105 StringRef Prefix = Original.substr(0, FirstMeta);
Simon Pilgrim34d512e2018-03-24 21:04:20 +0000106 StringRef PatStr = Original.substr(FirstMeta);
107 if (!PatStr.empty()) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000108 // For the rest use a python-style prefix match.
Simon Pilgrim34d512e2018-03-24 21:04:20 +0000109 std::string pat = PatStr;
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000110 if (pat[0] != '^') {
111 pat.insert(0, "^(");
112 pat.insert(pat.end(), ')');
113 }
114 Regexpr = Regex(pat);
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000115 }
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000116
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000117 int NumMatches = 0;
118
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000119 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000120 for (auto *Inst : Generics) {
121 StringRef InstName = Inst->TheDef->getName();
122 if (InstName.startswith(Prefix) &&
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000123 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))) {
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000124 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000125 NumMatches++;
126 }
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000127 }
128
Roman Tereshin9e493182018-05-23 22:10:21 +0000129 // Target instructions are split into two ranges: pseudo instructions
130 // first, than non-pseudos. Each range is in lexicographical order
131 // sorted by name. Find the sub-ranges that start with our prefix.
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000132 struct Comp {
133 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
134 return LHS->TheDef->getName() < RHS;
135 }
136 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
137 return LHS < RHS->TheDef->getName() &&
138 !RHS->TheDef->getName().startswith(LHS);
139 }
140 };
Roman Tereshin9e493182018-05-23 22:10:21 +0000141 auto Range1 =
142 std::equal_range(Pseudos.begin(), Pseudos.end(), Prefix, Comp());
143 auto Range2 = std::equal_range(NonPseudos.begin(), NonPseudos.end(),
144 Prefix, Comp());
Benjamin Kramercbce2f02018-01-23 23:05:04 +0000145
Roman Tereshin9e493182018-05-23 22:10:21 +0000146 // For these ranges we know that instruction names start with the prefix.
147 // Check if there's a regex that needs to be checked.
Roman Tereshind760c202018-05-23 20:45:43 +0000148 const auto HandleNonGeneric = [&](const CodeGenInstruction *Inst) {
Simon Pilgrim75cc2f92018-03-20 22:20:28 +0000149 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000150 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))) {
Craig Topper8a417c12014-12-09 08:05:51 +0000151 Elts.insert(Inst->TheDef);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000152 NumMatches++;
153 }
Roman Tereshind760c202018-05-23 20:45:43 +0000154 };
Roman Tereshin9e493182018-05-23 22:10:21 +0000155 std::for_each(Range1.first, Range1.second, HandleNonGeneric);
156 std::for_each(Range2.first, Range2.second, HandleNonGeneric);
Simon Pilgrimd044f9c2018-03-25 19:20:08 +0000157
158 if (0 == NumMatches)
159 PrintFatalError(Loc, "instregex has no matches: " + Original);
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000160 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000161 }
Andrew Trick9e1deb62012-10-03 23:06:32 +0000162};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000163
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000164} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000165
Andrew Trick76686492012-09-15 00:19:57 +0000166/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +0000167CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
168 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000169 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +0000170
Andrew Trick9e1deb62012-10-03 23:06:32 +0000171 Sets.addFieldExpander("InstRW", "Instrs");
172
173 // Allow Set evaluation to recognize the dags used in InstRW records:
174 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000175 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
176 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000177
Andrew Trick76686492012-09-15 00:19:57 +0000178 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
179 // that are explicitly referenced in tablegen records. Resources associated
180 // with each processor will be derived later. Populate ProcModelMap with the
181 // CodeGenProcModel instances.
182 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000183
Andrew Trick76686492012-09-15 00:19:57 +0000184 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
185 // defined, and populate SchedReads and SchedWrites vectors. Implicit
186 // SchedReadWrites that represent sequences derived from expanded variant will
187 // be inferred later.
188 collectSchedRW();
189
190 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
191 // required by an instruction definition, and populate SchedClassIdxMap. Set
192 // NumItineraryClasses to the number of explicit itinerary classes referenced
193 // by instructions. Set NumInstrSchedClasses to the number of itinerary
194 // classes plus any classes implied by instructions that derive from class
195 // Sched and provide SchedRW list. This does not infer any new classes from
196 // SchedVariant.
197 collectSchedClasses();
198
199 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000200 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000201 // all itinerary classes to be discovered.
202 collectProcItins();
203
204 // Find ItinRW records for each processor and itinerary class.
205 // (For per-operand resources mapped to itinerary classes).
206 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000207
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000208 // Find UnsupportedFeatures records for each processor.
209 // (For per-operand resources mapped to itinerary classes).
210 collectProcUnsupportedFeatures();
211
Andrew Trick33401e82012-09-15 00:19:59 +0000212 // Infer new SchedClasses from SchedVariant.
213 inferSchedClasses();
214
Andrew Trick1e46d482012-09-15 00:20:02 +0000215 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
216 // ProcResourceDefs.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000217 LLVM_DEBUG(
218 dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000219 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000220
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000221 // Collect optional processor description.
222 collectOptionalProcessorInfo();
223
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +0000224 // Check MCInstPredicate definitions.
225 checkMCInstPredicates();
226
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000227 // Check STIPredicate definitions.
228 checkSTIPredicates();
229
230 // Find STIPredicate definitions for each processor model, and construct
231 // STIPredicateFunction objects.
232 collectSTIPredicates();
233
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000234 checkCompleteness();
235}
236
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000237void CodeGenSchedModels::checkSTIPredicates() const {
238 DenseMap<StringRef, const Record *> Declarations;
239
240 // There cannot be multiple declarations with the same name.
241 const RecVec Decls = Records.getAllDerivedDefinitions("STIPredicateDecl");
242 for (const Record *R : Decls) {
243 StringRef Name = R->getValueAsString("Name");
244 const auto It = Declarations.find(Name);
245 if (It == Declarations.end()) {
246 Declarations[Name] = R;
247 continue;
248 }
249
250 PrintError(R->getLoc(), "STIPredicate " + Name + " multiply declared.");
251 PrintNote(It->second->getLoc(), "Previous declaration was here.");
252 PrintFatalError(R->getLoc(), "Invalid STIPredicateDecl found.");
253 }
254
255 // Disallow InstructionEquivalenceClasses with an empty instruction list.
256 const RecVec Defs =
257 Records.getAllDerivedDefinitions("InstructionEquivalenceClass");
258 for (const Record *R : Defs) {
259 RecVec Opcodes = R->getValueAsListOfDefs("Opcodes");
260 if (Opcodes.empty()) {
261 PrintFatalError(R->getLoc(), "Invalid InstructionEquivalenceClass "
262 "defined with an empty opcode list.");
263 }
264 }
265}
266
267// Used by function `processSTIPredicate` to construct a mask of machine
268// instruction operands.
269static APInt constructOperandMask(ArrayRef<int64_t> Indices) {
270 APInt OperandMask;
271 if (Indices.empty())
272 return OperandMask;
273
274 int64_t MaxIndex = *std::max_element(Indices.begin(), Indices.end());
275 assert(MaxIndex >= 0 && "Invalid negative indices in input!");
276 OperandMask = OperandMask.zext(MaxIndex + 1);
277 for (const int64_t Index : Indices) {
278 assert(Index >= 0 && "Invalid negative indices!");
279 OperandMask.setBit(Index);
280 }
281
282 return OperandMask;
283}
284
285static void
286processSTIPredicate(STIPredicateFunction &Fn,
287 const DenseMap<Record *, unsigned> &ProcModelMap) {
288 DenseMap<const Record *, unsigned> Opcode2Index;
289 using OpcodeMapPair = std::pair<const Record *, OpcodeInfo>;
290 std::vector<OpcodeMapPair> OpcodeMappings;
291 std::vector<std::pair<APInt, APInt>> OpcodeMasks;
292
293 DenseMap<const Record *, unsigned> Predicate2Index;
294 unsigned NumUniquePredicates = 0;
295
296 // Number unique predicates and opcodes used by InstructionEquivalenceClass
297 // definitions. Each unique opcode will be associated with an OpcodeInfo
298 // object.
299 for (const Record *Def : Fn.getDefinitions()) {
300 RecVec Classes = Def->getValueAsListOfDefs("Classes");
301 for (const Record *EC : Classes) {
302 const Record *Pred = EC->getValueAsDef("Predicate");
303 if (Predicate2Index.find(Pred) == Predicate2Index.end())
304 Predicate2Index[Pred] = NumUniquePredicates++;
305
306 RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
307 for (const Record *Opcode : Opcodes) {
308 if (Opcode2Index.find(Opcode) == Opcode2Index.end()) {
309 Opcode2Index[Opcode] = OpcodeMappings.size();
310 OpcodeMappings.emplace_back(Opcode, OpcodeInfo());
311 }
312 }
313 }
314 }
315
316 // Initialize vector `OpcodeMasks` with default values. We want to keep track
317 // of which processors "use" which opcodes. We also want to be able to
318 // identify predicates that are used by different processors for a same
319 // opcode.
320 // This information is used later on by this algorithm to sort OpcodeMapping
321 // elements based on their processor and predicate sets.
322 OpcodeMasks.resize(OpcodeMappings.size());
323 APInt DefaultProcMask(ProcModelMap.size(), 0);
324 APInt DefaultPredMask(NumUniquePredicates, 0);
325 for (std::pair<APInt, APInt> &MaskPair : OpcodeMasks)
326 MaskPair = std::make_pair(DefaultProcMask, DefaultPredMask);
327
328 // Construct a OpcodeInfo object for every unique opcode declared by an
329 // InstructionEquivalenceClass definition.
330 for (const Record *Def : Fn.getDefinitions()) {
331 RecVec Classes = Def->getValueAsListOfDefs("Classes");
332 const Record *SchedModel = Def->getValueAsDef("SchedModel");
333 unsigned ProcIndex = ProcModelMap.find(SchedModel)->second;
334 APInt ProcMask(ProcModelMap.size(), 0);
335 ProcMask.setBit(ProcIndex);
336
337 for (const Record *EC : Classes) {
338 RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
339
340 std::vector<int64_t> OpIndices =
341 EC->getValueAsListOfInts("OperandIndices");
342 APInt OperandMask = constructOperandMask(OpIndices);
343
344 const Record *Pred = EC->getValueAsDef("Predicate");
345 APInt PredMask(NumUniquePredicates, 0);
346 PredMask.setBit(Predicate2Index[Pred]);
347
348 for (const Record *Opcode : Opcodes) {
349 unsigned OpcodeIdx = Opcode2Index[Opcode];
350 if (OpcodeMasks[OpcodeIdx].first[ProcIndex]) {
351 std::string Message =
Clement Courbet41c8af32018-10-25 07:44:01 +0000352 "Opcode " + Opcode->getName().str() +
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000353 " used by multiple InstructionEquivalenceClass definitions.";
354 PrintFatalError(EC->getLoc(), Message);
355 }
356 OpcodeMasks[OpcodeIdx].first |= ProcMask;
357 OpcodeMasks[OpcodeIdx].second |= PredMask;
358 OpcodeInfo &OI = OpcodeMappings[OpcodeIdx].second;
359
360 OI.addPredicateForProcModel(ProcMask, OperandMask, Pred);
361 }
362 }
363 }
364
365 // Sort OpcodeMappings elements based on their CPU and predicate masks.
366 // As a last resort, order elements by opcode identifier.
Fangrui Song0cac7262018-09-27 02:13:45 +0000367 llvm::sort(OpcodeMappings,
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000368 [&](const OpcodeMapPair &Lhs, const OpcodeMapPair &Rhs) {
369 unsigned LhsIdx = Opcode2Index[Lhs.first];
370 unsigned RhsIdx = Opcode2Index[Rhs.first];
371 std::pair<APInt, APInt> &LhsMasks = OpcodeMasks[LhsIdx];
372 std::pair<APInt, APInt> &RhsMasks = OpcodeMasks[RhsIdx];
373
374 if (LhsMasks.first != RhsMasks.first) {
375 if (LhsMasks.first.countPopulation() <
376 RhsMasks.first.countPopulation())
377 return true;
378 return LhsMasks.first.countLeadingZeros() >
379 RhsMasks.first.countLeadingZeros();
380 }
381
382 if (LhsMasks.second != RhsMasks.second) {
383 if (LhsMasks.second.countPopulation() <
384 RhsMasks.second.countPopulation())
385 return true;
386 return LhsMasks.second.countLeadingZeros() >
387 RhsMasks.second.countLeadingZeros();
388 }
389
390 return LhsIdx < RhsIdx;
391 });
392
393 // Now construct opcode groups. Groups are used by the SubtargetEmitter when
394 // expanding the body of a STIPredicate function. In particular, each opcode
395 // group is expanded into a sequence of labels in a switch statement.
396 // It identifies opcodes for which different processors define same predicates
397 // and same opcode masks.
398 for (OpcodeMapPair &Info : OpcodeMappings)
399 Fn.addOpcode(Info.first, std::move(Info.second));
400}
401
402void CodeGenSchedModels::collectSTIPredicates() {
403 // Map STIPredicateDecl records to elements of vector
404 // CodeGenSchedModels::STIPredicates.
405 DenseMap<const Record *, unsigned> Decl2Index;
406
407 RecVec RV = Records.getAllDerivedDefinitions("STIPredicate");
408 for (const Record *R : RV) {
409 const Record *Decl = R->getValueAsDef("Declaration");
410
411 const auto It = Decl2Index.find(Decl);
412 if (It == Decl2Index.end()) {
413 Decl2Index[Decl] = STIPredicates.size();
414 STIPredicateFunction Predicate(Decl);
415 Predicate.addDefinition(R);
416 STIPredicates.emplace_back(std::move(Predicate));
417 continue;
418 }
419
420 STIPredicateFunction &PreviousDef = STIPredicates[It->second];
421 PreviousDef.addDefinition(R);
422 }
423
424 for (STIPredicateFunction &Fn : STIPredicates)
425 processSTIPredicate(Fn, ProcModelMap);
426}
427
428void OpcodeInfo::addPredicateForProcModel(const llvm::APInt &CpuMask,
429 const llvm::APInt &OperandMask,
430 const Record *Predicate) {
431 auto It = llvm::find_if(
432 Predicates, [&OperandMask, &Predicate](const PredicateInfo &P) {
433 return P.Predicate == Predicate && P.OperandMask == OperandMask;
434 });
435 if (It == Predicates.end()) {
436 Predicates.emplace_back(CpuMask, OperandMask, Predicate);
437 return;
438 }
439 It->ProcModelMask |= CpuMask;
440}
441
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +0000442void CodeGenSchedModels::checkMCInstPredicates() const {
443 RecVec MCPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
444 if (MCPredicates.empty())
445 return;
446
447 // A target cannot have multiple TIIPredicate definitions with a same name.
448 llvm::StringMap<const Record *> TIIPredicates(MCPredicates.size());
449 for (const Record *TIIPred : MCPredicates) {
450 StringRef Name = TIIPred->getValueAsString("FunctionName");
451 StringMap<const Record *>::const_iterator It = TIIPredicates.find(Name);
452 if (It == TIIPredicates.end()) {
453 TIIPredicates[Name] = TIIPred;
454 continue;
455 }
456
457 PrintError(TIIPred->getLoc(),
458 "TIIPredicate " + Name + " is multiply defined.");
459 PrintNote(It->second->getLoc(),
460 " Previous definition of " + Name + " was here.");
461 PrintFatalError(TIIPred->getLoc(),
462 "Found conflicting definitions of TIIPredicate.");
463 }
464}
465
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000466void CodeGenSchedModels::collectRetireControlUnits() {
467 RecVec Units = Records.getAllDerivedDefinitions("RetireControlUnit");
468
469 for (Record *RCU : Units) {
470 CodeGenProcModel &PM = getProcModel(RCU->getValueAsDef("SchedModel"));
471 if (PM.RetireControlUnit) {
472 PrintError(RCU->getLoc(),
473 "Expected a single RetireControlUnit definition");
474 PrintNote(PM.RetireControlUnit->getLoc(),
475 "Previous definition of RetireControlUnit was here");
476 }
477 PM.RetireControlUnit = RCU;
478 }
479}
480
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000481void CodeGenSchedModels::collectLoadStoreQueueInfo() {
482 RecVec Queues = Records.getAllDerivedDefinitions("MemoryQueue");
483
484 for (Record *Queue : Queues) {
485 CodeGenProcModel &PM = getProcModel(Queue->getValueAsDef("SchedModel"));
486 if (Queue->isSubClassOf("LoadQueue")) {
487 if (PM.LoadQueue) {
488 PrintError(Queue->getLoc(),
489 "Expected a single LoadQueue definition");
490 PrintNote(PM.LoadQueue->getLoc(),
491 "Previous definition of LoadQueue was here");
492 }
493
494 PM.LoadQueue = Queue;
495 }
496
497 if (Queue->isSubClassOf("StoreQueue")) {
498 if (PM.StoreQueue) {
499 PrintError(Queue->getLoc(),
500 "Expected a single StoreQueue definition");
501 PrintNote(PM.LoadQueue->getLoc(),
502 "Previous definition of StoreQueue was here");
503 }
504
505 PM.StoreQueue = Queue;
506 }
507 }
508}
509
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000510/// Collect optional processor information.
511void CodeGenSchedModels::collectOptionalProcessorInfo() {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000512 // Find register file definitions for each processor.
513 collectRegisterFiles();
514
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000515 // Collect processor RetireControlUnit descriptors if available.
516 collectRetireControlUnits();
Clement Courbetb4493792018-04-10 08:16:37 +0000517
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000518 // Collect information about load/store queues.
519 collectLoadStoreQueueInfo();
520
Clement Courbetb4493792018-04-10 08:16:37 +0000521 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000522}
523
Andrew Trick76686492012-09-15 00:19:57 +0000524/// Gather all processor models.
525void CodeGenSchedModels::collectProcModels() {
526 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +0000527 llvm::sort(ProcRecords, LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000528
Andrew Trick76686492012-09-15 00:19:57 +0000529 // Reserve space because we can. Reallocation would be ok.
530 ProcModels.reserve(ProcRecords.size()+1);
531
532 // Use idx=0 for NoModel/NoItineraries.
533 Record *NoModelDef = Records.getDef("NoSchedModel");
534 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000535 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000536 ProcModelMap[NoModelDef] = 0;
537
538 // For each processor, find a unique machine model.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000539 LLVM_DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000540 for (Record *ProcRecord : ProcRecords)
541 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000542}
543
544/// Get a unique processor model based on the defined MachineModel and
545/// ProcessorItineraries.
546void CodeGenSchedModels::addProcModel(Record *ProcDef) {
547 Record *ModelKey = getModelOrItinDef(ProcDef);
548 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
549 return;
550
551 std::string Name = ModelKey->getName();
552 if (ModelKey->isSubClassOf("SchedMachineModel")) {
553 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000554 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000555 }
556 else {
557 // An itinerary is defined without a machine model. Infer a new model.
558 if (!ModelKey->getValueAsListOfDefs("IID").empty())
559 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000560 ProcModels.emplace_back(ProcModels.size(), Name,
561 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000562 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000563 LLVM_DEBUG(ProcModels.back().dump());
Andrew Trick76686492012-09-15 00:19:57 +0000564}
565
566// Recursively find all reachable SchedReadWrite records.
567static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
568 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000569 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000570 return;
571 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000572 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000573 if (RWDef->isSubClassOf("WriteSequence")) {
574 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000575 for (Record *WSRec : Seq)
576 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000577 }
578 else if (RWDef->isSubClassOf("SchedVariant")) {
579 // Visit each variant (guarded by a different predicate).
580 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000581 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000582 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000583 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
584 for (Record *SelDef : Selected)
585 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000586 }
587 }
588}
589
590// Collect and sort all SchedReadWrites reachable via tablegen records.
591// More may be inferred later when inferring new SchedClasses from variants.
592void CodeGenSchedModels::collectSchedRW() {
593 // Reserve idx=0 for invalid writes/reads.
594 SchedWrites.resize(1);
595 SchedReads.resize(1);
596
597 SmallPtrSet<Record*, 16> RWSet;
598
599 // Find all SchedReadWrites referenced by instruction defs.
600 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000601 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000602 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000603 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000604 continue;
605 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000606 for (Record *RW : RWs) {
607 if (RW->isSubClassOf("SchedWrite"))
608 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000609 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000610 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
611 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000612 }
613 }
614 }
615 // Find all ReadWrites referenced by InstRW.
616 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000617 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000618 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000619 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
620 for (Record *RWDef : RWDefs) {
621 if (RWDef->isSubClassOf("SchedWrite"))
622 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000623 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000624 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
625 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000626 }
627 }
628 }
629 // Find all ReadWrites referenced by ItinRW.
630 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000631 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000632 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000633 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
634 for (Record *RWDef : RWDefs) {
635 if (RWDef->isSubClassOf("SchedWrite"))
636 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000637 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000638 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
639 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000640 }
641 }
642 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000643 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
644 // for the loop below that initializes Alias vectors.
645 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
Fangrui Song0cac7262018-09-27 02:13:45 +0000646 llvm::sort(AliasDefs, LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000647 for (Record *ADef : AliasDefs) {
648 Record *MatchDef = ADef->getValueAsDef("MatchRW");
649 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000650 if (MatchDef->isSubClassOf("SchedWrite")) {
651 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000652 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000653 scanSchedRW(AliasDef, SWDefs, RWSet);
654 }
655 else {
656 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
657 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000658 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000659 scanSchedRW(AliasDef, SRDefs, RWSet);
660 }
661 }
Andrew Trick76686492012-09-15 00:19:57 +0000662 // Sort and add the SchedReadWrites directly referenced by instructions or
663 // itinerary resources. Index reads and writes in separate domains.
Fangrui Song0cac7262018-09-27 02:13:45 +0000664 llvm::sort(SWDefs, LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000665 for (Record *SWDef : SWDefs) {
666 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
667 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000668 }
Fangrui Song0cac7262018-09-27 02:13:45 +0000669 llvm::sort(SRDefs, LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000670 for (Record *SRDef : SRDefs) {
671 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
672 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000673 }
674 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000675 for (CodeGenSchedRW &CGRW : SchedWrites) {
676 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000677 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000678 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000679 /*IsRead=*/false);
680 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000681 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000682 for (Record *ADef : AliasDefs) {
683 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000684 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000685 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000686 CodeGenSchedRW &RW = getSchedRW(MatchDef);
687 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000688 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
689 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000690 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000691 LLVM_DEBUG(
692 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
693 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
694 dbgs() << WIdx << ": ";
695 SchedWrites[WIdx].dump();
696 dbgs() << '\n';
697 } for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd;
698 ++RIdx) {
699 dbgs() << RIdx << ": ";
700 SchedReads[RIdx].dump();
701 dbgs() << '\n';
702 } RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
703 for (Record *RWDef
704 : RWDefs) {
705 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
706 StringRef Name = RWDef->getName();
707 if (Name != "NoWrite" && Name != "ReadDefault")
708 dbgs() << "Unused SchedReadWrite " << Name << '\n';
709 }
710 });
Andrew Trick76686492012-09-15 00:19:57 +0000711}
712
713/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000714std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000715 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000716 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000717 if (I != Seq.begin())
718 Name += '_';
719 Name += getSchedRW(*I, IsRead).Name;
720 }
721 Name += ')';
722 return Name;
723}
724
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000725unsigned CodeGenSchedModels::getSchedRWIdx(const Record *Def,
726 bool IsRead) const {
Andrew Trick76686492012-09-15 00:19:57 +0000727 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000728 const auto I = find_if(
729 RWVec, [Def](const CodeGenSchedRW &RW) { return RW.TheDef == Def; });
730 return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
Andrew Trick76686492012-09-15 00:19:57 +0000731}
732
Andrew Trickcfe222c2012-09-19 04:43:19 +0000733bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000734 for (const CodeGenSchedRW &Read : SchedReads) {
735 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000736 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
737 continue;
738
739 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000740 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000741 return true;
742 }
743 }
744 return false;
745}
746
Craig Topper6f2cc9b2018-03-21 05:13:01 +0000747static void splitSchedReadWrites(const RecVec &RWDefs,
748 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000749 for (Record *RWDef : RWDefs) {
750 if (RWDef->isSubClassOf("SchedWrite"))
751 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000752 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000753 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
754 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000755 }
756 }
757}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000758
Andrew Trick76686492012-09-15 00:19:57 +0000759// Split the SchedReadWrites defs and call findRWs for each list.
760void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
761 IdxVec &Writes, IdxVec &Reads) const {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000762 RecVec WriteDefs;
763 RecVec ReadDefs;
764 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
765 findRWs(WriteDefs, Writes, false);
766 findRWs(ReadDefs, Reads, true);
Andrew Trick76686492012-09-15 00:19:57 +0000767}
768
769// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
770void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
771 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000772 for (Record *RWDef : RWDefs) {
773 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000774 assert(Idx && "failed to collect SchedReadWrite");
775 RWs.push_back(Idx);
776 }
777}
778
Andrew Trick33401e82012-09-15 00:19:59 +0000779void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
780 bool IsRead) const {
781 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
782 if (!SchedRW.IsSequence) {
783 RWSeq.push_back(RWIdx);
784 return;
785 }
786 int Repeat =
787 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
788 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000789 for (unsigned I : SchedRW.Sequence) {
790 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000791 }
792 }
793}
794
Andrew Trickda984b12012-10-03 23:06:28 +0000795// Expand a SchedWrite as a sequence following any aliases that coincide with
796// the given processor model.
797void CodeGenSchedModels::expandRWSeqForProc(
798 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
799 const CodeGenProcModel &ProcModel) const {
800
801 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000802 Record *AliasDef = nullptr;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000803 for (const Record *Rec : SchedWrite.Aliases) {
804 const CodeGenSchedRW &AliasRW = getSchedRW(Rec->getValueAsDef("AliasRW"));
805 if (Rec->getValueInit("SchedModel")->isComplete()) {
806 Record *ModelDef = Rec->getValueAsDef("SchedModel");
Andrew Trickda984b12012-10-03 23:06:28 +0000807 if (&getProcModel(ModelDef) != &ProcModel)
808 continue;
809 }
810 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000811 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
812 "defined for processor " + ProcModel.ModelName +
813 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000814 AliasDef = AliasRW.TheDef;
815 }
816 if (AliasDef) {
817 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
818 RWSeq, IsRead,ProcModel);
819 return;
820 }
821 if (!SchedWrite.IsSequence) {
822 RWSeq.push_back(RWIdx);
823 return;
824 }
825 int Repeat =
826 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000827 for (int I = 0, E = Repeat; I < E; ++I) {
828 for (unsigned Idx : SchedWrite.Sequence) {
829 expandRWSeqForProc(Idx, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000830 }
831 }
832}
833
Andrew Trick33401e82012-09-15 00:19:59 +0000834// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000835unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000836 bool IsRead) {
837 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
838
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000839 auto I = find_if(RWVec, [Seq](CodeGenSchedRW &RW) {
840 return makeArrayRef(RW.Sequence) == Seq;
841 });
Andrew Trick33401e82012-09-15 00:19:59 +0000842 // Index zero reserved for invalid RW.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000843 return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
Andrew Trick33401e82012-09-15 00:19:59 +0000844}
845
846/// Add this ReadWrite if it doesn't already exist.
847unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
848 bool IsRead) {
849 assert(!Seq.empty() && "cannot insert empty sequence");
850 if (Seq.size() == 1)
851 return Seq.back();
852
853 unsigned Idx = findRWForSequence(Seq, IsRead);
854 if (Idx)
855 return Idx;
856
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000857 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
858 unsigned RWIdx = RWVec.size();
Andrew Trickda984b12012-10-03 23:06:28 +0000859 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000860 RWVec.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000861 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000862}
863
Andrew Trick76686492012-09-15 00:19:57 +0000864/// Visit all the instruction definitions for this target to gather and
865/// enumerate the itinerary classes. These are the explicitly specified
866/// SchedClasses. More SchedClasses may be inferred.
867void CodeGenSchedModels::collectSchedClasses() {
868
869 // NoItinerary is always the first class at Idx=0
Craig Topper281a19c2018-03-22 06:15:08 +0000870 assert(SchedClasses.empty() && "Expected empty sched class");
871 SchedClasses.emplace_back(0, "NoInstrModel",
872 Records.getDef("NoItinerary"));
Andrew Trick76686492012-09-15 00:19:57 +0000873 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000874
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000875 // Create a SchedClass for each unique combination of itinerary class and
876 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000877 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000878 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000879 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000880 if (!Inst->TheDef->isValueUnset("SchedRW"))
881 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000882
Andrew Trick76686492012-09-15 00:19:57 +0000883 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topper281a19c2018-03-22 06:15:08 +0000884 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topper8a417c12014-12-09 08:05:51 +0000885 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000886 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000887 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000888 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Fangrui Song0cac7262018-09-27 02:13:45 +0000889 llvm::sort(InstRWDefs, LessRecord());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000890 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000891 for (Record *RWDef : InstRWDefs)
892 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000893
Andrew Trick76686492012-09-15 00:19:57 +0000894 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000895
Andrew Trick76686492012-09-15 00:19:57 +0000896 bool EnableDump = false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000897 LLVM_DEBUG(EnableDump = true);
Andrew Trick76686492012-09-15 00:19:57 +0000898 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000899 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000900
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000901 LLVM_DEBUG(
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000902 dbgs()
903 << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n");
Craig Topper8cc904d2016-01-17 20:38:18 +0000904 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000905 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim949437e2018-03-21 18:09:34 +0000906 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000907 if (!SCIdx) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000908 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000909 if (!Inst->hasNoSchedulingInfo)
910 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
911 });
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000912 continue;
913 }
914 CodeGenSchedClass &SC = getSchedClass(SCIdx);
915 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000916 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000917 "must not be subtarget specific.");
918
919 IdxVec ProcIndices;
920 if (SC.ItinClassDef->getName() != "NoItinerary") {
921 ProcIndices.push_back(0);
922 dbgs() << "Itinerary for " << InstName << ": "
923 << SC.ItinClassDef->getName() << '\n';
924 }
925 if (!SC.Writes.empty()) {
926 ProcIndices.push_back(0);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000927 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000928 dbgs() << "SchedRW machine model for " << InstName;
929 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE;
930 ++WI)
931 dbgs() << " " << SchedWrites[*WI].Name;
932 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
933 dbgs() << " " << SchedReads[*RI].Name;
934 dbgs() << '\n';
935 });
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000936 }
937 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000938 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000939 const CodeGenProcModel &ProcModel =
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000940 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000941 ProcIndices.push_back(ProcModel.Index);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000942 LLVM_DEBUG(dbgs() << "InstRW on " << ProcModel.ModelName << " for "
943 << InstName);
Andrew Trick76686492012-09-15 00:19:57 +0000944 IdxVec Writes;
945 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000946 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000947 Writes, Reads);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000948 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000949 for (unsigned WIdx : Writes)
950 dbgs() << " " << SchedWrites[WIdx].Name;
951 for (unsigned RIdx : Reads)
952 dbgs() << " " << SchedReads[RIdx].Name;
953 dbgs() << '\n';
954 });
Andrew Trick76686492012-09-15 00:19:57 +0000955 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000956 // If ProcIndices contains zero, the class applies to all processors.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000957 LLVM_DEBUG({
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000958 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
959 for (const CodeGenProcModel &PM : ProcModels) {
960 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
961 dbgs() << "No machine model for " << Inst->TheDef->getName()
962 << " on processor " << PM.ModelName << '\n';
963 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000964 }
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000965 });
Andrew Trick87255e32012-07-07 04:00:00 +0000966 }
Andrew Trick76686492012-09-15 00:19:57 +0000967}
968
Andrew Trick76686492012-09-15 00:19:57 +0000969// Get the SchedClass index for an instruction.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +0000970unsigned
971CodeGenSchedModels::getSchedClassIdx(const CodeGenInstruction &Inst) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000972 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000973}
974
Benjamin Kramere1761952015-10-24 12:46:49 +0000975std::string
976CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
977 ArrayRef<unsigned> OperWrites,
978 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000979
980 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000981 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
982 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000983 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000984 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000985 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000986 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000987 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000988 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000989 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000990 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000991 }
992 return Name;
993}
994
995std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
996
997 std::string Name;
998 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
999 if (I != InstDefs.begin())
1000 Name += '_';
1001 Name += (*I)->getName();
1002 }
1003 return Name;
1004}
1005
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001006/// Add an inferred sched class from an itinerary class and per-operand list of
1007/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
1008/// processors that may utilize this class.
1009unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +00001010 ArrayRef<unsigned> OperWrites,
1011 ArrayRef<unsigned> OperReads,
1012 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +00001013 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
1014
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001015 auto IsKeyEqual = [=](const CodeGenSchedClass &SC) {
1016 return SC.isKeyEqual(ItinClassDef, OperWrites, OperReads);
1017 };
1018
1019 auto I = find_if(make_range(schedClassBegin(), schedClassEnd()), IsKeyEqual);
1020 unsigned Idx = I == schedClassEnd() ? 0 : std::distance(schedClassBegin(), I);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001021 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +00001022 IdxVec PI;
1023 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
1024 SchedClasses[Idx].ProcIndices.end(),
1025 ProcIndices.begin(), ProcIndices.end(),
1026 std::back_inserter(PI));
Craig Topper59d13772018-03-24 22:58:00 +00001027 SchedClasses[Idx].ProcIndices = std::move(PI);
Andrew Trick76686492012-09-15 00:19:57 +00001028 return Idx;
1029 }
1030 Idx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +00001031 SchedClasses.emplace_back(Idx,
1032 createSchedClassName(ItinClassDef, OperWrites,
1033 OperReads),
1034 ItinClassDef);
Andrew Trick76686492012-09-15 00:19:57 +00001035 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick76686492012-09-15 00:19:57 +00001036 SC.Writes = OperWrites;
1037 SC.Reads = OperReads;
1038 SC.ProcIndices = ProcIndices;
1039
1040 return Idx;
1041}
1042
1043// Create classes for each set of opcodes that are in the same InstReadWrite
1044// definition across all processors.
1045void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
1046 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
1047 // intersects with an existing class via a previous InstRWDef. Instrs that do
1048 // not intersect with an existing class refer back to their former class as
1049 // determined from ItinDef or SchedRW.
Craig Topperf19eacf2018-03-21 02:48:34 +00001050 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +00001051 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +00001052 const RecVec *InstDefs = Sets.expand(InstRWDef);
1053 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001054 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +00001055
Craig Topper93dd77d2018-03-18 08:38:03 +00001056 for (Record *InstDef : *InstDefs) {
Javed Absarfc500042017-10-05 13:27:43 +00001057 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001058 if (Pos == InstrClassMap.end())
Javed Absarfc500042017-10-05 13:27:43 +00001059 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001060 unsigned SCIdx = Pos->second;
Craig Topperf19eacf2018-03-21 02:48:34 +00001061 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick76686492012-09-15 00:19:57 +00001062 }
1063 // For each set of Instrs, create a new class if necessary, and map or remap
1064 // the Instrs to it.
Craig Topperf19eacf2018-03-21 02:48:34 +00001065 for (auto &Entry : ClassInstrs) {
1066 unsigned OldSCIdx = Entry.first;
1067 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick76686492012-09-15 00:19:57 +00001068 // If the all instrs in the current class are accounted for, then leave
1069 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +00001070 if (OldSCIdx) {
1071 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
1072 if (!RWDefs.empty()) {
1073 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper06d78372018-03-21 19:30:30 +00001074 unsigned OrigNumInstrs =
1075 count_if(*OrigInstDefs, [&](Record *OIDef) {
1076 return InstrClassMap[OIDef] == OldSCIdx;
1077 });
Andrew Trick78a08512013-06-05 06:55:20 +00001078 if (OrigNumInstrs == InstDefs.size()) {
1079 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
1080 "expected a generic SchedClass");
Craig Toppere1d6a4d2018-03-18 19:56:15 +00001081 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1082 // Make sure we didn't already have a InstRW containing this
1083 // instruction on this model.
1084 for (Record *RWD : RWDefs) {
1085 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
1086 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
1087 for (Record *Inst : InstDefs) {
1088 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
1089 Inst->getName() + " also matches " +
1090 RWD->getValue("Instrs")->getValue()->getAsString());
1091 }
1092 }
1093 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001094 LLVM_DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
1095 << SchedClasses[OldSCIdx].Name << " on "
1096 << RWModelDef->getName() << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +00001097 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
1098 continue;
1099 }
1100 }
Andrew Trick76686492012-09-15 00:19:57 +00001101 }
1102 unsigned SCIdx = SchedClasses.size();
Craig Topper281a19c2018-03-22 06:15:08 +00001103 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick76686492012-09-15 00:19:57 +00001104 CodeGenSchedClass &SC = SchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001105 LLVM_DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
1106 << InstRWDef->getValueAsDef("SchedModel")->getName()
1107 << "\n");
Andrew Trick78a08512013-06-05 06:55:20 +00001108
Andrew Trick76686492012-09-15 00:19:57 +00001109 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
1110 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
1111 SC.Writes = SchedClasses[OldSCIdx].Writes;
1112 SC.Reads = SchedClasses[OldSCIdx].Reads;
1113 SC.ProcIndices.push_back(0);
Craig Topper989d94d2018-03-21 19:52:13 +00001114 // If we had an old class, copy it's InstRWs to this new class.
1115 if (OldSCIdx) {
1116 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1117 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
1118 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
1119 for (Record *InstDef : InstDefs) {
Craig Topper9fbbe5d2018-03-21 19:30:31 +00001120 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
1121 InstDef->getName() + " also matches " +
1122 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick9e1deb62012-10-03 23:06:32 +00001123 }
Andrew Trick9e1deb62012-10-03 23:06:32 +00001124 }
Craig Topper989d94d2018-03-21 19:52:13 +00001125 assert(OldRWDef != InstRWDef &&
1126 "SchedClass has duplicate InstRW def");
1127 SC.InstRWs.push_back(OldRWDef);
Andrew Trick76686492012-09-15 00:19:57 +00001128 }
Andrew Trick76686492012-09-15 00:19:57 +00001129 }
Craig Topper989d94d2018-03-21 19:52:13 +00001130 // Map each Instr to this new class.
1131 for (Record *InstDef : InstDefs)
1132 InstrClassMap[InstDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +00001133 SC.InstRWs.push_back(InstRWDef);
1134 }
Andrew Trick87255e32012-07-07 04:00:00 +00001135}
1136
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001137// True if collectProcItins found anything.
1138bool CodeGenSchedModels::hasItineraries() const {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001139 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd()))
Javed Absar67b042c2017-09-13 10:31:10 +00001140 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001141 return true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001142 return false;
1143}
1144
Andrew Trick87255e32012-07-07 04:00:00 +00001145// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +00001146void CodeGenSchedModels::collectProcItins() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001147 LLVM_DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +00001148 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001149 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +00001150 continue;
Andrew Trick76686492012-09-15 00:19:57 +00001151
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001152 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
1153 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
1154
1155 // Populate ItinDefList with Itinerary records.
1156 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +00001157
1158 // Insert each itinerary data record in the correct position within
1159 // the processor model's ItinDefList.
Javed Absarfc500042017-10-05 13:27:43 +00001160 for (Record *ItinData : ItinRecords) {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001161 const Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001162 bool FoundClass = false;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001163
1164 for (const CodeGenSchedClass &SC :
1165 make_range(schedClassBegin(), schedClassEnd())) {
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001166 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001167 if (SC.ItinClassDef == ItinDef) {
1168 ProcModel.ItinDefList[SC.Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001169 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001170 }
Andrew Trick76686492012-09-15 00:19:57 +00001171 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +00001172 if (!FoundClass) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001173 LLVM_DEBUG(dbgs() << ProcModel.ItinsDef->getName()
1174 << " missing class for itinerary "
1175 << ItinDef->getName() << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001176 }
Andrew Trick87255e32012-07-07 04:00:00 +00001177 }
Andrew Trick76686492012-09-15 00:19:57 +00001178 // Check for missing itinerary entries.
1179 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001180 LLVM_DEBUG(
1181 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
1182 if (!ProcModel.ItinDefList[i])
1183 dbgs() << ProcModel.ItinsDef->getName()
1184 << " missing itinerary for class " << SchedClasses[i].Name
1185 << '\n';
1186 });
Andrew Trick87255e32012-07-07 04:00:00 +00001187 }
Andrew Trick87255e32012-07-07 04:00:00 +00001188}
Andrew Trick76686492012-09-15 00:19:57 +00001189
1190// Gather the read/write types for each itinerary class.
1191void CodeGenSchedModels::collectProcItinRW() {
1192 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Fangrui Song0cac7262018-09-27 02:13:45 +00001193 llvm::sort(ItinRWDefs, LessRecord());
Javed Absar21c75912017-10-09 16:21:25 +00001194 for (Record *RWDef : ItinRWDefs) {
Javed Absarf45d0b92017-10-08 17:23:30 +00001195 if (!RWDef->getValueInit("SchedModel")->isComplete())
1196 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
1197 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick76686492012-09-15 00:19:57 +00001198 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
1199 if (I == ProcModelMap.end()) {
Javed Absarf45d0b92017-10-08 17:23:30 +00001200 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +00001201 + ModelDef->getName());
1202 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001203 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +00001204 }
1205}
1206
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001207// Gather the unsupported features for processor models.
1208void CodeGenSchedModels::collectProcUnsupportedFeatures() {
1209 for (CodeGenProcModel &ProcModel : ProcModels) {
1210 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
1211 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
1212 }
1213 }
1214}
1215
Andrew Trick33401e82012-09-15 00:19:59 +00001216/// Infer new classes from existing classes. In the process, this may create new
1217/// SchedWrites from sequences of existing SchedWrites.
1218void CodeGenSchedModels::inferSchedClasses() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001219 LLVM_DEBUG(
1220 dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
1221 LLVM_DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001222
Andrew Trick33401e82012-09-15 00:19:59 +00001223 // Visit all existing classes and newly created classes.
1224 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001225 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
1226
Andrew Trick33401e82012-09-15 00:19:59 +00001227 if (SchedClasses[Idx].ItinClassDef)
1228 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001229 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +00001230 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001231 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +00001232 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
1233 Idx, SchedClasses[Idx].ProcIndices);
1234 }
1235 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
1236 "too many SchedVariants");
1237 }
1238}
1239
1240/// Infer classes from per-processor itinerary resources.
1241void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
1242 unsigned FromClassIdx) {
1243 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1244 const CodeGenProcModel &PM = ProcModels[PIdx];
1245 // For all ItinRW entries.
1246 bool HasMatch = false;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001247 for (const Record *Rec : PM.ItinRWDefs) {
1248 RecVec Matched = Rec->getValueAsListOfDefs("MatchedItinClasses");
Andrew Trick33401e82012-09-15 00:19:59 +00001249 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1250 continue;
1251 if (HasMatch)
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001252 PrintFatalError(Rec->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +00001253 + ItinClassDef->getName()
1254 + " in ItinResources for " + PM.ModelName);
1255 HasMatch = true;
1256 IdxVec Writes, Reads;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001257 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001258 inferFromRW(Writes, Reads, FromClassIdx, PIdx);
Andrew Trick33401e82012-09-15 00:19:59 +00001259 }
1260 }
1261}
1262
1263/// Infer classes from per-processor InstReadWrite definitions.
1264void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +00001265 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +00001266 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +00001267 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
1268 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +00001269 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +00001270 for (; II != IE; ++II) {
1271 if (InstrClassMap[*II] == SCIdx)
1272 break;
1273 }
1274 // If this class no longer has any instructions mapped to it, it has become
1275 // irrelevant.
1276 if (II == IE)
1277 continue;
1278 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +00001279 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1280 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Craig Topper9f3293a2018-03-24 21:57:35 +00001281 inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +00001282 }
1283}
1284
1285namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001286
Andrew Trick9257b8f2012-09-22 02:24:21 +00001287// Helper for substituteVariantOperand.
1288struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +00001289 Record *VarOrSeqDef; // Variant or sequence.
1290 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001291 unsigned ProcIdx; // Processor model index or zero for any.
1292 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
1293
1294 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +00001295 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +00001296};
1297
Andrew Trick33401e82012-09-15 00:19:59 +00001298// Associate a predicate with the SchedReadWrite that it guards.
1299// RWIdx is the index of the read/write variant.
1300struct PredCheck {
1301 bool IsRead;
1302 unsigned RWIdx;
1303 Record *Predicate;
1304
1305 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
1306};
1307
1308// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1309struct PredTransition {
1310 // A predicate term is a conjunction of PredChecks.
1311 SmallVector<PredCheck, 4> PredTerm;
1312 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1313 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001314 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001315};
1316
1317// Encapsulate a set of partially constructed transitions.
1318// The results are built by repeated calls to substituteVariants.
1319class PredTransitions {
1320 CodeGenSchedModels &SchedModels;
1321
1322public:
1323 std::vector<PredTransition> TransVec;
1324
1325 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1326
1327 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1328 bool IsRead, unsigned StartIdx);
1329
1330 void substituteVariants(const PredTransition &Trans);
1331
1332#ifndef NDEBUG
1333 void dump() const;
1334#endif
1335
1336private:
1337 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +00001338 void getIntersectingVariants(
1339 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1340 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001341 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001342};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001343
1344} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +00001345
1346// Return true if this predicate is mutually exclusive with a PredTerm. This
1347// degenerates into checking if the predicate is mutually exclusive with any
1348// predicate in the Term's conjunction.
1349//
1350// All predicates associated with a given SchedRW are considered mutually
1351// exclusive. This should work even if the conditions expressed by the
1352// predicates are not exclusive because the predicates for a given SchedWrite
1353// are always checked in the order they are defined in the .td file. Later
1354// conditions implicitly negate any prior condition.
1355bool PredTransitions::mutuallyExclusive(Record *PredDef,
1356 ArrayRef<PredCheck> Term) {
Javed Absar21c75912017-10-09 16:21:25 +00001357 for (const PredCheck &PC: Term) {
Javed Absarfc500042017-10-05 13:27:43 +00001358 if (PC.Predicate == PredDef)
Andrew Trick33401e82012-09-15 00:19:59 +00001359 return false;
1360
Javed Absarfc500042017-10-05 13:27:43 +00001361 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001362 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1363 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001364 if (any_of(Variants, [PredDef](const Record *R) {
1365 return R->getValueAsDef("Predicate") == PredDef;
1366 }))
1367 return true;
Andrew Trick33401e82012-09-15 00:19:59 +00001368 }
1369 return false;
1370}
1371
Andrew Trickda984b12012-10-03 23:06:28 +00001372static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1373 CodeGenSchedModels &SchedModels) {
1374 if (RW.HasVariants)
1375 return true;
1376
Javed Absar21c75912017-10-09 16:21:25 +00001377 for (Record *Alias : RW.Aliases) {
Andrew Trickda984b12012-10-03 23:06:28 +00001378 const CodeGenSchedRW &AliasRW =
Javed Absarfc500042017-10-05 13:27:43 +00001379 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +00001380 if (AliasRW.HasVariants)
1381 return true;
1382 if (AliasRW.IsSequence) {
1383 IdxVec ExpandedRWs;
1384 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001385 for (unsigned SI : ExpandedRWs) {
1386 if (hasAliasedVariants(SchedModels.getSchedRW(SI, AliasRW.IsRead),
1387 SchedModels))
Andrew Trickda984b12012-10-03 23:06:28 +00001388 return true;
Andrew Trickda984b12012-10-03 23:06:28 +00001389 }
1390 }
1391 }
1392 return false;
1393}
1394
1395static bool hasVariant(ArrayRef<PredTransition> Transitions,
1396 CodeGenSchedModels &SchedModels) {
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001397 for (const PredTransition &PTI : Transitions) {
1398 for (const SmallVectorImpl<unsigned> &WSI : PTI.WriteSequences)
1399 for (unsigned WI : WSI)
1400 if (hasAliasedVariants(SchedModels.getSchedWrite(WI), SchedModels))
Andrew Trickda984b12012-10-03 23:06:28 +00001401 return true;
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001402
1403 for (const SmallVectorImpl<unsigned> &RSI : PTI.ReadSequences)
1404 for (unsigned RI : RSI)
1405 if (hasAliasedVariants(SchedModels.getSchedRead(RI), SchedModels))
Andrew Trickda984b12012-10-03 23:06:28 +00001406 return true;
Andrew Trickda984b12012-10-03 23:06:28 +00001407 }
1408 return false;
1409}
1410
1411// Populate IntersectingVariants with any variants or aliased sequences of the
1412// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001413// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001414void PredTransitions::getIntersectingVariants(
1415 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1416 std::vector<TransVariant> &IntersectingVariants) {
1417
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001418 bool GenericRW = false;
1419
Andrew Trickda984b12012-10-03 23:06:28 +00001420 std::vector<TransVariant> Variants;
1421 if (SchedRW.HasVariants) {
1422 unsigned VarProcIdx = 0;
1423 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1424 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1425 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1426 }
1427 // Push each variant. Assign TransVecIdx later.
1428 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absarf45d0b92017-10-08 17:23:30 +00001429 for (Record *VarDef : VarDefs)
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001430 Variants.emplace_back(VarDef, SchedRW.Index, VarProcIdx, 0);
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001431 if (VarProcIdx == 0)
1432 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001433 }
1434 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1435 AI != AE; ++AI) {
1436 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1437 // to is defined within a processor model, constrain all variants to
1438 // that processor.
1439 unsigned AliasProcIdx = 0;
1440 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1441 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1442 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1443 }
1444 const CodeGenSchedRW &AliasRW =
1445 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1446
1447 if (AliasRW.HasVariants) {
1448 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar9003dd72017-10-10 15:58:45 +00001449 for (Record *VD : VarDefs)
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001450 Variants.emplace_back(VD, AliasRW.Index, AliasProcIdx, 0);
Andrew Trickda984b12012-10-03 23:06:28 +00001451 }
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001452 if (AliasRW.IsSequence)
1453 Variants.emplace_back(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0);
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001454 if (AliasProcIdx == 0)
1455 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001456 }
Javed Absarf45d0b92017-10-08 17:23:30 +00001457 for (TransVariant &Variant : Variants) {
Andrew Trickda984b12012-10-03 23:06:28 +00001458 // Don't expand variants if the processor models don't intersect.
1459 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001460 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absarf45d0b92017-10-08 17:23:30 +00001461 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trickda984b12012-10-03 23:06:28 +00001462 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1463 Variant.ProcIdx);
1464 if (!Cnt)
1465 continue;
1466 if (Cnt > 1) {
1467 const CodeGenProcModel &PM =
1468 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001469 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1470 "Multiple variants defined for processor " +
1471 PM.ModelName +
1472 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001473 }
1474 }
1475 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1476 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1477 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1478 continue;
1479 }
1480 if (IntersectingVariants.empty()) {
1481 // The first variant builds on the existing transition.
1482 Variant.TransVecIdx = TransIdx;
1483 IntersectingVariants.push_back(Variant);
1484 }
1485 else {
1486 // Push another copy of the current transition for more variants.
1487 Variant.TransVecIdx = TransVec.size();
1488 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001489 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001490 }
1491 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001492 if (GenericRW && IntersectingVariants.empty()) {
1493 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1494 "a matching predicate on any processor");
1495 }
Andrew Trickda984b12012-10-03 23:06:28 +00001496}
1497
Andrew Trick9257b8f2012-09-22 02:24:21 +00001498// Push the Reads/Writes selected by this variant onto the PredTransition
1499// specified by VInfo.
1500void PredTransitions::
1501pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001502 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1503
Andrew Trick9257b8f2012-09-22 02:24:21 +00001504 // If this operand transition is reached through a processor-specific alias,
1505 // then the whole transition is specific to this processor.
1506 if (VInfo.ProcIdx != 0)
1507 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1508
Andrew Trick33401e82012-09-15 00:19:59 +00001509 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001510 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1511 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001512 Trans.PredTerm.emplace_back(IsRead, VInfo.RWIdx,PredDef);
Andrew Trickda984b12012-10-03 23:06:28 +00001513 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1514 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1515 }
1516 else {
1517 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1518 "variant must be a SchedVariant or aliased WriteSequence");
1519 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1520 }
Andrew Trick33401e82012-09-15 00:19:59 +00001521
Andrew Trick9257b8f2012-09-22 02:24:21 +00001522 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001523
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001524 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001525 ? Trans.ReadSequences : Trans.WriteSequences;
1526 if (SchedRW.IsVariadic) {
1527 unsigned OperIdx = RWSequences.size()-1;
1528 // Make N-1 copies of this transition's last sequence.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001529 RWSequences.insert(RWSequences.end(), SelectedRWs.size() - 1,
1530 RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001531 // Push each of the N elements of the SelectedRWs onto a copy of the last
1532 // sequence (split the current operand into N operands).
1533 // Note that write sequences should be expanded within this loop--the entire
1534 // sequence belongs to a single operand.
1535 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1536 RWI != RWE; ++RWI, ++OperIdx) {
1537 IdxVec ExpandedRWs;
1538 if (IsRead)
1539 ExpandedRWs.push_back(*RWI);
1540 else
1541 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1542 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1543 ExpandedRWs.begin(), ExpandedRWs.end());
1544 }
1545 assert(OperIdx == RWSequences.size() && "missed a sequence");
1546 }
1547 else {
1548 // Push this transition's expanded sequence onto this transition's last
1549 // sequence (add to the current operand's sequence).
1550 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1551 IdxVec ExpandedRWs;
1552 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1553 RWI != RWE; ++RWI) {
1554 if (IsRead)
1555 ExpandedRWs.push_back(*RWI);
1556 else
1557 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1558 }
1559 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1560 }
1561}
1562
1563// RWSeq is a sequence of all Reads or all Writes for the next read or write
1564// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001565// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001566// of TransVec.
1567void PredTransitions::substituteVariantOperand(
1568 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1569
1570 // Visit each original RW within the current sequence.
1571 for (SmallVectorImpl<unsigned>::const_iterator
1572 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1573 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1574 // Push this RW on all partial PredTransitions or distribute variants.
1575 // New PredTransitions may be pushed within this loop which should not be
1576 // revisited (TransEnd must be loop invariant).
1577 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1578 TransIdx != TransEnd; ++TransIdx) {
1579 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001580 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001581 if (IsRead)
1582 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1583 else
1584 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1585 continue;
1586 }
1587 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001588 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001589 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001590 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001591 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001592 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001593 IVI = IntersectingVariants.begin(),
1594 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001595 IVI != IVE; ++IVI) {
1596 pushVariant(*IVI, IsRead);
1597 }
Andrew Trick33401e82012-09-15 00:19:59 +00001598 }
1599 }
1600}
1601
1602// For each variant of a Read/Write in Trans, substitute the sequence of
1603// Read/Writes guarded by the variant. This is exponential in the number of
1604// variant Read/Writes, but in practice detection of mutually exclusive
1605// predicates should result in linear growth in the total number variants.
1606//
1607// This is one step in a breadth-first search of nested variants.
1608void PredTransitions::substituteVariants(const PredTransition &Trans) {
1609 // Build up a set of partial results starting at the back of
1610 // PredTransitions. Remember the first new transition.
1611 unsigned StartIdx = TransVec.size();
Craig Topper195aaaf2018-03-22 06:15:10 +00001612 TransVec.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001613 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001614 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001615
1616 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001617 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001618 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1619 WSI != WSE; ++WSI) {
1620 // Push a new (empty) write sequence onto all partial Transitions.
1621 for (std::vector<PredTransition>::iterator I =
1622 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001623 I->WriteSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001624 }
1625 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1626 }
1627 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001628 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001629 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1630 RSI != RSE; ++RSI) {
1631 // Push a new (empty) read sequence onto all partial Transitions.
1632 for (std::vector<PredTransition>::iterator I =
1633 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topper195aaaf2018-03-22 06:15:10 +00001634 I->ReadSequences.emplace_back();
Andrew Trick33401e82012-09-15 00:19:59 +00001635 }
1636 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1637 }
1638}
1639
Andrew Trick33401e82012-09-15 00:19:59 +00001640// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001641static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001642 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001643 CodeGenSchedModels &SchedModels) {
1644 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1645 // requires creating a new SchedClass.
1646 for (ArrayRef<PredTransition>::iterator
1647 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1648 IdxVec OperWritesVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001649 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1650 [&SchedModels](ArrayRef<unsigned> WS) {
1651 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1652 });
Andrew Trick33401e82012-09-15 00:19:59 +00001653 IdxVec OperReadsVariant;
Craig Topper1970e952018-03-20 20:24:12 +00001654 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1655 [&SchedModels](ArrayRef<unsigned> RS) {
1656 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1657 });
Andrew Trick33401e82012-09-15 00:19:59 +00001658 CodeGenSchedTransition SCTrans;
1659 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001660 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Craig Topper2ed54072018-03-24 22:58:03 +00001661 OperReadsVariant, I->ProcIndices);
1662 SCTrans.ProcIndices.assign(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001663 // The final PredTerm is unique set of predicates guarding the transition.
1664 RecVec Preds;
Craig Topper1970e952018-03-20 20:24:12 +00001665 transform(I->PredTerm, std::back_inserter(Preds),
1666 [](const PredCheck &P) {
1667 return P.Predicate;
1668 });
Craig Topperb5ed2752018-03-20 20:24:10 +00001669 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Craig Topper18cfa2c2018-03-24 22:58:02 +00001670 SCTrans.PredTerm = std::move(Preds);
1671 SchedModels.getSchedClass(FromClassIdx)
1672 .Transitions.push_back(std::move(SCTrans));
Andrew Trick33401e82012-09-15 00:19:59 +00001673 }
1674}
1675
Andrew Trick9257b8f2012-09-22 02:24:21 +00001676// Create new SchedClasses for the given ReadWrite list. If any of the
1677// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1678// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001679void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1680 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001681 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001682 ArrayRef<unsigned> ProcIndices) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001683 LLVM_DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices);
1684 dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001685
1686 // Create a seed transition with an empty PredTerm and the expanded sequences
1687 // of SchedWrites for the current SchedClass.
1688 std::vector<PredTransition> LastTransitions;
Craig Topper195aaaf2018-03-22 06:15:10 +00001689 LastTransitions.emplace_back();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001690 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1691 ProcIndices.end());
1692
Benjamin Kramere1761952015-10-24 12:46:49 +00001693 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001694 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001695 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topper195aaaf2018-03-22 06:15:10 +00001696 LastTransitions[0].WriteSequences.emplace_back();
1697 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001698 Seq.append(WriteSeq.begin(), WriteSeq.end());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001699 LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001700 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001701 LLVM_DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001702 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001703 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001704 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topper195aaaf2018-03-22 06:15:10 +00001705 LastTransitions[0].ReadSequences.emplace_back();
1706 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper1f57456c2018-03-20 20:24:14 +00001707 Seq.append(ReadSeq.begin(), ReadSeq.end());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001708 LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001709 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001710 LLVM_DEBUG(dbgs() << '\n');
Andrew Trick33401e82012-09-15 00:19:59 +00001711
1712 // Collect all PredTransitions for individual operands.
1713 // Iterate until no variant writes remain.
1714 while (hasVariant(LastTransitions, *this)) {
1715 PredTransitions Transitions(*this);
Craig Topperf6114252018-03-20 20:24:16 +00001716 for (const PredTransition &Trans : LastTransitions)
1717 Transitions.substituteVariants(Trans);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001718 LLVM_DEBUG(Transitions.dump());
Andrew Trick33401e82012-09-15 00:19:59 +00001719 LastTransitions.swap(Transitions.TransVec);
1720 }
1721 // If the first transition has no variants, nothing to do.
1722 if (LastTransitions[0].PredTerm.empty())
1723 return;
1724
1725 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1726 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001727 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001728}
1729
Andrew Trickcf398b22013-04-23 23:45:14 +00001730// Check if any processor resource group contains all resource records in
1731// SubUnits.
1732bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1733 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1734 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1735 continue;
1736 RecVec SuperUnits =
1737 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1738 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1739 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001740 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001741 break;
1742 }
1743 }
1744 if (RI == RE)
1745 return true;
1746 }
1747 return false;
1748}
1749
1750// Verify that overlapping groups have a common supergroup.
1751void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1752 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1753 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1754 continue;
1755 RecVec CheckUnits =
1756 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1757 for (unsigned j = i+1; j < e; ++j) {
1758 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1759 continue;
1760 RecVec OtherUnits =
1761 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1762 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1763 OtherUnits.begin(), OtherUnits.end())
1764 != CheckUnits.end()) {
1765 // CheckUnits and OtherUnits overlap
1766 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1767 CheckUnits.end());
1768 if (!hasSuperGroup(OtherUnits, PM)) {
1769 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1770 "proc resource group overlaps with "
1771 + PM.ProcResourceDefs[j]->getName()
1772 + " but no supergroup contains both.");
1773 }
1774 }
1775 }
1776 }
1777}
1778
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001779// Collect all the RegisterFile definitions available in this target.
1780void CodeGenSchedModels::collectRegisterFiles() {
1781 RecVec RegisterFileDefs = Records.getAllDerivedDefinitions("RegisterFile");
1782
1783 // RegisterFiles is the vector of CodeGenRegisterFile.
1784 for (Record *RF : RegisterFileDefs) {
1785 // For each register file definition, construct a CodeGenRegisterFile object
1786 // and add it to the appropriate scheduling model.
1787 CodeGenProcModel &PM = getProcModel(RF->getValueAsDef("SchedModel"));
1788 PM.RegisterFiles.emplace_back(CodeGenRegisterFile(RF->getName(),RF));
1789 CodeGenRegisterFile &CGRF = PM.RegisterFiles.back();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +00001790 CGRF.MaxMovesEliminatedPerCycle =
1791 RF->getValueAsInt("MaxMovesEliminatedPerCycle");
1792 CGRF.AllowZeroMoveEliminationOnly =
1793 RF->getValueAsBit("AllowZeroMoveEliminationOnly");
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001794
1795 // Now set the number of physical registers as well as the cost of registers
1796 // in each register class.
1797 CGRF.NumPhysRegs = RF->getValueAsInt("NumPhysRegs");
Andrea Di Biagiof455e352018-10-11 10:39:03 +00001798 if (!CGRF.NumPhysRegs) {
1799 PrintFatalError(RF->getLoc(),
1800 "Invalid RegisterFile with zero physical registers");
1801 }
1802
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001803 RecVec RegisterClasses = RF->getValueAsListOfDefs("RegClasses");
1804 std::vector<int64_t> RegisterCosts = RF->getValueAsListOfInts("RegCosts");
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +00001805 ListInit *MoveElimInfo = RF->getValueAsListInit("AllowMoveElimination");
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001806 for (unsigned I = 0, E = RegisterClasses.size(); I < E; ++I) {
1807 int Cost = RegisterCosts.size() > I ? RegisterCosts[I] : 1;
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +00001808
1809 bool AllowMoveElim = false;
1810 if (MoveElimInfo->size() > I) {
1811 BitInit *Val = cast<BitInit>(MoveElimInfo->getElement(I));
1812 AllowMoveElim = Val->getValue();
1813 }
1814
1815 CGRF.Costs.emplace_back(RegisterClasses[I], Cost, AllowMoveElim);
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001816 }
1817 }
1818}
1819
Andrew Trick1e46d482012-09-15 00:20:02 +00001820// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1821void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001822 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1823 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1824
Andrew Trick1e46d482012-09-15 00:20:02 +00001825 // Add any subtarget-specific SchedReadWrites that are directly associated
1826 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1827 // determine which processors they apply to.
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001828 for (const CodeGenSchedClass &SC :
1829 make_range(schedClassBegin(), schedClassEnd())) {
1830 if (SC.ItinClassDef) {
1831 collectItinProcResources(SC.ItinClassDef);
1832 continue;
Andrew Trick4fe440d2013-02-01 03:19:54 +00001833 }
Andrea Di Biagio38fe2272018-04-26 12:56:26 +00001834
1835 // This class may have a default ReadWrite list which can be overriden by
1836 // InstRW definitions.
1837 for (Record *RW : SC.InstRWs) {
1838 Record *RWModelDef = RW->getValueAsDef("SchedModel");
1839 unsigned PIdx = getProcModel(RWModelDef).Index;
1840 IdxVec Writes, Reads;
1841 findRWs(RW->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1842 collectRWResources(Writes, Reads, PIdx);
1843 }
1844
1845 collectRWResources(SC.Writes, SC.Reads, SC.ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001846 }
1847 // Add resources separately defined by each subtarget.
1848 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001849 for (Record *WR : WRDefs) {
1850 Record *ModelDef = WR->getValueAsDef("SchedModel");
1851 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001852 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001853 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absar2c9570c2017-10-11 09:33:23 +00001854 for (Record *SWR : SWRDefs) {
1855 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1856 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001857 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001858 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001859 for (Record *RA : RADefs) {
1860 Record *ModelDef = RA->getValueAsDef("SchedModel");
1861 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick1e46d482012-09-15 00:20:02 +00001862 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001863 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absar2c9570c2017-10-11 09:33:23 +00001864 for (Record *SRA : SRADefs) {
1865 if (SRA->getValueInit("SchedModel")->isComplete()) {
1866 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1867 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trickdca870b2014-03-13 03:49:20 +00001868 }
1869 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001870 // Add ProcResGroups that are defined within this processor model, which may
1871 // not be directly referenced but may directly specify a buffer size.
1872 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absar21c75912017-10-09 16:21:25 +00001873 for (Record *PRG : ProcResGroups) {
Javed Absarfc500042017-10-05 13:27:43 +00001874 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Trick40c4f382013-06-15 04:50:06 +00001875 continue;
Javed Absarfc500042017-10-05 13:27:43 +00001876 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1877 if (!is_contained(PM.ProcResourceDefs, PRG))
1878 PM.ProcResourceDefs.push_back(PRG);
Andrew Trick40c4f382013-06-15 04:50:06 +00001879 }
Clement Courbeteb4f5d22018-02-05 12:23:51 +00001880 // Add ProcResourceUnits unconditionally.
1881 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1882 if (!PRU->getValueInit("SchedModel")->isComplete())
1883 continue;
1884 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1885 if (!is_contained(PM.ProcResourceDefs, PRU))
1886 PM.ProcResourceDefs.push_back(PRU);
1887 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001888 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001889 for (CodeGenProcModel &PM : ProcModels) {
Fangrui Song3507c6e2018-09-30 22:31:29 +00001890 llvm::sort(PM.WriteResDefs, LessRecord());
1891 llvm::sort(PM.ReadAdvanceDefs, LessRecord());
1892 llvm::sort(PM.ProcResourceDefs, LessRecord());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001893 LLVM_DEBUG(
1894 PM.dump();
1895 dbgs() << "WriteResDefs: "; for (RecIter RI = PM.WriteResDefs.begin(),
1896 RE = PM.WriteResDefs.end();
1897 RI != RE; ++RI) {
1898 if ((*RI)->isSubClassOf("WriteRes"))
1899 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1900 else
1901 dbgs() << (*RI)->getName() << " ";
1902 } dbgs() << "\nReadAdvanceDefs: ";
1903 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1904 RE = PM.ReadAdvanceDefs.end();
1905 RI != RE; ++RI) {
1906 if ((*RI)->isSubClassOf("ReadAdvance"))
1907 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1908 else
1909 dbgs() << (*RI)->getName() << " ";
1910 } dbgs()
1911 << "\nProcResourceDefs: ";
1912 for (RecIter RI = PM.ProcResourceDefs.begin(),
1913 RE = PM.ProcResourceDefs.end();
1914 RI != RE; ++RI) { dbgs() << (*RI)->getName() << " "; } dbgs()
1915 << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001916 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001917 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001918
1919 ProcResourceDefs.clear();
1920 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001921}
1922
Matthias Braun17cb5792016-03-01 20:03:21 +00001923void CodeGenSchedModels::checkCompleteness() {
1924 bool Complete = true;
1925 bool HadCompleteModel = false;
1926 for (const CodeGenProcModel &ProcModel : procModels()) {
Simon Pilgrim1d793b82018-04-05 13:11:36 +00001927 const bool HasItineraries = ProcModel.hasItineraries();
Matthias Braun17cb5792016-03-01 20:03:21 +00001928 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1929 continue;
1930 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1931 if (Inst->hasNoSchedulingInfo)
1932 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001933 if (ProcModel.isUnsupported(*Inst))
1934 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001935 unsigned SCIdx = getSchedClassIdx(*Inst);
1936 if (!SCIdx) {
1937 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1938 PrintError("No schedule information for instruction '"
1939 + Inst->TheDef->getName() + "'");
1940 Complete = false;
1941 }
1942 continue;
1943 }
1944
1945 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1946 if (!SC.Writes.empty())
1947 continue;
Simon Pilgrim1d793b82018-04-05 13:11:36 +00001948 if (HasItineraries && SC.ItinClassDef != nullptr &&
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001949 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001950 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001951
1952 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001953 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1954 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1955 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001956 if (I == InstRWs.end()) {
1957 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1958 Inst->TheDef->getName() + "'");
1959 Complete = false;
1960 }
1961 }
1962 HadCompleteModel = true;
1963 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001964 if (!Complete) {
1965 errs() << "\n\nIncomplete schedule models found.\n"
1966 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1967 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1968 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001969 "you may temporarily use an empty list.\n"
1970 << "- Instructions related to unsupported features can be excluded with "
1971 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1972 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001973 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001974 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001975}
1976
Andrew Trick1e46d482012-09-15 00:20:02 +00001977// Collect itinerary class resources for each processor.
1978void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1979 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1980 const CodeGenProcModel &PM = ProcModels[PIdx];
1981 // For all ItinRW entries.
1982 bool HasMatch = false;
1983 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1984 II != IE; ++II) {
1985 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1986 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1987 continue;
1988 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001989 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1990 + ItinClassDef->getName()
1991 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001992 HasMatch = true;
1993 IdxVec Writes, Reads;
1994 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper9f3293a2018-03-24 21:57:35 +00001995 collectRWResources(Writes, Reads, PIdx);
Andrew Trick1e46d482012-09-15 00:20:02 +00001996 }
1997 }
1998}
1999
Andrew Trickd0b9c442012-10-10 05:43:13 +00002000void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00002001 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00002002 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
2003 if (SchedRW.TheDef) {
2004 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00002005 for (unsigned Idx : ProcIndices)
2006 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00002007 }
2008 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00002009 for (unsigned Idx : ProcIndices)
2010 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00002011 }
2012 }
2013 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
2014 AI != AE; ++AI) {
2015 IdxVec AliasProcIndices;
2016 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
2017 AliasProcIndices.push_back(
2018 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
2019 }
2020 else
2021 AliasProcIndices = ProcIndices;
2022 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
2023 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
2024
2025 IdxVec ExpandedRWs;
2026 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
2027 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
2028 SI != SE; ++SI) {
2029 collectRWResources(*SI, IsRead, AliasProcIndices);
2030 }
2031 }
2032}
Andrew Trick1e46d482012-09-15 00:20:02 +00002033
2034// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00002035void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
2036 ArrayRef<unsigned> Reads,
2037 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00002038 for (unsigned Idx : Writes)
2039 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00002040
Benjamin Kramere1761952015-10-24 12:46:49 +00002041 for (unsigned Idx : Reads)
2042 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00002043}
2044
2045// Find the processor's resource units for this kind of resource.
2046Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002047 const CodeGenProcModel &PM,
2048 ArrayRef<SMLoc> Loc) const {
Andrew Trick1e46d482012-09-15 00:20:02 +00002049 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
2050 return ProcResKind;
2051
Craig Topper24064772014-04-15 07:20:03 +00002052 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00002053 assert(!ProcResourceDefs.empty());
2054 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00002055
Javed Absar67b042c2017-09-13 10:31:10 +00002056 for (Record *ProcResDef : ProcResourceDefs) {
2057 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
2058 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00002059 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002060 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00002061 "Multiple ProcessorResourceUnits associated with "
2062 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00002063 }
Javed Absar67b042c2017-09-13 10:31:10 +00002064 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00002065 }
2066 }
Javed Absar67b042c2017-09-13 10:31:10 +00002067 for (Record *ProcResGroup : ProcResGroups) {
2068 if (ProcResGroup == ProcResKind
2069 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00002070 if (ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002071 PrintFatalError(Loc,
Andrew Trick4e67cba2013-03-14 21:21:50 +00002072 "Multiple ProcessorResourceUnits associated with "
2073 + ProcResKind->getName());
2074 }
Javed Absar67b042c2017-09-13 10:31:10 +00002075 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00002076 }
2077 }
Andrew Trick1e46d482012-09-15 00:20:02 +00002078 if (!ProcUnitDef) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002079 PrintFatalError(Loc,
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00002080 "No ProcessorResources associated with "
2081 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00002082 }
2083 return ProcUnitDef;
2084}
2085
2086// Iteratively add a resource and its super resources.
2087void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002088 CodeGenProcModel &PM,
2089 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002090 while (true) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002091 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +00002092
2093 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00002094 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00002095 return;
2096
2097 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00002098 if (ProcResUnits->isSubClassOf("ProcResGroup"))
2099 return;
2100
Andrew Trick1e46d482012-09-15 00:20:02 +00002101 if (!ProcResUnits->getValueInit("Super")->isComplete())
2102 return;
2103
2104 ProcResKind = ProcResUnits->getValueAsDef("Super");
2105 }
2106}
2107
2108// Add resources for a SchedWrite to this processor if they don't exist.
2109void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00002110 assert(PIdx && "don't add resources to an invalid Processor model");
2111
Andrew Trick1e46d482012-09-15 00:20:02 +00002112 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00002113 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00002114 return;
2115 WRDefs.push_back(ProcWriteResDef);
2116
2117 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
2118 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
2119 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
2120 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +00002121 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick1e46d482012-09-15 00:20:02 +00002122 }
2123}
2124
2125// Add resources for a ReadAdvance to this processor if they don't exist.
2126void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
2127 unsigned PIdx) {
2128 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00002129 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00002130 return;
2131 RADefs.push_back(ProcReadAdvanceDef);
2132}
2133
Andrew Trick8fa00f52012-09-17 22:18:43 +00002134unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00002135 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00002136 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00002137 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
2138 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00002139 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00002140 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00002141}
2142
Simon Dardis5f95c9a2016-06-24 08:43:27 +00002143bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
2144 for (const Record *TheDef : UnsupportedFeaturesDefs) {
2145 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
2146 if (TheDef->getName() == PredDef->getName())
2147 return true;
2148 }
2149 }
2150 return false;
2151}
2152
Andrew Trick76686492012-09-15 00:19:57 +00002153#ifndef NDEBUG
2154void CodeGenProcModel::dump() const {
2155 dbgs() << Index << ": " << ModelName << " "
2156 << (ModelDef ? ModelDef->getName() : "inferred") << " "
2157 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
2158}
2159
2160void CodeGenSchedRW::dump() const {
2161 dbgs() << Name << (IsVariadic ? " (V) " : " ");
2162 if (IsSequence) {
2163 dbgs() << "(";
2164 dumpIdxVec(Sequence);
2165 dbgs() << ")";
2166 }
2167}
2168
2169void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00002170 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00002171 << " Writes: ";
2172 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
2173 SchedModels->getSchedWrite(Writes[i]).dump();
2174 if (i < N-1) {
2175 dbgs() << '\n';
2176 dbgs().indent(10);
2177 }
2178 }
2179 dbgs() << "\n Reads: ";
2180 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
2181 SchedModels->getSchedRead(Reads[i]).dump();
2182 if (i < N-1) {
2183 dbgs() << '\n';
2184 dbgs().indent(10);
2185 }
2186 }
2187 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00002188 if (!Transitions.empty()) {
2189 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00002190 for (const CodeGenSchedTransition &Transition : Transitions) {
2191 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00002192 }
2193 }
Andrew Trick76686492012-09-15 00:19:57 +00002194}
Andrew Trick33401e82012-09-15 00:19:59 +00002195
2196void PredTransitions::dump() const {
2197 dbgs() << "Expanded Variants:\n";
2198 for (std::vector<PredTransition>::const_iterator
2199 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
2200 dbgs() << "{";
2201 for (SmallVectorImpl<PredCheck>::const_iterator
2202 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
2203 PCI != PCE; ++PCI) {
2204 if (PCI != TI->PredTerm.begin())
2205 dbgs() << ", ";
2206 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
2207 << ":" << PCI->Predicate->getName();
2208 }
2209 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002210 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00002211 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
2212 WSI != WSE; ++WSI) {
2213 dbgs() << "(";
2214 for (SmallVectorImpl<unsigned>::const_iterator
2215 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
2216 if (WI != WSI->begin())
2217 dbgs() << ", ";
2218 dbgs() << SchedModels.getSchedWrite(*WI).Name;
2219 }
2220 dbgs() << "),";
2221 }
2222 dbgs() << "}\n";
2223 }
2224}
Andrew Trick76686492012-09-15 00:19:57 +00002225#endif // NDEBUG