blob: 792c957ea19c2fb4a82ee71c13b1396f0a148823 [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 Topper094bbca2016-02-14 05:22:01 +000076 void Enumeration(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000077 unsigned FeatureKeyValues(raw_ostream &OS);
78 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000079 void FormItineraryStageString(const std::string &Names,
80 Record *ItinData, std::string &ItinString,
81 unsigned &NStages);
82 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
83 unsigned &NOperandCycles);
84 void FormItineraryBypassString(const std::string &Names,
85 Record *ItinData,
86 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick87255e32012-07-07 04:00:00 +000087 void EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000088 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000089 &ProcItinLists);
90 void EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000091 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +000092 &ProcItinLists);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000093 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
94 raw_ostream &OS);
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +000095 void EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
96 raw_ostream &OS);
Andrea Di Biagio378d75a2018-04-04 11:53:13 +000097 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
98 raw_ostream &OS);
Mehdi Amini32986ed2016-10-04 23:47:33 +000099 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000100 char Separator);
Clement Courbet39911e22018-02-08 08:46:48 +0000101 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
102 raw_ostream &OS);
Andrew Trick23f3c652012-09-17 22:18:45 +0000103 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
104 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000105 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +0000106 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000107 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
108 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000109 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
110 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000111 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
112 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +0000113 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000114 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000115 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000116 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000117 void emitSchedModelHelpersImpl(raw_ostream &OS,
118 bool OnlyExpandMCInstPredicates = false);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +0000119 void emitGenMCSubtargetInfo(raw_ostream &OS);
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +0000120 void EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS);
Andrea Di Biagio95140022018-05-25 15:55:37 +0000121
Andrew Trick87255e32012-07-07 04:00:00 +0000122 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000123 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000124 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
125 unsigned NumProcs);
126
127public:
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000128 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
129 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
130 Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000131
132 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000133};
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000134
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000135} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000136
Jim Laskeya1beea62005-10-22 07:59:56 +0000137//
Jim Laskeya2b52352005-10-26 17:30:34 +0000138// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000139//
Craig Topper094bbca2016-02-14 05:22:01 +0000140void SubtargetEmitter::Enumeration(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000141 // Get all records of class and sort
Craig Topper094bbca2016-02-14 05:22:01 +0000142 std::vector<Record*> DefList =
143 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +0000144 llvm::sort(DefList, LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000145
Evan Chenga2e61292011-04-15 19:35:46 +0000146 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000147 if (N == 0)
148 return;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000149 if (N > MAX_SUBTARGET_FEATURES)
150 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chenga2e61292011-04-15 19:35:46 +0000151
Evan Cheng54b68e32011-07-01 20:45:01 +0000152 OS << "namespace " << Target << " {\n";
153
Craig Topperbcdb0f22016-02-13 17:58:14 +0000154 // Open enumeration.
Craig Topper2d45c1d2016-02-13 06:03:29 +0000155 OS << "enum {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000156
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000157 // For each record
Craig Topperdf1285b2017-10-24 15:50:53 +0000158 for (unsigned i = 0; i < N; ++i) {
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000159 // Next record
160 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000161
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000162 // Get and emit name
Craig Topperdf1285b2017-10-24 15:50:53 +0000163 OS << " " << Def->getName() << " = " << i << ",\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000164 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000165
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000166 // Close enumeration and namespace
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000167 OS << "};\n";
168 OS << "} // end namespace " << Target << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000169}
170
171//
Bill Wendlinge6182262007-05-04 20:38:40 +0000172// FeatureKeyValues - Emit data of all the subtarget features. Used by the
173// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000174//
Evan Cheng54b68e32011-07-01 20:45:01 +0000175unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000176 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000177 std::vector<Record*> FeatureList =
178 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000179
180 if (FeatureList.empty())
181 return 0;
182
Fangrui Song0cac7262018-09-27 02:13:45 +0000183 llvm::sort(FeatureList, LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000184
Jim Laskey19595752005-10-28 15:20:43 +0000185 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000186 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000187 << "extern const llvm::SubtargetFeatureKV " << Target
188 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000189
Jim Laskey19595752005-10-28 15:20:43 +0000190 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000191 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000192 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000193 // Next feature
194 Record *Feature = FeatureList[i];
195
Craig Topperbcd3c372017-05-31 21:12:46 +0000196 StringRef Name = Feature->getName();
197 StringRef CommandLineName = Feature->getValueAsString("Name");
198 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000199
Jim Laskey3f7d0472006-12-12 20:55:58 +0000200 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000201
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000202 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000203 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000204 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000205 << "\"" << Desc << "\", "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000206 << "{ " << Target << "::" << Name << " }, ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000207
Craig Topper37eeb322018-03-23 00:02:45 +0000208 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000209
Craig Topper4ceea0a2016-01-03 08:57:41 +0000210 OS << "{";
211 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
212 OS << " " << Target << "::" << ImpliesList[j]->getName();
213 if (++j < M) OS << ",";
Bill Wendlinge6182262007-05-04 20:38:40 +0000214 }
Craig Topperdf1285b2017-10-24 15:50:53 +0000215 OS << " } },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000216 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000217 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000218
Jim Laskey19595752005-10-28 15:20:43 +0000219 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000220 OS << "};\n";
221
Evan Cheng54b68e32011-07-01 20:45:01 +0000222 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000223}
224
225//
226// CPUKeyValues - Emit data of all the subtarget processors. Used by command
227// line.
228//
Evan Cheng54b68e32011-07-01 20:45:01 +0000229unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000230 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000231 std::vector<Record*> ProcessorList =
232 Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +0000233 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000234
Jim Laskey19595752005-10-28 15:20:43 +0000235 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000236 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000237 << "extern const llvm::SubtargetFeatureKV " << Target
238 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000239
Jim Laskey19595752005-10-28 15:20:43 +0000240 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000241 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000242 StringRef Name = Processor->getValueAsString("Name");
Craig Topper37eeb322018-03-23 00:02:45 +0000243 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000244
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000245 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000246 OS << " { "
247 << "\"" << Name << "\", "
248 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000249
Craig Topper4ceea0a2016-01-03 08:57:41 +0000250 OS << "{";
251 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
252 OS << " " << Target << "::" << FeatureList[j]->getName();
253 if (++j < M) OS << ",";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000254 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000255 // The { } is for the "implies" section of this data structure.
Craig Topperdf1285b2017-10-24 15:50:53 +0000256 OS << " }, { } },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000257 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000258
Jim Laskey19595752005-10-28 15:20:43 +0000259 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000260 OS << "};\n";
261
Evan Cheng54b68e32011-07-01 20:45:01 +0000262 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000263}
Jim Laskeya1beea62005-10-22 07:59:56 +0000264
Jim Laskeya2b52352005-10-26 17:30:34 +0000265//
David Goodwind813cbf2009-08-17 16:02:57 +0000266// FormItineraryStageString - Compose a string containing the stage
267// data initialization for the specified itinerary. N is the number
268// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000269//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000270void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
271 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000272 std::string &ItinString,
273 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000274 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000275 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000276
277 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000278 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000279 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000280 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000281 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000282
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000283 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000284 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000285 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000286
Jim Laskeydffe5972005-10-28 21:47:29 +0000287 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000288 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000289
Jim Laskey19595752005-10-28 15:20:43 +0000290 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000291 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000292 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000293 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000294 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000295 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000296
David Goodwinb369ee42009-08-12 18:31:53 +0000297 int TimeInc = Stage->getValueAsInt("TimeInc");
298 ItinString += ", " + itostr(TimeInc);
299
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000300 int Kind = Stage->getValueAsInt("Kind");
301 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
302
Jim Laskey19595752005-10-28 15:20:43 +0000303 // Close off stage
304 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000305 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000306 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000307}
308
309//
David Goodwind813cbf2009-08-17 16:02:57 +0000310// FormItineraryOperandCycleString - Compose a string containing the
311// operand cycle initialization for the specified itinerary. N is the
312// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000313//
David Goodwind813cbf2009-08-17 16:02:57 +0000314void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
315 std::string &ItinString, unsigned &NOperandCycles) {
316 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000317 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000318 ItinData->getValueAsListOfInts("OperandCycles");
319
320 // For each operand cycle
321 unsigned N = NOperandCycles = OperandCycleList.size();
322 for (unsigned i = 0; i < N;) {
323 // Next operand cycle
324 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000325
David Goodwind813cbf2009-08-17 16:02:57 +0000326 ItinString += " " + itostr(OCycle);
327 if (++i < N) ItinString += ", ";
328 }
329}
330
Evan Cheng0097dd02010-09-28 23:50:49 +0000331void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
332 Record *ItinData,
333 std::string &ItinString,
334 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000335 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000336 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000337 unsigned i = 0;
338 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000339 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000340 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000341 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000342 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000343 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000344 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000345 }
346}
347
David Goodwind813cbf2009-08-17 16:02:57 +0000348//
Andrew Trick87255e32012-07-07 04:00:00 +0000349// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
350// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
351// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000352//
Andrew Trick87255e32012-07-07 04:00:00 +0000353void SubtargetEmitter::
354EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000355 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000356 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000357 // Multiple processor models may share an itinerary record. Emit it once.
358 SmallPtrSet<Record*, 8> ItinsDefSet;
359
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000360 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000361 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000362
Craig Topper29c55dcb2016-02-13 06:03:32 +0000363 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000364 continue;
365
Craig Topper37eeb322018-03-23 00:02:45 +0000366 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000367 if (FUs.empty())
368 continue;
369
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000370 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000371 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000372 << "namespace " << Name << "FU {\n";
373
374 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000375 OS << " const unsigned " << FUs[j]->getName()
376 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000377
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000378 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000379
Craig Topper37eeb322018-03-23 00:02:45 +0000380 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000381 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000382 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000383 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000384
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000385 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000386 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000387 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000388 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000389
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000390 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000391 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000392 }
393
Jim Laskey19595752005-10-28 15:20:43 +0000394 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000395 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
396 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000397 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000398
David Goodwind813cbf2009-08-17 16:02:57 +0000399 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000400 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000401 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000402 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000403
404 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000405 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000406 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000407 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000408
Andrew Trick87255e32012-07-07 04:00:00 +0000409 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000410 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000411 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000412 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000413 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000414 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000415 // Add process itinerary to the list.
416 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000417
Andrew Trick87255e32012-07-07 04:00:00 +0000418 // If this processor defines no itineraries, then leave the itinerary list
419 // empty.
420 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000421 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000422 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000423
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000424 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000425
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000426 ItinList.resize(SchedModels.numInstrSchedClasses());
427 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
428
429 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000430 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
431
Jim Laskeydffe5972005-10-28 21:47:29 +0000432 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000433 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000434
Jim Laskey19595752005-10-28 15:20:43 +0000435 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000436 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000437 unsigned NStages = 0;
438 if (ItinData)
439 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000440
David Goodwind813cbf2009-08-17 16:02:57 +0000441 // Get string and operand cycle count
442 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000443 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000444 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000445 if (ItinData) {
446 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
447 NOperandCycles);
448
449 FormItineraryBypassString(Name, ItinData, ItinBypassString,
450 NOperandCycles);
451 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000452
David Goodwind813cbf2009-08-17 16:02:57 +0000453 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000454 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000455 if (NStages > 0) {
456 FindStage = ItinStageMap[ItinStageString];
457 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000458 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
459 StageTable += ItinStageString + ", // " + itostr(StageCount);
460 if (NStages > 1)
461 StageTable += "-" + itostr(StageCount + NStages - 1);
462 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000463 // Record Itin class number.
464 ItinStageMap[ItinStageString] = FindStage = StageCount;
465 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000466 }
467 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000468
David Goodwind813cbf2009-08-17 16:02:57 +0000469 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000470 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000471 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000472 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
473 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000474 if (FindOperandCycle == 0) {
475 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000476 OperandCycleTable += ItinOperandCycleString + ", // ";
477 std::string OperandIdxComment = itostr(OperandCycleCount);
478 if (NOperandCycles > 1)
479 OperandIdxComment += "-"
480 + itostr(OperandCycleCount + NOperandCycles - 1);
481 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000482 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000483 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000484 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000485 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000486 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000487 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000488 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000489 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000490
Evan Cheng367a5df2010-09-09 18:18:55 +0000491 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000492 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
493 InstrItinerary Intinerary = {
494 NumUOps,
495 FindStage,
496 uint16_t(FindStage + NStages),
497 FindOperandCycle,
498 uint16_t(FindOperandCycle + NOperandCycles),
499 };
Evan Cheng367a5df2010-09-09 18:18:55 +0000500
Jim Laskey19595752005-10-28 15:20:43 +0000501 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000502 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000503 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000504 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000505
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000506 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000507 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000508 StageTable += "};\n";
509
510 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000511 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000512 OperandCycleTable += "};\n";
513
Andrew Trick87255e32012-07-07 04:00:00 +0000514 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000515 BypassTable += "};\n";
516
David Goodwind813cbf2009-08-17 16:02:57 +0000517 // Emit tables.
518 OS << StageTable;
519 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000520 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000521}
522
Andrew Trick87255e32012-07-07 04:00:00 +0000523//
524// EmitProcessorData - Generate data for processor itineraries that were
525// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
526// Itineraries for each processor. The Itinerary lists are indexed on
527// CodeGenSchedClass::Index.
528//
529void SubtargetEmitter::
530EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000531 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000532 // Multiple processor models may share an itinerary record. Emit it once.
533 SmallPtrSet<Record*, 8> ItinsDefSet;
534
Andrew Trick87255e32012-07-07 04:00:00 +0000535 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000536 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000537 ProcItinListsIter = ProcItinLists.begin();
538 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000539 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000540
Andrew Trick87255e32012-07-07 04:00:00 +0000541 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000542 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000543 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000544
Andrew Trick87255e32012-07-07 04:00:00 +0000545 // Get the itinerary list for the processor.
546 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000547 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000548
Pete Cooperc0eb1532014-09-02 23:23:34 +0000549 // Empty itineraries aren't referenced anywhere in the tablegen output
550 // so don't emit them.
551 if (ItinList.empty())
552 continue;
553
Andrew Trick87255e32012-07-07 04:00:00 +0000554 OS << "\n";
555 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000556
557 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000558 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000559
560 // For each itinerary class in CodeGenSchedClass::Index order.
561 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
562 InstrItinerary &Intinerary = ItinList[j];
563
564 // Emit Itinerary in the form of
565 // { firstStage, lastStage, firstCycle, lastCycle } // index
566 OS << " { " <<
567 Intinerary.NumMicroOps << ", " <<
568 Intinerary.FirstStage << ", " <<
569 Intinerary.LastStage << ", " <<
570 Intinerary.FirstOperandCycle << ", " <<
571 Intinerary.LastOperandCycle << " }" <<
572 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
573 }
574 // End processor itinerary table
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000575 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
576 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000577 OS << "};\n";
578 }
579}
580
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000581// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000582// value defined in the C++ header. The Record is null if the processor does not
583// define a model.
584void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000585 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000586 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000587 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000588 if (V >= 0)
589 OS << V << Separator << " // " << Name;
590 else
Andrew Trick87255e32012-07-07 04:00:00 +0000591 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000592 OS << '\n';
593}
594
Clement Courbet39911e22018-02-08 08:46:48 +0000595void SubtargetEmitter::EmitProcessorResourceSubUnits(
596 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
597 OS << "\nstatic const unsigned " << ProcModel.ModelName
598 << "ProcResourceSubUnits[] = {\n"
599 << " 0, // Invalid\n";
600
601 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
602 Record *PRDef = ProcModel.ProcResourceDefs[i];
603 if (!PRDef->isSubClassOf("ProcResGroup"))
604 continue;
605 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
606 for (Record *RUDef : ResUnits) {
607 Record *const RU =
608 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
609 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
610 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
611 }
612 }
613 OS << " // " << PRDef->getName() << "\n";
614 }
615 OS << "};\n";
616}
617
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000618static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
619 raw_ostream &OS) {
Andrea Di Biagio9730bb82018-04-05 15:53:31 +0000620 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000621 if (Record *RCU = ProcModel.RetireControlUnit) {
622 ReorderBufferSize =
623 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
624 MaxRetirePerCycle =
625 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
626 }
627
628 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
629 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
630}
631
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000632static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
633 unsigned NumRegisterFiles,
634 unsigned NumCostEntries, raw_ostream &OS) {
635 if (NumRegisterFiles)
636 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
637 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000638 OS << "nullptr,\n 0";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000639
640 OS << ", // Number of register files.\n ";
641 if (NumCostEntries)
642 OS << ProcModel.ModelName << "RegisterCosts,\n ";
643 else
Andrea Di Biagio8fd4be32018-04-05 13:59:52 +0000644 OS << "nullptr,\n ";
Clement Courbetb4493792018-04-10 08:16:37 +0000645 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000646}
647
648unsigned
649SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
650 raw_ostream &OS) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000651 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
652 return RF.hasDefaultCosts();
653 }))
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000654 return 0;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000655
656 // Print the RegisterCost table first.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000657 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000658 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
659 << "RegisterCosts"
660 << "[] = {\n";
661
662 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
663 // Skip register files with a default cost table.
664 if (RF.hasDefaultCosts())
665 continue;
666 // Add entries to the cost table.
667 for (const CodeGenRegisterCost &RC : RF.Costs) {
668 OS << " { ";
669 Record *Rec = RC.RCDef;
670 if (Rec->getValue("Namespace"))
671 OS << Rec->getValueAsString("Namespace") << "::";
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000672 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
673 << RC.AllowMoveElimination << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000674 }
675 }
676 OS << "};\n";
677
678 // Now generate a table with register file info.
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000679 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
680 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000681 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
682 << "RegisterFiles"
683 << "[] = {\n"
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000684 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000685 unsigned CostTblIndex = 0;
686
687 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
688 OS << " { ";
689 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
690 unsigned NumCostEntries = RD.Costs.size();
Andrea Di Biagio6eebbe02018-10-12 11:23:04 +0000691 OS << NumCostEntries << ", " << CostTblIndex << ", "
692 << RD.MaxMovesEliminatedPerCycle << ", "
693 << RD.AllowZeroMoveEliminationOnly << "},\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000694 CostTblIndex += NumCostEntries;
695 }
696 OS << "};\n";
697
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000698 return CostTblIndex;
699}
Simon Pilgrimb04cd1b2018-04-19 10:59:49 +0000700
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000701void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
702 raw_ostream &OS) {
703 unsigned QueueID = 0;
704 if (ProcModel.LoadQueue) {
705 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
706 QueueID =
707 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
708 std::find(ProcModel.ProcResourceDefs.begin(),
709 ProcModel.ProcResourceDefs.end(), Queue));
710 }
711 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
712
713 QueueID = 0;
714 if (ProcModel.StoreQueue) {
715 const Record *Queue =
716 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
717 QueueID =
718 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
719 std::find(ProcModel.ProcResourceDefs.begin(),
720 ProcModel.ProcResourceDefs.end(), Queue));
721 }
722 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
723}
724
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000725void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
726 raw_ostream &OS) {
727 // Generate a table of register file descriptors (one entry per each user
728 // defined register file), and a table of register costs.
729 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
730
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000731 // Now generate a table for the extra processor info.
732 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000733 << "ExtraInfo = {\n ";
734
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000735 // Add information related to the retire control unit.
736 EmitRetireControlUnitInfo(ProcModel, OS);
737
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000738 // Add information related to the register files (i.e. where to find register
739 // file descriptors and register costs).
740 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
741 NumCostEntries, OS);
742
Andrea Di Biagio373a4cc2018-11-29 12:15:56 +0000743 // Add information about load/store queues.
744 EmitLoadStoreQueueInfo(ProcModel, OS);
745
Andrea Di Biagio378d75a2018-04-04 11:53:13 +0000746 OS << "};\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000747}
748
Andrew Trick23f3c652012-09-17 22:18:45 +0000749void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
750 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000751 EmitProcessorResourceSubUnits(ProcModel, OS);
752
Jinsong Ji05941622018-09-18 15:38:56 +0000753 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000754 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
755 << "ProcResources"
756 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000757 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000758
Clement Courbet39911e22018-02-08 08:46:48 +0000759 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000760 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
761 Record *PRDef = ProcModel.ProcResourceDefs[i];
762
Craig Topper24064772014-04-15 07:20:03 +0000763 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000764 unsigned SuperIdx = 0;
765 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000766 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000767 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000768 if (PRDef->isSubClassOf("ProcResGroup")) {
769 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000770 for (Record *RU : ResUnits) {
771 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000772 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000773 }
774 }
775 else {
776 // Find the SuperIdx
777 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000778 SuperDef =
779 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
780 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000781 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
782 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000783 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000784 }
785 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000786 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000787 if (PRDef->getName().size() < 15)
788 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000789 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
790 if (SubUnitsBeginOffset != SubUnitsOffset) {
791 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
792 << SubUnitsBeginOffset;
793 } else {
794 OS << "nullptr";
795 }
796 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000797 if (SuperDef)
798 OS << ", Super=" << SuperDef->getName();
799 OS << "\n";
800 }
801 OS << "};\n";
802}
803
Andrew Trick9ef08822012-09-17 22:18:48 +0000804// Find the WriteRes Record that defines processor resources for this
805// SchedWrite.
806Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000807 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000808
809 // Check if the SchedWrite is already subtarget-specific and directly
810 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000811 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
812 return SchedWrite.TheDef;
813
Craig Topper24064772014-04-15 07:20:03 +0000814 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000815 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000816 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000817 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000818 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
819 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
820 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
821 continue;
822 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000823 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000824 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000825 "defined for processor " + ProcModel.ModelName +
826 " Ensure only one SchedAlias exists per RW.");
827 AliasDef = AliasRW.TheDef;
828 }
829 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
830 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000831
832 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000833 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000834 for (Record *WR : ProcModel.WriteResDefs) {
835 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000836 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000837 if (AliasDef == WR->getValueAsDef("WriteType")
838 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000839 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000840 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000841 "SchedWrite and its alias on processor " +
842 ProcModel.ModelName);
843 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000844 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000845 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000846 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000847 // TODO: If ProcModel has a base model (previous generation processor),
848 // then call FindWriteResources recursively with that model here.
849 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000850 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000851 Twine("Processor does not define resources for ") +
852 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000853 }
854 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000855}
856
857/// Find the ReadAdvance record for the given SchedRead on this processor or
858/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000859Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000860 const CodeGenProcModel &ProcModel) {
861 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000862 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
863 return SchedRead.TheDef;
864
865 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000866 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000867 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000868 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000869 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000870 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
871 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
872 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
873 continue;
874 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000875 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000876 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000877 "defined for processor " + ProcModel.ModelName +
878 " Ensure only one SchedAlias exists per RW.");
879 AliasDef = AliasRW.TheDef;
880 }
881 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
882 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000883
884 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000885 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000886 for (Record *RA : ProcModel.ReadAdvanceDefs) {
887 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000888 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000889 if (AliasDef == RA->getValueAsDef("ReadType")
890 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000891 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000892 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000893 "SchedRead and its alias on processor " +
894 ProcModel.ModelName);
895 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000896 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000897 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000898 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000899 // TODO: If ProcModel has a base model (previous generation processor),
900 // then call FindReadAdvance recursively with that model here.
901 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000902 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000903 Twine("Processor does not define resources for ") +
904 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000905 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000906 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000907}
908
Andrew Trick4e67cba2013-03-14 21:21:50 +0000909// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000910// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000911void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
912 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000913 const CodeGenProcModel &PM) {
Clement Courbet5eeed772018-06-13 09:41:49 +0000914 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000915 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000916 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000917 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000918 if (PRDef->isSubClassOf("ProcResGroup"))
919 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000920 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000921 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000922 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000923 for (Record *SubDef = PRDef;
924 SubDef->getValueInit("Super")->isComplete();) {
925 if (SubDef->isSubClassOf("ProcResGroup")) {
926 // Disallow this for simplicitly.
927 PrintFatalError(SubDef->getLoc(), "Processor resource group "
928 " cannot be a super resources.");
929 }
930 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000931 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
932 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000933 PRVec.push_back(SuperDef);
934 Cycles.push_back(Cycles[i]);
935 SubDef = SuperDef;
936 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000937 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000938 for (Record *PR : PM.ProcResourceDefs) {
939 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000940 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000941 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000942 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000943 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000944 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000945 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000946 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000947 }
948 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000949 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000950 Cycles.push_back(Cycles[i]);
951 }
952 }
953 }
954}
955
Andrew Trick9ef08822012-09-17 22:18:48 +0000956// Generate the SchedClass table for this processor and update global
957// tables. Must be called for each processor in order.
958void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
959 SchedClassTables &SchedTables) {
960 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
961 if (!ProcModel.hasInstrSchedModel())
962 return;
963
964 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000965 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000966 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000967 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000968
Andrew Trick9ef08822012-09-17 22:18:48 +0000969 SCTab.resize(SCTab.size() + 1);
970 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000971 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000972 SCDesc.NumMicroOps = 0;
973 SCDesc.BeginGroup = false;
974 SCDesc.EndGroup = false;
975 SCDesc.WriteProcResIdx = 0;
976 SCDesc.WriteLatencyIdx = 0;
977 SCDesc.ReadAdvanceIdx = 0;
978
979 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000980 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +0000981 for (const CodeGenSchedTransition &CGT :
982 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
983 if (CGT.ProcIndices[0] == 0 ||
984 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +0000985 HasVariants = true;
986 break;
987 }
988 }
989 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000990 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
991 continue;
992 }
993
994 // Determine if the SchedClass is actually reachable on this processor. If
995 // not don't try to locate the processor resources, it will fail.
996 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000997 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
998 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +0000999 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +00001000 continue;
1001 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001002 IdxVec Writes = SC.Writes;
1003 IdxVec Reads = SC.Reads;
1004 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +00001005 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +00001006 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +00001007 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001008 for (Record *RW : SC.InstRWs) {
1009 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +00001010 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001011 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +00001012 break;
1013 }
1014 }
1015 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +00001016 Writes.clear();
1017 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +00001018 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1019 Writes, Reads);
1020 }
1021 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001022 if (Writes.empty()) {
1023 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001024 for (Record *I : ProcModel.ItinRWDefs) {
1025 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +00001026 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001027 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001028 Writes, Reads);
1029 break;
1030 }
1031 }
1032 if (Writes.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001033 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1034 << " does not have resources for class " << SC.Name
1035 << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001036 }
1037 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001038 // Sum resources across all operand writes.
1039 std::vector<MCWriteProcResEntry> WriteProcResources;
1040 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001041 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +00001042 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001043 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001044 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001045 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +00001046 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001047
1048 // For each operand, create a latency entry.
1049 MCWriteLatencyEntry WLEntry;
1050 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +00001051 unsigned WriteID = WriteSeq.back();
1052 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1053 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1054 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001055 if (!SchedModels.hasReadOfWrite(
1056 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +00001057 WriteID = 0;
1058 }
1059 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +00001060
Craig Topper29c55dcb2016-02-13 06:03:32 +00001061 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001062
Andrew Trick9257b8f2012-09-22 02:24:21 +00001063 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +00001064 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001065
1066 // Mark the parent class as invalid for unsupported write types.
1067 if (WriteRes->getValueAsBit("Unsupported")) {
1068 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1069 break;
1070 }
1071 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1072 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1073 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1074 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +00001075 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1076 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +00001077
1078 // Create an entry for each ProcResource listed in WriteRes.
1079 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1080 std::vector<int64_t> Cycles =
1081 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001082
Clement Courbet5eeed772018-06-13 09:41:49 +00001083 if (Cycles.empty()) {
1084 // If ResourceCycles is not provided, default to one cycle per
1085 // resource.
1086 Cycles.resize(PRVec.size(), 1);
1087 } else if (Cycles.size() != PRVec.size()) {
1088 // If ResourceCycles is provided, check consistency.
1089 PrintFatalError(
1090 WriteRes->getLoc(),
1091 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1092 "!size(ProcResources): ")
1093 .concat(Twine(PRVec.size()))
1094 .concat(" vs ")
1095 .concat(Twine(Cycles.size())));
1096 }
1097
Andrew Trick4e67cba2013-03-14 21:21:50 +00001098 ExpandProcResources(PRVec, Cycles, ProcModel);
1099
Andrew Trick9ef08822012-09-17 22:18:48 +00001100 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1101 PRIdx != PREnd; ++PRIdx) {
1102 MCWriteProcResEntry WPREntry;
1103 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1104 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +00001105 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +00001106 // If this resource is already used in this sequence, add the current
1107 // entry's cycles so that the same resource appears to be used
1108 // serially, rather than multiple parallel uses. This is important for
1109 // in-order machine where the resource consumption is a hazard.
1110 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1111 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1112 if (WriteProcResources[WPRIdx].ProcResourceIdx
1113 == WPREntry.ProcResourceIdx) {
1114 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1115 break;
1116 }
1117 }
1118 if (WPRIdx == WPREnd)
1119 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +00001120 }
1121 }
1122 WriteLatencies.push_back(WLEntry);
1123 }
1124 // Create an entry for each operand Read in this SchedClass.
1125 // Entries must be sorted first by UseIdx then by WriteResourceID.
1126 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1127 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +00001128 Record *ReadAdvance =
1129 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +00001130 if (!ReadAdvance)
1131 continue;
1132
1133 // Mark the parent class as invalid for unsupported write types.
1134 if (ReadAdvance->getValueAsBit("Unsupported")) {
1135 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1136 break;
1137 }
1138 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1139 IdxVec WriteIDs;
1140 if (ValidWrites.empty())
1141 WriteIDs.push_back(0);
1142 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001143 for (Record *VW : ValidWrites) {
1144 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +00001145 }
1146 }
Fangrui Song0cac7262018-09-27 02:13:45 +00001147 llvm::sort(WriteIDs);
Craig Topper29c55dcb2016-02-13 06:03:32 +00001148 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +00001149 MCReadAdvanceEntry RAEntry;
1150 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001151 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +00001152 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1153 ReadAdvanceEntries.push_back(RAEntry);
1154 }
1155 }
1156 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1157 WriteProcResources.clear();
1158 WriteLatencies.clear();
1159 ReadAdvanceEntries.clear();
1160 }
1161 // Add the information for this SchedClass to the global tables using basic
1162 // compression.
1163 //
1164 // WritePrecRes entries are sorted by ProcResIdx.
Fangrui Song0cac7262018-09-27 02:13:45 +00001165 llvm::sort(WriteProcResources, LessWriteProcResources());
Andrew Trick9ef08822012-09-17 22:18:48 +00001166
1167 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1168 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1169 std::search(SchedTables.WriteProcResources.begin(),
1170 SchedTables.WriteProcResources.end(),
1171 WriteProcResources.begin(), WriteProcResources.end());
1172 if (WPRPos != SchedTables.WriteProcResources.end())
1173 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1174 else {
1175 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1176 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1177 WriteProcResources.end());
1178 }
1179 // Latency entries must remain in operand order.
1180 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1181 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1182 std::search(SchedTables.WriteLatencies.begin(),
1183 SchedTables.WriteLatencies.end(),
1184 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001185 if (WLPos != SchedTables.WriteLatencies.end()) {
1186 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1187 SCDesc.WriteLatencyIdx = idx;
1188 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1189 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1190 std::string::npos) {
1191 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1192 }
1193 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001194 else {
1195 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001196 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1197 WriteLatencies.begin(),
1198 WriteLatencies.end());
1199 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1200 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001201 }
1202 // ReadAdvanceEntries must remain in operand order.
1203 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1204 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1205 std::search(SchedTables.ReadAdvanceEntries.begin(),
1206 SchedTables.ReadAdvanceEntries.end(),
1207 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1208 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1209 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1210 else {
1211 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1212 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1213 ReadAdvanceEntries.end());
1214 }
1215 }
1216}
1217
Andrew Tricka72fca62012-09-17 22:18:50 +00001218// Emit SchedClass tables for all processors and associated global tables.
1219void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1220 raw_ostream &OS) {
1221 // Emit global WriteProcResTable.
1222 OS << "\n// {ProcResourceIdx, Cycles}\n"
1223 << "extern const llvm::MCWriteProcResEntry "
1224 << Target << "WriteProcResTable[] = {\n"
1225 << " { 0, 0}, // Invalid\n";
1226 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1227 WPRIdx != WPREnd; ++WPRIdx) {
1228 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1229 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1230 << format("%2d", WPREntry.Cycles) << "}";
1231 if (WPRIdx + 1 < WPREnd)
1232 OS << ',';
1233 OS << " // #" << WPRIdx << '\n';
1234 }
1235 OS << "}; // " << Target << "WriteProcResTable\n";
1236
1237 // Emit global WriteLatencyTable.
1238 OS << "\n// {Cycles, WriteResourceID}\n"
1239 << "extern const llvm::MCWriteLatencyEntry "
1240 << Target << "WriteLatencyTable[] = {\n"
1241 << " { 0, 0}, // Invalid\n";
1242 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1243 WLIdx != WLEnd; ++WLIdx) {
1244 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1245 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1246 << format("%2d", WLEntry.WriteResourceID) << "}";
1247 if (WLIdx + 1 < WLEnd)
1248 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001249 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001250 }
1251 OS << "}; // " << Target << "WriteLatencyTable\n";
1252
1253 // Emit global ReadAdvanceTable.
1254 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1255 << "extern const llvm::MCReadAdvanceEntry "
1256 << Target << "ReadAdvanceTable[] = {\n"
1257 << " {0, 0, 0}, // Invalid\n";
1258 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1259 RAIdx != RAEnd; ++RAIdx) {
1260 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1261 OS << " {" << RAEntry.UseIdx << ", "
1262 << format("%2d", RAEntry.WriteResourceID) << ", "
1263 << format("%2d", RAEntry.Cycles) << "}";
1264 if (RAIdx + 1 < RAEnd)
1265 OS << ',';
1266 OS << " // #" << RAIdx << '\n';
1267 }
1268 OS << "}; // " << Target << "ReadAdvanceTable\n";
1269
1270 // Emit a SchedClass table for each processor.
1271 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1272 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1273 if (!PI->hasInstrSchedModel())
1274 continue;
1275
1276 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001277 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001278
1279 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1280 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1281 OS << "static const llvm::MCSchedClassDesc "
1282 << PI->ModelName << "SchedClasses[] = {\n";
1283
1284 // The first class is always invalid. We no way to distinguish it except by
1285 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001286 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001287 && "invalid class not first");
1288 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1289 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001290 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001291
1292 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1293 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1294 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1295 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1296 if (SchedClass.Name.size() < 18)
1297 OS.indent(18 - SchedClass.Name.size());
1298 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001299 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1300 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001301 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1302 << ", " << MCDesc.NumWriteProcResEntries
1303 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1304 << ", " << MCDesc.NumWriteLatencyEntries
1305 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001306 << ", " << MCDesc.NumReadAdvanceEntries
1307 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001308 }
1309 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1310 }
1311}
1312
Andrew Trick87255e32012-07-07 04:00:00 +00001313void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1314 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001315 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001316 // Emit extra processor info if available.
1317 if (PM.hasExtraProcessorInfo())
1318 EmitExtraProcessorInfo(PM, OS);
Andrew Trick23f3c652012-09-17 22:18:45 +00001319 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001320 if (PM.hasInstrSchedModel())
1321 EmitProcessorResources(PM, OS);
1322 else if(!PM.ProcResourceDefs.empty())
1323 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001324 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001325
Andrew Trick73d77362012-06-05 03:44:40 +00001326 // Begin processor itinerary properties
1327 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001328 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1329 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1330 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1331 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1332 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1333 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1334 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001335
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001336 bool PostRAScheduler =
1337 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001338
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001339 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1340 << "PostRAScheduler\n";
1341
1342 bool CompleteModel =
1343 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1344
1345 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1346 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001347
Craig Topper29c55dcb2016-02-13 06:03:32 +00001348 OS << " " << PM.Index << ", // Processor ID\n";
1349 if (PM.hasInstrSchedModel())
1350 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1351 << " " << PM.ModelName << "SchedClasses" << ",\n"
1352 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001353 << " " << (SchedModels.schedClassEnd()
1354 - SchedModels.schedClassBegin()) << ",\n";
1355 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001356 OS << " nullptr, nullptr, 0, 0,"
1357 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001358 if (PM.hasItineraries())
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001359 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001360 else
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001361 OS << " nullptr, // No Itinerary\n";
1362 if (PM.hasExtraProcessorInfo())
Clement Courbetb4493792018-04-10 08:16:37 +00001363 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +00001364 else
Clement Courbetb4493792018-04-10 08:16:37 +00001365 OS << " nullptr // No extra processor descriptor\n";
Craig Topper194cb742017-10-24 15:50:55 +00001366 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001367 }
Jim Laskey3763a502005-10-31 17:16:01 +00001368}
1369
1370//
Clement Courbet41c8af32018-10-25 07:44:01 +00001371// EmitProcessorLookup - generate cpu name to sched model lookup tables.
Jim Laskey3763a502005-10-31 17:16:01 +00001372//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001373void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001374 // Gather and sort processor information
1375 std::vector<Record*> ProcessorList =
1376 Records.getAllDerivedDefinitions("Processor");
Fangrui Song0cac7262018-09-27 02:13:45 +00001377 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001378
Clement Courbet41c8af32018-10-25 07:44:01 +00001379 // Begin processor->sched model table
Jim Laskey3763a502005-10-31 17:16:01 +00001380 OS << "\n";
Clement Courbet41c8af32018-10-25 07:44:01 +00001381 OS << "// Sorted (by key) array of sched model for CPU subtype.\n"
1382 << "extern const llvm::SubtargetInfoKV " << Target
1383 << "ProcSchedKV[] = {\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001384 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001385 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001386 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001387 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001388 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001389
Jim Laskey3763a502005-10-31 17:16:01 +00001390 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001391 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001392 }
Clement Courbet41c8af32018-10-25 07:44:01 +00001393 // End processor->sched model table
Jim Laskey3763a502005-10-31 17:16:01 +00001394 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001395}
1396
1397//
Andrew Trick87255e32012-07-07 04:00:00 +00001398// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001399//
Andrew Trick87255e32012-07-07 04:00:00 +00001400void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001401 OS << "#ifdef DBGFIELD\n"
1402 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1403 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001404 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001405 << "#define DBGFIELD(x) x,\n"
1406 << "#else\n"
1407 << "#define DBGFIELD(x)\n"
1408 << "#endif\n";
1409
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001410 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001411 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001412 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001413 EmitStageAndOperandCycleData(OS, ProcItinLists);
1414 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001415 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001416 OS << "\n// ===============================================================\n"
1417 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001418
Andrew Trick9ef08822012-09-17 22:18:48 +00001419 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001420 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1421 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001422 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001423 EmitSchedClassTables(SchedTables, OS);
1424
1425 // Emit the processor machine model
1426 EmitProcessorModels(OS);
1427 // Emit the processor lookup data
1428 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001429
Craig Topper194cb742017-10-24 15:50:55 +00001430 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001431}
1432
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001433static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1434 std::string Buffer;
1435 raw_string_ostream Stream(Buffer);
1436
1437 // Collect all the PredicateProlog records and print them to the output
1438 // stream.
1439 std::vector<Record *> Prologs =
1440 Records.getAllDerivedDefinitions("PredicateProlog");
Fangrui Song0cac7262018-09-27 02:13:45 +00001441 llvm::sort(Prologs, LessRecord());
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001442 for (Record *P : Prologs)
1443 Stream << P->getValueAsString("Code") << '\n';
1444
1445 Stream.flush();
1446 OS << Buffer;
1447}
1448
1449static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001450 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001451 raw_ostream &OS) {
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001452 std::string Buffer;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001453 raw_string_ostream SS(Buffer);
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001454
1455 auto IsTruePredicate = [](const Record *Rec) {
1456 return Rec->isSubClassOf("MCSchedPredicate") &&
1457 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1458 };
1459
1460 // If not all predicates are MCTrue, then we need an if-stmt.
1461 unsigned NumNonTruePreds =
1462 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001463
1464 SS.indent(PE.getIndentLevel() * 2);
1465
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001466 if (NumNonTruePreds) {
1467 bool FirstNonTruePredicate = true;
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001468 SS << "if (";
1469
1470 PE.setIndentLevel(PE.getIndentLevel() + 2);
1471
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001472 for (const Record *Rec : T.PredTerm) {
1473 // Skip predicates that evaluate to "true".
1474 if (IsTruePredicate(Rec))
1475 continue;
1476
1477 if (FirstNonTruePredicate) {
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001478 FirstNonTruePredicate = false;
1479 } else {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001480 SS << "\n";
1481 SS.indent(PE.getIndentLevel() * 2);
1482 SS << "&& ";
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001483 }
1484
1485 if (Rec->isSubClassOf("MCSchedPredicate")) {
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001486 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001487 continue;
1488 }
1489
1490 // Expand this legacy predicate and wrap it around braces if there is more
1491 // than one predicate to expand.
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001492 SS << ((NumNonTruePreds > 1) ? "(" : "")
1493 << Rec->getValueAsString("Predicate")
1494 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagio95140022018-05-25 15:55:37 +00001495 }
Andrea Di Biagio24d86d82018-08-13 11:09:04 +00001496
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001497 SS << ")\n"; // end of if-stmt
1498 PE.decreaseIndentLevel();
1499 SS.indent(PE.getIndentLevel() * 2);
1500 PE.decreaseIndentLevel();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001501 }
1502
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001503 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1504 SS.flush();
Andrea Di Biagiob31f9182018-04-26 18:03:24 +00001505 OS << Buffer;
1506}
1507
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001508// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1509// epilogue code for the auto-generated helper.
1510void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1511 if (ShouldReturnZero) {
Andrea Di Biagio95140022018-05-25 15:55:37 +00001512 OS << " // Don't know how to resolve this scheduling class.\n"
1513 << " return 0;\n";
1514 return;
1515 }
1516
1517 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1518}
1519
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001520bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1521 return all_of(T.PredTerm, [](const Record *Rec) {
1522 return Rec->isSubClassOf("MCSchedPredicate");
1523 });
1524}
1525
1526void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1527 IdxVec &VariantClasses,
1528 bool OnlyExpandMCInstPredicates) {
1529 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1530 // Ignore non-variant scheduling classes.
1531 if (SC.Transitions.empty())
1532 continue;
1533
1534 if (OnlyExpandMCInstPredicates) {
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001535 // Ignore this variant scheduling class no transitions use any meaningful
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001536 // MCSchedPredicate definitions.
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001537 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001538 return hasMCSchedPredicates(T);
1539 }))
1540 continue;
1541 }
1542
1543 VariantClasses.push_back(SC.Index);
1544 }
1545}
1546
1547void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1548 // A variant scheduling class may define transitions for multiple
1549 // processors. This function identifies wich processors are associated with
1550 // transition rules specified by variant class `SC`.
1551 for (const CodeGenSchedTransition &T : SC.Transitions) {
1552 IdxVec PI;
1553 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1554 ProcIndices.begin(), ProcIndices.end(),
1555 std::back_inserter(PI));
1556 ProcIndices.swap(PI);
1557 }
1558}
1559
1560void SubtargetEmitter::emitSchedModelHelpersImpl(
1561 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1562 IdxVec VariantClasses;
1563 collectVariantClasses(SchedModels, VariantClasses,
1564 OnlyExpandMCInstPredicates);
1565
1566 if (VariantClasses.empty()) {
1567 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1568 return;
1569 }
1570
1571 // Construct a switch statement where the condition is a check on the
1572 // scheduling class identifier. There is a `case` for every variant class
1573 // defined by the processor models of this target.
1574 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1575 // a variant scheduling class to another scheduling class. Rules are
1576 // described by instances of CodeGenSchedTransition. Note that transitions may
1577 // not be valid for all processors.
1578 OS << " switch (SchedClass) {\n";
1579 for (unsigned VC : VariantClasses) {
1580 IdxVec ProcIndices;
1581 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1582 collectProcessorIndices(SC, ProcIndices);
1583
1584 OS << " case " << VC << ": // " << SC.Name << '\n';
1585
Andrea Di Biagio9eaf5aa2018-08-14 18:36:54 +00001586 PredicateExpander PE(Target);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001587 PE.setByRef(false);
1588 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1589 for (unsigned PI : ProcIndices) {
1590 OS << " ";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001591
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001592 // Emit a guard on the processor ID.
1593 if (PI != 0) {
1594 OS << (OnlyExpandMCInstPredicates
1595 ? "if (CPUID == "
1596 : "if (SchedModel->getProcessorID() == ");
1597 OS << PI << ") ";
1598 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1599 }
1600
1601 // Now emit transitions associated with processor PI.
1602 for (const CodeGenSchedTransition &T : SC.Transitions) {
1603 if (PI != 0 && !count(T.ProcIndices, PI))
1604 continue;
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001605
1606 // Emit only transitions based on MCSchedPredicate, if it's the case.
1607 // At least the transition specified by NoSchedPred is emitted,
1608 // which becomes the default transition for those variants otherwise
1609 // not based on MCSchedPredicate.
1610 // FIXME: preferably, llvm-mca should instead assume a reasonable
1611 // default when a variant transition is not based on MCSchedPredicate
1612 // for a given processor.
1613 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1614 continue;
1615
Andrea Di Biagio2c6cbc8b2018-08-13 15:13:35 +00001616 PE.setIndentLevel(3);
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001617 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1618 }
1619
1620 OS << " }\n";
Evandro Menezes079bf4b2018-11-23 21:17:33 +00001621
Andrea Di Biagio8bdfd522018-08-10 10:43:43 +00001622 if (PI == 0)
1623 break;
1624 }
1625
1626 if (SC.isInferred())
1627 OS << " return " << SC.Index << ";\n";
1628 OS << " break;\n";
1629 }
1630
1631 OS << " };\n";
1632
1633 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1634}
1635
Andrea Di Biagio95140022018-05-25 15:55:37 +00001636void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1637 raw_ostream &OS) {
1638 OS << "unsigned " << ClassName
1639 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1640 << " const TargetSchedModel *SchedModel) const {\n";
1641
1642 // Emit the predicate prolog code.
1643 emitPredicateProlog(Records, OS);
1644
1645 // Emit target predicates.
1646 emitSchedModelHelpersImpl(OS);
Clement Courbet41c8af32018-10-25 07:44:01 +00001647
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001648 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagio95140022018-05-25 15:55:37 +00001649
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001650 OS << "unsigned " << ClassName
1651 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1652 << " unsigned CPUID) const {\n"
1653 << " return " << Target << "_MC"
1654 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001655 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1656
1657 STIPredicateExpander PE(Target);
1658 PE.setClassPrefix(ClassName);
1659 PE.setExpandDefinition(true);
1660 PE.setByRef(false);
1661 PE.setIndentLevel(0);
1662
1663 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1664 PE.expandSTIPredicate(OS, Fn);
Andrew Trickc6c88152012-09-18 03:41:43 +00001665}
1666
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001667void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1668 raw_ostream &OS) {
1669 const CodeGenHwModes &CGH = TGT.getHwModes();
1670 assert(CGH.getNumModeIds() > 0);
1671 if (CGH.getNumModeIds() == 1)
1672 return;
1673
1674 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1675 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1676 const HwMode &HM = CGH.getMode(M);
1677 OS << " if (checkFeatures(\"" << HM.Features
1678 << "\")) return " << M << ";\n";
1679 }
1680 OS << " return 0;\n}\n";
1681}
1682
Jim Laskey86f002c2005-10-27 19:47:21 +00001683//
Jim Laskeya2b52352005-10-26 17:30:34 +00001684// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1685// the subtarget features string.
1686//
Evan Cheng54b68e32011-07-01 20:45:01 +00001687void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1688 unsigned NumFeatures,
1689 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001690 std::vector<Record*> Features =
1691 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song0cac7262018-09-27 02:13:45 +00001692 llvm::sort(Features, LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001693
Andrew Trickdb6ed642011-04-01 01:56:55 +00001694 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1695 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001696 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001697 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001698 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001699 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1700 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001701
1702 if (Features.empty()) {
1703 OS << "}\n";
1704 return;
1705 }
1706
Andrew Trickba7b9212012-09-18 05:33:15 +00001707 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001708 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001709
Craig Topper29c55dcb2016-02-13 06:03:32 +00001710 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001711 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001712 StringRef Instance = R->getName();
1713 StringRef Value = R->getValueAsString("Value");
1714 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001715
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001716 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001717 OS << " if (Bits[" << Target << "::"
1718 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001719 << Attribute << " = " << Value << ";\n";
1720 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001721 OS << " if (Bits[" << Target << "::"
1722 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001723 << Attribute << " < " << Value << ") "
1724 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001725 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001726
Evan Chengfe6e4052011-06-30 01:53:36 +00001727 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001728}
1729
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001730void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001731 OS << "namespace " << Target << "_MC {\n"
1732 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1733 << " const MCInst *MI, unsigned CPUID) {\n";
1734 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1735 OS << "}\n";
1736 OS << "} // end of namespace " << Target << "_MC\n\n";
1737
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001738 OS << "struct " << Target
1739 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1740 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1741 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
1742 << " ArrayRef<SubtargetFeatureKV> PD,\n"
1743 << " const SubtargetInfoKV *ProcSched,\n"
1744 << " const MCWriteProcResEntry *WPR,\n"
1745 << " const MCWriteLatencyEntry *WL,\n"
1746 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1747 << " const unsigned *OC, const unsigned *FP) :\n"
1748 << " MCSubtargetInfo(TT, CPU, FS, PF, PD, ProcSched,\n"
1749 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1750 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001751 << " const MCInst *MI, unsigned CPUID) const override {\n"
1752 << " return " << Target << "_MC"
1753 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +00001754 OS << " }\n";
1755 OS << "};\n";
1756}
1757
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001758void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1759 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1760 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1761
1762 STIPredicateExpander PE(Target);
1763 PE.setExpandForMC(true);
1764 PE.setByRef(true);
1765 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1766 PE.expandSTIPredicate(OS, Fn);
1767
1768 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1769
1770 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1771 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1772
1773 std::string ClassPrefix = Target + "MCInstrAnalysis";
1774 PE.setExpandDefinition(true);
1775 PE.setClassPrefix(ClassPrefix);
1776 PE.setIndentLevel(0);
1777 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1778 PE.expandSTIPredicate(OS, Fn);
1779
1780 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1781}
1782
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001783//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001784// SubtargetEmitter::run - Main subtarget enumeration emitter.
1785//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001786void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001787 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001788
Evan Cheng4d1ca962011-07-08 01:53:10 +00001789 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001790 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001791
1792 OS << "namespace llvm {\n";
Craig Topper094bbca2016-02-14 05:22:01 +00001793 Enumeration(OS);
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
Evan Cheng54b68e32011-07-01 20:45:01 +00001804 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001805 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001806 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001807 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001808 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001809 OS << "\n";
1810#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);
Andrew Trickab722bd2012-09-18 03:18:56 +00001830 OS << Target << "ProcSchedKV, "
1831 << Target << "WriteProcResTable, "
1832 << Target << "WriteLatencyTable, "
1833 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001834 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001835 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001836 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001837 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001838 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001839 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001840 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001841 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001842
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001843 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001844
1845 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1846
1847 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001848 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001849
1850 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001851 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001852 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1853
1854 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1855
Evan Cheng0d639a22011-07-01 21:01:15 +00001856 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001857 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001858 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001859
1860 std::string ClassName = Target + "GenSubtargetInfo";
1861 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001862 OS << "class DFAPacketizer;\n";
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001863 OS << "namespace " << Target << "_MC {\n"
1864 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1865 << " const MCInst *MI, unsigned CPUID);\n"
1866 << "}\n\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001867 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001868 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001869 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001870 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001871 << " unsigned resolveSchedClass(unsigned SchedClass, "
1872 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001873 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagiobe8616f2018-05-31 13:30:42 +00001874 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1875 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001876 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001877 << " const;\n";
1878 if (TGT.getHwModes().getNumModeIds() > 1)
1879 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001880
1881 STIPredicateExpander PE(Target);
1882 PE.setByRef(false);
1883 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1884 PE.expandSTIPredicate(OS, Fn);
1885
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001886 OS << "};\n"
1887 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001888
1889 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1890
1891 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001892 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001893
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001894 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001895 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001896 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1897 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001898 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1899 OS << "extern const llvm::MCWriteProcResEntry "
1900 << Target << "WriteProcResTable[];\n";
1901 OS << "extern const llvm::MCWriteLatencyEntry "
1902 << Target << "WriteLatencyTable[];\n";
1903 OS << "extern const llvm::MCReadAdvanceEntry "
1904 << Target << "ReadAdvanceTable[];\n";
1905
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001906 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001907 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1908 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001909 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001910 }
1911
Daniel Sanders50f17232015-09-15 16:17:27 +00001912 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1913 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001914 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001915 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001916 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001917 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001918 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001919 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001920 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001921 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001922 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001923 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001924 OS << Target << "ProcSchedKV, "
1925 << Target << "WriteProcResTable, "
1926 << Target << "WriteLatencyTable, "
1927 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001928 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001929 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001930 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001931 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001932 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001933 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001934 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001935 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001936
Andrew Trickc6c88152012-09-18 03:41:43 +00001937 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001938 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001939
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001940 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001941
1942 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Andrea Di Biagio8b6c3142018-09-19 15:57:45 +00001943
1944 EmitMCInstrAnalysisPredicateFunctions(OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001945}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001946
1947namespace llvm {
1948
1949void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001950 CodeGenTarget CGTarget(RK);
1951 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001952}
1953
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001954} // end namespace llvm