blob: 8a8911c5087d878ebb5cec3fc81c429888b54305 [file] [log] [blame]
Andrew Trick87255e32012-07-07 04:00:00 +00001//===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alp Tokercb402912014-01-24 17:20:08 +000010// This file defines structures to encapsulate the machine model as described in
Andrew Trick87255e32012-07-07 04:00:00 +000011// the target description.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000015#include "CodeGenInstruction.h"
Andrew Trick87255e32012-07-07 04:00:00 +000016#include "CodeGenSchedule.h"
17#include "CodeGenTarget.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000021#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000022#include "llvm/Support/Casting.h"
Andrew Trick87255e32012-07-07 04:00:00 +000023#include "llvm/Support/Debug.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000024#include "llvm/Support/raw_ostream.h"
Andrew Trick9e1deb62012-10-03 23:06:32 +000025#include "llvm/Support/Regex.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000026#include "llvm/TableGen/Error.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000027#include <algorithm>
28#include <iterator>
29#include <utility>
Andrew Trick87255e32012-07-07 04:00:00 +000030
31using namespace llvm;
32
Chandler Carruth97acce22014-04-22 03:06:00 +000033#define DEBUG_TYPE "subtarget-emitter"
34
Andrew Trick76686492012-09-15 00:19:57 +000035#ifndef NDEBUG
Benjamin Kramere1761952015-10-24 12:46:49 +000036static void dumpIdxVec(ArrayRef<unsigned> V) {
37 for (unsigned Idx : V)
38 dbgs() << Idx << ", ";
Andrew Trick33401e82012-09-15 00:19:59 +000039}
Andrew Trick76686492012-09-15 00:19:57 +000040#endif
41
Juergen Ributzka05c5a932013-11-19 03:08:35 +000042namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000043
Andrew Trick9e1deb62012-10-03 23:06:32 +000044// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
45struct InstrsOp : public SetTheory::Operator {
Craig Topper716b0732014-03-05 05:17:42 +000046 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
47 ArrayRef<SMLoc> Loc) override {
Juergen Ributzka05c5a932013-11-19 03:08:35 +000048 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
49 }
50};
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000051
Andrew Trick9e1deb62012-10-03 23:06:32 +000052// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
53//
54// TODO: Since this is a prefix match, perform a binary search over the
55// instruction names using lower_bound. Note that the predefined instrs must be
56// scanned linearly first. However, this is only safe if the regex pattern has
57// no top-level bars. The DAG already has a list of patterns, so there's no
58// reason to use top-level bars, but we need a way to verify they don't exist
59// before implementing the optimization.
60struct InstRegexOp : public SetTheory::Operator {
61 const CodeGenTarget &Target;
62 InstRegexOp(const CodeGenTarget &t): Target(t) {}
63
Juergen Ributzka05c5a932013-11-19 03:08:35 +000064 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
Craig Topper716b0732014-03-05 05:17:42 +000065 ArrayRef<SMLoc> Loc) override {
David Blaikie80721252014-04-21 21:49:08 +000066 SmallVector<Regex, 4> RegexList;
Juergen Ributzka05c5a932013-11-19 03:08:35 +000067 for (DagInit::const_arg_iterator
68 AI = Expr->arg_begin(), AE = Expr->arg_end(); AI != AE; ++AI) {
69 StringInit *SI = dyn_cast<StringInit>(*AI);
70 if (!SI)
71 PrintFatalError(Loc, "instregex requires pattern string: "
72 + Expr->getAsString());
73 std::string pat = SI->getValue();
74 // Implement a python-style prefix match.
75 if (pat[0] != '^') {
76 pat.insert(0, "^(");
77 pat.insert(pat.end(), ')');
78 }
David Blaikie80721252014-04-21 21:49:08 +000079 RegexList.push_back(Regex(pat));
Juergen Ributzka05c5a932013-11-19 03:08:35 +000080 }
Craig Topper8cc904d2016-01-17 20:38:18 +000081 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
David Blaikie80721252014-04-21 21:49:08 +000082 for (auto &R : RegexList) {
Craig Topper8a417c12014-12-09 08:05:51 +000083 if (R.match(Inst->TheDef->getName()))
84 Elts.insert(Inst->TheDef);
Juergen Ributzka05c5a932013-11-19 03:08:35 +000085 }
86 }
Juergen Ributzka05c5a932013-11-19 03:08:35 +000087 }
Andrew Trick9e1deb62012-10-03 23:06:32 +000088};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000089
Juergen Ributzka05c5a932013-11-19 03:08:35 +000090} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000091
Andrew Trick76686492012-09-15 00:19:57 +000092/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick87255e32012-07-07 04:00:00 +000093CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
94 const CodeGenTarget &TGT):
Andrew Trickbf8a28d2013-03-16 18:58:55 +000095 Records(RK), Target(TGT) {
Andrew Trick87255e32012-07-07 04:00:00 +000096
Andrew Trick9e1deb62012-10-03 23:06:32 +000097 Sets.addFieldExpander("InstRW", "Instrs");
98
99 // Allow Set evaluation to recognize the dags used in InstRW records:
100 // (instrs Op1, Op1...)
Craig Topperba6057d2015-04-24 06:49:44 +0000101 Sets.addOperator("instrs", llvm::make_unique<InstrsOp>());
102 Sets.addOperator("instregex", llvm::make_unique<InstRegexOp>(Target));
Andrew Trick9e1deb62012-10-03 23:06:32 +0000103
Andrew Trick76686492012-09-15 00:19:57 +0000104 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
105 // that are explicitly referenced in tablegen records. Resources associated
106 // with each processor will be derived later. Populate ProcModelMap with the
107 // CodeGenProcModel instances.
108 collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000109
Andrew Trick76686492012-09-15 00:19:57 +0000110 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
111 // defined, and populate SchedReads and SchedWrites vectors. Implicit
112 // SchedReadWrites that represent sequences derived from expanded variant will
113 // be inferred later.
114 collectSchedRW();
115
116 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
117 // required by an instruction definition, and populate SchedClassIdxMap. Set
118 // NumItineraryClasses to the number of explicit itinerary classes referenced
119 // by instructions. Set NumInstrSchedClasses to the number of itinerary
120 // classes plus any classes implied by instructions that derive from class
121 // Sched and provide SchedRW list. This does not infer any new classes from
122 // SchedVariant.
123 collectSchedClasses();
124
125 // Find instruction itineraries for each processor. Sort and populate
Andrew Trick9257b8f2012-09-22 02:24:21 +0000126 // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
Andrew Trick76686492012-09-15 00:19:57 +0000127 // all itinerary classes to be discovered.
128 collectProcItins();
129
130 // Find ItinRW records for each processor and itinerary class.
131 // (For per-operand resources mapped to itinerary classes).
132 collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000133
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000134 // Find UnsupportedFeatures records for each processor.
135 // (For per-operand resources mapped to itinerary classes).
136 collectProcUnsupportedFeatures();
137
Andrew Trick33401e82012-09-15 00:19:59 +0000138 // Infer new SchedClasses from SchedVariant.
139 inferSchedClasses();
140
Andrew Trick1e46d482012-09-15 00:20:02 +0000141 // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
142 // ProcResourceDefs.
Joel Jones80372332017-06-28 00:06:40 +0000143 DEBUG(dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
Andrew Trick1e46d482012-09-15 00:20:02 +0000144 collectProcResources();
Matthias Braun17cb5792016-03-01 20:03:21 +0000145
146 checkCompleteness();
Andrew Trick87255e32012-07-07 04:00:00 +0000147}
148
Andrew Trick76686492012-09-15 00:19:57 +0000149/// Gather all processor models.
150void CodeGenSchedModels::collectProcModels() {
151 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
152 std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick87255e32012-07-07 04:00:00 +0000153
Andrew Trick76686492012-09-15 00:19:57 +0000154 // Reserve space because we can. Reallocation would be ok.
155 ProcModels.reserve(ProcRecords.size()+1);
156
157 // Use idx=0 for NoModel/NoItineraries.
158 Record *NoModelDef = Records.getDef("NoSchedModel");
159 Record *NoItinsDef = Records.getDef("NoItineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000160 ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000161 ProcModelMap[NoModelDef] = 0;
162
163 // For each processor, find a unique machine model.
Joel Jones80372332017-06-28 00:06:40 +0000164 DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000165 for (Record *ProcRecord : ProcRecords)
166 addProcModel(ProcRecord);
Andrew Trick76686492012-09-15 00:19:57 +0000167}
168
169/// Get a unique processor model based on the defined MachineModel and
170/// ProcessorItineraries.
171void CodeGenSchedModels::addProcModel(Record *ProcDef) {
172 Record *ModelKey = getModelOrItinDef(ProcDef);
173 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
174 return;
175
176 std::string Name = ModelKey->getName();
177 if (ModelKey->isSubClassOf("SchedMachineModel")) {
178 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000179 ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
Andrew Trick76686492012-09-15 00:19:57 +0000180 }
181 else {
182 // An itinerary is defined without a machine model. Infer a new model.
183 if (!ModelKey->getValueAsListOfDefs("IID").empty())
184 Name = Name + "Model";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000185 ProcModels.emplace_back(ProcModels.size(), Name,
186 ProcDef->getValueAsDef("SchedModel"), ModelKey);
Andrew Trick76686492012-09-15 00:19:57 +0000187 }
188 DEBUG(ProcModels.back().dump());
189}
190
191// Recursively find all reachable SchedReadWrite records.
192static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
193 SmallPtrSet<Record*, 16> &RWSet) {
David Blaikie70573dc2014-11-19 07:49:26 +0000194 if (!RWSet.insert(RWDef).second)
Andrew Trick76686492012-09-15 00:19:57 +0000195 return;
196 RWDefs.push_back(RWDef);
Javed Absar67b042c2017-09-13 10:31:10 +0000197 // Reads don't currently have sequence records, but it can be added later.
Andrew Trick76686492012-09-15 00:19:57 +0000198 if (RWDef->isSubClassOf("WriteSequence")) {
199 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
Javed Absar67b042c2017-09-13 10:31:10 +0000200 for (Record *WSRec : Seq)
201 scanSchedRW(WSRec, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000202 }
203 else if (RWDef->isSubClassOf("SchedVariant")) {
204 // Visit each variant (guarded by a different predicate).
205 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
Javed Absar67b042c2017-09-13 10:31:10 +0000206 for (Record *Variant : Vars) {
Andrew Trick76686492012-09-15 00:19:57 +0000207 // Visit each RW in the sequence selected by the current variant.
Javed Absar67b042c2017-09-13 10:31:10 +0000208 RecVec Selected = Variant->getValueAsListOfDefs("Selected");
209 for (Record *SelDef : Selected)
210 scanSchedRW(SelDef, RWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000211 }
212 }
213}
214
215// Collect and sort all SchedReadWrites reachable via tablegen records.
216// More may be inferred later when inferring new SchedClasses from variants.
217void CodeGenSchedModels::collectSchedRW() {
218 // Reserve idx=0 for invalid writes/reads.
219 SchedWrites.resize(1);
220 SchedReads.resize(1);
221
222 SmallPtrSet<Record*, 16> RWSet;
223
224 // Find all SchedReadWrites referenced by instruction defs.
225 RecVec SWDefs, SRDefs;
Craig Topper8cc904d2016-01-17 20:38:18 +0000226 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000227 Record *SchedDef = Inst->TheDef;
Jakob Stoklund Olesena4a361d2013-03-15 22:51:13 +0000228 if (SchedDef->isValueUnset("SchedRW"))
Andrew Trick76686492012-09-15 00:19:57 +0000229 continue;
230 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000231 for (Record *RW : RWs) {
232 if (RW->isSubClassOf("SchedWrite"))
233 scanSchedRW(RW, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000234 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000235 assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
236 scanSchedRW(RW, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000237 }
238 }
239 }
240 // Find all ReadWrites referenced by InstRW.
241 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000242 for (Record *InstRWDef : InstRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000243 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000244 RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
245 for (Record *RWDef : RWDefs) {
246 if (RWDef->isSubClassOf("SchedWrite"))
247 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000248 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000249 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
250 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000251 }
252 }
253 }
254 // Find all ReadWrites referenced by ItinRW.
255 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
Javed Absar67b042c2017-09-13 10:31:10 +0000256 for (Record *ItinRWDef : ItinRWDefs) {
Andrew Trick76686492012-09-15 00:19:57 +0000257 // For all OperandReadWrites.
Javed Absar67b042c2017-09-13 10:31:10 +0000258 RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
259 for (Record *RWDef : RWDefs) {
260 if (RWDef->isSubClassOf("SchedWrite"))
261 scanSchedRW(RWDef, SWDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000262 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000263 assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
264 scanSchedRW(RWDef, SRDefs, RWSet);
Andrew Trick76686492012-09-15 00:19:57 +0000265 }
266 }
267 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000268 // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
269 // for the loop below that initializes Alias vectors.
270 RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
271 std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000272 for (Record *ADef : AliasDefs) {
273 Record *MatchDef = ADef->getValueAsDef("MatchRW");
274 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000275 if (MatchDef->isSubClassOf("SchedWrite")) {
276 if (!AliasDef->isSubClassOf("SchedWrite"))
Javed Absar67b042c2017-09-13 10:31:10 +0000277 PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000278 scanSchedRW(AliasDef, SWDefs, RWSet);
279 }
280 else {
281 assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
282 if (!AliasDef->isSubClassOf("SchedRead"))
Javed Absar67b042c2017-09-13 10:31:10 +0000283 PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000284 scanSchedRW(AliasDef, SRDefs, RWSet);
285 }
286 }
Andrew Trick76686492012-09-15 00:19:57 +0000287 // Sort and add the SchedReadWrites directly referenced by instructions or
288 // itinerary resources. Index reads and writes in separate domains.
289 std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000290 for (Record *SWDef : SWDefs) {
291 assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
292 SchedWrites.emplace_back(SchedWrites.size(), SWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000293 }
294 std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
Javed Absar67b042c2017-09-13 10:31:10 +0000295 for (Record *SRDef : SRDefs) {
296 assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
297 SchedReads.emplace_back(SchedReads.size(), SRDef);
Andrew Trick76686492012-09-15 00:19:57 +0000298 }
299 // Initialize WriteSequence vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000300 for (CodeGenSchedRW &CGRW : SchedWrites) {
301 if (!CGRW.IsSequence)
Andrew Trick76686492012-09-15 00:19:57 +0000302 continue;
Javed Absar67b042c2017-09-13 10:31:10 +0000303 findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
Andrew Trick76686492012-09-15 00:19:57 +0000304 /*IsRead=*/false);
305 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000306 // Initialize Aliases vectors.
Javed Absar67b042c2017-09-13 10:31:10 +0000307 for (Record *ADef : AliasDefs) {
308 Record *AliasDef = ADef->getValueAsDef("AliasRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000309 getSchedRW(AliasDef).IsAlias = true;
Javed Absar67b042c2017-09-13 10:31:10 +0000310 Record *MatchDef = ADef->getValueAsDef("MatchRW");
Andrew Trick9257b8f2012-09-22 02:24:21 +0000311 CodeGenSchedRW &RW = getSchedRW(MatchDef);
312 if (RW.IsAlias)
Javed Absar67b042c2017-09-13 10:31:10 +0000313 PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
314 RW.Aliases.push_back(ADef);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000315 }
Andrew Trick76686492012-09-15 00:19:57 +0000316 DEBUG(
Joel Jones80372332017-06-28 00:06:40 +0000317 dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
Andrew Trick76686492012-09-15 00:19:57 +0000318 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
319 dbgs() << WIdx << ": ";
320 SchedWrites[WIdx].dump();
321 dbgs() << '\n';
322 }
323 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
324 dbgs() << RIdx << ": ";
325 SchedReads[RIdx].dump();
326 dbgs() << '\n';
327 }
328 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
Javed Absar67b042c2017-09-13 10:31:10 +0000329 for (Record *RWDef : RWDefs) {
330 if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
331 const std::string &Name = RWDef->getName();
Andrew Trick76686492012-09-15 00:19:57 +0000332 if (Name != "NoWrite" && Name != "ReadDefault")
Javed Absar67b042c2017-09-13 10:31:10 +0000333 dbgs() << "Unused SchedReadWrite " << RWDef->getName() << '\n';
Andrew Trick76686492012-09-15 00:19:57 +0000334 }
335 });
336}
337
338/// Compute a SchedWrite name from a sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000339std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
Andrew Trick76686492012-09-15 00:19:57 +0000340 std::string Name("(");
Benjamin Kramere1761952015-10-24 12:46:49 +0000341 for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000342 if (I != Seq.begin())
343 Name += '_';
344 Name += getSchedRW(*I, IsRead).Name;
345 }
346 Name += ')';
347 return Name;
348}
349
350unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead,
351 unsigned After) const {
352 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
353 assert(After < RWVec.size() && "start position out of bounds");
354 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin() + After,
355 E = RWVec.end(); I != E; ++I) {
356 if (I->TheDef == Def)
357 return I - RWVec.begin();
358 }
359 return 0;
360}
361
Andrew Trickcfe222c2012-09-19 04:43:19 +0000362bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000363 for (const CodeGenSchedRW &Read : SchedReads) {
364 Record *ReadDef = Read.TheDef;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000365 if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
366 continue;
367
368 RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
David Majnemer0d955d02016-08-11 22:21:41 +0000369 if (is_contained(ValidWrites, WriteDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000370 return true;
371 }
372 }
373 return false;
374}
375
Andrew Trick76686492012-09-15 00:19:57 +0000376namespace llvm {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000377
Andrew Trick76686492012-09-15 00:19:57 +0000378void splitSchedReadWrites(const RecVec &RWDefs,
379 RecVec &WriteDefs, RecVec &ReadDefs) {
Javed Absar67b042c2017-09-13 10:31:10 +0000380 for (Record *RWDef : RWDefs) {
381 if (RWDef->isSubClassOf("SchedWrite"))
382 WriteDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000383 else {
Javed Absar67b042c2017-09-13 10:31:10 +0000384 assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
385 ReadDefs.push_back(RWDef);
Andrew Trick76686492012-09-15 00:19:57 +0000386 }
387 }
388}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000389
390} // end namespace llvm
Andrew Trick76686492012-09-15 00:19:57 +0000391
392// Split the SchedReadWrites defs and call findRWs for each list.
393void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
394 IdxVec &Writes, IdxVec &Reads) const {
395 RecVec WriteDefs;
396 RecVec ReadDefs;
397 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
398 findRWs(WriteDefs, Writes, false);
399 findRWs(ReadDefs, Reads, true);
400}
401
402// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
403void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
404 bool IsRead) const {
Javed Absar67b042c2017-09-13 10:31:10 +0000405 for (Record *RWDef : RWDefs) {
406 unsigned Idx = getSchedRWIdx(RWDef, IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000407 assert(Idx && "failed to collect SchedReadWrite");
408 RWs.push_back(Idx);
409 }
410}
411
Andrew Trick33401e82012-09-15 00:19:59 +0000412void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
413 bool IsRead) const {
414 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
415 if (!SchedRW.IsSequence) {
416 RWSeq.push_back(RWIdx);
417 return;
418 }
419 int Repeat =
420 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
421 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000422 for (unsigned I : SchedRW.Sequence) {
423 expandRWSequence(I, RWSeq, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000424 }
425 }
426}
427
Andrew Trickda984b12012-10-03 23:06:28 +0000428// Expand a SchedWrite as a sequence following any aliases that coincide with
429// the given processor model.
430void CodeGenSchedModels::expandRWSeqForProc(
431 unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
432 const CodeGenProcModel &ProcModel) const {
433
434 const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
Craig Topper24064772014-04-15 07:20:03 +0000435 Record *AliasDef = nullptr;
Andrew Trickda984b12012-10-03 23:06:28 +0000436 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
437 AI != AE; ++AI) {
438 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
439 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
440 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
441 if (&getProcModel(ModelDef) != &ProcModel)
442 continue;
443 }
444 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000445 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
446 "defined for processor " + ProcModel.ModelName +
447 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +0000448 AliasDef = AliasRW.TheDef;
449 }
450 if (AliasDef) {
451 expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
452 RWSeq, IsRead,ProcModel);
453 return;
454 }
455 if (!SchedWrite.IsSequence) {
456 RWSeq.push_back(RWIdx);
457 return;
458 }
459 int Repeat =
460 SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
461 for (int i = 0; i < Repeat; ++i) {
Javed Absar67b042c2017-09-13 10:31:10 +0000462 for (unsigned I : SchedWrite.Sequence) {
463 expandRWSeqForProc(I, RWSeq, IsRead, ProcModel);
Andrew Trickda984b12012-10-03 23:06:28 +0000464 }
465 }
466}
467
Andrew Trick33401e82012-09-15 00:19:59 +0000468// Find the existing SchedWrite that models this sequence of writes.
Benjamin Kramere1761952015-10-24 12:46:49 +0000469unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
Andrew Trick33401e82012-09-15 00:19:59 +0000470 bool IsRead) {
471 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
472
473 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
474 I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000475 if (makeArrayRef(I->Sequence) == Seq)
Andrew Trick33401e82012-09-15 00:19:59 +0000476 return I - RWVec.begin();
477 }
478 // Index zero reserved for invalid RW.
479 return 0;
480}
481
482/// Add this ReadWrite if it doesn't already exist.
483unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
484 bool IsRead) {
485 assert(!Seq.empty() && "cannot insert empty sequence");
486 if (Seq.size() == 1)
487 return Seq.back();
488
489 unsigned Idx = findRWForSequence(Seq, IsRead);
490 if (Idx)
491 return Idx;
492
Andrew Trickda984b12012-10-03 23:06:28 +0000493 unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
494 CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
495 if (IsRead)
Andrew Trick33401e82012-09-15 00:19:59 +0000496 SchedReads.push_back(SchedRW);
Andrew Trickda984b12012-10-03 23:06:28 +0000497 else
498 SchedWrites.push_back(SchedRW);
499 return RWIdx;
Andrew Trick33401e82012-09-15 00:19:59 +0000500}
501
Andrew Trick76686492012-09-15 00:19:57 +0000502/// Visit all the instruction definitions for this target to gather and
503/// enumerate the itinerary classes. These are the explicitly specified
504/// SchedClasses. More SchedClasses may be inferred.
505void CodeGenSchedModels::collectSchedClasses() {
506
507 // NoItinerary is always the first class at Idx=0
Andrew Trick87255e32012-07-07 04:00:00 +0000508 SchedClasses.resize(1);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000509 SchedClasses.back().Index = 0;
510 SchedClasses.back().Name = "NoInstrModel";
511 SchedClasses.back().ItinClassDef = Records.getDef("NoItinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000512 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick87255e32012-07-07 04:00:00 +0000513
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000514 // Create a SchedClass for each unique combination of itinerary class and
515 // SchedRW list.
Craig Topper8cc904d2016-01-17 20:38:18 +0000516 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topper8a417c12014-12-09 08:05:51 +0000517 Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
Andrew Trick76686492012-09-15 00:19:57 +0000518 IdxVec Writes, Reads;
Craig Topper8a417c12014-12-09 08:05:51 +0000519 if (!Inst->TheDef->isValueUnset("SchedRW"))
520 findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000521
Andrew Trick76686492012-09-15 00:19:57 +0000522 // ProcIdx == 0 indicates the class applies to all processors.
523 IdxVec ProcIndices(1, 0);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000524
525 unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, ProcIndices);
Craig Topper8a417c12014-12-09 08:05:51 +0000526 InstrClassMap[Inst->TheDef] = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000527 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000528 // Create classes for InstRW defs.
Andrew Trick76686492012-09-15 00:19:57 +0000529 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
530 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
Joel Jones80372332017-06-28 00:06:40 +0000531 DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
Javed Absar67b042c2017-09-13 10:31:10 +0000532 for (Record *RWDef : InstRWDefs)
533 createInstRWClass(RWDef);
Andrew Trick87255e32012-07-07 04:00:00 +0000534
Andrew Trick76686492012-09-15 00:19:57 +0000535 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000536
Andrew Trick76686492012-09-15 00:19:57 +0000537 bool EnableDump = false;
538 DEBUG(EnableDump = true);
539 if (!EnableDump)
Andrew Trick87255e32012-07-07 04:00:00 +0000540 return;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000541
Joel Jones80372332017-06-28 00:06:40 +0000542 dbgs() << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n";
Craig Topper8cc904d2016-01-17 20:38:18 +0000543 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000544 StringRef InstName = Inst->TheDef->getName();
Craig Topper8a417c12014-12-09 08:05:51 +0000545 unsigned SCIdx = InstrClassMap.lookup(Inst->TheDef);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000546 if (!SCIdx) {
Matthias Braun8e0a7342016-03-01 20:03:11 +0000547 if (!Inst->hasNoSchedulingInfo)
548 dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000549 continue;
550 }
551 CodeGenSchedClass &SC = getSchedClass(SCIdx);
552 if (SC.ProcIndices[0] != 0)
Craig Topper8a417c12014-12-09 08:05:51 +0000553 PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000554 "must not be subtarget specific.");
555
556 IdxVec ProcIndices;
557 if (SC.ItinClassDef->getName() != "NoItinerary") {
558 ProcIndices.push_back(0);
559 dbgs() << "Itinerary for " << InstName << ": "
560 << SC.ItinClassDef->getName() << '\n';
561 }
562 if (!SC.Writes.empty()) {
563 ProcIndices.push_back(0);
564 dbgs() << "SchedRW machine model for " << InstName;
565 for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
566 dbgs() << " " << SchedWrites[*WI].Name;
567 for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
568 dbgs() << " " << SchedReads[*RI].Name;
569 dbgs() << '\n';
570 }
571 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
Javed Absar67b042c2017-09-13 10:31:10 +0000572 for (Record *RWDef : RWDefs) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000573 const CodeGenProcModel &ProcModel =
Javed Absar67b042c2017-09-13 10:31:10 +0000574 getProcModel(RWDef->getValueAsDef("SchedModel"));
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000575 ProcIndices.push_back(ProcModel.Index);
576 dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
Andrew Trick76686492012-09-15 00:19:57 +0000577 IdxVec Writes;
578 IdxVec Reads;
Javed Absar67b042c2017-09-13 10:31:10 +0000579 findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000580 Writes, Reads);
Javed Absar67b042c2017-09-13 10:31:10 +0000581 for (unsigned WIdx : Writes)
582 dbgs() << " " << SchedWrites[WIdx].Name;
583 for (unsigned RIdx : Reads)
584 dbgs() << " " << SchedReads[RIdx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000585 dbgs() << '\n';
586 }
Andrew Trickf9df92c92016-10-18 04:17:44 +0000587 // If ProcIndices contains zero, the class applies to all processors.
588 if (!std::count(ProcIndices.begin(), ProcIndices.end(), 0)) {
589 for (std::vector<CodeGenProcModel>::iterator PI = ProcModels.begin(),
590 PE = ProcModels.end(); PI != PE; ++PI) {
591 if (!std::count(ProcIndices.begin(), ProcIndices.end(), PI->Index))
592 dbgs() << "No machine model for " << Inst->TheDef->getName()
593 << " on processor " << PI->ModelName << '\n';
594 }
Andrew Trick87255e32012-07-07 04:00:00 +0000595 }
596 }
Andrew Trick76686492012-09-15 00:19:57 +0000597}
598
Andrew Trick76686492012-09-15 00:19:57 +0000599/// Find an SchedClass that has been inferred from a per-operand list of
600/// SchedWrites and SchedReads.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000601unsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000602 ArrayRef<unsigned> Writes,
603 ArrayRef<unsigned> Reads) const {
Andrew Trick76686492012-09-15 00:19:57 +0000604 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I) {
Benjamin Kramere1761952015-10-24 12:46:49 +0000605 if (I->ItinClassDef == ItinClassDef && makeArrayRef(I->Writes) == Writes &&
606 makeArrayRef(I->Reads) == Reads) {
Andrew Trick76686492012-09-15 00:19:57 +0000607 return I - schedClassBegin();
608 }
Andrew Trick87255e32012-07-07 04:00:00 +0000609 }
Andrew Trick76686492012-09-15 00:19:57 +0000610 return 0;
611}
Andrew Trick87255e32012-07-07 04:00:00 +0000612
Andrew Trick76686492012-09-15 00:19:57 +0000613// Get the SchedClass index for an instruction.
614unsigned CodeGenSchedModels::getSchedClassIdx(
615 const CodeGenInstruction &Inst) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000616
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000617 return InstrClassMap.lookup(Inst.TheDef);
Andrew Trick76686492012-09-15 00:19:57 +0000618}
619
Benjamin Kramere1761952015-10-24 12:46:49 +0000620std::string
621CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
622 ArrayRef<unsigned> OperWrites,
623 ArrayRef<unsigned> OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000624
625 std::string Name;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000626 if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
627 Name = ItinClassDef->getName();
Benjamin Kramere1761952015-10-24 12:46:49 +0000628 for (unsigned Idx : OperWrites) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000629 if (!Name.empty())
Andrew Trick76686492012-09-15 00:19:57 +0000630 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000631 Name += SchedWrites[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000632 }
Benjamin Kramere1761952015-10-24 12:46:49 +0000633 for (unsigned Idx : OperReads) {
Andrew Trick76686492012-09-15 00:19:57 +0000634 Name += '_';
Benjamin Kramere1761952015-10-24 12:46:49 +0000635 Name += SchedReads[Idx].Name;
Andrew Trick76686492012-09-15 00:19:57 +0000636 }
637 return Name;
638}
639
640std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
641
642 std::string Name;
643 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
644 if (I != InstDefs.begin())
645 Name += '_';
646 Name += (*I)->getName();
647 }
648 return Name;
649}
650
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000651/// Add an inferred sched class from an itinerary class and per-operand list of
652/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
653/// processors that may utilize this class.
654unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000655 ArrayRef<unsigned> OperWrites,
656 ArrayRef<unsigned> OperReads,
657 ArrayRef<unsigned> ProcIndices) {
Andrew Trick76686492012-09-15 00:19:57 +0000658 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
659
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000660 unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
661 if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
Andrew Trick76686492012-09-15 00:19:57 +0000662 IdxVec PI;
663 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
664 SchedClasses[Idx].ProcIndices.end(),
665 ProcIndices.begin(), ProcIndices.end(),
666 std::back_inserter(PI));
667 SchedClasses[Idx].ProcIndices.swap(PI);
668 return Idx;
669 }
670 Idx = SchedClasses.size();
671 SchedClasses.resize(Idx+1);
672 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000673 SC.Index = Idx;
674 SC.Name = createSchedClassName(ItinClassDef, OperWrites, OperReads);
675 SC.ItinClassDef = ItinClassDef;
Andrew Trick76686492012-09-15 00:19:57 +0000676 SC.Writes = OperWrites;
677 SC.Reads = OperReads;
678 SC.ProcIndices = ProcIndices;
679
680 return Idx;
681}
682
683// Create classes for each set of opcodes that are in the same InstReadWrite
684// definition across all processors.
685void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
686 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
687 // intersects with an existing class via a previous InstRWDef. Instrs that do
688 // not intersect with an existing class refer back to their former class as
689 // determined from ItinDef or SchedRW.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000690 SmallVector<std::pair<unsigned, SmallVector<Record *, 8>>, 4> ClassInstrs;
Andrew Trick76686492012-09-15 00:19:57 +0000691 // Sort Instrs into sets.
Andrew Trick9e1deb62012-10-03 23:06:32 +0000692 const RecVec *InstDefs = Sets.expand(InstRWDef);
693 if (InstDefs->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000694 PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
Andrew Trick9e1deb62012-10-03 23:06:32 +0000695
696 for (RecIter I = InstDefs->begin(), E = InstDefs->end(); I != E; ++I) {
Andrew Trick76686492012-09-15 00:19:57 +0000697 InstClassMapTy::const_iterator Pos = InstrClassMap.find(*I);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000698 if (Pos == InstrClassMap.end())
699 PrintFatalError((*I)->getLoc(), "No sched class for instruction.");
700 unsigned SCIdx = Pos->second;
Andrew Trick76686492012-09-15 00:19:57 +0000701 unsigned CIdx = 0, CEnd = ClassInstrs.size();
702 for (; CIdx != CEnd; ++CIdx) {
703 if (ClassInstrs[CIdx].first == SCIdx)
704 break;
705 }
706 if (CIdx == CEnd) {
707 ClassInstrs.resize(CEnd + 1);
708 ClassInstrs[CIdx].first = SCIdx;
709 }
710 ClassInstrs[CIdx].second.push_back(*I);
711 }
712 // For each set of Instrs, create a new class if necessary, and map or remap
713 // the Instrs to it.
714 unsigned CIdx = 0, CEnd = ClassInstrs.size();
715 for (; CIdx != CEnd; ++CIdx) {
716 unsigned OldSCIdx = ClassInstrs[CIdx].first;
717 ArrayRef<Record*> InstDefs = ClassInstrs[CIdx].second;
718 // If the all instrs in the current class are accounted for, then leave
719 // them mapped to their old class.
Andrew Trick78a08512013-06-05 06:55:20 +0000720 if (OldSCIdx) {
721 const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
722 if (!RWDefs.empty()) {
723 const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
724 unsigned OrigNumInstrs = 0;
Javed Absar67b042c2017-09-13 10:31:10 +0000725 for (Record *OIDef : make_range(OrigInstDefs->begin(), OrigInstDefs->end())) {
726 if (InstrClassMap[OIDef] == OldSCIdx)
Andrew Trick78a08512013-06-05 06:55:20 +0000727 ++OrigNumInstrs;
728 }
729 if (OrigNumInstrs == InstDefs.size()) {
730 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
731 "expected a generic SchedClass");
732 DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
733 << SchedClasses[OldSCIdx].Name << " on "
734 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
735 SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
736 continue;
737 }
738 }
Andrew Trick76686492012-09-15 00:19:57 +0000739 }
740 unsigned SCIdx = SchedClasses.size();
741 SchedClasses.resize(SCIdx+1);
742 CodeGenSchedClass &SC = SchedClasses.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000743 SC.Index = SCIdx;
Andrew Trick76686492012-09-15 00:19:57 +0000744 SC.Name = createSchedClassName(InstDefs);
Andrew Trick78a08512013-06-05 06:55:20 +0000745 DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
746 << InstRWDef->getValueAsDef("SchedModel")->getName() << "\n");
747
Andrew Trick76686492012-09-15 00:19:57 +0000748 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
749 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
750 SC.Writes = SchedClasses[OldSCIdx].Writes;
751 SC.Reads = SchedClasses[OldSCIdx].Reads;
752 SC.ProcIndices.push_back(0);
753 // Map each Instr to this new class.
754 // Note that InstDefs may be a smaller list than InstRWDef's "Instrs".
Andrew Trick9e1deb62012-10-03 23:06:32 +0000755 Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
756 SmallSet<unsigned, 4> RemappedClassIDs;
Andrew Trick76686492012-09-15 00:19:57 +0000757 for (ArrayRef<Record*>::const_iterator
758 II = InstDefs.begin(), IE = InstDefs.end(); II != IE; ++II) {
759 unsigned OldSCIdx = InstrClassMap[*II];
David Blaikie70573dc2014-11-19 07:49:26 +0000760 if (OldSCIdx && RemappedClassIDs.insert(OldSCIdx).second) {
Andrew Trick9e1deb62012-10-03 23:06:32 +0000761 for (RecIter RI = SchedClasses[OldSCIdx].InstRWs.begin(),
762 RE = SchedClasses[OldSCIdx].InstRWs.end(); RI != RE; ++RI) {
763 if ((*RI)->getValueAsDef("SchedModel") == RWModelDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000764 PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
Andrew Trick9e1deb62012-10-03 23:06:32 +0000765 (*II)->getName() + " also matches " +
766 (*RI)->getValue("Instrs")->getValue()->getAsString());
767 }
768 assert(*RI != InstRWDef && "SchedClass has duplicate InstRW def");
769 SC.InstRWs.push_back(*RI);
770 }
Andrew Trick76686492012-09-15 00:19:57 +0000771 }
772 InstrClassMap[*II] = SCIdx;
773 }
774 SC.InstRWs.push_back(InstRWDef);
775 }
Andrew Trick87255e32012-07-07 04:00:00 +0000776}
777
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000778// True if collectProcItins found anything.
779bool CodeGenSchedModels::hasItineraries() const {
Javed Absar67b042c2017-09-13 10:31:10 +0000780 for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd())) {
781 if (PM.hasItineraries())
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000782 return true;
783 }
784 return false;
785}
786
Andrew Trick87255e32012-07-07 04:00:00 +0000787// Gather the processor itineraries.
Andrew Trick76686492012-09-15 00:19:57 +0000788void CodeGenSchedModels::collectProcItins() {
Joel Jones80372332017-06-28 00:06:40 +0000789 DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
Craig Topper8a417c12014-12-09 08:05:51 +0000790 for (CodeGenProcModel &ProcModel : ProcModels) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000791 if (!ProcModel.hasItineraries())
Andrew Trick87255e32012-07-07 04:00:00 +0000792 continue;
Andrew Trick76686492012-09-15 00:19:57 +0000793
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000794 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
795 assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
796
797 // Populate ItinDefList with Itinerary records.
798 ProcModel.ItinDefList.resize(NumInstrSchedClasses);
Andrew Trick76686492012-09-15 00:19:57 +0000799
800 // Insert each itinerary data record in the correct position within
801 // the processor model's ItinDefList.
802 for (unsigned i = 0, N = ItinRecords.size(); i < N; i++) {
803 Record *ItinData = ItinRecords[i];
804 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000805 bool FoundClass = false;
806 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
807 SCI != SCE; ++SCI) {
808 // Multiple SchedClasses may share an itinerary. Update all of them.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000809 if (SCI->ItinClassDef == ItinDef) {
810 ProcModel.ItinDefList[SCI->Index] = ItinData;
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000811 FoundClass = true;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000812 }
Andrew Trick76686492012-09-15 00:19:57 +0000813 }
Andrew Tricke7bac5f2013-03-18 20:42:25 +0000814 if (!FoundClass) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000815 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
816 << " missing class for itinerary " << ItinDef->getName() << '\n');
817 }
Andrew Trick87255e32012-07-07 04:00:00 +0000818 }
Andrew Trick76686492012-09-15 00:19:57 +0000819 // Check for missing itinerary entries.
820 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
821 DEBUG(
822 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
823 if (!ProcModel.ItinDefList[i])
824 dbgs() << ProcModel.ItinsDef->getName()
825 << " missing itinerary for class "
826 << SchedClasses[i].Name << '\n';
827 });
Andrew Trick87255e32012-07-07 04:00:00 +0000828 }
Andrew Trick87255e32012-07-07 04:00:00 +0000829}
Andrew Trick76686492012-09-15 00:19:57 +0000830
831// Gather the read/write types for each itinerary class.
832void CodeGenSchedModels::collectProcItinRW() {
833 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
834 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
835 for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) {
836 if (!(*II)->getValueInit("SchedModel")->isComplete())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000837 PrintFatalError((*II)->getLoc(), "SchedModel is undefined");
Andrew Trick76686492012-09-15 00:19:57 +0000838 Record *ModelDef = (*II)->getValueAsDef("SchedModel");
839 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
840 if (I == ProcModelMap.end()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000841 PrintFatalError((*II)->getLoc(), "Undefined SchedMachineModel "
Andrew Trick76686492012-09-15 00:19:57 +0000842 + ModelDef->getName());
843 }
844 ProcModels[I->second].ItinRWDefs.push_back(*II);
845 }
846}
847
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000848// Gather the unsupported features for processor models.
849void CodeGenSchedModels::collectProcUnsupportedFeatures() {
850 for (CodeGenProcModel &ProcModel : ProcModels) {
851 for (Record *Pred : ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures")) {
852 ProcModel.UnsupportedFeaturesDefs.push_back(Pred);
853 }
854 }
855}
856
Andrew Trick33401e82012-09-15 00:19:59 +0000857/// Infer new classes from existing classes. In the process, this may create new
858/// SchedWrites from sequences of existing SchedWrites.
859void CodeGenSchedModels::inferSchedClasses() {
Joel Jones80372332017-06-28 00:06:40 +0000860 DEBUG(dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000861 DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
862
Andrew Trick33401e82012-09-15 00:19:59 +0000863 // Visit all existing classes and newly created classes.
864 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000865 assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
866
Andrew Trick33401e82012-09-15 00:19:59 +0000867 if (SchedClasses[Idx].ItinClassDef)
868 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000869 if (!SchedClasses[Idx].InstRWs.empty())
Andrew Trick33401e82012-09-15 00:19:59 +0000870 inferFromInstRWs(Idx);
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000871 if (!SchedClasses[Idx].Writes.empty()) {
Andrew Trick33401e82012-09-15 00:19:59 +0000872 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
873 Idx, SchedClasses[Idx].ProcIndices);
874 }
875 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
876 "too many SchedVariants");
877 }
878}
879
880/// Infer classes from per-processor itinerary resources.
881void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
882 unsigned FromClassIdx) {
883 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
884 const CodeGenProcModel &PM = ProcModels[PIdx];
885 // For all ItinRW entries.
886 bool HasMatch = false;
887 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
888 II != IE; ++II) {
889 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
890 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
891 continue;
892 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000893 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
Andrew Trick33401e82012-09-15 00:19:59 +0000894 + ItinClassDef->getName()
895 + " in ItinResources for " + PM.ModelName);
896 HasMatch = true;
897 IdxVec Writes, Reads;
898 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
899 IdxVec ProcIndices(1, PIdx);
900 inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
901 }
902 }
903}
904
905/// Infer classes from per-processor InstReadWrite definitions.
906void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000907 for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
Benjamin Kramerb22643a2013-06-10 20:19:35 +0000908 assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000909 Record *Rec = SchedClasses[SCIdx].InstRWs[I];
910 const RecVec *InstDefs = Sets.expand(Rec);
Andrew Trick9e1deb62012-10-03 23:06:32 +0000911 RecIter II = InstDefs->begin(), IE = InstDefs->end();
Andrew Trick33401e82012-09-15 00:19:59 +0000912 for (; II != IE; ++II) {
913 if (InstrClassMap[*II] == SCIdx)
914 break;
915 }
916 // If this class no longer has any instructions mapped to it, it has become
917 // irrelevant.
918 if (II == IE)
919 continue;
920 IdxVec Writes, Reads;
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000921 findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
922 unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
Andrew Trick33401e82012-09-15 00:19:59 +0000923 IdxVec ProcIndices(1, PIdx);
Benjamin Kramer58bd79c2013-06-09 15:20:23 +0000924 inferFromRW(Writes, Reads, SCIdx, ProcIndices); // May mutate SchedClasses.
Andrew Trick33401e82012-09-15 00:19:59 +0000925 }
926}
927
928namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000929
Andrew Trick9257b8f2012-09-22 02:24:21 +0000930// Helper for substituteVariantOperand.
931struct TransVariant {
Andrew Trickda984b12012-10-03 23:06:28 +0000932 Record *VarOrSeqDef; // Variant or sequence.
933 unsigned RWIdx; // Index of this variant or sequence's matched type.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000934 unsigned ProcIdx; // Processor model index or zero for any.
935 unsigned TransVecIdx; // Index into PredTransitions::TransVec.
936
937 TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
Andrew Trickda984b12012-10-03 23:06:28 +0000938 VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
Andrew Trick9257b8f2012-09-22 02:24:21 +0000939};
940
Andrew Trick33401e82012-09-15 00:19:59 +0000941// Associate a predicate with the SchedReadWrite that it guards.
942// RWIdx is the index of the read/write variant.
943struct PredCheck {
944 bool IsRead;
945 unsigned RWIdx;
946 Record *Predicate;
947
948 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
949};
950
951// A Predicate transition is a list of RW sequences guarded by a PredTerm.
952struct PredTransition {
953 // A predicate term is a conjunction of PredChecks.
954 SmallVector<PredCheck, 4> PredTerm;
955 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
956 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000957 SmallVector<unsigned, 4> ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +0000958};
959
960// Encapsulate a set of partially constructed transitions.
961// The results are built by repeated calls to substituteVariants.
962class PredTransitions {
963 CodeGenSchedModels &SchedModels;
964
965public:
966 std::vector<PredTransition> TransVec;
967
968 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
969
970 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
971 bool IsRead, unsigned StartIdx);
972
973 void substituteVariants(const PredTransition &Trans);
974
975#ifndef NDEBUG
976 void dump() const;
977#endif
978
979private:
980 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
Andrew Trickda984b12012-10-03 23:06:28 +0000981 void getIntersectingVariants(
982 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
983 std::vector<TransVariant> &IntersectingVariants);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000984 void pushVariant(const TransVariant &VInfo, bool IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +0000985};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000986
987} // end anonymous namespace
Andrew Trick33401e82012-09-15 00:19:59 +0000988
989// Return true if this predicate is mutually exclusive with a PredTerm. This
990// degenerates into checking if the predicate is mutually exclusive with any
991// predicate in the Term's conjunction.
992//
993// All predicates associated with a given SchedRW are considered mutually
994// exclusive. This should work even if the conditions expressed by the
995// predicates are not exclusive because the predicates for a given SchedWrite
996// are always checked in the order they are defined in the .td file. Later
997// conditions implicitly negate any prior condition.
998bool PredTransitions::mutuallyExclusive(Record *PredDef,
999 ArrayRef<PredCheck> Term) {
Andrew Trick33401e82012-09-15 00:19:59 +00001000 for (ArrayRef<PredCheck>::iterator I = Term.begin(), E = Term.end();
1001 I != E; ++I) {
1002 if (I->Predicate == PredDef)
1003 return false;
1004
1005 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(I->RWIdx, I->IsRead);
1006 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
1007 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1008 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
1009 if ((*VI)->getValueAsDef("Predicate") == PredDef)
1010 return true;
1011 }
1012 }
1013 return false;
1014}
1015
Andrew Trickda984b12012-10-03 23:06:28 +00001016static bool hasAliasedVariants(const CodeGenSchedRW &RW,
1017 CodeGenSchedModels &SchedModels) {
1018 if (RW.HasVariants)
1019 return true;
1020
1021 for (RecIter I = RW.Aliases.begin(), E = RW.Aliases.end(); I != E; ++I) {
1022 const CodeGenSchedRW &AliasRW =
1023 SchedModels.getSchedRW((*I)->getValueAsDef("AliasRW"));
1024 if (AliasRW.HasVariants)
1025 return true;
1026 if (AliasRW.IsSequence) {
1027 IdxVec ExpandedRWs;
1028 SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
1029 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1030 SI != SE; ++SI) {
1031 if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
1032 SchedModels)) {
1033 return true;
1034 }
1035 }
1036 }
1037 }
1038 return false;
1039}
1040
1041static bool hasVariant(ArrayRef<PredTransition> Transitions,
1042 CodeGenSchedModels &SchedModels) {
1043 for (ArrayRef<PredTransition>::iterator
1044 PTI = Transitions.begin(), PTE = Transitions.end();
1045 PTI != PTE; ++PTI) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001046 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001047 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
1048 WSI != WSE; ++WSI) {
1049 for (SmallVectorImpl<unsigned>::const_iterator
1050 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1051 if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
1052 return true;
1053 }
1054 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001055 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trickda984b12012-10-03 23:06:28 +00001056 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
1057 RSI != RSE; ++RSI) {
1058 for (SmallVectorImpl<unsigned>::const_iterator
1059 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
1060 if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
1061 return true;
1062 }
1063 }
1064 }
1065 return false;
1066}
1067
1068// Populate IntersectingVariants with any variants or aliased sequences of the
1069// given SchedRW whose processor indices and predicates are not mutually
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001070// exclusive with the given transition.
Andrew Trickda984b12012-10-03 23:06:28 +00001071void PredTransitions::getIntersectingVariants(
1072 const CodeGenSchedRW &SchedRW, unsigned TransIdx,
1073 std::vector<TransVariant> &IntersectingVariants) {
1074
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001075 bool GenericRW = false;
1076
Andrew Trickda984b12012-10-03 23:06:28 +00001077 std::vector<TransVariant> Variants;
1078 if (SchedRW.HasVariants) {
1079 unsigned VarProcIdx = 0;
1080 if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1081 Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1082 VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
1083 }
1084 // Push each variant. Assign TransVecIdx later.
1085 const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
1086 for (RecIter RI = VarDefs.begin(), RE = VarDefs.end(); RI != RE; ++RI)
1087 Variants.push_back(TransVariant(*RI, SchedRW.Index, VarProcIdx, 0));
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001088 if (VarProcIdx == 0)
1089 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001090 }
1091 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1092 AI != AE; ++AI) {
1093 // If either the SchedAlias itself or the SchedReadWrite that it aliases
1094 // to is defined within a processor model, constrain all variants to
1095 // that processor.
1096 unsigned AliasProcIdx = 0;
1097 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1098 Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1099 AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
1100 }
1101 const CodeGenSchedRW &AliasRW =
1102 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
1103
1104 if (AliasRW.HasVariants) {
1105 const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
1106 for (RecIter RI = VarDefs.begin(), RE = VarDefs.end(); RI != RE; ++RI)
1107 Variants.push_back(TransVariant(*RI, AliasRW.Index, AliasProcIdx, 0));
1108 }
1109 if (AliasRW.IsSequence) {
1110 Variants.push_back(
1111 TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
1112 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001113 if (AliasProcIdx == 0)
1114 GenericRW = true;
Andrew Trickda984b12012-10-03 23:06:28 +00001115 }
1116 for (unsigned VIdx = 0, VEnd = Variants.size(); VIdx != VEnd; ++VIdx) {
1117 TransVariant &Variant = Variants[VIdx];
1118 // Don't expand variants if the processor models don't intersect.
1119 // A zero processor index means any processor.
Craig Topperb94011f2013-07-14 04:42:23 +00001120 SmallVectorImpl<unsigned> &ProcIndices = TransVec[TransIdx].ProcIndices;
Andrew Trickda984b12012-10-03 23:06:28 +00001121 if (ProcIndices[0] && Variants[VIdx].ProcIdx) {
1122 unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
1123 Variant.ProcIdx);
1124 if (!Cnt)
1125 continue;
1126 if (Cnt > 1) {
1127 const CodeGenProcModel &PM =
1128 *(SchedModels.procModelBegin() + Variant.ProcIdx);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001129 PrintFatalError(Variant.VarOrSeqDef->getLoc(),
1130 "Multiple variants defined for processor " +
1131 PM.ModelName +
1132 " Ensure only one SchedAlias exists per RW.");
Andrew Trickda984b12012-10-03 23:06:28 +00001133 }
1134 }
1135 if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1136 Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1137 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
1138 continue;
1139 }
1140 if (IntersectingVariants.empty()) {
1141 // The first variant builds on the existing transition.
1142 Variant.TransVecIdx = TransIdx;
1143 IntersectingVariants.push_back(Variant);
1144 }
1145 else {
1146 // Push another copy of the current transition for more variants.
1147 Variant.TransVecIdx = TransVec.size();
1148 IntersectingVariants.push_back(Variant);
Dan Gohmanf6169d02013-03-29 00:13:08 +00001149 TransVec.push_back(TransVec[TransIdx]);
Andrew Trickda984b12012-10-03 23:06:28 +00001150 }
1151 }
Andrew Trickd97ff1f2013-03-29 19:08:31 +00001152 if (GenericRW && IntersectingVariants.empty()) {
1153 PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1154 "a matching predicate on any processor");
1155 }
Andrew Trickda984b12012-10-03 23:06:28 +00001156}
1157
Andrew Trick9257b8f2012-09-22 02:24:21 +00001158// Push the Reads/Writes selected by this variant onto the PredTransition
1159// specified by VInfo.
1160void PredTransitions::
1161pushVariant(const TransVariant &VInfo, bool IsRead) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001162 PredTransition &Trans = TransVec[VInfo.TransVecIdx];
1163
Andrew Trick9257b8f2012-09-22 02:24:21 +00001164 // If this operand transition is reached through a processor-specific alias,
1165 // then the whole transition is specific to this processor.
1166 if (VInfo.ProcIdx != 0)
1167 Trans.ProcIndices.assign(1, VInfo.ProcIdx);
1168
Andrew Trick33401e82012-09-15 00:19:59 +00001169 IdxVec SelectedRWs;
Andrew Trickda984b12012-10-03 23:06:28 +00001170 if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1171 Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1172 Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
1173 RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
1174 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
1175 }
1176 else {
1177 assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
1178 "variant must be a SchedVariant or aliased WriteSequence");
1179 SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
1180 }
Andrew Trick33401e82012-09-15 00:19:59 +00001181
Andrew Trick9257b8f2012-09-22 02:24:21 +00001182 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
Andrew Trick33401e82012-09-15 00:19:59 +00001183
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001184 SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
Andrew Trick33401e82012-09-15 00:19:59 +00001185 ? Trans.ReadSequences : Trans.WriteSequences;
1186 if (SchedRW.IsVariadic) {
1187 unsigned OperIdx = RWSequences.size()-1;
1188 // Make N-1 copies of this transition's last sequence.
1189 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
Arnold Schwaighofer3bd25242013-06-06 23:23:14 +00001190 // Create a temporary copy the vector could reallocate.
Arnold Schwaighoferf84a03a2013-06-07 00:04:30 +00001191 RWSequences.reserve(RWSequences.size() + 1);
1192 RWSequences.push_back(RWSequences[OperIdx]);
Andrew Trick33401e82012-09-15 00:19:59 +00001193 }
1194 // Push each of the N elements of the SelectedRWs onto a copy of the last
1195 // sequence (split the current operand into N operands).
1196 // Note that write sequences should be expanded within this loop--the entire
1197 // sequence belongs to a single operand.
1198 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1199 RWI != RWE; ++RWI, ++OperIdx) {
1200 IdxVec ExpandedRWs;
1201 if (IsRead)
1202 ExpandedRWs.push_back(*RWI);
1203 else
1204 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1205 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
1206 ExpandedRWs.begin(), ExpandedRWs.end());
1207 }
1208 assert(OperIdx == RWSequences.size() && "missed a sequence");
1209 }
1210 else {
1211 // Push this transition's expanded sequence onto this transition's last
1212 // sequence (add to the current operand's sequence).
1213 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
1214 IdxVec ExpandedRWs;
1215 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
1216 RWI != RWE; ++RWI) {
1217 if (IsRead)
1218 ExpandedRWs.push_back(*RWI);
1219 else
1220 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
1221 }
1222 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
1223 }
1224}
1225
1226// RWSeq is a sequence of all Reads or all Writes for the next read or write
1227// operand. StartIdx is an index into TransVec where partial results
Andrew Trick9257b8f2012-09-22 02:24:21 +00001228// starts. RWSeq must be applied to all transitions between StartIdx and the end
Andrew Trick33401e82012-09-15 00:19:59 +00001229// of TransVec.
1230void PredTransitions::substituteVariantOperand(
1231 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
1232
1233 // Visit each original RW within the current sequence.
1234 for (SmallVectorImpl<unsigned>::const_iterator
1235 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
1236 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
1237 // Push this RW on all partial PredTransitions or distribute variants.
1238 // New PredTransitions may be pushed within this loop which should not be
1239 // revisited (TransEnd must be loop invariant).
1240 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
1241 TransIdx != TransEnd; ++TransIdx) {
1242 // In the common case, push RW onto the current operand's sequence.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001243 if (!hasAliasedVariants(SchedRW, SchedModels)) {
Andrew Trick33401e82012-09-15 00:19:59 +00001244 if (IsRead)
1245 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
1246 else
1247 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
1248 continue;
1249 }
1250 // Distribute this partial PredTransition across intersecting variants.
Andrew Trickda984b12012-10-03 23:06:28 +00001251 // This will push a copies of TransVec[TransIdx] on the back of TransVec.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001252 std::vector<TransVariant> IntersectingVariants;
Andrew Trickda984b12012-10-03 23:06:28 +00001253 getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
Andrew Trick33401e82012-09-15 00:19:59 +00001254 // Now expand each variant on top of its copy of the transition.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001255 for (std::vector<TransVariant>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001256 IVI = IntersectingVariants.begin(),
1257 IVE = IntersectingVariants.end();
Andrew Trick9257b8f2012-09-22 02:24:21 +00001258 IVI != IVE; ++IVI) {
1259 pushVariant(*IVI, IsRead);
1260 }
Andrew Trick33401e82012-09-15 00:19:59 +00001261 }
1262 }
1263}
1264
1265// For each variant of a Read/Write in Trans, substitute the sequence of
1266// Read/Writes guarded by the variant. This is exponential in the number of
1267// variant Read/Writes, but in practice detection of mutually exclusive
1268// predicates should result in linear growth in the total number variants.
1269//
1270// This is one step in a breadth-first search of nested variants.
1271void PredTransitions::substituteVariants(const PredTransition &Trans) {
1272 // Build up a set of partial results starting at the back of
1273 // PredTransitions. Remember the first new transition.
1274 unsigned StartIdx = TransVec.size();
1275 TransVec.resize(TransVec.size() + 1);
1276 TransVec.back().PredTerm = Trans.PredTerm;
Andrew Trick9257b8f2012-09-22 02:24:21 +00001277 TransVec.back().ProcIndices = Trans.ProcIndices;
Andrew Trick33401e82012-09-15 00:19:59 +00001278
1279 // Visit each original write sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001280 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001281 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
1282 WSI != WSE; ++WSI) {
1283 // Push a new (empty) write sequence onto all partial Transitions.
1284 for (std::vector<PredTransition>::iterator I =
1285 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
1286 I->WriteSequences.resize(I->WriteSequences.size() + 1);
1287 }
1288 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
1289 }
1290 // Visit each original read sequence.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001291 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001292 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
1293 RSI != RSE; ++RSI) {
1294 // Push a new (empty) read sequence onto all partial Transitions.
1295 for (std::vector<PredTransition>::iterator I =
1296 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
1297 I->ReadSequences.resize(I->ReadSequences.size() + 1);
1298 }
1299 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
1300 }
1301}
1302
Andrew Trick33401e82012-09-15 00:19:59 +00001303// Create a new SchedClass for each variant found by inferFromRW. Pass
Andrew Trick33401e82012-09-15 00:19:59 +00001304static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
Andrew Trick9257b8f2012-09-22 02:24:21 +00001305 unsigned FromClassIdx,
Andrew Trick33401e82012-09-15 00:19:59 +00001306 CodeGenSchedModels &SchedModels) {
1307 // For each PredTransition, create a new CodeGenSchedTransition, which usually
1308 // requires creating a new SchedClass.
1309 for (ArrayRef<PredTransition>::iterator
1310 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
1311 IdxVec OperWritesVariant;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001312 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001313 WSI = I->WriteSequences.begin(), WSE = I->WriteSequences.end();
1314 WSI != WSE; ++WSI) {
1315 // Create a new write representing the expanded sequence.
1316 OperWritesVariant.push_back(
1317 SchedModels.findOrInsertRW(*WSI, /*IsRead=*/false));
1318 }
1319 IdxVec OperReadsVariant;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001320 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001321 RSI = I->ReadSequences.begin(), RSE = I->ReadSequences.end();
1322 RSI != RSE; ++RSI) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001323 // Create a new read representing the expanded sequence.
Andrew Trick33401e82012-09-15 00:19:59 +00001324 OperReadsVariant.push_back(
1325 SchedModels.findOrInsertRW(*RSI, /*IsRead=*/true));
1326 }
Andrew Trick9257b8f2012-09-22 02:24:21 +00001327 IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end());
Andrew Trick33401e82012-09-15 00:19:59 +00001328 CodeGenSchedTransition SCTrans;
1329 SCTrans.ToClassIdx =
Craig Topper24064772014-04-15 07:20:03 +00001330 SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001331 OperReadsVariant, ProcIndices);
Andrew Trick33401e82012-09-15 00:19:59 +00001332 SCTrans.ProcIndices = ProcIndices;
1333 // The final PredTerm is unique set of predicates guarding the transition.
1334 RecVec Preds;
1335 for (SmallVectorImpl<PredCheck>::const_iterator
1336 PI = I->PredTerm.begin(), PE = I->PredTerm.end(); PI != PE; ++PI) {
1337 Preds.push_back(PI->Predicate);
1338 }
1339 RecIter PredsEnd = std::unique(Preds.begin(), Preds.end());
1340 Preds.resize(PredsEnd - Preds.begin());
1341 SCTrans.PredTerm = Preds;
1342 SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
1343 }
1344}
1345
Andrew Trick9257b8f2012-09-22 02:24:21 +00001346// Create new SchedClasses for the given ReadWrite list. If any of the
1347// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
1348// of the ReadWrite list, following Aliases if necessary.
Benjamin Kramere1761952015-10-24 12:46:49 +00001349void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
1350 ArrayRef<unsigned> OperReads,
Andrew Trick33401e82012-09-15 00:19:59 +00001351 unsigned FromClassIdx,
Benjamin Kramere1761952015-10-24 12:46:49 +00001352 ArrayRef<unsigned> ProcIndices) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001353 DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
Andrew Trick33401e82012-09-15 00:19:59 +00001354
1355 // Create a seed transition with an empty PredTerm and the expanded sequences
1356 // of SchedWrites for the current SchedClass.
1357 std::vector<PredTransition> LastTransitions;
1358 LastTransitions.resize(1);
Andrew Trick9257b8f2012-09-22 02:24:21 +00001359 LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
1360 ProcIndices.end());
1361
Benjamin Kramere1761952015-10-24 12:46:49 +00001362 for (unsigned WriteIdx : OperWrites) {
Andrew Trick33401e82012-09-15 00:19:59 +00001363 IdxVec WriteSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001364 expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
Andrew Trick33401e82012-09-15 00:19:59 +00001365 unsigned Idx = LastTransitions[0].WriteSequences.size();
1366 LastTransitions[0].WriteSequences.resize(Idx + 1);
1367 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences[Idx];
1368 for (IdxIter WI = WriteSeq.begin(), WE = WriteSeq.end(); WI != WE; ++WI)
1369 Seq.push_back(*WI);
1370 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1371 }
1372 DEBUG(dbgs() << " Reads: ");
Benjamin Kramere1761952015-10-24 12:46:49 +00001373 for (unsigned ReadIdx : OperReads) {
Andrew Trick33401e82012-09-15 00:19:59 +00001374 IdxVec ReadSeq;
Benjamin Kramere1761952015-10-24 12:46:49 +00001375 expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
Andrew Trick33401e82012-09-15 00:19:59 +00001376 unsigned Idx = LastTransitions[0].ReadSequences.size();
1377 LastTransitions[0].ReadSequences.resize(Idx + 1);
1378 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences[Idx];
1379 for (IdxIter RI = ReadSeq.begin(), RE = ReadSeq.end(); RI != RE; ++RI)
1380 Seq.push_back(*RI);
1381 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1382 }
1383 DEBUG(dbgs() << '\n');
1384
1385 // Collect all PredTransitions for individual operands.
1386 // Iterate until no variant writes remain.
1387 while (hasVariant(LastTransitions, *this)) {
1388 PredTransitions Transitions(*this);
1389 for (std::vector<PredTransition>::const_iterator
1390 I = LastTransitions.begin(), E = LastTransitions.end();
1391 I != E; ++I) {
1392 Transitions.substituteVariants(*I);
1393 }
1394 DEBUG(Transitions.dump());
1395 LastTransitions.swap(Transitions.TransVec);
1396 }
1397 // If the first transition has no variants, nothing to do.
1398 if (LastTransitions[0].PredTerm.empty())
1399 return;
1400
1401 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1402 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
Andrew Trick9257b8f2012-09-22 02:24:21 +00001403 inferFromTransitions(LastTransitions, FromClassIdx, *this);
Andrew Trick33401e82012-09-15 00:19:59 +00001404}
1405
Andrew Trickcf398b22013-04-23 23:45:14 +00001406// Check if any processor resource group contains all resource records in
1407// SubUnits.
1408bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1409 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1410 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1411 continue;
1412 RecVec SuperUnits =
1413 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1414 RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1415 for ( ; RI != RE; ++RI) {
David Majnemer0d955d02016-08-11 22:21:41 +00001416 if (!is_contained(SuperUnits, *RI)) {
Andrew Trickcf398b22013-04-23 23:45:14 +00001417 break;
1418 }
1419 }
1420 if (RI == RE)
1421 return true;
1422 }
1423 return false;
1424}
1425
1426// Verify that overlapping groups have a common supergroup.
1427void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1428 for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1429 if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1430 continue;
1431 RecVec CheckUnits =
1432 PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1433 for (unsigned j = i+1; j < e; ++j) {
1434 if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1435 continue;
1436 RecVec OtherUnits =
1437 PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1438 if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1439 OtherUnits.begin(), OtherUnits.end())
1440 != CheckUnits.end()) {
1441 // CheckUnits and OtherUnits overlap
1442 OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1443 CheckUnits.end());
1444 if (!hasSuperGroup(OtherUnits, PM)) {
1445 PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1446 "proc resource group overlaps with "
1447 + PM.ProcResourceDefs[j]->getName()
1448 + " but no supergroup contains both.");
1449 }
1450 }
1451 }
1452 }
1453}
1454
Andrew Trick1e46d482012-09-15 00:20:02 +00001455// Collect and sort WriteRes, ReadAdvance, and ProcResources.
1456void CodeGenSchedModels::collectProcResources() {
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001457 ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
1458 ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1459
Andrew Trick1e46d482012-09-15 00:20:02 +00001460 // Add any subtarget-specific SchedReadWrites that are directly associated
1461 // with processor resources. Refer to the parent SchedClass's ProcIndices to
1462 // determine which processors they apply to.
1463 for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
1464 SCI != SCE; ++SCI) {
1465 if (SCI->ItinClassDef)
1466 collectItinProcResources(SCI->ItinClassDef);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001467 else {
1468 // This class may have a default ReadWrite list which can be overriden by
1469 // InstRW definitions.
1470 if (!SCI->InstRWs.empty()) {
1471 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1472 RWI != RWE; ++RWI) {
1473 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
1474 IdxVec ProcIndices(1, getProcModel(RWModelDef).Index);
1475 IdxVec Writes, Reads;
1476 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1477 Writes, Reads);
1478 collectRWResources(Writes, Reads, ProcIndices);
1479 }
1480 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001481 collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
Andrew Trick4fe440d2013-02-01 03:19:54 +00001482 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001483 }
1484 // Add resources separately defined by each subtarget.
1485 RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
1486 for (RecIter WRI = WRDefs.begin(), WRE = WRDefs.end(); WRI != WRE; ++WRI) {
1487 Record *ModelDef = (*WRI)->getValueAsDef("SchedModel");
1488 addWriteRes(*WRI, getProcModel(ModelDef).Index);
1489 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001490 RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
1491 for (RecIter WRI = SWRDefs.begin(), WRE = SWRDefs.end(); WRI != WRE; ++WRI) {
1492 Record *ModelDef = (*WRI)->getValueAsDef("SchedModel");
1493 addWriteRes(*WRI, getProcModel(ModelDef).Index);
1494 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001495 RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
1496 for (RecIter RAI = RADefs.begin(), RAE = RADefs.end(); RAI != RAE; ++RAI) {
1497 Record *ModelDef = (*RAI)->getValueAsDef("SchedModel");
1498 addReadAdvance(*RAI, getProcModel(ModelDef).Index);
1499 }
Andrew Trickdca870b2014-03-13 03:49:20 +00001500 RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
1501 for (RecIter RAI = SRADefs.begin(), RAE = SRADefs.end(); RAI != RAE; ++RAI) {
1502 if ((*RAI)->getValueInit("SchedModel")->isComplete()) {
1503 Record *ModelDef = (*RAI)->getValueAsDef("SchedModel");
1504 addReadAdvance(*RAI, getProcModel(ModelDef).Index);
1505 }
1506 }
Andrew Trick40c4f382013-06-15 04:50:06 +00001507 // Add ProcResGroups that are defined within this processor model, which may
1508 // not be directly referenced but may directly specify a buffer size.
1509 RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
1510 for (RecIter RI = ProcResGroups.begin(), RE = ProcResGroups.end();
1511 RI != RE; ++RI) {
1512 if (!(*RI)->getValueInit("SchedModel")->isComplete())
1513 continue;
1514 CodeGenProcModel &PM = getProcModel((*RI)->getValueAsDef("SchedModel"));
David Majnemer42531262016-08-12 03:55:06 +00001515 if (!is_contained(PM.ProcResourceDefs, *RI))
Andrew Trick40c4f382013-06-15 04:50:06 +00001516 PM.ProcResourceDefs.push_back(*RI);
1517 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001518 // Finalize each ProcModel by sorting the record arrays.
Craig Topper8a417c12014-12-09 08:05:51 +00001519 for (CodeGenProcModel &PM : ProcModels) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001520 std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
1521 LessRecord());
1522 std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
1523 LessRecord());
1524 std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
1525 LessRecord());
1526 DEBUG(
1527 PM.dump();
1528 dbgs() << "WriteResDefs: ";
1529 for (RecIter RI = PM.WriteResDefs.begin(),
1530 RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
1531 if ((*RI)->isSubClassOf("WriteRes"))
1532 dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
1533 else
1534 dbgs() << (*RI)->getName() << " ";
1535 }
1536 dbgs() << "\nReadAdvanceDefs: ";
1537 for (RecIter RI = PM.ReadAdvanceDefs.begin(),
1538 RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
1539 if ((*RI)->isSubClassOf("ReadAdvance"))
1540 dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
1541 else
1542 dbgs() << (*RI)->getName() << " ";
1543 }
1544 dbgs() << "\nProcResourceDefs: ";
1545 for (RecIter RI = PM.ProcResourceDefs.begin(),
1546 RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
1547 dbgs() << (*RI)->getName() << " ";
1548 }
1549 dbgs() << '\n');
Andrew Trickcf398b22013-04-23 23:45:14 +00001550 verifyProcResourceGroups(PM);
Andrew Trick1e46d482012-09-15 00:20:02 +00001551 }
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001552
1553 ProcResourceDefs.clear();
1554 ProcResGroups.clear();
Andrew Trick1e46d482012-09-15 00:20:02 +00001555}
1556
Matthias Braun17cb5792016-03-01 20:03:21 +00001557void CodeGenSchedModels::checkCompleteness() {
1558 bool Complete = true;
1559 bool HadCompleteModel = false;
1560 for (const CodeGenProcModel &ProcModel : procModels()) {
Matthias Braun17cb5792016-03-01 20:03:21 +00001561 if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
1562 continue;
1563 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1564 if (Inst->hasNoSchedulingInfo)
1565 continue;
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001566 if (ProcModel.isUnsupported(*Inst))
1567 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001568 unsigned SCIdx = getSchedClassIdx(*Inst);
1569 if (!SCIdx) {
1570 if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
1571 PrintError("No schedule information for instruction '"
1572 + Inst->TheDef->getName() + "'");
1573 Complete = false;
1574 }
1575 continue;
1576 }
1577
1578 const CodeGenSchedClass &SC = getSchedClass(SCIdx);
1579 if (!SC.Writes.empty())
1580 continue;
Ulrich Weigand75cda2f2016-10-31 18:59:52 +00001581 if (SC.ItinClassDef != nullptr &&
1582 SC.ItinClassDef->getName() != "NoItinerary")
Matthias Braun42d9ad92016-03-03 00:04:59 +00001583 continue;
Matthias Braun17cb5792016-03-01 20:03:21 +00001584
1585 const RecVec &InstRWs = SC.InstRWs;
David Majnemer562e8292016-08-12 00:18:03 +00001586 auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
1587 return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
1588 });
Matthias Braun17cb5792016-03-01 20:03:21 +00001589 if (I == InstRWs.end()) {
1590 PrintError("'" + ProcModel.ModelName + "' lacks information for '" +
1591 Inst->TheDef->getName() + "'");
1592 Complete = false;
1593 }
1594 }
1595 HadCompleteModel = true;
1596 }
Matthias Brauna939bd02016-03-01 21:36:12 +00001597 if (!Complete) {
1598 errs() << "\n\nIncomplete schedule models found.\n"
1599 << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
1600 << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
1601 << "- Instructions should usually have Sched<[...]> as a superclass, "
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001602 "you may temporarily use an empty list.\n"
1603 << "- Instructions related to unsupported features can be excluded with "
1604 "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
1605 "processor model.\n\n";
Matthias Braun17cb5792016-03-01 20:03:21 +00001606 PrintFatalError("Incomplete schedule model");
Matthias Brauna939bd02016-03-01 21:36:12 +00001607 }
Matthias Braun17cb5792016-03-01 20:03:21 +00001608}
1609
Andrew Trick1e46d482012-09-15 00:20:02 +00001610// Collect itinerary class resources for each processor.
1611void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
1612 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
1613 const CodeGenProcModel &PM = ProcModels[PIdx];
1614 // For all ItinRW entries.
1615 bool HasMatch = false;
1616 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
1617 II != IE; ++II) {
1618 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
1619 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
1620 continue;
1621 if (HasMatch)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001622 PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
1623 + ItinClassDef->getName()
1624 + " in ItinResources for " + PM.ModelName);
Andrew Trick1e46d482012-09-15 00:20:02 +00001625 HasMatch = true;
1626 IdxVec Writes, Reads;
1627 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
1628 IdxVec ProcIndices(1, PIdx);
1629 collectRWResources(Writes, Reads, ProcIndices);
1630 }
1631 }
1632}
1633
Andrew Trickd0b9c442012-10-10 05:43:13 +00001634void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +00001635 ArrayRef<unsigned> ProcIndices) {
Andrew Trickd0b9c442012-10-10 05:43:13 +00001636 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1637 if (SchedRW.TheDef) {
1638 if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001639 for (unsigned Idx : ProcIndices)
1640 addWriteRes(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001641 }
1642 else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001643 for (unsigned Idx : ProcIndices)
1644 addReadAdvance(SchedRW.TheDef, Idx);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001645 }
1646 }
1647 for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1648 AI != AE; ++AI) {
1649 IdxVec AliasProcIndices;
1650 if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1651 AliasProcIndices.push_back(
1652 getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1653 }
1654 else
1655 AliasProcIndices = ProcIndices;
1656 const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1657 assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1658
1659 IdxVec ExpandedRWs;
1660 expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1661 for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1662 SI != SE; ++SI) {
1663 collectRWResources(*SI, IsRead, AliasProcIndices);
1664 }
1665 }
1666}
Andrew Trick1e46d482012-09-15 00:20:02 +00001667
1668// Collect resources for a set of read/write types and processor indices.
Benjamin Kramere1761952015-10-24 12:46:49 +00001669void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
1670 ArrayRef<unsigned> Reads,
1671 ArrayRef<unsigned> ProcIndices) {
Benjamin Kramere1761952015-10-24 12:46:49 +00001672 for (unsigned Idx : Writes)
1673 collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +00001674
Benjamin Kramere1761952015-10-24 12:46:49 +00001675 for (unsigned Idx : Reads)
1676 collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +00001677}
1678
1679// Find the processor's resource units for this kind of resource.
1680Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
1681 const CodeGenProcModel &PM) const {
1682 if (ProcResKind->isSubClassOf("ProcResourceUnits"))
1683 return ProcResKind;
1684
Craig Topper24064772014-04-15 07:20:03 +00001685 Record *ProcUnitDef = nullptr;
Matthias Braun6b1fd9a2016-06-21 03:24:03 +00001686 assert(!ProcResourceDefs.empty());
1687 assert(!ProcResGroups.empty());
Andrew Trick1e46d482012-09-15 00:20:02 +00001688
Javed Absar67b042c2017-09-13 10:31:10 +00001689 for (Record *ProcResDef : ProcResourceDefs) {
1690 if (ProcResDef->getValueAsDef("Kind") == ProcResKind
1691 && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001692 if (ProcUnitDef) {
Javed Absar67b042c2017-09-13 10:31:10 +00001693 PrintFatalError(ProcResDef->getLoc(),
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001694 "Multiple ProcessorResourceUnits associated with "
1695 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001696 }
Javed Absar67b042c2017-09-13 10:31:10 +00001697 ProcUnitDef = ProcResDef;
Andrew Trick1e46d482012-09-15 00:20:02 +00001698 }
1699 }
Javed Absar67b042c2017-09-13 10:31:10 +00001700 for (Record *ProcResGroup : ProcResGroups) {
1701 if (ProcResGroup == ProcResKind
1702 && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
Andrew Trick4e67cba2013-03-14 21:21:50 +00001703 if (ProcUnitDef) {
Javed Absar67b042c2017-09-13 10:31:10 +00001704 PrintFatalError((ProcResGroup)->getLoc(),
Andrew Trick4e67cba2013-03-14 21:21:50 +00001705 "Multiple ProcessorResourceUnits associated with "
1706 + ProcResKind->getName());
1707 }
Javed Absar67b042c2017-09-13 10:31:10 +00001708 ProcUnitDef = ProcResGroup;
Andrew Trick4e67cba2013-03-14 21:21:50 +00001709 }
1710 }
Andrew Trick1e46d482012-09-15 00:20:02 +00001711 if (!ProcUnitDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001712 PrintFatalError(ProcResKind->getLoc(),
1713 "No ProcessorResources associated with "
1714 + ProcResKind->getName());
Andrew Trick1e46d482012-09-15 00:20:02 +00001715 }
1716 return ProcUnitDef;
1717}
1718
1719// Iteratively add a resource and its super resources.
1720void CodeGenSchedModels::addProcResource(Record *ProcResKind,
1721 CodeGenProcModel &PM) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001722 while (true) {
Andrew Trick1e46d482012-09-15 00:20:02 +00001723 Record *ProcResUnits = findProcResUnits(ProcResKind, PM);
1724
1725 // See if this ProcResource is already associated with this processor.
David Majnemer42531262016-08-12 03:55:06 +00001726 if (is_contained(PM.ProcResourceDefs, ProcResUnits))
Andrew Trick1e46d482012-09-15 00:20:02 +00001727 return;
1728
1729 PM.ProcResourceDefs.push_back(ProcResUnits);
Andrew Trick4e67cba2013-03-14 21:21:50 +00001730 if (ProcResUnits->isSubClassOf("ProcResGroup"))
1731 return;
1732
Andrew Trick1e46d482012-09-15 00:20:02 +00001733 if (!ProcResUnits->getValueInit("Super")->isComplete())
1734 return;
1735
1736 ProcResKind = ProcResUnits->getValueAsDef("Super");
1737 }
1738}
1739
1740// Add resources for a SchedWrite to this processor if they don't exist.
1741void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001742 assert(PIdx && "don't add resources to an invalid Processor model");
1743
Andrew Trick1e46d482012-09-15 00:20:02 +00001744 RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
David Majnemer42531262016-08-12 03:55:06 +00001745 if (is_contained(WRDefs, ProcWriteResDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001746 return;
1747 WRDefs.push_back(ProcWriteResDef);
1748
1749 // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
1750 RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
1751 for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
1752 WritePRI != WritePRE; ++WritePRI) {
1753 addProcResource(*WritePRI, ProcModels[PIdx]);
1754 }
1755}
1756
1757// Add resources for a ReadAdvance to this processor if they don't exist.
1758void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
1759 unsigned PIdx) {
1760 RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
David Majnemer42531262016-08-12 03:55:06 +00001761 if (is_contained(RADefs, ProcReadAdvanceDef))
Andrew Trick1e46d482012-09-15 00:20:02 +00001762 return;
1763 RADefs.push_back(ProcReadAdvanceDef);
1764}
1765
Andrew Trick8fa00f52012-09-17 22:18:43 +00001766unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
David Majnemer0d955d02016-08-11 22:21:41 +00001767 RecIter PRPos = find(ProcResourceDefs, PRDef);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001768 if (PRPos == ProcResourceDefs.end())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001769 PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
1770 "the ProcResources list for " + ModelName);
Andrew Trick8fa00f52012-09-17 22:18:43 +00001771 // Idx=0 is reserved for invalid.
Rafael Espindola72961392012-11-02 20:57:36 +00001772 return 1 + (PRPos - ProcResourceDefs.begin());
Andrew Trick8fa00f52012-09-17 22:18:43 +00001773}
1774
Simon Dardis5f95c9a2016-06-24 08:43:27 +00001775bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
1776 for (const Record *TheDef : UnsupportedFeaturesDefs) {
1777 for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
1778 if (TheDef->getName() == PredDef->getName())
1779 return true;
1780 }
1781 }
1782 return false;
1783}
1784
Andrew Trick76686492012-09-15 00:19:57 +00001785#ifndef NDEBUG
1786void CodeGenProcModel::dump() const {
1787 dbgs() << Index << ": " << ModelName << " "
1788 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1789 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1790}
1791
1792void CodeGenSchedRW::dump() const {
1793 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1794 if (IsSequence) {
1795 dbgs() << "(";
1796 dumpIdxVec(Sequence);
1797 dbgs() << ")";
1798 }
1799}
1800
1801void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001802 dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
Andrew Trick76686492012-09-15 00:19:57 +00001803 << " Writes: ";
1804 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1805 SchedModels->getSchedWrite(Writes[i]).dump();
1806 if (i < N-1) {
1807 dbgs() << '\n';
1808 dbgs().indent(10);
1809 }
1810 }
1811 dbgs() << "\n Reads: ";
1812 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1813 SchedModels->getSchedRead(Reads[i]).dump();
1814 if (i < N-1) {
1815 dbgs() << '\n';
1816 dbgs().indent(10);
1817 }
1818 }
1819 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
Andrew Tricke97978f2013-03-26 21:36:39 +00001820 if (!Transitions.empty()) {
1821 dbgs() << "\n Transitions for Proc ";
Javed Absar67b042c2017-09-13 10:31:10 +00001822 for (const CodeGenSchedTransition &Transition : Transitions) {
1823 dumpIdxVec(Transition.ProcIndices);
Andrew Tricke97978f2013-03-26 21:36:39 +00001824 }
1825 }
Andrew Trick76686492012-09-15 00:19:57 +00001826}
Andrew Trick33401e82012-09-15 00:19:59 +00001827
1828void PredTransitions::dump() const {
1829 dbgs() << "Expanded Variants:\n";
1830 for (std::vector<PredTransition>::const_iterator
1831 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1832 dbgs() << "{";
1833 for (SmallVectorImpl<PredCheck>::const_iterator
1834 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1835 PCI != PCE; ++PCI) {
1836 if (PCI != TI->PredTerm.begin())
1837 dbgs() << ", ";
1838 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1839 << ":" << PCI->Predicate->getName();
1840 }
1841 dbgs() << "},\n => {";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001842 for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
Andrew Trick33401e82012-09-15 00:19:59 +00001843 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1844 WSI != WSE; ++WSI) {
1845 dbgs() << "(";
1846 for (SmallVectorImpl<unsigned>::const_iterator
1847 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1848 if (WI != WSI->begin())
1849 dbgs() << ", ";
1850 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1851 }
1852 dbgs() << "),";
1853 }
1854 dbgs() << "}\n";
1855 }
1856}
Andrew Trick76686492012-09-15 00:19:57 +00001857#endif // NDEBUG