blob: 9b094adb7d5ced321bfcc2c3b8fecb8dac987c64 [file] [log] [blame]
Jim Laskeycfda85a2005-10-21 19:00:04 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jim Laskeycfda85a2005-10-21 19:00:04 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner73fbe142006-03-03 02:04:07 +00009// This tablegen backend emits subtarget enumerations.
Jim Laskeycfda85a2005-10-21 19:00:04 +000010//
11//===----------------------------------------------------------------------===//
12
Jim Laskeycfda85a2005-10-21 19:00:04 +000013#include "CodeGenTarget.h"
Andrew Trick87255e32012-07-07 04:00:00 +000014#include "CodeGenSchedule.h"
Andrea Di Biagio95140022018-05-25 15:55:37 +000015#include "PredicateExpander.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000016#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000019#include "llvm/ADT/StringRef.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000020#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000021#include "llvm/MC/MCSchedule.h"
Michael Kupersteindb0712f2015-05-26 10:47:10 +000022#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/Format.h"
Eugene Zelenko75259bb2016-05-17 17:04:23 +000025#include "llvm/Support/raw_ostream.h"
Andrew Trick23f3c652012-09-17 22:18:45 +000026#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027#include "llvm/TableGen/Record.h"
28#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenb0aa47b2005-10-28 01:43:09 +000029#include <algorithm>
Eugene Zelenko75259bb2016-05-17 17:04:23 +000030#include <cassert>
31#include <cstdint>
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000032#include <iterator>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000033#include <map>
34#include <string>
35#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000036
Jim Laskeycfda85a2005-10-21 19:00:04 +000037using namespace llvm;
38
Chandler Carruth97acce22014-04-22 03:06:00 +000039#define DEBUG_TYPE "subtarget-emitter"
40
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000041namespace {
Eugene Zelenko75259bb2016-05-17 17:04:23 +000042
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043class SubtargetEmitter {
Andrew Trick9ef08822012-09-17 22:18:48 +000044 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
45 // The SchedClassDesc table indexes into a global write resource table, write
46 // latency table, and read advance table.
47 struct SchedClassTables {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000048 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
Andrew Trick9ef08822012-09-17 22:18:48 +000049 std::vector<MCWriteProcResEntry> WriteProcResources;
50 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +000051 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +000052 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
53
54 // Reserve an invalid entry at index 0
55 SchedClassTables() {
56 ProcSchedClasses.resize(1);
57 WriteProcResources.resize(1);
58 WriteLatencies.resize(1);
Andrew Trickcfe222c2012-09-19 04:43:19 +000059 WriterNames.push_back("InvalidWrite");
Andrew Trick9ef08822012-09-17 22:18:48 +000060 ReadAdvanceEntries.resize(1);
61 }
62 };
63
64 struct LessWriteProcResources {
65 bool operator()(const MCWriteProcResEntry &LHS,
66 const MCWriteProcResEntry &RHS) {
67 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
68 }
69 };
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000070
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +000071 const CodeGenTarget &TGT;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000072 RecordKeeper &Records;
Andrew Trick87255e32012-07-07 04:00:00 +000073 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000074 std::string Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000075
Craig Topper4f613082019-03-01 02:19:26 +000076 void Enumeration(raw_ostream &OS, DenseMap<Record *, unsigned> &FeatureMap);
77 unsigned FeatureKeyValues(raw_ostream &OS,
78 const DenseMap<Record *, unsigned> &FeatureMap);
79 unsigned CPUKeyValues(raw_ostream &OS,
80 const DenseMap<Record *, unsigned> &FeatureMap);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000081 void FormItineraryStageString(const std::string &Names,
82 Record *ItinData, std::string &ItinString,
83 unsigned &NStages);
84 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
85 unsigned &NOperandCycles);
86 void FormItineraryBypassString(const std::string &Names,
87 Record *ItinData,
88 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000089 void EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000090 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000091 &ProcItinLists);
92 void EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000093 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000094 &ProcItinLists);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000095 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
96 raw_ostream &OS);
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +000097 void EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
98 raw_ostream &OS);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000099 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
100 raw_ostream &OS);
Mehdi Amini32986ed2016-10-04 23:47:33 +0000101 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000102 char Separator);
Clement Courbet39911e22018-02-08 08:46:48 +0000103 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
104 raw_ostream &OS);
Andrew Trick23f3c652012-09-17 22:18:45 +0000105 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
106 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000107 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +0000108 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000109 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
110 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000111 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
112 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000113 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
114 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +0000115 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000116 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000117 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000118 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000119 void emitSchedModelHelpersImpl(raw_ostream &OS,
120 bool OnlyExpandMCInstPredicates = false);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +0000121 void emitGenMCSubtargetInfo(raw_ostream &OS);
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000122 void EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000123
Andrew Trick87255e32012-07-07 04:00:00 +0000124 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000125 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000126 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
127 unsigned NumProcs);
128
129public:
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000130 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
131 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
132 Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000133
134 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000135};
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000136
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000137} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000138
Jim Laskeya1beea62005-10-22 07:59:56 +0000139//
Jim Laskeya2b52352005-10-26 17:30:34 +0000140// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000141//
Craig Topper4f613082019-03-01 02:19:26 +0000142void SubtargetEmitter::Enumeration(raw_ostream &OS,
143 DenseMap<Record *, unsigned> &FeatureMap) {
Jim Laskey19595752005-10-28 15:20:43 +0000144 // Get all records of class and sort
Craig Topper094bbca2016-02-14 05:22:01 +0000145 std::vector<Record*> DefList =
146 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +0000147 llvm::sort(DefList, LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000148
Evan Chenga2e61292011-04-15 19:35:46 +0000149 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000150 if (N == 0)
151 return;
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)
Hal Finkel8db55472012-06-22 20:27:13 +0000399 OS << " const unsigned " << FUs[j]->getName()
400 << " = 1 << " << 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)
463 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000464
David Goodwind813cbf2009-08-17 16:02:57 +0000465 // Get string and operand cycle count
466 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000467 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000468 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000469 if (ItinData) {
470 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
471 NOperandCycles);
472
473 FormItineraryBypassString(Name, ItinData, ItinBypassString,
474 NOperandCycles);
475 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000476
David Goodwind813cbf2009-08-17 16:02:57 +0000477 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000478 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000479 if (NStages > 0) {
480 FindStage = ItinStageMap[ItinStageString];
481 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000482 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
483 StageTable += ItinStageString + ", // " + itostr(StageCount);
484 if (NStages > 1)
485 StageTable += "-" + itostr(StageCount + NStages - 1);
486 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000487 // Record Itin class number.
488 ItinStageMap[ItinStageString] = FindStage = StageCount;
489 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000490 }
491 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000492
David Goodwind813cbf2009-08-17 16:02:57 +0000493 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000494 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000495 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000496 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
497 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000498 if (FindOperandCycle == 0) {
499 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000500 OperandCycleTable += ItinOperandCycleString + ", // ";
501 std::string OperandIdxComment = itostr(OperandCycleCount);
502 if (NOperandCycles > 1)
503 OperandIdxComment += "-"
504 + itostr(OperandCycleCount + NOperandCycles - 1);
505 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000506 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000507 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000508 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000509 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000510 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000511 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000512 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000513 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000514
Evan Cheng367a5df2010-09-09 18:18:55 +0000515 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000516 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
517 InstrItinerary Intinerary = {
518 NumUOps,
519 FindStage,
520 uint16_t(FindStage + NStages),
521 FindOperandCycle,
522 uint16_t(FindOperandCycle + NOperandCycles),
523 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000524
Jim Laskey19595752005-10-28 15:20:43 +0000525 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000526 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000527 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000528 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000529
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000530 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000531 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000532 StageTable += "};\n";
533
534 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000535 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000536 OperandCycleTable += "};\n";
537
Andrew Trick87255e32012-07-07 04:00:00 +0000538 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000539 BypassTable += "};\n";
540
David Goodwind813cbf2009-08-17 16:02:57 +0000541 // Emit tables.
542 OS << StageTable;
543 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000544 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000545}
546
Andrew Trick87255e32012-07-07 04:00:00 +0000547//
548// EmitProcessorData - Generate data for processor itineraries that were
549// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
550// Itineraries for each processor. The Itinerary lists are indexed on
551// CodeGenSchedClass::Index.
552//
553void SubtargetEmitter::
554EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000555 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000556 // Multiple processor models may share an itinerary record. Emit it once.
557 SmallPtrSet<Record*, 8> ItinsDefSet;
558
Andrew Trick87255e32012-07-07 04:00:00 +0000559 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000560 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000561 ProcItinListsIter = ProcItinLists.begin();
562 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000563 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000564
Andrew Trick87255e32012-07-07 04:00:00 +0000565 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000566 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000567 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000568
Andrew Trick87255e32012-07-07 04:00:00 +0000569 // Get the itinerary list for the processor.
570 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000571 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000572
Pete Cooperc0eb1532014-09-02 23:23:34 +0000573 // Empty itineraries aren't referenced anywhere in the tablegen output
574 // so don't emit them.
575 if (ItinList.empty())
576 continue;
577
Andrew Trick87255e32012-07-07 04:00:00 +0000578 OS << "\n";
579 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000580
581 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000582 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000583
584 // For each itinerary class in CodeGenSchedClass::Index order.
585 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
586 InstrItinerary &Intinerary = ItinList[j];
587
588 // Emit Itinerary in the form of
589 // { firstStage, lastStage, firstCycle, lastCycle } // index
590 OS << " { " <<
591 Intinerary.NumMicroOps << ", " <<
592 Intinerary.FirstStage << ", " <<
593 Intinerary.LastStage << ", " <<
594 Intinerary.FirstOperandCycle << ", " <<
595 Intinerary.LastOperandCycle << " }" <<
596 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
597 }
598 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000599 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
600 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000601 OS << "};\n";
602 }
603}
604
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000605// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000606// value defined in the C++ header. The Record is null if the processor does not
607// define a model.
608void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000609 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000610 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000611 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000612 if (V >= 0)
613 OS << V << Separator << " // " << Name;
614 else
Andrew Trick87255e32012-07-07 04:00:00 +0000615 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000616 OS << '\n';
617}
618
Clement Courbet39911e22018-02-08 08:46:48 +0000619void SubtargetEmitter::EmitProcessorResourceSubUnits(
620 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
621 OS << "\nstatic const unsigned " << ProcModel.ModelName
622 << "ProcResourceSubUnits[] = {\n"
623 << " 0, // Invalid\n";
624
625 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
626 Record *PRDef = ProcModel.ProcResourceDefs[i];
627 if (!PRDef->isSubClassOf("ProcResGroup"))
628 continue;
629 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
630 for (Record *RUDef : ResUnits) {
631 Record *const RU =
632 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
633 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
634 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
635 }
636 }
637 OS << " // " << PRDef->getName() << "\n";
638 }
639 OS << "};\n";
640}
641
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000642static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
643 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000644 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000645 if (Record *RCU = ProcModel.RetireControlUnit) {
646 ReorderBufferSize =
647 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
648 MaxRetirePerCycle =
649 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
650 }
651
652 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
653 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
654}
655
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000656static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
657 unsigned NumRegisterFiles,
658 unsigned NumCostEntries, raw_ostream &OS) {
659 if (NumRegisterFiles)
660 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
661 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000662 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000663
664 OS << ", // Number of register files.\n ";
665 if (NumCostEntries)
666 OS << ProcModel.ModelName << "RegisterCosts,\n ";
667 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000668 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000669 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000670}
671
672unsigned
673SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
674 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000675 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
676 return RF.hasDefaultCosts();
677 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000678 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000679
680 // Print the RegisterCost table first.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000681 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000682 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
683 << "RegisterCosts"
684 << "[] = {\n";
685
686 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
687 // Skip register files with a default cost table.
688 if (RF.hasDefaultCosts())
689 continue;
690 // Add entries to the cost table.
691 for (const CodeGenRegisterCost &RC : RF.Costs) {
692 OS << " { ";
693 Record *Rec = RC.RCDef;
694 if (Rec->getValue("Namespace"))
695 OS << Rec->getValueAsString("Namespace") << "::";
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000696 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
697 << RC.AllowMoveElimination << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000698 }
699 }
700 OS << "};\n";
701
702 // Now generate a table with register file info.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000703 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
704 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000705 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
706 << "RegisterFiles"
707 << "[] = {\n"
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000708 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000709 unsigned CostTblIndex = 0;
710
711 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
712 OS << " { ";
713 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
714 unsigned NumCostEntries = RD.Costs.size();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000715 OS << NumCostEntries << ", " << CostTblIndex << ", "
716 << RD.MaxMovesEliminatedPerCycle << ", "
717 << RD.AllowZeroMoveEliminationOnly << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000718 CostTblIndex += NumCostEntries;
719 }
720 OS << "};\n";
721
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000722 return CostTblIndex;
723}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000724
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000725void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
726 raw_ostream &OS) {
727 unsigned QueueID = 0;
728 if (ProcModel.LoadQueue) {
729 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
730 QueueID =
731 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
732 std::find(ProcModel.ProcResourceDefs.begin(),
733 ProcModel.ProcResourceDefs.end(), Queue));
734 }
735 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
736
737 QueueID = 0;
738 if (ProcModel.StoreQueue) {
739 const Record *Queue =
740 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
741 QueueID =
742 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
743 std::find(ProcModel.ProcResourceDefs.begin(),
744 ProcModel.ProcResourceDefs.end(), Queue));
745 }
746 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
747}
748
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000749void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
750 raw_ostream &OS) {
751 // Generate a table of register file descriptors (one entry per each user
752 // defined register file), and a table of register costs.
753 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
754
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000755 // Now generate a table for the extra processor info.
756 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000757 << "ExtraInfo = {\n ";
758
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000759 // Add information related to the retire control unit.
760 EmitRetireControlUnitInfo(ProcModel, OS);
761
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000762 // Add information related to the register files (i.e. where to find register
763 // file descriptors and register costs).
764 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
765 NumCostEntries, OS);
766
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000767 // Add information about load/store queues.
768 EmitLoadStoreQueueInfo(ProcModel, OS);
769
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000770 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000771}
772
Andrew Trick23f3c652012-09-17 22:18:45 +0000773void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
774 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000775 EmitProcessorResourceSubUnits(ProcModel, OS);
776
Jinsong Ji05941622018-09-18 15:38:56 +0000777 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000778 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
779 << "ProcResources"
780 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000781 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000782
Clement Courbet39911e22018-02-08 08:46:48 +0000783 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000784 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
785 Record *PRDef = ProcModel.ProcResourceDefs[i];
786
Craig Topper24064772014-04-15 07:20:03 +0000787 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000788 unsigned SuperIdx = 0;
789 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000790 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000791 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000792 if (PRDef->isSubClassOf("ProcResGroup")) {
793 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000794 for (Record *RU : ResUnits) {
795 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000796 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000797 }
798 }
799 else {
800 // Find the SuperIdx
801 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000802 SuperDef =
803 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
804 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000805 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
806 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000807 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000808 }
809 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000810 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000811 if (PRDef->getName().size() < 15)
812 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000813 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
814 if (SubUnitsBeginOffset != SubUnitsOffset) {
815 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
816 << SubUnitsBeginOffset;
817 } else {
818 OS << "nullptr";
819 }
820 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000821 if (SuperDef)
822 OS << ", Super=" << SuperDef->getName();
823 OS << "\n";
824 }
825 OS << "};\n";
826}
827
Andrew Trick9ef08822012-09-17 22:18:48 +0000828// Find the WriteRes Record that defines processor resources for this
829// SchedWrite.
830Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000831 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000832
833 // Check if the SchedWrite is already subtarget-specific and directly
834 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000835 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
836 return SchedWrite.TheDef;
837
Craig Topper24064772014-04-15 07:20:03 +0000838 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000839 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000840 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000841 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000842 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
843 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
844 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
845 continue;
846 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000847 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000848 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000849 "defined for processor " + ProcModel.ModelName +
850 " Ensure only one SchedAlias exists per RW.");
851 AliasDef = AliasRW.TheDef;
852 }
853 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
854 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000855
856 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000857 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000858 for (Record *WR : ProcModel.WriteResDefs) {
859 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000860 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000861 if (AliasDef == WR->getValueAsDef("WriteType")
862 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000863 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000864 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000865 "SchedWrite and its alias on processor " +
866 ProcModel.ModelName);
867 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000868 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000869 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000870 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000871 // TODO: If ProcModel has a base model (previous generation processor),
872 // then call FindWriteResources recursively with that model here.
873 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000874 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000875 Twine("Processor does not define resources for ") +
876 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000877 }
878 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000879}
880
881/// Find the ReadAdvance record for the given SchedRead on this processor or
882/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000883Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000884 const CodeGenProcModel &ProcModel) {
885 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000886 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
887 return SchedRead.TheDef;
888
889 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000890 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000891 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000892 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000893 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000894 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
895 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
896 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
897 continue;
898 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000899 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000900 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000901 "defined for processor " + ProcModel.ModelName +
902 " Ensure only one SchedAlias exists per RW.");
903 AliasDef = AliasRW.TheDef;
904 }
905 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
906 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000907
908 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000909 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000910 for (Record *RA : ProcModel.ReadAdvanceDefs) {
911 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000912 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000913 if (AliasDef == RA->getValueAsDef("ReadType")
914 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000915 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000916 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000917 "SchedRead and its alias on processor " +
918 ProcModel.ModelName);
919 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000920 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000921 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000922 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000923 // TODO: If ProcModel has a base model (previous generation processor),
924 // then call FindReadAdvance recursively with that model here.
925 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000926 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000927 Twine("Processor does not define resources for ") +
928 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000929 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000930 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000931}
932
Andrew Trick4e67cba2013-03-14 21:21:50 +0000933// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000934// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000935void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
936 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000937 const CodeGenProcModel &PM) {
Clement Courbet5eeed772018-06-13 09:41:49 +0000938 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000939 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000940 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000941 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000942 if (PRDef->isSubClassOf("ProcResGroup"))
943 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000944 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000945 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000946 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000947 for (Record *SubDef = PRDef;
948 SubDef->getValueInit("Super")->isComplete();) {
949 if (SubDef->isSubClassOf("ProcResGroup")) {
950 // Disallow this for simplicitly.
951 PrintFatalError(SubDef->getLoc(), "Processor resource group "
952 " cannot be a super resources.");
953 }
954 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000955 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
956 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000957 PRVec.push_back(SuperDef);
958 Cycles.push_back(Cycles[i]);
959 SubDef = SuperDef;
960 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000961 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000962 for (Record *PR : PM.ProcResourceDefs) {
963 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000964 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000965 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000966 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000967 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000968 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000969 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000970 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000971 }
972 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000973 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000974 Cycles.push_back(Cycles[i]);
975 }
976 }
977 }
978}
979
Andrew Trick9ef08822012-09-17 22:18:48 +0000980// Generate the SchedClass table for this processor and update global
981// tables. Must be called for each processor in order.
982void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
983 SchedClassTables &SchedTables) {
984 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
985 if (!ProcModel.hasInstrSchedModel())
986 return;
987
988 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000989 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000990 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000991 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000992
Andrew Trick9ef08822012-09-17 22:18:48 +0000993 SCTab.resize(SCTab.size() + 1);
994 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000995 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000996 SCDesc.NumMicroOps = 0;
997 SCDesc.BeginGroup = false;
998 SCDesc.EndGroup = false;
999 SCDesc.WriteProcResIdx = 0;
1000 SCDesc.WriteLatencyIdx = 0;
1001 SCDesc.ReadAdvanceIdx = 0;
1002
1003 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +00001004 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +00001005 for (const CodeGenSchedTransition &CGT :
1006 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1007 if (CGT.ProcIndices[0] == 0 ||
1008 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +00001009 HasVariants = true;
1010 break;
1011 }
1012 }
1013 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001014 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1015 continue;
1016 }
1017
1018 // Determine if the SchedClass is actually reachable on this processor. If
1019 // not don't try to locate the processor resources, it will fail.
1020 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001021 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1022 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +00001023 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001024 continue;
1025 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001026 IdxVec Writes = SC.Writes;
1027 IdxVec Reads = SC.Reads;
1028 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001029 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001030 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001031 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001032 for (Record *RW : SC.InstRWs) {
1033 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001034 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001035 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001036 break;
1037 }
1038 }
1039 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001040 Writes.clear();
1041 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001042 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1043 Writes, Reads);
1044 }
1045 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001046 if (Writes.empty()) {
1047 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001048 for (Record *I : ProcModel.ItinRWDefs) {
1049 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001050 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001051 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001052 Writes, Reads);
1053 break;
1054 }
1055 }
1056 if (Writes.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001057 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1058 << " does not have resources for class " << SC.Name
1059 << '\n');
QingShan Zhangbb8d5402019-10-11 08:36:54 +00001060 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001061 }
1062 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001063 // Sum resources across all operand writes.
1064 std::vector<MCWriteProcResEntry> WriteProcResources;
1065 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001066 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001067 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001068 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001069 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001070 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001071 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001072
1073 // For each operand, create a latency entry.
1074 MCWriteLatencyEntry WLEntry;
1075 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001076 unsigned WriteID = WriteSeq.back();
1077 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1078 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1079 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001080 if (!SchedModels.hasReadOfWrite(
1081 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001082 WriteID = 0;
1083 }
1084 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001085
Craig Topper29c55dcb2016-02-13 06:03:32 +00001086 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001087
Andrew Trick9257b8f2012-09-22 02:24:21 +00001088 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001089 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001090
1091 // Mark the parent class as invalid for unsupported write types.
1092 if (WriteRes->getValueAsBit("Unsupported")) {
1093 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1094 break;
1095 }
1096 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1097 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1098 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1099 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001100 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1101 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001102
1103 // Create an entry for each ProcResource listed in WriteRes.
1104 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1105 std::vector<int64_t> Cycles =
1106 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001107
Clement Courbet5eeed772018-06-13 09:41:49 +00001108 if (Cycles.empty()) {
1109 // If ResourceCycles is not provided, default to one cycle per
1110 // resource.
1111 Cycles.resize(PRVec.size(), 1);
1112 } else if (Cycles.size() != PRVec.size()) {
1113 // If ResourceCycles is provided, check consistency.
1114 PrintFatalError(
1115 WriteRes->getLoc(),
1116 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1117 "!size(ProcResources): ")
1118 .concat(Twine(PRVec.size()))
1119 .concat(" vs ")
1120 .concat(Twine(Cycles.size())));
1121 }
1122
Andrew Trick4e67cba2013-03-14 21:21:50 +00001123 ExpandProcResources(PRVec, Cycles, ProcModel);
1124
Andrew Trick9ef08822012-09-17 22:18:48 +00001125 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1126 PRIdx != PREnd; ++PRIdx) {
1127 MCWriteProcResEntry WPREntry;
1128 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1129 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001130 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001131 // If this resource is already used in this sequence, add the current
1132 // entry's cycles so that the same resource appears to be used
1133 // serially, rather than multiple parallel uses. This is important for
1134 // in-order machine where the resource consumption is a hazard.
1135 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1136 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1137 if (WriteProcResources[WPRIdx].ProcResourceIdx
1138 == WPREntry.ProcResourceIdx) {
1139 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1140 break;
1141 }
1142 }
1143 if (WPRIdx == WPREnd)
1144 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001145 }
1146 }
1147 WriteLatencies.push_back(WLEntry);
1148 }
1149 // Create an entry for each operand Read in this SchedClass.
1150 // Entries must be sorted first by UseIdx then by WriteResourceID.
1151 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1152 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001153 Record *ReadAdvance =
1154 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001155 if (!ReadAdvance)
1156 continue;
1157
1158 // Mark the parent class as invalid for unsupported write types.
1159 if (ReadAdvance->getValueAsBit("Unsupported")) {
1160 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1161 break;
1162 }
1163 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1164 IdxVec WriteIDs;
1165 if (ValidWrites.empty())
1166 WriteIDs.push_back(0);
1167 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001168 for (Record *VW : ValidWrites) {
1169 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001170 }
1171 }
Fangrui Song0cac7262018-09-27 02:13:45 +00001172 llvm::sort(WriteIDs);
Craig Topper29c55dcb2016-02-13 06:03:32 +00001173 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001174 MCReadAdvanceEntry RAEntry;
1175 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001176 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001177 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1178 ReadAdvanceEntries.push_back(RAEntry);
1179 }
1180 }
1181 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1182 WriteProcResources.clear();
1183 WriteLatencies.clear();
1184 ReadAdvanceEntries.clear();
1185 }
1186 // Add the information for this SchedClass to the global tables using basic
1187 // compression.
1188 //
1189 // WritePrecRes entries are sorted by ProcResIdx.
Fangrui Song0cac7262018-09-27 02:13:45 +00001190 llvm::sort(WriteProcResources, LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001191
1192 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1193 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1194 std::search(SchedTables.WriteProcResources.begin(),
1195 SchedTables.WriteProcResources.end(),
1196 WriteProcResources.begin(), WriteProcResources.end());
1197 if (WPRPos != SchedTables.WriteProcResources.end())
1198 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1199 else {
1200 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1201 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1202 WriteProcResources.end());
1203 }
1204 // Latency entries must remain in operand order.
1205 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1206 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1207 std::search(SchedTables.WriteLatencies.begin(),
1208 SchedTables.WriteLatencies.end(),
1209 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001210 if (WLPos != SchedTables.WriteLatencies.end()) {
1211 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1212 SCDesc.WriteLatencyIdx = idx;
1213 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1214 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1215 std::string::npos) {
1216 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1217 }
1218 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001219 else {
1220 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001221 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1222 WriteLatencies.begin(),
1223 WriteLatencies.end());
1224 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1225 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001226 }
1227 // ReadAdvanceEntries must remain in operand order.
1228 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1229 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1230 std::search(SchedTables.ReadAdvanceEntries.begin(),
1231 SchedTables.ReadAdvanceEntries.end(),
1232 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1233 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1234 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1235 else {
1236 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1237 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1238 ReadAdvanceEntries.end());
1239 }
1240 }
1241}
1242
Andrew Tricka72fca62012-09-17 22:18:50 +00001243// Emit SchedClass tables for all processors and associated global tables.
1244void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1245 raw_ostream &OS) {
1246 // Emit global WriteProcResTable.
1247 OS << "\n// {ProcResourceIdx, Cycles}\n"
1248 << "extern const llvm::MCWriteProcResEntry "
1249 << Target << "WriteProcResTable[] = {\n"
1250 << " { 0, 0}, // Invalid\n";
1251 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1252 WPRIdx != WPREnd; ++WPRIdx) {
1253 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1254 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1255 << format("%2d", WPREntry.Cycles) << "}";
1256 if (WPRIdx + 1 < WPREnd)
1257 OS << ',';
1258 OS << " // #" << WPRIdx << '\n';
1259 }
1260 OS << "}; // " << Target << "WriteProcResTable\n";
1261
1262 // Emit global WriteLatencyTable.
1263 OS << "\n// {Cycles, WriteResourceID}\n"
1264 << "extern const llvm::MCWriteLatencyEntry "
1265 << Target << "WriteLatencyTable[] = {\n"
1266 << " { 0, 0}, // Invalid\n";
1267 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1268 WLIdx != WLEnd; ++WLIdx) {
1269 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1270 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1271 << format("%2d", WLEntry.WriteResourceID) << "}";
1272 if (WLIdx + 1 < WLEnd)
1273 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001274 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001275 }
1276 OS << "}; // " << Target << "WriteLatencyTable\n";
1277
1278 // Emit global ReadAdvanceTable.
1279 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1280 << "extern const llvm::MCReadAdvanceEntry "
1281 << Target << "ReadAdvanceTable[] = {\n"
1282 << " {0, 0, 0}, // Invalid\n";
1283 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1284 RAIdx != RAEnd; ++RAIdx) {
1285 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1286 OS << " {" << RAEntry.UseIdx << ", "
1287 << format("%2d", RAEntry.WriteResourceID) << ", "
1288 << format("%2d", RAEntry.Cycles) << "}";
1289 if (RAIdx + 1 < RAEnd)
1290 OS << ',';
1291 OS << " // #" << RAIdx << '\n';
1292 }
1293 OS << "}; // " << Target << "ReadAdvanceTable\n";
1294
1295 // Emit a SchedClass table for each processor.
1296 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1297 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1298 if (!PI->hasInstrSchedModel())
1299 continue;
1300
1301 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001302 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001303
1304 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1305 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1306 OS << "static const llvm::MCSchedClassDesc "
1307 << PI->ModelName << "SchedClasses[] = {\n";
1308
1309 // The first class is always invalid. We no way to distinguish it except by
1310 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001311 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001312 && "invalid class not first");
1313 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1314 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001315 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001316
1317 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1318 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1319 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1320 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1321 if (SchedClass.Name.size() < 18)
1322 OS.indent(18 - SchedClass.Name.size());
1323 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001324 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1325 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001326 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1327 << ", " << MCDesc.NumWriteProcResEntries
1328 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1329 << ", " << MCDesc.NumWriteLatencyEntries
1330 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001331 << ", " << MCDesc.NumReadAdvanceEntries
1332 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001333 }
1334 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1335 }
1336}
1337
Andrew Trick87255e32012-07-07 04:00:00 +00001338void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1339 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001340 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001341 // Emit extra processor info if available.
1342 if (PM.hasExtraProcessorInfo())
1343 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001344 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001345 if (PM.hasInstrSchedModel())
1346 EmitProcessorResources(PM, OS);
1347 else if(!PM.ProcResourceDefs.empty())
1348 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001349 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001350
Andrew Trick73d77362012-06-05 03:44:40 +00001351 // Begin processor itinerary properties
1352 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001353 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1354 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1355 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1356 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1357 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1358 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1359 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001360
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001361 bool PostRAScheduler =
1362 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001363
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001364 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1365 << "PostRAScheduler\n";
1366
1367 bool CompleteModel =
1368 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1369
1370 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1371 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001372
Craig Topper29c55dcb2016-02-13 06:03:32 +00001373 OS << " " << PM.Index << ", // Processor ID\n";
1374 if (PM.hasInstrSchedModel())
1375 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1376 << " " << PM.ModelName << "SchedClasses" << ",\n"
1377 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001378 << " " << (SchedModels.schedClassEnd()
1379 - SchedModels.schedClassBegin()) << ",\n";
1380 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001381 OS << " nullptr, nullptr, 0, 0,"
1382 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001383 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001384 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001385 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001386 OS << " nullptr, // No Itinerary\n";
1387 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001388 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001389 else
Clement Courbetb4493792018-04-10 08:16:37 +00001390 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001391 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001392 }
Jim Laskey3763a502005-10-31 17:16:01 +00001393}
1394
1395//
Andrew Trick87255e32012-07-07 04:00:00 +00001396// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001397//
Andrew Trick87255e32012-07-07 04:00:00 +00001398void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001399 OS << "#ifdef DBGFIELD\n"
1400 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1401 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001402 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001403 << "#define DBGFIELD(x) x,\n"
1404 << "#else\n"
1405 << "#define DBGFIELD(x)\n"
1406 << "#endif\n";
1407
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001408 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001409 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001410 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001411 EmitStageAndOperandCycleData(OS, ProcItinLists);
1412 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001413 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001414 OS << "\n// ===============================================================\n"
1415 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001416
Andrew Trick9ef08822012-09-17 22:18:48 +00001417 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001418 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1419 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001420 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001421 EmitSchedClassTables(SchedTables, OS);
1422
Craig Topper2982b842019-03-05 18:54:38 +00001423 OS << "\n#undef DBGFIELD\n";
1424
Andrew Tricka72fca62012-09-17 22:18:50 +00001425 // Emit the processor machine model
1426 EmitProcessorModels(OS);
Jim Laskey86f002c2005-10-27 19:47:21 +00001427}
1428
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001429static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1430 std::string Buffer;
1431 raw_string_ostream Stream(Buffer);
1432
1433 // Collect all the PredicateProlog records and print them to the output
1434 // stream.
1435 std::vector<Record *> Prologs =
1436 Records.getAllDerivedDefinitions("PredicateProlog");
Fangrui Song0cac7262018-09-27 02:13:45 +00001437 llvm::sort(Prologs, LessRecord());
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001438 for (Record *P : Prologs)
1439 Stream << P->getValueAsString("Code") << '\n';
1440
1441 Stream.flush();
1442 OS << Buffer;
1443}
1444
1445static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001446 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001447 raw_ostream &OS) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001448 std::string Buffer;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001449 raw_string_ostream SS(Buffer);
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001450
1451 auto IsTruePredicate = [](const Record *Rec) {
1452 return Rec->isSubClassOf("MCSchedPredicate") &&
1453 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1454 };
1455
1456 // If not all predicates are MCTrue, then we need an if-stmt.
1457 unsigned NumNonTruePreds =
1458 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001459
1460 SS.indent(PE.getIndentLevel() * 2);
1461
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001462 if (NumNonTruePreds) {
1463 bool FirstNonTruePredicate = true;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001464 SS << "if (";
1465
1466 PE.setIndentLevel(PE.getIndentLevel() + 2);
1467
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001468 for (const Record *Rec : T.PredTerm) {
1469 // Skip predicates that evaluate to "true".
1470 if (IsTruePredicate(Rec))
1471 continue;
1472
1473 if (FirstNonTruePredicate) {
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001474 FirstNonTruePredicate = false;
1475 } else {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001476 SS << "\n";
1477 SS.indent(PE.getIndentLevel() * 2);
1478 SS << "&& ";
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001479 }
1480
1481 if (Rec->isSubClassOf("MCSchedPredicate")) {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001482 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001483 continue;
1484 }
1485
1486 // Expand this legacy predicate and wrap it around braces if there is more
1487 // than one predicate to expand.
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001488 SS << ((NumNonTruePreds > 1) ? "(" : "")
1489 << Rec->getValueAsString("Predicate")
1490 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagio95140022018-05-25 15:55:37 +00001491 }
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001492
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001493 SS << ")\n"; // end of if-stmt
1494 PE.decreaseIndentLevel();
1495 SS.indent(PE.getIndentLevel() * 2);
1496 PE.decreaseIndentLevel();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001497 }
1498
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001499 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1500 SS.flush();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001501 OS << Buffer;
1502}
1503
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001504// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1505// epilogue code for the auto-generated helper.
1506void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1507 if (ShouldReturnZero) {
Andrea Di Biagio95140022018-05-25 15:55:37 +00001508 OS << " // Don't know how to resolve this scheduling class.\n"
1509 << " return 0;\n";
1510 return;
1511 }
1512
1513 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1514}
1515
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001516bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1517 return all_of(T.PredTerm, [](const Record *Rec) {
1518 return Rec->isSubClassOf("MCSchedPredicate");
1519 });
1520}
1521
1522void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1523 IdxVec &VariantClasses,
1524 bool OnlyExpandMCInstPredicates) {
1525 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1526 // Ignore non-variant scheduling classes.
1527 if (SC.Transitions.empty())
1528 continue;
1529
1530 if (OnlyExpandMCInstPredicates) {
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001531 // Ignore this variant scheduling class no transitions use any meaningful
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001532 // MCSchedPredicate definitions.
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001533 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001534 return hasMCSchedPredicates(T);
1535 }))
1536 continue;
1537 }
1538
1539 VariantClasses.push_back(SC.Index);
1540 }
1541}
1542
1543void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1544 // A variant scheduling class may define transitions for multiple
1545 // processors. This function identifies wich processors are associated with
1546 // transition rules specified by variant class `SC`.
1547 for (const CodeGenSchedTransition &T : SC.Transitions) {
1548 IdxVec PI;
1549 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1550 ProcIndices.begin(), ProcIndices.end(),
1551 std::back_inserter(PI));
1552 ProcIndices.swap(PI);
1553 }
1554}
1555
1556void SubtargetEmitter::emitSchedModelHelpersImpl(
1557 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1558 IdxVec VariantClasses;
1559 collectVariantClasses(SchedModels, VariantClasses,
1560 OnlyExpandMCInstPredicates);
1561
1562 if (VariantClasses.empty()) {
1563 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1564 return;
1565 }
1566
1567 // Construct a switch statement where the condition is a check on the
1568 // scheduling class identifier. There is a `case` for every variant class
1569 // defined by the processor models of this target.
1570 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1571 // a variant scheduling class to another scheduling class. Rules are
1572 // described by instances of CodeGenSchedTransition. Note that transitions may
1573 // not be valid for all processors.
1574 OS << " switch (SchedClass) {\n";
1575 for (unsigned VC : VariantClasses) {
1576 IdxVec ProcIndices;
1577 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1578 collectProcessorIndices(SC, ProcIndices);
1579
1580 OS << " case " << VC << ": // " << SC.Name << '\n';
1581
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +00001582 PredicateExpander PE(Target);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001583 PE.setByRef(false);
1584 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1585 for (unsigned PI : ProcIndices) {
1586 OS << " ";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001587
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001588 // Emit a guard on the processor ID.
1589 if (PI != 0) {
1590 OS << (OnlyExpandMCInstPredicates
1591 ? "if (CPUID == "
1592 : "if (SchedModel->getProcessorID() == ");
1593 OS << PI << ") ";
1594 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1595 }
1596
1597 // Now emit transitions associated with processor PI.
1598 for (const CodeGenSchedTransition &T : SC.Transitions) {
1599 if (PI != 0 && !count(T.ProcIndices, PI))
1600 continue;
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001601
1602 // Emit only transitions based on MCSchedPredicate, if it's the case.
1603 // At least the transition specified by NoSchedPred is emitted,
1604 // which becomes the default transition for those variants otherwise
1605 // not based on MCSchedPredicate.
1606 // FIXME: preferably, llvm-mca should instead assume a reasonable
1607 // default when a variant transition is not based on MCSchedPredicate
1608 // for a given processor.
1609 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1610 continue;
1611
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001612 PE.setIndentLevel(3);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001613 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1614 }
1615
1616 OS << " }\n";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001617
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001618 if (PI == 0)
1619 break;
1620 }
1621
1622 if (SC.isInferred())
1623 OS << " return " << SC.Index << ";\n";
1624 OS << " break;\n";
1625 }
1626
1627 OS << " };\n";
1628
1629 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1630}
1631
Andrea Di Biagio95140022018-05-25 15:55:37 +00001632void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1633 raw_ostream &OS) {
1634 OS << "unsigned " << ClassName
1635 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1636 << " const TargetSchedModel *SchedModel) const {\n";
1637
1638 // Emit the predicate prolog code.
1639 emitPredicateProlog(Records, OS);
1640
1641 // Emit target predicates.
1642 emitSchedModelHelpersImpl(OS);
Clement Courbet41c8af32018-10-25 07:44:01 +00001643
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001644 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagio95140022018-05-25 15:55:37 +00001645
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001646 OS << "unsigned " << ClassName
1647 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1648 << " unsigned CPUID) const {\n"
1649 << " return " << Target << "_MC"
1650 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001651 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1652
1653 STIPredicateExpander PE(Target);
1654 PE.setClassPrefix(ClassName);
1655 PE.setExpandDefinition(true);
1656 PE.setByRef(false);
1657 PE.setIndentLevel(0);
1658
1659 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1660 PE.expandSTIPredicate(OS, Fn);
Andrew Trickc6c88152012-09-18 03:41:43 +00001661}
1662
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001663void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1664 raw_ostream &OS) {
1665 const CodeGenHwModes &CGH = TGT.getHwModes();
1666 assert(CGH.getNumModeIds() > 0);
1667 if (CGH.getNumModeIds() == 1)
1668 return;
1669
1670 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1671 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1672 const HwMode &HM = CGH.getMode(M);
1673 OS << " if (checkFeatures(\"" << HM.Features
1674 << "\")) return " << M << ";\n";
1675 }
1676 OS << " return 0;\n}\n";
1677}
1678
Jim Laskey86f002c2005-10-27 19:47:21 +00001679//
Jim Laskeya2b52352005-10-26 17:30:34 +00001680// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1681// the subtarget features string.
1682//
Evan Cheng54b68e32011-07-01 20:45:01 +00001683void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1684 unsigned NumFeatures,
1685 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001686 std::vector<Record*> Features =
1687 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +00001688 llvm::sort(Features, LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001689
Andrew Trickdb6ed642011-04-01 01:56:55 +00001690 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1691 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001692 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001693 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001694 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001695 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1696 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001697
1698 if (Features.empty()) {
1699 OS << "}\n";
1700 return;
1701 }
1702
Andrew Trickba7b9212012-09-18 05:33:15 +00001703 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001704 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001705
Craig Topper29c55dcb2016-02-13 06:03:32 +00001706 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001707 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001708 StringRef Instance = R->getName();
1709 StringRef Value = R->getValueAsString("Value");
1710 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001711
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001712 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001713 OS << " if (Bits[" << Target << "::"
1714 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001715 << Attribute << " = " << Value << ";\n";
1716 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001717 OS << " if (Bits[" << Target << "::"
1718 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001719 << Attribute << " < " << Value << ") "
1720 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001721 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001722
Evan Chengfe6e4052011-06-30 01:53:36 +00001723 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001724}
1725
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001726void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001727 OS << "namespace " << Target << "_MC {\n"
1728 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1729 << " const MCInst *MI, unsigned CPUID) {\n";
1730 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1731 OS << "}\n";
Bjorn Pettersson6bd3a9e2019-08-25 10:47:30 +00001732 OS << "} // end namespace " << Target << "_MC\n\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001733
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001734 OS << "struct " << Target
1735 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1736 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1737 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
Craig Topperca268082019-03-05 18:54:34 +00001738 << " ArrayRef<SubtargetSubTypeKV> PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001739 << " const MCWriteProcResEntry *WPR,\n"
1740 << " const MCWriteLatencyEntry *WL,\n"
1741 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1742 << " const unsigned *OC, const unsigned *FP) :\n"
Craig Topper2982b842019-03-05 18:54:38 +00001743 << " MCSubtargetInfo(TT, CPU, FS, PF, PD,\n"
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001744 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1745 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001746 << " const MCInst *MI, unsigned CPUID) const override {\n"
1747 << " return " << Target << "_MC"
1748 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001749 OS << " }\n";
James Molloy88a5fbf2019-09-19 13:39:54 +00001750 if (TGT.getHwModes().getNumModeIds() > 1)
1751 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001752 OS << "};\n";
James Molloy88a5fbf2019-09-19 13:39:54 +00001753 EmitHwModeCheck(Target + "GenMCSubtargetInfo", OS);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001754}
1755
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001756void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1757 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1758 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1759
1760 STIPredicateExpander PE(Target);
1761 PE.setExpandForMC(true);
1762 PE.setByRef(true);
1763 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1764 PE.expandSTIPredicate(OS, Fn);
1765
1766 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1767
1768 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1769 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1770
1771 std::string ClassPrefix = Target + "MCInstrAnalysis";
1772 PE.setExpandDefinition(true);
1773 PE.setClassPrefix(ClassPrefix);
1774 PE.setIndentLevel(0);
1775 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1776 PE.expandSTIPredicate(OS, Fn);
1777
1778 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1779}
1780
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001781//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001782// SubtargetEmitter::run - Main subtarget enumeration emitter.
1783//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001784void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001785 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001786
Evan Cheng4d1ca962011-07-08 01:53:10 +00001787 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001788 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001789
Craig Topper4f613082019-03-01 02:19:26 +00001790 DenseMap<Record *, unsigned> FeatureMap;
1791
Evan Cheng4d1ca962011-07-08 01:53:10 +00001792 OS << "namespace llvm {\n";
Craig Topper4f613082019-03-01 02:19:26 +00001793 Enumeration(OS, FeatureMap);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001794 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001795 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1796
Evan Cheng54b68e32011-07-01 20:45:01 +00001797 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001798 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001799
Evan Cheng54b68e32011-07-01 20:45:01 +00001800 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001801#if 0
1802 OS << "namespace {\n";
1803#endif
Craig Topper4f613082019-03-01 02:19:26 +00001804 unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap);
Evan Chengbc153d42011-07-14 20:59:42 +00001805 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001806 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001807 OS << "\n";
Craig Topper2982b842019-03-05 18:54:38 +00001808 unsigned NumProcs = CPUKeyValues(OS, FeatureMap);
1809 OS << "\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001810#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001811 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001812#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001813
1814 // MCInstrInfo initialization routine.
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001815 emitGenMCSubtargetInfo(OS);
1816
Craig Topper194cb742017-10-24 15:50:55 +00001817 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001818 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001819 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001820 OS << " return new " << Target << "GenMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001821 if (NumFeatures)
1822 OS << Target << "FeatureKV, ";
1823 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001824 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001825 if (NumProcs)
1826 OS << Target << "SubTypeKV, ";
1827 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001828 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001829 OS << '\n'; OS.indent(22);
Craig Topper2982b842019-03-05 18:54:38 +00001830 OS << Target << "WriteProcResTable, "
Andrew Trickab722bd2012-09-18 03:18:56 +00001831 << Target << "WriteLatencyTable, "
1832 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001833 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001834 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001835 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001836 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001837 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001838 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001839 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001840 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001841
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001842 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001843
1844 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1845
1846 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001847 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001848
1849 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001850 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001851 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1852
1853 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1854
Evan Cheng0d639a22011-07-01 21:01:15 +00001855 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001856 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001857 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001858
1859 std::string ClassName = Target + "GenSubtargetInfo";
1860 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001861 OS << "class DFAPacketizer;\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001862 OS << "namespace " << Target << "_MC {\n"
1863 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1864 << " const MCInst *MI, unsigned CPUID);\n"
Bjorn Pettersson6bd3a9e2019-08-25 10:47:30 +00001865 << "} // end namespace " << Target << "_MC\n\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001866 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001867 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001868 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001869 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001870 << " unsigned resolveSchedClass(unsigned SchedClass, "
1871 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001872 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001873 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1874 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001875 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001876 << " const;\n";
1877 if (TGT.getHwModes().getNumModeIds() > 1)
1878 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001879
1880 STIPredicateExpander PE(Target);
1881 PE.setByRef(false);
1882 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1883 PE.expandSTIPredicate(OS, Fn);
1884
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001885 OS << "};\n"
1886 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001887
1888 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1889
1890 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001891 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001892
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001893 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001894 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001895 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
Craig Topperca268082019-03-05 18:54:34 +00001896 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001897 OS << "extern const llvm::MCWriteProcResEntry "
1898 << Target << "WriteProcResTable[];\n";
1899 OS << "extern const llvm::MCWriteLatencyEntry "
1900 << Target << "WriteLatencyTable[];\n";
1901 OS << "extern const llvm::MCReadAdvanceEntry "
1902 << Target << "ReadAdvanceTable[];\n";
1903
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001904 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001905 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1906 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001907 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001908 }
1909
Daniel Sanders50f17232015-09-15 16:17:27 +00001910 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1911 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001912 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001913 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001914 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001915 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001916 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001917 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001918 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001919 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001920 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001921 OS << '\n'; OS.indent(24);
Craig Topper2982b842019-03-05 18:54:38 +00001922 OS << Target << "WriteProcResTable, "
Andrew Trickab722bd2012-09-18 03:18:56 +00001923 << Target << "WriteLatencyTable, "
1924 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001925 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001926 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001927 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001928 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001929 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001930 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001931 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001932 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001933
Andrew Trickc6c88152012-09-18 03:41:43 +00001934 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001935 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001936
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001937 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001938
1939 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001940
1941 EmitMCInstrAnalysisPredicateFunctions(OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001942}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001943
1944namespace llvm {
1945
1946void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001947 CodeGenTarget CGTarget(RK);
1948 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001949}
1950
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001951} // end namespace llvm