blob: c7ccc60acaf37559e0a41bf253af29f116fb2546 [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";
David Blaikiee6503d82018-02-08 19:57:05 +0000611 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
612 << "ProcResources"
613 << "[] = {\n"
614 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0, 0},\n";
Andrew Trick23f3c652012-09-17 22:18:45 +0000615
Clement Courbet39911e22018-02-08 08:46:48 +0000616 unsigned SubUnitsOffset = 1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000617 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
618 Record *PRDef = ProcModel.ProcResourceDefs[i];
619
Craig Topper24064772014-04-15 07:20:03 +0000620 Record *SuperDef = nullptr;
Andrew Trick4e67cba2013-03-14 21:21:50 +0000621 unsigned SuperIdx = 0;
622 unsigned NumUnits = 0;
Clement Courbet39911e22018-02-08 08:46:48 +0000623 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Trick40c4f382013-06-15 04:50:06 +0000624 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000625 if (PRDef->isSubClassOf("ProcResGroup")) {
626 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000627 for (Record *RU : ResUnits) {
628 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbet873aa112018-02-09 10:28:46 +0000629 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000630 }
631 }
632 else {
633 // Find the SuperIdx
634 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000635 SuperDef =
636 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
637 ProcModel, PRDef->getLoc());
Andrew Trick4e67cba2013-03-14 21:21:50 +0000638 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
639 }
Andrew Tricka5c747b2013-03-14 22:47:01 +0000640 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick23f3c652012-09-17 22:18:45 +0000641 }
642 // Emit the ProcResourceDesc
Andrew Trick23f3c652012-09-17 22:18:45 +0000643 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
644 if (PRDef->getName().size() < 15)
645 OS.indent(15 - PRDef->getName().size());
Clement Courbet39911e22018-02-08 08:46:48 +0000646 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
647 if (SubUnitsBeginOffset != SubUnitsOffset) {
648 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
649 << SubUnitsBeginOffset;
650 } else {
651 OS << "nullptr";
652 }
653 OS << "}, // #" << i+1;
Andrew Trick23f3c652012-09-17 22:18:45 +0000654 if (SuperDef)
655 OS << ", Super=" << SuperDef->getName();
656 OS << "\n";
657 }
658 OS << "};\n";
659}
660
Andrew Trick9ef08822012-09-17 22:18:48 +0000661// Find the WriteRes Record that defines processor resources for this
662// SchedWrite.
663Record *SubtargetEmitter::FindWriteResources(
Andrew Trick9257b8f2012-09-22 02:24:21 +0000664 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000665
666 // Check if the SchedWrite is already subtarget-specific and directly
667 // specifies a set of processor resources.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000668 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
669 return SchedWrite.TheDef;
670
Craig Topper24064772014-04-15 07:20:03 +0000671 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000672 for (Record *A : SchedWrite.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000673 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000674 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000675 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
676 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
677 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
678 continue;
679 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000680 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000681 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000682 "defined for processor " + ProcModel.ModelName +
683 " Ensure only one SchedAlias exists per RW.");
684 AliasDef = AliasRW.TheDef;
685 }
686 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
687 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000688
689 // Check this processor's list of write resources.
Craig Topper24064772014-04-15 07:20:03 +0000690 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000691 for (Record *WR : ProcModel.WriteResDefs) {
692 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000693 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000694 if (AliasDef == WR->getValueAsDef("WriteType")
695 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000696 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000697 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000698 "SchedWrite and its alias on processor " +
699 ProcModel.ModelName);
700 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000701 ResDef = WR;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000702 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000703 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000704 // TODO: If ProcModel has a base model (previous generation processor),
705 // then call FindWriteResources recursively with that model here.
706 if (!ResDef) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000707 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000708 Twine("Processor does not define resources for ") +
709 SchedWrite.TheDef->getName());
Andrew Trick9257b8f2012-09-22 02:24:21 +0000710 }
711 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000712}
713
714/// Find the ReadAdvance record for the given SchedRead on this processor or
715/// return NULL.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000716Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick9ef08822012-09-17 22:18:48 +0000717 const CodeGenProcModel &ProcModel) {
718 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick9257b8f2012-09-22 02:24:21 +0000719 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
720 return SchedRead.TheDef;
721
722 // Check this processor's list of aliases for SchedRead.
Craig Topper24064772014-04-15 07:20:03 +0000723 Record *AliasDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000724 for (Record *A : SchedRead.Aliases) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000725 const CodeGenSchedRW &AliasRW =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000726 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trickda984b12012-10-03 23:06:28 +0000727 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
728 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
729 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
730 continue;
731 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000732 if (AliasDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000733 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000734 "defined for processor " + ProcModel.ModelName +
735 " Ensure only one SchedAlias exists per RW.");
736 AliasDef = AliasRW.TheDef;
737 }
738 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
739 return AliasDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000740
741 // Check this processor's ReadAdvanceList.
Craig Topper24064772014-04-15 07:20:03 +0000742 Record *ResDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000743 for (Record *RA : ProcModel.ReadAdvanceDefs) {
744 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick9ef08822012-09-17 22:18:48 +0000745 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000746 if (AliasDef == RA->getValueAsDef("ReadType")
747 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000748 if (ResDef) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000749 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick9257b8f2012-09-22 02:24:21 +0000750 "SchedRead and its alias on processor " +
751 ProcModel.ModelName);
752 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000753 ResDef = RA;
Andrew Trick9257b8f2012-09-22 02:24:21 +0000754 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000755 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000756 // TODO: If ProcModel has a base model (previous generation processor),
757 // then call FindReadAdvance recursively with that model here.
758 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000759 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper01ebd9b2017-10-26 20:49:36 +0000760 Twine("Processor does not define resources for ") +
761 SchedRead.TheDef->getName());
Andrew Trick9ef08822012-09-17 22:18:48 +0000762 }
Andrew Trick9257b8f2012-09-22 02:24:21 +0000763 return ResDef;
Andrew Trick9ef08822012-09-17 22:18:48 +0000764}
765
Andrew Trick4e67cba2013-03-14 21:21:50 +0000766// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka3801a32013-04-23 23:45:16 +0000767// resource groups and super resources that cover them.
Andrew Trick4e67cba2013-03-14 21:21:50 +0000768void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
769 std::vector<int64_t> &Cycles,
Andrew Tricka3801a32013-04-23 23:45:16 +0000770 const CodeGenProcModel &PM) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000771 // Default to 1 resource cycle.
772 Cycles.resize(PRVec.size(), 1);
773 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka3801a32013-04-23 23:45:16 +0000774 Record *PRDef = PRVec[i];
Andrew Trick4e67cba2013-03-14 21:21:50 +0000775 RecVec SubResources;
Andrew Tricka3801a32013-04-23 23:45:16 +0000776 if (PRDef->isSubClassOf("ProcResGroup"))
777 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000778 else {
Andrew Tricka3801a32013-04-23 23:45:16 +0000779 SubResources.push_back(PRDef);
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000780 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000781 for (Record *SubDef = PRDef;
782 SubDef->getValueInit("Super")->isComplete();) {
783 if (SubDef->isSubClassOf("ProcResGroup")) {
784 // Disallow this for simplicitly.
785 PrintFatalError(SubDef->getLoc(), "Processor resource group "
786 " cannot be a super resources.");
787 }
788 Record *SuperDef =
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000789 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
790 SubDef->getLoc());
Andrew Tricka3801a32013-04-23 23:45:16 +0000791 PRVec.push_back(SuperDef);
792 Cycles.push_back(Cycles[i]);
793 SubDef = SuperDef;
794 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000795 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000796 for (Record *PR : PM.ProcResourceDefs) {
797 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick4e67cba2013-03-14 21:21:50 +0000798 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000799 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000800 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6aa7a872013-04-23 23:45:11 +0000801 for( ; SubI != SubE; ++SubI) {
David Majnemer0d955d02016-08-11 22:21:41 +0000802 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick4e67cba2013-03-14 21:21:50 +0000803 break;
Andrew Trick6aa7a872013-04-23 23:45:11 +0000804 }
Andrew Trick4e67cba2013-03-14 21:21:50 +0000805 }
806 if (SubI == SubE) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000807 PRVec.push_back(PR);
Andrew Trick4e67cba2013-03-14 21:21:50 +0000808 Cycles.push_back(Cycles[i]);
809 }
810 }
811 }
812}
813
Andrew Trick9ef08822012-09-17 22:18:48 +0000814// Generate the SchedClass table for this processor and update global
815// tables. Must be called for each processor in order.
816void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
817 SchedClassTables &SchedTables) {
818 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
819 if (!ProcModel.hasInstrSchedModel())
820 return;
821
822 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Joel Jones80372332017-06-28 00:06:40 +0000823 DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topper29c55dcb2016-02-13 06:03:32 +0000824 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
825 DEBUG(SC.dump(&SchedModels));
Andrew Trick7aba6be2012-10-03 23:06:25 +0000826
Andrew Trick9ef08822012-09-17 22:18:48 +0000827 SCTab.resize(SCTab.size() + 1);
828 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Trickab722bd2012-09-18 03:18:56 +0000829 // SCDesc.Name is guarded by NDEBUG
Andrew Trick9ef08822012-09-17 22:18:48 +0000830 SCDesc.NumMicroOps = 0;
831 SCDesc.BeginGroup = false;
832 SCDesc.EndGroup = false;
833 SCDesc.WriteProcResIdx = 0;
834 SCDesc.WriteLatencyIdx = 0;
835 SCDesc.ReadAdvanceIdx = 0;
836
837 // A Variant SchedClass has no resources of its own.
Andrew Tricke97978f2013-03-26 21:36:39 +0000838 bool HasVariants = false;
Javed Absar32e3cb72017-10-06 15:25:04 +0000839 for (const CodeGenSchedTransition &CGT :
840 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
841 if (CGT.ProcIndices[0] == 0 ||
842 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Tricke97978f2013-03-26 21:36:39 +0000843 HasVariants = true;
844 break;
845 }
846 }
847 if (HasVariants) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000848 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
849 continue;
850 }
851
852 // Determine if the SchedClass is actually reachable on this processor. If
853 // not don't try to locate the processor resources, it will fail.
854 // If ProcIndices contains 0, this class applies to all processors.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000855 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
856 if (SC.ProcIndices[0] != 0) {
David Majnemer42531262016-08-12 03:55:06 +0000857 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick9ef08822012-09-17 22:18:48 +0000858 continue;
859 }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000860 IdxVec Writes = SC.Writes;
861 IdxVec Reads = SC.Reads;
862 if (!SC.InstRWs.empty()) {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000863 // This class has a default ReadWrite list which can be overriden by
Andrew Trick7aba6be2012-10-03 23:06:25 +0000864 // InstRW definitions.
Craig Topper24064772014-04-15 07:20:03 +0000865 Record *RWDef = nullptr;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000866 for (Record *RW : SC.InstRWs) {
867 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick9ef08822012-09-17 22:18:48 +0000868 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000869 RWDef = RW;
Andrew Trick9ef08822012-09-17 22:18:48 +0000870 break;
871 }
872 }
873 if (RWDef) {
Andrew Trickda984b12012-10-03 23:06:28 +0000874 Writes.clear();
875 Reads.clear();
Andrew Trick9ef08822012-09-17 22:18:48 +0000876 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
877 Writes, Reads);
878 }
879 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000880 if (Writes.empty()) {
881 // Check this processor's itinerary class resources.
Craig Topper29c55dcb2016-02-13 06:03:32 +0000882 for (Record *I : ProcModel.ItinRWDefs) {
883 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer0d955d02016-08-11 22:21:41 +0000884 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000885 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000886 Writes, Reads);
887 break;
888 }
889 }
890 if (Writes.empty()) {
891 DEBUG(dbgs() << ProcModel.ModelName
Craig Topper29c55dcb2016-02-13 06:03:32 +0000892 << " does not have resources for class " << SC.Name << '\n');
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000893 }
894 }
Andrew Trick9ef08822012-09-17 22:18:48 +0000895 // Sum resources across all operand writes.
896 std::vector<MCWriteProcResEntry> WriteProcResources;
897 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000898 std::vector<std::string> WriterNames;
Andrew Trick9ef08822012-09-17 22:18:48 +0000899 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000900 for (unsigned W : Writes) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000901 IdxVec WriteSeq;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000902 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trickda984b12012-10-03 23:06:28 +0000903 ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000904
905 // For each operand, create a latency entry.
906 MCWriteLatencyEntry WLEntry;
907 WLEntry.Cycles = 0;
Andrew Trickcfe222c2012-09-19 04:43:19 +0000908 unsigned WriteID = WriteSeq.back();
909 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
910 // If this Write is not referenced by a ReadAdvance, don't distinguish it
911 // from other WriteLatency entries.
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000912 if (!SchedModels.hasReadOfWrite(
913 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trickcfe222c2012-09-19 04:43:19 +0000914 WriteID = 0;
915 }
916 WLEntry.WriteResourceID = WriteID;
Andrew Trick9ef08822012-09-17 22:18:48 +0000917
Craig Topper29c55dcb2016-02-13 06:03:32 +0000918 for (unsigned WS : WriteSeq) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000919
Andrew Trick9257b8f2012-09-22 02:24:21 +0000920 Record *WriteRes =
Craig Topper29c55dcb2016-02-13 06:03:32 +0000921 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000922
923 // Mark the parent class as invalid for unsupported write types.
924 if (WriteRes->getValueAsBit("Unsupported")) {
925 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
926 break;
927 }
928 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
929 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
930 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
931 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar3d594372017-03-27 20:46:37 +0000932 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
933 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick9ef08822012-09-17 22:18:48 +0000934
935 // Create an entry for each ProcResource listed in WriteRes.
936 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
937 std::vector<int64_t> Cycles =
938 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000939
940 ExpandProcResources(PRVec, Cycles, ProcModel);
941
Andrew Trick9ef08822012-09-17 22:18:48 +0000942 for (unsigned PRIdx = 0, PREnd = PRVec.size();
943 PRIdx != PREnd; ++PRIdx) {
944 MCWriteProcResEntry WPREntry;
945 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
946 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick4e67cba2013-03-14 21:21:50 +0000947 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trick3821d9d2013-03-01 23:31:26 +0000948 // If this resource is already used in this sequence, add the current
949 // entry's cycles so that the same resource appears to be used
950 // serially, rather than multiple parallel uses. This is important for
951 // in-order machine where the resource consumption is a hazard.
952 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
953 for( ; WPRIdx != WPREnd; ++WPRIdx) {
954 if (WriteProcResources[WPRIdx].ProcResourceIdx
955 == WPREntry.ProcResourceIdx) {
956 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
957 break;
958 }
959 }
960 if (WPRIdx == WPREnd)
961 WriteProcResources.push_back(WPREntry);
Andrew Trick9ef08822012-09-17 22:18:48 +0000962 }
963 }
964 WriteLatencies.push_back(WLEntry);
965 }
966 // Create an entry for each operand Read in this SchedClass.
967 // Entries must be sorted first by UseIdx then by WriteResourceID.
968 for (unsigned UseIdx = 0, EndIdx = Reads.size();
969 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000970 Record *ReadAdvance =
971 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick9ef08822012-09-17 22:18:48 +0000972 if (!ReadAdvance)
973 continue;
974
975 // Mark the parent class as invalid for unsupported write types.
976 if (ReadAdvance->getValueAsBit("Unsupported")) {
977 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
978 break;
979 }
980 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
981 IdxVec WriteIDs;
982 if (ValidWrites.empty())
983 WriteIDs.push_back(0);
984 else {
Craig Topper29c55dcb2016-02-13 06:03:32 +0000985 for (Record *VW : ValidWrites) {
986 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick9ef08822012-09-17 22:18:48 +0000987 }
988 }
989 std::sort(WriteIDs.begin(), WriteIDs.end());
Craig Topper29c55dcb2016-02-13 06:03:32 +0000990 for(unsigned W : WriteIDs) {
Andrew Trick9ef08822012-09-17 22:18:48 +0000991 MCReadAdvanceEntry RAEntry;
992 RAEntry.UseIdx = UseIdx;
Craig Topper29c55dcb2016-02-13 06:03:32 +0000993 RAEntry.WriteResourceID = W;
Andrew Trick9ef08822012-09-17 22:18:48 +0000994 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
995 ReadAdvanceEntries.push_back(RAEntry);
996 }
997 }
998 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
999 WriteProcResources.clear();
1000 WriteLatencies.clear();
1001 ReadAdvanceEntries.clear();
1002 }
1003 // Add the information for this SchedClass to the global tables using basic
1004 // compression.
1005 //
1006 // WritePrecRes entries are sorted by ProcResIdx.
1007 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1008 LessWriteProcResources());
1009
1010 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1011 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1012 std::search(SchedTables.WriteProcResources.begin(),
1013 SchedTables.WriteProcResources.end(),
1014 WriteProcResources.begin(), WriteProcResources.end());
1015 if (WPRPos != SchedTables.WriteProcResources.end())
1016 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1017 else {
1018 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1019 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1020 WriteProcResources.end());
1021 }
1022 // Latency entries must remain in operand order.
1023 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1024 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1025 std::search(SchedTables.WriteLatencies.begin(),
1026 SchedTables.WriteLatencies.end(),
1027 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trickcfe222c2012-09-19 04:43:19 +00001028 if (WLPos != SchedTables.WriteLatencies.end()) {
1029 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1030 SCDesc.WriteLatencyIdx = idx;
1031 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1032 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1033 std::string::npos) {
1034 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1035 }
1036 }
Andrew Trick9ef08822012-09-17 22:18:48 +00001037 else {
1038 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trickcfe222c2012-09-19 04:43:19 +00001039 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1040 WriteLatencies.begin(),
1041 WriteLatencies.end());
1042 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1043 WriterNames.begin(), WriterNames.end());
Andrew Trick9ef08822012-09-17 22:18:48 +00001044 }
1045 // ReadAdvanceEntries must remain in operand order.
1046 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1047 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1048 std::search(SchedTables.ReadAdvanceEntries.begin(),
1049 SchedTables.ReadAdvanceEntries.end(),
1050 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1051 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1052 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1053 else {
1054 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1055 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1056 ReadAdvanceEntries.end());
1057 }
1058 }
1059}
1060
Andrew Tricka72fca62012-09-17 22:18:50 +00001061// Emit SchedClass tables for all processors and associated global tables.
1062void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1063 raw_ostream &OS) {
1064 // Emit global WriteProcResTable.
1065 OS << "\n// {ProcResourceIdx, Cycles}\n"
1066 << "extern const llvm::MCWriteProcResEntry "
1067 << Target << "WriteProcResTable[] = {\n"
1068 << " { 0, 0}, // Invalid\n";
1069 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1070 WPRIdx != WPREnd; ++WPRIdx) {
1071 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1072 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1073 << format("%2d", WPREntry.Cycles) << "}";
1074 if (WPRIdx + 1 < WPREnd)
1075 OS << ',';
1076 OS << " // #" << WPRIdx << '\n';
1077 }
1078 OS << "}; // " << Target << "WriteProcResTable\n";
1079
1080 // Emit global WriteLatencyTable.
1081 OS << "\n// {Cycles, WriteResourceID}\n"
1082 << "extern const llvm::MCWriteLatencyEntry "
1083 << Target << "WriteLatencyTable[] = {\n"
1084 << " { 0, 0}, // Invalid\n";
1085 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1086 WLIdx != WLEnd; ++WLIdx) {
1087 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1088 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1089 << format("%2d", WLEntry.WriteResourceID) << "}";
1090 if (WLIdx + 1 < WLEnd)
1091 OS << ',';
Andrew Trickcfe222c2012-09-19 04:43:19 +00001092 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001093 }
1094 OS << "}; // " << Target << "WriteLatencyTable\n";
1095
1096 // Emit global ReadAdvanceTable.
1097 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1098 << "extern const llvm::MCReadAdvanceEntry "
1099 << Target << "ReadAdvanceTable[] = {\n"
1100 << " {0, 0, 0}, // Invalid\n";
1101 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1102 RAIdx != RAEnd; ++RAIdx) {
1103 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1104 OS << " {" << RAEntry.UseIdx << ", "
1105 << format("%2d", RAEntry.WriteResourceID) << ", "
1106 << format("%2d", RAEntry.Cycles) << "}";
1107 if (RAIdx + 1 < RAEnd)
1108 OS << ',';
1109 OS << " // #" << RAIdx << '\n';
1110 }
1111 OS << "}; // " << Target << "ReadAdvanceTable\n";
1112
1113 // Emit a SchedClass table for each processor.
1114 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1115 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1116 if (!PI->hasInstrSchedModel())
1117 continue;
1118
1119 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola72961392012-11-02 20:57:36 +00001120 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Tricka72fca62012-09-17 22:18:50 +00001121
1122 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1123 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1124 OS << "static const llvm::MCSchedClassDesc "
1125 << PI->ModelName << "SchedClasses[] = {\n";
1126
1127 // The first class is always invalid. We no way to distinguish it except by
1128 // name and position.
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001129 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Tricka72fca62012-09-17 22:18:50 +00001130 && "invalid class not first");
1131 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1132 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001133 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001134
1135 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1136 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1137 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1138 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1139 if (SchedClass.Name.size() < 18)
1140 OS.indent(18 - SchedClass.Name.size());
1141 OS << MCDesc.NumMicroOps
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001142 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1143 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Tricka72fca62012-09-17 22:18:50 +00001144 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1145 << ", " << MCDesc.NumWriteProcResEntries
1146 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1147 << ", " << MCDesc.NumWriteLatencyEntries
1148 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperdf1285b2017-10-24 15:50:53 +00001149 << ", " << MCDesc.NumReadAdvanceEntries
1150 << "}, // #" << SCIdx << '\n';
Andrew Tricka72fca62012-09-17 22:18:50 +00001151 }
1152 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1153 }
1154}
1155
Andrew Trick87255e32012-07-07 04:00:00 +00001156void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1157 // For each processor model.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001158 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001159 // Emit processor resource table.
Craig Topper29c55dcb2016-02-13 06:03:32 +00001160 if (PM.hasInstrSchedModel())
1161 EmitProcessorResources(PM, OS);
1162 else if(!PM.ProcResourceDefs.empty())
1163 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick9ef08822012-09-17 22:18:48 +00001164 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick23f3c652012-09-17 22:18:45 +00001165
Andrew Trick73d77362012-06-05 03:44:40 +00001166 // Begin processor itinerary properties
1167 OS << "\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001168 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1169 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1170 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1171 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1172 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1173 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1174 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trickb6854d82013-09-25 18:14:12 +00001175
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001176 bool PostRAScheduler =
1177 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patela2f658d2014-07-15 22:39:58 +00001178
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001179 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1180 << "PostRAScheduler\n";
1181
1182 bool CompleteModel =
1183 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1184
1185 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1186 << "CompleteModel\n";
Andrew Trickb6854d82013-09-25 18:14:12 +00001187
Craig Topper29c55dcb2016-02-13 06:03:32 +00001188 OS << " " << PM.Index << ", // Processor ID\n";
1189 if (PM.hasInstrSchedModel())
1190 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1191 << " " << PM.ModelName << "SchedClasses" << ",\n"
1192 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Trickab722bd2012-09-18 03:18:56 +00001193 << " " << (SchedModels.schedClassEnd()
1194 - SchedModels.schedClassBegin()) << ",\n";
1195 else
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001196 OS << " nullptr, nullptr, 0, 0,"
1197 << " // No instruction-level machine model.\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001198 if (PM.hasItineraries())
Craig Topper194cb742017-10-24 15:50:55 +00001199 OS << " " << PM.ItinsDef->getName() << "\n";
Andrew Trick9c302672012-06-22 03:58:51 +00001200 else
Craig Topper194cb742017-10-24 15:50:55 +00001201 OS << " nullptr // No Itinerary\n";
1202 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001203 }
Jim Laskey3763a502005-10-31 17:16:01 +00001204}
1205
1206//
1207// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1208//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001209void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey3763a502005-10-31 17:16:01 +00001210 // Gather and sort processor information
1211 std::vector<Record*> ProcessorList =
1212 Records.getAllDerivedDefinitions("Processor");
Duraid Madina018da4f2005-12-30 14:56:37 +00001213 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey3763a502005-10-31 17:16:01 +00001214
1215 // Begin processor table
1216 OS << "\n";
1217 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001218 << "extern const llvm::SubtargetInfoKV "
Andrew Trick87255e32012-07-07 04:00:00 +00001219 << Target << "ProcSchedKV[] = {\n";
Andrew Trickdb6ed642011-04-01 01:56:55 +00001220
Jim Laskey3763a502005-10-31 17:16:01 +00001221 // For each processor
Craig Topperdf1285b2017-10-24 15:50:53 +00001222 for (Record *Processor : ProcessorList) {
Craig Topperbcd3c372017-05-31 21:12:46 +00001223 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick87255e32012-07-07 04:00:00 +00001224 const std::string &ProcModelName =
Andrew Trick76686492012-09-15 00:19:57 +00001225 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickdb6ed642011-04-01 01:56:55 +00001226
Jim Laskey3763a502005-10-31 17:16:01 +00001227 // Emit as { "cpu", procinit },
Craig Topperdf1285b2017-10-24 15:50:53 +00001228 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey3763a502005-10-31 17:16:01 +00001229 }
Andrew Trickdb6ed642011-04-01 01:56:55 +00001230
Jim Laskey3763a502005-10-31 17:16:01 +00001231 // End processor table
1232 OS << "};\n";
Jim Laskey86f002c2005-10-27 19:47:21 +00001233}
1234
1235//
Andrew Trick87255e32012-07-07 04:00:00 +00001236// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey86f002c2005-10-27 19:47:21 +00001237//
Andrew Trick87255e32012-07-07 04:00:00 +00001238void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick23f3c652012-09-17 22:18:45 +00001239 OS << "#ifdef DBGFIELD\n"
1240 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1241 << "#endif\n"
Aaron Ballman615eb472017-10-15 14:32:27 +00001242 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick23f3c652012-09-17 22:18:45 +00001243 << "#define DBGFIELD(x) x,\n"
1244 << "#else\n"
1245 << "#define DBGFIELD(x)\n"
1246 << "#endif\n";
1247
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001248 if (SchedModels.hasItineraries()) {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001249 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey802748c2005-11-01 20:06:59 +00001250 // Emit the stage data
Andrew Trick87255e32012-07-07 04:00:00 +00001251 EmitStageAndOperandCycleData(OS, ProcItinLists);
1252 EmitItineraries(OS, ProcItinLists);
Jim Laskey802748c2005-11-01 20:06:59 +00001253 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001254 OS << "\n// ===============================================================\n"
1255 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick23f3c652012-09-17 22:18:45 +00001256
Andrew Trick9ef08822012-09-17 22:18:48 +00001257 SchedClassTables SchedTables;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001258 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1259 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick9ef08822012-09-17 22:18:48 +00001260 }
Andrew Tricka72fca62012-09-17 22:18:50 +00001261 EmitSchedClassTables(SchedTables, OS);
1262
1263 // Emit the processor machine model
1264 EmitProcessorModels(OS);
1265 // Emit the processor lookup data
1266 EmitProcessorLookup(OS);
Andrew Trick9ef08822012-09-17 22:18:48 +00001267
Craig Topper194cb742017-10-24 15:50:55 +00001268 OS << "\n#undef DBGFIELD";
Jim Laskey86f002c2005-10-27 19:47:21 +00001269}
1270
Benjamin Kramerc321e532016-06-08 19:09:22 +00001271void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
Andrew Trickc6c88152012-09-18 03:41:43 +00001272 raw_ostream &OS) {
1273 OS << "unsigned " << ClassName
1274 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1275 << " const TargetSchedModel *SchedModel) const {\n";
1276
1277 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1278 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
Craig Topper29c55dcb2016-02-13 06:03:32 +00001279 for (Record *P : Prologs) {
1280 OS << P->getValueAsString("Code") << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001281 }
1282 IdxVec VariantClasses;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001283 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1284 if (SC.Transitions.empty())
Andrew Trickc6c88152012-09-18 03:41:43 +00001285 continue;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001286 VariantClasses.push_back(SC.Index);
Andrew Trickc6c88152012-09-18 03:41:43 +00001287 }
1288 if (!VariantClasses.empty()) {
1289 OS << " switch (SchedClass) {\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001290 for (unsigned VC : VariantClasses) {
1291 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1292 OS << " case " << VC << ": // " << SC.Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001293 IdxVec ProcIndices;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001294 for (const CodeGenSchedTransition &T : SC.Transitions) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001295 IdxVec PI;
Craig Topper29c55dcb2016-02-13 06:03:32 +00001296 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
Andrew Trickc6c88152012-09-18 03:41:43 +00001297 ProcIndices.begin(), ProcIndices.end(),
1298 std::back_inserter(PI));
1299 ProcIndices.swap(PI);
1300 }
Craig Topper29c55dcb2016-02-13 06:03:32 +00001301 for (unsigned PI : ProcIndices) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001302 OS << " ";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001303 if (PI != 0)
1304 OS << "if (SchedModel->getProcessorID() == " << PI << ") ";
1305 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName
Andrew Trickc6c88152012-09-18 03:41:43 +00001306 << '\n';
Craig Topper29c55dcb2016-02-13 06:03:32 +00001307 for (const CodeGenSchedTransition &T : SC.Transitions) {
1308 if (PI != 0 && !std::count(T.ProcIndices.begin(),
1309 T.ProcIndices.end(), PI)) {
Andrew Trickc6c88152012-09-18 03:41:43 +00001310 continue;
1311 }
Arnold Schwaighofer218f6d82013-06-05 14:06:50 +00001312 OS << " if (";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001313 for (RecIter RI = T.PredTerm.begin(), RE = T.PredTerm.end();
Andrew Trickc6c88152012-09-18 03:41:43 +00001314 RI != RE; ++RI) {
Craig Topper29c55dcb2016-02-13 06:03:32 +00001315 if (RI != T.PredTerm.begin())
Andrew Trickc6c88152012-09-18 03:41:43 +00001316 OS << "\n && ";
1317 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1318 }
1319 OS << ")\n"
Craig Topper29c55dcb2016-02-13 06:03:32 +00001320 << " return " << T.ToClassIdx << "; // "
1321 << SchedModels.getSchedClass(T.ToClassIdx).Name << '\n';
Andrew Trickc6c88152012-09-18 03:41:43 +00001322 }
1323 OS << " }\n";
Craig Topper29c55dcb2016-02-13 06:03:32 +00001324 if (PI == 0)
Andrew Trickc6c88152012-09-18 03:41:43 +00001325 break;
1326 }
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001327 if (SC.isInferred())
1328 OS << " return " << SC.Index << ";\n";
Andrew Trickc6c88152012-09-18 03:41:43 +00001329 OS << " break;\n";
1330 }
1331 OS << " };\n";
1332 }
1333 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1334 << "} // " << ClassName << "::resolveSchedClass\n";
1335}
1336
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001337void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1338 raw_ostream &OS) {
1339 const CodeGenHwModes &CGH = TGT.getHwModes();
1340 assert(CGH.getNumModeIds() > 0);
1341 if (CGH.getNumModeIds() == 1)
1342 return;
1343
1344 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1345 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1346 const HwMode &HM = CGH.getMode(M);
1347 OS << " if (checkFeatures(\"" << HM.Features
1348 << "\")) return " << M << ";\n";
1349 }
1350 OS << " return 0;\n}\n";
1351}
1352
Jim Laskey86f002c2005-10-27 19:47:21 +00001353//
Jim Laskeya2b52352005-10-26 17:30:34 +00001354// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1355// the subtarget features string.
1356//
Evan Cheng54b68e32011-07-01 20:45:01 +00001357void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1358 unsigned NumFeatures,
1359 unsigned NumProcs) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001360 std::vector<Record*> Features =
1361 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina018da4f2005-12-30 14:56:37 +00001362 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskeya2b52352005-10-26 17:30:34 +00001363
Andrew Trickdb6ed642011-04-01 01:56:55 +00001364 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1365 << "// subtarget options.\n"
Evan Chengfe6e4052011-06-30 01:53:36 +00001366 << "void llvm::";
Jim Laskeya2b52352005-10-26 17:30:34 +00001367 OS << Target;
Evan Cheng1a72add62011-07-07 07:07:08 +00001368 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenefb652a72010-01-05 17:47:41 +00001369 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel060f5d22012-06-12 04:21:36 +00001370 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001371
1372 if (Features.empty()) {
1373 OS << "}\n";
1374 return;
1375 }
1376
Andrew Trickba7b9212012-09-18 05:33:15 +00001377 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001378 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendlinge6182262007-05-04 20:38:40 +00001379
Craig Topper29c55dcb2016-02-13 06:03:32 +00001380 for (Record *R : Features) {
Jim Laskeydffe5972005-10-28 21:47:29 +00001381 // Next record
Craig Topperbcd3c372017-05-31 21:12:46 +00001382 StringRef Instance = R->getName();
1383 StringRef Value = R->getValueAsString("Value");
1384 StringRef Attribute = R->getValueAsString("Attribute");
Evan Chengd98701c2006-01-27 08:09:42 +00001385
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001386 if (Value=="true" || Value=="false")
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001387 OS << " if (Bits[" << Target << "::"
1388 << Instance << "]) "
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +00001389 << Attribute << " = " << Value << ";\n";
1390 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001391 OS << " if (Bits[" << Target << "::"
1392 << Instance << "] && "
Evan Cheng54b68e32011-07-01 20:45:01 +00001393 << Attribute << " < " << Value << ") "
1394 << Attribute << " = " << Value << ";\n";
Jim Laskey802748c2005-11-01 20:06:59 +00001395 }
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001396
Evan Chengfe6e4052011-06-30 01:53:36 +00001397 OS << "}\n";
Jim Laskeya2b52352005-10-26 17:30:34 +00001398}
1399
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +00001400//
Jim Laskeycfda85a2005-10-21 19:00:04 +00001401// SubtargetEmitter::run - Main subtarget enumeration emitter.
1402//
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001403void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001404 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskeycfda85a2005-10-21 19:00:04 +00001405
Evan Cheng4d1ca962011-07-08 01:53:10 +00001406 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001407 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001408
1409 OS << "namespace llvm {\n";
Craig Topper094bbca2016-02-14 05:22:01 +00001410 Enumeration(OS);
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001411 OS << "} // end namespace llvm\n\n";
Evan Cheng4d1ca962011-07-08 01:53:10 +00001412 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1413
Evan Cheng54b68e32011-07-01 20:45:01 +00001414 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001415 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov7d62e332010-04-18 20:31:01 +00001416
Evan Cheng54b68e32011-07-01 20:45:01 +00001417 OS << "namespace llvm {\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001418#if 0
1419 OS << "namespace {\n";
1420#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001421 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001422 OS << "\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001423 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001424 OS << "\n";
Andrew Trick87255e32012-07-07 04:00:00 +00001425 EmitSchedModel(OS);
Evan Chengbc153d42011-07-14 20:59:42 +00001426 OS << "\n";
1427#if 0
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001428 OS << "} // end anonymous namespace\n\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001429#endif
Evan Cheng54b68e32011-07-01 20:45:01 +00001430
1431 // MCInstrInfo initialization routine.
Craig Topper194cb742017-10-24 15:50:55 +00001432 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001433 << "MCSubtargetInfoImpl("
Daniel Sanders50f17232015-09-15 16:17:27 +00001434 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001435 OS << " return new MCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001436 if (NumFeatures)
1437 OS << Target << "FeatureKV, ";
1438 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001439 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001440 if (NumProcs)
1441 OS << Target << "SubTypeKV, ";
1442 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001443 OS << "None, ";
Andrew Tricka72fca62012-09-17 22:18:50 +00001444 OS << '\n'; OS.indent(22);
Andrew Trickab722bd2012-09-18 03:18:56 +00001445 OS << Target << "ProcSchedKV, "
1446 << Target << "WriteProcResTable, "
1447 << Target << "WriteLatencyTable, "
1448 << Target << "ReadAdvanceTable, ";
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001449 OS << '\n'; OS.indent(22);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001450 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001451 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001452 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001453 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001454 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001455 OS << "nullptr, nullptr, nullptr";
Eric Christopherdc5072d2014-05-06 20:23:04 +00001456 OS << ");\n}\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001457
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001458 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001459
1460 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1461
1462 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001463 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001464
1465 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001466 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001467 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1468
1469 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1470
Evan Cheng0d639a22011-07-01 21:01:15 +00001471 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng54b68e32011-07-01 20:45:01 +00001472 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001473 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001474
1475 std::string ClassName = Target + "GenSubtargetInfo";
1476 OS << "namespace llvm {\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001477 OS << "class DFAPacketizer;\n";
Evan Cheng0d639a22011-07-01 21:01:15 +00001478 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders50f17232015-09-15 16:17:27 +00001479 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng1a72add62011-07-07 07:07:08 +00001480 << "StringRef FS);\n"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001481 << "public:\n"
Daniel Sandersa73f1fd2015-06-10 12:11:26 +00001482 << " unsigned resolveSchedClass(unsigned SchedClass, "
1483 << " const MachineInstr *DefMI,"
Craig Topper2d9361e2014-03-09 07:44:38 +00001484 << " const TargetSchedModel *SchedModel) const override;\n"
Sebastian Popac35a4d2011-12-06 17:34:16 +00001485 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001486 << " const;\n";
1487 if (TGT.getHwModes().getNumModeIds() > 1)
1488 OS << " unsigned getHwMode() const override;\n";
1489 OS << "};\n"
1490 << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001491
1492 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1493
1494 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001495 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001496
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001497 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001498 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001499 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1500 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001501 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1502 OS << "extern const llvm::MCWriteProcResEntry "
1503 << Target << "WriteProcResTable[];\n";
1504 OS << "extern const llvm::MCWriteLatencyEntry "
1505 << Target << "WriteLatencyTable[];\n";
1506 OS << "extern const llvm::MCReadAdvanceEntry "
1507 << Target << "ReadAdvanceTable[];\n";
1508
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001509 if (SchedModels.hasItineraries()) {
Benjamin Kramer0d6d0982011-10-22 16:50:00 +00001510 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1511 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Trick030e2f82012-07-07 03:59:48 +00001512 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengbc153d42011-07-14 20:59:42 +00001513 }
1514
Daniel Sanders50f17232015-09-15 16:17:27 +00001515 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1516 << "StringRef FS)\n"
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001517 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001518 if (NumFeatures)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001519 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001520 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001521 OS << "None, ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001522 if (NumProcs)
Eric Christopherdc5072d2014-05-06 20:23:04 +00001523 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng54b68e32011-07-01 20:45:01 +00001524 else
Eric Christopherdc5072d2014-05-06 20:23:04 +00001525 OS << "None, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001526 OS << '\n'; OS.indent(24);
Andrew Trickab722bd2012-09-18 03:18:56 +00001527 OS << Target << "ProcSchedKV, "
1528 << Target << "WriteProcResTable, "
1529 << Target << "WriteLatencyTable, "
1530 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001531 OS << '\n'; OS.indent(24);
Andrew Trickbf8a28d2013-03-16 18:58:55 +00001532 if (SchedModels.hasItineraries()) {
Andrew Trickab722bd2012-09-18 03:18:56 +00001533 OS << Target << "Stages, "
Evan Cheng54b68e32011-07-01 20:45:01 +00001534 << Target << "OperandCycles, "
Eric Christopherdc5072d2014-05-06 20:23:04 +00001535 << Target << "ForwardingPaths";
Evan Cheng54b68e32011-07-01 20:45:01 +00001536 } else
Eugene Zelenko2bc2f332016-12-09 22:06:55 +00001537 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith754e21f2015-07-10 22:43:42 +00001538 OS << ") {}\n\n";
Andrew Tricka72fca62012-09-17 22:18:50 +00001539
Andrew Trickc6c88152012-09-18 03:41:43 +00001540 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszek788e7682017-09-14 20:44:20 +00001541 EmitHwModeCheck(ClassName, OS);
Andrew Trickc6c88152012-09-18 03:41:43 +00001542
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001543 OS << "} // end namespace llvm\n\n";
Evan Cheng54b68e32011-07-01 20:45:01 +00001544
1545 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskeycfda85a2005-10-21 19:00:04 +00001546}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001547
1548namespace llvm {
1549
1550void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick87255e32012-07-07 04:00:00 +00001551 CodeGenTarget CGTarget(RK);
1552 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001553}
1554
Eugene Zelenko75259bb2016-05-17 17:04:23 +00001555} // end namespace llvm