blob: 280a9736e58a11a75d9a1e753a991f43d2c00831 [file] [log] [blame]
Jim Laskeycfda85a2005-10-21 19:00:04 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeycfda85a2005-10-21 19:00:04 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner73fbe142006-03-03 02:04:07 +000010// This tablegen backend emits subtarget enumerations.
Jim Laskeycfda85a2005-10-21 19:00:04 +000011//
12//===----------------------------------------------------------------------===//
13
Jim Laskeycfda85a2005-10-21 19:00:04 +000014#include "CodeGenTarget.h"
Andrew Trick87255e32012-07-07 04:00:00 +000015#include "CodeGenSchedule.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);
Mehdi Amini32986ed2016-10-04 23:47:33 +000093 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000094 char Separator);
Clement Courbet39911e22018-02-08 08:46:48 +000095 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
96 raw_ostream &OS);
Andrew Trick23f3c652012-09-17 22:18:45 +000097 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
98 raw_ostream &OS);
Andrew Trick9257b8f2012-09-22 02:24:21 +000099 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick9ef08822012-09-17 22:18:48 +0000100 const CodeGenProcModel &ProcModel);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000101 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
102 const CodeGenProcModel &ProcModel);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000103 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
104 const CodeGenProcModel &ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000105 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
106 SchedClassTables &SchedTables);
Andrew Tricka72fca62012-09-17 22:18:50 +0000107 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000108 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000109 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000110 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrew Trick87255e32012-07-07 04:00:00 +0000111 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000112 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000113 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
114 unsigned NumProcs);
115
116public:
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +0000117 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
118 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
119 Target(TGT.getName()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000120
121 void run(raw_ostream &o);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000122};
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000123
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000124} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000125
Jim Laskeya1beea62005-10-22 07:59:56 +0000126//
Jim Laskeya2b52352005-10-26 17:30:34 +0000127// Enumeration - Emit the specified class as an enumeration.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000128//
Craig Topper094bbca2016-02-14 05:22:01 +0000129void SubtargetEmitter::Enumeration(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000130 // Get all records of class and sort
Craig Topper094bbca2016-02-14 05:22:01 +0000131 std::vector<Record*> DefList =
132 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +0000133 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000134
Evan Chenga2e61292011-04-15 19:35:46 +0000135 unsigned N = DefList.size();
Evan Cheng54b68e32011-07-01 20:45:01 +0000136 if (N == 0)
137 return;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000138 if (N > MAX_SUBTARGET_FEATURES)
139 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chenga2e61292011-04-15 19:35:46 +0000140
Evan Cheng54b68e32011-07-01 20:45:01 +0000141 OS << "namespace " << Target << " {\n";
142
Craig Topperbcdb0f22016-02-13 17:58:14 +0000143 // Open enumeration.
Craig Topper2d45c1d2016-02-13 06:03:29 +0000144 OS << "enum {\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000145
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000146 // For each record
Craig Topperdf1285b2017-10-24 15:50:53 +0000147 for (unsigned i = 0; i < N; ++i) {
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000148 // Next record
149 Record *Def = DefList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000150
Reid Kleckner294fa7a2015-03-09 20:23:14 +0000151 // Get and emit name
Craig Topperdf1285b2017-10-24 15:50:53 +0000152 OS << " " << Def->getName() << " = " << i << ",\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000153 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000154
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000155 // Close enumeration and namespace
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000156 OS << "};\n";
157 OS << "} // end namespace " << Target << "\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000158}
159
160//
Bill Wendlinge6182262007-05-04 20:38:40 +0000161// FeatureKeyValues - Emit data of all the subtarget features. Used by the
162// command line.
Jim Laskey1b7369b2005-10-25 15:16:36 +0000163//
Evan Cheng54b68e32011-07-01 20:45:01 +0000164unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000165 // Gather and sort all the features
Jim Laskeydffe5972005-10-28 21:47:29 +0000166 std::vector<Record*> FeatureList =
167 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng54b68e32011-07-01 20:45:01 +0000168
169 if (FeatureList.empty())
170 return 0;
171
Jim Grosbach56938af2008-09-11 17:05:32 +0000172 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000173
Jim Laskey19595752005-10-28 15:20:43 +0000174 // Begin feature table
Jim Laskeya2b52352005-10-26 17:30:34 +0000175 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000176 << "extern const llvm::SubtargetFeatureKV " << Target
177 << "FeatureKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000178
Jim Laskey19595752005-10-28 15:20:43 +0000179 // For each feature
Evan Cheng54b68e32011-07-01 20:45:01 +0000180 unsigned NumFeatures = 0;
Jim Laskey3f7d0472006-12-12 20:55:58 +0000181 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000182 // Next feature
183 Record *Feature = FeatureList[i];
184
Craig Topperbcd3c372017-05-31 21:12:46 +0000185 StringRef Name = Feature->getName();
186 StringRef CommandLineName = Feature->getValueAsString("Name");
187 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000188
Jim Laskey3f7d0472006-12-12 20:55:58 +0000189 if (CommandLineName.empty()) continue;
Andrew Trickdb6ed642011-04-01 01:56:55 +0000190
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000191 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskey1b7369b2005-10-25 15:16:36 +0000192 OS << " { "
Jim Laskeydffe5972005-10-28 21:47:29 +0000193 << "\"" << CommandLineName << "\", "
Jim Laskey1b7369b2005-10-25 15:16:36 +0000194 << "\"" << Desc << "\", "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000195 << "{ " << Target << "::" << Name << " }, ";
Bill Wendlinge6182262007-05-04 20:38:40 +0000196
Andrew Trickdb6ed642011-04-01 01:56:55 +0000197 const std::vector<Record*> &ImpliesList =
Bill Wendlinge6182262007-05-04 20:38:40 +0000198 Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000199
Craig Topper4ceea0a2016-01-03 08:57:41 +0000200 OS << "{";
201 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
202 OS << " " << Target << "::" << ImpliesList[j]->getName();
203 if (++j < M) OS << ",";
Bill Wendlinge6182262007-05-04 20:38:40 +0000204 }
Craig Topperdf1285b2017-10-24 15:50:53 +0000205 OS << " } },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000206 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000207 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000208
Jim Laskey19595752005-10-28 15:20:43 +0000209 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000210 OS << "};\n";
211
Evan Cheng54b68e32011-07-01 20:45:01 +0000212 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000213}
214
215//
216// CPUKeyValues - Emit data of all the subtarget processors. Used by command
217// line.
218//
Evan Cheng54b68e32011-07-01 20:45:01 +0000219unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000220 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000221 std::vector<Record*> ProcessorList =
222 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000223 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000224
Jim Laskey19595752005-10-28 15:20:43 +0000225 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000226 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000227 << "extern const llvm::SubtargetFeatureKV " << Target
228 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000229
Jim Laskey19595752005-10-28 15:20:43 +0000230 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000231 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000232 StringRef Name = Processor->getValueAsString("Name");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000233 const std::vector<Record*> &FeatureList =
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000234 Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000235
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000236 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000237 OS << " { "
238 << "\"" << Name << "\", "
239 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000240
Craig Topper4ceea0a2016-01-03 08:57:41 +0000241 OS << "{";
242 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
243 OS << " " << Target << "::" << FeatureList[j]->getName();
244 if (++j < M) OS << ",";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000245 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000246 // The { } is for the "implies" section of this data structure.
Craig Topperdf1285b2017-10-24 15:50:53 +0000247 OS << " }, { } },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000248 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000249
Jim Laskey19595752005-10-28 15:20:43 +0000250 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000251 OS << "};\n";
252
Evan Cheng54b68e32011-07-01 20:45:01 +0000253 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000254}
Jim Laskeya1beea62005-10-22 07:59:56 +0000255
Jim Laskeya2b52352005-10-26 17:30:34 +0000256//
David Goodwind813cbf2009-08-17 16:02:57 +0000257// FormItineraryStageString - Compose a string containing the stage
258// data initialization for the specified itinerary. N is the number
259// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000260//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000261void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
262 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000263 std::string &ItinString,
264 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000265 // Get states list
Bill Wendlinge6182262007-05-04 20:38:40 +0000266 const std::vector<Record*> &StageList =
267 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000268
269 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000270 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000271 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000272 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000273 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000274
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000275 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000276 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000277 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000278
Jim Laskeydffe5972005-10-28 21:47:29 +0000279 // Get unit list
Bill Wendlinge6182262007-05-04 20:38:40 +0000280 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000281
Jim Laskey19595752005-10-28 15:20:43 +0000282 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000283 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000284 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000285 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000286 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000287 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000288
David Goodwinb369ee42009-08-12 18:31:53 +0000289 int TimeInc = Stage->getValueAsInt("TimeInc");
290 ItinString += ", " + itostr(TimeInc);
291
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000292 int Kind = Stage->getValueAsInt("Kind");
293 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
294
Jim Laskey19595752005-10-28 15:20:43 +0000295 // Close off stage
296 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000297 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000298 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000299}
300
301//
David Goodwind813cbf2009-08-17 16:02:57 +0000302// FormItineraryOperandCycleString - Compose a string containing the
303// operand cycle initialization for the specified itinerary. N is the
304// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000305//
David Goodwind813cbf2009-08-17 16:02:57 +0000306void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
307 std::string &ItinString, unsigned &NOperandCycles) {
308 // Get operand cycle list
309 const std::vector<int64_t> &OperandCycleList =
310 ItinData->getValueAsListOfInts("OperandCycles");
311
312 // For each operand cycle
313 unsigned N = NOperandCycles = OperandCycleList.size();
314 for (unsigned i = 0; i < N;) {
315 // Next operand cycle
316 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000317
David Goodwind813cbf2009-08-17 16:02:57 +0000318 ItinString += " " + itostr(OCycle);
319 if (++i < N) ItinString += ", ";
320 }
321}
322
Evan Cheng0097dd02010-09-28 23:50:49 +0000323void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
324 Record *ItinData,
325 std::string &ItinString,
326 unsigned NOperandCycles) {
327 const std::vector<Record*> &BypassList =
328 ItinData->getValueAsListOfDefs("Bypasses");
329 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000330 unsigned i = 0;
331 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000332 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000333 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000334 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000335 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000336 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000337 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000338 }
339}
340
David Goodwind813cbf2009-08-17 16:02:57 +0000341//
Andrew Trick87255e32012-07-07 04:00:00 +0000342// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
343// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
344// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000345//
Andrew Trick87255e32012-07-07 04:00:00 +0000346void SubtargetEmitter::
347EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000348 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000349 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000350 // Multiple processor models may share an itinerary record. Emit it once.
351 SmallPtrSet<Record*, 8> ItinsDefSet;
352
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000353 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000354 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000355
Craig Topper29c55dcb2016-02-13 06:03:32 +0000356 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000357 continue;
358
Craig Topper29c55dcb2016-02-13 06:03:32 +0000359 std::vector<Record*> FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000360 if (FUs.empty())
361 continue;
362
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000363 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000364 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000365 << "namespace " << Name << "FU {\n";
366
367 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000368 OS << " const unsigned " << FUs[j]->getName()
369 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000370
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000371 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000372
Craig Topper29c55dcb2016-02-13 06:03:32 +0000373 std::vector<Record*> BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000374 if (!BPs.empty()) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000375 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
376 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000377
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000378 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000379 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000380 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000381 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000382
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000383 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000384 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000385 }
386
Jim Laskey19595752005-10-28 15:20:43 +0000387 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000388 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
389 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000390 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000391
David Goodwind813cbf2009-08-17 16:02:57 +0000392 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000393 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000394 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000395 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000396
397 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000398 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000399 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000400 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000401
Andrew Trick87255e32012-07-07 04:00:00 +0000402 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000403 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000404 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000405 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000406 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000407 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000408 // Add process itinerary to the list.
409 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000410
Andrew Trick87255e32012-07-07 04:00:00 +0000411 // If this processor defines no itineraries, then leave the itinerary list
412 // empty.
413 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000414 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000415 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000416
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000417 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000418
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000419 ItinList.resize(SchedModels.numInstrSchedClasses());
420 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
421
422 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000423 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
424
Jim Laskeydffe5972005-10-28 21:47:29 +0000425 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000426 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000427
Jim Laskey19595752005-10-28 15:20:43 +0000428 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000429 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000430 unsigned NStages = 0;
431 if (ItinData)
432 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000433
David Goodwind813cbf2009-08-17 16:02:57 +0000434 // Get string and operand cycle count
435 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000436 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000437 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000438 if (ItinData) {
439 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
440 NOperandCycles);
441
442 FormItineraryBypassString(Name, ItinData, ItinBypassString,
443 NOperandCycles);
444 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000445
David Goodwind813cbf2009-08-17 16:02:57 +0000446 // Check to see if stage already exists and create if it doesn't
447 unsigned FindStage = 0;
448 if (NStages > 0) {
449 FindStage = ItinStageMap[ItinStageString];
450 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000451 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
452 StageTable += ItinStageString + ", // " + itostr(StageCount);
453 if (NStages > 1)
454 StageTable += "-" + itostr(StageCount + NStages - 1);
455 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000456 // Record Itin class number.
457 ItinStageMap[ItinStageString] = FindStage = StageCount;
458 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000459 }
460 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000461
David Goodwind813cbf2009-08-17 16:02:57 +0000462 // Check to see if operand cycle already exists and create if it doesn't
463 unsigned FindOperandCycle = 0;
464 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000465 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
466 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000467 if (FindOperandCycle == 0) {
468 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000469 OperandCycleTable += ItinOperandCycleString + ", // ";
470 std::string OperandIdxComment = itostr(OperandCycleCount);
471 if (NOperandCycles > 1)
472 OperandIdxComment += "-"
473 + itostr(OperandCycleCount + NOperandCycles - 1);
474 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000475 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000476 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000477 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000478 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000479 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000480 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000481 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000482 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000483
Evan Cheng367a5df2010-09-09 18:18:55 +0000484 // Set up itinerary as location and location + stage count
Andrew Trick87255e32012-07-07 04:00:00 +0000485 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng367a5df2010-09-09 18:18:55 +0000486 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
487 FindOperandCycle,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000488 FindOperandCycle + NOperandCycles };
Evan Cheng367a5df2010-09-09 18:18:55 +0000489
Jim Laskey19595752005-10-28 15:20:43 +0000490 // Inject - empty slots will be 0, 0
Andrew Trick87255e32012-07-07 04:00:00 +0000491 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey86f002c2005-10-27 19:47:21 +0000492 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000493 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000494
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000495 // Closing stage
Andrew Trick87255e32012-07-07 04:00:00 +0000496 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000497 StageTable += "};\n";
498
499 // Closing operand cycles
Andrew Trick87255e32012-07-07 04:00:00 +0000500 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000501 OperandCycleTable += "};\n";
502
Andrew Trick87255e32012-07-07 04:00:00 +0000503 BypassTable += " 0 // End bypass tables\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000504 BypassTable += "};\n";
505
David Goodwind813cbf2009-08-17 16:02:57 +0000506 // Emit tables.
507 OS << StageTable;
508 OS << OperandCycleTable;
Evan Cheng0097dd02010-09-28 23:50:49 +0000509 OS << BypassTable;
Jim Laskey86f002c2005-10-27 19:47:21 +0000510}
511
Andrew Trick87255e32012-07-07 04:00:00 +0000512//
513// EmitProcessorData - Generate data for processor itineraries that were
514// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
515// Itineraries for each processor. The Itinerary lists are indexed on
516// CodeGenSchedClass::Index.
517//
518void SubtargetEmitter::
519EmitItineraries(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000520 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000521 // Multiple processor models may share an itinerary record. Emit it once.
522 SmallPtrSet<Record*, 8> ItinsDefSet;
523
Andrew Trick87255e32012-07-07 04:00:00 +0000524 // For each processor's machine model
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000525 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick87255e32012-07-07 04:00:00 +0000526 ProcItinListsIter = ProcItinLists.begin();
527 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick76686492012-09-15 00:19:57 +0000528 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000529
Andrew Trick87255e32012-07-07 04:00:00 +0000530 Record *ItinsDef = PI->ItinsDef;
David Blaikie70573dc2014-11-19 07:49:26 +0000531 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000532 continue;
Andrew Trick87255e32012-07-07 04:00:00 +0000533
Andrew Trick87255e32012-07-07 04:00:00 +0000534 // Get the itinerary list for the processor.
535 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick76686492012-09-15 00:19:57 +0000536 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick87255e32012-07-07 04:00:00 +0000537
Pete Cooperc0eb1532014-09-02 23:23:34 +0000538 // Empty itineraries aren't referenced anywhere in the tablegen output
539 // so don't emit them.
540 if (ItinList.empty())
541 continue;
542
Andrew Trick87255e32012-07-07 04:00:00 +0000543 OS << "\n";
544 OS << "static const llvm::InstrItinerary ";
Andrew Trick87255e32012-07-07 04:00:00 +0000545
546 // Begin processor itinerary table
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000547 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000548
549 // For each itinerary class in CodeGenSchedClass::Index order.
550 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
551 InstrItinerary &Intinerary = ItinList[j];
552
553 // Emit Itinerary in the form of
554 // { firstStage, lastStage, firstCycle, lastCycle } // index
555 OS << " { " <<
556 Intinerary.NumMicroOps << ", " <<
557 Intinerary.FirstStage << ", " <<
558 Intinerary.LastStage << ", " <<
559 Intinerary.FirstOperandCycle << ", " <<
560 Intinerary.LastOperandCycle << " }" <<
561 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
562 }
563 // End processor itinerary table
564 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
565 OS << "};\n";
566 }
567}
568
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000569// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000570// value defined in the C++ header. The Record is null if the processor does not
571// define a model.
572void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000573 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000574 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000575 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000576 if (V >= 0)
577 OS << V << Separator << " // " << Name;
578 else
Andrew Trick87255e32012-07-07 04:00:00 +0000579 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000580 OS << '\n';
581}
582
Clement Courbet39911e22018-02-08 08:46:48 +0000583void SubtargetEmitter::EmitProcessorResourceSubUnits(
584 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
585 OS << "\nstatic const unsigned " << ProcModel.ModelName
586 << "ProcResourceSubUnits[] = {\n"
587 << " 0, // Invalid\n";
588
589 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
590 Record *PRDef = ProcModel.ProcResourceDefs[i];
591 if (!PRDef->isSubClassOf("ProcResGroup"))
592 continue;
593 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
594 for (Record *RUDef : ResUnits) {
595 Record *const RU =
596 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
597 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
598 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
599 }
600 }
601 OS << " // " << PRDef->getName() << "\n";
602 }
603 OS << "};\n";
604}
605
Andrew Trick23f3c652012-09-17 22:18:45 +0000606void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
607 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000608 EmitProcessorResourceSubUnits(ProcModel, OS);
609
610 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered, SubUnitsIdxBegin}\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000611 OS << "static const llvm::MCProcResourceDesc "
612 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Craig Topperdf1285b2017-10-24 15:50:53 +0000613 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000614
Clement Courbet39911e22018-02-08 08:46:48 +0000615 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000616 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
617 Record *PRDef = ProcModel.ProcResourceDefs[i];
618
Craig Topper24064772014-04-15 07:20:03 +0000619 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000620 unsigned SuperIdx = 0;
621 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000622 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000623 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000624 if (PRDef->isSubClassOf("ProcResGroup")) {
625 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000626 for (Record *RU : ResUnits) {
627 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet39911e22018-02-08 08:46:48 +0000628 SubUnitsOffset += NumUnits;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000629 }
630 }
631 else {
632 // Find the SuperIdx
633 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000634 SuperDef =
635 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
636 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000637 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
638 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000639 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000640 }
641 // Emit the ProcResourceDesc
Andrew Trick23f3c652012-09-17 22:18:45 +0000642 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
643 if (PRDef->getName().size() < 15)
644 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000645 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
646 if (SubUnitsBeginOffset != SubUnitsOffset) {
647 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
648 << SubUnitsBeginOffset;
649 } else {
650 OS << "nullptr";
651 }
652 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000653 if (SuperDef)
654 OS << ", Super=" << SuperDef->getName();
655 OS << "\n";
656 }
657 OS << "};\n";
658}
659
Andrew Trick9ef08822012-09-17 22:18:48 +0000660// Find the WriteRes Record that defines processor resources for this
661// SchedWrite.
662Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000663 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000664
665 // Check if the SchedWrite is already subtarget-specific and directly
666 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000667 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
668 return SchedWrite.TheDef;
669
Craig Topper24064772014-04-15 07:20:03 +0000670 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000671 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000672 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000673 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000674 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
675 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
676 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
677 continue;
678 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000679 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000680 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000681 "defined for processor " + ProcModel.ModelName +
682 " Ensure only one SchedAlias exists per RW.");
683 AliasDef = AliasRW.TheDef;
684 }
685 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
686 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000687
688 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000689 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000690 for (Record *WR : ProcModel.WriteResDefs) {
691 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000692 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000693 if (AliasDef == WR->getValueAsDef("WriteType")
694 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000695 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000696 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000697 "SchedWrite and its alias on processor " +
698 ProcModel.ModelName);
699 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000700 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000701 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000702 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000703 // TODO: If ProcModel has a base model (previous generation processor),
704 // then call FindWriteResources recursively with that model here.
705 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000706 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000707 Twine("Processor does not define resources for ") +
708 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000709 }
710 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000711}
712
713/// Find the ReadAdvance record for the given SchedRead on this processor or
714/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000715Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000716 const CodeGenProcModel &ProcModel) {
717 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000718 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
719 return SchedRead.TheDef;
720
721 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000722 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000723 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000724 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000725 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000726 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
727 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
728 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
729 continue;
730 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000731 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000732 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000733 "defined for processor " + ProcModel.ModelName +
734 " Ensure only one SchedAlias exists per RW.");
735 AliasDef = AliasRW.TheDef;
736 }
737 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
738 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000739
740 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000741 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000742 for (Record *RA : ProcModel.ReadAdvanceDefs) {
743 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000744 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000745 if (AliasDef == RA->getValueAsDef("ReadType")
746 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000747 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000748 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000749 "SchedRead and its alias on processor " +
750 ProcModel.ModelName);
751 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000752 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000753 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000754 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000755 // TODO: If ProcModel has a base model (previous generation processor),
756 // then call FindReadAdvance recursively with that model here.
757 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000758 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000759 Twine("Processor does not define resources for ") +
760 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000761 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000762 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000763}
764
Andrew Trick4e67cba2013-03-14 21:21:50 +0000765// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000766// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000767void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
768 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000769 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000770 // Default to 1 resource cycle.
771 Cycles.resize(PRVec.size(), 1);
772 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000773 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000774 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000775 if (PRDef->isSubClassOf("ProcResGroup"))
776 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000777 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000778 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000779 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000780 for (Record *SubDef = PRDef;
781 SubDef->getValueInit("Super")->isComplete();) {
782 if (SubDef->isSubClassOf("ProcResGroup")) {
783 // Disallow this for simplicitly.
784 PrintFatalError(SubDef->getLoc(), "Processor resource group "
785 " cannot be a super resources.");
786 }
787 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000788 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
789 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000790 PRVec.push_back(SuperDef);
791 Cycles.push_back(Cycles[i]);
792 SubDef = SuperDef;
793 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000794 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000795 for (Record *PR : PM.ProcResourceDefs) {
796 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000797 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000798 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000799 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000800 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000801 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000802 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000803 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000804 }
805 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000806 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000807 Cycles.push_back(Cycles[i]);
808 }
809 }
810 }
811}
812
Andrew Trick9ef08822012-09-17 22:18:48 +0000813// Generate the SchedClass table for this processor and update global
814// tables. Must be called for each processor in order.
815void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
816 SchedClassTables &SchedTables) {
817 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
818 if (!ProcModel.hasInstrSchedModel())
819 return;
820
821 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Joel Jones80372332017-06-28 00:06:40 +0000822 DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000823 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
824 DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000825
Andrew Trick9ef08822012-09-17 22:18:48 +0000826 SCTab.resize(SCTab.size() + 1);
827 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000828 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000829 SCDesc.NumMicroOps = 0;
830 SCDesc.BeginGroup = false;
831 SCDesc.EndGroup = false;
832 SCDesc.WriteProcResIdx = 0;
833 SCDesc.WriteLatencyIdx = 0;
834 SCDesc.ReadAdvanceIdx = 0;
835
836 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000837 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +0000838 for (const CodeGenSchedTransition &CGT :
839 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
840 if (CGT.ProcIndices[0] == 0 ||
841 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +0000842 HasVariants = true;
843 break;
844 }
845 }
846 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000847 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
848 continue;
849 }
850
851 // Determine if the SchedClass is actually reachable on this processor. If
852 // not don't try to locate the processor resources, it will fail.
853 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000854 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
855 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +0000856 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +0000857 continue;
858 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000859 IdxVec Writes = SC.Writes;
860 IdxVec Reads = SC.Reads;
861 if (!SC.InstRWs.empty()) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000862 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000863 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000864 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000865 for (Record *RW : SC.InstRWs) {
866 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +0000867 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000868 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +0000869 break;
870 }
871 }
872 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000873 Writes.clear();
874 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000875 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
876 Writes, Reads);
877 }
878 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000879 if (Writes.empty()) {
880 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000881 for (Record *I : ProcModel.ItinRWDefs) {
882 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +0000883 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000884 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000885 Writes, Reads);
886 break;
887 }
888 }
889 if (Writes.empty()) {
890 DEBUG(dbgs() << ProcModel.ModelName
Craig Topper29c55dcb2016-02-13 06:03:32 +0000891 << " does not have resources for class " << SC.Name << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000892 }
893 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000894 // Sum resources across all operand writes.
895 std::vector<MCWriteProcResEntry> WriteProcResources;
896 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000897 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000898 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000899 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000900 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000901 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +0000902 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000903
904 // For each operand, create a latency entry.
905 MCWriteLatencyEntry WLEntry;
906 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000907 unsigned WriteID = WriteSeq.back();
908 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
909 // If this Write is not referenced by a ReadAdvance, don't distinguish it
910 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000911 if (!SchedModels.hasReadOfWrite(
912 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000913 WriteID = 0;
914 }
915 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000916
Craig Topper29c55dcb2016-02-13 06:03:32 +0000917 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000918
Andrew Trick9257b8f2012-09-22 02:24:21 +0000919 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000920 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000921
922 // Mark the parent class as invalid for unsupported write types.
923 if (WriteRes->getValueAsBit("Unsupported")) {
924 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
925 break;
926 }
927 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
928 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
929 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
930 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +0000931 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
932 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +0000933
934 // Create an entry for each ProcResource listed in WriteRes.
935 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
936 std::vector<int64_t> Cycles =
937 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000938
939 ExpandProcResources(PRVec, Cycles, ProcModel);
940
Andrew Trick9ef08822012-09-17 22:18:48 +0000941 for (unsigned PRIdx = 0, PREnd = PRVec.size();
942 PRIdx != PREnd; ++PRIdx) {
943 MCWriteProcResEntry WPREntry;
944 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
945 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000946 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000947 // If this resource is already used in this sequence, add the current
948 // entry's cycles so that the same resource appears to be used
949 // serially, rather than multiple parallel uses. This is important for
950 // in-order machine where the resource consumption is a hazard.
951 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
952 for( ; WPRIdx != WPREnd; ++WPRIdx) {
953 if (WriteProcResources[WPRIdx].ProcResourceIdx
954 == WPREntry.ProcResourceIdx) {
955 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
956 break;
957 }
958 }
959 if (WPRIdx == WPREnd)
960 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000961 }
962 }
963 WriteLatencies.push_back(WLEntry);
964 }
965 // Create an entry for each operand Read in this SchedClass.
966 // Entries must be sorted first by UseIdx then by WriteResourceID.
967 for (unsigned UseIdx = 0, EndIdx = Reads.size();
968 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000969 Record *ReadAdvance =
970 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000971 if (!ReadAdvance)
972 continue;
973
974 // Mark the parent class as invalid for unsupported write types.
975 if (ReadAdvance->getValueAsBit("Unsupported")) {
976 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
977 break;
978 }
979 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
980 IdxVec WriteIDs;
981 if (ValidWrites.empty())
982 WriteIDs.push_back(0);
983 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000984 for (Record *VW : ValidWrites) {
985 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +0000986 }
987 }
988 std::sort(WriteIDs.begin(), WriteIDs.end());
Craig Topper29c55dcb2016-02-13 06:03:32 +0000989 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000990 MCReadAdvanceEntry RAEntry;
991 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000992 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +0000993 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
994 ReadAdvanceEntries.push_back(RAEntry);
995 }
996 }
997 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
998 WriteProcResources.clear();
999 WriteLatencies.clear();
1000 ReadAdvanceEntries.clear();
1001 }
1002 // Add the information for this SchedClass to the global tables using basic
1003 // compression.
1004 //
1005 // WritePrecRes entries are sorted by ProcResIdx.
1006 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1007 LessWriteProcResources());
1008
1009 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1010 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1011 std::search(SchedTables.WriteProcResources.begin(),
1012 SchedTables.WriteProcResources.end(),
1013 WriteProcResources.begin(), WriteProcResources.end());
1014 if (WPRPos != SchedTables.WriteProcResources.end())
1015 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1016 else {
1017 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1018 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1019 WriteProcResources.end());
1020 }
1021 // Latency entries must remain in operand order.
1022 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1023 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1024 std::search(SchedTables.WriteLatencies.begin(),
1025 SchedTables.WriteLatencies.end(),
1026 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001027 if (WLPos != SchedTables.WriteLatencies.end()) {
1028 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1029 SCDesc.WriteLatencyIdx = idx;
1030 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1031 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1032 std::string::npos) {
1033 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1034 }
1035 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001036 else {
1037 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001038 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1039 WriteLatencies.begin(),
1040 WriteLatencies.end());
1041 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1042 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001043 }
1044 // ReadAdvanceEntries must remain in operand order.
1045 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1046 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1047 std::search(SchedTables.ReadAdvanceEntries.begin(),
1048 SchedTables.ReadAdvanceEntries.end(),
1049 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1050 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1051 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1052 else {
1053 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1054 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1055 ReadAdvanceEntries.end());
1056 }
1057 }
1058}
1059
Andrew Tricka72fca62012-09-17 22:18:50 +00001060// Emit SchedClass tables for all processors and associated global tables.
1061void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1062 raw_ostream &OS) {
1063 // Emit global WriteProcResTable.
1064 OS << "\n// {ProcResourceIdx, Cycles}\n"
1065 << "extern const llvm::MCWriteProcResEntry "
1066 << Target << "WriteProcResTable[] = {\n"
1067 << " { 0, 0}, // Invalid\n";
1068 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1069 WPRIdx != WPREnd; ++WPRIdx) {
1070 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1071 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1072 << format("%2d", WPREntry.Cycles) << "}";
1073 if (WPRIdx + 1 < WPREnd)
1074 OS << ',';
1075 OS << " // #" << WPRIdx << '\n';
1076 }
1077 OS << "}; // " << Target << "WriteProcResTable\n";
1078
1079 // Emit global WriteLatencyTable.
1080 OS << "\n// {Cycles, WriteResourceID}\n"
1081 << "extern const llvm::MCWriteLatencyEntry "
1082 << Target << "WriteLatencyTable[] = {\n"
1083 << " { 0, 0}, // Invalid\n";
1084 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1085 WLIdx != WLEnd; ++WLIdx) {
1086 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1087 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1088 << format("%2d", WLEntry.WriteResourceID) << "}";
1089 if (WLIdx + 1 < WLEnd)
1090 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001091 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001092 }
1093 OS << "}; // " << Target << "WriteLatencyTable\n";
1094
1095 // Emit global ReadAdvanceTable.
1096 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1097 << "extern const llvm::MCReadAdvanceEntry "
1098 << Target << "ReadAdvanceTable[] = {\n"
1099 << " {0, 0, 0}, // Invalid\n";
1100 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1101 RAIdx != RAEnd; ++RAIdx) {
1102 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1103 OS << " {" << RAEntry.UseIdx << ", "
1104 << format("%2d", RAEntry.WriteResourceID) << ", "
1105 << format("%2d", RAEntry.Cycles) << "}";
1106 if (RAIdx + 1 < RAEnd)
1107 OS << ',';
1108 OS << " // #" << RAIdx << '\n';
1109 }
1110 OS << "}; // " << Target << "ReadAdvanceTable\n";
1111
1112 // Emit a SchedClass table for each processor.
1113 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1114 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1115 if (!PI->hasInstrSchedModel())
1116 continue;
1117
1118 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001119 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001120
1121 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1122 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1123 OS << "static const llvm::MCSchedClassDesc "
1124 << PI->ModelName << "SchedClasses[] = {\n";
1125
1126 // The first class is always invalid. We no way to distinguish it except by
1127 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001128 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001129 && "invalid class not first");
1130 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1131 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001132 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001133
1134 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1135 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1136 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1137 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1138 if (SchedClass.Name.size() < 18)
1139 OS.indent(18 - SchedClass.Name.size());
1140 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001141 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1142 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001143 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1144 << ", " << MCDesc.NumWriteProcResEntries
1145 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1146 << ", " << MCDesc.NumWriteLatencyEntries
1147 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001148 << ", " << MCDesc.NumReadAdvanceEntries
1149 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001150 }
1151 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1152 }
1153}
1154
Andrew Trick87255e32012-07-07 04:00:00 +00001155void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1156 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001157 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001158 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001159 if (PM.hasInstrSchedModel())
1160 EmitProcessorResources(PM, OS);
1161 else if(!PM.ProcResourceDefs.empty())
1162 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001163 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001164
Andrew Trick73d77362012-06-05 03:44:40 +00001165 // Begin processor itinerary properties
1166 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001167 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1168 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1169 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1170 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1171 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1172 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1173 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001174
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001175 bool PostRAScheduler =
1176 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001177
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001178 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1179 << "PostRAScheduler\n";
1180
1181 bool CompleteModel =
1182 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1183
1184 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1185 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001186
Craig Topper29c55dcb2016-02-13 06:03:32 +00001187 OS << " " << PM.Index << ", // Processor ID\n";
1188 if (PM.hasInstrSchedModel())
1189 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1190 << " " << PM.ModelName << "SchedClasses" << ",\n"
1191 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001192 << " " << (SchedModels.schedClassEnd()
1193 - SchedModels.schedClassBegin()) << ",\n";
1194 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001195 OS << " nullptr, nullptr, 0, 0,"
1196 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001197 if (PM.hasItineraries())
Craig Topper194cb742017-10-24 15:50:55 +00001198 OS << " " << PM.ItinsDef->getName() << "\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001199 else
Craig Topper194cb742017-10-24 15:50:55 +00001200 OS << " nullptr // No Itinerary\n";
1201 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001202 }
Jim Laskey3763a502005-10-31 17:16:01 +00001203}
1204
1205//
1206// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1207//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001208void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001209 // Gather and sort processor information
1210 std::vector<Record*> ProcessorList =
1211 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001212 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001213
1214 // Begin processor table
1215 OS << "\n";
1216 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001217 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001218 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001219
Jim Laskey3763a502005-10-31 17:16:01 +00001220 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001221 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001222 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001223 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001224 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001225
Jim Laskey3763a502005-10-31 17:16:01 +00001226 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001227 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001228 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001229
Jim Laskey3763a502005-10-31 17:16:01 +00001230 // End processor table
1231 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001232}
1233
1234//
Andrew Trick87255e32012-07-07 04:00:00 +00001235// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001236//
Andrew Trick87255e32012-07-07 04:00:00 +00001237void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001238 OS << "#ifdef DBGFIELD\n"
1239 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1240 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001241 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001242 << "#define DBGFIELD(x) x,\n"
1243 << "#else\n"
1244 << "#define DBGFIELD(x)\n"
1245 << "#endif\n";
1246
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001247 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001248 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001249 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001250 EmitStageAndOperandCycleData(OS, ProcItinLists);
1251 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001252 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001253 OS << "\n// ===============================================================\n"
1254 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001255
Andrew Trick9ef08822012-09-17 22:18:48 +00001256 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001257 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1258 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001259 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001260 EmitSchedClassTables(SchedTables, OS);
1261
1262 // Emit the processor machine model
1263 EmitProcessorModels(OS);
1264 // Emit the processor lookup data
1265 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001266
Craig Topper194cb742017-10-24 15:50:55 +00001267 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001268}
1269
Benjamin Kramerc321e532016-06-08 19:09:22 +00001270void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
Andrew Trickc6c88152012-09-18 03:41:43 +00001271 raw_ostream &OS) {
1272 OS << "unsigned " << ClassName
1273 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1274 << " const TargetSchedModel *SchedModel) const {\n";
1275
1276 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1277 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
Craig Topper29c55dcb2016-02-13 06:03:32 +00001278 for (Record *P : Prologs) {
1279 OS << P->getValueAsString("Code") << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001280 }
1281 IdxVec VariantClasses;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001282 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1283 if (SC.Transitions.empty())
Andrew Trickc6c88152012-09-18 03:41:43 +00001284 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001285 VariantClasses.push_back(SC.Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001286 }
1287 if (!VariantClasses.empty()) {
1288 OS << " switch (SchedClass) {\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001289 for (unsigned VC : VariantClasses) {
1290 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1291 OS << " case " << VC << ": // " << SC.Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001292 IdxVec ProcIndices;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001293 for (const CodeGenSchedTransition &T : SC.Transitions) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001294 IdxVec PI;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001295 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
Andrew Trickc6c88152012-09-18 03:41:43 +00001296 ProcIndices.begin(), ProcIndices.end(),
1297 std::back_inserter(PI));
1298 ProcIndices.swap(PI);
1299 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001300 for (unsigned PI : ProcIndices) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001301 OS << " ";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001302 if (PI != 0)
1303 OS << "if (SchedModel->getProcessorID() == " << PI << ") ";
1304 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName
Andrew Trickc6c88152012-09-18 03:41:43 +00001305 << '\n';
Craig Topper29c55dcb2016-02-13 06:03:32 +00001306 for (const CodeGenSchedTransition &T : SC.Transitions) {
1307 if (PI != 0 && !std::count(T.ProcIndices.begin(),
1308 T.ProcIndices.end(), PI)) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001309 continue;
1310 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001311 OS << " if (";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001312 for (RecIter RI = T.PredTerm.begin(), RE = T.PredTerm.end();
Andrew Trickc6c88152012-09-18 03:41:43 +00001313 RI != RE; ++RI) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001314 if (RI != T.PredTerm.begin())
Andrew Trickc6c88152012-09-18 03:41:43 +00001315 OS << "\n && ";
1316 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1317 }
1318 OS << ")\n"
Craig Topper29c55dcb2016-02-13 06:03:32 +00001319 << " return " << T.ToClassIdx << "; // "
1320 << SchedModels.getSchedClass(T.ToClassIdx).Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001321 }
1322 OS << " }\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001323 if (PI == 0)
Andrew Trickc6c88152012-09-18 03:41:43 +00001324 break;
1325 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001326 if (SC.isInferred())
1327 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001328 OS << " break;\n";
1329 }
1330 OS << " };\n";
1331 }
1332 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1333 << "} // " << ClassName << "::resolveSchedClass\n";
1334}
1335
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001336void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1337 raw_ostream &OS) {
1338 const CodeGenHwModes &CGH = TGT.getHwModes();
1339 assert(CGH.getNumModeIds() > 0);
1340 if (CGH.getNumModeIds() == 1)
1341 return;
1342
1343 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1344 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1345 const HwMode &HM = CGH.getMode(M);
1346 OS << " if (checkFeatures(\"" << HM.Features
1347 << "\")) return " << M << ";\n";
1348 }
1349 OS << " return 0;\n}\n";
1350}
1351
Jim Laskey86f002c2005-10-27 19:47:21 +00001352//
Jim Laskeya2b52352005-10-26 17:30:34 +00001353// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1354// the subtarget features string.
1355//
Evan Cheng54b68e32011-07-01 20:45:01 +00001356void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1357 unsigned NumFeatures,
1358 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001359 std::vector<Record*> Features =
1360 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001361 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001362
Andrew Trickdb6ed642011-04-01 01:56:55 +00001363 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1364 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001365 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001366 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001367 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001368 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001369 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001370
1371 if (Features.empty()) {
1372 OS << "}\n";
1373 return;
1374 }
1375
Andrew Trickba7b9212012-09-18 05:33:15 +00001376 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001377 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001378
Craig Topper29c55dcb2016-02-13 06:03:32 +00001379 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001380 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001381 StringRef Instance = R->getName();
1382 StringRef Value = R->getValueAsString("Value");
1383 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001384
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001385 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001386 OS << " if (Bits[" << Target << "::"
1387 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001388 << Attribute << " = " << Value << ";\n";
1389 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001390 OS << " if (Bits[" << Target << "::"
1391 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001392 << Attribute << " < " << Value << ") "
1393 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001394 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001395
Evan Chengfe6e4052011-06-30 01:53:36 +00001396 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001397}
1398
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001399//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001400// SubtargetEmitter::run - Main subtarget enumeration emitter.
1401//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001402void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001403 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001404
Evan Cheng4d1ca962011-07-08 01:53:10 +00001405 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001406 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001407
1408 OS << "namespace llvm {\n";
Craig Topper094bbca2016-02-14 05:22:01 +00001409 Enumeration(OS);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001410 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001411 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1412
Evan Cheng54b68e32011-07-01 20:45:01 +00001413 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001414 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001415
Evan Cheng54b68e32011-07-01 20:45:01 +00001416 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001417#if 0
1418 OS << "namespace {\n";
1419#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001420 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001421 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001422 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001423 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001424 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001425 OS << "\n";
1426#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001427 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001428#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001429
1430 // MCInstrInfo initialization routine.
Craig Topper194cb742017-10-24 15:50:55 +00001431 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001432 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001433 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001434 OS << " return new MCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001435 if (NumFeatures)
1436 OS << Target << "FeatureKV, ";
1437 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001438 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001439 if (NumProcs)
1440 OS << Target << "SubTypeKV, ";
1441 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001442 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001443 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001444 OS << Target << "ProcSchedKV, "
1445 << Target << "WriteProcResTable, "
1446 << Target << "WriteLatencyTable, "
1447 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001448 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001449 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001450 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001451 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001452 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001453 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001454 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001455 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001456
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001457 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001458
1459 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1460
1461 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001462 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001463
1464 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001465 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001466 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1467
1468 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1469
Evan Cheng0d639a22011-07-01 21:01:15 +00001470 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001471 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001472 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001473
1474 std::string ClassName = Target + "GenSubtargetInfo";
1475 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001476 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001477 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001478 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001479 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001480 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001481 << " unsigned resolveSchedClass(unsigned SchedClass, "
1482 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001483 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001484 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001485 << " const;\n";
1486 if (TGT.getHwModes().getNumModeIds() > 1)
1487 OS << " unsigned getHwMode() const override;\n";
1488 OS << "};\n"
1489 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001490
1491 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1492
1493 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001494 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001495
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001496 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001497 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001498 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1499 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001500 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1501 OS << "extern const llvm::MCWriteProcResEntry "
1502 << Target << "WriteProcResTable[];\n";
1503 OS << "extern const llvm::MCWriteLatencyEntry "
1504 << Target << "WriteLatencyTable[];\n";
1505 OS << "extern const llvm::MCReadAdvanceEntry "
1506 << Target << "ReadAdvanceTable[];\n";
1507
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001508 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001509 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1510 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001511 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001512 }
1513
Daniel Sanders50f17232015-09-15 16:17:27 +00001514 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1515 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001516 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001517 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001518 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001519 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001520 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001521 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001522 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001523 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001524 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001525 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001526 OS << Target << "ProcSchedKV, "
1527 << Target << "WriteProcResTable, "
1528 << Target << "WriteLatencyTable, "
1529 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001530 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001531 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001532 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001533 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001534 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001535 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001536 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001537 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001538
Andrew Trickc6c88152012-09-18 03:41:43 +00001539 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001540 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001541
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001542 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001543
1544 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001545}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001546
1547namespace llvm {
1548
1549void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001550 CodeGenTarget CGTarget(RK);
1551 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001552}
1553
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001554} // end namespace llvm