blob: 9d134675fea044c9e567028cf0cee1b8b8e328b7 [file] [log] [blame]
Jim Laskeycfda85a2005-10-21 19:00:04 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jim Laskeycfda85a2005-10-21 19:00:04 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner73fbe142006-03-03 02:04:07 +00009// This tablegen backend emits subtarget enumerations.
Jim Laskeycfda85a2005-10-21 19:00:04 +000010//
11//===----------------------------------------------------------------------===//
12
Jim Laskeycfda85a2005-10-21 19:00:04 +000013#include "CodeGenTarget.h"
Andrew Trick87255e32012-07-07 04:00:00 +000014#include "CodeGenSchedule.h"
Andrea Di Biagio95140022018-05-25 15:55:37 +000015#include "PredicateExpander.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000016#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000019#include "llvm/ADT/StringRef.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000020#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000021#include "llvm/MC/MCSchedule.h"
Michael Kupersteindb0712f2015-05-26 10:47:10 +000022#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/Format.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000025#include "llvm/Support/raw_ostream.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000026#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027#include "llvm/TableGen/Record.h"
28#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenb0aa47b2005-10-28 01:43:09 +000029#include <algorithm>
Eugene Zelenko75259bb2016-05-17 17:04:23 +000030#include <cassert>
31#include <cstdint>
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000032#include <iterator>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000033#include <map>
34#include <string>
35#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000036
Jim Laskeycfda85a2005-10-21 19:00:04 +000037using namespace llvm;
38
Chandler Carruth97acce22014-04-22 03:06:00 +000039#define DEBUG_TYPE "subtarget-emitter"
40
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000041namespace {
Eugene Zelenko75259bb2016-05-17 17:04:23 +000042
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000044 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
45 // The SchedClassDesc table indexes into a global write resource table, write
46 // latency table, and read advance table.
47 struct SchedClassTables {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000048 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
Andrew Trick9ef08822012-09-17 22:18:48 +000049 std::vector<MCWriteProcResEntry> WriteProcResources;
50 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000051 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000052 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
53
54 // Reserve an invalid entry at index 0
55 SchedClassTables() {
56 ProcSchedClasses.resize(1);
57 WriteProcResources.resize(1);
58 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000059 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000060 ReadAdvanceEntries.resize(1);
61 }
62 };
63
64 struct LessWriteProcResources {
65 bool operator()(const MCWriteProcResEntry &LHS,
66 const MCWriteProcResEntry &RHS) {
67 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
68 }
69 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000070
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +000071 const CodeGenTarget &TGT;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000072 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000073 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000074 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000075
Craig Topper4f613082019-03-01 02:19:26 +000076 void Enumeration(raw_ostream &OS, DenseMap<Record *, unsigned> &FeatureMap);
77 unsigned FeatureKeyValues(raw_ostream &OS,
78 const DenseMap<Record *, unsigned> &FeatureMap);
79 unsigned CPUKeyValues(raw_ostream &OS,
80 const DenseMap<Record *, unsigned> &FeatureMap);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000081 void FormItineraryStageString(const std::string &Names,
82 Record *ItinData, std::string &ItinString,
83 unsigned &NStages);
84 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
85 unsigned &NOperandCycles);
86 void FormItineraryBypassString(const std::string &Names,
87 Record *ItinData,
88 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000089 void EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000090 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000091 &ProcItinLists);
92 void EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000093 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000094 &ProcItinLists);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000095 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
96 raw_ostream &OS);
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +000097 void EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
98 raw_ostream &OS);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000099 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
100 raw_ostream &OS);
Mehdi Amini32986ed2016-10-04 23:47:33 +0000101 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000102 char Separator);
Clement Courbet39911e22018-02-08 08:46:48 +0000103 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
104 raw_ostream &OS);
Andrew Trick23f3c652012-09-17 22:18:45 +0000105 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
106 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000107 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +0000108 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000109 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
110 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000111 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
112 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000113 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
114 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +0000115 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000116 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000117 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000118 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000119 void emitSchedModelHelpersImpl(raw_ostream &OS,
120 bool OnlyExpandMCInstPredicates = false);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +0000121 void emitGenMCSubtargetInfo(raw_ostream &OS);
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000122 void EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000123
Andrew Trick87255e32012-07-07 04:00:00 +0000124 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000125 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000126 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
127 unsigned NumProcs);
128
129public:
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000130 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
131 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
132 Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000133
134 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000135};
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000136
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000137} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000138
Jim Laskeya1beea62005-10-22 07:59:56 +0000139//
Jim Laskeya2b52352005-10-26 17:30:34 +0000140// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000141//
Craig Topper4f613082019-03-01 02:19:26 +0000142void SubtargetEmitter::Enumeration(raw_ostream &OS,
143 DenseMap<Record *, unsigned> &FeatureMap) {
Jim Laskey19595752005-10-28 15:20:43 +0000144 // Get all records of class and sort
Craig Topper094bbca2016-02-14 05:22:01 +0000145 std::vector<Record*> DefList =
146 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +0000147 llvm::sort(DefList, LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000148
Evan Chenga2e61292011-04-15 19:35:46 +0000149 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000150 if (N == 0)
151 return;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000152 if (N > MAX_SUBTARGET_FEATURES)
153 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chenga2e61292011-04-15 19:35:46 +0000154
Evan Cheng54b68e32011-07-01 20:45:01 +0000155 OS << "namespace " << Target << " {\n";
156
Craig Topperbcdb0f22016-02-13 17:58:14 +0000157 // Open enumeration.
Craig Topper2d45c1d2016-02-13 06:03:29 +0000158 OS << "enum {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000159
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000160 // For each record
Craig Topperdf1285b2017-10-24 15:50:53 +0000161 for (unsigned i = 0; i < N; ++i) {
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000162 // Next record
163 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000164
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000165 // Get and emit name
Craig Topperdf1285b2017-10-24 15:50:53 +0000166 OS << " " << Def->getName() << " = " << i << ",\n";
Craig Topper4f613082019-03-01 02:19:26 +0000167
168 // Save the index for this feature.
169 FeatureMap[Def] = i;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000170 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000171
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000172 // Close enumeration and namespace
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000173 OS << "};\n";
174 OS << "} // end namespace " << Target << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000175}
176
Craig Topper4f613082019-03-01 02:19:26 +0000177static void printFeatureMask(raw_ostream &OS, RecVec &FeatureList,
178 const DenseMap<Record *, unsigned> &FeatureMap) {
179 std::array<uint64_t, MAX_SUBTARGET_WORDS> Mask = {};
180 for (unsigned j = 0, M = FeatureList.size(); j < M; ++j) {
181 unsigned Bit = FeatureMap.lookup(FeatureList[j]);
182 Mask[Bit / 64] |= 1ULL << (Bit % 64);
183 }
184
Craig Topper2729a912019-03-04 19:23:37 +0000185 OS << "{ { { ";
Craig Topper4f613082019-03-01 02:19:26 +0000186 for (unsigned i = 0; i != Mask.size(); ++i) {
187 OS << "0x";
188 OS.write_hex(Mask[i]);
189 OS << "ULL, ";
190 }
Craig Topper2729a912019-03-04 19:23:37 +0000191 OS << "} } }";
Craig Topper4f613082019-03-01 02:19:26 +0000192}
193
Jim Laskey1b7369b2005-10-25 15:16:36 +0000194//
Bill Wendlinge6182262007-05-04 20:38:40 +0000195// FeatureKeyValues - Emit data of all the subtarget features. Used by the
196// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000197//
Craig Topper4f613082019-03-01 02:19:26 +0000198unsigned SubtargetEmitter::FeatureKeyValues(
199 raw_ostream &OS, const DenseMap<Record *, unsigned> &FeatureMap) {
Jim Laskey19595752005-10-28 15:20:43 +0000200 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000201 std::vector<Record*> FeatureList =
202 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000203
204 if (FeatureList.empty())
205 return 0;
206
Fangrui Song0cac7262018-09-27 02:13:45 +0000207 llvm::sort(FeatureList, LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000208
Jim Laskey19595752005-10-28 15:20:43 +0000209 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000210 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000211 << "extern const llvm::SubtargetFeatureKV " << Target
212 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000213
Jim Laskey19595752005-10-28 15:20:43 +0000214 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000215 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000216 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000217 // Next feature
218 Record *Feature = FeatureList[i];
219
Craig Topperbcd3c372017-05-31 21:12:46 +0000220 StringRef Name = Feature->getName();
221 StringRef CommandLineName = Feature->getValueAsString("Name");
222 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000223
Jim Laskey3f7d0472006-12-12 20:55:58 +0000224 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000225
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000226 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000227 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000228 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000229 << "\"" << Desc << "\", "
Craig Topper4cf59aa2019-02-18 06:46:17 +0000230 << Target << "::" << Name << ", ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000231
Craig Topper37eeb322018-03-23 00:02:45 +0000232 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000233
Craig Topper4f613082019-03-01 02:19:26 +0000234 printFeatureMask(OS, ImpliesList, FeatureMap);
235
236 OS << " },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000237 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000238 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000239
Jim Laskey19595752005-10-28 15:20:43 +0000240 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000241 OS << "};\n";
242
Evan Cheng54b68e32011-07-01 20:45:01 +0000243 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000244}
245
246//
247// CPUKeyValues - Emit data of all the subtarget processors. Used by command
248// line.
249//
Craig Topper4f613082019-03-01 02:19:26 +0000250unsigned
251SubtargetEmitter::CPUKeyValues(raw_ostream &OS,
252 const DenseMap<Record *, unsigned> &FeatureMap) {
Jim Laskey19595752005-10-28 15:20:43 +0000253 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000254 std::vector<Record*> ProcessorList =
255 Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +0000256 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000257
Jim Laskey19595752005-10-28 15:20:43 +0000258 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000259 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Craig Topperca268082019-03-05 18:54:34 +0000260 << "extern const llvm::SubtargetSubTypeKV " << Target
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000261 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000262
Jim Laskey19595752005-10-28 15:20:43 +0000263 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000264 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000265 StringRef Name = Processor->getValueAsString("Name");
Craig Topper37eeb322018-03-23 00:02:45 +0000266 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000267
Craig Topper4cf59aa2019-02-18 06:46:17 +0000268 // Emit as { "cpu", "description", 0, { f1 , f2 , ... fn } },
Craig Topper4cf59aa2019-02-18 06:46:17 +0000269 OS << " { "
Craig Topperca268082019-03-05 18:54:34 +0000270 << "\"" << Name << "\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000271
Craig Topper4f613082019-03-01 02:19:26 +0000272 printFeatureMask(OS, FeatureList, FeatureMap);
273
Craig Topper2982b842019-03-05 18:54:38 +0000274 // Emit the scheduler model pointer.
275 const std::string &ProcModelName =
276 SchedModels.getModelForProc(Processor).ModelName;
277 OS << ", &" << ProcModelName << " },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000278 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000279
Jim Laskey19595752005-10-28 15:20:43 +0000280 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000281 OS << "};\n";
282
Evan Cheng54b68e32011-07-01 20:45:01 +0000283 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000284}
Jim Laskeya1beea62005-10-22 07:59:56 +0000285
Jim Laskeya2b52352005-10-26 17:30:34 +0000286//
David Goodwind813cbf2009-08-17 16:02:57 +0000287// FormItineraryStageString - Compose a string containing the stage
288// data initialization for the specified itinerary. N is the number
289// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000290//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000291void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
292 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000293 std::string &ItinString,
294 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000295 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000296 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000297
298 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000299 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000300 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000301 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000302 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000303
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000304 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000305 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000306 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000307
Jim Laskeydffe5972005-10-28 21:47:29 +0000308 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000309 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000310
Jim Laskey19595752005-10-28 15:20:43 +0000311 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000312 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000313 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000314 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000315 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000316 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000317
David Goodwinb369ee42009-08-12 18:31:53 +0000318 int TimeInc = Stage->getValueAsInt("TimeInc");
319 ItinString += ", " + itostr(TimeInc);
320
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000321 int Kind = Stage->getValueAsInt("Kind");
322 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
323
Jim Laskey19595752005-10-28 15:20:43 +0000324 // Close off stage
325 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000326 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000327 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000328}
329
330//
David Goodwind813cbf2009-08-17 16:02:57 +0000331// FormItineraryOperandCycleString - Compose a string containing the
332// operand cycle initialization for the specified itinerary. N is the
333// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000334//
David Goodwind813cbf2009-08-17 16:02:57 +0000335void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
336 std::string &ItinString, unsigned &NOperandCycles) {
337 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000338 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000339 ItinData->getValueAsListOfInts("OperandCycles");
340
341 // For each operand cycle
342 unsigned N = NOperandCycles = OperandCycleList.size();
343 for (unsigned i = 0; i < N;) {
344 // Next operand cycle
345 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000346
David Goodwind813cbf2009-08-17 16:02:57 +0000347 ItinString += " " + itostr(OCycle);
348 if (++i < N) ItinString += ", ";
349 }
350}
351
Evan Cheng0097dd02010-09-28 23:50:49 +0000352void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
353 Record *ItinData,
354 std::string &ItinString,
355 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000356 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000357 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000358 unsigned i = 0;
359 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000360 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000361 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000362 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000363 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000364 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000365 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000366 }
367}
368
David Goodwind813cbf2009-08-17 16:02:57 +0000369//
Andrew Trick87255e32012-07-07 04:00:00 +0000370// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
371// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
372// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000373//
Andrew Trick87255e32012-07-07 04:00:00 +0000374void SubtargetEmitter::
375EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000376 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000377 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000378 // Multiple processor models may share an itinerary record. Emit it once.
379 SmallPtrSet<Record*, 8> ItinsDefSet;
380
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000381 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000382 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000383
Craig Topper29c55dcb2016-02-13 06:03:32 +0000384 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000385 continue;
386
Craig Topper37eeb322018-03-23 00:02:45 +0000387 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000388 if (FUs.empty())
389 continue;
390
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000391 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000392 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000393 << "namespace " << Name << "FU {\n";
394
395 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000396 OS << " const unsigned " << FUs[j]->getName()
397 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000398
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000399 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000400
Craig Topper37eeb322018-03-23 00:02:45 +0000401 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000402 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000403 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000404 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000405
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000406 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000407 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000408 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000409 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000410
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000411 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000412 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000413 }
414
Jim Laskey19595752005-10-28 15:20:43 +0000415 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000416 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
417 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000418 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000419
David Goodwind813cbf2009-08-17 16:02:57 +0000420 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000421 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000422 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000423 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000424
425 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000426 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000427 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000428 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000429
Andrew Trick87255e32012-07-07 04:00:00 +0000430 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000431 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000432 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000433 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000434 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000435 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000436 // Add process itinerary to the list.
437 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000438
Andrew Trick87255e32012-07-07 04:00:00 +0000439 // If this processor defines no itineraries, then leave the itinerary list
440 // empty.
441 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000442 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000443 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000444
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000445 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000446
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000447 ItinList.resize(SchedModels.numInstrSchedClasses());
448 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
449
450 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000451 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
452
Jim Laskeydffe5972005-10-28 21:47:29 +0000453 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000454 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000455
Jim Laskey19595752005-10-28 15:20:43 +0000456 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000457 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000458 unsigned NStages = 0;
459 if (ItinData)
460 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000461
David Goodwind813cbf2009-08-17 16:02:57 +0000462 // Get string and operand cycle count
463 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000464 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000465 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000466 if (ItinData) {
467 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
468 NOperandCycles);
469
470 FormItineraryBypassString(Name, ItinData, ItinBypassString,
471 NOperandCycles);
472 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000473
David Goodwind813cbf2009-08-17 16:02:57 +0000474 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000475 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000476 if (NStages > 0) {
477 FindStage = ItinStageMap[ItinStageString];
478 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000479 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
480 StageTable += ItinStageString + ", // " + itostr(StageCount);
481 if (NStages > 1)
482 StageTable += "-" + itostr(StageCount + NStages - 1);
483 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000484 // Record Itin class number.
485 ItinStageMap[ItinStageString] = FindStage = StageCount;
486 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000487 }
488 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000489
David Goodwind813cbf2009-08-17 16:02:57 +0000490 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000491 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000492 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000493 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
494 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000495 if (FindOperandCycle == 0) {
496 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000497 OperandCycleTable += ItinOperandCycleString + ", // ";
498 std::string OperandIdxComment = itostr(OperandCycleCount);
499 if (NOperandCycles > 1)
500 OperandIdxComment += "-"
501 + itostr(OperandCycleCount + NOperandCycles - 1);
502 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000503 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000504 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000505 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000506 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000507 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000508 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000509 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000510 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000511
Evan Cheng367a5df2010-09-09 18:18:55 +0000512 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000513 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
514 InstrItinerary Intinerary = {
515 NumUOps,
516 FindStage,
517 uint16_t(FindStage + NStages),
518 FindOperandCycle,
519 uint16_t(FindOperandCycle + NOperandCycles),
520 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000521
Jim Laskey19595752005-10-28 15:20:43 +0000522 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000523 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000524 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000525 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000526
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000527 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000528 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000529 StageTable += "};\n";
530
531 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000532 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000533 OperandCycleTable += "};\n";
534
Andrew Trick87255e32012-07-07 04:00:00 +0000535 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000536 BypassTable += "};\n";
537
David Goodwind813cbf2009-08-17 16:02:57 +0000538 // Emit tables.
539 OS << StageTable;
540 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000541 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000542}
543
Andrew Trick87255e32012-07-07 04:00:00 +0000544//
545// EmitProcessorData - Generate data for processor itineraries that were
546// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
547// Itineraries for each processor. The Itinerary lists are indexed on
548// CodeGenSchedClass::Index.
549//
550void SubtargetEmitter::
551EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000552 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000553 // Multiple processor models may share an itinerary record. Emit it once.
554 SmallPtrSet<Record*, 8> ItinsDefSet;
555
Andrew Trick87255e32012-07-07 04:00:00 +0000556 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000557 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000558 ProcItinListsIter = ProcItinLists.begin();
559 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000560 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000561
Andrew Trick87255e32012-07-07 04:00:00 +0000562 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000563 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000564 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000565
Andrew Trick87255e32012-07-07 04:00:00 +0000566 // Get the itinerary list for the processor.
567 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000568 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000569
Pete Cooperc0eb1532014-09-02 23:23:34 +0000570 // Empty itineraries aren't referenced anywhere in the tablegen output
571 // so don't emit them.
572 if (ItinList.empty())
573 continue;
574
Andrew Trick87255e32012-07-07 04:00:00 +0000575 OS << "\n";
576 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000577
578 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000579 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000580
581 // For each itinerary class in CodeGenSchedClass::Index order.
582 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
583 InstrItinerary &Intinerary = ItinList[j];
584
585 // Emit Itinerary in the form of
586 // { firstStage, lastStage, firstCycle, lastCycle } // index
587 OS << " { " <<
588 Intinerary.NumMicroOps << ", " <<
589 Intinerary.FirstStage << ", " <<
590 Intinerary.LastStage << ", " <<
591 Intinerary.FirstOperandCycle << ", " <<
592 Intinerary.LastOperandCycle << " }" <<
593 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
594 }
595 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000596 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
597 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000598 OS << "};\n";
599 }
600}
601
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000602// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000603// value defined in the C++ header. The Record is null if the processor does not
604// define a model.
605void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000606 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000607 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000608 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000609 if (V >= 0)
610 OS << V << Separator << " // " << Name;
611 else
Andrew Trick87255e32012-07-07 04:00:00 +0000612 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000613 OS << '\n';
614}
615
Clement Courbet39911e22018-02-08 08:46:48 +0000616void SubtargetEmitter::EmitProcessorResourceSubUnits(
617 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
618 OS << "\nstatic const unsigned " << ProcModel.ModelName
619 << "ProcResourceSubUnits[] = {\n"
620 << " 0, // Invalid\n";
621
622 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
623 Record *PRDef = ProcModel.ProcResourceDefs[i];
624 if (!PRDef->isSubClassOf("ProcResGroup"))
625 continue;
626 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
627 for (Record *RUDef : ResUnits) {
628 Record *const RU =
629 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
630 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
631 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
632 }
633 }
634 OS << " // " << PRDef->getName() << "\n";
635 }
636 OS << "};\n";
637}
638
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000639static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
640 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000641 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000642 if (Record *RCU = ProcModel.RetireControlUnit) {
643 ReorderBufferSize =
644 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
645 MaxRetirePerCycle =
646 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
647 }
648
649 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
650 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
651}
652
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000653static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
654 unsigned NumRegisterFiles,
655 unsigned NumCostEntries, raw_ostream &OS) {
656 if (NumRegisterFiles)
657 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
658 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000659 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000660
661 OS << ", // Number of register files.\n ";
662 if (NumCostEntries)
663 OS << ProcModel.ModelName << "RegisterCosts,\n ";
664 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000665 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000666 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000667}
668
669unsigned
670SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
671 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000672 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
673 return RF.hasDefaultCosts();
674 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000675 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000676
677 // Print the RegisterCost table first.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000678 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000679 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
680 << "RegisterCosts"
681 << "[] = {\n";
682
683 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
684 // Skip register files with a default cost table.
685 if (RF.hasDefaultCosts())
686 continue;
687 // Add entries to the cost table.
688 for (const CodeGenRegisterCost &RC : RF.Costs) {
689 OS << " { ";
690 Record *Rec = RC.RCDef;
691 if (Rec->getValue("Namespace"))
692 OS << Rec->getValueAsString("Namespace") << "::";
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000693 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
694 << RC.AllowMoveElimination << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000695 }
696 }
697 OS << "};\n";
698
699 // Now generate a table with register file info.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000700 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
701 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000702 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
703 << "RegisterFiles"
704 << "[] = {\n"
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000705 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000706 unsigned CostTblIndex = 0;
707
708 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
709 OS << " { ";
710 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
711 unsigned NumCostEntries = RD.Costs.size();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000712 OS << NumCostEntries << ", " << CostTblIndex << ", "
713 << RD.MaxMovesEliminatedPerCycle << ", "
714 << RD.AllowZeroMoveEliminationOnly << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000715 CostTblIndex += NumCostEntries;
716 }
717 OS << "};\n";
718
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000719 return CostTblIndex;
720}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000721
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000722void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
723 raw_ostream &OS) {
724 unsigned QueueID = 0;
725 if (ProcModel.LoadQueue) {
726 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
727 QueueID =
728 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
729 std::find(ProcModel.ProcResourceDefs.begin(),
730 ProcModel.ProcResourceDefs.end(), Queue));
731 }
732 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
733
734 QueueID = 0;
735 if (ProcModel.StoreQueue) {
736 const Record *Queue =
737 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
738 QueueID =
739 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
740 std::find(ProcModel.ProcResourceDefs.begin(),
741 ProcModel.ProcResourceDefs.end(), Queue));
742 }
743 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
744}
745
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000746void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
747 raw_ostream &OS) {
748 // Generate a table of register file descriptors (one entry per each user
749 // defined register file), and a table of register costs.
750 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
751
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000752 // Now generate a table for the extra processor info.
753 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000754 << "ExtraInfo = {\n ";
755
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000756 // Add information related to the retire control unit.
757 EmitRetireControlUnitInfo(ProcModel, OS);
758
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000759 // Add information related to the register files (i.e. where to find register
760 // file descriptors and register costs).
761 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
762 NumCostEntries, OS);
763
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000764 // Add information about load/store queues.
765 EmitLoadStoreQueueInfo(ProcModel, OS);
766
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000767 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000768}
769
Andrew Trick23f3c652012-09-17 22:18:45 +0000770void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
771 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000772 EmitProcessorResourceSubUnits(ProcModel, OS);
773
Jinsong Ji05941622018-09-18 15:38:56 +0000774 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000775 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
776 << "ProcResources"
777 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000778 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000779
Clement Courbet39911e22018-02-08 08:46:48 +0000780 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000781 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
782 Record *PRDef = ProcModel.ProcResourceDefs[i];
783
Craig Topper24064772014-04-15 07:20:03 +0000784 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000785 unsigned SuperIdx = 0;
786 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000787 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000788 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000789 if (PRDef->isSubClassOf("ProcResGroup")) {
790 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000791 for (Record *RU : ResUnits) {
792 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000793 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000794 }
795 }
796 else {
797 // Find the SuperIdx
798 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000799 SuperDef =
800 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
801 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000802 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
803 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000804 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000805 }
806 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000807 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000808 if (PRDef->getName().size() < 15)
809 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000810 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
811 if (SubUnitsBeginOffset != SubUnitsOffset) {
812 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
813 << SubUnitsBeginOffset;
814 } else {
815 OS << "nullptr";
816 }
817 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000818 if (SuperDef)
819 OS << ", Super=" << SuperDef->getName();
820 OS << "\n";
821 }
822 OS << "};\n";
823}
824
Andrew Trick9ef08822012-09-17 22:18:48 +0000825// Find the WriteRes Record that defines processor resources for this
826// SchedWrite.
827Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000828 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000829
830 // Check if the SchedWrite is already subtarget-specific and directly
831 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000832 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
833 return SchedWrite.TheDef;
834
Craig Topper24064772014-04-15 07:20:03 +0000835 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000836 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000837 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000838 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000839 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
840 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
841 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
842 continue;
843 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000844 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000845 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000846 "defined for processor " + ProcModel.ModelName +
847 " Ensure only one SchedAlias exists per RW.");
848 AliasDef = AliasRW.TheDef;
849 }
850 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
851 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000852
853 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000854 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000855 for (Record *WR : ProcModel.WriteResDefs) {
856 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000857 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000858 if (AliasDef == WR->getValueAsDef("WriteType")
859 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000860 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000861 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000862 "SchedWrite and its alias on processor " +
863 ProcModel.ModelName);
864 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000865 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000866 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000867 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000868 // TODO: If ProcModel has a base model (previous generation processor),
869 // then call FindWriteResources recursively with that model here.
870 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000871 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000872 Twine("Processor does not define resources for ") +
873 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000874 }
875 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000876}
877
878/// Find the ReadAdvance record for the given SchedRead on this processor or
879/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000880Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000881 const CodeGenProcModel &ProcModel) {
882 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000883 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
884 return SchedRead.TheDef;
885
886 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000887 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000888 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000889 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000890 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000891 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
892 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
893 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
894 continue;
895 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000896 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000897 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000898 "defined for processor " + ProcModel.ModelName +
899 " Ensure only one SchedAlias exists per RW.");
900 AliasDef = AliasRW.TheDef;
901 }
902 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
903 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000904
905 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000906 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000907 for (Record *RA : ProcModel.ReadAdvanceDefs) {
908 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000909 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000910 if (AliasDef == RA->getValueAsDef("ReadType")
911 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000912 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000913 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000914 "SchedRead and its alias on processor " +
915 ProcModel.ModelName);
916 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000917 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000918 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000919 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000920 // TODO: If ProcModel has a base model (previous generation processor),
921 // then call FindReadAdvance recursively with that model here.
922 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000923 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000924 Twine("Processor does not define resources for ") +
925 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000926 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000927 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000928}
929
Andrew Trick4e67cba2013-03-14 21:21:50 +0000930// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000931// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000932void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
933 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000934 const CodeGenProcModel &PM) {
Clement Courbet5eeed772018-06-13 09:41:49 +0000935 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000936 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000937 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000938 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000939 if (PRDef->isSubClassOf("ProcResGroup"))
940 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000941 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000942 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000943 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000944 for (Record *SubDef = PRDef;
945 SubDef->getValueInit("Super")->isComplete();) {
946 if (SubDef->isSubClassOf("ProcResGroup")) {
947 // Disallow this for simplicitly.
948 PrintFatalError(SubDef->getLoc(), "Processor resource group "
949 " cannot be a super resources.");
950 }
951 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000952 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
953 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000954 PRVec.push_back(SuperDef);
955 Cycles.push_back(Cycles[i]);
956 SubDef = SuperDef;
957 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000958 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000959 for (Record *PR : PM.ProcResourceDefs) {
960 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000961 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000962 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000963 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000964 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000965 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000966 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000967 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000968 }
969 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000970 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000971 Cycles.push_back(Cycles[i]);
972 }
973 }
974 }
975}
976
Andrew Trick9ef08822012-09-17 22:18:48 +0000977// Generate the SchedClass table for this processor and update global
978// tables. Must be called for each processor in order.
979void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
980 SchedClassTables &SchedTables) {
981 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
982 if (!ProcModel.hasInstrSchedModel())
983 return;
984
985 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000986 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000987 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000988 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000989
Andrew Trick9ef08822012-09-17 22:18:48 +0000990 SCTab.resize(SCTab.size() + 1);
991 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000992 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000993 SCDesc.NumMicroOps = 0;
994 SCDesc.BeginGroup = false;
995 SCDesc.EndGroup = false;
996 SCDesc.WriteProcResIdx = 0;
997 SCDesc.WriteLatencyIdx = 0;
998 SCDesc.ReadAdvanceIdx = 0;
999
1000 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +00001001 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +00001002 for (const CodeGenSchedTransition &CGT :
1003 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1004 if (CGT.ProcIndices[0] == 0 ||
1005 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001006 HasVariants = true;
1007 break;
1008 }
1009 }
1010 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001011 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1012 continue;
1013 }
1014
1015 // Determine if the SchedClass is actually reachable on this processor. If
1016 // not don't try to locate the processor resources, it will fail.
1017 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001018 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1019 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +00001020 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001021 continue;
1022 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001023 IdxVec Writes = SC.Writes;
1024 IdxVec Reads = SC.Reads;
1025 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001026 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001027 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001028 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001029 for (Record *RW : SC.InstRWs) {
1030 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001031 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001032 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001033 break;
1034 }
1035 }
1036 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001037 Writes.clear();
1038 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001039 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1040 Writes, Reads);
1041 }
1042 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001043 if (Writes.empty()) {
1044 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001045 for (Record *I : ProcModel.ItinRWDefs) {
1046 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001047 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001048 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001049 Writes, Reads);
1050 break;
1051 }
1052 }
1053 if (Writes.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001054 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1055 << " does not have resources for class " << SC.Name
1056 << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001057 }
1058 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001059 // Sum resources across all operand writes.
1060 std::vector<MCWriteProcResEntry> WriteProcResources;
1061 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001062 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001063 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001064 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001065 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001066 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001067 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001068
1069 // For each operand, create a latency entry.
1070 MCWriteLatencyEntry WLEntry;
1071 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001072 unsigned WriteID = WriteSeq.back();
1073 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1074 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1075 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001076 if (!SchedModels.hasReadOfWrite(
1077 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001078 WriteID = 0;
1079 }
1080 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001081
Craig Topper29c55dcb2016-02-13 06:03:32 +00001082 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001083
Andrew Trick9257b8f2012-09-22 02:24:21 +00001084 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001085 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001086
1087 // Mark the parent class as invalid for unsupported write types.
1088 if (WriteRes->getValueAsBit("Unsupported")) {
1089 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1090 break;
1091 }
1092 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1093 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1094 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1095 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001096 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1097 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001098
1099 // Create an entry for each ProcResource listed in WriteRes.
1100 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1101 std::vector<int64_t> Cycles =
1102 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001103
Clement Courbet5eeed772018-06-13 09:41:49 +00001104 if (Cycles.empty()) {
1105 // If ResourceCycles is not provided, default to one cycle per
1106 // resource.
1107 Cycles.resize(PRVec.size(), 1);
1108 } else if (Cycles.size() != PRVec.size()) {
1109 // If ResourceCycles is provided, check consistency.
1110 PrintFatalError(
1111 WriteRes->getLoc(),
1112 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1113 "!size(ProcResources): ")
1114 .concat(Twine(PRVec.size()))
1115 .concat(" vs ")
1116 .concat(Twine(Cycles.size())));
1117 }
1118
Andrew Trick4e67cba2013-03-14 21:21:50 +00001119 ExpandProcResources(PRVec, Cycles, ProcModel);
1120
Andrew Trick9ef08822012-09-17 22:18:48 +00001121 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1122 PRIdx != PREnd; ++PRIdx) {
1123 MCWriteProcResEntry WPREntry;
1124 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1125 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001126 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001127 // If this resource is already used in this sequence, add the current
1128 // entry's cycles so that the same resource appears to be used
1129 // serially, rather than multiple parallel uses. This is important for
1130 // in-order machine where the resource consumption is a hazard.
1131 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1132 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1133 if (WriteProcResources[WPRIdx].ProcResourceIdx
1134 == WPREntry.ProcResourceIdx) {
1135 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1136 break;
1137 }
1138 }
1139 if (WPRIdx == WPREnd)
1140 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001141 }
1142 }
1143 WriteLatencies.push_back(WLEntry);
1144 }
1145 // Create an entry for each operand Read in this SchedClass.
1146 // Entries must be sorted first by UseIdx then by WriteResourceID.
1147 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1148 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001149 Record *ReadAdvance =
1150 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001151 if (!ReadAdvance)
1152 continue;
1153
1154 // Mark the parent class as invalid for unsupported write types.
1155 if (ReadAdvance->getValueAsBit("Unsupported")) {
1156 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1157 break;
1158 }
1159 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1160 IdxVec WriteIDs;
1161 if (ValidWrites.empty())
1162 WriteIDs.push_back(0);
1163 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001164 for (Record *VW : ValidWrites) {
1165 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001166 }
1167 }
Fangrui Song0cac7262018-09-27 02:13:45 +00001168 llvm::sort(WriteIDs);
Craig Topper29c55dcb2016-02-13 06:03:32 +00001169 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001170 MCReadAdvanceEntry RAEntry;
1171 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001172 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001173 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1174 ReadAdvanceEntries.push_back(RAEntry);
1175 }
1176 }
1177 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1178 WriteProcResources.clear();
1179 WriteLatencies.clear();
1180 ReadAdvanceEntries.clear();
1181 }
1182 // Add the information for this SchedClass to the global tables using basic
1183 // compression.
1184 //
1185 // WritePrecRes entries are sorted by ProcResIdx.
Fangrui Song0cac7262018-09-27 02:13:45 +00001186 llvm::sort(WriteProcResources, LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001187
1188 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1189 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1190 std::search(SchedTables.WriteProcResources.begin(),
1191 SchedTables.WriteProcResources.end(),
1192 WriteProcResources.begin(), WriteProcResources.end());
1193 if (WPRPos != SchedTables.WriteProcResources.end())
1194 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1195 else {
1196 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1197 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1198 WriteProcResources.end());
1199 }
1200 // Latency entries must remain in operand order.
1201 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1202 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1203 std::search(SchedTables.WriteLatencies.begin(),
1204 SchedTables.WriteLatencies.end(),
1205 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001206 if (WLPos != SchedTables.WriteLatencies.end()) {
1207 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1208 SCDesc.WriteLatencyIdx = idx;
1209 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1210 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1211 std::string::npos) {
1212 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1213 }
1214 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001215 else {
1216 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001217 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1218 WriteLatencies.begin(),
1219 WriteLatencies.end());
1220 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1221 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001222 }
1223 // ReadAdvanceEntries must remain in operand order.
1224 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1225 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1226 std::search(SchedTables.ReadAdvanceEntries.begin(),
1227 SchedTables.ReadAdvanceEntries.end(),
1228 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1229 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1230 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1231 else {
1232 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1233 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1234 ReadAdvanceEntries.end());
1235 }
1236 }
1237}
1238
Andrew Tricka72fca62012-09-17 22:18:50 +00001239// Emit SchedClass tables for all processors and associated global tables.
1240void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1241 raw_ostream &OS) {
1242 // Emit global WriteProcResTable.
1243 OS << "\n// {ProcResourceIdx, Cycles}\n"
1244 << "extern const llvm::MCWriteProcResEntry "
1245 << Target << "WriteProcResTable[] = {\n"
1246 << " { 0, 0}, // Invalid\n";
1247 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1248 WPRIdx != WPREnd; ++WPRIdx) {
1249 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1250 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1251 << format("%2d", WPREntry.Cycles) << "}";
1252 if (WPRIdx + 1 < WPREnd)
1253 OS << ',';
1254 OS << " // #" << WPRIdx << '\n';
1255 }
1256 OS << "}; // " << Target << "WriteProcResTable\n";
1257
1258 // Emit global WriteLatencyTable.
1259 OS << "\n// {Cycles, WriteResourceID}\n"
1260 << "extern const llvm::MCWriteLatencyEntry "
1261 << Target << "WriteLatencyTable[] = {\n"
1262 << " { 0, 0}, // Invalid\n";
1263 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1264 WLIdx != WLEnd; ++WLIdx) {
1265 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1266 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1267 << format("%2d", WLEntry.WriteResourceID) << "}";
1268 if (WLIdx + 1 < WLEnd)
1269 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001270 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001271 }
1272 OS << "}; // " << Target << "WriteLatencyTable\n";
1273
1274 // Emit global ReadAdvanceTable.
1275 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1276 << "extern const llvm::MCReadAdvanceEntry "
1277 << Target << "ReadAdvanceTable[] = {\n"
1278 << " {0, 0, 0}, // Invalid\n";
1279 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1280 RAIdx != RAEnd; ++RAIdx) {
1281 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1282 OS << " {" << RAEntry.UseIdx << ", "
1283 << format("%2d", RAEntry.WriteResourceID) << ", "
1284 << format("%2d", RAEntry.Cycles) << "}";
1285 if (RAIdx + 1 < RAEnd)
1286 OS << ',';
1287 OS << " // #" << RAIdx << '\n';
1288 }
1289 OS << "}; // " << Target << "ReadAdvanceTable\n";
1290
1291 // Emit a SchedClass table for each processor.
1292 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1293 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1294 if (!PI->hasInstrSchedModel())
1295 continue;
1296
1297 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001298 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001299
1300 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1301 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1302 OS << "static const llvm::MCSchedClassDesc "
1303 << PI->ModelName << "SchedClasses[] = {\n";
1304
1305 // The first class is always invalid. We no way to distinguish it except by
1306 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001307 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001308 && "invalid class not first");
1309 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1310 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001311 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001312
1313 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1314 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1315 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1316 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1317 if (SchedClass.Name.size() < 18)
1318 OS.indent(18 - SchedClass.Name.size());
1319 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001320 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1321 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001322 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1323 << ", " << MCDesc.NumWriteProcResEntries
1324 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1325 << ", " << MCDesc.NumWriteLatencyEntries
1326 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001327 << ", " << MCDesc.NumReadAdvanceEntries
1328 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001329 }
1330 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1331 }
1332}
1333
Andrew Trick87255e32012-07-07 04:00:00 +00001334void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1335 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001336 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001337 // Emit extra processor info if available.
1338 if (PM.hasExtraProcessorInfo())
1339 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001340 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001341 if (PM.hasInstrSchedModel())
1342 EmitProcessorResources(PM, OS);
1343 else if(!PM.ProcResourceDefs.empty())
1344 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001345 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001346
Andrew Trick73d77362012-06-05 03:44:40 +00001347 // Begin processor itinerary properties
1348 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001349 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1350 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1351 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1352 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1353 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1354 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1355 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001356
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001357 bool PostRAScheduler =
1358 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001359
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001360 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1361 << "PostRAScheduler\n";
1362
1363 bool CompleteModel =
1364 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1365
1366 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1367 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001368
Craig Topper29c55dcb2016-02-13 06:03:32 +00001369 OS << " " << PM.Index << ", // Processor ID\n";
1370 if (PM.hasInstrSchedModel())
1371 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1372 << " " << PM.ModelName << "SchedClasses" << ",\n"
1373 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001374 << " " << (SchedModels.schedClassEnd()
1375 - SchedModels.schedClassBegin()) << ",\n";
1376 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001377 OS << " nullptr, nullptr, 0, 0,"
1378 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001379 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001380 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001381 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001382 OS << " nullptr, // No Itinerary\n";
1383 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001384 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001385 else
Clement Courbetb4493792018-04-10 08:16:37 +00001386 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001387 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001388 }
Jim Laskey3763a502005-10-31 17:16:01 +00001389}
1390
1391//
Andrew Trick87255e32012-07-07 04:00:00 +00001392// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001393//
Andrew Trick87255e32012-07-07 04:00:00 +00001394void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001395 OS << "#ifdef DBGFIELD\n"
1396 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1397 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001398 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001399 << "#define DBGFIELD(x) x,\n"
1400 << "#else\n"
1401 << "#define DBGFIELD(x)\n"
1402 << "#endif\n";
1403
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001404 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001405 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001406 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001407 EmitStageAndOperandCycleData(OS, ProcItinLists);
1408 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001409 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001410 OS << "\n// ===============================================================\n"
1411 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001412
Andrew Trick9ef08822012-09-17 22:18:48 +00001413 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001414 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1415 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001416 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001417 EmitSchedClassTables(SchedTables, OS);
1418
Craig Topper2982b842019-03-05 18:54:38 +00001419 OS << "\n#undef DBGFIELD\n";
1420
Andrew Tricka72fca62012-09-17 22:18:50 +00001421 // Emit the processor machine model
1422 EmitProcessorModels(OS);
Jim Laskey86f002c2005-10-27 19:47:21 +00001423}
1424
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001425static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1426 std::string Buffer;
1427 raw_string_ostream Stream(Buffer);
1428
1429 // Collect all the PredicateProlog records and print them to the output
1430 // stream.
1431 std::vector<Record *> Prologs =
1432 Records.getAllDerivedDefinitions("PredicateProlog");
Fangrui Song0cac7262018-09-27 02:13:45 +00001433 llvm::sort(Prologs, LessRecord());
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001434 for (Record *P : Prologs)
1435 Stream << P->getValueAsString("Code") << '\n';
1436
1437 Stream.flush();
1438 OS << Buffer;
1439}
1440
1441static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001442 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001443 raw_ostream &OS) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001444 std::string Buffer;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001445 raw_string_ostream SS(Buffer);
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001446
1447 auto IsTruePredicate = [](const Record *Rec) {
1448 return Rec->isSubClassOf("MCSchedPredicate") &&
1449 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1450 };
1451
1452 // If not all predicates are MCTrue, then we need an if-stmt.
1453 unsigned NumNonTruePreds =
1454 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001455
1456 SS.indent(PE.getIndentLevel() * 2);
1457
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001458 if (NumNonTruePreds) {
1459 bool FirstNonTruePredicate = true;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001460 SS << "if (";
1461
1462 PE.setIndentLevel(PE.getIndentLevel() + 2);
1463
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001464 for (const Record *Rec : T.PredTerm) {
1465 // Skip predicates that evaluate to "true".
1466 if (IsTruePredicate(Rec))
1467 continue;
1468
1469 if (FirstNonTruePredicate) {
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001470 FirstNonTruePredicate = false;
1471 } else {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001472 SS << "\n";
1473 SS.indent(PE.getIndentLevel() * 2);
1474 SS << "&& ";
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001475 }
1476
1477 if (Rec->isSubClassOf("MCSchedPredicate")) {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001478 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001479 continue;
1480 }
1481
1482 // Expand this legacy predicate and wrap it around braces if there is more
1483 // than one predicate to expand.
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001484 SS << ((NumNonTruePreds > 1) ? "(" : "")
1485 << Rec->getValueAsString("Predicate")
1486 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagio95140022018-05-25 15:55:37 +00001487 }
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001488
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001489 SS << ")\n"; // end of if-stmt
1490 PE.decreaseIndentLevel();
1491 SS.indent(PE.getIndentLevel() * 2);
1492 PE.decreaseIndentLevel();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001493 }
1494
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001495 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1496 SS.flush();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001497 OS << Buffer;
1498}
1499
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001500// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1501// epilogue code for the auto-generated helper.
1502void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1503 if (ShouldReturnZero) {
Andrea Di Biagio95140022018-05-25 15:55:37 +00001504 OS << " // Don't know how to resolve this scheduling class.\n"
1505 << " return 0;\n";
1506 return;
1507 }
1508
1509 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1510}
1511
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001512bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1513 return all_of(T.PredTerm, [](const Record *Rec) {
1514 return Rec->isSubClassOf("MCSchedPredicate");
1515 });
1516}
1517
1518void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1519 IdxVec &VariantClasses,
1520 bool OnlyExpandMCInstPredicates) {
1521 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1522 // Ignore non-variant scheduling classes.
1523 if (SC.Transitions.empty())
1524 continue;
1525
1526 if (OnlyExpandMCInstPredicates) {
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001527 // Ignore this variant scheduling class no transitions use any meaningful
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001528 // MCSchedPredicate definitions.
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001529 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001530 return hasMCSchedPredicates(T);
1531 }))
1532 continue;
1533 }
1534
1535 VariantClasses.push_back(SC.Index);
1536 }
1537}
1538
1539void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1540 // A variant scheduling class may define transitions for multiple
1541 // processors. This function identifies wich processors are associated with
1542 // transition rules specified by variant class `SC`.
1543 for (const CodeGenSchedTransition &T : SC.Transitions) {
1544 IdxVec PI;
1545 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1546 ProcIndices.begin(), ProcIndices.end(),
1547 std::back_inserter(PI));
1548 ProcIndices.swap(PI);
1549 }
1550}
1551
1552void SubtargetEmitter::emitSchedModelHelpersImpl(
1553 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1554 IdxVec VariantClasses;
1555 collectVariantClasses(SchedModels, VariantClasses,
1556 OnlyExpandMCInstPredicates);
1557
1558 if (VariantClasses.empty()) {
1559 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1560 return;
1561 }
1562
1563 // Construct a switch statement where the condition is a check on the
1564 // scheduling class identifier. There is a `case` for every variant class
1565 // defined by the processor models of this target.
1566 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1567 // a variant scheduling class to another scheduling class. Rules are
1568 // described by instances of CodeGenSchedTransition. Note that transitions may
1569 // not be valid for all processors.
1570 OS << " switch (SchedClass) {\n";
1571 for (unsigned VC : VariantClasses) {
1572 IdxVec ProcIndices;
1573 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1574 collectProcessorIndices(SC, ProcIndices);
1575
1576 OS << " case " << VC << ": // " << SC.Name << '\n';
1577
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +00001578 PredicateExpander PE(Target);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001579 PE.setByRef(false);
1580 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1581 for (unsigned PI : ProcIndices) {
1582 OS << " ";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001583
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001584 // Emit a guard on the processor ID.
1585 if (PI != 0) {
1586 OS << (OnlyExpandMCInstPredicates
1587 ? "if (CPUID == "
1588 : "if (SchedModel->getProcessorID() == ");
1589 OS << PI << ") ";
1590 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1591 }
1592
1593 // Now emit transitions associated with processor PI.
1594 for (const CodeGenSchedTransition &T : SC.Transitions) {
1595 if (PI != 0 && !count(T.ProcIndices, PI))
1596 continue;
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001597
1598 // Emit only transitions based on MCSchedPredicate, if it's the case.
1599 // At least the transition specified by NoSchedPred is emitted,
1600 // which becomes the default transition for those variants otherwise
1601 // not based on MCSchedPredicate.
1602 // FIXME: preferably, llvm-mca should instead assume a reasonable
1603 // default when a variant transition is not based on MCSchedPredicate
1604 // for a given processor.
1605 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1606 continue;
1607
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001608 PE.setIndentLevel(3);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001609 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1610 }
1611
1612 OS << " }\n";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001613
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001614 if (PI == 0)
1615 break;
1616 }
1617
1618 if (SC.isInferred())
1619 OS << " return " << SC.Index << ";\n";
1620 OS << " break;\n";
1621 }
1622
1623 OS << " };\n";
1624
1625 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1626}
1627
Andrea Di Biagio95140022018-05-25 15:55:37 +00001628void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1629 raw_ostream &OS) {
1630 OS << "unsigned " << ClassName
1631 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1632 << " const TargetSchedModel *SchedModel) const {\n";
1633
1634 // Emit the predicate prolog code.
1635 emitPredicateProlog(Records, OS);
1636
1637 // Emit target predicates.
1638 emitSchedModelHelpersImpl(OS);
Clement Courbet41c8af32018-10-25 07:44:01 +00001639
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001640 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagio95140022018-05-25 15:55:37 +00001641
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001642 OS << "unsigned " << ClassName
1643 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1644 << " unsigned CPUID) const {\n"
1645 << " return " << Target << "_MC"
1646 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001647 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1648
1649 STIPredicateExpander PE(Target);
1650 PE.setClassPrefix(ClassName);
1651 PE.setExpandDefinition(true);
1652 PE.setByRef(false);
1653 PE.setIndentLevel(0);
1654
1655 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1656 PE.expandSTIPredicate(OS, Fn);
Andrew Trickc6c88152012-09-18 03:41:43 +00001657}
1658
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001659void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1660 raw_ostream &OS) {
1661 const CodeGenHwModes &CGH = TGT.getHwModes();
1662 assert(CGH.getNumModeIds() > 0);
1663 if (CGH.getNumModeIds() == 1)
1664 return;
1665
1666 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1667 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1668 const HwMode &HM = CGH.getMode(M);
1669 OS << " if (checkFeatures(\"" << HM.Features
1670 << "\")) return " << M << ";\n";
1671 }
1672 OS << " return 0;\n}\n";
1673}
1674
Jim Laskey86f002c2005-10-27 19:47:21 +00001675//
Jim Laskeya2b52352005-10-26 17:30:34 +00001676// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1677// the subtarget features string.
1678//
Evan Cheng54b68e32011-07-01 20:45:01 +00001679void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1680 unsigned NumFeatures,
1681 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001682 std::vector<Record*> Features =
1683 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +00001684 llvm::sort(Features, LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001685
Andrew Trickdb6ed642011-04-01 01:56:55 +00001686 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1687 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001688 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001689 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001690 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001691 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1692 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001693
1694 if (Features.empty()) {
1695 OS << "}\n";
1696 return;
1697 }
1698
Andrew Trickba7b9212012-09-18 05:33:15 +00001699 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001700 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001701
Craig Topper29c55dcb2016-02-13 06:03:32 +00001702 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001703 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001704 StringRef Instance = R->getName();
1705 StringRef Value = R->getValueAsString("Value");
1706 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001707
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001708 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001709 OS << " if (Bits[" << Target << "::"
1710 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001711 << Attribute << " = " << Value << ";\n";
1712 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001713 OS << " if (Bits[" << Target << "::"
1714 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001715 << Attribute << " < " << Value << ") "
1716 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001717 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001718
Evan Chengfe6e4052011-06-30 01:53:36 +00001719 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001720}
1721
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001722void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001723 OS << "namespace " << Target << "_MC {\n"
1724 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1725 << " const MCInst *MI, unsigned CPUID) {\n";
1726 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1727 OS << "}\n";
1728 OS << "} // end of namespace " << Target << "_MC\n\n";
1729
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001730 OS << "struct " << Target
1731 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1732 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1733 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
Craig Topperca268082019-03-05 18:54:34 +00001734 << " ArrayRef<SubtargetSubTypeKV> PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001735 << " const MCWriteProcResEntry *WPR,\n"
1736 << " const MCWriteLatencyEntry *WL,\n"
1737 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1738 << " const unsigned *OC, const unsigned *FP) :\n"
Craig Topper2982b842019-03-05 18:54:38 +00001739 << " MCSubtargetInfo(TT, CPU, FS, PF, PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001740 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1741 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001742 << " const MCInst *MI, unsigned CPUID) const override {\n"
1743 << " return " << Target << "_MC"
1744 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001745 OS << " }\n";
1746 OS << "};\n";
1747}
1748
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001749void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1750 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1751 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1752
1753 STIPredicateExpander PE(Target);
1754 PE.setExpandForMC(true);
1755 PE.setByRef(true);
1756 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1757 PE.expandSTIPredicate(OS, Fn);
1758
1759 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1760
1761 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1762 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1763
1764 std::string ClassPrefix = Target + "MCInstrAnalysis";
1765 PE.setExpandDefinition(true);
1766 PE.setClassPrefix(ClassPrefix);
1767 PE.setIndentLevel(0);
1768 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1769 PE.expandSTIPredicate(OS, Fn);
1770
1771 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1772}
1773
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001774//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001775// SubtargetEmitter::run - Main subtarget enumeration emitter.
1776//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001777void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001778 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001779
Evan Cheng4d1ca962011-07-08 01:53:10 +00001780 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001781 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001782
Craig Topper4f613082019-03-01 02:19:26 +00001783 DenseMap<Record *, unsigned> FeatureMap;
1784
Evan Cheng4d1ca962011-07-08 01:53:10 +00001785 OS << "namespace llvm {\n";
Craig Topper4f613082019-03-01 02:19:26 +00001786 Enumeration(OS, FeatureMap);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001787 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001788 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1789
Evan Cheng54b68e32011-07-01 20:45:01 +00001790 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001791 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001792
Evan Cheng54b68e32011-07-01 20:45:01 +00001793 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001794#if 0
1795 OS << "namespace {\n";
1796#endif
Craig Topper4f613082019-03-01 02:19:26 +00001797 unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap);
Evan Chengbc153d42011-07-14 20:59:42 +00001798 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001799 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001800 OS << "\n";
Craig Topper2982b842019-03-05 18:54:38 +00001801 unsigned NumProcs = CPUKeyValues(OS, FeatureMap);
1802 OS << "\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001803#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001804 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001805#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001806
1807 // MCInstrInfo initialization routine.
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001808 emitGenMCSubtargetInfo(OS);
1809
Craig Topper194cb742017-10-24 15:50:55 +00001810 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001811 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001812 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001813 OS << " return new " << Target << "GenMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001814 if (NumFeatures)
1815 OS << Target << "FeatureKV, ";
1816 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001817 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001818 if (NumProcs)
1819 OS << Target << "SubTypeKV, ";
1820 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001821 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001822 OS << '\n'; OS.indent(22);
Craig Topper2982b842019-03-05 18:54:38 +00001823 OS << Target << "WriteProcResTable, "
Andrew Trickab722bd2012-09-18 03:18:56 +00001824 << Target << "WriteLatencyTable, "
1825 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001826 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001827 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001828 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001829 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001830 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001831 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001832 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001833 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001834
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001835 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001836
1837 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1838
1839 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001840 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001841
1842 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001843 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001844 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1845
1846 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1847
Evan Cheng0d639a22011-07-01 21:01:15 +00001848 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001849 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001850 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001851
1852 std::string ClassName = Target + "GenSubtargetInfo";
1853 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001854 OS << "class DFAPacketizer;\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001855 OS << "namespace " << Target << "_MC {\n"
1856 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1857 << " const MCInst *MI, unsigned CPUID);\n"
1858 << "}\n\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001859 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001860 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001861 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001862 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001863 << " unsigned resolveSchedClass(unsigned SchedClass, "
1864 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001865 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001866 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1867 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001868 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001869 << " const;\n";
1870 if (TGT.getHwModes().getNumModeIds() > 1)
1871 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001872
1873 STIPredicateExpander PE(Target);
1874 PE.setByRef(false);
1875 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1876 PE.expandSTIPredicate(OS, Fn);
1877
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001878 OS << "};\n"
1879 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001880
1881 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1882
1883 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001884 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001885
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001886 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001887 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001888 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
Craig Topperca268082019-03-05 18:54:34 +00001889 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001890 OS << "extern const llvm::MCWriteProcResEntry "
1891 << Target << "WriteProcResTable[];\n";
1892 OS << "extern const llvm::MCWriteLatencyEntry "
1893 << Target << "WriteLatencyTable[];\n";
1894 OS << "extern const llvm::MCReadAdvanceEntry "
1895 << Target << "ReadAdvanceTable[];\n";
1896
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001897 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001898 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1899 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001900 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001901 }
1902
Daniel Sanders50f17232015-09-15 16:17:27 +00001903 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1904 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001905 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001906 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001907 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001908 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001909 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001910 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001911 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001912 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001913 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001914 OS << '\n'; OS.indent(24);
Craig Topper2982b842019-03-05 18:54:38 +00001915 OS << Target << "WriteProcResTable, "
Andrew Trickab722bd2012-09-18 03:18:56 +00001916 << Target << "WriteLatencyTable, "
1917 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001918 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001919 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001920 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001921 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001922 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001923 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001924 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001925 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001926
Andrew Trickc6c88152012-09-18 03:41:43 +00001927 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001928 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001929
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001930 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001931
1932 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001933
1934 EmitMCInstrAnalysisPredicateFunctions(OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001935}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001936
1937namespace llvm {
1938
1939void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001940 CodeGenTarget CGTarget(RK);
1941 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001942}
1943
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001944} // end namespace llvm