blob: d8cf0d1e6ea7de252933f6752cef416c0ebf2242 [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 Kupersteinefd7a962015-02-19 11:38:11 +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 Kupersteinefd7a962015-02-19 11:38:11 +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 Kupersteinefd7a962015-02-19 11:38:11 +0000124 if (N > 64) {
125 errs() << "Too many (> 64) subtarget features!\n";
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 Kupersteinefd7a962015-02-19 11:38:11 +0000131 // For bit flag enumerations with more than 32 items, emit constants.
132 // Emit an enum for everything else.
133 if (isBits && N > 32) {
134 // For each record
135 for (unsigned i = 0; i < N; i++) {
136 // Next record
137 Record *Def = DefList[i];
Evan Cheng54b68e32011-07-01 20:45:01 +0000138
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000139 // Get and emit name and expression (1 << i)
140 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
141 }
142 } else {
143 // Open enumeration
144 OS << "enum {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000145
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000146 // For each record
147 for (unsigned i = 0; i < N;) {
148 // Next record
149 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000150
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000151 // Get and emit name
152 OS << " " << Def->getName();
153
154 // If bit flags then emit expression (1 << i)
155 if (isBits) OS << " = " << " 1ULL << " << i;
156
157 // Depending on 'if more in the list' emit comma
158 if (++i < N) OS << ",";
159
160 OS << "\n";
161 }
162
163 // Close enumeration
164 OS << "};\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000165 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000166
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000167 OS << "}\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000168}
169
170//
Bill Wendlinge6182262007-05-04 20:38:40 +0000171// FeatureKeyValues - Emit data of all the subtarget features. Used by the
172// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000173//
Evan Cheng54b68e32011-07-01 20:45:01 +0000174unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000175 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000176 std::vector<Record*> FeatureList =
177 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000178
179 if (FeatureList.empty())
180 return 0;
181
Jim Grosbach56938af2008-09-11 17:05:32 +0000182 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000183
Jim Laskey19595752005-10-28 15:20:43 +0000184 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000185 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000186 << "extern const llvm::SubtargetFeatureKV " << Target
187 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000188
Jim Laskey19595752005-10-28 15:20:43 +0000189 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000190 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000191 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000192 // Next feature
193 Record *Feature = FeatureList[i];
194
Bill Wendlinge6182262007-05-04 20:38:40 +0000195 const std::string &Name = Feature->getName();
196 const std::string &CommandLineName = Feature->getValueAsString("Name");
197 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000198
Jim Laskey3f7d0472006-12-12 20:55:58 +0000199 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000200
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000201 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000202 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000203 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000204 << "\"" << Desc << "\", "
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000205 << Target << "::" << Name << ", ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000206
Andrew Trickdb6ed642011-04-01 01:56:55 +0000207 const std::vector<Record*> &ImpliesList =
Bill Wendlinge6182262007-05-04 20:38:40 +0000208 Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000209
Bill Wendlinge6182262007-05-04 20:38:40 +0000210 if (ImpliesList.empty()) {
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000211 OS << "0ULL";
Bill Wendlinge6182262007-05-04 20:38:40 +0000212 } else {
213 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000214 OS << Target << "::" << ImpliesList[j]->getName();
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000215 if (++j < M) OS << " | ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000216 }
217 }
218
219 OS << " }";
Evan Cheng54b68e32011-07-01 20:45:01 +0000220 ++NumFeatures;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000221
Jim Laskey3763a502005-10-31 17:16:01 +0000222 // Depending on 'if more in the list' emit comma
Jim Laskey3f7d0472006-12-12 20:55:58 +0000223 if ((i + 1) < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000224
Jim Laskeydffe5972005-10-28 21:47:29 +0000225 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000226 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000227
Jim Laskey19595752005-10-28 15:20:43 +0000228 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000229 OS << "};\n";
230
Evan Cheng54b68e32011-07-01 20:45:01 +0000231 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000232}
233
234//
235// CPUKeyValues - Emit data of all the subtarget processors. Used by command
236// line.
237//
Evan Cheng54b68e32011-07-01 20:45:01 +0000238unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000239 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000240 std::vector<Record*> ProcessorList =
241 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000242 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000243
Jim Laskey19595752005-10-28 15:20:43 +0000244 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000245 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000246 << "extern const llvm::SubtargetFeatureKV " << Target
247 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000248
Jim Laskey19595752005-10-28 15:20:43 +0000249 // For each processor
Jim Laskeydffe5972005-10-28 21:47:29 +0000250 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
251 // Next processor
252 Record *Processor = ProcessorList[i];
253
Bill Wendlinge6182262007-05-04 20:38:40 +0000254 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000255 const std::vector<Record*> &FeatureList =
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000256 Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000257
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000258 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000259 OS << " { "
260 << "\"" << Name << "\", "
261 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000262
Jim Laskeydffe5972005-10-28 21:47:29 +0000263 if (FeatureList.empty()) {
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000264 OS << "0ULL";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000265 } else {
Jim Laskeydffe5972005-10-28 21:47:29 +0000266 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000267 OS << Target << "::" << FeatureList[j]->getName();
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000268 if (++j < M) OS << " | ";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000269 }
270 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000271
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000272 // The "0" is for the "implies" section of this data structure.
273 OS << ", 0ULL }";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000274
Jim Laskey3763a502005-10-31 17:16:01 +0000275 // Depending on 'if more in the list' emit comma
Jim Laskeydffe5972005-10-28 21:47:29 +0000276 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000277
Jim Laskeydffe5972005-10-28 21:47:29 +0000278 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000279 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000280
Jim Laskey19595752005-10-28 15:20:43 +0000281 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000282 OS << "};\n";
283
Evan Cheng54b68e32011-07-01 20:45:01 +0000284 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000285}
Jim Laskeya1beea62005-10-22 07:59:56 +0000286
Jim Laskeya2b52352005-10-26 17:30:34 +0000287//
David Goodwind813cbf2009-08-17 16:02:57 +0000288// FormItineraryStageString - Compose a string containing the stage
289// data initialization for the specified itinerary. N is the number
290// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000291//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000292void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
293 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000294 std::string &ItinString,
295 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000296 // Get states list
Bill Wendlinge6182262007-05-04 20:38:40 +0000297 const std::vector<Record*> &StageList =
298 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000299
300 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000301 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000302 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000303 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000304 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000305
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000306 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000307 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000308 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000309
Jim Laskeydffe5972005-10-28 21:47:29 +0000310 // Get unit list
Bill Wendlinge6182262007-05-04 20:38:40 +0000311 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000312
Jim Laskey19595752005-10-28 15:20:43 +0000313 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000314 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000315 // Add name and bitwise or
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000316 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeydffe5972005-10-28 21:47:29 +0000317 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000318 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000319
David Goodwinb369ee42009-08-12 18:31:53 +0000320 int TimeInc = Stage->getValueAsInt("TimeInc");
321 ItinString += ", " + itostr(TimeInc);
322
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000323 int Kind = Stage->getValueAsInt("Kind");
324 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
325
Jim Laskey19595752005-10-28 15:20:43 +0000326 // Close off stage
327 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000328 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000329 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000330}
331
332//
David Goodwind813cbf2009-08-17 16:02:57 +0000333// FormItineraryOperandCycleString - Compose a string containing the
334// operand cycle initialization for the specified itinerary. N is the
335// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000336//
David Goodwind813cbf2009-08-17 16:02:57 +0000337void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
338 std::string &ItinString, unsigned &NOperandCycles) {
339 // Get operand cycle list
340 const std::vector<int64_t> &OperandCycleList =
341 ItinData->getValueAsListOfInts("OperandCycles");
342
343 // For each operand cycle
344 unsigned N = NOperandCycles = OperandCycleList.size();
345 for (unsigned i = 0; i < N;) {
346 // Next operand cycle
347 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000348
David Goodwind813cbf2009-08-17 16:02:57 +0000349 ItinString += " " + itostr(OCycle);
350 if (++i < N) ItinString += ", ";
351 }
352}
353
Evan Cheng0097dd02010-09-28 23:50:49 +0000354void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
355 Record *ItinData,
356 std::string &ItinString,
357 unsigned NOperandCycles) {
358 const std::vector<Record*> &BypassList =
359 ItinData->getValueAsListOfDefs("Bypasses");
360 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000361 unsigned i = 0;
362 for (; i < N;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000363 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000364 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000365 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000366 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000367 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000368 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000369 }
370}
371
David Goodwind813cbf2009-08-17 16:02:57 +0000372//
Andrew Trick87255e32012-07-07 04:00:00 +0000373// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
374// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
375// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000376//
Andrew Trick87255e32012-07-07 04:00:00 +0000377void SubtargetEmitter::
378EmitStageAndOperandCycleData(raw_ostream &OS,
379 std::vector<std::vector<InstrItinerary> >
380 &ProcItinLists) {
Jim Laskey19595752005-10-28 15:20:43 +0000381
Andrew Trickfb982dd2012-07-09 20:43:03 +0000382 // Multiple processor models may share an itinerary record. Emit it once.
383 SmallPtrSet<Record*, 8> ItinsDefSet;
384
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000385 // Emit functional units for all the itineraries.
Andrew Trick87255e32012-07-07 04:00:00 +0000386 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
387 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000388
David Blaikie70573dc2014-11-19 07:49:26 +0000389 if (!ItinsDefSet.insert(PI->ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000390 continue;
391
Andrew Trick87255e32012-07-07 04:00:00 +0000392 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000393 if (FUs.empty())
394 continue;
395
Andrew Trick87255e32012-07-07 04:00:00 +0000396 const std::string &Name = PI->ItinsDef->getName();
397 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000398 << "namespace " << Name << "FU {\n";
399
400 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000401 OS << " const unsigned " << FUs[j]->getName()
402 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000403
404 OS << "}\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000405
Andrew Trick87255e32012-07-07 04:00:00 +0000406 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000407 if (!BPs.empty()) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000408 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
409 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000410
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000411 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000412 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000413 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000414 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000415
Evan Cheng4a010fd2010-09-29 22:42:35 +0000416 OS << "}\n";
417 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000418 }
419
Jim Laskey19595752005-10-28 15:20:43 +0000420 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000421 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
422 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000423 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000424
David Goodwind813cbf2009-08-17 16:02:57 +0000425 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000426 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000427 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000428 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000429
430 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000431 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000432 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000433 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000434
Andrew Trick87255e32012-07-07 04:00:00 +0000435 // For each Itinerary across all processors, add a unique entry to the stages,
436 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
437 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000438 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000439 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick87255e32012-07-07 04:00:00 +0000440 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
441 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
442 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000443
Andrew Trick87255e32012-07-07 04:00:00 +0000444 // Add process itinerary to the list.
445 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000446
Andrew Trick87255e32012-07-07 04:00:00 +0000447 // If this processor defines no itineraries, then leave the itinerary list
448 // empty.
449 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000450 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000451 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000452
Andrew Trick87255e32012-07-07 04:00:00 +0000453 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000454
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000455 ItinList.resize(SchedModels.numInstrSchedClasses());
456 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
457
458 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000459 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
460
Jim Laskeydffe5972005-10-28 21:47:29 +0000461 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000462 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000463
Jim Laskey19595752005-10-28 15:20:43 +0000464 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000465 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000466 unsigned NStages = 0;
467 if (ItinData)
468 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000469
David Goodwind813cbf2009-08-17 16:02:57 +0000470 // Get string and operand cycle count
471 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000472 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000473 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000474 if (ItinData) {
475 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
476 NOperandCycles);
477
478 FormItineraryBypassString(Name, ItinData, ItinBypassString,
479 NOperandCycles);
480 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000481
David Goodwind813cbf2009-08-17 16:02:57 +0000482 // Check to see if stage already exists and create if it doesn't
483 unsigned FindStage = 0;
484 if (NStages > 0) {
485 FindStage = ItinStageMap[ItinStageString];
486 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000487 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
488 StageTable += ItinStageString + ", // " + itostr(StageCount);
489 if (NStages > 1)
490 StageTable += "-" + itostr(StageCount + NStages - 1);
491 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000492 // Record Itin class number.
493 ItinStageMap[ItinStageString] = FindStage = StageCount;
494 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000495 }
496 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000497
David Goodwind813cbf2009-08-17 16:02:57 +0000498 // Check to see if operand cycle already exists and create if it doesn't
499 unsigned FindOperandCycle = 0;
500 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000501 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
502 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000503 if (FindOperandCycle == 0) {
504 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000505 OperandCycleTable += ItinOperandCycleString + ", // ";
506 std::string OperandIdxComment = itostr(OperandCycleCount);
507 if (NOperandCycles > 1)
508 OperandIdxComment += "-"
509 + itostr(OperandCycleCount + NOperandCycles - 1);
510 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000511 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000512 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000513 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000514 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000515 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000516 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000517 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000518 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000519
Evan Cheng367a5df2010-09-09 18:18:55 +0000520 // Set up itinerary as location and location + stage count
Andrew Trick87255e32012-07-07 04:00:00 +0000521 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng367a5df2010-09-09 18:18:55 +0000522 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
523 FindOperandCycle,
524 FindOperandCycle + NOperandCycles};
525
Jim Laskey19595752005-10-28 15:20:43 +0000526 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000527 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000528 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000529 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000530
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000531 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000532 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000533 StageTable += "};\n";
534
535 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000536 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000537 OperandCycleTable += "};\n";
538
Andrew Trick87255e32012-07-07 04:00:00 +0000539 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000540 BypassTable += "};\n";
541
David Goodwind813cbf2009-08-17 16:02:57 +0000542 // Emit tables.
543 OS << StageTable;
544 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000545 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000546}
547
Andrew Trick87255e32012-07-07 04:00:00 +0000548//
549// EmitProcessorData - Generate data for processor itineraries that were
550// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
551// Itineraries for each processor. The Itinerary lists are indexed on
552// CodeGenSchedClass::Index.
553//
554void SubtargetEmitter::
555EmitItineraries(raw_ostream &OS,
556 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
557
Andrew Trickfb982dd2012-07-09 20:43:03 +0000558 // Multiple processor models may share an itinerary record. Emit it once.
559 SmallPtrSet<Record*, 8> ItinsDefSet;
560
Andrew Trick87255e32012-07-07 04:00:00 +0000561 // For each processor's machine model
562 std::vector<std::vector<InstrItinerary> >::iterator
563 ProcItinListsIter = ProcItinLists.begin();
564 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000565 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000566
Andrew Trick87255e32012-07-07 04:00:00 +0000567 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000568 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000569 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000570
571 // Get processor itinerary name
572 const std::string &Name = ItinsDef->getName();
573
574 // Get the itinerary list for the processor.
575 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000576 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000577
Pete Cooperc0eb1532014-09-02 23:23:34 +0000578 // Empty itineraries aren't referenced anywhere in the tablegen output
579 // so don't emit them.
580 if (ItinList.empty())
581 continue;
582
Andrew Trick87255e32012-07-07 04:00:00 +0000583 OS << "\n";
584 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000585
586 // Begin processor itinerary table
587 OS << Name << "[] = {\n";
588
589 // For each itinerary class in CodeGenSchedClass::Index order.
590 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
591 InstrItinerary &Intinerary = ItinList[j];
592
593 // Emit Itinerary in the form of
594 // { firstStage, lastStage, firstCycle, lastCycle } // index
595 OS << " { " <<
596 Intinerary.NumMicroOps << ", " <<
597 Intinerary.FirstStage << ", " <<
598 Intinerary.LastStage << ", " <<
599 Intinerary.FirstOperandCycle << ", " <<
600 Intinerary.LastOperandCycle << " }" <<
601 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
602 }
603 // End processor itinerary table
604 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
605 OS << "};\n";
606 }
607}
608
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000609// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000610// value defined in the C++ header. The Record is null if the processor does not
611// define a model.
612void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trick73d77362012-06-05 03:44:40 +0000613 const char *Name, char Separator) {
614 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000615 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000616 if (V >= 0)
617 OS << V << Separator << " // " << Name;
618 else
Andrew Trick87255e32012-07-07 04:00:00 +0000619 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000620 OS << '\n';
621}
622
Andrew Trick23f3c652012-09-17 22:18:45 +0000623void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
624 raw_ostream &OS) {
625 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
626
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000627 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000628 OS << "static const llvm::MCProcResourceDesc "
629 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000630 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000631
632 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
633 Record *PRDef = ProcModel.ProcResourceDefs[i];
634
Craig Topper24064772014-04-15 07:20:03 +0000635 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000636 unsigned SuperIdx = 0;
637 unsigned NumUnits = 0;
Andrew Trick40c4f382013-06-15 04:50:06 +0000638 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000639 if (PRDef->isSubClassOf("ProcResGroup")) {
640 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
641 for (RecIter RUI = ResUnits.begin(), RUE = ResUnits.end();
642 RUI != RUE; ++RUI) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000643 NumUnits += (*RUI)->getValueAsInt("NumUnits");
644 }
645 }
646 else {
647 // Find the SuperIdx
648 if (PRDef->getValueInit("Super")->isComplete()) {
649 SuperDef = SchedModels.findProcResUnits(
650 PRDef->getValueAsDef("Super"), ProcModel);
651 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
652 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000653 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000654 }
655 // Emit the ProcResourceDesc
656 if (i+1 == e)
657 Sep = ' ';
658 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
659 if (PRDef->getName().size() < 15)
660 OS.indent(15 - PRDef->getName().size());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000661 OS << NumUnits << ", " << SuperIdx << ", "
Andrew Trickde2109e2013-06-15 04:49:57 +0000662 << BufferSize << "}" << Sep << " // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000663 if (SuperDef)
664 OS << ", Super=" << SuperDef->getName();
665 OS << "\n";
666 }
667 OS << "};\n";
668}
669
Andrew Trick9ef08822012-09-17 22:18:48 +0000670// Find the WriteRes Record that defines processor resources for this
671// SchedWrite.
672Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000673 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000674
675 // Check if the SchedWrite is already subtarget-specific and directly
676 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000677 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
678 return SchedWrite.TheDef;
679
Craig Topper24064772014-04-15 07:20:03 +0000680 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000681 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
682 AI != AE; ++AI) {
683 const CodeGenSchedRW &AliasRW =
684 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000685 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
686 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
687 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
688 continue;
689 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000690 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000691 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000692 "defined for processor " + ProcModel.ModelName +
693 " Ensure only one SchedAlias exists per RW.");
694 AliasDef = AliasRW.TheDef;
695 }
696 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
697 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000698
699 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000700 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000701 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
702 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
703 if (!(*WRI)->isSubClassOf("WriteRes"))
704 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000705 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
706 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
707 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000708 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000709 "SchedWrite and its alias on processor " +
710 ProcModel.ModelName);
711 }
712 ResDef = *WRI;
713 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000714 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000715 // TODO: If ProcModel has a base model (previous generation processor),
716 // then call FindWriteResources recursively with that model here.
717 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000718 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9257b8f2012-09-22 02:24:21 +0000719 std::string("Processor does not define resources for ")
720 + SchedWrite.TheDef->getName());
721 }
722 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000723}
724
725/// Find the ReadAdvance record for the given SchedRead on this processor or
726/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000727Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000728 const CodeGenProcModel &ProcModel) {
729 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000730 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
731 return SchedRead.TheDef;
732
733 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000734 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000735 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
736 AI != AE; ++AI) {
737 const CodeGenSchedRW &AliasRW =
738 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000739 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
740 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
741 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
742 continue;
743 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000744 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000745 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000746 "defined for processor " + ProcModel.ModelName +
747 " Ensure only one SchedAlias exists per RW.");
748 AliasDef = AliasRW.TheDef;
749 }
750 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
751 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000752
753 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000754 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000755 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
756 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
757 if (!(*RAI)->isSubClassOf("ReadAdvance"))
758 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000759 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
760 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
761 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000762 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000763 "SchedRead and its alias on processor " +
764 ProcModel.ModelName);
765 }
766 ResDef = *RAI;
767 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000768 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000769 // TODO: If ProcModel has a base model (previous generation processor),
770 // then call FindReadAdvance recursively with that model here.
771 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000772 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9ef08822012-09-17 22:18:48 +0000773 std::string("Processor does not define resources for ")
Andrew Trick9257b8f2012-09-22 02:24:21 +0000774 + SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000775 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000776 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000777}
778
Andrew Trick4e67cba2013-03-14 21:21:50 +0000779// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000780// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000781void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
782 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000783 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000784 // Default to 1 resource cycle.
785 Cycles.resize(PRVec.size(), 1);
786 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000787 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000788 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000789 if (PRDef->isSubClassOf("ProcResGroup"))
790 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000791 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000792 SubResources.push_back(PRDef);
793 PRDef = SchedModels.findProcResUnits(PRVec[i], PM);
794 for (Record *SubDef = PRDef;
795 SubDef->getValueInit("Super")->isComplete();) {
796 if (SubDef->isSubClassOf("ProcResGroup")) {
797 // Disallow this for simplicitly.
798 PrintFatalError(SubDef->getLoc(), "Processor resource group "
799 " cannot be a super resources.");
800 }
801 Record *SuperDef =
802 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM);
803 PRVec.push_back(SuperDef);
804 Cycles.push_back(Cycles[i]);
805 SubDef = SuperDef;
806 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000807 }
Andrew Tricka3801a32013-04-23 23:45:16 +0000808 for (RecIter PRI = PM.ProcResourceDefs.begin(),
809 PRE = PM.ProcResourceDefs.end();
Andrew Trick4e67cba2013-03-14 21:21:50 +0000810 PRI != PRE; ++PRI) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000811 if (*PRI == PRDef || !(*PRI)->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000812 continue;
813 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000814 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000815 for( ; SubI != SubE; ++SubI) {
816 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
817 == SuperResources.end()) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000818 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000819 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000820 }
821 if (SubI == SubE) {
822 PRVec.push_back(*PRI);
823 Cycles.push_back(Cycles[i]);
824 }
825 }
826 }
827}
828
Andrew Trick9ef08822012-09-17 22:18:48 +0000829// Generate the SchedClass table for this processor and update global
830// tables. Must be called for each processor in order.
831void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
832 SchedClassTables &SchedTables) {
833 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
834 if (!ProcModel.hasInstrSchedModel())
835 return;
836
837 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
838 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
839 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trick7aba6be2012-10-03 23:06:25 +0000840 DEBUG(SCI->dump(&SchedModels));
841
Andrew Trick9ef08822012-09-17 22:18:48 +0000842 SCTab.resize(SCTab.size() + 1);
843 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000844 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000845 SCDesc.NumMicroOps = 0;
846 SCDesc.BeginGroup = false;
847 SCDesc.EndGroup = false;
848 SCDesc.WriteProcResIdx = 0;
849 SCDesc.WriteLatencyIdx = 0;
850 SCDesc.ReadAdvanceIdx = 0;
851
852 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000853 bool HasVariants = false;
854 for (std::vector<CodeGenSchedTransition>::const_iterator
855 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
856 TI != TE; ++TI) {
857 if (TI->ProcIndices[0] == 0) {
858 HasVariants = true;
859 break;
860 }
861 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
862 TI->ProcIndices.end(), ProcModel.Index);
863 if (PIPos != TI->ProcIndices.end()) {
864 HasVariants = true;
865 break;
866 }
867 }
868 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000869 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
870 continue;
871 }
872
873 // Determine if the SchedClass is actually reachable on this processor. If
874 // not don't try to locate the processor resources, it will fail.
875 // If ProcIndices contains 0, this class applies to all processors.
876 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
877 if (SCI->ProcIndices[0] != 0) {
878 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
879 SCI->ProcIndices.end(), ProcModel.Index);
880 if (PIPos == SCI->ProcIndices.end())
881 continue;
882 }
883 IdxVec Writes = SCI->Writes;
884 IdxVec Reads = SCI->Reads;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000885 if (!SCI->InstRWs.empty()) {
886 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000887 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000888 Record *RWDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000889 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
890 RWI != RWE; ++RWI) {
891 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
892 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
893 RWDef = *RWI;
894 break;
895 }
896 }
897 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000898 Writes.clear();
899 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000900 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
901 Writes, Reads);
902 }
903 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000904 if (Writes.empty()) {
905 // Check this processor's itinerary class resources.
906 for (RecIter II = ProcModel.ItinRWDefs.begin(),
907 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
908 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
909 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
910 != Matched.end()) {
911 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
912 Writes, Reads);
913 break;
914 }
915 }
916 if (Writes.empty()) {
917 DEBUG(dbgs() << ProcModel.ModelName
918 << " does not have resources for class " << SCI->Name << '\n');
919 }
920 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000921 // Sum resources across all operand writes.
922 std::vector<MCWriteProcResEntry> WriteProcResources;
923 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000924 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000925 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
926 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
927 IdxVec WriteSeq;
Andrew Trickda984b12012-10-03 23:06:28 +0000928 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
929 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000930
931 // For each operand, create a latency entry.
932 MCWriteLatencyEntry WLEntry;
933 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000934 unsigned WriteID = WriteSeq.back();
935 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
936 // If this Write is not referenced by a ReadAdvance, don't distinguish it
937 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000938 if (!SchedModels.hasReadOfWrite(
939 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000940 WriteID = 0;
941 }
942 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000943
944 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
945 WSI != WSE; ++WSI) {
946
Andrew Trick9257b8f2012-09-22 02:24:21 +0000947 Record *WriteRes =
948 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000949
950 // Mark the parent class as invalid for unsupported write types.
951 if (WriteRes->getValueAsBit("Unsupported")) {
952 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
953 break;
954 }
955 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
956 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
957 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
958 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
959
960 // Create an entry for each ProcResource listed in WriteRes.
961 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
962 std::vector<int64_t> Cycles =
963 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000964
965 ExpandProcResources(PRVec, Cycles, ProcModel);
966
Andrew Trick9ef08822012-09-17 22:18:48 +0000967 for (unsigned PRIdx = 0, PREnd = PRVec.size();
968 PRIdx != PREnd; ++PRIdx) {
969 MCWriteProcResEntry WPREntry;
970 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
971 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000972 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000973 // If this resource is already used in this sequence, add the current
974 // entry's cycles so that the same resource appears to be used
975 // serially, rather than multiple parallel uses. This is important for
976 // in-order machine where the resource consumption is a hazard.
977 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
978 for( ; WPRIdx != WPREnd; ++WPRIdx) {
979 if (WriteProcResources[WPRIdx].ProcResourceIdx
980 == WPREntry.ProcResourceIdx) {
981 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
982 break;
983 }
984 }
985 if (WPRIdx == WPREnd)
986 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000987 }
988 }
989 WriteLatencies.push_back(WLEntry);
990 }
991 // Create an entry for each operand Read in this SchedClass.
992 // Entries must be sorted first by UseIdx then by WriteResourceID.
993 for (unsigned UseIdx = 0, EndIdx = Reads.size();
994 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000995 Record *ReadAdvance =
996 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000997 if (!ReadAdvance)
998 continue;
999
1000 // Mark the parent class as invalid for unsupported write types.
1001 if (ReadAdvance->getValueAsBit("Unsupported")) {
1002 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1003 break;
1004 }
1005 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1006 IdxVec WriteIDs;
1007 if (ValidWrites.empty())
1008 WriteIDs.push_back(0);
1009 else {
1010 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
1011 VWI != VWE; ++VWI) {
1012 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
1013 }
1014 }
1015 std::sort(WriteIDs.begin(), WriteIDs.end());
1016 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
1017 MCReadAdvanceEntry RAEntry;
1018 RAEntry.UseIdx = UseIdx;
1019 RAEntry.WriteResourceID = *WI;
1020 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1021 ReadAdvanceEntries.push_back(RAEntry);
1022 }
1023 }
1024 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1025 WriteProcResources.clear();
1026 WriteLatencies.clear();
1027 ReadAdvanceEntries.clear();
1028 }
1029 // Add the information for this SchedClass to the global tables using basic
1030 // compression.
1031 //
1032 // WritePrecRes entries are sorted by ProcResIdx.
1033 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1034 LessWriteProcResources());
1035
1036 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1037 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1038 std::search(SchedTables.WriteProcResources.begin(),
1039 SchedTables.WriteProcResources.end(),
1040 WriteProcResources.begin(), WriteProcResources.end());
1041 if (WPRPos != SchedTables.WriteProcResources.end())
1042 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1043 else {
1044 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1045 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1046 WriteProcResources.end());
1047 }
1048 // Latency entries must remain in operand order.
1049 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1050 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1051 std::search(SchedTables.WriteLatencies.begin(),
1052 SchedTables.WriteLatencies.end(),
1053 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001054 if (WLPos != SchedTables.WriteLatencies.end()) {
1055 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1056 SCDesc.WriteLatencyIdx = idx;
1057 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1058 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1059 std::string::npos) {
1060 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1061 }
1062 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001063 else {
1064 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001065 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1066 WriteLatencies.begin(),
1067 WriteLatencies.end());
1068 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1069 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001070 }
1071 // ReadAdvanceEntries must remain in operand order.
1072 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1073 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1074 std::search(SchedTables.ReadAdvanceEntries.begin(),
1075 SchedTables.ReadAdvanceEntries.end(),
1076 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1077 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1078 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1079 else {
1080 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1081 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1082 ReadAdvanceEntries.end());
1083 }
1084 }
1085}
1086
Andrew Tricka72fca62012-09-17 22:18:50 +00001087// Emit SchedClass tables for all processors and associated global tables.
1088void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1089 raw_ostream &OS) {
1090 // Emit global WriteProcResTable.
1091 OS << "\n// {ProcResourceIdx, Cycles}\n"
1092 << "extern const llvm::MCWriteProcResEntry "
1093 << Target << "WriteProcResTable[] = {\n"
1094 << " { 0, 0}, // Invalid\n";
1095 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1096 WPRIdx != WPREnd; ++WPRIdx) {
1097 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1098 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1099 << format("%2d", WPREntry.Cycles) << "}";
1100 if (WPRIdx + 1 < WPREnd)
1101 OS << ',';
1102 OS << " // #" << WPRIdx << '\n';
1103 }
1104 OS << "}; // " << Target << "WriteProcResTable\n";
1105
1106 // Emit global WriteLatencyTable.
1107 OS << "\n// {Cycles, WriteResourceID}\n"
1108 << "extern const llvm::MCWriteLatencyEntry "
1109 << Target << "WriteLatencyTable[] = {\n"
1110 << " { 0, 0}, // Invalid\n";
1111 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1112 WLIdx != WLEnd; ++WLIdx) {
1113 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1114 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1115 << format("%2d", WLEntry.WriteResourceID) << "}";
1116 if (WLIdx + 1 < WLEnd)
1117 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001118 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001119 }
1120 OS << "}; // " << Target << "WriteLatencyTable\n";
1121
1122 // Emit global ReadAdvanceTable.
1123 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1124 << "extern const llvm::MCReadAdvanceEntry "
1125 << Target << "ReadAdvanceTable[] = {\n"
1126 << " {0, 0, 0}, // Invalid\n";
1127 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1128 RAIdx != RAEnd; ++RAIdx) {
1129 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1130 OS << " {" << RAEntry.UseIdx << ", "
1131 << format("%2d", RAEntry.WriteResourceID) << ", "
1132 << format("%2d", RAEntry.Cycles) << "}";
1133 if (RAIdx + 1 < RAEnd)
1134 OS << ',';
1135 OS << " // #" << RAIdx << '\n';
1136 }
1137 OS << "}; // " << Target << "ReadAdvanceTable\n";
1138
1139 // Emit a SchedClass table for each processor.
1140 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1141 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1142 if (!PI->hasInstrSchedModel())
1143 continue;
1144
1145 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001146 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001147
1148 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1149 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1150 OS << "static const llvm::MCSchedClassDesc "
1151 << PI->ModelName << "SchedClasses[] = {\n";
1152
1153 // The first class is always invalid. We no way to distinguish it except by
1154 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001155 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001156 && "invalid class not first");
1157 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1158 << MCSchedClassDesc::InvalidNumMicroOps
1159 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1160
1161 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1162 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1163 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1164 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1165 if (SchedClass.Name.size() < 18)
1166 OS.indent(18 - SchedClass.Name.size());
1167 OS << MCDesc.NumMicroOps
1168 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1169 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1170 << ", " << MCDesc.NumWriteProcResEntries
1171 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1172 << ", " << MCDesc.NumWriteLatencyEntries
1173 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1174 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1175 if (SCIdx + 1 < SCEnd)
1176 OS << ',';
1177 OS << " // #" << SCIdx << '\n';
1178 }
1179 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1180 }
1181}
1182
Andrew Trick87255e32012-07-07 04:00:00 +00001183void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1184 // For each processor model.
1185 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1186 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001187 // Emit processor resource table.
1188 if (PI->hasInstrSchedModel())
1189 EmitProcessorResources(*PI, OS);
1190 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001191 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001192 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001193
Andrew Trick73d77362012-06-05 03:44:40 +00001194 // Begin processor itinerary properties
1195 OS << "\n";
Pete Cooper11759452014-09-02 17:43:54 +00001196 OS << "static const llvm::MCSchedModel " << PI->ModelName << " = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001197 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
Andrew Trickde2109e2013-06-15 04:49:57 +00001198 EmitProcessorProp(OS, PI->ModelDef, "MicroOpBufferSize", ',');
Hal Finkel6532c202014-05-08 09:14:44 +00001199 EmitProcessorProp(OS, PI->ModelDef, "LoopMicroOpBufferSize", ',');
Andrew Trick87255e32012-07-07 04:00:00 +00001200 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1201 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick352abc12012-08-08 02:44:16 +00001202 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001203
1204 OS << " " << (bool)(PI->ModelDef ?
Sanjay Patela2f658d2014-07-15 22:39:58 +00001205 PI->ModelDef->getValueAsBit("PostRAScheduler") : 0)
1206 << ", // " << "PostRAScheduler\n";
1207
1208 OS << " " << (bool)(PI->ModelDef ?
Andrew Trickb6854d82013-09-25 18:14:12 +00001209 PI->ModelDef->getValueAsBit("CompleteModel") : 0)
1210 << ", // " << "CompleteModel\n";
1211
Andrew Trickab722bd2012-09-18 03:18:56 +00001212 OS << " " << PI->Index << ", // Processor ID\n";
1213 if (PI->hasInstrSchedModel())
1214 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1215 << " " << PI->ModelName << "SchedClasses" << ",\n"
1216 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1217 << " " << (SchedModels.schedClassEnd()
1218 - SchedModels.schedClassBegin()) << ",\n";
1219 else
1220 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Pete Cooper11759452014-09-02 17:43:54 +00001221 if (PI->hasItineraries())
1222 OS << " " << PI->ItinsDef->getName() << "};\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001223 else
Pete Cooper11759452014-09-02 17:43:54 +00001224 OS << " nullptr}; // No Itinerary\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001225 }
Jim Laskey3763a502005-10-31 17:16:01 +00001226}
1227
1228//
1229// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1230//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001231void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001232 // Gather and sort processor information
1233 std::vector<Record*> ProcessorList =
1234 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001235 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001236
1237 // Begin processor table
1238 OS << "\n";
1239 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001240 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001241 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001242
Jim Laskey3763a502005-10-31 17:16:01 +00001243 // For each processor
1244 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1245 // Next processor
1246 Record *Processor = ProcessorList[i];
1247
Bill Wendlinge6182262007-05-04 20:38:40 +00001248 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001249 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001250 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001251
Jim Laskey3763a502005-10-31 17:16:01 +00001252 // Emit as { "cpu", procinit },
Andrew Trick23f3c652012-09-17 22:18:45 +00001253 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001254
Jim Laskey3763a502005-10-31 17:16:01 +00001255 // Depending on ''if more in the list'' emit comma
1256 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001257
Jim Laskey3763a502005-10-31 17:16:01 +00001258 OS << "\n";
1259 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001260
Jim Laskey3763a502005-10-31 17:16:01 +00001261 // End processor table
1262 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001263}
1264
1265//
Andrew Trick87255e32012-07-07 04:00:00 +00001266// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001267//
Andrew Trick87255e32012-07-07 04:00:00 +00001268void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001269 OS << "#ifdef DBGFIELD\n"
1270 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1271 << "#endif\n"
1272 << "#ifndef NDEBUG\n"
1273 << "#define DBGFIELD(x) x,\n"
1274 << "#else\n"
1275 << "#define DBGFIELD(x)\n"
1276 << "#endif\n";
1277
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001278 if (SchedModels.hasItineraries()) {
Andrew Trick87255e32012-07-07 04:00:00 +00001279 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001280 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001281 EmitStageAndOperandCycleData(OS, ProcItinLists);
1282 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001283 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001284 OS << "\n// ===============================================================\n"
1285 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001286
Andrew Trick9ef08822012-09-17 22:18:48 +00001287 SchedClassTables SchedTables;
1288 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1289 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1290 GenSchedClassTables(*PI, SchedTables);
1291 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001292 EmitSchedClassTables(SchedTables, OS);
1293
1294 // Emit the processor machine model
1295 EmitProcessorModels(OS);
1296 // Emit the processor lookup data
1297 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001298
Andrew Trick23f3c652012-09-17 22:18:45 +00001299 OS << "#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001300}
1301
Andrew Trickc6c88152012-09-18 03:41:43 +00001302void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1303 raw_ostream &OS) {
1304 OS << "unsigned " << ClassName
1305 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1306 << " const TargetSchedModel *SchedModel) const {\n";
1307
1308 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1309 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1310 for (std::vector<Record*>::const_iterator
1311 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1312 OS << (*PI)->getValueAsString("Code") << '\n';
1313 }
1314 IdxVec VariantClasses;
1315 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1316 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1317 if (SCI->Transitions.empty())
1318 continue;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001319 VariantClasses.push_back(SCI->Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001320 }
1321 if (!VariantClasses.empty()) {
1322 OS << " switch (SchedClass) {\n";
1323 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1324 VCI != VCE; ++VCI) {
1325 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1326 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1327 IdxVec ProcIndices;
1328 for (std::vector<CodeGenSchedTransition>::const_iterator
1329 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1330 TI != TE; ++TI) {
1331 IdxVec PI;
1332 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1333 ProcIndices.begin(), ProcIndices.end(),
1334 std::back_inserter(PI));
1335 ProcIndices.swap(PI);
1336 }
1337 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1338 PI != PE; ++PI) {
1339 OS << " ";
1340 if (*PI != 0)
1341 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1342 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1343 << '\n';
1344 for (std::vector<CodeGenSchedTransition>::const_iterator
1345 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1346 TI != TE; ++TI) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001347 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1348 TI->ProcIndices.end(), *PI)) {
1349 continue;
1350 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001351 OS << " if (";
Andrew Trickc6c88152012-09-18 03:41:43 +00001352 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1353 RI != RE; ++RI) {
1354 if (RI != TI->PredTerm.begin())
1355 OS << "\n && ";
1356 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1357 }
1358 OS << ")\n"
1359 << " return " << TI->ToClassIdx << "; // "
1360 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1361 }
1362 OS << " }\n";
1363 if (*PI == 0)
1364 break;
1365 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001366 if (SC.isInferred())
1367 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001368 OS << " break;\n";
1369 }
1370 OS << " };\n";
1371 }
1372 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1373 << "} // " << ClassName << "::resolveSchedClass\n";
1374}
1375
Jim Laskey86f002c2005-10-27 19:47:21 +00001376//
Jim Laskeya2b52352005-10-26 17:30:34 +00001377// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1378// the subtarget features string.
1379//
Evan Cheng54b68e32011-07-01 20:45:01 +00001380void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1381 unsigned NumFeatures,
1382 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001383 std::vector<Record*> Features =
1384 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001385 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001386
Andrew Trickdb6ed642011-04-01 01:56:55 +00001387 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1388 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001389 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001390 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001391 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001392 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001393 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001394
1395 if (Features.empty()) {
1396 OS << "}\n";
1397 return;
1398 }
1399
Andrew Trickba7b9212012-09-18 05:33:15 +00001400 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001401 << " uint64_t Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001402
Jim Laskeydffe5972005-10-28 21:47:29 +00001403 for (unsigned i = 0; i < Features.size(); i++) {
1404 // Next record
1405 Record *R = Features[i];
Bill Wendlinge6182262007-05-04 20:38:40 +00001406 const std::string &Instance = R->getName();
1407 const std::string &Value = R->getValueAsString("Value");
1408 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001409
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001410 if (Value=="true" || Value=="false")
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001411 OS << " if ((Bits & " << Target << "::"
1412 << Instance << ") != 0) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001413 << Attribute << " = " << Value << ";\n";
1414 else
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001415 OS << " if ((Bits & " << Target << "::"
1416 << Instance << ") != 0 && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001417 << Attribute << " < " << Value << ") "
1418 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001419 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001420
Evan Chengfe6e4052011-06-30 01:53:36 +00001421 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001422}
1423
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001424//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001425// SubtargetEmitter::run - Main subtarget enumeration emitter.
1426//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001427void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001428 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001429
Evan Cheng4d1ca962011-07-08 01:53:10 +00001430 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1431 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1432
1433 OS << "namespace llvm {\n";
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001434 Enumeration(OS, "SubtargetFeature", true);
Evan Cheng4d1ca962011-07-08 01:53:10 +00001435 OS << "} // End llvm namespace \n";
1436 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1437
Evan Cheng54b68e32011-07-01 20:45:01 +00001438 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1439 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001440
Evan Cheng54b68e32011-07-01 20:45:01 +00001441 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001442#if 0
1443 OS << "namespace {\n";
1444#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001445 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001446 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001447 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001448 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001449 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001450 OS << "\n";
1451#if 0
1452 OS << "}\n";
1453#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001454
1455 // MCInstrInfo initialization routine.
1456 OS << "static inline void Init" << Target
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001457 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1458 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1459 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001460 if (NumFeatures)
1461 OS << Target << "FeatureKV, ";
1462 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001463 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001464 if (NumProcs)
1465 OS << Target << "SubTypeKV, ";
1466 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001467 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001468 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001469 OS << Target << "ProcSchedKV, "
1470 << Target << "WriteProcResTable, "
1471 << Target << "WriteLatencyTable, "
1472 << Target << "ReadAdvanceTable, ";
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001473 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001474 OS << '\n'; OS.indent(22);
1475 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001476 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001477 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001478 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001479 OS << "0, 0, 0";
1480 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001481
1482 OS << "} // End llvm namespace \n";
1483
1484 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1485
1486 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1487 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1488
1489 OS << "#include \"llvm/Support/Debug.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001490 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1491
1492 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1493
Evan Cheng0d639a22011-07-01 21:01:15 +00001494 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001495 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1496 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1497
1498 std::string ClassName = Target + "GenSubtargetInfo";
1499 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001500 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001501 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng1a72add62011-07-07 07:07:08 +00001502 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1503 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001504 << "public:\n"
Andrew Trickc6c88152012-09-18 03:41:43 +00001505 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001506 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001507 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001508 << " const;\n"
Evan Cheng54b68e32011-07-01 20:45:01 +00001509 << "};\n";
1510 OS << "} // End llvm namespace \n";
1511
1512 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1513
1514 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1515 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1516
Andrew Trick1188e432012-09-18 03:32:57 +00001517 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001518 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001519 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1520 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001521 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1522 OS << "extern const llvm::MCWriteProcResEntry "
1523 << Target << "WriteProcResTable[];\n";
1524 OS << "extern const llvm::MCWriteLatencyEntry "
1525 << Target << "WriteLatencyTable[];\n";
1526 OS << "extern const llvm::MCReadAdvanceEntry "
1527 << Target << "ReadAdvanceTable[];\n";
1528
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001529 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001530 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1531 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001532 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001533 }
1534
Evan Cheng1a72add62011-07-07 07:07:08 +00001535 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1536 << "StringRef FS)\n"
Evan Cheng0d639a22011-07-01 21:01:15 +00001537 << " : TargetSubtargetInfo() {\n"
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001538 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001539 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001540 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001541 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001542 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001543 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001544 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001545 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001546 OS << "None, ";
Andrew Trickab722bd2012-09-18 03:18:56 +00001547 OS << '\n'; OS.indent(22);
1548 OS << Target << "ProcSchedKV, "
1549 << Target << "WriteProcResTable, "
1550 << Target << "WriteLatencyTable, "
1551 << Target << "ReadAdvanceTable, ";
1552 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001553 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001554 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001555 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001556 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001557 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001558 OS << "0, 0, 0";
1559 OS << ");\n}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001560
Andrew Trickc6c88152012-09-18 03:41:43 +00001561 EmitSchedModelHelpers(ClassName, OS);
1562
Evan Cheng54b68e32011-07-01 20:45:01 +00001563 OS << "} // End llvm namespace \n";
1564
1565 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001566}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001567
1568namespace llvm {
1569
1570void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001571 CodeGenTarget CGTarget(RK);
1572 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001573}
1574
1575} // End llvm namespace