blob: 198e5b4722bbb5ab9239b9d97d845b568ed09f91 [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
274 // The {{}} is for the "implies" section of this data structure.
275 OS << " },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000276 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000277
Jim Laskey19595752005-10-28 15:20:43 +0000278 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000279 OS << "};\n";
280
Evan Cheng54b68e32011-07-01 20:45:01 +0000281 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000282}
Jim Laskeya1beea62005-10-22 07:59:56 +0000283
Jim Laskeya2b52352005-10-26 17:30:34 +0000284//
David Goodwind813cbf2009-08-17 16:02:57 +0000285// FormItineraryStageString - Compose a string containing the stage
286// data initialization for the specified itinerary. N is the number
287// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000288//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000289void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
290 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000291 std::string &ItinString,
292 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000293 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000294 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000295
296 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000297 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000298 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000299 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000300 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000301
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000302 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000303 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000304 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000305
Jim Laskeydffe5972005-10-28 21:47:29 +0000306 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000307 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000308
Jim Laskey19595752005-10-28 15:20:43 +0000309 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000310 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000311 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000312 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000313 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000314 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000315
David Goodwinb369ee42009-08-12 18:31:53 +0000316 int TimeInc = Stage->getValueAsInt("TimeInc");
317 ItinString += ", " + itostr(TimeInc);
318
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000319 int Kind = Stage->getValueAsInt("Kind");
320 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
321
Jim Laskey19595752005-10-28 15:20:43 +0000322 // Close off stage
323 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000324 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000325 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000326}
327
328//
David Goodwind813cbf2009-08-17 16:02:57 +0000329// FormItineraryOperandCycleString - Compose a string containing the
330// operand cycle initialization for the specified itinerary. N is the
331// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000332//
David Goodwind813cbf2009-08-17 16:02:57 +0000333void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
334 std::string &ItinString, unsigned &NOperandCycles) {
335 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000336 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000337 ItinData->getValueAsListOfInts("OperandCycles");
338
339 // For each operand cycle
340 unsigned N = NOperandCycles = OperandCycleList.size();
341 for (unsigned i = 0; i < N;) {
342 // Next operand cycle
343 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000344
David Goodwind813cbf2009-08-17 16:02:57 +0000345 ItinString += " " + itostr(OCycle);
346 if (++i < N) ItinString += ", ";
347 }
348}
349
Evan Cheng0097dd02010-09-28 23:50:49 +0000350void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
351 Record *ItinData,
352 std::string &ItinString,
353 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000354 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000355 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000356 unsigned i = 0;
357 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000358 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000359 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000360 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000361 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000362 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000363 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000364 }
365}
366
David Goodwind813cbf2009-08-17 16:02:57 +0000367//
Andrew Trick87255e32012-07-07 04:00:00 +0000368// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
369// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
370// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000371//
Andrew Trick87255e32012-07-07 04:00:00 +0000372void SubtargetEmitter::
373EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000374 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000375 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000376 // Multiple processor models may share an itinerary record. Emit it once.
377 SmallPtrSet<Record*, 8> ItinsDefSet;
378
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000379 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000380 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000381
Craig Topper29c55dcb2016-02-13 06:03:32 +0000382 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000383 continue;
384
Craig Topper37eeb322018-03-23 00:02:45 +0000385 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000386 if (FUs.empty())
387 continue;
388
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000389 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000390 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000391 << "namespace " << Name << "FU {\n";
392
393 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000394 OS << " const unsigned " << FUs[j]->getName()
395 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000396
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000397 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000398
Craig Topper37eeb322018-03-23 00:02:45 +0000399 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000400 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000401 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000402 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000403
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000404 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000405 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000406 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000407 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000408
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000409 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000410 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000411 }
412
Jim Laskey19595752005-10-28 15:20:43 +0000413 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000414 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
415 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000416 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000417
David Goodwind813cbf2009-08-17 16:02:57 +0000418 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000419 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000420 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000421 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000422
423 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000424 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000425 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000426 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000427
Andrew Trick87255e32012-07-07 04:00:00 +0000428 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000429 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000430 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000431 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000432 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000433 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000434 // Add process itinerary to the list.
435 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000436
Andrew Trick87255e32012-07-07 04:00:00 +0000437 // If this processor defines no itineraries, then leave the itinerary list
438 // empty.
439 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000440 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000441 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000442
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000443 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000444
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000445 ItinList.resize(SchedModels.numInstrSchedClasses());
446 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
447
448 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000449 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
450
Jim Laskeydffe5972005-10-28 21:47:29 +0000451 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000452 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000453
Jim Laskey19595752005-10-28 15:20:43 +0000454 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000455 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000456 unsigned NStages = 0;
457 if (ItinData)
458 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000459
David Goodwind813cbf2009-08-17 16:02:57 +0000460 // Get string and operand cycle count
461 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000462 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000463 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000464 if (ItinData) {
465 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
466 NOperandCycles);
467
468 FormItineraryBypassString(Name, ItinData, ItinBypassString,
469 NOperandCycles);
470 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000471
David Goodwind813cbf2009-08-17 16:02:57 +0000472 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000473 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000474 if (NStages > 0) {
475 FindStage = ItinStageMap[ItinStageString];
476 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000477 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
478 StageTable += ItinStageString + ", // " + itostr(StageCount);
479 if (NStages > 1)
480 StageTable += "-" + itostr(StageCount + NStages - 1);
481 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000482 // Record Itin class number.
483 ItinStageMap[ItinStageString] = FindStage = StageCount;
484 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000485 }
486 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000487
David Goodwind813cbf2009-08-17 16:02:57 +0000488 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000489 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000490 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000491 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
492 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000493 if (FindOperandCycle == 0) {
494 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000495 OperandCycleTable += ItinOperandCycleString + ", // ";
496 std::string OperandIdxComment = itostr(OperandCycleCount);
497 if (NOperandCycles > 1)
498 OperandIdxComment += "-"
499 + itostr(OperandCycleCount + NOperandCycles - 1);
500 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000501 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000502 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000503 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000504 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000505 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000506 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000507 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000508 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000509
Evan Cheng367a5df2010-09-09 18:18:55 +0000510 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000511 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
512 InstrItinerary Intinerary = {
513 NumUOps,
514 FindStage,
515 uint16_t(FindStage + NStages),
516 FindOperandCycle,
517 uint16_t(FindOperandCycle + NOperandCycles),
518 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000519
Jim Laskey19595752005-10-28 15:20:43 +0000520 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000521 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000522 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000523 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000524
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000525 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000526 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000527 StageTable += "};\n";
528
529 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000530 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000531 OperandCycleTable += "};\n";
532
Andrew Trick87255e32012-07-07 04:00:00 +0000533 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000534 BypassTable += "};\n";
535
David Goodwind813cbf2009-08-17 16:02:57 +0000536 // Emit tables.
537 OS << StageTable;
538 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000539 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000540}
541
Andrew Trick87255e32012-07-07 04:00:00 +0000542//
543// EmitProcessorData - Generate data for processor itineraries that were
544// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
545// Itineraries for each processor. The Itinerary lists are indexed on
546// CodeGenSchedClass::Index.
547//
548void SubtargetEmitter::
549EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000550 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000551 // Multiple processor models may share an itinerary record. Emit it once.
552 SmallPtrSet<Record*, 8> ItinsDefSet;
553
Andrew Trick87255e32012-07-07 04:00:00 +0000554 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000555 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000556 ProcItinListsIter = ProcItinLists.begin();
557 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000558 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000559
Andrew Trick87255e32012-07-07 04:00:00 +0000560 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000561 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000562 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000563
Andrew Trick87255e32012-07-07 04:00:00 +0000564 // Get the itinerary list for the processor.
565 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000566 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000567
Pete Cooperc0eb1532014-09-02 23:23:34 +0000568 // Empty itineraries aren't referenced anywhere in the tablegen output
569 // so don't emit them.
570 if (ItinList.empty())
571 continue;
572
Andrew Trick87255e32012-07-07 04:00:00 +0000573 OS << "\n";
574 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000575
576 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000577 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000578
579 // For each itinerary class in CodeGenSchedClass::Index order.
580 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
581 InstrItinerary &Intinerary = ItinList[j];
582
583 // Emit Itinerary in the form of
584 // { firstStage, lastStage, firstCycle, lastCycle } // index
585 OS << " { " <<
586 Intinerary.NumMicroOps << ", " <<
587 Intinerary.FirstStage << ", " <<
588 Intinerary.LastStage << ", " <<
589 Intinerary.FirstOperandCycle << ", " <<
590 Intinerary.LastOperandCycle << " }" <<
591 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
592 }
593 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000594 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
595 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000596 OS << "};\n";
597 }
598}
599
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000600// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000601// value defined in the C++ header. The Record is null if the processor does not
602// define a model.
603void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000604 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000605 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000606 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000607 if (V >= 0)
608 OS << V << Separator << " // " << Name;
609 else
Andrew Trick87255e32012-07-07 04:00:00 +0000610 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000611 OS << '\n';
612}
613
Clement Courbet39911e22018-02-08 08:46:48 +0000614void SubtargetEmitter::EmitProcessorResourceSubUnits(
615 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
616 OS << "\nstatic const unsigned " << ProcModel.ModelName
617 << "ProcResourceSubUnits[] = {\n"
618 << " 0, // Invalid\n";
619
620 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
621 Record *PRDef = ProcModel.ProcResourceDefs[i];
622 if (!PRDef->isSubClassOf("ProcResGroup"))
623 continue;
624 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
625 for (Record *RUDef : ResUnits) {
626 Record *const RU =
627 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
628 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
629 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
630 }
631 }
632 OS << " // " << PRDef->getName() << "\n";
633 }
634 OS << "};\n";
635}
636
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000637static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
638 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000639 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000640 if (Record *RCU = ProcModel.RetireControlUnit) {
641 ReorderBufferSize =
642 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
643 MaxRetirePerCycle =
644 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
645 }
646
647 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
648 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
649}
650
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000651static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
652 unsigned NumRegisterFiles,
653 unsigned NumCostEntries, raw_ostream &OS) {
654 if (NumRegisterFiles)
655 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
656 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000657 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000658
659 OS << ", // Number of register files.\n ";
660 if (NumCostEntries)
661 OS << ProcModel.ModelName << "RegisterCosts,\n ";
662 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000663 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000664 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000665}
666
667unsigned
668SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
669 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000670 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
671 return RF.hasDefaultCosts();
672 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000673 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000674
675 // Print the RegisterCost table first.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000676 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000677 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
678 << "RegisterCosts"
679 << "[] = {\n";
680
681 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
682 // Skip register files with a default cost table.
683 if (RF.hasDefaultCosts())
684 continue;
685 // Add entries to the cost table.
686 for (const CodeGenRegisterCost &RC : RF.Costs) {
687 OS << " { ";
688 Record *Rec = RC.RCDef;
689 if (Rec->getValue("Namespace"))
690 OS << Rec->getValueAsString("Namespace") << "::";
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000691 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
692 << RC.AllowMoveElimination << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000693 }
694 }
695 OS << "};\n";
696
697 // Now generate a table with register file info.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000698 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
699 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000700 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
701 << "RegisterFiles"
702 << "[] = {\n"
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000703 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000704 unsigned CostTblIndex = 0;
705
706 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
707 OS << " { ";
708 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
709 unsigned NumCostEntries = RD.Costs.size();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000710 OS << NumCostEntries << ", " << CostTblIndex << ", "
711 << RD.MaxMovesEliminatedPerCycle << ", "
712 << RD.AllowZeroMoveEliminationOnly << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000713 CostTblIndex += NumCostEntries;
714 }
715 OS << "};\n";
716
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000717 return CostTblIndex;
718}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000719
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000720void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
721 raw_ostream &OS) {
722 unsigned QueueID = 0;
723 if (ProcModel.LoadQueue) {
724 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
725 QueueID =
726 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
727 std::find(ProcModel.ProcResourceDefs.begin(),
728 ProcModel.ProcResourceDefs.end(), Queue));
729 }
730 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
731
732 QueueID = 0;
733 if (ProcModel.StoreQueue) {
734 const Record *Queue =
735 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
736 QueueID =
737 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
738 std::find(ProcModel.ProcResourceDefs.begin(),
739 ProcModel.ProcResourceDefs.end(), Queue));
740 }
741 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
742}
743
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000744void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
745 raw_ostream &OS) {
746 // Generate a table of register file descriptors (one entry per each user
747 // defined register file), and a table of register costs.
748 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
749
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000750 // Now generate a table for the extra processor info.
751 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000752 << "ExtraInfo = {\n ";
753
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000754 // Add information related to the retire control unit.
755 EmitRetireControlUnitInfo(ProcModel, OS);
756
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000757 // Add information related to the register files (i.e. where to find register
758 // file descriptors and register costs).
759 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
760 NumCostEntries, OS);
761
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000762 // Add information about load/store queues.
763 EmitLoadStoreQueueInfo(ProcModel, OS);
764
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000765 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000766}
767
Andrew Trick23f3c652012-09-17 22:18:45 +0000768void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
769 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000770 EmitProcessorResourceSubUnits(ProcModel, OS);
771
Jinsong Ji05941622018-09-18 15:38:56 +0000772 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000773 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
774 << "ProcResources"
775 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000776 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000777
Clement Courbet39911e22018-02-08 08:46:48 +0000778 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000779 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
780 Record *PRDef = ProcModel.ProcResourceDefs[i];
781
Craig Topper24064772014-04-15 07:20:03 +0000782 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000783 unsigned SuperIdx = 0;
784 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000785 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000786 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000787 if (PRDef->isSubClassOf("ProcResGroup")) {
788 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000789 for (Record *RU : ResUnits) {
790 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000791 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000792 }
793 }
794 else {
795 // Find the SuperIdx
796 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000797 SuperDef =
798 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
799 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000800 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
801 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000802 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000803 }
804 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000805 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000806 if (PRDef->getName().size() < 15)
807 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000808 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
809 if (SubUnitsBeginOffset != SubUnitsOffset) {
810 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
811 << SubUnitsBeginOffset;
812 } else {
813 OS << "nullptr";
814 }
815 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000816 if (SuperDef)
817 OS << ", Super=" << SuperDef->getName();
818 OS << "\n";
819 }
820 OS << "};\n";
821}
822
Andrew Trick9ef08822012-09-17 22:18:48 +0000823// Find the WriteRes Record that defines processor resources for this
824// SchedWrite.
825Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000826 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000827
828 // Check if the SchedWrite is already subtarget-specific and directly
829 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000830 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
831 return SchedWrite.TheDef;
832
Craig Topper24064772014-04-15 07:20:03 +0000833 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000834 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000835 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000836 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000837 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
838 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
839 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
840 continue;
841 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000842 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000843 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000844 "defined for processor " + ProcModel.ModelName +
845 " Ensure only one SchedAlias exists per RW.");
846 AliasDef = AliasRW.TheDef;
847 }
848 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
849 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000850
851 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000852 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000853 for (Record *WR : ProcModel.WriteResDefs) {
854 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000855 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000856 if (AliasDef == WR->getValueAsDef("WriteType")
857 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000858 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000859 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000860 "SchedWrite and its alias on processor " +
861 ProcModel.ModelName);
862 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000863 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000864 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000865 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000866 // TODO: If ProcModel has a base model (previous generation processor),
867 // then call FindWriteResources recursively with that model here.
868 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000869 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000870 Twine("Processor does not define resources for ") +
871 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000872 }
873 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000874}
875
876/// Find the ReadAdvance record for the given SchedRead on this processor or
877/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000878Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000879 const CodeGenProcModel &ProcModel) {
880 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000881 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
882 return SchedRead.TheDef;
883
884 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000885 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000886 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000887 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000888 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000889 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
890 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
891 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
892 continue;
893 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000894 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000895 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000896 "defined for processor " + ProcModel.ModelName +
897 " Ensure only one SchedAlias exists per RW.");
898 AliasDef = AliasRW.TheDef;
899 }
900 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
901 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000902
903 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000904 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000905 for (Record *RA : ProcModel.ReadAdvanceDefs) {
906 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000907 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000908 if (AliasDef == RA->getValueAsDef("ReadType")
909 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000910 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000911 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000912 "SchedRead and its alias on processor " +
913 ProcModel.ModelName);
914 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000915 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000916 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000917 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000918 // TODO: If ProcModel has a base model (previous generation processor),
919 // then call FindReadAdvance recursively with that model here.
920 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000921 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000922 Twine("Processor does not define resources for ") +
923 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000924 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000925 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000926}
927
Andrew Trick4e67cba2013-03-14 21:21:50 +0000928// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000929// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000930void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
931 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000932 const CodeGenProcModel &PM) {
Clement Courbet5eeed772018-06-13 09:41:49 +0000933 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000934 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000935 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000936 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000937 if (PRDef->isSubClassOf("ProcResGroup"))
938 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000939 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000940 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000941 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000942 for (Record *SubDef = PRDef;
943 SubDef->getValueInit("Super")->isComplete();) {
944 if (SubDef->isSubClassOf("ProcResGroup")) {
945 // Disallow this for simplicitly.
946 PrintFatalError(SubDef->getLoc(), "Processor resource group "
947 " cannot be a super resources.");
948 }
949 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000950 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
951 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000952 PRVec.push_back(SuperDef);
953 Cycles.push_back(Cycles[i]);
954 SubDef = SuperDef;
955 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000956 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000957 for (Record *PR : PM.ProcResourceDefs) {
958 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000959 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000960 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000961 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000962 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000963 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000964 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000965 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000966 }
967 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000968 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000969 Cycles.push_back(Cycles[i]);
970 }
971 }
972 }
973}
974
Andrew Trick9ef08822012-09-17 22:18:48 +0000975// Generate the SchedClass table for this processor and update global
976// tables. Must be called for each processor in order.
977void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
978 SchedClassTables &SchedTables) {
979 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
980 if (!ProcModel.hasInstrSchedModel())
981 return;
982
983 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000984 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000985 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000986 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000987
Andrew Trick9ef08822012-09-17 22:18:48 +0000988 SCTab.resize(SCTab.size() + 1);
989 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000990 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000991 SCDesc.NumMicroOps = 0;
992 SCDesc.BeginGroup = false;
993 SCDesc.EndGroup = false;
994 SCDesc.WriteProcResIdx = 0;
995 SCDesc.WriteLatencyIdx = 0;
996 SCDesc.ReadAdvanceIdx = 0;
997
998 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000999 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +00001000 for (const CodeGenSchedTransition &CGT :
1001 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1002 if (CGT.ProcIndices[0] == 0 ||
1003 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001004 HasVariants = true;
1005 break;
1006 }
1007 }
1008 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001009 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1010 continue;
1011 }
1012
1013 // Determine if the SchedClass is actually reachable on this processor. If
1014 // not don't try to locate the processor resources, it will fail.
1015 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001016 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1017 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +00001018 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001019 continue;
1020 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001021 IdxVec Writes = SC.Writes;
1022 IdxVec Reads = SC.Reads;
1023 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001024 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001025 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001026 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001027 for (Record *RW : SC.InstRWs) {
1028 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001029 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001030 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001031 break;
1032 }
1033 }
1034 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001035 Writes.clear();
1036 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001037 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1038 Writes, Reads);
1039 }
1040 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001041 if (Writes.empty()) {
1042 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001043 for (Record *I : ProcModel.ItinRWDefs) {
1044 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001045 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001046 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001047 Writes, Reads);
1048 break;
1049 }
1050 }
1051 if (Writes.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001052 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1053 << " does not have resources for class " << SC.Name
1054 << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001055 }
1056 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001057 // Sum resources across all operand writes.
1058 std::vector<MCWriteProcResEntry> WriteProcResources;
1059 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001060 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001061 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001062 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001063 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001064 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001065 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001066
1067 // For each operand, create a latency entry.
1068 MCWriteLatencyEntry WLEntry;
1069 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001070 unsigned WriteID = WriteSeq.back();
1071 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1072 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1073 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001074 if (!SchedModels.hasReadOfWrite(
1075 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001076 WriteID = 0;
1077 }
1078 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001079
Craig Topper29c55dcb2016-02-13 06:03:32 +00001080 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001081
Andrew Trick9257b8f2012-09-22 02:24:21 +00001082 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001083 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001084
1085 // Mark the parent class as invalid for unsupported write types.
1086 if (WriteRes->getValueAsBit("Unsupported")) {
1087 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1088 break;
1089 }
1090 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1091 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1092 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1093 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001094 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1095 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001096
1097 // Create an entry for each ProcResource listed in WriteRes.
1098 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1099 std::vector<int64_t> Cycles =
1100 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001101
Clement Courbet5eeed772018-06-13 09:41:49 +00001102 if (Cycles.empty()) {
1103 // If ResourceCycles is not provided, default to one cycle per
1104 // resource.
1105 Cycles.resize(PRVec.size(), 1);
1106 } else if (Cycles.size() != PRVec.size()) {
1107 // If ResourceCycles is provided, check consistency.
1108 PrintFatalError(
1109 WriteRes->getLoc(),
1110 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1111 "!size(ProcResources): ")
1112 .concat(Twine(PRVec.size()))
1113 .concat(" vs ")
1114 .concat(Twine(Cycles.size())));
1115 }
1116
Andrew Trick4e67cba2013-03-14 21:21:50 +00001117 ExpandProcResources(PRVec, Cycles, ProcModel);
1118
Andrew Trick9ef08822012-09-17 22:18:48 +00001119 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1120 PRIdx != PREnd; ++PRIdx) {
1121 MCWriteProcResEntry WPREntry;
1122 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1123 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001124 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001125 // If this resource is already used in this sequence, add the current
1126 // entry's cycles so that the same resource appears to be used
1127 // serially, rather than multiple parallel uses. This is important for
1128 // in-order machine where the resource consumption is a hazard.
1129 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1130 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1131 if (WriteProcResources[WPRIdx].ProcResourceIdx
1132 == WPREntry.ProcResourceIdx) {
1133 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1134 break;
1135 }
1136 }
1137 if (WPRIdx == WPREnd)
1138 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001139 }
1140 }
1141 WriteLatencies.push_back(WLEntry);
1142 }
1143 // Create an entry for each operand Read in this SchedClass.
1144 // Entries must be sorted first by UseIdx then by WriteResourceID.
1145 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1146 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001147 Record *ReadAdvance =
1148 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001149 if (!ReadAdvance)
1150 continue;
1151
1152 // Mark the parent class as invalid for unsupported write types.
1153 if (ReadAdvance->getValueAsBit("Unsupported")) {
1154 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1155 break;
1156 }
1157 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1158 IdxVec WriteIDs;
1159 if (ValidWrites.empty())
1160 WriteIDs.push_back(0);
1161 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001162 for (Record *VW : ValidWrites) {
1163 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001164 }
1165 }
Fangrui Song0cac7262018-09-27 02:13:45 +00001166 llvm::sort(WriteIDs);
Craig Topper29c55dcb2016-02-13 06:03:32 +00001167 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001168 MCReadAdvanceEntry RAEntry;
1169 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001170 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001171 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1172 ReadAdvanceEntries.push_back(RAEntry);
1173 }
1174 }
1175 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1176 WriteProcResources.clear();
1177 WriteLatencies.clear();
1178 ReadAdvanceEntries.clear();
1179 }
1180 // Add the information for this SchedClass to the global tables using basic
1181 // compression.
1182 //
1183 // WritePrecRes entries are sorted by ProcResIdx.
Fangrui Song0cac7262018-09-27 02:13:45 +00001184 llvm::sort(WriteProcResources, LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001185
1186 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1187 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1188 std::search(SchedTables.WriteProcResources.begin(),
1189 SchedTables.WriteProcResources.end(),
1190 WriteProcResources.begin(), WriteProcResources.end());
1191 if (WPRPos != SchedTables.WriteProcResources.end())
1192 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1193 else {
1194 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1195 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1196 WriteProcResources.end());
1197 }
1198 // Latency entries must remain in operand order.
1199 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1200 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1201 std::search(SchedTables.WriteLatencies.begin(),
1202 SchedTables.WriteLatencies.end(),
1203 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001204 if (WLPos != SchedTables.WriteLatencies.end()) {
1205 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1206 SCDesc.WriteLatencyIdx = idx;
1207 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1208 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1209 std::string::npos) {
1210 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1211 }
1212 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001213 else {
1214 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001215 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1216 WriteLatencies.begin(),
1217 WriteLatencies.end());
1218 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1219 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001220 }
1221 // ReadAdvanceEntries must remain in operand order.
1222 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1223 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1224 std::search(SchedTables.ReadAdvanceEntries.begin(),
1225 SchedTables.ReadAdvanceEntries.end(),
1226 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1227 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1228 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1229 else {
1230 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1231 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1232 ReadAdvanceEntries.end());
1233 }
1234 }
1235}
1236
Andrew Tricka72fca62012-09-17 22:18:50 +00001237// Emit SchedClass tables for all processors and associated global tables.
1238void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1239 raw_ostream &OS) {
1240 // Emit global WriteProcResTable.
1241 OS << "\n// {ProcResourceIdx, Cycles}\n"
1242 << "extern const llvm::MCWriteProcResEntry "
1243 << Target << "WriteProcResTable[] = {\n"
1244 << " { 0, 0}, // Invalid\n";
1245 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1246 WPRIdx != WPREnd; ++WPRIdx) {
1247 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1248 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1249 << format("%2d", WPREntry.Cycles) << "}";
1250 if (WPRIdx + 1 < WPREnd)
1251 OS << ',';
1252 OS << " // #" << WPRIdx << '\n';
1253 }
1254 OS << "}; // " << Target << "WriteProcResTable\n";
1255
1256 // Emit global WriteLatencyTable.
1257 OS << "\n// {Cycles, WriteResourceID}\n"
1258 << "extern const llvm::MCWriteLatencyEntry "
1259 << Target << "WriteLatencyTable[] = {\n"
1260 << " { 0, 0}, // Invalid\n";
1261 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1262 WLIdx != WLEnd; ++WLIdx) {
1263 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1264 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1265 << format("%2d", WLEntry.WriteResourceID) << "}";
1266 if (WLIdx + 1 < WLEnd)
1267 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001268 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001269 }
1270 OS << "}; // " << Target << "WriteLatencyTable\n";
1271
1272 // Emit global ReadAdvanceTable.
1273 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1274 << "extern const llvm::MCReadAdvanceEntry "
1275 << Target << "ReadAdvanceTable[] = {\n"
1276 << " {0, 0, 0}, // Invalid\n";
1277 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1278 RAIdx != RAEnd; ++RAIdx) {
1279 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1280 OS << " {" << RAEntry.UseIdx << ", "
1281 << format("%2d", RAEntry.WriteResourceID) << ", "
1282 << format("%2d", RAEntry.Cycles) << "}";
1283 if (RAIdx + 1 < RAEnd)
1284 OS << ',';
1285 OS << " // #" << RAIdx << '\n';
1286 }
1287 OS << "}; // " << Target << "ReadAdvanceTable\n";
1288
1289 // Emit a SchedClass table for each processor.
1290 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1291 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1292 if (!PI->hasInstrSchedModel())
1293 continue;
1294
1295 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001296 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001297
1298 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1299 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1300 OS << "static const llvm::MCSchedClassDesc "
1301 << PI->ModelName << "SchedClasses[] = {\n";
1302
1303 // The first class is always invalid. We no way to distinguish it except by
1304 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001305 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001306 && "invalid class not first");
1307 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1308 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001309 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001310
1311 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1312 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1313 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1314 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1315 if (SchedClass.Name.size() < 18)
1316 OS.indent(18 - SchedClass.Name.size());
1317 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001318 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1319 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001320 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1321 << ", " << MCDesc.NumWriteProcResEntries
1322 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1323 << ", " << MCDesc.NumWriteLatencyEntries
1324 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001325 << ", " << MCDesc.NumReadAdvanceEntries
1326 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001327 }
1328 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1329 }
1330}
1331
Andrew Trick87255e32012-07-07 04:00:00 +00001332void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1333 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001334 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001335 // Emit extra processor info if available.
1336 if (PM.hasExtraProcessorInfo())
1337 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001338 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001339 if (PM.hasInstrSchedModel())
1340 EmitProcessorResources(PM, OS);
1341 else if(!PM.ProcResourceDefs.empty())
1342 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001343 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001344
Andrew Trick73d77362012-06-05 03:44:40 +00001345 // Begin processor itinerary properties
1346 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001347 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1348 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1349 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1350 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1351 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1352 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1353 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001354
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001355 bool PostRAScheduler =
1356 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001357
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001358 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1359 << "PostRAScheduler\n";
1360
1361 bool CompleteModel =
1362 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1363
1364 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1365 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001366
Craig Topper29c55dcb2016-02-13 06:03:32 +00001367 OS << " " << PM.Index << ", // Processor ID\n";
1368 if (PM.hasInstrSchedModel())
1369 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1370 << " " << PM.ModelName << "SchedClasses" << ",\n"
1371 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001372 << " " << (SchedModels.schedClassEnd()
1373 - SchedModels.schedClassBegin()) << ",\n";
1374 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001375 OS << " nullptr, nullptr, 0, 0,"
1376 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001377 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001378 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001379 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001380 OS << " nullptr, // No Itinerary\n";
1381 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001382 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001383 else
Clement Courbetb4493792018-04-10 08:16:37 +00001384 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001385 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001386 }
Jim Laskey3763a502005-10-31 17:16:01 +00001387}
1388
1389//
Clement Courbet41c8af32018-10-25 07:44:01 +00001390// EmitProcessorLookup - generate cpu name to sched model lookup tables.
Jim Laskey3763a502005-10-31 17:16:01 +00001391//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001392void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001393 // Gather and sort processor information
1394 std::vector<Record*> ProcessorList =
1395 Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +00001396 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001397
Clement Courbet41c8af32018-10-25 07:44:01 +00001398 // Begin processor->sched model table
Jim Laskey3763a502005-10-31 17:16:01 +00001399 OS << "\n";
Clement Courbet41c8af32018-10-25 07:44:01 +00001400 OS << "// Sorted (by key) array of sched model for CPU subtype.\n"
1401 << "extern const llvm::SubtargetInfoKV " << Target
1402 << "ProcSchedKV[] = {\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001403 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001404 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001405 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001406 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001407 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001408
Jim Laskey3763a502005-10-31 17:16:01 +00001409 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001410 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001411 }
Clement Courbet41c8af32018-10-25 07:44:01 +00001412 // End processor->sched model table
Jim Laskey3763a502005-10-31 17:16:01 +00001413 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001414}
1415
1416//
Andrew Trick87255e32012-07-07 04:00:00 +00001417// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001418//
Andrew Trick87255e32012-07-07 04:00:00 +00001419void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001420 OS << "#ifdef DBGFIELD\n"
1421 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1422 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001423 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001424 << "#define DBGFIELD(x) x,\n"
1425 << "#else\n"
1426 << "#define DBGFIELD(x)\n"
1427 << "#endif\n";
1428
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001429 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001430 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001431 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001432 EmitStageAndOperandCycleData(OS, ProcItinLists);
1433 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001434 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001435 OS << "\n// ===============================================================\n"
1436 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001437
Andrew Trick9ef08822012-09-17 22:18:48 +00001438 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001439 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1440 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001441 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001442 EmitSchedClassTables(SchedTables, OS);
1443
1444 // Emit the processor machine model
1445 EmitProcessorModels(OS);
1446 // Emit the processor lookup data
1447 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001448
Craig Topper194cb742017-10-24 15:50:55 +00001449 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001450}
1451
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001452static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1453 std::string Buffer;
1454 raw_string_ostream Stream(Buffer);
1455
1456 // Collect all the PredicateProlog records and print them to the output
1457 // stream.
1458 std::vector<Record *> Prologs =
1459 Records.getAllDerivedDefinitions("PredicateProlog");
Fangrui Song0cac7262018-09-27 02:13:45 +00001460 llvm::sort(Prologs, LessRecord());
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001461 for (Record *P : Prologs)
1462 Stream << P->getValueAsString("Code") << '\n';
1463
1464 Stream.flush();
1465 OS << Buffer;
1466}
1467
1468static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001469 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001470 raw_ostream &OS) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001471 std::string Buffer;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001472 raw_string_ostream SS(Buffer);
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001473
1474 auto IsTruePredicate = [](const Record *Rec) {
1475 return Rec->isSubClassOf("MCSchedPredicate") &&
1476 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1477 };
1478
1479 // If not all predicates are MCTrue, then we need an if-stmt.
1480 unsigned NumNonTruePreds =
1481 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001482
1483 SS.indent(PE.getIndentLevel() * 2);
1484
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001485 if (NumNonTruePreds) {
1486 bool FirstNonTruePredicate = true;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001487 SS << "if (";
1488
1489 PE.setIndentLevel(PE.getIndentLevel() + 2);
1490
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001491 for (const Record *Rec : T.PredTerm) {
1492 // Skip predicates that evaluate to "true".
1493 if (IsTruePredicate(Rec))
1494 continue;
1495
1496 if (FirstNonTruePredicate) {
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001497 FirstNonTruePredicate = false;
1498 } else {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001499 SS << "\n";
1500 SS.indent(PE.getIndentLevel() * 2);
1501 SS << "&& ";
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001502 }
1503
1504 if (Rec->isSubClassOf("MCSchedPredicate")) {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001505 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001506 continue;
1507 }
1508
1509 // Expand this legacy predicate and wrap it around braces if there is more
1510 // than one predicate to expand.
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001511 SS << ((NumNonTruePreds > 1) ? "(" : "")
1512 << Rec->getValueAsString("Predicate")
1513 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagio95140022018-05-25 15:55:37 +00001514 }
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001515
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001516 SS << ")\n"; // end of if-stmt
1517 PE.decreaseIndentLevel();
1518 SS.indent(PE.getIndentLevel() * 2);
1519 PE.decreaseIndentLevel();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001520 }
1521
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001522 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1523 SS.flush();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001524 OS << Buffer;
1525}
1526
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001527// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1528// epilogue code for the auto-generated helper.
1529void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1530 if (ShouldReturnZero) {
Andrea Di Biagio95140022018-05-25 15:55:37 +00001531 OS << " // Don't know how to resolve this scheduling class.\n"
1532 << " return 0;\n";
1533 return;
1534 }
1535
1536 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1537}
1538
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001539bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1540 return all_of(T.PredTerm, [](const Record *Rec) {
1541 return Rec->isSubClassOf("MCSchedPredicate");
1542 });
1543}
1544
1545void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1546 IdxVec &VariantClasses,
1547 bool OnlyExpandMCInstPredicates) {
1548 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1549 // Ignore non-variant scheduling classes.
1550 if (SC.Transitions.empty())
1551 continue;
1552
1553 if (OnlyExpandMCInstPredicates) {
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001554 // Ignore this variant scheduling class no transitions use any meaningful
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001555 // MCSchedPredicate definitions.
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001556 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001557 return hasMCSchedPredicates(T);
1558 }))
1559 continue;
1560 }
1561
1562 VariantClasses.push_back(SC.Index);
1563 }
1564}
1565
1566void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1567 // A variant scheduling class may define transitions for multiple
1568 // processors. This function identifies wich processors are associated with
1569 // transition rules specified by variant class `SC`.
1570 for (const CodeGenSchedTransition &T : SC.Transitions) {
1571 IdxVec PI;
1572 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1573 ProcIndices.begin(), ProcIndices.end(),
1574 std::back_inserter(PI));
1575 ProcIndices.swap(PI);
1576 }
1577}
1578
1579void SubtargetEmitter::emitSchedModelHelpersImpl(
1580 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1581 IdxVec VariantClasses;
1582 collectVariantClasses(SchedModels, VariantClasses,
1583 OnlyExpandMCInstPredicates);
1584
1585 if (VariantClasses.empty()) {
1586 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1587 return;
1588 }
1589
1590 // Construct a switch statement where the condition is a check on the
1591 // scheduling class identifier. There is a `case` for every variant class
1592 // defined by the processor models of this target.
1593 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1594 // a variant scheduling class to another scheduling class. Rules are
1595 // described by instances of CodeGenSchedTransition. Note that transitions may
1596 // not be valid for all processors.
1597 OS << " switch (SchedClass) {\n";
1598 for (unsigned VC : VariantClasses) {
1599 IdxVec ProcIndices;
1600 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1601 collectProcessorIndices(SC, ProcIndices);
1602
1603 OS << " case " << VC << ": // " << SC.Name << '\n';
1604
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +00001605 PredicateExpander PE(Target);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001606 PE.setByRef(false);
1607 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1608 for (unsigned PI : ProcIndices) {
1609 OS << " ";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001610
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001611 // Emit a guard on the processor ID.
1612 if (PI != 0) {
1613 OS << (OnlyExpandMCInstPredicates
1614 ? "if (CPUID == "
1615 : "if (SchedModel->getProcessorID() == ");
1616 OS << PI << ") ";
1617 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1618 }
1619
1620 // Now emit transitions associated with processor PI.
1621 for (const CodeGenSchedTransition &T : SC.Transitions) {
1622 if (PI != 0 && !count(T.ProcIndices, PI))
1623 continue;
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001624
1625 // Emit only transitions based on MCSchedPredicate, if it's the case.
1626 // At least the transition specified by NoSchedPred is emitted,
1627 // which becomes the default transition for those variants otherwise
1628 // not based on MCSchedPredicate.
1629 // FIXME: preferably, llvm-mca should instead assume a reasonable
1630 // default when a variant transition is not based on MCSchedPredicate
1631 // for a given processor.
1632 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1633 continue;
1634
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001635 PE.setIndentLevel(3);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001636 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1637 }
1638
1639 OS << " }\n";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001640
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001641 if (PI == 0)
1642 break;
1643 }
1644
1645 if (SC.isInferred())
1646 OS << " return " << SC.Index << ";\n";
1647 OS << " break;\n";
1648 }
1649
1650 OS << " };\n";
1651
1652 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1653}
1654
Andrea Di Biagio95140022018-05-25 15:55:37 +00001655void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1656 raw_ostream &OS) {
1657 OS << "unsigned " << ClassName
1658 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1659 << " const TargetSchedModel *SchedModel) const {\n";
1660
1661 // Emit the predicate prolog code.
1662 emitPredicateProlog(Records, OS);
1663
1664 // Emit target predicates.
1665 emitSchedModelHelpersImpl(OS);
Clement Courbet41c8af32018-10-25 07:44:01 +00001666
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001667 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagio95140022018-05-25 15:55:37 +00001668
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001669 OS << "unsigned " << ClassName
1670 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1671 << " unsigned CPUID) const {\n"
1672 << " return " << Target << "_MC"
1673 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001674 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1675
1676 STIPredicateExpander PE(Target);
1677 PE.setClassPrefix(ClassName);
1678 PE.setExpandDefinition(true);
1679 PE.setByRef(false);
1680 PE.setIndentLevel(0);
1681
1682 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1683 PE.expandSTIPredicate(OS, Fn);
Andrew Trickc6c88152012-09-18 03:41:43 +00001684}
1685
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001686void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1687 raw_ostream &OS) {
1688 const CodeGenHwModes &CGH = TGT.getHwModes();
1689 assert(CGH.getNumModeIds() > 0);
1690 if (CGH.getNumModeIds() == 1)
1691 return;
1692
1693 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1694 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1695 const HwMode &HM = CGH.getMode(M);
1696 OS << " if (checkFeatures(\"" << HM.Features
1697 << "\")) return " << M << ";\n";
1698 }
1699 OS << " return 0;\n}\n";
1700}
1701
Jim Laskey86f002c2005-10-27 19:47:21 +00001702//
Jim Laskeya2b52352005-10-26 17:30:34 +00001703// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1704// the subtarget features string.
1705//
Evan Cheng54b68e32011-07-01 20:45:01 +00001706void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1707 unsigned NumFeatures,
1708 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001709 std::vector<Record*> Features =
1710 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +00001711 llvm::sort(Features, LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001712
Andrew Trickdb6ed642011-04-01 01:56:55 +00001713 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1714 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001715 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001716 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001717 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001718 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1719 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001720
1721 if (Features.empty()) {
1722 OS << "}\n";
1723 return;
1724 }
1725
Andrew Trickba7b9212012-09-18 05:33:15 +00001726 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001727 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001728
Craig Topper29c55dcb2016-02-13 06:03:32 +00001729 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001730 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001731 StringRef Instance = R->getName();
1732 StringRef Value = R->getValueAsString("Value");
1733 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001734
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001735 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001736 OS << " if (Bits[" << Target << "::"
1737 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001738 << Attribute << " = " << Value << ";\n";
1739 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001740 OS << " if (Bits[" << Target << "::"
1741 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001742 << Attribute << " < " << Value << ") "
1743 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001744 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001745
Evan Chengfe6e4052011-06-30 01:53:36 +00001746 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001747}
1748
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001749void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001750 OS << "namespace " << Target << "_MC {\n"
1751 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1752 << " const MCInst *MI, unsigned CPUID) {\n";
1753 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1754 OS << "}\n";
1755 OS << "} // end of namespace " << Target << "_MC\n\n";
1756
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001757 OS << "struct " << Target
1758 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1759 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1760 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
Craig Topperca268082019-03-05 18:54:34 +00001761 << " ArrayRef<SubtargetSubTypeKV> PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001762 << " const SubtargetInfoKV *ProcSched,\n"
1763 << " const MCWriteProcResEntry *WPR,\n"
1764 << " const MCWriteLatencyEntry *WL,\n"
1765 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1766 << " const unsigned *OC, const unsigned *FP) :\n"
1767 << " MCSubtargetInfo(TT, CPU, FS, PF, PD, ProcSched,\n"
1768 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1769 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001770 << " const MCInst *MI, unsigned CPUID) const override {\n"
1771 << " return " << Target << "_MC"
1772 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001773 OS << " }\n";
1774 OS << "};\n";
1775}
1776
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001777void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1778 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1779 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1780
1781 STIPredicateExpander PE(Target);
1782 PE.setExpandForMC(true);
1783 PE.setByRef(true);
1784 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1785 PE.expandSTIPredicate(OS, Fn);
1786
1787 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1788
1789 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1790 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1791
1792 std::string ClassPrefix = Target + "MCInstrAnalysis";
1793 PE.setExpandDefinition(true);
1794 PE.setClassPrefix(ClassPrefix);
1795 PE.setIndentLevel(0);
1796 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1797 PE.expandSTIPredicate(OS, Fn);
1798
1799 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1800}
1801
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001802//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001803// SubtargetEmitter::run - Main subtarget enumeration emitter.
1804//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001805void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001806 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001807
Evan Cheng4d1ca962011-07-08 01:53:10 +00001808 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001809 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001810
Craig Topper4f613082019-03-01 02:19:26 +00001811 DenseMap<Record *, unsigned> FeatureMap;
1812
Evan Cheng4d1ca962011-07-08 01:53:10 +00001813 OS << "namespace llvm {\n";
Craig Topper4f613082019-03-01 02:19:26 +00001814 Enumeration(OS, FeatureMap);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001815 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001816 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1817
Evan Cheng54b68e32011-07-01 20:45:01 +00001818 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001819 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001820
Evan Cheng54b68e32011-07-01 20:45:01 +00001821 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001822#if 0
1823 OS << "namespace {\n";
1824#endif
Craig Topper4f613082019-03-01 02:19:26 +00001825 unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap);
Evan Chengbc153d42011-07-14 20:59:42 +00001826 OS << "\n";
Craig Topper4f613082019-03-01 02:19:26 +00001827 unsigned NumProcs = CPUKeyValues(OS, FeatureMap);
Evan Chengbc153d42011-07-14 20:59:42 +00001828 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001829 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001830 OS << "\n";
1831#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001832 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001833#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001834
1835 // MCInstrInfo initialization routine.
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001836 emitGenMCSubtargetInfo(OS);
1837
Craig Topper194cb742017-10-24 15:50:55 +00001838 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001839 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001840 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001841 OS << " return new " << Target << "GenMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001842 if (NumFeatures)
1843 OS << Target << "FeatureKV, ";
1844 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001845 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001846 if (NumProcs)
1847 OS << Target << "SubTypeKV, ";
1848 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001849 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001850 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001851 OS << Target << "ProcSchedKV, "
1852 << Target << "WriteProcResTable, "
1853 << Target << "WriteLatencyTable, "
1854 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001855 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001856 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001857 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001858 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001859 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001860 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001861 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001862 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001863
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001864 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001865
1866 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1867
1868 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001869 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001870
1871 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001872 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001873 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1874
1875 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1876
Evan Cheng0d639a22011-07-01 21:01:15 +00001877 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001878 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001879 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001880
1881 std::string ClassName = Target + "GenSubtargetInfo";
1882 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001883 OS << "class DFAPacketizer;\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001884 OS << "namespace " << Target << "_MC {\n"
1885 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1886 << " const MCInst *MI, unsigned CPUID);\n"
1887 << "}\n\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001888 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001889 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001890 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001891 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001892 << " unsigned resolveSchedClass(unsigned SchedClass, "
1893 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001894 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001895 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1896 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001897 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001898 << " const;\n";
1899 if (TGT.getHwModes().getNumModeIds() > 1)
1900 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001901
1902 STIPredicateExpander PE(Target);
1903 PE.setByRef(false);
1904 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1905 PE.expandSTIPredicate(OS, Fn);
1906
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001907 OS << "};\n"
1908 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001909
1910 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1911
1912 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001913 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001914
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001915 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001916 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001917 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
Craig Topperca268082019-03-05 18:54:34 +00001918 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001919 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1920 OS << "extern const llvm::MCWriteProcResEntry "
1921 << Target << "WriteProcResTable[];\n";
1922 OS << "extern const llvm::MCWriteLatencyEntry "
1923 << Target << "WriteLatencyTable[];\n";
1924 OS << "extern const llvm::MCReadAdvanceEntry "
1925 << Target << "ReadAdvanceTable[];\n";
1926
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001927 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001928 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1929 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001930 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001931 }
1932
Daniel Sanders50f17232015-09-15 16:17:27 +00001933 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1934 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001935 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001936 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001937 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001938 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001939 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001940 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001941 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001942 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001943 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001944 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001945 OS << Target << "ProcSchedKV, "
1946 << Target << "WriteProcResTable, "
1947 << Target << "WriteLatencyTable, "
1948 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001949 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001950 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001951 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001952 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001953 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001954 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001955 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001956 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001957
Andrew Trickc6c88152012-09-18 03:41:43 +00001958 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001959 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001960
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001961 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001962
1963 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001964
1965 EmitMCInstrAnalysisPredicateFunctions(OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001966}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001967
1968namespace llvm {
1969
1970void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001971 CodeGenTarget CGTarget(RK);
1972 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001973}
1974
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001975} // end namespace llvm