blob: e5c62fa13751bc59fe12967c5271c555e0487034 [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
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000131 // Open enumeration. Use a 64-bit underlying type.
132 OS << "enum : uint64_t {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000133
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000134 // For each record
135 for (unsigned i = 0; i < N;) {
136 // Next record
137 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000138
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000139 // Get and emit name
140 OS << " " << Def->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000141
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000142 // If bit flags then emit expression (1 << i)
143 if (isBits) OS << " = " << " 1ULL << " << i;
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000144
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000145 // Depending on 'if more in the list' emit comma
146 if (++i < N) OS << ",";
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000147
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000148 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000149 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000150
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000151 // Close enumeration
152 OS << "};\n";
153
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000154 OS << "}\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000155}
156
157//
Bill Wendlinge6182262007-05-04 20:38:40 +0000158// FeatureKeyValues - Emit data of all the subtarget features. Used by the
159// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000160//
Evan Cheng54b68e32011-07-01 20:45:01 +0000161unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000162 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000163 std::vector<Record*> FeatureList =
164 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000165
166 if (FeatureList.empty())
167 return 0;
168
Jim Grosbach56938af2008-09-11 17:05:32 +0000169 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000170
Jim Laskey19595752005-10-28 15:20:43 +0000171 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000172 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000173 << "extern const llvm::SubtargetFeatureKV " << Target
174 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000175
Jim Laskey19595752005-10-28 15:20:43 +0000176 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000177 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000178 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000179 // Next feature
180 Record *Feature = FeatureList[i];
181
Bill Wendlinge6182262007-05-04 20:38:40 +0000182 const std::string &Name = Feature->getName();
183 const std::string &CommandLineName = Feature->getValueAsString("Name");
184 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000185
Jim Laskey3f7d0472006-12-12 20:55:58 +0000186 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000187
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000188 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000189 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000190 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000191 << "\"" << Desc << "\", "
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000192 << Target << "::" << Name << ", ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000193
Andrew Trickdb6ed642011-04-01 01:56:55 +0000194 const std::vector<Record*> &ImpliesList =
Bill Wendlinge6182262007-05-04 20:38:40 +0000195 Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000196
Bill Wendlinge6182262007-05-04 20:38:40 +0000197 if (ImpliesList.empty()) {
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000198 OS << "0ULL";
Bill Wendlinge6182262007-05-04 20:38:40 +0000199 } else {
200 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000201 OS << Target << "::" << ImpliesList[j]->getName();
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000202 if (++j < M) OS << " | ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000203 }
204 }
205
206 OS << " }";
Evan Cheng54b68e32011-07-01 20:45:01 +0000207 ++NumFeatures;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000208
Jim Laskey3763a502005-10-31 17:16:01 +0000209 // Depending on 'if more in the list' emit comma
Jim Laskey3f7d0472006-12-12 20:55:58 +0000210 if ((i + 1) < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000211
Jim Laskeydffe5972005-10-28 21:47:29 +0000212 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000213 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000214
Jim Laskey19595752005-10-28 15:20:43 +0000215 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000216 OS << "};\n";
217
Evan Cheng54b68e32011-07-01 20:45:01 +0000218 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000219}
220
221//
222// CPUKeyValues - Emit data of all the subtarget processors. Used by command
223// line.
224//
Evan Cheng54b68e32011-07-01 20:45:01 +0000225unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000226 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000227 std::vector<Record*> ProcessorList =
228 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000229 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000230
Jim Laskey19595752005-10-28 15:20:43 +0000231 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000232 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000233 << "extern const llvm::SubtargetFeatureKV " << Target
234 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000235
Jim Laskey19595752005-10-28 15:20:43 +0000236 // For each processor
Jim Laskeydffe5972005-10-28 21:47:29 +0000237 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
238 // Next processor
239 Record *Processor = ProcessorList[i];
240
Bill Wendlinge6182262007-05-04 20:38:40 +0000241 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000242 const std::vector<Record*> &FeatureList =
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000243 Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000244
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000245 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000246 OS << " { "
247 << "\"" << Name << "\", "
248 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000249
Jim Laskeydffe5972005-10-28 21:47:29 +0000250 if (FeatureList.empty()) {
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000251 OS << "0ULL";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000252 } else {
Jim Laskeydffe5972005-10-28 21:47:29 +0000253 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng54b68e32011-07-01 20:45:01 +0000254 OS << Target << "::" << FeatureList[j]->getName();
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000255 if (++j < M) OS << " | ";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000256 }
257 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000258
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000259 // The "0" is for the "implies" section of this data structure.
260 OS << ", 0ULL }";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000261
Jim Laskey3763a502005-10-31 17:16:01 +0000262 // Depending on 'if more in the list' emit comma
Jim Laskeydffe5972005-10-28 21:47:29 +0000263 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000264
Jim Laskeydffe5972005-10-28 21:47:29 +0000265 OS << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000266 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000267
Jim Laskey19595752005-10-28 15:20:43 +0000268 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000269 OS << "};\n";
270
Evan Cheng54b68e32011-07-01 20:45:01 +0000271 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000272}
Jim Laskeya1beea62005-10-22 07:59:56 +0000273
Jim Laskeya2b52352005-10-26 17:30:34 +0000274//
David Goodwind813cbf2009-08-17 16:02:57 +0000275// FormItineraryStageString - Compose a string containing the stage
276// data initialization for the specified itinerary. N is the number
277// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000278//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000279void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
280 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000281 std::string &ItinString,
282 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000283 // Get states list
Bill Wendlinge6182262007-05-04 20:38:40 +0000284 const std::vector<Record*> &StageList =
285 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000286
287 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000288 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000289 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000290 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000291 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000292
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000293 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000294 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000295 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000296
Jim Laskeydffe5972005-10-28 21:47:29 +0000297 // Get unit list
Bill Wendlinge6182262007-05-04 20:38:40 +0000298 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000299
Jim Laskey19595752005-10-28 15:20:43 +0000300 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000301 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000302 // Add name and bitwise or
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000303 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeydffe5972005-10-28 21:47:29 +0000304 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000305 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000306
David Goodwinb369ee42009-08-12 18:31:53 +0000307 int TimeInc = Stage->getValueAsInt("TimeInc");
308 ItinString += ", " + itostr(TimeInc);
309
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000310 int Kind = Stage->getValueAsInt("Kind");
311 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
312
Jim Laskey19595752005-10-28 15:20:43 +0000313 // Close off stage
314 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000315 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000316 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000317}
318
319//
David Goodwind813cbf2009-08-17 16:02:57 +0000320// FormItineraryOperandCycleString - Compose a string containing the
321// operand cycle initialization for the specified itinerary. N is the
322// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000323//
David Goodwind813cbf2009-08-17 16:02:57 +0000324void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
325 std::string &ItinString, unsigned &NOperandCycles) {
326 // Get operand cycle list
327 const std::vector<int64_t> &OperandCycleList =
328 ItinData->getValueAsListOfInts("OperandCycles");
329
330 // For each operand cycle
331 unsigned N = NOperandCycles = OperandCycleList.size();
332 for (unsigned i = 0; i < N;) {
333 // Next operand cycle
334 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000335
David Goodwind813cbf2009-08-17 16:02:57 +0000336 ItinString += " " + itostr(OCycle);
337 if (++i < N) ItinString += ", ";
338 }
339}
340
Evan Cheng0097dd02010-09-28 23:50:49 +0000341void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
342 Record *ItinData,
343 std::string &ItinString,
344 unsigned NOperandCycles) {
345 const std::vector<Record*> &BypassList =
346 ItinData->getValueAsListOfDefs("Bypasses");
347 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000348 unsigned i = 0;
349 for (; i < N;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000350 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000351 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000352 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000353 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000354 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000355 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000356 }
357}
358
David Goodwind813cbf2009-08-17 16:02:57 +0000359//
Andrew Trick87255e32012-07-07 04:00:00 +0000360// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
361// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
362// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000363//
Andrew Trick87255e32012-07-07 04:00:00 +0000364void SubtargetEmitter::
365EmitStageAndOperandCycleData(raw_ostream &OS,
366 std::vector<std::vector<InstrItinerary> >
367 &ProcItinLists) {
Jim Laskey19595752005-10-28 15:20:43 +0000368
Andrew Trickfb982dd2012-07-09 20:43:03 +0000369 // Multiple processor models may share an itinerary record. Emit it once.
370 SmallPtrSet<Record*, 8> ItinsDefSet;
371
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000372 // Emit functional units for all the itineraries.
Andrew Trick87255e32012-07-07 04:00:00 +0000373 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
374 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000375
David Blaikie70573dc2014-11-19 07:49:26 +0000376 if (!ItinsDefSet.insert(PI->ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000377 continue;
378
Andrew Trick87255e32012-07-07 04:00:00 +0000379 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000380 if (FUs.empty())
381 continue;
382
Andrew Trick87255e32012-07-07 04:00:00 +0000383 const std::string &Name = PI->ItinsDef->getName();
384 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000385 << "namespace " << Name << "FU {\n";
386
387 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000388 OS << " const unsigned " << FUs[j]->getName()
389 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000390
391 OS << "}\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000392
Andrew Trick87255e32012-07-07 04:00:00 +0000393 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000394 if (!BPs.empty()) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000395 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
396 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000397
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000398 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000399 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000400 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000401 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000402
Evan Cheng4a010fd2010-09-29 22:42:35 +0000403 OS << "}\n";
404 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000405 }
406
Jim Laskey19595752005-10-28 15:20:43 +0000407 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000408 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
409 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000410 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000411
David Goodwind813cbf2009-08-17 16:02:57 +0000412 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000413 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000414 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000415 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000416
417 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000418 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000419 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000420 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000421
Andrew Trick87255e32012-07-07 04:00:00 +0000422 // For each Itinerary across all processors, add a unique entry to the stages,
423 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
424 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000425 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000426 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick87255e32012-07-07 04:00:00 +0000427 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
428 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
429 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000430
Andrew Trick87255e32012-07-07 04:00:00 +0000431 // Add process itinerary to the list.
432 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000433
Andrew Trick87255e32012-07-07 04:00:00 +0000434 // If this processor defines no itineraries, then leave the itinerary list
435 // empty.
436 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000437 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000438 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000439
Andrew Trick87255e32012-07-07 04:00:00 +0000440 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000441
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000442 ItinList.resize(SchedModels.numInstrSchedClasses());
443 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
444
445 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000446 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
447
Jim Laskeydffe5972005-10-28 21:47:29 +0000448 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000449 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000450
Jim Laskey19595752005-10-28 15:20:43 +0000451 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000452 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000453 unsigned NStages = 0;
454 if (ItinData)
455 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000456
David Goodwind813cbf2009-08-17 16:02:57 +0000457 // Get string and operand cycle count
458 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000459 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000460 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000461 if (ItinData) {
462 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
463 NOperandCycles);
464
465 FormItineraryBypassString(Name, ItinData, ItinBypassString,
466 NOperandCycles);
467 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000468
David Goodwind813cbf2009-08-17 16:02:57 +0000469 // Check to see if stage already exists and create if it doesn't
470 unsigned FindStage = 0;
471 if (NStages > 0) {
472 FindStage = ItinStageMap[ItinStageString];
473 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000474 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
475 StageTable += ItinStageString + ", // " + itostr(StageCount);
476 if (NStages > 1)
477 StageTable += "-" + itostr(StageCount + NStages - 1);
478 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000479 // Record Itin class number.
480 ItinStageMap[ItinStageString] = FindStage = StageCount;
481 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000482 }
483 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000484
David Goodwind813cbf2009-08-17 16:02:57 +0000485 // Check to see if operand cycle already exists and create if it doesn't
486 unsigned FindOperandCycle = 0;
487 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000488 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
489 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000490 if (FindOperandCycle == 0) {
491 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000492 OperandCycleTable += ItinOperandCycleString + ", // ";
493 std::string OperandIdxComment = itostr(OperandCycleCount);
494 if (NOperandCycles > 1)
495 OperandIdxComment += "-"
496 + itostr(OperandCycleCount + NOperandCycles - 1);
497 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000498 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000499 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000500 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000501 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000502 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000503 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000504 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000505 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000506
Evan Cheng367a5df2010-09-09 18:18:55 +0000507 // Set up itinerary as location and location + stage count
Andrew Trick87255e32012-07-07 04:00:00 +0000508 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng367a5df2010-09-09 18:18:55 +0000509 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
510 FindOperandCycle,
511 FindOperandCycle + NOperandCycles};
512
Jim Laskey19595752005-10-28 15:20:43 +0000513 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000514 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000515 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000516 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000517
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000518 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000519 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000520 StageTable += "};\n";
521
522 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000523 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000524 OperandCycleTable += "};\n";
525
Andrew Trick87255e32012-07-07 04:00:00 +0000526 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000527 BypassTable += "};\n";
528
David Goodwind813cbf2009-08-17 16:02:57 +0000529 // Emit tables.
530 OS << StageTable;
531 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000532 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000533}
534
Andrew Trick87255e32012-07-07 04:00:00 +0000535//
536// EmitProcessorData - Generate data for processor itineraries that were
537// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
538// Itineraries for each processor. The Itinerary lists are indexed on
539// CodeGenSchedClass::Index.
540//
541void SubtargetEmitter::
542EmitItineraries(raw_ostream &OS,
543 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
544
Andrew Trickfb982dd2012-07-09 20:43:03 +0000545 // Multiple processor models may share an itinerary record. Emit it once.
546 SmallPtrSet<Record*, 8> ItinsDefSet;
547
Andrew Trick87255e32012-07-07 04:00:00 +0000548 // For each processor's machine model
549 std::vector<std::vector<InstrItinerary> >::iterator
550 ProcItinListsIter = ProcItinLists.begin();
551 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000552 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000553
Andrew Trick87255e32012-07-07 04:00:00 +0000554 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000555 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000556 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000557
558 // Get processor itinerary name
559 const std::string &Name = ItinsDef->getName();
560
561 // Get the itinerary list for the processor.
562 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000563 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000564
Pete Cooperc0eb1532014-09-02 23:23:34 +0000565 // Empty itineraries aren't referenced anywhere in the tablegen output
566 // so don't emit them.
567 if (ItinList.empty())
568 continue;
569
Andrew Trick87255e32012-07-07 04:00:00 +0000570 OS << "\n";
571 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000572
573 // Begin processor itinerary table
574 OS << Name << "[] = {\n";
575
576 // For each itinerary class in CodeGenSchedClass::Index order.
577 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
578 InstrItinerary &Intinerary = ItinList[j];
579
580 // Emit Itinerary in the form of
581 // { firstStage, lastStage, firstCycle, lastCycle } // index
582 OS << " { " <<
583 Intinerary.NumMicroOps << ", " <<
584 Intinerary.FirstStage << ", " <<
585 Intinerary.LastStage << ", " <<
586 Intinerary.FirstOperandCycle << ", " <<
587 Intinerary.LastOperandCycle << " }" <<
588 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
589 }
590 // End processor itinerary table
591 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
592 OS << "};\n";
593 }
594}
595
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000596// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000597// value defined in the C++ header. The Record is null if the processor does not
598// define a model.
599void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trick73d77362012-06-05 03:44:40 +0000600 const char *Name, char Separator) {
601 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000602 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000603 if (V >= 0)
604 OS << V << Separator << " // " << Name;
605 else
Andrew Trick87255e32012-07-07 04:00:00 +0000606 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000607 OS << '\n';
608}
609
Andrew Trick23f3c652012-09-17 22:18:45 +0000610void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
611 raw_ostream &OS) {
612 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
613
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000614 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000615 OS << "static const llvm::MCProcResourceDesc "
616 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Andrew Trick8e9c1d82012-10-10 05:43:04 +0000617 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000618
619 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
620 Record *PRDef = ProcModel.ProcResourceDefs[i];
621
Craig Topper24064772014-04-15 07:20:03 +0000622 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000623 unsigned SuperIdx = 0;
624 unsigned NumUnits = 0;
Andrew Trick40c4f382013-06-15 04:50:06 +0000625 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000626 if (PRDef->isSubClassOf("ProcResGroup")) {
627 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
628 for (RecIter RUI = ResUnits.begin(), RUE = ResUnits.end();
629 RUI != RUE; ++RUI) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000630 NumUnits += (*RUI)->getValueAsInt("NumUnits");
631 }
632 }
633 else {
634 // Find the SuperIdx
635 if (PRDef->getValueInit("Super")->isComplete()) {
636 SuperDef = SchedModels.findProcResUnits(
637 PRDef->getValueAsDef("Super"), ProcModel);
638 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
639 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000640 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000641 }
642 // Emit the ProcResourceDesc
643 if (i+1 == e)
644 Sep = ' ';
645 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
646 if (PRDef->getName().size() < 15)
647 OS.indent(15 - PRDef->getName().size());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000648 OS << NumUnits << ", " << SuperIdx << ", "
Andrew Trickde2109e2013-06-15 04:49:57 +0000649 << BufferSize << "}" << Sep << " // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000650 if (SuperDef)
651 OS << ", Super=" << SuperDef->getName();
652 OS << "\n";
653 }
654 OS << "};\n";
655}
656
Andrew Trick9ef08822012-09-17 22:18:48 +0000657// Find the WriteRes Record that defines processor resources for this
658// SchedWrite.
659Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000660 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000661
662 // Check if the SchedWrite is already subtarget-specific and directly
663 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000664 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
665 return SchedWrite.TheDef;
666
Craig Topper24064772014-04-15 07:20:03 +0000667 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000668 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
669 AI != AE; ++AI) {
670 const CodeGenSchedRW &AliasRW =
671 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000672 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
673 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
674 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
675 continue;
676 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000677 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000678 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000679 "defined for processor " + ProcModel.ModelName +
680 " Ensure only one SchedAlias exists per RW.");
681 AliasDef = AliasRW.TheDef;
682 }
683 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
684 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000685
686 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000687 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000688 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
689 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
690 if (!(*WRI)->isSubClassOf("WriteRes"))
691 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000692 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
693 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
694 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000695 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000696 "SchedWrite and its alias on processor " +
697 ProcModel.ModelName);
698 }
699 ResDef = *WRI;
700 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000701 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000702 // TODO: If ProcModel has a base model (previous generation processor),
703 // then call FindWriteResources recursively with that model here.
704 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000705 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9257b8f2012-09-22 02:24:21 +0000706 std::string("Processor does not define resources for ")
707 + SchedWrite.TheDef->getName());
708 }
709 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000710}
711
712/// Find the ReadAdvance record for the given SchedRead on this processor or
713/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000714Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000715 const CodeGenProcModel &ProcModel) {
716 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000717 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
718 return SchedRead.TheDef;
719
720 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000721 Record *AliasDef = nullptr;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000722 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
723 AI != AE; ++AI) {
724 const CodeGenSchedRW &AliasRW =
725 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000726 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
727 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
728 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
729 continue;
730 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000731 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000732 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000733 "defined for processor " + ProcModel.ModelName +
734 " Ensure only one SchedAlias exists per RW.");
735 AliasDef = AliasRW.TheDef;
736 }
737 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
738 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000739
740 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000741 Record *ResDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000742 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
743 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
744 if (!(*RAI)->isSubClassOf("ReadAdvance"))
745 continue;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000746 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
747 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
748 if (ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000749 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000750 "SchedRead and its alias on processor " +
751 ProcModel.ModelName);
752 }
753 ResDef = *RAI;
754 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000755 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000756 // TODO: If ProcModel has a base model (previous generation processor),
757 // then call FindReadAdvance recursively with that model here.
758 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000759 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick9ef08822012-09-17 22:18:48 +0000760 std::string("Processor does not define resources for ")
Andrew Trick9257b8f2012-09-22 02:24:21 +0000761 + SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000762 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000763 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000764}
765
Andrew Trick4e67cba2013-03-14 21:21:50 +0000766// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000767// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000768void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
769 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000770 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000771 // Default to 1 resource cycle.
772 Cycles.resize(PRVec.size(), 1);
773 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000774 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000775 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000776 if (PRDef->isSubClassOf("ProcResGroup"))
777 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000778 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000779 SubResources.push_back(PRDef);
780 PRDef = SchedModels.findProcResUnits(PRVec[i], PM);
781 for (Record *SubDef = PRDef;
782 SubDef->getValueInit("Super")->isComplete();) {
783 if (SubDef->isSubClassOf("ProcResGroup")) {
784 // Disallow this for simplicitly.
785 PrintFatalError(SubDef->getLoc(), "Processor resource group "
786 " cannot be a super resources.");
787 }
788 Record *SuperDef =
789 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM);
790 PRVec.push_back(SuperDef);
791 Cycles.push_back(Cycles[i]);
792 SubDef = SuperDef;
793 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000794 }
Andrew Tricka3801a32013-04-23 23:45:16 +0000795 for (RecIter PRI = PM.ProcResourceDefs.begin(),
796 PRE = PM.ProcResourceDefs.end();
Andrew Trick4e67cba2013-03-14 21:21:50 +0000797 PRI != PRE; ++PRI) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000798 if (*PRI == PRDef || !(*PRI)->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000799 continue;
800 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000801 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000802 for( ; SubI != SubE; ++SubI) {
803 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
804 == SuperResources.end()) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000805 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000806 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000807 }
808 if (SubI == SubE) {
809 PRVec.push_back(*PRI);
810 Cycles.push_back(Cycles[i]);
811 }
812 }
813 }
814}
815
Andrew Trick9ef08822012-09-17 22:18:48 +0000816// Generate the SchedClass table for this processor and update global
817// tables. Must be called for each processor in order.
818void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
819 SchedClassTables &SchedTables) {
820 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
821 if (!ProcModel.hasInstrSchedModel())
822 return;
823
824 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
825 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
826 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trick7aba6be2012-10-03 23:06:25 +0000827 DEBUG(SCI->dump(&SchedModels));
828
Andrew Trick9ef08822012-09-17 22:18:48 +0000829 SCTab.resize(SCTab.size() + 1);
830 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000831 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000832 SCDesc.NumMicroOps = 0;
833 SCDesc.BeginGroup = false;
834 SCDesc.EndGroup = false;
835 SCDesc.WriteProcResIdx = 0;
836 SCDesc.WriteLatencyIdx = 0;
837 SCDesc.ReadAdvanceIdx = 0;
838
839 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000840 bool HasVariants = false;
841 for (std::vector<CodeGenSchedTransition>::const_iterator
842 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
843 TI != TE; ++TI) {
844 if (TI->ProcIndices[0] == 0) {
845 HasVariants = true;
846 break;
847 }
848 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
849 TI->ProcIndices.end(), ProcModel.Index);
850 if (PIPos != TI->ProcIndices.end()) {
851 HasVariants = true;
852 break;
853 }
854 }
855 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000856 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
857 continue;
858 }
859
860 // Determine if the SchedClass is actually reachable on this processor. If
861 // not don't try to locate the processor resources, it will fail.
862 // If ProcIndices contains 0, this class applies to all processors.
863 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
864 if (SCI->ProcIndices[0] != 0) {
865 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
866 SCI->ProcIndices.end(), ProcModel.Index);
867 if (PIPos == SCI->ProcIndices.end())
868 continue;
869 }
870 IdxVec Writes = SCI->Writes;
871 IdxVec Reads = SCI->Reads;
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000872 if (!SCI->InstRWs.empty()) {
873 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000874 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000875 Record *RWDef = nullptr;
Andrew Trick9ef08822012-09-17 22:18:48 +0000876 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
877 RWI != RWE; ++RWI) {
878 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
879 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
880 RWDef = *RWI;
881 break;
882 }
883 }
884 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000885 Writes.clear();
886 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000887 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
888 Writes, Reads);
889 }
890 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000891 if (Writes.empty()) {
892 // Check this processor's itinerary class resources.
893 for (RecIter II = ProcModel.ItinRWDefs.begin(),
894 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
895 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
896 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
897 != Matched.end()) {
898 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
899 Writes, Reads);
900 break;
901 }
902 }
903 if (Writes.empty()) {
904 DEBUG(dbgs() << ProcModel.ModelName
905 << " does not have resources for class " << SCI->Name << '\n');
906 }
907 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000908 // Sum resources across all operand writes.
909 std::vector<MCWriteProcResEntry> WriteProcResources;
910 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000911 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000912 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
913 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
914 IdxVec WriteSeq;
Andrew Trickda984b12012-10-03 23:06:28 +0000915 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
916 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000917
918 // For each operand, create a latency entry.
919 MCWriteLatencyEntry WLEntry;
920 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000921 unsigned WriteID = WriteSeq.back();
922 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
923 // If this Write is not referenced by a ReadAdvance, don't distinguish it
924 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000925 if (!SchedModels.hasReadOfWrite(
926 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000927 WriteID = 0;
928 }
929 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000930
931 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
932 WSI != WSE; ++WSI) {
933
Andrew Trick9257b8f2012-09-22 02:24:21 +0000934 Record *WriteRes =
935 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000936
937 // Mark the parent class as invalid for unsupported write types.
938 if (WriteRes->getValueAsBit("Unsupported")) {
939 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
940 break;
941 }
942 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
943 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
944 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
945 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
946
947 // Create an entry for each ProcResource listed in WriteRes.
948 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
949 std::vector<int64_t> Cycles =
950 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000951
952 ExpandProcResources(PRVec, Cycles, ProcModel);
953
Andrew Trick9ef08822012-09-17 22:18:48 +0000954 for (unsigned PRIdx = 0, PREnd = PRVec.size();
955 PRIdx != PREnd; ++PRIdx) {
956 MCWriteProcResEntry WPREntry;
957 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
958 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000959 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000960 // If this resource is already used in this sequence, add the current
961 // entry's cycles so that the same resource appears to be used
962 // serially, rather than multiple parallel uses. This is important for
963 // in-order machine where the resource consumption is a hazard.
964 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
965 for( ; WPRIdx != WPREnd; ++WPRIdx) {
966 if (WriteProcResources[WPRIdx].ProcResourceIdx
967 == WPREntry.ProcResourceIdx) {
968 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
969 break;
970 }
971 }
972 if (WPRIdx == WPREnd)
973 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000974 }
975 }
976 WriteLatencies.push_back(WLEntry);
977 }
978 // Create an entry for each operand Read in this SchedClass.
979 // Entries must be sorted first by UseIdx then by WriteResourceID.
980 for (unsigned UseIdx = 0, EndIdx = Reads.size();
981 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000982 Record *ReadAdvance =
983 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000984 if (!ReadAdvance)
985 continue;
986
987 // Mark the parent class as invalid for unsupported write types.
988 if (ReadAdvance->getValueAsBit("Unsupported")) {
989 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
990 break;
991 }
992 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
993 IdxVec WriteIDs;
994 if (ValidWrites.empty())
995 WriteIDs.push_back(0);
996 else {
997 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
998 VWI != VWE; ++VWI) {
999 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
1000 }
1001 }
1002 std::sort(WriteIDs.begin(), WriteIDs.end());
1003 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
1004 MCReadAdvanceEntry RAEntry;
1005 RAEntry.UseIdx = UseIdx;
1006 RAEntry.WriteResourceID = *WI;
1007 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1008 ReadAdvanceEntries.push_back(RAEntry);
1009 }
1010 }
1011 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1012 WriteProcResources.clear();
1013 WriteLatencies.clear();
1014 ReadAdvanceEntries.clear();
1015 }
1016 // Add the information for this SchedClass to the global tables using basic
1017 // compression.
1018 //
1019 // WritePrecRes entries are sorted by ProcResIdx.
1020 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1021 LessWriteProcResources());
1022
1023 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1024 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1025 std::search(SchedTables.WriteProcResources.begin(),
1026 SchedTables.WriteProcResources.end(),
1027 WriteProcResources.begin(), WriteProcResources.end());
1028 if (WPRPos != SchedTables.WriteProcResources.end())
1029 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1030 else {
1031 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1032 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1033 WriteProcResources.end());
1034 }
1035 // Latency entries must remain in operand order.
1036 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1037 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1038 std::search(SchedTables.WriteLatencies.begin(),
1039 SchedTables.WriteLatencies.end(),
1040 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001041 if (WLPos != SchedTables.WriteLatencies.end()) {
1042 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1043 SCDesc.WriteLatencyIdx = idx;
1044 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1045 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1046 std::string::npos) {
1047 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1048 }
1049 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001050 else {
1051 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001052 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1053 WriteLatencies.begin(),
1054 WriteLatencies.end());
1055 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1056 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001057 }
1058 // ReadAdvanceEntries must remain in operand order.
1059 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1060 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1061 std::search(SchedTables.ReadAdvanceEntries.begin(),
1062 SchedTables.ReadAdvanceEntries.end(),
1063 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1064 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1065 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1066 else {
1067 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1068 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1069 ReadAdvanceEntries.end());
1070 }
1071 }
1072}
1073
Andrew Tricka72fca62012-09-17 22:18:50 +00001074// Emit SchedClass tables for all processors and associated global tables.
1075void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1076 raw_ostream &OS) {
1077 // Emit global WriteProcResTable.
1078 OS << "\n// {ProcResourceIdx, Cycles}\n"
1079 << "extern const llvm::MCWriteProcResEntry "
1080 << Target << "WriteProcResTable[] = {\n"
1081 << " { 0, 0}, // Invalid\n";
1082 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1083 WPRIdx != WPREnd; ++WPRIdx) {
1084 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1085 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1086 << format("%2d", WPREntry.Cycles) << "}";
1087 if (WPRIdx + 1 < WPREnd)
1088 OS << ',';
1089 OS << " // #" << WPRIdx << '\n';
1090 }
1091 OS << "}; // " << Target << "WriteProcResTable\n";
1092
1093 // Emit global WriteLatencyTable.
1094 OS << "\n// {Cycles, WriteResourceID}\n"
1095 << "extern const llvm::MCWriteLatencyEntry "
1096 << Target << "WriteLatencyTable[] = {\n"
1097 << " { 0, 0}, // Invalid\n";
1098 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1099 WLIdx != WLEnd; ++WLIdx) {
1100 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1101 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1102 << format("%2d", WLEntry.WriteResourceID) << "}";
1103 if (WLIdx + 1 < WLEnd)
1104 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001105 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001106 }
1107 OS << "}; // " << Target << "WriteLatencyTable\n";
1108
1109 // Emit global ReadAdvanceTable.
1110 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1111 << "extern const llvm::MCReadAdvanceEntry "
1112 << Target << "ReadAdvanceTable[] = {\n"
1113 << " {0, 0, 0}, // Invalid\n";
1114 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1115 RAIdx != RAEnd; ++RAIdx) {
1116 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1117 OS << " {" << RAEntry.UseIdx << ", "
1118 << format("%2d", RAEntry.WriteResourceID) << ", "
1119 << format("%2d", RAEntry.Cycles) << "}";
1120 if (RAIdx + 1 < RAEnd)
1121 OS << ',';
1122 OS << " // #" << RAIdx << '\n';
1123 }
1124 OS << "}; // " << Target << "ReadAdvanceTable\n";
1125
1126 // Emit a SchedClass table for each processor.
1127 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1128 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1129 if (!PI->hasInstrSchedModel())
1130 continue;
1131
1132 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001133 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001134
1135 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1136 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1137 OS << "static const llvm::MCSchedClassDesc "
1138 << PI->ModelName << "SchedClasses[] = {\n";
1139
1140 // The first class is always invalid. We no way to distinguish it except by
1141 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001142 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001143 && "invalid class not first");
1144 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1145 << MCSchedClassDesc::InvalidNumMicroOps
1146 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1147
1148 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1149 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1150 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1151 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1152 if (SchedClass.Name.size() < 18)
1153 OS.indent(18 - SchedClass.Name.size());
1154 OS << MCDesc.NumMicroOps
1155 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1156 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1157 << ", " << MCDesc.NumWriteProcResEntries
1158 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1159 << ", " << MCDesc.NumWriteLatencyEntries
1160 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1161 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1162 if (SCIdx + 1 < SCEnd)
1163 OS << ',';
1164 OS << " // #" << SCIdx << '\n';
1165 }
1166 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1167 }
1168}
1169
Andrew Trick87255e32012-07-07 04:00:00 +00001170void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1171 // For each processor model.
1172 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1173 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001174 // Emit processor resource table.
1175 if (PI->hasInstrSchedModel())
1176 EmitProcessorResources(*PI, OS);
1177 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +00001178 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001179 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001180
Andrew Trick73d77362012-06-05 03:44:40 +00001181 // Begin processor itinerary properties
1182 OS << "\n";
Pete Cooper11759452014-09-02 17:43:54 +00001183 OS << "static const llvm::MCSchedModel " << PI->ModelName << " = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001184 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
Andrew Trickde2109e2013-06-15 04:49:57 +00001185 EmitProcessorProp(OS, PI->ModelDef, "MicroOpBufferSize", ',');
Hal Finkel6532c202014-05-08 09:14:44 +00001186 EmitProcessorProp(OS, PI->ModelDef, "LoopMicroOpBufferSize", ',');
Andrew Trick87255e32012-07-07 04:00:00 +00001187 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1188 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick352abc12012-08-08 02:44:16 +00001189 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001190
1191 OS << " " << (bool)(PI->ModelDef ?
Sanjay Patela2f658d2014-07-15 22:39:58 +00001192 PI->ModelDef->getValueAsBit("PostRAScheduler") : 0)
1193 << ", // " << "PostRAScheduler\n";
1194
1195 OS << " " << (bool)(PI->ModelDef ?
Andrew Trickb6854d82013-09-25 18:14:12 +00001196 PI->ModelDef->getValueAsBit("CompleteModel") : 0)
1197 << ", // " << "CompleteModel\n";
1198
Andrew Trickab722bd2012-09-18 03:18:56 +00001199 OS << " " << PI->Index << ", // Processor ID\n";
1200 if (PI->hasInstrSchedModel())
1201 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1202 << " " << PI->ModelName << "SchedClasses" << ",\n"
1203 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1204 << " " << (SchedModels.schedClassEnd()
1205 - SchedModels.schedClassBegin()) << ",\n";
1206 else
1207 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Pete Cooper11759452014-09-02 17:43:54 +00001208 if (PI->hasItineraries())
1209 OS << " " << PI->ItinsDef->getName() << "};\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001210 else
Pete Cooper11759452014-09-02 17:43:54 +00001211 OS << " nullptr}; // No Itinerary\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001212 }
Jim Laskey3763a502005-10-31 17:16:01 +00001213}
1214
1215//
1216// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1217//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001218void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001219 // Gather and sort processor information
1220 std::vector<Record*> ProcessorList =
1221 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001222 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001223
1224 // Begin processor table
1225 OS << "\n";
1226 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001227 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001228 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001229
Jim Laskey3763a502005-10-31 17:16:01 +00001230 // For each processor
1231 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1232 // Next processor
1233 Record *Processor = ProcessorList[i];
1234
Bill Wendlinge6182262007-05-04 20:38:40 +00001235 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001236 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001237 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001238
Jim Laskey3763a502005-10-31 17:16:01 +00001239 // Emit as { "cpu", procinit },
Andrew Trick23f3c652012-09-17 22:18:45 +00001240 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001241
Jim Laskey3763a502005-10-31 17:16:01 +00001242 // Depending on ''if more in the list'' emit comma
1243 if (++i < N) OS << ",";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001244
Jim Laskey3763a502005-10-31 17:16:01 +00001245 OS << "\n";
1246 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001247
Jim Laskey3763a502005-10-31 17:16:01 +00001248 // End processor table
1249 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001250}
1251
1252//
Andrew Trick87255e32012-07-07 04:00:00 +00001253// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001254//
Andrew Trick87255e32012-07-07 04:00:00 +00001255void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001256 OS << "#ifdef DBGFIELD\n"
1257 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1258 << "#endif\n"
1259 << "#ifndef NDEBUG\n"
1260 << "#define DBGFIELD(x) x,\n"
1261 << "#else\n"
1262 << "#define DBGFIELD(x)\n"
1263 << "#endif\n";
1264
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001265 if (SchedModels.hasItineraries()) {
Andrew Trick87255e32012-07-07 04:00:00 +00001266 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001267 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001268 EmitStageAndOperandCycleData(OS, ProcItinLists);
1269 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001270 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001271 OS << "\n// ===============================================================\n"
1272 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001273
Andrew Trick9ef08822012-09-17 22:18:48 +00001274 SchedClassTables SchedTables;
1275 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1276 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1277 GenSchedClassTables(*PI, SchedTables);
1278 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001279 EmitSchedClassTables(SchedTables, OS);
1280
1281 // Emit the processor machine model
1282 EmitProcessorModels(OS);
1283 // Emit the processor lookup data
1284 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001285
Andrew Trick23f3c652012-09-17 22:18:45 +00001286 OS << "#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001287}
1288
Andrew Trickc6c88152012-09-18 03:41:43 +00001289void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1290 raw_ostream &OS) {
1291 OS << "unsigned " << ClassName
1292 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1293 << " const TargetSchedModel *SchedModel) const {\n";
1294
1295 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1296 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1297 for (std::vector<Record*>::const_iterator
1298 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1299 OS << (*PI)->getValueAsString("Code") << '\n';
1300 }
1301 IdxVec VariantClasses;
1302 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1303 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1304 if (SCI->Transitions.empty())
1305 continue;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001306 VariantClasses.push_back(SCI->Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001307 }
1308 if (!VariantClasses.empty()) {
1309 OS << " switch (SchedClass) {\n";
1310 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1311 VCI != VCE; ++VCI) {
1312 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1313 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1314 IdxVec ProcIndices;
1315 for (std::vector<CodeGenSchedTransition>::const_iterator
1316 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1317 TI != TE; ++TI) {
1318 IdxVec PI;
1319 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1320 ProcIndices.begin(), ProcIndices.end(),
1321 std::back_inserter(PI));
1322 ProcIndices.swap(PI);
1323 }
1324 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1325 PI != PE; ++PI) {
1326 OS << " ";
1327 if (*PI != 0)
1328 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1329 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1330 << '\n';
1331 for (std::vector<CodeGenSchedTransition>::const_iterator
1332 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1333 TI != TE; ++TI) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001334 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1335 TI->ProcIndices.end(), *PI)) {
1336 continue;
1337 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001338 OS << " if (";
Andrew Trickc6c88152012-09-18 03:41:43 +00001339 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1340 RI != RE; ++RI) {
1341 if (RI != TI->PredTerm.begin())
1342 OS << "\n && ";
1343 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1344 }
1345 OS << ")\n"
1346 << " return " << TI->ToClassIdx << "; // "
1347 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1348 }
1349 OS << " }\n";
1350 if (*PI == 0)
1351 break;
1352 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001353 if (SC.isInferred())
1354 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001355 OS << " break;\n";
1356 }
1357 OS << " };\n";
1358 }
1359 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1360 << "} // " << ClassName << "::resolveSchedClass\n";
1361}
1362
Jim Laskey86f002c2005-10-27 19:47:21 +00001363//
Jim Laskeya2b52352005-10-26 17:30:34 +00001364// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1365// the subtarget features string.
1366//
Evan Cheng54b68e32011-07-01 20:45:01 +00001367void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1368 unsigned NumFeatures,
1369 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001370 std::vector<Record*> Features =
1371 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001372 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001373
Andrew Trickdb6ed642011-04-01 01:56:55 +00001374 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1375 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001376 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001377 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001378 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001379 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001380 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001381
1382 if (Features.empty()) {
1383 OS << "}\n";
1384 return;
1385 }
1386
Andrew Trickba7b9212012-09-18 05:33:15 +00001387 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001388 << " uint64_t Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001389
Jim Laskeydffe5972005-10-28 21:47:29 +00001390 for (unsigned i = 0; i < Features.size(); i++) {
1391 // Next record
1392 Record *R = Features[i];
Bill Wendlinge6182262007-05-04 20:38:40 +00001393 const std::string &Instance = R->getName();
1394 const std::string &Value = R->getValueAsString("Value");
1395 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001396
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001397 if (Value=="true" || Value=="false")
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001398 OS << " if ((Bits & " << Target << "::"
1399 << Instance << ") != 0) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001400 << Attribute << " = " << Value << ";\n";
1401 else
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001402 OS << " if ((Bits & " << Target << "::"
1403 << Instance << ") != 0 && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001404 << Attribute << " < " << Value << ") "
1405 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001406 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001407
Evan Chengfe6e4052011-06-30 01:53:36 +00001408 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001409}
1410
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001411//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001412// SubtargetEmitter::run - Main subtarget enumeration emitter.
1413//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001414void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001415 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001416
Evan Cheng4d1ca962011-07-08 01:53:10 +00001417 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1418 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1419
1420 OS << "namespace llvm {\n";
Michael Kupersteinefd7a962015-02-19 11:38:11 +00001421 Enumeration(OS, "SubtargetFeature", true);
Evan Cheng4d1ca962011-07-08 01:53:10 +00001422 OS << "} // End llvm namespace \n";
1423 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1424
Evan Cheng54b68e32011-07-01 20:45:01 +00001425 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1426 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001427
Evan Cheng54b68e32011-07-01 20:45:01 +00001428 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001429#if 0
1430 OS << "namespace {\n";
1431#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001432 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001433 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001434 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001435 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001436 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001437 OS << "\n";
1438#if 0
1439 OS << "}\n";
1440#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001441
1442 // MCInstrInfo initialization routine.
1443 OS << "static inline void Init" << Target
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001444 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1445 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1446 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001447 if (NumFeatures)
1448 OS << Target << "FeatureKV, ";
1449 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001450 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001451 if (NumProcs)
1452 OS << Target << "SubTypeKV, ";
1453 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001454 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001455 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001456 OS << Target << "ProcSchedKV, "
1457 << Target << "WriteProcResTable, "
1458 << Target << "WriteLatencyTable, "
1459 << Target << "ReadAdvanceTable, ";
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001460 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001461 OS << '\n'; OS.indent(22);
1462 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001463 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001464 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001465 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001466 OS << "0, 0, 0";
1467 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001468
1469 OS << "} // End llvm namespace \n";
1470
1471 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1472
1473 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1474 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1475
1476 OS << "#include \"llvm/Support/Debug.h\"\n";
Benjamin Kramerb85d3752015-03-23 18:45:56 +00001477 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001478 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1479
1480 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1481
Evan Cheng0d639a22011-07-01 21:01:15 +00001482 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001483 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1484 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1485
1486 std::string ClassName = Target + "GenSubtargetInfo";
1487 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001488 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001489 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng1a72add62011-07-07 07:07:08 +00001490 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1491 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001492 << "public:\n"
Andrew Trickc6c88152012-09-18 03:41:43 +00001493 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001494 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001495 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001496 << " const;\n"
Evan Cheng54b68e32011-07-01 20:45:01 +00001497 << "};\n";
1498 OS << "} // End llvm namespace \n";
1499
1500 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1501
1502 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1503 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1504
Andrew Trick1188e432012-09-18 03:32:57 +00001505 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001506 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001507 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1508 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001509 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1510 OS << "extern const llvm::MCWriteProcResEntry "
1511 << Target << "WriteProcResTable[];\n";
1512 OS << "extern const llvm::MCWriteLatencyEntry "
1513 << Target << "WriteLatencyTable[];\n";
1514 OS << "extern const llvm::MCReadAdvanceEntry "
1515 << Target << "ReadAdvanceTable[];\n";
1516
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001517 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001518 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1519 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001520 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001521 }
1522
Evan Cheng1a72add62011-07-07 07:07:08 +00001523 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1524 << "StringRef FS)\n"
Evan Cheng0d639a22011-07-01 21:01:15 +00001525 << " : TargetSubtargetInfo() {\n"
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001526 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001527 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001528 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001529 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001530 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001531 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001532 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001533 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001534 OS << "None, ";
Andrew Trickab722bd2012-09-18 03:18:56 +00001535 OS << '\n'; OS.indent(22);
1536 OS << Target << "ProcSchedKV, "
1537 << Target << "WriteProcResTable, "
1538 << Target << "WriteLatencyTable, "
1539 << Target << "ReadAdvanceTable, ";
1540 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001541 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001542 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001543 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001544 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001545 } else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001546 OS << "0, 0, 0";
1547 OS << ");\n}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001548
Andrew Trickc6c88152012-09-18 03:41:43 +00001549 EmitSchedModelHelpers(ClassName, OS);
1550
Evan Cheng54b68e32011-07-01 20:45:01 +00001551 OS << "} // End llvm namespace \n";
1552
1553 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001554}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001555
1556namespace llvm {
1557
1558void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001559 CodeGenTarget CGTarget(RK);
1560 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001561}
1562
1563} // End llvm namespace