blob: 5ab7b136cf472db91e767b58c43bd09a0cc688e7 [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"
Chandler Carruth91d19d82012-12-04 10:37:14 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/Format.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000021#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000022#include "llvm/TableGen/Record.h"
23#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenb0aa47b2005-10-28 01:43:09 +000024#include <algorithm>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025#include <map>
26#include <string>
27#include <vector>
Jim Laskeycfda85a2005-10-21 19:00:04 +000028using namespace llvm;
29
Chandler Carruth97acce22014-04-22 03:06:00 +000030#define DEBUG_TYPE "subtarget-emitter"
31
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000032namespace {
33class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000034 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
35 // The SchedClassDesc table indexes into a global write resource table, write
36 // latency table, and read advance table.
37 struct SchedClassTables {
38 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
39 std::vector<MCWriteProcResEntry> WriteProcResources;
40 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000041 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000042 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
43
44 // Reserve an invalid entry at index 0
45 SchedClassTables() {
46 ProcSchedClasses.resize(1);
47 WriteProcResources.resize(1);
48 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000049 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000050 ReadAdvanceEntries.resize(1);
51 }
52 };
53
54 struct LessWriteProcResources {
55 bool operator()(const MCWriteProcResEntry &LHS,
56 const MCWriteProcResEntry &RHS) {
57 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
58 }
59 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000060
61 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000062 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000063 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000064
Michael Kupersteinc3434b32015-05-13 10:28:46 +000065 void Enumeration(raw_ostream &OS, const char *ClassName, bool isBits);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000066 unsigned FeatureKeyValues(raw_ostream &OS);
67 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000068 void FormItineraryStageString(const std::string &Names,
69 Record *ItinData, std::string &ItinString,
70 unsigned &NStages);
71 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
72 unsigned &NOperandCycles);
73 void FormItineraryBypassString(const std::string &Names,
74 Record *ItinData,
75 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000076 void EmitStageAndOperandCycleData(raw_ostream &OS,
77 std::vector<std::vector<InstrItinerary> >
78 &ProcItinLists);
79 void EmitItineraries(raw_ostream &OS,
80 std::vector<std::vector<InstrItinerary> >
81 &ProcItinLists);
82 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000083 char Separator);
Andrew Trick23f3c652012-09-17 22:18:45 +000084 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
85 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +000086 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +000087 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +000088 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
89 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +000090 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
91 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +000092 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
93 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +000094 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +000095 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000096 void EmitProcessorLookup(raw_ostream &OS);
Andrew Trickc6c88152012-09-18 03:41:43 +000097 void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +000098 void EmitSchedModel(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000099 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
100 unsigned NumProcs);
101
102public:
Andrew Trick87255e32012-07-07 04:00:00 +0000103 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
104 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000105
106 void run(raw_ostream &o);
107
108};
109} // End anonymous namespace
110
Jim Laskeya1beea62005-10-22 07:59:56 +0000111//
Jim Laskeya2b52352005-10-26 17:30:34 +0000112// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000113//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000114void SubtargetEmitter::Enumeration(raw_ostream &OS,
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000115 const char *ClassName,
116 bool isBits) {
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 Kupersteinc3434b32015-05-13 10:28:46 +0000124 if (N > 64)
125 PrintFatalError("Too many (> 64) 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 Kupersteinc3434b32015-05-13 10:28:46 +0000138 OS << " " << Def->getName();
139
140 // If bit flags then emit expression (1 << i)
141 if (isBits) OS << " = " << " 1ULL << " << i;
142
143 // Depending on 'if more in the list' emit comma
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000144 if (++i < N) OS << ",";
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000145
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000146 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000147 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000148
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000149 // Close enumeration
150 OS << "};\n";
151
152 OS << "}\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000153}
154
155//
Bill Wendlinge6182262007-05-04 20:38:40 +0000156// FeatureKeyValues - Emit data of all the subtarget features. Used by the
157// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000158//
Evan Cheng54b68e32011-07-01 20:45:01 +0000159unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000160 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000161 std::vector<Record*> FeatureList =
162 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000163
164 if (FeatureList.empty())
165 return 0;
166
Jim Grosbach56938af2008-09-11 17:05:32 +0000167 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000168
Jim Laskey19595752005-10-28 15:20:43 +0000169 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000170 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000171 << "extern const llvm::SubtargetFeatureKV " << Target
172 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000173
Jim Laskey19595752005-10-28 15:20:43 +0000174 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000175 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000176 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000177 // Next feature
178 Record *Feature = FeatureList[i];
179
Bill Wendlinge6182262007-05-04 20:38:40 +0000180 const std::string &Name = Feature->getName();
181 const std::string &CommandLineName = Feature->getValueAsString("Name");
182 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000183
Jim Laskey3f7d0472006-12-12 20:55:58 +0000184 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000185
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000186 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000187 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000188 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000189 << "\"" << Desc << "\", "
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000190 << Target << "::" << Name << ", ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000191
Andrew Trickdb6ed642011-04-01 01:56:55 +0000192 const std::vector<Record*> &ImpliesList =
Bill Wendlinge6182262007-05-04 20:38:40 +0000193 Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000194
Bill Wendlinge6182262007-05-04 20:38:40 +0000195 if (ImpliesList.empty()) {
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000196 OS << "0ULL";
Bill Wendlinge6182262007-05-04 20:38:40 +0000197 } else {
198 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000199 OS << Target << "::" << ImpliesList[j]->getName();
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000200 if (++j < M) OS << " | ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000201 }
202 }
203
204 OS << " }";
Evan Cheng54b68e32011-07-01 20:45:01 +0000205 ++NumFeatures;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000206
Jim Laskey3763a502005-10-31 17:16:01 +0000207 // Depending on 'if more in the list' emit comma
Jim Laskey3f7d0472006-12-12 20:55:58 +0000208 if ((i + 1) < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000209
Jim Laskeydffe5972005-10-28 21:47:29 +0000210 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000211 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000212
Jim Laskey19595752005-10-28 15:20:43 +0000213 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000214 OS << "};\n";
215
Evan Cheng54b68e32011-07-01 20:45:01 +0000216 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000217}
218
219//
220// CPUKeyValues - Emit data of all the subtarget processors. Used by command
221// line.
222//
Evan Cheng54b68e32011-07-01 20:45:01 +0000223unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000224 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000225 std::vector<Record*> ProcessorList =
226 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000227 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000228
Jim Laskey19595752005-10-28 15:20:43 +0000229 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000230 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000231 << "extern const llvm::SubtargetFeatureKV " << Target
232 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000233
Jim Laskey19595752005-10-28 15:20:43 +0000234 // For each processor
Jim Laskeydffe5972005-10-28 21:47:29 +0000235 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
236 // Next processor
237 Record *Processor = ProcessorList[i];
238
Bill Wendlinge6182262007-05-04 20:38:40 +0000239 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000240 const std::vector<Record*> &FeatureList =
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000241 Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000242
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000243 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000244 OS << " { "
245 << "\"" << Name << "\", "
246 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000247
Jim Laskeydffe5972005-10-28 21:47:29 +0000248 if (FeatureList.empty()) {
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000249 OS << "0ULL";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000250 } else {
Jim Laskeydffe5972005-10-28 21:47:29 +0000251 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000252 OS << Target << "::" << FeatureList[j]->getName();
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000253 if (++j < M) OS << " | ";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000254 }
255 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000256
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000257 // The "0" is for the "implies" section of this data structure.
258 OS << ", 0ULL }";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000259
Jim Laskey3763a502005-10-31 17:16:01 +0000260 // Depending on 'if more in the list' emit comma
Jim Laskeydffe5972005-10-28 21:47:29 +0000261 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000262
Jim Laskeydffe5972005-10-28 21:47:29 +0000263 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000264 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000265
Jim Laskey19595752005-10-28 15:20:43 +0000266 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000267 OS << "};\n";
268
Evan Cheng54b68e32011-07-01 20:45:01 +0000269 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000270}
Jim Laskeya1beea62005-10-22 07:59:56 +0000271
Jim Laskeya2b52352005-10-26 17:30:34 +0000272//
David Goodwind813cbf2009-08-17 16:02:57 +0000273// FormItineraryStageString - Compose a string containing the stage
274// data initialization for the specified itinerary. N is the number
275// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000276//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000277void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
278 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000279 std::string &ItinString,
280 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000281 // Get states list
Bill Wendlinge6182262007-05-04 20:38:40 +0000282 const std::vector<Record*> &StageList =
283 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000284
285 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000286 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000287 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000288 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000289 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000290
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000291 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000292 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000293 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000294
Jim Laskeydffe5972005-10-28 21:47:29 +0000295 // Get unit list
Bill Wendlinge6182262007-05-04 20:38:40 +0000296 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000297
Jim Laskey19595752005-10-28 15:20:43 +0000298 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000299 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000300 // Add name and bitwise or
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000301 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeydffe5972005-10-28 21:47:29 +0000302 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000303 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000304
David Goodwinb369ee42009-08-12 18:31:53 +0000305 int TimeInc = Stage->getValueAsInt("TimeInc");
306 ItinString += ", " + itostr(TimeInc);
307
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000308 int Kind = Stage->getValueAsInt("Kind");
309 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
310
Jim Laskey19595752005-10-28 15:20:43 +0000311 // Close off stage
312 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000313 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000314 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000315}
316
317//
David Goodwind813cbf2009-08-17 16:02:57 +0000318// FormItineraryOperandCycleString - Compose a string containing the
319// operand cycle initialization for the specified itinerary. N is the
320// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000321//
David Goodwind813cbf2009-08-17 16:02:57 +0000322void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
323 std::string &ItinString, unsigned &NOperandCycles) {
324 // Get operand cycle list
325 const std::vector<int64_t> &OperandCycleList =
326 ItinData->getValueAsListOfInts("OperandCycles");
327
328 // For each operand cycle
329 unsigned N = NOperandCycles = OperandCycleList.size();
330 for (unsigned i = 0; i < N;) {
331 // Next operand cycle
332 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000333
David Goodwind813cbf2009-08-17 16:02:57 +0000334 ItinString += " " + itostr(OCycle);
335 if (++i < N) ItinString += ", ";
336 }
337}
338
Evan Cheng0097dd02010-09-28 23:50:49 +0000339void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
340 Record *ItinData,
341 std::string &ItinString,
342 unsigned NOperandCycles) {
343 const std::vector<Record*> &BypassList =
344 ItinData->getValueAsListOfDefs("Bypasses");
345 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000346 unsigned i = 0;
347 for (; i < N;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000348 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000349 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000350 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000351 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000352 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000353 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000354 }
355}
356
David Goodwind813cbf2009-08-17 16:02:57 +0000357//
Andrew Trick87255e32012-07-07 04:00:00 +0000358// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
359// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
360// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000361//
Andrew Trick87255e32012-07-07 04:00:00 +0000362void SubtargetEmitter::
363EmitStageAndOperandCycleData(raw_ostream &OS,
364 std::vector<std::vector<InstrItinerary> >
365 &ProcItinLists) {
Jim Laskey19595752005-10-28 15:20:43 +0000366
Andrew Trickfb982dd2012-07-09 20:43:03 +0000367 // Multiple processor models may share an itinerary record. Emit it once.
368 SmallPtrSet<Record*, 8> ItinsDefSet;
369
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000370 // Emit functional units for all the itineraries.
Andrew Trick87255e32012-07-07 04:00:00 +0000371 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
372 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000373
David Blaikie70573dc2014-11-19 07:49:26 +0000374 if (!ItinsDefSet.insert(PI->ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000375 continue;
376
Andrew Trick87255e32012-07-07 04:00:00 +0000377 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000378 if (FUs.empty())
379 continue;
380
Andrew Trick87255e32012-07-07 04:00:00 +0000381 const std::string &Name = PI->ItinsDef->getName();
382 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000383 << "namespace " << Name << "FU {\n";
384
385 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000386 OS << " const unsigned " << FUs[j]->getName()
387 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000388
389 OS << "}\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000390
Andrew Trick87255e32012-07-07 04:00:00 +0000391 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000392 if (!BPs.empty()) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000393 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
394 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000395
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000396 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000397 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000398 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000399 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000400
Evan Cheng4a010fd2010-09-29 22:42:35 +0000401 OS << "}\n";
402 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000403 }
404
Jim Laskey19595752005-10-28 15:20:43 +0000405 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000406 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
407 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000408 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000409
David Goodwind813cbf2009-08-17 16:02:57 +0000410 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000411 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000412 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000413 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000414
415 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000416 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000417 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000418 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000419
Andrew Trick87255e32012-07-07 04:00:00 +0000420 // For each Itinerary across all processors, add a unique entry to the stages,
421 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
422 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000423 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000424 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick87255e32012-07-07 04:00:00 +0000425 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
426 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
427 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000428
Andrew Trick87255e32012-07-07 04:00:00 +0000429 // Add process itinerary to the list.
430 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000431
Andrew Trick87255e32012-07-07 04:00:00 +0000432 // If this processor defines no itineraries, then leave the itinerary list
433 // empty.
434 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000435 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000436 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000437
Andrew Trick87255e32012-07-07 04:00:00 +0000438 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000439
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000440 ItinList.resize(SchedModels.numInstrSchedClasses());
441 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
442
443 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000444 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
445
Jim Laskeydffe5972005-10-28 21:47:29 +0000446 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000447 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000448
Jim Laskey19595752005-10-28 15:20:43 +0000449 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000450 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000451 unsigned NStages = 0;
452 if (ItinData)
453 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000454
David Goodwind813cbf2009-08-17 16:02:57 +0000455 // Get string and operand cycle count
456 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000457 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000458 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000459 if (ItinData) {
460 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
461 NOperandCycles);
462
463 FormItineraryBypassString(Name, ItinData, ItinBypassString,
464 NOperandCycles);
465 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000466
David Goodwind813cbf2009-08-17 16:02:57 +0000467 // Check to see if stage already exists and create if it doesn't
468 unsigned FindStage = 0;
469 if (NStages > 0) {
470 FindStage = ItinStageMap[ItinStageString];
471 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000472 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
473 StageTable += ItinStageString + ", // " + itostr(StageCount);
474 if (NStages > 1)
475 StageTable += "-" + itostr(StageCount + NStages - 1);
476 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000477 // Record Itin class number.
478 ItinStageMap[ItinStageString] = FindStage = StageCount;
479 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000480 }
481 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000482
David Goodwind813cbf2009-08-17 16:02:57 +0000483 // Check to see if operand cycle already exists and create if it doesn't
484 unsigned FindOperandCycle = 0;
485 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000486 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
487 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000488 if (FindOperandCycle == 0) {
489 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000490 OperandCycleTable += ItinOperandCycleString + ", // ";
491 std::string OperandIdxComment = itostr(OperandCycleCount);
492 if (NOperandCycles > 1)
493 OperandIdxComment += "-"
494 + itostr(OperandCycleCount + NOperandCycles - 1);
495 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000496 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000497 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000498 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000499 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000500 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000501 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000502 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000503 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000504
Evan Cheng367a5df2010-09-09 18:18:55 +0000505 // Set up itinerary as location and location + stage count
Andrew Trick87255e32012-07-07 04:00:00 +0000506 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng367a5df2010-09-09 18:18:55 +0000507 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
508 FindOperandCycle,
509 FindOperandCycle + NOperandCycles};
510
Jim Laskey19595752005-10-28 15:20:43 +0000511 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000512 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000513 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000514 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000515
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000516 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000517 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000518 StageTable += "};\n";
519
520 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000521 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000522 OperandCycleTable += "};\n";
523
Andrew Trick87255e32012-07-07 04:00:00 +0000524 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000525 BypassTable += "};\n";
526
David Goodwind813cbf2009-08-17 16:02:57 +0000527 // Emit tables.
528 OS << StageTable;
529 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000530 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000531}
532
Andrew Trick87255e32012-07-07 04:00:00 +0000533//
534// EmitProcessorData - Generate data for processor itineraries that were
535// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
536// Itineraries for each processor. The Itinerary lists are indexed on
537// CodeGenSchedClass::Index.
538//
539void SubtargetEmitter::
540EmitItineraries(raw_ostream &OS,
541 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
542
Andrew Trickfb982dd2012-07-09 20:43:03 +0000543 // Multiple processor models may share an itinerary record. Emit it once.
544 SmallPtrSet<Record*, 8> ItinsDefSet;
545
Andrew Trick87255e32012-07-07 04:00:00 +0000546 // For each processor's machine model
547 std::vector<std::vector<InstrItinerary> >::iterator
548 ProcItinListsIter = ProcItinLists.begin();
549 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000550 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000551
Andrew Trick87255e32012-07-07 04:00:00 +0000552 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000553 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000554 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000555
556 // Get processor itinerary name
557 const std::string &Name = ItinsDef->getName();
558
559 // Get the itinerary list for the processor.
560 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000561 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000562
Pete Cooperc0eb1532014-09-02 23:23:34 +0000563 // Empty itineraries aren't referenced anywhere in the tablegen output
564 // so don't emit them.
565 if (ItinList.empty())
566 continue;
567
Andrew Trick87255e32012-07-07 04:00:00 +0000568 OS << "\n";
569 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000570
571 // Begin processor itinerary table
572 OS << Name << "[] = {\n";
573
574 // For each itinerary class in CodeGenSchedClass::Index order.
575 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
576 InstrItinerary &Intinerary = ItinList[j];
577
578 // Emit Itinerary in the form of
579 // { firstStage, lastStage, firstCycle, lastCycle } // index
580 OS << " { " <<
581 Intinerary.NumMicroOps << ", " <<
582 Intinerary.FirstStage << ", " <<
583 Intinerary.LastStage << ", " <<
584 Intinerary.FirstOperandCycle << ", " <<
585 Intinerary.LastOperandCycle << " }" <<
586 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
587 }
588 // End processor itinerary table
589 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
590 OS << "};\n";
591 }
592}
593
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000594// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000595// value defined in the C++ header. The Record is null if the processor does not
596// define a model.
597void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trick73d77362012-06-05 03:44:40 +0000598 const char *Name, char Separator) {
599 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000600 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000601 if (V >= 0)
602 OS << V << Separator << " // " << Name;
603 else
Andrew Trick87255e32012-07-07 04:00:00 +0000604 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000605 OS << '\n';
606}
607
Andrew Trick23f3c652012-09-17 22:18:45 +0000608void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
609 raw_ostream &OS) {
610 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
611
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000612 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000613 OS << "static const llvm::MCProcResourceDesc "
614 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000615 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000616
617 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
618 Record *PRDef = ProcModel.ProcResourceDefs[i];
619
Craig Topper24064772014-04-15 07:20:03 +0000620 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000621 unsigned SuperIdx = 0;
622 unsigned NumUnits = 0;
Andrew Trick40c4f382013-06-15 04:50:06 +0000623 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000624 if (PRDef->isSubClassOf("ProcResGroup")) {
625 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
626 for (RecIter RUI = ResUnits.begin(), RUE = ResUnits.end();
627 RUI != RUE; ++RUI) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000628 NumUnits += (*RUI)->getValueAsInt("NumUnits");
629 }
630 }
631 else {
632 // Find the SuperIdx
633 if (PRDef->getValueInit("Super")->isComplete()) {
634 SuperDef = SchedModels.findProcResUnits(
635 PRDef->getValueAsDef("Super"), ProcModel);
636 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
637 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000638 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000639 }
640 // Emit the ProcResourceDesc
641 if (i+1 == e)
642 Sep = ' ';
643 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
644 if (PRDef->getName().size() < 15)
645 OS.indent(15 - PRDef->getName().size());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000646 OS << NumUnits << ", " << SuperIdx << ", "
Andrew Trickde2109e2013-06-15 04:49:57 +0000647 << BufferSize << "}" << Sep << " // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000648 if (SuperDef)
649 OS << ", Super=" << SuperDef->getName();
650 OS << "\n";
651 }
652 OS << "};\n";
653}
654
Andrew Trick9ef08822012-09-17 22:18:48 +0000655// Find the WriteRes Record that defines processor resources for this
656// SchedWrite.
657Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000658 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000659
660 // Check if the SchedWrite is already subtarget-specific and directly
661 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000662 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
663 return SchedWrite.TheDef;
664
Craig Topper24064772014-04-15 07:20:03 +0000665 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000666 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
667 AI != AE; ++AI) {
668 const CodeGenSchedRW &AliasRW =
669 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000670 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
671 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
672 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
673 continue;
674 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000675 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000676 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000677 "defined for processor " + ProcModel.ModelName +
678 " Ensure only one SchedAlias exists per RW.");
679 AliasDef = AliasRW.TheDef;
680 }
681 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
682 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000683
684 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000685 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000686 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
687 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
688 if (!(*WRI)->isSubClassOf("WriteRes"))
689 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000690 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
691 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
692 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000693 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000694 "SchedWrite and its alias on processor " +
695 ProcModel.ModelName);
696 }
697 ResDef = *WRI;
698 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000699 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000700 // TODO: If ProcModel has a base model (previous generation processor),
701 // then call FindWriteResources recursively with that model here.
702 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000703 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9257b8f2012-09-22 02:24:21 +0000704 std::string("Processor does not define resources for ")
705 + SchedWrite.TheDef->getName());
706 }
707 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000708}
709
710/// Find the ReadAdvance record for the given SchedRead on this processor or
711/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000712Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000713 const CodeGenProcModel &ProcModel) {
714 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000715 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
716 return SchedRead.TheDef;
717
718 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000719 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000720 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
721 AI != AE; ++AI) {
722 const CodeGenSchedRW &AliasRW =
723 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000724 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
725 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
726 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
727 continue;
728 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000729 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000730 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000731 "defined for processor " + ProcModel.ModelName +
732 " Ensure only one SchedAlias exists per RW.");
733 AliasDef = AliasRW.TheDef;
734 }
735 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
736 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000737
738 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000739 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000740 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
741 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
742 if (!(*RAI)->isSubClassOf("ReadAdvance"))
743 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000744 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
745 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
746 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000747 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000748 "SchedRead and its alias on processor " +
749 ProcModel.ModelName);
750 }
751 ResDef = *RAI;
752 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000753 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000754 // TODO: If ProcModel has a base model (previous generation processor),
755 // then call FindReadAdvance recursively with that model here.
756 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000757 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9ef08822012-09-17 22:18:48 +0000758 std::string("Processor does not define resources for ")
Andrew Trick9257b8f2012-09-22 02:24:21 +0000759 + SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000760 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000761 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000762}
763
Andrew Trick4e67cba2013-03-14 21:21:50 +0000764// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000765// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000766void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
767 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000768 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000769 // Default to 1 resource cycle.
770 Cycles.resize(PRVec.size(), 1);
771 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000772 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000773 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000774 if (PRDef->isSubClassOf("ProcResGroup"))
775 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000776 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000777 SubResources.push_back(PRDef);
778 PRDef = SchedModels.findProcResUnits(PRVec[i], PM);
779 for (Record *SubDef = PRDef;
780 SubDef->getValueInit("Super")->isComplete();) {
781 if (SubDef->isSubClassOf("ProcResGroup")) {
782 // Disallow this for simplicitly.
783 PrintFatalError(SubDef->getLoc(), "Processor resource group "
784 " cannot be a super resources.");
785 }
786 Record *SuperDef =
787 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM);
788 PRVec.push_back(SuperDef);
789 Cycles.push_back(Cycles[i]);
790 SubDef = SuperDef;
791 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000792 }
Andrew Tricka3801a32013-04-23 23:45:16 +0000793 for (RecIter PRI = PM.ProcResourceDefs.begin(),
794 PRE = PM.ProcResourceDefs.end();
Andrew Trick4e67cba2013-03-14 21:21:50 +0000795 PRI != PRE; ++PRI) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000796 if (*PRI == PRDef || !(*PRI)->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000797 continue;
798 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000799 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000800 for( ; SubI != SubE; ++SubI) {
801 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
802 == SuperResources.end()) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000803 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000804 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000805 }
806 if (SubI == SubE) {
807 PRVec.push_back(*PRI);
808 Cycles.push_back(Cycles[i]);
809 }
810 }
811 }
812}
813
Andrew Trick9ef08822012-09-17 22:18:48 +0000814// Generate the SchedClass table for this processor and update global
815// tables. Must be called for each processor in order.
816void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
817 SchedClassTables &SchedTables) {
818 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
819 if (!ProcModel.hasInstrSchedModel())
820 return;
821
822 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
823 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
824 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trick7aba6be2012-10-03 23:06:25 +0000825 DEBUG(SCI->dump(&SchedModels));
826
Andrew Trick9ef08822012-09-17 22:18:48 +0000827 SCTab.resize(SCTab.size() + 1);
828 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000829 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000830 SCDesc.NumMicroOps = 0;
831 SCDesc.BeginGroup = false;
832 SCDesc.EndGroup = false;
833 SCDesc.WriteProcResIdx = 0;
834 SCDesc.WriteLatencyIdx = 0;
835 SCDesc.ReadAdvanceIdx = 0;
836
837 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000838 bool HasVariants = false;
839 for (std::vector<CodeGenSchedTransition>::const_iterator
840 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
841 TI != TE; ++TI) {
842 if (TI->ProcIndices[0] == 0) {
843 HasVariants = true;
844 break;
845 }
846 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
847 TI->ProcIndices.end(), ProcModel.Index);
848 if (PIPos != TI->ProcIndices.end()) {
849 HasVariants = true;
850 break;
851 }
852 }
853 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000854 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
855 continue;
856 }
857
858 // Determine if the SchedClass is actually reachable on this processor. If
859 // not don't try to locate the processor resources, it will fail.
860 // If ProcIndices contains 0, this class applies to all processors.
861 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
862 if (SCI->ProcIndices[0] != 0) {
863 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
864 SCI->ProcIndices.end(), ProcModel.Index);
865 if (PIPos == SCI->ProcIndices.end())
866 continue;
867 }
868 IdxVec Writes = SCI->Writes;
869 IdxVec Reads = SCI->Reads;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000870 if (!SCI->InstRWs.empty()) {
871 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000872 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000873 Record *RWDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000874 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
875 RWI != RWE; ++RWI) {
876 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
877 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
878 RWDef = *RWI;
879 break;
880 }
881 }
882 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000883 Writes.clear();
884 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000885 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
886 Writes, Reads);
887 }
888 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000889 if (Writes.empty()) {
890 // Check this processor's itinerary class resources.
891 for (RecIter II = ProcModel.ItinRWDefs.begin(),
892 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
893 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
894 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
895 != Matched.end()) {
896 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
897 Writes, Reads);
898 break;
899 }
900 }
901 if (Writes.empty()) {
902 DEBUG(dbgs() << ProcModel.ModelName
903 << " does not have resources for class " << SCI->Name << '\n');
904 }
905 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000906 // Sum resources across all operand writes.
907 std::vector<MCWriteProcResEntry> WriteProcResources;
908 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000909 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000910 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
911 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
912 IdxVec WriteSeq;
Andrew Trickda984b12012-10-03 23:06:28 +0000913 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
914 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000915
916 // For each operand, create a latency entry.
917 MCWriteLatencyEntry WLEntry;
918 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000919 unsigned WriteID = WriteSeq.back();
920 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
921 // If this Write is not referenced by a ReadAdvance, don't distinguish it
922 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000923 if (!SchedModels.hasReadOfWrite(
924 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000925 WriteID = 0;
926 }
927 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000928
929 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
930 WSI != WSE; ++WSI) {
931
Andrew Trick9257b8f2012-09-22 02:24:21 +0000932 Record *WriteRes =
933 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000934
935 // Mark the parent class as invalid for unsupported write types.
936 if (WriteRes->getValueAsBit("Unsupported")) {
937 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
938 break;
939 }
940 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
941 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
942 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
943 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
944
945 // Create an entry for each ProcResource listed in WriteRes.
946 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
947 std::vector<int64_t> Cycles =
948 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000949
950 ExpandProcResources(PRVec, Cycles, ProcModel);
951
Andrew Trick9ef08822012-09-17 22:18:48 +0000952 for (unsigned PRIdx = 0, PREnd = PRVec.size();
953 PRIdx != PREnd; ++PRIdx) {
954 MCWriteProcResEntry WPREntry;
955 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
956 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000957 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000958 // If this resource is already used in this sequence, add the current
959 // entry's cycles so that the same resource appears to be used
960 // serially, rather than multiple parallel uses. This is important for
961 // in-order machine where the resource consumption is a hazard.
962 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
963 for( ; WPRIdx != WPREnd; ++WPRIdx) {
964 if (WriteProcResources[WPRIdx].ProcResourceIdx
965 == WPREntry.ProcResourceIdx) {
966 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
967 break;
968 }
969 }
970 if (WPRIdx == WPREnd)
971 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000972 }
973 }
974 WriteLatencies.push_back(WLEntry);
975 }
976 // Create an entry for each operand Read in this SchedClass.
977 // Entries must be sorted first by UseIdx then by WriteResourceID.
978 for (unsigned UseIdx = 0, EndIdx = Reads.size();
979 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000980 Record *ReadAdvance =
981 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000982 if (!ReadAdvance)
983 continue;
984
985 // Mark the parent class as invalid for unsupported write types.
986 if (ReadAdvance->getValueAsBit("Unsupported")) {
987 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
988 break;
989 }
990 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
991 IdxVec WriteIDs;
992 if (ValidWrites.empty())
993 WriteIDs.push_back(0);
994 else {
995 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
996 VWI != VWE; ++VWI) {
997 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
998 }
999 }
1000 std::sort(WriteIDs.begin(), WriteIDs.end());
1001 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
1002 MCReadAdvanceEntry RAEntry;
1003 RAEntry.UseIdx = UseIdx;
1004 RAEntry.WriteResourceID = *WI;
1005 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1006 ReadAdvanceEntries.push_back(RAEntry);
1007 }
1008 }
1009 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1010 WriteProcResources.clear();
1011 WriteLatencies.clear();
1012 ReadAdvanceEntries.clear();
1013 }
1014 // Add the information for this SchedClass to the global tables using basic
1015 // compression.
1016 //
1017 // WritePrecRes entries are sorted by ProcResIdx.
1018 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1019 LessWriteProcResources());
1020
1021 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1022 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1023 std::search(SchedTables.WriteProcResources.begin(),
1024 SchedTables.WriteProcResources.end(),
1025 WriteProcResources.begin(), WriteProcResources.end());
1026 if (WPRPos != SchedTables.WriteProcResources.end())
1027 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1028 else {
1029 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1030 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1031 WriteProcResources.end());
1032 }
1033 // Latency entries must remain in operand order.
1034 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1035 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1036 std::search(SchedTables.WriteLatencies.begin(),
1037 SchedTables.WriteLatencies.end(),
1038 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001039 if (WLPos != SchedTables.WriteLatencies.end()) {
1040 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1041 SCDesc.WriteLatencyIdx = idx;
1042 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1043 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1044 std::string::npos) {
1045 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1046 }
1047 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001048 else {
1049 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001050 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1051 WriteLatencies.begin(),
1052 WriteLatencies.end());
1053 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1054 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001055 }
1056 // ReadAdvanceEntries must remain in operand order.
1057 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1058 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1059 std::search(SchedTables.ReadAdvanceEntries.begin(),
1060 SchedTables.ReadAdvanceEntries.end(),
1061 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1062 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1063 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1064 else {
1065 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1066 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1067 ReadAdvanceEntries.end());
1068 }
1069 }
1070}
1071
Andrew Tricka72fca62012-09-17 22:18:50 +00001072// Emit SchedClass tables for all processors and associated global tables.
1073void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1074 raw_ostream &OS) {
1075 // Emit global WriteProcResTable.
1076 OS << "\n// {ProcResourceIdx, Cycles}\n"
1077 << "extern const llvm::MCWriteProcResEntry "
1078 << Target << "WriteProcResTable[] = {\n"
1079 << " { 0, 0}, // Invalid\n";
1080 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1081 WPRIdx != WPREnd; ++WPRIdx) {
1082 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1083 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1084 << format("%2d", WPREntry.Cycles) << "}";
1085 if (WPRIdx + 1 < WPREnd)
1086 OS << ',';
1087 OS << " // #" << WPRIdx << '\n';
1088 }
1089 OS << "}; // " << Target << "WriteProcResTable\n";
1090
1091 // Emit global WriteLatencyTable.
1092 OS << "\n// {Cycles, WriteResourceID}\n"
1093 << "extern const llvm::MCWriteLatencyEntry "
1094 << Target << "WriteLatencyTable[] = {\n"
1095 << " { 0, 0}, // Invalid\n";
1096 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1097 WLIdx != WLEnd; ++WLIdx) {
1098 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1099 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1100 << format("%2d", WLEntry.WriteResourceID) << "}";
1101 if (WLIdx + 1 < WLEnd)
1102 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001103 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001104 }
1105 OS << "}; // " << Target << "WriteLatencyTable\n";
1106
1107 // Emit global ReadAdvanceTable.
1108 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1109 << "extern const llvm::MCReadAdvanceEntry "
1110 << Target << "ReadAdvanceTable[] = {\n"
1111 << " {0, 0, 0}, // Invalid\n";
1112 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1113 RAIdx != RAEnd; ++RAIdx) {
1114 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1115 OS << " {" << RAEntry.UseIdx << ", "
1116 << format("%2d", RAEntry.WriteResourceID) << ", "
1117 << format("%2d", RAEntry.Cycles) << "}";
1118 if (RAIdx + 1 < RAEnd)
1119 OS << ',';
1120 OS << " // #" << RAIdx << '\n';
1121 }
1122 OS << "}; // " << Target << "ReadAdvanceTable\n";
1123
1124 // Emit a SchedClass table for each processor.
1125 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1126 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1127 if (!PI->hasInstrSchedModel())
1128 continue;
1129
1130 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001131 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001132
1133 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1134 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1135 OS << "static const llvm::MCSchedClassDesc "
1136 << PI->ModelName << "SchedClasses[] = {\n";
1137
1138 // The first class is always invalid. We no way to distinguish it except by
1139 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001140 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001141 && "invalid class not first");
1142 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1143 << MCSchedClassDesc::InvalidNumMicroOps
1144 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1145
1146 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1147 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1148 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1149 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1150 if (SchedClass.Name.size() < 18)
1151 OS.indent(18 - SchedClass.Name.size());
1152 OS << MCDesc.NumMicroOps
1153 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1154 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1155 << ", " << MCDesc.NumWriteProcResEntries
1156 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1157 << ", " << MCDesc.NumWriteLatencyEntries
1158 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1159 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1160 if (SCIdx + 1 < SCEnd)
1161 OS << ',';
1162 OS << " // #" << SCIdx << '\n';
1163 }
1164 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1165 }
1166}
1167
Andrew Trick87255e32012-07-07 04:00:00 +00001168void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1169 // For each processor model.
1170 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1171 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001172 // Emit processor resource table.
1173 if (PI->hasInstrSchedModel())
1174 EmitProcessorResources(*PI, OS);
1175 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001176 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001177 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001178
Andrew Trick73d77362012-06-05 03:44:40 +00001179 // Begin processor itinerary properties
1180 OS << "\n";
Pete Cooper11759452014-09-02 17:43:54 +00001181 OS << "static const llvm::MCSchedModel " << PI->ModelName << " = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001182 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
Andrew Trickde2109e2013-06-15 04:49:57 +00001183 EmitProcessorProp(OS, PI->ModelDef, "MicroOpBufferSize", ',');
Hal Finkel6532c202014-05-08 09:14:44 +00001184 EmitProcessorProp(OS, PI->ModelDef, "LoopMicroOpBufferSize", ',');
Andrew Trick87255e32012-07-07 04:00:00 +00001185 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1186 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick352abc12012-08-08 02:44:16 +00001187 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001188
1189 OS << " " << (bool)(PI->ModelDef ?
Sanjay Patela2f658d2014-07-15 22:39:58 +00001190 PI->ModelDef->getValueAsBit("PostRAScheduler") : 0)
1191 << ", // " << "PostRAScheduler\n";
1192
1193 OS << " " << (bool)(PI->ModelDef ?
Andrew Trickb6854d82013-09-25 18:14:12 +00001194 PI->ModelDef->getValueAsBit("CompleteModel") : 0)
1195 << ", // " << "CompleteModel\n";
1196
Andrew Trickab722bd2012-09-18 03:18:56 +00001197 OS << " " << PI->Index << ", // Processor ID\n";
1198 if (PI->hasInstrSchedModel())
1199 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1200 << " " << PI->ModelName << "SchedClasses" << ",\n"
1201 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1202 << " " << (SchedModels.schedClassEnd()
1203 - SchedModels.schedClassBegin()) << ",\n";
1204 else
1205 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Pete Cooper11759452014-09-02 17:43:54 +00001206 if (PI->hasItineraries())
1207 OS << " " << PI->ItinsDef->getName() << "};\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001208 else
Pete Cooper11759452014-09-02 17:43:54 +00001209 OS << " nullptr}; // No Itinerary\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001210 }
Jim Laskey3763a502005-10-31 17:16:01 +00001211}
1212
1213//
1214// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1215//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001216void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001217 // Gather and sort processor information
1218 std::vector<Record*> ProcessorList =
1219 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001220 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001221
1222 // Begin processor table
1223 OS << "\n";
1224 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001225 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001226 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001227
Jim Laskey3763a502005-10-31 17:16:01 +00001228 // For each processor
1229 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1230 // Next processor
1231 Record *Processor = ProcessorList[i];
1232
Bill Wendlinge6182262007-05-04 20:38:40 +00001233 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001234 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001235 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001236
Jim Laskey3763a502005-10-31 17:16:01 +00001237 // Emit as { "cpu", procinit },
Andrew Trick23f3c652012-09-17 22:18:45 +00001238 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001239
Jim Laskey3763a502005-10-31 17:16:01 +00001240 // Depending on ''if more in the list'' emit comma
1241 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001242
Jim Laskey3763a502005-10-31 17:16:01 +00001243 OS << "\n";
1244 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001245
Jim Laskey3763a502005-10-31 17:16:01 +00001246 // End processor table
1247 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001248}
1249
1250//
Andrew Trick87255e32012-07-07 04:00:00 +00001251// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001252//
Andrew Trick87255e32012-07-07 04:00:00 +00001253void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001254 OS << "#ifdef DBGFIELD\n"
1255 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1256 << "#endif\n"
1257 << "#ifndef NDEBUG\n"
1258 << "#define DBGFIELD(x) x,\n"
1259 << "#else\n"
1260 << "#define DBGFIELD(x)\n"
1261 << "#endif\n";
1262
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001263 if (SchedModels.hasItineraries()) {
Andrew Trick87255e32012-07-07 04:00:00 +00001264 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001265 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001266 EmitStageAndOperandCycleData(OS, ProcItinLists);
1267 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001268 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001269 OS << "\n// ===============================================================\n"
1270 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001271
Andrew Trick9ef08822012-09-17 22:18:48 +00001272 SchedClassTables SchedTables;
1273 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1274 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1275 GenSchedClassTables(*PI, SchedTables);
1276 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001277 EmitSchedClassTables(SchedTables, OS);
1278
1279 // Emit the processor machine model
1280 EmitProcessorModels(OS);
1281 // Emit the processor lookup data
1282 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001283
Andrew Trick23f3c652012-09-17 22:18:45 +00001284 OS << "#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001285}
1286
Andrew Trickc6c88152012-09-18 03:41:43 +00001287void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1288 raw_ostream &OS) {
1289 OS << "unsigned " << ClassName
1290 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1291 << " const TargetSchedModel *SchedModel) const {\n";
1292
1293 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1294 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1295 for (std::vector<Record*>::const_iterator
1296 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1297 OS << (*PI)->getValueAsString("Code") << '\n';
1298 }
1299 IdxVec VariantClasses;
1300 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1301 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1302 if (SCI->Transitions.empty())
1303 continue;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001304 VariantClasses.push_back(SCI->Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001305 }
1306 if (!VariantClasses.empty()) {
1307 OS << " switch (SchedClass) {\n";
1308 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1309 VCI != VCE; ++VCI) {
1310 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1311 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1312 IdxVec ProcIndices;
1313 for (std::vector<CodeGenSchedTransition>::const_iterator
1314 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1315 TI != TE; ++TI) {
1316 IdxVec PI;
1317 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1318 ProcIndices.begin(), ProcIndices.end(),
1319 std::back_inserter(PI));
1320 ProcIndices.swap(PI);
1321 }
1322 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1323 PI != PE; ++PI) {
1324 OS << " ";
1325 if (*PI != 0)
1326 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1327 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1328 << '\n';
1329 for (std::vector<CodeGenSchedTransition>::const_iterator
1330 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1331 TI != TE; ++TI) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001332 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1333 TI->ProcIndices.end(), *PI)) {
1334 continue;
1335 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001336 OS << " if (";
Andrew Trickc6c88152012-09-18 03:41:43 +00001337 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1338 RI != RE; ++RI) {
1339 if (RI != TI->PredTerm.begin())
1340 OS << "\n && ";
1341 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1342 }
1343 OS << ")\n"
1344 << " return " << TI->ToClassIdx << "; // "
1345 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1346 }
1347 OS << " }\n";
1348 if (*PI == 0)
1349 break;
1350 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001351 if (SC.isInferred())
1352 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001353 OS << " break;\n";
1354 }
1355 OS << " };\n";
1356 }
1357 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1358 << "} // " << ClassName << "::resolveSchedClass\n";
1359}
1360
Jim Laskey86f002c2005-10-27 19:47:21 +00001361//
Jim Laskeya2b52352005-10-26 17:30:34 +00001362// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1363// the subtarget features string.
1364//
Evan Cheng54b68e32011-07-01 20:45:01 +00001365void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1366 unsigned NumFeatures,
1367 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001368 std::vector<Record*> Features =
1369 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001370 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001371
Andrew Trickdb6ed642011-04-01 01:56:55 +00001372 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1373 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001374 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001375 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001376 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001377 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001378 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001379
1380 if (Features.empty()) {
1381 OS << "}\n";
1382 return;
1383 }
1384
Andrew Trickba7b9212012-09-18 05:33:15 +00001385 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteinc3434b32015-05-13 10:28:46 +00001386 << " uint64_t Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001387
Jim Laskeydffe5972005-10-28 21:47:29 +00001388 for (unsigned i = 0; i < Features.size(); i++) {
1389 // Next record
1390 Record *R = Features[i];
Bill Wendlinge6182262007-05-04 20:38:40 +00001391 const std::string &Instance = R->getName();
1392 const std::string &Value = R->getValueAsString("Value");
1393 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001394
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001395 if (Value=="true" || Value=="false")
Michael Kupersteinc3434b32015-05-13 10:28:46 +00001396 OS << " if ((Bits & " << Target << "::"
1397 << Instance << ") != 0) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001398 << Attribute << " = " << Value << ";\n";
1399 else
Michael Kupersteinc3434b32015-05-13 10:28:46 +00001400 OS << " if ((Bits & " << Target << "::"
1401 << Instance << ") != 0 && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001402 << Attribute << " < " << Value << ") "
1403 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001404 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001405
Evan Chengfe6e4052011-06-30 01:53:36 +00001406 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001407}
1408
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001409//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001410// SubtargetEmitter::run - Main subtarget enumeration emitter.
1411//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001412void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001413 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001414
Evan Cheng4d1ca962011-07-08 01:53:10 +00001415 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1416 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1417
1418 OS << "namespace llvm {\n";
Michael Kupersteinc3434b32015-05-13 10:28:46 +00001419 Enumeration(OS, "SubtargetFeature", true);
Evan Cheng4d1ca962011-07-08 01:53:10 +00001420 OS << "} // End llvm namespace \n";
1421 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1422
Evan Cheng54b68e32011-07-01 20:45:01 +00001423 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1424 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001425
Evan Cheng54b68e32011-07-01 20:45:01 +00001426 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001427#if 0
1428 OS << "namespace {\n";
1429#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001430 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001431 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001432 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001433 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001434 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001435 OS << "\n";
1436#if 0
1437 OS << "}\n";
1438#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001439
1440 // MCInstrInfo initialization routine.
1441 OS << "static inline void Init" << Target
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001442 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1443 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1444 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001445 if (NumFeatures)
1446 OS << Target << "FeatureKV, ";
1447 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001448 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001449 if (NumProcs)
1450 OS << Target << "SubTypeKV, ";
1451 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001452 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001453 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001454 OS << Target << "ProcSchedKV, "
1455 << Target << "WriteProcResTable, "
1456 << Target << "WriteLatencyTable, "
1457 << Target << "ReadAdvanceTable, ";
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001458 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001459 OS << '\n'; OS.indent(22);
1460 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001461 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001462 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001463 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001464 OS << "0, 0, 0";
1465 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001466
1467 OS << "} // End llvm namespace \n";
1468
1469 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1470
1471 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1472 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1473
1474 OS << "#include \"llvm/Support/Debug.h\"\n";
Benjamin Kramerb85d3752015-03-23 18:45:56 +00001475 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001476 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1477
1478 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1479
Evan Cheng0d639a22011-07-01 21:01:15 +00001480 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001481 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1482 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1483
1484 std::string ClassName = Target + "GenSubtargetInfo";
1485 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001486 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001487 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng1a72add62011-07-07 07:07:08 +00001488 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1489 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001490 << "public:\n"
Andrew Trickc6c88152012-09-18 03:41:43 +00001491 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001492 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001493 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001494 << " const;\n"
Evan Cheng54b68e32011-07-01 20:45:01 +00001495 << "};\n";
1496 OS << "} // End llvm namespace \n";
1497
1498 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1499
1500 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1501 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1502
Andrew Trick1188e432012-09-18 03:32:57 +00001503 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001504 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001505 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1506 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001507 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1508 OS << "extern const llvm::MCWriteProcResEntry "
1509 << Target << "WriteProcResTable[];\n";
1510 OS << "extern const llvm::MCWriteLatencyEntry "
1511 << Target << "WriteLatencyTable[];\n";
1512 OS << "extern const llvm::MCReadAdvanceEntry "
1513 << Target << "ReadAdvanceTable[];\n";
1514
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001515 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001516 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1517 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001518 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001519 }
1520
Evan Cheng1a72add62011-07-07 07:07:08 +00001521 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1522 << "StringRef FS)\n"
Evan Cheng0d639a22011-07-01 21:01:15 +00001523 << " : TargetSubtargetInfo() {\n"
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001524 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001525 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001526 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001527 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001528 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001529 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001530 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001531 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001532 OS << "None, ";
Andrew Trickab722bd2012-09-18 03:18:56 +00001533 OS << '\n'; OS.indent(22);
1534 OS << Target << "ProcSchedKV, "
1535 << Target << "WriteProcResTable, "
1536 << Target << "WriteLatencyTable, "
1537 << Target << "ReadAdvanceTable, ";
1538 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001539 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001540 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001541 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001542 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001543 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001544 OS << "0, 0, 0";
1545 OS << ");\n}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001546
Andrew Trickc6c88152012-09-18 03:41:43 +00001547 EmitSchedModelHelpers(ClassName, OS);
1548
Evan Cheng54b68e32011-07-01 20:45:01 +00001549 OS << "} // End llvm namespace \n";
1550
1551 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001552}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001553
1554namespace llvm {
1555
1556void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001557 CodeGenTarget CGTarget(RK);
1558 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001559}
1560
1561} // End llvm namespace