blob: 4da8012732cc83a676efa6faeffb29fb227f9959 [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"
Andrea Di Biagio95140022018-05-25 15:55:37 +000016#include "PredicateExpander.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000017#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000019#include "llvm/ADT/StringExtras.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000020#include "llvm/ADT/StringRef.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000022#include "llvm/MC/MCSchedule.h"
Michael Kupersteindb0712f2015-05-26 10:47:10 +000023#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/Format.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000026#include "llvm/Support/raw_ostream.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000027#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000028#include "llvm/TableGen/Record.h"
29#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenb0aa47b2005-10-28 01:43:09 +000030#include <algorithm>
Eugene Zelenko75259bb2016-05-17 17:04:23 +000031#include <cassert>
32#include <cstdint>
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000033#include <iterator>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000034#include <map>
35#include <string>
36#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000037
Jim Laskeycfda85a2005-10-21 19:00:04 +000038using namespace llvm;
39
Chandler Carruth97acce22014-04-22 03:06:00 +000040#define DEBUG_TYPE "subtarget-emitter"
41
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000042namespace {
Eugene Zelenko75259bb2016-05-17 17:04:23 +000043
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000045 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
46 // The SchedClassDesc table indexes into a global write resource table, write
47 // latency table, and read advance table.
48 struct SchedClassTables {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000049 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
Andrew Trick9ef08822012-09-17 22:18:48 +000050 std::vector<MCWriteProcResEntry> WriteProcResources;
51 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000052 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000053 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
54
55 // Reserve an invalid entry at index 0
56 SchedClassTables() {
57 ProcSchedClasses.resize(1);
58 WriteProcResources.resize(1);
59 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000060 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000061 ReadAdvanceEntries.resize(1);
62 }
63 };
64
65 struct LessWriteProcResources {
66 bool operator()(const MCWriteProcResEntry &LHS,
67 const MCWriteProcResEntry &RHS) {
68 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
69 }
70 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000071
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +000072 const CodeGenTarget &TGT;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000073 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000074 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000075 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000076
Craig Topper094bbca2016-02-14 05:22:01 +000077 void Enumeration(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000078 unsigned FeatureKeyValues(raw_ostream &OS);
79 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000080 void FormItineraryStageString(const std::string &Names,
81 Record *ItinData, std::string &ItinString,
82 unsigned &NStages);
83 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
84 unsigned &NOperandCycles);
85 void FormItineraryBypassString(const std::string &Names,
86 Record *ItinData,
87 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000088 void EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000089 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000090 &ProcItinLists);
91 void EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000092 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000093 &ProcItinLists);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000094 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
95 raw_ostream &OS);
96 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
97 raw_ostream &OS);
Mehdi Amini32986ed2016-10-04 23:47:33 +000098 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000099 char Separator);
Clement Courbet39911e22018-02-08 08:46:48 +0000100 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
101 raw_ostream &OS);
Andrew Trick23f3c652012-09-17 22:18:45 +0000102 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
103 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000104 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +0000105 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000106 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
107 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000108 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
109 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000110 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
111 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +0000112 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000113 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000114 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000115 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000116 void emitSchedModelHelpersImpl(raw_ostream &OS,
117 bool OnlyExpandMCInstPredicates = false);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +0000118 void emitGenMCSubtargetInfo(raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000119
Andrew Trick87255e32012-07-07 04:00:00 +0000120 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000121 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000122 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
123 unsigned NumProcs);
124
125public:
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000126 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
127 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
128 Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000129
130 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000131};
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000132
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000133} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000134
Jim Laskeya1beea62005-10-22 07:59:56 +0000135//
Jim Laskeya2b52352005-10-26 17:30:34 +0000136// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000137//
Craig Topper094bbca2016-02-14 05:22:01 +0000138void SubtargetEmitter::Enumeration(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000139 // Get all records of class and sort
Craig Topper094bbca2016-02-14 05:22:01 +0000140 std::vector<Record*> DefList =
141 Records.getAllDerivedDefinitions("SubtargetFeature");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000142 llvm::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000143
Evan Chenga2e61292011-04-15 19:35:46 +0000144 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000145 if (N == 0)
146 return;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000147 if (N > MAX_SUBTARGET_FEATURES)
148 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chenga2e61292011-04-15 19:35:46 +0000149
Evan Cheng54b68e32011-07-01 20:45:01 +0000150 OS << "namespace " << Target << " {\n";
151
Craig Topperbcdb0f22016-02-13 17:58:14 +0000152 // Open enumeration.
Craig Topper2d45c1d2016-02-13 06:03:29 +0000153 OS << "enum {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000154
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000155 // For each record
Craig Topperdf1285b2017-10-24 15:50:53 +0000156 for (unsigned i = 0; i < N; ++i) {
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000157 // Next record
158 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000159
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000160 // Get and emit name
Craig Topperdf1285b2017-10-24 15:50:53 +0000161 OS << " " << Def->getName() << " = " << i << ",\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000162 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000163
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000164 // Close enumeration and namespace
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000165 OS << "};\n";
166 OS << "} // end namespace " << Target << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000167}
168
169//
Bill Wendlinge6182262007-05-04 20:38:40 +0000170// FeatureKeyValues - Emit data of all the subtarget features. Used by the
171// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000172//
Evan Cheng54b68e32011-07-01 20:45:01 +0000173unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000174 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000175 std::vector<Record*> FeatureList =
176 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000177
178 if (FeatureList.empty())
179 return 0;
180
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000181 llvm::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000182
Jim Laskey19595752005-10-28 15:20:43 +0000183 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000184 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000185 << "extern const llvm::SubtargetFeatureKV " << Target
186 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000187
Jim Laskey19595752005-10-28 15:20:43 +0000188 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000189 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000190 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000191 // Next feature
192 Record *Feature = FeatureList[i];
193
Craig Topperbcd3c372017-05-31 21:12:46 +0000194 StringRef Name = Feature->getName();
195 StringRef CommandLineName = Feature->getValueAsString("Name");
196 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000197
Jim Laskey3f7d0472006-12-12 20:55:58 +0000198 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000199
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000200 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000201 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000202 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000203 << "\"" << Desc << "\", "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000204 << "{ " << Target << "::" << Name << " }, ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000205
Craig Topper37eeb322018-03-23 00:02:45 +0000206 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000207
Craig Topper4ceea0a2016-01-03 08:57:41 +0000208 OS << "{";
209 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
210 OS << " " << Target << "::" << ImpliesList[j]->getName();
211 if (++j < M) OS << ",";
Bill Wendlinge6182262007-05-04 20:38:40 +0000212 }
Craig Topperdf1285b2017-10-24 15:50:53 +0000213 OS << " } },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000214 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000215 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000216
Jim Laskey19595752005-10-28 15:20:43 +0000217 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000218 OS << "};\n";
219
Evan Cheng54b68e32011-07-01 20:45:01 +0000220 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000221}
222
223//
224// CPUKeyValues - Emit data of all the subtarget processors. Used by command
225// line.
226//
Evan Cheng54b68e32011-07-01 20:45:01 +0000227unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000228 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000229 std::vector<Record*> ProcessorList =
230 Records.getAllDerivedDefinitions("Processor");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +0000231 llvm::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000232
Jim Laskey19595752005-10-28 15:20:43 +0000233 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000234 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000235 << "extern const llvm::SubtargetFeatureKV " << Target
236 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000237
Jim Laskey19595752005-10-28 15:20:43 +0000238 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000239 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000240 StringRef Name = Processor->getValueAsString("Name");
Craig Topper37eeb322018-03-23 00:02:45 +0000241 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000242
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000243 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000244 OS << " { "
245 << "\"" << Name << "\", "
246 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000247
Craig Topper4ceea0a2016-01-03 08:57:41 +0000248 OS << "{";
249 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
250 OS << " " << Target << "::" << FeatureList[j]->getName();
251 if (++j < M) OS << ",";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000252 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000253 // The { } is for the "implies" section of this data structure.
Craig Topperdf1285b2017-10-24 15:50:53 +0000254 OS << " }, { } },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000255 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000256
Jim Laskey19595752005-10-28 15:20:43 +0000257 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000258 OS << "};\n";
259
Evan Cheng54b68e32011-07-01 20:45:01 +0000260 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000261}
Jim Laskeya1beea62005-10-22 07:59:56 +0000262
Jim Laskeya2b52352005-10-26 17:30:34 +0000263//
David Goodwind813cbf2009-08-17 16:02:57 +0000264// FormItineraryStageString - Compose a string containing the stage
265// data initialization for the specified itinerary. N is the number
266// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000267//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000268void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
269 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000270 std::string &ItinString,
271 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000272 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000273 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000274
275 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000276 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000277 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000278 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000279 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000280
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000281 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000282 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000283 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000284
Jim Laskeydffe5972005-10-28 21:47:29 +0000285 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000286 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000287
Jim Laskey19595752005-10-28 15:20:43 +0000288 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000289 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000290 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000291 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000292 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000293 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000294
David Goodwinb369ee42009-08-12 18:31:53 +0000295 int TimeInc = Stage->getValueAsInt("TimeInc");
296 ItinString += ", " + itostr(TimeInc);
297
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000298 int Kind = Stage->getValueAsInt("Kind");
299 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
300
Jim Laskey19595752005-10-28 15:20:43 +0000301 // Close off stage
302 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000303 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000304 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000305}
306
307//
David Goodwind813cbf2009-08-17 16:02:57 +0000308// FormItineraryOperandCycleString - Compose a string containing the
309// operand cycle initialization for the specified itinerary. N is the
310// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000311//
David Goodwind813cbf2009-08-17 16:02:57 +0000312void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
313 std::string &ItinString, unsigned &NOperandCycles) {
314 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000315 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000316 ItinData->getValueAsListOfInts("OperandCycles");
317
318 // For each operand cycle
319 unsigned N = NOperandCycles = OperandCycleList.size();
320 for (unsigned i = 0; i < N;) {
321 // Next operand cycle
322 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000323
David Goodwind813cbf2009-08-17 16:02:57 +0000324 ItinString += " " + itostr(OCycle);
325 if (++i < N) ItinString += ", ";
326 }
327}
328
Evan Cheng0097dd02010-09-28 23:50:49 +0000329void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
330 Record *ItinData,
331 std::string &ItinString,
332 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000333 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000334 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000335 unsigned i = 0;
336 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000337 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000338 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000339 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000340 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000341 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000342 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000343 }
344}
345
David Goodwind813cbf2009-08-17 16:02:57 +0000346//
Andrew Trick87255e32012-07-07 04:00:00 +0000347// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
348// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
349// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000350//
Andrew Trick87255e32012-07-07 04:00:00 +0000351void SubtargetEmitter::
352EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000353 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000354 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000355 // Multiple processor models may share an itinerary record. Emit it once.
356 SmallPtrSet<Record*, 8> ItinsDefSet;
357
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000358 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000359 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000360
Craig Topper29c55dcb2016-02-13 06:03:32 +0000361 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000362 continue;
363
Craig Topper37eeb322018-03-23 00:02:45 +0000364 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000365 if (FUs.empty())
366 continue;
367
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000368 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000369 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000370 << "namespace " << Name << "FU {\n";
371
372 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000373 OS << " const unsigned " << FUs[j]->getName()
374 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000375
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000376 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000377
Craig Topper37eeb322018-03-23 00:02:45 +0000378 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000379 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000380 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000381 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000382
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000383 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000384 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000385 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000386 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000387
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000388 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000389 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000390 }
391
Jim Laskey19595752005-10-28 15:20:43 +0000392 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000393 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
394 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000395 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000396
David Goodwind813cbf2009-08-17 16:02:57 +0000397 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000398 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000399 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000400 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000401
402 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000403 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000404 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000405 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000406
Andrew Trick87255e32012-07-07 04:00:00 +0000407 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000408 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000409 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000410 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000411 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000412 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000413 // Add process itinerary to the list.
414 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000415
Andrew Trick87255e32012-07-07 04:00:00 +0000416 // If this processor defines no itineraries, then leave the itinerary list
417 // empty.
418 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000419 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000420 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000421
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000422 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000423
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000424 ItinList.resize(SchedModels.numInstrSchedClasses());
425 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
426
427 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000428 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
429
Jim Laskeydffe5972005-10-28 21:47:29 +0000430 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000431 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000432
Jim Laskey19595752005-10-28 15:20:43 +0000433 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000434 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000435 unsigned NStages = 0;
436 if (ItinData)
437 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000438
David Goodwind813cbf2009-08-17 16:02:57 +0000439 // Get string and operand cycle count
440 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000441 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000442 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000443 if (ItinData) {
444 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
445 NOperandCycles);
446
447 FormItineraryBypassString(Name, ItinData, ItinBypassString,
448 NOperandCycles);
449 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000450
David Goodwind813cbf2009-08-17 16:02:57 +0000451 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000452 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000453 if (NStages > 0) {
454 FindStage = ItinStageMap[ItinStageString];
455 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000456 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
457 StageTable += ItinStageString + ", // " + itostr(StageCount);
458 if (NStages > 1)
459 StageTable += "-" + itostr(StageCount + NStages - 1);
460 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000461 // Record Itin class number.
462 ItinStageMap[ItinStageString] = FindStage = StageCount;
463 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000464 }
465 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000466
David Goodwind813cbf2009-08-17 16:02:57 +0000467 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000468 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000469 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000470 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
471 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000472 if (FindOperandCycle == 0) {
473 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000474 OperandCycleTable += ItinOperandCycleString + ", // ";
475 std::string OperandIdxComment = itostr(OperandCycleCount);
476 if (NOperandCycles > 1)
477 OperandIdxComment += "-"
478 + itostr(OperandCycleCount + NOperandCycles - 1);
479 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000480 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000481 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000482 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000483 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000484 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000485 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000486 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000487 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000488
Evan Cheng367a5df2010-09-09 18:18:55 +0000489 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000490 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
491 InstrItinerary Intinerary = {
492 NumUOps,
493 FindStage,
494 uint16_t(FindStage + NStages),
495 FindOperandCycle,
496 uint16_t(FindOperandCycle + NOperandCycles),
497 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000498
Jim Laskey19595752005-10-28 15:20:43 +0000499 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000500 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000501 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000502 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000503
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000504 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000505 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000506 StageTable += "};\n";
507
508 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000509 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000510 OperandCycleTable += "};\n";
511
Andrew Trick87255e32012-07-07 04:00:00 +0000512 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000513 BypassTable += "};\n";
514
David Goodwind813cbf2009-08-17 16:02:57 +0000515 // Emit tables.
516 OS << StageTable;
517 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000518 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000519}
520
Andrew Trick87255e32012-07-07 04:00:00 +0000521//
522// EmitProcessorData - Generate data for processor itineraries that were
523// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
524// Itineraries for each processor. The Itinerary lists are indexed on
525// CodeGenSchedClass::Index.
526//
527void SubtargetEmitter::
528EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000529 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000530 // Multiple processor models may share an itinerary record. Emit it once.
531 SmallPtrSet<Record*, 8> ItinsDefSet;
532
Andrew Trick87255e32012-07-07 04:00:00 +0000533 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000534 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000535 ProcItinListsIter = ProcItinLists.begin();
536 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000537 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000538
Andrew Trick87255e32012-07-07 04:00:00 +0000539 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000540 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000541 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000542
Andrew Trick87255e32012-07-07 04:00:00 +0000543 // Get the itinerary list for the processor.
544 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000545 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000546
Pete Cooperc0eb1532014-09-02 23:23:34 +0000547 // Empty itineraries aren't referenced anywhere in the tablegen output
548 // so don't emit them.
549 if (ItinList.empty())
550 continue;
551
Andrew Trick87255e32012-07-07 04:00:00 +0000552 OS << "\n";
553 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000554
555 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000556 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000557
558 // For each itinerary class in CodeGenSchedClass::Index order.
559 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
560 InstrItinerary &Intinerary = ItinList[j];
561
562 // Emit Itinerary in the form of
563 // { firstStage, lastStage, firstCycle, lastCycle } // index
564 OS << " { " <<
565 Intinerary.NumMicroOps << ", " <<
566 Intinerary.FirstStage << ", " <<
567 Intinerary.LastStage << ", " <<
568 Intinerary.FirstOperandCycle << ", " <<
569 Intinerary.LastOperandCycle << " }" <<
570 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
571 }
572 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000573 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
574 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000575 OS << "};\n";
576 }
577}
578
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000579// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000580// value defined in the C++ header. The Record is null if the processor does not
581// define a model.
582void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000583 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000584 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000585 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000586 if (V >= 0)
587 OS << V << Separator << " // " << Name;
588 else
Andrew Trick87255e32012-07-07 04:00:00 +0000589 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000590 OS << '\n';
591}
592
Clement Courbet39911e22018-02-08 08:46:48 +0000593void SubtargetEmitter::EmitProcessorResourceSubUnits(
594 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
595 OS << "\nstatic const unsigned " << ProcModel.ModelName
596 << "ProcResourceSubUnits[] = {\n"
597 << " 0, // Invalid\n";
598
599 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
600 Record *PRDef = ProcModel.ProcResourceDefs[i];
601 if (!PRDef->isSubClassOf("ProcResGroup"))
602 continue;
603 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
604 for (Record *RUDef : ResUnits) {
605 Record *const RU =
606 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
607 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
608 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
609 }
610 }
611 OS << " // " << PRDef->getName() << "\n";
612 }
613 OS << "};\n";
614}
615
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000616static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
617 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000618 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000619 if (Record *RCU = ProcModel.RetireControlUnit) {
620 ReorderBufferSize =
621 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
622 MaxRetirePerCycle =
623 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
624 }
625
626 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
627 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
628}
629
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000630static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
631 unsigned NumRegisterFiles,
632 unsigned NumCostEntries, raw_ostream &OS) {
633 if (NumRegisterFiles)
634 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
635 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000636 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000637
638 OS << ", // Number of register files.\n ";
639 if (NumCostEntries)
640 OS << ProcModel.ModelName << "RegisterCosts,\n ";
641 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000642 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000643 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000644}
645
646unsigned
647SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
648 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000649 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
650 return RF.hasDefaultCosts();
651 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000652 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000653
654 // Print the RegisterCost table first.
655 OS << "\n// {RegisterClassID, Register Cost}\n";
656 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
657 << "RegisterCosts"
658 << "[] = {\n";
659
660 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
661 // Skip register files with a default cost table.
662 if (RF.hasDefaultCosts())
663 continue;
664 // Add entries to the cost table.
665 for (const CodeGenRegisterCost &RC : RF.Costs) {
666 OS << " { ";
667 Record *Rec = RC.RCDef;
668 if (Rec->getValue("Namespace"))
669 OS << Rec->getValueAsString("Namespace") << "::";
670 OS << Rec->getName() << "RegClassID, " << RC.Cost << "},\n";
671 }
672 }
673 OS << "};\n";
674
675 // Now generate a table with register file info.
676 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl}\n";
677 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
678 << "RegisterFiles"
679 << "[] = {\n"
680 << " { \"InvalidRegisterFile\", 0, 0, 0 },\n";
681 unsigned CostTblIndex = 0;
682
683 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
684 OS << " { ";
685 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
686 unsigned NumCostEntries = RD.Costs.size();
687 OS << NumCostEntries << ", " << CostTblIndex << "},\n";
688 CostTblIndex += NumCostEntries;
689 }
690 OS << "};\n";
691
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000692 return CostTblIndex;
693}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000694
Clement Courbetb4493792018-04-10 08:16:37 +0000695static bool EmitPfmIssueCountersTable(const CodeGenProcModel &ProcModel,
696 raw_ostream &OS) {
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000697 unsigned NumCounterDefs = 1 + ProcModel.ProcResourceDefs.size();
698 std::vector<const Record *> CounterDefs(NumCounterDefs);
Clement Courbetb4493792018-04-10 08:16:37 +0000699 bool HasCounters = false;
700 for (const Record *CounterDef : ProcModel.PfmIssueCounterDefs) {
701 const Record *&CD = CounterDefs[ProcModel.getProcResourceIdx(
702 CounterDef->getValueAsDef("Resource"))];
703 if (CD) {
704 PrintFatalError(CounterDef->getLoc(),
705 "multiple issue counters for " +
706 CounterDef->getValueAsDef("Resource")->getName());
707 }
708 CD = CounterDef;
709 HasCounters = true;
710 }
711 if (!HasCounters) {
712 return false;
713 }
714 OS << "\nstatic const char* " << ProcModel.ModelName
715 << "PfmIssueCounters[] = {\n";
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000716 for (unsigned i = 0; i != NumCounterDefs; ++i) {
717 const Record *CounterDef = CounterDefs[i];
Clement Courbetb4493792018-04-10 08:16:37 +0000718 if (CounterDef) {
719 const auto PfmCounters = CounterDef->getValueAsListOfStrings("Counters");
720 if (PfmCounters.empty())
721 PrintFatalError(CounterDef->getLoc(), "empty counter list");
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000722 OS << " \"" << PfmCounters[0];
723 for (unsigned p = 1, e = PfmCounters.size(); p != e; ++p)
724 OS << ",\" \"" << PfmCounters[p];
725 OS << "\", // #" << i << " = ";
726 OS << CounterDef->getValueAsDef("Resource")->getName() << "\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000727 } else {
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000728 OS << " nullptr, // #" << i << "\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000729 }
730 }
731 OS << "};\n";
732 return true;
733}
734
735static void EmitPfmCounters(const CodeGenProcModel &ProcModel,
736 const bool HasPfmIssueCounters, raw_ostream &OS) {
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000737 OS << " {\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000738 // Emit the cycle counter.
739 if (ProcModel.PfmCycleCounterDef)
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000740 OS << " \"" << ProcModel.PfmCycleCounterDef->getValueAsString("Counter")
Clement Courbetb4493792018-04-10 08:16:37 +0000741 << "\", // Cycle counter.\n";
742 else
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000743 OS << " nullptr, // No cycle counter.\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000744
745 // Emit a reference to issue counters table.
746 if (HasPfmIssueCounters)
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000747 OS << " " << ProcModel.ModelName << "PfmIssueCounters\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000748 else
Clement Courbetbfa20ddd2018-04-10 08:43:46 +0000749 OS << " nullptr // No issue counters.\n";
750 OS << " }\n";
Clement Courbetb4493792018-04-10 08:16:37 +0000751}
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000752
753void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
754 raw_ostream &OS) {
755 // Generate a table of register file descriptors (one entry per each user
756 // defined register file), and a table of register costs.
757 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
758
Clement Courbetb4493792018-04-10 08:16:37 +0000759 // Generate a table of ProcRes counter names.
760 const bool HasPfmIssueCounters = EmitPfmIssueCountersTable(ProcModel, OS);
761
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000762 // Now generate a table for the extra processor info.
763 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000764 << "ExtraInfo = {\n ";
765
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000766 // Add information related to the retire control unit.
767 EmitRetireControlUnitInfo(ProcModel, OS);
768
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000769 // Add information related to the register files (i.e. where to find register
770 // file descriptors and register costs).
771 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
772 NumCostEntries, OS);
773
Clement Courbetb4493792018-04-10 08:16:37 +0000774 EmitPfmCounters(ProcModel, HasPfmIssueCounters, OS);
775
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000776 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000777}
778
Andrew Trick23f3c652012-09-17 22:18:45 +0000779void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
780 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000781 EmitProcessorResourceSubUnits(ProcModel, OS);
782
783 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000784 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
785 << "ProcResources"
786 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000787 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000788
Clement Courbet39911e22018-02-08 08:46:48 +0000789 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000790 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
791 Record *PRDef = ProcModel.ProcResourceDefs[i];
792
Craig Topper24064772014-04-15 07:20:03 +0000793 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000794 unsigned SuperIdx = 0;
795 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000796 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000797 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000798 if (PRDef->isSubClassOf("ProcResGroup")) {
799 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000800 for (Record *RU : ResUnits) {
801 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000802 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000803 }
804 }
805 else {
806 // Find the SuperIdx
807 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000808 SuperDef =
809 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
810 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000811 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
812 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000813 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000814 }
815 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000816 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000817 if (PRDef->getName().size() < 15)
818 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000819 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
820 if (SubUnitsBeginOffset != SubUnitsOffset) {
821 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
822 << SubUnitsBeginOffset;
823 } else {
824 OS << "nullptr";
825 }
826 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000827 if (SuperDef)
828 OS << ", Super=" << SuperDef->getName();
829 OS << "\n";
830 }
831 OS << "};\n";
832}
833
Andrew Trick9ef08822012-09-17 22:18:48 +0000834// Find the WriteRes Record that defines processor resources for this
835// SchedWrite.
836Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000837 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000838
839 // Check if the SchedWrite is already subtarget-specific and directly
840 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000841 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
842 return SchedWrite.TheDef;
843
Craig Topper24064772014-04-15 07:20:03 +0000844 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000845 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000846 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000847 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000848 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
849 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
850 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
851 continue;
852 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000853 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000854 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000855 "defined for processor " + ProcModel.ModelName +
856 " Ensure only one SchedAlias exists per RW.");
857 AliasDef = AliasRW.TheDef;
858 }
859 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
860 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000861
862 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000863 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000864 for (Record *WR : ProcModel.WriteResDefs) {
865 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000866 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000867 if (AliasDef == WR->getValueAsDef("WriteType")
868 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000869 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000870 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000871 "SchedWrite and its alias on processor " +
872 ProcModel.ModelName);
873 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000874 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000875 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000876 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000877 // TODO: If ProcModel has a base model (previous generation processor),
878 // then call FindWriteResources recursively with that model here.
879 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000880 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000881 Twine("Processor does not define resources for ") +
882 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000883 }
884 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000885}
886
887/// Find the ReadAdvance record for the given SchedRead on this processor or
888/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000889Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000890 const CodeGenProcModel &ProcModel) {
891 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000892 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
893 return SchedRead.TheDef;
894
895 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000896 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000897 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000898 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000899 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000900 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
901 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
902 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
903 continue;
904 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000905 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000906 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000907 "defined for processor " + ProcModel.ModelName +
908 " Ensure only one SchedAlias exists per RW.");
909 AliasDef = AliasRW.TheDef;
910 }
911 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
912 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000913
914 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000915 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000916 for (Record *RA : ProcModel.ReadAdvanceDefs) {
917 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000918 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000919 if (AliasDef == RA->getValueAsDef("ReadType")
920 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000921 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000922 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000923 "SchedRead and its alias on processor " +
924 ProcModel.ModelName);
925 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000926 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000927 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000928 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000929 // TODO: If ProcModel has a base model (previous generation processor),
930 // then call FindReadAdvance recursively with that model here.
931 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000932 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000933 Twine("Processor does not define resources for ") +
934 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000935 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000936 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000937}
938
Andrew Trick4e67cba2013-03-14 21:21:50 +0000939// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000940// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000941void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
942 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000943 const CodeGenProcModel &PM) {
Clement Courbet5eeed772018-06-13 09:41:49 +0000944 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000945 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000946 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000947 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000948 if (PRDef->isSubClassOf("ProcResGroup"))
949 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000950 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000951 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000952 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000953 for (Record *SubDef = PRDef;
954 SubDef->getValueInit("Super")->isComplete();) {
955 if (SubDef->isSubClassOf("ProcResGroup")) {
956 // Disallow this for simplicitly.
957 PrintFatalError(SubDef->getLoc(), "Processor resource group "
958 " cannot be a super resources.");
959 }
960 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000961 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
962 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000963 PRVec.push_back(SuperDef);
964 Cycles.push_back(Cycles[i]);
965 SubDef = SuperDef;
966 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000967 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000968 for (Record *PR : PM.ProcResourceDefs) {
969 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000970 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000971 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000972 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000973 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000974 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000975 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000976 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000977 }
978 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000979 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000980 Cycles.push_back(Cycles[i]);
981 }
982 }
983 }
984}
985
Andrew Trick9ef08822012-09-17 22:18:48 +0000986// Generate the SchedClass table for this processor and update global
987// tables. Must be called for each processor in order.
988void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
989 SchedClassTables &SchedTables) {
990 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
991 if (!ProcModel.hasInstrSchedModel())
992 return;
993
994 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000995 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000996 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000997 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000998
Andrew Trick9ef08822012-09-17 22:18:48 +0000999 SCTab.resize(SCTab.size() + 1);
1000 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +00001001 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +00001002 SCDesc.NumMicroOps = 0;
1003 SCDesc.BeginGroup = false;
1004 SCDesc.EndGroup = false;
1005 SCDesc.WriteProcResIdx = 0;
1006 SCDesc.WriteLatencyIdx = 0;
1007 SCDesc.ReadAdvanceIdx = 0;
1008
1009 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +00001010 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +00001011 for (const CodeGenSchedTransition &CGT :
1012 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1013 if (CGT.ProcIndices[0] == 0 ||
1014 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001015 HasVariants = true;
1016 break;
1017 }
1018 }
1019 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001020 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1021 continue;
1022 }
1023
1024 // Determine if the SchedClass is actually reachable on this processor. If
1025 // not don't try to locate the processor resources, it will fail.
1026 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001027 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1028 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +00001029 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001030 continue;
1031 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001032 IdxVec Writes = SC.Writes;
1033 IdxVec Reads = SC.Reads;
1034 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001035 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001036 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001037 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001038 for (Record *RW : SC.InstRWs) {
1039 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001040 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001041 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001042 break;
1043 }
1044 }
1045 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001046 Writes.clear();
1047 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001048 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1049 Writes, Reads);
1050 }
1051 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001052 if (Writes.empty()) {
1053 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001054 for (Record *I : ProcModel.ItinRWDefs) {
1055 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001056 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001057 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001058 Writes, Reads);
1059 break;
1060 }
1061 }
1062 if (Writes.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001063 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1064 << " does not have resources for class " << SC.Name
1065 << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001066 }
1067 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001068 // Sum resources across all operand writes.
1069 std::vector<MCWriteProcResEntry> WriteProcResources;
1070 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001071 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001072 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001073 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001074 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001075 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001076 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001077
1078 // For each operand, create a latency entry.
1079 MCWriteLatencyEntry WLEntry;
1080 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001081 unsigned WriteID = WriteSeq.back();
1082 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1083 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1084 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001085 if (!SchedModels.hasReadOfWrite(
1086 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001087 WriteID = 0;
1088 }
1089 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001090
Craig Topper29c55dcb2016-02-13 06:03:32 +00001091 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001092
Andrew Trick9257b8f2012-09-22 02:24:21 +00001093 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001094 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001095
1096 // Mark the parent class as invalid for unsupported write types.
1097 if (WriteRes->getValueAsBit("Unsupported")) {
1098 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1099 break;
1100 }
1101 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1102 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1103 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1104 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001105 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1106 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001107
1108 // Create an entry for each ProcResource listed in WriteRes.
1109 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1110 std::vector<int64_t> Cycles =
1111 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001112
Clement Courbet5eeed772018-06-13 09:41:49 +00001113 if (Cycles.empty()) {
1114 // If ResourceCycles is not provided, default to one cycle per
1115 // resource.
1116 Cycles.resize(PRVec.size(), 1);
1117 } else if (Cycles.size() != PRVec.size()) {
1118 // If ResourceCycles is provided, check consistency.
1119 PrintFatalError(
1120 WriteRes->getLoc(),
1121 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1122 "!size(ProcResources): ")
1123 .concat(Twine(PRVec.size()))
1124 .concat(" vs ")
1125 .concat(Twine(Cycles.size())));
1126 }
1127
Andrew Trick4e67cba2013-03-14 21:21:50 +00001128 ExpandProcResources(PRVec, Cycles, ProcModel);
1129
Andrew Trick9ef08822012-09-17 22:18:48 +00001130 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1131 PRIdx != PREnd; ++PRIdx) {
1132 MCWriteProcResEntry WPREntry;
1133 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1134 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001135 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001136 // If this resource is already used in this sequence, add the current
1137 // entry's cycles so that the same resource appears to be used
1138 // serially, rather than multiple parallel uses. This is important for
1139 // in-order machine where the resource consumption is a hazard.
1140 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1141 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1142 if (WriteProcResources[WPRIdx].ProcResourceIdx
1143 == WPREntry.ProcResourceIdx) {
1144 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1145 break;
1146 }
1147 }
1148 if (WPRIdx == WPREnd)
1149 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001150 }
1151 }
1152 WriteLatencies.push_back(WLEntry);
1153 }
1154 // Create an entry for each operand Read in this SchedClass.
1155 // Entries must be sorted first by UseIdx then by WriteResourceID.
1156 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1157 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001158 Record *ReadAdvance =
1159 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001160 if (!ReadAdvance)
1161 continue;
1162
1163 // Mark the parent class as invalid for unsupported write types.
1164 if (ReadAdvance->getValueAsBit("Unsupported")) {
1165 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1166 break;
1167 }
1168 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1169 IdxVec WriteIDs;
1170 if (ValidWrites.empty())
1171 WriteIDs.push_back(0);
1172 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001173 for (Record *VW : ValidWrites) {
1174 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001175 }
1176 }
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001177 llvm::sort(WriteIDs.begin(), WriteIDs.end());
Craig Topper29c55dcb2016-02-13 06:03:32 +00001178 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001179 MCReadAdvanceEntry RAEntry;
1180 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001181 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001182 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1183 ReadAdvanceEntries.push_back(RAEntry);
1184 }
1185 }
1186 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1187 WriteProcResources.clear();
1188 WriteLatencies.clear();
1189 ReadAdvanceEntries.clear();
1190 }
1191 // Add the information for this SchedClass to the global tables using basic
1192 // compression.
1193 //
1194 // WritePrecRes entries are sorted by ProcResIdx.
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001195 llvm::sort(WriteProcResources.begin(), WriteProcResources.end(),
1196 LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001197
1198 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1199 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1200 std::search(SchedTables.WriteProcResources.begin(),
1201 SchedTables.WriteProcResources.end(),
1202 WriteProcResources.begin(), WriteProcResources.end());
1203 if (WPRPos != SchedTables.WriteProcResources.end())
1204 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1205 else {
1206 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1207 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1208 WriteProcResources.end());
1209 }
1210 // Latency entries must remain in operand order.
1211 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1212 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1213 std::search(SchedTables.WriteLatencies.begin(),
1214 SchedTables.WriteLatencies.end(),
1215 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001216 if (WLPos != SchedTables.WriteLatencies.end()) {
1217 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1218 SCDesc.WriteLatencyIdx = idx;
1219 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1220 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1221 std::string::npos) {
1222 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1223 }
1224 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001225 else {
1226 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001227 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1228 WriteLatencies.begin(),
1229 WriteLatencies.end());
1230 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1231 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001232 }
1233 // ReadAdvanceEntries must remain in operand order.
1234 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1235 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1236 std::search(SchedTables.ReadAdvanceEntries.begin(),
1237 SchedTables.ReadAdvanceEntries.end(),
1238 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1239 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1240 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1241 else {
1242 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1243 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1244 ReadAdvanceEntries.end());
1245 }
1246 }
1247}
1248
Andrew Tricka72fca62012-09-17 22:18:50 +00001249// Emit SchedClass tables for all processors and associated global tables.
1250void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1251 raw_ostream &OS) {
1252 // Emit global WriteProcResTable.
1253 OS << "\n// {ProcResourceIdx, Cycles}\n"
1254 << "extern const llvm::MCWriteProcResEntry "
1255 << Target << "WriteProcResTable[] = {\n"
1256 << " { 0, 0}, // Invalid\n";
1257 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1258 WPRIdx != WPREnd; ++WPRIdx) {
1259 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1260 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1261 << format("%2d", WPREntry.Cycles) << "}";
1262 if (WPRIdx + 1 < WPREnd)
1263 OS << ',';
1264 OS << " // #" << WPRIdx << '\n';
1265 }
1266 OS << "}; // " << Target << "WriteProcResTable\n";
1267
1268 // Emit global WriteLatencyTable.
1269 OS << "\n// {Cycles, WriteResourceID}\n"
1270 << "extern const llvm::MCWriteLatencyEntry "
1271 << Target << "WriteLatencyTable[] = {\n"
1272 << " { 0, 0}, // Invalid\n";
1273 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1274 WLIdx != WLEnd; ++WLIdx) {
1275 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1276 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1277 << format("%2d", WLEntry.WriteResourceID) << "}";
1278 if (WLIdx + 1 < WLEnd)
1279 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001280 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001281 }
1282 OS << "}; // " << Target << "WriteLatencyTable\n";
1283
1284 // Emit global ReadAdvanceTable.
1285 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1286 << "extern const llvm::MCReadAdvanceEntry "
1287 << Target << "ReadAdvanceTable[] = {\n"
1288 << " {0, 0, 0}, // Invalid\n";
1289 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1290 RAIdx != RAEnd; ++RAIdx) {
1291 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1292 OS << " {" << RAEntry.UseIdx << ", "
1293 << format("%2d", RAEntry.WriteResourceID) << ", "
1294 << format("%2d", RAEntry.Cycles) << "}";
1295 if (RAIdx + 1 < RAEnd)
1296 OS << ',';
1297 OS << " // #" << RAIdx << '\n';
1298 }
1299 OS << "}; // " << Target << "ReadAdvanceTable\n";
1300
1301 // Emit a SchedClass table for each processor.
1302 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1303 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1304 if (!PI->hasInstrSchedModel())
1305 continue;
1306
1307 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001308 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001309
1310 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1311 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1312 OS << "static const llvm::MCSchedClassDesc "
1313 << PI->ModelName << "SchedClasses[] = {\n";
1314
1315 // The first class is always invalid. We no way to distinguish it except by
1316 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001317 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001318 && "invalid class not first");
1319 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1320 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001321 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001322
1323 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1324 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1325 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1326 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1327 if (SchedClass.Name.size() < 18)
1328 OS.indent(18 - SchedClass.Name.size());
1329 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001330 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1331 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001332 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1333 << ", " << MCDesc.NumWriteProcResEntries
1334 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1335 << ", " << MCDesc.NumWriteLatencyEntries
1336 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001337 << ", " << MCDesc.NumReadAdvanceEntries
1338 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001339 }
1340 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1341 }
1342}
1343
Andrew Trick87255e32012-07-07 04:00:00 +00001344void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1345 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001346 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001347 // Emit extra processor info if available.
1348 if (PM.hasExtraProcessorInfo())
1349 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001350 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001351 if (PM.hasInstrSchedModel())
1352 EmitProcessorResources(PM, OS);
1353 else if(!PM.ProcResourceDefs.empty())
1354 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001355 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001356
Andrew Trick73d77362012-06-05 03:44:40 +00001357 // Begin processor itinerary properties
1358 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001359 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1360 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1361 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1362 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1363 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1364 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1365 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001366
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001367 bool PostRAScheduler =
1368 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001369
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001370 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1371 << "PostRAScheduler\n";
1372
1373 bool CompleteModel =
1374 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1375
1376 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1377 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001378
Craig Topper29c55dcb2016-02-13 06:03:32 +00001379 OS << " " << PM.Index << ", // Processor ID\n";
1380 if (PM.hasInstrSchedModel())
1381 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1382 << " " << PM.ModelName << "SchedClasses" << ",\n"
1383 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001384 << " " << (SchedModels.schedClassEnd()
1385 - SchedModels.schedClassBegin()) << ",\n";
1386 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001387 OS << " nullptr, nullptr, 0, 0,"
1388 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001389 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001390 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001391 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001392 OS << " nullptr, // No Itinerary\n";
1393 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001394 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001395 else
Clement Courbetb4493792018-04-10 08:16:37 +00001396 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001397 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001398 }
Jim Laskey3763a502005-10-31 17:16:01 +00001399}
1400
1401//
1402// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1403//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001404void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001405 // Gather and sort processor information
1406 std::vector<Record*> ProcessorList =
1407 Records.getAllDerivedDefinitions("Processor");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001408 llvm::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001409
1410 // Begin processor table
1411 OS << "\n";
1412 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001413 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001414 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001415
Jim Laskey3763a502005-10-31 17:16:01 +00001416 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001417 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001418 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001419 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001420 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001421
Jim Laskey3763a502005-10-31 17:16:01 +00001422 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001423 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001424 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001425
Jim Laskey3763a502005-10-31 17:16:01 +00001426 // End processor table
1427 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001428}
1429
1430//
Andrew Trick87255e32012-07-07 04:00:00 +00001431// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001432//
Andrew Trick87255e32012-07-07 04:00:00 +00001433void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001434 OS << "#ifdef DBGFIELD\n"
1435 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1436 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001437 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001438 << "#define DBGFIELD(x) x,\n"
1439 << "#else\n"
1440 << "#define DBGFIELD(x)\n"
1441 << "#endif\n";
1442
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001443 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001444 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001445 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001446 EmitStageAndOperandCycleData(OS, ProcItinLists);
1447 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001448 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001449 OS << "\n// ===============================================================\n"
1450 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001451
Andrew Trick9ef08822012-09-17 22:18:48 +00001452 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001453 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1454 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001455 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001456 EmitSchedClassTables(SchedTables, OS);
1457
1458 // Emit the processor machine model
1459 EmitProcessorModels(OS);
1460 // Emit the processor lookup data
1461 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001462
Craig Topper194cb742017-10-24 15:50:55 +00001463 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001464}
1465
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001466static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1467 std::string Buffer;
1468 raw_string_ostream Stream(Buffer);
1469
1470 // Collect all the PredicateProlog records and print them to the output
1471 // stream.
1472 std::vector<Record *> Prologs =
1473 Records.getAllDerivedDefinitions("PredicateProlog");
1474 llvm::sort(Prologs.begin(), Prologs.end(), LessRecord());
1475 for (Record *P : Prologs)
1476 Stream << P->getValueAsString("Code") << '\n';
1477
1478 Stream.flush();
1479 OS << Buffer;
1480}
1481
1482static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001483 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001484 raw_ostream &OS) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001485 std::string Buffer;
Andrea Di Biagio95140022018-05-25 15:55:37 +00001486 raw_string_ostream StringStream(Buffer);
1487 formatted_raw_ostream FOS(StringStream);
1488
1489 FOS.PadToColumn(6);
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001490
1491 auto IsTruePredicate = [](const Record *Rec) {
1492 return Rec->isSubClassOf("MCSchedPredicate") &&
1493 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1494 };
1495
1496 // If not all predicates are MCTrue, then we need an if-stmt.
1497 unsigned NumNonTruePreds =
1498 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
1499 if (NumNonTruePreds) {
1500 bool FirstNonTruePredicate = true;
1501 for (const Record *Rec : T.PredTerm) {
1502 // Skip predicates that evaluate to "true".
1503 if (IsTruePredicate(Rec))
1504 continue;
1505
1506 if (FirstNonTruePredicate) {
1507 FOS << "if (";
1508 FirstNonTruePredicate = false;
1509 } else {
1510 FOS << "\n";
1511 FOS.PadToColumn(8);
1512 FOS << "&& ";
1513 }
1514
1515 if (Rec->isSubClassOf("MCSchedPredicate")) {
1516 PE.expandPredicate(FOS, Rec->getValueAsDef("Pred"));
1517 continue;
1518 }
1519
1520 // Expand this legacy predicate and wrap it around braces if there is more
1521 // than one predicate to expand.
1522 FOS << ((NumNonTruePreds > 1) ? "(" : "")
1523 << Rec->getValueAsString("Predicate")
1524 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagio95140022018-05-25 15:55:37 +00001525 }
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001526
1527 FOS << ")\n"; // end of if-stmt
1528 FOS.PadToColumn(8);
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001529 }
1530
Andrea Di Biagio95140022018-05-25 15:55:37 +00001531 FOS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1532 FOS.flush();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001533 OS << Buffer;
1534}
1535
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001536// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1537// epilogue code for the auto-generated helper.
1538void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1539 if (ShouldReturnZero) {
Andrea Di Biagio95140022018-05-25 15:55:37 +00001540 OS << " // Don't know how to resolve this scheduling class.\n"
1541 << " return 0;\n";
1542 return;
1543 }
1544
1545 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1546}
1547
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001548bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1549 return all_of(T.PredTerm, [](const Record *Rec) {
1550 return Rec->isSubClassOf("MCSchedPredicate");
1551 });
1552}
1553
1554void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1555 IdxVec &VariantClasses,
1556 bool OnlyExpandMCInstPredicates) {
1557 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1558 // Ignore non-variant scheduling classes.
1559 if (SC.Transitions.empty())
1560 continue;
1561
1562 if (OnlyExpandMCInstPredicates) {
1563 // Ignore this variant scheduling class if transitions don't uses any
1564 // MCSchedPredicate definitions.
1565 if (!all_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
1566 return hasMCSchedPredicates(T);
1567 }))
1568 continue;
1569 }
1570
1571 VariantClasses.push_back(SC.Index);
1572 }
1573}
1574
1575void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1576 // A variant scheduling class may define transitions for multiple
1577 // processors. This function identifies wich processors are associated with
1578 // transition rules specified by variant class `SC`.
1579 for (const CodeGenSchedTransition &T : SC.Transitions) {
1580 IdxVec PI;
1581 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1582 ProcIndices.begin(), ProcIndices.end(),
1583 std::back_inserter(PI));
1584 ProcIndices.swap(PI);
1585 }
1586}
1587
1588void SubtargetEmitter::emitSchedModelHelpersImpl(
1589 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1590 IdxVec VariantClasses;
1591 collectVariantClasses(SchedModels, VariantClasses,
1592 OnlyExpandMCInstPredicates);
1593
1594 if (VariantClasses.empty()) {
1595 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1596 return;
1597 }
1598
1599 // Construct a switch statement where the condition is a check on the
1600 // scheduling class identifier. There is a `case` for every variant class
1601 // defined by the processor models of this target.
1602 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1603 // a variant scheduling class to another scheduling class. Rules are
1604 // described by instances of CodeGenSchedTransition. Note that transitions may
1605 // not be valid for all processors.
1606 OS << " switch (SchedClass) {\n";
1607 for (unsigned VC : VariantClasses) {
1608 IdxVec ProcIndices;
1609 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1610 collectProcessorIndices(SC, ProcIndices);
1611
1612 OS << " case " << VC << ": // " << SC.Name << '\n';
1613
1614 PredicateExpander PE;
1615 PE.setByRef(false);
1616 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1617 for (unsigned PI : ProcIndices) {
1618 OS << " ";
1619 // Emit a guard on the processor ID.
1620 if (PI != 0) {
1621 OS << (OnlyExpandMCInstPredicates
1622 ? "if (CPUID == "
1623 : "if (SchedModel->getProcessorID() == ");
1624 OS << PI << ") ";
1625 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1626 }
1627
1628 // Now emit transitions associated with processor PI.
1629 for (const CodeGenSchedTransition &T : SC.Transitions) {
1630 if (PI != 0 && !count(T.ProcIndices, PI))
1631 continue;
1632 PE.setIndentLevel(4);
1633 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1634 }
1635
1636 OS << " }\n";
1637 if (PI == 0)
1638 break;
1639 }
1640
1641 if (SC.isInferred())
1642 OS << " return " << SC.Index << ";\n";
1643 OS << " break;\n";
1644 }
1645
1646 OS << " };\n";
1647
1648 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1649}
1650
Andrea Di Biagio95140022018-05-25 15:55:37 +00001651void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1652 raw_ostream &OS) {
1653 OS << "unsigned " << ClassName
1654 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1655 << " const TargetSchedModel *SchedModel) const {\n";
1656
1657 // Emit the predicate prolog code.
1658 emitPredicateProlog(Records, OS);
1659
1660 // Emit target predicates.
1661 emitSchedModelHelpersImpl(OS);
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001662
1663 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagio95140022018-05-25 15:55:37 +00001664
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001665 OS << "unsigned " << ClassName
1666 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1667 << " unsigned CPUID) const {\n"
1668 << " return " << Target << "_MC"
1669 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
1670 << "} // " << ClassName << "::resolveVariantSchedClass\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001671}
1672
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001673void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1674 raw_ostream &OS) {
1675 const CodeGenHwModes &CGH = TGT.getHwModes();
1676 assert(CGH.getNumModeIds() > 0);
1677 if (CGH.getNumModeIds() == 1)
1678 return;
1679
1680 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1681 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1682 const HwMode &HM = CGH.getMode(M);
1683 OS << " if (checkFeatures(\"" << HM.Features
1684 << "\")) return " << M << ";\n";
1685 }
1686 OS << " return 0;\n}\n";
1687}
1688
Jim Laskey86f002c2005-10-27 19:47:21 +00001689//
Jim Laskeya2b52352005-10-26 17:30:34 +00001690// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1691// the subtarget features string.
1692//
Evan Cheng54b68e32011-07-01 20:45:01 +00001693void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1694 unsigned NumFeatures,
1695 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001696 std::vector<Record*> Features =
1697 Records.getAllDerivedDefinitions("SubtargetFeature");
Mandeep Singh Grang1b0e2f22018-04-06 20:18:05 +00001698 llvm::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001699
Andrew Trickdb6ed642011-04-01 01:56:55 +00001700 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1701 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001702 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001703 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001704 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001705 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1706 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001707
1708 if (Features.empty()) {
1709 OS << "}\n";
1710 return;
1711 }
1712
Andrew Trickba7b9212012-09-18 05:33:15 +00001713 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001714 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001715
Craig Topper29c55dcb2016-02-13 06:03:32 +00001716 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001717 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001718 StringRef Instance = R->getName();
1719 StringRef Value = R->getValueAsString("Value");
1720 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001721
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001722 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001723 OS << " if (Bits[" << Target << "::"
1724 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001725 << Attribute << " = " << Value << ";\n";
1726 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001727 OS << " if (Bits[" << Target << "::"
1728 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001729 << Attribute << " < " << Value << ") "
1730 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001731 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001732
Evan Chengfe6e4052011-06-30 01:53:36 +00001733 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001734}
1735
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001736void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001737 OS << "namespace " << Target << "_MC {\n"
1738 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1739 << " const MCInst *MI, unsigned CPUID) {\n";
1740 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1741 OS << "}\n";
1742 OS << "} // end of namespace " << Target << "_MC\n\n";
1743
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001744 OS << "struct " << Target
1745 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1746 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1747 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
1748 << " ArrayRef<SubtargetFeatureKV> PD,\n"
1749 << " const SubtargetInfoKV *ProcSched,\n"
1750 << " const MCWriteProcResEntry *WPR,\n"
1751 << " const MCWriteLatencyEntry *WL,\n"
1752 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1753 << " const unsigned *OC, const unsigned *FP) :\n"
1754 << " MCSubtargetInfo(TT, CPU, FS, PF, PD, ProcSched,\n"
1755 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1756 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001757 << " const MCInst *MI, unsigned CPUID) const override {\n"
1758 << " return " << Target << "_MC"
1759 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001760 OS << " }\n";
1761 OS << "};\n";
1762}
1763
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001764//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001765// SubtargetEmitter::run - Main subtarget enumeration emitter.
1766//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001767void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001768 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001769
Evan Cheng4d1ca962011-07-08 01:53:10 +00001770 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001771 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001772
1773 OS << "namespace llvm {\n";
Craig Topper094bbca2016-02-14 05:22:01 +00001774 Enumeration(OS);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001775 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001776 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1777
Evan Cheng54b68e32011-07-01 20:45:01 +00001778 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001779 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001780
Evan Cheng54b68e32011-07-01 20:45:01 +00001781 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001782#if 0
1783 OS << "namespace {\n";
1784#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001785 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001786 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001787 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001788 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001789 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001790 OS << "\n";
1791#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001792 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001793#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001794
1795 // MCInstrInfo initialization routine.
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001796 emitGenMCSubtargetInfo(OS);
1797
Craig Topper194cb742017-10-24 15:50:55 +00001798 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001799 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001800 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001801 OS << " return new " << Target << "GenMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001802 if (NumFeatures)
1803 OS << Target << "FeatureKV, ";
1804 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001805 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001806 if (NumProcs)
1807 OS << Target << "SubTypeKV, ";
1808 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001809 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001810 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001811 OS << Target << "ProcSchedKV, "
1812 << Target << "WriteProcResTable, "
1813 << Target << "WriteLatencyTable, "
1814 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001815 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001816 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001817 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001818 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001819 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001820 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001821 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001822 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001823
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001824 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001825
1826 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1827
1828 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001829 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001830
1831 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001832 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001833 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1834
1835 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1836
Evan Cheng0d639a22011-07-01 21:01:15 +00001837 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001838 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001839 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001840
1841 std::string ClassName = Target + "GenSubtargetInfo";
1842 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001843 OS << "class DFAPacketizer;\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001844 OS << "namespace " << Target << "_MC {\n"
1845 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1846 << " const MCInst *MI, unsigned CPUID);\n"
1847 << "}\n\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001848 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001849 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001850 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001851 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001852 << " unsigned resolveSchedClass(unsigned SchedClass, "
1853 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001854 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001855 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1856 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001857 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001858 << " const;\n";
1859 if (TGT.getHwModes().getNumModeIds() > 1)
1860 OS << " unsigned getHwMode() const override;\n";
1861 OS << "};\n"
1862 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001863
1864 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1865
1866 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001867 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001868
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001869 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001870 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001871 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1872 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001873 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1874 OS << "extern const llvm::MCWriteProcResEntry "
1875 << Target << "WriteProcResTable[];\n";
1876 OS << "extern const llvm::MCWriteLatencyEntry "
1877 << Target << "WriteLatencyTable[];\n";
1878 OS << "extern const llvm::MCReadAdvanceEntry "
1879 << Target << "ReadAdvanceTable[];\n";
1880
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001881 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001882 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1883 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001884 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001885 }
1886
Daniel Sanders50f17232015-09-15 16:17:27 +00001887 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1888 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001889 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001890 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001891 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001892 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001893 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001894 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001895 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001896 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001897 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001898 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001899 OS << Target << "ProcSchedKV, "
1900 << Target << "WriteProcResTable, "
1901 << Target << "WriteLatencyTable, "
1902 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001903 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001904 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001905 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001906 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001907 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001908 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001909 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001910 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001911
Andrew Trickc6c88152012-09-18 03:41:43 +00001912 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001913 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001914
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001915 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001916
1917 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001918}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001919
1920namespace llvm {
1921
1922void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001923 CodeGenTarget CGTarget(RK);
1924 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001925}
1926
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001927} // end namespace llvm