blob: 4127b379b15db4724c939f7fb1141dcd6391a9c8 [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"
Andrew Trick544c8802012-09-17 22:18:50 +000023#include "llvm/Support/Format.h"
Jeff Cohen9489c042005-10-28 01:43:09 +000024#include <algorithm>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000025#include <map>
26#include <string>
27#include <vector>
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000028using namespace llvm;
29
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000030namespace {
31class SubtargetEmitter {
Andrew Trick52c3a1d2012-09-17 22:18:48 +000032 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
33 // The SchedClassDesc table indexes into a global write resource table, write
34 // latency table, and read advance table.
35 struct SchedClassTables {
36 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
37 std::vector<MCWriteProcResEntry> WriteProcResources;
38 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +000039 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +000040 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
41
42 // Reserve an invalid entry at index 0
43 SchedClassTables() {
44 ProcSchedClasses.resize(1);
45 WriteProcResources.resize(1);
46 WriteLatencies.resize(1);
Andrew Trick3b8fb642012-09-19 04:43:19 +000047 WriterNames.push_back("InvalidWrite");
Andrew Trick52c3a1d2012-09-17 22:18:48 +000048 ReadAdvanceEntries.resize(1);
49 }
50 };
51
52 struct LessWriteProcResources {
53 bool operator()(const MCWriteProcResEntry &LHS,
54 const MCWriteProcResEntry &RHS) {
55 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
56 }
57 };
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000058
59 RecordKeeper &Records;
Andrew Trick2661b412012-07-07 04:00:00 +000060 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000061 std::string Target;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000062
63 void Enumeration(raw_ostream &OS, const char *ClassName, bool isBits);
64 unsigned FeatureKeyValues(raw_ostream &OS);
65 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000066 void FormItineraryStageString(const std::string &Names,
67 Record *ItinData, std::string &ItinString,
68 unsigned &NStages);
69 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
70 unsigned &NOperandCycles);
71 void FormItineraryBypassString(const std::string &Names,
72 Record *ItinData,
73 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick2661b412012-07-07 04:00:00 +000074 void EmitStageAndOperandCycleData(raw_ostream &OS,
75 std::vector<std::vector<InstrItinerary> >
76 &ProcItinLists);
77 void EmitItineraries(raw_ostream &OS,
78 std::vector<std::vector<InstrItinerary> >
79 &ProcItinLists);
80 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000081 char Separator);
Andrew Trick40096d22012-09-17 22:18:45 +000082 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
83 raw_ostream &OS);
Andrew Trick92649882012-09-22 02:24:21 +000084 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick52c3a1d2012-09-17 22:18:48 +000085 const CodeGenProcModel &ProcModel);
Andrew Trick92649882012-09-22 02:24:21 +000086 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
87 const CodeGenProcModel &ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +000088 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
89 SchedClassTables &SchedTables);
Andrew Trick544c8802012-09-17 22:18:50 +000090 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick2661b412012-07-07 04:00:00 +000091 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000092 void EmitProcessorLookup(raw_ostream &OS);
Andrew Trick4d2d1c42012-09-18 03:41:43 +000093 void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS);
Andrew Trick2661b412012-07-07 04:00:00 +000094 void EmitSchedModel(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000095 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
96 unsigned NumProcs);
97
98public:
Andrew Trick2661b412012-07-07 04:00:00 +000099 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
100 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000101
102 void run(raw_ostream &o);
103
104};
105} // End anonymous namespace
106
Jim Laskey7dc02042005-10-22 07:59:56 +0000107//
Jim Laskey581a8f72005-10-26 17:30:34 +0000108// Enumeration - Emit the specified class as an enumeration.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000109//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000110void SubtargetEmitter::Enumeration(raw_ostream &OS,
Jim Laskey581a8f72005-10-26 17:30:34 +0000111 const char *ClassName,
112 bool isBits) {
Jim Laskey908ae272005-10-28 15:20:43 +0000113 // Get all records of class and sort
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000114 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
Duraid Madina42d24c72005-12-30 14:56:37 +0000115 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000116
Evan Chengb6a63882011-04-15 19:35:46 +0000117 unsigned N = DefList.size();
Evan Cheng94214702011-07-01 20:45:01 +0000118 if (N == 0)
119 return;
Evan Chengb6a63882011-04-15 19:35:46 +0000120 if (N > 64) {
121 errs() << "Too many (> 64) subtarget features!\n";
122 exit(1);
123 }
124
Evan Cheng94214702011-07-01 20:45:01 +0000125 OS << "namespace " << Target << " {\n";
126
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000127 // For bit flag enumerations with more than 32 items, emit constants.
128 // Emit an enum for everything else.
129 if (isBits && N > 32) {
130 // For each record
131 for (unsigned i = 0; i < N; i++) {
132 // Next record
133 Record *Def = DefList[i];
Evan Cheng94214702011-07-01 20:45:01 +0000134
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000135 // Get and emit name and expression (1 << i)
136 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
137 }
138 } else {
139 // Open enumeration
140 OS << "enum {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000141
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000142 // For each record
143 for (unsigned i = 0; i < N;) {
144 // Next record
145 Record *Def = DefList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000146
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000147 // Get and emit name
148 OS << " " << Def->getName();
Jim Laskey908ae272005-10-28 15:20:43 +0000149
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000150 // If bit flags then emit expression (1 << i)
151 if (isBits) OS << " = " << " 1ULL << " << i;
Andrew Trickda96cf22011-04-01 01:56:55 +0000152
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000153 // Depending on 'if more in the list' emit comma
154 if (++i < N) OS << ",";
155
156 OS << "\n";
157 }
158
159 // Close enumeration
160 OS << "};\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000161 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000162
Evan Cheng94214702011-07-01 20:45:01 +0000163 OS << "}\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000164}
165
166//
Bill Wendling4222d802007-05-04 20:38:40 +0000167// FeatureKeyValues - Emit data of all the subtarget features. Used by the
168// command line.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000169//
Evan Cheng94214702011-07-01 20:45:01 +0000170unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000171 // Gather and sort all the features
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000172 std::vector<Record*> FeatureList =
173 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng94214702011-07-01 20:45:01 +0000174
175 if (FeatureList.empty())
176 return 0;
177
Jim Grosbach7c9a7722008-09-11 17:05:32 +0000178 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000179
Jim Laskey908ae272005-10-28 15:20:43 +0000180 // Begin feature table
Jim Laskey581a8f72005-10-26 17:30:34 +0000181 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000182 << "extern const llvm::SubtargetFeatureKV " << Target
183 << "FeatureKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000184
Jim Laskey908ae272005-10-28 15:20:43 +0000185 // For each feature
Evan Cheng94214702011-07-01 20:45:01 +0000186 unsigned NumFeatures = 0;
Jim Laskeydbe40062006-12-12 20:55:58 +0000187 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000188 // Next feature
189 Record *Feature = FeatureList[i];
190
Bill Wendling4222d802007-05-04 20:38:40 +0000191 const std::string &Name = Feature->getName();
192 const std::string &CommandLineName = Feature->getValueAsString("Name");
193 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickda96cf22011-04-01 01:56:55 +0000194
Jim Laskeydbe40062006-12-12 20:55:58 +0000195 if (CommandLineName.empty()) continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000196
Jim Grosbachda4231f2009-03-26 16:17:51 +0000197 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000198 OS << " { "
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000199 << "\"" << CommandLineName << "\", "
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000200 << "\"" << Desc << "\", "
Evan Cheng94214702011-07-01 20:45:01 +0000201 << Target << "::" << Name << ", ";
Bill Wendling4222d802007-05-04 20:38:40 +0000202
Andrew Trickda96cf22011-04-01 01:56:55 +0000203 const std::vector<Record*> &ImpliesList =
Bill Wendling4222d802007-05-04 20:38:40 +0000204 Feature->getValueAsListOfDefs("Implies");
Andrew Trickda96cf22011-04-01 01:56:55 +0000205
Bill Wendling4222d802007-05-04 20:38:40 +0000206 if (ImpliesList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000207 OS << "0ULL";
Bill Wendling4222d802007-05-04 20:38:40 +0000208 } else {
209 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000210 OS << Target << "::" << ImpliesList[j]->getName();
Bill Wendling4222d802007-05-04 20:38:40 +0000211 if (++j < M) OS << " | ";
212 }
213 }
214
215 OS << " }";
Evan Cheng94214702011-07-01 20:45:01 +0000216 ++NumFeatures;
Andrew Trickda96cf22011-04-01 01:56:55 +0000217
Jim Laskey10b1dd92005-10-31 17:16:01 +0000218 // Depending on 'if more in the list' emit comma
Jim Laskeydbe40062006-12-12 20:55:58 +0000219 if ((i + 1) < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000220
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000221 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000222 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000223
Jim Laskey908ae272005-10-28 15:20:43 +0000224 // End feature table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000225 OS << "};\n";
226
Evan Cheng94214702011-07-01 20:45:01 +0000227 return NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000228}
229
230//
231// CPUKeyValues - Emit data of all the subtarget processors. Used by command
232// line.
233//
Evan Cheng94214702011-07-01 20:45:01 +0000234unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000235 // Gather and sort processor information
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000236 std::vector<Record*> ProcessorList =
237 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000238 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000239
Jim Laskey908ae272005-10-28 15:20:43 +0000240 // Begin processor table
Jim Laskey581a8f72005-10-26 17:30:34 +0000241 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000242 << "extern const llvm::SubtargetFeatureKV " << Target
243 << "SubTypeKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000244
Jim Laskey908ae272005-10-28 15:20:43 +0000245 // For each processor
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000246 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
247 // Next processor
248 Record *Processor = ProcessorList[i];
249
Bill Wendling4222d802007-05-04 20:38:40 +0000250 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickda96cf22011-04-01 01:56:55 +0000251 const std::vector<Record*> &FeatureList =
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000252 Processor->getValueAsListOfDefs("Features");
Andrew Trickda96cf22011-04-01 01:56:55 +0000253
Jim Laskey908ae272005-10-28 15:20:43 +0000254 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000255 OS << " { "
256 << "\"" << Name << "\", "
257 << "\"Select the " << Name << " processor\", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000258
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000259 if (FeatureList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000260 OS << "0ULL";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000261 } else {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000262 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000263 OS << Target << "::" << FeatureList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000264 if (++j < M) OS << " | ";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000265 }
266 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000267
Bill Wendling4222d802007-05-04 20:38:40 +0000268 // The "0" is for the "implies" section of this data structure.
Evan Chengb6a63882011-04-15 19:35:46 +0000269 OS << ", 0ULL }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000270
Jim Laskey10b1dd92005-10-31 17:16:01 +0000271 // Depending on 'if more in the list' emit comma
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000272 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000273
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000274 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000275 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000276
Jim Laskey908ae272005-10-28 15:20:43 +0000277 // End processor table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000278 OS << "};\n";
279
Evan Cheng94214702011-07-01 20:45:01 +0000280 return ProcessorList.size();
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000281}
Jim Laskey7dc02042005-10-22 07:59:56 +0000282
Jim Laskey581a8f72005-10-26 17:30:34 +0000283//
David Goodwinfac85412009-08-17 16:02:57 +0000284// FormItineraryStageString - Compose a string containing the stage
285// data initialization for the specified itinerary. N is the number
286// of stages.
Jim Laskey0d841e02005-10-27 19:47:21 +0000287//
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000288void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
289 Record *ItinData,
David Goodwinfac85412009-08-17 16:02:57 +0000290 std::string &ItinString,
291 unsigned &NStages) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000292 // Get states list
Bill Wendling4222d802007-05-04 20:38:40 +0000293 const std::vector<Record*> &StageList =
294 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey908ae272005-10-28 15:20:43 +0000295
296 // For each stage
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000297 unsigned N = NStages = StageList.size();
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000298 for (unsigned i = 0; i < N;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000299 // Next stage
Bill Wendling4222d802007-05-04 20:38:40 +0000300 const Record *Stage = StageList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000301
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000302 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey0d841e02005-10-27 19:47:21 +0000303 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskey7f39c142005-11-03 22:47:41 +0000304 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000305
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000306 // Get unit list
Bill Wendling4222d802007-05-04 20:38:40 +0000307 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickda96cf22011-04-01 01:56:55 +0000308
Jim Laskey908ae272005-10-28 15:20:43 +0000309 // For each unit
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000310 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000311 // Add name and bitwise or
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000312 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000313 if (++j < M) ItinString += " | ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000314 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000315
David Goodwin1a8f36e2009-08-12 18:31:53 +0000316 int TimeInc = Stage->getValueAsInt("TimeInc");
317 ItinString += ", " + itostr(TimeInc);
318
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000319 int Kind = Stage->getValueAsInt("Kind");
320 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
321
Jim Laskey908ae272005-10-28 15:20:43 +0000322 // Close off stage
323 ItinString += " }";
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000324 if (++i < N) ItinString += ", ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000325 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000326}
327
328//
David Goodwinfac85412009-08-17 16:02:57 +0000329// FormItineraryOperandCycleString - Compose a string containing the
330// operand cycle initialization for the specified itinerary. N is the
331// number of operands that has cycles specified.
Jim Laskey0d841e02005-10-27 19:47:21 +0000332//
David Goodwinfac85412009-08-17 16:02:57 +0000333void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
334 std::string &ItinString, unsigned &NOperandCycles) {
335 // Get operand cycle list
336 const std::vector<int64_t> &OperandCycleList =
337 ItinData->getValueAsListOfInts("OperandCycles");
338
339 // For each operand cycle
340 unsigned N = NOperandCycles = OperandCycleList.size();
341 for (unsigned i = 0; i < N;) {
342 // Next operand cycle
343 const int OCycle = OperandCycleList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000344
David Goodwinfac85412009-08-17 16:02:57 +0000345 ItinString += " " + itostr(OCycle);
346 if (++i < N) ItinString += ", ";
347 }
348}
349
Evan Cheng63d66ee2010-09-28 23:50:49 +0000350void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
351 Record *ItinData,
352 std::string &ItinString,
353 unsigned NOperandCycles) {
354 const std::vector<Record*> &BypassList =
355 ItinData->getValueAsListOfDefs("Bypasses");
356 unsigned N = BypassList.size();
Evan Cheng3881cb72010-09-29 22:42:35 +0000357 unsigned i = 0;
358 for (; i < N;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000359 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng3881cb72010-09-29 22:42:35 +0000360 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000361 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000362 for (; i < NOperandCycles;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000363 ItinString += " 0";
Evan Cheng3881cb72010-09-29 22:42:35 +0000364 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000365 }
366}
367
David Goodwinfac85412009-08-17 16:02:57 +0000368//
Andrew Trick2661b412012-07-07 04:00:00 +0000369// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
370// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
371// by CodeGenSchedClass::Index.
David Goodwinfac85412009-08-17 16:02:57 +0000372//
Andrew Trick2661b412012-07-07 04:00:00 +0000373void SubtargetEmitter::
374EmitStageAndOperandCycleData(raw_ostream &OS,
375 std::vector<std::vector<InstrItinerary> >
376 &ProcItinLists) {
Jim Laskey908ae272005-10-28 15:20:43 +0000377
Andrew Trickcb941922012-07-09 20:43:03 +0000378 // Multiple processor models may share an itinerary record. Emit it once.
379 SmallPtrSet<Record*, 8> ItinsDefSet;
380
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000381 // Emit functional units for all the itineraries.
Andrew Trick2661b412012-07-07 04:00:00 +0000382 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
383 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000384
Andrew Trickcb941922012-07-09 20:43:03 +0000385 if (!ItinsDefSet.insert(PI->ItinsDef))
386 continue;
387
Andrew Trick2661b412012-07-07 04:00:00 +0000388 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000389 if (FUs.empty())
390 continue;
391
Andrew Trick2661b412012-07-07 04:00:00 +0000392 const std::string &Name = PI->ItinsDef->getName();
393 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000394 << "namespace " << Name << "FU {\n";
395
396 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkelb460a332012-06-22 20:27:13 +0000397 OS << " const unsigned " << FUs[j]->getName()
398 << " = 1 << " << j << ";\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000399
400 OS << "}\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000401
Andrew Trick2661b412012-07-07 04:00:00 +0000402 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Evan Cheng3881cb72010-09-29 22:42:35 +0000403 if (BPs.size()) {
404 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
405 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000406
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000407 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000408 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000409 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng3881cb72010-09-29 22:42:35 +0000410 << " = 1 << " << j << ";\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000411
Evan Cheng3881cb72010-09-29 22:42:35 +0000412 OS << "}\n";
413 }
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000414 }
415
Jim Laskey908ae272005-10-28 15:20:43 +0000416 // Begin stages table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000417 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
418 "Stages[] = {\n";
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000419 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000420
David Goodwinfac85412009-08-17 16:02:57 +0000421 // Begin operand cycle table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000422 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000423 "OperandCycles[] = {\n";
David Goodwinfac85412009-08-17 16:02:57 +0000424 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000425
426 // Begin pipeline bypass table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000427 std::string BypassTable = "extern const unsigned " + Target +
Andrew Tricka11a6282012-07-07 03:59:48 +0000428 "ForwardingPaths[] = {\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000429 BypassTable += " 0, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000430
Andrew Trick2661b412012-07-07 04:00:00 +0000431 // For each Itinerary across all processors, add a unique entry to the stages,
432 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
433 // object with computed offsets to the ProcItinLists result.
David Goodwinfac85412009-08-17 16:02:57 +0000434 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng3881cb72010-09-29 22:42:35 +0000435 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick2661b412012-07-07 04:00:00 +0000436 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
437 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
438 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickda96cf22011-04-01 01:56:55 +0000439
Andrew Trick2661b412012-07-07 04:00:00 +0000440 // Add process itinerary to the list.
441 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickda96cf22011-04-01 01:56:55 +0000442
Andrew Trick2661b412012-07-07 04:00:00 +0000443 // If this processor defines no itineraries, then leave the itinerary list
444 // empty.
445 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
446 if (ProcModel.ItinDefList.empty())
Andrew Trickd85934b2012-06-22 03:58:51 +0000447 continue;
Andrew Trickd85934b2012-06-22 03:58:51 +0000448
Andrew Trick2661b412012-07-07 04:00:00 +0000449 // Reserve index==0 for NoItinerary.
450 ItinList.resize(SchedModels.numItineraryClasses()+1);
451
452 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000453
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000454 // For each itinerary data
Andrew Trick2661b412012-07-07 04:00:00 +0000455 for (unsigned SchedClassIdx = 0,
456 SchedClassEnd = ProcModel.ItinDefList.size();
457 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
458
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000459 // Next itinerary data
Andrew Trick2661b412012-07-07 04:00:00 +0000460 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickda96cf22011-04-01 01:56:55 +0000461
Jim Laskey908ae272005-10-28 15:20:43 +0000462 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000463 std::string ItinStageString;
Andrew Trick2661b412012-07-07 04:00:00 +0000464 unsigned NStages = 0;
465 if (ItinData)
466 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000467
David Goodwinfac85412009-08-17 16:02:57 +0000468 // Get string and operand cycle count
469 std::string ItinOperandCycleString;
Andrew Trick2661b412012-07-07 04:00:00 +0000470 unsigned NOperandCycles = 0;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000471 std::string ItinBypassString;
Andrew Trick2661b412012-07-07 04:00:00 +0000472 if (ItinData) {
473 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
474 NOperandCycles);
475
476 FormItineraryBypassString(Name, ItinData, ItinBypassString,
477 NOperandCycles);
478 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000479
David Goodwinfac85412009-08-17 16:02:57 +0000480 // Check to see if stage already exists and create if it doesn't
481 unsigned FindStage = 0;
482 if (NStages > 0) {
483 FindStage = ItinStageMap[ItinStageString];
484 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000485 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
486 StageTable += ItinStageString + ", // " + itostr(StageCount);
487 if (NStages > 1)
488 StageTable += "-" + itostr(StageCount + NStages - 1);
489 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000490 // Record Itin class number.
491 ItinStageMap[ItinStageString] = FindStage = StageCount;
492 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000493 }
494 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000495
David Goodwinfac85412009-08-17 16:02:57 +0000496 // Check to see if operand cycle already exists and create if it doesn't
497 unsigned FindOperandCycle = 0;
498 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000499 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
500 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000501 if (FindOperandCycle == 0) {
502 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000503 OperandCycleTable += ItinOperandCycleString + ", // ";
504 std::string OperandIdxComment = itostr(OperandCycleCount);
505 if (NOperandCycles > 1)
506 OperandIdxComment += "-"
507 + itostr(OperandCycleCount + NOperandCycles - 1);
508 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000509 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000510 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000511 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000512 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000513 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000514 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000515 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000516 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000517
Evan Cheng5f54ce32010-09-09 18:18:55 +0000518 // Set up itinerary as location and location + stage count
Andrew Trick2661b412012-07-07 04:00:00 +0000519 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000520 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
521 FindOperandCycle,
522 FindOperandCycle + NOperandCycles};
523
Jim Laskey908ae272005-10-28 15:20:43 +0000524 // Inject - empty slots will be 0, 0
Andrew Trick2661b412012-07-07 04:00:00 +0000525 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000526 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000527 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000528
Jim Laskey7f39c142005-11-03 22:47:41 +0000529 // Closing stage
Andrew Trick2661b412012-07-07 04:00:00 +0000530 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwinfac85412009-08-17 16:02:57 +0000531 StageTable += "};\n";
532
533 // Closing operand cycles
Andrew Trick2661b412012-07-07 04:00:00 +0000534 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwinfac85412009-08-17 16:02:57 +0000535 OperandCycleTable += "};\n";
536
Andrew Trick2661b412012-07-07 04:00:00 +0000537 BypassTable += " 0 // End bypass tables\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000538 BypassTable += "};\n";
539
David Goodwinfac85412009-08-17 16:02:57 +0000540 // Emit tables.
541 OS << StageTable;
542 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000543 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000544}
545
Andrew Trick2661b412012-07-07 04:00:00 +0000546//
547// EmitProcessorData - Generate data for processor itineraries that were
548// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
549// Itineraries for each processor. The Itinerary lists are indexed on
550// CodeGenSchedClass::Index.
551//
552void SubtargetEmitter::
553EmitItineraries(raw_ostream &OS,
554 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
555
Andrew Trickcb941922012-07-09 20:43:03 +0000556 // Multiple processor models may share an itinerary record. Emit it once.
557 SmallPtrSet<Record*, 8> ItinsDefSet;
558
Andrew Trick2661b412012-07-07 04:00:00 +0000559 // For each processor's machine model
560 std::vector<std::vector<InstrItinerary> >::iterator
561 ProcItinListsIter = ProcItinLists.begin();
562 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick48605c32012-09-15 00:19:57 +0000563 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickcb941922012-07-09 20:43:03 +0000564
Andrew Trick2661b412012-07-07 04:00:00 +0000565 Record *ItinsDef = PI->ItinsDef;
Andrew Trickcb941922012-07-09 20:43:03 +0000566 if (!ItinsDefSet.insert(ItinsDef))
567 continue;
Andrew Trick2661b412012-07-07 04:00:00 +0000568
569 // Get processor itinerary name
570 const std::string &Name = ItinsDef->getName();
571
572 // Get the itinerary list for the processor.
573 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick48605c32012-09-15 00:19:57 +0000574 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick2661b412012-07-07 04:00:00 +0000575
576 OS << "\n";
577 OS << "static const llvm::InstrItinerary ";
578 if (ItinList.empty()) {
579 OS << '*' << Name << " = 0;\n";
580 continue;
581 }
582
583 // Begin processor itinerary table
584 OS << Name << "[] = {\n";
585
586 // For each itinerary class in CodeGenSchedClass::Index order.
587 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
588 InstrItinerary &Intinerary = ItinList[j];
589
590 // Emit Itinerary in the form of
591 // { firstStage, lastStage, firstCycle, lastCycle } // index
592 OS << " { " <<
593 Intinerary.NumMicroOps << ", " <<
594 Intinerary.FirstStage << ", " <<
595 Intinerary.LastStage << ", " <<
596 Intinerary.FirstOperandCycle << ", " <<
597 Intinerary.LastOperandCycle << " }" <<
598 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
599 }
600 // End processor itinerary table
601 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
602 OS << "};\n";
603 }
604}
605
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000606// Emit either the value defined in the TableGen Record, or the default
Andrew Trick2661b412012-07-07 04:00:00 +0000607// value defined in the C++ header. The Record is null if the processor does not
608// define a model.
609void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trickfc992992012-06-05 03:44:40 +0000610 const char *Name, char Separator) {
611 OS << " ";
Andrew Trick2661b412012-07-07 04:00:00 +0000612 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trickfc992992012-06-05 03:44:40 +0000613 if (V >= 0)
614 OS << V << Separator << " // " << Name;
615 else
Andrew Trick2661b412012-07-07 04:00:00 +0000616 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000617 OS << '\n';
618}
619
Andrew Trick40096d22012-09-17 22:18:45 +0000620void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
621 raw_ostream &OS) {
622 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
623
624 OS << "\n// {Name, NumUnits, SuperIdx}\n";
625 OS << "static const llvm::MCProcResourceDesc "
626 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
627 << " {DBGFIELD(\"InvalidUnit\") 0, 0}" << Sep << "\n";
628
629 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
630 Record *PRDef = ProcModel.ProcResourceDefs[i];
631
632 // Find the SuperIdx
633 unsigned SuperIdx = 0;
634 Record *SuperDef = 0;
635 if (PRDef->getValueInit("Super")->isComplete()) {
636 SuperDef =
637 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"), ProcModel);
638 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
639 }
640 // Emit the ProcResourceDesc
641 if (i+1 == e)
642 Sep = ' ';
643 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
644 if (PRDef->getName().size() < 15)
645 OS.indent(15 - PRDef->getName().size());
646 OS << PRDef->getValueAsInt("NumUnits") << ", " << SuperIdx
647 << "}" << Sep << " // #" << i+1;
648 if (SuperDef)
649 OS << ", Super=" << SuperDef->getName();
650 OS << "\n";
651 }
652 OS << "};\n";
653}
654
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000655// Find the WriteRes Record that defines processor resources for this
656// SchedWrite.
657Record *SubtargetEmitter::FindWriteResources(
Andrew Trick92649882012-09-22 02:24:21 +0000658 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000659
660 // Check if the SchedWrite is already subtarget-specific and directly
661 // specifies a set of processor resources.
Andrew Trick92649882012-09-22 02:24:21 +0000662 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
663 return SchedWrite.TheDef;
664
665 // Check this processor's list of aliases for SchedWrite.
666 Record *AliasDef = 0;
667 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
668 AI != AE; ++AI) {
669 const CodeGenSchedRW &AliasRW =
670 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
671 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
672 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
673 continue;
674 if (AliasDef)
675 throw TGError(AliasRW.TheDef->getLoc(), "Multiple aliases "
676 "defined for processor " + ProcModel.ModelName +
677 " Ensure only one SchedAlias exists per RW.");
678 AliasDef = AliasRW.TheDef;
679 }
680 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
681 return AliasDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000682
683 // Check this processor's list of write resources.
Andrew Trick92649882012-09-22 02:24:21 +0000684 Record *ResDef = 0;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000685 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
686 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
687 if (!(*WRI)->isSubClassOf("WriteRes"))
688 continue;
Andrew Trick92649882012-09-22 02:24:21 +0000689 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
690 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
691 if (ResDef) {
692 throw TGError((*WRI)->getLoc(), "Resources are defined for both "
693 "SchedWrite and its alias on processor " +
694 ProcModel.ModelName);
695 }
696 ResDef = *WRI;
697 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000698 }
Andrew Trick92649882012-09-22 02:24:21 +0000699 // TODO: If ProcModel has a base model (previous generation processor),
700 // then call FindWriteResources recursively with that model here.
701 if (!ResDef) {
702 throw TGError(ProcModel.ModelDef->getLoc(),
703 std::string("Processor does not define resources for ")
704 + SchedWrite.TheDef->getName());
705 }
706 return ResDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000707}
708
709/// Find the ReadAdvance record for the given SchedRead on this processor or
710/// return NULL.
Andrew Trick92649882012-09-22 02:24:21 +0000711Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000712 const CodeGenProcModel &ProcModel) {
713 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick92649882012-09-22 02:24:21 +0000714 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
715 return SchedRead.TheDef;
716
717 // Check this processor's list of aliases for SchedRead.
718 Record *AliasDef = 0;
719 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
720 AI != AE; ++AI) {
721 const CodeGenSchedRW &AliasRW =
722 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
723 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
724 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
725 continue;
726 if (AliasDef)
727 throw TGError(AliasRW.TheDef->getLoc(), "Multiple aliases "
728 "defined for processor " + ProcModel.ModelName +
729 " Ensure only one SchedAlias exists per RW.");
730 AliasDef = AliasRW.TheDef;
731 }
732 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
733 return AliasDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000734
735 // Check this processor's ReadAdvanceList.
Andrew Trick92649882012-09-22 02:24:21 +0000736 Record *ResDef = 0;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000737 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
738 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
739 if (!(*RAI)->isSubClassOf("ReadAdvance"))
740 continue;
Andrew Trick92649882012-09-22 02:24:21 +0000741 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
742 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
743 if (ResDef) {
744 throw TGError((*RAI)->getLoc(), "Resources are defined for both "
745 "SchedRead and its alias on processor " +
746 ProcModel.ModelName);
747 }
748 ResDef = *RAI;
749 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000750 }
Andrew Trick92649882012-09-22 02:24:21 +0000751 // TODO: If ProcModel has a base model (previous generation processor),
752 // then call FindReadAdvance recursively with that model here.
753 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000754 throw TGError(ProcModel.ModelDef->getLoc(),
755 std::string("Processor does not define resources for ")
Andrew Trick92649882012-09-22 02:24:21 +0000756 + SchedRead.TheDef->getName());
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000757 }
Andrew Trick92649882012-09-22 02:24:21 +0000758 return ResDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000759}
760
761// Generate the SchedClass table for this processor and update global
762// tables. Must be called for each processor in order.
763void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
764 SchedClassTables &SchedTables) {
765 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
766 if (!ProcModel.hasInstrSchedModel())
767 return;
768
769 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
770 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
771 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
772 SCTab.resize(SCTab.size() + 1);
773 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Tricke127dfd2012-09-18 03:18:56 +0000774 // SCDesc.Name is guarded by NDEBUG
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000775 SCDesc.NumMicroOps = 0;
776 SCDesc.BeginGroup = false;
777 SCDesc.EndGroup = false;
778 SCDesc.WriteProcResIdx = 0;
779 SCDesc.WriteLatencyIdx = 0;
780 SCDesc.ReadAdvanceIdx = 0;
781
782 // A Variant SchedClass has no resources of its own.
783 if (!SCI->Transitions.empty()) {
784 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
785 continue;
786 }
787
788 // Determine if the SchedClass is actually reachable on this processor. If
789 // not don't try to locate the processor resources, it will fail.
790 // If ProcIndices contains 0, this class applies to all processors.
791 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
792 if (SCI->ProcIndices[0] != 0) {
793 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
794 SCI->ProcIndices.end(), ProcModel.Index);
795 if (PIPos == SCI->ProcIndices.end())
796 continue;
797 }
798 IdxVec Writes = SCI->Writes;
799 IdxVec Reads = SCI->Reads;
800 if (SCI->ItinClassDef) {
801 assert(SCI->InstRWs.empty() && "ItinClass should not have InstRWs");
802 // Check this processor's itinerary class resources.
803 for (RecIter II = ProcModel.ItinRWDefs.begin(),
804 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
805 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
806 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
807 != Matched.end()) {
808 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
809 Writes, Reads);
810 break;
811 }
812 }
813 if (Writes.empty()) {
814 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
815 << " does not have resources for itinerary class "
816 << SCI->ItinClassDef->getName() << '\n');
817 }
818 }
819 else if (!SCI->InstRWs.empty()) {
820 assert(SCI->Writes.empty() && SCI->Reads.empty() &&
821 "InstRW class should not have its own ReadWrites");
822 Record *RWDef = 0;
823 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
824 RWI != RWE; ++RWI) {
825 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
826 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
827 RWDef = *RWI;
828 break;
829 }
830 }
831 if (RWDef) {
832 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
833 Writes, Reads);
834 }
835 }
836 // Sum resources across all operand writes.
837 std::vector<MCWriteProcResEntry> WriteProcResources;
838 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000839 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000840 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
841 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
842 IdxVec WriteSeq;
843 SchedModels.expandRWSequence(*WI, WriteSeq, /*IsRead=*/false);
844
845 // For each operand, create a latency entry.
846 MCWriteLatencyEntry WLEntry;
847 WLEntry.Cycles = 0;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000848 unsigned WriteID = WriteSeq.back();
849 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
850 // If this Write is not referenced by a ReadAdvance, don't distinguish it
851 // from other WriteLatency entries.
852 if (!SchedModels.hasReadOfWrite(SchedModels.getSchedWrite(WriteID).TheDef)) {
853 WriteID = 0;
854 }
855 WLEntry.WriteResourceID = WriteID;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000856
857 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
858 WSI != WSE; ++WSI) {
859
Andrew Trick92649882012-09-22 02:24:21 +0000860 Record *WriteRes =
861 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000862
863 // Mark the parent class as invalid for unsupported write types.
864 if (WriteRes->getValueAsBit("Unsupported")) {
865 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
866 break;
867 }
868 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
869 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
870 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
871 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
872
873 // Create an entry for each ProcResource listed in WriteRes.
874 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
875 std::vector<int64_t> Cycles =
876 WriteRes->getValueAsListOfInts("ResourceCycles");
877 for (unsigned PRIdx = 0, PREnd = PRVec.size();
878 PRIdx != PREnd; ++PRIdx) {
879 MCWriteProcResEntry WPREntry;
880 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
881 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
882 if (Cycles.size() > PRIdx)
883 WPREntry.Cycles = Cycles[PRIdx];
884 else
885 WPREntry.Cycles = 1;
886 WriteProcResources.push_back(WPREntry);
887 }
888 }
889 WriteLatencies.push_back(WLEntry);
890 }
891 // Create an entry for each operand Read in this SchedClass.
892 // Entries must be sorted first by UseIdx then by WriteResourceID.
893 for (unsigned UseIdx = 0, EndIdx = Reads.size();
894 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick92649882012-09-22 02:24:21 +0000895 Record *ReadAdvance =
896 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000897 if (!ReadAdvance)
898 continue;
899
900 // Mark the parent class as invalid for unsupported write types.
901 if (ReadAdvance->getValueAsBit("Unsupported")) {
902 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
903 break;
904 }
905 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
906 IdxVec WriteIDs;
907 if (ValidWrites.empty())
908 WriteIDs.push_back(0);
909 else {
910 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
911 VWI != VWE; ++VWI) {
912 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
913 }
914 }
915 std::sort(WriteIDs.begin(), WriteIDs.end());
916 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
917 MCReadAdvanceEntry RAEntry;
918 RAEntry.UseIdx = UseIdx;
919 RAEntry.WriteResourceID = *WI;
920 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
921 ReadAdvanceEntries.push_back(RAEntry);
922 }
923 }
924 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
925 WriteProcResources.clear();
926 WriteLatencies.clear();
927 ReadAdvanceEntries.clear();
928 }
929 // Add the information for this SchedClass to the global tables using basic
930 // compression.
931 //
932 // WritePrecRes entries are sorted by ProcResIdx.
933 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
934 LessWriteProcResources());
935
936 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
937 std::vector<MCWriteProcResEntry>::iterator WPRPos =
938 std::search(SchedTables.WriteProcResources.begin(),
939 SchedTables.WriteProcResources.end(),
940 WriteProcResources.begin(), WriteProcResources.end());
941 if (WPRPos != SchedTables.WriteProcResources.end())
942 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
943 else {
944 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
945 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
946 WriteProcResources.end());
947 }
948 // Latency entries must remain in operand order.
949 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
950 std::vector<MCWriteLatencyEntry>::iterator WLPos =
951 std::search(SchedTables.WriteLatencies.begin(),
952 SchedTables.WriteLatencies.end(),
953 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trick3b8fb642012-09-19 04:43:19 +0000954 if (WLPos != SchedTables.WriteLatencies.end()) {
955 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
956 SCDesc.WriteLatencyIdx = idx;
957 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
958 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
959 std::string::npos) {
960 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
961 }
962 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000963 else {
964 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trick3b8fb642012-09-19 04:43:19 +0000965 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
966 WriteLatencies.begin(),
967 WriteLatencies.end());
968 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
969 WriterNames.begin(), WriterNames.end());
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000970 }
971 // ReadAdvanceEntries must remain in operand order.
972 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
973 std::vector<MCReadAdvanceEntry>::iterator RAPos =
974 std::search(SchedTables.ReadAdvanceEntries.begin(),
975 SchedTables.ReadAdvanceEntries.end(),
976 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
977 if (RAPos != SchedTables.ReadAdvanceEntries.end())
978 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
979 else {
980 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
981 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
982 ReadAdvanceEntries.end());
983 }
984 }
985}
986
Andrew Trick544c8802012-09-17 22:18:50 +0000987// Emit SchedClass tables for all processors and associated global tables.
988void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
989 raw_ostream &OS) {
990 // Emit global WriteProcResTable.
991 OS << "\n// {ProcResourceIdx, Cycles}\n"
992 << "extern const llvm::MCWriteProcResEntry "
993 << Target << "WriteProcResTable[] = {\n"
994 << " { 0, 0}, // Invalid\n";
995 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
996 WPRIdx != WPREnd; ++WPRIdx) {
997 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
998 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
999 << format("%2d", WPREntry.Cycles) << "}";
1000 if (WPRIdx + 1 < WPREnd)
1001 OS << ',';
1002 OS << " // #" << WPRIdx << '\n';
1003 }
1004 OS << "}; // " << Target << "WriteProcResTable\n";
1005
1006 // Emit global WriteLatencyTable.
1007 OS << "\n// {Cycles, WriteResourceID}\n"
1008 << "extern const llvm::MCWriteLatencyEntry "
1009 << Target << "WriteLatencyTable[] = {\n"
1010 << " { 0, 0}, // Invalid\n";
1011 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1012 WLIdx != WLEnd; ++WLIdx) {
1013 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1014 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1015 << format("%2d", WLEntry.WriteResourceID) << "}";
1016 if (WLIdx + 1 < WLEnd)
1017 OS << ',';
Andrew Trick3b8fb642012-09-19 04:43:19 +00001018 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Trick544c8802012-09-17 22:18:50 +00001019 }
1020 OS << "}; // " << Target << "WriteLatencyTable\n";
1021
1022 // Emit global ReadAdvanceTable.
1023 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1024 << "extern const llvm::MCReadAdvanceEntry "
1025 << Target << "ReadAdvanceTable[] = {\n"
1026 << " {0, 0, 0}, // Invalid\n";
1027 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1028 RAIdx != RAEnd; ++RAIdx) {
1029 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1030 OS << " {" << RAEntry.UseIdx << ", "
1031 << format("%2d", RAEntry.WriteResourceID) << ", "
1032 << format("%2d", RAEntry.Cycles) << "}";
1033 if (RAIdx + 1 < RAEnd)
1034 OS << ',';
1035 OS << " // #" << RAIdx << '\n';
1036 }
1037 OS << "}; // " << Target << "ReadAdvanceTable\n";
1038
1039 // Emit a SchedClass table for each processor.
1040 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1041 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1042 if (!PI->hasInstrSchedModel())
1043 continue;
1044
1045 std::vector<MCSchedClassDesc> &SCTab =
1046 SchedTables.ProcSchedClasses[1 + PI - SchedModels.procModelBegin()];
1047
1048 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1049 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1050 OS << "static const llvm::MCSchedClassDesc "
1051 << PI->ModelName << "SchedClasses[] = {\n";
1052
1053 // The first class is always invalid. We no way to distinguish it except by
1054 // name and position.
Andrew Tricke4095f92012-09-17 23:14:15 +00001055 assert(SchedModels.getSchedClass(0).Name == "NoItinerary"
Andrew Trick544c8802012-09-17 22:18:50 +00001056 && "invalid class not first");
1057 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1058 << MCSchedClassDesc::InvalidNumMicroOps
1059 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1060
1061 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1062 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1063 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1064 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1065 if (SchedClass.Name.size() < 18)
1066 OS.indent(18 - SchedClass.Name.size());
1067 OS << MCDesc.NumMicroOps
1068 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1069 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1070 << ", " << MCDesc.NumWriteProcResEntries
1071 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1072 << ", " << MCDesc.NumWriteLatencyEntries
1073 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1074 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1075 if (SCIdx + 1 < SCEnd)
1076 OS << ',';
1077 OS << " // #" << SCIdx << '\n';
1078 }
1079 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1080 }
1081}
1082
Andrew Trick2661b412012-07-07 04:00:00 +00001083void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1084 // For each processor model.
1085 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1086 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick40096d22012-09-17 22:18:45 +00001087 // Emit processor resource table.
1088 if (PI->hasInstrSchedModel())
1089 EmitProcessorResources(*PI, OS);
1090 else if(!PI->ProcResourceDefs.empty())
1091 throw TGError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001092 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick40096d22012-09-17 22:18:45 +00001093
Andrew Trickfc992992012-06-05 03:44:40 +00001094 // Begin processor itinerary properties
1095 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001096 OS << "static const llvm::MCSchedModel " << PI->ModelName << "(\n";
1097 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
1098 EmitProcessorProp(OS, PI->ModelDef, "MinLatency", ',');
1099 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1100 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trickd43b5c92012-08-08 02:44:16 +00001101 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Tricke127dfd2012-09-18 03:18:56 +00001102 OS << " " << PI->Index << ", // Processor ID\n";
1103 if (PI->hasInstrSchedModel())
1104 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1105 << " " << PI->ModelName << "SchedClasses" << ",\n"
1106 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1107 << " " << (SchedModels.schedClassEnd()
1108 - SchedModels.schedClassBegin()) << ",\n";
1109 else
1110 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001111 if (SchedModels.hasItineraryClasses())
Andrew Trick40096d22012-09-17 22:18:45 +00001112 OS << " " << PI->ItinsDef->getName() << ");\n";
Andrew Trickd85934b2012-06-22 03:58:51 +00001113 else
Andrew Trick40096d22012-09-17 22:18:45 +00001114 OS << " 0); // No Itinerary\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001115 }
Jim Laskey10b1dd92005-10-31 17:16:01 +00001116}
1117
1118//
1119// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1120//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001121void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +00001122 // Gather and sort processor information
1123 std::vector<Record*> ProcessorList =
1124 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +00001125 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +00001126
1127 // Begin processor table
1128 OS << "\n";
1129 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001130 << "extern const llvm::SubtargetInfoKV "
Andrew Trick2661b412012-07-07 04:00:00 +00001131 << Target << "ProcSchedKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +00001132
Jim Laskey10b1dd92005-10-31 17:16:01 +00001133 // For each processor
1134 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1135 // Next processor
1136 Record *Processor = ProcessorList[i];
1137
Bill Wendling4222d802007-05-04 20:38:40 +00001138 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick2661b412012-07-07 04:00:00 +00001139 const std::string &ProcModelName =
Andrew Trick48605c32012-09-15 00:19:57 +00001140 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickda96cf22011-04-01 01:56:55 +00001141
Jim Laskey10b1dd92005-10-31 17:16:01 +00001142 // Emit as { "cpu", procinit },
Andrew Trick40096d22012-09-17 22:18:45 +00001143 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +00001144
Jim Laskey10b1dd92005-10-31 17:16:01 +00001145 // Depending on ''if more in the list'' emit comma
1146 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +00001147
Jim Laskey10b1dd92005-10-31 17:16:01 +00001148 OS << "\n";
1149 }
Andrew Trickda96cf22011-04-01 01:56:55 +00001150
Jim Laskey10b1dd92005-10-31 17:16:01 +00001151 // End processor table
1152 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001153}
1154
1155//
Andrew Trick2661b412012-07-07 04:00:00 +00001156// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey0d841e02005-10-27 19:47:21 +00001157//
Andrew Trick2661b412012-07-07 04:00:00 +00001158void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick40096d22012-09-17 22:18:45 +00001159 OS << "#ifdef DBGFIELD\n"
1160 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1161 << "#endif\n"
1162 << "#ifndef NDEBUG\n"
1163 << "#define DBGFIELD(x) x,\n"
1164 << "#else\n"
1165 << "#define DBGFIELD(x)\n"
1166 << "#endif\n";
1167
Andrew Trick2661b412012-07-07 04:00:00 +00001168 if (SchedModels.hasItineraryClasses()) {
1169 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey6cee6302005-11-01 20:06:59 +00001170 // Emit the stage data
Andrew Trick2661b412012-07-07 04:00:00 +00001171 EmitStageAndOperandCycleData(OS, ProcItinLists);
1172 EmitItineraries(OS, ProcItinLists);
Jim Laskey6cee6302005-11-01 20:06:59 +00001173 }
Andrew Trick544c8802012-09-17 22:18:50 +00001174 OS << "\n// ===============================================================\n"
1175 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick40096d22012-09-17 22:18:45 +00001176
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001177 SchedClassTables SchedTables;
1178 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1179 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1180 GenSchedClassTables(*PI, SchedTables);
1181 }
Andrew Trick544c8802012-09-17 22:18:50 +00001182 EmitSchedClassTables(SchedTables, OS);
1183
1184 // Emit the processor machine model
1185 EmitProcessorModels(OS);
1186 // Emit the processor lookup data
1187 EmitProcessorLookup(OS);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001188
Andrew Trick40096d22012-09-17 22:18:45 +00001189 OS << "#undef DBGFIELD";
Jim Laskey0d841e02005-10-27 19:47:21 +00001190}
1191
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001192void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1193 raw_ostream &OS) {
1194 OS << "unsigned " << ClassName
1195 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1196 << " const TargetSchedModel *SchedModel) const {\n";
1197
1198 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1199 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1200 for (std::vector<Record*>::const_iterator
1201 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1202 OS << (*PI)->getValueAsString("Code") << '\n';
1203 }
1204 IdxVec VariantClasses;
1205 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1206 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1207 if (SCI->Transitions.empty())
1208 continue;
1209 VariantClasses.push_back(SCI - SchedModels.schedClassBegin());
1210 }
1211 if (!VariantClasses.empty()) {
1212 OS << " switch (SchedClass) {\n";
1213 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1214 VCI != VCE; ++VCI) {
1215 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1216 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1217 IdxVec ProcIndices;
1218 for (std::vector<CodeGenSchedTransition>::const_iterator
1219 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1220 TI != TE; ++TI) {
1221 IdxVec PI;
1222 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1223 ProcIndices.begin(), ProcIndices.end(),
1224 std::back_inserter(PI));
1225 ProcIndices.swap(PI);
1226 }
1227 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1228 PI != PE; ++PI) {
1229 OS << " ";
1230 if (*PI != 0)
1231 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1232 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1233 << '\n';
1234 for (std::vector<CodeGenSchedTransition>::const_iterator
1235 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1236 TI != TE; ++TI) {
1237 OS << " if (";
1238 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1239 TI->ProcIndices.end(), *PI)) {
1240 continue;
1241 }
1242 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1243 RI != RE; ++RI) {
1244 if (RI != TI->PredTerm.begin())
1245 OS << "\n && ";
1246 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1247 }
1248 OS << ")\n"
1249 << " return " << TI->ToClassIdx << "; // "
1250 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1251 }
1252 OS << " }\n";
1253 if (*PI == 0)
1254 break;
1255 }
1256 unsigned SCIdx = 0;
1257 if (SC.ItinClassDef)
1258 SCIdx = SchedModels.getSchedClassIdxForItin(SC.ItinClassDef);
1259 else
1260 SCIdx = SchedModels.findSchedClassIdx(SC.Writes, SC.Reads);
1261 if (SCIdx != *VCI)
1262 OS << " return " << SCIdx << ";\n";
1263 OS << " break;\n";
1264 }
1265 OS << " };\n";
1266 }
1267 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1268 << "} // " << ClassName << "::resolveSchedClass\n";
1269}
1270
Jim Laskey0d841e02005-10-27 19:47:21 +00001271//
Jim Laskey581a8f72005-10-26 17:30:34 +00001272// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1273// the subtarget features string.
1274//
Evan Cheng94214702011-07-01 20:45:01 +00001275void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1276 unsigned NumFeatures,
1277 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001278 std::vector<Record*> Features =
1279 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +00001280 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +00001281
Andrew Trickda96cf22011-04-01 01:56:55 +00001282 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1283 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +00001284 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +00001285 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001286 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +00001287 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel3f696e52012-06-12 04:21:36 +00001288 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +00001289
1290 if (Features.empty()) {
1291 OS << "}\n";
1292 return;
1293 }
1294
Andrew Trick34aadd62012-09-18 05:33:15 +00001295 OS << " InitMCProcessorInfo(CPU, FS);\n"
1296 << " uint64_t Bits = getFeatureBits();\n";
Bill Wendling4222d802007-05-04 20:38:40 +00001297
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001298 for (unsigned i = 0; i < Features.size(); i++) {
1299 // Next record
1300 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +00001301 const std::string &Instance = R->getName();
1302 const std::string &Value = R->getValueAsString("Value");
1303 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +00001304
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001305 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001306 OS << " if ((Bits & " << Target << "::"
1307 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001308 << Attribute << " = " << Value << ";\n";
1309 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001310 OS << " if ((Bits & " << Target << "::"
1311 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +00001312 << Attribute << " < " << Value << ") "
1313 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +00001314 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001315
Evan Cheng276365d2011-06-30 01:53:36 +00001316 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +00001317}
1318
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001319//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001320// SubtargetEmitter::run - Main subtarget enumeration emitter.
1321//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001322void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001323 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001324
Evan Chengebdeeab2011-07-08 01:53:10 +00001325 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1326 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1327
1328 OS << "namespace llvm {\n";
1329 Enumeration(OS, "SubtargetFeature", true);
1330 OS << "} // End llvm namespace \n";
1331 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1332
Evan Cheng94214702011-07-01 20:45:01 +00001333 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1334 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +00001335
Evan Cheng94214702011-07-01 20:45:01 +00001336 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001337#if 0
1338 OS << "namespace {\n";
1339#endif
Evan Cheng94214702011-07-01 20:45:01 +00001340 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001341 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +00001342 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001343 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001344 EmitSchedModel(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001345 OS << "\n";
1346#if 0
1347 OS << "}\n";
1348#endif
Evan Cheng94214702011-07-01 20:45:01 +00001349
1350 // MCInstrInfo initialization routine.
1351 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +00001352 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1353 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1354 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001355 if (NumFeatures)
1356 OS << Target << "FeatureKV, ";
1357 else
1358 OS << "0, ";
1359 if (NumProcs)
1360 OS << Target << "SubTypeKV, ";
1361 else
1362 OS << "0, ";
Andrew Trick544c8802012-09-17 22:18:50 +00001363 OS << '\n'; OS.indent(22);
Andrew Tricke127dfd2012-09-18 03:18:56 +00001364 OS << Target << "ProcSchedKV, "
1365 << Target << "WriteProcResTable, "
1366 << Target << "WriteLatencyTable, "
1367 << Target << "ReadAdvanceTable, ";
Andrew Trick2661b412012-07-07 04:00:00 +00001368 if (SchedModels.hasItineraryClasses()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001369 OS << '\n'; OS.indent(22);
1370 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001371 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001372 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001373 } else
Andrew Tricke127dfd2012-09-18 03:18:56 +00001374 OS << "0, 0, 0, ";
Evan Cheng94214702011-07-01 20:45:01 +00001375 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1376
1377 OS << "} // End llvm namespace \n";
1378
1379 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1380
1381 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1382 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1383
1384 OS << "#include \"llvm/Support/Debug.h\"\n";
1385 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
1386 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1387
1388 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1389
Evan Cheng5b1b44892011-07-01 21:01:15 +00001390 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +00001391 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1392 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1393
1394 std::string ClassName = Target + "GenSubtargetInfo";
1395 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001396 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +00001397 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001398 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1399 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001400 << "public:\n"
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001401 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
1402 << " const TargetSchedModel *SchedModel) const;\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +00001403 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001404 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +00001405 << "};\n";
1406 OS << "} // End llvm namespace \n";
1407
1408 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1409
1410 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1411 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1412
Andrew Trickee290ba2012-09-18 03:32:57 +00001413 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng94214702011-07-01 20:45:01 +00001414 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001415 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1416 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001417 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1418 OS << "extern const llvm::MCWriteProcResEntry "
1419 << Target << "WriteProcResTable[];\n";
1420 OS << "extern const llvm::MCWriteLatencyEntry "
1421 << Target << "WriteLatencyTable[];\n";
1422 OS << "extern const llvm::MCReadAdvanceEntry "
1423 << Target << "ReadAdvanceTable[];\n";
1424
Andrew Trick2661b412012-07-07 04:00:00 +00001425 if (SchedModels.hasItineraryClasses()) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001426 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1427 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Tricka11a6282012-07-07 03:59:48 +00001428 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001429 }
1430
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001431 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1432 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +00001433 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +00001434 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001435 if (NumFeatures)
1436 OS << Target << "FeatureKV, ";
1437 else
1438 OS << "0, ";
1439 if (NumProcs)
1440 OS << Target << "SubTypeKV, ";
1441 else
1442 OS << "0, ";
Andrew Tricke127dfd2012-09-18 03:18:56 +00001443 OS << '\n'; OS.indent(22);
1444 OS << Target << "ProcSchedKV, "
1445 << Target << "WriteProcResTable, "
1446 << Target << "WriteLatencyTable, "
1447 << Target << "ReadAdvanceTable, ";
1448 OS << '\n'; OS.indent(22);
Andrew Trick2661b412012-07-07 04:00:00 +00001449 if (SchedModels.hasItineraryClasses()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001450 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001451 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001452 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001453 } else
Andrew Tricke127dfd2012-09-18 03:18:56 +00001454 OS << "0, 0, 0, ";
Evan Cheng94214702011-07-01 20:45:01 +00001455 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001456
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001457 EmitSchedModelHelpers(ClassName, OS);
1458
Evan Cheng94214702011-07-01 20:45:01 +00001459 OS << "} // End llvm namespace \n";
1460
1461 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001462}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001463
1464namespace llvm {
1465
1466void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick2661b412012-07-07 04:00:00 +00001467 CodeGenTarget CGTarget(RK);
1468 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001469}
1470
1471} // End llvm namespace