1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by James M. Laskey and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This tablegen backend emits subtarget enumerations.
//
//===----------------------------------------------------------------------===//
#include "SubtargetEmitter.h"
#include "CodeGenTarget.h"
#include "Record.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include <algorithm>
using namespace llvm;
//
// Convenience types.
//
typedef std::vector<Record*> RecordList;
//
// RecordListIter - Simplify iterating through a std::vector of records.
//
class RecordListIter {
std::vector<Record*>::iterator RI; // Currect cursor
std::vector<Record*>::iterator E; // End point
public:
//
// Ctor.
//
RecordListIter(RecordList &RL)
: RI(RL.begin()), E(RL.end())
{}
//
// isMore - Return true if more records are available.
//
bool isMore() const { return RI != E; }
//
// next - Return the next record or NULL if none.
//
Record *next() { return isMore() ? *RI++ : NULL; }
};
//
// DefListIter - Simplify iterating through a field which is a list of records.
//
struct DefListIter {
ListInit *List; // List of DefInit
unsigned N; // Number of elements in list
unsigned i; // Current index in list
//
// Ctor - Lookup field and get list and length.
//
DefListIter(Record *R, const std::string &Name)
: List(R->getValueAsListInit(Name)), N(List->getSize()), i(0)
{}
//
// isMore - Return true if more records are available.
//
bool isMore() const { return i < N; }
//
// next - Return the next record or NULL if none.
//
Record *next() {
if (isMore()) {
if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i++))) {
return DI->getDef();
}
}
return NULL;
}
};
//
// Record sort by name function.
//
struct LessRecord {
bool operator()(const Record *Rec1, const Record *Rec2) const {
return Rec1->getName() < Rec2->getName();
}
};
//
// Record sort by field "Name" function.
//
struct LessRecordFieldName {
bool operator()(const Record *Rec1, const Record *Rec2) const {
return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
}
};
//
// Enumeration - Emit the specified class as an enumeration.
//
void SubtargetEmitter::Enumeration(std::ostream &OS,
const char *ClassName,
bool isBits) {
// Get all records of class and sort
RecordList Defs = Records.getAllDerivedDefinitions(ClassName);
sort(Defs.begin(), Defs.end(), LessRecord());
// Track position if isBits
int i = 0;
// Open enumeration
OS << "enum {\n";
// For each record
RecordListIter DI(Defs);
while (Record *R = DI.next()) {
// Get and emit name
std::string Name = R->getName();
OS << " " << Name;
// If bit flags then emit expression (1 << i)
if (isBits) OS << " = " << " 1 << " << i++;
// Depending on if more in the list, emit comma and new line
OS << (DI.isMore() ? ",\n" : "\n");
}
// Close enumeration
OS << "};\n";
}
//
// FeatureKeyValues - Emit data of all the subtarget features. Used by command
// line.
//
void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
// Gather and sort all the features
RecordList Features = Records.getAllDerivedDefinitions("SubtargetFeature");
sort(Features.begin(), Features.end(), LessRecord());
// Begin feature table
OS << "// Sorted (by key) array of values for CPU features.\n"
<< "static llvm::SubtargetFeatureKV FeatureKV[] = {\n";
// For each feature
RecordListIter FI(Features);
while (Record *R = FI.next()) {
std::string Instance = R->getName();
std::string Name = R->getValueAsString("Name");
std::string Desc = R->getValueAsString("Desc");
// Emit as { "feature", "decription", feactureEnum }
OS << " { "
<< "\"" << Name << "\", "
<< "\"" << Desc << "\", "
<< Instance
<< (FI.isMore() ? " },\n" : " }\n");
}
// End feature table
OS << "};\n";
// Emit size of table
OS<<"\nenum {\n";
OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
OS<<"};\n";
}
//
// CPUKeyValues - Emit data of all the subtarget processors. Used by command
// line.
//
void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
// Gather and sort processor information
RecordList Processors = Records.getAllDerivedDefinitions("Processor");
sort(Processors.begin(), Processors.end(), LessRecordFieldName());
// Begin processor table
OS << "// Sorted (by key) array of values for CPU subtype.\n"
<< "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
// For each processor
RecordListIter PI(Processors);
while (Record *R = PI.next()) {
std::string Name = R->getValueAsString("Name");
DefListIter FI(R, "Features");
// Emit as { "cpu", "description", f1 | f2 | ... fn },
OS << " { "
<< "\"" << Name << "\", "
<< "\"Select the " << Name << " processor\", ";
if (!FI.isMore()) {
OS << "0";
} else {
while (Record *Feature = FI.next()) {
std::string Name = Feature->getName();
OS << Name;
if (FI.isMore()) OS << " | ";
}
}
OS << (PI.isMore() ? " },\n" : " }\n");
}
// End processor table
OS << "};\n";
// Emit size of table
OS<<"\nenum {\n";
OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
OS<<"};\n";
}
//
// CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
// Returns itinerary class count.
//
unsigned SubtargetEmitter::CollectAllItinClasses(IntMap &ItinClassesMap) {
// Gather and sort all itinerary classes
RecordList ICL = Records.getAllDerivedDefinitions("InstrItinClass");
sort(ICL.begin(), ICL.end(), LessRecord());
// Track enumeration
unsigned Index = 0;
// For each class
RecordListIter ICI(ICL);
while (Record *ItinClass = ICI.next()) {
// Get name of itinerary class
std::string Name = ItinClass->getName();
// Assign itinerary class a unique number
ItinClassesMap[Name] = Index++;
}
// Return itinerary class count
return Index;
}
//
// FormItineraryString - Compose a string containing the data initialization
// for the specified itinerary. N is the number of stages.
//
void SubtargetEmitter::FormItineraryString(Record *ItinData,
std::string &ItinString,
unsigned &N) {
// Set up stages iterator
DefListIter SLI(ItinData, "Stages");
// Get stage count
N = SLI.N;
// For each stage
while (Record *Stage = SLI.next()) {
// Form string as ,{ cycles, u1 | u2 | ... | un }
int Cycles = Stage->getValueAsInt("Cycles");
ItinString += " ,{ " + itostr(Cycles) + ", ";
// For each unit
DefListIter ULI(Stage, "Units");
while (Record *Unit = ULI.next()) {
std::string Name = Unit->getName();
ItinString += Name;
if (ULI.isMore())ItinString += " | ";
}
// Close off stage
ItinString += " }";
}
}
//
// EmitStageData - Generate unique itinerary stages. Record itineraries for
// processors.
//
void SubtargetEmitter::EmitStageData(std::ostream &OS,
unsigned N,
IntMap &ItinClassesMap,
ProcessorList &ProcList) {
// Gather processor iteraries
RecordList Itins = Records.getAllDerivedDefinitions("ProcessorItineraries");
// If just no itinerary then don't bother
if (Itins.size() < 2) return;
// Begin stages table
OS << "static llvm::InstrStage Stages[] = {\n"
" { 0, 0 } // No itinerary\n";
IntMap ItinMap;
unsigned Index = 1;
RecordListIter II(Itins);
while (Record *Itin = II.next()) {
// Get processor itinerary name
std::string Name = Itin->getName();
// Skip default
if (Name == "NoItineraries") continue;
// Create and expand processor itinerary to cover all itinerary classes
IntineraryList IL;
IL.resize(N);
// For each itinerary
DefListIter IDLI(Itin, "IID");
while (Record *ItinData = IDLI.next()) {
// Get string and stage count
std::string ItinString;
unsigned M;
FormItineraryString(ItinData, ItinString, M);
// Check to see if it already exists
unsigned Find = ItinMap[ItinString];
// If new itinerary
if (Find == 0) {
// Emit as ,{ cycles, u1 | u2 | ... | un } // index
OS << ItinString << " // " << Index << "\n";
ItinMap[ItinString] = Find = Index++;
}
// Set up itinerary as location and location + stage count
InstrItinerary Intinerary = { Find, Find + M };
// Locate where to inject into processor itinerary table
std::string Name = ItinData->getValueAsDef("TheClass")->getName();
Find = ItinClassesMap[Name];
// Inject - empty slots will be 0, 0
IL[Find] = Intinerary;
}
// Add process itinerary to list
ProcList.push_back(IL);
}
// End stages table
OS << "};\n";
}
//
// EmitProcessData - Generate data for processor itineraries.
//
void SubtargetEmitter::EmitProcessData(std::ostream &OS,
ProcessorList &ProcList) {
// Get an iterator for processor itinerary stages
ProcessorList::iterator PLI = ProcList.begin();
// For each processor itinerary
RecordList Itins = Records.getAllDerivedDefinitions("ProcessorItineraries");
RecordListIter II(Itins);
while (Record *Itin = II.next()) {
// Get processor itinerary name
std::string Name = Itin->getName();
// Skip default
if (Name == "NoItineraries") continue;
// Begin processor itinerary table
OS << "\n";
OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
// For each itinerary class
IntineraryList &IL = *PLI++;
unsigned Index = 0;
for (IntineraryList::iterator ILI = IL.begin(), E = IL.end(); ILI != E;) {
InstrItinerary &Intinerary = *ILI++;
// Emit in the form of { first, last } // index
if (Intinerary.First == 0) {
OS << " { 0, 0 }";
} else {
OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
}
if (ILI != E) OS << ",";
OS << " // " << Index++ << "\n";
}
// End processor itinerary table
OS << "};\n";
}
}
//
// EmitData - Emits all stages and itineries, folding common patterns.
//
void SubtargetEmitter::EmitData(std::ostream &OS) {
IntMap ItinClassesMap;
ProcessorList ProcList;
// Enumerate all the itinerary classes
unsigned N = CollectAllItinClasses(ItinClassesMap);
// Emit the stage data
EmitStageData(OS, N, ItinClassesMap, ProcList);
// Emit the processor itinerary data
EmitProcessData(OS, ProcList);
}
//
// ParseFeaturesFunction - Produces a subtarget specific function for parsing
// the subtarget features string.
//
void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
RecordList Features = Records.getAllDerivedDefinitions("SubtargetFeature");
sort(Features.begin(), Features.end(), LessRecord());
OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
"// subtarget options.\n"
"void llvm::";
OS << Target;
OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
" const std::string &CPU) {\n"
" SubtargetFeatures Features(FS);\n"
" Features.setCPUIfNone(CPU);\n"
" uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
" FeatureKV, FeatureKVSize);\n";
RecordListIter FI(Features);
while (Record *R = FI.next()) {
std::string Instance = R->getName();
std::string Name = R->getValueAsString("Name");
std::string Type = R->getValueAsString("Type");
std::string Attribute = R->getValueAsString("Attribute");
OS << " " << Attribute << " = (Bits & " << Instance << ") != 0;\n";
}
OS << "}\n";
}
//
// SubtargetEmitter::run - Main subtarget enumeration emitter.
//
void SubtargetEmitter::run(std::ostream &OS) {
Target = CodeGenTarget().getName();
EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
Enumeration(OS, "FuncUnit", true);
OS<<"\n";
// Enumeration(OS, "InstrItinClass", false);
// OS<<"\n";
Enumeration(OS, "SubtargetFeature", true);
OS<<"\n";
FeatureKeyValues(OS);
OS<<"\n";
CPUKeyValues(OS);
OS<<"\n";
EmitData(OS);
OS<<"\n";
ParseFeaturesFunction(OS);
}
|