blob: 15018268643ea75d18316750847ae885198354dc [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
Craig Topper37eeb322018-03-23 00:02:45 +0000197 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000198
Craig Topper4ceea0a2016-01-03 08:57:41 +0000199 OS << "{";
200 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
201 OS << " " << Target << "::" << ImpliesList[j]->getName();
202 if (++j < M) OS << ",";
Bill Wendlinge6182262007-05-04 20:38:40 +0000203 }
Craig Topperdf1285b2017-10-24 15:50:53 +0000204 OS << " } },\n";
Evan Cheng54b68e32011-07-01 20:45:01 +0000205 ++NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000206 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000207
Jim Laskey19595752005-10-28 15:20:43 +0000208 // End feature table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000209 OS << "};\n";
210
Evan Cheng54b68e32011-07-01 20:45:01 +0000211 return NumFeatures;
Jim Laskey1b7369b2005-10-25 15:16:36 +0000212}
213
214//
215// CPUKeyValues - Emit data of all the subtarget processors. Used by command
216// line.
217//
Evan Cheng54b68e32011-07-01 20:45:01 +0000218unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey19595752005-10-28 15:20:43 +0000219 // Gather and sort processor information
Jim Laskeydffe5972005-10-28 21:47:29 +0000220 std::vector<Record*> ProcessorList =
221 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +0000222 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey1b7369b2005-10-25 15:16:36 +0000223
Jim Laskey19595752005-10-28 15:20:43 +0000224 // Begin processor table
Jim Laskeya2b52352005-10-26 17:30:34 +0000225 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000226 << "extern const llvm::SubtargetFeatureKV " << Target
227 << "SubTypeKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000228
Jim Laskey19595752005-10-28 15:20:43 +0000229 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +0000230 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000231 StringRef Name = Processor->getValueAsString("Name");
Craig Topper37eeb322018-03-23 00:02:45 +0000232 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000233
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000234 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskey1b7369b2005-10-25 15:16:36 +0000235 OS << " { "
236 << "\"" << Name << "\", "
237 << "\"Select the " << Name << " processor\", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000238
Craig Topper4ceea0a2016-01-03 08:57:41 +0000239 OS << "{";
240 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
241 OS << " " << Target << "::" << FeatureList[j]->getName();
242 if (++j < M) OS << ",";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000243 }
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000244 // The { } is for the "implies" section of this data structure.
Craig Topperdf1285b2017-10-24 15:50:53 +0000245 OS << " }, { } },\n";
Jim Laskey1b7369b2005-10-25 15:16:36 +0000246 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000247
Jim Laskey19595752005-10-28 15:20:43 +0000248 // End processor table
Jim Laskey1b7369b2005-10-25 15:16:36 +0000249 OS << "};\n";
250
Evan Cheng54b68e32011-07-01 20:45:01 +0000251 return ProcessorList.size();
Jim Laskey1b7369b2005-10-25 15:16:36 +0000252}
Jim Laskeya1beea62005-10-22 07:59:56 +0000253
Jim Laskeya2b52352005-10-26 17:30:34 +0000254//
David Goodwind813cbf2009-08-17 16:02:57 +0000255// FormItineraryStageString - Compose a string containing the stage
256// data initialization for the specified itinerary. N is the number
257// of stages.
Jim Laskey86f002c2005-10-27 19:47:21 +0000258//
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000259void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
260 Record *ItinData,
David Goodwind813cbf2009-08-17 16:02:57 +0000261 std::string &ItinString,
262 unsigned &NStages) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000263 // Get states list
Craig Topper37eeb322018-03-23 00:02:45 +0000264 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey19595752005-10-28 15:20:43 +0000265
266 // For each stage
Jim Laskeydffe5972005-10-28 21:47:29 +0000267 unsigned N = NStages = StageList.size();
Christopher Lamb8996dce2007-04-22 09:04:24 +0000268 for (unsigned i = 0; i < N;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000269 // Next stage
Bill Wendlinge6182262007-05-04 20:38:40 +0000270 const Record *Stage = StageList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000271
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000272 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey86f002c2005-10-27 19:47:21 +0000273 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskeyd6d3afb2005-11-03 22:47:41 +0000274 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000275
Jim Laskeydffe5972005-10-28 21:47:29 +0000276 // Get unit list
Craig Topper37eeb322018-03-23 00:02:45 +0000277 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickdb6ed642011-04-01 01:56:55 +0000278
Jim Laskey19595752005-10-28 15:20:43 +0000279 // For each unit
Jim Laskeydffe5972005-10-28 21:47:29 +0000280 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeydffe5972005-10-28 21:47:29 +0000281 // Add name and bitwise or
Matthias Braun4a86d452016-12-04 05:48:16 +0000282 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeydffe5972005-10-28 21:47:29 +0000283 if (++j < M) ItinString += " | ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000284 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000285
David Goodwinb369ee42009-08-12 18:31:53 +0000286 int TimeInc = Stage->getValueAsInt("TimeInc");
287 ItinString += ", " + itostr(TimeInc);
288
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000289 int Kind = Stage->getValueAsInt("Kind");
290 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
291
Jim Laskey19595752005-10-28 15:20:43 +0000292 // Close off stage
293 ItinString += " }";
Christopher Lamb8996dce2007-04-22 09:04:24 +0000294 if (++i < N) ItinString += ", ";
Jim Laskey86f002c2005-10-27 19:47:21 +0000295 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000296}
297
298//
David Goodwind813cbf2009-08-17 16:02:57 +0000299// FormItineraryOperandCycleString - Compose a string containing the
300// operand cycle initialization for the specified itinerary. N is the
301// number of operands that has cycles specified.
Jim Laskey86f002c2005-10-27 19:47:21 +0000302//
David Goodwind813cbf2009-08-17 16:02:57 +0000303void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
304 std::string &ItinString, unsigned &NOperandCycles) {
305 // Get operand cycle list
Craig Topper37eeb322018-03-23 00:02:45 +0000306 std::vector<int64_t> OperandCycleList =
David Goodwind813cbf2009-08-17 16:02:57 +0000307 ItinData->getValueAsListOfInts("OperandCycles");
308
309 // For each operand cycle
310 unsigned N = NOperandCycles = OperandCycleList.size();
311 for (unsigned i = 0; i < N;) {
312 // Next operand cycle
313 const int OCycle = OperandCycleList[i];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000314
David Goodwind813cbf2009-08-17 16:02:57 +0000315 ItinString += " " + itostr(OCycle);
316 if (++i < N) ItinString += ", ";
317 }
318}
319
Evan Cheng0097dd02010-09-28 23:50:49 +0000320void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
321 Record *ItinData,
322 std::string &ItinString,
323 unsigned NOperandCycles) {
Craig Topper37eeb322018-03-23 00:02:45 +0000324 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng0097dd02010-09-28 23:50:49 +0000325 unsigned N = BypassList.size();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000326 unsigned i = 0;
327 for (; i < N;) {
Matthias Braun4a86d452016-12-04 05:48:16 +0000328 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng4a010fd2010-09-29 22:42:35 +0000329 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000330 }
Evan Cheng4a010fd2010-09-29 22:42:35 +0000331 for (; i < NOperandCycles;) {
Evan Cheng0097dd02010-09-28 23:50:49 +0000332 ItinString += " 0";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000333 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng0097dd02010-09-28 23:50:49 +0000334 }
335}
336
David Goodwind813cbf2009-08-17 16:02:57 +0000337//
Andrew Trick87255e32012-07-07 04:00:00 +0000338// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
339// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
340// by CodeGenSchedClass::Index.
David Goodwind813cbf2009-08-17 16:02:57 +0000341//
Andrew Trick87255e32012-07-07 04:00:00 +0000342void SubtargetEmitter::
343EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000344 std::vector<std::vector<InstrItinerary>>
Andrew Trick87255e32012-07-07 04:00:00 +0000345 &ProcItinLists) {
Andrew Trickfb982dd2012-07-09 20:43:03 +0000346 // Multiple processor models may share an itinerary record. Emit it once.
347 SmallPtrSet<Record*, 8> ItinsDefSet;
348
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000349 // Emit functional units for all the itineraries.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000350 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000351
Craig Topper29c55dcb2016-02-13 06:03:32 +0000352 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickfb982dd2012-07-09 20:43:03 +0000353 continue;
354
Craig Topper37eeb322018-03-23 00:02:45 +0000355 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000356 if (FUs.empty())
357 continue;
358
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000359 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick87255e32012-07-07 04:00:00 +0000360 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000361 << "namespace " << Name << "FU {\n";
362
363 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkel8db55472012-06-22 20:27:13 +0000364 OS << " const unsigned " << FUs[j]->getName()
365 << " = 1 << " << j << ";\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000366
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000367 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000368
Craig Topper37eeb322018-03-23 00:02:45 +0000369 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000370 if (!BPs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000371 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng4a010fd2010-09-29 22:42:35 +0000372 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000373
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000374 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000375 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000376 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng4a010fd2010-09-29 22:42:35 +0000377 << " = 1 << " << j << ";\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000378
Eugene Zelenko75259bb2016-05-17 17:04:23 +0000379 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng4a010fd2010-09-29 22:42:35 +0000380 }
Anton Korobeynikov7d62e332010-04-18 20:31:01 +0000381 }
382
Jim Laskey19595752005-10-28 15:20:43 +0000383 // Begin stages table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000384 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
385 "Stages[] = {\n";
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000386 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000387
David Goodwind813cbf2009-08-17 16:02:57 +0000388 // Begin operand cycle table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000389 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng54b68e32011-07-01 20:45:01 +0000390 "OperandCycles[] = {\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000391 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng0097dd02010-09-28 23:50:49 +0000392
393 // Begin pipeline bypass table
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000394 std::string BypassTable = "extern const unsigned " + Target +
Andrew Trick030e2f82012-07-07 03:59:48 +0000395 "ForwardingPaths[] = {\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000396 BypassTable += " 0, // No itinerary\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +0000397
Andrew Trick87255e32012-07-07 04:00:00 +0000398 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berryb2cfea52017-05-08 15:33:08 +0000399 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick87255e32012-07-07 04:00:00 +0000400 // object with computed offsets to the ProcItinLists result.
David Goodwind813cbf2009-08-17 16:02:57 +0000401 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng4a010fd2010-09-29 22:42:35 +0000402 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000403 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick87255e32012-07-07 04:00:00 +0000404 // Add process itinerary to the list.
405 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickdb6ed642011-04-01 01:56:55 +0000406
Andrew Trick87255e32012-07-07 04:00:00 +0000407 // If this processor defines no itineraries, then leave the itinerary list
408 // empty.
409 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000410 if (!ProcModel.hasItineraries())
Andrew Trick9c302672012-06-22 03:58:51 +0000411 continue;
Andrew Trick9c302672012-06-22 03:58:51 +0000412
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +0000413 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickdb6ed642011-04-01 01:56:55 +0000414
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000415 ItinList.resize(SchedModels.numInstrSchedClasses());
416 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
417
418 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick87255e32012-07-07 04:00:00 +0000419 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
420
Jim Laskeydffe5972005-10-28 21:47:29 +0000421 // Next itinerary data
Andrew Trick87255e32012-07-07 04:00:00 +0000422 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickdb6ed642011-04-01 01:56:55 +0000423
Jim Laskey19595752005-10-28 15:20:43 +0000424 // Get string and stage count
David Goodwind813cbf2009-08-17 16:02:57 +0000425 std::string ItinStageString;
Andrew Trick87255e32012-07-07 04:00:00 +0000426 unsigned NStages = 0;
427 if (ItinData)
428 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey86f002c2005-10-27 19:47:21 +0000429
David Goodwind813cbf2009-08-17 16:02:57 +0000430 // Get string and operand cycle count
431 std::string ItinOperandCycleString;
Andrew Trick87255e32012-07-07 04:00:00 +0000432 unsigned NOperandCycles = 0;
Evan Cheng0097dd02010-09-28 23:50:49 +0000433 std::string ItinBypassString;
Andrew Trick87255e32012-07-07 04:00:00 +0000434 if (ItinData) {
435 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
436 NOperandCycles);
437
438 FormItineraryBypassString(Name, ItinData, ItinBypassString,
439 NOperandCycles);
440 }
Evan Cheng0097dd02010-09-28 23:50:49 +0000441
David Goodwind813cbf2009-08-17 16:02:57 +0000442 // Check to see if stage already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000443 uint16_t FindStage = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000444 if (NStages > 0) {
445 FindStage = ItinStageMap[ItinStageString];
446 if (FindStage == 0) {
Andrew Trick8a05f662011-04-01 02:22:47 +0000447 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
448 StageTable += ItinStageString + ", // " + itostr(StageCount);
449 if (NStages > 1)
450 StageTable += "-" + itostr(StageCount + NStages - 1);
451 StageTable += "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000452 // Record Itin class number.
453 ItinStageMap[ItinStageString] = FindStage = StageCount;
454 StageCount += NStages;
David Goodwind813cbf2009-08-17 16:02:57 +0000455 }
456 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000457
David Goodwind813cbf2009-08-17 16:02:57 +0000458 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000459 uint16_t FindOperandCycle = 0;
David Goodwind813cbf2009-08-17 16:02:57 +0000460 if (NOperandCycles > 0) {
Evan Cheng4a010fd2010-09-29 22:42:35 +0000461 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
462 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwind813cbf2009-08-17 16:02:57 +0000463 if (FindOperandCycle == 0) {
464 // Emit as cycle, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000465 OperandCycleTable += ItinOperandCycleString + ", // ";
466 std::string OperandIdxComment = itostr(OperandCycleCount);
467 if (NOperandCycles > 1)
468 OperandIdxComment += "-"
469 + itostr(OperandCycleCount + NOperandCycles - 1);
470 OperandCycleTable += OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000471 // Record Itin class number.
Andrew Trickdb6ed642011-04-01 01:56:55 +0000472 ItinOperandMap[ItinOperandCycleString] =
David Goodwind813cbf2009-08-17 16:02:57 +0000473 FindOperandCycle = OperandCycleCount;
Evan Cheng0097dd02010-09-28 23:50:49 +0000474 // Emit as bypass, // index
Andrew Trick8a05f662011-04-01 02:22:47 +0000475 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwind813cbf2009-08-17 16:02:57 +0000476 OperandCycleCount += NOperandCycles;
David Goodwind813cbf2009-08-17 16:02:57 +0000477 }
Jim Laskey86f002c2005-10-27 19:47:21 +0000478 }
Andrew Trickdb6ed642011-04-01 01:56:55 +0000479
Evan Cheng367a5df2010-09-09 18:18:55 +0000480 // Set up itinerary as location and location + stage count
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000481 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
482 InstrItinerary Intinerary = {
483 NumUOps,
484 FindStage,
485 uint16_t(FindStage + NStages),
486 FindOperandCycle,
487 uint16_t(FindOperandCycle + NOperandCycles),
488 };
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
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000564 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
565 "// end marker\n";
Andrew Trick87255e32012-07-07 04:00:00 +0000566 OS << "};\n";
567 }
568}
569
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000570// Emit either the value defined in the TableGen Record, or the default
Andrew Trick87255e32012-07-07 04:00:00 +0000571// value defined in the C++ header. The Record is null if the processor does not
572// define a model.
573void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Amini32986ed2016-10-04 23:47:33 +0000574 StringRef Name, char Separator) {
Andrew Trick73d77362012-06-05 03:44:40 +0000575 OS << " ";
Andrew Trick87255e32012-07-07 04:00:00 +0000576 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trick73d77362012-06-05 03:44:40 +0000577 if (V >= 0)
578 OS << V << Separator << " // " << Name;
579 else
Andrew Trick87255e32012-07-07 04:00:00 +0000580 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trick73d77362012-06-05 03:44:40 +0000581 OS << '\n';
582}
583
Clement Courbet39911e22018-02-08 08:46:48 +0000584void SubtargetEmitter::EmitProcessorResourceSubUnits(
585 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
586 OS << "\nstatic const unsigned " << ProcModel.ModelName
587 << "ProcResourceSubUnits[] = {\n"
588 << " 0, // Invalid\n";
589
590 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
591 Record *PRDef = ProcModel.ProcResourceDefs[i];
592 if (!PRDef->isSubClassOf("ProcResGroup"))
593 continue;
594 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
595 for (Record *RUDef : ResUnits) {
596 Record *const RU =
597 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
598 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
599 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
600 }
601 }
602 OS << " // " << PRDef->getName() << "\n";
603 }
604 OS << "};\n";
605}
606
Andrew Trick23f3c652012-09-17 22:18:45 +0000607void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
608 raw_ostream &OS) {
Clement Courbet39911e22018-02-08 08:46:48 +0000609 EmitProcessorResourceSubUnits(ProcModel, OS);
610
611 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered, SubUnitsIdxBegin}\n";
David Blaikiee6503d82018-02-08 19:57:05 +0000612 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
613 << "ProcResources"
614 << "[] = {\n"
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000615 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000616
Clement Courbet39911e22018-02-08 08:46:48 +0000617 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000618 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
619 Record *PRDef = ProcModel.ProcResourceDefs[i];
620
Craig Topper24064772014-04-15 07:20:03 +0000621 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000622 unsigned SuperIdx = 0;
623 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000624 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000625 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000626 if (PRDef->isSubClassOf("ProcResGroup")) {
627 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000628 for (Record *RU : ResUnits) {
629 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000630 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000631 }
632 }
633 else {
634 // Find the SuperIdx
635 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000636 SuperDef =
637 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
638 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000639 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
640 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000641 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000642 }
643 // Emit the ProcResourceDesc
Andrea Di Biagio30e94022018-03-08 10:38:45 +0000644 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick23f3c652012-09-17 22:18:45 +0000645 if (PRDef->getName().size() < 15)
646 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000647 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
648 if (SubUnitsBeginOffset != SubUnitsOffset) {
649 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
650 << SubUnitsBeginOffset;
651 } else {
652 OS << "nullptr";
653 }
654 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000655 if (SuperDef)
656 OS << ", Super=" << SuperDef->getName();
657 OS << "\n";
658 }
659 OS << "};\n";
660}
661
Andrew Trick9ef08822012-09-17 22:18:48 +0000662// Find the WriteRes Record that defines processor resources for this
663// SchedWrite.
664Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000665 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000666
667 // Check if the SchedWrite is already subtarget-specific and directly
668 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000669 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
670 return SchedWrite.TheDef;
671
Craig Topper24064772014-04-15 07:20:03 +0000672 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000673 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000674 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000675 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000676 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
677 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
678 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
679 continue;
680 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000681 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000682 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000683 "defined for processor " + ProcModel.ModelName +
684 " Ensure only one SchedAlias exists per RW.");
685 AliasDef = AliasRW.TheDef;
686 }
687 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
688 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000689
690 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000691 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000692 for (Record *WR : ProcModel.WriteResDefs) {
693 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000694 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000695 if (AliasDef == WR->getValueAsDef("WriteType")
696 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000697 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000698 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000699 "SchedWrite and its alias on processor " +
700 ProcModel.ModelName);
701 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000702 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000703 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000704 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000705 // TODO: If ProcModel has a base model (previous generation processor),
706 // then call FindWriteResources recursively with that model here.
707 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000708 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000709 Twine("Processor does not define resources for ") +
710 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000711 }
712 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000713}
714
715/// Find the ReadAdvance record for the given SchedRead on this processor or
716/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000717Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000718 const CodeGenProcModel &ProcModel) {
719 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000720 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
721 return SchedRead.TheDef;
722
723 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000724 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000725 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000726 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000727 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000728 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
729 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
730 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
731 continue;
732 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000733 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000734 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000735 "defined for processor " + ProcModel.ModelName +
736 " Ensure only one SchedAlias exists per RW.");
737 AliasDef = AliasRW.TheDef;
738 }
739 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
740 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000741
742 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000743 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000744 for (Record *RA : ProcModel.ReadAdvanceDefs) {
745 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000746 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000747 if (AliasDef == RA->getValueAsDef("ReadType")
748 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000749 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000750 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000751 "SchedRead and its alias on processor " +
752 ProcModel.ModelName);
753 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000754 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000755 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000756 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000757 // TODO: If ProcModel has a base model (previous generation processor),
758 // then call FindReadAdvance recursively with that model here.
759 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000760 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000761 Twine("Processor does not define resources for ") +
762 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000763 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000764 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000765}
766
Andrew Trick4e67cba2013-03-14 21:21:50 +0000767// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000768// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000769void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
770 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000771 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000772 // Default to 1 resource cycle.
773 Cycles.resize(PRVec.size(), 1);
774 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000775 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000776 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000777 if (PRDef->isSubClassOf("ProcResGroup"))
778 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000779 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000780 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000781 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000782 for (Record *SubDef = PRDef;
783 SubDef->getValueInit("Super")->isComplete();) {
784 if (SubDef->isSubClassOf("ProcResGroup")) {
785 // Disallow this for simplicitly.
786 PrintFatalError(SubDef->getLoc(), "Processor resource group "
787 " cannot be a super resources.");
788 }
789 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000790 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
791 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000792 PRVec.push_back(SuperDef);
793 Cycles.push_back(Cycles[i]);
794 SubDef = SuperDef;
795 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000796 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000797 for (Record *PR : PM.ProcResourceDefs) {
798 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000799 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000800 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000801 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000802 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000803 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000804 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000805 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000806 }
807 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000808 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000809 Cycles.push_back(Cycles[i]);
810 }
811 }
812 }
813}
814
Andrew Trick9ef08822012-09-17 22:18:48 +0000815// Generate the SchedClass table for this processor and update global
816// tables. Must be called for each processor in order.
817void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
818 SchedClassTables &SchedTables) {
819 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
820 if (!ProcModel.hasInstrSchedModel())
821 return;
822
823 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Joel Jones80372332017-06-28 00:06:40 +0000824 DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000825 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
826 DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000827
Andrew Trick9ef08822012-09-17 22:18:48 +0000828 SCTab.resize(SCTab.size() + 1);
829 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000830 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000831 SCDesc.NumMicroOps = 0;
832 SCDesc.BeginGroup = false;
833 SCDesc.EndGroup = false;
834 SCDesc.WriteProcResIdx = 0;
835 SCDesc.WriteLatencyIdx = 0;
836 SCDesc.ReadAdvanceIdx = 0;
837
838 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000839 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +0000840 for (const CodeGenSchedTransition &CGT :
841 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
842 if (CGT.ProcIndices[0] == 0 ||
843 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +0000844 HasVariants = true;
845 break;
846 }
847 }
848 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000849 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
850 continue;
851 }
852
853 // Determine if the SchedClass is actually reachable on this processor. If
854 // not don't try to locate the processor resources, it will fail.
855 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000856 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
857 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +0000858 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +0000859 continue;
860 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000861 IdxVec Writes = SC.Writes;
862 IdxVec Reads = SC.Reads;
863 if (!SC.InstRWs.empty()) {
Sylvestre Ledru543f15b2018-03-17 17:30:08 +0000864 // This class has a default ReadWrite list which can be overridden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000865 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000866 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000867 for (Record *RW : SC.InstRWs) {
868 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +0000869 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000870 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +0000871 break;
872 }
873 }
874 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000875 Writes.clear();
876 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000877 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
878 Writes, Reads);
879 }
880 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000881 if (Writes.empty()) {
882 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000883 for (Record *I : ProcModel.ItinRWDefs) {
884 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +0000885 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000886 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000887 Writes, Reads);
888 break;
889 }
890 }
891 if (Writes.empty()) {
892 DEBUG(dbgs() << ProcModel.ModelName
Craig Topper29c55dcb2016-02-13 06:03:32 +0000893 << " does not have resources for class " << SC.Name << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000894 }
895 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000896 // Sum resources across all operand writes.
897 std::vector<MCWriteProcResEntry> WriteProcResources;
898 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000899 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000900 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000901 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000902 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000903 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +0000904 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000905
906 // For each operand, create a latency entry.
907 MCWriteLatencyEntry WLEntry;
908 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000909 unsigned WriteID = WriteSeq.back();
910 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
911 // If this Write is not referenced by a ReadAdvance, don't distinguish it
912 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000913 if (!SchedModels.hasReadOfWrite(
914 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000915 WriteID = 0;
916 }
917 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000918
Craig Topper29c55dcb2016-02-13 06:03:32 +0000919 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000920
Andrew Trick9257b8f2012-09-22 02:24:21 +0000921 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000922 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000923
924 // Mark the parent class as invalid for unsupported write types.
925 if (WriteRes->getValueAsBit("Unsupported")) {
926 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
927 break;
928 }
929 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
930 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
931 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
932 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +0000933 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
934 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +0000935
936 // Create an entry for each ProcResource listed in WriteRes.
937 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
938 std::vector<int64_t> Cycles =
939 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000940
941 ExpandProcResources(PRVec, Cycles, ProcModel);
942
Andrew Trick9ef08822012-09-17 22:18:48 +0000943 for (unsigned PRIdx = 0, PREnd = PRVec.size();
944 PRIdx != PREnd; ++PRIdx) {
945 MCWriteProcResEntry WPREntry;
946 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
947 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000948 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000949 // If this resource is already used in this sequence, add the current
950 // entry's cycles so that the same resource appears to be used
951 // serially, rather than multiple parallel uses. This is important for
952 // in-order machine where the resource consumption is a hazard.
953 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
954 for( ; WPRIdx != WPREnd; ++WPRIdx) {
955 if (WriteProcResources[WPRIdx].ProcResourceIdx
956 == WPREntry.ProcResourceIdx) {
957 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
958 break;
959 }
960 }
961 if (WPRIdx == WPREnd)
962 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000963 }
964 }
965 WriteLatencies.push_back(WLEntry);
966 }
967 // Create an entry for each operand Read in this SchedClass.
968 // Entries must be sorted first by UseIdx then by WriteResourceID.
969 for (unsigned UseIdx = 0, EndIdx = Reads.size();
970 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000971 Record *ReadAdvance =
972 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000973 if (!ReadAdvance)
974 continue;
975
976 // Mark the parent class as invalid for unsupported write types.
977 if (ReadAdvance->getValueAsBit("Unsupported")) {
978 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
979 break;
980 }
981 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
982 IdxVec WriteIDs;
983 if (ValidWrites.empty())
984 WriteIDs.push_back(0);
985 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000986 for (Record *VW : ValidWrites) {
987 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +0000988 }
989 }
990 std::sort(WriteIDs.begin(), WriteIDs.end());
Craig Topper29c55dcb2016-02-13 06:03:32 +0000991 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000992 MCReadAdvanceEntry RAEntry;
993 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000994 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +0000995 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
996 ReadAdvanceEntries.push_back(RAEntry);
997 }
998 }
999 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1000 WriteProcResources.clear();
1001 WriteLatencies.clear();
1002 ReadAdvanceEntries.clear();
1003 }
1004 // Add the information for this SchedClass to the global tables using basic
1005 // compression.
1006 //
1007 // WritePrecRes entries are sorted by ProcResIdx.
1008 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1009 LessWriteProcResources());
1010
1011 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1012 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1013 std::search(SchedTables.WriteProcResources.begin(),
1014 SchedTables.WriteProcResources.end(),
1015 WriteProcResources.begin(), WriteProcResources.end());
1016 if (WPRPos != SchedTables.WriteProcResources.end())
1017 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1018 else {
1019 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1020 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1021 WriteProcResources.end());
1022 }
1023 // Latency entries must remain in operand order.
1024 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1025 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1026 std::search(SchedTables.WriteLatencies.begin(),
1027 SchedTables.WriteLatencies.end(),
1028 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001029 if (WLPos != SchedTables.WriteLatencies.end()) {
1030 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1031 SCDesc.WriteLatencyIdx = idx;
1032 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1033 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1034 std::string::npos) {
1035 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1036 }
1037 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001038 else {
1039 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001040 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1041 WriteLatencies.begin(),
1042 WriteLatencies.end());
1043 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1044 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001045 }
1046 // ReadAdvanceEntries must remain in operand order.
1047 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1048 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1049 std::search(SchedTables.ReadAdvanceEntries.begin(),
1050 SchedTables.ReadAdvanceEntries.end(),
1051 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1052 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1053 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1054 else {
1055 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1056 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1057 ReadAdvanceEntries.end());
1058 }
1059 }
1060}
1061
Andrew Tricka72fca62012-09-17 22:18:50 +00001062// Emit SchedClass tables for all processors and associated global tables.
1063void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1064 raw_ostream &OS) {
1065 // Emit global WriteProcResTable.
1066 OS << "\n// {ProcResourceIdx, Cycles}\n"
1067 << "extern const llvm::MCWriteProcResEntry "
1068 << Target << "WriteProcResTable[] = {\n"
1069 << " { 0, 0}, // Invalid\n";
1070 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1071 WPRIdx != WPREnd; ++WPRIdx) {
1072 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1073 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1074 << format("%2d", WPREntry.Cycles) << "}";
1075 if (WPRIdx + 1 < WPREnd)
1076 OS << ',';
1077 OS << " // #" << WPRIdx << '\n';
1078 }
1079 OS << "}; // " << Target << "WriteProcResTable\n";
1080
1081 // Emit global WriteLatencyTable.
1082 OS << "\n// {Cycles, WriteResourceID}\n"
1083 << "extern const llvm::MCWriteLatencyEntry "
1084 << Target << "WriteLatencyTable[] = {\n"
1085 << " { 0, 0}, // Invalid\n";
1086 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1087 WLIdx != WLEnd; ++WLIdx) {
1088 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1089 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1090 << format("%2d", WLEntry.WriteResourceID) << "}";
1091 if (WLIdx + 1 < WLEnd)
1092 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001093 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001094 }
1095 OS << "}; // " << Target << "WriteLatencyTable\n";
1096
1097 // Emit global ReadAdvanceTable.
1098 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1099 << "extern const llvm::MCReadAdvanceEntry "
1100 << Target << "ReadAdvanceTable[] = {\n"
1101 << " {0, 0, 0}, // Invalid\n";
1102 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1103 RAIdx != RAEnd; ++RAIdx) {
1104 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1105 OS << " {" << RAEntry.UseIdx << ", "
1106 << format("%2d", RAEntry.WriteResourceID) << ", "
1107 << format("%2d", RAEntry.Cycles) << "}";
1108 if (RAIdx + 1 < RAEnd)
1109 OS << ',';
1110 OS << " // #" << RAIdx << '\n';
1111 }
1112 OS << "}; // " << Target << "ReadAdvanceTable\n";
1113
1114 // Emit a SchedClass table for each processor.
1115 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1116 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1117 if (!PI->hasInstrSchedModel())
1118 continue;
1119
1120 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001121 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001122
1123 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1124 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1125 OS << "static const llvm::MCSchedClassDesc "
1126 << PI->ModelName << "SchedClasses[] = {\n";
1127
1128 // The first class is always invalid. We no way to distinguish it except by
1129 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001130 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001131 && "invalid class not first");
1132 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1133 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001134 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001135
1136 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1137 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1138 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1139 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1140 if (SchedClass.Name.size() < 18)
1141 OS.indent(18 - SchedClass.Name.size());
1142 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001143 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1144 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001145 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1146 << ", " << MCDesc.NumWriteProcResEntries
1147 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1148 << ", " << MCDesc.NumWriteLatencyEntries
1149 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001150 << ", " << MCDesc.NumReadAdvanceEntries
1151 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001152 }
1153 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1154 }
1155}
1156
Andrew Trick87255e32012-07-07 04:00:00 +00001157void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1158 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001159 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001160 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001161 if (PM.hasInstrSchedModel())
1162 EmitProcessorResources(PM, OS);
1163 else if(!PM.ProcResourceDefs.empty())
1164 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001165 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001166
Andrew Trick73d77362012-06-05 03:44:40 +00001167 // Begin processor itinerary properties
1168 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001169 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1170 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1171 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1172 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1173 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1174 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1175 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001176
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001177 bool PostRAScheduler =
1178 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001179
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001180 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1181 << "PostRAScheduler\n";
1182
1183 bool CompleteModel =
1184 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1185
1186 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1187 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001188
Craig Topper29c55dcb2016-02-13 06:03:32 +00001189 OS << " " << PM.Index << ", // Processor ID\n";
1190 if (PM.hasInstrSchedModel())
1191 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1192 << " " << PM.ModelName << "SchedClasses" << ",\n"
1193 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001194 << " " << (SchedModels.schedClassEnd()
1195 - SchedModels.schedClassBegin()) << ",\n";
1196 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001197 OS << " nullptr, nullptr, 0, 0,"
1198 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001199 if (PM.hasItineraries())
Craig Topper194cb742017-10-24 15:50:55 +00001200 OS << " " << PM.ItinsDef->getName() << "\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001201 else
Craig Topper194cb742017-10-24 15:50:55 +00001202 OS << " nullptr // No Itinerary\n";
1203 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001204 }
Jim Laskey3763a502005-10-31 17:16:01 +00001205}
1206
1207//
1208// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1209//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001210void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001211 // Gather and sort processor information
1212 std::vector<Record*> ProcessorList =
1213 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001214 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001215
1216 // Begin processor table
1217 OS << "\n";
1218 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001219 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001220 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001221
Jim Laskey3763a502005-10-31 17:16:01 +00001222 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001223 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001224 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001225 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001226 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001227
Jim Laskey3763a502005-10-31 17:16:01 +00001228 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001229 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001230 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001231
Jim Laskey3763a502005-10-31 17:16:01 +00001232 // End processor table
1233 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001234}
1235
1236//
Andrew Trick87255e32012-07-07 04:00:00 +00001237// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001238//
Andrew Trick87255e32012-07-07 04:00:00 +00001239void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001240 OS << "#ifdef DBGFIELD\n"
1241 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1242 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001243 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001244 << "#define DBGFIELD(x) x,\n"
1245 << "#else\n"
1246 << "#define DBGFIELD(x)\n"
1247 << "#endif\n";
1248
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001249 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001250 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001251 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001252 EmitStageAndOperandCycleData(OS, ProcItinLists);
1253 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001254 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001255 OS << "\n// ===============================================================\n"
1256 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001257
Andrew Trick9ef08822012-09-17 22:18:48 +00001258 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001259 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1260 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001261 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001262 EmitSchedClassTables(SchedTables, OS);
1263
1264 // Emit the processor machine model
1265 EmitProcessorModels(OS);
1266 // Emit the processor lookup data
1267 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001268
Craig Topper194cb742017-10-24 15:50:55 +00001269 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001270}
1271
Benjamin Kramerc321e532016-06-08 19:09:22 +00001272void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
Andrew Trickc6c88152012-09-18 03:41:43 +00001273 raw_ostream &OS) {
1274 OS << "unsigned " << ClassName
1275 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1276 << " const TargetSchedModel *SchedModel) const {\n";
1277
1278 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1279 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
Craig Topper29c55dcb2016-02-13 06:03:32 +00001280 for (Record *P : Prologs) {
1281 OS << P->getValueAsString("Code") << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001282 }
1283 IdxVec VariantClasses;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001284 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1285 if (SC.Transitions.empty())
Andrew Trickc6c88152012-09-18 03:41:43 +00001286 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001287 VariantClasses.push_back(SC.Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001288 }
1289 if (!VariantClasses.empty()) {
1290 OS << " switch (SchedClass) {\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001291 for (unsigned VC : VariantClasses) {
1292 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1293 OS << " case " << VC << ": // " << SC.Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001294 IdxVec ProcIndices;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001295 for (const CodeGenSchedTransition &T : SC.Transitions) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001296 IdxVec PI;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001297 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
Andrew Trickc6c88152012-09-18 03:41:43 +00001298 ProcIndices.begin(), ProcIndices.end(),
1299 std::back_inserter(PI));
1300 ProcIndices.swap(PI);
1301 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001302 for (unsigned PI : ProcIndices) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001303 OS << " ";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001304 if (PI != 0)
1305 OS << "if (SchedModel->getProcessorID() == " << PI << ") ";
1306 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName
Andrew Trickc6c88152012-09-18 03:41:43 +00001307 << '\n';
Craig Topper29c55dcb2016-02-13 06:03:32 +00001308 for (const CodeGenSchedTransition &T : SC.Transitions) {
1309 if (PI != 0 && !std::count(T.ProcIndices.begin(),
1310 T.ProcIndices.end(), PI)) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001311 continue;
1312 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001313 OS << " if (";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001314 for (RecIter RI = T.PredTerm.begin(), RE = T.PredTerm.end();
Andrew Trickc6c88152012-09-18 03:41:43 +00001315 RI != RE; ++RI) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001316 if (RI != T.PredTerm.begin())
Andrew Trickc6c88152012-09-18 03:41:43 +00001317 OS << "\n && ";
1318 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1319 }
1320 OS << ")\n"
Craig Topper29c55dcb2016-02-13 06:03:32 +00001321 << " return " << T.ToClassIdx << "; // "
1322 << SchedModels.getSchedClass(T.ToClassIdx).Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001323 }
1324 OS << " }\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001325 if (PI == 0)
Andrew Trickc6c88152012-09-18 03:41:43 +00001326 break;
1327 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001328 if (SC.isInferred())
1329 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001330 OS << " break;\n";
1331 }
1332 OS << " };\n";
1333 }
1334 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1335 << "} // " << ClassName << "::resolveSchedClass\n";
1336}
1337
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001338void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1339 raw_ostream &OS) {
1340 const CodeGenHwModes &CGH = TGT.getHwModes();
1341 assert(CGH.getNumModeIds() > 0);
1342 if (CGH.getNumModeIds() == 1)
1343 return;
1344
1345 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1346 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1347 const HwMode &HM = CGH.getMode(M);
1348 OS << " if (checkFeatures(\"" << HM.Features
1349 << "\")) return " << M << ";\n";
1350 }
1351 OS << " return 0;\n}\n";
1352}
1353
Jim Laskey86f002c2005-10-27 19:47:21 +00001354//
Jim Laskeya2b52352005-10-26 17:30:34 +00001355// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1356// the subtarget features string.
1357//
Evan Cheng54b68e32011-07-01 20:45:01 +00001358void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1359 unsigned NumFeatures,
1360 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001361 std::vector<Record*> Features =
1362 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001363 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001364
Andrew Trickdb6ed642011-04-01 01:56:55 +00001365 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1366 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001367 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001368 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001369 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001370 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001371 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001372
1373 if (Features.empty()) {
1374 OS << "}\n";
1375 return;
1376 }
1377
Andrew Trickba7b9212012-09-18 05:33:15 +00001378 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001379 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001380
Craig Topper29c55dcb2016-02-13 06:03:32 +00001381 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001382 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001383 StringRef Instance = R->getName();
1384 StringRef Value = R->getValueAsString("Value");
1385 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001386
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001387 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001388 OS << " if (Bits[" << Target << "::"
1389 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001390 << Attribute << " = " << Value << ";\n";
1391 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001392 OS << " if (Bits[" << Target << "::"
1393 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001394 << Attribute << " < " << Value << ") "
1395 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001396 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001397
Evan Chengfe6e4052011-06-30 01:53:36 +00001398 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001399}
1400
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001401//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001402// SubtargetEmitter::run - Main subtarget enumeration emitter.
1403//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001404void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001405 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001406
Evan Cheng4d1ca962011-07-08 01:53:10 +00001407 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001408 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001409
1410 OS << "namespace llvm {\n";
Craig Topper094bbca2016-02-14 05:22:01 +00001411 Enumeration(OS);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001412 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001413 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1414
Evan Cheng54b68e32011-07-01 20:45:01 +00001415 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001416 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001417
Evan Cheng54b68e32011-07-01 20:45:01 +00001418 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001419#if 0
1420 OS << "namespace {\n";
1421#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001422 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001423 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001424 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001425 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001426 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001427 OS << "\n";
1428#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001429 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001430#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001431
1432 // MCInstrInfo initialization routine.
Craig Topper194cb742017-10-24 15:50:55 +00001433 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001434 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001435 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001436 OS << " return new MCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001437 if (NumFeatures)
1438 OS << Target << "FeatureKV, ";
1439 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001440 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001441 if (NumProcs)
1442 OS << Target << "SubTypeKV, ";
1443 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001444 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001445 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001446 OS << Target << "ProcSchedKV, "
1447 << Target << "WriteProcResTable, "
1448 << Target << "WriteLatencyTable, "
1449 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001450 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001451 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001452 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001453 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001454 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001455 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001456 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001457 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001458
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001459 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001460
1461 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1462
1463 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001464 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001465
1466 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001467 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001468 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1469
1470 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1471
Evan Cheng0d639a22011-07-01 21:01:15 +00001472 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001473 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001474 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001475
1476 std::string ClassName = Target + "GenSubtargetInfo";
1477 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001478 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001479 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001480 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001481 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001482 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001483 << " unsigned resolveSchedClass(unsigned SchedClass, "
1484 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001485 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001486 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001487 << " const;\n";
1488 if (TGT.getHwModes().getNumModeIds() > 1)
1489 OS << " unsigned getHwMode() const override;\n";
1490 OS << "};\n"
1491 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001492
1493 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1494
1495 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001496 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001497
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001498 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001499 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001500 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1501 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001502 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1503 OS << "extern const llvm::MCWriteProcResEntry "
1504 << Target << "WriteProcResTable[];\n";
1505 OS << "extern const llvm::MCWriteLatencyEntry "
1506 << Target << "WriteLatencyTable[];\n";
1507 OS << "extern const llvm::MCReadAdvanceEntry "
1508 << Target << "ReadAdvanceTable[];\n";
1509
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001510 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001511 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1512 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001513 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001514 }
1515
Daniel Sanders50f17232015-09-15 16:17:27 +00001516 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1517 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001518 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001519 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001520 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001521 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001522 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001523 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001524 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001525 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001526 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001527 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001528 OS << Target << "ProcSchedKV, "
1529 << Target << "WriteProcResTable, "
1530 << Target << "WriteLatencyTable, "
1531 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001532 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001533 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001534 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001535 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001536 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001537 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001538 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001539 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001540
Andrew Trickc6c88152012-09-18 03:41:43 +00001541 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001542 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001543
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001544 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001545
1546 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001547}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001548
1549namespace llvm {
1550
1551void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001552 CodeGenTarget CGTarget(RK);
1553 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001554}
1555
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001556} // end namespace llvm