blob: 5911d9856f669c135fd9d595bfc45e1d3f757430 [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
14#include "SubtargetEmitter.h"
15#include "CodeGenTarget.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000016#include "llvm/TableGen/Record.h"
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000017#include "llvm/ADT/StringExtras.h"
18#include "llvm/Support/Debug.h"
Jeff Cohen9489c042005-10-28 01:43:09 +000019#include <algorithm>
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000020using namespace llvm;
21
Jim Laskey7dc02042005-10-22 07:59:56 +000022//
Jim Laskey581a8f72005-10-26 17:30:34 +000023// Enumeration - Emit the specified class as an enumeration.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000024//
Daniel Dunbar1a551802009-07-03 00:10:29 +000025void SubtargetEmitter::Enumeration(raw_ostream &OS,
Jim Laskey581a8f72005-10-26 17:30:34 +000026 const char *ClassName,
27 bool isBits) {
Jim Laskey908ae272005-10-28 15:20:43 +000028 // Get all records of class and sort
Jim Laskeyf7bcde02005-10-28 21:47:29 +000029 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
Duraid Madina42d24c72005-12-30 14:56:37 +000030 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000031
Evan Chengb6a63882011-04-15 19:35:46 +000032 unsigned N = DefList.size();
Evan Cheng94214702011-07-01 20:45:01 +000033 if (N == 0)
34 return;
Evan Chengb6a63882011-04-15 19:35:46 +000035 if (N > 64) {
36 errs() << "Too many (> 64) subtarget features!\n";
37 exit(1);
38 }
39
Evan Cheng94214702011-07-01 20:45:01 +000040 OS << "namespace " << Target << " {\n";
41
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000042 // For bit flag enumerations with more than 32 items, emit constants.
43 // Emit an enum for everything else.
44 if (isBits && N > 32) {
45 // For each record
46 for (unsigned i = 0; i < N; i++) {
47 // Next record
48 Record *Def = DefList[i];
Evan Cheng94214702011-07-01 20:45:01 +000049
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000050 // Get and emit name and expression (1 << i)
51 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
52 }
53 } else {
54 // Open enumeration
55 OS << "enum {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +000056
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000057 // For each record
58 for (unsigned i = 0; i < N;) {
59 // Next record
60 Record *Def = DefList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +000061
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000062 // Get and emit name
63 OS << " " << Def->getName();
Jim Laskey908ae272005-10-28 15:20:43 +000064
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000065 // If bit flags then emit expression (1 << i)
66 if (isBits) OS << " = " << " 1ULL << " << i;
Andrew Trickda96cf22011-04-01 01:56:55 +000067
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +000068 // Depending on 'if more in the list' emit comma
69 if (++i < N) OS << ",";
70
71 OS << "\n";
72 }
73
74 // Close enumeration
75 OS << "};\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000076 }
Andrew Trickda96cf22011-04-01 01:56:55 +000077
Evan Cheng94214702011-07-01 20:45:01 +000078 OS << "}\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000079}
80
81//
Bill Wendling4222d802007-05-04 20:38:40 +000082// FeatureKeyValues - Emit data of all the subtarget features. Used by the
83// command line.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000084//
Evan Cheng94214702011-07-01 20:45:01 +000085unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +000086 // Gather and sort all the features
Jim Laskeyf7bcde02005-10-28 21:47:29 +000087 std::vector<Record*> FeatureList =
88 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng94214702011-07-01 20:45:01 +000089
90 if (FeatureList.empty())
91 return 0;
92
Jim Grosbach7c9a7722008-09-11 17:05:32 +000093 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +000094
Jim Laskey908ae272005-10-28 15:20:43 +000095 // Begin feature table
Jim Laskey581a8f72005-10-26 17:30:34 +000096 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +000097 << "extern const llvm::SubtargetFeatureKV " << Target
98 << "FeatureKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +000099
Jim Laskey908ae272005-10-28 15:20:43 +0000100 // For each feature
Evan Cheng94214702011-07-01 20:45:01 +0000101 unsigned NumFeatures = 0;
Jim Laskeydbe40062006-12-12 20:55:58 +0000102 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000103 // Next feature
104 Record *Feature = FeatureList[i];
105
Bill Wendling4222d802007-05-04 20:38:40 +0000106 const std::string &Name = Feature->getName();
107 const std::string &CommandLineName = Feature->getValueAsString("Name");
108 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickda96cf22011-04-01 01:56:55 +0000109
Jim Laskeydbe40062006-12-12 20:55:58 +0000110 if (CommandLineName.empty()) continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000111
Jim Grosbachda4231f2009-03-26 16:17:51 +0000112 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000113 OS << " { "
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000114 << "\"" << CommandLineName << "\", "
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000115 << "\"" << Desc << "\", "
Evan Cheng94214702011-07-01 20:45:01 +0000116 << Target << "::" << Name << ", ";
Bill Wendling4222d802007-05-04 20:38:40 +0000117
Andrew Trickda96cf22011-04-01 01:56:55 +0000118 const std::vector<Record*> &ImpliesList =
Bill Wendling4222d802007-05-04 20:38:40 +0000119 Feature->getValueAsListOfDefs("Implies");
Andrew Trickda96cf22011-04-01 01:56:55 +0000120
Bill Wendling4222d802007-05-04 20:38:40 +0000121 if (ImpliesList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000122 OS << "0ULL";
Bill Wendling4222d802007-05-04 20:38:40 +0000123 } else {
124 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000125 OS << Target << "::" << ImpliesList[j]->getName();
Bill Wendling4222d802007-05-04 20:38:40 +0000126 if (++j < M) OS << " | ";
127 }
128 }
129
130 OS << " }";
Evan Cheng94214702011-07-01 20:45:01 +0000131 ++NumFeatures;
Andrew Trickda96cf22011-04-01 01:56:55 +0000132
Jim Laskey10b1dd92005-10-31 17:16:01 +0000133 // Depending on 'if more in the list' emit comma
Jim Laskeydbe40062006-12-12 20:55:58 +0000134 if ((i + 1) < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000135
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000136 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000137 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000138
Jim Laskey908ae272005-10-28 15:20:43 +0000139 // End feature table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000140 OS << "};\n";
141
Evan Cheng94214702011-07-01 20:45:01 +0000142 return NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000143}
144
145//
146// CPUKeyValues - Emit data of all the subtarget processors. Used by command
147// line.
148//
Evan Cheng94214702011-07-01 20:45:01 +0000149unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000150 // Gather and sort processor information
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000151 std::vector<Record*> ProcessorList =
152 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000153 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000154
Jim Laskey908ae272005-10-28 15:20:43 +0000155 // Begin processor table
Jim Laskey581a8f72005-10-26 17:30:34 +0000156 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000157 << "extern const llvm::SubtargetFeatureKV " << Target
158 << "SubTypeKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000159
Jim Laskey908ae272005-10-28 15:20:43 +0000160 // For each processor
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000161 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
162 // Next processor
163 Record *Processor = ProcessorList[i];
164
Bill Wendling4222d802007-05-04 20:38:40 +0000165 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickda96cf22011-04-01 01:56:55 +0000166 const std::vector<Record*> &FeatureList =
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000167 Processor->getValueAsListOfDefs("Features");
Andrew Trickda96cf22011-04-01 01:56:55 +0000168
Jim Laskey908ae272005-10-28 15:20:43 +0000169 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000170 OS << " { "
171 << "\"" << Name << "\", "
172 << "\"Select the " << Name << " processor\", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000173
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000174 if (FeatureList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000175 OS << "0ULL";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000176 } else {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000177 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000178 OS << Target << "::" << FeatureList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000179 if (++j < M) OS << " | ";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000180 }
181 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000182
Bill Wendling4222d802007-05-04 20:38:40 +0000183 // The "0" is for the "implies" section of this data structure.
Evan Chengb6a63882011-04-15 19:35:46 +0000184 OS << ", 0ULL }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000185
Jim Laskey10b1dd92005-10-31 17:16:01 +0000186 // Depending on 'if more in the list' emit comma
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000187 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000188
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000189 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000190 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000191
Jim Laskey908ae272005-10-28 15:20:43 +0000192 // End processor table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000193 OS << "};\n";
194
Evan Cheng94214702011-07-01 20:45:01 +0000195 return ProcessorList.size();
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000196}
Jim Laskey7dc02042005-10-22 07:59:56 +0000197
Jim Laskey581a8f72005-10-26 17:30:34 +0000198//
Jim Laskey0d841e02005-10-27 19:47:21 +0000199// CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
Jim Laskey908ae272005-10-28 15:20:43 +0000200// Returns itinerary class count.
Jim Laskey0d841e02005-10-27 19:47:21 +0000201//
Evan Cheng5f54ce32010-09-09 18:18:55 +0000202unsigned SubtargetEmitter::
203CollectAllItinClasses(raw_ostream &OS,
204 std::map<std::string, unsigned> &ItinClassesMap,
205 std::vector<Record*> &ItinClassList) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000206 // For each itinerary class
207 unsigned N = ItinClassList.size();
208 for (unsigned i = 0; i < N; i++) {
209 // Next itinerary class
Bill Wendling4222d802007-05-04 20:38:40 +0000210 const Record *ItinClass = ItinClassList[i];
Jim Laskey908ae272005-10-28 15:20:43 +0000211 // Get name of itinerary class
Jim Laskey908ae272005-10-28 15:20:43 +0000212 // Assign itinerary class a unique number
Bill Wendling4222d802007-05-04 20:38:40 +0000213 ItinClassesMap[ItinClass->getName()] = i;
Jim Laskey0d841e02005-10-27 19:47:21 +0000214 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000215
Jim Laskey908ae272005-10-28 15:20:43 +0000216 // Return itinerary class count
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000217 return N;
Jim Laskey0d841e02005-10-27 19:47:21 +0000218}
219
220//
David Goodwinfac85412009-08-17 16:02:57 +0000221// FormItineraryStageString - Compose a string containing the stage
222// data initialization for the specified itinerary. N is the number
223// of stages.
Jim Laskey0d841e02005-10-27 19:47:21 +0000224//
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000225void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
226 Record *ItinData,
David Goodwinfac85412009-08-17 16:02:57 +0000227 std::string &ItinString,
228 unsigned &NStages) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000229 // Get states list
Bill Wendling4222d802007-05-04 20:38:40 +0000230 const std::vector<Record*> &StageList =
231 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey908ae272005-10-28 15:20:43 +0000232
233 // For each stage
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000234 unsigned N = NStages = StageList.size();
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000235 for (unsigned i = 0; i < N;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000236 // Next stage
Bill Wendling4222d802007-05-04 20:38:40 +0000237 const Record *Stage = StageList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000238
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000239 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey0d841e02005-10-27 19:47:21 +0000240 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskey7f39c142005-11-03 22:47:41 +0000241 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000242
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000243 // Get unit list
Bill Wendling4222d802007-05-04 20:38:40 +0000244 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickda96cf22011-04-01 01:56:55 +0000245
Jim Laskey908ae272005-10-28 15:20:43 +0000246 // For each unit
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000247 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000248 // Add name and bitwise or
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000249 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000250 if (++j < M) ItinString += " | ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000251 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000252
David Goodwin1a8f36e2009-08-12 18:31:53 +0000253 int TimeInc = Stage->getValueAsInt("TimeInc");
254 ItinString += ", " + itostr(TimeInc);
255
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000256 int Kind = Stage->getValueAsInt("Kind");
257 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
258
Jim Laskey908ae272005-10-28 15:20:43 +0000259 // Close off stage
260 ItinString += " }";
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000261 if (++i < N) ItinString += ", ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000262 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000263}
264
265//
David Goodwinfac85412009-08-17 16:02:57 +0000266// FormItineraryOperandCycleString - Compose a string containing the
267// operand cycle initialization for the specified itinerary. N is the
268// number of operands that has cycles specified.
Jim Laskey0d841e02005-10-27 19:47:21 +0000269//
David Goodwinfac85412009-08-17 16:02:57 +0000270void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
271 std::string &ItinString, unsigned &NOperandCycles) {
272 // Get operand cycle list
273 const std::vector<int64_t> &OperandCycleList =
274 ItinData->getValueAsListOfInts("OperandCycles");
275
276 // For each operand cycle
277 unsigned N = NOperandCycles = OperandCycleList.size();
278 for (unsigned i = 0; i < N;) {
279 // Next operand cycle
280 const int OCycle = OperandCycleList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000281
David Goodwinfac85412009-08-17 16:02:57 +0000282 ItinString += " " + itostr(OCycle);
283 if (++i < N) ItinString += ", ";
284 }
285}
286
Evan Cheng63d66ee2010-09-28 23:50:49 +0000287void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
288 Record *ItinData,
289 std::string &ItinString,
290 unsigned NOperandCycles) {
291 const std::vector<Record*> &BypassList =
292 ItinData->getValueAsListOfDefs("Bypasses");
293 unsigned N = BypassList.size();
Evan Cheng3881cb72010-09-29 22:42:35 +0000294 unsigned i = 0;
295 for (; i < N;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000296 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng3881cb72010-09-29 22:42:35 +0000297 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000298 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000299 for (; i < NOperandCycles;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000300 ItinString += " 0";
Evan Cheng3881cb72010-09-29 22:42:35 +0000301 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000302 }
303}
304
David Goodwinfac85412009-08-17 16:02:57 +0000305//
306// EmitStageAndOperandCycleData - Generate unique itinerary stages and
307// operand cycle tables. Record itineraries for processors.
308//
309void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS,
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000310 unsigned NItinClasses,
Evan Cheng5f54ce32010-09-09 18:18:55 +0000311 std::map<std::string, unsigned> &ItinClassesMap,
312 std::vector<Record*> &ItinClassList,
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000313 std::vector<std::vector<InstrItinerary> > &ProcList) {
Jim Laskey908ae272005-10-28 15:20:43 +0000314 // Gather processor iteraries
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000315 std::vector<Record*> ProcItinList =
316 Records.getAllDerivedDefinitions("ProcessorItineraries");
Andrew Trickda96cf22011-04-01 01:56:55 +0000317
Jim Laskey908ae272005-10-28 15:20:43 +0000318 // If just no itinerary then don't bother
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000319 if (ProcItinList.size() < 2) return;
Jim Laskey908ae272005-10-28 15:20:43 +0000320
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000321 // Emit functional units for all the itineraries.
322 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
323 // Next record
324 Record *Proc = ProcItinList[i];
325
326 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
327 if (FUs.empty())
328 continue;
329
330 const std::string &Name = Proc->getName();
331 OS << "\n// Functional units for itineraries \"" << Name << "\"\n"
332 << "namespace " << Name << "FU {\n";
333
334 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
335 OS << " const unsigned " << FUs[j]->getName()
336 << " = 1 << " << j << ";\n";
337
338 OS << "}\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000339
340 std::vector<Record*> BPs = Proc->getValueAsListOfDefs("BP");
Evan Cheng3881cb72010-09-29 22:42:35 +0000341 if (BPs.size()) {
342 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
343 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000344
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000345 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000346 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000347 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng3881cb72010-09-29 22:42:35 +0000348 << " = 1 << " << j << ";\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000349
Evan Cheng3881cb72010-09-29 22:42:35 +0000350 OS << "}\n";
351 }
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000352 }
353
Jim Laskey908ae272005-10-28 15:20:43 +0000354 // Begin stages table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000355 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
356 "Stages[] = {\n";
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000357 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000358
David Goodwinfac85412009-08-17 16:02:57 +0000359 // Begin operand cycle table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000360 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000361 "OperandCycles[] = {\n";
David Goodwinfac85412009-08-17 16:02:57 +0000362 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000363
364 // Begin pipeline bypass table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000365 std::string BypassTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000366 "ForwardingPathes[] = {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000367 BypassTable += " 0, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000368
David Goodwinfac85412009-08-17 16:02:57 +0000369 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng3881cb72010-09-29 22:42:35 +0000370 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000371 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
372 // Next record
373 Record *Proc = ProcItinList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000374
Jim Laskey908ae272005-10-28 15:20:43 +0000375 // Get processor itinerary name
Bill Wendling4222d802007-05-04 20:38:40 +0000376 const std::string &Name = Proc->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000377
Jim Laskey908ae272005-10-28 15:20:43 +0000378 // Skip default
Jim Laskey0d841e02005-10-27 19:47:21 +0000379 if (Name == "NoItineraries") continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000380
Jim Laskey908ae272005-10-28 15:20:43 +0000381 // Create and expand processor itinerary to cover all itinerary classes
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000382 std::vector<InstrItinerary> ItinList;
383 ItinList.resize(NItinClasses);
Andrew Trickda96cf22011-04-01 01:56:55 +0000384
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000385 // Get itinerary data list
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000386 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
Andrew Trickda96cf22011-04-01 01:56:55 +0000387
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000388 // For each itinerary data
389 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
390 // Next itinerary data
391 Record *ItinData = ItinDataList[j];
Andrew Trickda96cf22011-04-01 01:56:55 +0000392
Jim Laskey908ae272005-10-28 15:20:43 +0000393 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000394 std::string ItinStageString;
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000395 unsigned NStages;
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000396 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000397
David Goodwinfac85412009-08-17 16:02:57 +0000398 // Get string and operand cycle count
399 std::string ItinOperandCycleString;
400 unsigned NOperandCycles;
401 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
402 NOperandCycles);
403
Evan Cheng63d66ee2010-09-28 23:50:49 +0000404 std::string ItinBypassString;
405 FormItineraryBypassString(Name, ItinData, ItinBypassString,
406 NOperandCycles);
407
David Goodwinfac85412009-08-17 16:02:57 +0000408 // Check to see if stage already exists and create if it doesn't
409 unsigned FindStage = 0;
410 if (NStages > 0) {
411 FindStage = ItinStageMap[ItinStageString];
412 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000413 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
414 StageTable += ItinStageString + ", // " + itostr(StageCount);
415 if (NStages > 1)
416 StageTable += "-" + itostr(StageCount + NStages - 1);
417 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000418 // Record Itin class number.
419 ItinStageMap[ItinStageString] = FindStage = StageCount;
420 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000421 }
422 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000423
David Goodwinfac85412009-08-17 16:02:57 +0000424 // Check to see if operand cycle already exists and create if it doesn't
425 unsigned FindOperandCycle = 0;
426 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000427 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
428 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000429 if (FindOperandCycle == 0) {
430 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000431 OperandCycleTable += ItinOperandCycleString + ", // ";
432 std::string OperandIdxComment = itostr(OperandCycleCount);
433 if (NOperandCycles > 1)
434 OperandIdxComment += "-"
435 + itostr(OperandCycleCount + NOperandCycles - 1);
436 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000437 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000438 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000439 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000440 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000441 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000442 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000443 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000444 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000445
Jim Laskey908ae272005-10-28 15:20:43 +0000446 // Locate where to inject into processor itinerary table
Bill Wendling4222d802007-05-04 20:38:40 +0000447 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
David Goodwinfac85412009-08-17 16:02:57 +0000448 unsigned Find = ItinClassesMap[Name];
Andrew Trickda96cf22011-04-01 01:56:55 +0000449
Evan Cheng5f54ce32010-09-09 18:18:55 +0000450 // Set up itinerary as location and location + stage count
451 unsigned NumUOps = ItinClassList[Find]->getValueAsInt("NumMicroOps");
452 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
453 FindOperandCycle,
454 FindOperandCycle + NOperandCycles};
455
Jim Laskey908ae272005-10-28 15:20:43 +0000456 // Inject - empty slots will be 0, 0
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000457 ItinList[Find] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000458 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000459
Jim Laskey908ae272005-10-28 15:20:43 +0000460 // Add process itinerary to list
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000461 ProcList.push_back(ItinList);
Jim Laskey0d841e02005-10-27 19:47:21 +0000462 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000463
Jim Laskey7f39c142005-11-03 22:47:41 +0000464 // Closing stage
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000465 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
David Goodwinfac85412009-08-17 16:02:57 +0000466 StageTable += "};\n";
467
468 // Closing operand cycles
469 OperandCycleTable += " 0 // End itinerary\n";
470 OperandCycleTable += "};\n";
471
Evan Cheng63d66ee2010-09-28 23:50:49 +0000472 BypassTable += " 0 // End itinerary\n";
473 BypassTable += "};\n";
474
David Goodwinfac85412009-08-17 16:02:57 +0000475 // Emit tables.
476 OS << StageTable;
477 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000478 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000479}
480
Andrew Trickfc992992012-06-05 03:44:40 +0000481void SubtargetEmitter::EmitItineraryProp(raw_ostream &OS, const Record *R,
482 const char *Name, char Separator) {
483 OS << " ";
484 int V = R->getValueAsInt(Name);
485 if (V >= 0)
486 OS << V << Separator << " // " << Name;
487 else
Andrew Trick0076ad72012-06-08 18:25:47 +0000488 OS << "InstrItineraryProps::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000489 OS << '\n';
490}
491
Jim Laskey0d841e02005-10-27 19:47:21 +0000492//
Jim Laskey10b1dd92005-10-31 17:16:01 +0000493// EmitProcessorData - Generate data for processor itineraries.
Jim Laskey0d841e02005-10-27 19:47:21 +0000494//
Andrew Trick23482322011-04-01 02:22:47 +0000495void SubtargetEmitter::
496EmitProcessorData(raw_ostream &OS,
497 std::vector<Record*> &ItinClassList,
498 std::vector<std::vector<InstrItinerary> > &ProcList) {
Andrew Trickfc992992012-06-05 03:44:40 +0000499
Jim Laskey908ae272005-10-28 15:20:43 +0000500 // Get an iterator for processor itinerary stages
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000501 std::vector<std::vector<InstrItinerary> >::iterator
502 ProcListIter = ProcList.begin();
Andrew Trickda96cf22011-04-01 01:56:55 +0000503
Jim Laskey908ae272005-10-28 15:20:43 +0000504 // For each processor itinerary
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000505 std::vector<Record*> Itins =
506 Records.getAllDerivedDefinitions("ProcessorItineraries");
507 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
508 // Next record
509 Record *Itin = Itins[i];
510
Jim Laskey908ae272005-10-28 15:20:43 +0000511 // Get processor itinerary name
Bill Wendling4222d802007-05-04 20:38:40 +0000512 const std::string &Name = Itin->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000513
Jim Laskey908ae272005-10-28 15:20:43 +0000514 // Skip default
Jim Laskey0d841e02005-10-27 19:47:21 +0000515 if (Name == "NoItineraries") continue;
516
Andrew Trickfc992992012-06-05 03:44:40 +0000517 // Begin processor itinerary properties
518 OS << "\n";
519 OS << "static const llvm::InstrItineraryProps " << Name << "Props(\n";
520 EmitItineraryProp(OS, Itin, "IssueWidth", ',');
521 EmitItineraryProp(OS, Itin, "MinLatency", ',');
522 EmitItineraryProp(OS, Itin, "LoadLatency", ',');
523 EmitItineraryProp(OS, Itin, "HighLatency", ' ');
524 OS << ");\n";
525
Jim Laskey908ae272005-10-28 15:20:43 +0000526 // Begin processor itinerary table
Jim Laskey0d841e02005-10-27 19:47:21 +0000527 OS << "\n";
Andrew Trickfc992992012-06-05 03:44:40 +0000528 OS << "static const llvm::InstrItinerary " << Name << "Entries"
529 << "[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000530
Jim Laskey908ae272005-10-28 15:20:43 +0000531 // For each itinerary class
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000532 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
Andrew Trick23482322011-04-01 02:22:47 +0000533 assert(ItinList.size() == ItinClassList.size() && "bad itinerary");
David Goodwin1f528952009-09-24 20:22:50 +0000534 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000535 InstrItinerary &Intinerary = ItinList[j];
Andrew Trickda96cf22011-04-01 01:56:55 +0000536
537 // Emit in the form of
David Goodwinfac85412009-08-17 16:02:57 +0000538 // { firstStage, lastStage, firstCycle, lastCycle } // index
539 if (Intinerary.FirstStage == 0) {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000540 OS << " { 1, 0, 0, 0, 0 }";
Jim Laskey0d841e02005-10-27 19:47:21 +0000541 } else {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000542 OS << " { " <<
543 Intinerary.NumMicroOps << ", " <<
Andrew Trickda96cf22011-04-01 01:56:55 +0000544 Intinerary.FirstStage << ", " <<
545 Intinerary.LastStage << ", " <<
546 Intinerary.FirstOperandCycle << ", " <<
David Goodwinfac85412009-08-17 16:02:57 +0000547 Intinerary.LastOperandCycle << " }";
Jim Laskey0d841e02005-10-27 19:47:21 +0000548 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000549
Andrew Trick23482322011-04-01 02:22:47 +0000550 OS << ", // " << j << " " << ItinClassList[j]->getName() << "\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000551 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000552
Jim Laskey908ae272005-10-28 15:20:43 +0000553 // End processor itinerary table
Evan Cheng5f54ce32010-09-09 18:18:55 +0000554 OS << " { 1, ~0U, ~0U, ~0U, ~0U } // end marker\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000555 OS << "};\n";
Andrew Trickfc992992012-06-05 03:44:40 +0000556
557 OS << '\n';
558 OS << "static const llvm::InstrItinerarySubtargetValue "
559 << Name << " = {\n";
560 OS << " &" << Name << "Props,\n";
561 OS << " " << Name << "Entries\n";
562 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000563 }
Jim Laskey10b1dd92005-10-31 17:16:01 +0000564}
565
566//
567// EmitProcessorLookup - generate cpu name to itinerary lookup table.
568//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000569void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +0000570 // Gather and sort processor information
571 std::vector<Record*> ProcessorList =
572 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000573 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +0000574
575 // Begin processor table
576 OS << "\n";
577 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000578 << "extern const llvm::SubtargetInfoKV "
Evan Cheng94214702011-07-01 20:45:01 +0000579 << Target << "ProcItinKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000580
Jim Laskey10b1dd92005-10-31 17:16:01 +0000581 // For each processor
582 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
583 // Next processor
584 Record *Processor = ProcessorList[i];
585
Bill Wendling4222d802007-05-04 20:38:40 +0000586 const std::string &Name = Processor->getValueAsString("Name");
587 const std::string &ProcItin =
588 Processor->getValueAsDef("ProcItin")->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000589
Jim Laskey10b1dd92005-10-31 17:16:01 +0000590 // Emit as { "cpu", procinit },
591 OS << " { "
592 << "\"" << Name << "\", "
593 << "(void *)&" << ProcItin;
Andrew Trickda96cf22011-04-01 01:56:55 +0000594
Jim Laskey10b1dd92005-10-31 17:16:01 +0000595 OS << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000596
Jim Laskey10b1dd92005-10-31 17:16:01 +0000597 // Depending on ''if more in the list'' emit comma
598 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000599
Jim Laskey10b1dd92005-10-31 17:16:01 +0000600 OS << "\n";
601 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000602
Jim Laskey10b1dd92005-10-31 17:16:01 +0000603 // End processor table
604 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000605}
606
607//
608// EmitData - Emits all stages and itineries, folding common patterns.
609//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000610void SubtargetEmitter::EmitData(raw_ostream &OS) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000611 std::map<std::string, unsigned> ItinClassesMap;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000612 // Gather and sort all itinerary classes
613 std::vector<Record*> ItinClassList =
614 Records.getAllDerivedDefinitions("InstrItinClass");
615 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
Andrew Trickda96cf22011-04-01 01:56:55 +0000616
Jim Laskey908ae272005-10-28 15:20:43 +0000617 // Enumerate all the itinerary classes
Evan Cheng5f54ce32010-09-09 18:18:55 +0000618 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap,
619 ItinClassList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000620 // Make sure the rest is worth the effort
Chris Lattner387e4bd2006-01-27 01:41:55 +0000621 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
Andrew Trickda96cf22011-04-01 01:56:55 +0000622
Jim Laskey6cee6302005-11-01 20:06:59 +0000623 if (HasItineraries) {
Evan Cheng5f54ce32010-09-09 18:18:55 +0000624 std::vector<std::vector<InstrItinerary> > ProcList;
Jim Laskey6cee6302005-11-01 20:06:59 +0000625 // Emit the stage data
Evan Cheng5f54ce32010-09-09 18:18:55 +0000626 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap,
627 ItinClassList, ProcList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000628 // Emit the processor itinerary data
Andrew Trick23482322011-04-01 02:22:47 +0000629 EmitProcessorData(OS, ItinClassList, ProcList);
Jim Laskey6cee6302005-11-01 20:06:59 +0000630 // Emit the processor lookup data
631 EmitProcessorLookup(OS);
632 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000633}
634
635//
Jim Laskey581a8f72005-10-26 17:30:34 +0000636// ParseFeaturesFunction - Produces a subtarget specific function for parsing
637// the subtarget features string.
638//
Evan Cheng94214702011-07-01 20:45:01 +0000639void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
640 unsigned NumFeatures,
641 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000642 std::vector<Record*> Features =
643 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +0000644 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +0000645
Andrew Trickda96cf22011-04-01 01:56:55 +0000646 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
647 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +0000648 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +0000649 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000650 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +0000651 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Evan Cheng94214702011-07-01 20:45:01 +0000652 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n";
653
654 if (Features.empty()) {
655 OS << "}\n";
656 return;
657 }
658
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000659 OS << " uint64_t Bits = ReInitMCSubtargetInfo(CPU, FS);\n";
Bill Wendling4222d802007-05-04 20:38:40 +0000660
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000661 for (unsigned i = 0; i < Features.size(); i++) {
662 // Next record
663 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +0000664 const std::string &Instance = R->getName();
665 const std::string &Value = R->getValueAsString("Value");
666 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +0000667
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000668 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000669 OS << " if ((Bits & " << Target << "::"
670 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000671 << Attribute << " = " << Value << ";\n";
672 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000673 OS << " if ((Bits & " << Target << "::"
674 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +0000675 << Attribute << " < " << Value << ") "
676 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +0000677 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000678
Evan Cheng276365d2011-06-30 01:53:36 +0000679 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +0000680}
681
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000682//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000683// SubtargetEmitter::run - Main subtarget enumeration emitter.
684//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000685void SubtargetEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +0000686 Target = CodeGenTarget(Records).getName();
Jim Laskey581a8f72005-10-26 17:30:34 +0000687
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000688 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000689
Evan Chengebdeeab2011-07-08 01:53:10 +0000690 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
691 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
692
693 OS << "namespace llvm {\n";
694 Enumeration(OS, "SubtargetFeature", true);
695 OS << "} // End llvm namespace \n";
696 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
697
Evan Cheng94214702011-07-01 20:45:01 +0000698 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
699 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000700
Evan Cheng94214702011-07-01 20:45:01 +0000701 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000702#if 0
703 OS << "namespace {\n";
704#endif
Evan Cheng94214702011-07-01 20:45:01 +0000705 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000706 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +0000707 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000708 OS << "\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000709 EmitData(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +0000710 OS << "\n";
711#if 0
712 OS << "}\n";
713#endif
Evan Cheng94214702011-07-01 20:45:01 +0000714
715 // MCInstrInfo initialization routine.
716 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +0000717 << "MCSubtargetInfo(MCSubtargetInfo *II, "
718 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
719 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +0000720 if (NumFeatures)
721 OS << Target << "FeatureKV, ";
722 else
723 OS << "0, ";
724 if (NumProcs)
725 OS << Target << "SubTypeKV, ";
726 else
727 OS << "0, ";
728 if (HasItineraries) {
729 OS << Target << "ProcItinKV, "
730 << Target << "Stages, "
731 << Target << "OperandCycles, "
732 << Target << "ForwardingPathes, ";
733 } else
734 OS << "0, 0, 0, 0, ";
735 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
736
737 OS << "} // End llvm namespace \n";
738
739 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
740
741 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
742 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
743
744 OS << "#include \"llvm/Support/Debug.h\"\n";
745 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
746 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
747
748 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
749
Evan Cheng5b1b44892011-07-01 21:01:15 +0000750 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +0000751 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
752 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
753
754 std::string ClassName = Target + "GenSubtargetInfo";
755 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000756 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +0000757 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000758 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
759 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000760 << "public:\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +0000761 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000762 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +0000763 << "};\n";
764 OS << "} // End llvm namespace \n";
765
766 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
767
768 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
769 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
770
771 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000772 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
773 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000774 if (HasItineraries) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000775 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcItinKV[];\n";
776 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
777 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
778 OS << "extern const unsigned " << Target << "ForwardingPathes[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +0000779 }
780
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000781 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
782 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +0000783 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +0000784 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +0000785 if (NumFeatures)
786 OS << Target << "FeatureKV, ";
787 else
788 OS << "0, ";
789 if (NumProcs)
790 OS << Target << "SubTypeKV, ";
791 else
792 OS << "0, ";
793 if (HasItineraries) {
794 OS << Target << "ProcItinKV, "
795 << Target << "Stages, "
796 << Target << "OperandCycles, "
797 << Target << "ForwardingPathes, ";
798 } else
799 OS << "0, 0, 0, 0, ";
800 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
801 OS << "} // End llvm namespace \n";
802
803 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +0000804}