blob: 1b871a8b66b367a6b0af13cae7fc9d0ab554319f [file] [log] [blame]
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner3d878112006-03-03 02:04:07 +000010// This tablegen backend emits subtarget enumerations.
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000011//
12//===----------------------------------------------------------------------===//
13
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000014#include "CodeGenTarget.h"
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000015#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000016#include "llvm/MC/MCInstrItineraries.h"
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000017#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000018#include "llvm/TableGen/Record.h"
19#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohen9489c042005-10-28 01:43:09 +000020#include <algorithm>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000021#include <map>
22#include <string>
23#include <vector>
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000024using namespace llvm;
25
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000026namespace {
27class SubtargetEmitter {
28
29 RecordKeeper &Records;
30 std::string Target;
31 bool HasItineraries;
32
33 void Enumeration(raw_ostream &OS, const char *ClassName, bool isBits);
34 unsigned FeatureKeyValues(raw_ostream &OS);
35 unsigned CPUKeyValues(raw_ostream &OS);
36 unsigned CollectAllItinClasses(raw_ostream &OS,
37 std::map<std::string,unsigned> &ItinClassesMap,
38 std::vector<Record*> &ItinClassList);
39 void FormItineraryStageString(const std::string &Names,
40 Record *ItinData, std::string &ItinString,
41 unsigned &NStages);
42 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
43 unsigned &NOperandCycles);
44 void FormItineraryBypassString(const std::string &Names,
45 Record *ItinData,
46 std::string &ItinString, unsigned NOperandCycles);
47 void EmitStageAndOperandCycleData(raw_ostream &OS, unsigned NItinClasses,
48 std::map<std::string, unsigned> &ItinClassesMap,
49 std::vector<Record*> &ItinClassList,
50 std::vector<std::vector<InstrItinerary> > &ProcList);
51 void EmitItineraryProp(raw_ostream &OS, const Record *R, const char *Name,
52 char Separator);
53 void EmitProcessorData(raw_ostream &OS,
54 std::vector<Record*> &ItinClassList,
55 std::vector<std::vector<InstrItinerary> > &ProcList);
56 void EmitProcessorLookup(raw_ostream &OS);
57 void EmitData(raw_ostream &OS);
58 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
59 unsigned NumProcs);
60
61public:
62 SubtargetEmitter(RecordKeeper &R) : Records(R), HasItineraries(false) {}
63
64 void run(raw_ostream &o);
65
66};
67} // End anonymous namespace
68
Jim Laskey7dc02042005-10-22 07:59:56 +000069//
Jim Laskey581a8f72005-10-26 17:30:34 +000070// Enumeration - Emit the specified class as an enumeration.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000071//
Daniel Dunbar1a551802009-07-03 00:10:29 +000072void SubtargetEmitter::Enumeration(raw_ostream &OS,
Jim Laskey581a8f72005-10-26 17:30:34 +000073 const char *ClassName,
74 bool isBits) {
Jim Laskey908ae272005-10-28 15:20:43 +000075 // Get all records of class and sort
Jim Laskeyf7bcde02005-10-28 21:47:29 +000076 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
Duraid Madina42d24c72005-12-30 14:56:37 +000077 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000078
Evan Chengb6a63882011-04-15 19:35:46 +000079 unsigned N = DefList.size();
Evan Cheng94214702011-07-01 20:45:01 +000080 if (N == 0)
81 return;
Evan Chengb6a63882011-04-15 19:35:46 +000082 if (N > 64) {
83 errs() << "Too many (> 64) subtarget features!\n";
84 exit(1);
85 }
86
Evan Cheng94214702011-07-01 20:45:01 +000087 OS << "namespace " << Target << " {\n";
88
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000089 // For bit flag enumerations with more than 32 items, emit constants.
90 // Emit an enum for everything else.
91 if (isBits && N > 32) {
92 // For each record
93 for (unsigned i = 0; i < N; i++) {
94 // Next record
95 Record *Def = DefList[i];
Evan Cheng94214702011-07-01 20:45:01 +000096
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000097 // Get and emit name and expression (1 << i)
98 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
99 }
100 } else {
101 // Open enumeration
102 OS << "enum {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000103
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000104 // For each record
105 for (unsigned i = 0; i < N;) {
106 // Next record
107 Record *Def = DefList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000108
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000109 // Get and emit name
110 OS << " " << Def->getName();
Jim Laskey908ae272005-10-28 15:20:43 +0000111
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000112 // If bit flags then emit expression (1 << i)
113 if (isBits) OS << " = " << " 1ULL << " << i;
Andrew Trickda96cf22011-04-01 01:56:55 +0000114
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000115 // Depending on 'if more in the list' emit comma
116 if (++i < N) OS << ",";
117
118 OS << "\n";
119 }
120
121 // Close enumeration
122 OS << "};\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000123 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000124
Evan Cheng94214702011-07-01 20:45:01 +0000125 OS << "}\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000126}
127
128//
Bill Wendling4222d802007-05-04 20:38:40 +0000129// FeatureKeyValues - Emit data of all the subtarget features. Used by the
130// command line.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000131//
Evan Cheng94214702011-07-01 20:45:01 +0000132unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000133 // Gather and sort all the features
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000134 std::vector<Record*> FeatureList =
135 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng94214702011-07-01 20:45:01 +0000136
137 if (FeatureList.empty())
138 return 0;
139
Jim Grosbach7c9a7722008-09-11 17:05:32 +0000140 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000141
Jim Laskey908ae272005-10-28 15:20:43 +0000142 // Begin feature table
Jim Laskey581a8f72005-10-26 17:30:34 +0000143 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000144 << "extern const llvm::SubtargetFeatureKV " << Target
145 << "FeatureKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000146
Jim Laskey908ae272005-10-28 15:20:43 +0000147 // For each feature
Evan Cheng94214702011-07-01 20:45:01 +0000148 unsigned NumFeatures = 0;
Jim Laskeydbe40062006-12-12 20:55:58 +0000149 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000150 // Next feature
151 Record *Feature = FeatureList[i];
152
Bill Wendling4222d802007-05-04 20:38:40 +0000153 const std::string &Name = Feature->getName();
154 const std::string &CommandLineName = Feature->getValueAsString("Name");
155 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickda96cf22011-04-01 01:56:55 +0000156
Jim Laskeydbe40062006-12-12 20:55:58 +0000157 if (CommandLineName.empty()) continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000158
Jim Grosbachda4231f2009-03-26 16:17:51 +0000159 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000160 OS << " { "
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000161 << "\"" << CommandLineName << "\", "
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000162 << "\"" << Desc << "\", "
Evan Cheng94214702011-07-01 20:45:01 +0000163 << Target << "::" << Name << ", ";
Bill Wendling4222d802007-05-04 20:38:40 +0000164
Andrew Trickda96cf22011-04-01 01:56:55 +0000165 const std::vector<Record*> &ImpliesList =
Bill Wendling4222d802007-05-04 20:38:40 +0000166 Feature->getValueAsListOfDefs("Implies");
Andrew Trickda96cf22011-04-01 01:56:55 +0000167
Bill Wendling4222d802007-05-04 20:38:40 +0000168 if (ImpliesList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000169 OS << "0ULL";
Bill Wendling4222d802007-05-04 20:38:40 +0000170 } else {
171 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000172 OS << Target << "::" << ImpliesList[j]->getName();
Bill Wendling4222d802007-05-04 20:38:40 +0000173 if (++j < M) OS << " | ";
174 }
175 }
176
177 OS << " }";
Evan Cheng94214702011-07-01 20:45:01 +0000178 ++NumFeatures;
Andrew Trickda96cf22011-04-01 01:56:55 +0000179
Jim Laskey10b1dd92005-10-31 17:16:01 +0000180 // Depending on 'if more in the list' emit comma
Jim Laskeydbe40062006-12-12 20:55:58 +0000181 if ((i + 1) < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000182
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000183 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000184 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000185
Jim Laskey908ae272005-10-28 15:20:43 +0000186 // End feature table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000187 OS << "};\n";
188
Evan Cheng94214702011-07-01 20:45:01 +0000189 return NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000190}
191
192//
193// CPUKeyValues - Emit data of all the subtarget processors. Used by command
194// line.
195//
Evan Cheng94214702011-07-01 20:45:01 +0000196unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000197 // Gather and sort processor information
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000198 std::vector<Record*> ProcessorList =
199 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000200 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000201
Jim Laskey908ae272005-10-28 15:20:43 +0000202 // Begin processor table
Jim Laskey581a8f72005-10-26 17:30:34 +0000203 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000204 << "extern const llvm::SubtargetFeatureKV " << Target
205 << "SubTypeKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000206
Jim Laskey908ae272005-10-28 15:20:43 +0000207 // For each processor
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000208 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
209 // Next processor
210 Record *Processor = ProcessorList[i];
211
Bill Wendling4222d802007-05-04 20:38:40 +0000212 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickda96cf22011-04-01 01:56:55 +0000213 const std::vector<Record*> &FeatureList =
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000214 Processor->getValueAsListOfDefs("Features");
Andrew Trickda96cf22011-04-01 01:56:55 +0000215
Jim Laskey908ae272005-10-28 15:20:43 +0000216 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000217 OS << " { "
218 << "\"" << Name << "\", "
219 << "\"Select the " << Name << " processor\", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000220
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000221 if (FeatureList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000222 OS << "0ULL";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000223 } else {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000224 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000225 OS << Target << "::" << FeatureList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000226 if (++j < M) OS << " | ";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000227 }
228 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000229
Bill Wendling4222d802007-05-04 20:38:40 +0000230 // The "0" is for the "implies" section of this data structure.
Evan Chengb6a63882011-04-15 19:35:46 +0000231 OS << ", 0ULL }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000232
Jim Laskey10b1dd92005-10-31 17:16:01 +0000233 // Depending on 'if more in the list' emit comma
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000234 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000235
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000236 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000237 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000238
Jim Laskey908ae272005-10-28 15:20:43 +0000239 // End processor table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000240 OS << "};\n";
241
Evan Cheng94214702011-07-01 20:45:01 +0000242 return ProcessorList.size();
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000243}
Jim Laskey7dc02042005-10-22 07:59:56 +0000244
Jim Laskey581a8f72005-10-26 17:30:34 +0000245//
Jim Laskey0d841e02005-10-27 19:47:21 +0000246// CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
Jim Laskey908ae272005-10-28 15:20:43 +0000247// Returns itinerary class count.
Jim Laskey0d841e02005-10-27 19:47:21 +0000248//
Evan Cheng5f54ce32010-09-09 18:18:55 +0000249unsigned SubtargetEmitter::
250CollectAllItinClasses(raw_ostream &OS,
251 std::map<std::string, unsigned> &ItinClassesMap,
252 std::vector<Record*> &ItinClassList) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000253 // For each itinerary class
254 unsigned N = ItinClassList.size();
255 for (unsigned i = 0; i < N; i++) {
256 // Next itinerary class
Bill Wendling4222d802007-05-04 20:38:40 +0000257 const Record *ItinClass = ItinClassList[i];
Jim Laskey908ae272005-10-28 15:20:43 +0000258 // Get name of itinerary class
Jim Laskey908ae272005-10-28 15:20:43 +0000259 // Assign itinerary class a unique number
Bill Wendling4222d802007-05-04 20:38:40 +0000260 ItinClassesMap[ItinClass->getName()] = i;
Jim Laskey0d841e02005-10-27 19:47:21 +0000261 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000262
Jim Laskey908ae272005-10-28 15:20:43 +0000263 // Return itinerary class count
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000264 return N;
Jim Laskey0d841e02005-10-27 19:47:21 +0000265}
266
267//
David Goodwinfac85412009-08-17 16:02:57 +0000268// FormItineraryStageString - Compose a string containing the stage
269// data initialization for the specified itinerary. N is the number
270// of stages.
Jim Laskey0d841e02005-10-27 19:47:21 +0000271//
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000272void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
273 Record *ItinData,
David Goodwinfac85412009-08-17 16:02:57 +0000274 std::string &ItinString,
275 unsigned &NStages) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000276 // Get states list
Bill Wendling4222d802007-05-04 20:38:40 +0000277 const std::vector<Record*> &StageList =
278 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey908ae272005-10-28 15:20:43 +0000279
280 // For each stage
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000281 unsigned N = NStages = StageList.size();
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000282 for (unsigned i = 0; i < N;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000283 // Next stage
Bill Wendling4222d802007-05-04 20:38:40 +0000284 const Record *Stage = StageList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000285
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000286 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey0d841e02005-10-27 19:47:21 +0000287 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskey7f39c142005-11-03 22:47:41 +0000288 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000289
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000290 // Get unit list
Bill Wendling4222d802007-05-04 20:38:40 +0000291 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickda96cf22011-04-01 01:56:55 +0000292
Jim Laskey908ae272005-10-28 15:20:43 +0000293 // For each unit
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000294 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000295 // Add name and bitwise or
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000296 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000297 if (++j < M) ItinString += " | ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000298 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000299
David Goodwin1a8f36e2009-08-12 18:31:53 +0000300 int TimeInc = Stage->getValueAsInt("TimeInc");
301 ItinString += ", " + itostr(TimeInc);
302
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000303 int Kind = Stage->getValueAsInt("Kind");
304 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
305
Jim Laskey908ae272005-10-28 15:20:43 +0000306 // Close off stage
307 ItinString += " }";
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000308 if (++i < N) ItinString += ", ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000309 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000310}
311
312//
David Goodwinfac85412009-08-17 16:02:57 +0000313// FormItineraryOperandCycleString - Compose a string containing the
314// operand cycle initialization for the specified itinerary. N is the
315// number of operands that has cycles specified.
Jim Laskey0d841e02005-10-27 19:47:21 +0000316//
David Goodwinfac85412009-08-17 16:02:57 +0000317void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
318 std::string &ItinString, unsigned &NOperandCycles) {
319 // Get operand cycle list
320 const std::vector<int64_t> &OperandCycleList =
321 ItinData->getValueAsListOfInts("OperandCycles");
322
323 // For each operand cycle
324 unsigned N = NOperandCycles = OperandCycleList.size();
325 for (unsigned i = 0; i < N;) {
326 // Next operand cycle
327 const int OCycle = OperandCycleList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000328
David Goodwinfac85412009-08-17 16:02:57 +0000329 ItinString += " " + itostr(OCycle);
330 if (++i < N) ItinString += ", ";
331 }
332}
333
Evan Cheng63d66ee2010-09-28 23:50:49 +0000334void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
335 Record *ItinData,
336 std::string &ItinString,
337 unsigned NOperandCycles) {
338 const std::vector<Record*> &BypassList =
339 ItinData->getValueAsListOfDefs("Bypasses");
340 unsigned N = BypassList.size();
Evan Cheng3881cb72010-09-29 22:42:35 +0000341 unsigned i = 0;
342 for (; i < N;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000343 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng3881cb72010-09-29 22:42:35 +0000344 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000345 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000346 for (; i < NOperandCycles;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000347 ItinString += " 0";
Evan Cheng3881cb72010-09-29 22:42:35 +0000348 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000349 }
350}
351
David Goodwinfac85412009-08-17 16:02:57 +0000352//
353// EmitStageAndOperandCycleData - Generate unique itinerary stages and
354// operand cycle tables. Record itineraries for processors.
355//
356void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS,
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000357 unsigned NItinClasses,
Evan Cheng5f54ce32010-09-09 18:18:55 +0000358 std::map<std::string, unsigned> &ItinClassesMap,
359 std::vector<Record*> &ItinClassList,
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000360 std::vector<std::vector<InstrItinerary> > &ProcList) {
Jim Laskey908ae272005-10-28 15:20:43 +0000361 // Gather processor iteraries
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000362 std::vector<Record*> ProcItinList =
363 Records.getAllDerivedDefinitions("ProcessorItineraries");
Andrew Trickda96cf22011-04-01 01:56:55 +0000364
Jim Laskey908ae272005-10-28 15:20:43 +0000365 // If just no itinerary then don't bother
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000366 if (ProcItinList.size() < 2) return;
Jim Laskey908ae272005-10-28 15:20:43 +0000367
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000368 // Emit functional units for all the itineraries.
369 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
370 // Next record
371 Record *Proc = ProcItinList[i];
372
373 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
374 if (FUs.empty())
375 continue;
376
377 const std::string &Name = Proc->getName();
378 OS << "\n// Functional units for itineraries \"" << Name << "\"\n"
379 << "namespace " << Name << "FU {\n";
380
381 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
382 OS << " const unsigned " << FUs[j]->getName()
383 << " = 1 << " << j << ";\n";
384
385 OS << "}\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000386
387 std::vector<Record*> BPs = Proc->getValueAsListOfDefs("BP");
Evan Cheng3881cb72010-09-29 22:42:35 +0000388 if (BPs.size()) {
389 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
390 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000391
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000392 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000393 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000394 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng3881cb72010-09-29 22:42:35 +0000395 << " = 1 << " << j << ";\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000396
Evan Cheng3881cb72010-09-29 22:42:35 +0000397 OS << "}\n";
398 }
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000399 }
400
Jim Laskey908ae272005-10-28 15:20:43 +0000401 // Begin stages table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000402 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
403 "Stages[] = {\n";
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000404 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000405
David Goodwinfac85412009-08-17 16:02:57 +0000406 // Begin operand cycle table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000407 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000408 "OperandCycles[] = {\n";
David Goodwinfac85412009-08-17 16:02:57 +0000409 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000410
411 // Begin pipeline bypass table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000412 std::string BypassTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000413 "ForwardingPathes[] = {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000414 BypassTable += " 0, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000415
David Goodwinfac85412009-08-17 16:02:57 +0000416 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng3881cb72010-09-29 22:42:35 +0000417 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000418 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
419 // Next record
420 Record *Proc = ProcItinList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000421
Jim Laskey908ae272005-10-28 15:20:43 +0000422 // Get processor itinerary name
Bill Wendling4222d802007-05-04 20:38:40 +0000423 const std::string &Name = Proc->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000424
Jim Laskey908ae272005-10-28 15:20:43 +0000425 // Skip default
Jim Laskey0d841e02005-10-27 19:47:21 +0000426 if (Name == "NoItineraries") continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000427
Jim Laskey908ae272005-10-28 15:20:43 +0000428 // Create and expand processor itinerary to cover all itinerary classes
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000429 std::vector<InstrItinerary> ItinList;
430 ItinList.resize(NItinClasses);
Andrew Trickda96cf22011-04-01 01:56:55 +0000431
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000432 // Get itinerary data list
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000433 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
Andrew Trickda96cf22011-04-01 01:56:55 +0000434
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000435 // For each itinerary data
436 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
437 // Next itinerary data
438 Record *ItinData = ItinDataList[j];
Andrew Trickda96cf22011-04-01 01:56:55 +0000439
Jim Laskey908ae272005-10-28 15:20:43 +0000440 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000441 std::string ItinStageString;
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000442 unsigned NStages;
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000443 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000444
David Goodwinfac85412009-08-17 16:02:57 +0000445 // Get string and operand cycle count
446 std::string ItinOperandCycleString;
447 unsigned NOperandCycles;
448 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
449 NOperandCycles);
450
Evan Cheng63d66ee2010-09-28 23:50:49 +0000451 std::string ItinBypassString;
452 FormItineraryBypassString(Name, ItinData, ItinBypassString,
453 NOperandCycles);
454
David Goodwinfac85412009-08-17 16:02:57 +0000455 // Check to see if stage already exists and create if it doesn't
456 unsigned FindStage = 0;
457 if (NStages > 0) {
458 FindStage = ItinStageMap[ItinStageString];
459 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000460 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
461 StageTable += ItinStageString + ", // " + itostr(StageCount);
462 if (NStages > 1)
463 StageTable += "-" + itostr(StageCount + NStages - 1);
464 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000465 // Record Itin class number.
466 ItinStageMap[ItinStageString] = FindStage = StageCount;
467 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000468 }
469 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000470
David Goodwinfac85412009-08-17 16:02:57 +0000471 // Check to see if operand cycle already exists and create if it doesn't
472 unsigned FindOperandCycle = 0;
473 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000474 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
475 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000476 if (FindOperandCycle == 0) {
477 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000478 OperandCycleTable += ItinOperandCycleString + ", // ";
479 std::string OperandIdxComment = itostr(OperandCycleCount);
480 if (NOperandCycles > 1)
481 OperandIdxComment += "-"
482 + itostr(OperandCycleCount + NOperandCycles - 1);
483 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000484 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000485 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000486 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000487 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000488 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000489 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000490 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000491 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000492
Jim Laskey908ae272005-10-28 15:20:43 +0000493 // Locate where to inject into processor itinerary table
Bill Wendling4222d802007-05-04 20:38:40 +0000494 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
David Goodwinfac85412009-08-17 16:02:57 +0000495 unsigned Find = ItinClassesMap[Name];
Andrew Trickda96cf22011-04-01 01:56:55 +0000496
Evan Cheng5f54ce32010-09-09 18:18:55 +0000497 // Set up itinerary as location and location + stage count
498 unsigned NumUOps = ItinClassList[Find]->getValueAsInt("NumMicroOps");
499 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
500 FindOperandCycle,
501 FindOperandCycle + NOperandCycles};
502
Jim Laskey908ae272005-10-28 15:20:43 +0000503 // Inject - empty slots will be 0, 0
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000504 ItinList[Find] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000505 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000506
Jim Laskey908ae272005-10-28 15:20:43 +0000507 // Add process itinerary to list
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000508 ProcList.push_back(ItinList);
Jim Laskey0d841e02005-10-27 19:47:21 +0000509 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000510
Jim Laskey7f39c142005-11-03 22:47:41 +0000511 // Closing stage
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000512 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
David Goodwinfac85412009-08-17 16:02:57 +0000513 StageTable += "};\n";
514
515 // Closing operand cycles
516 OperandCycleTable += " 0 // End itinerary\n";
517 OperandCycleTable += "};\n";
518
Evan Cheng63d66ee2010-09-28 23:50:49 +0000519 BypassTable += " 0 // End itinerary\n";
520 BypassTable += "};\n";
521
David Goodwinfac85412009-08-17 16:02:57 +0000522 // Emit tables.
523 OS << StageTable;
524 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000525 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000526}
527
Andrew Trickfc992992012-06-05 03:44:40 +0000528void SubtargetEmitter::EmitItineraryProp(raw_ostream &OS, const Record *R,
529 const char *Name, char Separator) {
530 OS << " ";
531 int V = R->getValueAsInt(Name);
532 if (V >= 0)
533 OS << V << Separator << " // " << Name;
534 else
Andrew Trick0076ad72012-06-08 18:25:47 +0000535 OS << "InstrItineraryProps::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000536 OS << '\n';
537}
538
Jim Laskey0d841e02005-10-27 19:47:21 +0000539//
Jim Laskey10b1dd92005-10-31 17:16:01 +0000540// EmitProcessorData - Generate data for processor itineraries.
Jim Laskey0d841e02005-10-27 19:47:21 +0000541//
Andrew Trick23482322011-04-01 02:22:47 +0000542void SubtargetEmitter::
543EmitProcessorData(raw_ostream &OS,
544 std::vector<Record*> &ItinClassList,
545 std::vector<std::vector<InstrItinerary> > &ProcList) {
Andrew Trickfc992992012-06-05 03:44:40 +0000546
Jim Laskey908ae272005-10-28 15:20:43 +0000547 // Get an iterator for processor itinerary stages
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000548 std::vector<std::vector<InstrItinerary> >::iterator
549 ProcListIter = ProcList.begin();
Andrew Trickda96cf22011-04-01 01:56:55 +0000550
Jim Laskey908ae272005-10-28 15:20:43 +0000551 // For each processor itinerary
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000552 std::vector<Record*> Itins =
553 Records.getAllDerivedDefinitions("ProcessorItineraries");
554 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
555 // Next record
556 Record *Itin = Itins[i];
557
Jim Laskey908ae272005-10-28 15:20:43 +0000558 // Get processor itinerary name
Bill Wendling4222d802007-05-04 20:38:40 +0000559 const std::string &Name = Itin->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000560
Jim Laskey908ae272005-10-28 15:20:43 +0000561 // Skip default
Jim Laskey0d841e02005-10-27 19:47:21 +0000562 if (Name == "NoItineraries") continue;
563
Andrew Trickfc992992012-06-05 03:44:40 +0000564 // Begin processor itinerary properties
565 OS << "\n";
566 OS << "static const llvm::InstrItineraryProps " << Name << "Props(\n";
567 EmitItineraryProp(OS, Itin, "IssueWidth", ',');
568 EmitItineraryProp(OS, Itin, "MinLatency", ',');
569 EmitItineraryProp(OS, Itin, "LoadLatency", ',');
570 EmitItineraryProp(OS, Itin, "HighLatency", ' ');
571 OS << ");\n";
572
Jim Laskey908ae272005-10-28 15:20:43 +0000573 // Begin processor itinerary table
Jim Laskey0d841e02005-10-27 19:47:21 +0000574 OS << "\n";
Andrew Trickfc992992012-06-05 03:44:40 +0000575 OS << "static const llvm::InstrItinerary " << Name << "Entries"
576 << "[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000577
Jim Laskey908ae272005-10-28 15:20:43 +0000578 // For each itinerary class
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000579 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
Andrew Trick23482322011-04-01 02:22:47 +0000580 assert(ItinList.size() == ItinClassList.size() && "bad itinerary");
David Goodwin1f528952009-09-24 20:22:50 +0000581 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000582 InstrItinerary &Intinerary = ItinList[j];
Andrew Trickda96cf22011-04-01 01:56:55 +0000583
584 // Emit in the form of
David Goodwinfac85412009-08-17 16:02:57 +0000585 // { firstStage, lastStage, firstCycle, lastCycle } // index
586 if (Intinerary.FirstStage == 0) {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000587 OS << " { 1, 0, 0, 0, 0 }";
Jim Laskey0d841e02005-10-27 19:47:21 +0000588 } else {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000589 OS << " { " <<
590 Intinerary.NumMicroOps << ", " <<
Andrew Trickda96cf22011-04-01 01:56:55 +0000591 Intinerary.FirstStage << ", " <<
592 Intinerary.LastStage << ", " <<
593 Intinerary.FirstOperandCycle << ", " <<
David Goodwinfac85412009-08-17 16:02:57 +0000594 Intinerary.LastOperandCycle << " }";
Jim Laskey0d841e02005-10-27 19:47:21 +0000595 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000596
Andrew Trick23482322011-04-01 02:22:47 +0000597 OS << ", // " << j << " " << ItinClassList[j]->getName() << "\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000598 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000599
Jim Laskey908ae272005-10-28 15:20:43 +0000600 // End processor itinerary table
Evan Cheng5f54ce32010-09-09 18:18:55 +0000601 OS << " { 1, ~0U, ~0U, ~0U, ~0U } // end marker\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000602 OS << "};\n";
Andrew Trickfc992992012-06-05 03:44:40 +0000603
604 OS << '\n';
605 OS << "static const llvm::InstrItinerarySubtargetValue "
606 << Name << " = {\n";
607 OS << " &" << Name << "Props,\n";
608 OS << " " << Name << "Entries\n";
609 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000610 }
Jim Laskey10b1dd92005-10-31 17:16:01 +0000611}
612
613//
614// EmitProcessorLookup - generate cpu name to itinerary lookup table.
615//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000616void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +0000617 // Gather and sort processor information
618 std::vector<Record*> ProcessorList =
619 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000620 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +0000621
622 // Begin processor table
623 OS << "\n";
624 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000625 << "extern const llvm::SubtargetInfoKV "
Evan Cheng94214702011-07-01 20:45:01 +0000626 << Target << "ProcItinKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000627
Jim Laskey10b1dd92005-10-31 17:16:01 +0000628 // For each processor
629 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
630 // Next processor
631 Record *Processor = ProcessorList[i];
632
Bill Wendling4222d802007-05-04 20:38:40 +0000633 const std::string &Name = Processor->getValueAsString("Name");
634 const std::string &ProcItin =
635 Processor->getValueAsDef("ProcItin")->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000636
Jim Laskey10b1dd92005-10-31 17:16:01 +0000637 // Emit as { "cpu", procinit },
638 OS << " { "
639 << "\"" << Name << "\", "
640 << "(void *)&" << ProcItin;
Andrew Trickda96cf22011-04-01 01:56:55 +0000641
Jim Laskey10b1dd92005-10-31 17:16:01 +0000642 OS << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000643
Jim Laskey10b1dd92005-10-31 17:16:01 +0000644 // Depending on ''if more in the list'' emit comma
645 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000646
Jim Laskey10b1dd92005-10-31 17:16:01 +0000647 OS << "\n";
648 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000649
Jim Laskey10b1dd92005-10-31 17:16:01 +0000650 // End processor table
651 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000652}
653
654//
655// EmitData - Emits all stages and itineries, folding common patterns.
656//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000657void SubtargetEmitter::EmitData(raw_ostream &OS) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000658 std::map<std::string, unsigned> ItinClassesMap;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000659 // Gather and sort all itinerary classes
660 std::vector<Record*> ItinClassList =
661 Records.getAllDerivedDefinitions("InstrItinClass");
662 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
Andrew Trickda96cf22011-04-01 01:56:55 +0000663
Jim Laskey908ae272005-10-28 15:20:43 +0000664 // Enumerate all the itinerary classes
Evan Cheng5f54ce32010-09-09 18:18:55 +0000665 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap,
666 ItinClassList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000667 // Make sure the rest is worth the effort
Chris Lattner387e4bd2006-01-27 01:41:55 +0000668 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
Andrew Trickda96cf22011-04-01 01:56:55 +0000669
Jim Laskey6cee6302005-11-01 20:06:59 +0000670 if (HasItineraries) {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000671 std::vector<std::vector<InstrItinerary> > ProcList;
Jim Laskey6cee6302005-11-01 20:06:59 +0000672 // Emit the stage data
Evan Cheng5f54ce32010-09-09 18:18:55 +0000673 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap,
674 ItinClassList, ProcList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000675 // Emit the processor itinerary data
Andrew Trick23482322011-04-01 02:22:47 +0000676 EmitProcessorData(OS, ItinClassList, ProcList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000677 // Emit the processor lookup data
678 EmitProcessorLookup(OS);
679 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000680}
681
682//
Jim Laskey581a8f72005-10-26 17:30:34 +0000683// ParseFeaturesFunction - Produces a subtarget specific function for parsing
684// the subtarget features string.
685//
Evan Cheng94214702011-07-01 20:45:01 +0000686void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
687 unsigned NumFeatures,
688 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000689 std::vector<Record*> Features =
690 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +0000691 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +0000692
Andrew Trickda96cf22011-04-01 01:56:55 +0000693 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
694 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +0000695 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +0000696 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000697 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +0000698 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel3f696e52012-06-12 04:21:36 +0000699 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +0000700
701 if (Features.empty()) {
702 OS << "}\n";
703 return;
704 }
705
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000706 OS << " uint64_t Bits = ReInitMCSubtargetInfo(CPU, FS);\n";
Bill Wendling4222d802007-05-04 20:38:40 +0000707
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000708 for (unsigned i = 0; i < Features.size(); i++) {
709 // Next record
710 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +0000711 const std::string &Instance = R->getName();
712 const std::string &Value = R->getValueAsString("Value");
713 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +0000714
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000715 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000716 OS << " if ((Bits & " << Target << "::"
717 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000718 << Attribute << " = " << Value << ";\n";
719 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000720 OS << " if ((Bits & " << Target << "::"
721 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +0000722 << Attribute << " < " << Value << ") "
723 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +0000724 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000725
Evan Cheng276365d2011-06-30 01:53:36 +0000726 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +0000727}
728
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000729//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000730// SubtargetEmitter::run - Main subtarget enumeration emitter.
731//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000732void SubtargetEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +0000733 Target = CodeGenTarget(Records).getName();
Jim Laskey581a8f72005-10-26 17:30:34 +0000734
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000735 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000736
Evan Chengebdeeab2011-07-08 01:53:10 +0000737 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
738 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
739
740 OS << "namespace llvm {\n";
741 Enumeration(OS, "SubtargetFeature", true);
742 OS << "} // End llvm namespace \n";
743 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
744
Evan Cheng94214702011-07-01 20:45:01 +0000745 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
746 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000747
Evan Cheng94214702011-07-01 20:45:01 +0000748 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000749#if 0
750 OS << "namespace {\n";
751#endif
Evan Cheng94214702011-07-01 20:45:01 +0000752 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000753 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +0000754 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000755 OS << "\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000756 EmitData(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000757 OS << "\n";
758#if 0
759 OS << "}\n";
760#endif
Evan Cheng94214702011-07-01 20:45:01 +0000761
762 // MCInstrInfo initialization routine.
763 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +0000764 << "MCSubtargetInfo(MCSubtargetInfo *II, "
765 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
766 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +0000767 if (NumFeatures)
768 OS << Target << "FeatureKV, ";
769 else
770 OS << "0, ";
771 if (NumProcs)
772 OS << Target << "SubTypeKV, ";
773 else
774 OS << "0, ";
775 if (HasItineraries) {
776 OS << Target << "ProcItinKV, "
777 << Target << "Stages, "
778 << Target << "OperandCycles, "
779 << Target << "ForwardingPathes, ";
780 } else
781 OS << "0, 0, 0, 0, ";
782 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
783
784 OS << "} // End llvm namespace \n";
785
786 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
787
788 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
789 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
790
791 OS << "#include \"llvm/Support/Debug.h\"\n";
792 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
793 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
794
795 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
796
Evan Cheng5b1b44892011-07-01 21:01:15 +0000797 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +0000798 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
799 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
800
801 std::string ClassName = Target + "GenSubtargetInfo";
802 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000803 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +0000804 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000805 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
806 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000807 << "public:\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +0000808 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000809 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +0000810 << "};\n";
811 OS << "} // End llvm namespace \n";
812
813 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
814
815 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
816 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
817
818 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000819 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
820 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000821 if (HasItineraries) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000822 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcItinKV[];\n";
823 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
824 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
825 OS << "extern const unsigned " << Target << "ForwardingPathes[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000826 }
827
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000828 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
829 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +0000830 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +0000831 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +0000832 if (NumFeatures)
833 OS << Target << "FeatureKV, ";
834 else
835 OS << "0, ";
836 if (NumProcs)
837 OS << Target << "SubTypeKV, ";
838 else
839 OS << "0, ";
840 if (HasItineraries) {
841 OS << Target << "ProcItinKV, "
842 << Target << "Stages, "
843 << Target << "OperandCycles, "
844 << Target << "ForwardingPathes, ";
845 } else
846 OS << "0, 0, 0, 0, ";
847 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
848 OS << "} // End llvm namespace \n";
849
850 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000851}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000852
853namespace llvm {
854
855void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
856 SubtargetEmitter(RK).run(OS);
857}
858
859} // End llvm namespace