blob: 68ee839c43bac0e0e47173a9f50edaa4358f3f59 [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)
Benjamin Krameradcd0262020-01-28 20:23:46 +0100131 : 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;
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000152 if (N + 1 > MAX_SUBTARGET_FEATURES)
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000153 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
Thomas Lively3f34e1b82019-03-29 00:14:01 +0000172 OS << " "
173 << "NumSubtargetFeatures = " << N << "\n";
174
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000175 // Close enumeration and namespace
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000176 OS << "};\n";
177 OS << "} // end namespace " << Target << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000178}
179
Craig Topper4f613082019-03-01 02:19:26 +0000180static void printFeatureMask(raw_ostream &OS, RecVec &FeatureList,
181 const DenseMap<Record *, unsigned> &FeatureMap) {
182 std::array<uint64_t, MAX_SUBTARGET_WORDS> Mask = {};
183 for (unsigned j = 0, M = FeatureList.size(); j < M; ++j) {
184 unsigned Bit = FeatureMap.lookup(FeatureList[j]);
185 Mask[Bit / 64] |= 1ULL << (Bit % 64);
186 }
187
Craig Topper2729a912019-03-04 19:23:37 +0000188 OS << "{ { { ";
Craig Topper4f613082019-03-01 02:19:26 +0000189 for (unsigned i = 0; i != Mask.size(); ++i) {
190 OS << "0x";
191 OS.write_hex(Mask[i]);
192 OS << "ULL, ";
193 }
Craig Topper2729a912019-03-04 19:23:37 +0000194 OS << "} } }";
Craig Topper4f613082019-03-01 02:19:26 +0000195}
196
Jim Laskey1b7369b2005-10-25 15:16:36 +0000197//
Bill Wendlinge6182262007-05-04 20:38:40 +0000198// FeatureKeyValues - Emit data of all the subtarget features. Used by the
199// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000200//
Craig Topper4f613082019-03-01 02:19:26 +0000201unsigned SubtargetEmitter::FeatureKeyValues(
202 raw_ostream &OS, const DenseMap<Record *, unsigned> &FeatureMap) {
Jim Laskey19595752005-10-28 15:20:43 +0000203 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000204 std::vector<Record*> FeatureList =
205 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000206
207 if (FeatureList.empty())
208 return 0;
209
Fangrui Song0cac7262018-09-27 02:13:45 +0000210 llvm::sort(FeatureList, LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000211
Jim Laskey19595752005-10-28 15:20:43 +0000212 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000213 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000214 << "extern const llvm::SubtargetFeatureKV " << Target
215 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000216
Jim Laskey19595752005-10-28 15:20:43 +0000217 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000218 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000219 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000220 // Next feature
221 Record *Feature = FeatureList[i];
222
Craig Topperbcd3c372017-05-31 21:12:46 +0000223 StringRef Name = Feature->getName();
224 StringRef CommandLineName = Feature->getValueAsString("Name");
225 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000226
Jim Laskey3f7d0472006-12-12 20:55:58 +0000227 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000228
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000229 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000230 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000231 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000232 << "\"" << Desc << "\", "
Craig Topper4cf59aa2019-02-18 06:46:17 +0000233 << Target << "::" << Name << ", ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000234
Craig Topper37eeb322018-03-23 00:02:45 +0000235 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000236
Craig Topper4f613082019-03-01 02:19:26 +0000237 printFeatureMask(OS, ImpliesList, FeatureMap);
238
239 OS << " },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000240 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000241 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000242
Jim Laskey19595752005-10-28 15:20:43 +0000243 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000244 OS << "};\n";
245
Evan Cheng54b68e32011-07-01 20:45:01 +0000246 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000247}
248
249//
250// CPUKeyValues - Emit data of all the subtarget processors. Used by command
251// line.
252//
Craig Topper4f613082019-03-01 02:19:26 +0000253unsigned
254SubtargetEmitter::CPUKeyValues(raw_ostream &OS,
255 const DenseMap<Record *, unsigned> &FeatureMap) {
Jim Laskey19595752005-10-28 15:20:43 +0000256 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000257 std::vector<Record*> ProcessorList =
258 Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +0000259 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000260
Jim Laskey19595752005-10-28 15:20:43 +0000261 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000262 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Craig Topperca268082019-03-05 18:54:34 +0000263 << "extern const llvm::SubtargetSubTypeKV " << Target
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000264 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000265
Jim Laskey19595752005-10-28 15:20:43 +0000266 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000267 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000268 StringRef Name = Processor->getValueAsString("Name");
Craig Topper37eeb322018-03-23 00:02:45 +0000269 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000270
Craig Topper4cf59aa2019-02-18 06:46:17 +0000271 // Emit as { "cpu", "description", 0, { f1 , f2 , ... fn } },
Craig Topper4cf59aa2019-02-18 06:46:17 +0000272 OS << " { "
Craig Topperca268082019-03-05 18:54:34 +0000273 << "\"" << Name << "\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000274
Craig Topper4f613082019-03-01 02:19:26 +0000275 printFeatureMask(OS, FeatureList, FeatureMap);
276
Craig Topper2982b842019-03-05 18:54:38 +0000277 // Emit the scheduler model pointer.
278 const std::string &ProcModelName =
279 SchedModels.getModelForProc(Processor).ModelName;
280 OS << ", &" << ProcModelName << " },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000281 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000282
Jim Laskey19595752005-10-28 15:20:43 +0000283 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000284 OS << "};\n";
285
Evan Cheng54b68e32011-07-01 20:45:01 +0000286 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000287}
Jim Laskeya1beea62005-10-22 07:59:56 +0000288
Jim Laskeya2b52352005-10-26 17:30:34 +0000289//
David Goodwind813cbf2009-08-17 16:02:57 +0000290// FormItineraryStageString - Compose a string containing the stage
291// data initialization for the specified itinerary. N is the number
292// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000293//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000294void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
295 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000296 std::string &ItinString,
297 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000298 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000299 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000300
301 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000302 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000303 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000304 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000305 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000306
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000307 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000308 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000309 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000310
Jim Laskeydffe5972005-10-28 21:47:29 +0000311 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000312 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000313
Jim Laskey19595752005-10-28 15:20:43 +0000314 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000315 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000316 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000317 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000318 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000319 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000320
David Goodwinb369ee42009-08-12 18:31:53 +0000321 int TimeInc = Stage->getValueAsInt("TimeInc");
322 ItinString += ", " + itostr(TimeInc);
323
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000324 int Kind = Stage->getValueAsInt("Kind");
325 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
326
Jim Laskey19595752005-10-28 15:20:43 +0000327 // Close off stage
328 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000329 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000330 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000331}
332
333//
David Goodwind813cbf2009-08-17 16:02:57 +0000334// FormItineraryOperandCycleString - Compose a string containing the
335// operand cycle initialization for the specified itinerary. N is the
336// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000337//
David Goodwind813cbf2009-08-17 16:02:57 +0000338void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
339 std::string &ItinString, unsigned &NOperandCycles) {
340 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000341 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000342 ItinData->getValueAsListOfInts("OperandCycles");
343
344 // For each operand cycle
345 unsigned N = NOperandCycles = OperandCycleList.size();
346 for (unsigned i = 0; i < N;) {
347 // Next operand cycle
348 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000349
David Goodwind813cbf2009-08-17 16:02:57 +0000350 ItinString += " " + itostr(OCycle);
351 if (++i < N) ItinString += ", ";
352 }
353}
354
Evan Cheng0097dd02010-09-28 23:50:49 +0000355void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
356 Record *ItinData,
357 std::string &ItinString,
358 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000359 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000360 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000361 unsigned i = 0;
362 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000363 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000364 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000365 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000366 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000367 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000368 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000369 }
370}
371
David Goodwind813cbf2009-08-17 16:02:57 +0000372//
Andrew Trick87255e32012-07-07 04:00:00 +0000373// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
374// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
375// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000376//
Andrew Trick87255e32012-07-07 04:00:00 +0000377void SubtargetEmitter::
378EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000379 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000380 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000381 // Multiple processor models may share an itinerary record. Emit it once.
382 SmallPtrSet<Record*, 8> ItinsDefSet;
383
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000384 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000385 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000386
Craig Topper29c55dcb2016-02-13 06:03:32 +0000387 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000388 continue;
389
Craig Topper37eeb322018-03-23 00:02:45 +0000390 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000391 if (FUs.empty())
392 continue;
393
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000394 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000395 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000396 << "namespace " << Name << "FU {\n";
397
398 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Bevin Hanssonc3f36ac2019-12-09 16:22:57 +0100399 OS << " const InstrStage::FuncUnits " << FUs[j]->getName()
400 << " = 1ULL << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000401
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000402 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000403
Craig Topper37eeb322018-03-23 00:02:45 +0000404 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000405 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000406 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000407 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000408
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000409 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000410 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000411 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000412 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000413
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000414 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000415 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000416 }
417
Jim Laskey19595752005-10-28 15:20:43 +0000418 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000419 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
420 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000421 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000422
David Goodwind813cbf2009-08-17 16:02:57 +0000423 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000424 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000425 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000426 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000427
428 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000429 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000430 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000431 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000432
Andrew Trick87255e32012-07-07 04:00:00 +0000433 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000434 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000435 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000436 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000437 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000438 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000439 // Add process itinerary to the list.
440 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000441
Andrew Trick87255e32012-07-07 04:00:00 +0000442 // If this processor defines no itineraries, then leave the itinerary list
443 // empty.
444 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000445 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000446 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000447
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000448 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000449
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000450 ItinList.resize(SchedModels.numInstrSchedClasses());
451 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
452
453 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000454 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
455
Jim Laskeydffe5972005-10-28 21:47:29 +0000456 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000457 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000458
Jim Laskey19595752005-10-28 15:20:43 +0000459 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000460 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000461 unsigned NStages = 0;
462 if (ItinData)
Benjamin Krameradcd0262020-01-28 20:23:46 +0100463 FormItineraryStageString(std::string(Name), ItinData, ItinStageString,
464 NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000465
David Goodwind813cbf2009-08-17 16:02:57 +0000466 // Get string and operand cycle count
467 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000468 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000469 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000470 if (ItinData) {
471 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
472 NOperandCycles);
473
Benjamin Krameradcd0262020-01-28 20:23:46 +0100474 FormItineraryBypassString(std::string(Name), ItinData, ItinBypassString,
Andrew Trick87255e32012-07-07 04:00:00 +0000475 NOperandCycles);
476 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000477
David Goodwind813cbf2009-08-17 16:02:57 +0000478 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000479 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000480 if (NStages > 0) {
481 FindStage = ItinStageMap[ItinStageString];
482 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000483 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
484 StageTable += ItinStageString + ", // " + itostr(StageCount);
485 if (NStages > 1)
486 StageTable += "-" + itostr(StageCount + NStages - 1);
487 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000488 // Record Itin class number.
489 ItinStageMap[ItinStageString] = FindStage = StageCount;
490 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000491 }
492 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000493
David Goodwind813cbf2009-08-17 16:02:57 +0000494 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000495 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000496 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000497 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
498 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000499 if (FindOperandCycle == 0) {
500 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000501 OperandCycleTable += ItinOperandCycleString + ", // ";
502 std::string OperandIdxComment = itostr(OperandCycleCount);
503 if (NOperandCycles > 1)
504 OperandIdxComment += "-"
505 + itostr(OperandCycleCount + NOperandCycles - 1);
506 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000507 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000508 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000509 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000510 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000511 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000512 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000513 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000514 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000515
Evan Cheng367a5df2010-09-09 18:18:55 +0000516 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000517 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
518 InstrItinerary Intinerary = {
519 NumUOps,
520 FindStage,
521 uint16_t(FindStage + NStages),
522 FindOperandCycle,
523 uint16_t(FindOperandCycle + NOperandCycles),
524 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000525
Jim Laskey19595752005-10-28 15:20:43 +0000526 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000527 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000528 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000529 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000530
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000531 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000532 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000533 StageTable += "};\n";
534
535 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000536 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000537 OperandCycleTable += "};\n";
538
Andrew Trick87255e32012-07-07 04:00:00 +0000539 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000540 BypassTable += "};\n";
541
David Goodwind813cbf2009-08-17 16:02:57 +0000542 // Emit tables.
543 OS << StageTable;
544 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000545 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000546}
547
Andrew Trick87255e32012-07-07 04:00:00 +0000548//
549// EmitProcessorData - Generate data for processor itineraries that were
550// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
551// Itineraries for each processor. The Itinerary lists are indexed on
552// CodeGenSchedClass::Index.
553//
554void SubtargetEmitter::
555EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000556 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000557 // Multiple processor models may share an itinerary record. Emit it once.
558 SmallPtrSet<Record*, 8> ItinsDefSet;
559
Andrew Trick87255e32012-07-07 04:00:00 +0000560 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000561 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000562 ProcItinListsIter = ProcItinLists.begin();
563 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000564 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000565
Andrew Trick87255e32012-07-07 04:00:00 +0000566 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000567 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000568 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000569
Andrew Trick87255e32012-07-07 04:00:00 +0000570 // Get the itinerary list for the processor.
571 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000572 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000573
Pete Cooperc0eb1532014-09-02 23:23:34 +0000574 // Empty itineraries aren't referenced anywhere in the tablegen output
575 // so don't emit them.
576 if (ItinList.empty())
577 continue;
578
Andrew Trick87255e32012-07-07 04:00:00 +0000579 OS << "\n";
580 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000581
582 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000583 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000584
585 // For each itinerary class in CodeGenSchedClass::Index order.
586 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
587 InstrItinerary &Intinerary = ItinList[j];
588
589 // Emit Itinerary in the form of
590 // { firstStage, lastStage, firstCycle, lastCycle } // index
591 OS << " { " <<
592 Intinerary.NumMicroOps << ", " <<
593 Intinerary.FirstStage << ", " <<
594 Intinerary.LastStage << ", " <<
595 Intinerary.FirstOperandCycle << ", " <<
596 Intinerary.LastOperandCycle << " }" <<
597 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
598 }
599 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000600 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
601 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000602 OS << "};\n";
603 }
604}
605
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000606// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000607// value defined in the C++ header. The Record is null if the processor does not
608// define a model.
609void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000610 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000611 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000612 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000613 if (V >= 0)
614 OS << V << Separator << " // " << Name;
615 else
Andrew Trick87255e32012-07-07 04:00:00 +0000616 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000617 OS << '\n';
618}
619
Clement Courbet39911e22018-02-08 08:46:48 +0000620void SubtargetEmitter::EmitProcessorResourceSubUnits(
621 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
622 OS << "\nstatic const unsigned " << ProcModel.ModelName
623 << "ProcResourceSubUnits[] = {\n"
624 << " 0, // Invalid\n";
625
626 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
627 Record *PRDef = ProcModel.ProcResourceDefs[i];
628 if (!PRDef->isSubClassOf("ProcResGroup"))
629 continue;
630 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
631 for (Record *RUDef : ResUnits) {
632 Record *const RU =
633 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
634 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
635 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
636 }
637 }
638 OS << " // " << PRDef->getName() << "\n";
639 }
640 OS << "};\n";
641}
642
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000643static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
644 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000645 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000646 if (Record *RCU = ProcModel.RetireControlUnit) {
647 ReorderBufferSize =
648 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
649 MaxRetirePerCycle =
650 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
651 }
652
653 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
654 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
655}
656
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000657static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
658 unsigned NumRegisterFiles,
659 unsigned NumCostEntries, raw_ostream &OS) {
660 if (NumRegisterFiles)
661 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
662 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000663 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000664
665 OS << ", // Number of register files.\n ";
666 if (NumCostEntries)
667 OS << ProcModel.ModelName << "RegisterCosts,\n ";
668 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000669 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000670 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000671}
672
673unsigned
674SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
675 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000676 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
677 return RF.hasDefaultCosts();
678 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000679 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000680
681 // Print the RegisterCost table first.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000682 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000683 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
684 << "RegisterCosts"
685 << "[] = {\n";
686
687 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
688 // Skip register files with a default cost table.
689 if (RF.hasDefaultCosts())
690 continue;
691 // Add entries to the cost table.
692 for (const CodeGenRegisterCost &RC : RF.Costs) {
693 OS << " { ";
694 Record *Rec = RC.RCDef;
695 if (Rec->getValue("Namespace"))
696 OS << Rec->getValueAsString("Namespace") << "::";
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000697 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
698 << RC.AllowMoveElimination << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000699 }
700 }
701 OS << "};\n";
702
703 // Now generate a table with register file info.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000704 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
705 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000706 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
707 << "RegisterFiles"
708 << "[] = {\n"
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000709 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000710 unsigned CostTblIndex = 0;
711
712 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
713 OS << " { ";
714 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
715 unsigned NumCostEntries = RD.Costs.size();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000716 OS << NumCostEntries << ", " << CostTblIndex << ", "
717 << RD.MaxMovesEliminatedPerCycle << ", "
718 << RD.AllowZeroMoveEliminationOnly << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000719 CostTblIndex += NumCostEntries;
720 }
721 OS << "};\n";
722
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000723 return CostTblIndex;
724}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000725
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000726void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
727 raw_ostream &OS) {
728 unsigned QueueID = 0;
729 if (ProcModel.LoadQueue) {
730 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
731 QueueID =
732 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
733 std::find(ProcModel.ProcResourceDefs.begin(),
734 ProcModel.ProcResourceDefs.end(), Queue));
735 }
736 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
737
738 QueueID = 0;
739 if (ProcModel.StoreQueue) {
740 const Record *Queue =
741 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
742 QueueID =
743 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
744 std::find(ProcModel.ProcResourceDefs.begin(),
745 ProcModel.ProcResourceDefs.end(), Queue));
746 }
747 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
748}
749
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000750void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
751 raw_ostream &OS) {
752 // Generate a table of register file descriptors (one entry per each user
753 // defined register file), and a table of register costs.
754 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
755
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000756 // Now generate a table for the extra processor info.
757 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000758 << "ExtraInfo = {\n ";
759
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000760 // Add information related to the retire control unit.
761 EmitRetireControlUnitInfo(ProcModel, OS);
762
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000763 // Add information related to the register files (i.e. where to find register
764 // file descriptors and register costs).
765 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
766 NumCostEntries, OS);
767
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000768 // Add information about load/store queues.
769 EmitLoadStoreQueueInfo(ProcModel, OS);
770
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000771 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000772}
773
Andrew Trick23f3c652012-09-17 22:18:45 +0000774void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
775 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000776 EmitProcessorResourceSubUnits(ProcModel, OS);
777
Jinsong Ji05941622018-09-18 15:38:56 +0000778 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000779 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
780 << "ProcResources"
781 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000782 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000783
Clement Courbet39911e22018-02-08 08:46:48 +0000784 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000785 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
786 Record *PRDef = ProcModel.ProcResourceDefs[i];
787
Craig Topper24064772014-04-15 07:20:03 +0000788 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000789 unsigned SuperIdx = 0;
790 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000791 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000792 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000793 if (PRDef->isSubClassOf("ProcResGroup")) {
794 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000795 for (Record *RU : ResUnits) {
796 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000797 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000798 }
799 }
800 else {
801 // Find the SuperIdx
802 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000803 SuperDef =
804 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
805 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000806 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
807 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000808 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000809 }
810 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000811 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000812 if (PRDef->getName().size() < 15)
813 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000814 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
815 if (SubUnitsBeginOffset != SubUnitsOffset) {
816 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
817 << SubUnitsBeginOffset;
818 } else {
819 OS << "nullptr";
820 }
821 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000822 if (SuperDef)
823 OS << ", Super=" << SuperDef->getName();
824 OS << "\n";
825 }
826 OS << "};\n";
827}
828
Andrew Trick9ef08822012-09-17 22:18:48 +0000829// Find the WriteRes Record that defines processor resources for this
830// SchedWrite.
831Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000832 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000833
834 // Check if the SchedWrite is already subtarget-specific and directly
835 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000836 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
837 return SchedWrite.TheDef;
838
Craig Topper24064772014-04-15 07:20:03 +0000839 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000840 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000841 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000842 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000843 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
844 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
845 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
846 continue;
847 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000848 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000849 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000850 "defined for processor " + ProcModel.ModelName +
851 " Ensure only one SchedAlias exists per RW.");
852 AliasDef = AliasRW.TheDef;
853 }
854 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
855 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000856
857 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000858 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000859 for (Record *WR : ProcModel.WriteResDefs) {
860 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000861 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000862 if (AliasDef == WR->getValueAsDef("WriteType")
863 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000864 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000865 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000866 "SchedWrite and its alias on processor " +
867 ProcModel.ModelName);
868 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000869 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000870 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000871 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000872 // TODO: If ProcModel has a base model (previous generation processor),
873 // then call FindWriteResources recursively with that model here.
874 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000875 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000876 Twine("Processor does not define resources for ") +
877 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000878 }
879 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000880}
881
882/// Find the ReadAdvance record for the given SchedRead on this processor or
883/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000884Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000885 const CodeGenProcModel &ProcModel) {
886 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000887 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
888 return SchedRead.TheDef;
889
890 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000891 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000892 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000893 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000894 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000895 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
896 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
897 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
898 continue;
899 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000900 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000901 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000902 "defined for processor " + ProcModel.ModelName +
903 " Ensure only one SchedAlias exists per RW.");
904 AliasDef = AliasRW.TheDef;
905 }
906 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
907 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000908
909 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000910 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000911 for (Record *RA : ProcModel.ReadAdvanceDefs) {
912 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000913 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000914 if (AliasDef == RA->getValueAsDef("ReadType")
915 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000916 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000917 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000918 "SchedRead and its alias on processor " +
919 ProcModel.ModelName);
920 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000921 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000922 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000923 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000924 // TODO: If ProcModel has a base model (previous generation processor),
925 // then call FindReadAdvance recursively with that model here.
926 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000927 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000928 Twine("Processor does not define resources for ") +
929 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000930 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000931 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000932}
933
Andrew Trick4e67cba2013-03-14 21:21:50 +0000934// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000935// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000936void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
937 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000938 const CodeGenProcModel &PM) {
Clement Courbet5eeed772018-06-13 09:41:49 +0000939 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000940 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000941 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000942 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000943 if (PRDef->isSubClassOf("ProcResGroup"))
944 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000945 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000946 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000947 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000948 for (Record *SubDef = PRDef;
949 SubDef->getValueInit("Super")->isComplete();) {
950 if (SubDef->isSubClassOf("ProcResGroup")) {
951 // Disallow this for simplicitly.
952 PrintFatalError(SubDef->getLoc(), "Processor resource group "
953 " cannot be a super resources.");
954 }
955 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000956 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
957 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000958 PRVec.push_back(SuperDef);
959 Cycles.push_back(Cycles[i]);
960 SubDef = SuperDef;
961 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000962 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000963 for (Record *PR : PM.ProcResourceDefs) {
964 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000965 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000966 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000967 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000968 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000969 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000970 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000971 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000972 }
973 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000974 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000975 Cycles.push_back(Cycles[i]);
976 }
977 }
978 }
979}
980
Andrew Trick9ef08822012-09-17 22:18:48 +0000981// Generate the SchedClass table for this processor and update global
982// tables. Must be called for each processor in order.
983void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
984 SchedClassTables &SchedTables) {
985 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
986 if (!ProcModel.hasInstrSchedModel())
987 return;
988
989 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000990 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000991 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000992 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000993
Andrew Trick9ef08822012-09-17 22:18:48 +0000994 SCTab.resize(SCTab.size() + 1);
995 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000996 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000997 SCDesc.NumMicroOps = 0;
998 SCDesc.BeginGroup = false;
999 SCDesc.EndGroup = false;
1000 SCDesc.WriteProcResIdx = 0;
1001 SCDesc.WriteLatencyIdx = 0;
1002 SCDesc.ReadAdvanceIdx = 0;
1003
1004 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +00001005 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +00001006 for (const CodeGenSchedTransition &CGT :
1007 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1008 if (CGT.ProcIndices[0] == 0 ||
1009 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001010 HasVariants = true;
1011 break;
1012 }
1013 }
1014 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001015 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1016 continue;
1017 }
1018
1019 // Determine if the SchedClass is actually reachable on this processor. If
1020 // not don't try to locate the processor resources, it will fail.
1021 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001022 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1023 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +00001024 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001025 continue;
1026 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001027 IdxVec Writes = SC.Writes;
1028 IdxVec Reads = SC.Reads;
1029 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001030 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001031 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001032 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001033 for (Record *RW : SC.InstRWs) {
1034 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001035 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001036 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001037 break;
1038 }
1039 }
1040 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001041 Writes.clear();
1042 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001043 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1044 Writes, Reads);
1045 }
1046 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001047 if (Writes.empty()) {
1048 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001049 for (Record *I : ProcModel.ItinRWDefs) {
1050 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001051 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001052 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001053 Writes, Reads);
1054 break;
1055 }
1056 }
1057 if (Writes.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001058 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1059 << " does not have resources for class " << SC.Name
1060 << '\n');
QingShan Zhangbb8d5402019-10-11 08:36:54 +00001061 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001062 }
1063 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001064 // Sum resources across all operand writes.
1065 std::vector<MCWriteProcResEntry> WriteProcResources;
1066 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001067 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001068 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001069 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001070 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001071 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001072 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001073
1074 // For each operand, create a latency entry.
1075 MCWriteLatencyEntry WLEntry;
1076 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001077 unsigned WriteID = WriteSeq.back();
1078 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1079 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1080 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001081 if (!SchedModels.hasReadOfWrite(
1082 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001083 WriteID = 0;
1084 }
1085 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001086
Craig Topper29c55dcb2016-02-13 06:03:32 +00001087 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001088
Andrew Trick9257b8f2012-09-22 02:24:21 +00001089 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001090 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001091
1092 // Mark the parent class as invalid for unsupported write types.
1093 if (WriteRes->getValueAsBit("Unsupported")) {
1094 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1095 break;
1096 }
1097 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1098 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1099 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1100 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001101 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1102 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001103
1104 // Create an entry for each ProcResource listed in WriteRes.
1105 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1106 std::vector<int64_t> Cycles =
1107 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001108
Clement Courbet5eeed772018-06-13 09:41:49 +00001109 if (Cycles.empty()) {
1110 // If ResourceCycles is not provided, default to one cycle per
1111 // resource.
1112 Cycles.resize(PRVec.size(), 1);
1113 } else if (Cycles.size() != PRVec.size()) {
1114 // If ResourceCycles is provided, check consistency.
1115 PrintFatalError(
1116 WriteRes->getLoc(),
1117 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1118 "!size(ProcResources): ")
1119 .concat(Twine(PRVec.size()))
1120 .concat(" vs ")
1121 .concat(Twine(Cycles.size())));
1122 }
1123
Andrew Trick4e67cba2013-03-14 21:21:50 +00001124 ExpandProcResources(PRVec, Cycles, ProcModel);
1125
Andrew Trick9ef08822012-09-17 22:18:48 +00001126 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1127 PRIdx != PREnd; ++PRIdx) {
1128 MCWriteProcResEntry WPREntry;
1129 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1130 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001131 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001132 // If this resource is already used in this sequence, add the current
1133 // entry's cycles so that the same resource appears to be used
1134 // serially, rather than multiple parallel uses. This is important for
1135 // in-order machine where the resource consumption is a hazard.
1136 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1137 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1138 if (WriteProcResources[WPRIdx].ProcResourceIdx
1139 == WPREntry.ProcResourceIdx) {
1140 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1141 break;
1142 }
1143 }
1144 if (WPRIdx == WPREnd)
1145 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001146 }
1147 }
1148 WriteLatencies.push_back(WLEntry);
1149 }
1150 // Create an entry for each operand Read in this SchedClass.
1151 // Entries must be sorted first by UseIdx then by WriteResourceID.
1152 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1153 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001154 Record *ReadAdvance =
1155 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001156 if (!ReadAdvance)
1157 continue;
1158
1159 // Mark the parent class as invalid for unsupported write types.
1160 if (ReadAdvance->getValueAsBit("Unsupported")) {
1161 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1162 break;
1163 }
1164 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1165 IdxVec WriteIDs;
1166 if (ValidWrites.empty())
1167 WriteIDs.push_back(0);
1168 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001169 for (Record *VW : ValidWrites) {
1170 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001171 }
1172 }
Fangrui Song0cac7262018-09-27 02:13:45 +00001173 llvm::sort(WriteIDs);
Craig Topper29c55dcb2016-02-13 06:03:32 +00001174 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001175 MCReadAdvanceEntry RAEntry;
1176 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001177 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001178 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1179 ReadAdvanceEntries.push_back(RAEntry);
1180 }
1181 }
1182 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1183 WriteProcResources.clear();
1184 WriteLatencies.clear();
1185 ReadAdvanceEntries.clear();
1186 }
1187 // Add the information for this SchedClass to the global tables using basic
1188 // compression.
1189 //
1190 // WritePrecRes entries are sorted by ProcResIdx.
Fangrui Song0cac7262018-09-27 02:13:45 +00001191 llvm::sort(WriteProcResources, LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001192
1193 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1194 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1195 std::search(SchedTables.WriteProcResources.begin(),
1196 SchedTables.WriteProcResources.end(),
1197 WriteProcResources.begin(), WriteProcResources.end());
1198 if (WPRPos != SchedTables.WriteProcResources.end())
1199 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1200 else {
1201 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1202 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1203 WriteProcResources.end());
1204 }
1205 // Latency entries must remain in operand order.
1206 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1207 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1208 std::search(SchedTables.WriteLatencies.begin(),
1209 SchedTables.WriteLatencies.end(),
1210 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001211 if (WLPos != SchedTables.WriteLatencies.end()) {
1212 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1213 SCDesc.WriteLatencyIdx = idx;
1214 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1215 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1216 std::string::npos) {
1217 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1218 }
1219 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001220 else {
1221 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001222 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1223 WriteLatencies.begin(),
1224 WriteLatencies.end());
1225 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1226 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001227 }
1228 // ReadAdvanceEntries must remain in operand order.
1229 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1230 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1231 std::search(SchedTables.ReadAdvanceEntries.begin(),
1232 SchedTables.ReadAdvanceEntries.end(),
1233 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1234 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1235 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1236 else {
1237 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1238 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1239 ReadAdvanceEntries.end());
1240 }
1241 }
1242}
1243
Andrew Tricka72fca62012-09-17 22:18:50 +00001244// Emit SchedClass tables for all processors and associated global tables.
1245void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1246 raw_ostream &OS) {
1247 // Emit global WriteProcResTable.
1248 OS << "\n// {ProcResourceIdx, Cycles}\n"
1249 << "extern const llvm::MCWriteProcResEntry "
1250 << Target << "WriteProcResTable[] = {\n"
1251 << " { 0, 0}, // Invalid\n";
1252 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1253 WPRIdx != WPREnd; ++WPRIdx) {
1254 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1255 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1256 << format("%2d", WPREntry.Cycles) << "}";
1257 if (WPRIdx + 1 < WPREnd)
1258 OS << ',';
1259 OS << " // #" << WPRIdx << '\n';
1260 }
1261 OS << "}; // " << Target << "WriteProcResTable\n";
1262
1263 // Emit global WriteLatencyTable.
1264 OS << "\n// {Cycles, WriteResourceID}\n"
1265 << "extern const llvm::MCWriteLatencyEntry "
1266 << Target << "WriteLatencyTable[] = {\n"
1267 << " { 0, 0}, // Invalid\n";
1268 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1269 WLIdx != WLEnd; ++WLIdx) {
1270 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1271 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1272 << format("%2d", WLEntry.WriteResourceID) << "}";
1273 if (WLIdx + 1 < WLEnd)
1274 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001275 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001276 }
1277 OS << "}; // " << Target << "WriteLatencyTable\n";
1278
1279 // Emit global ReadAdvanceTable.
1280 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1281 << "extern const llvm::MCReadAdvanceEntry "
1282 << Target << "ReadAdvanceTable[] = {\n"
1283 << " {0, 0, 0}, // Invalid\n";
1284 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1285 RAIdx != RAEnd; ++RAIdx) {
1286 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1287 OS << " {" << RAEntry.UseIdx << ", "
1288 << format("%2d", RAEntry.WriteResourceID) << ", "
1289 << format("%2d", RAEntry.Cycles) << "}";
1290 if (RAIdx + 1 < RAEnd)
1291 OS << ',';
1292 OS << " // #" << RAIdx << '\n';
1293 }
1294 OS << "}; // " << Target << "ReadAdvanceTable\n";
1295
1296 // Emit a SchedClass table for each processor.
1297 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1298 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1299 if (!PI->hasInstrSchedModel())
1300 continue;
1301
1302 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001303 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001304
1305 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1306 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1307 OS << "static const llvm::MCSchedClassDesc "
1308 << PI->ModelName << "SchedClasses[] = {\n";
1309
1310 // The first class is always invalid. We no way to distinguish it except by
1311 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001312 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001313 && "invalid class not first");
1314 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1315 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001316 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001317
1318 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1319 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1320 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1321 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1322 if (SchedClass.Name.size() < 18)
1323 OS.indent(18 - SchedClass.Name.size());
1324 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001325 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1326 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001327 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1328 << ", " << MCDesc.NumWriteProcResEntries
1329 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1330 << ", " << MCDesc.NumWriteLatencyEntries
1331 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001332 << ", " << MCDesc.NumReadAdvanceEntries
1333 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001334 }
1335 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1336 }
1337}
1338
Andrew Trick87255e32012-07-07 04:00:00 +00001339void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1340 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001341 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001342 // Emit extra processor info if available.
1343 if (PM.hasExtraProcessorInfo())
1344 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001345 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001346 if (PM.hasInstrSchedModel())
1347 EmitProcessorResources(PM, OS);
1348 else if(!PM.ProcResourceDefs.empty())
1349 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001350 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001351
Andrew Trick73d77362012-06-05 03:44:40 +00001352 // Begin processor itinerary properties
1353 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001354 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1355 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1356 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1357 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1358 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1359 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1360 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001361
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001362 bool PostRAScheduler =
1363 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001364
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001365 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1366 << "PostRAScheduler\n";
1367
1368 bool CompleteModel =
1369 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1370
1371 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1372 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001373
Craig Topper29c55dcb2016-02-13 06:03:32 +00001374 OS << " " << PM.Index << ", // Processor ID\n";
1375 if (PM.hasInstrSchedModel())
1376 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1377 << " " << PM.ModelName << "SchedClasses" << ",\n"
1378 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001379 << " " << (SchedModels.schedClassEnd()
1380 - SchedModels.schedClassBegin()) << ",\n";
1381 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001382 OS << " nullptr, nullptr, 0, 0,"
1383 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001384 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001385 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001386 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001387 OS << " nullptr, // No Itinerary\n";
1388 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001389 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001390 else
Clement Courbetb4493792018-04-10 08:16:37 +00001391 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001392 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001393 }
Jim Laskey3763a502005-10-31 17:16:01 +00001394}
1395
1396//
Andrew Trick87255e32012-07-07 04:00:00 +00001397// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001398//
Andrew Trick87255e32012-07-07 04:00:00 +00001399void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001400 OS << "#ifdef DBGFIELD\n"
1401 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1402 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001403 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001404 << "#define DBGFIELD(x) x,\n"
1405 << "#else\n"
1406 << "#define DBGFIELD(x)\n"
1407 << "#endif\n";
1408
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001409 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001410 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001411 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001412 EmitStageAndOperandCycleData(OS, ProcItinLists);
1413 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001414 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001415 OS << "\n// ===============================================================\n"
1416 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001417
Andrew Trick9ef08822012-09-17 22:18:48 +00001418 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001419 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1420 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001421 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001422 EmitSchedClassTables(SchedTables, OS);
1423
Craig Topper2982b842019-03-05 18:54:38 +00001424 OS << "\n#undef DBGFIELD\n";
1425
Andrew Tricka72fca62012-09-17 22:18:50 +00001426 // Emit the processor machine model
1427 EmitProcessorModels(OS);
Jim Laskey86f002c2005-10-27 19:47:21 +00001428}
1429
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001430static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1431 std::string Buffer;
1432 raw_string_ostream Stream(Buffer);
1433
1434 // Collect all the PredicateProlog records and print them to the output
1435 // stream.
1436 std::vector<Record *> Prologs =
1437 Records.getAllDerivedDefinitions("PredicateProlog");
Fangrui Song0cac7262018-09-27 02:13:45 +00001438 llvm::sort(Prologs, LessRecord());
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001439 for (Record *P : Prologs)
1440 Stream << P->getValueAsString("Code") << '\n';
1441
1442 Stream.flush();
1443 OS << Buffer;
1444}
1445
1446static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001447 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001448 raw_ostream &OS) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001449 std::string Buffer;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001450 raw_string_ostream SS(Buffer);
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001451
1452 auto IsTruePredicate = [](const Record *Rec) {
1453 return Rec->isSubClassOf("MCSchedPredicate") &&
1454 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1455 };
1456
1457 // If not all predicates are MCTrue, then we need an if-stmt.
1458 unsigned NumNonTruePreds =
1459 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001460
1461 SS.indent(PE.getIndentLevel() * 2);
1462
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001463 if (NumNonTruePreds) {
1464 bool FirstNonTruePredicate = true;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001465 SS << "if (";
1466
1467 PE.setIndentLevel(PE.getIndentLevel() + 2);
1468
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001469 for (const Record *Rec : T.PredTerm) {
1470 // Skip predicates that evaluate to "true".
1471 if (IsTruePredicate(Rec))
1472 continue;
1473
1474 if (FirstNonTruePredicate) {
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001475 FirstNonTruePredicate = false;
1476 } else {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001477 SS << "\n";
1478 SS.indent(PE.getIndentLevel() * 2);
1479 SS << "&& ";
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001480 }
1481
1482 if (Rec->isSubClassOf("MCSchedPredicate")) {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001483 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001484 continue;
1485 }
1486
1487 // Expand this legacy predicate and wrap it around braces if there is more
1488 // than one predicate to expand.
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001489 SS << ((NumNonTruePreds > 1) ? "(" : "")
1490 << Rec->getValueAsString("Predicate")
1491 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagio95140022018-05-25 15:55:37 +00001492 }
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001493
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001494 SS << ")\n"; // end of if-stmt
1495 PE.decreaseIndentLevel();
1496 SS.indent(PE.getIndentLevel() * 2);
1497 PE.decreaseIndentLevel();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001498 }
1499
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001500 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1501 SS.flush();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001502 OS << Buffer;
1503}
1504
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001505// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1506// epilogue code for the auto-generated helper.
1507void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1508 if (ShouldReturnZero) {
Andrea Di Biagio95140022018-05-25 15:55:37 +00001509 OS << " // Don't know how to resolve this scheduling class.\n"
1510 << " return 0;\n";
1511 return;
1512 }
1513
1514 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1515}
1516
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001517bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1518 return all_of(T.PredTerm, [](const Record *Rec) {
1519 return Rec->isSubClassOf("MCSchedPredicate");
1520 });
1521}
1522
1523void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1524 IdxVec &VariantClasses,
1525 bool OnlyExpandMCInstPredicates) {
1526 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1527 // Ignore non-variant scheduling classes.
1528 if (SC.Transitions.empty())
1529 continue;
1530
1531 if (OnlyExpandMCInstPredicates) {
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001532 // Ignore this variant scheduling class no transitions use any meaningful
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001533 // MCSchedPredicate definitions.
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001534 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001535 return hasMCSchedPredicates(T);
1536 }))
1537 continue;
1538 }
1539
1540 VariantClasses.push_back(SC.Index);
1541 }
1542}
1543
1544void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1545 // A variant scheduling class may define transitions for multiple
1546 // processors. This function identifies wich processors are associated with
1547 // transition rules specified by variant class `SC`.
1548 for (const CodeGenSchedTransition &T : SC.Transitions) {
1549 IdxVec PI;
1550 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1551 ProcIndices.begin(), ProcIndices.end(),
1552 std::back_inserter(PI));
1553 ProcIndices.swap(PI);
1554 }
1555}
1556
1557void SubtargetEmitter::emitSchedModelHelpersImpl(
1558 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1559 IdxVec VariantClasses;
1560 collectVariantClasses(SchedModels, VariantClasses,
1561 OnlyExpandMCInstPredicates);
1562
1563 if (VariantClasses.empty()) {
1564 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1565 return;
1566 }
1567
1568 // Construct a switch statement where the condition is a check on the
1569 // scheduling class identifier. There is a `case` for every variant class
1570 // defined by the processor models of this target.
1571 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1572 // a variant scheduling class to another scheduling class. Rules are
1573 // described by instances of CodeGenSchedTransition. Note that transitions may
1574 // not be valid for all processors.
1575 OS << " switch (SchedClass) {\n";
1576 for (unsigned VC : VariantClasses) {
1577 IdxVec ProcIndices;
1578 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1579 collectProcessorIndices(SC, ProcIndices);
1580
1581 OS << " case " << VC << ": // " << SC.Name << '\n';
1582
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +00001583 PredicateExpander PE(Target);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001584 PE.setByRef(false);
1585 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1586 for (unsigned PI : ProcIndices) {
1587 OS << " ";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001588
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001589 // Emit a guard on the processor ID.
1590 if (PI != 0) {
1591 OS << (OnlyExpandMCInstPredicates
1592 ? "if (CPUID == "
1593 : "if (SchedModel->getProcessorID() == ");
1594 OS << PI << ") ";
1595 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1596 }
1597
1598 // Now emit transitions associated with processor PI.
1599 for (const CodeGenSchedTransition &T : SC.Transitions) {
1600 if (PI != 0 && !count(T.ProcIndices, PI))
1601 continue;
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001602
1603 // Emit only transitions based on MCSchedPredicate, if it's the case.
1604 // At least the transition specified by NoSchedPred is emitted,
1605 // which becomes the default transition for those variants otherwise
1606 // not based on MCSchedPredicate.
1607 // FIXME: preferably, llvm-mca should instead assume a reasonable
1608 // default when a variant transition is not based on MCSchedPredicate
1609 // for a given processor.
1610 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1611 continue;
1612
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001613 PE.setIndentLevel(3);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001614 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1615 }
1616
1617 OS << " }\n";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001618
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001619 if (PI == 0)
1620 break;
1621 }
1622
1623 if (SC.isInferred())
1624 OS << " return " << SC.Index << ";\n";
1625 OS << " break;\n";
1626 }
1627
1628 OS << " };\n";
1629
1630 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1631}
1632
Andrea Di Biagio95140022018-05-25 15:55:37 +00001633void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1634 raw_ostream &OS) {
1635 OS << "unsigned " << ClassName
1636 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1637 << " const TargetSchedModel *SchedModel) const {\n";
1638
1639 // Emit the predicate prolog code.
1640 emitPredicateProlog(Records, OS);
1641
1642 // Emit target predicates.
1643 emitSchedModelHelpersImpl(OS);
Clement Courbet41c8af32018-10-25 07:44:01 +00001644
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001645 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagio95140022018-05-25 15:55:37 +00001646
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001647 OS << "unsigned " << ClassName
1648 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1649 << " unsigned CPUID) const {\n"
1650 << " return " << Target << "_MC"
1651 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001652 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1653
1654 STIPredicateExpander PE(Target);
1655 PE.setClassPrefix(ClassName);
1656 PE.setExpandDefinition(true);
1657 PE.setByRef(false);
1658 PE.setIndentLevel(0);
1659
1660 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1661 PE.expandSTIPredicate(OS, Fn);
Andrew Trickc6c88152012-09-18 03:41:43 +00001662}
1663
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001664void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1665 raw_ostream &OS) {
1666 const CodeGenHwModes &CGH = TGT.getHwModes();
1667 assert(CGH.getNumModeIds() > 0);
1668 if (CGH.getNumModeIds() == 1)
1669 return;
1670
1671 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1672 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1673 const HwMode &HM = CGH.getMode(M);
1674 OS << " if (checkFeatures(\"" << HM.Features
1675 << "\")) return " << M << ";\n";
1676 }
1677 OS << " return 0;\n}\n";
1678}
1679
Jim Laskey86f002c2005-10-27 19:47:21 +00001680//
Jim Laskeya2b52352005-10-26 17:30:34 +00001681// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1682// the subtarget features string.
1683//
Evan Cheng54b68e32011-07-01 20:45:01 +00001684void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1685 unsigned NumFeatures,
1686 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001687 std::vector<Record*> Features =
1688 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +00001689 llvm::sort(Features, LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001690
Andrew Trickdb6ed642011-04-01 01:56:55 +00001691 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1692 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001693 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001694 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001695 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001696 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1697 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001698
1699 if (Features.empty()) {
1700 OS << "}\n";
1701 return;
1702 }
1703
Andrew Trickba7b9212012-09-18 05:33:15 +00001704 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001705 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001706
Craig Topper29c55dcb2016-02-13 06:03:32 +00001707 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001708 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001709 StringRef Instance = R->getName();
1710 StringRef Value = R->getValueAsString("Value");
1711 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001712
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001713 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001714 OS << " if (Bits[" << Target << "::"
1715 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001716 << Attribute << " = " << Value << ";\n";
1717 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001718 OS << " if (Bits[" << Target << "::"
1719 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001720 << Attribute << " < " << Value << ") "
1721 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001722 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001723
Evan Chengfe6e4052011-06-30 01:53:36 +00001724 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001725}
1726
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001727void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001728 OS << "namespace " << Target << "_MC {\n"
1729 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1730 << " const MCInst *MI, unsigned CPUID) {\n";
1731 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1732 OS << "}\n";
Bjorn Pettersson6bd3a9e2019-08-25 10:47:30 +00001733 OS << "} // end namespace " << Target << "_MC\n\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001734
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001735 OS << "struct " << Target
1736 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1737 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1738 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
Craig Topperca268082019-03-05 18:54:34 +00001739 << " ArrayRef<SubtargetSubTypeKV> PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001740 << " const MCWriteProcResEntry *WPR,\n"
1741 << " const MCWriteLatencyEntry *WL,\n"
1742 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1743 << " const unsigned *OC, const unsigned *FP) :\n"
Craig Topper2982b842019-03-05 18:54:38 +00001744 << " MCSubtargetInfo(TT, CPU, FS, PF, PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001745 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1746 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001747 << " const MCInst *MI, unsigned CPUID) const override {\n"
1748 << " return " << Target << "_MC"
1749 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001750 OS << " }\n";
James Molloy88a5fbf2019-09-19 13:39:54 +00001751 if (TGT.getHwModes().getNumModeIds() > 1)
1752 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001753 OS << "};\n";
James Molloy88a5fbf2019-09-19 13:39:54 +00001754 EmitHwModeCheck(Target + "GenMCSubtargetInfo", OS);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001755}
1756
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001757void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1758 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1759 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1760
1761 STIPredicateExpander PE(Target);
1762 PE.setExpandForMC(true);
1763 PE.setByRef(true);
1764 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1765 PE.expandSTIPredicate(OS, Fn);
1766
1767 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1768
1769 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1770 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1771
1772 std::string ClassPrefix = Target + "MCInstrAnalysis";
1773 PE.setExpandDefinition(true);
1774 PE.setClassPrefix(ClassPrefix);
1775 PE.setIndentLevel(0);
1776 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1777 PE.expandSTIPredicate(OS, Fn);
1778
1779 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1780}
1781
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001782//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001783// SubtargetEmitter::run - Main subtarget enumeration emitter.
1784//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001785void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001786 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001787
Evan Cheng4d1ca962011-07-08 01:53:10 +00001788 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001789 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001790
Craig Topper4f613082019-03-01 02:19:26 +00001791 DenseMap<Record *, unsigned> FeatureMap;
1792
Evan Cheng4d1ca962011-07-08 01:53:10 +00001793 OS << "namespace llvm {\n";
Craig Topper4f613082019-03-01 02:19:26 +00001794 Enumeration(OS, FeatureMap);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001795 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001796 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1797
Evan Cheng54b68e32011-07-01 20:45:01 +00001798 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001799 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001800
Evan Cheng54b68e32011-07-01 20:45:01 +00001801 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001802#if 0
1803 OS << "namespace {\n";
1804#endif
Craig Topper4f613082019-03-01 02:19:26 +00001805 unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap);
Evan Chengbc153d42011-07-14 20:59:42 +00001806 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001807 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001808 OS << "\n";
Craig Topper2982b842019-03-05 18:54:38 +00001809 unsigned NumProcs = CPUKeyValues(OS, FeatureMap);
1810 OS << "\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001811#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001812 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001813#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001814
1815 // MCInstrInfo initialization routine.
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001816 emitGenMCSubtargetInfo(OS);
1817
Craig Topper194cb742017-10-24 15:50:55 +00001818 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001819 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001820 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001821 OS << " return new " << Target << "GenMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001822 if (NumFeatures)
1823 OS << Target << "FeatureKV, ";
1824 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001825 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001826 if (NumProcs)
1827 OS << Target << "SubTypeKV, ";
1828 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001829 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001830 OS << '\n'; OS.indent(22);
Craig Topper2982b842019-03-05 18:54:38 +00001831 OS << Target << "WriteProcResTable, "
Andrew Trickab722bd2012-09-18 03:18:56 +00001832 << Target << "WriteLatencyTable, "
1833 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001834 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001835 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001836 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001837 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001838 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001839 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001840 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001841 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001842
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001843 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001844
1845 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1846
1847 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001848 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001849
1850 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001851 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001852 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1853
1854 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1855
Evan Cheng0d639a22011-07-01 21:01:15 +00001856 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001857 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001858 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001859
1860 std::string ClassName = Target + "GenSubtargetInfo";
1861 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001862 OS << "class DFAPacketizer;\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001863 OS << "namespace " << Target << "_MC {\n"
1864 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1865 << " const MCInst *MI, unsigned CPUID);\n"
Bjorn Pettersson6bd3a9e2019-08-25 10:47:30 +00001866 << "} // end namespace " << Target << "_MC\n\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001867 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001868 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001869 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001870 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001871 << " unsigned resolveSchedClass(unsigned SchedClass, "
1872 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001873 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001874 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1875 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001876 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001877 << " const;\n";
1878 if (TGT.getHwModes().getNumModeIds() > 1)
1879 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001880
1881 STIPredicateExpander PE(Target);
1882 PE.setByRef(false);
1883 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1884 PE.expandSTIPredicate(OS, Fn);
1885
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001886 OS << "};\n"
1887 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001888
1889 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1890
1891 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001892 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001893
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001894 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001895 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001896 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
Craig Topperca268082019-03-05 18:54:34 +00001897 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001898 OS << "extern const llvm::MCWriteProcResEntry "
1899 << Target << "WriteProcResTable[];\n";
1900 OS << "extern const llvm::MCWriteLatencyEntry "
1901 << Target << "WriteLatencyTable[];\n";
1902 OS << "extern const llvm::MCReadAdvanceEntry "
1903 << Target << "ReadAdvanceTable[];\n";
1904
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001905 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001906 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1907 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001908 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001909 }
1910
Daniel Sanders50f17232015-09-15 16:17:27 +00001911 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1912 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001913 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001914 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001915 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001916 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001917 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001918 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001919 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001920 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001921 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001922 OS << '\n'; OS.indent(24);
Craig Topper2982b842019-03-05 18:54:38 +00001923 OS << Target << "WriteProcResTable, "
Andrew Trickab722bd2012-09-18 03:18:56 +00001924 << Target << "WriteLatencyTable, "
1925 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001926 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001927 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001928 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001929 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001930 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001931 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001932 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001933 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001934
Andrew Trickc6c88152012-09-18 03:41:43 +00001935 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001936 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001937
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001938 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001939
1940 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001941
1942 EmitMCInstrAnalysisPredicateFunctions(OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001943}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001944
1945namespace llvm {
1946
1947void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001948 CodeGenTarget CGTarget(RK);
1949 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001950}
1951
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001952} // end namespace llvm