blob: 731c14bdb9a0d1445c4566d07148d4b39401f69f [file] [log] [blame]
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Laskey4bb9cbb2005-10-21 19:00:04 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner3d878112006-03-03 02:04:07 +000010// This tablegen backend emits subtarget enumerations.
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000011//
12//===----------------------------------------------------------------------===//
13
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000014#include "CodeGenTarget.h"
Andrew Trick2661b412012-07-07 04:00:00 +000015#include "CodeGenSchedule.h"
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +000016#include "PredicateExpander.h"
Eugene Zelenkod307f962016-05-17 17:04:23 +000017#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko43dec7d2016-12-09 22:06:55 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000019#include "llvm/ADT/StringExtras.h"
Eugene Zelenko43dec7d2016-12-09 22:06:55 +000020#include "llvm/ADT/StringRef.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000021#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenkod307f962016-05-17 17:04:23 +000022#include "llvm/MC/MCSchedule.h"
Michael Kupersteind714fcf2015-05-26 10:47:10 +000023#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/Format.h"
Eugene Zelenkod307f962016-05-17 17:04:23 +000026#include "llvm/Support/raw_ostream.h"
Andrew Trick40096d22012-09-17 22:18:45 +000027#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000028#include "llvm/TableGen/Record.h"
29#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohen9489c042005-10-28 01:43:09 +000030#include <algorithm>
Eugene Zelenkod307f962016-05-17 17:04:23 +000031#include <cassert>
32#include <cstdint>
Eugene Zelenko43dec7d2016-12-09 22:06:55 +000033#include <iterator>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000034#include <map>
35#include <string>
36#include <vector>
Hans Wennborg4d651e42015-10-06 23:24:35 +000037
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000038using namespace llvm;
39
Chandler Carruth915c29c2014-04-22 03:06:00 +000040#define DEBUG_TYPE "subtarget-emitter"
41
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000042namespace {
Eugene Zelenkod307f962016-05-17 17:04:23 +000043
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000044class SubtargetEmitter {
Andrew Trick52c3a1d2012-09-17 22:18:48 +000045 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
46 // The SchedClassDesc table indexes into a global write resource table, write
47 // latency table, and read advance table.
48 struct SchedClassTables {
Eugene Zelenko43dec7d2016-12-09 22:06:55 +000049 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
Andrew Trick52c3a1d2012-09-17 22:18:48 +000050 std::vector<MCWriteProcResEntry> WriteProcResources;
51 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +000052 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +000053 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
54
55 // Reserve an invalid entry at index 0
56 SchedClassTables() {
57 ProcSchedClasses.resize(1);
58 WriteProcResources.resize(1);
59 WriteLatencies.resize(1);
Andrew Trick3b8fb642012-09-19 04:43:19 +000060 WriterNames.push_back("InvalidWrite");
Andrew Trick52c3a1d2012-09-17 22:18:48 +000061 ReadAdvanceEntries.resize(1);
62 }
63 };
64
65 struct LessWriteProcResources {
66 bool operator()(const MCWriteProcResEntry &LHS,
67 const MCWriteProcResEntry &RHS) {
68 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
69 }
70 };
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000071
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +000072 const CodeGenTarget &TGT;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000073 RecordKeeper &Records;
Andrew Trick2661b412012-07-07 04:00:00 +000074 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000075 std::string Target;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000076
Craig Topper487820c2016-02-14 05:22:01 +000077 void Enumeration(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000078 unsigned FeatureKeyValues(raw_ostream &OS);
79 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000080 void FormItineraryStageString(const std::string &Names,
81 Record *ItinData, std::string &ItinString,
82 unsigned &NStages);
83 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
84 unsigned &NOperandCycles);
85 void FormItineraryBypassString(const std::string &Names,
86 Record *ItinData,
87 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick2661b412012-07-07 04:00:00 +000088 void EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko43dec7d2016-12-09 22:06:55 +000089 std::vector<std::vector<InstrItinerary>>
Andrew Trick2661b412012-07-07 04:00:00 +000090 &ProcItinLists);
91 void EmitItineraries(raw_ostream &OS,
Eugene Zelenko43dec7d2016-12-09 22:06:55 +000092 std::vector<std::vector<InstrItinerary>>
Andrew Trick2661b412012-07-07 04:00:00 +000093 &ProcItinLists);
Andrea Di Biagio375e6592018-04-04 11:53:13 +000094 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
95 raw_ostream &OS);
Andrea Di Biagio51af6fd2018-11-29 12:15:56 +000096 void EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
97 raw_ostream &OS);
Andrea Di Biagio375e6592018-04-04 11:53:13 +000098 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
99 raw_ostream &OS);
Mehdi Aminid0bf4472016-10-04 23:47:33 +0000100 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000101 char Separator);
Clement Courbetee765a12018-02-08 08:46:48 +0000102 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
103 raw_ostream &OS);
Andrew Trick40096d22012-09-17 22:18:45 +0000104 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
105 raw_ostream &OS);
Andrew Trick92649882012-09-22 02:24:21 +0000106 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000107 const CodeGenProcModel &ProcModel);
Andrew Trick92649882012-09-22 02:24:21 +0000108 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
109 const CodeGenProcModel &ProcModel);
Andrew Trick1754aca2013-03-14 21:21:50 +0000110 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
111 const CodeGenProcModel &ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000112 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
113 SchedClassTables &SchedTables);
Andrew Trick544c8802012-09-17 22:18:50 +0000114 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick2661b412012-07-07 04:00:00 +0000115 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000116 void EmitProcessorLookup(raw_ostream &OS);
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000117 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +0000118 void emitSchedModelHelpersImpl(raw_ostream &OS,
119 bool OnlyExpandMCInstPredicates = false);
Andrea Di Biagio0d3a4af2018-05-25 16:02:43 +0000120 void emitGenMCSubtargetInfo(raw_ostream &OS);
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +0000121 void EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS);
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +0000122
Andrew Trick2661b412012-07-07 04:00:00 +0000123 void EmitSchedModel(raw_ostream &OS);
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +0000124 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000125 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
126 unsigned NumProcs);
127
128public:
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +0000129 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
130 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
131 Target(TGT.getName()) {}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000132
133 void run(raw_ostream &o);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000134};
Eugene Zelenkod307f962016-05-17 17:04:23 +0000135
Hans Wennborg4d651e42015-10-06 23:24:35 +0000136} // end anonymous namespace
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000137
Jim Laskey7dc02042005-10-22 07:59:56 +0000138//
Jim Laskey581a8f72005-10-26 17:30:34 +0000139// Enumeration - Emit the specified class as an enumeration.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000140//
Craig Topper487820c2016-02-14 05:22:01 +0000141void SubtargetEmitter::Enumeration(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000142 // Get all records of class and sort
Craig Topper487820c2016-02-14 05:22:01 +0000143 std::vector<Record*> DefList =
144 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song3b35e172018-09-27 02:13:45 +0000145 llvm::sort(DefList, LessRecord());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000146
Evan Chengb6a63882011-04-15 19:35:46 +0000147 unsigned N = DefList.size();
Evan Cheng94214702011-07-01 20:45:01 +0000148 if (N == 0)
149 return;
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000150 if (N > MAX_SUBTARGET_FEATURES)
151 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
Evan Chengb6a63882011-04-15 19:35:46 +0000152
Evan Cheng94214702011-07-01 20:45:01 +0000153 OS << "namespace " << Target << " {\n";
154
Craig Topper9a4d26a2016-02-13 17:58:14 +0000155 // Open enumeration.
Craig Topperc46e3092016-02-13 06:03:29 +0000156 OS << "enum {\n";
Evan Cheng94214702011-07-01 20:45:01 +0000157
Reid Klecknerfe8490c2015-03-09 20:23:14 +0000158 // For each record
Craig Topperd4897342017-10-24 15:50:53 +0000159 for (unsigned i = 0; i < N; ++i) {
Reid Klecknerfe8490c2015-03-09 20:23:14 +0000160 // Next record
161 Record *Def = DefList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000162
Reid Klecknerfe8490c2015-03-09 20:23:14 +0000163 // Get and emit name
Craig Topperd4897342017-10-24 15:50:53 +0000164 OS << " " << Def->getName() << " = " << i << ",\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000165 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000166
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000167 // Close enumeration and namespace
Eugene Zelenkod307f962016-05-17 17:04:23 +0000168 OS << "};\n";
169 OS << "} // end namespace " << Target << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000170}
171
172//
Bill Wendling4222d802007-05-04 20:38:40 +0000173// FeatureKeyValues - Emit data of all the subtarget features. Used by the
174// command line.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000175//
Evan Cheng94214702011-07-01 20:45:01 +0000176unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000177 // Gather and sort all the features
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000178 std::vector<Record*> FeatureList =
179 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng94214702011-07-01 20:45:01 +0000180
181 if (FeatureList.empty())
182 return 0;
183
Fangrui Song3b35e172018-09-27 02:13:45 +0000184 llvm::sort(FeatureList, LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000185
Jim Laskey908ae272005-10-28 15:20:43 +0000186 // Begin feature table
Jim Laskey581a8f72005-10-26 17:30:34 +0000187 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000188 << "extern const llvm::SubtargetFeatureKV " << Target
189 << "FeatureKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000190
Jim Laskey908ae272005-10-28 15:20:43 +0000191 // For each feature
Evan Cheng94214702011-07-01 20:45:01 +0000192 unsigned NumFeatures = 0;
Jim Laskeydbe40062006-12-12 20:55:58 +0000193 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000194 // Next feature
195 Record *Feature = FeatureList[i];
196
Craig Topper2a129872017-05-31 21:12:46 +0000197 StringRef Name = Feature->getName();
198 StringRef CommandLineName = Feature->getValueAsString("Name");
199 StringRef Desc = Feature->getValueAsString("Desc");
Andrew Trickda96cf22011-04-01 01:56:55 +0000200
Jim Laskeydbe40062006-12-12 20:55:58 +0000201 if (CommandLineName.empty()) continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000202
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000203 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000204 OS << " { "
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000205 << "\"" << CommandLineName << "\", "
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000206 << "\"" << Desc << "\", "
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000207 << "{ " << Target << "::" << Name << " }, ";
Bill Wendling4222d802007-05-04 20:38:40 +0000208
Craig Topper729e2bc2018-03-23 00:02:45 +0000209 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
Andrew Trickda96cf22011-04-01 01:56:55 +0000210
Craig Topper07466512016-01-03 08:57:41 +0000211 OS << "{";
212 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
213 OS << " " << Target << "::" << ImpliesList[j]->getName();
214 if (++j < M) OS << ",";
Bill Wendling4222d802007-05-04 20:38:40 +0000215 }
Craig Topperd4897342017-10-24 15:50:53 +0000216 OS << " } },\n";
Evan Cheng94214702011-07-01 20:45:01 +0000217 ++NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000218 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000219
Jim Laskey908ae272005-10-28 15:20:43 +0000220 // End feature table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000221 OS << "};\n";
222
Evan Cheng94214702011-07-01 20:45:01 +0000223 return NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000224}
225
226//
227// CPUKeyValues - Emit data of all the subtarget processors. Used by command
228// line.
229//
Evan Cheng94214702011-07-01 20:45:01 +0000230unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000231 // Gather and sort processor information
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000232 std::vector<Record*> ProcessorList =
233 Records.getAllDerivedDefinitions("Processor");
Fangrui Song3b35e172018-09-27 02:13:45 +0000234 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000235
Jim Laskey908ae272005-10-28 15:20:43 +0000236 // Begin processor table
Jim Laskey581a8f72005-10-26 17:30:34 +0000237 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000238 << "extern const llvm::SubtargetFeatureKV " << Target
239 << "SubTypeKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000240
Jim Laskey908ae272005-10-28 15:20:43 +0000241 // For each processor
Craig Topperd4897342017-10-24 15:50:53 +0000242 for (Record *Processor : ProcessorList) {
Craig Topper2a129872017-05-31 21:12:46 +0000243 StringRef Name = Processor->getValueAsString("Name");
Craig Topper729e2bc2018-03-23 00:02:45 +0000244 RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
Andrew Trickda96cf22011-04-01 01:56:55 +0000245
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000246 // Emit as { "cpu", "description", { f1 , f2 , ... fn } },
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000247 OS << " { "
248 << "\"" << Name << "\", "
249 << "\"Select the " << Name << " processor\", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000250
Craig Topper07466512016-01-03 08:57:41 +0000251 OS << "{";
252 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
253 OS << " " << Target << "::" << FeatureList[j]->getName();
254 if (++j < M) OS << ",";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000255 }
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000256 // The { } is for the "implies" section of this data structure.
Craig Topperd4897342017-10-24 15:50:53 +0000257 OS << " }, { } },\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000258 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000259
Jim Laskey908ae272005-10-28 15:20:43 +0000260 // End processor table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000261 OS << "};\n";
262
Evan Cheng94214702011-07-01 20:45:01 +0000263 return ProcessorList.size();
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000264}
Jim Laskey7dc02042005-10-22 07:59:56 +0000265
Jim Laskey581a8f72005-10-26 17:30:34 +0000266//
David Goodwinfac85412009-08-17 16:02:57 +0000267// FormItineraryStageString - Compose a string containing the stage
268// data initialization for the specified itinerary. N is the number
269// of stages.
Jim Laskey0d841e02005-10-27 19:47:21 +0000270//
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000271void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
272 Record *ItinData,
David Goodwinfac85412009-08-17 16:02:57 +0000273 std::string &ItinString,
274 unsigned &NStages) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000275 // Get states list
Craig Topper729e2bc2018-03-23 00:02:45 +0000276 RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
Jim Laskey908ae272005-10-28 15:20:43 +0000277
278 // For each stage
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000279 unsigned N = NStages = StageList.size();
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000280 for (unsigned i = 0; i < N;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000281 // Next stage
Bill Wendling4222d802007-05-04 20:38:40 +0000282 const Record *Stage = StageList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000283
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000284 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey0d841e02005-10-27 19:47:21 +0000285 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskey7f39c142005-11-03 22:47:41 +0000286 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000287
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000288 // Get unit list
Craig Topper729e2bc2018-03-23 00:02:45 +0000289 RecVec UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickda96cf22011-04-01 01:56:55 +0000290
Jim Laskey908ae272005-10-28 15:20:43 +0000291 // For each unit
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000292 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000293 // Add name and bitwise or
Matthias Braun0c517c82016-12-04 05:48:16 +0000294 ItinString += Name + "FU::" + UnitList[j]->getName().str();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000295 if (++j < M) ItinString += " | ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000296 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000297
David Goodwin1a8f36e2009-08-12 18:31:53 +0000298 int TimeInc = Stage->getValueAsInt("TimeInc");
299 ItinString += ", " + itostr(TimeInc);
300
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000301 int Kind = Stage->getValueAsInt("Kind");
302 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
303
Jim Laskey908ae272005-10-28 15:20:43 +0000304 // Close off stage
305 ItinString += " }";
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000306 if (++i < N) ItinString += ", ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000307 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000308}
309
310//
David Goodwinfac85412009-08-17 16:02:57 +0000311// FormItineraryOperandCycleString - Compose a string containing the
312// operand cycle initialization for the specified itinerary. N is the
313// number of operands that has cycles specified.
Jim Laskey0d841e02005-10-27 19:47:21 +0000314//
David Goodwinfac85412009-08-17 16:02:57 +0000315void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
316 std::string &ItinString, unsigned &NOperandCycles) {
317 // Get operand cycle list
Craig Topper729e2bc2018-03-23 00:02:45 +0000318 std::vector<int64_t> OperandCycleList =
David Goodwinfac85412009-08-17 16:02:57 +0000319 ItinData->getValueAsListOfInts("OperandCycles");
320
321 // For each operand cycle
322 unsigned N = NOperandCycles = OperandCycleList.size();
323 for (unsigned i = 0; i < N;) {
324 // Next operand cycle
325 const int OCycle = OperandCycleList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000326
David Goodwinfac85412009-08-17 16:02:57 +0000327 ItinString += " " + itostr(OCycle);
328 if (++i < N) ItinString += ", ";
329 }
330}
331
Evan Cheng63d66ee2010-09-28 23:50:49 +0000332void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
333 Record *ItinData,
334 std::string &ItinString,
335 unsigned NOperandCycles) {
Craig Topper729e2bc2018-03-23 00:02:45 +0000336 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
Evan Cheng63d66ee2010-09-28 23:50:49 +0000337 unsigned N = BypassList.size();
Evan Cheng3881cb72010-09-29 22:42:35 +0000338 unsigned i = 0;
339 for (; i < N;) {
Matthias Braun0c517c82016-12-04 05:48:16 +0000340 ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
Evan Cheng3881cb72010-09-29 22:42:35 +0000341 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000342 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000343 for (; i < NOperandCycles;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000344 ItinString += " 0";
Evan Cheng3881cb72010-09-29 22:42:35 +0000345 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000346 }
347}
348
David Goodwinfac85412009-08-17 16:02:57 +0000349//
Andrew Trick2661b412012-07-07 04:00:00 +0000350// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
351// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
352// by CodeGenSchedClass::Index.
David Goodwinfac85412009-08-17 16:02:57 +0000353//
Andrew Trick2661b412012-07-07 04:00:00 +0000354void SubtargetEmitter::
355EmitStageAndOperandCycleData(raw_ostream &OS,
Eugene Zelenko43dec7d2016-12-09 22:06:55 +0000356 std::vector<std::vector<InstrItinerary>>
Andrew Trick2661b412012-07-07 04:00:00 +0000357 &ProcItinLists) {
Andrew Trickcb941922012-07-09 20:43:03 +0000358 // Multiple processor models may share an itinerary record. Emit it once.
359 SmallPtrSet<Record*, 8> ItinsDefSet;
360
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000361 // Emit functional units for all the itineraries.
Craig Topperdcbac182016-02-13 06:03:32 +0000362 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000363
Craig Topperdcbac182016-02-13 06:03:32 +0000364 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
Andrew Trickcb941922012-07-09 20:43:03 +0000365 continue;
366
Craig Topper729e2bc2018-03-23 00:02:45 +0000367 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000368 if (FUs.empty())
369 continue;
370
Alexander Shaposhnikov18b16182017-07-05 20:14:54 +0000371 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trick2661b412012-07-07 04:00:00 +0000372 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000373 << "namespace " << Name << "FU {\n";
374
375 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkelb460a332012-06-22 20:27:13 +0000376 OS << " const unsigned " << FUs[j]->getName()
377 << " = 1 << " << j << ";\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000378
Eugene Zelenkod307f962016-05-17 17:04:23 +0000379 OS << "} // end namespace " << Name << "FU\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000380
Craig Topper729e2bc2018-03-23 00:02:45 +0000381 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
Alexander Kornienkob4c62672015-01-15 11:41:30 +0000382 if (!BPs.empty()) {
Sylvestre Ledrub3fc3412018-03-17 17:30:08 +0000383 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
Evan Cheng3881cb72010-09-29 22:42:35 +0000384 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000385
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000386 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000387 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000388 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng3881cb72010-09-29 22:42:35 +0000389 << " = 1 << " << j << ";\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000390
Eugene Zelenkod307f962016-05-17 17:04:23 +0000391 OS << "} // end namespace " << Name << "Bypass\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000392 }
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000393 }
394
Jim Laskey908ae272005-10-28 15:20:43 +0000395 // Begin stages table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000396 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
397 "Stages[] = {\n";
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000398 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000399
David Goodwinfac85412009-08-17 16:02:57 +0000400 // Begin operand cycle table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000401 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000402 "OperandCycles[] = {\n";
David Goodwinfac85412009-08-17 16:02:57 +0000403 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000404
405 // Begin pipeline bypass table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000406 std::string BypassTable = "extern const unsigned " + Target +
Andrew Tricka11a6282012-07-07 03:59:48 +0000407 "ForwardingPaths[] = {\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000408 BypassTable += " 0, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000409
Andrew Trick2661b412012-07-07 04:00:00 +0000410 // For each Itinerary across all processors, add a unique entry to the stages,
Geoff Berry69223822017-05-08 15:33:08 +0000411 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
Andrew Trick2661b412012-07-07 04:00:00 +0000412 // object with computed offsets to the ProcItinLists result.
David Goodwinfac85412009-08-17 16:02:57 +0000413 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng3881cb72010-09-29 22:42:35 +0000414 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Craig Topperdcbac182016-02-13 06:03:32 +0000415 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
Andrew Trick2661b412012-07-07 04:00:00 +0000416 // Add process itinerary to the list.
417 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickda96cf22011-04-01 01:56:55 +0000418
Andrew Trick2661b412012-07-07 04:00:00 +0000419 // If this processor defines no itineraries, then leave the itinerary list
420 // empty.
421 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trick1ab961f2013-03-16 18:58:55 +0000422 if (!ProcModel.hasItineraries())
Andrew Trickd85934b2012-06-22 03:58:51 +0000423 continue;
Andrew Trickd85934b2012-06-22 03:58:51 +0000424
Alexander Shaposhnikov18b16182017-07-05 20:14:54 +0000425 StringRef Name = ProcModel.ItinsDef->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000426
Andrew Trick1ab961f2013-03-16 18:58:55 +0000427 ItinList.resize(SchedModels.numInstrSchedClasses());
428 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
429
430 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick2661b412012-07-07 04:00:00 +0000431 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
432
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000433 // Next itinerary data
Andrew Trick2661b412012-07-07 04:00:00 +0000434 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickda96cf22011-04-01 01:56:55 +0000435
Jim Laskey908ae272005-10-28 15:20:43 +0000436 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000437 std::string ItinStageString;
Andrew Trick2661b412012-07-07 04:00:00 +0000438 unsigned NStages = 0;
439 if (ItinData)
440 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000441
David Goodwinfac85412009-08-17 16:02:57 +0000442 // Get string and operand cycle count
443 std::string ItinOperandCycleString;
Andrew Trick2661b412012-07-07 04:00:00 +0000444 unsigned NOperandCycles = 0;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000445 std::string ItinBypassString;
Andrew Trick2661b412012-07-07 04:00:00 +0000446 if (ItinData) {
447 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
448 NOperandCycles);
449
450 FormItineraryBypassString(Name, ItinData, ItinBypassString,
451 NOperandCycles);
452 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000453
David Goodwinfac85412009-08-17 16:02:57 +0000454 // Check to see if stage already exists and create if it doesn't
Benjamin Kramer9de0bdb2018-02-23 19:32:56 +0000455 uint16_t FindStage = 0;
David Goodwinfac85412009-08-17 16:02:57 +0000456 if (NStages > 0) {
457 FindStage = ItinStageMap[ItinStageString];
458 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000459 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
460 StageTable += ItinStageString + ", // " + itostr(StageCount);
461 if (NStages > 1)
462 StageTable += "-" + itostr(StageCount + NStages - 1);
463 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000464 // Record Itin class number.
465 ItinStageMap[ItinStageString] = FindStage = StageCount;
466 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000467 }
468 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000469
David Goodwinfac85412009-08-17 16:02:57 +0000470 // Check to see if operand cycle already exists and create if it doesn't
Benjamin Kramer9de0bdb2018-02-23 19:32:56 +0000471 uint16_t FindOperandCycle = 0;
David Goodwinfac85412009-08-17 16:02:57 +0000472 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000473 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
474 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000475 if (FindOperandCycle == 0) {
476 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000477 OperandCycleTable += ItinOperandCycleString + ", // ";
478 std::string OperandIdxComment = itostr(OperandCycleCount);
479 if (NOperandCycles > 1)
480 OperandIdxComment += "-"
481 + itostr(OperandCycleCount + NOperandCycles - 1);
482 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000483 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000484 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000485 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000486 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000487 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000488 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000489 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000490 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000491
Evan Cheng5f54ce32010-09-09 18:18:55 +0000492 // Set up itinerary as location and location + stage count
Benjamin Kramer9de0bdb2018-02-23 19:32:56 +0000493 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
494 InstrItinerary Intinerary = {
495 NumUOps,
496 FindStage,
497 uint16_t(FindStage + NStages),
498 FindOperandCycle,
499 uint16_t(FindOperandCycle + NOperandCycles),
500 };
Evan Cheng5f54ce32010-09-09 18:18:55 +0000501
Jim Laskey908ae272005-10-28 15:20:43 +0000502 // Inject - empty slots will be 0, 0
Andrew Trick2661b412012-07-07 04:00:00 +0000503 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000504 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000505 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000506
Jim Laskey7f39c142005-11-03 22:47:41 +0000507 // Closing stage
Andrew Trick2661b412012-07-07 04:00:00 +0000508 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwinfac85412009-08-17 16:02:57 +0000509 StageTable += "};\n";
510
511 // Closing operand cycles
Andrew Trick2661b412012-07-07 04:00:00 +0000512 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwinfac85412009-08-17 16:02:57 +0000513 OperandCycleTable += "};\n";
514
Andrew Trick2661b412012-07-07 04:00:00 +0000515 BypassTable += " 0 // End bypass tables\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000516 BypassTable += "};\n";
517
David Goodwinfac85412009-08-17 16:02:57 +0000518 // Emit tables.
519 OS << StageTable;
520 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000521 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000522}
523
Andrew Trick2661b412012-07-07 04:00:00 +0000524//
525// EmitProcessorData - Generate data for processor itineraries that were
526// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
527// Itineraries for each processor. The Itinerary lists are indexed on
528// CodeGenSchedClass::Index.
529//
530void SubtargetEmitter::
531EmitItineraries(raw_ostream &OS,
Eugene Zelenko43dec7d2016-12-09 22:06:55 +0000532 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
Andrew Trickcb941922012-07-09 20:43:03 +0000533 // Multiple processor models may share an itinerary record. Emit it once.
534 SmallPtrSet<Record*, 8> ItinsDefSet;
535
Andrew Trick2661b412012-07-07 04:00:00 +0000536 // For each processor's machine model
Eugene Zelenko43dec7d2016-12-09 22:06:55 +0000537 std::vector<std::vector<InstrItinerary>>::iterator
Andrew Trick2661b412012-07-07 04:00:00 +0000538 ProcItinListsIter = ProcItinLists.begin();
539 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick48605c32012-09-15 00:19:57 +0000540 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickcb941922012-07-09 20:43:03 +0000541
Andrew Trick2661b412012-07-07 04:00:00 +0000542 Record *ItinsDef = PI->ItinsDef;
David Blaikie5401ba72014-11-19 07:49:26 +0000543 if (!ItinsDefSet.insert(ItinsDef).second)
Andrew Trickcb941922012-07-09 20:43:03 +0000544 continue;
Andrew Trick2661b412012-07-07 04:00:00 +0000545
Andrew Trick2661b412012-07-07 04:00:00 +0000546 // Get the itinerary list for the processor.
547 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick48605c32012-09-15 00:19:57 +0000548 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick2661b412012-07-07 04:00:00 +0000549
Pete Cooper0a304f02014-09-02 23:23:34 +0000550 // Empty itineraries aren't referenced anywhere in the tablegen output
551 // so don't emit them.
552 if (ItinList.empty())
553 continue;
554
Andrew Trick2661b412012-07-07 04:00:00 +0000555 OS << "\n";
556 OS << "static const llvm::InstrItinerary ";
Andrew Trick2661b412012-07-07 04:00:00 +0000557
558 // Begin processor itinerary table
Alexander Shaposhnikov18b16182017-07-05 20:14:54 +0000559 OS << ItinsDef->getName() << "[] = {\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000560
561 // For each itinerary class in CodeGenSchedClass::Index order.
562 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
563 InstrItinerary &Intinerary = ItinList[j];
564
565 // Emit Itinerary in the form of
566 // { firstStage, lastStage, firstCycle, lastCycle } // index
567 OS << " { " <<
568 Intinerary.NumMicroOps << ", " <<
569 Intinerary.FirstStage << ", " <<
570 Intinerary.LastStage << ", " <<
571 Intinerary.FirstOperandCycle << ", " <<
572 Intinerary.LastOperandCycle << " }" <<
573 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
574 }
575 // End processor itinerary table
Benjamin Kramer9de0bdb2018-02-23 19:32:56 +0000576 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
577 "// end marker\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000578 OS << "};\n";
579 }
580}
581
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000582// Emit either the value defined in the TableGen Record, or the default
Andrew Trick2661b412012-07-07 04:00:00 +0000583// value defined in the C++ header. The Record is null if the processor does not
584// define a model.
585void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Mehdi Aminid0bf4472016-10-04 23:47:33 +0000586 StringRef Name, char Separator) {
Andrew Trickfc992992012-06-05 03:44:40 +0000587 OS << " ";
Andrew Trick2661b412012-07-07 04:00:00 +0000588 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trickfc992992012-06-05 03:44:40 +0000589 if (V >= 0)
590 OS << V << Separator << " // " << Name;
591 else
Andrew Trick2661b412012-07-07 04:00:00 +0000592 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000593 OS << '\n';
594}
595
Clement Courbetee765a12018-02-08 08:46:48 +0000596void SubtargetEmitter::EmitProcessorResourceSubUnits(
597 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
598 OS << "\nstatic const unsigned " << ProcModel.ModelName
599 << "ProcResourceSubUnits[] = {\n"
600 << " 0, // Invalid\n";
601
602 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
603 Record *PRDef = ProcModel.ProcResourceDefs[i];
604 if (!PRDef->isSubClassOf("ProcResGroup"))
605 continue;
606 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
607 for (Record *RUDef : ResUnits) {
608 Record *const RU =
609 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
610 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
611 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
612 }
613 }
614 OS << " // " << PRDef->getName() << "\n";
615 }
616 OS << "};\n";
617}
618
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000619static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
620 raw_ostream &OS) {
Andrea Di Biagio39903ab2018-04-05 15:53:31 +0000621 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000622 if (Record *RCU = ProcModel.RetireControlUnit) {
623 ReorderBufferSize =
624 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
625 MaxRetirePerCycle =
626 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
627 }
628
629 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
630 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
631}
632
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000633static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
634 unsigned NumRegisterFiles,
635 unsigned NumCostEntries, raw_ostream &OS) {
636 if (NumRegisterFiles)
637 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
638 else
Andrea Di Biagio166f95e2018-04-05 13:59:52 +0000639 OS << "nullptr,\n 0";
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000640
641 OS << ", // Number of register files.\n ";
642 if (NumCostEntries)
643 OS << ProcModel.ModelName << "RegisterCosts,\n ";
644 else
Andrea Di Biagio166f95e2018-04-05 13:59:52 +0000645 OS << "nullptr,\n ";
Clement Courbetc486bdf2018-04-10 08:16:37 +0000646 OS << NumCostEntries << ", // Number of register cost entries.\n";
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000647}
648
649unsigned
650SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
651 raw_ostream &OS) {
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000652 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
653 return RF.hasDefaultCosts();
654 }))
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000655 return 0;
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000656
657 // Print the RegisterCost table first.
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +0000658 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000659 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
660 << "RegisterCosts"
661 << "[] = {\n";
662
663 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
664 // Skip register files with a default cost table.
665 if (RF.hasDefaultCosts())
666 continue;
667 // Add entries to the cost table.
668 for (const CodeGenRegisterCost &RC : RF.Costs) {
669 OS << " { ";
670 Record *Rec = RC.RCDef;
671 if (Rec->getValue("Namespace"))
672 OS << Rec->getValueAsString("Namespace") << "::";
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +0000673 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
674 << RC.AllowMoveElimination << "},\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000675 }
676 }
677 OS << "};\n";
678
679 // Now generate a table with register file info.
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +0000680 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
681 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000682 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
683 << "RegisterFiles"
684 << "[] = {\n"
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +0000685 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000686 unsigned CostTblIndex = 0;
687
688 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
689 OS << " { ";
690 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
691 unsigned NumCostEntries = RD.Costs.size();
Andrea Di Biagiof39b0d92018-10-12 11:23:04 +0000692 OS << NumCostEntries << ", " << CostTblIndex << ", "
693 << RD.MaxMovesEliminatedPerCycle << ", "
694 << RD.AllowZeroMoveEliminationOnly << "},\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000695 CostTblIndex += NumCostEntries;
696 }
697 OS << "};\n";
698
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000699 return CostTblIndex;
700}
Simon Pilgrim1603ab52018-04-19 10:59:49 +0000701
Andrea Di Biagio51af6fd2018-11-29 12:15:56 +0000702void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
703 raw_ostream &OS) {
704 unsigned QueueID = 0;
705 if (ProcModel.LoadQueue) {
706 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
707 QueueID =
708 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
709 std::find(ProcModel.ProcResourceDefs.begin(),
710 ProcModel.ProcResourceDefs.end(), Queue));
711 }
712 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
713
714 QueueID = 0;
715 if (ProcModel.StoreQueue) {
716 const Record *Queue =
717 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
718 QueueID =
719 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
720 std::find(ProcModel.ProcResourceDefs.begin(),
721 ProcModel.ProcResourceDefs.end(), Queue));
722 }
723 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
724}
725
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000726void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
727 raw_ostream &OS) {
728 // Generate a table of register file descriptors (one entry per each user
729 // defined register file), and a table of register costs.
730 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
731
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000732 // Now generate a table for the extra processor info.
733 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000734 << "ExtraInfo = {\n ";
735
Andrea Di Biagio8c6c5162018-04-05 15:41:41 +0000736 // Add information related to the retire control unit.
737 EmitRetireControlUnitInfo(ProcModel, OS);
738
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000739 // Add information related to the register files (i.e. where to find register
740 // file descriptors and register costs).
741 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
742 NumCostEntries, OS);
743
Andrea Di Biagio51af6fd2018-11-29 12:15:56 +0000744 // Add information about load/store queues.
745 EmitLoadStoreQueueInfo(ProcModel, OS);
746
Andrea Di Biagio375e6592018-04-04 11:53:13 +0000747 OS << "};\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +0000748}
749
Andrew Trick40096d22012-09-17 22:18:45 +0000750void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
751 raw_ostream &OS) {
Clement Courbetee765a12018-02-08 08:46:48 +0000752 EmitProcessorResourceSubUnits(ProcModel, OS);
753
Jinsong Jic5c0a4b2018-09-18 15:38:56 +0000754 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
David Blaikiefa797f42018-02-08 19:57:05 +0000755 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
756 << "ProcResources"
757 << "[] = {\n"
Andrea Di Biagiob54703c2018-03-08 10:38:45 +0000758 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
Andrew Trick40096d22012-09-17 22:18:45 +0000759
Clement Courbetee765a12018-02-08 08:46:48 +0000760 unsigned SubUnitsOffset = 1;
Andrew Trick40096d22012-09-17 22:18:45 +0000761 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
762 Record *PRDef = ProcModel.ProcResourceDefs[i];
763
Craig Topper095734c2014-04-15 07:20:03 +0000764 Record *SuperDef = nullptr;
Andrew Trick1754aca2013-03-14 21:21:50 +0000765 unsigned SuperIdx = 0;
766 unsigned NumUnits = 0;
Clement Courbetee765a12018-02-08 08:46:48 +0000767 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
Andrew Tricka3d82ce2013-06-15 04:50:06 +0000768 int BufferSize = PRDef->getValueAsInt("BufferSize");
Andrew Trick1754aca2013-03-14 21:21:50 +0000769 if (PRDef->isSubClassOf("ProcResGroup")) {
770 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
Craig Topperdcbac182016-02-13 06:03:32 +0000771 for (Record *RU : ResUnits) {
772 NumUnits += RU->getValueAsInt("NumUnits");
Clement Courbetf1973362018-02-09 10:28:46 +0000773 SubUnitsOffset += RU->getValueAsInt("NumUnits");
Andrew Trick1754aca2013-03-14 21:21:50 +0000774 }
775 }
776 else {
777 // Find the SuperIdx
778 if (PRDef->getValueInit("Super")->isComplete()) {
Evandro Menezes8304ff22017-11-21 21:33:52 +0000779 SuperDef =
780 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
781 ProcModel, PRDef->getLoc());
Andrew Trick1754aca2013-03-14 21:21:50 +0000782 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
783 }
Andrew Trick157c6c42013-03-14 22:47:01 +0000784 NumUnits = PRDef->getValueAsInt("NumUnits");
Andrew Trick40096d22012-09-17 22:18:45 +0000785 }
786 // Emit the ProcResourceDesc
Andrea Di Biagiob54703c2018-03-08 10:38:45 +0000787 OS << " {\"" << PRDef->getName() << "\", ";
Andrew Trick40096d22012-09-17 22:18:45 +0000788 if (PRDef->getName().size() < 15)
789 OS.indent(15 - PRDef->getName().size());
Clement Courbetee765a12018-02-08 08:46:48 +0000790 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
791 if (SubUnitsBeginOffset != SubUnitsOffset) {
792 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
793 << SubUnitsBeginOffset;
794 } else {
795 OS << "nullptr";
796 }
797 OS << "}, // #" << i+1;
Andrew Trick40096d22012-09-17 22:18:45 +0000798 if (SuperDef)
799 OS << ", Super=" << SuperDef->getName();
800 OS << "\n";
801 }
802 OS << "};\n";
803}
804
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000805// Find the WriteRes Record that defines processor resources for this
806// SchedWrite.
807Record *SubtargetEmitter::FindWriteResources(
Andrew Trick92649882012-09-22 02:24:21 +0000808 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000809
810 // Check if the SchedWrite is already subtarget-specific and directly
811 // specifies a set of processor resources.
Andrew Trick92649882012-09-22 02:24:21 +0000812 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
813 return SchedWrite.TheDef;
814
Craig Topper095734c2014-04-15 07:20:03 +0000815 Record *AliasDef = nullptr;
Craig Topperdcbac182016-02-13 06:03:32 +0000816 for (Record *A : SchedWrite.Aliases) {
Andrew Trick92649882012-09-22 02:24:21 +0000817 const CodeGenSchedRW &AliasRW =
Craig Topperdcbac182016-02-13 06:03:32 +0000818 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trick2062b122012-10-03 23:06:28 +0000819 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
820 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
821 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
822 continue;
823 }
Andrew Trick92649882012-09-22 02:24:21 +0000824 if (AliasDef)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000825 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick92649882012-09-22 02:24:21 +0000826 "defined for processor " + ProcModel.ModelName +
827 " Ensure only one SchedAlias exists per RW.");
828 AliasDef = AliasRW.TheDef;
829 }
830 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
831 return AliasDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000832
833 // Check this processor's list of write resources.
Craig Topper095734c2014-04-15 07:20:03 +0000834 Record *ResDef = nullptr;
Craig Topperdcbac182016-02-13 06:03:32 +0000835 for (Record *WR : ProcModel.WriteResDefs) {
836 if (!WR->isSubClassOf("WriteRes"))
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000837 continue;
Craig Topperdcbac182016-02-13 06:03:32 +0000838 if (AliasDef == WR->getValueAsDef("WriteType")
839 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
Andrew Trick92649882012-09-22 02:24:21 +0000840 if (ResDef) {
Craig Topperdcbac182016-02-13 06:03:32 +0000841 PrintFatalError(WR->getLoc(), "Resources are defined for both "
Andrew Trick92649882012-09-22 02:24:21 +0000842 "SchedWrite and its alias on processor " +
843 ProcModel.ModelName);
844 }
Craig Topperdcbac182016-02-13 06:03:32 +0000845 ResDef = WR;
Andrew Trick92649882012-09-22 02:24:21 +0000846 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000847 }
Andrew Trick92649882012-09-22 02:24:21 +0000848 // TODO: If ProcModel has a base model (previous generation processor),
849 // then call FindWriteResources recursively with that model here.
850 if (!ResDef) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000851 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper1af80772017-10-26 20:49:36 +0000852 Twine("Processor does not define resources for ") +
853 SchedWrite.TheDef->getName());
Andrew Trick92649882012-09-22 02:24:21 +0000854 }
855 return ResDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000856}
857
858/// Find the ReadAdvance record for the given SchedRead on this processor or
859/// return NULL.
Andrew Trick92649882012-09-22 02:24:21 +0000860Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000861 const CodeGenProcModel &ProcModel) {
862 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick92649882012-09-22 02:24:21 +0000863 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
864 return SchedRead.TheDef;
865
866 // Check this processor's list of aliases for SchedRead.
Craig Topper095734c2014-04-15 07:20:03 +0000867 Record *AliasDef = nullptr;
Craig Topperdcbac182016-02-13 06:03:32 +0000868 for (Record *A : SchedRead.Aliases) {
Andrew Trick92649882012-09-22 02:24:21 +0000869 const CodeGenSchedRW &AliasRW =
Craig Topperdcbac182016-02-13 06:03:32 +0000870 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
Andrew Trick2062b122012-10-03 23:06:28 +0000871 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
872 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
873 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
874 continue;
875 }
Andrew Trick92649882012-09-22 02:24:21 +0000876 if (AliasDef)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000877 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick92649882012-09-22 02:24:21 +0000878 "defined for processor " + ProcModel.ModelName +
879 " Ensure only one SchedAlias exists per RW.");
880 AliasDef = AliasRW.TheDef;
881 }
882 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
883 return AliasDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000884
885 // Check this processor's ReadAdvanceList.
Craig Topper095734c2014-04-15 07:20:03 +0000886 Record *ResDef = nullptr;
Craig Topperdcbac182016-02-13 06:03:32 +0000887 for (Record *RA : ProcModel.ReadAdvanceDefs) {
888 if (!RA->isSubClassOf("ReadAdvance"))
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000889 continue;
Craig Topperdcbac182016-02-13 06:03:32 +0000890 if (AliasDef == RA->getValueAsDef("ReadType")
891 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
Andrew Trick92649882012-09-22 02:24:21 +0000892 if (ResDef) {
Craig Topperdcbac182016-02-13 06:03:32 +0000893 PrintFatalError(RA->getLoc(), "Resources are defined for both "
Andrew Trick92649882012-09-22 02:24:21 +0000894 "SchedRead and its alias on processor " +
895 ProcModel.ModelName);
896 }
Craig Topperdcbac182016-02-13 06:03:32 +0000897 ResDef = RA;
Andrew Trick92649882012-09-22 02:24:21 +0000898 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000899 }
Andrew Trick92649882012-09-22 02:24:21 +0000900 // TODO: If ProcModel has a base model (previous generation processor),
901 // then call FindReadAdvance recursively with that model here.
902 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000903 PrintFatalError(ProcModel.ModelDef->getLoc(),
Craig Topper1af80772017-10-26 20:49:36 +0000904 Twine("Processor does not define resources for ") +
905 SchedRead.TheDef->getName());
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000906 }
Andrew Trick92649882012-09-22 02:24:21 +0000907 return ResDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000908}
909
Andrew Trick1754aca2013-03-14 21:21:50 +0000910// Expand an explicit list of processor resources into a full list of implied
Andrew Tricka809c8d2013-04-23 23:45:16 +0000911// resource groups and super resources that cover them.
Andrew Trick1754aca2013-03-14 21:21:50 +0000912void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
913 std::vector<int64_t> &Cycles,
Andrew Tricka809c8d2013-04-23 23:45:16 +0000914 const CodeGenProcModel &PM) {
Clement Courbetfa811242018-06-13 09:41:49 +0000915 assert(PRVec.size() == Cycles.size() && "failed precondition");
Andrew Trick1754aca2013-03-14 21:21:50 +0000916 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka809c8d2013-04-23 23:45:16 +0000917 Record *PRDef = PRVec[i];
Andrew Trick1754aca2013-03-14 21:21:50 +0000918 RecVec SubResources;
Andrew Tricka809c8d2013-04-23 23:45:16 +0000919 if (PRDef->isSubClassOf("ProcResGroup"))
920 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick1754aca2013-03-14 21:21:50 +0000921 else {
Andrew Tricka809c8d2013-04-23 23:45:16 +0000922 SubResources.push_back(PRDef);
Evandro Menezes8304ff22017-11-21 21:33:52 +0000923 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
Andrew Tricka809c8d2013-04-23 23:45:16 +0000924 for (Record *SubDef = PRDef;
925 SubDef->getValueInit("Super")->isComplete();) {
926 if (SubDef->isSubClassOf("ProcResGroup")) {
927 // Disallow this for simplicitly.
928 PrintFatalError(SubDef->getLoc(), "Processor resource group "
929 " cannot be a super resources.");
930 }
931 Record *SuperDef =
Evandro Menezes8304ff22017-11-21 21:33:52 +0000932 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
933 SubDef->getLoc());
Andrew Tricka809c8d2013-04-23 23:45:16 +0000934 PRVec.push_back(SuperDef);
935 Cycles.push_back(Cycles[i]);
936 SubDef = SuperDef;
937 }
Andrew Trick1754aca2013-03-14 21:21:50 +0000938 }
Craig Topperdcbac182016-02-13 06:03:32 +0000939 for (Record *PR : PM.ProcResourceDefs) {
940 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
Andrew Trick1754aca2013-03-14 21:21:50 +0000941 continue;
Craig Topperdcbac182016-02-13 06:03:32 +0000942 RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
Andrew Trick1754aca2013-03-14 21:21:50 +0000943 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6982bdd2013-04-23 23:45:11 +0000944 for( ; SubI != SubE; ++SubI) {
David Majnemer975248e2016-08-11 22:21:41 +0000945 if (!is_contained(SuperResources, *SubI)) {
Andrew Trick1754aca2013-03-14 21:21:50 +0000946 break;
Andrew Trick6982bdd2013-04-23 23:45:11 +0000947 }
Andrew Trick1754aca2013-03-14 21:21:50 +0000948 }
949 if (SubI == SubE) {
Craig Topperdcbac182016-02-13 06:03:32 +0000950 PRVec.push_back(PR);
Andrew Trick1754aca2013-03-14 21:21:50 +0000951 Cycles.push_back(Cycles[i]);
952 }
953 }
954 }
955}
956
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000957// Generate the SchedClass table for this processor and update global
958// tables. Must be called for each processor in order.
959void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
960 SchedClassTables &SchedTables) {
961 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
962 if (!ProcModel.hasInstrSchedModel())
963 return;
964
965 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
Nicola Zaghen0818e782018-05-14 12:53:11 +0000966 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
Craig Topperdcbac182016-02-13 06:03:32 +0000967 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000968 LLVM_DEBUG(SC.dump(&SchedModels));
Andrew Trickfe05d982012-10-03 23:06:25 +0000969
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000970 SCTab.resize(SCTab.size() + 1);
971 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Tricke127dfd2012-09-18 03:18:56 +0000972 // SCDesc.Name is guarded by NDEBUG
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000973 SCDesc.NumMicroOps = 0;
974 SCDesc.BeginGroup = false;
975 SCDesc.EndGroup = false;
976 SCDesc.WriteProcResIdx = 0;
977 SCDesc.WriteLatencyIdx = 0;
978 SCDesc.ReadAdvanceIdx = 0;
979
980 // A Variant SchedClass has no resources of its own.
Andrew Trick82e7c4f2013-03-26 21:36:39 +0000981 bool HasVariants = false;
Javed Absarf1af77a2017-10-06 15:25:04 +0000982 for (const CodeGenSchedTransition &CGT :
983 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
984 if (CGT.ProcIndices[0] == 0 ||
985 is_contained(CGT.ProcIndices, ProcModel.Index)) {
Andrew Trick82e7c4f2013-03-26 21:36:39 +0000986 HasVariants = true;
987 break;
988 }
989 }
990 if (HasVariants) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000991 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
992 continue;
993 }
994
995 // Determine if the SchedClass is actually reachable on this processor. If
996 // not don't try to locate the processor resources, it will fail.
997 // If ProcIndices contains 0, this class applies to all processors.
Craig Topperdcbac182016-02-13 06:03:32 +0000998 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
999 if (SC.ProcIndices[0] != 0) {
David Majnemer2d62ce62016-08-12 03:55:06 +00001000 if (!is_contained(SC.ProcIndices, ProcModel.Index))
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001001 continue;
1002 }
Craig Topperdcbac182016-02-13 06:03:32 +00001003 IdxVec Writes = SC.Writes;
1004 IdxVec Reads = SC.Reads;
1005 if (!SC.InstRWs.empty()) {
Sylvestre Ledrub3fc3412018-03-17 17:30:08 +00001006 // This class has a default ReadWrite list which can be overridden by
Andrew Trickfe05d982012-10-03 23:06:25 +00001007 // InstRW definitions.
Craig Topper095734c2014-04-15 07:20:03 +00001008 Record *RWDef = nullptr;
Craig Topperdcbac182016-02-13 06:03:32 +00001009 for (Record *RW : SC.InstRWs) {
1010 Record *RWModelDef = RW->getValueAsDef("SchedModel");
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001011 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
Craig Topperdcbac182016-02-13 06:03:32 +00001012 RWDef = RW;
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001013 break;
1014 }
1015 }
1016 if (RWDef) {
Andrew Trick2062b122012-10-03 23:06:28 +00001017 Writes.clear();
1018 Reads.clear();
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001019 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1020 Writes, Reads);
1021 }
1022 }
Andrew Trick1ab961f2013-03-16 18:58:55 +00001023 if (Writes.empty()) {
1024 // Check this processor's itinerary class resources.
Craig Topperdcbac182016-02-13 06:03:32 +00001025 for (Record *I : ProcModel.ItinRWDefs) {
1026 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
David Majnemer975248e2016-08-11 22:21:41 +00001027 if (is_contained(Matched, SC.ItinClassDef)) {
Craig Topperdcbac182016-02-13 06:03:32 +00001028 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
Andrew Trick1ab961f2013-03-16 18:58:55 +00001029 Writes, Reads);
1030 break;
1031 }
1032 }
1033 if (Writes.empty()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001034 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1035 << " does not have resources for class " << SC.Name
1036 << '\n');
Andrew Trick1ab961f2013-03-16 18:58:55 +00001037 }
1038 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001039 // Sum resources across all operand writes.
1040 std::vector<MCWriteProcResEntry> WriteProcResources;
1041 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +00001042 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001043 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
Craig Topperdcbac182016-02-13 06:03:32 +00001044 for (unsigned W : Writes) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001045 IdxVec WriteSeq;
Craig Topperdcbac182016-02-13 06:03:32 +00001046 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
Andrew Trick2062b122012-10-03 23:06:28 +00001047 ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001048
1049 // For each operand, create a latency entry.
1050 MCWriteLatencyEntry WLEntry;
1051 WLEntry.Cycles = 0;
Andrew Trick3b8fb642012-09-19 04:43:19 +00001052 unsigned WriteID = WriteSeq.back();
1053 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1054 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1055 // from other WriteLatency entries.
Andrew Trick1ab961f2013-03-16 18:58:55 +00001056 if (!SchedModels.hasReadOfWrite(
1057 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trick3b8fb642012-09-19 04:43:19 +00001058 WriteID = 0;
1059 }
1060 WLEntry.WriteResourceID = WriteID;
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001061
Craig Topperdcbac182016-02-13 06:03:32 +00001062 for (unsigned WS : WriteSeq) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001063
Andrew Trick92649882012-09-22 02:24:21 +00001064 Record *WriteRes =
Craig Topperdcbac182016-02-13 06:03:32 +00001065 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001066
1067 // Mark the parent class as invalid for unsupported write types.
1068 if (WriteRes->getValueAsBit("Unsupported")) {
1069 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1070 break;
1071 }
1072 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1073 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1074 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1075 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
Javed Absar47652292017-03-27 20:46:37 +00001076 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1077 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001078
1079 // Create an entry for each ProcResource listed in WriteRes.
1080 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1081 std::vector<int64_t> Cycles =
1082 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick1754aca2013-03-14 21:21:50 +00001083
Clement Courbetfa811242018-06-13 09:41:49 +00001084 if (Cycles.empty()) {
1085 // If ResourceCycles is not provided, default to one cycle per
1086 // resource.
1087 Cycles.resize(PRVec.size(), 1);
1088 } else if (Cycles.size() != PRVec.size()) {
1089 // If ResourceCycles is provided, check consistency.
1090 PrintFatalError(
1091 WriteRes->getLoc(),
1092 Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1093 "!size(ProcResources): ")
1094 .concat(Twine(PRVec.size()))
1095 .concat(" vs ")
1096 .concat(Twine(Cycles.size())));
1097 }
1098
Andrew Trick1754aca2013-03-14 21:21:50 +00001099 ExpandProcResources(PRVec, Cycles, ProcModel);
1100
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001101 for (unsigned PRIdx = 0, PREnd = PRVec.size();
1102 PRIdx != PREnd; ++PRIdx) {
1103 MCWriteProcResEntry WPREntry;
1104 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1105 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick1754aca2013-03-14 21:21:50 +00001106 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trickc8121102013-03-01 23:31:26 +00001107 // If this resource is already used in this sequence, add the current
1108 // entry's cycles so that the same resource appears to be used
1109 // serially, rather than multiple parallel uses. This is important for
1110 // in-order machine where the resource consumption is a hazard.
1111 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1112 for( ; WPRIdx != WPREnd; ++WPRIdx) {
1113 if (WriteProcResources[WPRIdx].ProcResourceIdx
1114 == WPREntry.ProcResourceIdx) {
1115 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1116 break;
1117 }
1118 }
1119 if (WPRIdx == WPREnd)
1120 WriteProcResources.push_back(WPREntry);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001121 }
1122 }
1123 WriteLatencies.push_back(WLEntry);
1124 }
1125 // Create an entry for each operand Read in this SchedClass.
1126 // Entries must be sorted first by UseIdx then by WriteResourceID.
1127 for (unsigned UseIdx = 0, EndIdx = Reads.size();
1128 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick92649882012-09-22 02:24:21 +00001129 Record *ReadAdvance =
1130 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001131 if (!ReadAdvance)
1132 continue;
1133
1134 // Mark the parent class as invalid for unsupported write types.
1135 if (ReadAdvance->getValueAsBit("Unsupported")) {
1136 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1137 break;
1138 }
1139 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1140 IdxVec WriteIDs;
1141 if (ValidWrites.empty())
1142 WriteIDs.push_back(0);
1143 else {
Craig Topperdcbac182016-02-13 06:03:32 +00001144 for (Record *VW : ValidWrites) {
1145 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001146 }
1147 }
Fangrui Song3b35e172018-09-27 02:13:45 +00001148 llvm::sort(WriteIDs);
Craig Topperdcbac182016-02-13 06:03:32 +00001149 for(unsigned W : WriteIDs) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001150 MCReadAdvanceEntry RAEntry;
1151 RAEntry.UseIdx = UseIdx;
Craig Topperdcbac182016-02-13 06:03:32 +00001152 RAEntry.WriteResourceID = W;
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001153 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1154 ReadAdvanceEntries.push_back(RAEntry);
1155 }
1156 }
1157 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1158 WriteProcResources.clear();
1159 WriteLatencies.clear();
1160 ReadAdvanceEntries.clear();
1161 }
1162 // Add the information for this SchedClass to the global tables using basic
1163 // compression.
1164 //
1165 // WritePrecRes entries are sorted by ProcResIdx.
Fangrui Song3b35e172018-09-27 02:13:45 +00001166 llvm::sort(WriteProcResources, LessWriteProcResources());
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001167
1168 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1169 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1170 std::search(SchedTables.WriteProcResources.begin(),
1171 SchedTables.WriteProcResources.end(),
1172 WriteProcResources.begin(), WriteProcResources.end());
1173 if (WPRPos != SchedTables.WriteProcResources.end())
1174 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1175 else {
1176 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1177 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1178 WriteProcResources.end());
1179 }
1180 // Latency entries must remain in operand order.
1181 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1182 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1183 std::search(SchedTables.WriteLatencies.begin(),
1184 SchedTables.WriteLatencies.end(),
1185 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trick3b8fb642012-09-19 04:43:19 +00001186 if (WLPos != SchedTables.WriteLatencies.end()) {
1187 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1188 SCDesc.WriteLatencyIdx = idx;
1189 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1190 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1191 std::string::npos) {
1192 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1193 }
1194 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001195 else {
1196 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trick3b8fb642012-09-19 04:43:19 +00001197 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1198 WriteLatencies.begin(),
1199 WriteLatencies.end());
1200 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1201 WriterNames.begin(), WriterNames.end());
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001202 }
1203 // ReadAdvanceEntries must remain in operand order.
1204 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1205 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1206 std::search(SchedTables.ReadAdvanceEntries.begin(),
1207 SchedTables.ReadAdvanceEntries.end(),
1208 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1209 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1210 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1211 else {
1212 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1213 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1214 ReadAdvanceEntries.end());
1215 }
1216 }
1217}
1218
Andrew Trick544c8802012-09-17 22:18:50 +00001219// Emit SchedClass tables for all processors and associated global tables.
1220void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1221 raw_ostream &OS) {
1222 // Emit global WriteProcResTable.
1223 OS << "\n// {ProcResourceIdx, Cycles}\n"
1224 << "extern const llvm::MCWriteProcResEntry "
1225 << Target << "WriteProcResTable[] = {\n"
1226 << " { 0, 0}, // Invalid\n";
1227 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1228 WPRIdx != WPREnd; ++WPRIdx) {
1229 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1230 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1231 << format("%2d", WPREntry.Cycles) << "}";
1232 if (WPRIdx + 1 < WPREnd)
1233 OS << ',';
1234 OS << " // #" << WPRIdx << '\n';
1235 }
1236 OS << "}; // " << Target << "WriteProcResTable\n";
1237
1238 // Emit global WriteLatencyTable.
1239 OS << "\n// {Cycles, WriteResourceID}\n"
1240 << "extern const llvm::MCWriteLatencyEntry "
1241 << Target << "WriteLatencyTable[] = {\n"
1242 << " { 0, 0}, // Invalid\n";
1243 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1244 WLIdx != WLEnd; ++WLIdx) {
1245 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1246 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1247 << format("%2d", WLEntry.WriteResourceID) << "}";
1248 if (WLIdx + 1 < WLEnd)
1249 OS << ',';
Andrew Trick3b8fb642012-09-19 04:43:19 +00001250 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Trick544c8802012-09-17 22:18:50 +00001251 }
1252 OS << "}; // " << Target << "WriteLatencyTable\n";
1253
1254 // Emit global ReadAdvanceTable.
1255 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1256 << "extern const llvm::MCReadAdvanceEntry "
1257 << Target << "ReadAdvanceTable[] = {\n"
1258 << " {0, 0, 0}, // Invalid\n";
1259 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1260 RAIdx != RAEnd; ++RAIdx) {
1261 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1262 OS << " {" << RAEntry.UseIdx << ", "
1263 << format("%2d", RAEntry.WriteResourceID) << ", "
1264 << format("%2d", RAEntry.Cycles) << "}";
1265 if (RAIdx + 1 < RAEnd)
1266 OS << ',';
1267 OS << " // #" << RAIdx << '\n';
1268 }
1269 OS << "}; // " << Target << "ReadAdvanceTable\n";
1270
1271 // Emit a SchedClass table for each processor.
1272 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1273 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1274 if (!PI->hasInstrSchedModel())
1275 continue;
1276
1277 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola322ff882012-11-02 20:57:36 +00001278 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Trick544c8802012-09-17 22:18:50 +00001279
1280 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1281 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1282 OS << "static const llvm::MCSchedClassDesc "
1283 << PI->ModelName << "SchedClasses[] = {\n";
1284
1285 // The first class is always invalid. We no way to distinguish it except by
1286 // name and position.
Andrew Trick1ab961f2013-03-16 18:58:55 +00001287 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Trick544c8802012-09-17 22:18:50 +00001288 && "invalid class not first");
1289 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1290 << MCSchedClassDesc::InvalidNumMicroOps
Eugene Zelenkod307f962016-05-17 17:04:23 +00001291 << ", false, false, 0, 0, 0, 0, 0, 0},\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001292
1293 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1294 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1295 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1296 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1297 if (SchedClass.Name.size() < 18)
1298 OS.indent(18 - SchedClass.Name.size());
1299 OS << MCDesc.NumMicroOps
Eugene Zelenkod307f962016-05-17 17:04:23 +00001300 << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1301 << ", " << ( MCDesc.EndGroup ? "true" : "false" )
Andrew Trick544c8802012-09-17 22:18:50 +00001302 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1303 << ", " << MCDesc.NumWriteProcResEntries
1304 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1305 << ", " << MCDesc.NumWriteLatencyEntries
1306 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
Craig Topperd4897342017-10-24 15:50:53 +00001307 << ", " << MCDesc.NumReadAdvanceEntries
1308 << "}, // #" << SCIdx << '\n';
Andrew Trick544c8802012-09-17 22:18:50 +00001309 }
1310 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1311 }
1312}
1313
Andrew Trick2661b412012-07-07 04:00:00 +00001314void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1315 // For each processor model.
Craig Topperdcbac182016-02-13 06:03:32 +00001316 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001317 // Emit extra processor info if available.
1318 if (PM.hasExtraProcessorInfo())
1319 EmitExtraProcessorInfo(PM, OS);
Andrew Trick40096d22012-09-17 22:18:45 +00001320 // Emit processor resource table.
Craig Topperdcbac182016-02-13 06:03:32 +00001321 if (PM.hasInstrSchedModel())
1322 EmitProcessorResources(PM, OS);
1323 else if(!PM.ProcResourceDefs.empty())
1324 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001325 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick40096d22012-09-17 22:18:45 +00001326
Andrew Trickfc992992012-06-05 03:44:40 +00001327 // Begin processor itinerary properties
1328 OS << "\n";
Craig Topperdcbac182016-02-13 06:03:32 +00001329 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1330 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1331 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1332 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1333 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1334 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1335 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
Andrew Trick07015642013-09-25 18:14:12 +00001336
Eugene Zelenkod307f962016-05-17 17:04:23 +00001337 bool PostRAScheduler =
1338 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
Sanjay Patelf7e04232014-07-15 22:39:58 +00001339
Eugene Zelenkod307f962016-05-17 17:04:23 +00001340 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1341 << "PostRAScheduler\n";
1342
1343 bool CompleteModel =
1344 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1345
1346 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1347 << "CompleteModel\n";
Andrew Trick07015642013-09-25 18:14:12 +00001348
Craig Topperdcbac182016-02-13 06:03:32 +00001349 OS << " " << PM.Index << ", // Processor ID\n";
1350 if (PM.hasInstrSchedModel())
1351 OS << " " << PM.ModelName << "ProcResources" << ",\n"
1352 << " " << PM.ModelName << "SchedClasses" << ",\n"
1353 << " " << PM.ProcResourceDefs.size()+1 << ",\n"
Andrew Tricke127dfd2012-09-18 03:18:56 +00001354 << " " << (SchedModels.schedClassEnd()
1355 - SchedModels.schedClassBegin()) << ",\n";
1356 else
Hans Wennborg4d651e42015-10-06 23:24:35 +00001357 OS << " nullptr, nullptr, 0, 0,"
1358 << " // No instruction-level machine model.\n";
Craig Topperdcbac182016-02-13 06:03:32 +00001359 if (PM.hasItineraries())
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001360 OS << " " << PM.ItinsDef->getName() << ",\n";
Andrew Trickd85934b2012-06-22 03:58:51 +00001361 else
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001362 OS << " nullptr, // No Itinerary\n";
1363 if (PM.hasExtraProcessorInfo())
Clement Courbetc486bdf2018-04-10 08:16:37 +00001364 OS << " &" << PM.ModelName << "ExtraInfo,\n";
Andrea Di Biagioce79db62018-04-03 13:36:24 +00001365 else
Clement Courbetc486bdf2018-04-10 08:16:37 +00001366 OS << " nullptr // No extra processor descriptor\n";
Craig Topper1e073392017-10-24 15:50:55 +00001367 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001368 }
Jim Laskey10b1dd92005-10-31 17:16:01 +00001369}
1370
1371//
Clement Courbetf4fb61b2018-10-25 07:44:01 +00001372// EmitProcessorLookup - generate cpu name to sched model lookup tables.
Jim Laskey10b1dd92005-10-31 17:16:01 +00001373//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001374void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +00001375 // Gather and sort processor information
1376 std::vector<Record*> ProcessorList =
1377 Records.getAllDerivedDefinitions("Processor");
Fangrui Song3b35e172018-09-27 02:13:45 +00001378 llvm::sort(ProcessorList, LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +00001379
Clement Courbetf4fb61b2018-10-25 07:44:01 +00001380 // Begin processor->sched model table
Jim Laskey10b1dd92005-10-31 17:16:01 +00001381 OS << "\n";
Clement Courbetf4fb61b2018-10-25 07:44:01 +00001382 OS << "// Sorted (by key) array of sched model for CPU subtype.\n"
1383 << "extern const llvm::SubtargetInfoKV " << Target
1384 << "ProcSchedKV[] = {\n";
Jim Laskey10b1dd92005-10-31 17:16:01 +00001385 // For each processor
Craig Topperd4897342017-10-24 15:50:53 +00001386 for (Record *Processor : ProcessorList) {
Craig Topper2a129872017-05-31 21:12:46 +00001387 StringRef Name = Processor->getValueAsString("Name");
Andrew Trick2661b412012-07-07 04:00:00 +00001388 const std::string &ProcModelName =
Andrew Trick48605c32012-09-15 00:19:57 +00001389 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickda96cf22011-04-01 01:56:55 +00001390
Jim Laskey10b1dd92005-10-31 17:16:01 +00001391 // Emit as { "cpu", procinit },
Craig Topperd4897342017-10-24 15:50:53 +00001392 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " },\n";
Jim Laskey10b1dd92005-10-31 17:16:01 +00001393 }
Clement Courbetf4fb61b2018-10-25 07:44:01 +00001394 // End processor->sched model table
Jim Laskey10b1dd92005-10-31 17:16:01 +00001395 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001396}
1397
1398//
Andrew Trick2661b412012-07-07 04:00:00 +00001399// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey0d841e02005-10-27 19:47:21 +00001400//
Andrew Trick2661b412012-07-07 04:00:00 +00001401void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick40096d22012-09-17 22:18:45 +00001402 OS << "#ifdef DBGFIELD\n"
1403 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1404 << "#endif\n"
Aaron Ballman1d03d382017-10-15 14:32:27 +00001405 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
Andrew Trick40096d22012-09-17 22:18:45 +00001406 << "#define DBGFIELD(x) x,\n"
1407 << "#else\n"
1408 << "#define DBGFIELD(x)\n"
1409 << "#endif\n";
1410
Andrew Trick1ab961f2013-03-16 18:58:55 +00001411 if (SchedModels.hasItineraries()) {
Eugene Zelenko43dec7d2016-12-09 22:06:55 +00001412 std::vector<std::vector<InstrItinerary>> ProcItinLists;
Jim Laskey6cee6302005-11-01 20:06:59 +00001413 // Emit the stage data
Andrew Trick2661b412012-07-07 04:00:00 +00001414 EmitStageAndOperandCycleData(OS, ProcItinLists);
1415 EmitItineraries(OS, ProcItinLists);
Jim Laskey6cee6302005-11-01 20:06:59 +00001416 }
Andrew Trick544c8802012-09-17 22:18:50 +00001417 OS << "\n// ===============================================================\n"
1418 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick40096d22012-09-17 22:18:45 +00001419
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001420 SchedClassTables SchedTables;
Craig Topperdcbac182016-02-13 06:03:32 +00001421 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1422 GenSchedClassTables(ProcModel, SchedTables);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001423 }
Andrew Trick544c8802012-09-17 22:18:50 +00001424 EmitSchedClassTables(SchedTables, OS);
1425
1426 // Emit the processor machine model
1427 EmitProcessorModels(OS);
1428 // Emit the processor lookup data
1429 EmitProcessorLookup(OS);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001430
Craig Topper1e073392017-10-24 15:50:55 +00001431 OS << "\n#undef DBGFIELD";
Jim Laskey0d841e02005-10-27 19:47:21 +00001432}
1433
Andrea Di Biagio421a0c12018-04-26 18:03:24 +00001434static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1435 std::string Buffer;
1436 raw_string_ostream Stream(Buffer);
1437
1438 // Collect all the PredicateProlog records and print them to the output
1439 // stream.
1440 std::vector<Record *> Prologs =
1441 Records.getAllDerivedDefinitions("PredicateProlog");
Fangrui Song3b35e172018-09-27 02:13:45 +00001442 llvm::sort(Prologs, LessRecord());
Andrea Di Biagio421a0c12018-04-26 18:03:24 +00001443 for (Record *P : Prologs)
1444 Stream << P->getValueAsString("Code") << '\n';
1445
1446 Stream.flush();
1447 OS << Buffer;
1448}
1449
1450static void emitPredicates(const CodeGenSchedTransition &T,
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001451 const CodeGenSchedClass &SC, PredicateExpander &PE,
Andrea Di Biagio421a0c12018-04-26 18:03:24 +00001452 raw_ostream &OS) {
Andrea Di Biagio421a0c12018-04-26 18:03:24 +00001453 std::string Buffer;
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001454 raw_string_ostream SS(Buffer);
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001455
1456 auto IsTruePredicate = [](const Record *Rec) {
1457 return Rec->isSubClassOf("MCSchedPredicate") &&
1458 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1459 };
1460
1461 // If not all predicates are MCTrue, then we need an if-stmt.
1462 unsigned NumNonTruePreds =
1463 T.PredTerm.size() - count_if(T.PredTerm, IsTruePredicate);
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001464
1465 SS.indent(PE.getIndentLevel() * 2);
1466
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001467 if (NumNonTruePreds) {
1468 bool FirstNonTruePredicate = true;
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001469 SS << "if (";
1470
1471 PE.setIndentLevel(PE.getIndentLevel() + 2);
1472
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001473 for (const Record *Rec : T.PredTerm) {
1474 // Skip predicates that evaluate to "true".
1475 if (IsTruePredicate(Rec))
1476 continue;
1477
1478 if (FirstNonTruePredicate) {
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001479 FirstNonTruePredicate = false;
1480 } else {
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001481 SS << "\n";
1482 SS.indent(PE.getIndentLevel() * 2);
1483 SS << "&& ";
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001484 }
1485
1486 if (Rec->isSubClassOf("MCSchedPredicate")) {
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001487 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001488 continue;
1489 }
1490
1491 // Expand this legacy predicate and wrap it around braces if there is more
1492 // than one predicate to expand.
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001493 SS << ((NumNonTruePreds > 1) ? "(" : "")
1494 << Rec->getValueAsString("Predicate")
1495 << ((NumNonTruePreds > 1) ? ")" : "");
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +00001496 }
Andrea Di Biagio61e1c202018-08-13 11:09:04 +00001497
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001498 SS << ")\n"; // end of if-stmt
1499 PE.decreaseIndentLevel();
1500 SS.indent(PE.getIndentLevel() * 2);
1501 PE.decreaseIndentLevel();
Andrea Di Biagio421a0c12018-04-26 18:03:24 +00001502 }
1503
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001504 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1505 SS.flush();
Andrea Di Biagio421a0c12018-04-26 18:03:24 +00001506 OS << Buffer;
1507}
1508
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001509// Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1510// epilogue code for the auto-generated helper.
1511void emitSchedModelHelperEpilogue(raw_ostream &OS, bool ShouldReturnZero) {
1512 if (ShouldReturnZero) {
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +00001513 OS << " // Don't know how to resolve this scheduling class.\n"
1514 << " return 0;\n";
1515 return;
1516 }
1517
1518 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1519}
1520
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001521bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1522 return all_of(T.PredTerm, [](const Record *Rec) {
1523 return Rec->isSubClassOf("MCSchedPredicate");
1524 });
1525}
1526
1527void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1528 IdxVec &VariantClasses,
1529 bool OnlyExpandMCInstPredicates) {
1530 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1531 // Ignore non-variant scheduling classes.
1532 if (SC.Transitions.empty())
1533 continue;
1534
1535 if (OnlyExpandMCInstPredicates) {
Evandro Menezes7280a872018-11-23 21:17:33 +00001536 // Ignore this variant scheduling class no transitions use any meaningful
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001537 // MCSchedPredicate definitions.
Evandro Menezes7280a872018-11-23 21:17:33 +00001538 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001539 return hasMCSchedPredicates(T);
1540 }))
1541 continue;
1542 }
1543
1544 VariantClasses.push_back(SC.Index);
1545 }
1546}
1547
1548void collectProcessorIndices(const CodeGenSchedClass &SC, IdxVec &ProcIndices) {
1549 // A variant scheduling class may define transitions for multiple
1550 // processors. This function identifies wich processors are associated with
1551 // transition rules specified by variant class `SC`.
1552 for (const CodeGenSchedTransition &T : SC.Transitions) {
1553 IdxVec PI;
1554 std::set_union(T.ProcIndices.begin(), T.ProcIndices.end(),
1555 ProcIndices.begin(), ProcIndices.end(),
1556 std::back_inserter(PI));
1557 ProcIndices.swap(PI);
1558 }
1559}
1560
1561void SubtargetEmitter::emitSchedModelHelpersImpl(
1562 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1563 IdxVec VariantClasses;
1564 collectVariantClasses(SchedModels, VariantClasses,
1565 OnlyExpandMCInstPredicates);
1566
1567 if (VariantClasses.empty()) {
1568 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1569 return;
1570 }
1571
1572 // Construct a switch statement where the condition is a check on the
1573 // scheduling class identifier. There is a `case` for every variant class
1574 // defined by the processor models of this target.
1575 // Each `case` implements a number of rules to resolve (i.e. to transition from)
1576 // a variant scheduling class to another scheduling class. Rules are
1577 // described by instances of CodeGenSchedTransition. Note that transitions may
1578 // not be valid for all processors.
1579 OS << " switch (SchedClass) {\n";
1580 for (unsigned VC : VariantClasses) {
1581 IdxVec ProcIndices;
1582 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1583 collectProcessorIndices(SC, ProcIndices);
1584
1585 OS << " case " << VC << ": // " << SC.Name << '\n';
1586
Andrea Di Biagioe9759dd2018-08-14 18:36:54 +00001587 PredicateExpander PE(Target);
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001588 PE.setByRef(false);
1589 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1590 for (unsigned PI : ProcIndices) {
1591 OS << " ";
Evandro Menezes7280a872018-11-23 21:17:33 +00001592
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001593 // Emit a guard on the processor ID.
1594 if (PI != 0) {
1595 OS << (OnlyExpandMCInstPredicates
1596 ? "if (CPUID == "
1597 : "if (SchedModel->getProcessorID() == ");
1598 OS << PI << ") ";
1599 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1600 }
1601
1602 // Now emit transitions associated with processor PI.
1603 for (const CodeGenSchedTransition &T : SC.Transitions) {
1604 if (PI != 0 && !count(T.ProcIndices, PI))
1605 continue;
Evandro Menezes7280a872018-11-23 21:17:33 +00001606
1607 // Emit only transitions based on MCSchedPredicate, if it's the case.
1608 // At least the transition specified by NoSchedPred is emitted,
1609 // which becomes the default transition for those variants otherwise
1610 // not based on MCSchedPredicate.
1611 // FIXME: preferably, llvm-mca should instead assume a reasonable
1612 // default when a variant transition is not based on MCSchedPredicate
1613 // for a given processor.
1614 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1615 continue;
1616
Andrea Di Biagio87972c42018-08-13 15:13:35 +00001617 PE.setIndentLevel(3);
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001618 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1619 }
1620
1621 OS << " }\n";
Evandro Menezes7280a872018-11-23 21:17:33 +00001622
Andrea Di Biagio6ec2af82018-08-10 10:43:43 +00001623 if (PI == 0)
1624 break;
1625 }
1626
1627 if (SC.isInferred())
1628 OS << " return " << SC.Index << ";\n";
1629 OS << " break;\n";
1630 }
1631
1632 OS << " };\n";
1633
1634 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1635}
1636
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +00001637void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1638 raw_ostream &OS) {
1639 OS << "unsigned " << ClassName
1640 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1641 << " const TargetSchedModel *SchedModel) const {\n";
1642
1643 // Emit the predicate prolog code.
1644 emitPredicateProlog(Records, OS);
1645
1646 // Emit target predicates.
1647 emitSchedModelHelpersImpl(OS);
Clement Courbetf4fb61b2018-10-25 07:44:01 +00001648
Andrea Di Biagioae5fb652018-05-31 13:30:42 +00001649 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
Andrea Di Biagioe68d92b2018-05-25 15:55:37 +00001650
Andrea Di Biagioae5fb652018-05-31 13:30:42 +00001651 OS << "unsigned " << ClassName
1652 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1653 << " unsigned CPUID) const {\n"
1654 << " return " << Target << "_MC"
1655 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID);\n"
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +00001656 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1657
1658 STIPredicateExpander PE(Target);
1659 PE.setClassPrefix(ClassName);
1660 PE.setExpandDefinition(true);
1661 PE.setByRef(false);
1662 PE.setIndentLevel(0);
1663
1664 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1665 PE.expandSTIPredicate(OS, Fn);
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001666}
1667
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +00001668void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1669 raw_ostream &OS) {
1670 const CodeGenHwModes &CGH = TGT.getHwModes();
1671 assert(CGH.getNumModeIds() > 0);
1672 if (CGH.getNumModeIds() == 1)
1673 return;
1674
1675 OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1676 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1677 const HwMode &HM = CGH.getMode(M);
1678 OS << " if (checkFeatures(\"" << HM.Features
1679 << "\")) return " << M << ";\n";
1680 }
1681 OS << " return 0;\n}\n";
1682}
1683
Jim Laskey0d841e02005-10-27 19:47:21 +00001684//
Jim Laskey581a8f72005-10-26 17:30:34 +00001685// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1686// the subtarget features string.
1687//
Evan Cheng94214702011-07-01 20:45:01 +00001688void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1689 unsigned NumFeatures,
1690 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001691 std::vector<Record*> Features =
1692 Records.getAllDerivedDefinitions("SubtargetFeature");
Fangrui Song3b35e172018-09-27 02:13:45 +00001693 llvm::sort(Features, LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +00001694
Andrew Trickda96cf22011-04-01 01:56:55 +00001695 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1696 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +00001697 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +00001698 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001699 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
Nicola Zaghen0818e782018-05-14 12:53:11 +00001700 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1701 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +00001702
1703 if (Features.empty()) {
1704 OS << "}\n";
1705 return;
1706 }
1707
Andrew Trick34aadd62012-09-18 05:33:15 +00001708 OS << " InitMCProcessorInfo(CPU, FS);\n"
Michael Kupersteind714fcf2015-05-26 10:47:10 +00001709 << " const FeatureBitset& Bits = getFeatureBits();\n";
Bill Wendling4222d802007-05-04 20:38:40 +00001710
Craig Topperdcbac182016-02-13 06:03:32 +00001711 for (Record *R : Features) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001712 // Next record
Craig Topper2a129872017-05-31 21:12:46 +00001713 StringRef Instance = R->getName();
1714 StringRef Value = R->getValueAsString("Value");
1715 StringRef Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +00001716
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001717 if (Value=="true" || Value=="false")
Michael Kupersteind714fcf2015-05-26 10:47:10 +00001718 OS << " if (Bits[" << Target << "::"
1719 << Instance << "]) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001720 << Attribute << " = " << Value << ";\n";
1721 else
Michael Kupersteind714fcf2015-05-26 10:47:10 +00001722 OS << " if (Bits[" << Target << "::"
1723 << Instance << "] && "
Evan Cheng94214702011-07-01 20:45:01 +00001724 << Attribute << " < " << Value << ") "
1725 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +00001726 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001727
Evan Cheng276365d2011-06-30 01:53:36 +00001728 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +00001729}
1730
Andrea Di Biagio0d3a4af2018-05-25 16:02:43 +00001731void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
Andrea Di Biagioae5fb652018-05-31 13:30:42 +00001732 OS << "namespace " << Target << "_MC {\n"
1733 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1734 << " const MCInst *MI, unsigned CPUID) {\n";
1735 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1736 OS << "}\n";
1737 OS << "} // end of namespace " << Target << "_MC\n\n";
1738
Andrea Di Biagio0d3a4af2018-05-25 16:02:43 +00001739 OS << "struct " << Target
1740 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1741 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT, \n"
1742 << " StringRef CPU, StringRef FS, ArrayRef<SubtargetFeatureKV> PF,\n"
1743 << " ArrayRef<SubtargetFeatureKV> PD,\n"
1744 << " const SubtargetInfoKV *ProcSched,\n"
1745 << " const MCWriteProcResEntry *WPR,\n"
1746 << " const MCWriteLatencyEntry *WL,\n"
1747 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1748 << " const unsigned *OC, const unsigned *FP) :\n"
1749 << " MCSubtargetInfo(TT, CPU, FS, PF, PD, ProcSched,\n"
1750 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1751 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
Andrea Di Biagioae5fb652018-05-31 13:30:42 +00001752 << " const MCInst *MI, unsigned CPUID) const override {\n"
1753 << " return " << Target << "_MC"
1754 << "::resolveVariantSchedClassImpl(SchedClass, MI, CPUID); \n";
Andrea Di Biagio0d3a4af2018-05-25 16:02:43 +00001755 OS << " }\n";
1756 OS << "};\n";
1757}
1758
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +00001759void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1760 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1761 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1762
1763 STIPredicateExpander PE(Target);
1764 PE.setExpandForMC(true);
1765 PE.setByRef(true);
1766 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1767 PE.expandSTIPredicate(OS, Fn);
1768
1769 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1770
1771 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1772 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1773
1774 std::string ClassPrefix = Target + "MCInstrAnalysis";
1775 PE.setExpandDefinition(true);
1776 PE.setClassPrefix(ClassPrefix);
1777 PE.setIndentLevel(0);
1778 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1779 PE.expandSTIPredicate(OS, Fn);
1780
1781 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1782}
1783
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001784//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001785// SubtargetEmitter::run - Main subtarget enumeration emitter.
1786//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001787void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001788 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001789
Evan Chengebdeeab2011-07-08 01:53:10 +00001790 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
Eugene Zelenkod307f962016-05-17 17:04:23 +00001791 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00001792
1793 OS << "namespace llvm {\n";
Craig Topper487820c2016-02-14 05:22:01 +00001794 Enumeration(OS);
Eugene Zelenkod307f962016-05-17 17:04:23 +00001795 OS << "} // end namespace llvm\n\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00001796 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1797
Evan Cheng94214702011-07-01 20:45:01 +00001798 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
Eugene Zelenkod307f962016-05-17 17:04:23 +00001799 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +00001800
Evan Cheng94214702011-07-01 20:45:01 +00001801 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001802#if 0
1803 OS << "namespace {\n";
1804#endif
Evan Cheng94214702011-07-01 20:45:01 +00001805 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001806 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +00001807 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001808 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001809 EmitSchedModel(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001810 OS << "\n";
1811#if 0
Eugene Zelenkod307f962016-05-17 17:04:23 +00001812 OS << "} // end anonymous namespace\n\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001813#endif
Evan Cheng94214702011-07-01 20:45:01 +00001814
1815 // MCInstrInfo initialization routine.
Andrea Di Biagio0d3a4af2018-05-25 16:02:43 +00001816 emitGenMCSubtargetInfo(OS);
1817
Craig Topper1e073392017-10-24 15:50:55 +00001818 OS << "\nstatic inline MCSubtargetInfo *create" << Target
Duncan P. N. Exon Smith16859aa2015-07-10 22:43:42 +00001819 << "MCSubtargetInfoImpl("
Daniel Sanders47b167d2015-09-15 16:17:27 +00001820 << "const Triple &TT, StringRef CPU, StringRef FS) {\n";
Andrea Di Biagio0d3a4af2018-05-25 16:02:43 +00001821 OS << " return new " << Target << "GenMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001822 if (NumFeatures)
1823 OS << Target << "FeatureKV, ";
1824 else
Eric Christopherd4741812014-05-06 20:23:04 +00001825 OS << "None, ";
Evan Cheng94214702011-07-01 20:45:01 +00001826 if (NumProcs)
1827 OS << Target << "SubTypeKV, ";
1828 else
Eric Christopherd4741812014-05-06 20:23:04 +00001829 OS << "None, ";
Andrew Trick544c8802012-09-17 22:18:50 +00001830 OS << '\n'; OS.indent(22);
Andrew Tricke127dfd2012-09-18 03:18:56 +00001831 OS << Target << "ProcSchedKV, "
1832 << Target << "WriteProcResTable, "
1833 << Target << "WriteLatencyTable, "
1834 << Target << "ReadAdvanceTable, ";
Eugene Zelenko43dec7d2016-12-09 22:06:55 +00001835 OS << '\n'; OS.indent(22);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001836 if (SchedModels.hasItineraries()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001837 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001838 << Target << "OperandCycles, "
Eric Christopherd4741812014-05-06 20:23:04 +00001839 << Target << "ForwardingPaths";
Evan Cheng94214702011-07-01 20:45:01 +00001840 } else
Eugene Zelenko43dec7d2016-12-09 22:06:55 +00001841 OS << "nullptr, nullptr, nullptr";
Eric Christopherd4741812014-05-06 20:23:04 +00001842 OS << ");\n}\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001843
Eugene Zelenkod307f962016-05-17 17:04:23 +00001844 OS << "} // end namespace llvm\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001845
1846 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1847
1848 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
Eugene Zelenkod307f962016-05-17 17:04:23 +00001849 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001850
1851 OS << "#include \"llvm/Support/Debug.h\"\n";
Eugene Zelenkod307f962016-05-17 17:04:23 +00001852 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001853 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1854
1855 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1856
Evan Cheng5b1b44892011-07-01 21:01:15 +00001857 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +00001858 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
Eugene Zelenkod307f962016-05-17 17:04:23 +00001859 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001860
1861 std::string ClassName = Target + "GenSubtargetInfo";
1862 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001863 OS << "class DFAPacketizer;\n";
Andrea Di Biagioae5fb652018-05-31 13:30:42 +00001864 OS << "namespace " << Target << "_MC {\n"
1865 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1866 << " const MCInst *MI, unsigned CPUID);\n"
1867 << "}\n\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +00001868 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Daniel Sanders47b167d2015-09-15 16:17:27 +00001869 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001870 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001871 << "public:\n"
Daniel Sanders4d13f312015-06-10 12:11:26 +00001872 << " unsigned resolveSchedClass(unsigned SchedClass, "
1873 << " const MachineInstr *DefMI,"
Craig Topperf2c9fef2014-03-09 07:44:38 +00001874 << " const TargetSchedModel *SchedModel) const override;\n"
Andrea Di Biagioae5fb652018-05-31 13:30:42 +00001875 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
1876 << " const MCInst *MI, unsigned CPUID) const override;\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +00001877 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +00001878 << " const;\n";
1879 if (TGT.getHwModes().getNumModeIds() > 1)
1880 OS << " unsigned getHwMode() const override;\n";
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +00001881
1882 STIPredicateExpander PE(Target);
1883 PE.setByRef(false);
1884 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1885 PE.expandSTIPredicate(OS, Fn);
1886
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +00001887 OS << "};\n"
1888 << "} // end namespace llvm\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001889
1890 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1891
1892 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
Eugene Zelenkod307f962016-05-17 17:04:23 +00001893 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001894
Eugene Zelenkod307f962016-05-17 17:04:23 +00001895 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001896 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001897 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1898 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001899 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1900 OS << "extern const llvm::MCWriteProcResEntry "
1901 << Target << "WriteProcResTable[];\n";
1902 OS << "extern const llvm::MCWriteLatencyEntry "
1903 << Target << "WriteLatencyTable[];\n";
1904 OS << "extern const llvm::MCReadAdvanceEntry "
1905 << Target << "ReadAdvanceTable[];\n";
1906
Andrew Trick1ab961f2013-03-16 18:58:55 +00001907 if (SchedModels.hasItineraries()) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001908 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1909 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Tricka11a6282012-07-07 03:59:48 +00001910 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001911 }
1912
Daniel Sanders47b167d2015-09-15 16:17:27 +00001913 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1914 << "StringRef FS)\n"
Duncan P. N. Exon Smith16859aa2015-07-10 22:43:42 +00001915 << " : TargetSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001916 if (NumFeatures)
Eric Christopherd4741812014-05-06 20:23:04 +00001917 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
Evan Cheng94214702011-07-01 20:45:01 +00001918 else
Eric Christopherd4741812014-05-06 20:23:04 +00001919 OS << "None, ";
Evan Cheng94214702011-07-01 20:45:01 +00001920 if (NumProcs)
Eric Christopherd4741812014-05-06 20:23:04 +00001921 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
Evan Cheng94214702011-07-01 20:45:01 +00001922 else
Eric Christopherd4741812014-05-06 20:23:04 +00001923 OS << "None, ";
Duncan P. N. Exon Smith16859aa2015-07-10 22:43:42 +00001924 OS << '\n'; OS.indent(24);
Andrew Tricke127dfd2012-09-18 03:18:56 +00001925 OS << Target << "ProcSchedKV, "
1926 << Target << "WriteProcResTable, "
1927 << Target << "WriteLatencyTable, "
1928 << Target << "ReadAdvanceTable, ";
Duncan P. N. Exon Smith16859aa2015-07-10 22:43:42 +00001929 OS << '\n'; OS.indent(24);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001930 if (SchedModels.hasItineraries()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001931 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001932 << Target << "OperandCycles, "
Eric Christopherd4741812014-05-06 20:23:04 +00001933 << Target << "ForwardingPaths";
Evan Cheng94214702011-07-01 20:45:01 +00001934 } else
Eugene Zelenko43dec7d2016-12-09 22:06:55 +00001935 OS << "nullptr, nullptr, nullptr";
Duncan P. N. Exon Smith16859aa2015-07-10 22:43:42 +00001936 OS << ") {}\n\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001937
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001938 EmitSchedModelHelpers(ClassName, OS);
Krzysztof Parzyszekc84b8592017-09-14 20:44:20 +00001939 EmitHwModeCheck(ClassName, OS);
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001940
Eugene Zelenkod307f962016-05-17 17:04:23 +00001941 OS << "} // end namespace llvm\n\n";
Evan Cheng94214702011-07-01 20:45:01 +00001942
1943 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Andrea Di Biagioa9c15c12018-09-19 15:57:45 +00001944
1945 EmitMCInstrAnalysisPredicateFunctions(OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001946}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001947
1948namespace llvm {
1949
1950void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick2661b412012-07-07 04:00:00 +00001951 CodeGenTarget CGTarget(RK);
1952 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001953}
1954
Eugene Zelenkod307f962016-05-17 17:04:23 +00001955} // end namespace llvm