blob: d056de003e189beb09be84380dafe2ea39add51e [file] [log] [blame]
Jim Laskeycfda85a2005-10-21 19:00:04 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Laskeycfda85a2005-10-21 19:00:04 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner73fbe142006-03-03 02:04:07 +000010// This tablegen backend emits subtarget enumerations.
Jim Laskeycfda85a2005-10-21 19:00:04 +000011//
12//===----------------------------------------------------------------------===//
13
Jim Laskeycfda85a2005-10-21 19:00:04 +000014#include "CodeGenTarget.h"
Andrew Trick87255e32012-07-07 04:00:00 +000015#include "CodeGenSchedule.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000017#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000018#include "llvm/MC/MCInstrItineraries.h"
Michael Kupersteindb0712f2015-05-26 10:47:10 +000019#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/Format.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000022#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000023#include "llvm/TableGen/Record.h"
24#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenb0aa47b2005-10-28 01:43:09 +000025#include <algorithm>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000026#include <map>
27#include <string>
28#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000029
Jim Laskeycfda85a2005-10-21 19:00:04 +000030using namespace llvm;
31
Chandler Carruth97acce22014-04-22 03:06:00 +000032#define DEBUG_TYPE "subtarget-emitter"
33
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000034namespace {
35class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000036 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
37 // The SchedClassDesc table indexes into a global write resource table, write
38 // latency table, and read advance table.
39 struct SchedClassTables {
40 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
41 std::vector<MCWriteProcResEntry> WriteProcResources;
42 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000043 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000044 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
45
46 // Reserve an invalid entry at index 0
47 SchedClassTables() {
48 ProcSchedClasses.resize(1);
49 WriteProcResources.resize(1);
50 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000051 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000052 ReadAdvanceEntries.resize(1);
53 }
54 };
55
56 struct LessWriteProcResources {
57 bool operator()(const MCWriteProcResEntry &LHS,
58 const MCWriteProcResEntry &RHS) {
59 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
60 }
61 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000062
63 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000064 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000065 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000066
Michael Kupersteindb0712f2015-05-26 10:47:10 +000067 void Enumeration(raw_ostream &OS, const char *ClassName);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000068 unsigned FeatureKeyValues(raw_ostream &OS);
69 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000070 void FormItineraryStageString(const std::string &Names,
71 Record *ItinData, std::string &ItinString,
72 unsigned &NStages);
73 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
74 unsigned &NOperandCycles);
75 void FormItineraryBypassString(const std::string &Names,
76 Record *ItinData,
77 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000078 void EmitStageAndOperandCycleData(raw_ostream &OS,
79 std::vector<std::vector<InstrItinerary> >
80 &ProcItinLists);
81 void EmitItineraries(raw_ostream &OS,
82 std::vector<std::vector<InstrItinerary> >
83 &ProcItinLists);
84 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000085 char Separator);
Andrew Trick23f3c652012-09-17 22:18:45 +000086 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
87 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +000088 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +000089 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +000090 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
91 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +000092 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
93 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +000094 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
95 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +000096 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +000097 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000098 void EmitProcessorLookup(raw_ostream &OS);
Andrew Trickc6c88152012-09-18 03:41:43 +000099 void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000100 void EmitSchedModel(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000101 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
102 unsigned NumProcs);
103
104public:
Andrew Trick87255e32012-07-07 04:00:00 +0000105 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
106 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000107
108 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000109};
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000110} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000111
Jim Laskeya1beea62005-10-22 07:59:56 +0000112//
Jim Laskeya2b52352005-10-26 17:30:34 +0000113// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000114//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000115void SubtargetEmitter::Enumeration(raw_ostream &OS,
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000116 const char *ClassName) {
Jim Laskey19595752005-10-28 15:20:43 +0000117 // Get all records of class and sort
Jim Laskeydffe5972005-10-28 21:47:29 +0000118 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
Duraid Madina018da4f2005-12-30 14:56:37 +0000119 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000120
Evan Chenga2e61292011-04-15 19:35:46 +0000121 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000122 if (N == 0)
123 return;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000124 if (N > MAX_SUBTARGET_FEATURES)
125 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chenga2e61292011-04-15 19:35:46 +0000126
Evan Cheng54b68e32011-07-01 20:45:01 +0000127 OS << "namespace " << Target << " {\n";
128
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000129 // Open enumeration. Use a 64-bit underlying type.
130 OS << "enum : uint64_t {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000131
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000132 // For each record
133 for (unsigned i = 0; i < N;) {
134 // Next record
135 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000136
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000137 // Get and emit name
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000138 OS << " " << Def->getName() << " = " << i;
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000139 if (++i < N) OS << ",";
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000140
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000141 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000142 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000143
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000144 // Close enumeration and namespace
145 OS << "};\n}\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000146}
147
148//
Bill Wendlinge6182262007-05-04 20:38:40 +0000149// FeatureKeyValues - Emit data of all the subtarget features. Used by the
150// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000151//
Evan Cheng54b68e32011-07-01 20:45:01 +0000152unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000153 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000154 std::vector<Record*> FeatureList =
155 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000156
157 if (FeatureList.empty())
158 return 0;
159
Jim Grosbach56938af2008-09-11 17:05:32 +0000160 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000161
Jim Laskey19595752005-10-28 15:20:43 +0000162 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000163 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000164 << "extern const llvm::SubtargetFeatureKV " << Target
165 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000166
Jim Laskey19595752005-10-28 15:20:43 +0000167 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000168 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000169 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000170 // Next feature
171 Record *Feature = FeatureList[i];
172
Bill Wendlinge6182262007-05-04 20:38:40 +0000173 const std::string &Name = Feature->getName();
174 const std::string &CommandLineName = Feature->getValueAsString("Name");
175 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000176
Jim Laskey3f7d0472006-12-12 20:55:58 +0000177 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000178
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000179 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000180 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000181 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000182 << "\"" << Desc << "\", "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000183 << "{ " << Target << "::" << Name << " }, ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000184
Andrew Trickdb6ed642011-04-01 01:56:55 +0000185 const std::vector<Record*> &ImpliesList =
Bill Wendlinge6182262007-05-04 20:38:40 +0000186 Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000187
Craig Topper4ceea0a2016-01-03 08:57:41 +0000188 OS << "{";
189 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
190 OS << " " << Target << "::" << ImpliesList[j]->getName();
191 if (++j < M) OS << ",";
Bill Wendlinge6182262007-05-04 20:38:40 +0000192 }
Craig Topper4ceea0a2016-01-03 08:57:41 +0000193 OS << " }";
Bill Wendlinge6182262007-05-04 20:38:40 +0000194
195 OS << " }";
Evan Cheng54b68e32011-07-01 20:45:01 +0000196 ++NumFeatures;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000197
Jim Laskey3763a502005-10-31 17:16:01 +0000198 // Depending on 'if more in the list' emit comma
Jim Laskey3f7d0472006-12-12 20:55:58 +0000199 if ((i + 1) < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000200
Jim Laskeydffe5972005-10-28 21:47:29 +0000201 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000202 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000203
Jim Laskey19595752005-10-28 15:20:43 +0000204 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000205 OS << "};\n";
206
Evan Cheng54b68e32011-07-01 20:45:01 +0000207 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000208}
209
210//
211// CPUKeyValues - Emit data of all the subtarget processors. Used by command
212// line.
213//
Evan Cheng54b68e32011-07-01 20:45:01 +0000214unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000215 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000216 std::vector<Record*> ProcessorList =
217 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000218 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000219
Jim Laskey19595752005-10-28 15:20:43 +0000220 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000221 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000222 << "extern const llvm::SubtargetFeatureKV " << Target
223 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000224
Jim Laskey19595752005-10-28 15:20:43 +0000225 // For each processor
Jim Laskeydffe5972005-10-28 21:47:29 +0000226 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
227 // Next processor
228 Record *Processor = ProcessorList[i];
229
Bill Wendlinge6182262007-05-04 20:38:40 +0000230 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000231 const std::vector<Record*> &FeatureList =
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000232 Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000233
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000234 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000235 OS << " { "
236 << "\"" << Name << "\", "
237 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000238
Craig Topper4ceea0a2016-01-03 08:57:41 +0000239 OS << "{";
240 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
241 OS << " " << Target << "::" << FeatureList[j]->getName();
242 if (++j < M) OS << ",";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000243 }
Craig Topper4ceea0a2016-01-03 08:57:41 +0000244 OS << " }";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000245
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000246 // The { } is for the "implies" section of this data structure.
247 OS << ", { } }";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000248
Jim Laskey3763a502005-10-31 17:16:01 +0000249 // Depending on 'if more in the list' emit comma
Jim Laskeydffe5972005-10-28 21:47:29 +0000250 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000251
Jim Laskeydffe5972005-10-28 21:47:29 +0000252 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000253 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000254
Jim Laskey19595752005-10-28 15:20:43 +0000255 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000256 OS << "};\n";
257
Evan Cheng54b68e32011-07-01 20:45:01 +0000258 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000259}
Jim Laskeya1beea62005-10-22 07:59:56 +0000260
Jim Laskeya2b52352005-10-26 17:30:34 +0000261//
David Goodwind813cbf2009-08-17 16:02:57 +0000262// FormItineraryStageString - Compose a string containing the stage
263// data initialization for the specified itinerary. N is the number
264// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000265//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000266void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
267 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000268 std::string &ItinString,
269 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000270 // Get states list
Bill Wendlinge6182262007-05-04 20:38:40 +0000271 const std::vector<Record*> &StageList =
272 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000273
274 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000275 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000276 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000277 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000278 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000279
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000280 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000281 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000282 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000283
Jim Laskeydffe5972005-10-28 21:47:29 +0000284 // Get unit list
Bill Wendlinge6182262007-05-04 20:38:40 +0000285 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000286
Jim Laskey19595752005-10-28 15:20:43 +0000287 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000288 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000289 // Add name and bitwise or
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000290 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeydffe5972005-10-28 21:47:29 +0000291 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000292 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000293
David Goodwinb369ee42009-08-12 18:31:53 +0000294 int TimeInc = Stage->getValueAsInt("TimeInc");
295 ItinString += ", " + itostr(TimeInc);
296
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000297 int Kind = Stage->getValueAsInt("Kind");
298 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
299
Jim Laskey19595752005-10-28 15:20:43 +0000300 // Close off stage
301 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000302 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000303 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000304}
305
306//
David Goodwind813cbf2009-08-17 16:02:57 +0000307// FormItineraryOperandCycleString - Compose a string containing the
308// operand cycle initialization for the specified itinerary. N is the
309// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000310//
David Goodwind813cbf2009-08-17 16:02:57 +0000311void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
312 std::string &ItinString, unsigned &NOperandCycles) {
313 // Get operand cycle list
314 const std::vector<int64_t> &OperandCycleList =
315 ItinData->getValueAsListOfInts("OperandCycles");
316
317 // For each operand cycle
318 unsigned N = NOperandCycles = OperandCycleList.size();
319 for (unsigned i = 0; i < N;) {
320 // Next operand cycle
321 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000322
David Goodwind813cbf2009-08-17 16:02:57 +0000323 ItinString += " " + itostr(OCycle);
324 if (++i < N) ItinString += ", ";
325 }
326}
327
Evan Cheng0097dd02010-09-28 23:50:49 +0000328void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
329 Record *ItinData,
330 std::string &ItinString,
331 unsigned NOperandCycles) {
332 const std::vector<Record*> &BypassList =
333 ItinData->getValueAsListOfDefs("Bypasses");
334 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000335 unsigned i = 0;
336 for (; i < N;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000337 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000338 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000339 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000340 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000341 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000342 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000343 }
344}
345
David Goodwind813cbf2009-08-17 16:02:57 +0000346//
Andrew Trick87255e32012-07-07 04:00:00 +0000347// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
348// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
349// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000350//
Andrew Trick87255e32012-07-07 04:00:00 +0000351void SubtargetEmitter::
352EmitStageAndOperandCycleData(raw_ostream &OS,
353 std::vector<std::vector<InstrItinerary> >
354 &ProcItinLists) {
Jim Laskey19595752005-10-28 15:20:43 +0000355
Andrew Trickfb982dd2012-07-09 20:43:03 +0000356 // Multiple processor models may share an itinerary record. Emit it once.
357 SmallPtrSet<Record*, 8> ItinsDefSet;
358
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000359 // Emit functional units for all the itineraries.
Andrew Trick87255e32012-07-07 04:00:00 +0000360 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
361 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000362
David Blaikie70573dc2014-11-19 07:49:26 +0000363 if (!ItinsDefSet.insert(PI->ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000364 continue;
365
Andrew Trick87255e32012-07-07 04:00:00 +0000366 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000367 if (FUs.empty())
368 continue;
369
Andrew Trick87255e32012-07-07 04:00:00 +0000370 const std::string &Name = PI->ItinsDef->getName();
371 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000372 << "namespace " << Name << "FU {\n";
373
374 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000375 OS << " const unsigned " << FUs[j]->getName()
376 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000377
378 OS << "}\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000379
Andrew Trick87255e32012-07-07 04:00:00 +0000380 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000381 if (!BPs.empty()) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000382 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
383 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000384
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000385 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000386 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000387 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000388 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000389
Evan Cheng4a010fd2010-09-29 22:42:35 +0000390 OS << "}\n";
391 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000392 }
393
Jim Laskey19595752005-10-28 15:20:43 +0000394 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000395 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
396 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000397 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000398
David Goodwind813cbf2009-08-17 16:02:57 +0000399 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000400 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000401 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000402 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000403
404 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000405 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000406 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000407 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000408
Andrew Trick87255e32012-07-07 04:00:00 +0000409 // For each Itinerary across all processors, add a unique entry to the stages,
410 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
411 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000412 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000413 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick87255e32012-07-07 04:00:00 +0000414 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
415 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
416 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000417
Andrew Trick87255e32012-07-07 04:00:00 +0000418 // Add process itinerary to the list.
419 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000420
Andrew Trick87255e32012-07-07 04:00:00 +0000421 // If this processor defines no itineraries, then leave the itinerary list
422 // empty.
423 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000424 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000425 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000426
Andrew Trick87255e32012-07-07 04:00:00 +0000427 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000428
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000429 ItinList.resize(SchedModels.numInstrSchedClasses());
430 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
431
432 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000433 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
434
Jim Laskeydffe5972005-10-28 21:47:29 +0000435 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000436 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000437
Jim Laskey19595752005-10-28 15:20:43 +0000438 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000439 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000440 unsigned NStages = 0;
441 if (ItinData)
442 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000443
David Goodwind813cbf2009-08-17 16:02:57 +0000444 // Get string and operand cycle count
445 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000446 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000447 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000448 if (ItinData) {
449 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
450 NOperandCycles);
451
452 FormItineraryBypassString(Name, ItinData, ItinBypassString,
453 NOperandCycles);
454 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000455
David Goodwind813cbf2009-08-17 16:02:57 +0000456 // Check to see if stage already exists and create if it doesn't
457 unsigned FindStage = 0;
458 if (NStages > 0) {
459 FindStage = ItinStageMap[ItinStageString];
460 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000461 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
462 StageTable += ItinStageString + ", // " + itostr(StageCount);
463 if (NStages > 1)
464 StageTable += "-" + itostr(StageCount + NStages - 1);
465 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000466 // Record Itin class number.
467 ItinStageMap[ItinStageString] = FindStage = StageCount;
468 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000469 }
470 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000471
David Goodwind813cbf2009-08-17 16:02:57 +0000472 // Check to see if operand cycle already exists and create if it doesn't
473 unsigned FindOperandCycle = 0;
474 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000475 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
476 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000477 if (FindOperandCycle == 0) {
478 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000479 OperandCycleTable += ItinOperandCycleString + ", // ";
480 std::string OperandIdxComment = itostr(OperandCycleCount);
481 if (NOperandCycles > 1)
482 OperandIdxComment += "-"
483 + itostr(OperandCycleCount + NOperandCycles - 1);
484 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000485 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000486 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000487 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000488 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000489 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000490 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000491 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000492 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000493
Evan Cheng367a5df2010-09-09 18:18:55 +0000494 // Set up itinerary as location and location + stage count
Andrew Trick87255e32012-07-07 04:00:00 +0000495 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng367a5df2010-09-09 18:18:55 +0000496 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
497 FindOperandCycle,
498 FindOperandCycle + NOperandCycles};
499
Jim Laskey19595752005-10-28 15:20:43 +0000500 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000501 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000502 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000503 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000504
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000505 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000506 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000507 StageTable += "};\n";
508
509 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000510 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000511 OperandCycleTable += "};\n";
512
Andrew Trick87255e32012-07-07 04:00:00 +0000513 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000514 BypassTable += "};\n";
515
David Goodwind813cbf2009-08-17 16:02:57 +0000516 // Emit tables.
517 OS << StageTable;
518 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000519 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000520}
521
Andrew Trick87255e32012-07-07 04:00:00 +0000522//
523// EmitProcessorData - Generate data for processor itineraries that were
524// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
525// Itineraries for each processor. The Itinerary lists are indexed on
526// CodeGenSchedClass::Index.
527//
528void SubtargetEmitter::
529EmitItineraries(raw_ostream &OS,
530 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
531
Andrew Trickfb982dd2012-07-09 20:43:03 +0000532 // Multiple processor models may share an itinerary record. Emit it once.
533 SmallPtrSet<Record*, 8> ItinsDefSet;
534
Andrew Trick87255e32012-07-07 04:00:00 +0000535 // For each processor's machine model
536 std::vector<std::vector<InstrItinerary> >::iterator
537 ProcItinListsIter = ProcItinLists.begin();
538 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000539 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000540
Andrew Trick87255e32012-07-07 04:00:00 +0000541 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000542 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000543 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000544
545 // Get processor itinerary name
546 const std::string &Name = ItinsDef->getName();
547
548 // Get the itinerary list for the processor.
549 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000550 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000551
Pete Cooperc0eb1532014-09-02 23:23:34 +0000552 // Empty itineraries aren't referenced anywhere in the tablegen output
553 // so don't emit them.
554 if (ItinList.empty())
555 continue;
556
Andrew Trick87255e32012-07-07 04:00:00 +0000557 OS << "\n";
558 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000559
560 // Begin processor itinerary table
561 OS << Name << "[] = {\n";
562
563 // For each itinerary class in CodeGenSchedClass::Index order.
564 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
565 InstrItinerary &Intinerary = ItinList[j];
566
567 // Emit Itinerary in the form of
568 // { firstStage, lastStage, firstCycle, lastCycle } // index
569 OS << " { " <<
570 Intinerary.NumMicroOps << ", " <<
571 Intinerary.FirstStage << ", " <<
572 Intinerary.LastStage << ", " <<
573 Intinerary.FirstOperandCycle << ", " <<
574 Intinerary.LastOperandCycle << " }" <<
575 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
576 }
577 // End processor itinerary table
578 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
579 OS << "};\n";
580 }
581}
582
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000583// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000584// value defined in the C++ header. The Record is null if the processor does not
585// define a model.
586void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trick73d77362012-06-05 03:44:40 +0000587 const char *Name, char Separator) {
588 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000589 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000590 if (V >= 0)
591 OS << V << Separator << " // " << Name;
592 else
Andrew Trick87255e32012-07-07 04:00:00 +0000593 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000594 OS << '\n';
595}
596
Andrew Trick23f3c652012-09-17 22:18:45 +0000597void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
598 raw_ostream &OS) {
599 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
600
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000601 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000602 OS << "static const llvm::MCProcResourceDesc "
603 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000604 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000605
606 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
607 Record *PRDef = ProcModel.ProcResourceDefs[i];
608
Craig Topper24064772014-04-15 07:20:03 +0000609 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000610 unsigned SuperIdx = 0;
611 unsigned NumUnits = 0;
Andrew Trick40c4f382013-06-15 04:50:06 +0000612 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000613 if (PRDef->isSubClassOf("ProcResGroup")) {
614 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
615 for (RecIter RUI = ResUnits.begin(), RUE = ResUnits.end();
616 RUI != RUE; ++RUI) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000617 NumUnits += (*RUI)->getValueAsInt("NumUnits");
618 }
619 }
620 else {
621 // Find the SuperIdx
622 if (PRDef->getValueInit("Super")->isComplete()) {
623 SuperDef = SchedModels.findProcResUnits(
624 PRDef->getValueAsDef("Super"), ProcModel);
625 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
626 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000627 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000628 }
629 // Emit the ProcResourceDesc
630 if (i+1 == e)
631 Sep = ' ';
632 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
633 if (PRDef->getName().size() < 15)
634 OS.indent(15 - PRDef->getName().size());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000635 OS << NumUnits << ", " << SuperIdx << ", "
Andrew Trickde2109e2013-06-15 04:49:57 +0000636 << BufferSize << "}" << Sep << " // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000637 if (SuperDef)
638 OS << ", Super=" << SuperDef->getName();
639 OS << "\n";
640 }
641 OS << "};\n";
642}
643
Andrew Trick9ef08822012-09-17 22:18:48 +0000644// Find the WriteRes Record that defines processor resources for this
645// SchedWrite.
646Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000647 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000648
649 // Check if the SchedWrite is already subtarget-specific and directly
650 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000651 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
652 return SchedWrite.TheDef;
653
Craig Topper24064772014-04-15 07:20:03 +0000654 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000655 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
656 AI != AE; ++AI) {
657 const CodeGenSchedRW &AliasRW =
658 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000659 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
660 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
661 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
662 continue;
663 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000664 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000665 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000666 "defined for processor " + ProcModel.ModelName +
667 " Ensure only one SchedAlias exists per RW.");
668 AliasDef = AliasRW.TheDef;
669 }
670 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
671 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000672
673 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000674 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000675 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
676 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
677 if (!(*WRI)->isSubClassOf("WriteRes"))
678 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000679 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
680 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
681 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000682 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000683 "SchedWrite and its alias on processor " +
684 ProcModel.ModelName);
685 }
686 ResDef = *WRI;
687 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000688 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000689 // TODO: If ProcModel has a base model (previous generation processor),
690 // then call FindWriteResources recursively with that model here.
691 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000692 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9257b8f2012-09-22 02:24:21 +0000693 std::string("Processor does not define resources for ")
694 + SchedWrite.TheDef->getName());
695 }
696 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000697}
698
699/// Find the ReadAdvance record for the given SchedRead on this processor or
700/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000701Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000702 const CodeGenProcModel &ProcModel) {
703 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000704 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
705 return SchedRead.TheDef;
706
707 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000708 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000709 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
710 AI != AE; ++AI) {
711 const CodeGenSchedRW &AliasRW =
712 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000713 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
714 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
715 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
716 continue;
717 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000718 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000719 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000720 "defined for processor " + ProcModel.ModelName +
721 " Ensure only one SchedAlias exists per RW.");
722 AliasDef = AliasRW.TheDef;
723 }
724 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
725 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000726
727 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000728 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000729 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
730 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
731 if (!(*RAI)->isSubClassOf("ReadAdvance"))
732 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000733 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
734 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
735 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000736 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000737 "SchedRead and its alias on processor " +
738 ProcModel.ModelName);
739 }
740 ResDef = *RAI;
741 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000742 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000743 // TODO: If ProcModel has a base model (previous generation processor),
744 // then call FindReadAdvance recursively with that model here.
745 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000746 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9ef08822012-09-17 22:18:48 +0000747 std::string("Processor does not define resources for ")
Andrew Trick9257b8f2012-09-22 02:24:21 +0000748 + SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000749 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000750 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000751}
752
Andrew Trick4e67cba2013-03-14 21:21:50 +0000753// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000754// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000755void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
756 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000757 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000758 // Default to 1 resource cycle.
759 Cycles.resize(PRVec.size(), 1);
760 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000761 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000762 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000763 if (PRDef->isSubClassOf("ProcResGroup"))
764 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000765 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000766 SubResources.push_back(PRDef);
767 PRDef = SchedModels.findProcResUnits(PRVec[i], PM);
768 for (Record *SubDef = PRDef;
769 SubDef->getValueInit("Super")->isComplete();) {
770 if (SubDef->isSubClassOf("ProcResGroup")) {
771 // Disallow this for simplicitly.
772 PrintFatalError(SubDef->getLoc(), "Processor resource group "
773 " cannot be a super resources.");
774 }
775 Record *SuperDef =
776 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM);
777 PRVec.push_back(SuperDef);
778 Cycles.push_back(Cycles[i]);
779 SubDef = SuperDef;
780 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000781 }
Andrew Tricka3801a32013-04-23 23:45:16 +0000782 for (RecIter PRI = PM.ProcResourceDefs.begin(),
783 PRE = PM.ProcResourceDefs.end();
Andrew Trick4e67cba2013-03-14 21:21:50 +0000784 PRI != PRE; ++PRI) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000785 if (*PRI == PRDef || !(*PRI)->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000786 continue;
787 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000788 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000789 for( ; SubI != SubE; ++SubI) {
790 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
791 == SuperResources.end()) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000792 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000793 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000794 }
795 if (SubI == SubE) {
796 PRVec.push_back(*PRI);
797 Cycles.push_back(Cycles[i]);
798 }
799 }
800 }
801}
802
Andrew Trick9ef08822012-09-17 22:18:48 +0000803// Generate the SchedClass table for this processor and update global
804// tables. Must be called for each processor in order.
805void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
806 SchedClassTables &SchedTables) {
807 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
808 if (!ProcModel.hasInstrSchedModel())
809 return;
810
811 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
812 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
813 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trick7aba6be2012-10-03 23:06:25 +0000814 DEBUG(SCI->dump(&SchedModels));
815
Andrew Trick9ef08822012-09-17 22:18:48 +0000816 SCTab.resize(SCTab.size() + 1);
817 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000818 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000819 SCDesc.NumMicroOps = 0;
820 SCDesc.BeginGroup = false;
821 SCDesc.EndGroup = false;
822 SCDesc.WriteProcResIdx = 0;
823 SCDesc.WriteLatencyIdx = 0;
824 SCDesc.ReadAdvanceIdx = 0;
825
826 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000827 bool HasVariants = false;
828 for (std::vector<CodeGenSchedTransition>::const_iterator
829 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
830 TI != TE; ++TI) {
831 if (TI->ProcIndices[0] == 0) {
832 HasVariants = true;
833 break;
834 }
835 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
836 TI->ProcIndices.end(), ProcModel.Index);
837 if (PIPos != TI->ProcIndices.end()) {
838 HasVariants = true;
839 break;
840 }
841 }
842 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000843 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
844 continue;
845 }
846
847 // Determine if the SchedClass is actually reachable on this processor. If
848 // not don't try to locate the processor resources, it will fail.
849 // If ProcIndices contains 0, this class applies to all processors.
850 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
851 if (SCI->ProcIndices[0] != 0) {
852 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
853 SCI->ProcIndices.end(), ProcModel.Index);
854 if (PIPos == SCI->ProcIndices.end())
855 continue;
856 }
857 IdxVec Writes = SCI->Writes;
858 IdxVec Reads = SCI->Reads;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000859 if (!SCI->InstRWs.empty()) {
860 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000861 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000862 Record *RWDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000863 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
864 RWI != RWE; ++RWI) {
865 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
866 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
867 RWDef = *RWI;
868 break;
869 }
870 }
871 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000872 Writes.clear();
873 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000874 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
875 Writes, Reads);
876 }
877 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000878 if (Writes.empty()) {
879 // Check this processor's itinerary class resources.
880 for (RecIter II = ProcModel.ItinRWDefs.begin(),
881 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
882 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
883 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
884 != Matched.end()) {
885 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
886 Writes, Reads);
887 break;
888 }
889 }
890 if (Writes.empty()) {
891 DEBUG(dbgs() << ProcModel.ModelName
892 << " does not have resources for class " << SCI->Name << '\n');
893 }
894 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000895 // Sum resources across all operand writes.
896 std::vector<MCWriteProcResEntry> WriteProcResources;
897 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000898 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000899 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
900 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
901 IdxVec WriteSeq;
Andrew Trickda984b12012-10-03 23:06:28 +0000902 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
903 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000904
905 // For each operand, create a latency entry.
906 MCWriteLatencyEntry WLEntry;
907 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000908 unsigned WriteID = WriteSeq.back();
909 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
910 // If this Write is not referenced by a ReadAdvance, don't distinguish it
911 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000912 if (!SchedModels.hasReadOfWrite(
913 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000914 WriteID = 0;
915 }
916 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000917
918 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
919 WSI != WSE; ++WSI) {
920
Andrew Trick9257b8f2012-09-22 02:24:21 +0000921 Record *WriteRes =
922 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000923
924 // Mark the parent class as invalid for unsupported write types.
925 if (WriteRes->getValueAsBit("Unsupported")) {
926 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
927 break;
928 }
929 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
930 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
931 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
932 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
933
934 // Create an entry for each ProcResource listed in WriteRes.
935 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
936 std::vector<int64_t> Cycles =
937 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000938
939 ExpandProcResources(PRVec, Cycles, ProcModel);
940
Andrew Trick9ef08822012-09-17 22:18:48 +0000941 for (unsigned PRIdx = 0, PREnd = PRVec.size();
942 PRIdx != PREnd; ++PRIdx) {
943 MCWriteProcResEntry WPREntry;
944 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
945 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000946 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000947 // If this resource is already used in this sequence, add the current
948 // entry's cycles so that the same resource appears to be used
949 // serially, rather than multiple parallel uses. This is important for
950 // in-order machine where the resource consumption is a hazard.
951 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
952 for( ; WPRIdx != WPREnd; ++WPRIdx) {
953 if (WriteProcResources[WPRIdx].ProcResourceIdx
954 == WPREntry.ProcResourceIdx) {
955 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
956 break;
957 }
958 }
959 if (WPRIdx == WPREnd)
960 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000961 }
962 }
963 WriteLatencies.push_back(WLEntry);
964 }
965 // Create an entry for each operand Read in this SchedClass.
966 // Entries must be sorted first by UseIdx then by WriteResourceID.
967 for (unsigned UseIdx = 0, EndIdx = Reads.size();
968 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000969 Record *ReadAdvance =
970 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000971 if (!ReadAdvance)
972 continue;
973
974 // Mark the parent class as invalid for unsupported write types.
975 if (ReadAdvance->getValueAsBit("Unsupported")) {
976 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
977 break;
978 }
979 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
980 IdxVec WriteIDs;
981 if (ValidWrites.empty())
982 WriteIDs.push_back(0);
983 else {
984 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
985 VWI != VWE; ++VWI) {
986 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
987 }
988 }
989 std::sort(WriteIDs.begin(), WriteIDs.end());
990 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
991 MCReadAdvanceEntry RAEntry;
992 RAEntry.UseIdx = UseIdx;
993 RAEntry.WriteResourceID = *WI;
994 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
995 ReadAdvanceEntries.push_back(RAEntry);
996 }
997 }
998 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
999 WriteProcResources.clear();
1000 WriteLatencies.clear();
1001 ReadAdvanceEntries.clear();
1002 }
1003 // Add the information for this SchedClass to the global tables using basic
1004 // compression.
1005 //
1006 // WritePrecRes entries are sorted by ProcResIdx.
1007 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1008 LessWriteProcResources());
1009
1010 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1011 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1012 std::search(SchedTables.WriteProcResources.begin(),
1013 SchedTables.WriteProcResources.end(),
1014 WriteProcResources.begin(), WriteProcResources.end());
1015 if (WPRPos != SchedTables.WriteProcResources.end())
1016 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1017 else {
1018 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1019 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1020 WriteProcResources.end());
1021 }
1022 // Latency entries must remain in operand order.
1023 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1024 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1025 std::search(SchedTables.WriteLatencies.begin(),
1026 SchedTables.WriteLatencies.end(),
1027 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001028 if (WLPos != SchedTables.WriteLatencies.end()) {
1029 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1030 SCDesc.WriteLatencyIdx = idx;
1031 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1032 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1033 std::string::npos) {
1034 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1035 }
1036 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001037 else {
1038 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001039 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1040 WriteLatencies.begin(),
1041 WriteLatencies.end());
1042 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1043 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001044 }
1045 // ReadAdvanceEntries must remain in operand order.
1046 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1047 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1048 std::search(SchedTables.ReadAdvanceEntries.begin(),
1049 SchedTables.ReadAdvanceEntries.end(),
1050 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1051 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1052 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1053 else {
1054 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1055 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1056 ReadAdvanceEntries.end());
1057 }
1058 }
1059}
1060
Andrew Tricka72fca62012-09-17 22:18:50 +00001061// Emit SchedClass tables for all processors and associated global tables.
1062void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1063 raw_ostream &OS) {
1064 // Emit global WriteProcResTable.
1065 OS << "\n// {ProcResourceIdx, Cycles}\n"
1066 << "extern const llvm::MCWriteProcResEntry "
1067 << Target << "WriteProcResTable[] = {\n"
1068 << " { 0, 0}, // Invalid\n";
1069 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1070 WPRIdx != WPREnd; ++WPRIdx) {
1071 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1072 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1073 << format("%2d", WPREntry.Cycles) << "}";
1074 if (WPRIdx + 1 < WPREnd)
1075 OS << ',';
1076 OS << " // #" << WPRIdx << '\n';
1077 }
1078 OS << "}; // " << Target << "WriteProcResTable\n";
1079
1080 // Emit global WriteLatencyTable.
1081 OS << "\n// {Cycles, WriteResourceID}\n"
1082 << "extern const llvm::MCWriteLatencyEntry "
1083 << Target << "WriteLatencyTable[] = {\n"
1084 << " { 0, 0}, // Invalid\n";
1085 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1086 WLIdx != WLEnd; ++WLIdx) {
1087 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1088 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1089 << format("%2d", WLEntry.WriteResourceID) << "}";
1090 if (WLIdx + 1 < WLEnd)
1091 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001092 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001093 }
1094 OS << "}; // " << Target << "WriteLatencyTable\n";
1095
1096 // Emit global ReadAdvanceTable.
1097 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1098 << "extern const llvm::MCReadAdvanceEntry "
1099 << Target << "ReadAdvanceTable[] = {\n"
1100 << " {0, 0, 0}, // Invalid\n";
1101 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1102 RAIdx != RAEnd; ++RAIdx) {
1103 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1104 OS << " {" << RAEntry.UseIdx << ", "
1105 << format("%2d", RAEntry.WriteResourceID) << ", "
1106 << format("%2d", RAEntry.Cycles) << "}";
1107 if (RAIdx + 1 < RAEnd)
1108 OS << ',';
1109 OS << " // #" << RAIdx << '\n';
1110 }
1111 OS << "}; // " << Target << "ReadAdvanceTable\n";
1112
1113 // Emit a SchedClass table for each processor.
1114 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1115 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1116 if (!PI->hasInstrSchedModel())
1117 continue;
1118
1119 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001120 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001121
1122 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1123 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1124 OS << "static const llvm::MCSchedClassDesc "
1125 << PI->ModelName << "SchedClasses[] = {\n";
1126
1127 // The first class is always invalid. We no way to distinguish it except by
1128 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001129 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001130 && "invalid class not first");
1131 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1132 << MCSchedClassDesc::InvalidNumMicroOps
1133 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1134
1135 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1136 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1137 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1138 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1139 if (SchedClass.Name.size() < 18)
1140 OS.indent(18 - SchedClass.Name.size());
1141 OS << MCDesc.NumMicroOps
1142 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1143 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1144 << ", " << MCDesc.NumWriteProcResEntries
1145 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1146 << ", " << MCDesc.NumWriteLatencyEntries
1147 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1148 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1149 if (SCIdx + 1 < SCEnd)
1150 OS << ',';
1151 OS << " // #" << SCIdx << '\n';
1152 }
1153 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1154 }
1155}
1156
Andrew Trick87255e32012-07-07 04:00:00 +00001157void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1158 // For each processor model.
1159 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1160 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001161 // Emit processor resource table.
1162 if (PI->hasInstrSchedModel())
1163 EmitProcessorResources(*PI, OS);
1164 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001165 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001166 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001167
Andrew Trick73d77362012-06-05 03:44:40 +00001168 // Begin processor itinerary properties
1169 OS << "\n";
Pete Cooper11759452014-09-02 17:43:54 +00001170 OS << "static const llvm::MCSchedModel " << PI->ModelName << " = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001171 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
Andrew Trickde2109e2013-06-15 04:49:57 +00001172 EmitProcessorProp(OS, PI->ModelDef, "MicroOpBufferSize", ',');
Hal Finkel6532c202014-05-08 09:14:44 +00001173 EmitProcessorProp(OS, PI->ModelDef, "LoopMicroOpBufferSize", ',');
Andrew Trick87255e32012-07-07 04:00:00 +00001174 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1175 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick352abc12012-08-08 02:44:16 +00001176 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001177
1178 OS << " " << (bool)(PI->ModelDef ?
Sanjay Patela2f658d2014-07-15 22:39:58 +00001179 PI->ModelDef->getValueAsBit("PostRAScheduler") : 0)
1180 << ", // " << "PostRAScheduler\n";
1181
1182 OS << " " << (bool)(PI->ModelDef ?
Andrew Trickb6854d82013-09-25 18:14:12 +00001183 PI->ModelDef->getValueAsBit("CompleteModel") : 0)
1184 << ", // " << "CompleteModel\n";
1185
Andrew Trickab722bd2012-09-18 03:18:56 +00001186 OS << " " << PI->Index << ", // Processor ID\n";
1187 if (PI->hasInstrSchedModel())
1188 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1189 << " " << PI->ModelName << "SchedClasses" << ",\n"
1190 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1191 << " " << (SchedModels.schedClassEnd()
1192 - SchedModels.schedClassBegin()) << ",\n";
1193 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001194 OS << " nullptr, nullptr, 0, 0,"
1195 << " // No instruction-level machine model.\n";
Pete Cooper11759452014-09-02 17:43:54 +00001196 if (PI->hasItineraries())
1197 OS << " " << PI->ItinsDef->getName() << "};\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001198 else
Pete Cooper11759452014-09-02 17:43:54 +00001199 OS << " nullptr}; // No Itinerary\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001200 }
Jim Laskey3763a502005-10-31 17:16:01 +00001201}
1202
1203//
1204// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1205//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001206void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001207 // Gather and sort processor information
1208 std::vector<Record*> ProcessorList =
1209 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001210 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001211
1212 // Begin processor table
1213 OS << "\n";
1214 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001215 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001216 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001217
Jim Laskey3763a502005-10-31 17:16:01 +00001218 // For each processor
1219 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1220 // Next processor
1221 Record *Processor = ProcessorList[i];
1222
Bill Wendlinge6182262007-05-04 20:38:40 +00001223 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001224 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001225 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001226
Jim Laskey3763a502005-10-31 17:16:01 +00001227 // Emit as { "cpu", procinit },
Andrew Trick23f3c652012-09-17 22:18:45 +00001228 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001229
Jim Laskey3763a502005-10-31 17:16:01 +00001230 // Depending on ''if more in the list'' emit comma
1231 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001232
Jim Laskey3763a502005-10-31 17:16:01 +00001233 OS << "\n";
1234 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001235
Jim Laskey3763a502005-10-31 17:16:01 +00001236 // End processor table
1237 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001238}
1239
1240//
Andrew Trick87255e32012-07-07 04:00:00 +00001241// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001242//
Andrew Trick87255e32012-07-07 04:00:00 +00001243void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001244 OS << "#ifdef DBGFIELD\n"
1245 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1246 << "#endif\n"
1247 << "#ifndef NDEBUG\n"
1248 << "#define DBGFIELD(x) x,\n"
1249 << "#else\n"
1250 << "#define DBGFIELD(x)\n"
1251 << "#endif\n";
1252
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001253 if (SchedModels.hasItineraries()) {
Andrew Trick87255e32012-07-07 04:00:00 +00001254 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001255 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001256 EmitStageAndOperandCycleData(OS, ProcItinLists);
1257 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001258 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001259 OS << "\n// ===============================================================\n"
1260 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001261
Andrew Trick9ef08822012-09-17 22:18:48 +00001262 SchedClassTables SchedTables;
1263 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1264 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1265 GenSchedClassTables(*PI, SchedTables);
1266 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001267 EmitSchedClassTables(SchedTables, OS);
1268
1269 // Emit the processor machine model
1270 EmitProcessorModels(OS);
1271 // Emit the processor lookup data
1272 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001273
Andrew Trick23f3c652012-09-17 22:18:45 +00001274 OS << "#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001275}
1276
Andrew Trickc6c88152012-09-18 03:41:43 +00001277void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1278 raw_ostream &OS) {
1279 OS << "unsigned " << ClassName
1280 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1281 << " const TargetSchedModel *SchedModel) const {\n";
1282
1283 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1284 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1285 for (std::vector<Record*>::const_iterator
1286 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1287 OS << (*PI)->getValueAsString("Code") << '\n';
1288 }
1289 IdxVec VariantClasses;
1290 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1291 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1292 if (SCI->Transitions.empty())
1293 continue;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001294 VariantClasses.push_back(SCI->Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001295 }
1296 if (!VariantClasses.empty()) {
1297 OS << " switch (SchedClass) {\n";
1298 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1299 VCI != VCE; ++VCI) {
1300 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1301 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1302 IdxVec ProcIndices;
1303 for (std::vector<CodeGenSchedTransition>::const_iterator
1304 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1305 TI != TE; ++TI) {
1306 IdxVec PI;
1307 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1308 ProcIndices.begin(), ProcIndices.end(),
1309 std::back_inserter(PI));
1310 ProcIndices.swap(PI);
1311 }
1312 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1313 PI != PE; ++PI) {
1314 OS << " ";
1315 if (*PI != 0)
1316 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1317 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1318 << '\n';
1319 for (std::vector<CodeGenSchedTransition>::const_iterator
1320 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1321 TI != TE; ++TI) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001322 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1323 TI->ProcIndices.end(), *PI)) {
1324 continue;
1325 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001326 OS << " if (";
Andrew Trickc6c88152012-09-18 03:41:43 +00001327 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1328 RI != RE; ++RI) {
1329 if (RI != TI->PredTerm.begin())
1330 OS << "\n && ";
1331 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1332 }
1333 OS << ")\n"
1334 << " return " << TI->ToClassIdx << "; // "
1335 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1336 }
1337 OS << " }\n";
1338 if (*PI == 0)
1339 break;
1340 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001341 if (SC.isInferred())
1342 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001343 OS << " break;\n";
1344 }
1345 OS << " };\n";
1346 }
1347 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1348 << "} // " << ClassName << "::resolveSchedClass\n";
1349}
1350
Jim Laskey86f002c2005-10-27 19:47:21 +00001351//
Jim Laskeya2b52352005-10-26 17:30:34 +00001352// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1353// the subtarget features string.
1354//
Evan Cheng54b68e32011-07-01 20:45:01 +00001355void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1356 unsigned NumFeatures,
1357 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001358 std::vector<Record*> Features =
1359 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001360 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001361
Andrew Trickdb6ed642011-04-01 01:56:55 +00001362 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1363 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001364 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001365 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001366 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001367 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001368 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001369
1370 if (Features.empty()) {
1371 OS << "}\n";
1372 return;
1373 }
1374
Andrew Trickba7b9212012-09-18 05:33:15 +00001375 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001376 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001377
Jim Laskeydffe5972005-10-28 21:47:29 +00001378 for (unsigned i = 0; i < Features.size(); i++) {
1379 // Next record
1380 Record *R = Features[i];
Bill Wendlinge6182262007-05-04 20:38:40 +00001381 const std::string &Instance = R->getName();
1382 const std::string &Value = R->getValueAsString("Value");
1383 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001384
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001385 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001386 OS << " if (Bits[" << Target << "::"
1387 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001388 << Attribute << " = " << Value << ";\n";
1389 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001390 OS << " if (Bits[" << Target << "::"
1391 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001392 << Attribute << " < " << Value << ") "
1393 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001394 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001395
Evan Chengfe6e4052011-06-30 01:53:36 +00001396 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001397}
1398
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001399//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001400// SubtargetEmitter::run - Main subtarget enumeration emitter.
1401//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001402void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001403 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001404
Evan Cheng4d1ca962011-07-08 01:53:10 +00001405 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1406 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1407
1408 OS << "namespace llvm {\n";
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001409 Enumeration(OS, "SubtargetFeature");
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001410 OS << "} // end llvm namespace\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001411 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1412
Evan Cheng54b68e32011-07-01 20:45:01 +00001413 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1414 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001415
Evan Cheng54b68e32011-07-01 20:45:01 +00001416 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001417#if 0
1418 OS << "namespace {\n";
1419#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001420 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001421 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001422 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001423 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001424 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001425 OS << "\n";
1426#if 0
1427 OS << "}\n";
1428#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001429
1430 // MCInstrInfo initialization routine.
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001431 OS << "static inline MCSubtargetInfo *create" << Target
1432 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001433 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001434 OS << " return new MCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001435 if (NumFeatures)
1436 OS << Target << "FeatureKV, ";
1437 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001438 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001439 if (NumProcs)
1440 OS << Target << "SubTypeKV, ";
1441 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001442 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001443 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001444 OS << Target << "ProcSchedKV, "
1445 << Target << "WriteProcResTable, "
1446 << Target << "WriteLatencyTable, "
1447 << Target << "ReadAdvanceTable, ";
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001448 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001449 OS << '\n'; OS.indent(22);
1450 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001451 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001452 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001453 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001454 OS << "0, 0, 0";
1455 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001456
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001457 OS << "} // end llvm namespace\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001458
1459 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1460
1461 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1462 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1463
1464 OS << "#include \"llvm/Support/Debug.h\"\n";
Benjamin Kramerb85d3752015-03-23 18:45:56 +00001465 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001466 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1467
1468 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1469
Evan Cheng0d639a22011-07-01 21:01:15 +00001470 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001471 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1472 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1473
1474 std::string ClassName = Target + "GenSubtargetInfo";
1475 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001476 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001477 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001478 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001479 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001480 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001481 << " unsigned resolveSchedClass(unsigned SchedClass, "
1482 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001483 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001484 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001485 << " const;\n"
Evan Cheng54b68e32011-07-01 20:45:01 +00001486 << "};\n";
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001487 OS << "} // end llvm namespace\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001488
1489 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1490
1491 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1492 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1493
Andrew Trick1188e432012-09-18 03:32:57 +00001494 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001495 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001496 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1497 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001498 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1499 OS << "extern const llvm::MCWriteProcResEntry "
1500 << Target << "WriteProcResTable[];\n";
1501 OS << "extern const llvm::MCWriteLatencyEntry "
1502 << Target << "WriteLatencyTable[];\n";
1503 OS << "extern const llvm::MCReadAdvanceEntry "
1504 << Target << "ReadAdvanceTable[];\n";
1505
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001506 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001507 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1508 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001509 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001510 }
1511
Daniel Sanders50f17232015-09-15 16:17:27 +00001512 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1513 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001514 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001515 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001516 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001517 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001518 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001519 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001520 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001521 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001522 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001523 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001524 OS << Target << "ProcSchedKV, "
1525 << Target << "WriteProcResTable, "
1526 << Target << "WriteLatencyTable, "
1527 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001528 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001529 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001530 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001531 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001532 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001533 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001534 OS << "0, 0, 0";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001535 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001536
Andrew Trickc6c88152012-09-18 03:41:43 +00001537 EmitSchedModelHelpers(ClassName, OS);
1538
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001539 OS << "} // end llvm namespace\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001540
1541 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001542}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001543
1544namespace llvm {
1545
1546void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001547 CodeGenTarget CGTarget(RK);
1548 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001549}
1550
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001551} // end llvm namespace