blob: 344d89e6f0022dedce8d664eaa99a8eda754ce58 [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"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000016#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000019#include "llvm/ADT/StringRef.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000020#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000021#include "llvm/MC/MCSchedule.h"
Michael Kupersteindb0712f2015-05-26 10:47:10 +000022#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/Format.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000025#include "llvm/Support/raw_ostream.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000026#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027#include "llvm/TableGen/Record.h"
28#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenb0aa47b2005-10-28 01:43:09 +000029#include <algorithm>
Eugene Zelenko75259bb2016-05-17 17:04:23 +000030#include <cassert>
31#include <cstdint>
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000032#include <iterator>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000033#include <map>
34#include <string>
35#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000036
Jim Laskeycfda85a2005-10-21 19:00:04 +000037using namespace llvm;
38
Chandler Carruth97acce22014-04-22 03:06:00 +000039#define DEBUG_TYPE "subtarget-emitter"
40
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000041namespace {
Eugene Zelenko75259bb2016-05-17 17:04:23 +000042
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000044 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
45 // The SchedClassDesc table indexes into a global write resource table, write
46 // latency table, and read advance table.
47 struct SchedClassTables {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000048 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
Andrew Trick9ef08822012-09-17 22:18:48 +000049 std::vector<MCWriteProcResEntry> WriteProcResources;
50 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000051 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000052 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
53
54 // Reserve an invalid entry at index 0
55 SchedClassTables() {
56 ProcSchedClasses.resize(1);
57 WriteProcResources.resize(1);
58 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000059 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000060 ReadAdvanceEntries.resize(1);
61 }
62 };
63
64 struct LessWriteProcResources {
65 bool operator()(const MCWriteProcResEntry &LHS,
66 const MCWriteProcResEntry &RHS) {
67 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
68 }
69 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000070
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +000071 const CodeGenTarget &TGT;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000072 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000073 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000074 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000075
Craig Topper094bbca2016-02-14 05:22:01 +000076 void Enumeration(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000077 unsigned FeatureKeyValues(raw_ostream &OS);
78 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000079 void FormItineraryStageString(const std::string &Names,
80 Record *ItinData, std::string &ItinString,
81 unsigned &NStages);
82 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
83 unsigned &NOperandCycles);
84 void FormItineraryBypassString(const std::string &Names,
85 Record *ItinData,
86 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000087 void EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000088 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000089 &ProcItinLists);
90 void EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000091 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000092 &ProcItinLists);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000093 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
94 raw_ostream &OS);
95 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
96 raw_ostream &OS);
Mehdi Amini32986ed2016-10-04 23:47:33 +000097 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000098 char Separator);
Clement Courbet39911e22018-02-08 08:46:48 +000099 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
100 raw_ostream &OS);
Andrew Trick23f3c652012-09-17 22:18:45 +0000101 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
102 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000103 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +0000104 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000105 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
106 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000107 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
108 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000109 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
110 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +0000111 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000112 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000113 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000114 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000115 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000116 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000117 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
118 unsigned NumProcs);
119
120public:
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000121 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
122 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
123 Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000124
125 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000126};
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000127
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000128} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000129
Jim Laskeya1beea62005-10-22 07:59:56 +0000130//
Jim Laskeya2b52352005-10-26 17:30:34 +0000131// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000132//
Craig Topper094bbca2016-02-14 05:22:01 +0000133void SubtargetEmitter::Enumeration(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000134 // Get all records of class and sort
Craig Topper094bbca2016-02-14 05:22:01 +0000135 std::vector<Record*> DefList =
136 Records.getAllDerivedDefinitions("SubtargetFeature");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000137 llvm::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000138
Evan Chenga2e61292011-04-15 19:35:46 +0000139 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000140 if (N == 0)
141 return;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000142 if (N > MAX_SUBTARGET_FEATURES)
143 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chenga2e61292011-04-15 19:35:46 +0000144
Evan Cheng54b68e32011-07-01 20:45:01 +0000145 OS << "namespace " << Target << " {\n";
146
Craig Topperbcdb0f22016-02-13 17:58:14 +0000147 // Open enumeration.
Craig Topper2d45c1d2016-02-13 06:03:29 +0000148 OS << "enum {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000149
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000150 // For each record
Craig Topperdf1285b2017-10-24 15:50:53 +0000151 for (unsigned i = 0; i < N; ++i) {
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000152 // Next record
153 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000154
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000155 // Get and emit name
Craig Topperdf1285b2017-10-24 15:50:53 +0000156 OS << " " << Def->getName() << " = " << i << ",\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000157 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000158
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000159 // Close enumeration and namespace
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000160 OS << "};\n";
161 OS << "} // end namespace " << Target << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000162}
163
164//
Bill Wendlinge6182262007-05-04 20:38:40 +0000165// FeatureKeyValues - Emit data of all the subtarget features. Used by the
166// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000167//
Evan Cheng54b68e32011-07-01 20:45:01 +0000168unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000169 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000170 std::vector<Record*> FeatureList =
171 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000172
173 if (FeatureList.empty())
174 return 0;
175
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000176 llvm::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000177
Jim Laskey19595752005-10-28 15:20:43 +0000178 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000179 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000180 << "extern const llvm::SubtargetFeatureKV " << Target
181 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000182
Jim Laskey19595752005-10-28 15:20:43 +0000183 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000184 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000185 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000186 // Next feature
187 Record *Feature = FeatureList[i];
188
Craig Topperbcd3c372017-05-31 21:12:46 +0000189 StringRef Name = Feature->getName();
190 StringRef CommandLineName = Feature->getValueAsString("Name");
191 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000192
Jim Laskey3f7d0472006-12-12 20:55:58 +0000193 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000194
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000195 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000196 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000197 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000198 << "\"" << Desc << "\", "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000199 << "{ " << Target << "::" << Name << " }, ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000200
Craig Topper37eeb322018-03-23 00:02:45 +0000201 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000202
Craig Topper4ceea0a2016-01-03 08:57:41 +0000203 OS << "{";
204 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
205 OS << " " << Target << "::" << ImpliesList[j]->getName();
206 if (++j < M) OS << ",";
Bill Wendlinge6182262007-05-04 20:38:40 +0000207 }
Craig Topperdf1285b2017-10-24 15:50:53 +0000208 OS << " } },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000209 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000210 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000211
Jim Laskey19595752005-10-28 15:20:43 +0000212 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000213 OS << "};\n";
214
Evan Cheng54b68e32011-07-01 20:45:01 +0000215 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000216}
217
218//
219// CPUKeyValues - Emit data of all the subtarget processors. Used by command
220// line.
221//
Evan Cheng54b68e32011-07-01 20:45:01 +0000222unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000223 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000224 std::vector<Record*> ProcessorList =
225 Records.getAllDerivedDefinitions("Processor");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000226 llvm::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000227
Jim Laskey19595752005-10-28 15:20:43 +0000228 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000229 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000230 << "extern const llvm::SubtargetFeatureKV " << Target
231 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000232
Jim Laskey19595752005-10-28 15:20:43 +0000233 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000234 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000235 StringRef Name = Processor->getValueAsString("Name");
Craig Topper37eeb322018-03-23 00:02:45 +0000236 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000237
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000238 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000239 OS << " { "
240 << "\"" << Name << "\", "
241 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000242
Craig Topper4ceea0a2016-01-03 08:57:41 +0000243 OS << "{";
244 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
245 OS << " " << Target << "::" << FeatureList[j]->getName();
246 if (++j < M) OS << ",";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000247 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000248 // The { } is for the "implies" section of this data structure.
Craig Topperdf1285b2017-10-24 15:50:53 +0000249 OS << " }, { } },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000250 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000251
Jim Laskey19595752005-10-28 15:20:43 +0000252 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000253 OS << "};\n";
254
Evan Cheng54b68e32011-07-01 20:45:01 +0000255 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000256}
Jim Laskeya1beea62005-10-22 07:59:56 +0000257
Jim Laskeya2b52352005-10-26 17:30:34 +0000258//
David Goodwind813cbf2009-08-17 16:02:57 +0000259// FormItineraryStageString - Compose a string containing the stage
260// data initialization for the specified itinerary. N is the number
261// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000262//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000263void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
264 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000265 std::string &ItinString,
266 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000267 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000268 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000269
270 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000271 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000272 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000273 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000274 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000275
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000276 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000277 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000278 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000279
Jim Laskeydffe5972005-10-28 21:47:29 +0000280 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000281 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000282
Jim Laskey19595752005-10-28 15:20:43 +0000283 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000284 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000285 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000286 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000287 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000288 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000289
David Goodwinb369ee42009-08-12 18:31:53 +0000290 int TimeInc = Stage->getValueAsInt("TimeInc");
291 ItinString += ", " + itostr(TimeInc);
292
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000293 int Kind = Stage->getValueAsInt("Kind");
294 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
295
Jim Laskey19595752005-10-28 15:20:43 +0000296 // Close off stage
297 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000298 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000299 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000300}
301
302//
David Goodwind813cbf2009-08-17 16:02:57 +0000303// FormItineraryOperandCycleString - Compose a string containing the
304// operand cycle initialization for the specified itinerary. N is the
305// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000306//
David Goodwind813cbf2009-08-17 16:02:57 +0000307void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
308 std::string &ItinString, unsigned &NOperandCycles) {
309 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000310 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000311 ItinData->getValueAsListOfInts("OperandCycles");
312
313 // For each operand cycle
314 unsigned N = NOperandCycles = OperandCycleList.size();
315 for (unsigned i = 0; i < N;) {
316 // Next operand cycle
317 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000318
David Goodwind813cbf2009-08-17 16:02:57 +0000319 ItinString += " " + itostr(OCycle);
320 if (++i < N) ItinString += ", ";
321 }
322}
323
Evan Cheng0097dd02010-09-28 23:50:49 +0000324void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
325 Record *ItinData,
326 std::string &ItinString,
327 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000328 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000329 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000330 unsigned i = 0;
331 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000332 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000333 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000334 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000335 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000336 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000337 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000338 }
339}
340
David Goodwind813cbf2009-08-17 16:02:57 +0000341//
Andrew Trick87255e32012-07-07 04:00:00 +0000342// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
343// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
344// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000345//
Andrew Trick87255e32012-07-07 04:00:00 +0000346void SubtargetEmitter::
347EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000348 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000349 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000350 // Multiple processor models may share an itinerary record. Emit it once.
351 SmallPtrSet<Record*, 8> ItinsDefSet;
352
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000353 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000354 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000355
Craig Topper29c55dcb2016-02-13 06:03:32 +0000356 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000357 continue;
358
Craig Topper37eeb322018-03-23 00:02:45 +0000359 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000360 if (FUs.empty())
361 continue;
362
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000363 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000364 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000365 << "namespace " << Name << "FU {\n";
366
367 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000368 OS << " const unsigned " << FUs[j]->getName()
369 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000370
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000371 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000372
Craig Topper37eeb322018-03-23 00:02:45 +0000373 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000374 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000375 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000376 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000377
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000378 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000379 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000380 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000381 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000382
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000383 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000384 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000385 }
386
Jim Laskey19595752005-10-28 15:20:43 +0000387 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000388 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
389 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000390 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000391
David Goodwind813cbf2009-08-17 16:02:57 +0000392 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000393 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000394 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000395 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000396
397 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000398 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000399 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000400 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000401
Andrew Trick87255e32012-07-07 04:00:00 +0000402 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000403 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000404 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000405 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000406 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000407 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000408 // Add process itinerary to the list.
409 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000410
Andrew Trick87255e32012-07-07 04:00:00 +0000411 // If this processor defines no itineraries, then leave the itinerary list
412 // empty.
413 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000414 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000415 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000416
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000417 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000418
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000419 ItinList.resize(SchedModels.numInstrSchedClasses());
420 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
421
422 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000423 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
424
Jim Laskeydffe5972005-10-28 21:47:29 +0000425 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000426 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000427
Jim Laskey19595752005-10-28 15:20:43 +0000428 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000429 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000430 unsigned NStages = 0;
431 if (ItinData)
432 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000433
David Goodwind813cbf2009-08-17 16:02:57 +0000434 // Get string and operand cycle count
435 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000436 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000437 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000438 if (ItinData) {
439 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
440 NOperandCycles);
441
442 FormItineraryBypassString(Name, ItinData, ItinBypassString,
443 NOperandCycles);
444 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000445
David Goodwind813cbf2009-08-17 16:02:57 +0000446 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000447 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000448 if (NStages > 0) {
449 FindStage = ItinStageMap[ItinStageString];
450 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000451 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
452 StageTable += ItinStageString + ", // " + itostr(StageCount);
453 if (NStages > 1)
454 StageTable += "-" + itostr(StageCount + NStages - 1);
455 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000456 // Record Itin class number.
457 ItinStageMap[ItinStageString] = FindStage = StageCount;
458 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000459 }
460 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000461
David Goodwind813cbf2009-08-17 16:02:57 +0000462 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000463 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000464 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000465 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
466 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000467 if (FindOperandCycle == 0) {
468 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000469 OperandCycleTable += ItinOperandCycleString + ", // ";
470 std::string OperandIdxComment = itostr(OperandCycleCount);
471 if (NOperandCycles > 1)
472 OperandIdxComment += "-"
473 + itostr(OperandCycleCount + NOperandCycles - 1);
474 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000475 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000476 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000477 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000478 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000479 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000480 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000481 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000482 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000483
Evan Cheng367a5df2010-09-09 18:18:55 +0000484 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000485 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
486 InstrItinerary Intinerary = {
487 NumUOps,
488 FindStage,
489 uint16_t(FindStage + NStages),
490 FindOperandCycle,
491 uint16_t(FindOperandCycle + NOperandCycles),
492 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000493
Jim Laskey19595752005-10-28 15:20:43 +0000494 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000495 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000496 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000497 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000498
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000499 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000500 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000501 StageTable += "};\n";
502
503 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000504 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000505 OperandCycleTable += "};\n";
506
Andrew Trick87255e32012-07-07 04:00:00 +0000507 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000508 BypassTable += "};\n";
509
David Goodwind813cbf2009-08-17 16:02:57 +0000510 // Emit tables.
511 OS << StageTable;
512 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000513 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000514}
515
Andrew Trick87255e32012-07-07 04:00:00 +0000516//
517// EmitProcessorData - Generate data for processor itineraries that were
518// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
519// Itineraries for each processor. The Itinerary lists are indexed on
520// CodeGenSchedClass::Index.
521//
522void SubtargetEmitter::
523EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000524 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000525 // Multiple processor models may share an itinerary record. Emit it once.
526 SmallPtrSet<Record*, 8> ItinsDefSet;
527
Andrew Trick87255e32012-07-07 04:00:00 +0000528 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000529 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000530 ProcItinListsIter = ProcItinLists.begin();
531 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000532 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000533
Andrew Trick87255e32012-07-07 04:00:00 +0000534 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000535 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000536 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000537
Andrew Trick87255e32012-07-07 04:00:00 +0000538 // Get the itinerary list for the processor.
539 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000540 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000541
Pete Cooperc0eb1532014-09-02 23:23:34 +0000542 // Empty itineraries aren't referenced anywhere in the tablegen output
543 // so don't emit them.
544 if (ItinList.empty())
545 continue;
546
Andrew Trick87255e32012-07-07 04:00:00 +0000547 OS << "\n";
548 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000549
550 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000551 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000552
553 // For each itinerary class in CodeGenSchedClass::Index order.
554 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
555 InstrItinerary &Intinerary = ItinList[j];
556
557 // Emit Itinerary in the form of
558 // { firstStage, lastStage, firstCycle, lastCycle } // index
559 OS << " { " <<
560 Intinerary.NumMicroOps << ", " <<
561 Intinerary.FirstStage << ", " <<
562 Intinerary.LastStage << ", " <<
563 Intinerary.FirstOperandCycle << ", " <<
564 Intinerary.LastOperandCycle << " }" <<
565 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
566 }
567 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000568 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
569 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000570 OS << "};\n";
571 }
572}
573
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000574// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000575// value defined in the C++ header. The Record is null if the processor does not
576// define a model.
577void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000578 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000579 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000580 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000581 if (V >= 0)
582 OS << V << Separator << " // " << Name;
583 else
Andrew Trick87255e32012-07-07 04:00:00 +0000584 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000585 OS << '\n';
586}
587
Clement Courbet39911e22018-02-08 08:46:48 +0000588void SubtargetEmitter::EmitProcessorResourceSubUnits(
589 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
590 OS << "\nstatic const unsigned " << ProcModel.ModelName
591 << "ProcResourceSubUnits[] = {\n"
592 << " 0, // Invalid\n";
593
594 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
595 Record *PRDef = ProcModel.ProcResourceDefs[i];
596 if (!PRDef->isSubClassOf("ProcResGroup"))
597 continue;
598 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
599 for (Record *RUDef : ResUnits) {
600 Record *const RU =
601 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
602 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
603 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
604 }
605 }
606 OS << " // " << PRDef->getName() << "\n";
607 }
608 OS << "};\n";
609}
610
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000611static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
612 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000613 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000614 if (Record *RCU = ProcModel.RetireControlUnit) {
615 ReorderBufferSize =
616 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
617 MaxRetirePerCycle =
618 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
619 }
620
621 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
622 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
623}
624
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000625static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
626 unsigned NumRegisterFiles,
627 unsigned NumCostEntries, raw_ostream &OS) {
628 if (NumRegisterFiles)
629 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
630 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000631 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000632
633 OS << ", // Number of register files.\n ";
634 if (NumCostEntries)
635 OS << ProcModel.ModelName << "RegisterCosts,\n ";
636 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000637 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000638 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000639}
640
641unsigned
642SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
643 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000644 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
645 return RF.hasDefaultCosts();
646 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000647 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000648
649 // Print the RegisterCost table first.
650 OS << "\n// {RegisterClassID, Register Cost}\n";
651 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
652 << "RegisterCosts"
653 << "[] = {\n";
654
655 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
656 // Skip register files with a default cost table.
657 if (RF.hasDefaultCosts())
658 continue;
659 // Add entries to the cost table.
660 for (const CodeGenRegisterCost &RC : RF.Costs) {
661 OS << " { ";
662 Record *Rec = RC.RCDef;
663 if (Rec->getValue("Namespace"))
664 OS << Rec->getValueAsString("Namespace") << "::";
665 OS << Rec->getName() << "RegClassID, " << RC.Cost << "},\n";
666 }
667 }
668 OS << "};\n";
669
670 // Now generate a table with register file info.
671 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl}\n";
672 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
673 << "RegisterFiles"
674 << "[] = {\n"
675 << " { \"InvalidRegisterFile\", 0, 0, 0 },\n";
676 unsigned CostTblIndex = 0;
677
678 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
679 OS << " { ";
680 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
681 unsigned NumCostEntries = RD.Costs.size();
682 OS << NumCostEntries << ", " << CostTblIndex << "},\n";
683 CostTblIndex += NumCostEntries;
684 }
685 OS << "};\n";
686
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000687 return CostTblIndex;
688}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000689
Clement Courbetb4493792018-04-10 08:16:37 +0000690static bool EmitPfmIssueCountersTable(const CodeGenProcModel &ProcModel,
691 raw_ostream &OS) {
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000692 unsigned NumCounterDefs = 1 + ProcModel.ProcResourceDefs.size();
693 std::vector<const Record *> CounterDefs(NumCounterDefs);
Clement Courbetb4493792018-04-10 08:16:37 +0000694 bool HasCounters = false;
695 for (const Record *CounterDef : ProcModel.PfmIssueCounterDefs) {
696 const Record *&CD = CounterDefs[ProcModel.getProcResourceIdx(
697 CounterDef->getValueAsDef("Resource"))];
698 if (CD) {
699 PrintFatalError(CounterDef->getLoc(),
700 "multiple issue counters for " +
701 CounterDef->getValueAsDef("Resource")->getName());
702 }
703 CD = CounterDef;
704 HasCounters = true;
705 }
706 if (!HasCounters) {
707 return false;
708 }
709 OS << "\nstatic const char* " << ProcModel.ModelName
710 << "PfmIssueCounters[] = {\n";
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000711 for (unsigned i = 0; i != NumCounterDefs; ++i) {
712 const Record *CounterDef = CounterDefs[i];
Clement Courbetb4493792018-04-10 08:16:37 +0000713 if (CounterDef) {
714 const auto PfmCounters = CounterDef->getValueAsListOfStrings("Counters");
715 if (PfmCounters.empty())
716 PrintFatalError(CounterDef->getLoc(), "empty counter list");
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000717 OS << " \"" << PfmCounters[0];
718 for (unsigned p = 1, e = PfmCounters.size(); p != e; ++p)
719 OS << ",\" \"" << PfmCounters[p];
720 OS << "\", // #" << i << " = ";
721 OS << CounterDef->getValueAsDef("Resource")->getName() << "\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000722 } else {
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000723 OS << " nullptr, // #" << i << "\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000724 }
725 }
726 OS << "};\n";
727 return true;
728}
729
730static void EmitPfmCounters(const CodeGenProcModel &ProcModel,
731 const bool HasPfmIssueCounters, raw_ostream &OS) {
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000732 OS << " {\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000733 // Emit the cycle counter.
734 if (ProcModel.PfmCycleCounterDef)
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000735 OS << " \"" << ProcModel.PfmCycleCounterDef->getValueAsString("Counter")
Clement Courbetb4493792018-04-10 08:16:37 +0000736 << "\", // Cycle counter.\n";
737 else
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000738 OS << " nullptr, // No cycle counter.\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000739
740 // Emit a reference to issue counters table.
741 if (HasPfmIssueCounters)
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000742 OS << " " << ProcModel.ModelName << "PfmIssueCounters\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000743 else
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000744 OS << " nullptr // No issue counters.\n";
745 OS << " }\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000746}
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000747
748void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
749 raw_ostream &OS) {
750 // Generate a table of register file descriptors (one entry per each user
751 // defined register file), and a table of register costs.
752 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
753
Clement Courbetb4493792018-04-10 08:16:37 +0000754 // Generate a table of ProcRes counter names.
755 const bool HasPfmIssueCounters = EmitPfmIssueCountersTable(ProcModel, OS);
756
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000757 // Now generate a table for the extra processor info.
758 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000759 << "ExtraInfo = {\n ";
760
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000761 // Add information related to the retire control unit.
762 EmitRetireControlUnitInfo(ProcModel, OS);
763
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000764 // Add information related to the register files (i.e. where to find register
765 // file descriptors and register costs).
766 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
767 NumCostEntries, OS);
768
Clement Courbetb4493792018-04-10 08:16:37 +0000769 EmitPfmCounters(ProcModel, HasPfmIssueCounters, OS);
770
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000771 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000772}
773
Andrew Trick23f3c652012-09-17 22:18:45 +0000774void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
775 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000776 EmitProcessorResourceSubUnits(ProcModel, OS);
777
778 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000779 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
780 << "ProcResources"
781 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000782 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000783
Clement Courbet39911e22018-02-08 08:46:48 +0000784 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000785 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
786 Record *PRDef = ProcModel.ProcResourceDefs[i];
787
Craig Topper24064772014-04-15 07:20:03 +0000788 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000789 unsigned SuperIdx = 0;
790 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000791 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000792 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000793 if (PRDef->isSubClassOf("ProcResGroup")) {
794 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000795 for (Record *RU : ResUnits) {
796 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000797 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000798 }
799 }
800 else {
801 // Find the SuperIdx
802 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000803 SuperDef =
804 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
805 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000806 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
807 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000808 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000809 }
810 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000811 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000812 if (PRDef->getName().size() < 15)
813 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000814 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
815 if (SubUnitsBeginOffset != SubUnitsOffset) {
816 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
817 << SubUnitsBeginOffset;
818 } else {
819 OS << "nullptr";
820 }
821 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000822 if (SuperDef)
823 OS << ", Super=" << SuperDef->getName();
824 OS << "\n";
825 }
826 OS << "};\n";
827}
828
Andrew Trick9ef08822012-09-17 22:18:48 +0000829// Find the WriteRes Record that defines processor resources for this
830// SchedWrite.
831Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000832 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000833
834 // Check if the SchedWrite is already subtarget-specific and directly
835 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000836 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
837 return SchedWrite.TheDef;
838
Craig Topper24064772014-04-15 07:20:03 +0000839 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000840 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000841 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000842 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000843 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
844 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
845 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
846 continue;
847 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000848 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000849 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000850 "defined for processor " + ProcModel.ModelName +
851 " Ensure only one SchedAlias exists per RW.");
852 AliasDef = AliasRW.TheDef;
853 }
854 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
855 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000856
857 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000858 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000859 for (Record *WR : ProcModel.WriteResDefs) {
860 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000861 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000862 if (AliasDef == WR->getValueAsDef("WriteType")
863 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000864 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000865 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000866 "SchedWrite and its alias on processor " +
867 ProcModel.ModelName);
868 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000869 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000870 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000871 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000872 // TODO: If ProcModel has a base model (previous generation processor),
873 // then call FindWriteResources recursively with that model here.
874 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000875 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000876 Twine("Processor does not define resources for ") +
877 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000878 }
879 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000880}
881
882/// Find the ReadAdvance record for the given SchedRead on this processor or
883/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000884Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000885 const CodeGenProcModel &ProcModel) {
886 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000887 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
888 return SchedRead.TheDef;
889
890 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000891 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000892 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000893 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000894 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000895 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
896 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
897 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
898 continue;
899 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000900 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000901 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000902 "defined for processor " + ProcModel.ModelName +
903 " Ensure only one SchedAlias exists per RW.");
904 AliasDef = AliasRW.TheDef;
905 }
906 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
907 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000908
909 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000910 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000911 for (Record *RA : ProcModel.ReadAdvanceDefs) {
912 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000913 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000914 if (AliasDef == RA->getValueAsDef("ReadType")
915 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000916 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000917 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000918 "SchedRead and its alias on processor " +
919 ProcModel.ModelName);
920 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000921 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000922 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000923 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000924 // TODO: If ProcModel has a base model (previous generation processor),
925 // then call FindReadAdvance recursively with that model here.
926 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000927 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000928 Twine("Processor does not define resources for ") +
929 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000930 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000931 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000932}
933
Andrew Trick4e67cba2013-03-14 21:21:50 +0000934// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000935// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000936void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
937 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000938 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000939 // Default to 1 resource cycle.
940 Cycles.resize(PRVec.size(), 1);
941 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000942 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000943 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000944 if (PRDef->isSubClassOf("ProcResGroup"))
945 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000946 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000947 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000948 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000949 for (Record *SubDef = PRDef;
950 SubDef->getValueInit("Super")->isComplete();) {
951 if (SubDef->isSubClassOf("ProcResGroup")) {
952 // Disallow this for simplicitly.
953 PrintFatalError(SubDef->getLoc(), "Processor resource group "
954 " cannot be a super resources.");
955 }
956 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000957 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
958 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000959 PRVec.push_back(SuperDef);
960 Cycles.push_back(Cycles[i]);
961 SubDef = SuperDef;
962 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000963 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000964 for (Record *PR : PM.ProcResourceDefs) {
965 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000966 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000967 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000968 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000969 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000970 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000971 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000972 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000973 }
974 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000975 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000976 Cycles.push_back(Cycles[i]);
977 }
978 }
979 }
980}
981
Andrew Trick9ef08822012-09-17 22:18:48 +0000982// Generate the SchedClass table for this processor and update global
983// tables. Must be called for each processor in order.
984void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
985 SchedClassTables &SchedTables) {
986 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
987 if (!ProcModel.hasInstrSchedModel())
988 return;
989
990 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Joel Jones80372332017-06-28 00:06:40 +0000991 DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000992 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
993 DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000994
Andrew Trick9ef08822012-09-17 22:18:48 +0000995 SCTab.resize(SCTab.size() + 1);
996 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000997 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000998 SCDesc.NumMicroOps = 0;
999 SCDesc.BeginGroup = false;
1000 SCDesc.EndGroup = false;
1001 SCDesc.WriteProcResIdx = 0;
1002 SCDesc.WriteLatencyIdx = 0;
1003 SCDesc.ReadAdvanceIdx = 0;
1004
1005 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +00001006 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +00001007 for (const CodeGenSchedTransition &CGT :
1008 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1009 if (CGT.ProcIndices[0] == 0 ||
1010 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001011 HasVariants = true;
1012 break;
1013 }
1014 }
1015 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001016 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1017 continue;
1018 }
1019
1020 // Determine if the SchedClass is actually reachable on this processor. If
1021 // not don't try to locate the processor resources, it will fail.
1022 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001023 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1024 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +00001025 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001026 continue;
1027 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001028 IdxVec Writes = SC.Writes;
1029 IdxVec Reads = SC.Reads;
1030 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001031 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001032 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001033 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001034 for (Record *RW : SC.InstRWs) {
1035 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001036 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001037 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001038 break;
1039 }
1040 }
1041 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001042 Writes.clear();
1043 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001044 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1045 Writes, Reads);
1046 }
1047 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001048 if (Writes.empty()) {
1049 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001050 for (Record *I : ProcModel.ItinRWDefs) {
1051 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001052 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001053 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001054 Writes, Reads);
1055 break;
1056 }
1057 }
1058 if (Writes.empty()) {
1059 DEBUG(dbgs() << ProcModel.ModelName
Craig Topper29c55dcb2016-02-13 06:03:32 +00001060 << " does not have resources for class " << SC.Name << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001061 }
1062 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001063 // Sum resources across all operand writes.
1064 std::vector<MCWriteProcResEntry> WriteProcResources;
1065 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001066 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001067 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001068 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001069 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001070 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001071 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001072
1073 // For each operand, create a latency entry.
1074 MCWriteLatencyEntry WLEntry;
1075 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001076 unsigned WriteID = WriteSeq.back();
1077 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1078 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1079 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001080 if (!SchedModels.hasReadOfWrite(
1081 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001082 WriteID = 0;
1083 }
1084 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001085
Craig Topper29c55dcb2016-02-13 06:03:32 +00001086 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001087
Andrew Trick9257b8f2012-09-22 02:24:21 +00001088 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001089 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001090
1091 // Mark the parent class as invalid for unsupported write types.
1092 if (WriteRes->getValueAsBit("Unsupported")) {
1093 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1094 break;
1095 }
1096 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1097 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1098 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1099 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001100 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1101 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001102
1103 // Create an entry for each ProcResource listed in WriteRes.
1104 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1105 std::vector<int64_t> Cycles =
1106 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001107
1108 ExpandProcResources(PRVec, Cycles, ProcModel);
1109
Andrew Trick9ef08822012-09-17 22:18:48 +00001110 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1111 PRIdx != PREnd; ++PRIdx) {
1112 MCWriteProcResEntry WPREntry;
1113 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1114 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001115 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001116 // If this resource is already used in this sequence, add the current
1117 // entry's cycles so that the same resource appears to be used
1118 // serially, rather than multiple parallel uses. This is important for
1119 // in-order machine where the resource consumption is a hazard.
1120 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1121 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1122 if (WriteProcResources[WPRIdx].ProcResourceIdx
1123 == WPREntry.ProcResourceIdx) {
1124 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1125 break;
1126 }
1127 }
1128 if (WPRIdx == WPREnd)
1129 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001130 }
1131 }
1132 WriteLatencies.push_back(WLEntry);
1133 }
1134 // Create an entry for each operand Read in this SchedClass.
1135 // Entries must be sorted first by UseIdx then by WriteResourceID.
1136 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1137 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001138 Record *ReadAdvance =
1139 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001140 if (!ReadAdvance)
1141 continue;
1142
1143 // Mark the parent class as invalid for unsupported write types.
1144 if (ReadAdvance->getValueAsBit("Unsupported")) {
1145 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1146 break;
1147 }
1148 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1149 IdxVec WriteIDs;
1150 if (ValidWrites.empty())
1151 WriteIDs.push_back(0);
1152 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001153 for (Record *VW : ValidWrites) {
1154 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001155 }
1156 }
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001157 llvm::sort(WriteIDs.begin(), WriteIDs.end());
Craig Topper29c55dcb2016-02-13 06:03:32 +00001158 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001159 MCReadAdvanceEntry RAEntry;
1160 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001161 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001162 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1163 ReadAdvanceEntries.push_back(RAEntry);
1164 }
1165 }
1166 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1167 WriteProcResources.clear();
1168 WriteLatencies.clear();
1169 ReadAdvanceEntries.clear();
1170 }
1171 // Add the information for this SchedClass to the global tables using basic
1172 // compression.
1173 //
1174 // WritePrecRes entries are sorted by ProcResIdx.
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001175 llvm::sort(WriteProcResources.begin(), WriteProcResources.end(),
1176 LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001177
1178 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1179 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1180 std::search(SchedTables.WriteProcResources.begin(),
1181 SchedTables.WriteProcResources.end(),
1182 WriteProcResources.begin(), WriteProcResources.end());
1183 if (WPRPos != SchedTables.WriteProcResources.end())
1184 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1185 else {
1186 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1187 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1188 WriteProcResources.end());
1189 }
1190 // Latency entries must remain in operand order.
1191 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1192 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1193 std::search(SchedTables.WriteLatencies.begin(),
1194 SchedTables.WriteLatencies.end(),
1195 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001196 if (WLPos != SchedTables.WriteLatencies.end()) {
1197 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1198 SCDesc.WriteLatencyIdx = idx;
1199 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1200 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1201 std::string::npos) {
1202 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1203 }
1204 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001205 else {
1206 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001207 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1208 WriteLatencies.begin(),
1209 WriteLatencies.end());
1210 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1211 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001212 }
1213 // ReadAdvanceEntries must remain in operand order.
1214 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1215 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1216 std::search(SchedTables.ReadAdvanceEntries.begin(),
1217 SchedTables.ReadAdvanceEntries.end(),
1218 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1219 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1220 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1221 else {
1222 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1223 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1224 ReadAdvanceEntries.end());
1225 }
1226 }
1227}
1228
Andrew Tricka72fca62012-09-17 22:18:50 +00001229// Emit SchedClass tables for all processors and associated global tables.
1230void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1231 raw_ostream &OS) {
1232 // Emit global WriteProcResTable.
1233 OS << "\n// {ProcResourceIdx, Cycles}\n"
1234 << "extern const llvm::MCWriteProcResEntry "
1235 << Target << "WriteProcResTable[] = {\n"
1236 << " { 0, 0}, // Invalid\n";
1237 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1238 WPRIdx != WPREnd; ++WPRIdx) {
1239 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1240 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1241 << format("%2d", WPREntry.Cycles) << "}";
1242 if (WPRIdx + 1 < WPREnd)
1243 OS << ',';
1244 OS << " // #" << WPRIdx << '\n';
1245 }
1246 OS << "}; // " << Target << "WriteProcResTable\n";
1247
1248 // Emit global WriteLatencyTable.
1249 OS << "\n// {Cycles, WriteResourceID}\n"
1250 << "extern const llvm::MCWriteLatencyEntry "
1251 << Target << "WriteLatencyTable[] = {\n"
1252 << " { 0, 0}, // Invalid\n";
1253 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1254 WLIdx != WLEnd; ++WLIdx) {
1255 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1256 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1257 << format("%2d", WLEntry.WriteResourceID) << "}";
1258 if (WLIdx + 1 < WLEnd)
1259 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001260 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001261 }
1262 OS << "}; // " << Target << "WriteLatencyTable\n";
1263
1264 // Emit global ReadAdvanceTable.
1265 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1266 << "extern const llvm::MCReadAdvanceEntry "
1267 << Target << "ReadAdvanceTable[] = {\n"
1268 << " {0, 0, 0}, // Invalid\n";
1269 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1270 RAIdx != RAEnd; ++RAIdx) {
1271 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1272 OS << " {" << RAEntry.UseIdx << ", "
1273 << format("%2d", RAEntry.WriteResourceID) << ", "
1274 << format("%2d", RAEntry.Cycles) << "}";
1275 if (RAIdx + 1 < RAEnd)
1276 OS << ',';
1277 OS << " // #" << RAIdx << '\n';
1278 }
1279 OS << "}; // " << Target << "ReadAdvanceTable\n";
1280
1281 // Emit a SchedClass table for each processor.
1282 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1283 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1284 if (!PI->hasInstrSchedModel())
1285 continue;
1286
1287 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001288 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001289
1290 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1291 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1292 OS << "static const llvm::MCSchedClassDesc "
1293 << PI->ModelName << "SchedClasses[] = {\n";
1294
1295 // The first class is always invalid. We no way to distinguish it except by
1296 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001297 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001298 && "invalid class not first");
1299 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1300 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001301 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001302
1303 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1304 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1305 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1306 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1307 if (SchedClass.Name.size() < 18)
1308 OS.indent(18 - SchedClass.Name.size());
1309 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001310 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1311 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001312 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1313 << ", " << MCDesc.NumWriteProcResEntries
1314 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1315 << ", " << MCDesc.NumWriteLatencyEntries
1316 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001317 << ", " << MCDesc.NumReadAdvanceEntries
1318 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001319 }
1320 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1321 }
1322}
1323
Andrew Trick87255e32012-07-07 04:00:00 +00001324void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1325 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001326 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001327 // Emit extra processor info if available.
1328 if (PM.hasExtraProcessorInfo())
1329 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001330 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001331 if (PM.hasInstrSchedModel())
1332 EmitProcessorResources(PM, OS);
1333 else if(!PM.ProcResourceDefs.empty())
1334 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001335 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001336
Andrew Trick73d77362012-06-05 03:44:40 +00001337 // Begin processor itinerary properties
1338 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001339 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1340 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1341 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1342 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1343 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1344 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1345 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001346
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001347 bool PostRAScheduler =
1348 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001349
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001350 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1351 << "PostRAScheduler\n";
1352
1353 bool CompleteModel =
1354 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1355
1356 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1357 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001358
Craig Topper29c55dcb2016-02-13 06:03:32 +00001359 OS << " " << PM.Index << ", // Processor ID\n";
1360 if (PM.hasInstrSchedModel())
1361 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1362 << " " << PM.ModelName << "SchedClasses" << ",\n"
1363 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001364 << " " << (SchedModels.schedClassEnd()
1365 - SchedModels.schedClassBegin()) << ",\n";
1366 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001367 OS << " nullptr, nullptr, 0, 0,"
1368 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001369 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001370 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001371 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001372 OS << " nullptr, // No Itinerary\n";
1373 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001374 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001375 else
Clement Courbetb4493792018-04-10 08:16:37 +00001376 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001377 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001378 }
Jim Laskey3763a502005-10-31 17:16:01 +00001379}
1380
1381//
1382// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1383//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001384void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001385 // Gather and sort processor information
1386 std::vector<Record*> ProcessorList =
1387 Records.getAllDerivedDefinitions("Processor");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001388 llvm::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001389
1390 // Begin processor table
1391 OS << "\n";
1392 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001393 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001394 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001395
Jim Laskey3763a502005-10-31 17:16:01 +00001396 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001397 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001398 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001399 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001400 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001401
Jim Laskey3763a502005-10-31 17:16:01 +00001402 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001403 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001404 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001405
Jim Laskey3763a502005-10-31 17:16:01 +00001406 // End processor table
1407 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001408}
1409
1410//
Andrew Trick87255e32012-07-07 04:00:00 +00001411// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001412//
Andrew Trick87255e32012-07-07 04:00:00 +00001413void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001414 OS << "#ifdef DBGFIELD\n"
1415 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1416 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001417 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001418 << "#define DBGFIELD(x) x,\n"
1419 << "#else\n"
1420 << "#define DBGFIELD(x)\n"
1421 << "#endif\n";
1422
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001423 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001424 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001425 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001426 EmitStageAndOperandCycleData(OS, ProcItinLists);
1427 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001428 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001429 OS << "\n// ===============================================================\n"
1430 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001431
Andrew Trick9ef08822012-09-17 22:18:48 +00001432 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001433 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1434 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001435 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001436 EmitSchedClassTables(SchedTables, OS);
1437
1438 // Emit the processor machine model
1439 EmitProcessorModels(OS);
1440 // Emit the processor lookup data
1441 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001442
Craig Topper194cb742017-10-24 15:50:55 +00001443 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001444}
1445
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001446static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1447 std::string Buffer;
1448 raw_string_ostream Stream(Buffer);
1449
1450 // Collect all the PredicateProlog records and print them to the output
1451 // stream.
1452 std::vector<Record *> Prologs =
1453 Records.getAllDerivedDefinitions("PredicateProlog");
1454 llvm::sort(Prologs.begin(), Prologs.end(), LessRecord());
1455 for (Record *P : Prologs)
1456 Stream << P->getValueAsString("Code") << '\n';
1457
1458 Stream.flush();
1459 OS << Buffer;
1460}
1461
1462static void emitPredicates(const CodeGenSchedTransition &T,
1463 const CodeGenSchedClass &SC, unsigned ProcIdx,
1464 raw_ostream &OS) {
1465 if (ProcIdx && !count(T.ProcIndices, ProcIdx))
1466 return;
1467
1468 std::string Buffer;
1469 raw_string_ostream Stream(Buffer);
1470 Stream << " if (";
1471 for (RecIter RI = T.PredTerm.begin(), RE = T.PredTerm.end(); RI != RE; ++RI) {
1472 if (RI != T.PredTerm.begin())
1473 Stream << "\n && ";
1474 Stream << "(" << (*RI)->getValueAsString("Predicate") << ")";
1475 }
1476
1477 Stream << ")\n"
1478 << " return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1479 Stream.flush();
1480 OS << Buffer;
1481}
1482
Benjamin Kramerc321e532016-06-08 19:09:22 +00001483void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
Andrew Trickc6c88152012-09-18 03:41:43 +00001484 raw_ostream &OS) {
1485 OS << "unsigned " << ClassName
1486 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1487 << " const TargetSchedModel *SchedModel) const {\n";
1488
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001489 // Emit the predicate prolog code.
1490 emitPredicateProlog(Records, OS);
1491
1492 // Collect Variant Classes.
Andrew Trickc6c88152012-09-18 03:41:43 +00001493 IdxVec VariantClasses;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001494 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1495 if (SC.Transitions.empty())
Andrew Trickc6c88152012-09-18 03:41:43 +00001496 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001497 VariantClasses.push_back(SC.Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001498 }
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001499
Andrew Trickc6c88152012-09-18 03:41:43 +00001500 if (!VariantClasses.empty()) {
1501 OS << " switch (SchedClass) {\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001502 for (unsigned VC : VariantClasses) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001503 // Emit code for each variant scheduling class.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001504 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1505 OS << " case " << VC << ": // " << SC.Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001506 IdxVec ProcIndices;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001507 for (const CodeGenSchedTransition &T : SC.Transitions) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001508 IdxVec PI;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001509 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
Andrew Trickc6c88152012-09-18 03:41:43 +00001510 ProcIndices.begin(), ProcIndices.end(),
1511 std::back_inserter(PI));
1512 ProcIndices.swap(PI);
1513 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001514 for (unsigned PI : ProcIndices) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001515 OS << " ";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001516 if (PI != 0)
1517 OS << "if (SchedModel->getProcessorID() == " << PI << ") ";
1518 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName
Andrew Trickc6c88152012-09-18 03:41:43 +00001519 << '\n';
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001520
1521 for (const CodeGenSchedTransition &T : SC.Transitions)
1522 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PI, OS);
1523
Andrew Trickc6c88152012-09-18 03:41:43 +00001524 OS << " }\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001525 if (PI == 0)
Andrew Trickc6c88152012-09-18 03:41:43 +00001526 break;
1527 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001528 if (SC.isInferred())
1529 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001530 OS << " break;\n";
1531 }
1532 OS << " };\n";
1533 }
1534 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1535 << "} // " << ClassName << "::resolveSchedClass\n";
1536}
1537
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001538void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1539 raw_ostream &OS) {
1540 const CodeGenHwModes &CGH = TGT.getHwModes();
1541 assert(CGH.getNumModeIds() > 0);
1542 if (CGH.getNumModeIds() == 1)
1543 return;
1544
1545 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1546 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1547 const HwMode &HM = CGH.getMode(M);
1548 OS << " if (checkFeatures(\"" << HM.Features
1549 << "\")) return " << M << ";\n";
1550 }
1551 OS << " return 0;\n}\n";
1552}
1553
Jim Laskey86f002c2005-10-27 19:47:21 +00001554//
Jim Laskeya2b52352005-10-26 17:30:34 +00001555// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1556// the subtarget features string.
1557//
Evan Cheng54b68e32011-07-01 20:45:01 +00001558void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1559 unsigned NumFeatures,
1560 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001561 std::vector<Record*> Features =
1562 Records.getAllDerivedDefinitions("SubtargetFeature");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001563 llvm::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001564
Andrew Trickdb6ed642011-04-01 01:56:55 +00001565 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1566 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001567 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001568 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001569 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001570 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001571 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001572
1573 if (Features.empty()) {
1574 OS << "}\n";
1575 return;
1576 }
1577
Andrew Trickba7b9212012-09-18 05:33:15 +00001578 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001579 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001580
Craig Topper29c55dcb2016-02-13 06:03:32 +00001581 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001582 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001583 StringRef Instance = R->getName();
1584 StringRef Value = R->getValueAsString("Value");
1585 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001586
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001587 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001588 OS << " if (Bits[" << Target << "::"
1589 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001590 << Attribute << " = " << Value << ";\n";
1591 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001592 OS << " if (Bits[" << Target << "::"
1593 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001594 << Attribute << " < " << Value << ") "
1595 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001596 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001597
Evan Chengfe6e4052011-06-30 01:53:36 +00001598 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001599}
1600
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001601//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001602// SubtargetEmitter::run - Main subtarget enumeration emitter.
1603//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001604void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001605 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001606
Evan Cheng4d1ca962011-07-08 01:53:10 +00001607 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001608 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001609
1610 OS << "namespace llvm {\n";
Craig Topper094bbca2016-02-14 05:22:01 +00001611 Enumeration(OS);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001612 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001613 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1614
Evan Cheng54b68e32011-07-01 20:45:01 +00001615 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001616 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001617
Evan Cheng54b68e32011-07-01 20:45:01 +00001618 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001619#if 0
1620 OS << "namespace {\n";
1621#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001622 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001623 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001624 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001625 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001626 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001627 OS << "\n";
1628#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001629 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001630#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001631
1632 // MCInstrInfo initialization routine.
Craig Topper194cb742017-10-24 15:50:55 +00001633 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001634 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001635 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001636 OS << " return new MCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001637 if (NumFeatures)
1638 OS << Target << "FeatureKV, ";
1639 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001640 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001641 if (NumProcs)
1642 OS << Target << "SubTypeKV, ";
1643 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001644 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001645 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001646 OS << Target << "ProcSchedKV, "
1647 << Target << "WriteProcResTable, "
1648 << Target << "WriteLatencyTable, "
1649 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001650 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001651 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001652 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001653 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001654 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001655 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001656 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001657 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001658
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001659 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001660
1661 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1662
1663 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001664 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001665
1666 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001667 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001668 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1669
1670 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1671
Evan Cheng0d639a22011-07-01 21:01:15 +00001672 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001673 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001674 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001675
1676 std::string ClassName = Target + "GenSubtargetInfo";
1677 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001678 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001679 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001680 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001681 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001682 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001683 << " unsigned resolveSchedClass(unsigned SchedClass, "
1684 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001685 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001686 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001687 << " const;\n";
1688 if (TGT.getHwModes().getNumModeIds() > 1)
1689 OS << " unsigned getHwMode() const override;\n";
1690 OS << "};\n"
1691 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001692
1693 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1694
1695 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001696 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001697
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001698 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001699 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001700 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1701 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001702 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1703 OS << "extern const llvm::MCWriteProcResEntry "
1704 << Target << "WriteProcResTable[];\n";
1705 OS << "extern const llvm::MCWriteLatencyEntry "
1706 << Target << "WriteLatencyTable[];\n";
1707 OS << "extern const llvm::MCReadAdvanceEntry "
1708 << Target << "ReadAdvanceTable[];\n";
1709
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001710 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001711 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1712 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001713 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001714 }
1715
Daniel Sanders50f17232015-09-15 16:17:27 +00001716 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1717 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001718 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001719 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001720 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001721 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001722 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001723 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001724 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001725 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001726 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001727 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001728 OS << Target << "ProcSchedKV, "
1729 << Target << "WriteProcResTable, "
1730 << Target << "WriteLatencyTable, "
1731 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001732 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001733 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001734 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001735 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001736 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001737 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001738 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001739 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001740
Andrew Trickc6c88152012-09-18 03:41:43 +00001741 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001742 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001743
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001744 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001745
1746 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001747}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001748
1749namespace llvm {
1750
1751void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001752 CodeGenTarget CGTarget(RK);
1753 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001754}
1755
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001756} // end namespace llvm