blob: 6d259cbb33ee3e2908cfa612a881ccf6c5802c71 [file] [log] [blame]
Andrew Trick2661b412012-07-07 04:00:00 +00001//===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alp Tokerae43cab62014-01-24 17:20:08 +000010// This file defines structures to encapsulate the machine model as described in
Andrew Trick2661b412012-07-07 04:00:00 +000011// the target description.
12//
13//===----------------------------------------------------------------------===//
14
Andrew Trick2661b412012-07-07 04:00:00 +000015#include "CodeGenSchedule.h"
Benjamin Kramered48d8b2018-01-23 23:05:04 +000016#include "CodeGenInstruction.h"
Andrew Trick2661b412012-07-07 04:00:00 +000017#include "CodeGenTarget.h"
Craig Topperb6f73f82018-03-21 02:48:34 +000018#include "llvm/ADT/MapVector.h"
Benjamin Kramered48d8b2018-01-23 23:05:04 +000019#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000023#include "llvm/Support/Casting.h"
Andrew Trick2661b412012-07-07 04:00:00 +000024#include "llvm/Support/Debug.h"
Andrew Trick13745262012-10-03 23:06:32 +000025#include "llvm/Support/Regex.h"
Benjamin Kramered48d8b2018-01-23 23:05:04 +000026#include "llvm/Support/raw_ostream.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000027#include "llvm/TableGen/Error.h"
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000028#include <algorithm>
29#include <iterator>
30#include <utility>
Andrew Trick2661b412012-07-07 04:00:00 +000031
32using namespace llvm;
33
Chandler Carruth915c29c2014-04-22 03:06:00 +000034#define DEBUG_TYPE "subtarget-emitter"
35
Andrew Trick48605c32012-09-15 00:19:57 +000036#ifndef NDEBUG
Benjamin Kramer109f9e62015-10-24 12:46:49 +000037static void dumpIdxVec(ArrayRef<unsigned> V) {
38 for (unsigned Idx : V)
39 dbgs() << Idx << ", ";
Andrew Trick5e613c22012-09-15 00:19:59 +000040}
Andrew Trick48605c32012-09-15 00:19:57 +000041#endif
42
Juergen Ributzkaba0f9912013-11-19 03:08:35 +000043namespace {
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000044
Andrew Trick13745262012-10-03 23:06:32 +000045// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
46struct InstrsOp : public SetTheory::Operator {
Craig Topper0bfd5262014-03-05 05:17:42 +000047 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
48 ArrayRef<SMLoc> Loc) override {
Juergen Ributzkaba0f9912013-11-19 03:08:35 +000049 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
50 }
51};
Juergen Ributzka35436252013-11-19 00:57:56 +000052
Andrew Trick13745262012-10-03 23:06:32 +000053// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
Andrew Trick13745262012-10-03 23:06:32 +000054struct InstRegexOp : public SetTheory::Operator {
55 const CodeGenTarget &Target;
56 InstRegexOp(const CodeGenTarget &t): Target(t) {}
57
Benjamin Kramered48d8b2018-01-23 23:05:04 +000058 /// Remove any text inside of parentheses from S.
59 static std::string removeParens(llvm::StringRef S) {
60 std::string Result;
61 unsigned Paren = 0;
62 // NB: We don't care about escaped parens here.
63 for (char C : S) {
64 switch (C) {
65 case '(':
66 ++Paren;
67 break;
68 case ')':
69 --Paren;
70 break;
71 default:
72 if (Paren == 0)
73 Result += C;
74 }
75 }
76 return Result;
77 }
78
Juergen Ributzkaba0f9912013-11-19 03:08:35 +000079 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
Craig Topper0bfd5262014-03-05 05:17:42 +000080 ArrayRef<SMLoc> Loc) override {
Roman Tereshinc349f812018-05-23 20:45:43 +000081 ArrayRef<const CodeGenInstruction *> Instructions =
82 Target.getInstructionsByEnumValue();
83
84 unsigned NumGeneric = Target.getNumFixedInstructions();
Roman Tereshin673840e2018-05-23 22:10:21 +000085 unsigned NumPseudos = Target.getNumPseudoInstructions();
Roman Tereshinc349f812018-05-23 20:45:43 +000086 auto Generics = Instructions.slice(0, NumGeneric);
Roman Tereshin673840e2018-05-23 22:10:21 +000087 auto Pseudos = Instructions.slice(NumGeneric, NumPseudos);
88 auto NonPseudos = Instructions.slice(NumGeneric + NumPseudos);
Roman Tereshinc349f812018-05-23 20:45:43 +000089
Javed Absar3c69ee52017-10-05 13:27:43 +000090 for (Init *Arg : make_range(Expr->arg_begin(), Expr->arg_end())) {
91 StringInit *SI = dyn_cast<StringInit>(Arg);
Juergen Ributzkaba0f9912013-11-19 03:08:35 +000092 if (!SI)
Benjamin Kramered48d8b2018-01-23 23:05:04 +000093 PrintFatalError(Loc, "instregex requires pattern string: " +
94 Expr->getAsString());
Simon Pilgrim10d59492018-03-20 22:20:28 +000095 StringRef Original = SI->getValue();
96
Benjamin Kramered48d8b2018-01-23 23:05:04 +000097 // Extract a prefix that we can binary search on.
98 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
Simon Pilgrim10d59492018-03-20 22:20:28 +000099 auto FirstMeta = Original.find_first_of(RegexMetachars);
100
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000101 // Look for top-level | or ?. We cannot optimize them to binary search.
Simon Pilgrim10d59492018-03-20 22:20:28 +0000102 if (removeParens(Original).find_first_of("|?") != std::string::npos)
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000103 FirstMeta = 0;
Simon Pilgrim10d59492018-03-20 22:20:28 +0000104
105 Optional<Regex> Regexpr = None;
106 StringRef Prefix = Original.substr(0, FirstMeta);
Simon Pilgrim79483b22018-03-24 21:04:20 +0000107 StringRef PatStr = Original.substr(FirstMeta);
108 if (!PatStr.empty()) {
Simon Pilgrim10d59492018-03-20 22:20:28 +0000109 // For the rest use a python-style prefix match.
Simon Pilgrim79483b22018-03-24 21:04:20 +0000110 std::string pat = PatStr;
Simon Pilgrim10d59492018-03-20 22:20:28 +0000111 if (pat[0] != '^') {
112 pat.insert(0, "^(");
113 pat.insert(pat.end(), ')');
114 }
115 Regexpr = Regex(pat);
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000116 }
Simon Pilgrim10d59492018-03-20 22:20:28 +0000117
Simon Pilgrimfb875052018-03-25 19:20:08 +0000118 int NumMatches = 0;
119
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000120 // The generic opcodes are unsorted, handle them manually.
Simon Pilgrim10d59492018-03-20 22:20:28 +0000121 for (auto *Inst : Generics) {
122 StringRef InstName = Inst->TheDef->getName();
123 if (InstName.startswith(Prefix) &&
Simon Pilgrimfb875052018-03-25 19:20:08 +0000124 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))) {
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000125 Elts.insert(Inst->TheDef);
Simon Pilgrimfb875052018-03-25 19:20:08 +0000126 NumMatches++;
127 }
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000128 }
129
Roman Tereshin673840e2018-05-23 22:10:21 +0000130 // Target instructions are split into two ranges: pseudo instructions
131 // first, than non-pseudos. Each range is in lexicographical order
132 // sorted by name. Find the sub-ranges that start with our prefix.
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000133 struct Comp {
134 bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
135 return LHS->TheDef->getName() < RHS;
136 }
137 bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
138 return LHS < RHS->TheDef->getName() &&
139 !RHS->TheDef->getName().startswith(LHS);
140 }
141 };
Roman Tereshin673840e2018-05-23 22:10:21 +0000142 auto Range1 =
143 std::equal_range(Pseudos.begin(), Pseudos.end(), Prefix, Comp());
144 auto Range2 = std::equal_range(NonPseudos.begin(), NonPseudos.end(),
145 Prefix, Comp());
Benjamin Kramered48d8b2018-01-23 23:05:04 +0000146
Roman Tereshin673840e2018-05-23 22:10:21 +0000147 // For these ranges we know that instruction names start with the prefix.
148 // Check if there's a regex that needs to be checked.
Roman Tereshinc349f812018-05-23 20:45:43 +0000149 const auto HandleNonGeneric = [&](const CodeGenInstruction *Inst) {
Simon Pilgrim10d59492018-03-20 22:20:28 +0000150 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrimfb875052018-03-25 19:20:08 +0000151 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))) {
Craig Topperd5578c82014-12-09 08:05:51 +0000152 Elts.insert(Inst->TheDef);
Simon Pilgrimfb875052018-03-25 19:20:08 +0000153 NumMatches++;
154 }
Roman Tereshinc349f812018-05-23 20:45:43 +0000155 };
Roman Tereshin673840e2018-05-23 22:10:21 +0000156 std::for_each(Range1.first, Range1.second, HandleNonGeneric);
157 std::for_each(Range2.first, Range2.second, HandleNonGeneric);
Simon Pilgrimfb875052018-03-25 19:20:08 +0000158
159 if (0 == NumMatches)
160 PrintFatalError(Loc, "instregex has no matches: " + Original);
Juergen Ributzkaba0f9912013-11-19 03:08:35 +0000161 }
Juergen Ributzkaba0f9912013-11-19 03:08:35 +0000162 }
Andrew Trick13745262012-10-03 23:06:32 +0000163};
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000164
Juergen Ributzkaba0f9912013-11-19 03:08:35 +0000165} // end anonymous namespace
Juergen Ributzka35436252013-11-19 00:57:56 +0000166
Andrew Trick48605c32012-09-15 00:19:57 +0000167/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick2661b412012-07-07 04:00:00 +0000168CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
169 const CodeGenTarget &TGT):
Andrew Trick1ab961f2013-03-16 18:58:55 +0000170 Records(RK), Target(TGT) {
Andrew Trick2661b412012-07-07 04:00:00 +0000171
Andrew Trick13745262012-10-03 23:06:32 +0000172 Sets.addFieldExpander("InstRW", "Instrs");
173
174 // Allow Set evaluation to recognize the dags used in InstRW records:
175 // (instrs Op1, Op1...)
Craig Toppera9a01d92015-04-24 06:49:44 +0000176 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
177 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick13745262012-10-03 23:06:32 +0000178
Andrew Trick48605c32012-09-15 00:19:57 +0000179 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
180 // that are explicitly referenced in tablegen records. Resources associated
181 // with each processor will be derived later. Populate ProcModelMap with the
182 // CodeGenProcModel instances.
183 collectProcModels();
Andrew Trick2661b412012-07-07 04:00:00 +0000184
Andrew Trick48605c32012-09-15 00:19:57 +0000185 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
186 // defined, and populate SchedReads and SchedWrites vectors. Implicit
187 // SchedReadWrites that represent sequences derived from expanded variant will
188 // be inferred later.
189 collectSchedRW();
190
191 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
192 // required by an instruction definition, and populate SchedClassIdxMap. Set
193 // NumItineraryClasses to the number of explicit itinerary classes referenced
194 // by instructions. Set NumInstrSchedClasses to the number of itinerary
195 // classes plus any classes implied by instructions that derive from class
196 // Sched and provide SchedRW list. This does not infer any new classes from
197 // SchedVariant.
198 collectSchedClasses();
199
200 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick92649882012-09-22 02:24:21 +0000201 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick48605c32012-09-15 00:19:57 +0000202 // all itinerary classes to be discovered.
203 collectProcItins();
204
205 // Find ItinRW records for each processor and itinerary class.
206 // (For per-operand resources mapped to itinerary classes).
207 collectProcItinRW();
Andrew Trick5e613c22012-09-15 00:19:59 +0000208
Simon Dardis111ec252016-06-24 08:43:27 +0000209 // Find UnsupportedFeatures records for each processor.
210 // (For per-operand resources mapped to itinerary classes).
211 collectProcUnsupportedFeatures();
212
Andrew Trick5e613c22012-09-15 00:19:59 +0000213 // Infer new SchedClasses from SchedVariant.
214 inferSchedClasses();
215
Andrew Trick3cbd1782012-09-15 00:20:02 +0000216 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
217 // ProcResourceDefs.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000218 LLVM_DEBUG(
219 dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick3cbd1782012-09-15 00:20:02 +0000220 collectProcResources();
Matthias Brauneab28692016-03-01 20:03:21 +0000221
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000222 // Collect optional processor description.
223 collectOptionalProcessorInfo();
224
Andrea Di Biagioe9759dd2018-08-14 18:36:54 +0000225 // Check MCInstPredicate definitions.
226 checkMCInstPredicates();
227
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +0000228 // Check STIPredicate definitions.
229 checkSTIPredicates();
230
231 // Find STIPredicate definitions for each processor model, and construct
232 // STIPredicateFunction objects.
233 collectSTIPredicates();
234
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000235 checkCompleteness();
236}
237
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +0000238void CodeGenSchedModels::checkSTIPredicates() const {
239 DenseMap<StringRef, const Record *> Declarations;
240
241 // There cannot be multiple declarations with the same name.
242 const RecVec Decls = Records.getAllDerivedDefinitions("STIPredicateDecl");
243 for (const Record *R : Decls) {
244 StringRef Name = R->getValueAsString("Name");
245 const auto It = Declarations.find(Name);
246 if (It == Declarations.end()) {
247 Declarations[Name] = R;
248 continue;
249 }
250
251 PrintError(R->getLoc(), "STIPredicate " + Name + " multiply declared.");
252 PrintNote(It->second->getLoc(), "Previous declaration was here.");
253 PrintFatalError(R->getLoc(), "Invalid STIPredicateDecl found.");
254 }
255
256 // Disallow InstructionEquivalenceClasses with an empty instruction list.
257 const RecVec Defs =
258 Records.getAllDerivedDefinitions("InstructionEquivalenceClass");
259 for (const Record *R : Defs) {
260 RecVec Opcodes = R->getValueAsListOfDefs("Opcodes");
261 if (Opcodes.empty()) {
262 PrintFatalError(R->getLoc(), "Invalid InstructionEquivalenceClass "
263 "defined with an empty opcode list.");
264 }
265 }
266}
267
268// Used by function `processSTIPredicate` to construct a mask of machine
269// instruction operands.
270static APInt constructOperandMask(ArrayRef<int64_t> Indices) {
271 APInt OperandMask;
272 if (Indices.empty())
273 return OperandMask;
274
275 int64_t MaxIndex = *std::max_element(Indices.begin(), Indices.end());
276 assert(MaxIndex >= 0 && "Invalid negative indices in input!");
277 OperandMask = OperandMask.zext(MaxIndex + 1);
278 for (const int64_t Index : Indices) {
279 assert(Index >= 0 && "Invalid negative indices!");
280 OperandMask.setBit(Index);
281 }
282
283 return OperandMask;
284}
285
286static void
287processSTIPredicate(STIPredicateFunction &Fn,
288 const DenseMap<Record *, unsigned> &ProcModelMap) {
289 DenseMap<const Record *, unsigned> Opcode2Index;
290 using OpcodeMapPair = std::pair<const Record *, OpcodeInfo>;
291 std::vector<OpcodeMapPair> OpcodeMappings;
292 std::vector<std::pair<APInt, APInt>> OpcodeMasks;
293
294 DenseMap<const Record *, unsigned> Predicate2Index;
295 unsigned NumUniquePredicates = 0;
296
297 // Number unique predicates and opcodes used by InstructionEquivalenceClass
298 // definitions. Each unique opcode will be associated with an OpcodeInfo
299 // object.
300 for (const Record *Def : Fn.getDefinitions()) {
301 RecVec Classes = Def->getValueAsListOfDefs("Classes");
302 for (const Record *EC : Classes) {
303 const Record *Pred = EC->getValueAsDef("Predicate");
304 if (Predicate2Index.find(Pred) == Predicate2Index.end())
305 Predicate2Index[Pred] = NumUniquePredicates++;
306
307 RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
308 for (const Record *Opcode : Opcodes) {
309 if (Opcode2Index.find(Opcode) == Opcode2Index.end()) {
310 Opcode2Index[Opcode] = OpcodeMappings.size();
311 OpcodeMappings.emplace_back(Opcode, OpcodeInfo());
312 }
313 }
314 }
315 }
316
317 // Initialize vector `OpcodeMasks` with default values. We want to keep track
318 // of which processors "use" which opcodes. We also want to be able to
319 // identify predicates that are used by different processors for a same
320 // opcode.
321 // This information is used later on by this algorithm to sort OpcodeMapping
322 // elements based on their processor and predicate sets.
323 OpcodeMasks.resize(OpcodeMappings.size());
324 APInt DefaultProcMask(ProcModelMap.size(), 0);
325 APInt DefaultPredMask(NumUniquePredicates, 0);
326 for (std::pair<APInt, APInt> &MaskPair : OpcodeMasks)
327 MaskPair = std::make_pair(DefaultProcMask, DefaultPredMask);
328
329 // Construct a OpcodeInfo object for every unique opcode declared by an
330 // InstructionEquivalenceClass definition.
331 for (const Record *Def : Fn.getDefinitions()) {
332 RecVec Classes = Def->getValueAsListOfDefs("Classes");
333 const Record *SchedModel = Def->getValueAsDef("SchedModel");
334 unsigned ProcIndex = ProcModelMap.find(SchedModel)->second;
335 APInt ProcMask(ProcModelMap.size(), 0);
336 ProcMask.setBit(ProcIndex);
337
338 for (const Record *EC : Classes) {
339 RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
340
341 std::vector<int64_t> OpIndices =
342 EC->getValueAsListOfInts("OperandIndices");
343 APInt OperandMask = constructOperandMask(OpIndices);
344
345 const Record *Pred = EC->getValueAsDef("Predicate");
346 APInt PredMask(NumUniquePredicates, 0);
347 PredMask.setBit(Predicate2Index[Pred]);
348
349 for (const Record *Opcode : Opcodes) {
350 unsigned OpcodeIdx = Opcode2Index[Opcode];
351 if (OpcodeMasks[OpcodeIdx].first[ProcIndex]) {
352 std::string Message =
Clement Courbetf4fb61b2018-10-25 07:44:01 +0000353 "Opcode " + Opcode->getName().str() +
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +0000354 " used by multiple InstructionEquivalenceClass definitions.";
355 PrintFatalError(EC->getLoc(), Message);
356 }
357 OpcodeMasks[OpcodeIdx].first |= ProcMask;
358 OpcodeMasks[OpcodeIdx].second |= PredMask;
359 OpcodeInfo &OI = OpcodeMappings[OpcodeIdx].second;
360
361 OI.addPredicateForProcModel(ProcMask, OperandMask, Pred);
362 }
363 }
364 }
365
366 // Sort OpcodeMappings elements based on their CPU and predicate masks.
367 // As a last resort, order elements by opcode identifier.
Fangrui Song3b35e172018-09-27 02:13:45 +0000368 llvm::sort(OpcodeMappings,
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +0000369 [&](const OpcodeMapPair &Lhs, const OpcodeMapPair &Rhs) {
370 unsigned LhsIdx = Opcode2Index[Lhs.first];
371 unsigned RhsIdx = Opcode2Index[Rhs.first];
372 std::pair<APInt, APInt> &LhsMasks = OpcodeMasks[LhsIdx];
373 std::pair<APInt, APInt> &RhsMasks = OpcodeMasks[RhsIdx];
374
375 if (LhsMasks.first != RhsMasks.first) {
376 if (LhsMasks.first.countPopulation() <
377 RhsMasks.first.countPopulation())
378 return true;
379 return LhsMasks.first.countLeadingZeros() >
380 RhsMasks.first.countLeadingZeros();
381 }
382
383 if (LhsMasks.second != RhsMasks.second) {
384 if (LhsMasks.second.countPopulation() <
385 RhsMasks.second.countPopulation())
386 return true;
387 return LhsMasks.second.countLeadingZeros() >
388 RhsMasks.second.countLeadingZeros();
389 }
390
391 return LhsIdx < RhsIdx;
392 });
393
394 // Now construct opcode groups. Groups are used by the SubtargetEmitter when
395 // expanding the body of a STIPredicate function. In particular, each opcode
396 // group is expanded into a sequence of labels in a switch statement.
397 // It identifies opcodes for which different processors define same predicates
398 // and same opcode masks.
399 for (OpcodeMapPair &Info : OpcodeMappings)
400 Fn.addOpcode(Info.first, std::move(Info.second));
401}
402
403void CodeGenSchedModels::collectSTIPredicates() {
404 // Map STIPredicateDecl records to elements of vector
405 // CodeGenSchedModels::STIPredicates.
406 DenseMap<const Record *, unsigned> Decl2Index;
407
408 RecVec RV = Records.getAllDerivedDefinitions("STIPredicate");
409 for (const Record *R : RV) {
410 const Record *Decl = R->getValueAsDef("Declaration");
411
412 const auto It = Decl2Index.find(Decl);
413 if (It == Decl2Index.end()) {
414 Decl2Index[Decl] = STIPredicates.size();
415 STIPredicateFunction Predicate(Decl);
416 Predicate.addDefinition(R);
417 STIPredicates.emplace_back(std::move(Predicate));
418 continue;
419 }
420
421 STIPredicateFunction &PreviousDef = STIPredicates[It->second];
422 PreviousDef.addDefinition(R);
423 }
424
425 for (STIPredicateFunction &Fn : STIPredicates)
426 processSTIPredicate(Fn, ProcModelMap);
427}
428
429void OpcodeInfo::addPredicateForProcModel(const llvm::APInt &CpuMask,
430 const llvm::APInt &OperandMask,
431 const Record *Predicate) {
432 auto It = llvm::find_if(
433 Predicates, [&OperandMask, &Predicate](const PredicateInfo &P) {
434 return P.Predicate == Predicate && P.OperandMask == OperandMask;
435 });
436 if (It == Predicates.end()) {
437 Predicates.emplace_back(CpuMask, OperandMask, Predicate);
438 return;
439 }
440 It->ProcModelMask |= CpuMask;
441}
442
Andrea Di Biagioe9759dd2018-08-14 18:36:54 +0000443void CodeGenSchedModels::checkMCInstPredicates() const {
444 RecVec MCPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
445 if (MCPredicates.empty())
446 return;
447
448 // A target cannot have multiple TIIPredicate definitions with a same name.
449 llvm::StringMap<const Record *> TIIPredicates(MCPredicates.size());
450 for (const Record *TIIPred : MCPredicates) {
451 StringRef Name = TIIPred->getValueAsString("FunctionName");
452 StringMap<const Record *>::const_iterator It = TIIPredicates.find(Name);
453 if (It == TIIPredicates.end()) {
454 TIIPredicates[Name] = TIIPred;
455 continue;
456 }
457
458 PrintError(TIIPred->getLoc(),
459 "TIIPredicate " + Name + " is multiply defined.");
460 PrintNote(It->second->getLoc(),
461 " Previous definition of " + Name + " was here.");
462 PrintFatalError(TIIPred->getLoc(),
463 "Found conflicting definitions of TIIPredicate.");
464 }
465}
466
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000467void CodeGenSchedModels::collectRetireControlUnits() {
468 RecVec Units = Records.getAllDerivedDefinitions("RetireControlUnit");
469
470 for (Record *RCU : Units) {
471 CodeGenProcModel &PM = getProcModel(RCU->getValueAsDef("SchedModel"));
472 if (PM.RetireControlUnit) {
473 PrintError(RCU->getLoc(),
474 "Expected a single RetireControlUnit definition");
475 PrintNote(PM.RetireControlUnit->getLoc(),
476 "Previous definition of RetireControlUnit was here");
477 }
478 PM.RetireControlUnit = RCU;
479 }
480}
481
Andrea Di Biagio51af6fd2018-11-29 12:15:56 +0000482void CodeGenSchedModels::collectLoadStoreQueueInfo() {
483 RecVec Queues = Records.getAllDerivedDefinitions("MemoryQueue");
484
485 for (Record *Queue : Queues) {
486 CodeGenProcModel &PM = getProcModel(Queue->getValueAsDef("SchedModel"));
487 if (Queue->isSubClassOf("LoadQueue")) {
488 if (PM.LoadQueue) {
489 PrintError(Queue->getLoc(),
490 "Expected a single LoadQueue definition");
491 PrintNote(PM.LoadQueue->getLoc(),
492 "Previous definition of LoadQueue was here");
493 }
494
495 PM.LoadQueue = Queue;
496 }
497
498 if (Queue->isSubClassOf("StoreQueue")) {
499 if (PM.StoreQueue) {
500 PrintError(Queue->getLoc(),
501 "Expected a single StoreQueue definition");
502 PrintNote(PM.LoadQueue->getLoc(),
503 "Previous definition of StoreQueue was here");
504 }
505
506 PM.StoreQueue = Queue;
507 }
508 }
509}
510
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000511/// Collect optional processor information.
512void CodeGenSchedModels::collectOptionalProcessorInfo() {
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000513 // Find register file definitions for each processor.
514 collectRegisterFiles();
515
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000516 // Collect processor RetireControlUnit descriptors if available.
517 collectRetireControlUnits();
Clement Courbetc486bdf2018-04-10 08:16:37 +0000518
Andrea Di Biagio51af6fd2018-11-29 12:15:56 +0000519 // Collect information about load/store queues.
520 collectLoadStoreQueueInfo();
521
Clement Courbetc486bdf2018-04-10 08:16:37 +0000522 checkCompleteness();
Andrew Trick2661b412012-07-07 04:00:00 +0000523}
524
Andrew Trick48605c32012-09-15 00:19:57 +0000525/// Gather all processor models.
526void CodeGenSchedModels::collectProcModels() {
527 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
Fangrui Song3b35e172018-09-27 02:13:45 +0000528 llvm::sort(ProcRecords, LessRecordFieldName());
Andrew Trick2661b412012-07-07 04:00:00 +0000529
Andrew Trick48605c32012-09-15 00:19:57 +0000530 // Reserve space because we can. Reallocation would be ok.
531 ProcModels.reserve(ProcRecords.size()+1);
532
533 // Use idx=0 for NoModel/NoItineraries.
534 Record *NoModelDef = Records.getDef("NoSchedModel");
535 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramer9589ff82015-05-29 19:43:39 +0000536 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000537 ProcModelMap[NoModelDef] = 0;
538
539 // For each processor, find a unique machine model.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000540 LLVM_DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar0a82c732017-09-13 10:31:10 +0000541 for (Record *ProcRecord : ProcRecords)
542 addProcModel(ProcRecord);
Andrew Trick48605c32012-09-15 00:19:57 +0000543}
544
545/// Get a unique processor model based on the defined MachineModel and
546/// ProcessorItineraries.
547void CodeGenSchedModels::addProcModel(Record *ProcDef) {
548 Record *ModelKey = getModelOrItinDef(ProcDef);
549 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
550 return;
551
552 std::string Name = ModelKey->getName();
553 if (ModelKey->isSubClassOf("SchedMachineModel")) {
554 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramer9589ff82015-05-29 19:43:39 +0000555 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000556 }
557 else {
558 // An itinerary is defined without a machine model. Infer a new model.
559 if (!ModelKey->getValueAsListOfDefs("IID").empty())
560 Name = Name + "Model";
Benjamin Kramer9589ff82015-05-29 19:43:39 +0000561 ProcModels.emplace_back(ProcModels.size(), Name,
562 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick48605c32012-09-15 00:19:57 +0000563 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000564 LLVM_DEBUG(ProcModels.back().dump());
Andrew Trick48605c32012-09-15 00:19:57 +0000565}
566
567// Recursively find all reachable SchedReadWrite records.
568static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
569 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie5401ba72014-11-19 07:49:26 +0000570 if (!RWSet.insert(RWDef).second)
Andrew Trick48605c32012-09-15 00:19:57 +0000571 return;
572 RWDefs.push_back(RWDef);
Javed Absar0a82c732017-09-13 10:31:10 +0000573 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick48605c32012-09-15 00:19:57 +0000574 if (RWDef->isSubClassOf("WriteSequence")) {
575 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar0a82c732017-09-13 10:31:10 +0000576 for (Record *WSRec : Seq)
577 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000578 }
579 else if (RWDef->isSubClassOf("SchedVariant")) {
580 // Visit each variant (guarded by a different predicate).
581 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar0a82c732017-09-13 10:31:10 +0000582 for (Record *Variant : Vars) {
Andrew Trick48605c32012-09-15 00:19:57 +0000583 // Visit each RW in the sequence selected by the current variant.
Javed Absar0a82c732017-09-13 10:31:10 +0000584 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
585 for (Record *SelDef : Selected)
586 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000587 }
588 }
589}
590
591// Collect and sort all SchedReadWrites reachable via tablegen records.
592// More may be inferred later when inferring new SchedClasses from variants.
593void CodeGenSchedModels::collectSchedRW() {
594 // Reserve idx=0 for invalid writes/reads.
595 SchedWrites.resize(1);
596 SchedReads.resize(1);
597
598 SmallPtrSet<Record*, 16> RWSet;
599
600 // Find all SchedReadWrites referenced by instruction defs.
601 RecVec SWDefs, SRDefs;
Craig Toppere4b85522016-01-17 20:38:18 +0000602 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperd5578c82014-12-09 08:05:51 +0000603 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesen64110ff2013-03-15 22:51:13 +0000604 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick48605c32012-09-15 00:19:57 +0000605 continue;
606 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar0a82c732017-09-13 10:31:10 +0000607 for (Record *RW : RWs) {
608 if (RW->isSubClassOf("SchedWrite"))
609 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000610 else {
Javed Absar0a82c732017-09-13 10:31:10 +0000611 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
612 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000613 }
614 }
615 }
616 // Find all ReadWrites referenced by InstRW.
617 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar0a82c732017-09-13 10:31:10 +0000618 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick48605c32012-09-15 00:19:57 +0000619 // For all OperandReadWrites.
Javed Absar0a82c732017-09-13 10:31:10 +0000620 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
621 for (Record *RWDef : RWDefs) {
622 if (RWDef->isSubClassOf("SchedWrite"))
623 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000624 else {
Javed Absar0a82c732017-09-13 10:31:10 +0000625 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
626 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000627 }
628 }
629 }
630 // Find all ReadWrites referenced by ItinRW.
631 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar0a82c732017-09-13 10:31:10 +0000632 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick48605c32012-09-15 00:19:57 +0000633 // For all OperandReadWrites.
Javed Absar0a82c732017-09-13 10:31:10 +0000634 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
635 for (Record *RWDef : RWDefs) {
636 if (RWDef->isSubClassOf("SchedWrite"))
637 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000638 else {
Javed Absar0a82c732017-09-13 10:31:10 +0000639 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
640 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick48605c32012-09-15 00:19:57 +0000641 }
642 }
643 }
Andrew Trick92649882012-09-22 02:24:21 +0000644 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
645 // for the loop below that initializes Alias vectors.
646 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
Fangrui Song3b35e172018-09-27 02:13:45 +0000647 llvm::sort(AliasDefs, LessRecord());
Javed Absar0a82c732017-09-13 10:31:10 +0000648 for (Record *ADef : AliasDefs) {
649 Record *MatchDef = ADef->getValueAsDef("MatchRW");
650 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick92649882012-09-22 02:24:21 +0000651 if (MatchDef->isSubClassOf("SchedWrite")) {
652 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar0a82c732017-09-13 10:31:10 +0000653 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick92649882012-09-22 02:24:21 +0000654 scanSchedRW(AliasDef, SWDefs, RWSet);
655 }
656 else {
657 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
658 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar0a82c732017-09-13 10:31:10 +0000659 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick92649882012-09-22 02:24:21 +0000660 scanSchedRW(AliasDef, SRDefs, RWSet);
661 }
662 }
Andrew Trick48605c32012-09-15 00:19:57 +0000663 // Sort and add the SchedReadWrites directly referenced by instructions or
664 // itinerary resources. Index reads and writes in separate domains.
Fangrui Song3b35e172018-09-27 02:13:45 +0000665 llvm::sort(SWDefs, LessRecord());
Javed Absar0a82c732017-09-13 10:31:10 +0000666 for (Record *SWDef : SWDefs) {
667 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
668 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000669 }
Fangrui Song3b35e172018-09-27 02:13:45 +0000670 llvm::sort(SRDefs, LessRecord());
Javed Absar0a82c732017-09-13 10:31:10 +0000671 for (Record *SRDef : SRDefs) {
672 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
673 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000674 }
675 // Initialize WriteSequence vectors.
Javed Absar0a82c732017-09-13 10:31:10 +0000676 for (CodeGenSchedRW &CGRW : SchedWrites) {
677 if (!CGRW.IsSequence)
Andrew Trick48605c32012-09-15 00:19:57 +0000678 continue;
Javed Absar0a82c732017-09-13 10:31:10 +0000679 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick48605c32012-09-15 00:19:57 +0000680 /*IsRead=*/false);
681 }
Andrew Trick92649882012-09-22 02:24:21 +0000682 // Initialize Aliases vectors.
Javed Absar0a82c732017-09-13 10:31:10 +0000683 for (Record *ADef : AliasDefs) {
684 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick92649882012-09-22 02:24:21 +0000685 getSchedRW(AliasDef).IsAlias = true;
Javed Absar0a82c732017-09-13 10:31:10 +0000686 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick92649882012-09-22 02:24:21 +0000687 CodeGenSchedRW &RW = getSchedRW(MatchDef);
688 if (RW.IsAlias)
Javed Absar0a82c732017-09-13 10:31:10 +0000689 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
690 RW.Aliases.push_back(ADef);
Andrew Trick92649882012-09-22 02:24:21 +0000691 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000692 LLVM_DEBUG(
693 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
694 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
695 dbgs() << WIdx << ": ";
696 SchedWrites[WIdx].dump();
697 dbgs() << '\n';
698 } for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd;
699 ++RIdx) {
700 dbgs() << RIdx << ": ";
701 SchedReads[RIdx].dump();
702 dbgs() << '\n';
703 } RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
704 for (Record *RWDef
705 : RWDefs) {
706 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
707 StringRef Name = RWDef->getName();
708 if (Name != "NoWrite" && Name != "ReadDefault")
709 dbgs() << "Unused SchedReadWrite " << Name << '\n';
710 }
711 });
Andrew Trick48605c32012-09-15 00:19:57 +0000712}
713
714/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000715std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick48605c32012-09-15 00:19:57 +0000716 std::string Name("(");
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000717 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick48605c32012-09-15 00:19:57 +0000718 if (I != Seq.begin())
719 Name += '_';
720 Name += getSchedRW(*I, IsRead).Name;
721 }
722 Name += ')';
723 return Name;
724}
725
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000726unsigned CodeGenSchedModels::getSchedRWIdx(const Record *Def,
727 bool IsRead) const {
Andrew Trick48605c32012-09-15 00:19:57 +0000728 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000729 const auto I = find_if(
730 RWVec, [Def](const CodeGenSchedRW &RW) { return RW.TheDef == Def; });
731 return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
Andrew Trick48605c32012-09-15 00:19:57 +0000732}
733
Andrew Trick3b8fb642012-09-19 04:43:19 +0000734bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar0a82c732017-09-13 10:31:10 +0000735 for (const CodeGenSchedRW &Read : SchedReads) {
736 Record *ReadDef = Read.TheDef;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000737 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
738 continue;
739
740 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer975248e2016-08-11 22:21:41 +0000741 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trick3b8fb642012-09-19 04:43:19 +0000742 return true;
743 }
744 }
745 return false;
746}
747
Craig Topper6eda3292018-03-21 05:13:01 +0000748static void splitSchedReadWrites(const RecVec &RWDefs,
749 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar0a82c732017-09-13 10:31:10 +0000750 for (Record *RWDef : RWDefs) {
751 if (RWDef->isSubClassOf("SchedWrite"))
752 WriteDefs.push_back(RWDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000753 else {
Javed Absar0a82c732017-09-13 10:31:10 +0000754 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
755 ReadDefs.push_back(RWDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000756 }
757 }
758}
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000759
Andrew Trick48605c32012-09-15 00:19:57 +0000760// Split the SchedReadWrites defs and call findRWs for each list.
761void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
762 IdxVec &Writes, IdxVec &Reads) const {
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000763 RecVec WriteDefs;
764 RecVec ReadDefs;
765 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
766 findRWs(WriteDefs, Writes, false);
767 findRWs(ReadDefs, Reads, true);
Andrew Trick48605c32012-09-15 00:19:57 +0000768}
769
770// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
771void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
772 bool IsRead) const {
Javed Absar0a82c732017-09-13 10:31:10 +0000773 for (Record *RWDef : RWDefs) {
774 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick48605c32012-09-15 00:19:57 +0000775 assert(Idx && "failed to collect SchedReadWrite");
776 RWs.push_back(Idx);
777 }
778}
779
Andrew Trick5e613c22012-09-15 00:19:59 +0000780void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
781 bool IsRead) const {
782 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
783 if (!SchedRW.IsSequence) {
784 RWSeq.push_back(RWIdx);
785 return;
786 }
787 int Repeat =
788 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
789 for (int i = 0; i < Repeat; ++i) {
Javed Absar0a82c732017-09-13 10:31:10 +0000790 for (unsigned I : SchedRW.Sequence) {
791 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick5e613c22012-09-15 00:19:59 +0000792 }
793 }
794}
795
Andrew Trick2062b122012-10-03 23:06:28 +0000796// Expand a SchedWrite as a sequence following any aliases that coincide with
797// the given processor model.
798void CodeGenSchedModels::expandRWSeqForProc(
799 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
800 const CodeGenProcModel &ProcModel) const {
801
802 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper095734c2014-04-15 07:20:03 +0000803 Record *AliasDef = nullptr;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000804 for (const Record *Rec : SchedWrite.Aliases) {
805 const CodeGenSchedRW &AliasRW = getSchedRW(Rec->getValueAsDef("AliasRW"));
806 if (Rec->getValueInit("SchedModel")->isComplete()) {
807 Record *ModelDef = Rec->getValueAsDef("SchedModel");
Andrew Trick2062b122012-10-03 23:06:28 +0000808 if (&getProcModel(ModelDef) != &ProcModel)
809 continue;
810 }
811 if (AliasDef)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000812 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
813 "defined for processor " + ProcModel.ModelName +
814 " Ensure only one SchedAlias exists per RW.");
Andrew Trick2062b122012-10-03 23:06:28 +0000815 AliasDef = AliasRW.TheDef;
816 }
817 if (AliasDef) {
818 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
819 RWSeq, IsRead,ProcModel);
820 return;
821 }
822 if (!SchedWrite.IsSequence) {
823 RWSeq.push_back(RWIdx);
824 return;
825 }
826 int Repeat =
827 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000828 for (int I = 0, E = Repeat; I < E; ++I) {
829 for (unsigned Idx : SchedWrite.Sequence) {
830 expandRWSeqForProc(Idx, RWSeq, IsRead, ProcModel);
Andrew Trick2062b122012-10-03 23:06:28 +0000831 }
832 }
833}
834
Andrew Trick5e613c22012-09-15 00:19:59 +0000835// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000836unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick5e613c22012-09-15 00:19:59 +0000837 bool IsRead) {
838 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
839
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000840 auto I = find_if(RWVec, [Seq](CodeGenSchedRW &RW) {
841 return makeArrayRef(RW.Sequence) == Seq;
842 });
Andrew Trick5e613c22012-09-15 00:19:59 +0000843 // Index zero reserved for invalid RW.
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000844 return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
Andrew Trick5e613c22012-09-15 00:19:59 +0000845}
846
847/// Add this ReadWrite if it doesn't already exist.
848unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
849 bool IsRead) {
850 assert(!Seq.empty() && "cannot insert empty sequence");
851 if (Seq.size() == 1)
852 return Seq.back();
853
854 unsigned Idx = findRWForSequence(Seq, IsRead);
855 if (Idx)
856 return Idx;
857
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000858 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
859 unsigned RWIdx = RWVec.size();
Andrew Trick2062b122012-10-03 23:06:28 +0000860 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000861 RWVec.push_back(SchedRW);
Andrew Trick2062b122012-10-03 23:06:28 +0000862 return RWIdx;
Andrew Trick5e613c22012-09-15 00:19:59 +0000863}
864
Andrew Trick48605c32012-09-15 00:19:57 +0000865/// Visit all the instruction definitions for this target to gather and
866/// enumerate the itinerary classes. These are the explicitly specified
867/// SchedClasses. More SchedClasses may be inferred.
868void CodeGenSchedModels::collectSchedClasses() {
869
870 // NoItinerary is always the first class at Idx=0
Craig Topperd74de622018-03-22 06:15:08 +0000871 assert(SchedClasses.empty() && "Expected empty sched class");
872 SchedClasses.emplace_back(0, "NoInstrModel",
873 Records.getDef("NoItinerary"));
Andrew Trick48605c32012-09-15 00:19:57 +0000874 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick2661b412012-07-07 04:00:00 +0000875
Andrew Trick1ab961f2013-03-16 18:58:55 +0000876 // Create a SchedClass for each unique combination of itinerary class and
877 // SchedRW list.
Craig Toppere4b85522016-01-17 20:38:18 +0000878 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperd5578c82014-12-09 08:05:51 +0000879 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick48605c32012-09-15 00:19:57 +0000880 IdxVec Writes, Reads;
Craig Topperd5578c82014-12-09 08:05:51 +0000881 if (!Inst->TheDef->isValueUnset("SchedRW"))
882 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trick1ab961f2013-03-16 18:58:55 +0000883
Andrew Trick48605c32012-09-15 00:19:57 +0000884 // ProcIdx == 0 indicates the class applies to all processors.
Craig Topperd74de622018-03-22 06:15:08 +0000885 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
Craig Topperd5578c82014-12-09 08:05:51 +0000886 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick48605c32012-09-15 00:19:57 +0000887 }
Andrew Trick92649882012-09-22 02:24:21 +0000888 // Create classes for InstRW defs.
Andrew Trick48605c32012-09-15 00:19:57 +0000889 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Fangrui Song3b35e172018-09-27 02:13:45 +0000890 llvm::sort(InstRWDefs, LessRecord());
Nicola Zaghen0818e782018-05-14 12:53:11 +0000891 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar0a82c732017-09-13 10:31:10 +0000892 for (Record *RWDef : InstRWDefs)
893 createInstRWClass(RWDef);
Andrew Trick2661b412012-07-07 04:00:00 +0000894
Andrew Trick48605c32012-09-15 00:19:57 +0000895 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick2661b412012-07-07 04:00:00 +0000896
Andrew Trick48605c32012-09-15 00:19:57 +0000897 bool EnableDump = false;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000898 LLVM_DEBUG(EnableDump = true);
Andrew Trick48605c32012-09-15 00:19:57 +0000899 if (!EnableDump)
Andrew Trick2661b412012-07-07 04:00:00 +0000900 return;
Andrew Trick1ab961f2013-03-16 18:58:55 +0000901
Nicola Zaghen0818e782018-05-14 12:53:11 +0000902 LLVM_DEBUG(
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000903 dbgs()
904 << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n");
Craig Toppere4b85522016-01-17 20:38:18 +0000905 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper2a129872017-05-31 21:12:46 +0000906 StringRef InstName = Inst->TheDef->getName();
Simon Pilgrim4a705d12018-03-21 18:09:34 +0000907 unsigned SCIdx = getSchedClassIdx(*Inst);
Andrew Trick1ab961f2013-03-16 18:58:55 +0000908 if (!SCIdx) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000909 LLVM_DEBUG({
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000910 if (!Inst->hasNoSchedulingInfo)
911 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
912 });
Andrew Trick1ab961f2013-03-16 18:58:55 +0000913 continue;
914 }
915 CodeGenSchedClass &SC = getSchedClass(SCIdx);
916 if (SC.ProcIndices[0] != 0)
Craig Topperd5578c82014-12-09 08:05:51 +0000917 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trick1ab961f2013-03-16 18:58:55 +0000918 "must not be subtarget specific.");
919
920 IdxVec ProcIndices;
921 if (SC.ItinClassDef->getName() != "NoItinerary") {
922 ProcIndices.push_back(0);
923 dbgs() << "Itinerary for " << InstName << ": "
924 << SC.ItinClassDef->getName() << '\n';
925 }
926 if (!SC.Writes.empty()) {
927 ProcIndices.push_back(0);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000928 LLVM_DEBUG({
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000929 dbgs() << "SchedRW machine model for " << InstName;
930 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE;
931 ++WI)
932 dbgs() << " " << SchedWrites[*WI].Name;
933 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
934 dbgs() << " " << SchedReads[*RI].Name;
935 dbgs() << '\n';
936 });
Andrew Trick1ab961f2013-03-16 18:58:55 +0000937 }
938 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar0a82c732017-09-13 10:31:10 +0000939 for (Record *RWDef : RWDefs) {
Andrew Trick1ab961f2013-03-16 18:58:55 +0000940 const CodeGenProcModel &ProcModel =
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000941 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trick1ab961f2013-03-16 18:58:55 +0000942 ProcIndices.push_back(ProcModel.Index);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000943 LLVM_DEBUG(dbgs() << "InstRW on " << ProcModel.ModelName << " for "
944 << InstName);
Andrew Trick48605c32012-09-15 00:19:57 +0000945 IdxVec Writes;
946 IdxVec Reads;
Javed Absar0a82c732017-09-13 10:31:10 +0000947 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trick1ab961f2013-03-16 18:58:55 +0000948 Writes, Reads);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000949 LLVM_DEBUG({
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000950 for (unsigned WIdx : Writes)
951 dbgs() << " " << SchedWrites[WIdx].Name;
952 for (unsigned RIdx : Reads)
953 dbgs() << " " << SchedReads[RIdx].Name;
954 dbgs() << '\n';
955 });
Andrew Trick48605c32012-09-15 00:19:57 +0000956 }
Andrew Trick3c59fd42016-10-18 04:17:44 +0000957 // If ProcIndices contains zero, the class applies to all processors.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000958 LLVM_DEBUG({
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000959 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
960 for (const CodeGenProcModel &PM : ProcModels) {
961 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PM.Index))
962 dbgs() << "No machine model for " << Inst->TheDef->getName()
963 << " on processor " << PM.ModelName << '\n';
964 }
Andrew Trick3c59fd42016-10-18 04:17:44 +0000965 }
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000966 });
Andrew Trick2661b412012-07-07 04:00:00 +0000967 }
Andrew Trick48605c32012-09-15 00:19:57 +0000968}
969
Andrew Trick48605c32012-09-15 00:19:57 +0000970// Get the SchedClass index for an instruction.
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +0000971unsigned
972CodeGenSchedModels::getSchedClassIdx(const CodeGenInstruction &Inst) const {
Andrew Trick1ab961f2013-03-16 18:58:55 +0000973 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick48605c32012-09-15 00:19:57 +0000974}
975
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000976std::string
977CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
978 ArrayRef<unsigned> OperWrites,
979 ArrayRef<unsigned> OperReads) {
Andrew Trick48605c32012-09-15 00:19:57 +0000980
981 std::string Name;
Andrew Trick1ab961f2013-03-16 18:58:55 +0000982 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
983 Name = ItinClassDef->getName();
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000984 for (unsigned Idx : OperWrites) {
Andrew Trick1ab961f2013-03-16 18:58:55 +0000985 if (!Name.empty())
Andrew Trick48605c32012-09-15 00:19:57 +0000986 Name += '_';
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000987 Name += SchedWrites[Idx].Name;
Andrew Trick48605c32012-09-15 00:19:57 +0000988 }
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000989 for (unsigned Idx : OperReads) {
Andrew Trick48605c32012-09-15 00:19:57 +0000990 Name += '_';
Benjamin Kramer109f9e62015-10-24 12:46:49 +0000991 Name += SchedReads[Idx].Name;
Andrew Trick48605c32012-09-15 00:19:57 +0000992 }
993 return Name;
994}
995
996std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
997
998 std::string Name;
999 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
1000 if (I != InstDefs.begin())
1001 Name += '_';
1002 Name += (*I)->getName();
1003 }
1004 return Name;
1005}
1006
Andrew Trick1ab961f2013-03-16 18:58:55 +00001007/// Add an inferred sched class from an itinerary class and per-operand list of
1008/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
1009/// processors that may utilize this class.
1010unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001011 ArrayRef<unsigned> OperWrites,
1012 ArrayRef<unsigned> OperReads,
1013 ArrayRef<unsigned> ProcIndices) {
Andrew Trick48605c32012-09-15 00:19:57 +00001014 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
1015
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001016 auto IsKeyEqual = [=](const CodeGenSchedClass &SC) {
1017 return SC.isKeyEqual(ItinClassDef, OperWrites, OperReads);
1018 };
1019
1020 auto I = find_if(make_range(schedClassBegin(), schedClassEnd()), IsKeyEqual);
1021 unsigned Idx = I == schedClassEnd() ? 0 : std::distance(schedClassBegin(), I);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001022 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick48605c32012-09-15 00:19:57 +00001023 IdxVec PI;
1024 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
1025 SchedClasses[Idx].ProcIndices.end(),
1026 ProcIndices.begin(), ProcIndices.end(),
1027 std::back_inserter(PI));
Craig Topperaf807a62018-03-24 22:58:00 +00001028 SchedClasses[Idx].ProcIndices = std::move(PI);
Andrew Trick48605c32012-09-15 00:19:57 +00001029 return Idx;
1030 }
1031 Idx = SchedClasses.size();
Craig Topperd74de622018-03-22 06:15:08 +00001032 SchedClasses.emplace_back(Idx,
1033 createSchedClassName(ItinClassDef, OperWrites,
1034 OperReads),
1035 ItinClassDef);
Andrew Trick48605c32012-09-15 00:19:57 +00001036 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trick48605c32012-09-15 00:19:57 +00001037 SC.Writes = OperWrites;
1038 SC.Reads = OperReads;
1039 SC.ProcIndices = ProcIndices;
1040
1041 return Idx;
1042}
1043
1044// Create classes for each set of opcodes that are in the same InstReadWrite
1045// definition across all processors.
1046void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
1047 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
1048 // intersects with an existing class via a previous InstRWDef. Instrs that do
1049 // not intersect with an existing class refer back to their former class as
1050 // determined from ItinDef or SchedRW.
Craig Topperb6f73f82018-03-21 02:48:34 +00001051 SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
Andrew Trick48605c32012-09-15 00:19:57 +00001052 // Sort Instrs into sets.
Andrew Trick13745262012-10-03 23:06:32 +00001053 const RecVec *InstDefs = Sets.expand(InstRWDef);
1054 if (InstDefs->empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001055 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick13745262012-10-03 23:06:32 +00001056
Craig Topper6ef67012018-03-18 08:38:03 +00001057 for (Record *InstDef : *InstDefs) {
Javed Absar3c69ee52017-10-05 13:27:43 +00001058 InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001059 if (Pos == InstrClassMap.end())
Javed Absar3c69ee52017-10-05 13:27:43 +00001060 PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
Andrew Trick1ab961f2013-03-16 18:58:55 +00001061 unsigned SCIdx = Pos->second;
Craig Topperb6f73f82018-03-21 02:48:34 +00001062 ClassInstrs[SCIdx].push_back(InstDef);
Andrew Trick48605c32012-09-15 00:19:57 +00001063 }
1064 // For each set of Instrs, create a new class if necessary, and map or remap
1065 // the Instrs to it.
Craig Topperb6f73f82018-03-21 02:48:34 +00001066 for (auto &Entry : ClassInstrs) {
1067 unsigned OldSCIdx = Entry.first;
1068 ArrayRef<Record*> InstDefs = Entry.second;
Andrew Trick48605c32012-09-15 00:19:57 +00001069 // If the all instrs in the current class are accounted for, then leave
1070 // them mapped to their old class.
Andrew Trickd7aad342013-06-05 06:55:20 +00001071 if (OldSCIdx) {
1072 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
1073 if (!RWDefs.empty()) {
1074 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
Craig Topper788fc6f2018-03-21 19:30:30 +00001075 unsigned OrigNumInstrs =
1076 count_if(*OrigInstDefs, [&](Record *OIDef) {
1077 return InstrClassMap[OIDef] == OldSCIdx;
1078 });
Andrew Trickd7aad342013-06-05 06:55:20 +00001079 if (OrigNumInstrs == InstDefs.size()) {
1080 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
1081 "expected a generic SchedClass");
Craig Topper27bdb272018-03-18 19:56:15 +00001082 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1083 // Make sure we didn't already have a InstRW containing this
1084 // instruction on this model.
1085 for (Record *RWD : RWDefs) {
1086 if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
1087 RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
1088 for (Record *Inst : InstDefs) {
1089 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
1090 Inst->getName() + " also matches " +
1091 RWD->getValue("Instrs")->getValue()->getAsString());
1092 }
1093 }
1094 }
Nicola Zaghen0818e782018-05-14 12:53:11 +00001095 LLVM_DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
1096 << SchedClasses[OldSCIdx].Name << " on "
1097 << RWModelDef->getName() << "\n");
Andrew Trickd7aad342013-06-05 06:55:20 +00001098 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
1099 continue;
1100 }
1101 }
Andrew Trick48605c32012-09-15 00:19:57 +00001102 }
1103 unsigned SCIdx = SchedClasses.size();
Craig Topperd74de622018-03-22 06:15:08 +00001104 SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
Andrew Trick48605c32012-09-15 00:19:57 +00001105 CodeGenSchedClass &SC = SchedClasses.back();
Nicola Zaghen0818e782018-05-14 12:53:11 +00001106 LLVM_DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
1107 << InstRWDef->getValueAsDef("SchedModel")->getName()
1108 << "\n");
Andrew Trickd7aad342013-06-05 06:55:20 +00001109
Andrew Trick48605c32012-09-15 00:19:57 +00001110 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
1111 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
1112 SC.Writes = SchedClasses[OldSCIdx].Writes;
1113 SC.Reads = SchedClasses[OldSCIdx].Reads;
1114 SC.ProcIndices.push_back(0);
Craig Topper67906382018-03-21 19:52:13 +00001115 // If we had an old class, copy it's InstRWs to this new class.
1116 if (OldSCIdx) {
1117 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1118 for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
1119 if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
1120 for (Record *InstDef : InstDefs) {
Craig Toppere2cd7232018-03-21 19:30:31 +00001121 PrintFatalError(OldRWDef->getLoc(), "Overlapping InstRW def " +
1122 InstDef->getName() + " also matches " +
1123 OldRWDef->getValue("Instrs")->getValue()->getAsString());
Andrew Trick13745262012-10-03 23:06:32 +00001124 }
Andrew Trick13745262012-10-03 23:06:32 +00001125 }
Craig Topper67906382018-03-21 19:52:13 +00001126 assert(OldRWDef != InstRWDef &&
1127 "SchedClass has duplicate InstRW def");
1128 SC.InstRWs.push_back(OldRWDef);
Andrew Trick48605c32012-09-15 00:19:57 +00001129 }
Andrew Trick48605c32012-09-15 00:19:57 +00001130 }
Craig Topper67906382018-03-21 19:52:13 +00001131 // Map each Instr to this new class.
1132 for (Record *InstDef : InstDefs)
1133 InstrClassMap[InstDef] = SCIdx;
Andrew Trick48605c32012-09-15 00:19:57 +00001134 SC.InstRWs.push_back(InstRWDef);
1135 }
Andrew Trick2661b412012-07-07 04:00:00 +00001136}
1137
Andrew Trick1ab961f2013-03-16 18:58:55 +00001138// True if collectProcItins found anything.
1139bool CodeGenSchedModels::hasItineraries() const {
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001140 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd()))
Javed Absar0a82c732017-09-13 10:31:10 +00001141 if (PM.hasItineraries())
Andrew Trick1ab961f2013-03-16 18:58:55 +00001142 return true;
Andrew Trick1ab961f2013-03-16 18:58:55 +00001143 return false;
1144}
1145
Andrew Trick2661b412012-07-07 04:00:00 +00001146// Gather the processor itineraries.
Andrew Trick48605c32012-09-15 00:19:57 +00001147void CodeGenSchedModels::collectProcItins() {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001148 LLVM_DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topperd5578c82014-12-09 08:05:51 +00001149 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trick1ab961f2013-03-16 18:58:55 +00001150 if (!ProcModel.hasItineraries())
Andrew Trick2661b412012-07-07 04:00:00 +00001151 continue;
Andrew Trick48605c32012-09-15 00:19:57 +00001152
Andrew Trick1ab961f2013-03-16 18:58:55 +00001153 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
1154 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
1155
1156 // Populate ItinDefList with Itinerary records.
1157 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick48605c32012-09-15 00:19:57 +00001158
1159 // Insert each itinerary data record in the correct position within
1160 // the processor model's ItinDefList.
Javed Absar3c69ee52017-10-05 13:27:43 +00001161 for (Record *ItinData : ItinRecords) {
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001162 const Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Trick02fec342013-03-18 20:42:25 +00001163 bool FoundClass = false;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001164
1165 for (const CodeGenSchedClass &SC :
1166 make_range(schedClassBegin(), schedClassEnd())) {
Andrew Trick02fec342013-03-18 20:42:25 +00001167 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001168 if (SC.ItinClassDef == ItinDef) {
1169 ProcModel.ItinDefList[SC.Index] = ItinData;
Andrew Trick02fec342013-03-18 20:42:25 +00001170 FoundClass = true;
Andrew Trick1ab961f2013-03-16 18:58:55 +00001171 }
Andrew Trick48605c32012-09-15 00:19:57 +00001172 }
Andrew Trick02fec342013-03-18 20:42:25 +00001173 if (!FoundClass) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001174 LLVM_DEBUG(dbgs() << ProcModel.ItinsDef->getName()
1175 << " missing class for itinerary "
1176 << ItinDef->getName() << '\n');
Andrew Trick1ab961f2013-03-16 18:58:55 +00001177 }
Andrew Trick2661b412012-07-07 04:00:00 +00001178 }
Andrew Trick48605c32012-09-15 00:19:57 +00001179 // Check for missing itinerary entries.
1180 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
Nicola Zaghen0818e782018-05-14 12:53:11 +00001181 LLVM_DEBUG(
1182 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
1183 if (!ProcModel.ItinDefList[i])
1184 dbgs() << ProcModel.ItinsDef->getName()
1185 << " missing itinerary for class " << SchedClasses[i].Name
1186 << '\n';
1187 });
Andrew Trick2661b412012-07-07 04:00:00 +00001188 }
Andrew Trick2661b412012-07-07 04:00:00 +00001189}
Andrew Trick48605c32012-09-15 00:19:57 +00001190
1191// Gather the read/write types for each itinerary class.
1192void CodeGenSchedModels::collectProcItinRW() {
1193 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Fangrui Song3b35e172018-09-27 02:13:45 +00001194 llvm::sort(ItinRWDefs, LessRecord());
Javed Absarc4533062017-10-09 16:21:25 +00001195 for (Record *RWDef : ItinRWDefs) {
Javed Absar52122df2017-10-08 17:23:30 +00001196 if (!RWDef->getValueInit("SchedModel")->isComplete())
1197 PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
1198 Record *ModelDef = RWDef->getValueAsDef("SchedModel");
Andrew Trick48605c32012-09-15 00:19:57 +00001199 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
1200 if (I == ProcModelMap.end()) {
Javed Absar52122df2017-10-08 17:23:30 +00001201 PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
Andrew Trick48605c32012-09-15 00:19:57 +00001202 + ModelDef->getName());
1203 }
Javed Absar52122df2017-10-08 17:23:30 +00001204 ProcModels[I->second].ItinRWDefs.push_back(RWDef);
Andrew Trick48605c32012-09-15 00:19:57 +00001205 }
1206}
1207
Simon Dardis111ec252016-06-24 08:43:27 +00001208// Gather the unsupported features for processor models.
1209void CodeGenSchedModels::collectProcUnsupportedFeatures() {
1210 for (CodeGenProcModel &ProcModel : ProcModels) {
1211 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
1212 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
1213 }
1214 }
1215}
1216
Andrew Trick5e613c22012-09-15 00:19:59 +00001217/// Infer new classes from existing classes. In the process, this may create new
1218/// SchedWrites from sequences of existing SchedWrites.
1219void CodeGenSchedModels::inferSchedClasses() {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001220 LLVM_DEBUG(
1221 dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
1222 LLVM_DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
Andrew Trick1ab961f2013-03-16 18:58:55 +00001223
Andrew Trick5e613c22012-09-15 00:19:59 +00001224 // Visit all existing classes and newly created classes.
1225 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trick1ab961f2013-03-16 18:58:55 +00001226 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
1227
Andrew Trick5e613c22012-09-15 00:19:59 +00001228 if (SchedClasses[Idx].ItinClassDef)
1229 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001230 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick5e613c22012-09-15 00:19:59 +00001231 inferFromInstRWs(Idx);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001232 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick5e613c22012-09-15 00:19:59 +00001233 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
1234 Idx, SchedClasses[Idx].ProcIndices);
1235 }
1236 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
1237 "too many SchedVariants");
1238 }
1239}
1240
1241/// Infer classes from per-processor itinerary resources.
1242void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
1243 unsigned FromClassIdx) {
1244 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1245 const CodeGenProcModel &PM = ProcModels[PIdx];
1246 // For all ItinRW entries.
1247 bool HasMatch = false;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001248 for (const Record *Rec : PM.ItinRWDefs) {
1249 RecVec Matched = Rec->getValueAsListOfDefs("MatchedItinClasses");
Andrew Trick5e613c22012-09-15 00:19:59 +00001250 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1251 continue;
1252 if (HasMatch)
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001253 PrintFatalError(Rec->getLoc(), "Duplicate itinerary class "
Andrew Trick5e613c22012-09-15 00:19:59 +00001254 + ItinClassDef->getName()
1255 + " in ItinResources for " + PM.ModelName);
1256 HasMatch = true;
1257 IdxVec Writes, Reads;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001258 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper92016652018-03-24 21:57:35 +00001259 inferFromRW(Writes, Reads, FromClassIdx, PIdx);
Andrew Trick5e613c22012-09-15 00:19:59 +00001260 }
1261 }
1262}
1263
1264/// Infer classes from per-processor InstReadWrite definitions.
1265void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer068ecc12013-06-09 15:20:23 +00001266 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Krameraca93cf2013-06-10 20:19:35 +00001267 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer068ecc12013-06-09 15:20:23 +00001268 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
1269 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick13745262012-10-03 23:06:32 +00001270 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick5e613c22012-09-15 00:19:59 +00001271 for (; II != IE; ++II) {
1272 if (InstrClassMap[*II] == SCIdx)
1273 break;
1274 }
1275 // If this class no longer has any instructions mapped to it, it has become
1276 // irrelevant.
1277 if (II == IE)
1278 continue;
1279 IdxVec Writes, Reads;
Benjamin Kramer068ecc12013-06-09 15:20:23 +00001280 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1281 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Craig Topper92016652018-03-24 21:57:35 +00001282 inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
Andrew Trick5e613c22012-09-15 00:19:59 +00001283 }
1284}
1285
1286namespace {
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00001287
Andrew Trick92649882012-09-22 02:24:21 +00001288// Helper for substituteVariantOperand.
1289struct TransVariant {
Andrew Trick2062b122012-10-03 23:06:28 +00001290 Record *VarOrSeqDef; // Variant or sequence.
1291 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick92649882012-09-22 02:24:21 +00001292 unsigned ProcIdx; // Processor model index or zero for any.
1293 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
1294
1295 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trick2062b122012-10-03 23:06:28 +00001296 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick92649882012-09-22 02:24:21 +00001297};
1298
Andrew Trick5e613c22012-09-15 00:19:59 +00001299// Associate a predicate with the SchedReadWrite that it guards.
1300// RWIdx is the index of the read/write variant.
1301struct PredCheck {
1302 bool IsRead;
1303 unsigned RWIdx;
1304 Record *Predicate;
1305
1306 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
1307};
1308
1309// A Predicate transition is a list of RW sequences guarded by a PredTerm.
1310struct PredTransition {
1311 // A predicate term is a conjunction of PredChecks.
1312 SmallVector<PredCheck, 4> PredTerm;
1313 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
1314 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick92649882012-09-22 02:24:21 +00001315 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick5e613c22012-09-15 00:19:59 +00001316};
1317
1318// Encapsulate a set of partially constructed transitions.
1319// The results are built by repeated calls to substituteVariants.
1320class PredTransitions {
1321 CodeGenSchedModels &SchedModels;
1322
1323public:
1324 std::vector<PredTransition> TransVec;
1325
1326 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
1327
1328 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
1329 bool IsRead, unsigned StartIdx);
1330
1331 void substituteVariants(const PredTransition &Trans);
1332
1333#ifndef NDEBUG
1334 void dump() const;
1335#endif
1336
1337private:
1338 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trick2062b122012-10-03 23:06:28 +00001339 void getIntersectingVariants(
1340 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1341 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick92649882012-09-22 02:24:21 +00001342 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick5e613c22012-09-15 00:19:59 +00001343};
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00001344
1345} // end anonymous namespace
Andrew Trick5e613c22012-09-15 00:19:59 +00001346
1347// Return true if this predicate is mutually exclusive with a PredTerm. This
1348// degenerates into checking if the predicate is mutually exclusive with any
1349// predicate in the Term's conjunction.
1350//
1351// All predicates associated with a given SchedRW are considered mutually
1352// exclusive. This should work even if the conditions expressed by the
1353// predicates are not exclusive because the predicates for a given SchedWrite
1354// are always checked in the order they are defined in the .td file. Later
1355// conditions implicitly negate any prior condition.
1356bool PredTransitions::mutuallyExclusive(Record *PredDef,
1357 ArrayRef<PredCheck> Term) {
Javed Absarc4533062017-10-09 16:21:25 +00001358 for (const PredCheck &PC: Term) {
Javed Absar3c69ee52017-10-05 13:27:43 +00001359 if (PC.Predicate == PredDef)
Andrew Trick5e613c22012-09-15 00:19:59 +00001360 return false;
1361
Javed Absar3c69ee52017-10-05 13:27:43 +00001362 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
Andrew Trick5e613c22012-09-15 00:19:59 +00001363 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1364 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001365 if (any_of(Variants, [PredDef](const Record *R) {
1366 return R->getValueAsDef("Predicate") == PredDef;
1367 }))
1368 return true;
Andrew Trick5e613c22012-09-15 00:19:59 +00001369 }
1370 return false;
1371}
1372
Andrew Trick2062b122012-10-03 23:06:28 +00001373static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1374 CodeGenSchedModels &SchedModels) {
1375 if (RW.HasVariants)
1376 return true;
1377
Javed Absarc4533062017-10-09 16:21:25 +00001378 for (Record *Alias : RW.Aliases) {
Andrew Trick2062b122012-10-03 23:06:28 +00001379 const CodeGenSchedRW &AliasRW =
Javed Absar3c69ee52017-10-05 13:27:43 +00001380 SchedModels.getSchedRW(Alias->getValueAsDef("AliasRW"));
Andrew Trick2062b122012-10-03 23:06:28 +00001381 if (AliasRW.HasVariants)
1382 return true;
1383 if (AliasRW.IsSequence) {
1384 IdxVec ExpandedRWs;
1385 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001386 for (unsigned SI : ExpandedRWs) {
1387 if (hasAliasedVariants(SchedModels.getSchedRW(SI, AliasRW.IsRead),
1388 SchedModels))
Andrew Trick2062b122012-10-03 23:06:28 +00001389 return true;
Andrew Trick2062b122012-10-03 23:06:28 +00001390 }
1391 }
1392 }
1393 return false;
1394}
1395
1396static bool hasVariant(ArrayRef<PredTransition> Transitions,
1397 CodeGenSchedModels &SchedModels) {
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001398 for (const PredTransition &PTI : Transitions) {
1399 for (const SmallVectorImpl<unsigned> &WSI : PTI.WriteSequences)
1400 for (unsigned WI : WSI)
1401 if (hasAliasedVariants(SchedModels.getSchedWrite(WI), SchedModels))
Andrew Trick2062b122012-10-03 23:06:28 +00001402 return true;
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001403
1404 for (const SmallVectorImpl<unsigned> &RSI : PTI.ReadSequences)
1405 for (unsigned RI : RSI)
1406 if (hasAliasedVariants(SchedModels.getSchedRead(RI), SchedModels))
Andrew Trick2062b122012-10-03 23:06:28 +00001407 return true;
Andrew Trick2062b122012-10-03 23:06:28 +00001408 }
1409 return false;
1410}
1411
1412// Populate IntersectingVariants with any variants or aliased sequences of the
1413// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickbc4de7c2013-03-29 19:08:31 +00001414// exclusive with the given transition.
Andrew Trick2062b122012-10-03 23:06:28 +00001415void PredTransitions::getIntersectingVariants(
1416 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1417 std::vector<TransVariant> &IntersectingVariants) {
1418
Andrew Trickbc4de7c2013-03-29 19:08:31 +00001419 bool GenericRW = false;
1420
Andrew Trick2062b122012-10-03 23:06:28 +00001421 std::vector<TransVariant> Variants;
1422 if (SchedRW.HasVariants) {
1423 unsigned VarProcIdx = 0;
1424 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1425 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1426 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1427 }
1428 // Push each variant. Assign TransVecIdx later.
1429 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar52122df2017-10-08 17:23:30 +00001430 for (Record *VarDef : VarDefs)
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001431 Variants.emplace_back(VarDef, SchedRW.Index, VarProcIdx, 0);
Andrew Trickbc4de7c2013-03-29 19:08:31 +00001432 if (VarProcIdx == 0)
1433 GenericRW = true;
Andrew Trick2062b122012-10-03 23:06:28 +00001434 }
1435 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1436 AI != AE; ++AI) {
1437 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1438 // to is defined within a processor model, constrain all variants to
1439 // that processor.
1440 unsigned AliasProcIdx = 0;
1441 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1442 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1443 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1444 }
1445 const CodeGenSchedRW &AliasRW =
1446 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1447
1448 if (AliasRW.HasVariants) {
1449 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
Javed Absar1eab66d2017-10-10 15:58:45 +00001450 for (Record *VD : VarDefs)
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001451 Variants.emplace_back(VD, AliasRW.Index, AliasProcIdx, 0);
Andrew Trick2062b122012-10-03 23:06:28 +00001452 }
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001453 if (AliasRW.IsSequence)
1454 Variants.emplace_back(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0);
Andrew Trickbc4de7c2013-03-29 19:08:31 +00001455 if (AliasProcIdx == 0)
1456 GenericRW = true;
Andrew Trick2062b122012-10-03 23:06:28 +00001457 }
Javed Absar52122df2017-10-08 17:23:30 +00001458 for (TransVariant &Variant : Variants) {
Andrew Trick2062b122012-10-03 23:06:28 +00001459 // Don't expand variants if the processor models don't intersect.
1460 // A zero processor index means any processor.
Craig Toppera0ec3f92013-07-14 04:42:23 +00001461 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Javed Absar52122df2017-10-08 17:23:30 +00001462 if (ProcIndices[0] && Variant.ProcIdx) {
Andrew Trick2062b122012-10-03 23:06:28 +00001463 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1464 Variant.ProcIdx);
1465 if (!Cnt)
1466 continue;
1467 if (Cnt > 1) {
1468 const CodeGenProcModel &PM =
1469 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001470 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1471 "Multiple variants defined for processor " +
1472 PM.ModelName +
1473 " Ensure only one SchedAlias exists per RW.");
Andrew Trick2062b122012-10-03 23:06:28 +00001474 }
1475 }
1476 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1477 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1478 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1479 continue;
1480 }
1481 if (IntersectingVariants.empty()) {
1482 // The first variant builds on the existing transition.
1483 Variant.TransVecIdx = TransIdx;
1484 IntersectingVariants.push_back(Variant);
1485 }
1486 else {
1487 // Push another copy of the current transition for more variants.
1488 Variant.TransVecIdx = TransVec.size();
1489 IntersectingVariants.push_back(Variant);
Dan Gohman1cbd4012013-03-29 00:13:08 +00001490 TransVec.push_back(TransVec[TransIdx]);
Andrew Trick2062b122012-10-03 23:06:28 +00001491 }
1492 }
Andrew Trickbc4de7c2013-03-29 19:08:31 +00001493 if (GenericRW && IntersectingVariants.empty()) {
1494 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1495 "a matching predicate on any processor");
1496 }
Andrew Trick2062b122012-10-03 23:06:28 +00001497}
1498
Andrew Trick92649882012-09-22 02:24:21 +00001499// Push the Reads/Writes selected by this variant onto the PredTransition
1500// specified by VInfo.
1501void PredTransitions::
1502pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick92649882012-09-22 02:24:21 +00001503 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1504
Andrew Trick92649882012-09-22 02:24:21 +00001505 // If this operand transition is reached through a processor-specific alias,
1506 // then the whole transition is specific to this processor.
1507 if (VInfo.ProcIdx != 0)
1508 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1509
Andrew Trick5e613c22012-09-15 00:19:59 +00001510 IdxVec SelectedRWs;
Andrew Trick2062b122012-10-03 23:06:28 +00001511 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1512 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001513 Trans.PredTerm.emplace_back(IsRead, VInfo.RWIdx,PredDef);
Andrew Trick2062b122012-10-03 23:06:28 +00001514 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1515 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1516 }
1517 else {
1518 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1519 "variant must be a SchedVariant or aliased WriteSequence");
1520 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1521 }
Andrew Trick5e613c22012-09-15 00:19:59 +00001522
Andrew Trick92649882012-09-22 02:24:21 +00001523 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick5e613c22012-09-15 00:19:59 +00001524
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00001525 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick5e613c22012-09-15 00:19:59 +00001526 ? Trans.ReadSequences : Trans.WriteSequences;
1527 if (SchedRW.IsVariadic) {
1528 unsigned OperIdx = RWSequences.size()-1;
1529 // Make N-1 copies of this transition's last sequence.
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001530 RWSequences.insert(RWSequences.end(), SelectedRWs.size() - 1,
1531 RWSequences[OperIdx]);
Andrew Trick5e613c22012-09-15 00:19:59 +00001532 // Push each of the N elements of the SelectedRWs onto a copy of the last
1533 // sequence (split the current operand into N operands).
1534 // Note that write sequences should be expanded within this loop--the entire
1535 // sequence belongs to a single operand.
1536 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1537 RWI != RWE; ++RWI, ++OperIdx) {
1538 IdxVec ExpandedRWs;
1539 if (IsRead)
1540 ExpandedRWs.push_back(*RWI);
1541 else
1542 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1543 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1544 ExpandedRWs.begin(), ExpandedRWs.end());
1545 }
1546 assert(OperIdx == RWSequences.size() && "missed a sequence");
1547 }
1548 else {
1549 // Push this transition's expanded sequence onto this transition's last
1550 // sequence (add to the current operand's sequence).
1551 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1552 IdxVec ExpandedRWs;
1553 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1554 RWI != RWE; ++RWI) {
1555 if (IsRead)
1556 ExpandedRWs.push_back(*RWI);
1557 else
1558 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1559 }
1560 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1561 }
1562}
1563
1564// RWSeq is a sequence of all Reads or all Writes for the next read or write
1565// operand. StartIdx is an index into TransVec where partial results
Andrew Trick92649882012-09-22 02:24:21 +00001566// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick5e613c22012-09-15 00:19:59 +00001567// of TransVec.
1568void PredTransitions::substituteVariantOperand(
1569 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1570
1571 // Visit each original RW within the current sequence.
1572 for (SmallVectorImpl<unsigned>::const_iterator
1573 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1574 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1575 // Push this RW on all partial PredTransitions or distribute variants.
1576 // New PredTransitions may be pushed within this loop which should not be
1577 // revisited (TransEnd must be loop invariant).
1578 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1579 TransIdx != TransEnd; ++TransIdx) {
1580 // In the common case, push RW onto the current operand's sequence.
Andrew Trick92649882012-09-22 02:24:21 +00001581 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick5e613c22012-09-15 00:19:59 +00001582 if (IsRead)
1583 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1584 else
1585 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1586 continue;
1587 }
1588 // Distribute this partial PredTransition across intersecting variants.
Andrew Trick2062b122012-10-03 23:06:28 +00001589 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick92649882012-09-22 02:24:21 +00001590 std::vector<TransVariant> IntersectingVariants;
Andrew Trick2062b122012-10-03 23:06:28 +00001591 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick5e613c22012-09-15 00:19:59 +00001592 // Now expand each variant on top of its copy of the transition.
Andrew Trick92649882012-09-22 02:24:21 +00001593 for (std::vector<TransVariant>::const_iterator
Andrew Trick5e613c22012-09-15 00:19:59 +00001594 IVI = IntersectingVariants.begin(),
1595 IVE = IntersectingVariants.end();
Andrew Trick92649882012-09-22 02:24:21 +00001596 IVI != IVE; ++IVI) {
1597 pushVariant(*IVI, IsRead);
1598 }
Andrew Trick5e613c22012-09-15 00:19:59 +00001599 }
1600 }
1601}
1602
1603// For each variant of a Read/Write in Trans, substitute the sequence of
1604// Read/Writes guarded by the variant. This is exponential in the number of
1605// variant Read/Writes, but in practice detection of mutually exclusive
1606// predicates should result in linear growth in the total number variants.
1607//
1608// This is one step in a breadth-first search of nested variants.
1609void PredTransitions::substituteVariants(const PredTransition &Trans) {
1610 // Build up a set of partial results starting at the back of
1611 // PredTransitions. Remember the first new transition.
1612 unsigned StartIdx = TransVec.size();
Craig Topperb0f8e692018-03-22 06:15:10 +00001613 TransVec.emplace_back();
Andrew Trick5e613c22012-09-15 00:19:59 +00001614 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick92649882012-09-22 02:24:21 +00001615 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick5e613c22012-09-15 00:19:59 +00001616
1617 // Visit each original write sequence.
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00001618 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick5e613c22012-09-15 00:19:59 +00001619 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1620 WSI != WSE; ++WSI) {
1621 // Push a new (empty) write sequence onto all partial Transitions.
1622 for (std::vector<PredTransition>::iterator I =
1623 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topperb0f8e692018-03-22 06:15:10 +00001624 I->WriteSequences.emplace_back();
Andrew Trick5e613c22012-09-15 00:19:59 +00001625 }
1626 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1627 }
1628 // Visit each original read sequence.
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00001629 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick5e613c22012-09-15 00:19:59 +00001630 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1631 RSI != RSE; ++RSI) {
1632 // Push a new (empty) read sequence onto all partial Transitions.
1633 for (std::vector<PredTransition>::iterator I =
1634 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
Craig Topperb0f8e692018-03-22 06:15:10 +00001635 I->ReadSequences.emplace_back();
Andrew Trick5e613c22012-09-15 00:19:59 +00001636 }
1637 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1638 }
1639}
1640
Andrew Trick5e613c22012-09-15 00:19:59 +00001641// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick5e613c22012-09-15 00:19:59 +00001642static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick92649882012-09-22 02:24:21 +00001643 unsigned FromClassIdx,
Andrew Trick5e613c22012-09-15 00:19:59 +00001644 CodeGenSchedModels &SchedModels) {
1645 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1646 // requires creating a new SchedClass.
1647 for (ArrayRef<PredTransition>::iterator
1648 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1649 IdxVec OperWritesVariant;
Craig Topper24e5cd22018-03-20 20:24:12 +00001650 transform(I->WriteSequences, std::back_inserter(OperWritesVariant),
1651 [&SchedModels](ArrayRef<unsigned> WS) {
1652 return SchedModels.findOrInsertRW(WS, /*IsRead=*/false);
1653 });
Andrew Trick5e613c22012-09-15 00:19:59 +00001654 IdxVec OperReadsVariant;
Craig Topper24e5cd22018-03-20 20:24:12 +00001655 transform(I->ReadSequences, std::back_inserter(OperReadsVariant),
1656 [&SchedModels](ArrayRef<unsigned> RS) {
1657 return SchedModels.findOrInsertRW(RS, /*IsRead=*/true);
1658 });
Andrew Trick5e613c22012-09-15 00:19:59 +00001659 CodeGenSchedTransition SCTrans;
1660 SCTrans.ToClassIdx =
Craig Topper095734c2014-04-15 07:20:03 +00001661 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Craig Topper419fa242018-03-24 22:58:03 +00001662 OperReadsVariant, I->ProcIndices);
1663 SCTrans.ProcIndices.assign(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick5e613c22012-09-15 00:19:59 +00001664 // The final PredTerm is unique set of predicates guarding the transition.
1665 RecVec Preds;
Craig Topper24e5cd22018-03-20 20:24:12 +00001666 transform(I->PredTerm, std::back_inserter(Preds),
1667 [](const PredCheck &P) {
1668 return P.Predicate;
1669 });
Craig Topper17ab24c2018-03-20 20:24:10 +00001670 Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
Craig Topper284e1ff2018-03-24 22:58:02 +00001671 SCTrans.PredTerm = std::move(Preds);
1672 SchedModels.getSchedClass(FromClassIdx)
1673 .Transitions.push_back(std::move(SCTrans));
Andrew Trick5e613c22012-09-15 00:19:59 +00001674 }
1675}
1676
Andrew Trick92649882012-09-22 02:24:21 +00001677// Create new SchedClasses for the given ReadWrite list. If any of the
1678// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1679// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001680void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1681 ArrayRef<unsigned> OperReads,
Andrew Trick5e613c22012-09-15 00:19:59 +00001682 unsigned FromClassIdx,
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001683 ArrayRef<unsigned> ProcIndices) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001684 LLVM_DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices);
1685 dbgs() << ") ");
Andrew Trick5e613c22012-09-15 00:19:59 +00001686
1687 // Create a seed transition with an empty PredTerm and the expanded sequences
1688 // of SchedWrites for the current SchedClass.
1689 std::vector<PredTransition> LastTransitions;
Craig Topperb0f8e692018-03-22 06:15:10 +00001690 LastTransitions.emplace_back();
Andrew Trick92649882012-09-22 02:24:21 +00001691 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1692 ProcIndices.end());
1693
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001694 for (unsigned WriteIdx : OperWrites) {
Andrew Trick5e613c22012-09-15 00:19:59 +00001695 IdxVec WriteSeq;
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001696 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Craig Topperb0f8e692018-03-22 06:15:10 +00001697 LastTransitions[0].WriteSequences.emplace_back();
1698 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
Craig Topper36fc59c2018-03-20 20:24:14 +00001699 Seq.append(WriteSeq.begin(), WriteSeq.end());
Nicola Zaghen0818e782018-05-14 12:53:11 +00001700 LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
Andrew Trick5e613c22012-09-15 00:19:59 +00001701 }
Nicola Zaghen0818e782018-05-14 12:53:11 +00001702 LLVM_DEBUG(dbgs() << " Reads: ");
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001703 for (unsigned ReadIdx : OperReads) {
Andrew Trick5e613c22012-09-15 00:19:59 +00001704 IdxVec ReadSeq;
Benjamin Kramer109f9e62015-10-24 12:46:49 +00001705 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Craig Topperb0f8e692018-03-22 06:15:10 +00001706 LastTransitions[0].ReadSequences.emplace_back();
1707 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
Craig Topper36fc59c2018-03-20 20:24:14 +00001708 Seq.append(ReadSeq.begin(), ReadSeq.end());
Nicola Zaghen0818e782018-05-14 12:53:11 +00001709 LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
Andrew Trick5e613c22012-09-15 00:19:59 +00001710 }
Nicola Zaghen0818e782018-05-14 12:53:11 +00001711 LLVM_DEBUG(dbgs() << '\n');
Andrew Trick5e613c22012-09-15 00:19:59 +00001712
1713 // Collect all PredTransitions for individual operands.
1714 // Iterate until no variant writes remain.
1715 while (hasVariant(LastTransitions, *this)) {
1716 PredTransitions Transitions(*this);
Craig Topper0cd071c2018-03-20 20:24:16 +00001717 for (const PredTransition &Trans : LastTransitions)
1718 Transitions.substituteVariants(Trans);
Nicola Zaghen0818e782018-05-14 12:53:11 +00001719 LLVM_DEBUG(Transitions.dump());
Andrew Trick5e613c22012-09-15 00:19:59 +00001720 LastTransitions.swap(Transitions.TransVec);
1721 }
1722 // If the first transition has no variants, nothing to do.
1723 if (LastTransitions[0].PredTerm.empty())
1724 return;
1725
1726 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1727 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick92649882012-09-22 02:24:21 +00001728 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick5e613c22012-09-15 00:19:59 +00001729}
1730
Andrew Tricke30f32a2013-04-23 23:45:14 +00001731// Check if any processor resource group contains all resource records in
1732// SubUnits.
1733bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1734 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1735 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1736 continue;
1737 RecVec SuperUnits =
1738 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1739 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1740 for ( ; RI != RE; ++RI) {
David Majnemer975248e2016-08-11 22:21:41 +00001741 if (!is_contained(SuperUnits, *RI)) {
Andrew Tricke30f32a2013-04-23 23:45:14 +00001742 break;
1743 }
1744 }
1745 if (RI == RE)
1746 return true;
1747 }
1748 return false;
1749}
1750
1751// Verify that overlapping groups have a common supergroup.
1752void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1753 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1754 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1755 continue;
1756 RecVec CheckUnits =
1757 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1758 for (unsigned j = i+1; j < e; ++j) {
1759 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1760 continue;
1761 RecVec OtherUnits =
1762 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1763 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1764 OtherUnits.begin(), OtherUnits.end())
1765 != CheckUnits.end()) {
1766 // CheckUnits and OtherUnits overlap
1767 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1768 CheckUnits.end());
1769 if (!hasSuperGroup(OtherUnits, PM)) {
1770 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1771 "proc resource group overlaps with "
1772 + PM.ProcResourceDefs[j]->getName()
1773 + " but no supergroup contains both.");
1774 }
1775 }
1776 }
1777 }
1778}
1779
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001780// Collect all the RegisterFile definitions available in this target.
1781void CodeGenSchedModels::collectRegisterFiles() {
1782 RecVec RegisterFileDefs = Records.getAllDerivedDefinitions("RegisterFile");
1783
1784 // RegisterFiles is the vector of CodeGenRegisterFile.
1785 for (Record *RF : RegisterFileDefs) {
1786 // For each register file definition, construct a CodeGenRegisterFile object
1787 // and add it to the appropriate scheduling model.
1788 CodeGenProcModel &PM = getProcModel(RF->getValueAsDef("SchedModel"));
1789 PM.RegisterFiles.emplace_back(CodeGenRegisterFile(RF->getName(),RF));
1790 CodeGenRegisterFile &CGRF = PM.RegisterFiles.back();
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +00001791 CGRF.MaxMovesEliminatedPerCycle =
1792 RF->getValueAsInt("MaxMovesEliminatedPerCycle");
1793 CGRF.AllowZeroMoveEliminationOnly =
1794 RF->getValueAsBit("AllowZeroMoveEliminationOnly");
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001795
1796 // Now set the number of physical registers as well as the cost of registers
1797 // in each register class.
1798 CGRF.NumPhysRegs = RF->getValueAsInt("NumPhysRegs");
Andrea Di Biagio7a175722018-10-11 10:39:03 +00001799 if (!CGRF.NumPhysRegs) {
1800 PrintFatalError(RF->getLoc(),
1801 "Invalid RegisterFile with zero physical registers");
1802 }
1803
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001804 RecVec RegisterClasses = RF->getValueAsListOfDefs("RegClasses");
1805 std::vector<int64_t> RegisterCosts = RF->getValueAsListOfInts("RegCosts");
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +00001806 ListInit *MoveElimInfo = RF->getValueAsListInit("AllowMoveElimination");
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001807 for (unsigned I = 0, E = RegisterClasses.size(); I < E; ++I) {
1808 int Cost = RegisterCosts.size() > I ? RegisterCosts[I] : 1;
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +00001809
1810 bool AllowMoveElim = false;
1811 if (MoveElimInfo->size() > I) {
1812 BitInit *Val = cast<BitInit>(MoveElimInfo->getElement(I));
1813 AllowMoveElim = Val->getValue();
1814 }
1815
1816 CGRF.Costs.emplace_back(RegisterClasses[I], Cost, AllowMoveElim);
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001817 }
1818 }
1819}
1820
Andrew Trick3cbd1782012-09-15 00:20:02 +00001821// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1822void CodeGenSchedModels::collectProcResources() {
Matthias Braun4492b0e2016-06-21 03:24:03 +00001823 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1824 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1825
Andrew Trick3cbd1782012-09-15 00:20:02 +00001826 // Add any subtarget-specific SchedReadWrites that are directly associated
1827 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1828 // determine which processors they apply to.
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001829 for (const CodeGenSchedClass &SC :
1830 make_range(schedClassBegin(), schedClassEnd())) {
1831 if (SC.ItinClassDef) {
1832 collectItinProcResources(SC.ItinClassDef);
1833 continue;
Andrew Trickd9a4f0c2013-02-01 03:19:54 +00001834 }
Andrea Di Biagiobf50bde2018-04-26 12:56:26 +00001835
1836 // This class may have a default ReadWrite list which can be overriden by
1837 // InstRW definitions.
1838 for (Record *RW : SC.InstRWs) {
1839 Record *RWModelDef = RW->getValueAsDef("SchedModel");
1840 unsigned PIdx = getProcModel(RWModelDef).Index;
1841 IdxVec Writes, Reads;
1842 findRWs(RW->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1843 collectRWResources(Writes, Reads, PIdx);
1844 }
1845
1846 collectRWResources(SC.Writes, SC.Reads, SC.ProcIndices);
Andrew Trick3cbd1782012-09-15 00:20:02 +00001847 }
1848 // Add resources separately defined by each subtarget.
1849 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
Javed Absard138a572017-10-11 09:33:23 +00001850 for (Record *WR : WRDefs) {
1851 Record *ModelDef = WR->getValueAsDef("SchedModel");
1852 addWriteRes(WR, getProcModel(ModelDef).Index);
Andrew Trick3cbd1782012-09-15 00:20:02 +00001853 }
Andrew Trick0e8c89a2014-03-13 03:49:20 +00001854 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
Javed Absard138a572017-10-11 09:33:23 +00001855 for (Record *SWR : SWRDefs) {
1856 Record *ModelDef = SWR->getValueAsDef("SchedModel");
1857 addWriteRes(SWR, getProcModel(ModelDef).Index);
Andrew Trick0e8c89a2014-03-13 03:49:20 +00001858 }
Andrew Trick3cbd1782012-09-15 00:20:02 +00001859 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
Javed Absard138a572017-10-11 09:33:23 +00001860 for (Record *RA : RADefs) {
1861 Record *ModelDef = RA->getValueAsDef("SchedModel");
1862 addReadAdvance(RA, getProcModel(ModelDef).Index);
Andrew Trick3cbd1782012-09-15 00:20:02 +00001863 }
Andrew Trick0e8c89a2014-03-13 03:49:20 +00001864 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
Javed Absard138a572017-10-11 09:33:23 +00001865 for (Record *SRA : SRADefs) {
1866 if (SRA->getValueInit("SchedModel")->isComplete()) {
1867 Record *ModelDef = SRA->getValueAsDef("SchedModel");
1868 addReadAdvance(SRA, getProcModel(ModelDef).Index);
Andrew Trick0e8c89a2014-03-13 03:49:20 +00001869 }
1870 }
Andrew Tricka3d82ce2013-06-15 04:50:06 +00001871 // Add ProcResGroups that are defined within this processor model, which may
1872 // not be directly referenced but may directly specify a buffer size.
1873 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
Javed Absarc4533062017-10-09 16:21:25 +00001874 for (Record *PRG : ProcResGroups) {
Javed Absar3c69ee52017-10-05 13:27:43 +00001875 if (!PRG->getValueInit("SchedModel")->isComplete())
Andrew Tricka3d82ce2013-06-15 04:50:06 +00001876 continue;
Javed Absar3c69ee52017-10-05 13:27:43 +00001877 CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
1878 if (!is_contained(PM.ProcResourceDefs, PRG))
1879 PM.ProcResourceDefs.push_back(PRG);
Andrew Tricka3d82ce2013-06-15 04:50:06 +00001880 }
Clement Courbet703a63c2018-02-05 12:23:51 +00001881 // Add ProcResourceUnits unconditionally.
1882 for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
1883 if (!PRU->getValueInit("SchedModel")->isComplete())
1884 continue;
1885 CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
1886 if (!is_contained(PM.ProcResourceDefs, PRU))
1887 PM.ProcResourceDefs.push_back(PRU);
1888 }
Andrew Trick3cbd1782012-09-15 00:20:02 +00001889 // Finalize each ProcModel by sorting the record arrays.
Craig Topperd5578c82014-12-09 08:05:51 +00001890 for (CodeGenProcModel &PM : ProcModels) {
Fangrui Songc4662642018-09-30 22:31:29 +00001891 llvm::sort(PM.WriteResDefs, LessRecord());
1892 llvm::sort(PM.ReadAdvanceDefs, LessRecord());
1893 llvm::sort(PM.ProcResourceDefs, LessRecord());
Nicola Zaghen0818e782018-05-14 12:53:11 +00001894 LLVM_DEBUG(
1895 PM.dump();
1896 dbgs() << "WriteResDefs: "; for (RecIter RI = PM.WriteResDefs.begin(),
1897 RE = PM.WriteResDefs.end();
1898 RI != RE; ++RI) {
1899 if ((*RI)->isSubClassOf("WriteRes"))
1900 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1901 else
1902 dbgs() << (*RI)->getName() << " ";
1903 } dbgs() << "\nReadAdvanceDefs: ";
1904 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1905 RE = PM.ReadAdvanceDefs.end();
1906 RI != RE; ++RI) {
1907 if ((*RI)->isSubClassOf("ReadAdvance"))
1908 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1909 else
1910 dbgs() << (*RI)->getName() << " ";
1911 } dbgs()
1912 << "\nProcResourceDefs: ";
1913 for (RecIter RI = PM.ProcResourceDefs.begin(),
1914 RE = PM.ProcResourceDefs.end();
1915 RI != RE; ++RI) { dbgs() << (*RI)->getName() << " "; } dbgs()
1916 << '\n');
Andrew Tricke30f32a2013-04-23 23:45:14 +00001917 verifyProcResourceGroups(PM);
Andrew Trick3cbd1782012-09-15 00:20:02 +00001918 }
Matthias Braun4492b0e2016-06-21 03:24:03 +00001919
1920 ProcResourceDefs.clear();
1921 ProcResGroups.clear();
Andrew Trick3cbd1782012-09-15 00:20:02 +00001922}
1923
Matthias Brauneab28692016-03-01 20:03:21 +00001924void CodeGenSchedModels::checkCompleteness() {
1925 bool Complete = true;
1926 bool HadCompleteModel = false;
1927 for (const CodeGenProcModel &ProcModel : procModels()) {
Simon Pilgrim00a96df2018-04-05 13:11:36 +00001928 const bool HasItineraries = ProcModel.hasItineraries();
Matthias Brauneab28692016-03-01 20:03:21 +00001929 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1930 continue;
1931 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1932 if (Inst->hasNoSchedulingInfo)
1933 continue;
Simon Dardis111ec252016-06-24 08:43:27 +00001934 if (ProcModel.isUnsupported(*Inst))
1935 continue;
Matthias Brauneab28692016-03-01 20:03:21 +00001936 unsigned SCIdx = getSchedClassIdx(*Inst);
1937 if (!SCIdx) {
1938 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1939 PrintError("No schedule information for instruction '"
1940 + Inst->TheDef->getName() + "'");
1941 Complete = false;
1942 }
1943 continue;
1944 }
1945
1946 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1947 if (!SC.Writes.empty())
1948 continue;
Simon Pilgrim00a96df2018-04-05 13:11:36 +00001949 if (HasItineraries && SC.ItinClassDef != nullptr &&
Ulrich Weigand96c7b392016-10-31 18:59:52 +00001950 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun0101cb02016-03-03 00:04:59 +00001951 continue;
Matthias Brauneab28692016-03-01 20:03:21 +00001952
1953 const RecVec &InstRWs = SC.InstRWs;
David Majnemerb0353c62016-08-12 00:18:03 +00001954 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1955 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1956 });
Matthias Brauneab28692016-03-01 20:03:21 +00001957 if (I == InstRWs.end()) {
1958 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1959 Inst->TheDef->getName() + "'");
1960 Complete = false;
1961 }
1962 }
1963 HadCompleteModel = true;
1964 }
Matthias Braun3b1588a2016-03-01 21:36:12 +00001965 if (!Complete) {
1966 errs() << "\n\nIncomplete schedule models found.\n"
1967 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1968 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1969 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis111ec252016-06-24 08:43:27 +00001970 "you may temporarily use an empty list.\n"
1971 << "- Instructions related to unsupported features can be excluded with "
1972 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1973 "processor model.\n\n";
Matthias Brauneab28692016-03-01 20:03:21 +00001974 PrintFatalError("Incomplete schedule model");
Matthias Braun3b1588a2016-03-01 21:36:12 +00001975 }
Matthias Brauneab28692016-03-01 20:03:21 +00001976}
1977
Andrew Trick3cbd1782012-09-15 00:20:02 +00001978// Collect itinerary class resources for each processor.
1979void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1980 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1981 const CodeGenProcModel &PM = ProcModels[PIdx];
1982 // For all ItinRW entries.
1983 bool HasMatch = false;
1984 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1985 II != IE; ++II) {
1986 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1987 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1988 continue;
1989 if (HasMatch)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001990 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1991 + ItinClassDef->getName()
1992 + " in ItinResources for " + PM.ModelName);
Andrew Trick3cbd1782012-09-15 00:20:02 +00001993 HasMatch = true;
1994 IdxVec Writes, Reads;
1995 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
Craig Topper92016652018-03-24 21:57:35 +00001996 collectRWResources(Writes, Reads, PIdx);
Andrew Trick3cbd1782012-09-15 00:20:02 +00001997 }
1998 }
1999}
2000
Andrew Trickdbe6d432012-10-10 05:43:13 +00002001void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramer109f9e62015-10-24 12:46:49 +00002002 ArrayRef<unsigned> ProcIndices) {
Andrew Trickdbe6d432012-10-10 05:43:13 +00002003 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
2004 if (SchedRW.TheDef) {
2005 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramer109f9e62015-10-24 12:46:49 +00002006 for (unsigned Idx : ProcIndices)
2007 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickdbe6d432012-10-10 05:43:13 +00002008 }
2009 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramer109f9e62015-10-24 12:46:49 +00002010 for (unsigned Idx : ProcIndices)
2011 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickdbe6d432012-10-10 05:43:13 +00002012 }
2013 }
2014 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
2015 AI != AE; ++AI) {
2016 IdxVec AliasProcIndices;
2017 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
2018 AliasProcIndices.push_back(
2019 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
2020 }
2021 else
2022 AliasProcIndices = ProcIndices;
2023 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
2024 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
2025
2026 IdxVec ExpandedRWs;
2027 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
2028 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
2029 SI != SE; ++SI) {
2030 collectRWResources(*SI, IsRead, AliasProcIndices);
2031 }
2032 }
2033}
Andrew Trick3cbd1782012-09-15 00:20:02 +00002034
2035// Collect resources for a set of read/write types and processor indices.
Benjamin Kramer109f9e62015-10-24 12:46:49 +00002036void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
2037 ArrayRef<unsigned> Reads,
2038 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramer109f9e62015-10-24 12:46:49 +00002039 for (unsigned Idx : Writes)
2040 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickdbe6d432012-10-10 05:43:13 +00002041
Benjamin Kramer109f9e62015-10-24 12:46:49 +00002042 for (unsigned Idx : Reads)
2043 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick3cbd1782012-09-15 00:20:02 +00002044}
2045
2046// Find the processor's resource units for this kind of resource.
2047Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
Evandro Menezes8304ff22017-11-21 21:33:52 +00002048 const CodeGenProcModel &PM,
2049 ArrayRef<SMLoc> Loc) const {
Andrew Trick3cbd1782012-09-15 00:20:02 +00002050 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
2051 return ProcResKind;
2052
Craig Topper095734c2014-04-15 07:20:03 +00002053 Record *ProcUnitDef = nullptr;
Matthias Braun4492b0e2016-06-21 03:24:03 +00002054 assert(!ProcResourceDefs.empty());
2055 assert(!ProcResGroups.empty());
Andrew Trick3cbd1782012-09-15 00:20:02 +00002056
Javed Absar0a82c732017-09-13 10:31:10 +00002057 for (Record *ProcResDef : ProcResourceDefs) {
2058 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
2059 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick3cbd1782012-09-15 00:20:02 +00002060 if (ProcUnitDef) {
Evandro Menezes8304ff22017-11-21 21:33:52 +00002061 PrintFatalError(Loc,
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00002062 "Multiple ProcessorResourceUnits associated with "
2063 + ProcResKind->getName());
Andrew Trick3cbd1782012-09-15 00:20:02 +00002064 }
Javed Absar0a82c732017-09-13 10:31:10 +00002065 ProcUnitDef = ProcResDef;
Andrew Trick3cbd1782012-09-15 00:20:02 +00002066 }
2067 }
Javed Absar0a82c732017-09-13 10:31:10 +00002068 for (Record *ProcResGroup : ProcResGroups) {
2069 if (ProcResGroup == ProcResKind
2070 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1754aca2013-03-14 21:21:50 +00002071 if (ProcUnitDef) {
Evandro Menezes8304ff22017-11-21 21:33:52 +00002072 PrintFatalError(Loc,
Andrew Trick1754aca2013-03-14 21:21:50 +00002073 "Multiple ProcessorResourceUnits associated with "
2074 + ProcResKind->getName());
2075 }
Javed Absar0a82c732017-09-13 10:31:10 +00002076 ProcUnitDef = ProcResGroup;
Andrew Trick1754aca2013-03-14 21:21:50 +00002077 }
2078 }
Andrew Trick3cbd1782012-09-15 00:20:02 +00002079 if (!ProcUnitDef) {
Evandro Menezes8304ff22017-11-21 21:33:52 +00002080 PrintFatalError(Loc,
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00002081 "No ProcessorResources associated with "
2082 + ProcResKind->getName());
Andrew Trick3cbd1782012-09-15 00:20:02 +00002083 }
2084 return ProcUnitDef;
2085}
2086
2087// Iteratively add a resource and its super resources.
2088void CodeGenSchedModels::addProcResource(Record *ProcResKind,
Evandro Menezes8304ff22017-11-21 21:33:52 +00002089 CodeGenProcModel &PM,
2090 ArrayRef<SMLoc> Loc) {
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00002091 while (true) {
Evandro Menezes8304ff22017-11-21 21:33:52 +00002092 Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
Andrew Trick3cbd1782012-09-15 00:20:02 +00002093
2094 // See if this ProcResource is already associated with this processor.
David Majnemer2d62ce62016-08-12 03:55:06 +00002095 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick3cbd1782012-09-15 00:20:02 +00002096 return;
2097
2098 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick1754aca2013-03-14 21:21:50 +00002099 if (ProcResUnits->isSubClassOf("ProcResGroup"))
2100 return;
2101
Andrew Trick3cbd1782012-09-15 00:20:02 +00002102 if (!ProcResUnits->getValueInit("Super")->isComplete())
2103 return;
2104
2105 ProcResKind = ProcResUnits->getValueAsDef("Super");
2106 }
2107}
2108
2109// Add resources for a SchedWrite to this processor if they don't exist.
2110void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick92649882012-09-22 02:24:21 +00002111 assert(PIdx && "don't add resources to an invalid Processor model");
2112
Andrew Trick3cbd1782012-09-15 00:20:02 +00002113 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer2d62ce62016-08-12 03:55:06 +00002114 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick3cbd1782012-09-15 00:20:02 +00002115 return;
2116 WRDefs.push_back(ProcWriteResDef);
2117
2118 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
2119 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
2120 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
2121 WritePRI != WritePRE; ++WritePRI) {
Evandro Menezes8304ff22017-11-21 21:33:52 +00002122 addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
Andrew Trick3cbd1782012-09-15 00:20:02 +00002123 }
2124}
2125
2126// Add resources for a ReadAdvance to this processor if they don't exist.
2127void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
2128 unsigned PIdx) {
2129 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer2d62ce62016-08-12 03:55:06 +00002130 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick3cbd1782012-09-15 00:20:02 +00002131 return;
2132 RADefs.push_back(ProcReadAdvanceDef);
2133}
2134
Andrew Trickbc4ff6e2012-09-17 22:18:43 +00002135unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer975248e2016-08-11 22:21:41 +00002136 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trickbc4ff6e2012-09-17 22:18:43 +00002137 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00002138 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
2139 "the ProcResources list for " + ModelName);
Andrew Trickbc4ff6e2012-09-17 22:18:43 +00002140 // Idx=0 is reserved for invalid.
Rafael Espindola322ff882012-11-02 20:57:36 +00002141 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trickbc4ff6e2012-09-17 22:18:43 +00002142}
2143
Simon Dardis111ec252016-06-24 08:43:27 +00002144bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
2145 for (const Record *TheDef : UnsupportedFeaturesDefs) {
2146 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
2147 if (TheDef->getName() == PredDef->getName())
2148 return true;
2149 }
2150 }
2151 return false;
2152}
2153
Andrew Trick48605c32012-09-15 00:19:57 +00002154#ifndef NDEBUG
2155void CodeGenProcModel::dump() const {
2156 dbgs() << Index << ": " << ModelName << " "
2157 << (ModelDef ? ModelDef->getName() : "inferred") << " "
2158 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
2159}
2160
2161void CodeGenSchedRW::dump() const {
2162 dbgs() << Name << (IsVariadic ? " (V) " : " ");
2163 if (IsSequence) {
2164 dbgs() << "(";
2165 dumpIdxVec(Sequence);
2166 dbgs() << ")";
2167 }
2168}
2169
2170void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trick1ab961f2013-03-16 18:58:55 +00002171 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick48605c32012-09-15 00:19:57 +00002172 << " Writes: ";
2173 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
2174 SchedModels->getSchedWrite(Writes[i]).dump();
2175 if (i < N-1) {
2176 dbgs() << '\n';
2177 dbgs().indent(10);
2178 }
2179 }
2180 dbgs() << "\n Reads: ";
2181 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
2182 SchedModels->getSchedRead(Reads[i]).dump();
2183 if (i < N-1) {
2184 dbgs() << '\n';
2185 dbgs().indent(10);
2186 }
2187 }
2188 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Trick82e7c4f2013-03-26 21:36:39 +00002189 if (!Transitions.empty()) {
2190 dbgs() << "\n Transitions for Proc ";
Javed Absar0a82c732017-09-13 10:31:10 +00002191 for (const CodeGenSchedTransition &Transition : Transitions) {
2192 dumpIdxVec(Transition.ProcIndices);
Andrew Trick82e7c4f2013-03-26 21:36:39 +00002193 }
2194 }
Andrew Trick48605c32012-09-15 00:19:57 +00002195}
Andrew Trick5e613c22012-09-15 00:19:59 +00002196
2197void PredTransitions::dump() const {
2198 dbgs() << "Expanded Variants:\n";
2199 for (std::vector<PredTransition>::const_iterator
2200 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
2201 dbgs() << "{";
2202 for (SmallVectorImpl<PredCheck>::const_iterator
2203 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
2204 PCI != PCE; ++PCI) {
2205 if (PCI != TI->PredTerm.begin())
2206 dbgs() << ", ";
2207 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
2208 << ":" << PCI->Predicate->getName();
2209 }
2210 dbgs() << "},\n => {";
Eugene Zelenkoc02caf52016-11-30 17:48:10 +00002211 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick5e613c22012-09-15 00:19:59 +00002212 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
2213 WSI != WSE; ++WSI) {
2214 dbgs() << "(";
2215 for (SmallVectorImpl<unsigned>::const_iterator
2216 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
2217 if (WI != WSI->begin())
2218 dbgs() << ", ";
2219 dbgs() << SchedModels.getSchedWrite(*WI).Name;
2220 }
2221 dbgs() << "),";
2222 }
2223 dbgs() << "}\n";
2224 }
2225}
Andrew Trick48605c32012-09-15 00:19:57 +00002226#endif // NDEBUG