blob: d57ecfb27e0c079a5e02f89be6c14600f787cf62 [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 Kupersteinba5b04c2015-02-19 09:01:04 +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>
Jim Laskeycfda85a2005-10-21 19:00:04 +000029using namespace llvm;
30
Chandler Carruth97acce22014-04-22 03:06:00 +000031#define DEBUG_TYPE "subtarget-emitter"
32
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000033namespace {
34class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000035 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
36 // The SchedClassDesc table indexes into a global write resource table, write
37 // latency table, and read advance table.
38 struct SchedClassTables {
39 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
40 std::vector<MCWriteProcResEntry> WriteProcResources;
41 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000042 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000043 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
44
45 // Reserve an invalid entry at index 0
46 SchedClassTables() {
47 ProcSchedClasses.resize(1);
48 WriteProcResources.resize(1);
49 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000050 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000051 ReadAdvanceEntries.resize(1);
52 }
53 };
54
55 struct LessWriteProcResources {
56 bool operator()(const MCWriteProcResEntry &LHS,
57 const MCWriteProcResEntry &RHS) {
58 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
59 }
60 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000061
62 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000063 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000064 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000065
Michael Kupersteinba5b04c2015-02-19 09:01:04 +000066 void Enumeration(raw_ostream &OS, const char *ClassName);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000067 unsigned FeatureKeyValues(raw_ostream &OS);
68 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000069 void FormItineraryStageString(const std::string &Names,
70 Record *ItinData, std::string &ItinString,
71 unsigned &NStages);
72 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
73 unsigned &NOperandCycles);
74 void FormItineraryBypassString(const std::string &Names,
75 Record *ItinData,
76 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000077 void EmitStageAndOperandCycleData(raw_ostream &OS,
78 std::vector<std::vector<InstrItinerary> >
79 &ProcItinLists);
80 void EmitItineraries(raw_ostream &OS,
81 std::vector<std::vector<InstrItinerary> >
82 &ProcItinLists);
83 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000084 char Separator);
Andrew Trick23f3c652012-09-17 22:18:45 +000085 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
86 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +000087 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +000088 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +000089 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
90 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +000091 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
92 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +000093 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
94 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +000095 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +000096 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000097 void EmitProcessorLookup(raw_ostream &OS);
Andrew Trickc6c88152012-09-18 03:41:43 +000098 void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +000099 void EmitSchedModel(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000100 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
101 unsigned NumProcs);
102
103public:
Andrew Trick87255e32012-07-07 04:00:00 +0000104 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
105 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000106
107 void run(raw_ostream &o);
108
109};
110} // End anonymous namespace
111
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 Kupersteinba5b04c2015-02-19 09:01:04 +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 Kupersteinba5b04c2015-02-19 09:01:04 +0000124 if (N > MAX_SUBTARGET_FEATURES) {
125 errs() << "Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.";
Evan Chenga2e61292011-04-15 19:35:46 +0000126 exit(1);
127 }
128
Evan Cheng54b68e32011-07-01 20:45:01 +0000129 OS << "namespace " << Target << " {\n";
130
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000131 // Open enumeration
132 OS << "enum {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000133
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000134 // For each record
135 for (unsigned i = 0; i < N;) {
136 // Next record
137 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000138
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000139 // Get and emit name
140 OS << " " << Def->getName() << " = " << i;
141 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000142
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000143 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000144 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000145
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000146 // Close enumeration and namespace
147 OS << "};\n}\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000148}
149
150//
Bill Wendlinge6182262007-05-04 20:38:40 +0000151// FeatureKeyValues - Emit data of all the subtarget features. Used by the
152// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000153//
Evan Cheng54b68e32011-07-01 20:45:01 +0000154unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000155 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000156 std::vector<Record*> FeatureList =
157 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000158
159 if (FeatureList.empty())
160 return 0;
161
Jim Grosbach56938af2008-09-11 17:05:32 +0000162 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000163
Jim Laskey19595752005-10-28 15:20:43 +0000164 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000165 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000166 << "extern const llvm::SubtargetFeatureKV " << Target
167 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000168
Jim Laskey19595752005-10-28 15:20:43 +0000169 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000170 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000171 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000172 // Next feature
173 Record *Feature = FeatureList[i];
174
Bill Wendlinge6182262007-05-04 20:38:40 +0000175 const std::string &Name = Feature->getName();
176 const std::string &CommandLineName = Feature->getValueAsString("Name");
177 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000178
Jim Laskey3f7d0472006-12-12 20:55:58 +0000179 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000180
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000181 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000182 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000183 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000184 << "\"" << Desc << "\", "
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000185 << "{ " << Target << "::" << Name << " }, ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000186
Andrew Trickdb6ed642011-04-01 01:56:55 +0000187 const std::vector<Record*> &ImpliesList =
Bill Wendlinge6182262007-05-04 20:38:40 +0000188 Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000189
Bill Wendlinge6182262007-05-04 20:38:40 +0000190 if (ImpliesList.empty()) {
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000191 OS << "{ }";
Bill Wendlinge6182262007-05-04 20:38:40 +0000192 } else {
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000193 OS << "{ ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000194 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000195 OS << Target << "::" << ImpliesList[j]->getName();
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000196 if (++j < M) OS << ", ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000197 }
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000198 OS << " }";
Bill Wendlinge6182262007-05-04 20:38:40 +0000199 }
200
201 OS << " }";
Evan Cheng54b68e32011-07-01 20:45:01 +0000202 ++NumFeatures;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000203
Jim Laskey3763a502005-10-31 17:16:01 +0000204 // Depending on 'if more in the list' emit comma
Jim Laskey3f7d0472006-12-12 20:55:58 +0000205 if ((i + 1) < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000206
Jim Laskeydffe5972005-10-28 21:47:29 +0000207 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000208 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000209
Jim Laskey19595752005-10-28 15:20:43 +0000210 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000211 OS << "};\n";
212
Evan Cheng54b68e32011-07-01 20:45:01 +0000213 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000214}
215
216//
217// CPUKeyValues - Emit data of all the subtarget processors. Used by command
218// line.
219//
Evan Cheng54b68e32011-07-01 20:45:01 +0000220unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000221 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000222 std::vector<Record*> ProcessorList =
223 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000224 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000225
Jim Laskey19595752005-10-28 15:20:43 +0000226 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000227 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000228 << "extern const llvm::SubtargetFeatureKV " << Target
229 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000230
Jim Laskey19595752005-10-28 15:20:43 +0000231 // For each processor
Jim Laskeydffe5972005-10-28 21:47:29 +0000232 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
233 // Next processor
234 Record *Processor = ProcessorList[i];
235
Bill Wendlinge6182262007-05-04 20:38:40 +0000236 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000237 const std::vector<Record*> &FeatureList =
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000238 Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000239
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000240 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000241 OS << " { "
242 << "\"" << Name << "\", "
243 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000244
Jim Laskeydffe5972005-10-28 21:47:29 +0000245 if (FeatureList.empty()) {
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000246 OS << "{ }";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000247 } else {
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000248 OS << "{ ";
Jim Laskeydffe5972005-10-28 21:47:29 +0000249 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000250 OS << Target << "::" << FeatureList[j]->getName();
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000251 if (++j < M) OS << ", ";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000252 }
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000253 OS << " }";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000254 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000255
Michael Kupersteinba5b04c2015-02-19 09:01:04 +0000256 // The { } is for the "implies" section of this data structure.
257 OS << ", { } }";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000258
Jim Laskey3763a502005-10-31 17:16:01 +0000259 // Depending on 'if more in the list' emit comma
Jim Laskeydffe5972005-10-28 21:47:29 +0000260 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000261
Jim Laskeydffe5972005-10-28 21:47:29 +0000262 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000263 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000264
Jim Laskey19595752005-10-28 15:20:43 +0000265 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000266 OS << "};\n";
267
Evan Cheng54b68e32011-07-01 20:45:01 +0000268 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000269}
Jim Laskeya1beea62005-10-22 07:59:56 +0000270
Jim Laskeya2b52352005-10-26 17:30:34 +0000271//
David Goodwind813cbf2009-08-17 16:02:57 +0000272// FormItineraryStageString - Compose a string containing the stage
273// data initialization for the specified itinerary. N is the number
274// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000275//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000276void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
277 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000278 std::string &ItinString,
279 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000280 // Get states list
Bill Wendlinge6182262007-05-04 20:38:40 +0000281 const std::vector<Record*> &StageList =
282 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000283
284 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000285 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000286 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000287 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000288 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000289
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000290 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000291 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000292 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000293
Jim Laskeydffe5972005-10-28 21:47:29 +0000294 // Get unit list
Bill Wendlinge6182262007-05-04 20:38:40 +0000295 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000296
Jim Laskey19595752005-10-28 15:20:43 +0000297 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000298 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000299 // Add name and bitwise or
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000300 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeydffe5972005-10-28 21:47:29 +0000301 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000302 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000303
David Goodwinb369ee42009-08-12 18:31:53 +0000304 int TimeInc = Stage->getValueAsInt("TimeInc");
305 ItinString += ", " + itostr(TimeInc);
306
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000307 int Kind = Stage->getValueAsInt("Kind");
308 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
309
Jim Laskey19595752005-10-28 15:20:43 +0000310 // Close off stage
311 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000312 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000313 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000314}
315
316//
David Goodwind813cbf2009-08-17 16:02:57 +0000317// FormItineraryOperandCycleString - Compose a string containing the
318// operand cycle initialization for the specified itinerary. N is the
319// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000320//
David Goodwind813cbf2009-08-17 16:02:57 +0000321void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
322 std::string &ItinString, unsigned &NOperandCycles) {
323 // Get operand cycle list
324 const std::vector<int64_t> &OperandCycleList =
325 ItinData->getValueAsListOfInts("OperandCycles");
326
327 // For each operand cycle
328 unsigned N = NOperandCycles = OperandCycleList.size();
329 for (unsigned i = 0; i < N;) {
330 // Next operand cycle
331 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000332
David Goodwind813cbf2009-08-17 16:02:57 +0000333 ItinString += " " + itostr(OCycle);
334 if (++i < N) ItinString += ", ";
335 }
336}
337
Evan Cheng0097dd02010-09-28 23:50:49 +0000338void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
339 Record *ItinData,
340 std::string &ItinString,
341 unsigned NOperandCycles) {
342 const std::vector<Record*> &BypassList =
343 ItinData->getValueAsListOfDefs("Bypasses");
344 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000345 unsigned i = 0;
346 for (; i < N;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000347 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000348 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000349 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000350 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000351 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000352 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000353 }
354}
355
David Goodwind813cbf2009-08-17 16:02:57 +0000356//
Andrew Trick87255e32012-07-07 04:00:00 +0000357// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
358// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
359// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000360//
Andrew Trick87255e32012-07-07 04:00:00 +0000361void SubtargetEmitter::
362EmitStageAndOperandCycleData(raw_ostream &OS,
363 std::vector<std::vector<InstrItinerary> >
364 &ProcItinLists) {
Jim Laskey19595752005-10-28 15:20:43 +0000365
Andrew Trickfb982dd2012-07-09 20:43:03 +0000366 // Multiple processor models may share an itinerary record. Emit it once.
367 SmallPtrSet<Record*, 8> ItinsDefSet;
368
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000369 // Emit functional units for all the itineraries.
Andrew Trick87255e32012-07-07 04:00:00 +0000370 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
371 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000372
David Blaikie70573dc2014-11-19 07:49:26 +0000373 if (!ItinsDefSet.insert(PI->ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000374 continue;
375
Andrew Trick87255e32012-07-07 04:00:00 +0000376 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000377 if (FUs.empty())
378 continue;
379
Andrew Trick87255e32012-07-07 04:00:00 +0000380 const std::string &Name = PI->ItinsDef->getName();
381 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000382 << "namespace " << Name << "FU {\n";
383
384 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000385 OS << " const unsigned " << FUs[j]->getName()
386 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000387
388 OS << "}\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000389
Andrew Trick87255e32012-07-07 04:00:00 +0000390 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000391 if (!BPs.empty()) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000392 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
393 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000394
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000395 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000396 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000397 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000398 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000399
Evan Cheng4a010fd2010-09-29 22:42:35 +0000400 OS << "}\n";
401 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000402 }
403
Jim Laskey19595752005-10-28 15:20:43 +0000404 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000405 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
406 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000407 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000408
David Goodwind813cbf2009-08-17 16:02:57 +0000409 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000410 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000411 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000412 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000413
414 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000415 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000416 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000417 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000418
Andrew Trick87255e32012-07-07 04:00:00 +0000419 // For each Itinerary across all processors, add a unique entry to the stages,
420 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
421 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000422 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000423 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick87255e32012-07-07 04:00:00 +0000424 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
425 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
426 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000427
Andrew Trick87255e32012-07-07 04:00:00 +0000428 // Add process itinerary to the list.
429 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000430
Andrew Trick87255e32012-07-07 04:00:00 +0000431 // If this processor defines no itineraries, then leave the itinerary list
432 // empty.
433 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000434 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000435 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000436
Andrew Trick87255e32012-07-07 04:00:00 +0000437 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000438
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000439 ItinList.resize(SchedModels.numInstrSchedClasses());
440 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
441
442 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000443 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
444
Jim Laskeydffe5972005-10-28 21:47:29 +0000445 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000446 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000447
Jim Laskey19595752005-10-28 15:20:43 +0000448 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000449 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000450 unsigned NStages = 0;
451 if (ItinData)
452 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000453
David Goodwind813cbf2009-08-17 16:02:57 +0000454 // Get string and operand cycle count
455 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000456 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000457 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000458 if (ItinData) {
459 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
460 NOperandCycles);
461
462 FormItineraryBypassString(Name, ItinData, ItinBypassString,
463 NOperandCycles);
464 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000465
David Goodwind813cbf2009-08-17 16:02:57 +0000466 // Check to see if stage already exists and create if it doesn't
467 unsigned FindStage = 0;
468 if (NStages > 0) {
469 FindStage = ItinStageMap[ItinStageString];
470 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000471 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
472 StageTable += ItinStageString + ", // " + itostr(StageCount);
473 if (NStages > 1)
474 StageTable += "-" + itostr(StageCount + NStages - 1);
475 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000476 // Record Itin class number.
477 ItinStageMap[ItinStageString] = FindStage = StageCount;
478 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000479 }
480 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000481
David Goodwind813cbf2009-08-17 16:02:57 +0000482 // Check to see if operand cycle already exists and create if it doesn't
483 unsigned FindOperandCycle = 0;
484 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000485 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
486 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000487 if (FindOperandCycle == 0) {
488 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000489 OperandCycleTable += ItinOperandCycleString + ", // ";
490 std::string OperandIdxComment = itostr(OperandCycleCount);
491 if (NOperandCycles > 1)
492 OperandIdxComment += "-"
493 + itostr(OperandCycleCount + NOperandCycles - 1);
494 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000495 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000496 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000497 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000498 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000499 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000500 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000501 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000502 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000503
Evan Cheng367a5df2010-09-09 18:18:55 +0000504 // Set up itinerary as location and location + stage count
Andrew Trick87255e32012-07-07 04:00:00 +0000505 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng367a5df2010-09-09 18:18:55 +0000506 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
507 FindOperandCycle,
508 FindOperandCycle + NOperandCycles};
509
Jim Laskey19595752005-10-28 15:20:43 +0000510 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000511 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000512 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000513 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000514
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000515 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000516 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000517 StageTable += "};\n";
518
519 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000520 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000521 OperandCycleTable += "};\n";
522
Andrew Trick87255e32012-07-07 04:00:00 +0000523 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000524 BypassTable += "};\n";
525
David Goodwind813cbf2009-08-17 16:02:57 +0000526 // Emit tables.
527 OS << StageTable;
528 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000529 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000530}
531
Andrew Trick87255e32012-07-07 04:00:00 +0000532//
533// EmitProcessorData - Generate data for processor itineraries that were
534// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
535// Itineraries for each processor. The Itinerary lists are indexed on
536// CodeGenSchedClass::Index.
537//
538void SubtargetEmitter::
539EmitItineraries(raw_ostream &OS,
540 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
541
Andrew Trickfb982dd2012-07-09 20:43:03 +0000542 // Multiple processor models may share an itinerary record. Emit it once.
543 SmallPtrSet<Record*, 8> ItinsDefSet;
544
Andrew Trick87255e32012-07-07 04:00:00 +0000545 // For each processor's machine model
546 std::vector<std::vector<InstrItinerary> >::iterator
547 ProcItinListsIter = ProcItinLists.begin();
548 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000549 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000550
Andrew Trick87255e32012-07-07 04:00:00 +0000551 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000552 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000553 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000554
555 // Get processor itinerary name
556 const std::string &Name = ItinsDef->getName();
557
558 // Get the itinerary list for the processor.
559 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000560 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000561
Pete Cooperc0eb1532014-09-02 23:23:34 +0000562 // Empty itineraries aren't referenced anywhere in the tablegen output
563 // so don't emit them.
564 if (ItinList.empty())
565 continue;
566
Andrew Trick87255e32012-07-07 04:00:00 +0000567 OS << "\n";
568 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000569
570 // Begin processor itinerary table
571 OS << Name << "[] = {\n";
572
573 // For each itinerary class in CodeGenSchedClass::Index order.
574 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
575 InstrItinerary &Intinerary = ItinList[j];
576
577 // Emit Itinerary in the form of
578 // { firstStage, lastStage, firstCycle, lastCycle } // index
579 OS << " { " <<
580 Intinerary.NumMicroOps << ", " <<
581 Intinerary.FirstStage << ", " <<
582 Intinerary.LastStage << ", " <<
583 Intinerary.FirstOperandCycle << ", " <<
584 Intinerary.LastOperandCycle << " }" <<
585 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
586 }
587 // End processor itinerary table
588 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
589 OS << "};\n";
590 }
591}
592
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000593// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000594// value defined in the C++ header. The Record is null if the processor does not
595// define a model.
596void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trick73d77362012-06-05 03:44:40 +0000597 const char *Name, char Separator) {
598 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000599 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000600 if (V >= 0)
601 OS << V << Separator << " // " << Name;
602 else
Andrew Trick87255e32012-07-07 04:00:00 +0000603 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000604 OS << '\n';
605}
606
Andrew Trick23f3c652012-09-17 22:18:45 +0000607void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
608 raw_ostream &OS) {
609 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
610
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000611 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000612 OS << "static const llvm::MCProcResourceDesc "
613 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000614 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000615
616 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
617 Record *PRDef = ProcModel.ProcResourceDefs[i];
618
Craig Topper24064772014-04-15 07:20:03 +0000619 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000620 unsigned SuperIdx = 0;
621 unsigned NumUnits = 0;
Andrew Trick40c4f382013-06-15 04:50:06 +0000622 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000623 if (PRDef->isSubClassOf("ProcResGroup")) {
624 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
625 for (RecIter RUI = ResUnits.begin(), RUE = ResUnits.end();
626 RUI != RUE; ++RUI) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000627 NumUnits += (*RUI)->getValueAsInt("NumUnits");
628 }
629 }
630 else {
631 // Find the SuperIdx
632 if (PRDef->getValueInit("Super")->isComplete()) {
633 SuperDef = SchedModels.findProcResUnits(
634 PRDef->getValueAsDef("Super"), ProcModel);
635 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
636 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000637 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000638 }
639 // Emit the ProcResourceDesc
640 if (i+1 == e)
641 Sep = ' ';
642 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
643 if (PRDef->getName().size() < 15)
644 OS.indent(15 - PRDef->getName().size());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000645 OS << NumUnits << ", " << SuperIdx << ", "
Andrew Trickde2109e2013-06-15 04:49:57 +0000646 << BufferSize << "}" << Sep << " // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000647 if (SuperDef)
648 OS << ", Super=" << SuperDef->getName();
649 OS << "\n";
650 }
651 OS << "};\n";
652}
653
Andrew Trick9ef08822012-09-17 22:18:48 +0000654// Find the WriteRes Record that defines processor resources for this
655// SchedWrite.
656Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000657 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000658
659 // Check if the SchedWrite is already subtarget-specific and directly
660 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000661 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
662 return SchedWrite.TheDef;
663
Craig Topper24064772014-04-15 07:20:03 +0000664 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000665 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
666 AI != AE; ++AI) {
667 const CodeGenSchedRW &AliasRW =
668 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000669 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
670 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
671 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
672 continue;
673 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000674 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000675 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000676 "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 Trick9ef08822012-09-17 22:18:48 +0000682
683 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000684 Record *ResDef = nullptr;
Andrew Trick9ef08822012-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 Trick9257b8f2012-09-22 02:24:21 +0000689 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
690 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
691 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000692 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000693 "SchedWrite and its alias on processor " +
694 ProcModel.ModelName);
695 }
696 ResDef = *WRI;
697 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000698 }
Andrew Trick9257b8f2012-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) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000702 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9257b8f2012-09-22 02:24:21 +0000703 std::string("Processor does not define resources for ")
704 + SchedWrite.TheDef->getName());
705 }
706 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000707}
708
709/// Find the ReadAdvance record for the given SchedRead on this processor or
710/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000711Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000712 const CodeGenProcModel &ProcModel) {
713 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-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.
Craig Topper24064772014-04-15 07:20:03 +0000718 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000719 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
720 AI != AE; ++AI) {
721 const CodeGenSchedRW &AliasRW =
722 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000723 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
724 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
725 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
726 continue;
727 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000728 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000729 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000730 "defined for processor " + ProcModel.ModelName +
731 " Ensure only one SchedAlias exists per RW.");
732 AliasDef = AliasRW.TheDef;
733 }
734 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
735 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000736
737 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000738 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000739 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
740 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
741 if (!(*RAI)->isSubClassOf("ReadAdvance"))
742 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000743 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
744 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
745 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000746 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000747 "SchedRead and its alias on processor " +
748 ProcModel.ModelName);
749 }
750 ResDef = *RAI;
751 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000752 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000753 // TODO: If ProcModel has a base model (previous generation processor),
754 // then call FindReadAdvance recursively with that model here.
755 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000756 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9ef08822012-09-17 22:18:48 +0000757 std::string("Processor does not define resources for ")
Andrew Trick9257b8f2012-09-22 02:24:21 +0000758 + SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000759 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000760 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000761}
762
Andrew Trick4e67cba2013-03-14 21:21:50 +0000763// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000764// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000765void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
766 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000767 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000768 // Default to 1 resource cycle.
769 Cycles.resize(PRVec.size(), 1);
770 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000771 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000772 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000773 if (PRDef->isSubClassOf("ProcResGroup"))
774 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000775 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000776 SubResources.push_back(PRDef);
777 PRDef = SchedModels.findProcResUnits(PRVec[i], PM);
778 for (Record *SubDef = PRDef;
779 SubDef->getValueInit("Super")->isComplete();) {
780 if (SubDef->isSubClassOf("ProcResGroup")) {
781 // Disallow this for simplicitly.
782 PrintFatalError(SubDef->getLoc(), "Processor resource group "
783 " cannot be a super resources.");
784 }
785 Record *SuperDef =
786 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM);
787 PRVec.push_back(SuperDef);
788 Cycles.push_back(Cycles[i]);
789 SubDef = SuperDef;
790 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000791 }
Andrew Tricka3801a32013-04-23 23:45:16 +0000792 for (RecIter PRI = PM.ProcResourceDefs.begin(),
793 PRE = PM.ProcResourceDefs.end();
Andrew Trick4e67cba2013-03-14 21:21:50 +0000794 PRI != PRE; ++PRI) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000795 if (*PRI == PRDef || !(*PRI)->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000796 continue;
797 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000798 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000799 for( ; SubI != SubE; ++SubI) {
800 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
801 == SuperResources.end()) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000802 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000803 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000804 }
805 if (SubI == SubE) {
806 PRVec.push_back(*PRI);
807 Cycles.push_back(Cycles[i]);
808 }
809 }
810 }
811}
812
Andrew Trick9ef08822012-09-17 22:18:48 +0000813// Generate the SchedClass table for this processor and update global
814// tables. Must be called for each processor in order.
815void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
816 SchedClassTables &SchedTables) {
817 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
818 if (!ProcModel.hasInstrSchedModel())
819 return;
820
821 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
822 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
823 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trick7aba6be2012-10-03 23:06:25 +0000824 DEBUG(SCI->dump(&SchedModels));
825
Andrew Trick9ef08822012-09-17 22:18:48 +0000826 SCTab.resize(SCTab.size() + 1);
827 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000828 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000829 SCDesc.NumMicroOps = 0;
830 SCDesc.BeginGroup = false;
831 SCDesc.EndGroup = false;
832 SCDesc.WriteProcResIdx = 0;
833 SCDesc.WriteLatencyIdx = 0;
834 SCDesc.ReadAdvanceIdx = 0;
835
836 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000837 bool HasVariants = false;
838 for (std::vector<CodeGenSchedTransition>::const_iterator
839 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
840 TI != TE; ++TI) {
841 if (TI->ProcIndices[0] == 0) {
842 HasVariants = true;
843 break;
844 }
845 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
846 TI->ProcIndices.end(), ProcModel.Index);
847 if (PIPos != TI->ProcIndices.end()) {
848 HasVariants = true;
849 break;
850 }
851 }
852 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000853 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
854 continue;
855 }
856
857 // Determine if the SchedClass is actually reachable on this processor. If
858 // not don't try to locate the processor resources, it will fail.
859 // If ProcIndices contains 0, this class applies to all processors.
860 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
861 if (SCI->ProcIndices[0] != 0) {
862 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
863 SCI->ProcIndices.end(), ProcModel.Index);
864 if (PIPos == SCI->ProcIndices.end())
865 continue;
866 }
867 IdxVec Writes = SCI->Writes;
868 IdxVec Reads = SCI->Reads;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000869 if (!SCI->InstRWs.empty()) {
870 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000871 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000872 Record *RWDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000873 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
874 RWI != RWE; ++RWI) {
875 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
876 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
877 RWDef = *RWI;
878 break;
879 }
880 }
881 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000882 Writes.clear();
883 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000884 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
885 Writes, Reads);
886 }
887 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000888 if (Writes.empty()) {
889 // Check this processor's itinerary class resources.
890 for (RecIter II = ProcModel.ItinRWDefs.begin(),
891 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
892 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
893 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
894 != Matched.end()) {
895 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
896 Writes, Reads);
897 break;
898 }
899 }
900 if (Writes.empty()) {
901 DEBUG(dbgs() << ProcModel.ModelName
902 << " does not have resources for class " << SCI->Name << '\n');
903 }
904 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000905 // Sum resources across all operand writes.
906 std::vector<MCWriteProcResEntry> WriteProcResources;
907 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000908 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000909 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
910 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
911 IdxVec WriteSeq;
Andrew Trickda984b12012-10-03 23:06:28 +0000912 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
913 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000914
915 // For each operand, create a latency entry.
916 MCWriteLatencyEntry WLEntry;
917 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000918 unsigned WriteID = WriteSeq.back();
919 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
920 // If this Write is not referenced by a ReadAdvance, don't distinguish it
921 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000922 if (!SchedModels.hasReadOfWrite(
923 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000924 WriteID = 0;
925 }
926 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000927
928 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
929 WSI != WSE; ++WSI) {
930
Andrew Trick9257b8f2012-09-22 02:24:21 +0000931 Record *WriteRes =
932 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000933
934 // Mark the parent class as invalid for unsupported write types.
935 if (WriteRes->getValueAsBit("Unsupported")) {
936 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
937 break;
938 }
939 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
940 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
941 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
942 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
943
944 // Create an entry for each ProcResource listed in WriteRes.
945 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
946 std::vector<int64_t> Cycles =
947 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000948
949 ExpandProcResources(PRVec, Cycles, ProcModel);
950
Andrew Trick9ef08822012-09-17 22:18:48 +0000951 for (unsigned PRIdx = 0, PREnd = PRVec.size();
952 PRIdx != PREnd; ++PRIdx) {
953 MCWriteProcResEntry WPREntry;
954 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
955 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000956 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000957 // If this resource is already used in this sequence, add the current
958 // entry's cycles so that the same resource appears to be used
959 // serially, rather than multiple parallel uses. This is important for
960 // in-order machine where the resource consumption is a hazard.
961 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
962 for( ; WPRIdx != WPREnd; ++WPRIdx) {
963 if (WriteProcResources[WPRIdx].ProcResourceIdx
964 == WPREntry.ProcResourceIdx) {
965 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
966 break;
967 }
968 }
969 if (WPRIdx == WPREnd)
970 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000971 }
972 }
973 WriteLatencies.push_back(WLEntry);
974 }
975 // Create an entry for each operand Read in this SchedClass.
976 // Entries must be sorted first by UseIdx then by WriteResourceID.
977 for (unsigned UseIdx = 0, EndIdx = Reads.size();
978 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000979 Record *ReadAdvance =
980 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000981 if (!ReadAdvance)
982 continue;
983
984 // Mark the parent class as invalid for unsupported write types.
985 if (ReadAdvance->getValueAsBit("Unsupported")) {
986 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
987 break;
988 }
989 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
990 IdxVec WriteIDs;
991 if (ValidWrites.empty())
992 WriteIDs.push_back(0);
993 else {
994 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
995 VWI != VWE; ++VWI) {
996 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
997 }
998 }
999 std::sort(WriteIDs.begin(), WriteIDs.end());
1000 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
1001 MCReadAdvanceEntry RAEntry;
1002 RAEntry.UseIdx = UseIdx;
1003 RAEntry.WriteResourceID = *WI;
1004 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1005 ReadAdvanceEntries.push_back(RAEntry);
1006 }
1007 }
1008 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1009 WriteProcResources.clear();
1010 WriteLatencies.clear();
1011 ReadAdvanceEntries.clear();
1012 }
1013 // Add the information for this SchedClass to the global tables using basic
1014 // compression.
1015 //
1016 // WritePrecRes entries are sorted by ProcResIdx.
1017 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1018 LessWriteProcResources());
1019
1020 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1021 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1022 std::search(SchedTables.WriteProcResources.begin(),
1023 SchedTables.WriteProcResources.end(),
1024 WriteProcResources.begin(), WriteProcResources.end());
1025 if (WPRPos != SchedTables.WriteProcResources.end())
1026 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1027 else {
1028 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1029 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1030 WriteProcResources.end());
1031 }
1032 // Latency entries must remain in operand order.
1033 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1034 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1035 std::search(SchedTables.WriteLatencies.begin(),
1036 SchedTables.WriteLatencies.end(),
1037 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001038 if (WLPos != SchedTables.WriteLatencies.end()) {
1039 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1040 SCDesc.WriteLatencyIdx = idx;
1041 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1042 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1043 std::string::npos) {
1044 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1045 }
1046 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001047 else {
1048 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001049 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1050 WriteLatencies.begin(),
1051 WriteLatencies.end());
1052 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1053 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001054 }
1055 // ReadAdvanceEntries must remain in operand order.
1056 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1057 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1058 std::search(SchedTables.ReadAdvanceEntries.begin(),
1059 SchedTables.ReadAdvanceEntries.end(),
1060 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1061 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1062 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1063 else {
1064 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1065 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1066 ReadAdvanceEntries.end());
1067 }
1068 }
1069}
1070
Andrew Tricka72fca62012-09-17 22:18:50 +00001071// Emit SchedClass tables for all processors and associated global tables.
1072void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1073 raw_ostream &OS) {
1074 // Emit global WriteProcResTable.
1075 OS << "\n// {ProcResourceIdx, Cycles}\n"
1076 << "extern const llvm::MCWriteProcResEntry "
1077 << Target << "WriteProcResTable[] = {\n"
1078 << " { 0, 0}, // Invalid\n";
1079 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1080 WPRIdx != WPREnd; ++WPRIdx) {
1081 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1082 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1083 << format("%2d", WPREntry.Cycles) << "}";
1084 if (WPRIdx + 1 < WPREnd)
1085 OS << ',';
1086 OS << " // #" << WPRIdx << '\n';
1087 }
1088 OS << "}; // " << Target << "WriteProcResTable\n";
1089
1090 // Emit global WriteLatencyTable.
1091 OS << "\n// {Cycles, WriteResourceID}\n"
1092 << "extern const llvm::MCWriteLatencyEntry "
1093 << Target << "WriteLatencyTable[] = {\n"
1094 << " { 0, 0}, // Invalid\n";
1095 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1096 WLIdx != WLEnd; ++WLIdx) {
1097 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1098 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1099 << format("%2d", WLEntry.WriteResourceID) << "}";
1100 if (WLIdx + 1 < WLEnd)
1101 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001102 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001103 }
1104 OS << "}; // " << Target << "WriteLatencyTable\n";
1105
1106 // Emit global ReadAdvanceTable.
1107 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1108 << "extern const llvm::MCReadAdvanceEntry "
1109 << Target << "ReadAdvanceTable[] = {\n"
1110 << " {0, 0, 0}, // Invalid\n";
1111 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1112 RAIdx != RAEnd; ++RAIdx) {
1113 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1114 OS << " {" << RAEntry.UseIdx << ", "
1115 << format("%2d", RAEntry.WriteResourceID) << ", "
1116 << format("%2d", RAEntry.Cycles) << "}";
1117 if (RAIdx + 1 < RAEnd)
1118 OS << ',';
1119 OS << " // #" << RAIdx << '\n';
1120 }
1121 OS << "}; // " << Target << "ReadAdvanceTable\n";
1122
1123 // Emit a SchedClass table for each processor.
1124 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1125 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1126 if (!PI->hasInstrSchedModel())
1127 continue;
1128
1129 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001130 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001131
1132 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1133 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1134 OS << "static const llvm::MCSchedClassDesc "
1135 << PI->ModelName << "SchedClasses[] = {\n";
1136
1137 // The first class is always invalid. We no way to distinguish it except by
1138 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001139 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001140 && "invalid class not first");
1141 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1142 << MCSchedClassDesc::InvalidNumMicroOps
1143 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1144
1145 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1146 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1147 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1148 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1149 if (SchedClass.Name.size() < 18)
1150 OS.indent(18 - SchedClass.Name.size());
1151 OS << MCDesc.NumMicroOps
1152 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1153 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1154 << ", " << MCDesc.NumWriteProcResEntries
1155 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1156 << ", " << MCDesc.NumWriteLatencyEntries
1157 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1158 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1159 if (SCIdx + 1 < SCEnd)
1160 OS << ',';
1161 OS << " // #" << SCIdx << '\n';
1162 }
1163 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1164 }
1165}
1166
Andrew Trick87255e32012-07-07 04:00:00 +00001167void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1168 // For each processor model.
1169 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1170 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001171 // Emit processor resource table.
1172 if (PI->hasInstrSchedModel())
1173 EmitProcessorResources(*PI, OS);
1174 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001175 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001176 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001177
Andrew Trick73d77362012-06-05 03:44:40 +00001178 // Begin processor itinerary properties
1179 OS << "\n";
Pete Cooper11759452014-09-02 17:43:54 +00001180 OS << "static const llvm::MCSchedModel " << PI->ModelName << " = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001181 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
Andrew Trickde2109e2013-06-15 04:49:57 +00001182 EmitProcessorProp(OS, PI->ModelDef, "MicroOpBufferSize", ',');
Hal Finkel6532c202014-05-08 09:14:44 +00001183 EmitProcessorProp(OS, PI->ModelDef, "LoopMicroOpBufferSize", ',');
Andrew Trick87255e32012-07-07 04:00:00 +00001184 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1185 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick352abc12012-08-08 02:44:16 +00001186 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001187
1188 OS << " " << (bool)(PI->ModelDef ?
Sanjay Patela2f658d2014-07-15 22:39:58 +00001189 PI->ModelDef->getValueAsBit("PostRAScheduler") : 0)
1190 << ", // " << "PostRAScheduler\n";
1191
1192 OS << " " << (bool)(PI->ModelDef ?
Andrew Trickb6854d82013-09-25 18:14:12 +00001193 PI->ModelDef->getValueAsBit("CompleteModel") : 0)
1194 << ", // " << "CompleteModel\n";
1195
Andrew Trickab722bd2012-09-18 03:18:56 +00001196 OS << " " << PI->Index << ", // Processor ID\n";
1197 if (PI->hasInstrSchedModel())
1198 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1199 << " " << PI->ModelName << "SchedClasses" << ",\n"
1200 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1201 << " " << (SchedModels.schedClassEnd()
1202 - SchedModels.schedClassBegin()) << ",\n";
1203 else
1204 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Pete Cooper11759452014-09-02 17:43:54 +00001205 if (PI->hasItineraries())
1206 OS << " " << PI->ItinsDef->getName() << "};\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001207 else
Pete Cooper11759452014-09-02 17:43:54 +00001208 OS << " nullptr}; // No Itinerary\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001209 }
Jim Laskey3763a502005-10-31 17:16:01 +00001210}
1211
1212//
1213// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1214//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001215void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001216 // Gather and sort processor information
1217 std::vector<Record*> ProcessorList =
1218 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001219 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001220
1221 // Begin processor table
1222 OS << "\n";
1223 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001224 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001225 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001226
Jim Laskey3763a502005-10-31 17:16:01 +00001227 // For each processor
1228 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1229 // Next processor
1230 Record *Processor = ProcessorList[i];
1231
Bill Wendlinge6182262007-05-04 20:38:40 +00001232 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001233 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001234 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001235
Jim Laskey3763a502005-10-31 17:16:01 +00001236 // Emit as { "cpu", procinit },
Andrew Trick23f3c652012-09-17 22:18:45 +00001237 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001238
Jim Laskey3763a502005-10-31 17:16:01 +00001239 // Depending on ''if more in the list'' emit comma
1240 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001241
Jim Laskey3763a502005-10-31 17:16:01 +00001242 OS << "\n";
1243 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001244
Jim Laskey3763a502005-10-31 17:16:01 +00001245 // End processor table
1246 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001247}
1248
1249//
Andrew Trick87255e32012-07-07 04:00:00 +00001250// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001251//
Andrew Trick87255e32012-07-07 04:00:00 +00001252void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001253 OS << "#ifdef DBGFIELD\n"
1254 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1255 << "#endif\n"
1256 << "#ifndef NDEBUG\n"
1257 << "#define DBGFIELD(x) x,\n"
1258 << "#else\n"
1259 << "#define DBGFIELD(x)\n"
1260 << "#endif\n";
1261
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001262 if (SchedModels.hasItineraries()) {
Andrew Trick87255e32012-07-07 04:00:00 +00001263 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001264 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001265 EmitStageAndOperandCycleData(OS, ProcItinLists);
1266 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001267 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001268 OS << "\n// ===============================================================\n"
1269 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001270
Andrew Trick9ef08822012-09-17 22:18:48 +00001271 SchedClassTables SchedTables;
1272 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1273 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1274 GenSchedClassTables(*PI, SchedTables);
1275 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001276 EmitSchedClassTables(SchedTables, OS);
1277
1278 // Emit the processor machine model
1279 EmitProcessorModels(OS);
1280 // Emit the processor lookup data
1281 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001282
Andrew Trick23f3c652012-09-17 22:18:45 +00001283 OS << "#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001284}
1285
Andrew Trickc6c88152012-09-18 03:41:43 +00001286void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1287 raw_ostream &OS) {
1288 OS << "unsigned " << ClassName
1289 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1290 << " const TargetSchedModel *SchedModel) const {\n";
1291
1292 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1293 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1294 for (std::vector<Record*>::const_iterator
1295 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1296 OS << (*PI)->getValueAsString("Code") << '\n';
1297 }
1298 IdxVec VariantClasses;
1299 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1300 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1301 if (SCI->Transitions.empty())
1302 continue;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001303 VariantClasses.push_back(SCI->Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001304 }
1305 if (!VariantClasses.empty()) {
1306 OS << " switch (SchedClass) {\n";
1307 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1308 VCI != VCE; ++VCI) {
1309 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1310 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1311 IdxVec ProcIndices;
1312 for (std::vector<CodeGenSchedTransition>::const_iterator
1313 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1314 TI != TE; ++TI) {
1315 IdxVec PI;
1316 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1317 ProcIndices.begin(), ProcIndices.end(),
1318 std::back_inserter(PI));
1319 ProcIndices.swap(PI);
1320 }
1321 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1322 PI != PE; ++PI) {
1323 OS << " ";
1324 if (*PI != 0)
1325 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1326 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1327 << '\n';
1328 for (std::vector<CodeGenSchedTransition>::const_iterator
1329 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1330 TI != TE; ++TI) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001331 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1332 TI->ProcIndices.end(), *PI)) {
1333 continue;
1334 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001335 OS << " if (";
Andrew Trickc6c88152012-09-18 03:41:43 +00001336 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1337 RI != RE; ++RI) {
1338 if (RI != TI->PredTerm.begin())
1339 OS << "\n && ";
1340 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1341 }
1342 OS << ")\n"
1343 << " return " << TI->ToClassIdx << "; // "
1344 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1345 }
1346 OS << " }\n";
1347 if (*PI == 0)
1348 break;
1349 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001350 if (SC.isInferred())
1351 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001352 OS << " break;\n";
1353 }
1354 OS << " };\n";
1355 }
1356 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1357 << "} // " << ClassName << "::resolveSchedClass\n";
1358}
1359
Jim Laskey86f002c2005-10-27 19:47:21 +00001360//
Jim Laskeya2b52352005-10-26 17:30:34 +00001361// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1362// the subtarget features string.
1363//
Evan Cheng54b68e32011-07-01 20:45:01 +00001364void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1365 unsigned NumFeatures,
1366 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001367 std::vector<Record*> Features =
1368 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001369 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001370
Andrew Trickdb6ed642011-04-01 01:56:55 +00001371 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1372 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001373 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001374 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001375 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001376 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001377 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001378
1379 if (Features.empty()) {
1380 OS << "}\n";
1381 return;
1382 }
1383
Andrew Trickba7b9212012-09-18 05:33:15 +00001384 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteinba5b04c2015-02-19 09:01:04 +00001385 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001386
Jim Laskeydffe5972005-10-28 21:47:29 +00001387 for (unsigned i = 0; i < Features.size(); i++) {
1388 // Next record
1389 Record *R = Features[i];
Bill Wendlinge6182262007-05-04 20:38:40 +00001390 const std::string &Instance = R->getName();
1391 const std::string &Value = R->getValueAsString("Value");
1392 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001393
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001394 if (Value=="true" || Value=="false")
Michael Kupersteinba5b04c2015-02-19 09:01:04 +00001395 OS << " if (Bits[" << Target << "::"
1396 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001397 << Attribute << " = " << Value << ";\n";
1398 else
Michael Kupersteinba5b04c2015-02-19 09:01:04 +00001399 OS << " if (Bits[" << Target << "::"
1400 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001401 << Attribute << " < " << Value << ") "
1402 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001403 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001404
Evan Chengfe6e4052011-06-30 01:53:36 +00001405 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001406}
1407
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001408//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001409// SubtargetEmitter::run - Main subtarget enumeration emitter.
1410//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001411void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001412 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001413
Evan Cheng4d1ca962011-07-08 01:53:10 +00001414 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1415 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1416
1417 OS << "namespace llvm {\n";
Michael Kupersteinba5b04c2015-02-19 09:01:04 +00001418 Enumeration(OS, "SubtargetFeature");
Evan Cheng4d1ca962011-07-08 01:53:10 +00001419 OS << "} // End llvm namespace \n";
1420 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1421
Evan Cheng54b68e32011-07-01 20:45:01 +00001422 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1423 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001424
Evan Cheng54b68e32011-07-01 20:45:01 +00001425 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001426#if 0
1427 OS << "namespace {\n";
1428#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001429 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001430 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001431 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001432 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001433 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001434 OS << "\n";
1435#if 0
1436 OS << "}\n";
1437#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001438
1439 // MCInstrInfo initialization routine.
1440 OS << "static inline void Init" << Target
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001441 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1442 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1443 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001444 if (NumFeatures)
1445 OS << Target << "FeatureKV, ";
1446 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001447 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001448 if (NumProcs)
1449 OS << Target << "SubTypeKV, ";
1450 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001451 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001452 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001453 OS << Target << "ProcSchedKV, "
1454 << Target << "WriteProcResTable, "
1455 << Target << "WriteLatencyTable, "
1456 << Target << "ReadAdvanceTable, ";
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001457 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001458 OS << '\n'; OS.indent(22);
1459 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001460 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001461 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001462 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001463 OS << "0, 0, 0";
1464 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001465
1466 OS << "} // End llvm namespace \n";
1467
1468 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1469
1470 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1471 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1472
1473 OS << "#include \"llvm/Support/Debug.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001474 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1475
1476 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1477
Evan Cheng0d639a22011-07-01 21:01:15 +00001478 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001479 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1480 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1481
1482 std::string ClassName = Target + "GenSubtargetInfo";
1483 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001484 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001485 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng1a72add62011-07-07 07:07:08 +00001486 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1487 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001488 << "public:\n"
Andrew Trickc6c88152012-09-18 03:41:43 +00001489 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001490 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001491 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001492 << " const;\n"
Evan Cheng54b68e32011-07-01 20:45:01 +00001493 << "};\n";
1494 OS << "} // End llvm namespace \n";
1495
1496 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1497
1498 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1499 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1500
Andrew Trick1188e432012-09-18 03:32:57 +00001501 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001502 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001503 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1504 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001505 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1506 OS << "extern const llvm::MCWriteProcResEntry "
1507 << Target << "WriteProcResTable[];\n";
1508 OS << "extern const llvm::MCWriteLatencyEntry "
1509 << Target << "WriteLatencyTable[];\n";
1510 OS << "extern const llvm::MCReadAdvanceEntry "
1511 << Target << "ReadAdvanceTable[];\n";
1512
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001513 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001514 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1515 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001516 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001517 }
1518
Evan Cheng1a72add62011-07-07 07:07:08 +00001519 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1520 << "StringRef FS)\n"
Evan Cheng0d639a22011-07-01 21:01:15 +00001521 << " : TargetSubtargetInfo() {\n"
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001522 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001523 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001524 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001525 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001526 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001527 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001528 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001529 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001530 OS << "None, ";
Andrew Trickab722bd2012-09-18 03:18:56 +00001531 OS << '\n'; OS.indent(22);
1532 OS << Target << "ProcSchedKV, "
1533 << Target << "WriteProcResTable, "
1534 << Target << "WriteLatencyTable, "
1535 << Target << "ReadAdvanceTable, ";
1536 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001537 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001538 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001539 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001540 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001541 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001542 OS << "0, 0, 0";
1543 OS << ");\n}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001544
Andrew Trickc6c88152012-09-18 03:41:43 +00001545 EmitSchedModelHelpers(ClassName, OS);
1546
Evan Cheng54b68e32011-07-01 20:45:01 +00001547 OS << "} // End llvm namespace \n";
1548
1549 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001550}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001551
1552namespace llvm {
1553
1554void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001555 CodeGenTarget CGTarget(RK);
1556 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001557}
1558
1559} // End llvm namespace