blob: 9f59e19ec5693326d9bb9eb23a9f0cbd2f1585ac [file] [log] [blame]
Andrew Trick2661b412012-07-07 04:00:00 +00001//===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines structures to encapsulate the machine model as decribed in
11// the target description.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "subtarget-emitter"
16
17#include "CodeGenSchedule.h"
18#include "CodeGenTarget.h"
Andrew Trick48605c32012-09-15 00:19:57 +000019#include "llvm/TableGen/Error.h"
Andrew Trick2661b412012-07-07 04:00:00 +000020#include "llvm/Support/Debug.h"
21
22using namespace llvm;
23
Andrew Trick48605c32012-09-15 00:19:57 +000024#ifndef NDEBUG
25static void dumpIdxVec(const IdxVec &V) {
26 for (unsigned i = 0, e = V.size(); i < e; ++i) {
27 dbgs() << V[i] << ", ";
28 }
29}
Andrew Trick5e613c22012-09-15 00:19:59 +000030static void dumpIdxVec(const SmallVectorImpl<unsigned> &V) {
31 for (unsigned i = 0, e = V.size(); i < e; ++i) {
32 dbgs() << V[i] << ", ";
33 }
34}
Andrew Trick48605c32012-09-15 00:19:57 +000035#endif
36
37/// CodeGenModels ctor interprets machine model records and populates maps.
Andrew Trick2661b412012-07-07 04:00:00 +000038CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
39 const CodeGenTarget &TGT):
Andrew Trick48605c32012-09-15 00:19:57 +000040 Records(RK), Target(TGT), NumItineraryClasses(0) {
Andrew Trick2661b412012-07-07 04:00:00 +000041
Andrew Trick48605c32012-09-15 00:19:57 +000042 // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
43 // that are explicitly referenced in tablegen records. Resources associated
44 // with each processor will be derived later. Populate ProcModelMap with the
45 // CodeGenProcModel instances.
46 collectProcModels();
Andrew Trick2661b412012-07-07 04:00:00 +000047
Andrew Trick48605c32012-09-15 00:19:57 +000048 // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
49 // defined, and populate SchedReads and SchedWrites vectors. Implicit
50 // SchedReadWrites that represent sequences derived from expanded variant will
51 // be inferred later.
52 collectSchedRW();
53
54 // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
55 // required by an instruction definition, and populate SchedClassIdxMap. Set
56 // NumItineraryClasses to the number of explicit itinerary classes referenced
57 // by instructions. Set NumInstrSchedClasses to the number of itinerary
58 // classes plus any classes implied by instructions that derive from class
59 // Sched and provide SchedRW list. This does not infer any new classes from
60 // SchedVariant.
61 collectSchedClasses();
62
63 // Find instruction itineraries for each processor. Sort and populate
64 // CodeGenProcMode::ItinDefList. (Cycle-to-cycle itineraries). This requires
65 // all itinerary classes to be discovered.
66 collectProcItins();
67
68 // Find ItinRW records for each processor and itinerary class.
69 // (For per-operand resources mapped to itinerary classes).
70 collectProcItinRW();
Andrew Trick5e613c22012-09-15 00:19:59 +000071
72 // Infer new SchedClasses from SchedVariant.
73 inferSchedClasses();
74
75 DEBUG(for (unsigned i = 0; i < SchedClasses.size(); ++i)
76 SchedClasses[i].dump(this));
Andrew Trick2661b412012-07-07 04:00:00 +000077}
78
Andrew Trick48605c32012-09-15 00:19:57 +000079/// Gather all processor models.
80void CodeGenSchedModels::collectProcModels() {
81 RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
82 std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
Andrew Trick2661b412012-07-07 04:00:00 +000083
Andrew Trick48605c32012-09-15 00:19:57 +000084 // Reserve space because we can. Reallocation would be ok.
85 ProcModels.reserve(ProcRecords.size()+1);
86
87 // Use idx=0 for NoModel/NoItineraries.
88 Record *NoModelDef = Records.getDef("NoSchedModel");
89 Record *NoItinsDef = Records.getDef("NoItineraries");
90 ProcModels.push_back(CodeGenProcModel(0, "NoSchedModel",
91 NoModelDef, NoItinsDef));
92 ProcModelMap[NoModelDef] = 0;
93
94 // For each processor, find a unique machine model.
95 for (unsigned i = 0, N = ProcRecords.size(); i < N; ++i)
96 addProcModel(ProcRecords[i]);
97}
98
99/// Get a unique processor model based on the defined MachineModel and
100/// ProcessorItineraries.
101void CodeGenSchedModels::addProcModel(Record *ProcDef) {
102 Record *ModelKey = getModelOrItinDef(ProcDef);
103 if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
104 return;
105
106 std::string Name = ModelKey->getName();
107 if (ModelKey->isSubClassOf("SchedMachineModel")) {
108 Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
109 ProcModels.push_back(
110 CodeGenProcModel(ProcModels.size(), Name, ModelKey, ItinsDef));
111 }
112 else {
113 // An itinerary is defined without a machine model. Infer a new model.
114 if (!ModelKey->getValueAsListOfDefs("IID").empty())
115 Name = Name + "Model";
116 ProcModels.push_back(
117 CodeGenProcModel(ProcModels.size(), Name,
118 ProcDef->getValueAsDef("SchedModel"), ModelKey));
119 }
120 DEBUG(ProcModels.back().dump());
121}
122
123// Recursively find all reachable SchedReadWrite records.
124static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
125 SmallPtrSet<Record*, 16> &RWSet) {
126 if (!RWSet.insert(RWDef))
127 return;
128 RWDefs.push_back(RWDef);
129 // Reads don't current have sequence records, but it can be added later.
130 if (RWDef->isSubClassOf("WriteSequence")) {
131 RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
132 for (RecIter I = Seq.begin(), E = Seq.end(); I != E; ++I)
133 scanSchedRW(*I, RWDefs, RWSet);
134 }
135 else if (RWDef->isSubClassOf("SchedVariant")) {
136 // Visit each variant (guarded by a different predicate).
137 RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
138 for (RecIter VI = Vars.begin(), VE = Vars.end(); VI != VE; ++VI) {
139 // Visit each RW in the sequence selected by the current variant.
140 RecVec Selected = (*VI)->getValueAsListOfDefs("Selected");
141 for (RecIter I = Selected.begin(), E = Selected.end(); I != E; ++I)
142 scanSchedRW(*I, RWDefs, RWSet);
143 }
144 }
145}
146
147// Collect and sort all SchedReadWrites reachable via tablegen records.
148// More may be inferred later when inferring new SchedClasses from variants.
149void CodeGenSchedModels::collectSchedRW() {
150 // Reserve idx=0 for invalid writes/reads.
151 SchedWrites.resize(1);
152 SchedReads.resize(1);
153
154 SmallPtrSet<Record*, 16> RWSet;
155
156 // Find all SchedReadWrites referenced by instruction defs.
157 RecVec SWDefs, SRDefs;
158 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
159 E = Target.inst_end(); I != E; ++I) {
160 Record *SchedDef = (*I)->TheDef;
161 if (!SchedDef->isSubClassOf("Sched"))
162 continue;
163 RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
164 for (RecIter RWI = RWs.begin(), RWE = RWs.end(); RWI != RWE; ++RWI) {
165 if ((*RWI)->isSubClassOf("SchedWrite"))
166 scanSchedRW(*RWI, SWDefs, RWSet);
167 else {
168 assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
169 scanSchedRW(*RWI, SRDefs, RWSet);
170 }
171 }
172 }
173 // Find all ReadWrites referenced by InstRW.
174 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
175 for (RecIter OI = InstRWDefs.begin(), OE = InstRWDefs.end(); OI != OE; ++OI) {
176 // For all OperandReadWrites.
177 RecVec RWDefs = (*OI)->getValueAsListOfDefs("OperandReadWrites");
178 for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end();
179 RWI != RWE; ++RWI) {
180 if ((*RWI)->isSubClassOf("SchedWrite"))
181 scanSchedRW(*RWI, SWDefs, RWSet);
182 else {
183 assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
184 scanSchedRW(*RWI, SRDefs, RWSet);
185 }
186 }
187 }
188 // Find all ReadWrites referenced by ItinRW.
189 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
190 for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) {
191 // For all OperandReadWrites.
192 RecVec RWDefs = (*II)->getValueAsListOfDefs("OperandReadWrites");
193 for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end();
194 RWI != RWE; ++RWI) {
195 if ((*RWI)->isSubClassOf("SchedWrite"))
196 scanSchedRW(*RWI, SWDefs, RWSet);
197 else {
198 assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
199 scanSchedRW(*RWI, SRDefs, RWSet);
200 }
201 }
202 }
203 // Sort and add the SchedReadWrites directly referenced by instructions or
204 // itinerary resources. Index reads and writes in separate domains.
205 std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
206 for (RecIter SWI = SWDefs.begin(), SWE = SWDefs.end(); SWI != SWE; ++SWI) {
207 assert(!getSchedRWIdx(*SWI, /*IsRead=*/false) && "duplicate SchedWrite");
208 SchedWrites.push_back(CodeGenSchedRW(*SWI));
209 }
210 std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
211 for (RecIter SRI = SRDefs.begin(), SRE = SRDefs.end(); SRI != SRE; ++SRI) {
212 assert(!getSchedRWIdx(*SRI, /*IsRead-*/true) && "duplicate SchedWrite");
213 SchedReads.push_back(CodeGenSchedRW(*SRI));
214 }
215 // Initialize WriteSequence vectors.
216 for (std::vector<CodeGenSchedRW>::iterator WI = SchedWrites.begin(),
217 WE = SchedWrites.end(); WI != WE; ++WI) {
218 if (!WI->IsSequence)
219 continue;
220 findRWs(WI->TheDef->getValueAsListOfDefs("Writes"), WI->Sequence,
221 /*IsRead=*/false);
222 }
223 DEBUG(
224 for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
225 dbgs() << WIdx << ": ";
226 SchedWrites[WIdx].dump();
227 dbgs() << '\n';
228 }
229 for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
230 dbgs() << RIdx << ": ";
231 SchedReads[RIdx].dump();
232 dbgs() << '\n';
233 }
234 RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
235 for (RecIter RI = RWDefs.begin(), RE = RWDefs.end();
236 RI != RE; ++RI) {
237 if (!getSchedRWIdx(*RI, (*RI)->isSubClassOf("SchedRead"))) {
238 const std::string &Name = (*RI)->getName();
239 if (Name != "NoWrite" && Name != "ReadDefault")
240 dbgs() << "Unused SchedReadWrite " << (*RI)->getName() << '\n';
241 }
242 });
243}
244
245/// Compute a SchedWrite name from a sequence of writes.
246std::string CodeGenSchedModels::genRWName(const IdxVec& Seq, bool IsRead) {
247 std::string Name("(");
248 for (IdxIter I = Seq.begin(), E = Seq.end(); I != E; ++I) {
249 if (I != Seq.begin())
250 Name += '_';
251 Name += getSchedRW(*I, IsRead).Name;
252 }
253 Name += ')';
254 return Name;
255}
256
257unsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead,
258 unsigned After) const {
259 const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
260 assert(After < RWVec.size() && "start position out of bounds");
261 for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin() + After,
262 E = RWVec.end(); I != E; ++I) {
263 if (I->TheDef == Def)
264 return I - RWVec.begin();
265 }
266 return 0;
267}
268
269namespace llvm {
270void splitSchedReadWrites(const RecVec &RWDefs,
271 RecVec &WriteDefs, RecVec &ReadDefs) {
272 for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); RWI != RWE; ++RWI) {
273 if ((*RWI)->isSubClassOf("SchedWrite"))
274 WriteDefs.push_back(*RWI);
275 else {
276 assert((*RWI)->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
277 ReadDefs.push_back(*RWI);
278 }
279 }
280}
281} // namespace llvm
282
283// Split the SchedReadWrites defs and call findRWs for each list.
284void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
285 IdxVec &Writes, IdxVec &Reads) const {
286 RecVec WriteDefs;
287 RecVec ReadDefs;
288 splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
289 findRWs(WriteDefs, Writes, false);
290 findRWs(ReadDefs, Reads, true);
291}
292
293// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
294void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
295 bool IsRead) const {
296 for (RecIter RI = RWDefs.begin(), RE = RWDefs.end(); RI != RE; ++RI) {
297 unsigned Idx = getSchedRWIdx(*RI, IsRead);
298 assert(Idx && "failed to collect SchedReadWrite");
299 RWs.push_back(Idx);
300 }
301}
302
Andrew Trick5e613c22012-09-15 00:19:59 +0000303void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
304 bool IsRead) const {
305 const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
306 if (!SchedRW.IsSequence) {
307 RWSeq.push_back(RWIdx);
308 return;
309 }
310 int Repeat =
311 SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
312 for (int i = 0; i < Repeat; ++i) {
313 for (IdxIter I = SchedRW.Sequence.begin(), E = SchedRW.Sequence.end();
314 I != E; ++I) {
315 expandRWSequence(*I, RWSeq, IsRead);
316 }
317 }
318}
319
320// Find the existing SchedWrite that models this sequence of writes.
321unsigned CodeGenSchedModels::findRWForSequence(const IdxVec &Seq,
322 bool IsRead) {
323 std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
324
325 for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
326 I != E; ++I) {
327 if (I->Sequence == Seq)
328 return I - RWVec.begin();
329 }
330 // Index zero reserved for invalid RW.
331 return 0;
332}
333
334/// Add this ReadWrite if it doesn't already exist.
335unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
336 bool IsRead) {
337 assert(!Seq.empty() && "cannot insert empty sequence");
338 if (Seq.size() == 1)
339 return Seq.back();
340
341 unsigned Idx = findRWForSequence(Seq, IsRead);
342 if (Idx)
343 return Idx;
344
345 CodeGenSchedRW SchedRW(Seq, genRWName(Seq, IsRead));
346 if (IsRead) {
347 SchedReads.push_back(SchedRW);
348 return SchedReads.size() - 1;
349 }
350 SchedWrites.push_back(SchedRW);
351 return SchedWrites.size() - 1;
352}
353
Andrew Trick48605c32012-09-15 00:19:57 +0000354/// Visit all the instruction definitions for this target to gather and
355/// enumerate the itinerary classes. These are the explicitly specified
356/// SchedClasses. More SchedClasses may be inferred.
357void CodeGenSchedModels::collectSchedClasses() {
358
359 // NoItinerary is always the first class at Idx=0
Andrew Trick2661b412012-07-07 04:00:00 +0000360 SchedClasses.resize(1);
361 SchedClasses.back().Name = "NoItinerary";
Andrew Trick48605c32012-09-15 00:19:57 +0000362 SchedClasses.back().ProcIndices.push_back(0);
Andrew Trick2661b412012-07-07 04:00:00 +0000363 SchedClassIdxMap[SchedClasses.back().Name] = 0;
364
365 // Gather and sort all itinerary classes used by instruction descriptions.
Andrew Trick48605c32012-09-15 00:19:57 +0000366 RecVec ItinClassList;
Andrew Trick2661b412012-07-07 04:00:00 +0000367 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
368 E = Target.inst_end(); I != E; ++I) {
Andrew Trick48605c32012-09-15 00:19:57 +0000369 Record *ItinDef = (*I)->TheDef->getValueAsDef("Itinerary");
Andrew Trick2661b412012-07-07 04:00:00 +0000370 // Map a new SchedClass with no index.
Andrew Trick48605c32012-09-15 00:19:57 +0000371 if (!SchedClassIdxMap.count(ItinDef->getName())) {
372 SchedClassIdxMap[ItinDef->getName()] = 0;
373 ItinClassList.push_back(ItinDef);
Andrew Trick2661b412012-07-07 04:00:00 +0000374 }
375 }
376 // Assign each itinerary class unique number, skipping NoItinerary==0
377 NumItineraryClasses = ItinClassList.size();
378 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
379 for (unsigned i = 0, N = NumItineraryClasses; i < N; i++) {
380 Record *ItinDef = ItinClassList[i];
381 SchedClassIdxMap[ItinDef->getName()] = SchedClasses.size();
382 SchedClasses.push_back(CodeGenSchedClass(ItinDef));
383 }
Andrew Trick48605c32012-09-15 00:19:57 +0000384 // Infer classes from SchedReadWrite resources listed for each
385 // instruction definition that inherits from class Sched.
386 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
387 E = Target.inst_end(); I != E; ++I) {
388 if (!(*I)->TheDef->isSubClassOf("Sched"))
389 continue;
390 IdxVec Writes, Reads;
391 findRWs((*I)->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
392 // ProcIdx == 0 indicates the class applies to all processors.
393 IdxVec ProcIndices(1, 0);
394 addSchedClass(Writes, Reads, ProcIndices);
395 }
396 // Create classes for InstReadWrite defs.
397 RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
398 std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
399 for (RecIter OI = InstRWDefs.begin(), OE = InstRWDefs.end(); OI != OE; ++OI)
400 createInstRWClass(*OI);
Andrew Trick2661b412012-07-07 04:00:00 +0000401
Andrew Trick48605c32012-09-15 00:19:57 +0000402 NumInstrSchedClasses = SchedClasses.size();
Andrew Trick2661b412012-07-07 04:00:00 +0000403
Andrew Trick48605c32012-09-15 00:19:57 +0000404 bool EnableDump = false;
405 DEBUG(EnableDump = true);
406 if (!EnableDump)
Andrew Trick2661b412012-07-07 04:00:00 +0000407 return;
Andrew Trick48605c32012-09-15 00:19:57 +0000408 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
409 E = Target.inst_end(); I != E; ++I) {
410 Record *SchedDef = (*I)->TheDef;
411 std::string InstName = (*I)->TheDef->getName();
412 if (SchedDef->isSubClassOf("Sched")) {
413 IdxVec Writes;
414 IdxVec Reads;
415 findRWs((*I)->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
416 dbgs() << "SchedRW machine model for " << InstName;
417 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI)
418 dbgs() << " " << SchedWrites[*WI].Name;
419 for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI)
420 dbgs() << " " << SchedReads[*RI].Name;
421 dbgs() << '\n';
422 }
423 unsigned SCIdx = InstrClassMap.lookup((*I)->TheDef);
424 if (SCIdx) {
425 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
426 for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end();
427 RWI != RWE; ++RWI) {
428 const CodeGenProcModel &ProcModel =
429 getProcModel((*RWI)->getValueAsDef("SchedModel"));
430 dbgs() << "InstrRW on " << ProcModel.ModelName << " for " << InstName;
431 IdxVec Writes;
432 IdxVec Reads;
433 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
434 Writes, Reads);
435 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI)
436 dbgs() << " " << SchedWrites[*WI].Name;
437 for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI)
438 dbgs() << " " << SchedReads[*RI].Name;
439 dbgs() << '\n';
440 }
441 continue;
442 }
443 if (!SchedDef->isSubClassOf("Sched")
444 && (SchedDef->getValueAsDef("Itinerary")->getName() == "NoItinerary")) {
445 dbgs() << "No machine model for " << (*I)->TheDef->getName() << '\n';
Andrew Trick2661b412012-07-07 04:00:00 +0000446 }
447 }
Andrew Trick48605c32012-09-15 00:19:57 +0000448}
449
450unsigned CodeGenSchedModels::getSchedClassIdx(
451 const RecVec &RWDefs) const {
452
453 IdxVec Writes, Reads;
454 findRWs(RWDefs, Writes, Reads);
455 return findSchedClassIdx(Writes, Reads);
456}
457
458/// Find an SchedClass that has been inferred from a per-operand list of
459/// SchedWrites and SchedReads.
460unsigned CodeGenSchedModels::findSchedClassIdx(const IdxVec &Writes,
461 const IdxVec &Reads) const {
462 for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I) {
463 // Classes with InstRWs may have the same Writes/Reads as a class originally
464 // produced by a SchedRW definition. We need to be able to recover the
465 // original class index for processors that don't match any InstRWs.
466 if (I->ItinClassDef || !I->InstRWs.empty())
467 continue;
468
469 if (I->Writes == Writes && I->Reads == Reads) {
470 return I - schedClassBegin();
471 }
Andrew Trick2661b412012-07-07 04:00:00 +0000472 }
Andrew Trick48605c32012-09-15 00:19:57 +0000473 return 0;
474}
Andrew Trick2661b412012-07-07 04:00:00 +0000475
Andrew Trick48605c32012-09-15 00:19:57 +0000476// Get the SchedClass index for an instruction.
477unsigned CodeGenSchedModels::getSchedClassIdx(
478 const CodeGenInstruction &Inst) const {
Andrew Trick2661b412012-07-07 04:00:00 +0000479
Andrew Trick48605c32012-09-15 00:19:57 +0000480 unsigned SCIdx = InstrClassMap.lookup(Inst.TheDef);
481 if (SCIdx)
482 return SCIdx;
Andrew Trick2661b412012-07-07 04:00:00 +0000483
Andrew Trick48605c32012-09-15 00:19:57 +0000484 // If this opcode isn't mapped by the subtarget fallback to the instruction
485 // definition's SchedRW or ItinDef values.
486 if (Inst.TheDef->isSubClassOf("Sched")) {
487 RecVec RWs = Inst.TheDef->getValueAsListOfDefs("SchedRW");
488 return getSchedClassIdx(RWs);
489 }
490 Record *ItinDef = Inst.TheDef->getValueAsDef("Itinerary");
491 assert(SchedClassIdxMap.count(ItinDef->getName()) && "missing ItinClass");
492 unsigned Idx = SchedClassIdxMap.lookup(ItinDef->getName());
493 assert(Idx <= NumItineraryClasses && "bad ItinClass index");
494 return Idx;
495}
496
497std::string CodeGenSchedModels::createSchedClassName(
498 const IdxVec &OperWrites, const IdxVec &OperReads) {
499
500 std::string Name;
501 for (IdxIter WI = OperWrites.begin(), WE = OperWrites.end(); WI != WE; ++WI) {
502 if (WI != OperWrites.begin())
503 Name += '_';
504 Name += SchedWrites[*WI].Name;
505 }
506 for (IdxIter RI = OperReads.begin(), RE = OperReads.end(); RI != RE; ++RI) {
507 Name += '_';
508 Name += SchedReads[*RI].Name;
509 }
510 return Name;
511}
512
513std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
514
515 std::string Name;
516 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
517 if (I != InstDefs.begin())
518 Name += '_';
519 Name += (*I)->getName();
520 }
521 return Name;
522}
523
524/// Add an inferred sched class from a per-operand list of SchedWrites and
525/// SchedReads. ProcIndices contains the set of IDs of processors that may
526/// utilize this class.
527unsigned CodeGenSchedModels::addSchedClass(const IdxVec &OperWrites,
528 const IdxVec &OperReads,
529 const IdxVec &ProcIndices)
530{
531 assert(!ProcIndices.empty() && "expect at least one ProcIdx");
532
533 unsigned Idx = findSchedClassIdx(OperWrites, OperReads);
534 if (Idx) {
535 IdxVec PI;
536 std::set_union(SchedClasses[Idx].ProcIndices.begin(),
537 SchedClasses[Idx].ProcIndices.end(),
538 ProcIndices.begin(), ProcIndices.end(),
539 std::back_inserter(PI));
540 SchedClasses[Idx].ProcIndices.swap(PI);
541 return Idx;
542 }
543 Idx = SchedClasses.size();
544 SchedClasses.resize(Idx+1);
545 CodeGenSchedClass &SC = SchedClasses.back();
546 SC.Name = createSchedClassName(OperWrites, OperReads);
547 SC.Writes = OperWrites;
548 SC.Reads = OperReads;
549 SC.ProcIndices = ProcIndices;
550
551 return Idx;
552}
553
554// Create classes for each set of opcodes that are in the same InstReadWrite
555// definition across all processors.
556void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
557 // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
558 // intersects with an existing class via a previous InstRWDef. Instrs that do
559 // not intersect with an existing class refer back to their former class as
560 // determined from ItinDef or SchedRW.
561 SmallVector<std::pair<unsigned, SmallVector<Record *, 8> >, 4> ClassInstrs;
562 // Sort Instrs into sets.
563 RecVec InstDefs = InstRWDef->getValueAsListOfDefs("Instrs");
564 std::sort(InstDefs.begin(), InstDefs.end(), LessRecord());
565 for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
566 unsigned SCIdx = 0;
567 InstClassMapTy::const_iterator Pos = InstrClassMap.find(*I);
568 if (Pos != InstrClassMap.end())
569 SCIdx = Pos->second;
570 else {
571 // This instruction has not been mapped yet. Get the original class. All
572 // instructions in the same InstrRW class must be from the same original
573 // class because that is the fall-back class for other processors.
574 Record *ItinDef = (*I)->getValueAsDef("Itinerary");
575 SCIdx = SchedClassIdxMap.lookup(ItinDef->getName());
576 if (!SCIdx && (*I)->isSubClassOf("Sched"))
577 SCIdx = getSchedClassIdx((*I)->getValueAsListOfDefs("SchedRW"));
578 }
579 unsigned CIdx = 0, CEnd = ClassInstrs.size();
580 for (; CIdx != CEnd; ++CIdx) {
581 if (ClassInstrs[CIdx].first == SCIdx)
582 break;
583 }
584 if (CIdx == CEnd) {
585 ClassInstrs.resize(CEnd + 1);
586 ClassInstrs[CIdx].first = SCIdx;
587 }
588 ClassInstrs[CIdx].second.push_back(*I);
589 }
590 // For each set of Instrs, create a new class if necessary, and map or remap
591 // the Instrs to it.
592 unsigned CIdx = 0, CEnd = ClassInstrs.size();
593 for (; CIdx != CEnd; ++CIdx) {
594 unsigned OldSCIdx = ClassInstrs[CIdx].first;
595 ArrayRef<Record*> InstDefs = ClassInstrs[CIdx].second;
596 // If the all instrs in the current class are accounted for, then leave
597 // them mapped to their old class.
598 if (SchedClasses[OldSCIdx].InstRWs.size() == InstDefs.size()) {
599 assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
600 "expected a generic SchedClass");
601 continue;
602 }
603 unsigned SCIdx = SchedClasses.size();
604 SchedClasses.resize(SCIdx+1);
605 CodeGenSchedClass &SC = SchedClasses.back();
606 SC.Name = createSchedClassName(InstDefs);
607 // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
608 SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
609 SC.Writes = SchedClasses[OldSCIdx].Writes;
610 SC.Reads = SchedClasses[OldSCIdx].Reads;
611 SC.ProcIndices.push_back(0);
612 // Map each Instr to this new class.
613 // Note that InstDefs may be a smaller list than InstRWDef's "Instrs".
614 for (ArrayRef<Record*>::const_iterator
615 II = InstDefs.begin(), IE = InstDefs.end(); II != IE; ++II) {
616 unsigned OldSCIdx = InstrClassMap[*II];
617 if (OldSCIdx) {
618 SC.InstRWs.insert(SC.InstRWs.end(),
619 SchedClasses[OldSCIdx].InstRWs.begin(),
620 SchedClasses[OldSCIdx].InstRWs.end());
621 }
622 InstrClassMap[*II] = SCIdx;
623 }
624 SC.InstRWs.push_back(InstRWDef);
625 }
Andrew Trick2661b412012-07-07 04:00:00 +0000626}
627
628// Gather the processor itineraries.
Andrew Trick48605c32012-09-15 00:19:57 +0000629void CodeGenSchedModels::collectProcItins() {
630 for (std::vector<CodeGenProcModel>::iterator PI = ProcModels.begin(),
631 PE = ProcModels.end(); PI != PE; ++PI) {
632 CodeGenProcModel &ProcModel = *PI;
633 RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
634 // Skip empty itinerary.
635 if (ItinRecords.empty())
Andrew Trick2661b412012-07-07 04:00:00 +0000636 continue;
Andrew Trick48605c32012-09-15 00:19:57 +0000637
638 ProcModel.ItinDefList.resize(NumItineraryClasses+1);
639
640 // Insert each itinerary data record in the correct position within
641 // the processor model's ItinDefList.
642 for (unsigned i = 0, N = ItinRecords.size(); i < N; i++) {
643 Record *ItinData = ItinRecords[i];
644 Record *ItinDef = ItinData->getValueAsDef("TheClass");
645 if (!SchedClassIdxMap.count(ItinDef->getName())) {
646 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
647 << " has unused itinerary class " << ItinDef->getName() << '\n');
648 continue;
649 }
650 assert(SchedClassIdxMap.count(ItinDef->getName()) && "missing ItinClass");
651 unsigned Idx = SchedClassIdxMap.lookup(ItinDef->getName());
652 assert(Idx <= NumItineraryClasses && "bad ItinClass index");
653 ProcModel.ItinDefList[Idx] = ItinData;
Andrew Trick2661b412012-07-07 04:00:00 +0000654 }
Andrew Trick48605c32012-09-15 00:19:57 +0000655 // Check for missing itinerary entries.
656 assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
657 DEBUG(
658 for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
659 if (!ProcModel.ItinDefList[i])
660 dbgs() << ProcModel.ItinsDef->getName()
661 << " missing itinerary for class "
662 << SchedClasses[i].Name << '\n';
663 });
Andrew Trick2661b412012-07-07 04:00:00 +0000664 }
Andrew Trick2661b412012-07-07 04:00:00 +0000665}
Andrew Trick48605c32012-09-15 00:19:57 +0000666
667// Gather the read/write types for each itinerary class.
668void CodeGenSchedModels::collectProcItinRW() {
669 RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
670 std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
671 for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) {
672 if (!(*II)->getValueInit("SchedModel")->isComplete())
673 throw TGError((*II)->getLoc(), "SchedModel is undefined");
674 Record *ModelDef = (*II)->getValueAsDef("SchedModel");
675 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
676 if (I == ProcModelMap.end()) {
677 throw TGError((*II)->getLoc(), "Undefined SchedMachineModel "
678 + ModelDef->getName());
679 }
680 ProcModels[I->second].ItinRWDefs.push_back(*II);
681 }
682}
683
Andrew Trick5e613c22012-09-15 00:19:59 +0000684/// Infer new classes from existing classes. In the process, this may create new
685/// SchedWrites from sequences of existing SchedWrites.
686void CodeGenSchedModels::inferSchedClasses() {
687 // Visit all existing classes and newly created classes.
688 for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
689 if (SchedClasses[Idx].ItinClassDef)
690 inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
691 else if (!SchedClasses[Idx].InstRWs.empty())
692 inferFromInstRWs(Idx);
693 else {
694 inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
695 Idx, SchedClasses[Idx].ProcIndices);
696 }
697 assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
698 "too many SchedVariants");
699 }
700}
701
702/// Infer classes from per-processor itinerary resources.
703void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
704 unsigned FromClassIdx) {
705 for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
706 const CodeGenProcModel &PM = ProcModels[PIdx];
707 // For all ItinRW entries.
708 bool HasMatch = false;
709 for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
710 II != IE; ++II) {
711 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
712 if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
713 continue;
714 if (HasMatch)
715 throw TGError((*II)->getLoc(), "Duplicate itinerary class "
716 + ItinClassDef->getName()
717 + " in ItinResources for " + PM.ModelName);
718 HasMatch = true;
719 IdxVec Writes, Reads;
720 findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
721 IdxVec ProcIndices(1, PIdx);
722 inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
723 }
724 }
725}
726
727/// Infer classes from per-processor InstReadWrite definitions.
728void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
729 const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
730 for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); RWI != RWE; ++RWI) {
731 RecVec Instrs = (*RWI)->getValueAsListOfDefs("Instrs");
732 RecIter II = Instrs.begin(), IE = Instrs.end();
733 for (; II != IE; ++II) {
734 if (InstrClassMap[*II] == SCIdx)
735 break;
736 }
737 // If this class no longer has any instructions mapped to it, it has become
738 // irrelevant.
739 if (II == IE)
740 continue;
741 IdxVec Writes, Reads;
742 findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
743 unsigned PIdx = getProcModel((*RWI)->getValueAsDef("SchedModel")).Index;
744 IdxVec ProcIndices(1, PIdx);
745 inferFromRW(Writes, Reads, SCIdx, ProcIndices);
746 }
747}
748
749namespace {
750// Associate a predicate with the SchedReadWrite that it guards.
751// RWIdx is the index of the read/write variant.
752struct PredCheck {
753 bool IsRead;
754 unsigned RWIdx;
755 Record *Predicate;
756
757 PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
758};
759
760// A Predicate transition is a list of RW sequences guarded by a PredTerm.
761struct PredTransition {
762 // A predicate term is a conjunction of PredChecks.
763 SmallVector<PredCheck, 4> PredTerm;
764 SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
765 SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
766};
767
768// Encapsulate a set of partially constructed transitions.
769// The results are built by repeated calls to substituteVariants.
770class PredTransitions {
771 CodeGenSchedModels &SchedModels;
772
773public:
774 std::vector<PredTransition> TransVec;
775
776 PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
777
778 void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
779 bool IsRead, unsigned StartIdx);
780
781 void substituteVariants(const PredTransition &Trans);
782
783#ifndef NDEBUG
784 void dump() const;
785#endif
786
787private:
788 bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
789 void pushVariant(unsigned SchedRW, Record *Variant, PredTransition &Trans,
790 bool IsRead);
791};
792} // anonymous
793
794// Return true if this predicate is mutually exclusive with a PredTerm. This
795// degenerates into checking if the predicate is mutually exclusive with any
796// predicate in the Term's conjunction.
797//
798// All predicates associated with a given SchedRW are considered mutually
799// exclusive. This should work even if the conditions expressed by the
800// predicates are not exclusive because the predicates for a given SchedWrite
801// are always checked in the order they are defined in the .td file. Later
802// conditions implicitly negate any prior condition.
803bool PredTransitions::mutuallyExclusive(Record *PredDef,
804 ArrayRef<PredCheck> Term) {
805
806 for (ArrayRef<PredCheck>::iterator I = Term.begin(), E = Term.end();
807 I != E; ++I) {
808 if (I->Predicate == PredDef)
809 return false;
810
811 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(I->RWIdx, I->IsRead);
812 assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
813 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
814 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
815 if ((*VI)->getValueAsDef("Predicate") == PredDef)
816 return true;
817 }
818 }
819 return false;
820}
821
822// Push the Reads/Writes selected by this variant onto the given PredTransition.
823void PredTransitions::pushVariant(unsigned RWIdx, Record *Variant,
824 PredTransition &Trans, bool IsRead) {
825 Trans.PredTerm.push_back(
826 PredCheck(IsRead, RWIdx, Variant->getValueAsDef("Predicate")));
827 RecVec SelectedDefs = Variant->getValueAsListOfDefs("Selected");
828 IdxVec SelectedRWs;
829 SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
830
831 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(RWIdx, IsRead);
832
833 SmallVectorImpl<SmallVector<unsigned,4> > &RWSequences = IsRead
834 ? Trans.ReadSequences : Trans.WriteSequences;
835 if (SchedRW.IsVariadic) {
836 unsigned OperIdx = RWSequences.size()-1;
837 // Make N-1 copies of this transition's last sequence.
838 for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
839 RWSequences.push_back(RWSequences[OperIdx]);
840 }
841 // Push each of the N elements of the SelectedRWs onto a copy of the last
842 // sequence (split the current operand into N operands).
843 // Note that write sequences should be expanded within this loop--the entire
844 // sequence belongs to a single operand.
845 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
846 RWI != RWE; ++RWI, ++OperIdx) {
847 IdxVec ExpandedRWs;
848 if (IsRead)
849 ExpandedRWs.push_back(*RWI);
850 else
851 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
852 RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
853 ExpandedRWs.begin(), ExpandedRWs.end());
854 }
855 assert(OperIdx == RWSequences.size() && "missed a sequence");
856 }
857 else {
858 // Push this transition's expanded sequence onto this transition's last
859 // sequence (add to the current operand's sequence).
860 SmallVectorImpl<unsigned> &Seq = RWSequences.back();
861 IdxVec ExpandedRWs;
862 for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
863 RWI != RWE; ++RWI) {
864 if (IsRead)
865 ExpandedRWs.push_back(*RWI);
866 else
867 SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
868 }
869 Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
870 }
871}
872
873// RWSeq is a sequence of all Reads or all Writes for the next read or write
874// operand. StartIdx is an index into TransVec where partial results
875// starts. RWSeq must be applied to all tranistions between StartIdx and the end
876// of TransVec.
877void PredTransitions::substituteVariantOperand(
878 const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
879
880 // Visit each original RW within the current sequence.
881 for (SmallVectorImpl<unsigned>::const_iterator
882 RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
883 const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
884 // Push this RW on all partial PredTransitions or distribute variants.
885 // New PredTransitions may be pushed within this loop which should not be
886 // revisited (TransEnd must be loop invariant).
887 for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
888 TransIdx != TransEnd; ++TransIdx) {
889 // In the common case, push RW onto the current operand's sequence.
890 if (!SchedRW.HasVariants) {
891 if (IsRead)
892 TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
893 else
894 TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
895 continue;
896 }
897 // Distribute this partial PredTransition across intersecting variants.
898 RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
899 std::vector<std::pair<Record*,unsigned> > IntersectingVariants;
900 for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
901 Record *PredDef = (*VI)->getValueAsDef("Predicate");
902 if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
903 continue;
904 if (IntersectingVariants.empty())
905 // The first variant builds on the existing transition.
906 IntersectingVariants.push_back(std::make_pair(*VI, TransIdx));
907 else {
908 // Push another copy of the current transition for more variants.
909 IntersectingVariants.push_back(
910 std::make_pair(*VI, TransVec.size()));
911 TransVec.push_back(TransVec[TransIdx]);
912 }
913 }
914 // Now expand each variant on top of its copy of the transition.
915 for (std::vector<std::pair<Record*, unsigned> >::const_iterator
916 IVI = IntersectingVariants.begin(),
917 IVE = IntersectingVariants.end();
918 IVI != IVE; ++IVI)
919 pushVariant(*RWI, IVI->first, TransVec[IVI->second], IsRead);
920 }
921 }
922}
923
924// For each variant of a Read/Write in Trans, substitute the sequence of
925// Read/Writes guarded by the variant. This is exponential in the number of
926// variant Read/Writes, but in practice detection of mutually exclusive
927// predicates should result in linear growth in the total number variants.
928//
929// This is one step in a breadth-first search of nested variants.
930void PredTransitions::substituteVariants(const PredTransition &Trans) {
931 // Build up a set of partial results starting at the back of
932 // PredTransitions. Remember the first new transition.
933 unsigned StartIdx = TransVec.size();
934 TransVec.resize(TransVec.size() + 1);
935 TransVec.back().PredTerm = Trans.PredTerm;
936
937 // Visit each original write sequence.
938 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
939 WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
940 WSI != WSE; ++WSI) {
941 // Push a new (empty) write sequence onto all partial Transitions.
942 for (std::vector<PredTransition>::iterator I =
943 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
944 I->WriteSequences.resize(I->WriteSequences.size() + 1);
945 }
946 substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
947 }
948 // Visit each original read sequence.
949 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
950 RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
951 RSI != RSE; ++RSI) {
952 // Push a new (empty) read sequence onto all partial Transitions.
953 for (std::vector<PredTransition>::iterator I =
954 TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
955 I->ReadSequences.resize(I->ReadSequences.size() + 1);
956 }
957 substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
958 }
959}
960
961static bool hasVariant(ArrayRef<PredTransition> Transitions,
962 CodeGenSchedModels &SchedModels) {
963 for (ArrayRef<PredTransition>::iterator
964 PTI = Transitions.begin(), PTE = Transitions.end();
965 PTI != PTE; ++PTI) {
966 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
967 WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
968 WSI != WSE; ++WSI) {
969 for (SmallVectorImpl<unsigned>::const_iterator
970 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
971 if (SchedModels.getSchedWrite(*WI).HasVariants)
972 return true;
973 }
974 }
975 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
976 RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
977 RSI != RSE; ++RSI) {
978 for (SmallVectorImpl<unsigned>::const_iterator
979 RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
980 if (SchedModels.getSchedRead(*RI).HasVariants)
981 return true;
982 }
983 }
984 }
985 return false;
986}
987
988// Create a new SchedClass for each variant found by inferFromRW. Pass
989// ProcIndices by copy to avoid referencing anything from SchedClasses.
990static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
991 unsigned FromClassIdx, IdxVec ProcIndices,
992 CodeGenSchedModels &SchedModels) {
993 // For each PredTransition, create a new CodeGenSchedTransition, which usually
994 // requires creating a new SchedClass.
995 for (ArrayRef<PredTransition>::iterator
996 I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
997 IdxVec OperWritesVariant;
998 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
999 WSI = I->WriteSequences.begin(), WSE = I->WriteSequences.end();
1000 WSI != WSE; ++WSI) {
1001 // Create a new write representing the expanded sequence.
1002 OperWritesVariant.push_back(
1003 SchedModels.findOrInsertRW(*WSI, /*IsRead=*/false));
1004 }
1005 IdxVec OperReadsVariant;
1006 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
1007 RSI = I->ReadSequences.begin(), RSE = I->ReadSequences.end();
1008 RSI != RSE; ++RSI) {
1009 // Create a new write representing the expanded sequence.
1010 OperReadsVariant.push_back(
1011 SchedModels.findOrInsertRW(*RSI, /*IsRead=*/true));
1012 }
1013 CodeGenSchedTransition SCTrans;
1014 SCTrans.ToClassIdx =
1015 SchedModels.addSchedClass(OperWritesVariant, OperReadsVariant,
1016 ProcIndices);
1017 SCTrans.ProcIndices = ProcIndices;
1018 // The final PredTerm is unique set of predicates guarding the transition.
1019 RecVec Preds;
1020 for (SmallVectorImpl<PredCheck>::const_iterator
1021 PI = I->PredTerm.begin(), PE = I->PredTerm.end(); PI != PE; ++PI) {
1022 Preds.push_back(PI->Predicate);
1023 }
1024 RecIter PredsEnd = std::unique(Preds.begin(), Preds.end());
1025 Preds.resize(PredsEnd - Preds.begin());
1026 SCTrans.PredTerm = Preds;
1027 SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
1028 }
1029}
1030
1031/// Find each variant write that OperWrites or OperaReads refers to and create a
1032/// new SchedClass for each variant.
1033void CodeGenSchedModels::inferFromRW(const IdxVec &OperWrites,
1034 const IdxVec &OperReads,
1035 unsigned FromClassIdx,
1036 const IdxVec &ProcIndices) {
1037 DEBUG(dbgs() << "INFERRW Writes: ");
1038
1039 // Create a seed transition with an empty PredTerm and the expanded sequences
1040 // of SchedWrites for the current SchedClass.
1041 std::vector<PredTransition> LastTransitions;
1042 LastTransitions.resize(1);
1043 for (IdxIter I = OperWrites.begin(), E = OperWrites.end(); I != E; ++I) {
1044 IdxVec WriteSeq;
1045 expandRWSequence(*I, WriteSeq, /*IsRead=*/false);
1046 unsigned Idx = LastTransitions[0].WriteSequences.size();
1047 LastTransitions[0].WriteSequences.resize(Idx + 1);
1048 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences[Idx];
1049 for (IdxIter WI = WriteSeq.begin(), WE = WriteSeq.end(); WI != WE; ++WI)
1050 Seq.push_back(*WI);
1051 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1052 }
1053 DEBUG(dbgs() << " Reads: ");
1054 for (IdxIter I = OperReads.begin(), E = OperReads.end(); I != E; ++I) {
1055 IdxVec ReadSeq;
1056 expandRWSequence(*I, ReadSeq, /*IsRead=*/true);
1057 unsigned Idx = LastTransitions[0].ReadSequences.size();
1058 LastTransitions[0].ReadSequences.resize(Idx + 1);
1059 SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences[Idx];
1060 for (IdxIter RI = ReadSeq.begin(), RE = ReadSeq.end(); RI != RE; ++RI)
1061 Seq.push_back(*RI);
1062 DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
1063 }
1064 DEBUG(dbgs() << '\n');
1065
1066 // Collect all PredTransitions for individual operands.
1067 // Iterate until no variant writes remain.
1068 while (hasVariant(LastTransitions, *this)) {
1069 PredTransitions Transitions(*this);
1070 for (std::vector<PredTransition>::const_iterator
1071 I = LastTransitions.begin(), E = LastTransitions.end();
1072 I != E; ++I) {
1073 Transitions.substituteVariants(*I);
1074 }
1075 DEBUG(Transitions.dump());
1076 LastTransitions.swap(Transitions.TransVec);
1077 }
1078 // If the first transition has no variants, nothing to do.
1079 if (LastTransitions[0].PredTerm.empty())
1080 return;
1081
1082 // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
1083 // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
1084 inferFromTransitions(LastTransitions, FromClassIdx, ProcIndices, *this);
1085}
1086
Andrew Trick48605c32012-09-15 00:19:57 +00001087#ifndef NDEBUG
1088void CodeGenProcModel::dump() const {
1089 dbgs() << Index << ": " << ModelName << " "
1090 << (ModelDef ? ModelDef->getName() : "inferred") << " "
1091 << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
1092}
1093
1094void CodeGenSchedRW::dump() const {
1095 dbgs() << Name << (IsVariadic ? " (V) " : " ");
1096 if (IsSequence) {
1097 dbgs() << "(";
1098 dumpIdxVec(Sequence);
1099 dbgs() << ")";
1100 }
1101}
1102
1103void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
1104 dbgs() << "SCHEDCLASS " << Name << '\n'
1105 << " Writes: ";
1106 for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
1107 SchedModels->getSchedWrite(Writes[i]).dump();
1108 if (i < N-1) {
1109 dbgs() << '\n';
1110 dbgs().indent(10);
1111 }
1112 }
1113 dbgs() << "\n Reads: ";
1114 for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
1115 SchedModels->getSchedRead(Reads[i]).dump();
1116 if (i < N-1) {
1117 dbgs() << '\n';
1118 dbgs().indent(10);
1119 }
1120 }
1121 dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
1122}
Andrew Trick5e613c22012-09-15 00:19:59 +00001123
1124void PredTransitions::dump() const {
1125 dbgs() << "Expanded Variants:\n";
1126 for (std::vector<PredTransition>::const_iterator
1127 TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
1128 dbgs() << "{";
1129 for (SmallVectorImpl<PredCheck>::const_iterator
1130 PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
1131 PCI != PCE; ++PCI) {
1132 if (PCI != TI->PredTerm.begin())
1133 dbgs() << ", ";
1134 dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
1135 << ":" << PCI->Predicate->getName();
1136 }
1137 dbgs() << "},\n => {";
1138 for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
1139 WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
1140 WSI != WSE; ++WSI) {
1141 dbgs() << "(";
1142 for (SmallVectorImpl<unsigned>::const_iterator
1143 WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
1144 if (WI != WSI->begin())
1145 dbgs() << ", ";
1146 dbgs() << SchedModels.getSchedWrite(*WI).Name;
1147 }
1148 dbgs() << "),";
1149 }
1150 dbgs() << "}\n";
1151 }
1152}
Andrew Trick48605c32012-09-15 00:19:57 +00001153#endif // NDEBUG