blob: 8089908c1fe44736640c8dda77abcfd40e203de1 [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)
Hal Finkelb460a332012-06-22 20:27:13 +0000382 OS << " const unsigned " << FUs[j]->getName()
383 << " = 1 << " << j << ";\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000384
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 Laskeyf7bcde02005-10-28 21:47:29 +0000425 // Get itinerary data list
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000426 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
Andrew Trickd85934b2012-06-22 03:58:51 +0000427 std::vector<InstrItinerary> ItinList;
428
429 // Add an empty itinerary.
430 if (ItinDataList.empty()) {
431 ProcList.push_back(ItinList);
432 continue;
433 }
434
435 // Expand processor itinerary to cover all itinerary classes
436 ItinList.resize(NItinClasses);
Andrew Trickda96cf22011-04-01 01:56:55 +0000437
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000438 // For each itinerary data
439 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
440 // Next itinerary data
441 Record *ItinData = ItinDataList[j];
Andrew Trickda96cf22011-04-01 01:56:55 +0000442
Jim Laskey908ae272005-10-28 15:20:43 +0000443 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000444 std::string ItinStageString;
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000445 unsigned NStages;
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000446 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000447
David Goodwinfac85412009-08-17 16:02:57 +0000448 // Get string and operand cycle count
449 std::string ItinOperandCycleString;
450 unsigned NOperandCycles;
451 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
452 NOperandCycles);
453
Evan Cheng63d66ee2010-09-28 23:50:49 +0000454 std::string ItinBypassString;
455 FormItineraryBypassString(Name, ItinData, ItinBypassString,
456 NOperandCycles);
457
David Goodwinfac85412009-08-17 16:02:57 +0000458 // Check to see if stage already exists and create if it doesn't
459 unsigned FindStage = 0;
460 if (NStages > 0) {
461 FindStage = ItinStageMap[ItinStageString];
462 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000463 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
464 StageTable += ItinStageString + ", // " + itostr(StageCount);
465 if (NStages > 1)
466 StageTable += "-" + itostr(StageCount + NStages - 1);
467 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000468 // Record Itin class number.
469 ItinStageMap[ItinStageString] = FindStage = StageCount;
470 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000471 }
472 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000473
David Goodwinfac85412009-08-17 16:02:57 +0000474 // Check to see if operand cycle already exists and create if it doesn't
475 unsigned FindOperandCycle = 0;
476 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000477 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
478 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000479 if (FindOperandCycle == 0) {
480 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000481 OperandCycleTable += ItinOperandCycleString + ", // ";
482 std::string OperandIdxComment = itostr(OperandCycleCount);
483 if (NOperandCycles > 1)
484 OperandIdxComment += "-"
485 + itostr(OperandCycleCount + NOperandCycles - 1);
486 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000487 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000488 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000489 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000490 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000491 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000492 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000493 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000494 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000495
Jim Laskey908ae272005-10-28 15:20:43 +0000496 // Locate where to inject into processor itinerary table
Bill Wendling4222d802007-05-04 20:38:40 +0000497 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
David Goodwinfac85412009-08-17 16:02:57 +0000498 unsigned Find = ItinClassesMap[Name];
Andrew Trickda96cf22011-04-01 01:56:55 +0000499
Evan Cheng5f54ce32010-09-09 18:18:55 +0000500 // Set up itinerary as location and location + stage count
Chandler Carruth506bb192012-07-02 18:28:34 +0000501 int NumUOps = ItinData->getValueAsInt("NumMicroOps");
Evan Cheng5f54ce32010-09-09 18:18:55 +0000502 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
503 FindOperandCycle,
504 FindOperandCycle + NOperandCycles};
505
Jim Laskey908ae272005-10-28 15:20:43 +0000506 // Inject - empty slots will be 0, 0
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000507 ItinList[Find] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000508 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000509
Jim Laskey908ae272005-10-28 15:20:43 +0000510 // Add process itinerary to list
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000511 ProcList.push_back(ItinList);
Jim Laskey0d841e02005-10-27 19:47:21 +0000512 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000513
Jim Laskey7f39c142005-11-03 22:47:41 +0000514 // Closing stage
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000515 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
David Goodwinfac85412009-08-17 16:02:57 +0000516 StageTable += "};\n";
517
518 // Closing operand cycles
519 OperandCycleTable += " 0 // End itinerary\n";
520 OperandCycleTable += "};\n";
521
Evan Cheng63d66ee2010-09-28 23:50:49 +0000522 BypassTable += " 0 // End itinerary\n";
523 BypassTable += "};\n";
524
David Goodwinfac85412009-08-17 16:02:57 +0000525 // Emit tables.
526 OS << StageTable;
527 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000528 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000529}
530
Andrew Trickfc992992012-06-05 03:44:40 +0000531void SubtargetEmitter::EmitItineraryProp(raw_ostream &OS, const Record *R,
532 const char *Name, char Separator) {
533 OS << " ";
534 int V = R->getValueAsInt(Name);
535 if (V >= 0)
536 OS << V << Separator << " // " << Name;
537 else
Andrew Trick0076ad72012-06-08 18:25:47 +0000538 OS << "InstrItineraryProps::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000539 OS << '\n';
540}
541
Jim Laskey0d841e02005-10-27 19:47:21 +0000542//
Jim Laskey10b1dd92005-10-31 17:16:01 +0000543// EmitProcessorData - Generate data for processor itineraries.
Jim Laskey0d841e02005-10-27 19:47:21 +0000544//
Andrew Trick23482322011-04-01 02:22:47 +0000545void SubtargetEmitter::
546EmitProcessorData(raw_ostream &OS,
547 std::vector<Record*> &ItinClassList,
548 std::vector<std::vector<InstrItinerary> > &ProcList) {
Andrew Trickfc992992012-06-05 03:44:40 +0000549
Jim Laskey908ae272005-10-28 15:20:43 +0000550 // Get an iterator for processor itinerary stages
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000551 std::vector<std::vector<InstrItinerary> >::iterator
552 ProcListIter = ProcList.begin();
Andrew Trickda96cf22011-04-01 01:56:55 +0000553
Jim Laskey908ae272005-10-28 15:20:43 +0000554 // For each processor itinerary
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000555 std::vector<Record*> Itins =
556 Records.getAllDerivedDefinitions("ProcessorItineraries");
557 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
558 // Next record
559 Record *Itin = Itins[i];
560
Jim Laskey908ae272005-10-28 15:20:43 +0000561 // Get processor itinerary name
Bill Wendling4222d802007-05-04 20:38:40 +0000562 const std::string &Name = Itin->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000563
Jim Laskey908ae272005-10-28 15:20:43 +0000564 // Skip default
Andrew Trickfc992992012-06-05 03:44:40 +0000565 // Begin processor itinerary properties
566 OS << "\n";
567 OS << "static const llvm::InstrItineraryProps " << Name << "Props(\n";
568 EmitItineraryProp(OS, Itin, "IssueWidth", ',');
569 EmitItineraryProp(OS, Itin, "MinLatency", ',');
570 EmitItineraryProp(OS, Itin, "LoadLatency", ',');
571 EmitItineraryProp(OS, Itin, "HighLatency", ' ');
572 OS << ");\n";
573
Jim Laskey908ae272005-10-28 15:20:43 +0000574 // For each itinerary class
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000575 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
Andrew Trickd85934b2012-06-22 03:58:51 +0000576 if (!ItinList.empty()) {
577 assert(ItinList.size() == ItinClassList.size() && "bad itinerary");
Andrew Trickda96cf22011-04-01 01:56:55 +0000578
Andrew Trickd85934b2012-06-22 03:58:51 +0000579 // Begin processor itinerary table
580 OS << "\n";
581 OS << "static const llvm::InstrItinerary " << Name << "Entries"
582 << "[] = {\n";
583
584 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
585 InstrItinerary &Intinerary = ItinList[j];
586
587 // Emit in the form of
588 // { firstStage, lastStage, firstCycle, lastCycle } // index
589 if (Intinerary.FirstStage == 0) {
590 OS << " { 1, 0, 0, 0, 0 }";
591 } else {
592 OS << " { " <<
593 Intinerary.NumMicroOps << ", " <<
594 Intinerary.FirstStage << ", " <<
595 Intinerary.LastStage << ", " <<
596 Intinerary.FirstOperandCycle << ", " <<
597 Intinerary.LastOperandCycle << " }";
598 }
599 OS << ", // " << j << " " << ItinClassList[j]->getName() << "\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000600 }
Andrew Trickd85934b2012-06-22 03:58:51 +0000601 // End processor itinerary table
602 OS << " { 1, ~0U, ~0U, ~0U, ~0U } // end marker\n";
603 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000604 }
Andrew Trickfc992992012-06-05 03:44:40 +0000605 OS << '\n';
606 OS << "static const llvm::InstrItinerarySubtargetValue "
607 << Name << " = {\n";
608 OS << " &" << Name << "Props,\n";
Andrew Trickd85934b2012-06-22 03:58:51 +0000609 if (ItinList.empty())
610 OS << " 0\n";
611 else
612 OS << " " << Name << "Entries\n";
Andrew Trickfc992992012-06-05 03:44:40 +0000613 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000614 }
Jim Laskey10b1dd92005-10-31 17:16:01 +0000615}
616
617//
618// EmitProcessorLookup - generate cpu name to itinerary lookup table.
619//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000620void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +0000621 // Gather and sort processor information
622 std::vector<Record*> ProcessorList =
623 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000624 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +0000625
626 // Begin processor table
627 OS << "\n";
628 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000629 << "extern const llvm::SubtargetInfoKV "
Evan Cheng94214702011-07-01 20:45:01 +0000630 << Target << "ProcItinKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000631
Jim Laskey10b1dd92005-10-31 17:16:01 +0000632 // For each processor
633 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
634 // Next processor
635 Record *Processor = ProcessorList[i];
636
Bill Wendling4222d802007-05-04 20:38:40 +0000637 const std::string &Name = Processor->getValueAsString("Name");
638 const std::string &ProcItin =
639 Processor->getValueAsDef("ProcItin")->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000640
Jim Laskey10b1dd92005-10-31 17:16:01 +0000641 // Emit as { "cpu", procinit },
642 OS << " { "
643 << "\"" << Name << "\", "
644 << "(void *)&" << ProcItin;
Andrew Trickda96cf22011-04-01 01:56:55 +0000645
Jim Laskey10b1dd92005-10-31 17:16:01 +0000646 OS << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000647
Jim Laskey10b1dd92005-10-31 17:16:01 +0000648 // Depending on ''if more in the list'' emit comma
649 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000650
Jim Laskey10b1dd92005-10-31 17:16:01 +0000651 OS << "\n";
652 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000653
Jim Laskey10b1dd92005-10-31 17:16:01 +0000654 // End processor table
655 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000656}
657
658//
659// EmitData - Emits all stages and itineries, folding common patterns.
660//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000661void SubtargetEmitter::EmitData(raw_ostream &OS) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000662 std::map<std::string, unsigned> ItinClassesMap;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000663 // Gather and sort all itinerary classes
664 std::vector<Record*> ItinClassList =
665 Records.getAllDerivedDefinitions("InstrItinClass");
666 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
Andrew Trickda96cf22011-04-01 01:56:55 +0000667
Jim Laskey908ae272005-10-28 15:20:43 +0000668 // Enumerate all the itinerary classes
Evan Cheng5f54ce32010-09-09 18:18:55 +0000669 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap,
670 ItinClassList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000671 // Make sure the rest is worth the effort
Chris Lattner387e4bd2006-01-27 01:41:55 +0000672 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
Andrew Trickda96cf22011-04-01 01:56:55 +0000673
Jim Laskey6cee6302005-11-01 20:06:59 +0000674 if (HasItineraries) {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000675 std::vector<std::vector<InstrItinerary> > ProcList;
Jim Laskey6cee6302005-11-01 20:06:59 +0000676 // Emit the stage data
Evan Cheng5f54ce32010-09-09 18:18:55 +0000677 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap,
678 ItinClassList, ProcList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000679 // Emit the processor itinerary data
Andrew Trick23482322011-04-01 02:22:47 +0000680 EmitProcessorData(OS, ItinClassList, ProcList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000681 // Emit the processor lookup data
682 EmitProcessorLookup(OS);
683 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000684}
685
686//
Jim Laskey581a8f72005-10-26 17:30:34 +0000687// ParseFeaturesFunction - Produces a subtarget specific function for parsing
688// the subtarget features string.
689//
Evan Cheng94214702011-07-01 20:45:01 +0000690void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
691 unsigned NumFeatures,
692 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000693 std::vector<Record*> Features =
694 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +0000695 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +0000696
Andrew Trickda96cf22011-04-01 01:56:55 +0000697 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
698 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +0000699 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +0000700 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000701 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +0000702 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel3f696e52012-06-12 04:21:36 +0000703 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +0000704
705 if (Features.empty()) {
706 OS << "}\n";
707 return;
708 }
709
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000710 OS << " uint64_t Bits = ReInitMCSubtargetInfo(CPU, FS);\n";
Bill Wendling4222d802007-05-04 20:38:40 +0000711
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000712 for (unsigned i = 0; i < Features.size(); i++) {
713 // Next record
714 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +0000715 const std::string &Instance = R->getName();
716 const std::string &Value = R->getValueAsString("Value");
717 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +0000718
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000719 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000720 OS << " if ((Bits & " << Target << "::"
721 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000722 << Attribute << " = " << Value << ";\n";
723 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000724 OS << " if ((Bits & " << Target << "::"
725 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +0000726 << Attribute << " < " << Value << ") "
727 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +0000728 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000729
Evan Cheng276365d2011-06-30 01:53:36 +0000730 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +0000731}
732
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000733//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000734// SubtargetEmitter::run - Main subtarget enumeration emitter.
735//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000736void SubtargetEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +0000737 Target = CodeGenTarget(Records).getName();
Jim Laskey581a8f72005-10-26 17:30:34 +0000738
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000739 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000740
Evan Chengebdeeab2011-07-08 01:53:10 +0000741 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
742 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
743
744 OS << "namespace llvm {\n";
745 Enumeration(OS, "SubtargetFeature", true);
746 OS << "} // End llvm namespace \n";
747 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
748
Evan Cheng94214702011-07-01 20:45:01 +0000749 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
750 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000751
Evan Cheng94214702011-07-01 20:45:01 +0000752 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000753#if 0
754 OS << "namespace {\n";
755#endif
Evan Cheng94214702011-07-01 20:45:01 +0000756 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000757 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +0000758 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000759 OS << "\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000760 EmitData(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000761 OS << "\n";
762#if 0
763 OS << "}\n";
764#endif
Evan Cheng94214702011-07-01 20:45:01 +0000765
766 // MCInstrInfo initialization routine.
767 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +0000768 << "MCSubtargetInfo(MCSubtargetInfo *II, "
769 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
770 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +0000771 if (NumFeatures)
772 OS << Target << "FeatureKV, ";
773 else
774 OS << "0, ";
775 if (NumProcs)
776 OS << Target << "SubTypeKV, ";
777 else
778 OS << "0, ";
779 if (HasItineraries) {
780 OS << Target << "ProcItinKV, "
781 << Target << "Stages, "
782 << Target << "OperandCycles, "
783 << Target << "ForwardingPathes, ";
784 } else
785 OS << "0, 0, 0, 0, ";
786 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
787
788 OS << "} // End llvm namespace \n";
789
790 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
791
792 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
793 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
794
795 OS << "#include \"llvm/Support/Debug.h\"\n";
796 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
797 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
798
799 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
800
Evan Cheng5b1b44892011-07-01 21:01:15 +0000801 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +0000802 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
803 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
804
805 std::string ClassName = Target + "GenSubtargetInfo";
806 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000807 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +0000808 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000809 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
810 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000811 << "public:\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +0000812 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000813 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +0000814 << "};\n";
815 OS << "} // End llvm namespace \n";
816
817 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
818
819 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
820 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
821
822 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000823 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
824 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000825 if (HasItineraries) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000826 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcItinKV[];\n";
827 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
828 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
829 OS << "extern const unsigned " << Target << "ForwardingPathes[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000830 }
831
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000832 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
833 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +0000834 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +0000835 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +0000836 if (NumFeatures)
837 OS << Target << "FeatureKV, ";
838 else
839 OS << "0, ";
840 if (NumProcs)
841 OS << Target << "SubTypeKV, ";
842 else
843 OS << "0, ";
844 if (HasItineraries) {
845 OS << Target << "ProcItinKV, "
846 << Target << "Stages, "
847 << Target << "OperandCycles, "
848 << Target << "ForwardingPathes, ";
849 } else
850 OS << "0, 0, 0, 0, ";
851 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
852 OS << "} // End llvm namespace \n";
853
854 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000855}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000856
857namespace llvm {
858
859void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
860 SubtargetEmitter(RK).run(OS);
861}
862
863} // End llvm namespace