blob: ad0d1f8d6666610befdf0287ead60e2692ac00e8 [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"
Andrew Trick2661b412012-07-07 04:00:00 +000015#include "CodeGenSchedule.h"
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000016#include "llvm/ADT/StringExtras.h"
Andrew Trick40096d22012-09-17 22:18:45 +000017#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000018#include "llvm/MC/MCInstrItineraries.h"
Andrew Trick40096d22012-09-17 22:18:45 +000019#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000020#include "llvm/TableGen/Record.h"
21#include "llvm/TableGen/TableGenBackend.h"
Andrew Trick40096d22012-09-17 22:18:45 +000022#include "llvm/Support/Debug.h"
Jeff Cohen9489c042005-10-28 01:43:09 +000023#include <algorithm>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include <map>
25#include <string>
26#include <vector>
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000027using namespace llvm;
28
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000029namespace {
30class SubtargetEmitter {
Andrew Trick52c3a1d2012-09-17 22:18:48 +000031 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
32 // The SchedClassDesc table indexes into a global write resource table, write
33 // latency table, and read advance table.
34 struct SchedClassTables {
35 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
36 std::vector<MCWriteProcResEntry> WriteProcResources;
37 std::vector<MCWriteLatencyEntry> WriteLatencies;
38 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
39
40 // Reserve an invalid entry at index 0
41 SchedClassTables() {
42 ProcSchedClasses.resize(1);
43 WriteProcResources.resize(1);
44 WriteLatencies.resize(1);
45 ReadAdvanceEntries.resize(1);
46 }
47 };
48
49 struct LessWriteProcResources {
50 bool operator()(const MCWriteProcResEntry &LHS,
51 const MCWriteProcResEntry &RHS) {
52 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
53 }
54 };
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000055
56 RecordKeeper &Records;
Andrew Trick2661b412012-07-07 04:00:00 +000057 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000058 std::string Target;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000059
60 void Enumeration(raw_ostream &OS, const char *ClassName, bool isBits);
61 unsigned FeatureKeyValues(raw_ostream &OS);
62 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000063 void FormItineraryStageString(const std::string &Names,
64 Record *ItinData, std::string &ItinString,
65 unsigned &NStages);
66 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
67 unsigned &NOperandCycles);
68 void FormItineraryBypassString(const std::string &Names,
69 Record *ItinData,
70 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick2661b412012-07-07 04:00:00 +000071 void EmitStageAndOperandCycleData(raw_ostream &OS,
72 std::vector<std::vector<InstrItinerary> >
73 &ProcItinLists);
74 void EmitItineraries(raw_ostream &OS,
75 std::vector<std::vector<InstrItinerary> >
76 &ProcItinLists);
77 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000078 char Separator);
Andrew Trick40096d22012-09-17 22:18:45 +000079 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
80 raw_ostream &OS);
Andrew Trick52c3a1d2012-09-17 22:18:48 +000081 Record *FindWriteResources(Record *WriteDef,
82 const CodeGenProcModel &ProcModel);
83 Record *FindReadAdvance(Record *ReadDef, const CodeGenProcModel &ProcModel);
84 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
85 SchedClassTables &SchedTables);
Andrew Trick2661b412012-07-07 04:00:00 +000086 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000087 void EmitProcessorLookup(raw_ostream &OS);
Andrew Trick2661b412012-07-07 04:00:00 +000088 void EmitSchedModel(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000089 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
90 unsigned NumProcs);
91
92public:
Andrew Trick2661b412012-07-07 04:00:00 +000093 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
94 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000095
96 void run(raw_ostream &o);
97
98};
99} // End anonymous namespace
100
Jim Laskey7dc02042005-10-22 07:59:56 +0000101//
Jim Laskey581a8f72005-10-26 17:30:34 +0000102// Enumeration - Emit the specified class as an enumeration.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000103//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000104void SubtargetEmitter::Enumeration(raw_ostream &OS,
Jim Laskey581a8f72005-10-26 17:30:34 +0000105 const char *ClassName,
106 bool isBits) {
Jim Laskey908ae272005-10-28 15:20:43 +0000107 // Get all records of class and sort
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000108 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
Duraid Madina42d24c72005-12-30 14:56:37 +0000109 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000110
Evan Chengb6a63882011-04-15 19:35:46 +0000111 unsigned N = DefList.size();
Evan Cheng94214702011-07-01 20:45:01 +0000112 if (N == 0)
113 return;
Evan Chengb6a63882011-04-15 19:35:46 +0000114 if (N > 64) {
115 errs() << "Too many (> 64) subtarget features!\n";
116 exit(1);
117 }
118
Evan Cheng94214702011-07-01 20:45:01 +0000119 OS << "namespace " << Target << " {\n";
120
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000121 // For bit flag enumerations with more than 32 items, emit constants.
122 // Emit an enum for everything else.
123 if (isBits && N > 32) {
124 // For each record
125 for (unsigned i = 0; i < N; i++) {
126 // Next record
127 Record *Def = DefList[i];
Evan Cheng94214702011-07-01 20:45:01 +0000128
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000129 // Get and emit name and expression (1 << i)
130 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
131 }
132 } else {
133 // Open enumeration
134 OS << "enum {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000135
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000136 // For each record
137 for (unsigned i = 0; i < N;) {
138 // Next record
139 Record *Def = DefList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000140
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000141 // Get and emit name
142 OS << " " << Def->getName();
Jim Laskey908ae272005-10-28 15:20:43 +0000143
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000144 // If bit flags then emit expression (1 << i)
145 if (isBits) OS << " = " << " 1ULL << " << i;
Andrew Trickda96cf22011-04-01 01:56:55 +0000146
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000147 // Depending on 'if more in the list' emit comma
148 if (++i < N) OS << ",";
149
150 OS << "\n";
151 }
152
153 // Close enumeration
154 OS << "};\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000155 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000156
Evan Cheng94214702011-07-01 20:45:01 +0000157 OS << "}\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000158}
159
160//
Bill Wendling4222d802007-05-04 20:38:40 +0000161// FeatureKeyValues - Emit data of all the subtarget features. Used by the
162// command line.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000163//
Evan Cheng94214702011-07-01 20:45:01 +0000164unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000165 // Gather and sort all the features
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000166 std::vector<Record*> FeatureList =
167 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng94214702011-07-01 20:45:01 +0000168
169 if (FeatureList.empty())
170 return 0;
171
Jim Grosbach7c9a7722008-09-11 17:05:32 +0000172 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000173
Jim Laskey908ae272005-10-28 15:20:43 +0000174 // Begin feature table
Jim Laskey581a8f72005-10-26 17:30:34 +0000175 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000176 << "extern const llvm::SubtargetFeatureKV " << Target
177 << "FeatureKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000178
Jim Laskey908ae272005-10-28 15:20:43 +0000179 // For each feature
Evan Cheng94214702011-07-01 20:45:01 +0000180 unsigned NumFeatures = 0;
Jim Laskeydbe40062006-12-12 20:55:58 +0000181 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000182 // Next feature
183 Record *Feature = FeatureList[i];
184
Bill Wendling4222d802007-05-04 20:38:40 +0000185 const std::string &Name = Feature->getName();
186 const std::string &CommandLineName = Feature->getValueAsString("Name");
187 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickda96cf22011-04-01 01:56:55 +0000188
Jim Laskeydbe40062006-12-12 20:55:58 +0000189 if (CommandLineName.empty()) continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000190
Jim Grosbachda4231f2009-03-26 16:17:51 +0000191 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000192 OS << " { "
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000193 << "\"" << CommandLineName << "\", "
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000194 << "\"" << Desc << "\", "
Evan Cheng94214702011-07-01 20:45:01 +0000195 << Target << "::" << Name << ", ";
Bill Wendling4222d802007-05-04 20:38:40 +0000196
Andrew Trickda96cf22011-04-01 01:56:55 +0000197 const std::vector<Record*> &ImpliesList =
Bill Wendling4222d802007-05-04 20:38:40 +0000198 Feature->getValueAsListOfDefs("Implies");
Andrew Trickda96cf22011-04-01 01:56:55 +0000199
Bill Wendling4222d802007-05-04 20:38:40 +0000200 if (ImpliesList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000201 OS << "0ULL";
Bill Wendling4222d802007-05-04 20:38:40 +0000202 } else {
203 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000204 OS << Target << "::" << ImpliesList[j]->getName();
Bill Wendling4222d802007-05-04 20:38:40 +0000205 if (++j < M) OS << " | ";
206 }
207 }
208
209 OS << " }";
Evan Cheng94214702011-07-01 20:45:01 +0000210 ++NumFeatures;
Andrew Trickda96cf22011-04-01 01:56:55 +0000211
Jim Laskey10b1dd92005-10-31 17:16:01 +0000212 // Depending on 'if more in the list' emit comma
Jim Laskeydbe40062006-12-12 20:55:58 +0000213 if ((i + 1) < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000214
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000215 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000216 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000217
Jim Laskey908ae272005-10-28 15:20:43 +0000218 // End feature table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000219 OS << "};\n";
220
Evan Cheng94214702011-07-01 20:45:01 +0000221 return NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000222}
223
224//
225// CPUKeyValues - Emit data of all the subtarget processors. Used by command
226// line.
227//
Evan Cheng94214702011-07-01 20:45:01 +0000228unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000229 // Gather and sort processor information
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000230 std::vector<Record*> ProcessorList =
231 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000232 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000233
Jim Laskey908ae272005-10-28 15:20:43 +0000234 // Begin processor table
Jim Laskey581a8f72005-10-26 17:30:34 +0000235 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000236 << "extern const llvm::SubtargetFeatureKV " << Target
237 << "SubTypeKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000238
Jim Laskey908ae272005-10-28 15:20:43 +0000239 // For each processor
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000240 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
241 // Next processor
242 Record *Processor = ProcessorList[i];
243
Bill Wendling4222d802007-05-04 20:38:40 +0000244 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickda96cf22011-04-01 01:56:55 +0000245 const std::vector<Record*> &FeatureList =
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000246 Processor->getValueAsListOfDefs("Features");
Andrew Trickda96cf22011-04-01 01:56:55 +0000247
Jim Laskey908ae272005-10-28 15:20:43 +0000248 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000249 OS << " { "
250 << "\"" << Name << "\", "
251 << "\"Select the " << Name << " processor\", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000252
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000253 if (FeatureList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000254 OS << "0ULL";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000255 } else {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000256 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000257 OS << Target << "::" << FeatureList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000258 if (++j < M) OS << " | ";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000259 }
260 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000261
Bill Wendling4222d802007-05-04 20:38:40 +0000262 // The "0" is for the "implies" section of this data structure.
Evan Chengb6a63882011-04-15 19:35:46 +0000263 OS << ", 0ULL }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000264
Jim Laskey10b1dd92005-10-31 17:16:01 +0000265 // Depending on 'if more in the list' emit comma
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000266 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000267
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000268 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000269 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000270
Jim Laskey908ae272005-10-28 15:20:43 +0000271 // End processor table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000272 OS << "};\n";
273
Evan Cheng94214702011-07-01 20:45:01 +0000274 return ProcessorList.size();
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000275}
Jim Laskey7dc02042005-10-22 07:59:56 +0000276
Jim Laskey581a8f72005-10-26 17:30:34 +0000277//
David Goodwinfac85412009-08-17 16:02:57 +0000278// FormItineraryStageString - Compose a string containing the stage
279// data initialization for the specified itinerary. N is the number
280// of stages.
Jim Laskey0d841e02005-10-27 19:47:21 +0000281//
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000282void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
283 Record *ItinData,
David Goodwinfac85412009-08-17 16:02:57 +0000284 std::string &ItinString,
285 unsigned &NStages) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000286 // Get states list
Bill Wendling4222d802007-05-04 20:38:40 +0000287 const std::vector<Record*> &StageList =
288 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey908ae272005-10-28 15:20:43 +0000289
290 // For each stage
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000291 unsigned N = NStages = StageList.size();
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000292 for (unsigned i = 0; i < N;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000293 // Next stage
Bill Wendling4222d802007-05-04 20:38:40 +0000294 const Record *Stage = StageList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000295
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000296 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey0d841e02005-10-27 19:47:21 +0000297 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskey7f39c142005-11-03 22:47:41 +0000298 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000299
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000300 // Get unit list
Bill Wendling4222d802007-05-04 20:38:40 +0000301 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickda96cf22011-04-01 01:56:55 +0000302
Jim Laskey908ae272005-10-28 15:20:43 +0000303 // For each unit
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000304 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000305 // Add name and bitwise or
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000306 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000307 if (++j < M) ItinString += " | ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000308 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000309
David Goodwin1a8f36e2009-08-12 18:31:53 +0000310 int TimeInc = Stage->getValueAsInt("TimeInc");
311 ItinString += ", " + itostr(TimeInc);
312
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000313 int Kind = Stage->getValueAsInt("Kind");
314 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
315
Jim Laskey908ae272005-10-28 15:20:43 +0000316 // Close off stage
317 ItinString += " }";
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000318 if (++i < N) ItinString += ", ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000319 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000320}
321
322//
David Goodwinfac85412009-08-17 16:02:57 +0000323// FormItineraryOperandCycleString - Compose a string containing the
324// operand cycle initialization for the specified itinerary. N is the
325// number of operands that has cycles specified.
Jim Laskey0d841e02005-10-27 19:47:21 +0000326//
David Goodwinfac85412009-08-17 16:02:57 +0000327void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
328 std::string &ItinString, unsigned &NOperandCycles) {
329 // Get operand cycle list
330 const std::vector<int64_t> &OperandCycleList =
331 ItinData->getValueAsListOfInts("OperandCycles");
332
333 // For each operand cycle
334 unsigned N = NOperandCycles = OperandCycleList.size();
335 for (unsigned i = 0; i < N;) {
336 // Next operand cycle
337 const int OCycle = OperandCycleList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000338
David Goodwinfac85412009-08-17 16:02:57 +0000339 ItinString += " " + itostr(OCycle);
340 if (++i < N) ItinString += ", ";
341 }
342}
343
Evan Cheng63d66ee2010-09-28 23:50:49 +0000344void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
345 Record *ItinData,
346 std::string &ItinString,
347 unsigned NOperandCycles) {
348 const std::vector<Record*> &BypassList =
349 ItinData->getValueAsListOfDefs("Bypasses");
350 unsigned N = BypassList.size();
Evan Cheng3881cb72010-09-29 22:42:35 +0000351 unsigned i = 0;
352 for (; i < N;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000353 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng3881cb72010-09-29 22:42:35 +0000354 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000355 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000356 for (; i < NOperandCycles;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000357 ItinString += " 0";
Evan Cheng3881cb72010-09-29 22:42:35 +0000358 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000359 }
360}
361
David Goodwinfac85412009-08-17 16:02:57 +0000362//
Andrew Trick2661b412012-07-07 04:00:00 +0000363// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
364// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
365// by CodeGenSchedClass::Index.
David Goodwinfac85412009-08-17 16:02:57 +0000366//
Andrew Trick2661b412012-07-07 04:00:00 +0000367void SubtargetEmitter::
368EmitStageAndOperandCycleData(raw_ostream &OS,
369 std::vector<std::vector<InstrItinerary> >
370 &ProcItinLists) {
Jim Laskey908ae272005-10-28 15:20:43 +0000371
Andrew Trickcb941922012-07-09 20:43:03 +0000372 // Multiple processor models may share an itinerary record. Emit it once.
373 SmallPtrSet<Record*, 8> ItinsDefSet;
374
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000375 // Emit functional units for all the itineraries.
Andrew Trick2661b412012-07-07 04:00:00 +0000376 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
377 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000378
Andrew Trickcb941922012-07-09 20:43:03 +0000379 if (!ItinsDefSet.insert(PI->ItinsDef))
380 continue;
381
Andrew Trick2661b412012-07-07 04:00:00 +0000382 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000383 if (FUs.empty())
384 continue;
385
Andrew Trick2661b412012-07-07 04:00:00 +0000386 const std::string &Name = PI->ItinsDef->getName();
387 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000388 << "namespace " << Name << "FU {\n";
389
390 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkelb460a332012-06-22 20:27:13 +0000391 OS << " const unsigned " << FUs[j]->getName()
392 << " = 1 << " << j << ";\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000393
394 OS << "}\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000395
Andrew Trick2661b412012-07-07 04:00:00 +0000396 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Evan Cheng3881cb72010-09-29 22:42:35 +0000397 if (BPs.size()) {
398 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
399 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000400
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000401 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000402 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000403 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng3881cb72010-09-29 22:42:35 +0000404 << " = 1 << " << j << ";\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000405
Evan Cheng3881cb72010-09-29 22:42:35 +0000406 OS << "}\n";
407 }
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000408 }
409
Jim Laskey908ae272005-10-28 15:20:43 +0000410 // Begin stages table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000411 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
412 "Stages[] = {\n";
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000413 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000414
David Goodwinfac85412009-08-17 16:02:57 +0000415 // Begin operand cycle table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000416 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000417 "OperandCycles[] = {\n";
David Goodwinfac85412009-08-17 16:02:57 +0000418 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000419
420 // Begin pipeline bypass table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000421 std::string BypassTable = "extern const unsigned " + Target +
Andrew Tricka11a6282012-07-07 03:59:48 +0000422 "ForwardingPaths[] = {\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000423 BypassTable += " 0, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000424
Andrew Trick2661b412012-07-07 04:00:00 +0000425 // For each Itinerary across all processors, add a unique entry to the stages,
426 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
427 // object with computed offsets to the ProcItinLists result.
David Goodwinfac85412009-08-17 16:02:57 +0000428 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng3881cb72010-09-29 22:42:35 +0000429 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick2661b412012-07-07 04:00:00 +0000430 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
431 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
432 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickda96cf22011-04-01 01:56:55 +0000433
Andrew Trick2661b412012-07-07 04:00:00 +0000434 // Add process itinerary to the list.
435 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickda96cf22011-04-01 01:56:55 +0000436
Andrew Trick2661b412012-07-07 04:00:00 +0000437 // If this processor defines no itineraries, then leave the itinerary list
438 // empty.
439 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
440 if (ProcModel.ItinDefList.empty())
Andrew Trickd85934b2012-06-22 03:58:51 +0000441 continue;
Andrew Trickd85934b2012-06-22 03:58:51 +0000442
Andrew Trick2661b412012-07-07 04:00:00 +0000443 // Reserve index==0 for NoItinerary.
444 ItinList.resize(SchedModels.numItineraryClasses()+1);
445
446 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000447
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000448 // For each itinerary data
Andrew Trick2661b412012-07-07 04:00:00 +0000449 for (unsigned SchedClassIdx = 0,
450 SchedClassEnd = ProcModel.ItinDefList.size();
451 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
452
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000453 // Next itinerary data
Andrew Trick2661b412012-07-07 04:00:00 +0000454 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickda96cf22011-04-01 01:56:55 +0000455
Jim Laskey908ae272005-10-28 15:20:43 +0000456 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000457 std::string ItinStageString;
Andrew Trick2661b412012-07-07 04:00:00 +0000458 unsigned NStages = 0;
459 if (ItinData)
460 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000461
David Goodwinfac85412009-08-17 16:02:57 +0000462 // Get string and operand cycle count
463 std::string ItinOperandCycleString;
Andrew Trick2661b412012-07-07 04:00:00 +0000464 unsigned NOperandCycles = 0;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000465 std::string ItinBypassString;
Andrew Trick2661b412012-07-07 04:00:00 +0000466 if (ItinData) {
467 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
468 NOperandCycles);
469
470 FormItineraryBypassString(Name, ItinData, ItinBypassString,
471 NOperandCycles);
472 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000473
David Goodwinfac85412009-08-17 16:02:57 +0000474 // Check to see if stage already exists and create if it doesn't
475 unsigned FindStage = 0;
476 if (NStages > 0) {
477 FindStage = ItinStageMap[ItinStageString];
478 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000479 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
480 StageTable += ItinStageString + ", // " + itostr(StageCount);
481 if (NStages > 1)
482 StageTable += "-" + itostr(StageCount + NStages - 1);
483 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000484 // Record Itin class number.
485 ItinStageMap[ItinStageString] = FindStage = StageCount;
486 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000487 }
488 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000489
David Goodwinfac85412009-08-17 16:02:57 +0000490 // Check to see if operand cycle already exists and create if it doesn't
491 unsigned FindOperandCycle = 0;
492 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000493 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
494 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000495 if (FindOperandCycle == 0) {
496 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000497 OperandCycleTable += ItinOperandCycleString + ", // ";
498 std::string OperandIdxComment = itostr(OperandCycleCount);
499 if (NOperandCycles > 1)
500 OperandIdxComment += "-"
501 + itostr(OperandCycleCount + NOperandCycles - 1);
502 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000503 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000504 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000505 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000506 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000507 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000508 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000509 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000510 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000511
Evan Cheng5f54ce32010-09-09 18:18:55 +0000512 // Set up itinerary as location and location + stage count
Andrew Trick2661b412012-07-07 04:00:00 +0000513 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000514 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
515 FindOperandCycle,
516 FindOperandCycle + NOperandCycles};
517
Jim Laskey908ae272005-10-28 15:20:43 +0000518 // Inject - empty slots will be 0, 0
Andrew Trick2661b412012-07-07 04:00:00 +0000519 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000520 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000521 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000522
Jim Laskey7f39c142005-11-03 22:47:41 +0000523 // Closing stage
Andrew Trick2661b412012-07-07 04:00:00 +0000524 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwinfac85412009-08-17 16:02:57 +0000525 StageTable += "};\n";
526
527 // Closing operand cycles
Andrew Trick2661b412012-07-07 04:00:00 +0000528 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwinfac85412009-08-17 16:02:57 +0000529 OperandCycleTable += "};\n";
530
Andrew Trick2661b412012-07-07 04:00:00 +0000531 BypassTable += " 0 // End bypass tables\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000532 BypassTable += "};\n";
533
David Goodwinfac85412009-08-17 16:02:57 +0000534 // Emit tables.
535 OS << StageTable;
536 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000537 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000538}
539
Andrew Trick2661b412012-07-07 04:00:00 +0000540//
541// EmitProcessorData - Generate data for processor itineraries that were
542// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
543// Itineraries for each processor. The Itinerary lists are indexed on
544// CodeGenSchedClass::Index.
545//
546void SubtargetEmitter::
547EmitItineraries(raw_ostream &OS,
548 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
549
Andrew Trickcb941922012-07-09 20:43:03 +0000550 // Multiple processor models may share an itinerary record. Emit it once.
551 SmallPtrSet<Record*, 8> ItinsDefSet;
552
Andrew Trick2661b412012-07-07 04:00:00 +0000553 // For each processor's machine model
554 std::vector<std::vector<InstrItinerary> >::iterator
555 ProcItinListsIter = ProcItinLists.begin();
556 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick48605c32012-09-15 00:19:57 +0000557 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickcb941922012-07-09 20:43:03 +0000558
Andrew Trick2661b412012-07-07 04:00:00 +0000559 Record *ItinsDef = PI->ItinsDef;
Andrew Trickcb941922012-07-09 20:43:03 +0000560 if (!ItinsDefSet.insert(ItinsDef))
561 continue;
Andrew Trick2661b412012-07-07 04:00:00 +0000562
563 // Get processor itinerary name
564 const std::string &Name = ItinsDef->getName();
565
566 // Get the itinerary list for the processor.
567 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick48605c32012-09-15 00:19:57 +0000568 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick2661b412012-07-07 04:00:00 +0000569
570 OS << "\n";
571 OS << "static const llvm::InstrItinerary ";
572 if (ItinList.empty()) {
573 OS << '*' << Name << " = 0;\n";
574 continue;
575 }
576
577 // Begin processor itinerary table
578 OS << Name << "[] = {\n";
579
580 // For each itinerary class in CodeGenSchedClass::Index order.
581 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
582 InstrItinerary &Intinerary = ItinList[j];
583
584 // Emit Itinerary in the form of
585 // { firstStage, lastStage, firstCycle, lastCycle } // index
586 OS << " { " <<
587 Intinerary.NumMicroOps << ", " <<
588 Intinerary.FirstStage << ", " <<
589 Intinerary.LastStage << ", " <<
590 Intinerary.FirstOperandCycle << ", " <<
591 Intinerary.LastOperandCycle << " }" <<
592 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
593 }
594 // End processor itinerary table
595 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
596 OS << "};\n";
597 }
598}
599
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000600// Emit either the value defined in the TableGen Record, or the default
Andrew Trick2661b412012-07-07 04:00:00 +0000601// value defined in the C++ header. The Record is null if the processor does not
602// define a model.
603void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trickfc992992012-06-05 03:44:40 +0000604 const char *Name, char Separator) {
605 OS << " ";
Andrew Trick2661b412012-07-07 04:00:00 +0000606 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trickfc992992012-06-05 03:44:40 +0000607 if (V >= 0)
608 OS << V << Separator << " // " << Name;
609 else
Andrew Trick2661b412012-07-07 04:00:00 +0000610 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000611 OS << '\n';
612}
613
Andrew Trick40096d22012-09-17 22:18:45 +0000614void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
615 raw_ostream &OS) {
616 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
617
618 OS << "\n// {Name, NumUnits, SuperIdx}\n";
619 OS << "static const llvm::MCProcResourceDesc "
620 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
621 << " {DBGFIELD(\"InvalidUnit\") 0, 0}" << Sep << "\n";
622
623 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
624 Record *PRDef = ProcModel.ProcResourceDefs[i];
625
626 // Find the SuperIdx
627 unsigned SuperIdx = 0;
628 Record *SuperDef = 0;
629 if (PRDef->getValueInit("Super")->isComplete()) {
630 SuperDef =
631 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"), ProcModel);
632 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
633 }
634 // Emit the ProcResourceDesc
635 if (i+1 == e)
636 Sep = ' ';
637 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
638 if (PRDef->getName().size() < 15)
639 OS.indent(15 - PRDef->getName().size());
640 OS << PRDef->getValueAsInt("NumUnits") << ", " << SuperIdx
641 << "}" << Sep << " // #" << i+1;
642 if (SuperDef)
643 OS << ", Super=" << SuperDef->getName();
644 OS << "\n";
645 }
646 OS << "};\n";
647}
648
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000649// Find the WriteRes Record that defines processor resources for this
650// SchedWrite.
651Record *SubtargetEmitter::FindWriteResources(
652 Record *WriteDef, const CodeGenProcModel &ProcModel) {
653
654 // Check if the SchedWrite is already subtarget-specific and directly
655 // specifies a set of processor resources.
656 if (WriteDef->isSubClassOf("SchedWriteRes"))
657 return WriteDef;
658
659 // Check this processor's list of write resources.
660 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
661 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
662 if (!(*WRI)->isSubClassOf("WriteRes"))
663 continue;
664 if (WriteDef == (*WRI)->getValueAsDef("WriteType"))
665 return *WRI;
666 }
667 throw TGError(ProcModel.ModelDef->getLoc(),
668 std::string("Processor does not define resources for ")
669 + WriteDef->getName());
670}
671
672/// Find the ReadAdvance record for the given SchedRead on this processor or
673/// return NULL.
674Record *SubtargetEmitter::FindReadAdvance(Record *ReadDef,
675 const CodeGenProcModel &ProcModel) {
676 // Check for SchedReads that directly specify a ReadAdvance.
677 if (ReadDef->isSubClassOf("SchedReadAdvance"))
678 return ReadDef;
679
680 // Check this processor's ReadAdvanceList.
681 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
682 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
683 if (!(*RAI)->isSubClassOf("ReadAdvance"))
684 continue;
685 if (ReadDef == (*RAI)->getValueAsDef("ReadType"))
686 return *RAI;
687 }
688 if (ReadDef->getName() != "ReadDefault") {
689 throw TGError(ProcModel.ModelDef->getLoc(),
690 std::string("Processor does not define resources for ")
691 + ReadDef->getName());
692 }
693 return NULL;
694}
695
696// Generate the SchedClass table for this processor and update global
697// tables. Must be called for each processor in order.
698void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
699 SchedClassTables &SchedTables) {
700 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
701 if (!ProcModel.hasInstrSchedModel())
702 return;
703
704 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
705 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
706 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
707 SCTab.resize(SCTab.size() + 1);
708 MCSchedClassDesc &SCDesc = SCTab.back();
709 SCDesc.Name = SCI->Name.c_str();
710 SCDesc.NumMicroOps = 0;
711 SCDesc.BeginGroup = false;
712 SCDesc.EndGroup = false;
713 SCDesc.WriteProcResIdx = 0;
714 SCDesc.WriteLatencyIdx = 0;
715 SCDesc.ReadAdvanceIdx = 0;
716
717 // A Variant SchedClass has no resources of its own.
718 if (!SCI->Transitions.empty()) {
719 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
720 continue;
721 }
722
723 // Determine if the SchedClass is actually reachable on this processor. If
724 // not don't try to locate the processor resources, it will fail.
725 // If ProcIndices contains 0, this class applies to all processors.
726 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
727 if (SCI->ProcIndices[0] != 0) {
728 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
729 SCI->ProcIndices.end(), ProcModel.Index);
730 if (PIPos == SCI->ProcIndices.end())
731 continue;
732 }
733 IdxVec Writes = SCI->Writes;
734 IdxVec Reads = SCI->Reads;
735 if (SCI->ItinClassDef) {
736 assert(SCI->InstRWs.empty() && "ItinClass should not have InstRWs");
737 // Check this processor's itinerary class resources.
738 for (RecIter II = ProcModel.ItinRWDefs.begin(),
739 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
740 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
741 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
742 != Matched.end()) {
743 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
744 Writes, Reads);
745 break;
746 }
747 }
748 if (Writes.empty()) {
749 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
750 << " does not have resources for itinerary class "
751 << SCI->ItinClassDef->getName() << '\n');
752 }
753 }
754 else if (!SCI->InstRWs.empty()) {
755 assert(SCI->Writes.empty() && SCI->Reads.empty() &&
756 "InstRW class should not have its own ReadWrites");
757 Record *RWDef = 0;
758 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
759 RWI != RWE; ++RWI) {
760 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
761 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
762 RWDef = *RWI;
763 break;
764 }
765 }
766 if (RWDef) {
767 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
768 Writes, Reads);
769 }
770 }
771 // Sum resources across all operand writes.
772 std::vector<MCWriteProcResEntry> WriteProcResources;
773 std::vector<MCWriteLatencyEntry> WriteLatencies;
774 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
775 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
776 IdxVec WriteSeq;
777 SchedModels.expandRWSequence(*WI, WriteSeq, /*IsRead=*/false);
778
779 // For each operand, create a latency entry.
780 MCWriteLatencyEntry WLEntry;
781 WLEntry.Cycles = 0;
782 WLEntry.WriteResourceID = WriteSeq.back();
783
784 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
785 WSI != WSE; ++WSI) {
786
787 Record *WriteDef = SchedModels.getSchedWrite(*WSI).TheDef;
788 Record *WriteRes = FindWriteResources(WriteDef, ProcModel);
789
790 // Mark the parent class as invalid for unsupported write types.
791 if (WriteRes->getValueAsBit("Unsupported")) {
792 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
793 break;
794 }
795 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
796 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
797 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
798 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
799
800 // Create an entry for each ProcResource listed in WriteRes.
801 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
802 std::vector<int64_t> Cycles =
803 WriteRes->getValueAsListOfInts("ResourceCycles");
804 for (unsigned PRIdx = 0, PREnd = PRVec.size();
805 PRIdx != PREnd; ++PRIdx) {
806 MCWriteProcResEntry WPREntry;
807 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
808 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
809 if (Cycles.size() > PRIdx)
810 WPREntry.Cycles = Cycles[PRIdx];
811 else
812 WPREntry.Cycles = 1;
813 WriteProcResources.push_back(WPREntry);
814 }
815 }
816 WriteLatencies.push_back(WLEntry);
817 }
818 // Create an entry for each operand Read in this SchedClass.
819 // Entries must be sorted first by UseIdx then by WriteResourceID.
820 for (unsigned UseIdx = 0, EndIdx = Reads.size();
821 UseIdx != EndIdx; ++UseIdx) {
822 Record *ReadDef = SchedModels.getSchedRead(Reads[UseIdx]).TheDef;
823 Record *ReadAdvance = FindReadAdvance(ReadDef, ProcModel);
824 if (!ReadAdvance)
825 continue;
826
827 // Mark the parent class as invalid for unsupported write types.
828 if (ReadAdvance->getValueAsBit("Unsupported")) {
829 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
830 break;
831 }
832 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
833 IdxVec WriteIDs;
834 if (ValidWrites.empty())
835 WriteIDs.push_back(0);
836 else {
837 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
838 VWI != VWE; ++VWI) {
839 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
840 }
841 }
842 std::sort(WriteIDs.begin(), WriteIDs.end());
843 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
844 MCReadAdvanceEntry RAEntry;
845 RAEntry.UseIdx = UseIdx;
846 RAEntry.WriteResourceID = *WI;
847 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
848 ReadAdvanceEntries.push_back(RAEntry);
849 }
850 }
851 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
852 WriteProcResources.clear();
853 WriteLatencies.clear();
854 ReadAdvanceEntries.clear();
855 }
856 // Add the information for this SchedClass to the global tables using basic
857 // compression.
858 //
859 // WritePrecRes entries are sorted by ProcResIdx.
860 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
861 LessWriteProcResources());
862
863 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
864 std::vector<MCWriteProcResEntry>::iterator WPRPos =
865 std::search(SchedTables.WriteProcResources.begin(),
866 SchedTables.WriteProcResources.end(),
867 WriteProcResources.begin(), WriteProcResources.end());
868 if (WPRPos != SchedTables.WriteProcResources.end())
869 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
870 else {
871 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
872 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
873 WriteProcResources.end());
874 }
875 // Latency entries must remain in operand order.
876 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
877 std::vector<MCWriteLatencyEntry>::iterator WLPos =
878 std::search(SchedTables.WriteLatencies.begin(),
879 SchedTables.WriteLatencies.end(),
880 WriteLatencies.begin(), WriteLatencies.end());
881 if (WLPos != SchedTables.WriteLatencies.end())
882 SCDesc.WriteLatencyIdx = WLPos - SchedTables.WriteLatencies.begin();
883 else {
884 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
885 SchedTables.WriteLatencies.insert(WLPos, WriteLatencies.begin(),
886 WriteLatencies.end());
887 }
888 // ReadAdvanceEntries must remain in operand order.
889 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
890 std::vector<MCReadAdvanceEntry>::iterator RAPos =
891 std::search(SchedTables.ReadAdvanceEntries.begin(),
892 SchedTables.ReadAdvanceEntries.end(),
893 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
894 if (RAPos != SchedTables.ReadAdvanceEntries.end())
895 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
896 else {
897 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
898 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
899 ReadAdvanceEntries.end());
900 }
901 }
902}
903
Andrew Trick2661b412012-07-07 04:00:00 +0000904void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
905 // For each processor model.
906 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
907 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick40096d22012-09-17 22:18:45 +0000908 // Emit processor resource table.
909 if (PI->hasInstrSchedModel())
910 EmitProcessorResources(*PI, OS);
911 else if(!PI->ProcResourceDefs.empty())
912 throw TGError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000913 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick40096d22012-09-17 22:18:45 +0000914
Andrew Trickfc992992012-06-05 03:44:40 +0000915 // Begin processor itinerary properties
916 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000917 OS << "static const llvm::MCSchedModel " << PI->ModelName << "(\n";
918 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
919 EmitProcessorProp(OS, PI->ModelDef, "MinLatency", ',');
920 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
921 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trickd43b5c92012-08-08 02:44:16 +0000922 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Trick2661b412012-07-07 04:00:00 +0000923 if (SchedModels.hasItineraryClasses())
Andrew Trick40096d22012-09-17 22:18:45 +0000924 OS << " " << PI->ItinsDef->getName() << ");\n";
Andrew Trickd85934b2012-06-22 03:58:51 +0000925 else
Andrew Trick40096d22012-09-17 22:18:45 +0000926 OS << " 0); // No Itinerary\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000927 }
Jim Laskey10b1dd92005-10-31 17:16:01 +0000928}
929
930//
931// EmitProcessorLookup - generate cpu name to itinerary lookup table.
932//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000933void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +0000934 // Gather and sort processor information
935 std::vector<Record*> ProcessorList =
936 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000937 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +0000938
939 // Begin processor table
940 OS << "\n";
941 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000942 << "extern const llvm::SubtargetInfoKV "
Andrew Trick2661b412012-07-07 04:00:00 +0000943 << Target << "ProcSchedKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000944
Jim Laskey10b1dd92005-10-31 17:16:01 +0000945 // For each processor
946 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
947 // Next processor
948 Record *Processor = ProcessorList[i];
949
Bill Wendling4222d802007-05-04 20:38:40 +0000950 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick2661b412012-07-07 04:00:00 +0000951 const std::string &ProcModelName =
Andrew Trick48605c32012-09-15 00:19:57 +0000952 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickda96cf22011-04-01 01:56:55 +0000953
Jim Laskey10b1dd92005-10-31 17:16:01 +0000954 // Emit as { "cpu", procinit },
Andrew Trick40096d22012-09-17 22:18:45 +0000955 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000956
Jim Laskey10b1dd92005-10-31 17:16:01 +0000957 // Depending on ''if more in the list'' emit comma
958 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000959
Jim Laskey10b1dd92005-10-31 17:16:01 +0000960 OS << "\n";
961 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000962
Jim Laskey10b1dd92005-10-31 17:16:01 +0000963 // End processor table
964 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +0000965}
966
967//
Andrew Trick2661b412012-07-07 04:00:00 +0000968// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey0d841e02005-10-27 19:47:21 +0000969//
Andrew Trick2661b412012-07-07 04:00:00 +0000970void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick40096d22012-09-17 22:18:45 +0000971 OS << "#ifdef DBGFIELD\n"
972 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
973 << "#endif\n"
974 << "#ifndef NDEBUG\n"
975 << "#define DBGFIELD(x) x,\n"
976 << "#else\n"
977 << "#define DBGFIELD(x)\n"
978 << "#endif\n";
979
Andrew Trick2661b412012-07-07 04:00:00 +0000980 if (SchedModels.hasItineraryClasses()) {
981 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey6cee6302005-11-01 20:06:59 +0000982 // Emit the stage data
Andrew Trick2661b412012-07-07 04:00:00 +0000983 EmitStageAndOperandCycleData(OS, ProcItinLists);
984 EmitItineraries(OS, ProcItinLists);
Jim Laskey6cee6302005-11-01 20:06:59 +0000985 }
Andrew Trick40096d22012-09-17 22:18:45 +0000986
Andrew Trick2661b412012-07-07 04:00:00 +0000987 // Emit the processor machine model
988 EmitProcessorModels(OS);
989 // Emit the processor lookup data
990 EmitProcessorLookup(OS);
Andrew Trick40096d22012-09-17 22:18:45 +0000991
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000992 SchedClassTables SchedTables;
993 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
994 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
995 GenSchedClassTables(*PI, SchedTables);
996 }
997
Andrew Trick40096d22012-09-17 22:18:45 +0000998 OS << "#undef DBGFIELD";
Jim Laskey0d841e02005-10-27 19:47:21 +0000999}
1000
1001//
Jim Laskey581a8f72005-10-26 17:30:34 +00001002// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1003// the subtarget features string.
1004//
Evan Cheng94214702011-07-01 20:45:01 +00001005void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1006 unsigned NumFeatures,
1007 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001008 std::vector<Record*> Features =
1009 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +00001010 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +00001011
Andrew Trickda96cf22011-04-01 01:56:55 +00001012 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1013 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +00001014 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +00001015 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001016 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +00001017 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel3f696e52012-06-12 04:21:36 +00001018 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +00001019
1020 if (Features.empty()) {
1021 OS << "}\n";
1022 return;
1023 }
1024
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001025 OS << " uint64_t Bits = ReInitMCSubtargetInfo(CPU, FS);\n";
Bill Wendling4222d802007-05-04 20:38:40 +00001026
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001027 for (unsigned i = 0; i < Features.size(); i++) {
1028 // Next record
1029 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +00001030 const std::string &Instance = R->getName();
1031 const std::string &Value = R->getValueAsString("Value");
1032 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +00001033
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001034 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001035 OS << " if ((Bits & " << Target << "::"
1036 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001037 << Attribute << " = " << Value << ";\n";
1038 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001039 OS << " if ((Bits & " << Target << "::"
1040 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +00001041 << Attribute << " < " << Value << ") "
1042 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +00001043 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001044
Evan Cheng276365d2011-06-30 01:53:36 +00001045 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +00001046}
1047
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001048//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001049// SubtargetEmitter::run - Main subtarget enumeration emitter.
1050//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001051void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001052 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001053
Evan Chengebdeeab2011-07-08 01:53:10 +00001054 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1055 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1056
1057 OS << "namespace llvm {\n";
1058 Enumeration(OS, "SubtargetFeature", true);
1059 OS << "} // End llvm namespace \n";
1060 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1061
Evan Cheng94214702011-07-01 20:45:01 +00001062 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1063 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +00001064
Evan Cheng94214702011-07-01 20:45:01 +00001065 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001066#if 0
1067 OS << "namespace {\n";
1068#endif
Evan Cheng94214702011-07-01 20:45:01 +00001069 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001070 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +00001071 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001072 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001073 EmitSchedModel(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001074 OS << "\n";
1075#if 0
1076 OS << "}\n";
1077#endif
Evan Cheng94214702011-07-01 20:45:01 +00001078
1079 // MCInstrInfo initialization routine.
1080 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +00001081 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1082 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1083 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001084 if (NumFeatures)
1085 OS << Target << "FeatureKV, ";
1086 else
1087 OS << "0, ";
1088 if (NumProcs)
1089 OS << Target << "SubTypeKV, ";
1090 else
1091 OS << "0, ";
Andrew Trick2661b412012-07-07 04:00:00 +00001092 if (SchedModels.hasItineraryClasses()) {
1093 OS << Target << "ProcSchedKV, "
Evan Cheng94214702011-07-01 20:45:01 +00001094 << Target << "Stages, "
1095 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001096 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001097 } else
1098 OS << "0, 0, 0, 0, ";
1099 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1100
1101 OS << "} // End llvm namespace \n";
1102
1103 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1104
1105 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1106 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1107
1108 OS << "#include \"llvm/Support/Debug.h\"\n";
1109 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
1110 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1111
1112 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1113
Evan Cheng5b1b44892011-07-01 21:01:15 +00001114 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +00001115 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1116 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1117
1118 std::string ClassName = Target + "GenSubtargetInfo";
1119 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001120 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +00001121 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001122 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1123 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001124 << "public:\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +00001125 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001126 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +00001127 << "};\n";
1128 OS << "} // End llvm namespace \n";
1129
1130 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1131
1132 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1133 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1134
1135 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001136 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1137 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001138 if (SchedModels.hasItineraryClasses()) {
1139 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001140 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1141 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Tricka11a6282012-07-07 03:59:48 +00001142 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001143 }
1144
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001145 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1146 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +00001147 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +00001148 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001149 if (NumFeatures)
1150 OS << Target << "FeatureKV, ";
1151 else
1152 OS << "0, ";
1153 if (NumProcs)
1154 OS << Target << "SubTypeKV, ";
1155 else
1156 OS << "0, ";
Andrew Trick2661b412012-07-07 04:00:00 +00001157 if (SchedModels.hasItineraryClasses()) {
1158 OS << Target << "ProcSchedKV, "
Evan Cheng94214702011-07-01 20:45:01 +00001159 << Target << "Stages, "
1160 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001161 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001162 } else
1163 OS << "0, 0, 0, 0, ";
1164 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1165 OS << "} // End llvm namespace \n";
1166
1167 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001168}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001169
1170namespace llvm {
1171
1172void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick2661b412012-07-07 04:00:00 +00001173 CodeGenTarget CGTarget(RK);
1174 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001175}
1176
1177} // End llvm namespace